Re: [PATCH] tools/mm: prevent page_owner_sort from truncating input
From: Andrew Morton
Date: Wed Jul 29 2026 - 20:32:59 EST
On Wed, 29 Jul 2026 13:38:12 +0800 Warren Xiong <warren.xiong@xxxxxxxxxx> wrote:
> 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.
Don't do that then? I mean, is there any expectation that this works?
hp2:/usr/src/25> ls -l x
-rw-rw-r-- 1 akpm akpm 55318 Jul 29 17:27 x
hp2:/usr/src/25> sort -o x x
hp2:/usr/src/25> ls -l x
-rw-rw-r-- 1 akpm akpm 55318 Jul 29 17:27 x
Oh. sort(1) does that. I never knew.
> Open the output without truncating it and compare the device and inode
> numbers of the opened files. Reject matching regular files, then truncate
> regular output files only after the check. This also detects hard link and
> symbolic link aliases while preserving non-regular outputs such as
> /dev/stdout.
This all sounds complex. How does sort(1) do it? Write to a temporary
file then a rename?
For page_owner_sort I expect we could load all the input into memory
before opening the output file?