Skip to content

Commit b9e2f58

Browse files
surenbaghdasaryanakpm00
authored andcommitted
alloc_tag: mark inaccurate allocation counters in /proc/allocinfo output
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: 39d117e ("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>
1 parent 5e1953d commit b9e2f58

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

Documentation/filesystems/proc.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,19 @@ number, module (if originates from a loadable module) and the function calling
10091009
the allocation. The number of bytes allocated and number of calls at each
10101010
location are reported. The first line indicates the version of the file, the
10111011
second line is the header listing fields in the file.
1012+
If file version is 2.0 or higher then each line may contain additional
1013+
<key>:<value> pairs representing extra information about the call site.
1014+
For example if the counters are not accurate, the line will be appended with
1015+
"accurate:no" pair.
1016+
1017+
Supported markers in v2:
1018+
accurate:no
1019+
1020+
Absolute values of the counters in this line are not accurate
1021+
because of the failure to allocate memory to track some of the
1022+
allocations made at this location. Deltas in these counters are
1023+
accurate, therefore counters can be used to track allocation size
1024+
and count changes.
10121025

10131026
Example output.
10141027

include/linux/alloc_tag.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes)
221221
ref->ct = NULL;
222222
}
223223

224+
static inline void alloc_tag_set_inaccurate(struct alloc_tag *tag)
225+
{
226+
tag->ct.flags |= CODETAG_FLAG_INACCURATE;
227+
}
228+
229+
static inline bool alloc_tag_is_inaccurate(struct alloc_tag *tag)
230+
{
231+
return !!(tag->ct.flags & CODETAG_FLAG_INACCURATE);
232+
}
233+
224234
#define alloc_tag_record(p) ((p) = current->alloc_tag)
225235

226236
#else /* CONFIG_MEM_ALLOC_PROFILING */
@@ -230,6 +240,8 @@ static inline bool mem_alloc_profiling_enabled(void) { return false; }
230240
static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,
231241
size_t bytes) {}
232242
static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) {}
243+
static inline void alloc_tag_set_inaccurate(struct alloc_tag *tag) {}
244+
static inline bool alloc_tag_is_inaccurate(struct alloc_tag *tag) { return false; }
233245
#define alloc_tag_record(p) do {} while (0)
234246

235247
#endif /* CONFIG_MEM_ALLOC_PROFILING */

include/linux/codetag.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ struct module;
1616
#define CODETAG_SECTION_START_PREFIX "__start_"
1717
#define CODETAG_SECTION_STOP_PREFIX "__stop_"
1818

19+
/* codetag flags */
20+
#define CODETAG_FLAG_INACCURATE (1 << 0)
21+
1922
/*
2023
* An instance of this structure is created in a special ELF section at every
2124
* code location being tagged. At runtime, the special section is treated as
2225
* an array of these.
2326
*/
2427
struct codetag {
25-
unsigned int flags; /* used in later patches */
28+
unsigned int flags;
2629
unsigned int lineno;
2730
const char *modname;
2831
const char *function;

lib/alloc_tag.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void allocinfo_stop(struct seq_file *m, void *arg)
8080
static void print_allocinfo_header(struct seq_buf *buf)
8181
{
8282
/* Output format version, so we can change it. */
83-
seq_buf_printf(buf, "allocinfo - version: 1.0\n");
83+
seq_buf_printf(buf, "allocinfo - version: 2.0\n");
8484
seq_buf_printf(buf, "# <size> <calls> <tag info>\n");
8585
}
8686

@@ -92,6 +92,8 @@ static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
9292

9393
seq_buf_printf(out, "%12lli %8llu ", bytes, counter.calls);
9494
codetag_to_text(out, ct);
95+
if (unlikely(alloc_tag_is_inaccurate(tag)))
96+
seq_buf_printf(out, " accurate:no");
9597
seq_buf_putc(out, ' ');
9698
seq_buf_putc(out, '\n');
9799
}

mm/slub.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,8 @@ __alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags)
21432143
*/
21442144
if (likely(obj_exts))
21452145
alloc_tag_add(&obj_exts->ref, current->alloc_tag, s->size);
2146+
else
2147+
alloc_tag_set_inaccurate(current->alloc_tag);
21462148
}
21472149

21482150
static inline void

0 commit comments

Comments
 (0)