[PATCH 1/3] afs: Handle len being extending over page end in write_begin/write_end

From: David Howells
Date: Mon Jun 14 2021 - 09:20:32 EST


With transparent huge pages, in the future, write_begin() and write_end()
may be passed a length parameter that, in combination with the offset into
the page, exceeds the length of that page. This allows
grab_cache_page_write_begin() to better choose the size of THP to allocate.

Fix afs's functions to handle this by trimming the length as needed after
the page has been allocated.

Fixes: e1b1240c1ff5 ("netfs: Add write_begin helper")
Reported-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
cc: linux-afs@xxxxxxxxxxxxxxxxxxx
---

fs/afs/write.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/fs/afs/write.c b/fs/afs/write.c
index a523bb86915d..f68274c39f47 100644
--- a/fs/afs/write.c
+++ b/fs/afs/write.c
@@ -25,7 +25,8 @@ int afs_set_page_dirty(struct page *page)
}

/*
- * prepare to perform part of a write to a page
+ * Prepare to perform part of a write to a page. Note that len may extend
+ * beyond the end of the page.
*/
int afs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, unsigned len, unsigned flags,
@@ -52,7 +53,8 @@ int afs_write_begin(struct file *file, struct address_space *mapping,
return ret;

index = page->index;
- from = pos - index * PAGE_SIZE;
+ from = offset_in_thp(page, pos);
+ len = min_t(size_t, len, thp_size(page) - from);
to = from + len;

try_again:
@@ -103,7 +105,8 @@ int afs_write_begin(struct file *file, struct address_space *mapping,
}

/*
- * finalise part of a write to a page
+ * Finalise part of a write to a page. Note that len may extend beyond the end
+ * of the page.
*/
int afs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, unsigned len, unsigned copied,
@@ -111,7 +114,7 @@ int afs_write_end(struct file *file, struct address_space *mapping,
{
struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
unsigned long priv;
- unsigned int f, from = pos & (thp_size(page) - 1);
+ unsigned int f, from = offset_in_thp(page, pos);
unsigned int t, to = from + copied;
loff_t i_size, maybe_i_size;