tools/mm/page_owner_sort: fix --sort, add module filter, improve usage
Subsystems (3)
cover tools/mm/page_owner_sort: fix --sort, add module filter, improve usage
From: Ye Liu <[email protected]>
This series improves the page_owner_sort tool with a bug fix, a new module-name feature, and better usage text.
Patch 1 fixes a long-standing bug where --sort was silently ignored when used without a short option (-a, -m, -p, etc.). The COMP_NO_FLAG case fell through to COMP_NUM and overwrote the sort conditions configured by parse_sort_args().
Patch 2 adds kernel module name support for sort, cull, and filter operations. Page owner stack traces already contain module names in the [module] format produced by %pS, but page_owner_sort had no way to use them. Records without module frames are assigned "vmlinux".
# Aggregate page usage per module ./page_owner_sort input.txt output.txt --cull=mod # Filter to records from xfs module only ./page_owner_sort input.txt output.txt --module xfs # Sort by module name, then by pid descending ./page_owner_sort input.txt output.txt --sort=mod,-pid
Patch 3 lists all available sort keys with abbreviations and examples directly in the --sort help section so users no longer need to read the source to discover valid keys.
Ye Liu (3):
tools/mm/page_owner_sort: fix --sort option being silently ignored tools/mm/page_owner_sort: add module name sort/cull/filter support tools/mm/page_owner_sort: show available sort keys in usage text
Documentation/mm/page_owner.rst | 8 +- tools/mm/page_owner_sort.c | 125 +++++++++++++++++++++++++++----- 2 files changed, 113 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 tools/mm/page_owner_sort: fix --sort option being silently ignored
From: Ye Liu <[email protected]>
When --sort is used without any short option (-a, -m, -p, etc.), compare_flag remains COMP_NO_FLAG. The switch (compare_flag) then falls through to the COMP_NUM case and calls set_single_cmp(), which unconditionally overwrites the sort conditions that parse_sort_args() already configured. This makes --sort silently ineffective unless a short option is also supplied.
Split COMP_NO_FLAG out of the COMP_NUM fallthrough so that --sort is respected when no short option is present.
Reproduction:
# Before fix: ascending order (ignored --sort=-pid) ./page_owner_sort --sort=-pid input.txt output.txt # After fix: descending order as expected
--- tools/mm/page_owner_sort.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/mm/page_owner_sort.c +4 -0
@@ -821,6 +821,10 @@ int main(int argc, char **argv)set_single_cmp (compare_stacktrace,SORT_ASC );break ;case COMP_NO_FLAG : +if (sc.size >0 ) +break ; +set_single_cmp (compare_num,SORT_DESC ); +break ;case COMP_NUM :set_single_cmp (compare_num,SORT_DESC );break ;
Signature · 1 line
2.25.1
PATCH 2/3 tools/mm/page_owner_sort: add module name sort/cull/filter support
In reply to Ye LiuFrom: Ye Liu <[email protected]>
Page owner stack traces already contain kernel module names in the "[module]" format produced by %pS, but page_owner_sort has no way to sort, cull, or filter by module.
Extract the first module name from each record's stack trace using the regex \[([a-zA-Z0-9_]+)\]. Records whose stack traces contain no module frames are assigned "vmlinux".
New options:
-M Sort by module name --sort=mod Sort by module name (supports +/- prefix) --cull=mod Cull (aggregate) by module name --module <modlist> Filter to records matching the given module(s)
The module field is also printed in cull output when relevant.
--- Documentation/mm/page_owner.rst | 8 ++- tools/mm/page_owner_sort.c | 112 ++++++++++++++++++++++++++------ 2 files changed, 100 insertions(+), 20 deletions(-)
Documentation/mm/page_owner.rst +7 -1
@@ -199,6 +199,7 @@ Usage -p Sort by pid. -P Sort by tgid. -n Sort by task command name. + -M Sort by module name. -r Sort by memory release time. -s Sort by stack trace. -t Sort by times (default).
@@ -240,8 +241,10 @@ Usage group ID numbers appear in <tgidlist>. --name <cmdlist> Select by task command name. This selects the blocks whose task command name appear in <cmdlist>. + --module <modlist> Select by module name. This selects the blocks whose + module name appear in <modlist>. - <pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list, + <pidlist>, <tgidlist>, <cmdlist>, <modlist> are single arguments in the form of a comma-separated list, which offers a way to specify individual selecting rules.
@@ -249,6 +252,7 @@ Usage ./page_owner_sort <input> <output> --pid=1 ./page_owner_sort <input> <output> --tgid=1,2,3 ./page_owner_sort <input> <output> --name name1,name2 + ./page_owner_sort <input> <output> --module xfs,ext4 STANDARD FORMAT SPECIFIERS ==========================
@@ -265,6 +269,7 @@ STANDARD FORMAT SPECIFIERS ft free_ts timestamp of the page when it was released at alloc_ts timestamp of the page when it was allocated ator allocator memory allocator for pages + mod module kernel module name For --cull option:
@@ -275,6 +280,7 @@ STANDARD FORMAT SPECIFIERS f free whether the page has been released or not st stacktrace stack trace of the page allocation ator allocator memory allocator for pages + mod module kernel module name Filtering page_owner output ============================
tools/mm/page_owner_sort.c +93 -19
282 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 3/3 tools/mm/page_owner_sort: show available sort keys in usage text
In reply to Ye LiuFrom: Ye Liu <[email protected]>
The --sort option accepts abbreviated or complete key names, but the usage text never listed them. Users had to read the source or the documentation to discover valid keys.
List all available keys (full form and abbreviation) with a brief description and examples directly in the --sort help section.
--- tools/mm/page_owner_sort.c | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/mm/page_owner_sort.c +9 -0
@@ -762,6 +762,15 @@ static void usage(void)"\t\t\targument in the form of a comma-separated list with some\n" "\t\t\tcommon fields predefined (pid, tgid, comm, stacktrace, allocator, module)\n" "--sort <order>\t\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n" +"\t\t\tAvailable keys:\n" +"\t\t\t pid(p), tgid(tg), name(n), stacktrace(st),\n" +"\t\t\t txt(T), alloc_ts(at), allocator(ator), module(mod)\n" +"\t\t\tThe \"+\" is optional since default direction is\n" +"\t\t\tincreasing numerical or lexicographic order.\n" +"\t\t\tMixed use of abbreviated and complete-form is allowed.\n" +"\t\t\tExamples:\n" +"\t\t\t --sort=n,+pid,-tgid\n" +"\t\t\t --sort=mod,at\n" ); }
Signature · 1 line
2.25.1
Andrew Morton
maintainerQuoted 22 lines · On Wed, 22 Jul 2026 10:37:23 +0800 Ye Liu <[email protected]> wrote:
This series improves the
page_owner_sorttool with a bug fix, a new module-name feature, and better usage text.Patch 1 fixes a long-standing bug where --sort was silently ignored when used without a short option (-a, -m, -p, etc.). The
COMP_NO_FLAGcase fell through toCOMP_NUMand overwrote the sort conditions configured byparse_sort_args().Patch 2 adds kernel module name support for sort, cull, and filter operations. Page owner stack traces already contain module names in the [module] format produced by %pS, but
page_owner_sorthad no way to use them. Records without module frames are assigned "vmlinux".# Aggregate page usage per module ./page_owner_sort input.txt output.txt --cull=mod # Filter to records from xfs module only ./page_owner_sort input.txt output.txt --module xfs # Sort by module name, then by pid descending ./page_owner_sort input.txt output.txt --sort=mod,-pid
Quoted 3 lines
Patch 3 lists all available sort keys with abbreviations and examples directly in the --sort help section so users no longer need to read the source to discover valid keys.
Thanks, these sound like nice changes.
AI review mentioned a couple of things, at least one of which appears legit:
https://sashiko.dev/#/patchset/[email protected]
Vlastimil Babka (SUSE)
reviewerQuoted 4 lines · Ye Liu
From: Ye Liu <[email protected]>
This series improves the
page_owner_sorttool with a bug fix, a new module-name feature, and better usage text.
For some reason, patches 1 and 3 have very limited recipient list compared to 0 and 2, please fix when sending a v2.
Quoted 32 lines
Patch 1 fixes a long-standing bug where --sort was silently ignored when used without a short option (-a, -m, -p, etc.). The
COMP_NO_FLAGcase fell through toCOMP_NUMand overwrote the sort conditions configured byparse_sort_args().Patch 2 adds kernel module name support for sort, cull, and filter operations. Page owner stack traces already contain module names in the [module] format produced by %pS, but
page_owner_sorthad no way to use them. Records without module frames are assigned "vmlinux".# Aggregate page usage per module ./page_owner_sort input.txt output.txt --cull=mod # Filter to records from xfs module only ./page_owner_sort input.txt output.txt --module xfs # Sort by module name, then by pid descending ./page_owner_sort input.txt output.txt --sort=mod,-pidPatch 3 lists all available sort keys with abbreviations and examples directly in the --sort help section so users no longer need to read the source to discover valid keys.
Ye Liu (3):
tools/mm/page_owner_sort: fix --sort option being silently ignoredtools/mm/page_owner_sort: add module name sort/cull/filter supporttools/mm/page_owner_sort: show available sort keys in usage textDocumentation/mm/page_owner.rst | 8 +- tools/mm/page_owner_sort.c | 125 +++++++++++++++++++++++++++----- 2 files changed, 113 insertions(+), 20 deletions(-)