Skip to contentSkip to conversation

alloc_tag: Introduce selective tracing for MAP

PATCH In review Abhishek Bapat 4 participants 10 messages Nov 4 – Nov 6

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

v5 cover alloc_tag: Introduce selective tracing for MAP

The existing memory allocation profiling (MAP) tool accurately maintains aggregate counters for overall memory accounting. However, relying purely on aggregated counters restrics the visibility needed for targeted debugging.

This patch series integrates three new tracepoints (alloc_tag_hit, alloc_tag_mem_alloced and alloc_tag_mem_freed) directly into the alloc_hooks_tag fast path.

Enabling trace events globally for all allocations introduces a severe baseline overhead. To limit the performance overhead, we let the admin select specific allocations to be traced.

A new IOCTL is introduced to allow privileged users to determine which allocation tags need tracing enabled. The IOCTL utilizes the pre-existing filtering mechanism to specify the allocation tags to enable tracing on.

v5 changes:
- Patch 3/5: Introduced a new patch to introduce a flags field wrapped in an anonymous union with the old inaccurate field. This allows us to extend the UAPI with more filters without breaking backwards compatibility.
- Patch 4/5: Minor update to filter out trace_on tags using flags.
- Patch 5/5: Updated the test to use the new flags UAPI.

v4 changes:
- Patch 1/4: Minor refactor, changed macro definition of alloc_tag_trace_hit() for the NOOP case.

v3 changes:
- Patch 2/4: Minor refactor, documented the CAP_SYS_ADMIN requirement. Moved the mutex lock / unlock outside the for loop.

v2 changes:
- Patch 1/4: Changed the trace events for the page allocator to track pointer to the head page instead of codetag_ref. alloc_tag_trace_enabled() no longer takes a tag arg, that arg gets added in patch 2 instead where the function makes use of it. Introduced new sections in the Documentation expanding on the newly introduced trace events. Minor typo fixes.
- Patch 2/4: Toggle traces IOCTL now uses the same filter as other related ioctls. IOCTL returns -ENOENT if no tags match and an empty filter matches all tags. Added a subsection in the Documentation expanding on the new IOCTL.
- Patch 4/4: Updated the test to work with the new UAPI from patch 2. Updated commit message to accurately mention that the test flips the state of trace_on, verifies it and restores to the original state.

Abhishek Bapat (5):
alloc_tag: Add trace events for tracing allocations
alloc_tag: Introduce IOCTLs to toggle allocation tracepoints
alloc_tag: Add a flags field to allocinfo_filter
alloc_tag: Extend allocinfo_filter to support tracing queries
alloc_tag: Add a test for trace state toggle and filtering

 Documentation/mm/allocation-profiling.rst     |  73 ++++++++
 MAINTAINERS                                   |   1 +
 include/linux/alloc_tag.h                     | 106 +++++++++---
 include/linux/codetag.h                       |   5 +-
 include/trace/events/alloc_tag.h              | 122 +++++++++++++
 include/uapi/linux/alloc_tag.h                |  37 +++-
 mm/alloc_tag.c                                | 161 +++++++++++++++++-
 mm/page_alloc.c                               |   4 +-
 mm/percpu.c                                   |  13 +-
 mm/slub.c                                     |   6 +-
 .../alloc_tag/allocinfo_ioctl_test.c          | 114 ++++++++++++-
 11 files changed, 589 insertions(+), 53 deletions(-)
 create mode 100644 include/trace/events/alloc_tag.h

base-commit: bcc036cfa1670a64500f6d857eaa71908b79e3fb

Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#1 lore raw

PATCH v5 1/5 alloc_tag: Add trace events for tracing allocations

The memory allocation profiling framework intercepts allocations across the core subsystems, but currently lacks runtime tracing hooks for standard observability tools to dynamically track the context (stack traces and lifecycles of the individual memory chunks) of the allocations made.

Introduce three standard trace events to allow this tracking:

1. alloc_tag_hit: Fired at the exact call site. This allows userspace tools to trigger and capture a call stack.
2. alloc_tag_mem_alloced: Fired in alloc_tag_add upon successful allocation. It records the allocated size, the tag, and the uniquely generated ptr value which is codetag_ref for slab and percpu allocators and the pointer to the head struct page for the page allocator.
3. alloc_tag_mem_freed: Fired in alloc_tag_sub right before memory is freed, yielding the same ptr value to allow tracing tools to find the corresponding allocation.

Because the introduced trace events occur at different stages in the call stack, userspace tracing tools must stitch them together to form a complete picture of a buffer's lifetime. Here's an example of how userspace correlates these three events:

1. On alloc_tag_hit: The tool captures the stack trace and caches it, keyed by the combination of the current thread's PID and the tag.
2. On alloc_tag_mem_alloced: The tool extracts the PID and tag from the event and looks up the stack trace cached in step 1. It creates a new active allocation record, mapping the new provided ptr value to this cached stack trace and the newly returned allocation size.
3. On alloc_tag_mem_freed: When the memory is freed, the event yields the same ptr value. The tool uses this reference to look up the original allocation record, correlates the free, and safely retires the tracking entry.

Modify alloc_tag_add() and alloc_tag_sub() with a ptr argument to map frees to allocations in the trace events. Slab and percpu pass the address of the object's codetag_ref. The page allocator passes the head struct page because its codetag_ref is a temporary copy on the stack.

Also, introduce alloc_tag_trace_key static key to minimize the overhead when no tags are being traced (the usual case). Once tracing for any tag is requested, the key is set, opening the path to check whether tracing is enabled for the current tag.
Note that the mechanism to enable tag tracing is implemented in the next patch, therefore for now, alloc_tag_trace_key stays always unset.

---
 Documentation/mm/allocation-profiling.rst |  53 ++++++++++
 MAINTAINERS                               |   1 +
 include/linux/alloc_tag.h                 |  69 +++++++++---
 include/trace/events/alloc_tag.h          | 122 ++++++++++++++++++++++
 mm/alloc_tag.c                            |  28 +++++
 mm/page_alloc.c                           |   4 +-
 mm/percpu.c                               |  13 ++-
 mm/slub.c                                 |   6 +-
 8 files changed, 270 insertions(+), 26 deletions(-)
 create mode 100644 include/trace/events/alloc_tag.h
Documentation/mm/allocation-profiling.rst +53 -0
index b11ea77c0673..b241450394df 100644
@@ -129,6 +129,59 @@ To do so:
 - Then, use the following form for your allocations:
   alloc_hooks_tag(ht->your_saved_tag, kmalloc_noprof(...))
 
+Tracing
+=======
+
+Three trace events are available under `/sys/kernel/tracing/events/alloc_tag` to
+expose the full call stack and the lifetime of individual allocations:
+
+- `alloc_tag_hit`: Fired at the exact call site, before the allocation happens.
+  Can be used to capture the call stack of the caller.
+
+- `alloc_tag_mem_alloced`: Fired once the allocation succeeds. Carries the `ptr`,
+  `tag` and `bytes` for each allocation.
+
+- `alloc_tag_mem_freed`: Fired before memory is freed. Carries the same
+  `ptr`, `tag` and `bytes` as the matching alloc event.
+
+`ptr` identifies the allocation, and its meaning depends on the allocator:
+
+- slab and percpu: the address of the object's `codetag_ref`
+- page allocator: the `struct page` pointer of the head page
+
+Correlating the events
+----------------------
+
+`alloc_tag_hit` and `alloc_tag_mem_alloced` come from different points in the
+call stack, so they have to be stitched together by the user consuming the
+events. Here's a typical flow:
+
+1. On `alloc_tag_hit`: Capture the stack trace and cache it keyed by `(pid, tag)`.
+
+2. On `alloc_tag_mem_alloced`: Look up the cached stack trace by `(pid, tag)`,
+   then create an active allocation record keyed by `ptr` that holds the stack
+   trace and `bytes`.
+
+3. On `alloc_tag_mem_freed`: Look up the record by `ptr` and retire it.
+
+Limitations
+-----------
+
+- For the page allocator, events are generated for the original allocation and
+  the free only. If the tag reference is split or moved to another folio in
+  between, no event is generated for that. As a result:
+
+  - a free event may carry a `ptr` that never appeared in an alloc event;
+  - `bytes` in a free event may be smaller than in the matching alloc event.
+
+  Tools should correlate on `ptr` but must not assume that freed bytes equal
+  allocated bytes, or that every free has a matching alloc.
+
+- Freeing a non-compound high-order page with `__free_pages()` while another CPU
+  holds a reference frees the tail pages immediately and the head page later when
+  the reference is dropped. The free event is emitted at the second point and
+  reports `PAGE_SIZE` rather than the full size.
+
 Notes
 =====
 
MAINTAINERS +1 -0
index d63d8b215277..d5969fbf9224 100644
@@ -17479,6 +17479,7 @@ S:	Maintained
 F:	Documentation/mm/allocation-profiling.rst
 F:	include/linux/alloc_tag.h
 F:	include/linux/pgalloc_tag.h
+F:	include/trace/events/alloc_tag.h
 F:	include/uapi/linux/alloc_tag.h
 F:	mm/alloc_tag.c
 F:	tools/testing/selftests/alloc_tag/
include/linux/alloc_tag.h +52 -17

108 lines not on this page · raw message

include/trace/events/alloc_tag.h new file +122 -0

122 lines not on this page · raw message

mm/alloc_tag.c +28 -0
index 82e2c3448dcf..ca2412a67312 100644
@@ -18,6 +18,9 @@
 #include <linux/kmemleak.h>
 #include <uapi/linux/alloc_tag.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/alloc_tag.h>
+
 #include "internal.h"
 #include "page_alloc.h"
 
@@ -54,6 +57,9 @@ EXPORT_SYMBOL(mem_alloc_profiling_key);
 
 DEFINE_STATIC_KEY_FALSE(mem_profiling_compressed);
 
+DEFINE_STATIC_KEY_FALSE(alloc_tag_trace_key);
+EXPORT_SYMBOL(alloc_tag_trace_key);
+
 struct alloc_tag_kernel_section kernel_tags = { NULL, 0 };
 unsigned long alloc_tag_ref_mask;
 int alloc_tag_ref_offs;
@@ -484,6 +490,28 @@ static const struct proc_ops allocinfo_proc_ops = {
 #endif
 };
 
+void __alloc_tag_trace_hit(struct alloc_tag *tag)
+{
+	if (unlikely(!tag))
+		return;
+	trace_alloc_tag_hit(tag);
+}
+EXPORT_SYMBOL(__alloc_tag_trace_hit);
+
+void alloc_tag_trace_mem_alloc(const void *ptr, struct alloc_tag *tag,
+			       size_t bytes)
+{
+	trace_alloc_tag_mem_alloced(ptr, tag, bytes);
+}
+EXPORT_SYMBOL(alloc_tag_trace_mem_alloc);
+
+void alloc_tag_trace_mem_free(const void *ptr, struct alloc_tag *tag,
+			      size_t bytes)
+{
+	trace_alloc_tag_mem_freed(ptr, tag, bytes);
+}
+EXPORT_SYMBOL(alloc_tag_trace_mem_free);
+
 size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep)
 {
 	struct codetag_iterator iter;
mm/page_alloc.c +2 -2
index 15ff5c1feef4..d6b1f4cf7b85 100644
@@ -1255,7 +1255,7 @@ void __pgalloc_tag_add(struct page *page, struct task_struct *task,
 	union codetag_ref ref;
 
 	if (likely(get_page_tag_ref(page, &ref, &handle))) {
-		alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr);
+		alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr, page);
 		update_page_tag_ref(handle, &ref);
 		put_page_tag_ref(handle);
 	} else {
@@ -1284,7 +1284,7 @@ void __pgalloc_tag_sub(struct page *page, unsigned int nr)
 	union codetag_ref ref;
 
 	if (get_page_tag_ref(page, &ref, &handle)) {
-		alloc_tag_sub(&ref, PAGE_SIZE * nr);
+		alloc_tag_sub(&ref, PAGE_SIZE * nr, page);
 		update_page_tag_ref(handle, &ref);
 		put_page_tag_ref(handle);
 	}
mm/percpu.c +8 -5
index b22a16980f64..9308d9b32a27 100644
@@ -1696,17 +1696,20 @@ static void pcpu_alloc_tag_alloc_hook(struct pcpu_chunk *chunk, int off,
 				      size_t full_size)
 {
 	if (mem_alloc_profiling_enabled() && likely(chunk->obj_exts)) {
-		alloc_tag_add(&chunk->obj_exts[off >> PCPU_MIN_ALLOC_SHIFT].tag,
-			      current->alloc_tag, full_size);
+		union codetag_ref *ref = &chunk->obj_exts[off >> PCPU_MIN_ALLOC_SHIFT].tag;
+
+		alloc_tag_add(ref, current->alloc_tag, full_size, ref);
 	}
 }
 
 static void pcpu_alloc_tag_free_hook(struct pcpu_chunk *chunk, int off,
 				     size_t full_size)
 {
-	if (mem_alloc_profiling_enabled() && likely(chunk->obj_exts))
-		alloc_tag_sub(&chunk->obj_exts[off >> PCPU_MIN_ALLOC_SHIFT].tag,
-			      full_size);
+	if (mem_alloc_profiling_enabled() && likely(chunk->obj_exts)) {
+		union codetag_ref *ref = &chunk->obj_exts[off >> PCPU_MIN_ALLOC_SHIFT].tag;
+
+		alloc_tag_sub(ref, full_size, ref);
+	}
 }
 #else
 static void pcpu_alloc_tag_alloc_hook(struct pcpu_chunk *chunk, int off,
mm/slub.c +4 -2
index cbc51440b3d9..a151268b9726 100644
@@ -2436,7 +2436,7 @@ __alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags,
 		obj_ext = slab_obj_ext(s, slab, obj_exts, object);
 		ref = slab_obj_ext_codetag_ref(slab, obj_ext);
 
-		alloc_tag_add(ref, current->alloc_tag, s->size);
+		alloc_tag_add(ref, current->alloc_tag, s->size, ref);
 
 		put_slab_obj_exts(obj_exts);
 	} else {
@@ -2476,9 +2476,11 @@ __alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p
 	get_slab_obj_exts(obj_exts);
 	for (int i = 0; i < objects; i++) {
 		struct slabobj_ext *ext;
+		union codetag_ref *ref;
 
 		ext = slab_obj_ext(s, slab, obj_exts, p[i]);
-		alloc_tag_sub(slab_obj_ext_codetag_ref(slab, ext), s->size);
+		ref = slab_obj_ext_codetag_ref(slab, ext);
+		alloc_tag_sub(ref, s->size, ref);
 	}
 	put_slab_obj_exts(obj_exts);
 }
Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#2 lore raw

PATCH v5 2/5 alloc_tag: Introduce IOCTLs to toggle allocation tracepoints

In reply to Abhishek Bapat

Introduce a new IOCTL (ALLOCINFO_IOC_TOGGLE_TRACE) to selectively toggle tracing on allocation call sites. Userspace tools can use the existing filtering mechanism to specify the set of tags to toggle tracing for.

To facilitate low overhead execution for non-targeted call sites, add a new CODETAG_FLAG_TRACE_ON flag to struct codetag to track per-site activation. Protect these conditional branch evaluations using the global alloc_tag_trace_key static branch and an inline static key check pattern in the allocator hooks (alloc_tag_add, alloc_tag_sub, etc). This ensures that the trace events are entirely skipped when no allocation call sites are being actively traced, leaving only a NOP on the allocation fast path.

---
 Documentation/mm/allocation-profiling.rst |  20 ++++
 include/linux/alloc_tag.h                 |  47 +++++---
 include/linux/codetag.h                   |   5 +-
 include/uapi/linux/alloc_tag.h            |  13 +++
 mm/alloc_tag.c                            | 127 ++++++++++++++++++++--
 5 files changed, 186 insertions(+), 26 deletions(-)
Documentation/mm/allocation-profiling.rst +20 -0
index b241450394df..b855f8ac87fd 100644
@@ -149,6 +149,26 @@ expose the full call stack and the lifetime of individual allocations:
 - slab and percpu: the address of the object's `codetag_ref`
 - page allocator: the `struct page` pointer of the head page
 
+Events are only emitted for tags that have tracing turned on and while profiling
+is enabled. When no tag is traced, the hooks are behind a static branch.
+
+Enabling Tracing
+----------------
+
+Tracing is toggled on and off per tag with the `ALLOCINFO_IOC_TOGGLE_TRACE`
+ioctl on `/proc/allocinfo` (requires `CAP_SYS_ADMIN`, returns `-EPERM`
+otherwise). It takes the same filter as used by `ALLOCINFO_IOC_GET_AT`; an
+empty mask selects all tags. Size limits in the filter are checked once,
+against the tag's size at the time of the call. Tags already in the requested
+state are skipped and a -ENOENT is returned if no tag matched.
+
+Tracing is turned off for a module's tags when the module is unloaded.
+
+Memory allocated before a tag was enabled has no alloc event but will still
+produce a free event. Likewise, memory alive when tag is disabled will not
+produce a free event. Tools should expect unmatched frees after enabling and
+missing frees after disabling.
+
 Correlating the events
 ----------------------
 
include/linux/alloc_tag.h +32 -15

78 lines not on this page · raw message

include/linux/codetag.h +3 -2
index a25a085c2df1..f728295d50c0 100644
@@ -18,6 +18,7 @@ struct module;
 
 /* codetag flags */
 #define CODETAG_FLAG_INACCURATE	(1 << 0)
+#define CODETAG_FLAG_TRACE_ON	(1 << 1)
 
 /*
  * An instance of this structure is created in a special ELF section at every
@@ -25,7 +26,7 @@ struct module;
  * an array of these.
  */
 struct codetag {
-	unsigned int flags;
+	atomic_t flags;
 	unsigned int lineno;
 	const char *modname;
 	const char *function;
@@ -71,7 +72,7 @@ struct codetag_iterator {
 	.function	= __func__,			\
 	.filename	= __FILE__,			\
 	.lineno		= __LINE__,			\
-	.flags		= 0,				\
+	.flags		= ATOMIC_INIT(0),		\
 }
 
 void codetag_lock_module_list(struct codetag_type *cttype);
include/uapi/linux/alloc_tag.h +13 -0
index 7d4618bea043..dafcba2f7e2c 100644
@@ -85,9 +85,20 @@ struct allocinfo_get_at {
 	struct allocinfo_tag_data data;
 };
 
+struct allocinfo_toggle_traces {
+	/* inputs */
+	/*
+	 * Tags matching the filter are toggled; an empty mask selects all tags.
+	 * Size criteria are checked once, against the tag's size at call time.
+	 */
+	struct allocinfo_filter filter;
+	__u64 enable;
+};
+
 #define _ALLOCINFO_IOC_CONTENT_ID	0
 #define _ALLOCINFO_IOC_GET_AT		1
 #define _ALLOCINFO_IOC_GET_NEXT		2
+#define _ALLOCINFO_IOC_TOGGLE_TRACE	3
 
 #define ALLOCINFO_IOC_BASE		0xA6
 #define ALLOCINFO_IOC_CONTENT_ID	_IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID,	\
@@ -96,5 +107,7 @@ struct allocinfo_get_at {
 					      struct allocinfo_get_at)
 #define ALLOCINFO_IOC_GET_NEXT		_IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT,	\
 					     struct allocinfo_tag_data)
+#define ALLOCINFO_IOC_TOGGLE_TRACE	_IOW(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_TOGGLE_TRACE,	\
+					    struct allocinfo_toggle_traces)
 
 #endif /* _UAPI_ALLOC_TAG_H */
mm/alloc_tag.c +118 -9

181 lines not on this page · raw message

Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#3 lore raw

Re: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints

kernel test robot noticed the following build warnings:

Hi Abhishek,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bcc036cfa1670a64500f6d857eaa71908b79e3fb]

url:    https://github.com/intel-lab-lkp/linux/commits/Abhishek-Bapat/alloc_tag-Add-trace-events-for-tracing-allocations/20261104-190336
base:   bcc036cfa1670a64500f6d857eaa71908b79e3fb
patch link:    https://lore.kernel.org/r/37ae7dca766994d3872fcc65b623a80d5d506369.1793818636.git.abhishekbapat%40google.com
patch subject: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints
config: xtensa-randconfig-1001-20261105 (https://download.01.org/0day-ci/archive/20261105/[email protected]/config)
compiler: xtensa-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261105/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> WARNING: modpost: vmlinux: section mismatch in reference: alloc_tag_trace_enabled+0x2c (section: .text.unlikely) -> initcall_level_names (section: .init.data)

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

maintainer
#4 lore raw

Re: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints

Quoted 14 lines · On Thu, 5 Nov 2026 23:15:33 +0800 kernel test robot <[email protected]> wrote:

Hi Abhishek,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bcc036cfa1670a64500f6d857eaa71908b79e3fb]

url:    https://github.com/intel-lab-lkp/linux/commits/Abhishek-Bapat/alloc_tag-Add-trace-events-for-tracing-allocations/20261104-190336
base:   bcc036cfa1670a64500f6d857eaa71908b79e3fb
patch link:    https://lore.kernel.org/r/37ae7dca766994d3872fcc65b623a80d5d506369.1793818636.git.abhishekbapat%40google.com
patch subject: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints
config: xtensa-randconfig-1001-20261105 (https://download.01.org/0day-ci/archive/20261105/[email protected]/config)
compiler: xtensa-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261105/[email protected]/reproduce)
Quoted 8 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>, old ones prefixed by <<):

Quoted 1 line
Quoted 1 line

WARNING: modpost: vmlinux: section mismatch in reference: alloc_tag_trace_enabled+0x2c (section: .text.unlikely) -> initcall_level_names (section: .init.data)

Thanks.

#syz test

From: Andrew Morton <[email protected]>
Subject: alloc_tag: force inline the allocation tracing check
Date: Thu, 05 Nov 2026 17:18:09 -0800

alloc_tag_trace_enabled() is declared inline, but the compiler can still emit an out-of-line copy. Force it inline,

---

 include/linux/alloc_tag.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/alloc_tag.h renamed from include/linux/alloc_tag.h~alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix +1 -1
@@ -161,7 +161,7 @@ static inline bool alloc_tag_is_traced(c
 	return !!(atomic_read(&tag->ct.flags) & CODETAG_FLAG_TRACE_ON);
 }
 
-static inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
+static __always_inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
 {
 	if (static_branch_unlikely(&alloc_tag_trace_key))
 		return tag && alloc_tag_is_traced(tag);

_

reviewer
#5 lore raw

Re: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints

Hi Andrew

Quoted 58 lines · Andrew Morton
Quoted 14 lines · On Thu, 5 Nov 2026 23:15:33 +0800 kernel test robot <[email protected]> wrote:

Hi Abhishek,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bcc036cfa1670a64500f6d857eaa71908b79e3fb]

url:    https://github.com/intel-lab-lkp/linux/commits/Abhishek-Bapat/alloc_tag-Add-trace-events-for-tracing-allocations/20261104-190336
base:   bcc036cfa1670a64500f6d857eaa71908b79e3fb
patch link:    https://lore.kernel.org/r/37ae7dca766994d3872fcc65b623a80d5d506369.1793818636.git.abhishekbapat%40google.com
patch subject: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints
config: xtensa-randconfig-1001-20261105 (https://download.01.org/0day-ci/archive/20261105/[email protected]/config)
compiler: xtensa-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261105/[email protected]/reproduce)
Quoted 8 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>, old ones prefixed by <<):

Quoted 1 line
Quoted 1 line

WARNING: modpost: vmlinux: section mismatch in reference: alloc_tag_trace_enabled+0x2c (section: .text.unlikely) -> initcall_level_names (section: .init.data)

Thanks.

#syz test

From: Andrew Morton <[email protected]>
Subject: alloc_tag: force inline the allocation tracing check
Date: Thu, 05 Nov 2026 17:18:09 -0800

alloc_tag_trace_enabled() is declared inline, but the compiler can still emit an out-of-line copy. Force it inline,

---

 include/linux/alloc_tag.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/alloc_tag.h renamed from include/linux/alloc_tag.h~alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix +1 -1
@@ -161,7 +161,7 @@ static inline bool alloc_tag_is_traced(c
 	return !!(atomic_read(&tag->ct.flags) & CODETAG_FLAG_TRACE_ON);
 }
 
-static inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
+static __always_inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
 {
 	if (static_branch_unlikely(&alloc_tag_trace_key))
 		return tag && alloc_tag_is_traced(tag);
Quoted 2 lines

_

Thanks a lot for looking at this - and for putting together a fix at all.

Perhaps alloc_tag_is_traced needs the same treatment as well?

Even with your patch applied, I still hit the warning, just attributed to a different symbol:

WARNING: modpost: vmlinux: section mismatch in reference:
alloc_tag_is_traced+0x10 (section: .text.unlikely) -> initcall_level_names (section: .init.data)

Marking both helpers __always_inline clears the warning in my build, and it would fit what the series intends for the fast path anyway.

Thanks
Best Regards
Hao

maintainer
#6 lore raw

Re: [PATCH v5 2/5] alloc_tag: Introduce IOCTLs to toggle allocation tracepoints

Quoted 14 lines · On Fri, 6 Nov 2026 15:54:33 +0800 Hao Ge <[email protected]> wrote:
Quoted 8 lines
(quoted diff) +1 -1
-static inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
+static __always_inline bool alloc_tag_trace_enabled(const struct alloc_tag *tag)
 {
 	if (static_branch_unlikely(&alloc_tag_trace_key))
 		return tag && alloc_tag_is_traced(tag);

_

Thanks a lot for looking at this - and for putting together a fix at all.

Perhaps alloc_tag_is_traced needs the same treatment as well?

Quoted 7 lines

Even with your patch applied, I still hit the warning, just attributed to a different symbol:

WARNING: modpost: vmlinux: section mismatch in reference:
alloc_tag_is_traced+0x10 (section: .text.unlikely) -> initcall_level_names (section: .init.data)

Marking both helpers __always_inline clears the warning in my build, and it would fit what the series intends for the fast path anyway.

Great, thanks, I added another -fix on top:

include/linux/alloc_tag.h renamed from include/linux/alloc_tag.h~alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix +1 -1
@@ -156,7 +156,7 @@ static inline void alloc_tag_clear_trace
 	atomic_andnot(CODETAG_FLAG_TRACE_ON, &tag->ct.flags);
 }
 
-static inline bool alloc_tag_is_traced(const struct alloc_tag *tag)
+static __always_inline inline bool alloc_tag_is_traced(const struct alloc_tag *tag)
 {
 	return !!(atomic_read(&tag->ct.flags) & CODETAG_FLAG_TRACE_ON);
 }

_

#7 lore raw

PATCH v5 3/5 alloc_tag: Add a flags field to allocinfo_filter

In reply to Abhishek Bapat

struct allocinfo_filter uses a whole __u64 to hold the single boolean used by the accuracy filter. Adding another boolean criterion the same way would grow struct allocinfo_get_at, which changes the ALLOCINFO_IOC_GET_AT ioctl number and breaks the UAPI.

Add a flags field that shares storage with inaccurate through an anonymous union, and use bit 0, ALLOCINFO_FILTER_FLAG_INACCURATE, for the accuracy criterion. New boolean criteria can take the remaining bits without changing the struct layout.

Keeping inaccurate in the union means userspace built against the older API still compiles, and the values 0 and 1 keep their meaning. Previously any non-zero value selected inaccurate tags; now only bit 0 is checked, so a value such as 2 selects accurate tags instead.

---
 include/uapi/linux/alloc_tag.h | 16 ++++++++++++++--
 mm/alloc_tag.c                 |  2 +-
 2 files changed, 15 insertions(+), 3 deletions(-)
include/uapi/linux/alloc_tag.h +14 -2
index dafcba2f7e2c..2fc843bb06bc 100644
@@ -68,13 +68,25 @@ enum {
 #define ALLOCINFO_FILTER_MASKS \
 	((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1)
 
+#define ALLOCINFO_FILTER_FLAG_INACCURATE	(1ULL << 0)
+
 struct allocinfo_filter {
 	__u64 mask; /* bitmask of the filter fields used */
 	struct allocinfo_tag fields;
 	__u64 min_size;
 	__u64 max_size;
-	/* filter criteria only; see allocinfo_counter.accurate for actual accuracy */
-	__u64 inaccurate;
+	/*
+	 * Filter criteria only; see allocinfo_counter for the actual state.
+	 * Set ALLOCINFO_FILTER_FLAG_* bits in flags for the criteria selected
+	 * in mask. inaccurate is kept as an alias of flags so that userspace
+	 * built against the older API still compiles. Note that only
+	 * ALLOCINFO_FILTER_FLAG_INACCURATE is checked, so a non-zero value
+	 * with bit 0 clear no longer selects inaccurate tags.
+	 */
+	union {
+		__u64 inaccurate;
+		__u64 flags;
+	};
 };
 
 struct allocinfo_get_at {
mm/alloc_tag.c +1 -1
index 9ff9dd04d692..504c1a85050d 100644
@@ -306,7 +306,7 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter,
 
 	if (filter->mask & ALLOCINFO_FILTER_MASK_INACCURATE) {
 		inaccurate = alloc_tag_is_inaccurate(ct_to_alloc_tag(ct));
-		if (inaccurate != !!(filter->inaccurate))
+		if (inaccurate != !!(filter->flags & ALLOCINFO_FILTER_FLAG_INACCURATE))
 			return false;
 	}
 
Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#8 lore raw

PATCH v5 4/5 alloc_tag: Extend allocinfo_filter to support tracing queries

In reply to Abhishek Bapat

Extend the allocinfo filtering mechanism to allow users to filter tags based on their trace state.

---
 include/uapi/linux/alloc_tag.h | 8 ++++++--
 mm/alloc_tag.c                 | 8 ++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
include/uapi/linux/alloc_tag.h +6 -2
index 2fc843bb06bc..ca0779051cf4 100644
@@ -38,7 +38,8 @@ struct allocinfo_counter {
 	__u64 bytes;
 	__u64 calls;
 	__u8 accurate;
-	__u8 pad[7];
+	__u8 trace_on;
+	__u8 pad[6];
 } __attribute__((aligned(8)));
 
 struct allocinfo_tag_data {
@@ -54,7 +55,8 @@ enum {
 	ALLOCINFO_FILTER_INACCURATE,
 	ALLOCINFO_FILTER_MIN_SIZE,
 	ALLOCINFO_FILTER_MAX_SIZE,
-	__ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_MAX_SIZE
+	ALLOCINFO_FILTER_TRACE_ON,
+	__ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_TRACE_ON
 };
 
 #define ALLOCINFO_FILTER_MASK_MODNAME		(1 << ALLOCINFO_FILTER_MODNAME)
@@ -64,11 +66,13 @@ enum {
 #define ALLOCINFO_FILTER_MASK_INACCURATE	(1 << ALLOCINFO_FILTER_INACCURATE)
 #define ALLOCINFO_FILTER_MASK_MIN_SIZE		(1 << ALLOCINFO_FILTER_MIN_SIZE)
 #define ALLOCINFO_FILTER_MASK_MAX_SIZE		(1 << ALLOCINFO_FILTER_MAX_SIZE)
+#define ALLOCINFO_FILTER_MASK_TRACE_ON		(1 << ALLOCINFO_FILTER_TRACE_ON)
 
 #define ALLOCINFO_FILTER_MASKS \
 	((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1)
 
 #define ALLOCINFO_FILTER_FLAG_INACCURATE	(1ULL << 0)
+#define ALLOCINFO_FILTER_FLAG_TRACE_ON		(1ULL << 1)
 
 struct allocinfo_filter {
 	__u64 mask; /* bitmask of the filter fields used */
mm/alloc_tag.c +8 -0
index 504c1a85050d..e9b70268eacb 100644
@@ -250,6 +250,7 @@ static void allocinfo_to_params(struct codetag *ct,
 	data->counter.bytes = counters->bytes;
 	data->counter.calls = counters->calls;
 	data->counter.accurate = !alloc_tag_is_inaccurate(ct_to_alloc_tag(ct));
+	data->counter.trace_on = alloc_tag_is_traced(ct_to_alloc_tag(ct));
 }
 
 /*
@@ -323,6 +324,13 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter,
 			return false;
 	}
 
+	if (filter->mask & ALLOCINFO_FILTER_MASK_TRACE_ON) {
+		bool tracing = alloc_tag_is_traced(ct_to_alloc_tag(ct));
+
+		if (tracing != !!(filter->flags & ALLOCINFO_FILTER_FLAG_TRACE_ON))
+			return false;
+	}
+
 	return true;
 }
 
Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#9 lore raw

PATCH v5 5/5 alloc_tag: Add a test for trace state toggle and filtering

In reply to Abhishek Bapat

Following the introduction of the ALLOCINFO_IOC_TOGGLE_TRACE IOCTL and ALLOCINFO_FILTER_MASK_TRACE_ON filters for selective memory allocation tracing, update the kselftests to validate this behaviour.

Introduce test_tracing_toggle_and_filter, which validates the architecture by:
1. Dynamically flipping trace execution state for a designated allocation.
2. Query the same tag again, and check that the returned tag reports the new state in its trace_on field.
3. Restore the tag's original trace state, also on failure, so the test does not leave tracing changed on the system.

The test uses dup_mm() function name with the function filter in the IOCTL to run the above steps. If the dup_mm() tag is not found, the test is skipped.

---
 .../alloc_tag/allocinfo_ioctl_test.c          | 114 +++++++++++++++++-
 1 file changed, 111 insertions(+), 3 deletions(-)
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +111 -3
index 74fd64b2370c..300460084d9f 100644
@@ -48,6 +48,11 @@ static inline int __allocinfo_get_next(int dev_fd, struct allocinfo_tag_data *pa
 	return ioctl(dev_fd, ALLOCINFO_IOC_GET_NEXT, params);
 }
 
+static inline int __allocinfo_toggle_trace(int dev_fd, struct allocinfo_toggle_traces *params)
+{
+	return ioctl(dev_fd, ALLOCINFO_IOC_TOGGLE_TRACE, params);
+}
+
 static bool match_entry(const struct allocinfo_tag_data *procfs_entry,
 			const struct allocinfo_tag_data *tag_data,
 			bool match_bytes, bool match_calls, bool match_lineno,
@@ -289,6 +294,8 @@ static int run_filter_test(const struct allocinfo_filter *filter)
 	return ret;
 }
 
+static const char *target_test_function = "dup_mm";
+
 static int test_filename_filter(void)
 {
 	struct allocinfo_filter filter;
@@ -304,11 +311,10 @@ static int test_filename_filter(void)
 static int test_function_filter(void)
 {
 	struct allocinfo_filter filter;
-	const char *target_function = "dup_mm";
 
 	memset(&filter, 0, sizeof(filter));
 	filter.mask |= ALLOCINFO_FILTER_MASK_FUNCTION;
-	strncpy(filter.fields.function, target_function, ALLOCINFO_STR_SIZE);
+	strncpy(filter.fields.function, target_test_function, ALLOCINFO_STR_SIZE);
 
 	return run_filter_test(&filter);
 }
@@ -514,11 +520,107 @@ static int test_lineno_filter(void)
 	return ret;
 }
 
+static enum ioctl_ret toggle_trace(struct allocinfo_tag *target_tag,
+				   bool enable)
+{
+	int fd;
+	struct allocinfo_toggle_traces toggle_params;
+
+	fd = open(ALLOCINFO_PROC, O_RDONLY);
+	if (fd < 0) {
+		ksft_print_msg("Failed to open " ALLOCINFO_PROC ": %s\n", strerror(errno));
+		return IOCTL_FAILURE;
+	}
+
+	memset(&toggle_params, 0, sizeof(toggle_params));
+	toggle_params.filter.mask = ALLOCINFO_FILTER_MASK_MODNAME | ALLOCINFO_FILTER_MASK_FUNCTION |
+				    ALLOCINFO_FILTER_MASK_FILENAME | ALLOCINFO_FILTER_MASK_LINENO;
+	toggle_params.filter.fields = *target_tag;
+	toggle_params.enable = enable;
+
+	if (__allocinfo_toggle_trace(fd, &toggle_params)) {
+		close(fd);
+		return IOCTL_FAILURE;
+	}
+
+	close(fd);
+	return IOCTL_SUCCESS;
+}
+
+static int test_tracing_toggle_and_filter(void)
+{
+	struct allocinfo_filter filter = { 0 };
+	enum ioctl_ret ioctl_status;
+	int ret = KSFT_PASS;
+	bool initial_state, target_state;
+	struct allocinfo_tag target_tag;
+	struct allocinfo_tag_data_vec *tags = calloc(1, sizeof(*tags));
+
+	if (!tags) {
+		ksft_print_msg("Memory allocation failed.\n");
+		return KSFT_FAIL;
+	}
+
+	filter.mask |= ALLOCINFO_FILTER_MASK_FUNCTION;
+	strncpy(filter.fields.function, target_test_function, ALLOCINFO_STR_SIZE);
+
+	ioctl_status = get_filtered_ioctl_entries(tags, &filter, 0);
+	if (ioctl_status != IOCTL_SUCCESS || tags->count == 0) {
+		ksft_print_msg("Could not retrieve IOCTL entries for %s\n", target_test_function);
+		ret = KSFT_SKIP;
+		goto exit;
+	}
+
+	target_tag = tags->tag[0].tag;
+	initial_state = tags->tag[0].counter.trace_on;
+	target_state = !initial_state;
+
+	ioctl_status = toggle_trace(&target_tag, target_state);
+	if (ioctl_status != IOCTL_SUCCESS) {
+		ksft_print_msg("Failed to toggle tracing\n");
+		ret = KSFT_FAIL;
+		goto exit;
+	}
+
+	filter.mask |= ALLOCINFO_FILTER_MASK_TRACE_ON;
+	if (target_state)
+		filter.flags |= ALLOCINFO_FILTER_FLAG_TRACE_ON;
+
+	ioctl_status = get_filtered_ioctl_entries(tags, &filter, 0);
+	if (ioctl_status != IOCTL_SUCCESS) {
+		ksft_print_msg("Error retrieving IOCTL entries with trace filter.\n");
+		ret = KSFT_FAIL;
+		goto exit_revert;
+	}
+
+	if (tags->count != 1) {
+		ksft_print_msg("Expected exactly 1 entry, but got %llu\n", tags->count);
+		ret = KSFT_FAIL;
+		goto exit_revert;
+	}
+
+	if (tags->tag[0].counter.trace_on != target_state) {
+		ksft_print_msg("Entry returned by trace filter does not match target state\n");
+		ret = KSFT_FAIL;
+	}
+
+exit_revert:
+	ioctl_status = toggle_trace(&target_tag, initial_state);
+	if (ioctl_status != IOCTL_SUCCESS) {
+		ksft_print_msg("Failed to revert tracing to initial state\n");
+		ret = KSFT_FAIL;
+	}
+
+exit:
+	free(tags);
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret;
 
-	ksft_set_plan(4);
+	ksft_set_plan(5);
 
 	ret = test_filename_filter();
 	if (ret == KSFT_SKIP)
@@ -526,6 +628,12 @@ int main(int argc, char *argv[])
 	else
 		ksft_test_result(ret == KSFT_PASS, "test_filename_filter\n");
 
+	ret = test_tracing_toggle_and_filter();
+	if (ret == KSFT_SKIP)
+		ksft_test_result_skip("Skipping test_tracing_toggle_and_filter\n");
+	else
+		ksft_test_result(ret == KSFT_PASS, "test_tracing_toggle_and_filter\n");
+
 	ret = test_function_filter();
 	if (ret == KSFT_SKIP)
 		ksft_test_result_skip("Skipping test_function_filter\n");
Signature · 1 line
2.56.0.506.g506a7eabc5-goog