From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 3B8C621CC5C for ; Thu, 30 Jul 2026 00:31:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785371500; cv=none; b=PQdmzeNvF59dG0BI/NJWaWyk6W/Zb7wBz/tAnihNakpV4vB/DleYpx5Fdj6MbkIjmkbSdndFfRSZLBVgCAlIahpyuu/hP8Apv1BAVNMrKTGhcfFBg02jQ+I1Z9s9uopNbtRHFSipzTclomn6+FDzjxRcSDMTOd3vycnuyRqD87Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785371500; c=relaxed/simple; bh=8ssVhEzDdyCLT+seqnQEUfaDZiNN/sexkzfx4Zh81zI=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=rU5gw4uEyWOG2w+oJ0TwGk7u1vAl8TfY9IBK67fgUZb4bm1ZZLOpXlkMqNBwcIaoZLTFCuMbxZfjvRCChF8zEaUzxSTtPOhDD/MiTSkJz8iEa6QLc1+EonqpgDZ7JIuIi+9Wm22ij9rTJd+yWYc38VzqlKQGVNa8taNdhz1RERk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=DMLoX7c+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="DMLoX7c+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF9371F00A3A; Thu, 30 Jul 2026 00:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1785371498; bh=W5hzEifNAICIGNN+PUDsMpr3lok91oEEG60TAB3VXM8=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=DMLoX7c+usdsyboVTHPWoLS8PUAu3rFTAKhUd0P1FAnsMdKXSbPc1Q+pL1eKZYNQ0 Yb4/cWXSDKG8WmFAlydSqS5t0uK+K78IHewKiPOG17mpOUxUbbaUxeIGhIj1jEDzt+ vFBgbHE27zF7jF4GKvQcCcTDiZR7+8uy7JSEQXwo= Date: Wed, 29 Jul 2026 17:31:38 -0700 From: Andrew Morton To: Warren Xiong Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tools/mm: prevent page_owner_sort from truncating input Message-Id: <20260729173138.fbc115b5285e1b465b055de7@linux-foundation.org> In-Reply-To: <20260729053812.882137-1-warren.xiong@ugreen.com> References: <20260729053812.882137-1-warren.xiong@ugreen.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 29 Jul 2026 13:38:12 +0800 Warren Xiong 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?