[PATCH 3/8] mm/page_owner: Add COMM filtering with wildcard support
From: Zhen Ni
Date: Thu Aug 27 2026 - 23:29:29 EST
Add process name (COMM) filtering to page_owner with glob-style wildcard
pattern matching support. Users can now filter page_owner output by
process names using flexible patterns.
Supported wildcards:
* : matches any sequence of characters
? : matches any single character
[abc]: matches any character in the set
[a-z]: matches any character in the range
Examples:
comm=python* : matches python, python3, python3.9, etc.
comm=*sh : matches bash, zsh, dash, etc.
Signed-off-by: Zhen Ni <zhen.ni@xxxxxxxxxxxx>
---
mm/page_owner.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 79 insertions(+), 3 deletions(-)
diff --git a/mm/page_owner.c b/mm/page_owner.c
index 077867e9e572..7cd533682054 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -14,6 +14,7 @@
#include <linux/sched/clock.h>
#include <linux/bsearch.h>
#include <linux/sort.h>
+#include <linux/glob.h>
#include "page_alloc.h"
@@ -72,6 +73,7 @@ static const char * const page_owner_print_mode_strings[] = {
#define PID_MAX_DIGITS 7
#define MAX_FILTER_PIDS 16
#define MAX_FILTER_TGIDS 16
+#define MAX_FILTER_COMMS 8
struct page_owner_filter_state {
enum page_owner_print_mode print_mode;
@@ -80,8 +82,10 @@ struct page_owner_filter_state {
nodemask_t nid_filter;
int pid_count;
int tgid_count;
+ int comm_count;
pid_t pid_list[MAX_FILTER_PIDS];
pid_t tgid_list[MAX_FILTER_TGIDS];
+ char comm_list[MAX_FILTER_COMMS][TASK_COMM_LEN];
};
static int cmp_pid_t(const void *a, const void *b)
@@ -854,6 +858,20 @@ read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
sizeof(pid_t),
cmp_pid_t) != NULL;
+ if (!proc_match && state->comm_count > 0) {
+ bool comm_match = false;
+ int i;
+
+ for (i = 0; i < state->comm_count; i++) {
+ if (glob_match(state->comm_list[i],
+ page_owner->comm)) {
+ comm_match = true;
+ break;
+ }
+ }
+ proc_match = comm_match;
+ }
+
if (!proc_match)
goto ext_put_continue;
}
@@ -1007,6 +1025,38 @@ static int parse_pid_t_list(const char *str, pid_t *list, int *count)
return ret;
}
+static int parse_comm_list(const char *str, char (*list)[TASK_COMM_LEN], int *count)
+{
+ char *str_copy, *token;
+ int i = 0;
+ int ret = 0;
+
+ str_copy = kstrdup(str, GFP_KERNEL);
+ if (!str_copy)
+ return -ENOMEM;
+
+ while ((token = strsep(&str_copy, ",")) != NULL) {
+ token = strstrip(token);
+ if (*token == '\0')
+ continue;
+ if (i >= MAX_FILTER_COMMS) {
+ ret = -E2BIG;
+ goto out_free;
+ }
+ strscpy(list[i], token, TASK_COMM_LEN);
+ i++;
+ }
+
+ *count = i;
+
+ if (i == 0)
+ ret = -EINVAL;
+
+out_free:
+ kfree(str_copy);
+ return ret;
+}
+
static ssize_t page_owner_write(struct file *file,
const char __user *buf,
size_t count, loff_t *ppos)
@@ -1022,8 +1072,10 @@ static ssize_t page_owner_write(struct file *file,
bool new_proc_filter_enabled;
pid_t new_pid_list[MAX_FILTER_PIDS];
pid_t new_tgid_list[MAX_FILTER_TGIDS];
+ char (*new_comm_list)[TASK_COMM_LEN] = NULL;
int new_pid_count = 0;
int new_tgid_count = 0;
+ int new_comm_count = 0;
/*
* Maximum input length for filter commands:
@@ -1035,12 +1087,19 @@ static ssize_t page_owner_write(struct file *file,
*/
if (count > 32 + 6 * MAX_NUMNODES +
(PID_MAX_DIGITS + 1) * MAX_FILTER_PIDS + 4 +
- (PID_MAX_DIGITS + 1) * MAX_FILTER_TGIDS + 5)
+ (PID_MAX_DIGITS + 1) * MAX_FILTER_TGIDS + 5 +
+ TASK_COMM_LEN * MAX_FILTER_COMMS + 5)
return -EINVAL;
+ new_comm_list = kmalloc_array(MAX_FILTER_COMMS, TASK_COMM_LEN, GFP_KERNEL);
+ if (!new_comm_list)
+ return -ENOMEM;
+
kbuf = memdup_user_nul(buf, count);
- if (IS_ERR(kbuf))
+ if (IS_ERR(kbuf)) {
+ kfree(new_comm_list);
return PTR_ERR(kbuf);
+ }
orig = kbuf;
@@ -1056,6 +1115,11 @@ static ssize_t page_owner_write(struct file *file,
memcpy(new_tgid_list, state->tgid_list, sizeof(state->tgid_list));
new_tgid_count = state->tgid_count;
}
+ if (state->comm_count > 0) {
+ memcpy(new_comm_list, state->comm_list,
+ state->comm_count * TASK_COMM_LEN);
+ new_comm_count = state->comm_count;
+ }
while ((token = strsep(&kbuf, " \t\n")) != NULL) {
if (*token == '\0')
@@ -1095,6 +1159,10 @@ static ssize_t page_owner_write(struct file *file,
ret = parse_pid_t_list(token + 5, new_tgid_list, &new_tgid_count);
if (ret < 0)
goto out_free;
+ } else if (!strncmp(token, "comm=", 5)) {
+ ret = parse_comm_list(token + 5, new_comm_list, &new_comm_count);
+ if (ret < 0)
+ goto out_free;
} else {
ret = -EINVAL;
goto out_free;
@@ -1105,7 +1173,9 @@ static ssize_t page_owner_write(struct file *file,
state->print_mode = new_print_mode;
state->nid_filter = new_nid_filter;
state->nid_filter_enabled = new_nid_filter_enabled;
- state->proc_filter_enabled = new_pid_count > 0 || new_tgid_count > 0;
+ state->proc_filter_enabled = new_pid_count > 0 ||
+ new_tgid_count > 0 ||
+ new_comm_count > 0;
if (new_pid_count > 0) {
memcpy(state->pid_list, new_pid_list, sizeof(state->pid_list));
state->pid_count = new_pid_count;
@@ -1120,10 +1190,16 @@ static ssize_t page_owner_write(struct file *file,
if (state->tgid_count > 1)
sort(state->tgid_list, state->tgid_count, sizeof(pid_t),
cmp_pid_t, NULL);
+ if (new_comm_count > 0) {
+ memcpy(state->comm_list, new_comm_list,
+ new_comm_count * TASK_COMM_LEN);
+ state->comm_count = new_comm_count;
+ }
ret = count;
out_free:
+ kfree(new_comm_list);
kfree(orig);
return ret;
}
--
2.20.1