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 2591A3358AD for ; Thu, 30 Jul 2026 20:45:31 +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=1785444334; cv=none; b=dDm/GTWViYxYQqe58fxX4y+OOQ4aofB8tUP0mlAP3nP1jqUmvFmGUvDbIjiGTZkrxnNs8ivYNdnuz/ZgFmjuu366YKtuRnfMimtOMFRhd3KBFL1q1vHw2STK2H6rsD3wWHqgqxxEOKx9RPYK+7JUQS9u1Zze3CzPZJ0H2y6abEM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785444334; c=relaxed/simple; bh=mw1kMvv2SMMVuFGgeDuXPFKkJuNNPPV+xIgKvcTDrXs=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=OXeA6yufMAdx7IWFbLYWyoV6vbAVtFpzYb+u1kQjnAz7yShZXX/8TtXQCuJrhJg4mjLijIHRP9Ok6XNU/uvnG7Mz7Io05KX6/RjQ6bIi9zZ0r2PmrxdGedyGjrzTM2R7p6yNrNDQs5DOIx9eGHW0MqNKBi5iBS1AYDPHZ4G0ivA= 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=qIEyBDuP; 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="qIEyBDuP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 007E51F000E9; Thu, 30 Jul 2026 20:45:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1785444330; bh=NI8F24gX7xS4QrXS/zMAnM2js9tvRYhDpN/5mL44Xko=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=qIEyBDuP0qreEfcDzd6RI5Q25NWu2jQmgr+7oEK0ueFWBnplcT8X3oaTQB6QCSTVs uad4oDBHBd+ZgfLc8u+8yXU0b3ZfI923aoKC5wPZczi0W2SwjhY/0iNFd1tErepElB KLMv8mhQb3/iAVdfWgcGqZE0ndZGKQbG0ByiI5mk= Date: Thu, 30 Jul 2026 13:45:29 -0700 From: Andrew Morton To: Warren Xiong Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] tools/mm: prevent page_owner_sort from truncating input Message-Id: <20260730134529.29151e44a6da7430a4caf10c@linux-foundation.org> In-Reply-To: <20260730015809.3819606-1-warren.xiong@ugreen.com> References: <20260729053812.882137-1-warren.xiong@ugreen.com> <20260730015809.3819606-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 Thu, 30 Jul 2026 09:58:09 +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. > > 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. Far nicer, thanks. However I spotted a thing which Sashiko missed! > @@ -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); > + } usage() calls printf(), which can set errno. So perror() might print the wrong thing. Not that I care very much ;)