From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-m3288.qiye.163.com (mail-m3288.qiye.163.com [220.197.32.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF9914779B0 for ; Thu, 30 Apr 2026 16:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.32.88 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777567105; cv=none; b=lC2/mr7qgg3rcvD2m1PRPCoiJ84i2mnkcMzBzCJsPLwlUrg3zhQXy0NyRhqvyI1hnUFrOGuOhv9yx2+zAfgtWfL1Q9WYWTRHh5PEjm7Vmse33j/efGrh7N4LL7BrPm/JN+tFofPnq9JuI95HU69Guoo7Kx8F2kTD0IsDz/1Yrbc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777567105; c=relaxed/simple; bh=f/Z8tzepaAqzUzsdflGUleBOH8UZXSIcXbC7UTrcYSA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TIiX6YGsZNvG0Wp0WyJc/jM7U0UtIO0mjccQwz1fmh6EGGbqy53xe2MSnvjI6ZTkUVdP27gxhBo0kODv5q8gw5g1MG2uABuF5QPyZiVQjhfHWiUlXT9TCAxSnhZI7aFDRYznw/DZANr34XnIxUmWNdYzmOA3b9IxJKkRVozQmcE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=easystack.cn; spf=pass smtp.mailfrom=easystack.cn; arc=none smtp.client-ip=220.197.32.88 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=easystack.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=easystack.cn Received: from localhost.localdomain (unknown [IPV6:2409:8a20:e24:8c24:8810:8f74:8c26:2]) by smtp.qiye.163.com (Hmail) with ESMTP id 199a96d4f; Fri, 1 May 2026 00:32:59 +0800 (GMT+08:00) From: Zhen Ni To: akpm@linux-foundation.org, vbabka@kernel.org Cc: surenb@google.com, mhocko@suse.com, jackmanb@google.com, hannes@cmpxchg.org, ziy@nvidia.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhen Ni Subject: [PATCH v4 1/3] mm/page_owner: add print_mode filter Date: Fri, 1 May 2026 00:32:45 +0800 Message-Id: <20260430163247.13628-2-zhen.ni@easystack.cn> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20260430163247.13628-1-zhen.ni@easystack.cn> References: <20260430163247.13628-1-zhen.ni@easystack.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-HM-Tid: 0a9ddf3c86c00229kunm7d32d45d8a83 X-HM-MType: 1 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFJQjdXWRgWCB1ZQUpXWS1ZQUlXWQ8JGhUIEh9ZQVlDThlMVkwaGR1KGBhKSRpLGVYVFA kWGhdVGRETFhoSFyQUDg9ZV1kYEgtZQVlJT0tCQUMaSUtBHklPQUMYSU9BQ0NKS0FDHUxPQUMYSU 1BSVlXWRYaDxIVHRRZQVlPS0hVSktJT09PSFVKS0tVSkJLS1kG Add a print_mode filter to page_owner that allows users to choose between printing full stack traces or only stack handles, significantly reducing output size for debugging and analysis. The filter provides a string-based interface under /sys/kernel/debug/page_owner_filter/: - Reading shows the current mode with [] brackets around active option - Writing accepts "full_stack" or "stack_handle" strings The default full_stack mode maintains backward compatibility with existing usage, displaying complete stack traces for each page allocation. The stack_handle mode dramatically reduces log size by showing only the handle number instead of the full stack trace. The mapping from handles to actual stack traces can be obtained via the show_stacks_handles interface. Example usage: # echo stack_handle > /sys/kernel/debug/page_owner_filter/print_mode # cat /sys/kernel/debug/page_owner_filter/print_mode full_stack [stack_handle] # cat /sys/kernel/debug/page_owner Page allocated via order 0, migratetype Unmovable, gfp_mask 0x1100ca, pid 1, tgid 1 (systemd), ts 123456789 ns PFN 0x1000 type Unmovable Block 1 type Unmovable Flags 0x3fffe800000084(referenced|lru|active|private|node=0|zone=1) handle: 17432583 ... Signed-off-by: Zhen Ni --- Changes in v4: - Change from numeric (0/1) to string-based interface ("full_stack"/"stack_handle") - Merge infrastructure patch into this patch Changes in v3: - No code changes Changes in v2: - Renamed from 'compact mode' to 'print_mode' for better clarity - Use enum values (0=full_stack, 1=stack_handle) instead of boolean - Update debugfs filename from 'compact' to 'print_mode' --- mm/page_owner.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 8178e0be557f..28766c854d02 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -54,6 +55,24 @@ struct stack_print_ctx { u8 flags; }; +enum page_owner_print_mode { + PAGE_OWNER_PRINT_FULL_STACK, + PAGE_OWNER_PRINT_STACK_HANDLE, +}; + +static const char * const page_owner_print_mode_strings[] = { + [PAGE_OWNER_PRINT_FULL_STACK] = "full_stack", + [PAGE_OWNER_PRINT_STACK_HANDLE] = "stack_handle", +}; + +struct page_owner_filter { + enum page_owner_print_mode print_mode; +}; + +static struct page_owner_filter owner_filter = { + .print_mode = PAGE_OWNER_PRINT_FULL_STACK, +}; + static bool page_owner_enabled __initdata; DEFINE_STATIC_KEY_FALSE(page_owner_inited); @@ -575,7 +594,11 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, migratetype_names[pageblock_mt], &page->flags); - ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); + if (READ_ONCE(owner_filter.print_mode) == PAGE_OWNER_PRINT_STACK_HANDLE) { + ret += scnprintf(kbuf + ret, count - ret, + "handle: %d\n", handle); + } else + ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); if (ret >= count) goto err; @@ -970,10 +993,71 @@ static int page_owner_threshold_set(void *data, u64 val) DEFINE_SIMPLE_ATTRIBUTE(page_owner_threshold_fops, &page_owner_threshold_get, &page_owner_threshold_set, "%llu"); +static ssize_t print_mode_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + const char *output; + int mode; + + mode = READ_ONCE(owner_filter.print_mode); + + if (mode == PAGE_OWNER_PRINT_FULL_STACK) + output = "[full_stack] stack_handle\n"; + else + output = "full_stack [stack_handle]\n"; + + return simple_read_from_buffer(buf, count, ppos, output, strlen(output)); +} + +static ssize_t print_mode_write(struct file *file, + const char __user *buf, + size_t count, loff_t *ppos) +{ + char *kbuf; + int mode; + int ret = count; + + /* + * Limit input size. Maximum valid input is "stack_handle" (12 chars) + * plus newline and null terminator. Use 32 bytes as a reasonable limit. + */ + if (count > 32) + return -EINVAL; + + kbuf = kmalloc(count + 1, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + + if (strncpy_from_user(kbuf, buf, count) < 0) { + ret = -EFAULT; + goto out_free; + } + kbuf[count] = '\0'; + + mode = sysfs_match_string(page_owner_print_mode_strings, kbuf); + if (mode < 0) { + ret = -EINVAL; + goto out_free; + } + + WRITE_ONCE(owner_filter.print_mode, mode); + +out_free: + kfree(kbuf); + return ret; +} + +static const struct file_operations page_owner_print_mode_fops = { + .owner = THIS_MODULE, + .read = print_mode_read, + .write = print_mode_write, + .llseek = default_llseek, +}; + static int __init pageowner_init(void) { - struct dentry *dir; + struct dentry *dir, *filter_dir; if (!static_branch_unlikely(&page_owner_inited)) { pr_info("page_owner is disabled\n"); @@ -981,6 +1065,11 @@ static int __init pageowner_init(void) } debugfs_create_file("page_owner", 0400, NULL, NULL, &page_owner_fops); + + filter_dir = debugfs_create_dir("page_owner_filter", NULL); + debugfs_create_file("print_mode", 0600, filter_dir, NULL, + &page_owner_print_mode_fops); + dir = debugfs_create_dir("page_owner_stacks", NULL); debugfs_create_file("show_stacks", 0400, dir, (void *)(STACK_PRINT_FLAG_STACK | -- 2.20.1