Re: [PATCH 1/7] xfs: write page faults in iomap are not buffered writes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Nov 02, 2022 at 09:12:05AM -0700, Darrick J. Wong wrote:
> On Tue, Nov 01, 2022 at 11:34:06AM +1100, Dave Chinner wrote:
> > From: Dave Chinner <dchinner@xxxxxxxxxx>
> > 
> > When we reserve a delalloc region in xfs_buffered_write_iomap_begin,
> > we mark the iomap as IOMAP_F_NEW so that the the write context
> > understands that it allocated the delalloc region.
> > 
> > If we then fail that buffered write, xfs_buffered_write_iomap_end()
> > checks for the IOMAP_F_NEW flag and if it is set, it punches out
> > the unused delalloc region that was allocated for the write.
> > 
> > The assumption this code makes is that all buffered write operations
> > that can allocate space are run under an exclusive lock (i_rwsem).
> > This is an invalid assumption: page faults in mmap()d regions call
> > through this same function pair to map the file range being faulted
> > and this runs only holding the inode->i_mapping->invalidate_lock in
> > shared mode.
> > 
> > IOWs, we can have races between page faults and write() calls that
> > fail the nested page cache write operation that result in data loss.
> > That is, the failing iomap_end call will punch out the data that
> > the other racing iomap iteration brought into the page cache. This
> > can be reproduced with generic/34[46] if we arbitrarily fail page
> > cache copy-in operations from write() syscalls.
> > 
> > Code analysis tells us that the iomap_page_mkwrite() function holds
> > the already instantiated and uptodate folio locked across the iomap
> > mapping iterations. Hence the folio cannot be removed from memory
> > whilst we are mapping the range it covers, and as such we do not
> > care if the mapping changes state underneath the iomap iteration
> > loop:
> > 
> > 1. if the folio is not already dirty, there is no writeback races
> >    possible.
> > 2. if we allocated the mapping (delalloc or unwritten), the folio
> >    cannot already be dirty. See #1.
> > 3. If the folio is already dirty, it must be up to date. As we hold
> >    it locked, it cannot be reclaimed from memory. Hence we always
> >    have valid data in the page cache while iterating the mapping.
> > 4. Valid data in the page cache can exist when the underlying
> >    mapping is DELALLOC, UNWRITTEN or WRITTEN. Having the mapping
> >    change from DELALLOC->UNWRITTEN or UNWRITTEN->WRITTEN does not
> >    change the data in the page - it only affects actions if we are
> >    initialising a new page. Hence #3 applies  and we don't care
> >    about these extent map transitions racing with
> >    iomap_page_mkwrite().
> > 5. iomap_page_mkwrite() checks for page invalidation races
> >    (truncate, hole punch, etc) after it locks the folio. We also
> >    hold the mapping->invalidation_lock here, and hence the mapping
> >    cannot change due to extent removal operations while we are
> >    iterating the folio.
> > 
> > As such, filesystems that don't use bufferheads will never fail
> > the iomap_folio_mkwrite_iter() operation on the current mapping,
> > regardless of whether the iomap should be considered stale.
> > 
> > Further, the range we are asked to iterate is limited to the range
> > inside EOF that the folio spans. Hence, for XFS, we will only map
> > the exact range we are asked for, and we will only do speculative
> > preallocation with delalloc if we are mapping a hole at the EOF
> > page. The iterator will consume the entire range of the folio that
> > is within EOF, and anything beyond the EOF block cannot be accessed.
> > We never need to truncate this post-EOF speculative prealloc away in
> > the context of the iomap_page_mkwrite() iterator because if it
> > remains unused we'll remove it when the last reference to the inode
> > goes away.
> 
> Why /do/ we need to trim the delalloc reservations after a failed
> write(), anyway?  I gather it's because we don't want to end up with a
> clean page backed by a delalloc reservation because writeback will never
> get run to convert that reservation into real space, which means we've
> leaked the reservation until someone dirties the page?

Yup, we leak the delalloc reservation and extent until the inode is
evicted from cache. What happens when reads/page faults happen over
that page from that point is now an interesting question - it might
all work correctly, but then again it might not...

> Ah.  Inode eviction also complains about inodes with delalloc
> reservations.  The blockgc code only touches cow fork mappings and
> post-eof blocks, which means it doesn't look for these dead/orphaned
> delalloc reservations either.

Yup, the inode eviction code that detects leaked delalloc blocks is
a useful canary - if it fires, it tends to indicate a bug in the
allocation/free code somewhere. If we allowed leaking delalloc
extents, we lose that canary.

And, yes, we'd need to make xfs_free_eofblocks() now search for
leaked delalloc blocks within EOF and punch them out, but then it
would have to either do the "search page cache for dirty pages" walk
or force writeback first.

None of these things feel like an improvement to me.

-Dave.

-- 
Dave Chinner
david@xxxxxxxxxxxxx



[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux