fuse: prep patches before enabling large folios
Subsystems (2)
cover fuse: prep patches before enabling large folios
These are the remaining places where fuse assumes a folio is a single page, or that a page aligned range is also folio aligned. None of them changes behavior today, so there are no Fixes tags. Each becomes reachable the moment fuse enables large folios.
Patch 1 is the virtio-fs counterpart to "fuse: zero the correct range on a short read reply" [1]. That one is a fix because /dev/fuse serves fuseblk mounts, where the block size can already be smaller than the folio. virtio-fs is never block backed, so its block size is guaranteed to be PAGE_SIZE until large folios are enabled on fuse.
This series is on top of "fuse: don't drop destination page cache on a zero byte copy reply" [2]. Patch 2 touches the same hunk and relies on the bytes_copied guard from it.
Thanks,
Joanne
[1] https://lore.kernel.org/fuse-devel/[email protected]/
[2] https://lore.kernel.org/fuse-devel/[email protected]/
Joanne Koong (3):
virtio_fs: zero the correct range on a short read reply
fuse: drop rather than zero the copied range in copy_file_range()
fuse: replace folio_test_highmem() with folio_test_partial_kmap()
fs/fuse/dev.c | 3 ++- fs/fuse/file.c | 21 +++++++++++---------- fs/fuse/virtio_fs.c | 6 ++++-- 3 files changed, 17 insertions(+), 13 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/3 virtio_fs: zero the correct range on a short read reply
virtio_fs_request_complete() zeroes the unfilled tail of a short reply at [len, thislen) within the folio. That is only correct when the read started at the beginning of the folio, which is what the WARN_ON(descs[i].offset) above it documents.
The assumption holds on every configuration that exists today. A fuse read covers a sub-range of a folio only when iomap trims it to the blocks of a folio that are not uptodate, which requires the file system block size to be smaller than the folio. virtio-fs is not block backed, so its block size is PAGE_SIZE, an order-0 folio is a single block, and descs[i].offset is always 0.
Large folios make i_blocks_per_folio() greater than one and the assumption false. iomap_adjust_read_range() skips the blocks that are already uptodate at the front of the range and hands the position of the first block that has to be read to ->read_folio_range(), both when __iomap_write_begin() fills the blocks around an unaligned write and when a folio that is only partially uptodate is read - in which case one folio can take several reads at different offsets. fuse turns that position into offset_in_folio(folio, iter->pos) and stores it in the descriptor, so the tail that needs zeroing is
[offset + len, offset + thislen).
Zero that range and drop the WARN_ON(). This is a no-op today and a prerequisite for enabling large folios.
--- fs/fuse/virtio_fs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/fuse/virtio_fs.c +4 -2
@@ -798,9 +798,11 @@ static void virtio_fs_request_complete(struct fuse_req *req,for (i =0 ; i < ap->num_folios; i++) { thislen = ap->descs[i].length;if (len < thislen) { -WARN_ON (ap->descs[i].offset); +unsigned int offset = ap->descs[i].offset; + folio = ap->folios[i]; -folio_zero_segment (folio, len, thislen); +folio_zero_segment (folio, offset + len, + offset + thislen); len =0 ; }else { len -= thislen;
Signature · 1 line
2.52.0
PATCH 2/3 fuse: drop rather than zero the copied range in copy_file_range()
In reply to Joanne KoongAfter a FUSE_COPY_FILE_RANGE the destination page cache holds stale data, so __fuse_copy_file_range() clears it with truncate_inode_pages_range() over a page aligned range.
truncate_inode_pages_range() is built for ranges that are meant to read back as zeros. A folio that the range only partially covers is handed to truncate_inode_partial_folio(), which zeroes the covered bytes and then tries to split the folio so that the covered part can be dropped. Today every folio is a single page, and a page aligned range is therefore also folio aligned, so every folio in the range is covered in full and nothing is ever zeroed.
Once folios can be larger than a page, the range may not line up with folios (eg the folio holding the start of the range can begin before it and the folio holding the end can go beyond it). In this case, only the copied ranges inside those folios will get zeroed. If blocks within those folios are dirty, then splitting returns -EBUSY and truncate_inode_partial_folio() returns false. The folio stays in the page cache with the copied range zeroed, and those zeros are written back to the server, which overwrites the previously copied file range. __fuse_copy_file_range() issues a synchronous writeback call before calling truncate_inode_pages_range(), so in most cases the folio is not dirty, but there is still the possibility that a memory map raced with the copy (which is the case the comment in the code already calls out).
Instead of calling truncate_inode_pages_range(), use invalidate_inode_pages2_range(). It works in whole folios and never zeroes, and fuse_writeback_range() has already run on the destination, so the folios are normally clean and are simply dropped. It is also what the rest of fuse already uses when the cache has to be re-read from the server.
Outside the mmap race, the two are equivalent over the copied range for order-0 folios. Regarding the mmap race however, the old call (truncate_inode_pages_range()) loses the mmap changes but preserves the copied ones, whereas the new code (invalidate_inode_pages2_range()) launders the folio before dropping it, which means it loses the copied changes but preserves the mmap ones. Neither is entirely correct, and the (nontrivial) fix for both is the mapping->invalidate_lock described in the comment.
--- fs/fuse/file.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
fs/fuse/file.c +11 -10
@@ -3002,14 +3002,16 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,/* * Write out dirty pages in the destination file before sending the COPY -* request to userspace. After the request is completed, truncate off -* pages (including partial ones) from the cache that have been copied, -* since these contain stale data at that point. +* request to userspace. After the request is completed, drop the +* folios covering the copied range from the cache, since these contain +* stale data at that point. * -* This should be mostly correct, but if the COPY writes to partial -* pages (at the start or end) and the parts not covered by the COPY are +* This should be mostly correct, but if the COPY writes to a partial +* folio (at the start or end) and the parts not covered by the COPY are * written through a memory map after calling fuse_writeback_range(), -* then these partial page modifications will be lost on truncation. +* then that folio is laundered before it is dropped, so the memory map +* modifications are written back over the range the server just copied +* into and the copied data is lost. * * It is unlikely that someone would rely on such mixed style * modifications. Yet this does give less guarantees than if the
@@ -3063,10 +3065,9 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, }if (bytes_copied) -truncate_inode_pages_range (inode_out->i_mapping, -ALIGN_DOWN (pos_out,PAGE_SIZE ), -ALIGN (pos_out + bytes_copied, -PAGE_SIZE ) -1 ); +invalidate_inode_pages2_range (inode_out->i_mapping, + pos_out >>PAGE_SHIFT , + (pos_out + bytes_copied -1 ) >>PAGE_SHIFT );file_update_time (file_out);fuse_write_update_attr (inode_out, pos_out + bytes_copied, bytes_copied);
Signature · 1 line
2.52.0
PATCH 3/3 fuse: replace folio_test_highmem() with folio_test_partial_kmap()
In reply to Joanne Koongfuse_copy_folio() maps the folio with kmap_local_folio() and limits the copy to the rest of the mapped page when the folio is in highmem, since kmap_local_folio() only maps one page in that case.
folio_test_highmem() is not the most accurate test. On a highmem build with CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP, kmap_local_page() maps just one page even for lowmem folios, where folio_test_highmem() returns false (see __kmap_local_page_prot() in mm/highmem.c).
The other highmem.h helpers that walk more than one page of a folio (memcpy_from_folio(), memcpy_to_folio(), folio_zero_tail(), etc) all use folio_test_partial_kmap() instead of folio_test_highmem() as well. Fuse should use it too.
This is a no-op today since with order-0 folios, offset + count never exceeds PAGE_SIZE. It matters once a descriptor can span more than one page, which can happen once fuse enables large folios. Without this change, fuse_copy_do() would copy past the end of the mapping on a highmem build with CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP.
--- fs/fuse/dev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fuse/dev.c +2 -1
@@ -1288,7 +1288,8 @@ int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop,unsigned int copy = count;unsigned int bytes_copied; -if (folio_test_highmem (folio) && count >PAGE_SIZE -offset_in_page (offset)) +if (folio_test_partial_kmap (folio) && + count >PAGE_SIZE -offset_in_page (offset)) copy =PAGE_SIZE -offset_in_page (offset); bytes_copied =fuse_copy_do (cs, &buf, ©);
Signature · 1 line
2.52.0