tools/mm/page_owner_sort: return explicit filter results

Patch series "tools/mm/page_owner_sort: fix filtering and cleanup issues",
v5.

Patch 1 renames is_need() to filter_record() and makes the filter path
return explicit results.  Patch 2 fixes the per-record allocation leaks. 
Patch 3 bounds search_pattern() output copies, addressing the pre-existing
issue reported by Sashiko/AI review.


This patch (of 3):

Rename is_need() to filter_record() and make the filter path return
explicit error, skip, and match results.  This lets callers distinguish
allocation failures from records that simply do not match active filters.

Link: https://lore.kernel.org/20260629014316.130307-1-chenyichong@uniontech.com
Link: https://lore.kernel.org/20260629014316.130307-2-chenyichong@uniontech.com
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
Reviewed-by: Vishal Moola <vishal.moola@gmail.com>
Cc: Ye Liu <ye.liu@linux.dev>
Cc: Zhen Ni <zhen.ni@easystack.cn>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Yichong Chen 2026-06-29 09:43:14 +08:00 committed by Andrew Morton
parent 6251d650f4
commit aef750043c

View File

@ -43,6 +43,13 @@ enum FILTER_BIT {
FILTER_TGID = 1<<2,
FILTER_COMM = 1<<3
};
enum FILTER_RESULT {
FILTER_ERROR,
FILTER_SKIP,
FILTER_MATCH
};
enum CULL_BIT {
CULL_PID = 1<<1,
CULL_TGID = 1<<2,
@ -372,6 +379,9 @@ static char *get_comm(char *buf)
{
char *comm_str = malloc(TASK_COMM_LEN);
if (!comm_str)
return NULL;
memset(comm_str, 0, TASK_COMM_LEN);
search_pattern(&comm_pattern, comm_str, buf);
@ -450,32 +460,44 @@ static bool match_str_list(const char *str, char **list, int list_size)
return false;
}
static bool is_need(char *buf)
static enum FILTER_RESULT filter_record(char *buf)
{
char *comm;
if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
return false;
return FILTER_SKIP;
if ((filter & FILTER_TGID) &&
!match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
return false;
return FILTER_SKIP;
if (!(filter & FILTER_COMM))
return FILTER_MATCH;
char *comm = get_comm(buf);
comm = get_comm(buf);
if (!comm)
return FILTER_ERROR;
if ((filter & FILTER_COMM) &&
!match_str_list(comm, fc.comms, fc.comms_size)) {
if (!match_str_list(comm, fc.comms, fc.comms_size)) {
free(comm);
return false;
return FILTER_SKIP;
}
free(comm);
return true;
return FILTER_MATCH;
}
static bool add_list(char *buf, int len, char *ext_buf)
{
enum FILTER_RESULT filter_result;
if (list_size == max_size) {
fprintf(stderr, "max_size too small??\n");
return false;
}
if (!is_need(buf))
filter_result = filter_record(buf);
if (filter_result == FILTER_ERROR) {
fprintf(stderr, "Out of memory\n");
return false;
}
if (filter_result == FILTER_SKIP)
return true;
list[list_size].pid = get_pid(buf);
list[list_size].tgid = get_tgid(buf);