Message ID | 20230202204428.3267832-2-willy@infradead.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp464636wrn; Thu, 2 Feb 2023 12:47:06 -0800 (PST) X-Google-Smtp-Source: AK7set8oBTwup9Mo5s43EqtRokR9D7X0c1Z+3ml34SDjalpU7QOp0KMGjxqVD9l7PPB0O1kJxXN0 X-Received: by 2002:a17:90a:34b:b0:22c:6b8e:1ccb with SMTP id 11-20020a17090a034b00b0022c6b8e1ccbmr7763332pjf.21.1675370825734; Thu, 02 Feb 2023 12:47:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675370825; cv=none; d=google.com; s=arc-20160816; b=wrL9Sas7dpoXEdcwZW3lF9v76BsC4GoXX2vvGCsDVMkTiK1XtLqWHsLfNTgOLb1DRh gjTjYrv0MeoCTSeoBiP3qUML60Rr7TMFERGsA6QBABfHJCQ4L9RRZ0XZhJ3WZCJIfNyp U6JudDSmp/VcarUiDjAK2vNBhrzfunoQDyQDU5AZ/xa6GurxGbwQ8p3K/9QjK+5P2iNk uETYGnBdiD9sZ0x2j486hkBK3S54YWRc5v7iJjYd0Mzx7oFRTLwRe7T9e8iBp67xmZel HQxdzU914XRd+rzGfN6q7VliSOraUFM7+eUUe1Y3mUfiM7xwu3rqlaMih6VIYa+N8tSj 4+ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3DLfbYeltsUE3NkYQxzmRoc7Gye1xXr71K3DBJBtYI8=; b=eJ0mKwDsjYhQJJc0zolUL5B00Fvv5/H8y7FOMW6t2UdrFkdL35LDKVEF0ACoBB3A7I XAkiFJWwPrdpV+J16WwfTACfCSkenOC8Az/ypZAQhbJUUyvbZh3tDNUv5O6QjrbedrTh nPs0Fr9lULr85JXvuh+x5c/gzqq2aslWdjAIdugdOu2//2h/rtpbdR3msDF68EDrtJKZ ZSG8AxgkrDQZdmmHwNfOA81mTN9GKYJjtZbvHuTk1cyYDzOtVSsH/pZ0bQe8UydhWfXy 9QWwmYbVMn0VpLqaMwPvL8c3+1T6tesbuUl3Syh+CAMkvcZkIvs5jtYp51iMYstMGrxY AtCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=My+pp2jn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lk16-20020a17090b33d000b00213ff43fdf1si6150871pjb.185.2023.02.02.12.46.53; Thu, 02 Feb 2023 12:47:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=My+pp2jn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233060AbjBBUov (ORCPT <rfc822;il.mystafa@gmail.com> + 99 others); Thu, 2 Feb 2023 15:44:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232111AbjBBUog (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 2 Feb 2023 15:44:36 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C76919EEA; Thu, 2 Feb 2023 12:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=3DLfbYeltsUE3NkYQxzmRoc7Gye1xXr71K3DBJBtYI8=; b=My+pp2jnzUtx0mU74aDrHBAfVv 47t6HILhQnY1KaWUmZhAh5Wqw/ZzWZ6ELcjVHzxBAaJVU/zPNhUYV+Oyu8b8NUlzlznAT7362AF5Q gSQhH0el02AH+zggm7MaQts0TQfCLVs/ET3zZFTx6kLY0UFy7JiwRH5IpJXhmrrpwqnp6dChipQdC PEYc0G6v9HjbUwlt4SWnW8UNsQnyNapLR7lOnkWFSFia65JmJyVQlKc6deSxOSY62WoNxa79NTi5h pyqzpXh91k9bcb2gbuVSWm4A6vmNmeqVjxhn6iRYRNaJwId/QFcOjPBC/JShhXajsyYYE1HBY8GMI sUEu9Ryw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNgRa-00Di7L-PL; Thu, 02 Feb 2023 20:44:30 +0000 From: "Matthew Wilcox (Oracle)" <willy@infradead.org> To: linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>, linux-afs@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Hugh Dickins <hughd@google.com>, linux-kernel@vger.kernel.org, fstests@vger.kernel.org Subject: [PATCH 1/5] truncate: Zero bytes after 'oldsize' if we're expanding the file Date: Thu, 2 Feb 2023 20:44:23 +0000 Message-Id: <20230202204428.3267832-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230202204428.3267832-1-willy@infradead.org> References: <20230202204428.3267832-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756753638700853729?= X-GMAIL-MSGID: =?utf-8?q?1756753638700853729?= |
Series |
Fix a minor POSIX conformance problem
|
|
Commit Message
Matthew Wilcox
Feb. 2, 2023, 8:44 p.m. UTC
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) <willy@infradead.org>
---
mm/truncate.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
Comments
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) <willy@infradead.org> > --- > 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 <dev> /mnt/ # xfs_io -c "mmap 0 1k" -c "mread -v 0 5" /mnt/file 00000000: 58 58 58 58 58 XXXXX Brian > } > EXPORT_SYMBOL(truncate_setsize); > > -- > 2.35.1 > >
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) <willy@infradead.org> > > --- > > 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 <dev> /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.
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); + } } EXPORT_SYMBOL(truncate_setsize);