alloc_tag: introduce IOCTL-based filtering for MAP
Subsystems (7)
v6 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. The IOCTL mechanism was chosen for allocinfo to address the per-CPU counter aggregation bottleneck. A traditional read() operation must report the total allocation count and sizes for every code tag in the system. Doing so requires iterating across all CPUs to sum their per-CPU counters for thousands of tags, which introduces substantial runtime overhead.
The IOCTL interface allows userspace to push selective filtering criteria directly into the kernel before the per-CPU counter aggregation. The kernel aggregates per-CPU counters only for a small subset of tags that match the filter. This results in significant performance improvement.
Beyond fast filtered retrieval, the IOCTL foundation allows introducing a context capture mechanism in the future to capture the context for specific allocations.
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)
v6 changes:
- Patch 1/6: Added comments explaining why last 64 characters are compared in the filter.
- Patch 3/6: Moved allocinfo_prefetch_counters outside of allocinfo_to_params
- Patch 5/6: Fixed fd leak in get_filtered_ioctl_entries() function. Added alloc_tag selftest to the top-level Makefile.
- Patch 6/6: Moved include for errno.h to this patch.
v5 changes:
- Patch 1/6: Added explicit mutex_destroy.
- Patch 5/6: Self-contained file descriptors to avoid wrap-around errors in retry loops.
- Patch 6/6: Fixed minor issues raised by sashiko in v4.
v4 changes:
- Patch 1/6: Fixed a copyright comment inside include/uapi/linux/alloc_tag.h
- Patch 3/6: Among other nits, fixed the inadvertent build failure introduced in v3.
- Patch 4/6: Included a comment stating that the accurate field in struct allocinfo_tag is only used for filtering.
- Patch 5/6: Modified test to trim prefix and keep suffix for entries with filenames exceeding the size limit.
- Patch 6/6: Modified test_size_filter such that if content_id changes between the moment when procfs and ioctl entries are read, both
entries are invalidated and re-fetched. Removed the tags->count == 0 check from test_lineno_filter as it's virtually unreachable.
v3 changes:
- Patch 1/6: Modified Documentation to indicate that map supports ioctl(). Modified struct allocinfo_count to use
__attribute__((aligned(8))) instead of manual padding. Removed redundance type-casting. Added comments for static functions in lib/alloc_tag.c. Introduced a new seq counter for content_id that gets bumped every time module is loaded / unloaded. Introduced logic to validate user specified position is not greater than number of allocation tags and return early if it is. Changed strscpy to strscpy_pad to not echo arbitrary user data back to the user.
- Patch 2/6: Handled the case where user wants to specifically filter for built-in modules. Included some comments for static functions.
- Patch 3/6: Modified logic to only fetch per-CPU counters for codetags that satisfy other filters. Included some comments for static
functions.
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
Documentation/mm/allocation-profiling.rst | 5 + .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 2 + include/linux/codetag.h | 2 + include/uapi/linux/alloc_tag.h | 99 ++++ lib/alloc_tag.c | 344 +++++++++++- lib/codetag.c | 18 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 531 ++++++++++++++++++ 10 files changed, 1011 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.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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.
Note, function file and module names often have the same prefixes, therefore when filtering for them, we compare the last 64 characters to minimize the chances of name collisions.
--- Documentation/mm/allocation-profiling.rst | 5 + .../userspace-api/ioctl/ioctl-number.rst | 2 + MAINTAINERS | 1 + include/linux/codetag.h | 2 + include/uapi/linux/alloc_tag.h | 65 +++++ lib/alloc_tag.c | 238 +++++++++++++++++- lib/codetag.c | 18 ++ 7 files changed, 329 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/alloc_tag.h
Documentation/mm/allocation-profiling.rst +5 -0
@@ -46,6 +46,11 @@ sysctl: Runtime info: /proc/allocinfo + Profiling data can be retrieved either by reading `/proc/allocinfo` directly as + text or programmatically via `ioctl()` calls defined in `<uapi/linux/alloc_tag.h>`. + The ioctl interface supports structured binary data extraction as well as filtering + by module name, function, file, line number, accuracy, or allocation size limits. + Example output:: root@moria-kvm:~# sort -g /proc/allocinfo|tail|numfmt --to=iec
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
@@ -16713,6 +16713,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 +2 -0
@@ -77,6 +77,8 @@ struct codetag_iterator {void codetag_lock_module_list (struct codetag_type *cttype);bool codetag_trylock_module_list (struct codetag_type *cttype);void codetag_unlock_module_list (struct codetag_type *cttype); +unsigned long codetag_get_content_id (struct codetag_type *cttype); +unsigned int codetag_get_count (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 +65 -0
65 lines not on this page · raw message
lib/alloc_tag.c +236 -2
268 lines not on this page · raw message
lib/codetag.c +18 -0
@@ -19,6 +19,8 @@ struct codetag_type {struct codetag_type_desc desc;/* generates unique sequence number for module load */ unsigned long next_mod_seq; +/* bumped on every module load and unload */ +unsigned long content_id; };struct codetag_range {
@@ -50,6 +52,20 @@ void codetag_unlock_module_list(struct codetag_type *cttype)up_read (&cttype->mod_lock); } +unsigned long codetag_get_content_id (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +return cttype->content_id; +} + +unsigned int codetag_get_count (struct codetag_type *cttype) +{ +lockdep_assert_held (&cttype->mod_lock); + +return cttype->count; +} +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) {struct codetag_iterator iter = {
@@ -204,6 +220,7 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod)down_write (&cttype->mod_lock); cmod->mod_seq = ++cttype->next_mod_seq; + ++cttype->content_id; mod_id =idr_alloc (&cttype->mod_idr, cmod,0 ,0 ,GFP_KERNEL );if (mod_id >=0 ) {if (cttype->desc.module_load) {
@@ -368,6 +385,7 @@ void codetag_unload_module(struct module *mod) cttype->count -=range_size (cttype, &cmod->range);idr_remove (&cttype->mod_idr, mod_id);kfree (cmod); + ++cttype->content_id; }up_write (&cttype->mod_lock);if (found && cttype->desc.free_section_mem)
Signature · 1 line
2.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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 | 68 ++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 5 deletions(-)
include/uapi/linux/alloc_tag.h +25 -1
@@ -45,8 +45,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 +64 -4
117 lines not on this page · raw message
Signature · 1 line
2.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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 | 64 +++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 14 deletions(-)
include/uapi/linux/alloc_tag.h +7 -1
@@ -50,13 +50,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)
@@ -64,6 +68,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 +51 -13
132 lines not on this page · raw message
Signature · 1 line
2.55.0.rc0.786.g65d90a0328-goog
Re: [PATCH v6 3/6] alloc_tag: add size-based filtering to ioctl
Quoted 11 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]>Acked-by Hao Ge <[email protected]>
Quoted 186 lines
--- include/uapi/linux/alloc_tag.h | 8 ++++- lib/alloc_tag.c | 64 +++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 14 deletions(-)include/uapi/linux/alloc_tag.h +7
-1@@ -50,13 +50,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)@@ -64,6 +68,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 +51
-14@@ -198,16 +198,20 @@ static int allocinfo_cmp_str(const char *str, const char *template)return strncmp (allocinfo_str (str), template,ALLOCINFO_STR_SIZE ); } +/* Fetch the per-CPU counters */ +static inline struct alloc_tag_counters allocinfo_prefetch_counters (struct codetag *ct) +{ +return alloc_tag_read (ct_to_alloc_tag (ct)); +} +/* * Populates the UAPI allocinfo_tag_data structure with active runtime * profiling counters extracted from the given kernel codetag. */ 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); -if (ct->modname)allocinfo_copy_str (data->tag.modname, ct->modname);else @@ -215,9 +219,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)); }/* @@ -241,7 +245,9 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg)* Verifies whether a given codetag satisfies the active filtering criteria by * matching its characteristics against the specified filter. */ -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, +bool *fetched_counters) {if (!filter || !filter->mask)return true;@@ -268,6 +274,19 @@ 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 |ALLOCINFO_FILTER_MASK_MAX_SIZE )) { +if (!*fetched_counters) { + *counters =allocinfo_prefetch_counters (ct); + *fetched_counters = true; + } +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; }@@ -281,6 +300,8 @@ 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; +struct alloc_tag_counters counters; +bool fetched_counters;if (copy_from_user (¶ms, arg,sizeof (params)))return -EFAULT ;@@ -288,6 +309,11 @@ 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 = m->private;mutex_lock (&priv->ioctl_lock);@@ -311,7 +337,8 @@ 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)) { + fetched_counters = false; +if (matches_filter (ct, &priv->filter, &counters, &fetched_counters)) {if (skip_count ==0 )break ; skip_count--;@@ -320,7 +347,9 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) }if (ct) { -allocinfo_to_params (ct, ¶ms.data); +if (!fetched_counters) + counters =allocinfo_prefetch_counters (ct); +allocinfo_to_params (ct, ¶ms.data, &counters); priv->positioned = true; }@@ -346,6 +375,8 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg)struct codetag *ct;struct allocinfo_tag_data params;int ret =0 ; +struct alloc_tag_counters counters; +bool fetched_counters;memset (¶ms,0 ,sizeof (params)); priv = m->private;@@ -359,11 +390,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) { + fetched_counters = false; +if (matches_filter (ct, &priv->filter, &counters, &fetched_counters)) +break ; ct =codetag_next_ct (&priv->ioctl_iter); -if (ct) -allocinfo_to_params (ct, ¶ms); + } +if (ct) { +if (!fetched_counters) + counters =allocinfo_prefetch_counters (ct); +allocinfo_to_params (ct, ¶ms, &counters); + }if (!ct) { priv->positioned = false; ret = -ENOENT ; --2.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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 | 4 ++++ lib/alloc_tag.c | 8 ++++++++ 2 files changed, 12 insertions(+)
include/uapi/linux/alloc_tag.h +4 -0
@@ -31,6 +31,8 @@ struct allocinfo_tag {char function[ALLOCINFO_STR_SIZE ];char filename[ALLOCINFO_STR_SIZE ];__u64 lineno; +/* filter criteria only; see allocinfo_counter.accurate for actual accuracy */ +__u64 inaccurate; };/* The alignment ensures 32-bit compatible interfaces are not broken */
@@ -50,6 +52,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
@@ -59,6 +62,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
@@ -249,6 +249,8 @@ static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter,struct alloc_tag_counters *counters, bool *fetched_counters) { + bool inaccurate; +if (!filter || !filter->mask)return true;
@@ -274,6 +276,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 |ALLOCINFO_FILTER_MASK_MAX_SIZE )) {if (!*fetched_counters) { *counters =allocinfo_prefetch_counters (ct);
Signature · 1 line
2.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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/Makefile | 1 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 335 ++++++++++++++++++ 4 files changed, 346 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
@@ -16715,6 +16715,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/Makefile +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 TARGETS += acct +TARGETS += alloc_tag TARGETS += alsa TARGETS += amd-pstate TARGETS += arm64
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 +335 -0
335 lines not on this page · raw message
Signature · 1 line
2.55.0.rc0.786.g65d90a0328-goog
Suren Baghdasaryan
maintainerRe: [PATCH v6 5/6] kselftest: alloc_tag: add kselftest for ioctl interface
Quoted 61 lines · Abhishek Bapat
Introduce 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 firstVEC_MAX_ENTRIESentries.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.
Signed-off-by Abhishek Bapat <[email protected]>--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 335 ++++++++++++++++++ 4 files changed, 346 insertions(+) create mode 100644 tools/testing/selftests/alloc_tag/Makefile create mode 100644 tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.cMAINTAINERS +1
-0@@ -16715,6 +16715,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/Makefile +1
-0@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 TARGETS += acct +TARGETS += alloc_tag TARGETS += alsa TARGETS += amd-pstate TARGETS += arm64tools/testing/selftests/alloc_tag/Makefile new file +0
-0
tools/testing/selftests/alloc_tag/Makefile line 6
Quoted 6 lines
tools/testing/selftests/alloc_tag/Makefile new file +6
-0@@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := allocinfo_ioctl_test + +CFLAGS += -Wall +CFLAGS += -I../../../../usr/include
I recall Hao suggested replacing this path with $(KHDR_INCLUDES). Have you tried that?
Quoted 208 lines
tools/testing/selftests/alloc_tag/Makefile new file +3
-0+ +include ../lib.mk +tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +199
-0@@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* kselftest for allocinfo ioctl +* allocinfo ioctl retrives allocinfo data through ioctl +* Copyright (C) 2026 Google, Inc. +*/ + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <linux/types.h> +#include <linux/alloc_tag.h> +#include "../kselftest.h" + +#define MAX_LINE_LEN 512 +#define ALLOCINFO_PROC "/proc/allocinfo" + +enum ioctl_ret { +IOCTL_SUCCESS =0 , +IOCTL_FAILURE =1 , +IOCTL_INVALID_DATA =2 , +}; + +#define VEC_MAX_ENTRIES 32 + +struct allocinfo_tag_data_vec { +struct allocinfo_tag_data tag[VEC_MAX_ENTRIES ]; +__u64 count; +}; + +static inline int __allocinfo_get_content_id (int dev_fd,struct allocinfo_content_id *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_CONTENT_ID , params); +} + +static inline int __allocinfo_get_at (int dev_fd,struct allocinfo_get_at *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_GET_AT , params); +} + +static inline int __allocinfo_get_next (int dev_fd,struct allocinfo_tag_data *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_GET_NEXT , params); +} + +static bool match_entry (const struct allocinfo_tag_data *procfs_entry, +const struct allocinfo_tag_data *tag_data, +bool match_bytes,bool match_calls,bool match_lineno, +bool match_function,bool match_filename) +{ +if (match_bytes && tag_data->counter.bytes != procfs_entry->counter.bytes) { +ksft_print_msg ("size retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_calls && tag_data->counter.calls != procfs_entry->counter.calls) { +ksft_print_msg ("call count retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_lineno && tag_data->tag.lineno != procfs_entry->tag.lineno) { +ksft_print_msg ("lineno retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_function && +strncmp (tag_data->tag.function, procfs_entry->tag.function,ALLOCINFO_STR_SIZE )) { +ksft_print_msg ("function retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_filename && +strncmp (tag_data->tag.filename, procfs_entry->tag.filename,ALLOCINFO_STR_SIZE )) { +ksft_print_msg ("filename retrieved through ioctl does not match procfs\n" ); +return false; + } +return true; +} + +static bool match_entries (const struct allocinfo_tag_data_vec *procfs_entries, +const struct allocinfo_tag_data_vec *tags, +bool match_bytes,bool match_calls,bool match_lineno, +bool match_function,bool match_filename) +{ +__u64 i; + +if (procfs_entries->count != tags->count) { +ksft_print_msg ("Entry count mismatch. ioctl entries: %llu, proc entries: %llu\n" , + tags->count, procfs_entries->count); +return false; + } +for (i =0 ; i < procfs_entries->count; i++) { +if (!match_entry (&procfs_entries->tag[i], &tags->tag[i], + match_bytes, match_calls, match_lineno, + match_function, match_filename)) { +ksft_print_msg ("%lluth entry does not match.\n" , i); +return false; + } + } +return true; +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +static void allocinfo_copy_str (char *dest,const char *src) +{ +strncpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE -1 ); + dest[ALLOCINFO_STR_SIZE -1 ] ='\0' ; +} + +static int get_filtered_procfs_entries (struct allocinfo_tag_data_vec *procfs_entries, +const struct allocinfo_filter *filter) +{ +FILE *fp =fopen (ALLOCINFO_PROC ,"r" ); +char line[MAX_LINE_LEN ]; +int matches; +struct allocinfo_tag_data procfs_entry; + +if (!fp) { +ksft_print_msg ("Failed to open " ALLOCINFO_PROC " for reading\n" ); +return 1 ; + } +memset (procfs_entries,0 ,sizeof (*procfs_entries)); +while (fgets (line,sizeof (line), fp) && procfs_entries->count <VEC_MAX_ENTRIES ) { +char filename[MAX_LINE_LEN ]; +char function[MAX_LINE_LEN ]; + +memset (&procfs_entry,0 ,sizeof (procfs_entry)); + matches =sscanf (line,"%llu %llu %[^:]:%llu func:%s" , + &procfs_entry.counter.bytes, + &procfs_entry.counter.calls, + filename, + &procfs_entry.tag.lineno, + function); + +if (matches !=5 ) +continue ; + +allocinfo_copy_str (procfs_entry.tag.filename, filename); +allocinfo_copy_str (procfs_entry.tag.function, function); + +if (filter->mask &ALLOCINFO_FILTER_MASK_FILENAME ) { +if (strncmp (procfs_entry.tag.filename, + filter->fields.filename,ALLOCINFO_STR_SIZE )) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_FUNCTION ) { +if (strncmp (procfs_entry.tag.function, + filter->fields.function,ALLOCINFO_STR_SIZE )) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_LINENO ) { +if (procfs_entry.tag.lineno != filter->fields.lineno) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) { +if (procfs_entry.counter.bytes < filter->min_size) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) { +if (procfs_entry.counter.bytes > filter->max_size) +continue ; + } + +memcpy (&procfs_entries->tag[procfs_entries->count++], &procfs_entry, +sizeof (procfs_entry)); + } +fclose (fp); +return 0 ; +} + +static enum ioctl_ret get_filtered_ioctl_entries (struct allocinfo_tag_data_vec *tags, +const struct allocinfo_filter *filter, +__u64 start_pos) +{ +int fd =open (ALLOCINFO_PROC ,O_RDONLY ); + +if (fd <0 ) { +ksft_print_msg ("Failed to open " ALLOCINFO_PROC " for IOCTL\n" ); +return IOCTL_FAILURE ; + } + +struct allocinfo_content_id start_cont_id, end_cont_id; +struct allocinfo_get_at get_at_params; +const int max_retries =10 ; +int retry_count =0 ; +int status; +
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c line 209
Quoted 10 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +10
-0+/* +* __allocinfo_get_content_id may return different values if a kernel module was loaded +* between the two calls. If that happens, the data gathered cannot be considered consistent +* and hence needs to be fetched again to avoid flakiness. +*/ + do { + if (__allocinfo_get_content_id(fd, &start_cont_id)) { + ksft_print_msg("allocinfo_get_content_id failed\n"); + status = IOCTL_FAILURE; + goto exit;
nit: I would prefer a "break" instead of these "goto exit" statements for exiting this loop. Makes the code flow more readable IMHO.
Quoted 129 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +126
-1+ } + +memset (tags,0 ,sizeof (*tags)); +memset (&get_at_params,0 ,sizeof (get_at_params)); +memcpy (&get_at_params.filter, filter,sizeof (*filter)); + get_at_params.pos = start_pos; +if (__allocinfo_get_at (fd, &get_at_params)) { +ksft_print_msg ("allocinfo_get_at failed\n" ); + status =IOCTL_FAILURE ; + goto exit; + } +memcpy (&tags->tag[tags->count++], &get_at_params.data,sizeof (get_at_params.data)); + +while (tags->count <VEC_MAX_ENTRIES && +__allocinfo_get_next (fd, &tags->tag[tags->count]) ==0 ) + tags->count++; + +if (__allocinfo_get_content_id (fd, &end_cont_id)) { +ksft_print_msg ("allocinfo_get_content_id failed\n" ); + status =IOCTL_FAILURE ; + goto exit; + } + +if (start_cont_id.id == end_cont_id.id) { + status =IOCTL_SUCCESS ; + }else { +ksft_print_msg ("allocinfo_get_content_id mismatch, retrying...\n" ); + status =IOCTL_INVALID_DATA ; + } + }while (status ==IOCTL_INVALID_DATA && retry_count++ < max_retries); + +exit: +close (fd); +return status; +} + +static int run_filter_test (const struct allocinfo_filter *filter) +{ +struct allocinfo_tag_data_vec *tags =malloc (sizeof (*tags)); +struct allocinfo_tag_data_vec *procfs_entries =malloc (sizeof (*procfs_entries)); +int ioctl_status; +int ret =KSFT_PASS ; + +if (!tags || !procfs_entries) { +ksft_print_msg ("Memory allocation failed.\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +if (get_filtered_procfs_entries (procfs_entries, filter)) { +ksft_print_msg ("Error retrieving entries from " ALLOCINFO_PROC "\n" ); + ret =KSFT_SKIP ; + goto exit; + } + +if (procfs_entries->count ==0 ) { +ksft_print_msg ("No entries found in " ALLOCINFO_PROC ", skipping test\n" ); + ret =KSFT_SKIP ; + goto exit; + } + + ioctl_status =get_filtered_ioctl_entries (tags, filter,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 (!match_entries (procfs_entries, tags, false, false, true, true, true)) + ret =KSFT_FAIL ; + +exit: +free (tags); +free (procfs_entries); +return ret; +} + +static int test_filename_filter (void ) +{ +struct allocinfo_filter filter; +const char *target_filename ="mm/memory.c" ; + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_FILENAME ; +strncpy (filter.fields.filename, target_filename,ALLOCINFO_STR_SIZE ); + +return run_filter_test (&filter); +} + +static int test_function_filter (void ) +{ +struct allocinfo_filter filter; +const char *target_function ="dup_mm" ; + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_FUNCTION ; +strncpy (filter.fields.function, target_function,ALLOCINFO_STR_SIZE ); + +return run_filter_test (&filter); +} + +int main (int argc,char *argv[]) +{ +int ret; + +ksft_set_plan (2 ); + + ret =test_filename_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_filename_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_filename_filter\n" ); + + ret =test_function_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_function_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_function_filter\n" ); + +ksft_finished (); +} --2.55.0.rc0.786.g65d90a0328-goog
Hao Ge
reviewerRe: [PATCH v6 5/6] kselftest: alloc_tag: add kselftest for ioctl interface
Quoted 62 lines · Suren Baghdasaryan
Quoted 60 lines · Abhishek Bapat
Introduce 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 firstVEC_MAX_ENTRIESentries.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.
Signed-off-by Abhishek Bapat <[email protected]>--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/alloc_tag/Makefile | 9 + .../alloc_tag/allocinfo_ioctl_test.c | 335 ++++++++++++++++++ 4 files changed, 346 insertions(+)create mode 100644
tools/testing/selftests/alloc_tag/Makefilecreate mode 100644tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.cMAINTAINERS +1
-0@@ -16715,6 +16715,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/Makefile +1
-0@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 TARGETS += acct +TARGETS += alloc_tag TARGETS += alsa TARGETS += amd-pstate TARGETS += arm64tools/testing/selftests/alloc_tag/Makefile new file +0
-0@@ -0,0 +1,9 @@
Quoted 8 lines
tools/testing/selftests/alloc_tag/Makefile line 6
Quoted 6 lines
tools/testing/selftests/alloc_tag/Makefile new file +6
-0+# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := allocinfo_ioctl_test + +CFLAGS += -Wall +CFLAGS += -I../../../../usr/includeI recall Hao suggested replacing this path with $(KHDR_INCLUDES). Have you tried that?
Hi Suren and Abhishek
Yes, I suggested this in my previous email.
Using CFLAGS += -I../../../../usr/include fails to support out-of-tree build workflows.
I ran the following command:
make O=/home alldebug_defconfig
make O=/home -j8
make O=/home headers_install
make -C tools/testing/selftests/ TARGETS=alloc_tag O=/home/
[root@localhost linux]# make -C tools/testing/selftests TARGETS=alloc_tag O=/home/
make: Entering directory '/home/linux/tools/testing/selftests'
CC allocinfo_ioctl_test
allocinfo_ioctl_test.c:16:10: fatal error: linux/alloc_tag.h: No such file or directory 16 | #include <linux/alloc_tag.h>
| ^~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [allocinfo_ioctl_test] Error 1
Therefore, using $(KHDR_INCLUDES) here is more appropriate. The corresponding build flow is as follows:
https://elixir.bootlin.com/linux/v7.1/source/tools/testing/selftests/Makefile#L198
After modification, the compilation output is shown below:
[root@localhost linux]# make -C tools/testing/selftests TARGETS=alloc_tag O=/home/
make: Entering directory '/home/linux/tools/testing/selftests'
CC allocinfo_ioctl_test
make: Leaving directory '/home/linux/tools/testing/selftests'
[root@localhost linux]# ls /home/kselftest/alloc_tag/allocinfo_ioctl_test /home/kselftest/alloc_tag/allocinfo_ioctl_test [root@localhost linux]#
Thanks
Best Regards
Hao
Quoted 351 lines
Quoted 209 lines
tools/testing/selftests/alloc_tag/Makefile new file +3
-0+ +include ../lib.mk +tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +199
-0@@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* kselftest for allocinfo ioctl +* allocinfo ioctl retrives allocinfo data through ioctl +* Copyright (C) 2026 Google, Inc. +*/ + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <linux/types.h> +#include <linux/alloc_tag.h> +#include "../kselftest.h" + +#define MAX_LINE_LEN 512 +#define ALLOCINFO_PROC "/proc/allocinfo" + +enum ioctl_ret { +IOCTL_SUCCESS =0 , +IOCTL_FAILURE =1 , +IOCTL_INVALID_DATA =2 , +}; + +#define VEC_MAX_ENTRIES 32 + +struct allocinfo_tag_data_vec { +struct allocinfo_tag_data tag[VEC_MAX_ENTRIES ]; +__u64 count; +}; + +static inline int __allocinfo_get_content_id (int dev_fd,struct allocinfo_content_id *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_CONTENT_ID , params); +} + +static inline int __allocinfo_get_at (int dev_fd,struct allocinfo_get_at *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_GET_AT , params); +} + +static inline int __allocinfo_get_next (int dev_fd,struct allocinfo_tag_data *params) +{ +return ioctl (dev_fd,ALLOCINFO_IOC_GET_NEXT , params); +} + +static bool match_entry (const struct allocinfo_tag_data *procfs_entry, +const struct allocinfo_tag_data *tag_data, +bool match_bytes,bool match_calls,bool match_lineno, +bool match_function,bool match_filename) +{ +if (match_bytes && tag_data->counter.bytes != procfs_entry->counter.bytes) { +ksft_print_msg ("size retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_calls && tag_data->counter.calls != procfs_entry->counter.calls) { +ksft_print_msg ("call count retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_lineno && tag_data->tag.lineno != procfs_entry->tag.lineno) { +ksft_print_msg ("lineno retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_function && +strncmp (tag_data->tag.function, procfs_entry->tag.function,ALLOCINFO_STR_SIZE )) { +ksft_print_msg ("function retrieved through ioctl does not match procfs\n" ); +return false; + } + +if (match_filename && +strncmp (tag_data->tag.filename, procfs_entry->tag.filename,ALLOCINFO_STR_SIZE )) { +ksft_print_msg ("filename retrieved through ioctl does not match procfs\n" ); +return false; + } +return true; +} + +static bool match_entries (const struct allocinfo_tag_data_vec *procfs_entries, +const struct allocinfo_tag_data_vec *tags, +bool match_bytes,bool match_calls,bool match_lineno, +bool match_function,bool match_filename) +{ +__u64 i; + +if (procfs_entries->count != tags->count) { +ksft_print_msg ("Entry count mismatch. ioctl entries: %llu, proc entries: %llu\n" , + tags->count, procfs_entries->count); +return false; + } +for (i =0 ; i < procfs_entries->count; i++) { +if (!match_entry (&procfs_entries->tag[i], &tags->tag[i], + match_bytes, match_calls, match_lineno, + match_function, match_filename)) { +ksft_print_msg ("%lluth entry does not match.\n" , i); +return false; + } + } +return true; +} + +static const char *allocinfo_str (const char *str) +{ +size_t len =strlen (str); + +if (len >=ALLOCINFO_STR_SIZE ) + str += (len -ALLOCINFO_STR_SIZE ) +1 ; +return str; +} + +static void allocinfo_copy_str (char *dest,const char *src) +{ +strncpy (dest,allocinfo_str (src),ALLOCINFO_STR_SIZE -1 ); + dest[ALLOCINFO_STR_SIZE -1 ] ='\0' ; +} + +static int get_filtered_procfs_entries (struct allocinfo_tag_data_vec *procfs_entries, +const struct allocinfo_filter *filter) +{ +FILE *fp =fopen (ALLOCINFO_PROC ,"r" ); +char line[MAX_LINE_LEN ]; +int matches; +struct allocinfo_tag_data procfs_entry; + +if (!fp) { +ksft_print_msg ("Failed to open " ALLOCINFO_PROC " for reading\n" ); +return 1 ; + } +memset (procfs_entries,0 ,sizeof (*procfs_entries)); +while (fgets (line,sizeof (line), fp) && procfs_entries->count <VEC_MAX_ENTRIES ) { +char filename[MAX_LINE_LEN ]; +char function[MAX_LINE_LEN ]; + +memset (&procfs_entry,0 ,sizeof (procfs_entry)); + matches =sscanf (line,"%llu %llu %[^:]:%llu func:%s" , + &procfs_entry.counter.bytes, + &procfs_entry.counter.calls, + filename, + &procfs_entry.tag.lineno, + function); + +if (matches !=5 ) +continue ; + +allocinfo_copy_str (procfs_entry.tag.filename, filename); +allocinfo_copy_str (procfs_entry.tag.function, function); + +if (filter->mask &ALLOCINFO_FILTER_MASK_FILENAME ) { +if (strncmp (procfs_entry.tag.filename, + filter->fields.filename,ALLOCINFO_STR_SIZE )) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_FUNCTION ) { +if (strncmp (procfs_entry.tag.function, + filter->fields.function,ALLOCINFO_STR_SIZE )) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_LINENO ) { +if (procfs_entry.tag.lineno != filter->fields.lineno) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_MIN_SIZE ) { +if (procfs_entry.counter.bytes < filter->min_size) +continue ; + } +if (filter->mask &ALLOCINFO_FILTER_MASK_MAX_SIZE ) { +if (procfs_entry.counter.bytes > filter->max_size) +continue ; + } + +memcpy (&procfs_entries->tag[procfs_entries->count++], &procfs_entry, +sizeof (procfs_entry)); + } +fclose (fp); +return 0 ; +} + +static enum ioctl_ret get_filtered_ioctl_entries (struct allocinfo_tag_data_vec *tags, +const struct allocinfo_filter *filter, +__u64 start_pos) +{ +int fd =open (ALLOCINFO_PROC ,O_RDONLY ); + +if (fd <0 ) { +ksft_print_msg ("Failed to open " ALLOCINFO_PROC " for IOCTL\n" ); +return IOCTL_FAILURE ; + } + +struct allocinfo_content_id start_cont_id, end_cont_id; +struct allocinfo_get_at get_at_params; +const int max_retries =10 ; +int retry_count =0 ; +int status; +tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c line 209
Quoted 10 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +10
-0+/* +* __allocinfo_get_content_id may return different values if a kernel module was loaded +* between the two calls. If that happens, the data gathered cannot be considered consistent +* and hence needs to be fetched again to avoid flakiness. +*/ + do { + if (__allocinfo_get_content_id(fd, &start_cont_id)) { + ksft_print_msg("allocinfo_get_content_id failed\n"); + status = IOCTL_FAILURE; + goto exit;nit: I would prefer a "break" instead of these "goto exit" statements for exiting this loop. Makes the code flow more readable IMHO.
Quoted 129 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c new file +126
-1+ } + +memset (tags,0 ,sizeof (*tags)); +memset (&get_at_params,0 ,sizeof (get_at_params)); +memcpy (&get_at_params.filter, filter,sizeof (*filter)); + get_at_params.pos = start_pos; +if (__allocinfo_get_at (fd, &get_at_params)) { +ksft_print_msg ("allocinfo_get_at failed\n" ); + status =IOCTL_FAILURE ; + goto exit; + } +memcpy (&tags->tag[tags->count++], &get_at_params.data,sizeof (get_at_params.data)); + +while (tags->count <VEC_MAX_ENTRIES && +__allocinfo_get_next (fd, &tags->tag[tags->count]) ==0 ) + tags->count++; + +if (__allocinfo_get_content_id (fd, &end_cont_id)) { +ksft_print_msg ("allocinfo_get_content_id failed\n" ); + status =IOCTL_FAILURE ; + goto exit; + } + +if (start_cont_id.id == end_cont_id.id) { + status =IOCTL_SUCCESS ; + }else { +ksft_print_msg ("allocinfo_get_content_id mismatch, retrying...\n" ); + status =IOCTL_INVALID_DATA ; + } + }while (status ==IOCTL_INVALID_DATA && retry_count++ < max_retries); + +exit: +close (fd); +return status; +} + +static int run_filter_test (const struct allocinfo_filter *filter) +{ +struct allocinfo_tag_data_vec *tags =malloc (sizeof (*tags)); +struct allocinfo_tag_data_vec *procfs_entries =malloc (sizeof (*procfs_entries)); +int ioctl_status; +int ret =KSFT_PASS ; + +if (!tags || !procfs_entries) { +ksft_print_msg ("Memory allocation failed.\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +if (get_filtered_procfs_entries (procfs_entries, filter)) { +ksft_print_msg ("Error retrieving entries from " ALLOCINFO_PROC "\n" ); + ret =KSFT_SKIP ; + goto exit; + } + +if (procfs_entries->count ==0 ) { +ksft_print_msg ("No entries found in " ALLOCINFO_PROC ", skipping test\n" ); + ret =KSFT_SKIP ; + goto exit; + } + + ioctl_status =get_filtered_ioctl_entries (tags, filter,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 (!match_entries (procfs_entries, tags, false, false, true, true, true)) + ret =KSFT_FAIL ; + +exit: +free (tags); +free (procfs_entries); +return ret; +} + +static int test_filename_filter (void ) +{ +struct allocinfo_filter filter; +const char *target_filename ="mm/memory.c" ; + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_FILENAME ; +strncpy (filter.fields.filename, target_filename,ALLOCINFO_STR_SIZE ); + +return run_filter_test (&filter); +} + +static int test_function_filter (void ) +{ +struct allocinfo_filter filter; +const char *target_function ="dup_mm" ; + +memset (&filter,0 ,sizeof (filter)); + filter.mask |=ALLOCINFO_FILTER_MASK_FUNCTION ; +strncpy (filter.fields.function, target_function,ALLOCINFO_STR_SIZE ); + +return run_filter_test (&filter); +} + +int main (int argc,char *argv[]) +{ +int ret; + +ksft_set_plan (2 ); + + ret =test_filename_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_filename_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_filename_filter\n" ); + + ret =test_function_filter (); +if (ret ==KSFT_SKIP ) +ksft_test_result_skip ("Skipping test_function_filter\n" ); +else +ksft_test_result (ret ==KSFT_PASS ,"test_function_filter\n" ); + +ksft_finished (); +} --2.55.0.rc0.786.g65d90a0328-goog
PATCH v6 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 | 198 +++++++++++++++++- 1 file changed, 197 insertions(+), 1 deletion(-)
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +197 -1
@@ -5,6 +5,7 @@* Copyright (C) 2026 Google, Inc. */ +#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h>
@@ -313,11 +314,194 @@ 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, pos; +bool found; +const char *target_function ="do_init_module" ; +struct allocinfo_content_id start_cont_id, end_cont_id; +int retry =0 ; +const int max_retries =10 ; + +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_print_msg ("Failed to open " ALLOCINFO_PROC ": %s\n" ,strerror (errno)); + ret =KSFT_FAIL ; + goto freemem; + } + +do { + found = false; + pos =0 ; + +if (__allocinfo_get_content_id (fd, &start_cont_id)) { +ksft_print_msg ("allocinfo_get_content_id failed\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +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)) { +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; + } + + 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; + +while (1 ) { +struct allocinfo_get_at get_at_params; + +memset (&get_at_params,0 ,sizeof (get_at_params)); +memcpy (&get_at_params.filter, &filter,sizeof (filter)); + get_at_params.pos = pos; + +if (__allocinfo_get_at (fd, &get_at_params)) +break ; + + tags->count =0 ; +memcpy (&tags->tag[tags->count++], &get_at_params.data, +sizeof (get_at_params.data)); + +while (tags->count <VEC_MAX_ENTRIES && +__allocinfo_get_next (fd, &tags->tag[tags->count]) ==0 ) + tags->count++; + +for (i =0 ; i < tags->count; i++) { +if (strcmp (tags->tag[i].tag.function, target_function) ==0 ) { + found = true; +break ; + } + } + +if (found || tags->count <VEC_MAX_ENTRIES ) +break ; + + pos += tags->count; + } + +if (__allocinfo_get_content_id (fd, &end_cont_id)) { +ksft_print_msg ("allocinfo_get_content_id failed\n" ); + ret =KSFT_FAIL ; + goto exit; + } + +if (start_cont_id.id == end_cont_id.id) +break ; + +ksft_print_msg ("Module load detected during size verification, retrying...\n" ); + }while (retry++ < max_retries); + +if (start_cont_id.id == end_cont_id.id && !found) { +ksft_print_msg ("Entry with function %s not found in IOCTL results\n" , + target_function); + ret =KSFT_FAIL ; + }else if (start_cont_id.id != end_cont_id.id) { +ksft_print_msg ("Failed to match content_ids for procfs and IOCTL, skipping...\n" ); + ret =KSFT_SKIP ; + } + +exit: +close (fd); +freemem: +free (tags); +free (procfs_entries); +return ret; +} + +static int test_lineno_filter (void ) +{ +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 exit; + } + +memset (&filter,0 ,sizeof (filter)); + +if (get_filtered_procfs_entries (procfs_entries, &filter)) { +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,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; + } + +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: +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 )
@@ -331,5 +515,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.55.0.rc0.786.g65d90a0328-goog
Suren Baghdasaryan
maintainerRe: [PATCH v6 6/6] kselftest: alloc_tag: extend the allocinfo ioctl kselftest
Quoted 15 lines · Abhishek Bapat
Add the following 2 scenarios to the allocinfo ioctl kselftest:
1. Validate size based filtering
2. Validate lineno based filteringThe 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 ofget_content_id()changes every time modules are loaded or unloaded. Hence, as long asget_content_id()values at the start and the end of the test are the same, the memory allocated by thedo_init_modulecall 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.
Quoted 1 line
Signed-off-by Abhishek Bapat <[email protected]>
Couple of improvement suggestions, but otherwise LGTM.
Quoted 121 lines
--- .../alloc_tag/allocinfo_ioctl_test.c | 198 +++++++++++++++++- 1 file changed, 197 insertions(+), 1 deletion(-)tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +102
-0@@ -5,6 +5,7 @@* Copyright (C) 2026 Google, Inc. */ +#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> @@ -313,11 +314,194 @@ 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, pos; +bool found; +const char *target_function ="do_init_module" ; +struct allocinfo_content_id start_cont_id, end_cont_id; +int retry =0 ; +const int max_retries =10 ; + +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_print_msg ("Failed to open " ALLOCINFO_PROC ": %s\n" ,strerror (errno)); + ret =KSFT_FAIL ; + goto freemem; + } + + do { + found = false; + pos = 0; + + if (__allocinfo_get_content_id(fd, &start_cont_id)) { + ksft_print_msg("allocinfo_get_content_id failed\n"); + ret = KSFT_FAIL; + goto exit; + } + + 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)) { + 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; + } + + 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; + + while (1) { + struct allocinfo_get_at get_at_params; + + memset(&get_at_params, 0, sizeof(get_at_params)); + memcpy(&get_at_params.filter, &filter, sizeof(filter)); + get_at_params.pos = pos; + + if (__allocinfo_get_at(fd, &get_at_params)) + break; + + tags->count = 0; + memcpy(&tags->tag[tags->count++], &get_at_params.data, + sizeof(get_at_params.data)); + + while (tags->count < VEC_MAX_ENTRIES && + __allocinfo_get_next(fd, &tags->tag[tags->count]) == 0) + tags->count++; + + for (i = 0; i < tags->count; i++) { + if (strcmp(tags->tag[i].tag.function, target_function) == 0) { + found = true; + break; + } + } + + if (found || tags->count < VEC_MAX_ENTRIES) + break; + + pos += tags->count; + } + + if (__allocinfo_get_content_id(fd, &end_cont_id)) { + ksft_print_msg("allocinfo_get_content_id failed\n"); + ret = KSFT_FAIL; + goto exit; + } + + if (start_cont_id.id == end_cont_id.id) + break; + + ksft_print_msg("Module load detected during size verification, retrying...\n");
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c static int test_function_filter(void) · line 427
Quoted 10 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +10
-0+ }while (retry++ < max_retries); + +if (start_cont_id.id == end_cont_id.id && !found) { +ksft_print_msg ("Entry with function %s not found in IOCTL results\n" , + target_function); + ret =KSFT_FAIL ; + }else if (start_cont_id.id != end_cont_id.id) { +ksft_print_msg ("Failed to match content_ids for procfs and IOCTL, skipping...\n" ); + ret =KSFT_SKIP ; + }
I know we found the tag filtering by target_size and this might be seen as unnecessary, but let's take one more final verification step and check that tag.counter.bytes of the tag we found indeed equals target_size. For that check you can replace "found" boolean with "found_tag" pointer and the rest becomes trivial.
Quoted 30 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +30
-0+ +exit: +close (fd); +freemem: +free (tags); +free (procfs_entries); +return ret; +} + +static int test_lineno_filter (void ) +{ +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 exit; + } + +memset (&filter,0 ,sizeof (filter)); + +if (get_filtered_procfs_entries (procfs_entries, &filter)) { +ksft_print_msg ("Error retrieving entries from " ALLOCINFO_PROC "\n" ); + ret =KSFT_FAIL ; + goto exit;
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c static int test_function_filter(void) · line 467
Quoted 10 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +10
-0+ } +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. +*/
The above comment is a bit unclear. What do you mean by "recycle the run_filter_test function"? Also there is no variable called ioctl_filter. I would change it to "filter later used in ioctl".
Quoted 14 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +14
-0+ 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,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 ;
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c static int test_function_filter(void) · line 491
Quoted 10 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +10
-0+ 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; + }
You can improve the test to also check if the filtered collection you retrieved contains the original tag you used to record target_lineno.
Quoted 38 lines
tools/testing/selftests/alloc_tag/allocinfo_ioctl_test.c +21
-2+ } + +exit: +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 )@@ -331,5 +515,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 (); } --2.55.0.rc0.786.g65d90a0328-goog