alloc_tag: introduce IOCTL-based filtering for MAP
Subsystems (6)
v2 cover alloc_tag: introduce IOCTL-based filtering for MAP
Currently, memory allocation profiling data is primarily exposed through /proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:
1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.
This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (alloc_tag_read) for any tags that fail the initial string or location filters.
Scenario 1: Specific File Filtering (arch/x86/events/rapl.c)
1. Traditional (cat /proc/allocinfo | grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)
Scenario 3: Size-Based Filtering (min_size = 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)
v2 changes:
- Patch 1/6: Introduced locking for m->private. Also included the new uapi header file in MAINTAINERS list.
- Patch 2/6: Handled the case where ALLOCINFO_FILTER_MASK_MODNAME is passed but ct->modname is NULL.
- Patch 3/6: Moved min_size and max_size outside of struct allocinfo_tag into struct allocinfo_filter. Added validation that min_size <= max_size. Prefetched alloc_tag_counters if size based filter masks are provided to avoid assimilating per-cpu counters twice.
- Patch 5/6: Removed the hardcoded logic to skip the header, instead the test will skip lines that don't match the format. Also included the newly added alloc_tag selftests directory in MAINTAINERS list.
Abhishek Bapat (5):
alloc_tag: add ioctl filters to /proc/allocinfo
alloc_tag: add size-based filtering to ioctl
alloc_tag: add accuracy based filtering to ioctl
kselftest: alloc_tag: add kselftest for ioctl interface
kselftest: alloc_tag: extend the allocinfo ioctl kselftest
Suren Baghdasaryan (1):
alloc_tag: add ioctl to /proc/allocinfo
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++ lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/alloc_tag.h create mode 100644 tools/testing/selftests/alloc_tag/Makefile create mode 100644 tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 1/6 alloc_tag: add ioctl to /proc/allocinfo
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for /proc/allocinfo file:
ALLOCINFO_IOC_CONTENT_ID - gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
ALLOCINFO_IOC_GET_AT - gets the record at the specified position. This is the position of a record in /proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT - gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.
--- .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 1 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 54 +++++ lib/alloc_tag.c | 193 +++++++++++++++++- lib/codetag.c | 11 + 6 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/alloc_tag.h
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
@@ -349,6 +349,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver <mailto:[email protected]> +0xA6 00-0F uapi/linux/alloc_tag.h Memory allocation profiling + <mailto:[email protected]> 0xAA 00-3F linux/uapi/linux/userfaultfd.h 0xAB 00-1F linux/nbd.h 0xAC 00-1F linux/raw.h
MAINTAINERS +1 -0
@@ -16709,6 +16709,7 @@ S: Maintained F: Documentation/mm/allocation-profiling.rst F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h +F: include/uapi/linux/alloc_tag.h F: lib/alloc_tag.c MEMORY CONTROLLER DRIVERS
include/linux/codetag.h +1 -0
@@ -76,6 +76,7 @@ struct codetag_iterator {void codetag_lock_module_list (struct codetag_type *cttype,bool lock);bool codetag_trylock_module_list (struct codetag_type *cttype); +unsigned long codetag_get_content_id (struct codetag_type *cttype);struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype);struct codetag *codetag_next_ct (struct codetag_iterator *iter);
include/uapi/linux/alloc_tag.h new file +54 -0
54 lines not on this page · raw message
lib/alloc_tag.c +191 -2
223 lines not on this page · raw message
lib/codetag.c +11 -0
@@ -48,6 +48,17 @@ bool codetag_trylock_module_list(struct codetag_type *cttype)return down_read_trylock (&cttype->mod_lock) !=0 ; } +unsigned long codetag_get_content_id (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +/* +* next_mod_seq is updated on every load, so can be used to identify +* content changes. +*/ +return cttype->next_mod_seq; +} +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) {struct codetag_iterator iter = {
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 2/6 alloc_tag: add ioctl filters to /proc/allocinfo
In reply to Abhishek BapatExtend the capability of the IOCTL mechanism to filter allocations based on tag's module name, function name, file name and line number.
--- include/uapi/linux/alloc_tag.h | 26 ++++++++++++++- lib/alloc_tag.c | 58 ++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-)
include/uapi/linux/alloc_tag.h +25 -1
@@ -34,8 +34,32 @@ struct allocinfo_tag_data {struct allocinfo_counter counter; }; +enum { +ALLOCINFO_FILTER_MODNAME , +ALLOCINFO_FILTER_FUNCTION , +ALLOCINFO_FILTER_FILENAME , +ALLOCINFO_FILTER_LINENO , + __ALLOCINFO_FILTER_LAST =ALLOCINFO_FILTER_LINENO +}; + +#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME) +#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION) +#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME) +#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) + +#define ALLOCINFO_FILTER_MASKS \ + ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1) + +struct allocinfo_filter { +__u64 mask;/* bitmask of the filter fields used */ +struct allocinfo_tag fields; +}; +struct allocinfo_get_at { -__u64 pos;/* input */ +/* inputs */ +__u64 pos; +struct allocinfo_filter filter; +/* output */ struct allocinfo_tag_data data; };
lib/alloc_tag.c +55 -3
100 lines not on this page · raw message
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 3/6 alloc_tag: add size-based filtering to ioctl
In reply to Abhishek BapatExtend the allocinfo filtering mechanism to allow users to filter tags based on the total number of bytes allocated [min_size, max_size]. The size range is inclusive.
Filtering by size involves retrieving allocinfo per-CPU counters, which is an expensive operation. Hence, the performance of size-based filtering will be worse than other filters.
--- include/uapi/linux/alloc_tag.h | 8 +++- lib/alloc_tag.c | 72 ++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 12 deletions(-)
include/uapi/linux/alloc_tag.h +7 -1
@@ -39,13 +39,17 @@ enum {ALLOCINFO_FILTER_FUNCTION ,ALLOCINFO_FILTER_FILENAME ,ALLOCINFO_FILTER_LINENO , - __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_LINENO +ALLOCINFO_FILTER_MIN_SIZE , +ALLOCINFO_FILTER_MAX_SIZE , + __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_MAX_SIZE };#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME)#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION)#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME)#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) +#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_MASKS \ ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1)
@@ -53,6 +57,8 @@ enum {struct allocinfo_filter {__u64 mask;/* bitmask of the filter fields used */ struct allocinfo_tag fields; +__u64 min_size; +__u64 max_size; };struct allocinfo_get_at {
lib/alloc_tag.c +61 -11
142 lines not on this page · raw message
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 4/6 alloc_tag: add accuracy based filtering to ioctl
In reply to Abhishek BapatExtend the allocinfo filtering mechanism to allow users to filter tags based on their accuracy.
--- include/uapi/linux/alloc_tag.h | 3 +++ lib/alloc_tag.c | 8 ++++++++ 2 files changed, 11 insertions(+)
include/uapi/linux/alloc_tag.h +3 -0
@@ -20,6 +20,7 @@ struct allocinfo_tag {char function[ALLOCINFO_STR_SIZE ];char filename[ALLOCINFO_STR_SIZE ];__u64 lineno; +__u64 inaccurate; };struct allocinfo_counter {
@@ -39,6 +40,7 @@ enum {ALLOCINFO_FILTER_FUNCTION ,ALLOCINFO_FILTER_FILENAME ,ALLOCINFO_FILTER_LINENO , +ALLOCINFO_FILTER_INACCURATE ,ALLOCINFO_FILTER_MIN_SIZE ,ALLOCINFO_FILTER_MAX_SIZE , __ALLOCINFO_FILTER_LAST =ALLOCINFO_FILTER_MAX_SIZE
@@ -48,6 +50,7 @@ enum {#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION)#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME)#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) +#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)
lib/alloc_tag.c +8 -0
@@ -217,6 +217,8 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter,struct alloc_tag_counters *counters) { +bool inaccurate; +if (!filter || !filter->mask)return true;
@@ -239,6 +241,12 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter, ct->lineno != filter->fields.lineno)return false; +if (filter->mask &ALLOCINFO_FILTER_MASK_INACCURATE ) { + inaccurate = !!(ct->flags &CODETAG_FLAG_INACCURATE ); +if (inaccurate != filter->fields.inaccurate) +return false; + } +if ((filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) || (filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE )) {/* We assume counters is not NULL here as per caller logic */
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 5/6 kselftest: alloc_tag: add kselftest for ioctl interface
In reply to Abhishek BapatIntroduce a kselftest to verify the new IOCTL-based interface for /proc/allocinfo. The test covers:
1. Validation of the filename filter.
2. Validation of the function filter.
The first test validates the functionality of the filename filter. Using "mm/memory.c" as the candidate filename filter, it retrieves filtered entries from both procfs and ioctl and matches the first VEC_MAX_ENTRIES entries.
The second test validates the functionality of the function filter. It uses "dup_mm" as the candidate function as we do not expect this function name to change frequently and hence won't be needing to modify this test often.
Note that both the tests match line no, function name and file name fields. Bytes allocated and calls are not matched as those values may change in the time when the data is being read from procfs and ioctl and hence can lead to false negatives.
--- MAINTAINERS | 1 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 313 ++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 tools/testing/selftests/alloc_tag/Makefile create mode 100644 tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c
MAINTAINERS +1 -0
@@ -16711,6 +16711,7 @@ F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h F: include/uapi/linux/alloc_tag.h F: lib/alloc_tag.c +F: tools/testing/selftests/alloc_tag/ MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski <[email protected]>
tools/testing/selftests/alloc_tag/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := allocinfo_ioctl_test + +CFLAGS += -Wall +CFLAGS += -I../../../../usr/include + +include ../lib.mk +
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +313 -0
313 lines not on this page · raw message
Signature · 1 line
2.54.0.746.g67dd491aae-goog
PATCH v2 6/6 kselftest: alloc_tag: extend the allocinfo ioctl kselftest
In reply to Abhishek BapatAdd the following 2 scenarios to the allocinfo ioctl kselftest:
1. Validate size based filtering
2. Validate lineno based filtering
The first test uses "do_init_module" as the candidate function for the test. This is because the associated site will only allocate memory when a kernel module is loaded. The return value of get_content_id() changes every time modules are loaded or unloaded. Hence, as long as get_content_id() values at the start and the end of the test are the same, the memory allocated by the do_init_module call site should also remain the same. Consequently, the test can assume consistency between the value returned by the ioctl and the procfs resulting in less flakiness.
--- .../alloc_tag/allocinfo_ioctl_test.c | 194 +++++++++++++++++- 1 file changed, 193 insertions(+), 1 deletion(-)
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +193 -1
@@ -291,11 +291,191 @@ static int test_function_filter(void)return run_filter_test (&filter); } +static int test_size_filter (void ) +{ +int fd; +struct allocinfo_tag_data_vec *tags =malloc (sizeof (*tags)); +struct allocinfo_tag_data_vec *procfs_entries =malloc (sizeof (*procfs_entries)); +struct allocinfo_filter filter; +int ret =KSFT_PASS ; +__u64 target_size, i; +bool found = false; +const char *target_function ="do_init_module" ; + +if (!tags || !procfs_entries) { +ksft_print_msg ("Memory allocation failed.\n" ); + ret =KSFT_FAIL ; + goto freemem; + } + + fd =open (ALLOCINFO_PROC ,O_RDONLY ); +if (fd <0 ) { +ksft_exit_skip ("Failed to open " ALLOCINFO_PROC ": %s\n" ,strerror (errno)); + ret =KSFT_FAIL ; + goto freemem; + } + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_FUNCTION ; +strncpy (filter.fields.function, target_function,ALLOCINFO_STR_SIZE ); + +if (get_filtered_procfs_entries (procfs_entries, &filter, fd)) { +ksft_print_msg ("Error retrieving entries from " ALLOCINFO_PROC "\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +if (procfs_entries->count ==0 ) { +ksft_print_msg ("Function %s not found in procfs\n" , target_function); + ret =KSFT_SKIP ; + goto exit; + } + +/* +* We depend on the result of procfs entries to create the ioctl_filter. Hence we +* cannot recycle the run_filter_test function here. +*/ + target_size = procfs_entries->tag[0 ].counter.bytes; + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_MIN_SIZE |ALLOCINFO_FILTER_MASK_MAX_SIZE ; + filter.min_size = target_size; + filter.max_size = target_size; + +__u64 pos =0 ; +enum ioctl_ret ioctl_status; + +/* +* This loop is required because the first 32 entries fetched by the IOCTL based on +* the size parameter might not contain the exact entry that was used from procfs. +* If that happens, we must update pos and fetch again until we find the exact entry. +*/ +while (1 ) { + ioctl_status =get_filtered_ioctl_entries (tags, &filter, fd, pos); +if (ioctl_status ==IOCTL_INVALID_DATA ) { +ksft_print_msg ("Trouble retrieving valid IOCTL entries, skipping.\n" ); + ret =KSFT_SKIP ; + goto exit; + } +if (ioctl_status ==IOCTL_FAILURE ) { +ksft_print_msg ("Error retrieving IOCTL entries.\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +for (i =0 ; i < tags->count; i++) { +if (strcmp (tags->tag[i].tag.function, target_function) ==0 ) { + found = true; +break ; + } + } + +if (found) +break ; + +if (tags->count <VEC_MAX_ENTRIES ) +break ; + + pos += tags->count; + } + +if (!found) { +ksft_print_msg ("Entry with function %s not found in IOCTL results\n" , + target_function); + ret =KSFT_FAIL ; + } + +exit: +close (fd); +freemem: +free (tags); +free (procfs_entries); +return ret; +} + +static int test_lineno_filter (void ) +{ +int fd; +struct allocinfo_tag_data_vec *tags =malloc (sizeof (*tags)); +struct allocinfo_tag_data_vec *procfs_entries =malloc (sizeof (*procfs_entries)); +struct allocinfo_filter filter; +enum ioctl_ret ioctl_status; +int ret =KSFT_PASS ; +__u64 target_lineno, i; + +if (!tags || !procfs_entries) { +ksft_print_msg ("Memory allocation failed.\n" ); + ret =KSFT_FAIL ; + goto freemem; + } + + fd =open (ALLOCINFO_PROC ,O_RDONLY ); +if (fd <0 ) { +ksft_exit_skip ("Failed to open " ALLOCINFO_PROC ": %s\n" ,strerror (errno)); + ret =KSFT_FAIL ; + goto freemem; + } + +memset (&filter,0 ,sizeof (filter)); + +if (get_filtered_procfs_entries (procfs_entries, &filter, fd)) { +ksft_print_msg ("Error retrieving entries from " ALLOCINFO_PROC "\n" ); + ret =KSFT_FAIL ; + goto exit; + } +if (procfs_entries->count ==0 ) { +ksft_print_msg ("Could not retrieve procfs entries\n" ); + ret =KSFT_SKIP ; + goto exit; + } +/* +* We depend on the result of procfs entries to create the ioctl_filter. Hence we +* cannot recycle the run_filter_test function here. +*/ + target_lineno = procfs_entries->tag[0 ].tag.lineno; + + filter.mask |=ALLOCINFO_FILTER_MASK_LINENO ; + filter.fields.lineno = target_lineno; + + ioctl_status =get_filtered_ioctl_entries (tags, &filter, fd,0 ); +if (ioctl_status ==IOCTL_INVALID_DATA ) { +ksft_print_msg ("Trouble retrieving valid IOCTL entries, skipping.\n" ); + ret =KSFT_SKIP ; + goto exit; + } +if (ioctl_status ==IOCTL_FAILURE ) { +ksft_print_msg ("Error retrieving IOCTL entries.\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +if (tags->count ==0 ) { +ksft_print_msg ("IOCTL returned 0 matches for target lineno %llu.\n" , target_lineno); + ret =KSFT_FAIL ; + goto exit; + } +for (i =0 ; i < tags->count; i++) { +if (tags->tag[i].tag.lineno != target_lineno) { +ksft_print_msg ("IOCTL entry %llu has incorrect lineno %llu.\n" , + i, tags->tag[i].tag.lineno); + ret =KSFT_FAIL ; + goto exit; + } + } + +exit: +close (fd); +freemem: +free (tags); +free (procfs_entries); +return ret; +} +int main (int argc,char *argv[]) {int ret; -ksft_set_plan (2 ); +ksft_set_plan (4 ); ret =test_filename_filter ();if (ret ==KSFT_SKIP )
@@ -309,5 +489,17 @@ int main(int argc, char *argv[])else ksft_test_result (ret ==KSFT_PASS ,"test_function_filter\n" ); + ret =test_size_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_size_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_size_filter\n" ); + + ret =test_lineno_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_lineno_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_lineno_filter\n" ); +ksft_finished (); }
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Andrew Morton
maintainerQuoted 29 lines · On Fri, 22 May 2026 17:45:32 +0000 Abhishek Bapat <[email protected]>…
Currently, memory allocation profiling data is primarily exposed through
/proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (
alloc_tag_read) for any tags that fail the initial string or location filters.Scenario 1: Specific File Filtering (
arch/x86/events/rapl.c)
1. Traditional (cat/proc/allocinfo| grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)
Quoted 7 lines
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)Scenario 3: Size-Based Filtering (
min_size= 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)
Yup, textual interfaces aren't fast.
And ioctl-baed interfaces aren't popular. One would prefer to see an interface which uses read()/lseek(), pread(), etc. It would be appropriate for this [0/N] to have a discussion of why that approach was not chosen.
Quoted 4 lines
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++
Quoted 8 lines
lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/alloc_tag.h create mode 100644 tools/testing/selftests/alloc_tag/Makefile create mode 100644 tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c
At some point this should grow user-facing documentation, please.
And the right time for that is now, because such documentation is useful for code review - it makes that review both easier and more useful.
Sashiko had a few things to say:
https://sashiko.dev/#/patchset/[email protected]
Andrew Morton
maintainerRe: [PATCH v2 1/6] alloc_tag: add ioctl to /proc/allocinfo
In reply to Abhishek BapatQuoted 41 lines · On Fri, 22 May 2026 17:45:33 +0000 Abhishek Bapat <[email protected]>…
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for
/proc/allocinfofile:
ALLOCINFO_IOC_CONTENT_ID- gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
ALLOCINFO_IOC_GET_AT- gets the record at the specified position. This is the position of a record in/proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT- gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.index 000000000000..e9a5b55fcc7a
include/uapi/linux/alloc_tag.h new file +19
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* include/linux/alloc_tag.h +*/ + +#ifndef _UAPI_ALLOC_TAG_H +#define _UAPI_ALLOC_TAG_H + +#include <linux/types.h> + +#define ALLOCINFO_STR_SIZE 64 + +struct allocinfo_content_id { + __u64 id; +}; + +struct allocinfo_tag { + /* Longer names are trimmed */ + char modname[ALLOCINFO_STR_SIZE];
include/uapi/linux/alloc_tag.h line 29
Quoted 10 lines
include/uapi/linux/alloc_tag.h new file +10
-0+char function[ALLOCINFO_STR_SIZE ]; +char filename[ALLOCINFO_STR_SIZE ]; +__u64 lineno; +}; + +struct allocinfo_counter { +__u64 bytes; +__u64 calls; +__u8 accurate; +__u8 pad[7 ];/* Add alignment to not break the 32-bit compatible interface */
This seems rather fragile, and makes assumptions about compiler layout?
Can't we use __attribute__((aligned)) in some fashion?
Quoted 64 lines
include/uapi/linux/alloc_tag.h new file +25
-0+}; + +struct allocinfo_tag_data { +struct allocinfo_tag tag; +struct allocinfo_counter counter; +}; + +struct allocinfo_get_at { +__u64 pos;/* input */ +struct allocinfo_tag_data data; +}; + +#define _ALLOCINFO_IOC_CONTENT_ID 0 +#define _ALLOCINFO_IOC_GET_AT 1 +#define _ALLOCINFO_IOC_GET_NEXT 2 + +#define ALLOCINFO_IOC_BASE 0xA6 +#define ALLOCINFO_IOC_CONTENT_ID _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID, \ + struct allocinfo_content_id) +#define ALLOCINFO_IOC_GET_AT _IOWR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_AT, \ + struct allocinfo_get_at) +#define ALLOCINFO_IOC_GET_NEXT _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT, \ + struct allocinfo_tag_data) + +#endif /* _UAPI_ALLOC_TAG_H */ lib/alloc_tag.c +10
-0@@ -5,6 +5,7 @@#include <linux/gfp.h> #include <linux/kallsyms.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/page_ext.h> #include <linux/pgalloc_tag.h> #include <linux/proc_fs.h> @@ -14,6 +15,7 @@#include <linux/string_choices.h> #include <linux/vmalloc.h> #include <linux/kmemleak.h> +#include <uapi/linux/alloc_tag.h> #define ALLOCINFO_FILE_NAME "allocinfo"#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))@@ -46,6 +48,10 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +/* ioctl uses a separate iterator not to interfere with reads */ +struct codetag_iterator ioctl_iter; +bool positioned;/* seq_open_private() sets to 0 */ +struct mutex ioctl_lock; };static void *allocinfo_start (struct seq_file *m,loff_t *pos)@@ -125,6 +131,190 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +static int allocinfo_open (struct inode *inode,struct file *file) +{ +int ret; +
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 147
Quoted 10 lines
lib/alloc_tag.c +10
-0+ ret =seq_open_private (file, &allocinfo_seq_op, +sizeof (struct allocinfo_private )); +if (!ret) { +struct seq_file *m = file->private_data; +struct allocinfo_private *priv = m->private; + +mutex_init (&priv->ioctl_lock); + } +return ret; +}
Generally, the commenting in here is very thin. Add some explanations of what the various functions do and, especially, why they do it?
Quoted 10 lines
lib/alloc_tag.c +10
-0+static int allocinfo_release (struct inode *inode,struct file *file) +{ +return seq_release_private (inode, file); +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +/* Keep an extra space for the trailing NULL. */
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 168
Quoted 10 lines
lib/alloc_tag.c +10
-0+if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +/* Copy a string and trim from the beginning if it's too long */ +static void allocinfo_copy_str (char *dest,const char *src) +{ +strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); +}
See, even these two little functions are unnecessarily difficult to review when one doesn"t know what they are setting out to do. One has to first reverse engineer their intent from the implementation, then check that the implementation implements that intent.
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 211
Quoted 11 lines
lib/alloc_tag.c +11
-0+static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +__u64 pos; +struct allocinfo_get_at params = {0 }; + +if (copy_from_user (¶ms, arg,sizeof (params))) +return -EFAULT ; + + priv = (struct allocinfo_private *)m->private;
Unneeded cast.
Quoted 25 lines
lib/alloc_tag.c +25
-0+ pos = params.pos; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +/* Find the codetag */ + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && pos--) + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) { +allocinfo_to_params (ct, ¶ms.data); + priv->positioned = true; + } + +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (!ct) +return -ENOENT ; + +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ;
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 246
Quoted 10 lines
lib/alloc_tag.c +10
-0+} + +static int allocinfo_ioctl_get_next (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +struct allocinfo_tag_data params = {0 }; +int ret =0 ; + + priv = (struct allocinfo_private *)m->private;
Ditto.
Quoted 28 lines
lib/alloc_tag.c +25
-0+mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +if (!priv->positioned) { + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + priv->positioned = true; + } + + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) +allocinfo_to_params (ct, ¶ms); + +if (!ct) { + priv->positioned = false; + ret = -ENOENT ; + } +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (ret ==0 ) { +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + } +return ret; +}
Hao Ge
reviewerRe: [PATCH v2 1/6] alloc_tag: add ioctl to /proc/allocinfo
In reply to Abhishek BapatHi Abhishek
Thanks for this patch. I had a few questions/comments after going
through it.
Quoted 10 lines · Abhishek Bapat
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for
/proc/allocinfofile:
ALLOCINFO_IOC_CONTENT_ID- gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
codetag_get_content_id() does not reflect module unload
codetag_get_content_id() returns cttype->next_mod_seq:
unsigned long codetag_get_content_id(struct codetag_type *cttype)
{
return cttype->next_mod_seq;
}
However, next_mod_seq is only bumped in codetag_module_init(),
i.e.the module load path:
https://elixir.bootlin.com/linux/v7.1-rc4/source/lib/codetag.c#L204
codetag_unload_module() does not increment next_mod_seq. This means
that if only a module unload happens (without a subsequent load),
content_id stays the same, so users comparing the id before and after
won't detect that the content has changed. The commit message says
"Every time a module is loaded / unloaded" -- I was wondering if this
is intentional? If not, would it make sense to also bump next_mod_seq
in the unload path?
Quoted 63 lines
ALLOCINFO_IOC_GET_AT- gets the record at the specified position. This is the position of a record in/proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT- gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.Signed-off-by Suren Baghdasaryan <[email protected]>Signed-off-by Abhishek Bapat <[email protected]>--- .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 1 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 54 +++++ lib/alloc_tag.c | 193 +++++++++++++++++- lib/codetag.c | 11 + 6 files changed, 260 insertions(+), 2 deletions(-)create mode 100644
include/uapi/linux/alloc_tag.hDocumentation/userspace-api/ioctl/ioctl-number.rst +2
-0@@ -349,6 +349,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver <mailto:[email protected]> +0xA6 00-0F uapi/linux/alloc_tag.h Memory allocation profiling + <mailto:[email protected]> 0xAA 00-3F linux/uapi/linux/userfaultfd.h 0xAB 00-1F linux/nbd.h 0xAC 00-1F linux/raw.hMAINTAINERS +1
-0@@ -16709,6 +16709,7 @@ S: Maintained F: Documentation/mm/allocation-profiling.rst F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h +F: include/uapi/linux/alloc_tag.h F: lib/alloc_tag.c MEMORY CONTROLLER DRIVERSinclude/linux/codetag.h +1
-0@@ -76,6 +76,7 @@ struct codetag_iterator {void codetag_lock_module_list (struct codetag_type *cttype,bool lock);bool codetag_trylock_module_list (struct codetag_type *cttype); +unsigned long codetag_get_content_id (struct codetag_type *cttype);struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype);struct codetag *codetag_next_ct (struct codetag_iterator *iter);include/uapi/linux/alloc_tag.h new file +0
-0
include/uapi/linux/alloc_tag.h line 3
Quoted 3 lines
include/uapi/linux/alloc_tag.h new file +3
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* include/linux/alloc_tag.h
nit: it should be include/uapi/linux/alloc_tag.h
Quoted 164 lines
include/uapi/linux/alloc_tag.h new file +51
-0+*/ + +#ifndef _UAPI_ALLOC_TAG_H +#define _UAPI_ALLOC_TAG_H + +#include <linux/types.h> + +#define ALLOCINFO_STR_SIZE 64 + +struct allocinfo_content_id { +__u64 id; +}; + +struct allocinfo_tag { +/* Longer names are trimmed */ +char modname[ALLOCINFO_STR_SIZE ]; +char function[ALLOCINFO_STR_SIZE ]; +char filename[ALLOCINFO_STR_SIZE ]; +__u64 lineno; +}; + +struct allocinfo_counter { +__u64 bytes; +__u64 calls; +__u8 accurate; +__u8 pad[7 ];/* Add alignment to not break the 32-bit compatible interface */ +}; + +struct allocinfo_tag_data { +struct allocinfo_tag tag; +struct allocinfo_counter counter; +}; + +struct allocinfo_get_at { +__u64 pos;/* input */ +struct allocinfo_tag_data data; +}; + +#define _ALLOCINFO_IOC_CONTENT_ID 0 +#define _ALLOCINFO_IOC_GET_AT 1 +#define _ALLOCINFO_IOC_GET_NEXT 2 + +#define ALLOCINFO_IOC_BASE 0xA6 +#define ALLOCINFO_IOC_CONTENT_ID _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID, \ + struct allocinfo_content_id) +#define ALLOCINFO_IOC_GET_AT _IOWR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_AT, \ + struct allocinfo_get_at) +#define ALLOCINFO_IOC_GET_NEXT _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT, \ + struct allocinfo_tag_data) + +#endif /* _UAPI_ALLOC_TAG_H */ lib/alloc_tag.c +84
-0@@ -5,6 +5,7 @@#include <linux/gfp.h> #include <linux/kallsyms.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/page_ext.h> #include <linux/pgalloc_tag.h> #include <linux/proc_fs.h> @@ -14,6 +15,7 @@#include <linux/string_choices.h> #include <linux/vmalloc.h> #include <linux/kmemleak.h> +#include <uapi/linux/alloc_tag.h> #define ALLOCINFO_FILE_NAME "allocinfo"#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))@@ -46,6 +48,10 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +/* ioctl uses a separate iterator not to interfere with reads */ +struct codetag_iterator ioctl_iter; +bool positioned;/* seq_open_private() sets to 0 */ +struct mutex ioctl_lock; };static void *allocinfo_start (struct seq_file *m,loff_t *pos)@@ -125,6 +131,190 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +static int allocinfo_open (struct inode *inode,struct file *file) +{ +int ret; + + ret =seq_open_private (file, &allocinfo_seq_op, +sizeof (struct allocinfo_private )); +if (!ret) { +struct seq_file *m = file->private_data; +struct allocinfo_private *priv = m->private; + +mutex_init (&priv->ioctl_lock); + } +return ret; +} + +static int allocinfo_release (struct inode *inode,struct file *file) +{ +return seq_release_private (inode, file); +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +/* Keep an extra space for the trailing NULL. */ +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +/* Copy a string and trim from the beginning if it's too long */ +static void allocinfo_copy_str (char *dest,const char *src) +{ +strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); +} + +static void allocinfo_to_params (struct codetag *ct, +struct allocinfo_tag_data *data) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); + +if (ct->modname) +allocinfo_copy_str (data->tag.modname, ct->modname); +else + data->tag.modname[0 ] ='\0' ; +allocinfo_copy_str (data->tag.function, ct->function); +allocinfo_copy_str (data->tag.filename, ct->filename); + data->tag.lineno = ct->lineno; + data->counter.bytes = counter.bytes; + data->counter.calls = counter.calls; + data->counter.accurate = !alloc_tag_is_inaccurate (tag); +} + +static int allocinfo_ioctl_get_content_id (struct seq_file *m,void __user *arg) +{ +struct allocinfo_content_id params; + +codetag_lock_module_list (alloc_tag_cttype, true); + params.id =codetag_get_content_id (alloc_tag_cttype); +codetag_lock_module_list (alloc_tag_cttype, false); +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +__u64 pos; +struct allocinfo_get_at params = {0 }; + +if (copy_from_user (¶ms, arg,sizeof (params))) +return -EFAULT ; + + priv = (struct allocinfo_private *)m->private;
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 221
Quoted 10 lines
lib/alloc_tag.c +10
-0+ pos = params.pos; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +/* Find the codetag */ + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && pos--) + ct =codetag_next_ct (&priv->ioctl_iter);
No upper bound check on pos in ALLOCINFO_IOC_GET_AT:
pos comes straight from userspace (__u64) with no validation.
If the system has 10000 tags and someone passes pos=10001,
the loop will still walk all 10000 tags just to return ENOENT
-- all while holding ioctl_lock and mod_lock. It might be worth
checking pos against the total tag count early. struct codetag_type
is not exposed outside codetag.c though, so this would need a small helper.
Thanks
Best Regards
Hao
Quoted 131 lines
lib/alloc_tag.c +97
-2+if (ct) { +allocinfo_to_params (ct, ¶ms.data); + priv->positioned = true; + } + +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (!ct) +return -ENOENT ; + +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_next (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +struct allocinfo_tag_data params = {0 }; +int ret =0 ; + + priv = (struct allocinfo_private *)m->private; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +if (!priv->positioned) { + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + priv->positioned = true; + } + + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) +allocinfo_to_params (ct, ¶ms); + +if (!ct) { + priv->positioned = false; + ret = -ENOENT ; + } +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (ret ==0 ) { +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + } +return ret; +} + +static long allocinfo_ioctl (struct file *file,unsigned int cmd, +unsigned long __arg) +{ +void __user *arg = (void __user *)__arg; +int ret; + +switch (cmd) { +case ALLOCINFO_IOC_CONTENT_ID : + ret =allocinfo_ioctl_get_content_id (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_AT : + ret =allocinfo_ioctl_get_at (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_NEXT : + ret =allocinfo_ioctl_get_next (file->private_data, arg); +break ; +default : + ret = -ENOIOCTLCMD ; +break ; + } + +return ret; +} + +#ifdef CONFIG_COMPAT +static long allocinfo_compat_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +return allocinfo_ioctl (file, cmd, (unsigned long )compat_ptr (arg)); +} +#endif + +static const struct proc_ops allocinfo_proc_ops = { + .proc_open = allocinfo_open, + .proc_read_iter = seq_read_iter, + .proc_lseek = seq_lseek, + .proc_release = allocinfo_release, + .proc_ioctl = allocinfo_ioctl, +#ifdef CONFIG_COMPAT + .proc_compat_ioctl = allocinfo_compat_ioctl, +#endif + +}; +size_t alloc_tag_top_users (struct codetag_bytes *tags,size_t count,bool can_sleep) {struct codetag_iterator iter;@@ -989,8 +1179,7 @@ static int __init alloc_tag_init(void)return 0 ; } -if (!proc_create_seq_private (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_seq_op, -sizeof (struct allocinfo_private ),NULL )) { +if (!proc_create (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_proc_ops)) {pr_err ("Failed to create %s file\n" ,ALLOCINFO_FILE_NAME );shutdown_mem_profiling (false);return -ENOMEM ;lib/codetag.c +11
-0@@ -48,6 +48,17 @@ bool codetag_trylock_module_list(struct codetag_type *cttype)return down_read_trylock (&cttype->mod_lock) !=0 ; } +unsigned long codetag_get_content_id (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +/* +* next_mod_seq is updated on every load, so can be used to identify +* content changes. +*/ +return cttype->next_mod_seq; +} +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) {struct codetag_iterator iter = {
Hao Ge
reviewerRe: [PATCH v2 2/6] alloc_tag: add ioctl filters to /proc/allocinfo
In reply to Abhishek BapatHi Abhishek
Quoted 77 lines · Abhishek Bapat
Extend the capability of the IOCTL mechanism to filter allocations based on tag's module name, function name, file name and line number.
Signed-off-by Abhishek Bapat <[email protected]>--- include/uapi/linux/alloc_tag.h | 26 ++++++++++++++- lib/alloc_tag.c | 58 ++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-)include/uapi/linux/alloc_tag.h +25
-1@@ -34,8 +34,32 @@ struct allocinfo_tag_data {struct allocinfo_counter counter; }; +enum { +ALLOCINFO_FILTER_MODNAME , +ALLOCINFO_FILTER_FUNCTION , +ALLOCINFO_FILTER_FILENAME , +ALLOCINFO_FILTER_LINENO , + __ALLOCINFO_FILTER_LAST =ALLOCINFO_FILTER_LINENO +}; + +#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME) +#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION) +#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME) +#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) + +#define ALLOCINFO_FILTER_MASKS \ + ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1) + +struct allocinfo_filter { +__u64 mask;/* bitmask of the filter fields used */ +struct allocinfo_tag fields; +}; +struct allocinfo_get_at { -__u64 pos;/* input */ +/* inputs */ +__u64 pos; +struct allocinfo_filter filter; +/* output */ struct allocinfo_tag_data data; };lib/alloc_tag.c +8
-0@@ -48,6 +48,7 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +struct allocinfo_filter filter;/* ioctl uses a separate iterator not to interfere with reads */ struct codetag_iterator ioctl_iter;bool positioned;/* seq_open_private() sets to 0 */ @@ -167,6 +168,11 @@ static void allocinfo_copy_str(char *dest, const char *src)strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); } +static int allocinfo_cmp_str (const char *str,const char *template) +{ +return strncmp (allocinfo_str (str), template,ALLOCINFO_STR_SIZE ); +} +static void allocinfo_to_params (struct codetag *ct,struct allocinfo_tag_data *data) {@@ -198,27 +204,71 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)return 0 ; } +static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter) +{
lib/alloc_tag.c static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg) · line 218
Quoted 10 lines
lib/alloc_tag.c +10
-0+if (!filter || !filter->mask) +return true; + +if (filter->mask &ALLOCINFO_FILTER_MASK_MODNAME ) { +if (!ct->modname) +return false; +if (allocinfo_cmp_str (ct->modname, filter->fields.modname)) +return false; + } +
Apologies -- I previously suggested the "!ct->modname ->
return false" approach, but I realized we broke the ability to
filter for built-in allocations. allocinfo_to_params() returns
modname="" for those, so a user would naturally try to filter
by modname="" -- except it never matches because ct->modname is
NULL, not an empty string.
Maybe something like this instead?
if (filter->mask & ALLOCINFO_FILTER_MASK_MODNAME) {
if (ct->modname) {
if (allocinfo_cmp_str(ct->modname, filter->fields.modname))
return false;
} else if (filter->fields.modname[0] != '\0') {
return false;
}
}
That way modname="" matches built-in tags, which lines up with
what the API actually returns.
Thanks
Hao
Quoted 68 lines
lib/alloc_tag.c +37
-3+if ((filter->mask &ALLOCINFO_FILTER_MASK_FUNCTION ) && + ct->function && (allocinfo_cmp_str (ct->function, filter->fields.function))) +return false; + +if ((filter->mask &ALLOCINFO_FILTER_MASK_FILENAME ) && + ct->filename && (allocinfo_cmp_str (ct->filename, filter->fields.filename))) +return false; + +if ((filter->mask &ALLOCINFO_FILTER_MASK_LINENO ) && + ct->lineno != filter->fields.lineno) +return false; + +return true; +} +static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) {struct allocinfo_private *priv;struct codetag *ct; -__u64 pos;struct allocinfo_get_at params = {0 }; +__u64 skip_count;if (copy_from_user (¶ms, arg,sizeof (params)))return -EFAULT ; +if (params.filter.mask & ~ALLOCINFO_FILTER_MASKS ) +return -EINVAL ; + priv = (struct allocinfo_private *)m->private; - pos = params.pos; + + skip_count = params.pos;mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true); +if (params.filter.mask) + priv->filter = params.filter; +else + priv->filter.mask =0 ; +/* Find the codetag */ priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); ct =codetag_next_ct (&priv->ioctl_iter); -while (ct && pos--) + +while (ct) { +if (matches_filter (ct, &priv->filter)) { +if (skip_count ==0 ) +break ; + skip_count--; + } ct =codetag_next_ct (&priv->ioctl_iter); + } +if (ct) {allocinfo_to_params (ct, ¶ms.data); priv->positioned = true;@@ -254,6 +304,8 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg) } ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && !matches_filter (ct, &priv->filter)) + ct =codetag_next_ct (&priv->ioctl_iter);if (ct)allocinfo_to_params (ct, ¶ms);
Hao Ge
reviewerHi Andrew and Suren
Quoted 59 lines · Andrew Morton
Quoted 29 lines · On Fri, 22 May 2026 17:45:32 +0000 Abhishek Bapat <[email protected]>…
Currently, memory allocation profiling data is primarily exposed through
/proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (
alloc_tag_read) for any tags that fail the initial string or location filters.Scenario 1: Specific File Filtering (
arch/x86/events/rapl.c)
1. Traditional (cat/proc/allocinfo| grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)Quoted 7 lines
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)Scenario 3: Size-Based Filtering (
min_size= 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)Yup, textual interfaces aren't fast.
And ioctl-baed interfaces aren't popular. One would prefer to see an interface which uses read()/lseek(),
pread(), etc. It would be appropriate for this [0/N] to have a discussion of why that approach was not chosen.Quoted 9 lines
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++ lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-)Quoted 3 lines
create mode 100644
include/uapi/linux/alloc_tag.h
create mode 100644tools/testing/selftests/alloc_tag/Makefilecreate mode 100644tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.cAt some point this should grow user-facing documentation, please.
Quoted 7 lines
And the right time for that is now, because such documentation is useful for code review - it makes that review both easier and more useful.
Sashiko had a few things to say:
https://sashiko.dev/#/patchset/[email protected]
I notice that Sashiko has reported a pre-existing issue, as described below:
Quoted 1 line
static void *allocinfo_start(struct
seq_file*m,loff_t*pos)
This is a pre-existing issue, but can resuming a sequential read on /proc/allocinfo cause a use-after-free if a kernel module is unloaded between read() system calls?
The seq_file read operation updates priv->iter.ct during allocinfo_next(), stops iteration, and returns to userspace. If the module containing priv->iter.ct is unloaded while the lock is dropped, the module's codetag memory is freed.
On the next read() system call, allocinfo_start() with pos > 0 reacquires the lock but returns priv without validating if priv->iter.ct still belongs to a valid module. Does allocinfo_show() then dereference this dangling pointer?
[ ... ]
This issue is unrelated to the current patch series and can be resolved
by reverting commit 9f44df50fee4.
Therefore, I have submitted a separate patch addressing this issue,
which is available at the link below:
https://lore.kernel.org/all/[email protected]/
Thanks
Best Regards
Hao
Hao Ge
reviewerRe: [PATCH v2 3/6] alloc_tag: add size-based filtering to ioctl
In reply to Abhishek BapatHi Abhishek
Quoted 144 lines · Abhishek Bapat
Extend the allocinfo filtering mechanism to allow users to filter tags based on the total number of bytes allocated [
min_size,max_size]. The size range is inclusive.Filtering by size involves retrieving allocinfo per-CPU counters, which is an expensive operation. Hence, the performance of size-based filtering will be worse than other filters.
Signed-off-by Abhishek Bapat <[email protected]>--- include/uapi/linux/alloc_tag.h | 8 +++- lib/alloc_tag.c | 72 ++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 12 deletions(-)include/uapi/linux/alloc_tag.h +7
-1@@ -39,13 +39,17 @@ enum {ALLOCINFO_FILTER_FUNCTION ,ALLOCINFO_FILTER_FILENAME ,ALLOCINFO_FILTER_LINENO , - __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_LINENO +ALLOCINFO_FILTER_MIN_SIZE , +ALLOCINFO_FILTER_MAX_SIZE , + __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_MAX_SIZE };#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME)#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION)#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME)#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) +#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_MASKS \ ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1)@@ -53,6 +57,8 @@ enum {struct allocinfo_filter {__u64 mask;/* bitmask of the filter fields used */ struct allocinfo_tag fields; +__u64 min_size; +__u64 max_size; };struct allocinfo_get_at {lib/alloc_tag.c +39
-7@@ -173,11 +173,21 @@ static int allocinfo_cmp_str(const char *str, const char *template)return strncmp (allocinfo_str (str), template,ALLOCINFO_STR_SIZE ); } +static inline struct alloc_tag_counters allocinfo_prefetch_counters (struct codetag *ct) +{ +return alloc_tag_read (ct_to_alloc_tag (ct)); +} +static void allocinfo_to_params (struct codetag *ct, -struct allocinfo_tag_data *data) +struct allocinfo_tag_data *data, +struct alloc_tag_counters *counters) { -struct alloc_tag *tag =ct_to_alloc_tag (ct); -struct alloc_tag_counters counter =alloc_tag_read (tag); +struct alloc_tag_counters local_counters; + +if (!counters) { + local_counters =allocinfo_prefetch_counters (ct); + counters = &local_counters; + }if (ct->modname)allocinfo_copy_str (data->tag.modname, ct->modname);@@ -186,9 +196,9 @@ static void allocinfo_to_params(struct codetag *ct,allocinfo_copy_str (data->tag.function, ct->function);allocinfo_copy_str (data->tag.filename, ct->filename); data->tag.lineno = ct->lineno; - data->counter.bytes = counter.bytes; - data->counter.calls = counter.calls; - data->counter.accurate = !alloc_tag_is_inaccurate (tag); + data->counter.bytes = counters->bytes; + data->counter.calls = counters->calls; + data->counter.accurate = !alloc_tag_is_inaccurate (ct_to_alloc_tag (ct)); }static int allocinfo_ioctl_get_content_id (struct seq_file *m,void __user *arg)@@ -204,7 +214,8 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)return 0 ; } -static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter) +static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter, +struct alloc_tag_counters *counters) {if (!filter || !filter->mask)return true;@@ -228,6 +239,17 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter) ct->lineno != filter->fields.lineno)return false; +if ((filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) || + (filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE )) { +/* We assume counters is not NULL here as per caller logic */ +if ((filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) && + counters->bytes < filter->min_size) +return false; +if ((filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) && + counters->bytes > filter->max_size) +return false; + } +return true; }@@ -237,6 +259,9 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg)struct codetag *ct;struct allocinfo_get_at params = {0 };__u64 skip_count; +bool sizes_set; +struct alloc_tag_counters counters; +struct alloc_tag_counters *counters_ptr =NULL ;if (copy_from_user (¶ms, arg,sizeof (params)))return -EFAULT ;@@ -244,9 +269,16 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg)if (params.filter.mask & ~ALLOCINFO_FILTER_MASKS )return -EINVAL ; +if ((params.filter.mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) && + (params.filter.mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) && + params.filter.min_size > params.filter.max_size) +return -EINVAL ; + priv = (struct allocinfo_private *)m->private; skip_count = params.pos; + sizes_set = (params.filter.mask & + (ALLOCINFO_FILTER_MASK_MIN_SIZE |ALLOCINFO_FILTER_MASK_MAX_SIZE ));mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true);
lib/alloc_tag.c static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) · line 300
Quoted 9 lines
lib/alloc_tag.c +5
-1@@ -261,7 +293,11 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) ct =codetag_next_ct (&priv->ioctl_iter);while (ct) { -if (matches_filter (ct, &priv->filter)) { +if (sizes_set) { + counters =allocinfo_prefetch_counters (ct); + counters_ptr = &counters; + } +if (matches_filter (ct, &priv->filter, counters_ptr)) {
alloc_tag_read() walks all per-CPU counters which is not cheap, but here
it's called for every codetag unconditionally when sizes_set is true,
even when the tag would be rejected by modname/function/filename checks
that are plain string comparisons.
For example, say the user filters with MODNAME | MIN_SIZE on a system
with 10000 tags, 100 of which belong to the target module. Today the
code would call alloc_tag_read() 10000 times (once per tag), but only
100 of those tags pass the modname check — the other 9900 per-CPU walks
are wasted.
Would it make sense to split the filter check so that per-CPU counter reads
only happen after tag-based checks pass? Something like:
static bool allocinfo_match_tag(struct codetag *ct,
struct allocinfo_filter *filter) { ... }
static bool allocinfo_match_size(struct alloc_tag_counters *counters,
struct allocinfo_filter *filter) { ... }
And in the caller:
bool match = allocinfo_match_tag(ct, &priv->filter);
/* Add comments to help subsequent developers understand the purpose of this modification. */
if (match && sizes_set) {
counters = allocinfo_prefetch_counters(ct);
counters_ptr = &counters;
match = allocinfo_match_size(counters_ptr, &priv->filter);
}
You may find a more elegant approach to resolve this issue.
Thanks
Best Regards
Hao
Quoted 49 lines
lib/alloc_tag.c +17
-3if (skip_count ==0 )break ; skip_count--;@@ -270,7 +306,7 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) }if (ct) { -allocinfo_to_params (ct, ¶ms.data); +allocinfo_to_params (ct, ¶ms.data, counters_ptr); priv->positioned = true; }@@ -292,9 +328,15 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg)struct codetag *ct;struct allocinfo_tag_data params = {0 };int ret =0 ; +bool sizes_set; +struct alloc_tag_counters counters; +struct alloc_tag_counters *counters_ptr =NULL ; priv = (struct allocinfo_private *)m->private; + sizes_set = (priv->filter.mask & + (ALLOCINFO_FILTER_MASK_MIN_SIZE |ALLOCINFO_FILTER_MASK_MAX_SIZE )); +mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true);@@ -304,10 +346,18 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg) } ct =codetag_next_ct (&priv->ioctl_iter); -while (ct && !matches_filter (ct, &priv->filter)) +while (ct) { +if (sizes_set) { + counters =allocinfo_prefetch_counters (ct); + counters_ptr = &counters; + } +if (matches_filter (ct, &priv->filter, counters_ptr)) +break ; ct =codetag_next_ct (&priv->ioctl_iter); + } +if (ct) -allocinfo_to_params (ct, ¶ms); +allocinfo_to_params (ct, ¶ms, counters_ptr);if (!ct) { priv->positioned = false;
Suren Baghdasaryan
maintainerQuoted 40 lines · Andrew Morton
Quoted 29 lines · On Fri, 22 May 2026 17:45:32 +0000 Abhishek Bapat <[email protected]>…
Currently, memory allocation profiling data is primarily exposed through
/proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (
alloc_tag_read) for any tags that fail the initial string or location filters.Scenario 1: Specific File Filtering (
arch/x86/events/rapl.c)
1. Traditional (cat/proc/allocinfo| grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)Quoted 7 lines
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)Scenario 3: Size-Based Filtering (
min_size= 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)
Quoted 6 lines
Yup, textual interfaces aren't fast.
And ioctl-baed interfaces aren't popular. One would prefer to see an interface which uses read()/lseek(),
pread(), etc. It would be appropriate for this [0/N] to have a discussion of why that approach was not chosen.
We chose ioctl because it allows us to filter data without aggregating the per-CPU counters, which is the main overhead when reading this file. That's why we can achieve 20x performance improvement, provided we do not filter based on the allocation size.
Aside from that, I plan on introducing an additional ioctl command to enable context capture for specific allocations.
Quoted 14 lines
Quoted 13 lines
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++ lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/alloc_tag.h create mode 100644 tools/testing/selftests/alloc_tag/Makefile create mode 100644 tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c
Quoted 5 lines
At some point this should grow user-facing documentation, please.
And the right time for that is now, because such documentation is useful for code review - it makes that review both easier and more useful.
Ack. I believe Abhishek is working on that.
Quoted 4 lines
Sashiko had a few things to say:
https://sashiko.dev/#/patchset/[email protected]
Ack.
Suren Baghdasaryan
maintainerQuoted 91 lines · Hao Ge
Hi Andrew and Suren
Quoted 59 lines · Andrew Morton
Quoted 29 lines · On Fri, 22 May 2026 17:45:32 +0000 Abhishek Bapat <[email protected]>…
Currently, memory allocation profiling data is primarily exposed through
/proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (
alloc_tag_read) for any tags that fail the initial string or location filters.Scenario 1: Specific File Filtering (
arch/x86/events/rapl.c)
1. Traditional (cat/proc/allocinfo| grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)Quoted 7 lines
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)Scenario 3: Size-Based Filtering (
min_size= 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)Yup, textual interfaces aren't fast.
And ioctl-baed interfaces aren't popular. One would prefer to see an interface which uses read()/lseek(),
pread(), etc. It would be appropriate for this [0/N] to have a discussion of why that approach was not chosen.Quoted 9 lines
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++ lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-)Quoted 3 lines
create mode 100644
include/uapi/linux/alloc_tag.h
create mode 100644tools/testing/selftests/alloc_tag/Makefilecreate mode 100644tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.cAt some point this should grow user-facing documentation, please.
Quoted 7 lines
And the right time for that is now, because such documentation is useful for code review - it makes that review both easier and more useful.
Sashiko had a few things to say:
https://sashiko.dev/#/patchset/[email protected]I notice that Sashiko has reported a pre-existing issue, as described below:
Quoted 1 line
static void *allocinfo_start(struct
seq_file*m,loff_t*pos)This is a pre-existing issue, but can resuming a sequential read on
/proc/allocinfocause a use-after-free if a kernel module is unloaded betweenread()system calls?
Theseq_fileread operation updatespriv->iter.ctduringallocinfo_next(), stops iteration, and returns to userspace. If the module containingpriv->iter.ctis unloaded while the lock is dropped, the module's codetag memory is freed.
On the nextread()system call,allocinfo_start()with pos > 0 reacquires the lock but returns priv without validating ifpriv->iter.ctstill belongs to a valid module. Doesallocinfo_show()then dereference this dangling pointer?
[ ... ]This issue is unrelated to the current patch series and can be resolved
Quoted 7 lines
by reverting commit
9f44df50fee4.Therefore, I have submitted a separate patch addressing this issue,
which is available at the link below:
Thanks Hao! I commented on your patch, please take a look. I think there is a better fix.
Quoted 7 lines
Thanks
Best Regards
Hao
Suren Baghdasaryan
maintainerRe: [PATCH v2 1/6] alloc_tag: add ioctl to /proc/allocinfo
In reply to Andrew MortonQuoted 57 lines · Andrew Morton
Quoted 41 lines · On Fri, 22 May 2026 17:45:33 +0000 Abhishek Bapat <[email protected]>…
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for
/proc/allocinfofile:
ALLOCINFO_IOC_CONTENT_ID- gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
ALLOCINFO_IOC_GET_AT- gets the record at the specified position. This is the position of a record in/proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT- gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.index 000000000000..e9a5b55fcc7a
include/uapi/linux/alloc_tag.h new file +19
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* include/linux/alloc_tag.h +*/ + +#ifndef _UAPI_ALLOC_TAG_H +#define _UAPI_ALLOC_TAG_H + +#include <linux/types.h> + +#define ALLOCINFO_STR_SIZE 64 + +struct allocinfo_content_id { + __u64 id; +}; + +struct allocinfo_tag { + /* Longer names are trimmed */ + char modname[ALLOCINFO_STR_SIZE];include/uapi/linux/alloc_tag.h line 29
Quoted 10 lines
include/uapi/linux/alloc_tag.h new file +10
-0+char function[ALLOCINFO_STR_SIZE ]; +char filename[ALLOCINFO_STR_SIZE ]; +__u64 lineno; +}; + +struct allocinfo_counter { +__u64 bytes; +__u64 calls; +__u8 accurate; +__u8 pad[7 ];/* Add alignment to not break the 32-bit compatible interface */ This seems rather fragile, and makes assumptions about compiler layout?
Quoted 1 line
Can't we use __attribute__((aligned)) in some fashion?
Ack. I think we can.
Quoted 70 lines
Quoted 70 lines
include/uapi/linux/alloc_tag.h new file +25
-0+}; + +struct allocinfo_tag_data { +struct allocinfo_tag tag; +struct allocinfo_counter counter; +}; + +struct allocinfo_get_at { +__u64 pos;/* input */ +struct allocinfo_tag_data data; +}; + +#define _ALLOCINFO_IOC_CONTENT_ID 0 +#define _ALLOCINFO_IOC_GET_AT 1 +#define _ALLOCINFO_IOC_GET_NEXT 2 + +#define ALLOCINFO_IOC_BASE 0xA6 +#define ALLOCINFO_IOC_CONTENT_ID _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID, \ + struct allocinfo_content_id) +#define ALLOCINFO_IOC_GET_AT _IOWR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_AT, \ + struct allocinfo_get_at) +#define ALLOCINFO_IOC_GET_NEXT _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT, \ + struct allocinfo_tag_data) + +#endif /* _UAPI_ALLOC_TAG_H */ lib/alloc_tag.c +15
-0@@ -5,6 +5,7 @@#include <linux/gfp.h> #include <linux/kallsyms.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/page_ext.h> #include <linux/pgalloc_tag.h> #include <linux/proc_fs.h> @@ -14,6 +15,7 @@#include <linux/string_choices.h> #include <linux/vmalloc.h> #include <linux/kmemleak.h> +#include <uapi/linux/alloc_tag.h> #define ALLOCINFO_FILE_NAME "allocinfo"#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))@@ -46,6 +48,10 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +/* ioctl uses a separate iterator not to interfere with reads */ +struct codetag_iterator ioctl_iter; +bool positioned;/* seq_open_private() sets to 0 */ +struct mutex ioctl_lock; };static void *allocinfo_start (struct seq_file *m,loff_t *pos)@@ -125,6 +131,190 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +static int allocinfo_open (struct inode *inode,struct file *file) +{ +int ret; + + ret =seq_open_private (file, &allocinfo_seq_op, +sizeof (struct allocinfo_private )); +if (!ret) { +struct seq_file *m = file->private_data; +struct allocinfo_private *priv = m->private;
Quoted 8 lines
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 147
Quoted 5 lines
lib/alloc_tag.c +5
-0+ +mutex_init (&priv->ioctl_lock); + } +return ret; +}Generally, the commenting in here is very thin. Add some explanations of what the various functions do and, especially, why they do it?
Ack. Will add.
Quoted 18 lines
Quoted 18 lines
lib/alloc_tag.c +17
-0+static int allocinfo_release (struct inode *inode,struct file *file) +{ +return seq_release_private (inode, file); +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +/* Keep an extra space for the trailing NULL. */ +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +/* Copy a string and trim from the beginning if it's too long */ +static void allocinfo_copy_str (char *dest,const char *src)
Quoted 8 lines
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 168
Quoted 3 lines
lib/alloc_tag.c +3
-0+{ +strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); +}See, even these two little functions are unnecessarily difficult to review when one doesn"t know what they are setting out to do. One has to first reverse engineer their intent from the implementation, then check that the implementation implements that intent.
Ack.
Quoted 14 lines
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 211
Quoted 12 lines
lib/alloc_tag.c +11
-0+static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +__u64 pos; +struct allocinfo_get_at params = {0 }; + +if (copy_from_user (¶ms, arg,sizeof (params))) +return -EFAULT ; + + priv = (struct allocinfo_private *)m->private;Unneeded cast.
Ack.
Quoted 30 lines
Quoted 30 lines
lib/alloc_tag.c +29
-0+ pos = params.pos; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +/* Find the codetag */ + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && pos--) + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) { +allocinfo_to_params (ct, ¶ms.data); + priv->positioned = true; + } + +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (!ct) +return -ENOENT ; + +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_next (struct seq_file *m,void __user *arg) +{
Quoted 8 lines
lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 246
Quoted 6 lines
lib/alloc_tag.c +6
-0+struct allocinfo_private *priv; +struct codetag *ct; +struct allocinfo_tag_data params = {0 }; +int ret =0 ; + + priv = (struct allocinfo_private *)m->private;Ditto.
Ack.
Quoted 29 lines
Quoted 29 lines
lib/alloc_tag.c +25
-0+mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +if (!priv->positioned) { + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + priv->positioned = true; + } + + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) +allocinfo_to_params (ct, ¶ms); + +if (!ct) { + priv->positioned = false; + ret = -ENOENT ; + } +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (ret ==0 ) { +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + } +return ret; +}
Suren Baghdasaryan
maintainerRe: [PATCH v2 1/6] alloc_tag: add ioctl to /proc/allocinfo
In reply to Hao GeQuoted 45 lines · Hao Ge
Hi Abhishek
Thanks for this patch. I had a few questions/comments after going
through it.
Quoted 10 lines · Abhishek Bapat
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for
/proc/allocinfofile:
ALLOCINFO_IOC_CONTENT_ID- gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
codetag_get_content_id()does not reflect module unload
codetag_get_content_id()returnscttype->next_mod_seq:unsigned long
codetag_get_content_id(struct codetag_type *cttype){
return cttype->next_mod_seq;}
However,
next_mod_seqis only bumped incodetag_module_init(),i.e.the module load path:
https://elixir.bootlin.com/linux/v7.1-rc4/source/lib/codetag.c#L204
codetag_unload_module()does not incrementnext_mod_seq. This meansthat if only a module unload happens (without a subsequent load),
content_idstays the same, so users comparing the id before and after
Quoted 7 lines
won't detect that the content has changed. The commit message says
"Every time a module is loaded / unloaded" -- I was wondering if this
is intentional? If not, would it make sense to also bump
next_mod_seqin the unload path?
Good point. I overlooked that when I wrote the prototype for this patch. We should not bump next_mod_seq in the unload path but instead use a separate seq_count that gets bumped every time codetag_load_module/codetag_unload_module is called.
Quoted 250 lines
Quoted 65 lines
ALLOCINFO_IOC_GET_AT- gets the record at the specified position. This is the position of a record in/proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT- gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.Signed-off-by Suren Baghdasaryan <[email protected]>Signed-off-by Abhishek Bapat <[email protected]>--- .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 1 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 54 +++++ lib/alloc_tag.c | 193 +++++++++++++++++- lib/codetag.c | 11 + 6 files changed, 260 insertions(+), 2 deletions(-)create mode 100644
include/uapi/linux/alloc_tag.hDocumentation/userspace-api/ioctl/ioctl-number.rst +2
-0@@ -349,6 +349,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver <mailto:[email protected]> +0xA6 00-0F uapi/linux/alloc_tag.h Memory allocation profiling + <mailto:[email protected]> 0xAA 00-3F linux/uapi/linux/userfaultfd.h 0xAB 00-1F linux/nbd.h 0xAC 00-1F linux/raw.hMAINTAINERS +1
-0@@ -16709,6 +16709,7 @@ S: Maintained F: Documentation/mm/allocation-profiling.rst F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h +F: include/uapi/linux/alloc_tag.h F: lib/alloc_tag.c MEMORY CONTROLLER DRIVERSinclude/linux/codetag.h +1
-0@@ -76,6 +76,7 @@ struct codetag_iterator {void codetag_lock_module_list (struct codetag_type *cttype,bool lock);bool codetag_trylock_module_list (struct codetag_type *cttype); +unsigned long codetag_get_content_id (struct codetag_type *cttype);struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype);struct codetag *codetag_next_ct (struct codetag_iterator *iter);include/uapi/linux/alloc_tag.h new file +0
-0include/uapi/linux/alloc_tag.h line 3
Quoted 3 lines
include/uapi/linux/alloc_tag.h new file +3
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* include/linux/alloc_tag.h nit: it should be
include/uapi/linux/alloc_tag.hQuoted 164 lines
include/uapi/linux/alloc_tag.h new file +51
-0+*/ + +#ifndef _UAPI_ALLOC_TAG_H +#define _UAPI_ALLOC_TAG_H + +#include <linux/types.h> + +#define ALLOCINFO_STR_SIZE 64 + +struct allocinfo_content_id { +__u64 id; +}; + +struct allocinfo_tag { +/* Longer names are trimmed */ +char modname[ALLOCINFO_STR_SIZE ]; +char function[ALLOCINFO_STR_SIZE ]; +char filename[ALLOCINFO_STR_SIZE ]; +__u64 lineno; +}; + +struct allocinfo_counter { +__u64 bytes; +__u64 calls; +__u8 accurate; +__u8 pad[7 ];/* Add alignment to not break the 32-bit compatible interface */ +}; + +struct allocinfo_tag_data { +struct allocinfo_tag tag; +struct allocinfo_counter counter; +}; + +struct allocinfo_get_at { +__u64 pos;/* input */ +struct allocinfo_tag_data data; +}; + +#define _ALLOCINFO_IOC_CONTENT_ID 0 +#define _ALLOCINFO_IOC_GET_AT 1 +#define _ALLOCINFO_IOC_GET_NEXT 2 + +#define ALLOCINFO_IOC_BASE 0xA6 +#define ALLOCINFO_IOC_CONTENT_ID _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID, \ + struct allocinfo_content_id) +#define ALLOCINFO_IOC_GET_AT _IOWR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_AT, \ + struct allocinfo_get_at) +#define ALLOCINFO_IOC_GET_NEXT _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT, \ + struct allocinfo_tag_data) + +#endif /* _UAPI_ALLOC_TAG_H */ lib/alloc_tag.c +84
-0@@ -5,6 +5,7 @@#include <linux/gfp.h> #include <linux/kallsyms.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/page_ext.h> #include <linux/pgalloc_tag.h> #include <linux/proc_fs.h> @@ -14,6 +15,7 @@#include <linux/string_choices.h> #include <linux/vmalloc.h> #include <linux/kmemleak.h> +#include <uapi/linux/alloc_tag.h> #define ALLOCINFO_FILE_NAME "allocinfo"#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))@@ -46,6 +48,10 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +/* ioctl uses a separate iterator not to interfere with reads */ +struct codetag_iterator ioctl_iter; +bool positioned;/* seq_open_private() sets to 0 */ +struct mutex ioctl_lock; };static void *allocinfo_start (struct seq_file *m,loff_t *pos)@@ -125,6 +131,190 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +static int allocinfo_open (struct inode *inode,struct file *file) +{ +int ret; + + ret =seq_open_private (file, &allocinfo_seq_op, +sizeof (struct allocinfo_private )); +if (!ret) { +struct seq_file *m = file->private_data; +struct allocinfo_private *priv = m->private; + +mutex_init (&priv->ioctl_lock); + } +return ret; +} + +static int allocinfo_release (struct inode *inode,struct file *file) +{ +return seq_release_private (inode, file); +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +/* Keep an extra space for the trailing NULL. */ +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +/* Copy a string and trim from the beginning if it's too long */ +static void allocinfo_copy_str (char *dest,const char *src) +{ +strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); +} + +static void allocinfo_to_params (struct codetag *ct, +struct allocinfo_tag_data *data) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); + +if (ct->modname) +allocinfo_copy_str (data->tag.modname, ct->modname); +else + data->tag.modname[0 ] ='\0' ; +allocinfo_copy_str (data->tag.function, ct->function); +allocinfo_copy_str (data->tag.filename, ct->filename); + data->tag.lineno = ct->lineno; + data->counter.bytes = counter.bytes; + data->counter.calls = counter.calls; + data->counter.accurate = !alloc_tag_is_inaccurate (tag); +} + +static int allocinfo_ioctl_get_content_id (struct seq_file *m,void __user *arg) +{ +struct allocinfo_content_id params; + +codetag_lock_module_list (alloc_tag_cttype, true); + params.id =codetag_get_content_id (alloc_tag_cttype); +codetag_lock_module_list (alloc_tag_cttype, false); +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +__u64 pos; +struct allocinfo_get_at params = {0 }; + +if (copy_from_user (¶ms, arg,sizeof (params))) +return -EFAULT ; + + priv = (struct allocinfo_private *)m->private;lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 221
Quoted 10 lines
lib/alloc_tag.c +10
-0+ pos = params.pos; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +/* Find the codetag */ + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && pos--) + ct =codetag_next_ct (&priv->ioctl_iter);No upper bound check on pos in
ALLOCINFO_IOC_GET_AT:pos comes straight from userspace (
__u64) with no validation.If the system has 10000 tags and someone passes pos=10001,
Quoted 7 lines
the loop will still walk all 10000 tags just to return ENOENT
-- all while holding
ioctl_lockandmod_lock. It might be worthchecking pos against the total tag count early. struct
codetag_typeis not exposed outside
codetag.cthough, so this would need a small helper.
Ack.
Quoted 139 lines
Thanks
Best Regards
Hao
Quoted 131 lines
lib/alloc_tag.c +97
-2+if (ct) { +allocinfo_to_params (ct, ¶ms.data); + priv->positioned = true; + } + +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (!ct) +return -ENOENT ; + +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_next (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +struct allocinfo_tag_data params = {0 }; +int ret =0 ; + + priv = (struct allocinfo_private *)m->private; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +if (!priv->positioned) { + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + priv->positioned = true; + } + + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) +allocinfo_to_params (ct, ¶ms); + +if (!ct) { + priv->positioned = false; + ret = -ENOENT ; + } +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (ret ==0 ) { +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + } +return ret; +} + +static long allocinfo_ioctl (struct file *file,unsigned int cmd, +unsigned long __arg) +{ +void __user *arg = (void __user *)__arg; +int ret; + +switch (cmd) { +case ALLOCINFO_IOC_CONTENT_ID : + ret =allocinfo_ioctl_get_content_id (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_AT : + ret =allocinfo_ioctl_get_at (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_NEXT : + ret =allocinfo_ioctl_get_next (file->private_data, arg); +break ; +default : + ret = -ENOIOCTLCMD ; +break ; + } + +return ret; +} + +#ifdef CONFIG_COMPAT +static long allocinfo_compat_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +return allocinfo_ioctl (file, cmd, (unsigned long )compat_ptr (arg)); +} +#endif + +static const struct proc_ops allocinfo_proc_ops = { + .proc_open = allocinfo_open, + .proc_read_iter = seq_read_iter, + .proc_lseek = seq_lseek, + .proc_release = allocinfo_release, + .proc_ioctl = allocinfo_ioctl, +#ifdef CONFIG_COMPAT + .proc_compat_ioctl = allocinfo_compat_ioctl, +#endif + +}; +size_t alloc_tag_top_users (struct codetag_bytes *tags,size_t count,bool can_sleep) {struct codetag_iterator iter;@@ -989,8 +1179,7 @@ static int __init alloc_tag_init(void)return 0 ; } -if (!proc_create_seq_private (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_seq_op, -sizeof (struct allocinfo_private ),NULL )) { +if (!proc_create (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_proc_ops)) {pr_err ("Failed to create %s file\n" ,ALLOCINFO_FILE_NAME );shutdown_mem_profiling (false);return -ENOMEM ;lib/codetag.c +11
-0@@ -48,6 +48,17 @@ bool codetag_trylock_module_list(struct codetag_type *cttype)return down_read_trylock (&cttype->mod_lock) !=0 ; } +unsigned long codetag_get_content_id (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +/* +* next_mod_seq is updated on every load, so can be used to identify +* content changes. +*/ +return cttype->next_mod_seq; +} +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) {struct codetag_iterator iter = {
Suren Baghdasaryan
maintainerRe: [PATCH v2 3/6] alloc_tag: add size-based filtering to ioctl
In reply to Hao GeQuoted 204 lines · Hao Ge
Hi Abhishek
Quoted 144 lines · Abhishek Bapat
Extend the allocinfo filtering mechanism to allow users to filter tags based on the total number of bytes allocated [
min_size,max_size]. The size range is inclusive.Filtering by size involves retrieving allocinfo per-CPU counters, which is an expensive operation. Hence, the performance of size-based filtering will be worse than other filters.
Signed-off-by Abhishek Bapat <[email protected]>--- include/uapi/linux/alloc_tag.h | 8 +++- lib/alloc_tag.c | 72 ++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 12 deletions(-)include/uapi/linux/alloc_tag.h +7
-1@@ -39,13 +39,17 @@ enum {ALLOCINFO_FILTER_FUNCTION ,ALLOCINFO_FILTER_FILENAME ,ALLOCINFO_FILTER_LINENO , - __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_LINENO +ALLOCINFO_FILTER_MIN_SIZE , +ALLOCINFO_FILTER_MAX_SIZE , + __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_MAX_SIZE };#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME)#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION)#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME)#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) +#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_MASKS \ ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1)@@ -53,6 +57,8 @@ enum {struct allocinfo_filter {__u64 mask;/* bitmask of the filter fields used */ struct allocinfo_tag fields; +__u64 min_size; +__u64 max_size; };struct allocinfo_get_at {lib/alloc_tag.c +39
-7@@ -173,11 +173,21 @@ static int allocinfo_cmp_str(const char *str, const char *template)return strncmp (allocinfo_str (str), template,ALLOCINFO_STR_SIZE ); } +static inline struct alloc_tag_counters allocinfo_prefetch_counters (struct codetag *ct) +{ +return alloc_tag_read (ct_to_alloc_tag (ct)); +} +static void allocinfo_to_params (struct codetag *ct, -struct allocinfo_tag_data *data) +struct allocinfo_tag_data *data, +struct alloc_tag_counters *counters) { -struct alloc_tag *tag =ct_to_alloc_tag (ct); -struct alloc_tag_counters counter =alloc_tag_read (tag); +struct alloc_tag_counters local_counters; + +if (!counters) { + local_counters =allocinfo_prefetch_counters (ct); + counters = &local_counters; + }if (ct->modname)allocinfo_copy_str (data->tag.modname, ct->modname);@@ -186,9 +196,9 @@ static void allocinfo_to_params(struct codetag *ct,allocinfo_copy_str (data->tag.function, ct->function);allocinfo_copy_str (data->tag.filename, ct->filename); data->tag.lineno = ct->lineno; - data->counter.bytes = counter.bytes; - data->counter.calls = counter.calls; - data->counter.accurate = !alloc_tag_is_inaccurate (tag); + data->counter.bytes = counters->bytes; + data->counter.calls = counters->calls; + data->counter.accurate = !alloc_tag_is_inaccurate (ct_to_alloc_tag (ct)); }static int allocinfo_ioctl_get_content_id (struct seq_file *m,void __user *arg)@@ -204,7 +214,8 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)return 0 ; } -static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter) +static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter, +struct alloc_tag_counters *counters) {if (!filter || !filter->mask)return true;@@ -228,6 +239,17 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter) ct->lineno != filter->fields.lineno)return false; +if ((filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) || + (filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE )) { +/* We assume counters is not NULL here as per caller logic */ +if ((filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) && + counters->bytes < filter->min_size) +return false; +if ((filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) && + counters->bytes > filter->max_size) +return false; + } +return true; }@@ -237,6 +259,9 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg)struct codetag *ct;struct allocinfo_get_at params = {0 };__u64 skip_count; +bool sizes_set; +struct alloc_tag_counters counters; +struct alloc_tag_counters *counters_ptr =NULL ;if (copy_from_user (¶ms, arg,sizeof (params)))return -EFAULT ;@@ -244,9 +269,16 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg)if (params.filter.mask & ~ALLOCINFO_FILTER_MASKS )return -EINVAL ; +if ((params.filter.mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) && + (params.filter.mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) && + params.filter.min_size > params.filter.max_size) +return -EINVAL ; + priv = (struct allocinfo_private *)m->private; skip_count = params.pos; + sizes_set = (params.filter.mask & + (ALLOCINFO_FILTER_MASK_MIN_SIZE |ALLOCINFO_FILTER_MASK_MAX_SIZE ));mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true);lib/alloc_tag.c static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) · line 300
Quoted 9 lines
lib/alloc_tag.c +5
-1@@ -261,7 +293,11 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) ct =codetag_next_ct (&priv->ioctl_iter);while (ct) { -if (matches_filter (ct, &priv->filter)) { +if (sizes_set) { + counters =allocinfo_prefetch_counters (ct); + counters_ptr = &counters; + } +if (matches_filter (ct, &priv->filter, counters_ptr)) {
alloc_tag_read()walks all per-CPU counters which is not cheap, but hereit's called for every codetag unconditionally when
sizes_setis true,even when the tag would be rejected by modname/function/filename checks
that are plain string comparisons.
For example, say the user filters with MODNAME |
MIN_SIZEon a systemwith 10000 tags, 100 of which belong to the target module. Today the
code would call
alloc_tag_read()10000 times (once per tag), but only100 of those tags pass the modname check — the other 9900 per-CPU walks
are wasted.
Would it make sense to split the filter check so that per-CPU counter reads
only happen after tag-based checks pass? Something like:
static bool allocinfo_match_tag(struct codetag *ct,
struct allocinfo_filter *filter) { ... }static bool allocinfo_match_size(struct
alloc_tag_counters*counters,struct allocinfo_filter *filter) { ... }And in the caller:
bool match =
allocinfo_match_tag(ct, &priv->filter);/* Add comments to help subsequent developers understand the purpose of this modification. */
if (match &&
sizes_set) {counters = allocinfo_prefetch_counters(ct); counters_ptr = &counters; match = allocinfo_match_size(counters_ptr, &priv->filter);
Quoted 3 lines
}
You may find a more elegant approach to resolve this issue.
That's a good point. The counters should be fetched only after all other filters have passed their checks. Otherwise you lose most of the performance benefits.
Quoted 56 lines
Thanks
Best Regards
Hao
Quoted 49 lines
lib/alloc_tag.c +17
-3if (skip_count ==0 )break ; skip_count--;@@ -270,7 +306,7 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) }if (ct) { -allocinfo_to_params (ct, ¶ms.data); +allocinfo_to_params (ct, ¶ms.data, counters_ptr); priv->positioned = true; }@@ -292,9 +328,15 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg)struct codetag *ct;struct allocinfo_tag_data params = {0 };int ret =0 ; +bool sizes_set; +struct alloc_tag_counters counters; +struct alloc_tag_counters *counters_ptr =NULL ; priv = (struct allocinfo_private *)m->private; + sizes_set = (priv->filter.mask & + (ALLOCINFO_FILTER_MASK_MIN_SIZE |ALLOCINFO_FILTER_MASK_MAX_SIZE )); +mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true);@@ -304,10 +346,18 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg) } ct =codetag_next_ct (&priv->ioctl_iter); -while (ct && !matches_filter (ct, &priv->filter)) +while (ct) { +if (sizes_set) { + counters =allocinfo_prefetch_counters (ct); + counters_ptr = &counters; + } +if (matches_filter (ct, &priv->filter, counters_ptr)) +break ; ct =codetag_next_ct (&priv->ioctl_iter); + } +if (ct) -allocinfo_to_params (ct, ¶ms); +allocinfo_to_params (ct, ¶ms, counters_ptr);if (!ct) { priv->positioned = false;
Quoted 95 lines · Suren Baghdasaryan
Quoted 93 lines · Hao Ge
Hi Andrew and Suren
Quoted 59 lines · Andrew Morton
Quoted 29 lines · On Fri, 22 May 2026 17:45:32 +0000 Abhishek Bapat <[email protected]>…
Currently, memory allocation profiling data is primarily exposed through
/proc/allocinfo. While useful for manual inspection, this text-based interface poses challenges for production monitoring and large-scale analysis:1. Userspace must parse large amounts of text to extract specific fields.
2. To find specific tags, userspace must read the entire dataset, requiring many context switches and high data copying.
3. The kernel currently aggregates per-CPU counters for every allocation size, even those the user intends to filter out immediately.This series introduces a new IOCTL-based binary interface for allocinfo that supports kernel-side filtering. By allowing the user to specify a filter mask, we significantly reduce the work performed in-kernel and the amount of data transferred to userspace.
Performance measurements were conducted on an Intel Xeon Platinum 8481C (224 CPUs) with caches dropped before each run.
The IOCTL mechanism shows a ~20x performance improvement for filtered queries. The kernel avoids the expensive per-CPU counter aggregation (
alloc_tag_read) for any tags that fail the initial string or location filters.Scenario 1: Specific File Filtering (
arch/x86/events/rapl.c)
1. Traditional (cat/proc/allocinfo| grep): 22ms (sys)
2. IOCTL Interface: 1ms (sys)Quoted 7 lines
Scenario 2: Compound Filtering (Filename + Size)
1. Traditional: (cat ... | grep | awk): 21ms (sys)
2. IOCTL Interface: 1ms (sys)Scenario 3: Size-Based Filtering (
min_size= 1MB)
1. Traditional: (cat ... | awk): 21ms (sys)
2. IOCTL Interface: 14ms (sys)Yup, textual interfaces aren't fast.
And ioctl-baed interfaces aren't popular. One would prefer to see an interface which uses read()/lseek(),
pread(), etc. It would be appropriate for this [0/N] to have a discussion of why that approach was not chosen.Quoted 9 lines
.../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 87 +++ lib/alloc_tag.c | 303 ++++++++++- lib/codetag.c | 11 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 505 ++++++++++++++++++ 8 files changed, 918 insertions(+), 2 deletions(-)Quoted 3 lines
create mode 100644
include/uapi/linux/alloc_tag.h
create mode 100644tools/testing/selftests/alloc_tag/Makefilecreate mode 100644tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.cAt some point this should grow user-facing documentation, please.
Quoted 7 lines
And the right time for that is now, because such documentation is useful for code review - it makes that review both easier and more useful.
Sashiko had a few things to say:
https://sashiko.dev/#/patchset/[email protected]I notice that Sashiko has reported a pre-existing issue, as described below:
Quoted 1 line
static void *allocinfo_start(struct
seq_file*m,loff_t*pos)This is a pre-existing issue, but can resuming a sequential read on
/proc/allocinfocause a use-after-free if a kernel module is unloaded betweenread()system calls?
Theseq_fileread operation updatespriv->iter.ctduringallocinfo_next(), stops iteration, and returns to userspace. If the module containingpriv->iter.ctis unloaded while the lock is dropped, the module's codetag memory is freed.
On the nextread()system call,allocinfo_start()with pos > 0 reacquires the lock but returns priv without validating ifpriv->iter.ctstill belongs to a valid module. Doesallocinfo_show()then dereference this dangling pointer?
[ ... ]This issue is unrelated to the current patch series and can be resolved
by reverting commit
9f44df50fee4.
Quoted 16 lines
Quoted 5 lines
Therefore, I have submitted a separate patch addressing this issue,
which is available at the link below:
Thanks Hao! I commented on your patch, please take a look. I think there is a better fix.
Quoted 7 lines
Thanks
Best Regards
Hao
All, just wanted to acknowledge that I've gone through the comments and will be sending out a v3 patchset addressing them. Thanks for the reviews!
Re: [PATCH v2 2/6] alloc_tag: add ioctl filters to /proc/allocinfo
In reply to Hao GeQuoted 123 lines · Hao Ge
Hi Abhishek
Quoted 77 lines · Abhishek Bapat
Extend the capability of the IOCTL mechanism to filter allocations based on tag's module name, function name, file name and line number.
Signed-off-by Abhishek Bapat <[email protected]>--- include/uapi/linux/alloc_tag.h | 26 ++++++++++++++- lib/alloc_tag.c | 58 ++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-)include/uapi/linux/alloc_tag.h +25
-1@@ -34,8 +34,32 @@ struct allocinfo_tag_data {struct allocinfo_counter counter; }; +enum { +ALLOCINFO_FILTER_MODNAME , +ALLOCINFO_FILTER_FUNCTION , +ALLOCINFO_FILTER_FILENAME , +ALLOCINFO_FILTER_LINENO , + __ALLOCINFO_FILTER_LAST =ALLOCINFO_FILTER_LINENO +}; + +#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME) +#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION) +#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME) +#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) + +#define ALLOCINFO_FILTER_MASKS \ + ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1) + +struct allocinfo_filter { +__u64 mask;/* bitmask of the filter fields used */ +struct allocinfo_tag fields; +}; +struct allocinfo_get_at { -__u64 pos;/* input */ +/* inputs */ +__u64 pos; +struct allocinfo_filter filter; +/* output */ struct allocinfo_tag_data data; };lib/alloc_tag.c +8
-0@@ -48,6 +48,7 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +struct allocinfo_filter filter;/* ioctl uses a separate iterator not to interfere with reads */ struct codetag_iterator ioctl_iter;bool positioned;/* seq_open_private() sets to 0 */ @@ -167,6 +168,11 @@ static void allocinfo_copy_str(char *dest, const char *src)strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); } +static int allocinfo_cmp_str (const char *str,const char *template) +{ +return strncmp (allocinfo_str (str), template,ALLOCINFO_STR_SIZE ); +} +static void allocinfo_to_params (struct codetag *ct,struct allocinfo_tag_data *data) {@@ -198,27 +204,71 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)return 0 ; } +static bool matches_filter (struct codetag *ct,struct allocinfo_filter *filter) +{lib/alloc_tag.c static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg) · line 218
Quoted 10 lines
lib/alloc_tag.c +10
-0+if (!filter || !filter->mask) +return true; + +if (filter->mask &ALLOCINFO_FILTER_MASK_MODNAME ) { +if (!ct->modname) +return false; +if (allocinfo_cmp_str (ct->modname, filter->fields.modname)) +return false; + } +Apologies -- I previously suggested the "!ct->modname ->
return false" approach, but I realized we broke the ability to
filter for built-in allocations.
allocinfo_to_params()returnsmodname="" for those, so a user would naturally try to filter
by modname="" -- except it never matches because
ct->modnameisNULL, not an empty string.
Maybe something like this instead?
if (
filter->mask&ALLOCINFO_FILTER_MASK_MODNAME) {if (ct->modname) { if (allocinfo_cmp_str(ct->modname, filter->fields.modname)) return false; } else if (filter->fields.modname[0] != '\0') { return false; }}
Quoted 8 lines
That way modname="" matches built-in tags, which lines up with
what the API actually returns.
Thanks
Hao
Ah good catch, will use the following logic that's a bit cleaner:
if (filter->mask & ALLOCINFO_FILTER_MASK_MODNAME) {
/* user wants to filter by modname but ct->modname is NULL */
if (!ct->modname) {
/* validate if user was attempting to filter
for built-in allocations */
if (filter->fields.modname[0] != '\0')
return false;
}
if (allocinfo_cmp_str(ct->modname, filter->fields.modname))
return false;
}Quoted 68 lines
Quoted 68 lines
lib/alloc_tag.c +37
-3+if ((filter->mask &ALLOCINFO_FILTER_MASK_FUNCTION ) && + ct->function && (allocinfo_cmp_str (ct->function, filter->fields.function))) +return false; + +if ((filter->mask &ALLOCINFO_FILTER_MASK_FILENAME ) && + ct->filename && (allocinfo_cmp_str (ct->filename, filter->fields.filename))) +return false; + +if ((filter->mask &ALLOCINFO_FILTER_MASK_LINENO ) && + ct->lineno != filter->fields.lineno) +return false; + +return true; +} +static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) {struct allocinfo_private *priv;struct codetag *ct; -__u64 pos;struct allocinfo_get_at params = {0 }; +__u64 skip_count;if (copy_from_user (¶ms, arg,sizeof (params)))return -EFAULT ; +if (params.filter.mask & ~ALLOCINFO_FILTER_MASKS ) +return -EINVAL ; + priv = (struct allocinfo_private *)m->private; - pos = params.pos; + + skip_count = params.pos;mutex_lock (&priv->ioctl_lock);codetag_lock_module_list (alloc_tag_cttype, true); +if (params.filter.mask) + priv->filter = params.filter; +else + priv->filter.mask =0 ; +/* Find the codetag */ priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); ct =codetag_next_ct (&priv->ioctl_iter); -while (ct && pos--) + +while (ct) { +if (matches_filter (ct, &priv->filter)) { +if (skip_count ==0 ) +break ; + skip_count--; + } ct =codetag_next_ct (&priv->ioctl_iter); + } +if (ct) {allocinfo_to_params (ct, ¶ms.data); priv->positioned = true;@@ -254,6 +304,8 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg) } ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && !matches_filter (ct, &priv->filter)) + ct =codetag_next_ct (&priv->ioctl_iter);if (ct)allocinfo_to_params (ct, ¶ms);
Hao Ge
reviewerRe: [PATCH v2 1/6] alloc_tag: add ioctl to /proc/allocinfo
In reply to Suren BaghdasaryanQuoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · Hao Ge
Hi Abhishek
Thanks for this patch. I had a few questions/comments after going
through it.
Quoted 10 lines · Abhishek Bapat
From: Suren Baghdasaryan <[email protected]>
Add the following ioctl commands for
/proc/allocinfofile:
ALLOCINFO_IOC_CONTENT_ID- gets content identifier which can be used to check whether the file content has changed specifically due to module load/unload. Every time a module is loaded / unloaded, the returned value will be different. By comparing the identifier value at the beginning and at the end of the content retrieval operation, users can validate retrieved information for consistency.
codetag_get_content_id()does not reflect module unload
codetag_get_content_id()returnscttype->next_mod_seq:unsigned long
codetag_get_content_id(struct codetag_type *cttype){
return cttype->next_mod_seq;}
However,
next_mod_seqis only bumped incodetag_module_init(),i.e.the module load path:
https://elixir.bootlin.com/linux/v7.1-rc4/source/lib/codetag.c#L204
codetag_unload_module()does not incrementnext_mod_seq. This meansthat if only a module unload happens (without a subsequent load),
content_idstays the same, so users comparing the id before and afterwon't detect that the content has changed. The commit message says
"Every time a module is loaded / unloaded" -- I was wondering if this
Quoted 7 lines
Quoted 3 lines
is intentional? If not, would it make sense to also bump
next_mod_seqin the unload path?
Good point. I overlooked that when I wrote the prototype for this patch. We should not bump
next_mod_seqin the unload path but instead use a separateseq_countthat gets bumped every time codetag_load_module/codetag_unload_module is called.
Make sense. I looked through the code with the question of why we should not bump next_mod_seq in the unload path.
I've now found the answer.
next_mod_seq is used as a sequence number allocator.
Every newly loaded module gets assigned ++cttype->next_mod_seq as its mod_seq.
The iterator then uses this mod_seq to detect module replacement at a given idr slot:
https://elixir.bootlin.com/linux/v7.1-rc6/source/lib/codetag.c#L102
Bumping it in the unload path would compromise the mod_seq uniqueness relied upon by the iterator.
Thanks for this.
Best Regards
Hao
Quoted 394 lines
Quoted 247 lines
Quoted 63 lines
ALLOCINFO_IOC_GET_AT- gets the record at the specified position. This is the position of a record in/proc/allocinfo.
ALLOCINFO_IOC_GET_NEXT- gets the record next to the last retrieved one. If no records were previously retrieved, returns the first record.Signed-off-by Suren Baghdasaryan <[email protected]>Signed-off-by Abhishek Bapat <[email protected]>--- .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 1 + include/linux/codetag.h | 1 + include/uapi/linux/alloc_tag.h | 54 +++++ lib/alloc_tag.c | 193 +++++++++++++++++- lib/codetag.c | 11 + 6 files changed, 260 insertions(+), 2 deletions(-)create mode 100644
include/uapi/linux/alloc_tag.hDocumentation/userspace-api/ioctl/ioctl-number.rst +2
-0@@ -349,6 +349,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver <mailto:[email protected]> +0xA6 00-0F uapi/linux/alloc_tag.h Memory allocation profiling + <mailto:[email protected]> 0xAA 00-3F linux/uapi/linux/userfaultfd.h 0xAB 00-1F linux/nbd.h 0xAC 00-1F linux/raw.hMAINTAINERS +1
-0@@ -16709,6 +16709,7 @@ S: Maintained F: Documentation/mm/allocation-profiling.rst F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h +F: include/uapi/linux/alloc_tag.h F: lib/alloc_tag.c MEMORY CONTROLLER DRIVERSinclude/linux/codetag.h +1
-0@@ -76,6 +76,7 @@ struct codetag_iterator {void codetag_lock_module_list (struct codetag_type *cttype,bool lock);bool codetag_trylock_module_list (struct codetag_type *cttype); +unsigned long codetag_get_content_id (struct codetag_type *cttype);struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype);struct codetag *codetag_next_ct (struct codetag_iterator *iter);include/uapi/linux/alloc_tag.h new file +0
-0include/uapi/linux/alloc_tag.h line 3
Quoted 3 lines
include/uapi/linux/alloc_tag.h new file +3
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* include/linux/alloc_tag.h nit: it should be
include/uapi/linux/alloc_tag.hQuoted 164 lines
include/uapi/linux/alloc_tag.h new file +51
-0+*/ + +#ifndef _UAPI_ALLOC_TAG_H +#define _UAPI_ALLOC_TAG_H + +#include <linux/types.h> + +#define ALLOCINFO_STR_SIZE 64 + +struct allocinfo_content_id { +__u64 id; +}; + +struct allocinfo_tag { +/* Longer names are trimmed */ +char modname[ALLOCINFO_STR_SIZE ]; +char function[ALLOCINFO_STR_SIZE ]; +char filename[ALLOCINFO_STR_SIZE ]; +__u64 lineno; +}; + +struct allocinfo_counter { +__u64 bytes; +__u64 calls; +__u8 accurate; +__u8 pad[7 ];/* Add alignment to not break the 32-bit compatible interface */ +}; + +struct allocinfo_tag_data { +struct allocinfo_tag tag; +struct allocinfo_counter counter; +}; + +struct allocinfo_get_at { +__u64 pos;/* input */ +struct allocinfo_tag_data data; +}; + +#define _ALLOCINFO_IOC_CONTENT_ID 0 +#define _ALLOCINFO_IOC_GET_AT 1 +#define _ALLOCINFO_IOC_GET_NEXT 2 + +#define ALLOCINFO_IOC_BASE 0xA6 +#define ALLOCINFO_IOC_CONTENT_ID _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_CONTENT_ID, \ + struct allocinfo_content_id) +#define ALLOCINFO_IOC_GET_AT _IOWR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_AT, \ + struct allocinfo_get_at) +#define ALLOCINFO_IOC_GET_NEXT _IOR(ALLOCINFO_IOC_BASE, _ALLOCINFO_IOC_GET_NEXT, \ + struct allocinfo_tag_data) + +#endif /* _UAPI_ALLOC_TAG_H */ lib/alloc_tag.c +84
-0@@ -5,6 +5,7 @@#include <linux/gfp.h> #include <linux/kallsyms.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/page_ext.h> #include <linux/pgalloc_tag.h> #include <linux/proc_fs.h> @@ -14,6 +15,7 @@#include <linux/string_choices.h> #include <linux/vmalloc.h> #include <linux/kmemleak.h> +#include <uapi/linux/alloc_tag.h> #define ALLOCINFO_FILE_NAME "allocinfo"#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))@@ -46,6 +48,10 @@ int alloc_tag_ref_offs;struct allocinfo_private {struct codetag_iterator iter;bool print_header; +/* ioctl uses a separate iterator not to interfere with reads */ +struct codetag_iterator ioctl_iter; +bool positioned;/* seq_open_private() sets to 0 */ +struct mutex ioctl_lock; };static void *allocinfo_start (struct seq_file *m,loff_t *pos)@@ -125,6 +131,190 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +static int allocinfo_open (struct inode *inode,struct file *file) +{ +int ret; + + ret =seq_open_private (file, &allocinfo_seq_op, +sizeof (struct allocinfo_private )); +if (!ret) { +struct seq_file *m = file->private_data; +struct allocinfo_private *priv = m->private; + +mutex_init (&priv->ioctl_lock); + } +return ret; +} + +static int allocinfo_release (struct inode *inode,struct file *file) +{ +return seq_release_private (inode, file); +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +/* Keep an extra space for the trailing NULL. */ +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +/* Copy a string and trim from the beginning if it's too long */ +static void allocinfo_copy_str (char *dest,const char *src) +{ +strscpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE ); +} + +static void allocinfo_to_params (struct codetag *ct, +struct allocinfo_tag_data *data) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); + +if (ct->modname) +allocinfo_copy_str (data->tag.modname, ct->modname); +else + data->tag.modname[0 ] ='\0' ; +allocinfo_copy_str (data->tag.function, ct->function); +allocinfo_copy_str (data->tag.filename, ct->filename); + data->tag.lineno = ct->lineno; + data->counter.bytes = counter.bytes; + data->counter.calls = counter.calls; + data->counter.accurate = !alloc_tag_is_inaccurate (tag); +} + +static int allocinfo_ioctl_get_content_id (struct seq_file *m,void __user *arg) +{ +struct allocinfo_content_id params; + +codetag_lock_module_list (alloc_tag_cttype, true); + params.id =codetag_get_content_id (alloc_tag_cttype); +codetag_lock_module_list (alloc_tag_cttype, false); +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_at (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +__u64 pos; +struct allocinfo_get_at params = {0 }; + +if (copy_from_user (¶ms, arg,sizeof (params))) +return -EFAULT ; + + priv = (struct allocinfo_private *)m->private;lib/alloc_tag.c static const struct seq_operations allocinfo_seq_op = { · line 221
Quoted 10 lines
lib/alloc_tag.c +10
-0+ pos = params.pos; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +/* Find the codetag */ + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + ct =codetag_next_ct (&priv->ioctl_iter); +while (ct && pos--) + ct =codetag_next_ct (&priv->ioctl_iter);No upper bound check on pos in
ALLOCINFO_IOC_GET_AT:pos comes straight from userspace (
__u64) with no validation.If the system has 10000 tags and someone passes pos=10001,
Quoted 7 lines
the loop will still walk all 10000 tags just to return ENOENT
-- all while holding
ioctl_lockandmod_lock. It might be worthchecking pos against the total tag count early. struct
codetag_typeis not exposed outside
codetag.cthough, so this would need a small helper.Ack.
Quoted 138 lines
Thanks
Best Regards
Hao
Quoted 131 lines
lib/alloc_tag.c +97
-2+if (ct) { +allocinfo_to_params (ct, ¶ms.data); + priv->positioned = true; + } + +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (!ct) +return -ENOENT ; + +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + +return 0 ; +} + +static int allocinfo_ioctl_get_next (struct seq_file *m,void __user *arg) +{ +struct allocinfo_private *priv; +struct codetag *ct; +struct allocinfo_tag_data params = {0 }; +int ret =0 ; + + priv = (struct allocinfo_private *)m->private; + +mutex_lock (&priv->ioctl_lock); +codetag_lock_module_list (alloc_tag_cttype, true); + +if (!priv->positioned) { + priv->ioctl_iter =codetag_get_ct_iter (alloc_tag_cttype); + priv->positioned = true; + } + + ct =codetag_next_ct (&priv->ioctl_iter); +if (ct) +allocinfo_to_params (ct, ¶ms); + +if (!ct) { + priv->positioned = false; + ret = -ENOENT ; + } +codetag_lock_module_list (alloc_tag_cttype, false); +mutex_unlock (&priv->ioctl_lock); + +if (ret ==0 ) { +if (copy_to_user (arg, ¶ms,sizeof (params))) +return -EFAULT ; + } +return ret; +} + +static long allocinfo_ioctl (struct file *file,unsigned int cmd, +unsigned long __arg) +{ +void __user *arg = (void __user *)__arg; +int ret; + +switch (cmd) { +case ALLOCINFO_IOC_CONTENT_ID : + ret =allocinfo_ioctl_get_content_id (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_AT : + ret =allocinfo_ioctl_get_at (file->private_data, arg); +break ; +case ALLOCINFO_IOC_GET_NEXT : + ret =allocinfo_ioctl_get_next (file->private_data, arg); +break ; +default : + ret = -ENOIOCTLCMD ; +break ; + } + +return ret; +} + +#ifdef CONFIG_COMPAT +static long allocinfo_compat_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +return allocinfo_ioctl (file, cmd, (unsigned long )compat_ptr (arg)); +} +#endif + +static const struct proc_ops allocinfo_proc_ops = { + .proc_open = allocinfo_open, + .proc_read_iter = seq_read_iter, + .proc_lseek = seq_lseek, + .proc_release = allocinfo_release, + .proc_ioctl = allocinfo_ioctl, +#ifdef CONFIG_COMPAT + .proc_compat_ioctl = allocinfo_compat_ioctl, +#endif + +}; +size_t alloc_tag_top_users (struct codetag_bytes *tags,size_t count,bool can_sleep) {struct codetag_iterator iter;@@ -989,8 +1179,7 @@ static int __init alloc_tag_init(void)return 0 ; } -if (!proc_create_seq_private (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_seq_op, -sizeof (struct allocinfo_private ),NULL )) { +if (!proc_create (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_proc_ops)) {pr_err ("Failed to create %s file\n" ,ALLOCINFO_FILE_NAME );shutdown_mem_profiling (false);return -ENOMEM ;lib/codetag.c +11
-0@@ -48,6 +48,17 @@ bool codetag_trylock_module_list(struct codetag_type *cttype)return down_read_trylock (&cttype->mod_lock) !=0 ; } +unsigned long codetag_get_content_id (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +/* +* next_mod_seq is updated on every load, so can be used to identify +* content changes. +*/ +return cttype->next_mod_seq; +} +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) {struct codetag_iterator iter = {