From: Warren Xiong <warren.xiong@ugreen.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Warren Xiong <warren.xiong@ugreen.com>
Subject: [PATCH v2] tools/mm: prevent page_owner_sort from truncating input
Date: Thu, 30 Jul 2026 09:58:09 +0800 [thread overview]
Message-ID: <20260730015809.3819606-1-warren.xiong@ugreen.com> (raw)
In-Reply-To: <20260729053812.882137-1-warren.xiong@ugreen.com>
page_owner_sort opens the output file with "w" before reading the input.
If both paths refer to the same file, this truncates the input and the
tool silently processes zero records before returning success.
Delay opening the output file until all input records have been loaded
into memory. This allows the tool to sort a file in place without
truncating data before it has been consumed.
Signed-off-by: Warren Xiong <warren.xiong@ugreen.com>
---
Changes in v2:
- Delay opening the output file until after all input has been loaded, as
suggested by Andrew Morton.
- Allow in-place sorting instead of rejecting matching input and output
files.
Tested with distinct paths, the same path, a hard-link alias, and a
symbolic-link alias.
Link to v1: https://lore.kernel.org/r/20260729053812.882137-1-warren.xiong@ugreen.com
tools/mm/page_owner_sort.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index 35d3d2549..22b3b500d 100644
--- a/tools/mm/page_owner_sort.c
+++ b/tools/mm/page_owner_sort.c
@@ -836,8 +836,7 @@ int main(int argc, char **argv)
}
fin = fopen(argv[optind], "r");
- fout = fopen(argv[optind + 1], "w");
- if (!fin || !fout) {
+ if (!fin) {
usage();
perror("open: ");
exit(1);
@@ -874,6 +873,13 @@ int main(int argc, char **argv)
goto out_free;
}
+ fout = fopen(argv[optind + 1], "w");
+ if (!fout) {
+ usage();
+ perror("open: ");
+ exit(1);
+ }
+
printf("loaded %d\n", list_size);
printf("sorting ....\n");
--
2.39.5
next prev parent reply other threads:[~2026-07-30 1:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 5:38 [PATCH] " Warren Xiong
2026-07-30 0:31 ` Andrew Morton
2026-07-30 1:30 ` 熊卧龙
2026-07-30 1:58 ` Warren Xiong [this message]
2026-07-30 20:45 ` [PATCH v2] " Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260730015809.3819606-1-warren.xiong@ugreen.com \
--to=warren.xiong@ugreen.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®