From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 727B2C64EC3 for ; Fri, 3 Feb 2023 15:07:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 085D56B0071; Fri, 3 Feb 2023 10:07:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 036596B0072; Fri, 3 Feb 2023 10:07:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1A1D6B0074; Fri, 3 Feb 2023 10:07:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D244A6B0071 for ; Fri, 3 Feb 2023 10:07:12 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9198EC0433 for ; Fri, 3 Feb 2023 15:07:12 +0000 (UTC) X-FDA: 80426308704.30.061E224 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf11.hostedemail.com (Postfix) with ESMTP id 64AA740016 for ; Fri, 3 Feb 2023 15:07:10 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=QTLBPiQN; spf=none (imf11.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675436831; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DFUkqlIb1koR34QSR5KYPFYQ+oC2ej0KqthqG2wrzL8=; b=GTIIOZDVByrsfarepiyxZCeIuXeLnm1hojJTCCDRGLIoNYAaJKcK3Iqk1XYZB3LvobhdbA YjpRsuOz7PBHegse3m3nq5Z509VDwnvaDViiwlJWLGb5ELJ785ykU6CxZvS07MSK8JkU0v VMio6aTx3tHVushlhraRw5KOu4HYpks= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=QTLBPiQN; spf=none (imf11.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675436831; a=rsa-sha256; cv=none; b=R50yE/zAFoX35WbCanHUFT3QaW7j6V0I4wSxxl+3/Cz1PQgY/7iDV5An4Llvuxu+jvvQDG Lh7rc6PhJFfseSJIHZ46l9yw2lnJKECX1tc+Xpr0WMvHVlHB31gayszabIya+rWni/0wJe ptLUJdY6dCJqMV2ae/b8EOsW+mPuDb8= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=DFUkqlIb1koR34QSR5KYPFYQ+oC2ej0KqthqG2wrzL8=; b=QTLBPiQNVGLCOGdfPgtGITnHy8 VdbEIQzfUpVRnoHj6uEvO9vNTNXB3Y5GAKE52fN3jq0ciwAKlSiiJniPLlZ9mW7lpssuUH3AHoauX tZVG9s5oncmxw8GO3wfZRHq52EYoApCHer2IBgXz0X8NmYslkeLdNfL7irqEC8W1Hl1CskyZtzQk3 di4ZfKsWGctoXkMvrHi/SoFhY8QeTD9OaEUlRQ6qGCXYvAaL8p8cvhaNwgS3XvjDjxRXDUWl9PdWk gclC2AyLWzE0WqfI2Atf8bmUX8wg49kdHjsRVo0OPzlYU1ARyDAyaa6dpiJLpDabruhiyI+kS6Ylf Qbl9VHoQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNxeb-00EOZb-KF; Fri, 03 Feb 2023 15:07:05 +0000 Date: Fri, 3 Feb 2023 15:07:05 +0000 From: Matthew Wilcox To: Brian Foster Cc: linux-fsdevel@vger.kernel.org, linux-afs@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Hugh Dickins , linux-kernel@vger.kernel.org, fstests@vger.kernel.org Subject: Re: [PATCH 1/5] truncate: Zero bytes after 'oldsize' if we're expanding the file Message-ID: References: <20230202204428.3267832-1-willy@infradead.org> <20230202204428.3267832-2-willy@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 64AA740016 X-Stat-Signature: gm9by8hj74qipx7a948qex9waoebuwx6 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675436830-621833 X-HE-Meta: U2FsdGVkX19NrIx2dseYH3ywiwRPYkUeEsBFbvNWpEm7/ll446l3nmnn7OKiunHD9aeoUC/Lnwv0wJfGx0hVPs2AtC9e/EprQPT2DIDJxCkSlIyxlY/0W11kQVhz6eSyQZUaA/Ho7umE6a5ns9ciyr2fANhX634XuImX1O90uecssLJoNzSr1jUDy8bBZLOn8UHkESv9ly3s7m+bSy15GabxSg529be0EI1VNppEbWhIN1xGYscbrbA4roGE9OrT/PhovoBFk+17kHLkQPBH6fkiqEaMjQRyFUp4fsGm3fxYxxbSx773RvIHp2dDdNuAgk1ag1A9A24Cv93yhztT9PrKxxJWpZ8BU3flGcSOKp4o8X4TXByfz8O8Pc/cPDihlrPoSmQmm7BMRMZXDNRmneEnUgyANBdCpctWphgNW30+Ni92HGt1b3FBlCcUDAk31LJEa3XO2oE+tfLtTA9F6wKX9TbruXtOCAfS/kyfWL+w0nKtlm214u98Y5DrIGI1SyFIMEB24/bwlj6dkpqwVicHdGOdhXzpEVT9ojWxx40SrW/KtR1M1lNOBiWCJrYAqf5IIj0mlAYbKiC5dttfk6E57btsPIEBDRPAvqotandwRaiKF2UTF6rHo77OB9GyCu4U9/l6JAjKwHG3UEiH+hXZO7qr+DGww5jON19XpaCdPrwhGQKgFLtM5yeTtbCbdUElyNFdnDioWxkDnoVdHQR+iJ3pSq8v016U0L6Op1wWgz+dyH4K00QYXnFpDeAkHjkOSLYCYvTXMa55Ob12QErnZoEDO0/pb70EXLQiAGNPCLX/f9qDeckYXCn8NlLzi0p8VvaM1iDpJ5SAFK6VT1S670KZb5Ar95IbXDYsUgvnHlkze18VdAUeXdHt3g383WACaxADYvprt9EYX/+fLGCKSjQmlUY0YUQ5Bo4tP8FezdxVXCtuaLzYk4dsiyWTbblVULVTyHEtD/MfEaT VJe1YsNo jw60bz6IOt191eK4CD09WhS6ia+pM7aWcMfKmQwgTFZ1Gv6PggL+ayto/8kcG+YGTqAO2s4H7r8zk+FMfKK3fIJ7xZYyLnMdtkzaOGP9skwZmHdEfb6AzNJSZk5BuKLE+QUOq8ezwFuQvFE/asm2Qv9yagdEnTTVwFoVq4kf9dyTO7lqbVkzNl+f/Pbyuj810/5c3B1zTY3aS+9b1Pm/KPMXFRA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Feb 03, 2023 at 08:00:16AM -0500, Brian Foster wrote: > On Thu, Feb 02, 2023 at 08:44:23PM +0000, Matthew Wilcox (Oracle) wrote: > > POSIX requires that "If the file size is increased, the extended area > > shall appear as if it were zero-filled". It is possible to use mmap to > > write past EOF and that data will become visible instead of zeroes. > > This fixes the problem for the filesystems which simply call > > truncate_setsize(). More complex filesystems will need their own > > patches. > > > > Signed-off-by: Matthew Wilcox (Oracle) > > --- > > mm/truncate.c | 7 +++++-- > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > diff --git a/mm/truncate.c b/mm/truncate.c > > index 7b4ea4c4a46b..cebfc5415e9a 100644 > > --- a/mm/truncate.c > > +++ b/mm/truncate.c > > @@ -763,9 +763,12 @@ void truncate_setsize(struct inode *inode, loff_t newsize) > > loff_t oldsize = inode->i_size; > > > > i_size_write(inode, newsize); > > - if (newsize > oldsize) > > + if (newsize > oldsize) { > > pagecache_isize_extended(inode, oldsize, newsize); > > - truncate_pagecache(inode, newsize); > > + truncate_pagecache(inode, oldsize); > > + } else { > > + truncate_pagecache(inode, newsize); > > + } > > I don't think this alone quite addresses the problem. Looking at ext4 > for example, if the eof page is dirty and writeback occurs between the > i_size update (because writeback also zeroes the post-eof portion of the > page) and the truncate_setsize() call, we end up with pagecache > inconsistency because pagecache truncate doesn't dirty the page it > zeroes. > > So for example, with this series plus a nefariously placed > filemap_flush() in ext4_setattr(): > > # xfs_io -fc "truncate 1" -c "mmap 0 1k" -c "mwrite 0 10" -c "truncate 5" -c "mread -v 0 5" /mnt/file > 00000000: 58 00 00 00 00 X.... > # umount /mnt/; mount /mnt/ > # xfs_io -c "mmap 0 1k" -c "mread -v 0 5" /mnt/file > 00000000: 58 58 58 58 58 XXXXX Hm, so switch the order of i_size_write() and truncate_pagecache()? There could still be a store between old-EOF and new-EOF from another thread, which would then be visible, but I don't think you could prove that store should have been zeroed. Not from the thread doing the ftruncate() anyway -- I think the thread doing the store could prove it, but that thread is relying on undefined behaviour anyway.