]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
alloc_tag: mark inaccurate allocation counters in /proc/allocinfo output
authorSuren Baghdasaryan <surenb@google.com>
Mon, 15 Sep 2025 23:02:24 +0000 (16:02 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 21 Sep 2025 21:22:36 +0000 (14:22 -0700)
While rare, memory allocation profiling can contain inaccurate counters if
slab object extension vector allocation fails.  That allocation might
succeed later but prior to that, slab allocations that would have used
that object extension vector will not be accounted for.  To indicate
incorrect counters, "accurate:no" marker is appended to the call site line
in the /proc/allocinfo output.  Bump up /proc/allocinfo version to reflect
the change in the file format and update documentation.

Example output with invalid counters:
allocinfo - version: 2.0
           0        0 arch/x86/kernel/kdebugfs.c:105 func:create_setup_data_nodes
           0        0 arch/x86/kernel/alternative.c:2090 func:alternatives_smp_module_add
           0        0 arch/x86/kernel/alternative.c:127 func:__its_alloc accurate:no
           0        0 arch/x86/kernel/fpu/regset.c:160 func:xstateregs_set
           0        0 arch/x86/kernel/fpu/xstate.c:1590 func:fpstate_realloc
           0        0 arch/x86/kernel/cpu/aperfmperf.c:379 func:arch_enable_hybrid_capacity_scale
           0        0 arch/x86/kernel/cpu/amd_cache_disable.c:258 func:init_amd_l3_attrs
       49152       48 arch/x86/kernel/cpu/mce/core.c:2709 func:mce_device_create accurate:no
       32768        1 arch/x86/kernel/cpu/mce/genpool.c:132 func:mce_gen_pool_create
           0        0 arch/x86/kernel/cpu/mce/amd.c:1341 func:mce_threshold_create_device

[surenb@google.com: document new "accurate:no" marker]
Fixes: 39d117e04d15 ("alloc_tag: mark inaccurate allocation counters in /proc/allocinfo output")
[akpm@linux-foundation.org: simplification per Usama, reflow text]
[akpm@linux-foundation.org: add newline to prevent docs warning, per Randy]
Link: https://lkml.kernel.org/r/20250915230224.4115531-1-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Usama Arif <usamaarif642@gmail.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: David Rientjes <rientjes@google.com>
Cc: David Wang <00107082@163.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Sourav Panda <souravpanda@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Documentation/filesystems/proc.rst
include/linux/alloc_tag.h
include/linux/codetag.h
lib/alloc_tag.c
mm/slub.c

index 915a3e44bc120b30e74c85ef673b5c005465cf21..6ffd04c736db61090d74ebaeda17c37e00e1d7cb 100644 (file)
@@ -1009,6 +1009,19 @@ number, module (if originates from a loadable module) and the function calling
 the allocation. The number of bytes allocated and number of calls at each
 location are reported. The first line indicates the version of the file, the
 second line is the header listing fields in the file.
+If file version is 2.0 or higher then each line may contain additional
+<key>:<value> pairs representing extra information about the call site.
+For example if the counters are not accurate, the line will be appended with
+"accurate:no" pair.
+
+Supported markers in v2:
+accurate:no
+
+              Absolute values of the counters in this line are not accurate
+              because of the failure to allocate memory to track some of the
+              allocations made at this location.  Deltas in these counters are
+              accurate, therefore counters can be used to track allocation size
+              and count changes.
 
 Example output.
 
index 9ef2633e2c08cbe5ea41de029de9b1533529e5f2..d40ac39bfbe8d91c02cd3220722046ae9be0db01 100644 (file)
@@ -221,6 +221,16 @@ static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes)
        ref->ct = NULL;
 }
 
+static inline void alloc_tag_set_inaccurate(struct alloc_tag *tag)
+{
+       tag->ct.flags |= CODETAG_FLAG_INACCURATE;
+}
+
+static inline bool alloc_tag_is_inaccurate(struct alloc_tag *tag)
+{
+       return !!(tag->ct.flags & CODETAG_FLAG_INACCURATE);
+}
+
 #define alloc_tag_record(p)    ((p) = current->alloc_tag)
 
 #else /* CONFIG_MEM_ALLOC_PROFILING */
@@ -230,6 +240,8 @@ static inline bool mem_alloc_profiling_enabled(void) { return false; }
 static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,
                                 size_t bytes) {}
 static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) {}
+static inline void alloc_tag_set_inaccurate(struct alloc_tag *tag) {}
+static inline bool alloc_tag_is_inaccurate(struct alloc_tag *tag) { return false; }
 #define alloc_tag_record(p)    do {} while (0)
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
index 457ed8fd321443cb3a4bfc15abe8e4e67e9ae362..8ea2a5f7c98a5851f3a304c8fe389e8fc7cf890f 100644 (file)
@@ -16,13 +16,16 @@ struct module;
 #define CODETAG_SECTION_START_PREFIX   "__start_"
 #define CODETAG_SECTION_STOP_PREFIX    "__stop_"
 
+/* codetag flags */
+#define CODETAG_FLAG_INACCURATE        (1 << 0)
+
 /*
  * An instance of this structure is created in a special ELF section at every
  * code location being tagged.  At runtime, the special section is treated as
  * an array of these.
  */
 struct codetag {
-       unsigned int flags; /* used in later patches */
+       unsigned int flags;
        unsigned int lineno;
        const char *modname;
        const char *function;
index f79217427d812a29de36d93829c2f729d119fed7..3ef702e6b69a8a92f9790aea0bceb5da9a6c8320 100644 (file)
@@ -80,7 +80,7 @@ static void allocinfo_stop(struct seq_file *m, void *arg)
 static void print_allocinfo_header(struct seq_buf *buf)
 {
        /* Output format version, so we can change it. */
-       seq_buf_printf(buf, "allocinfo - version: 1.0\n");
+       seq_buf_printf(buf, "allocinfo - version: 2.0\n");
        seq_buf_printf(buf, "#     <size>  <calls> <tag info>\n");
 }
 
@@ -92,6 +92,8 @@ static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
 
        seq_buf_printf(out, "%12lli %8llu ", bytes, counter.calls);
        codetag_to_text(out, ct);
+       if (unlikely(alloc_tag_is_inaccurate(tag)))
+               seq_buf_printf(out, " accurate:no");
        seq_buf_putc(out, ' ');
        seq_buf_putc(out, '\n');
 }
index af343ca570b57f714fbda892f51524fa2f68774a..9c04f29ee8deec6fcf666e7bfb9045aeb45242d0 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2143,6 +2143,8 @@ __alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags)
         */
        if (likely(obj_exts))
                alloc_tag_add(&obj_exts->ref, current->alloc_tag, s->size);
+       else
+               alloc_tag_set_inaccurate(current->alloc_tag);
 }
 
 static inline void