Skip to contentSkip to conversation

fuse: support large folios

PATCH Superseded Joanne Koong 5 participants 31 messages Apr 26, 2025 – Jul 8, 2025

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover fuse: support large folios

This patchset adds support for large folios in fuse.

This does not yet switch fuse to using large folios. Using large folios in fuse is dependent on adding granular dirty-page tracking. This will be done in a separate patchset that will have fuse use iomap [1]. There also needs to be a followup (also part of future work) for having dirty page balancing not tank performance for unprivileged servers where bdi limits lead to subpar throttling [1], before enabling large folios for fuse.

This patchset (v5) is pretty much identical to v3 except for fixing up readahead error handling.

[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1a38pv3OgFZRfdTiDMXuPWuBgN8KY47XfOsYHj=N2wxAg@mail.gmail.com/#t

Changelog:
v4: https://lore.kernel.org/linux-fsdevel/[email protected]/
v4 -> v5:
* Now that temp pages are removed in FUSE, resubmit v3.

v3: https://lore.kernel.org/linux-fsdevel/[email protected]/
v3 -> v4:
* Add Jeff's reviewed-bys
* Drop writeback large folios changes, drop turning large folios on. These will be part of a separate future patchset

v2: https://lore.kernel.org/linux-fsdevel/[email protected]/
v2 -> v3:
* Fix direct io parsing to check each extracted page instead of assuming all pages in a large folio will be used (Matthew)

v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
v1 -> v2:
* Change naming from "non-writeback write" to "writethrough write"
* Fix deadlock for writethrough writes by calling fault_in_iov_iter_readable()
* first
before __filemap_get_folio() (Josef)
* For readahead, retain original folio_size() for descs.length (Josef)
* Use folio_zero_range() api in fuse_copy_folio() (Josef)
* Add Josef's reviewed-bys

Joanne Koong (11):
fuse: support copying large folios
fuse: support large folios for retrieves
fuse: refactor fuse_fill_write_pages()
fuse: support large folios for writethrough writes
fuse: support large folios for folio reads
fuse: support large folios for symlinks
fuse: support large folios for stores
fuse: support large folios for queued writes
fuse: support large folios for readahead
fuse: optimize direct io large folios processing
fuse: support large folios for writeback

 fs/fuse/dev.c        | 126 ++++++++++++++++++------------------
 fs/fuse/dir.c        |   8 +--
 fs/fuse/file.c       | 148 +++++++++++++++++++++++++++++--------------
 fs/fuse/fuse_dev_i.h |   2 +-
 4 files changed, 169 insertions(+), 115 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v5 1/11 fuse: support copying large folios

Currently, all folios associated with fuse are one page size. As part of the work to enable large folios, this commit adds support for copying to/from folios larger than one page size.

---
 fs/fuse/dev.c        | 84 +++++++++++++++++++-------------------------
 fs/fuse/fuse_dev_i.h |  2 +-
 2 files changed, 37 insertions(+), 49 deletions(-)
fs/fuse/dev.c +36 -48
index 155bb6aeaef5..7b0e3a394480 100644
@@ -955,10 +955,10 @@ static int fuse_check_folio(struct folio *folio)
  * folio that was originally in @pagep will lose a reference and the new
  * folio returned in @pagep will carry a reference.
  */
-static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
+static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliop)
 {
 	int err;
-	struct folio *oldfolio = page_folio(*pagep);
+	struct folio *oldfolio = *foliop;
 	struct folio *newfolio;
 	struct pipe_buffer *buf = cs->pipebufs;
 
@@ -979,7 +979,7 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	cs->pipebufs++;
 	cs->nr_segs--;
 
-	if (cs->len != PAGE_SIZE)
+	if (cs->len != folio_size(oldfolio))
 		goto out_fallback;
 
 	if (!pipe_buf_try_steal(cs->pipe, buf))
@@ -1025,7 +1025,7 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	if (test_bit(FR_ABORTED, &cs->req->flags))
 		err = -ENOENT;
 	else
-		*pagep = &newfolio->page;
+		*foliop = newfolio;
 	spin_unlock(&cs->req->waitq.lock);
 
 	if (err) {
@@ -1058,8 +1058,8 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	goto out_put_old;
 }
 
-static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
-			 unsigned offset, unsigned count)
+static int fuse_ref_folio(struct fuse_copy_state *cs, struct folio *folio,
+			  unsigned offset, unsigned count)
 {
 	struct pipe_buffer *buf;
 	int err;
@@ -1067,17 +1067,17 @@ static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
 	if (cs->nr_segs >= cs->pipe->max_usage)
 		return -EIO;
 
-	get_page(page);
+	folio_get(folio);
 	err = unlock_request(cs->req);
 	if (err) {
-		put_page(page);
+		folio_put(folio);
 		return err;
 	}
 
 	fuse_copy_finish(cs);
 
 	buf = cs->pipebufs;
-	buf->page = page;
+	buf->page = &folio->page;
 	buf->offset = offset;
 	buf->len = count;
 
@@ -1089,20 +1089,21 @@ static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
 }
 
 /*
- * Copy a page in the request to/from the userspace buffer.  Must be
+ * Copy a folio in the request to/from the userspace buffer.  Must be
  * done atomically
  */
-static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
-			  unsigned offset, unsigned count, int zeroing)
+static int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop,
+			   unsigned offset, unsigned count, int zeroing)
 {
 	int err;
-	struct page *page = *pagep;
+	struct folio *folio = *foliop;
+	size_t size = folio_size(folio);
 
-	if (page && zeroing && count < PAGE_SIZE)
-		clear_highpage(page);
+	if (folio && zeroing && count < size)
+		folio_zero_range(folio, 0, size);
 
 	while (count) {
-		if (cs->write && cs->pipebufs && page) {
+		if (cs->write && cs->pipebufs && folio) {
 			/*
 			 * Can't control lifetime of pipe buffers, so always
 			 * copy user pages.
@@ -1112,12 +1113,12 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
 				if (err)
 					return err;
 			} else {
-				return fuse_ref_page(cs, page, offset, count);
+				return fuse_ref_folio(cs, folio, offset, count);
 			}
 		} else if (!cs->len) {
-			if (cs->move_pages && page &&
-			    offset == 0 && count == PAGE_SIZE) {
-				err = fuse_try_move_page(cs, pagep);
+			if (cs->move_folios && folio &&
+			    offset == 0 && count == folio_size(folio)) {
+				err = fuse_try_move_folio(cs, foliop);
 				if (err <= 0)
 					return err;
 			} else {
@@ -1126,22 +1127,22 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
 					return err;
 			}
 		}
-		if (page) {
-			void *mapaddr = kmap_local_page(page);
-			void *buf = mapaddr + offset;
+		if (folio) {
+			void *mapaddr = kmap_local_folio(folio, offset);
+			void *buf = mapaddr;
 			offset += fuse_copy_do(cs, &buf, &count);
 			kunmap_local(mapaddr);
 		} else
 			offset += fuse_copy_do(cs, NULL, &count);
 	}
-	if (page && !cs->write)
-		flush_dcache_page(page);
+	if (folio && !cs->write)
+		flush_dcache_folio(folio);
 	return 0;
 }
 
-/* Copy pages in the request to/from userspace buffer */
-static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
-			   int zeroing)
+/* Copy folios in the request to/from userspace buffer */
+static int fuse_copy_folios(struct fuse_copy_state *cs, unsigned nbytes,
+			    int zeroing)
 {
 	unsigned i;
 	struct fuse_req *req = cs->req;
@@ -1151,23 +1152,12 @@ static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
 		int err;
 		unsigned int offset = ap->descs[i].offset;
 		unsigned int count = min(nbytes, ap->descs[i].length);
-		struct page *orig, *pagep;
-
-		orig = pagep = &ap->folios[i]->page;
 
-		err = fuse_copy_page(cs, &pagep, offset, count, zeroing);
+		err = fuse_copy_folio(cs, &ap->folios[i], offset, count, zeroing);
 		if (err)
 			return err;
 
 		nbytes -= count;
-
-		/*
-		 *  fuse_copy_page may have moved a page from a pipe instead of
-		 *  copying into our given page, so update the folios if it was
-		 *  replaced.
-		 */
-		if (pagep != orig)
-			ap->folios[i] = page_folio(pagep);
 	}
 	return 0;
 }
@@ -1197,7 +1187,7 @@ int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
 	for (i = 0; !err && i < numargs; i++)  {
 		struct fuse_arg *arg = &args[i];
 		if (i == numargs - 1 && argpages)
-			err = fuse_copy_pages(cs, arg->size, zeroing);
+			err = fuse_copy_folios(cs, arg->size, zeroing);
 		else
 			err = fuse_copy_one(cs, arg->value, arg->size);
 	}
@@ -1786,7 +1776,6 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 	num = outarg.size;
 	while (num) {
 		struct folio *folio;
-		struct page *page;
 		unsigned int this_num;
 
 		folio = filemap_grab_folio(mapping, index);
@@ -1794,9 +1783,8 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		if (IS_ERR(folio))
 			goto out_iput;
 
-		page = &folio->page;
 		this_num = min_t(unsigned, num, folio_size(folio) - offset);
-		err = fuse_copy_page(cs, &page, offset, this_num, 0);
+		err = fuse_copy_folio(cs, &folio, offset, this_num, 0);
 		if (!folio_test_uptodate(folio) && !err && offset == 0 &&
 		    (this_num == folio_size(folio) || file_size == end)) {
 			folio_zero_segment(folio, this_num, folio_size(folio));
@@ -2037,8 +2025,8 @@ static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
 		       unsigned int size, struct fuse_copy_state *cs)
 {
-	/* Don't try to move pages (yet) */
-	cs->move_pages = false;
+	/* Don't try to move folios (yet) */
+	cs->move_folios = false;
 
 	switch (code) {
 	case FUSE_NOTIFY_POLL:
@@ -2189,7 +2177,7 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
 	spin_unlock(&fpq->lock);
 	cs->req = req;
 	if (!req->args->page_replace)
-		cs->move_pages = false;
+		cs->move_folios = false;
 
 	if (oh.error)
 		err = nbytes != sizeof(oh) ? -EINVAL : 0;
@@ -2307,7 +2295,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
 	cs.pipe = pipe;
 
 	if (flags & SPLICE_F_MOVE)
-		cs.move_pages = true;
+		cs.move_folios = true;
 
 	ret = fuse_dev_do_write(fud, &cs, len);
 
fs/fuse/fuse_dev_i.h +1 -1
index db136e045925..5a9bd771a319 100644
@@ -30,7 +30,7 @@ struct fuse_copy_state {
 	unsigned int len;
 	unsigned int offset;
 	bool write:1;
-	bool move_pages:1;
+	bool move_folios:1;
 	bool is_uring:1;
 	struct {
 		unsigned int copied_sz; /* copied size into the user buffer */
Signature · 1 line
2.47.1
Reviewed-by on v5 1/11
#2 lore raw

Re: [PATCH v5 01/11] fuse: support copying large folios

Quoted 6 lines · Joanne Koong

Currently, all folios associated with fuse are one page size. As part of the work to enable large folios, this commit adds support for copying to/from folios larger than one page size.

Quoted 244 lines
---
 fs/fuse/dev.c        | 84 +++++++++++++++++++-------------------------
 fs/fuse/fuse_dev_i.h |  2 +-
 2 files changed, 37 insertions(+), 49 deletions(-)
fs/fuse/dev.c +36 -48
index 155bb6aeaef5..7b0e3a394480 100644
@@ -955,10 +955,10 @@ static int fuse_check_folio(struct folio *folio)
  * folio that was originally in @pagep will lose a reference and the new
  * folio returned in @pagep will carry a reference.
  */
-static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
+static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliop)
 {
 	int err;
-	struct folio *oldfolio = page_folio(*pagep);
+	struct folio *oldfolio = *foliop;
 	struct folio *newfolio;
 	struct pipe_buffer *buf = cs->pipebufs;
 
@@ -979,7 +979,7 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	cs->pipebufs++;
 	cs->nr_segs--;
 
-	if (cs->len != PAGE_SIZE)
+	if (cs->len != folio_size(oldfolio))
 		goto out_fallback;
 
 	if (!pipe_buf_try_steal(cs->pipe, buf))
@@ -1025,7 +1025,7 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	if (test_bit(FR_ABORTED, &cs->req->flags))
 		err = -ENOENT;
 	else
-		*pagep = &newfolio->page;
+		*foliop = newfolio;
 	spin_unlock(&cs->req->waitq.lock);
 
 	if (err) {
@@ -1058,8 +1058,8 @@ static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
 	goto out_put_old;
 }
 
-static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
-			 unsigned offset, unsigned count)
+static int fuse_ref_folio(struct fuse_copy_state *cs, struct folio *folio,
+			  unsigned offset, unsigned count)
 {
 	struct pipe_buffer *buf;
 	int err;
@@ -1067,17 +1067,17 @@ static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
 	if (cs->nr_segs >= cs->pipe->max_usage)
 		return -EIO;
 
-	get_page(page);
+	folio_get(folio);
 	err = unlock_request(cs->req);
 	if (err) {
-		put_page(page);
+		folio_put(folio);
 		return err;
 	}
 
 	fuse_copy_finish(cs);
 
 	buf = cs->pipebufs;
-	buf->page = page;
+	buf->page = &folio->page;
 	buf->offset = offset;
 	buf->len = count;
 
@@ -1089,20 +1089,21 @@ static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
 }
 
 /*
- * Copy a page in the request to/from the userspace buffer.  Must be
+ * Copy a folio in the request to/from the userspace buffer.  Must be
  * done atomically
  */
-static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
-			  unsigned offset, unsigned count, int zeroing)
+static int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop,
+			   unsigned offset, unsigned count, int zeroing)
 {
 	int err;
-	struct page *page = *pagep;
+	struct folio *folio = *foliop;
+	size_t size = folio_size(folio);
 
-	if (page && zeroing && count < PAGE_SIZE)
-		clear_highpage(page);
+	if (folio && zeroing && count < size)
+		folio_zero_range(folio, 0, size);
 
 	while (count) {
-		if (cs->write && cs->pipebufs && page) {
+		if (cs->write && cs->pipebufs && folio) {
 			/*
 			 * Can't control lifetime of pipe buffers, so always
 			 * copy user pages.
@@ -1112,12 +1113,12 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
 				if (err)
 					return err;
 			} else {
-				return fuse_ref_page(cs, page, offset, count);
+				return fuse_ref_folio(cs, folio, offset, count);
 			}
 		} else if (!cs->len) {
-			if (cs->move_pages && page &&
-			    offset == 0 && count == PAGE_SIZE) {
-				err = fuse_try_move_page(cs, pagep);
+			if (cs->move_folios && folio &&
+			    offset == 0 && count == folio_size(folio)) {
+				err = fuse_try_move_folio(cs, foliop);
 				if (err <= 0)
 					return err;
 			} else {
@@ -1126,22 +1127,22 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
 					return err;
 			}
 		}
-		if (page) {
-			void *mapaddr = kmap_local_page(page);
-			void *buf = mapaddr + offset;
+		if (folio) {
+			void *mapaddr = kmap_local_folio(folio, offset);
+			void *buf = mapaddr;
 			offset += fuse_copy_do(cs, &buf, &count);
 			kunmap_local(mapaddr);
 		} else
 			offset += fuse_copy_do(cs, NULL, &count);
 	}
-	if (page && !cs->write)
-		flush_dcache_page(page);
+	if (folio && !cs->write)
+		flush_dcache_folio(folio);
 	return 0;
 }
 
-/* Copy pages in the request to/from userspace buffer */
-static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
-			   int zeroing)
+/* Copy folios in the request to/from userspace buffer */
+static int fuse_copy_folios(struct fuse_copy_state *cs, unsigned nbytes,
+			    int zeroing)
 {
 	unsigned i;
 	struct fuse_req *req = cs->req;
@@ -1151,23 +1152,12 @@ static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
 		int err;
 		unsigned int offset = ap->descs[i].offset;
 		unsigned int count = min(nbytes, ap->descs[i].length);
-		struct page *orig, *pagep;
-
-		orig = pagep = &ap->folios[i]->page;
 
-		err = fuse_copy_page(cs, &pagep, offset, count, zeroing);
+		err = fuse_copy_folio(cs, &ap->folios[i], offset, count, zeroing);
 		if (err)
 			return err;
 
 		nbytes -= count;
-
-		/*
-		 *  fuse_copy_page may have moved a page from a pipe instead of
-		 *  copying into our given page, so update the folios if it was
-		 *  replaced.
-		 */
-		if (pagep != orig)
-			ap->folios[i] = page_folio(pagep);
 	}
 	return 0;
 }
@@ -1197,7 +1187,7 @@ int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
 	for (i = 0; !err && i < numargs; i++)  {
 		struct fuse_arg *arg = &args[i];
 		if (i == numargs - 1 && argpages)
-			err = fuse_copy_pages(cs, arg->size, zeroing);
+			err = fuse_copy_folios(cs, arg->size, zeroing);
 		else
 			err = fuse_copy_one(cs, arg->value, arg->size);
 	}
@@ -1786,7 +1776,6 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 	num = outarg.size;
 	while (num) {
 		struct folio *folio;
-		struct page *page;
 		unsigned int this_num;
 
 		folio = filemap_grab_folio(mapping, index);
@@ -1794,9 +1783,8 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		if (IS_ERR(folio))
 			goto out_iput;
 
-		page = &folio->page;
 		this_num = min_t(unsigned, num, folio_size(folio) - offset);
-		err = fuse_copy_page(cs, &page, offset, this_num, 0);
+		err = fuse_copy_folio(cs, &folio, offset, this_num, 0);
 		if (!folio_test_uptodate(folio) && !err && offset == 0 &&
 		    (this_num == folio_size(folio) || file_size == end)) {
 			folio_zero_segment(folio, this_num, folio_size(folio));
@@ -2037,8 +2025,8 @@ static int fuse_notify_inc_epoch(struct fuse_conn *fc)
 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
 		       unsigned int size, struct fuse_copy_state *cs)
 {
-	/* Don't try to move pages (yet) */
-	cs->move_pages = false;
+	/* Don't try to move folios (yet) */
+	cs->move_folios = false;
 
 	switch (code) {
 	case FUSE_NOTIFY_POLL:
@@ -2189,7 +2177,7 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
 	spin_unlock(&fpq->lock);
 	cs->req = req;
 	if (!req->args->page_replace)
-		cs->move_pages = false;
+		cs->move_folios = false;
 
 	if (oh.error)
 		err = nbytes != sizeof(oh) ? -EINVAL : 0;
@@ -2307,7 +2295,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
 	cs.pipe = pipe;
 
 	if (flags & SPLICE_F_MOVE)
-		cs.move_pages = true;
+		cs.move_folios = true;
 
 	ret = fuse_dev_do_write(fud, &cs, len);
 
fs/fuse/fuse_dev_i.h +1 -1
index db136e045925..5a9bd771a319 100644
@@ -30,7 +30,7 @@ struct fuse_copy_state {
 	unsigned int len;
 	unsigned int offset;
 	bool write:1;
-	bool move_pages:1;
+	bool move_folios:1;
 	bool is_uring:1;
 	struct {
 		unsigned int copied_sz; /* copied size into the user buffer */
#3 lore raw

PATCH v5 2/11 fuse: support large folios for retrieves

In reply to Joanne Koong

Add support for folios larger than one page size for retrieves.

---
 fs/fuse/dev.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
fs/fuse/dev.c +15 -10
index 7b0e3a394480..fb81c0a1c6cd 100644
@@ -1837,7 +1837,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 	unsigned int num;
 	unsigned int offset;
 	size_t total_len = 0;
-	unsigned int num_pages, cur_pages = 0;
+	unsigned int num_pages;
 	struct fuse_conn *fc = fm->fc;
 	struct fuse_retrieve_args *ra;
 	size_t args_size = sizeof(*ra);
@@ -1855,6 +1855,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 	num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
 	num_pages = min(num_pages, fc->max_pages);
+	num = min(num, num_pages << PAGE_SHIFT);
 
 	args_size += num_pages * (sizeof(ap->folios[0]) + sizeof(ap->descs[0]));
 
@@ -1875,25 +1876,29 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 	index = outarg->offset >> PAGE_SHIFT;
 
-	while (num && cur_pages < num_pages) {
+	while (num) {
 		struct folio *folio;
-		unsigned int this_num;
+		unsigned int folio_offset;
+		unsigned int nr_bytes;
+		unsigned int nr_pages;
 
 		folio = filemap_get_folio(mapping, index);
 		if (IS_ERR(folio))
 			break;
 
-		this_num = min_t(unsigned, num, PAGE_SIZE - offset);
+		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		nr_bytes = min(folio_size(folio) - folio_offset, num);
+		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+
 		ap->folios[ap->num_folios] = folio;
-		ap->descs[ap->num_folios].offset = offset;
-		ap->descs[ap->num_folios].length = this_num;
+		ap->descs[ap->num_folios].offset = folio_offset;
+		ap->descs[ap->num_folios].length = nr_bytes;
 		ap->num_folios++;
-		cur_pages++;
 
 		offset = 0;
-		num -= this_num;
-		total_len += this_num;
-		index++;
+		num -= nr_bytes;
+		total_len += nr_bytes;
+		index += nr_pages;
 	}
 	ra->inarg.offset = outarg->offset;
 	ra->inarg.size = total_len;
Signature · 1 line
2.47.1
Reviewed-by on v5 2/11
#4 lore raw

Re: [PATCH v5 02/11] fuse: support large folios for retrieves

Quoted 5 lines · Joanne Koong

Add support for folios larger than one page size for retrieves.

Quoted 64 lines
---
 fs/fuse/dev.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
fs/fuse/dev.c +15 -10
index 7b0e3a394480..fb81c0a1c6cd 100644
@@ -1837,7 +1837,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 	unsigned int num;
 	unsigned int offset;
 	size_t total_len = 0;
-	unsigned int num_pages, cur_pages = 0;
+	unsigned int num_pages;
 	struct fuse_conn *fc = fm->fc;
 	struct fuse_retrieve_args *ra;
 	size_t args_size = sizeof(*ra);
@@ -1855,6 +1855,7 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 	num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
 	num_pages = min(num_pages, fc->max_pages);
+	num = min(num, num_pages << PAGE_SHIFT);
 
 	args_size += num_pages * (sizeof(ap->folios[0]) + sizeof(ap->descs[0]));
 
@@ -1875,25 +1876,29 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
 
 	index = outarg->offset >> PAGE_SHIFT;
 
-	while (num && cur_pages < num_pages) {
+	while (num) {
 		struct folio *folio;
-		unsigned int this_num;
+		unsigned int folio_offset;
+		unsigned int nr_bytes;
+		unsigned int nr_pages;
 
 		folio = filemap_get_folio(mapping, index);
 		if (IS_ERR(folio))
 			break;
 
-		this_num = min_t(unsigned, num, PAGE_SIZE - offset);
+		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		nr_bytes = min(folio_size(folio) - folio_offset, num);
+		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+
 		ap->folios[ap->num_folios] = folio;
-		ap->descs[ap->num_folios].offset = offset;
-		ap->descs[ap->num_folios].length = this_num;
+		ap->descs[ap->num_folios].offset = folio_offset;
+		ap->descs[ap->num_folios].length = nr_bytes;
 		ap->num_folios++;
-		cur_pages++;
 
 		offset = 0;
-		num -= this_num;
-		total_len += this_num;
-		index++;
+		num -= nr_bytes;
+		total_len += nr_bytes;
+		index += nr_pages;
 	}
 	ra->inarg.offset = outarg->offset;
 	ra->inarg.size = total_len;
#5 lore raw

PATCH v5 3/11 fuse: refactor fuse_fill_write_pages()

In reply to Joanne Koong

Refactor the logic in fuse_fill_write_pages() for copying out write data. This will make the future change for supporting large folios for writes easier. No functional changes.

---
 fs/fuse/file.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
fs/fuse/file.c +9 -10
index e203dd4fcc0f..edc86485065e 100644
@@ -1132,21 +1132,21 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 	struct fuse_args_pages *ap = &ia->ap;
 	struct fuse_conn *fc = get_fuse_conn(mapping->host);
 	unsigned offset = pos & (PAGE_SIZE - 1);
-	unsigned int nr_pages = 0;
 	size_t count = 0;
+	unsigned int num;
 	int err;
 
+	num = min(iov_iter_count(ii), fc->max_write);
+	num = min(num, max_pages << PAGE_SHIFT);
+
 	ap->args.in_pages = true;
 	ap->descs[0].offset = offset;
 
-	do {
+	while (num) {
 		size_t tmp;
 		struct folio *folio;
 		pgoff_t index = pos >> PAGE_SHIFT;
-		size_t bytes = min_t(size_t, PAGE_SIZE - offset,
-				     iov_iter_count(ii));
-
-		bytes = min_t(size_t, bytes, fc->max_write - count);
+		unsigned bytes = min(PAGE_SIZE - offset, num);
 
  again:
 		folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
@@ -1182,10 +1182,10 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		ap->folios[ap->num_folios] = folio;
 		ap->descs[ap->num_folios].length = tmp;
 		ap->num_folios++;
-		nr_pages++;
 
 		count += tmp;
 		pos += tmp;
+		num -= tmp;
 		offset += tmp;
 		if (offset == PAGE_SIZE)
 			offset = 0;
@@ -1200,10 +1200,9 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 			ia->write.folio_locked = true;
 			break;
 		}
-		if (!fc->big_writes)
+		if (!fc->big_writes || offset != 0)
 			break;
-	} while (iov_iter_count(ii) && count < fc->max_write &&
-		 nr_pages < max_pages && offset == 0);
+	}
 
 	return count > 0 ? count : err;
 }
Signature · 1 line
2.47.1
build bot
#6 lore raw

Re: [PATCH v5 03/11] fuse: refactor fuse_fill_write_pages()

BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: Joanne Koong…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: Joanne Koong <[email protected]>
TO: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]

Hi Joanne,

kernel test robot noticed the following build warnings:

[auto build test WARNING on mszeredi-fuse/for-next]
[also build test WARNING on next-20250424]
[cannot apply to linus/master v6.15-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/fuse-support-copying-large-folios/20250426-081219
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git for-next
patch link:    https://lore.kernel.org/r/20250426000828.3216220-4-joannelkoong%40gmail.com
patch subject: [PATCH v5 03/11] fuse: refactor fuse_fill_write_pages()

:::::: branch date: 19 hours ago
:::::: commit date: 19 hours ago
config: i386-randconfig-141-20250426 (https://download.01.org/0day-ci/archive/20250427/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/fuse/file.c:1207 fuse_fill_write_pages() error: uninitialized symbol 'err'.

vim +/err +1207 fs/fuse/file.c

ea9b9907b82a09 Nicholas Piggin    2008-04-30  1126  
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1127  static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1128  				     struct address_space *mapping,
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1129  				     struct iov_iter *ii, loff_t pos,
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1130  				     unsigned int max_pages)
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1131  {
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1132  	struct fuse_args_pages *ap = &ia->ap;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1133  	struct fuse_conn *fc = get_fuse_conn(mapping->host);
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1134  	unsigned offset = pos & (PAGE_SIZE - 1);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1135  	size_t count = 0;
dfda790dfda452 Joanne Koong       2025-04-25  1136  	unsigned int num;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1137  	int err;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1138  
dfda790dfda452 Joanne Koong       2025-04-25  1139  	num = min(iov_iter_count(ii), fc->max_write);
dfda790dfda452 Joanne Koong       2025-04-25  1140  	num = min(num, max_pages << PAGE_SHIFT);
dfda790dfda452 Joanne Koong       2025-04-25  1141  
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1142  	ap->args.in_pages = true;
68bfb7eb7f7de3 Joanne Koong       2024-10-24  1143  	ap->descs[0].offset = offset;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1144  
dfda790dfda452 Joanne Koong       2025-04-25  1145  	while (num) {
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1146  		size_t tmp;
9bafbe7ae01321 Josef Bacik        2024-09-30  1147  		struct folio *folio;
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1148  		pgoff_t index = pos >> PAGE_SHIFT;
dfda790dfda452 Joanne Koong       2025-04-25  1149  		unsigned bytes = min(PAGE_SIZE - offset, num);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1150  
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1151   again:
9bafbe7ae01321 Josef Bacik        2024-09-30  1152  		folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
9bafbe7ae01321 Josef Bacik        2024-09-30  1153  					    mapping_gfp_mask(mapping));
9bafbe7ae01321 Josef Bacik        2024-09-30  1154  		if (IS_ERR(folio)) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1155  			err = PTR_ERR(folio);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1156  			break;
9bafbe7ae01321 Josef Bacik        2024-09-30  1157  		}
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1158  
931e80e4b3263d anfei zhou         2010-02-02  1159  		if (mapping_writably_mapped(mapping))
9bafbe7ae01321 Josef Bacik        2024-09-30  1160  			flush_dcache_folio(folio);
931e80e4b3263d anfei zhou         2010-02-02  1161  
9bafbe7ae01321 Josef Bacik        2024-09-30  1162  		tmp = copy_folio_from_iter_atomic(folio, offset, bytes, ii);
9bafbe7ae01321 Josef Bacik        2024-09-30  1163  		flush_dcache_folio(folio);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1164  
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1165  		if (!tmp) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1166  			folio_unlock(folio);
9bafbe7ae01321 Josef Bacik        2024-09-30  1167  			folio_put(folio);
faa794dd2e17e7 Dave Hansen        2025-01-29  1168  
faa794dd2e17e7 Dave Hansen        2025-01-29  1169  			/*
faa794dd2e17e7 Dave Hansen        2025-01-29  1170  			 * Ensure forward progress by faulting in
faa794dd2e17e7 Dave Hansen        2025-01-29  1171  			 * while not holding the folio lock:
faa794dd2e17e7 Dave Hansen        2025-01-29  1172  			 */
faa794dd2e17e7 Dave Hansen        2025-01-29  1173  			if (fault_in_iov_iter_readable(ii, bytes)) {
faa794dd2e17e7 Dave Hansen        2025-01-29  1174  				err = -EFAULT;
faa794dd2e17e7 Dave Hansen        2025-01-29  1175  				break;
faa794dd2e17e7 Dave Hansen        2025-01-29  1176  			}
faa794dd2e17e7 Dave Hansen        2025-01-29  1177  
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1178  			goto again;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1179  		}
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1180  
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1181  		err = 0;
f2ef459bab7326 Joanne Koong       2024-10-24  1182  		ap->folios[ap->num_folios] = folio;
68bfb7eb7f7de3 Joanne Koong       2024-10-24  1183  		ap->descs[ap->num_folios].length = tmp;
f2ef459bab7326 Joanne Koong       2024-10-24  1184  		ap->num_folios++;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1185  
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1186  		count += tmp;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1187  		pos += tmp;
dfda790dfda452 Joanne Koong       2025-04-25  1188  		num -= tmp;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1189  		offset += tmp;
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1190  		if (offset == PAGE_SIZE)
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1191  			offset = 0;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1192  
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1193  		/* If we copied full page, mark it uptodate */
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1194  		if (tmp == PAGE_SIZE)
9bafbe7ae01321 Josef Bacik        2024-09-30  1195  			folio_mark_uptodate(folio);
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1196  
9bafbe7ae01321 Josef Bacik        2024-09-30  1197  		if (folio_test_uptodate(folio)) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1198  			folio_unlock(folio);
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1199  		} else {
f2ef459bab7326 Joanne Koong       2024-10-24  1200  			ia->write.folio_locked = true;
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1201  			break;
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1202  		}
dfda790dfda452 Joanne Koong       2025-04-25  1203  		if (!fc->big_writes || offset != 0)
78bb6cb9a890d3 Miklos Szeredi     2008-05-12  1204  			break;
dfda790dfda452 Joanne Koong       2025-04-25  1205  	}
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1206  
ea9b9907b82a09 Nicholas Piggin    2008-04-30 @1207  	return count > 0 ? count : err;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1208  }
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1209  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH v5 03/11] fuse: refactor fuse_fill_write_pages()

Quoted 108 lines · Dan Carpenter

Hi Joanne,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/fuse-support-copying-large-folios/20250426-081219
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git for-next
patch link:    https://lore.kernel.org/r/20250426000828.3216220-4-joannelkoong%40gmail.com
patch subject: [PATCH v5 03/11] fuse: refactor fuse_fill_write_pages()
config: i386-randconfig-141-20250426 (https://download.01.org/0day-ci/archive/20250427/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/fuse/file.c:1207 fuse_fill_write_pages() error: uninitialized symbol 'err'.

vim +/err +1207 fs/fuse/file.c

4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1127  static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1128                                   struct address_space *mapping,
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1129                                   struct iov_iter *ii, loff_t pos,
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1130                                   unsigned int max_pages)
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1131  {
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1132      struct fuse_args_pages *ap = &ia->ap;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1133      struct fuse_conn *fc = get_fuse_conn(mapping->host);
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1134      unsigned offset = pos & (PAGE_SIZE - 1);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1135      size_t count = 0;
dfda790dfda452 Joanne Koong       2025-04-25  1136      unsigned int num;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1137      int err;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1138
dfda790dfda452 Joanne Koong       2025-04-25  1139      num = min(iov_iter_count(ii), fc->max_write);

Can iov_iter_count() return zero here?

dfda790dfda452 Joanne Koong       2025-04-25  1140      num = min(num, max_pages << PAGE_SHIFT);
dfda790dfda452 Joanne Koong       2025-04-25  1141
338f2e3f3341a9 Miklos Szeredi     2019-09-10  1142      ap->args.in_pages = true;
68bfb7eb7f7de3 Joanne Koong       2024-10-24  1143      ap->descs[0].offset = offset;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1144
dfda790dfda452 Joanne Koong       2025-04-25  1145      while (num) {
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1146              size_t tmp;
9bafbe7ae01321 Josef Bacik        2024-09-30  1147              struct folio *folio;
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1148              pgoff_t index = pos >> PAGE_SHIFT;
dfda790dfda452 Joanne Koong       2025-04-25  1149              unsigned bytes = min(PAGE_SIZE - offset, num);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1150
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1151   again:
9bafbe7ae01321 Josef Bacik        2024-09-30  1152              folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
9bafbe7ae01321 Josef Bacik        2024-09-30  1153                                          mapping_gfp_mask(mapping));
9bafbe7ae01321 Josef Bacik        2024-09-30  1154              if (IS_ERR(folio)) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1155                      err = PTR_ERR(folio);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1156                      break;
9bafbe7ae01321 Josef Bacik        2024-09-30  1157              }
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1158
931e80e4b3263d anfei zhou         2010-02-02  1159              if (mapping_writably_mapped(mapping))
9bafbe7ae01321 Josef Bacik        2024-09-30  1160                      flush_dcache_folio(folio);
931e80e4b3263d anfei zhou         2010-02-02  1161
9bafbe7ae01321 Josef Bacik        2024-09-30  1162              tmp = copy_folio_from_iter_atomic(folio, offset, bytes, ii);
9bafbe7ae01321 Josef Bacik        2024-09-30  1163              flush_dcache_folio(folio);
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1164
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1165              if (!tmp) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1166                      folio_unlock(folio);
9bafbe7ae01321 Josef Bacik        2024-09-30  1167                      folio_put(folio);
faa794dd2e17e7 Dave Hansen        2025-01-29  1168
faa794dd2e17e7 Dave Hansen        2025-01-29  1169                      /*
faa794dd2e17e7 Dave Hansen        2025-01-29  1170                       * Ensure forward progress by faulting in
faa794dd2e17e7 Dave Hansen        2025-01-29  1171                       * while not holding the folio lock:
faa794dd2e17e7 Dave Hansen        2025-01-29  1172                       */
faa794dd2e17e7 Dave Hansen        2025-01-29  1173                      if (fault_in_iov_iter_readable(ii, bytes)) {
faa794dd2e17e7 Dave Hansen        2025-01-29  1174                              err = -EFAULT;
faa794dd2e17e7 Dave Hansen        2025-01-29  1175                              break;
faa794dd2e17e7 Dave Hansen        2025-01-29  1176                      }
faa794dd2e17e7 Dave Hansen        2025-01-29  1177
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1178                      goto again;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1179              }
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1180
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1181              err = 0;
f2ef459bab7326 Joanne Koong       2024-10-24  1182              ap->folios[ap->num_folios] = folio;
68bfb7eb7f7de3 Joanne Koong       2024-10-24  1183              ap->descs[ap->num_folios].length = tmp;
f2ef459bab7326 Joanne Koong       2024-10-24  1184              ap->num_folios++;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1185
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1186              count += tmp;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1187              pos += tmp;
dfda790dfda452 Joanne Koong       2025-04-25  1188              num -= tmp;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1189              offset += tmp;
09cbfeaf1a5a67 Kirill A. Shutemov 2016-04-01  1190              if (offset == PAGE_SIZE)
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1191                      offset = 0;
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1192
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1193              /* If we copied full page, mark it uptodate */
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1194              if (tmp == PAGE_SIZE)
9bafbe7ae01321 Josef Bacik        2024-09-30  1195                      folio_mark_uptodate(folio);
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1196
9bafbe7ae01321 Josef Bacik        2024-09-30  1197              if (folio_test_uptodate(folio)) {
9bafbe7ae01321 Josef Bacik        2024-09-30  1198                      folio_unlock(folio);
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1199              } else {
f2ef459bab7326 Joanne Koong       2024-10-24  1200                      ia->write.folio_locked = true;
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1201                      break;
4f06dd92b5d0a6 Vivek Goyal        2020-10-21  1202              }
dfda790dfda452 Joanne Koong       2025-04-25  1203              if (!fc->big_writes || offset != 0)
78bb6cb9a890d3 Miklos Szeredi     2008-05-12  1204                      break;
dfda790dfda452 Joanne Koong       2025-04-25  1205      }
ea9b9907b82a09 Nicholas Piggin    2008-04-30  1206
Quoted 3 lines

ea9b9907b82a09 Nicholas Piggin 2008-04-30 @1207 return count > 0 ? count : err;
ea9b9907b82a09 Nicholas Piggin 2008-04-30 1208 }

I'll initialize err to 0 in v6. I'll wait for more reviews/comments on the patchset before sending that out.

Thanks,
Joanne

Quoted 4 lines

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

Reviewed-by on v5 3/11
#8 lore raw

Re: [PATCH v5 03/11] fuse: refactor fuse_fill_write_pages()

In reply to Joanne Koong
Quoted 7 lines · Joanne Koong

Refactor the logic in fuse_fill_write_pages() for copying out write data. This will make the future change for supporting large folios for writes easier. No functional changes.

Quoted 61 lines
---
 fs/fuse/file.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
fs/fuse/file.c +9 -10
index e203dd4fcc0f..edc86485065e 100644
@@ -1132,21 +1132,21 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 	struct fuse_args_pages *ap = &ia->ap;
 	struct fuse_conn *fc = get_fuse_conn(mapping->host);
 	unsigned offset = pos & (PAGE_SIZE - 1);
-	unsigned int nr_pages = 0;
 	size_t count = 0;
+	unsigned int num;
 	int err;
 
+	num = min(iov_iter_count(ii), fc->max_write);
+	num = min(num, max_pages << PAGE_SHIFT);
+
 	ap->args.in_pages = true;
 	ap->descs[0].offset = offset;
 
-	do {
+	while (num) {
 		size_t tmp;
 		struct folio *folio;
 		pgoff_t index = pos >> PAGE_SHIFT;
-		size_t bytes = min_t(size_t, PAGE_SIZE - offset,
-				     iov_iter_count(ii));
-
-		bytes = min_t(size_t, bytes, fc->max_write - count);
+		unsigned bytes = min(PAGE_SIZE - offset, num);
 
  again:
 		folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
@@ -1182,10 +1182,10 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		ap->folios[ap->num_folios] = folio;
 		ap->descs[ap->num_folios].length = tmp;
 		ap->num_folios++;
-		nr_pages++;
 
 		count += tmp;
 		pos += tmp;
+		num -= tmp;
 		offset += tmp;
 		if (offset == PAGE_SIZE)
 			offset = 0;
@@ -1200,10 +1200,9 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 			ia->write.folio_locked = true;
 			break;
 		}
-		if (!fc->big_writes)
+		if (!fc->big_writes || offset != 0)
 			break;
-	} while (iov_iter_count(ii) && count < fc->max_write &&
-		 nr_pages < max_pages && offset == 0);
+	}
 
 	return count > 0 ? count : err;
 }
#9 lore raw

PATCH v5 4/11 fuse: support large folios for writethrough writes

In reply to Joanne Koong

Add support for folios larger than one page size for writethrough writes.

---
 fs/fuse/file.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
fs/fuse/file.c +10 -5
index edc86485065e..e44b6d26c1c6 100644
@@ -1146,7 +1146,8 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		size_t tmp;
 		struct folio *folio;
 		pgoff_t index = pos >> PAGE_SHIFT;
-		unsigned bytes = min(PAGE_SIZE - offset, num);
+		unsigned int bytes;
+		unsigned int folio_offset;
 
  again:
 		folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
@@ -1159,7 +1160,10 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		if (mapping_writably_mapped(mapping))
 			flush_dcache_folio(folio);
 
-		tmp = copy_folio_from_iter_atomic(folio, offset, bytes, ii);
+		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		bytes = min(folio_size(folio) - folio_offset, num);
+
+		tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii);
 		flush_dcache_folio(folio);
 
 		if (!tmp) {
@@ -1180,6 +1184,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 
 		err = 0;
 		ap->folios[ap->num_folios] = folio;
+		ap->descs[ap->num_folios].offset = folio_offset;
 		ap->descs[ap->num_folios].length = tmp;
 		ap->num_folios++;
 
@@ -1187,11 +1192,11 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		pos += tmp;
 		num -= tmp;
 		offset += tmp;
-		if (offset == PAGE_SIZE)
+		if (offset == folio_size(folio))
 			offset = 0;
 
-		/* If we copied full page, mark it uptodate */
-		if (tmp == PAGE_SIZE)
+		/* If we copied full folio, mark it uptodate */
+		if (tmp == folio_size(folio))
 			folio_mark_uptodate(folio);
 
 		if (folio_test_uptodate(folio)) {
Signature · 1 line
2.47.1
#10 lore raw

Re: [PATCH v5 04/11] fuse: support large folios for writethrough writes

Quoted 46 lines · Joanne Koong

Add support for folios larger than one page size for writethrough writes.

---
 fs/fuse/file.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
fs/fuse/file.c +7 -2
index edc86485065e..e44b6d26c1c6 100644
@@ -1146,7 +1146,8 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		size_t tmp;
 		struct folio *folio;
 		pgoff_t index = pos >> PAGE_SHIFT;
-		unsigned bytes = min(PAGE_SIZE - offset, num);
+		unsigned int bytes;
+		unsigned int folio_offset;
 
  again:
 		folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
@@ -1159,7 +1160,10 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		if (mapping_writably_mapped(mapping))
 			flush_dcache_folio(folio);
 
-		tmp = copy_folio_from_iter_atomic(folio, offset, bytes, ii);
+		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		bytes = min(folio_size(folio) - folio_offset, num);
+
+		tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii);
 		flush_dcache_folio(folio);
 
 		if (!tmp) {f 
@@ -1180,6 +1184,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 
 		err = 0;
 		ap->folios[ap->num_folios] = folio;
+		ap->descs[ap->num_folios].offset = folio_offset;
 		ap->descs[ap->num_folios].length = tmp;
 		ap->num_folios++;
 
@@ -1187,11 +1192,11 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		pos += tmp;
 		num -= tmp;

fs/fuse/file.c static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, · line 1200

Quoted 10 lines
fs/fuse/file.c +3 -3
 		offset += tmp;
-		if (offset == PAGE_SIZE)
+		if (offset == folio_size(folio))
 			offset = 0;
 
-		/* If we copied full page, mark it uptodate */
-		if (tmp == PAGE_SIZE)
+		/* If we copied full folio, mark it uptodate */
+		if (tmp == folio_size(folio))
 			folio_mark_uptodate(folio);

Here am I confused. I think tmp can be a subpart of the folio, let's say the folio is 2MB and somehow the again loop would iterate through the folio in smaller steps. So the folio would be entirely written out, but tmp might not be folio_size? Doesn't this need to sum up tmp for per folio and then use that value? And I actually wonder if we could use the above "(offset == folio_size(folio)" as well. At least if the initial offset for a folio is 0 it should work.

Thanks,
Bernd

Quoted 2 lines
fs/fuse/file.c +0 -0
 		if (folio_test_uptodate(folio)) {
#11 lore raw

Re: [PATCH v5 04/11] fuse: support large folios for writethrough writes

Quoted 55 lines · Bernd Schubert
Quoted 51 lines · Joanne Koong

Add support for folios larger than one page size for writethrough writes.

---
 fs/fuse/file.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
fs/fuse/file.c +8 -3
index edc86485065e..e44b6d26c1c6 100644
@@ -1146,7 +1146,8 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
              size_t tmp;
              struct folio *folio;
              pgoff_t index = pos >> PAGE_SHIFT;
-             unsigned bytes = min(PAGE_SIZE - offset, num);
+             unsigned int bytes;
+             unsigned int folio_offset;
 
  again:
              folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
@@ -1159,7 +1160,10 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
              if (mapping_writably_mapped(mapping))
                      flush_dcache_folio(folio);
 
-             tmp = copy_folio_from_iter_atomic(folio, offset, bytes, ii);
+             folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+             bytes = min(folio_size(folio) - folio_offset, num);
+
+             tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii);
              flush_dcache_folio(folio);
 
              if (!tmp) {f
@@ -1180,6 +1184,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 
              err = 0;
              ap->folios[ap->num_folios] = folio;
+             ap->descs[ap->num_folios].offset = folio_offset;
              ap->descs[ap->num_folios].length = tmp;
              ap->num_folios++;
 
@@ -1187,11 +1192,11 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
              pos += tmp;
              num -= tmp;
              offset += tmp;
-             if (offset == PAGE_SIZE)
+             if (offset == folio_size(folio))
                      offset = 0;
 
Quoted 14 lines

fs/fuse/file.c static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, · line 1200

Quoted 5 lines
fs/fuse/file.c +2 -2
-             /* If we copied full page, mark it uptodate */
-             if (tmp == PAGE_SIZE)
+             /* If we copied full folio, mark it uptodate */
+             if (tmp == folio_size(folio))
                      folio_mark_uptodate(folio);

Here am I confused. I think tmp can be a subpart of the folio, let's say the folio is 2MB and somehow the again loop would iterate through the folio in smaller steps. So the folio would be entirely written out, but tmp might not be folio_size? Doesn't this need to sum up tmp for per folio and then use that value? And I actually wonder if we could use the above "(offset == folio_size(folio)" as well. At least if the initial offset for a folio is 0 it should work.

Hi Bernd,

Thanks for taking a look at this series and reviewing.

I don't think this scenario is possible. In copy_folio_iter_atomic() which ends up calling into __copy_from_iter(), I don't see anywhere where only the subpart of the folio can be copied out. The iter is a ubuf, and from what I see, either copy_folio_iter_atomic() will return 0 or memcpy all requested bytes (unless bytes is greater than the bytes contained in the iter, which isn't possible here) into the folio.

Thanks,
Joanne

Quoted 7 lines

Thanks,
Bernd

Quoted 2 lines
fs/fuse/file.c +0 -0
              if (folio_test_uptodate(folio)) {
#12 lore raw

PATCH v5 5/11 fuse: support large folios for folio reads

In reply to Joanne Koong

Add support for folios larger than one page size for folio reads into the page cache.

---
 fs/fuse/file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fuse/file.c +1 -1
index e44b6d26c1c6..0ca3b31c59f9 100644
@@ -793,7 +793,7 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio)
 	struct inode *inode = folio->mapping->host;
 	struct fuse_mount *fm = get_fuse_mount(inode);
 	loff_t pos = folio_pos(folio);
-	struct fuse_folio_desc desc = { .length = PAGE_SIZE };
+	struct fuse_folio_desc desc = { .length = folio_size(folio) };
 	struct fuse_io_args ia = {
 		.ap.args.page_zeroing = true,
 		.ap.args.out_pages = true,
Signature · 1 line
2.47.1
Reviewed-by on v5 5/11
#13 lore raw

Re: [PATCH v5 05/11] fuse: support large folios for folio reads

Quoted 6 lines · Joanne Koong

Add support for folios larger than one page size for folio reads into the page cache.

Quoted 17 lines
---
 fs/fuse/file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fuse/file.c +1 -1
index e44b6d26c1c6..0ca3b31c59f9 100644
@@ -793,7 +793,7 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio)
 	struct inode *inode = folio->mapping->host;
 	struct fuse_mount *fm = get_fuse_mount(inode);
 	loff_t pos = folio_pos(folio);
-	struct fuse_folio_desc desc = { .length = PAGE_SIZE };
+	struct fuse_folio_desc desc = { .length = folio_size(folio) };
 	struct fuse_io_args ia = {
 		.ap.args.page_zeroing = true,
 		.ap.args.out_pages = true,
#14 lore raw

PATCH v5 6/11 fuse: support large folios for symlinks

In reply to Joanne Koong

Support large folios for symlinks and change the name from fuse_getlink_page() to fuse_getlink_folio().

---
 fs/fuse/dir.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dir.c +4 -4
index 1fb0b15a6088..3003119559e8 100644
@@ -1629,10 +1629,10 @@ static int fuse_permission(struct mnt_idmap *idmap,
 	return err;
 }
 
-static int fuse_readlink_page(struct inode *inode, struct folio *folio)
+static int fuse_readlink_folio(struct inode *inode, struct folio *folio)
 {
 	struct fuse_mount *fm = get_fuse_mount(inode);
-	struct fuse_folio_desc desc = { .length = PAGE_SIZE - 1 };
+	struct fuse_folio_desc desc = { .length = folio_size(folio) - 1 };
 	struct fuse_args_pages ap = {
 		.num_folios = 1,
 		.folios = &folio,
@@ -1687,7 +1687,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 	if (!folio)
 		goto out_err;
 
-	err = fuse_readlink_page(inode, folio);
+	err = fuse_readlink_folio(inode, folio);
 	if (err) {
 		folio_put(folio);
 		goto out_err;
@@ -2277,7 +2277,7 @@ void fuse_init_dir(struct inode *inode)
 
 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
 {
-	int err = fuse_readlink_page(folio->mapping->host, folio);
+	int err = fuse_readlink_folio(folio->mapping->host, folio);
 
 	if (!err)
 		folio_mark_uptodate(folio);
Signature · 1 line
2.47.1
Reviewed-by on v5 6/11
#15 lore raw

Re: [PATCH v5 06/11] fuse: support large folios for symlinks

Quoted 6 lines · Joanne Koong

Support large folios for symlinks and change the name from fuse_getlink_page() to fuse_getlink_folio().

Quoted 39 lines
---
 fs/fuse/dir.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/fuse/dir.c +4 -4
index 1fb0b15a6088..3003119559e8 100644
@@ -1629,10 +1629,10 @@ static int fuse_permission(struct mnt_idmap *idmap,
 	return err;
 }
 
-static int fuse_readlink_page(struct inode *inode, struct folio *folio)
+static int fuse_readlink_folio(struct inode *inode, struct folio *folio)
 {
 	struct fuse_mount *fm = get_fuse_mount(inode);
-	struct fuse_folio_desc desc = { .length = PAGE_SIZE - 1 };
+	struct fuse_folio_desc desc = { .length = folio_size(folio) - 1 };
 	struct fuse_args_pages ap = {
 		.num_folios = 1,
 		.folios = &folio,
@@ -1687,7 +1687,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 	if (!folio)
 		goto out_err;
 
-	err = fuse_readlink_page(inode, folio);
+	err = fuse_readlink_folio(inode, folio);
 	if (err) {
 		folio_put(folio);
 		goto out_err;
@@ -2277,7 +2277,7 @@ void fuse_init_dir(struct inode *inode)
 
 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
 {
-	int err = fuse_readlink_page(folio->mapping->host, folio);
+	int err = fuse_readlink_folio(folio->mapping->host, folio);
 
 	if (!err)
 		folio_mark_uptodate(folio);
#16 lore raw

PATCH v5 7/11 fuse: support large folios for stores

In reply to Joanne Koong

Add support for folios larger than one page size for stores. Also change variable naming from "this_num" to "nr_bytes".

---
 fs/fuse/dev.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
fs/fuse/dev.c +12 -7
index fb81c0a1c6cd..a6ee8cd0f5cb 100644
@@ -1776,18 +1776,23 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 	num = outarg.size;
 	while (num) {
 		struct folio *folio;
-		unsigned int this_num;
+		unsigned int folio_offset;
+		unsigned int nr_bytes;
+		unsigned int nr_pages;
 
 		folio = filemap_grab_folio(mapping, index);
 		err = PTR_ERR(folio);
 		if (IS_ERR(folio))
 			goto out_iput;
 
-		this_num = min_t(unsigned, num, folio_size(folio) - offset);
-		err = fuse_copy_folio(cs, &folio, offset, this_num, 0);
+		folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
+		nr_bytes = min_t(unsigned, num, folio_size(folio) - folio_offset);
+		nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+
+		err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
 		if (!folio_test_uptodate(folio) && !err && offset == 0 &&
-		    (this_num == folio_size(folio) || file_size == end)) {
-			folio_zero_segment(folio, this_num, folio_size(folio));
+		    (nr_bytes == folio_size(folio) || file_size == end)) {
+			folio_zero_segment(folio, nr_bytes, folio_size(folio));
 			folio_mark_uptodate(folio);
 		}
 		folio_unlock(folio);
@@ -1796,9 +1801,9 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		if (err)
 			goto out_iput;
 
-		num -= this_num;
+		num -= nr_bytes;
 		offset = 0;
-		index++;
+		index += nr_pages;
 	}
 
 	err = 0;
Signature · 1 line
2.47.1
#17 lore raw

PATCH v5 8/11 fuse: support large folios for queued writes

In reply to Joanne Koong

Add support for folios larger than one page size for queued writes.

---
 fs/fuse/file.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fs/fuse/file.c +7 -4
index 0ca3b31c59f9..1d38486fae50 100644
@@ -1790,11 +1790,14 @@ __releases(fi->lock)
 __acquires(fi->lock)
 {
 	struct fuse_inode *fi = get_fuse_inode(wpa->inode);
+	struct fuse_args_pages *ap = &wpa->ia.ap;
 	struct fuse_write_in *inarg = &wpa->ia.write.in;
-	struct fuse_args *args = &wpa->ia.ap.args;
-	/* Currently, all folios in FUSE are one page */
-	__u64 data_size = wpa->ia.ap.num_folios * PAGE_SIZE;
-	int err;
+	struct fuse_args *args = &ap->args;
+	__u64 data_size = 0;
+	int err, i;
+
+	for (i = 0; i < ap->num_folios; i++)
+		data_size += ap->descs[i].length;
 
 	fi->writectr++;
 	if (inarg->offset + data_size <= size) {
Signature · 1 line
2.47.1
Reviewed-by on v5 8/11
#18 lore raw

Re: [PATCH v5 08/11] fuse: support large folios for queued writes

Quoted 5 lines · Joanne Koong

Add support for folios larger than one page size for queued writes.

Quoted 27 lines
---
 fs/fuse/file.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
fs/fuse/file.c +7 -4
index 0ca3b31c59f9..1d38486fae50 100644
@@ -1790,11 +1790,14 @@ __releases(fi->lock)
 __acquires(fi->lock)
 {
 	struct fuse_inode *fi = get_fuse_inode(wpa->inode);
+	struct fuse_args_pages *ap = &wpa->ia.ap;
 	struct fuse_write_in *inarg = &wpa->ia.write.in;
-	struct fuse_args *args = &wpa->ia.ap.args;
-	/* Currently, all folios in FUSE are one page */
-	__u64 data_size = wpa->ia.ap.num_folios * PAGE_SIZE;
-	int err;
+	struct fuse_args *args = &ap->args;
+	__u64 data_size = 0;
+	int err, i;
+
+	for (i = 0; i < ap->num_folios; i++)
+		data_size += ap->descs[i].length;
 
 	fi->writectr++;
 	if (inarg->offset + data_size <= size) {
#19 lore raw

PATCH v5 9/11 fuse: support large folios for readahead

In reply to Joanne Koong

Add support for folios larger than one page size for readahead.

---
 fs/fuse/file.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
fs/fuse/file.c +27 -9
index 1d38486fae50..9a31f2a516b9 100644
@@ -876,14 +876,13 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
 	fuse_io_free(ia);
 }
 
-static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
+static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
+				unsigned int count)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_mount *fm = ff->fm;
 	struct fuse_args_pages *ap = &ia->ap;
 	loff_t pos = folio_pos(ap->folios[0]);
-	/* Currently, all folios in FUSE are one page */
-	size_t count = ap->num_folios << PAGE_SHIFT;
 	ssize_t res;
 	int err;
 
@@ -918,6 +917,7 @@ static void fuse_readahead(struct readahead_control *rac)
 	struct inode *inode = rac->mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	unsigned int max_pages, nr_pages;
+	struct folio *folio = NULL;
 
 	if (fuse_is_bad(inode))
 		return;
@@ -939,8 +939,8 @@ static void fuse_readahead(struct readahead_control *rac)
 	while (nr_pages) {
 		struct fuse_io_args *ia;
 		struct fuse_args_pages *ap;
-		struct folio *folio;
 		unsigned cur_pages = min(max_pages, nr_pages);
+		unsigned int pages = 0;
 
 		if (fc->num_background >= fc->congestion_threshold &&
 		    rac->ra->async_size >= readahead_count(rac))
@@ -952,10 +952,12 @@ static void fuse_readahead(struct readahead_control *rac)
 
 		ia = fuse_io_alloc(NULL, cur_pages);
 		if (!ia)
-			return;
+			break;
 		ap = &ia->ap;
 
-		while (ap->num_folios < cur_pages) {
+		while (pages < cur_pages) {
+			unsigned int folio_pages;
+
 			/*
 			 * This returns a folio with a ref held on it.
 			 * The ref needs to be held until the request is
@@ -963,13 +965,29 @@ static void fuse_readahead(struct readahead_control *rac)
 			 * fuse_try_move_page()) drops the ref after it's
 			 * replaced in the page cache.
 			 */
-			folio = __readahead_folio(rac);
+			if (!folio)
+				folio =  __readahead_folio(rac);
+
+			folio_pages = folio_nr_pages(folio);
+			if (folio_pages > cur_pages - pages)
+				break;
+
 			ap->folios[ap->num_folios] = folio;
 			ap->descs[ap->num_folios].length = folio_size(folio);
 			ap->num_folios++;
+			pages += folio_pages;
+			folio = NULL;
+		}
+		if (!pages) {
+			fuse_io_free(ia);
+			break;
 		}
-		fuse_send_readpages(ia, rac->file);
-		nr_pages -= cur_pages;
+		fuse_send_readpages(ia, rac->file, pages << PAGE_SHIFT);
+		nr_pages -= pages;
+	}
+	if (folio) {
+		folio_end_read(folio, false);
+		folio_put(folio);
 	}
 }
 
Signature · 1 line
2.47.1
#20 lore raw

Re: [PATCH v5 09/11] fuse: support large folios for readahead

Quoted 64 lines · Joanne Koong

Add support for folios larger than one page size for readahead.

---
 fs/fuse/file.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
fs/fuse/file.c +8 -6
index 1d38486fae50..9a31f2a516b9 100644
@@ -876,14 +876,13 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
 	fuse_io_free(ia);
 }
 
-static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
+static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
+				unsigned int count)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_mount *fm = ff->fm;
 	struct fuse_args_pages *ap = &ia->ap;
 	loff_t pos = folio_pos(ap->folios[0]);
-	/* Currently, all folios in FUSE are one page */
-	size_t count = ap->num_folios << PAGE_SHIFT;
 	ssize_t res;
 	int err;
 
@@ -918,6 +917,7 @@ static void fuse_readahead(struct readahead_control *rac)
 	struct inode *inode = rac->mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	unsigned int max_pages, nr_pages;
+	struct folio *folio = NULL;
 
 	if (fuse_is_bad(inode))
 		return;
@@ -939,8 +939,8 @@ static void fuse_readahead(struct readahead_control *rac)
 	while (nr_pages) {
 		struct fuse_io_args *ia;
 		struct fuse_args_pages *ap;
-		struct folio *folio;
 		unsigned cur_pages = min(max_pages, nr_pages);
+		unsigned int pages = 0;
 
 		if (fc->num_background >= fc->congestion_threshold &&
 		    rac->ra->async_size >= readahead_count(rac))
@@ -952,10 +952,12 @@ static void fuse_readahead(struct readahead_control *rac)
 
 		ia = fuse_io_alloc(NULL, cur_pages);
 		if (!ia)
-			return;
+			break;
 		ap = &ia->ap;
 
-		while (ap->num_folios < cur_pages) {
+		while (pages < cur_pages) {
+			unsigned int folio_pages;
+
 			/*
 			 * This returns a folio with a ref held on it.
 			 * The ref needs to be held until the request is
@@ -963,13 +965,29 @@ static void fuse_readahead(struct readahead_control *rac)
 			 * fuse_try_move_page()) drops the ref after it's

fs/fuse/file.c static void fuse_readahead(struct readahead_control *rac) · line 974

Quoted 10 lines
fs/fuse/file.c +7 -1
 			 * replaced in the page cache.
 			 */
-			folio = __readahead_folio(rac);
+			if (!folio)
+				folio =  __readahead_folio(rac);
+
+			folio_pages = folio_nr_pages(folio);
+			if (folio_pages > cur_pages - pages)
+				break;
+

Hmm, so let's assume this would be a 2MB folio, but fc->max_pages is limited to 1MB - we not do read-ahead anymore?

Thanks,
Bernd

Quoted 21 lines
fs/fuse/file.c +12 -2
 			ap->folios[ap->num_folios] = folio;
 			ap->descs[ap->num_folios].length = folio_size(folio);
 			ap->num_folios++;
+			pages += folio_pages;
+			folio = NULL;
+		}
+		if (!pages) {
+			fuse_io_free(ia);
+			break;
 		}
-		fuse_send_readpages(ia, rac->file);
-		nr_pages -= cur_pages;
+		fuse_send_readpages(ia, rac->file, pages << PAGE_SHIFT);
+		nr_pages -= pages;
+	}
+	if (folio) {
+		folio_end_read(folio, false);
+		folio_put(folio);
 	}
 }
 
#21 lore raw

Re: [PATCH v5 09/11] fuse: support large folios for readahead

Quoted 72 lines · Bernd Schubert
Quoted 69 lines · Joanne Koong

Add support for folios larger than one page size for readahead.

---
 fs/fuse/file.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
fs/fuse/file.c +10 -7
index 1d38486fae50..9a31f2a516b9 100644
@@ -876,14 +876,13 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
 	fuse_io_free(ia);
 }
 
-static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
+static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
+				unsigned int count)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_mount *fm = ff->fm;
 	struct fuse_args_pages *ap = &ia->ap;
 	loff_t pos = folio_pos(ap->folios[0]);
-	/* Currently, all folios in FUSE are one page */
-	size_t count = ap->num_folios << PAGE_SHIFT;
 	ssize_t res;
 	int err;
 
@@ -918,6 +917,7 @@ static void fuse_readahead(struct readahead_control *rac)
 	struct inode *inode = rac->mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	unsigned int max_pages, nr_pages;
+	struct folio *folio = NULL;
 
 	if (fuse_is_bad(inode))
 		return;
@@ -939,8 +939,8 @@ static void fuse_readahead(struct readahead_control *rac)
 	while (nr_pages) {
 		struct fuse_io_args *ia;
 		struct fuse_args_pages *ap;
-		struct folio *folio;
 		unsigned cur_pages = min(max_pages, nr_pages);
+		unsigned int pages = 0;
 
 		if (fc->num_background >= fc->congestion_threshold &&
 		    rac->ra->async_size >= readahead_count(rac))
@@ -952,10 +952,12 @@ static void fuse_readahead(struct readahead_control *rac)
 
 		ia = fuse_io_alloc(NULL, cur_pages);
 		if (!ia)
-			return;
+			break;
 		ap = &ia->ap;
 
-		while (ap->num_folios < cur_pages) {
+		while (pages < cur_pages) {
+			unsigned int folio_pages;
+
 			/*
 			 * This returns a folio with a ref held on it.
 			 * The ref needs to be held until the request is
@@ -963,13 +965,29 @@ static void fuse_readahead(struct readahead_control *rac)
 			 * fuse_try_move_page()) drops the ref after it's
 			 * replaced in the page cache.
 			 */
-			folio = __readahead_folio(rac);
+			if (!folio)
+				folio =  __readahead_folio(rac);
Quoted 8 lines

fs/fuse/file.c static void fuse_readahead(struct readahead_control *rac) · line 974

Quoted 5 lines
fs/fuse/file.c +5 -0
+
+			folio_pages = folio_nr_pages(folio);
+			if (folio_pages > cur_pages - pages)
+				break;
+

Hmm, so let's assume this would be a 2MB folio, but fc->max_pages is limited to 1MB - we not do read-ahead anymore?

It's hard for me to say without seeing the actual enablement patches, but filesystems are supposed to call mapping_set_folio_order_range to constrain the sizes of the folios that the pagecache requests.

--D

Quoted 27 lines

Thanks,
Bernd

Quoted 21 lines
fs/fuse/file.c +12 -2
 			ap->folios[ap->num_folios] = folio;
 			ap->descs[ap->num_folios].length = folio_size(folio);
 			ap->num_folios++;
+			pages += folio_pages;
+			folio = NULL;
+		}
+		if (!pages) {
+			fuse_io_free(ia);
+			break;
 		}
-		fuse_send_readpages(ia, rac->file);
-		nr_pages -= cur_pages;
+		fuse_send_readpages(ia, rac->file, pages << PAGE_SHIFT);
+		nr_pages -= pages;
+	}
+	if (folio) {
+		folio_end_read(folio, false);
+		folio_put(folio);
 	}
 }
 
#22 lore raw

Re: [PATCH v5 09/11] fuse: support large folios for readahead

Quoted 82 lines · Darrick J. Wong
Quoted 72 lines · Bernd Schubert
Quoted 69 lines · Joanne Koong

Add support for folios larger than one page size for readahead.

---
 fs/fuse/file.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
fs/fuse/file.c +10 -7
index 1d38486fae50..9a31f2a516b9 100644
@@ -876,14 +876,13 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
 	fuse_io_free(ia);
 }
 
-static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
+static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
+				unsigned int count)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_mount *fm = ff->fm;
 	struct fuse_args_pages *ap = &ia->ap;
 	loff_t pos = folio_pos(ap->folios[0]);
-	/* Currently, all folios in FUSE are one page */
-	size_t count = ap->num_folios << PAGE_SHIFT;
 	ssize_t res;
 	int err;
 
@@ -918,6 +917,7 @@ static void fuse_readahead(struct readahead_control *rac)
 	struct inode *inode = rac->mapping->host;
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	unsigned int max_pages, nr_pages;
+	struct folio *folio = NULL;
 
 	if (fuse_is_bad(inode))
 		return;
@@ -939,8 +939,8 @@ static void fuse_readahead(struct readahead_control *rac)
 	while (nr_pages) {
 		struct fuse_io_args *ia;
 		struct fuse_args_pages *ap;
-		struct folio *folio;
 		unsigned cur_pages = min(max_pages, nr_pages);
+		unsigned int pages = 0;
 
 		if (fc->num_background >= fc->congestion_threshold &&
 		    rac->ra->async_size >= readahead_count(rac))
@@ -952,10 +952,12 @@ static void fuse_readahead(struct readahead_control *rac)
 
 		ia = fuse_io_alloc(NULL, cur_pages);
 		if (!ia)
-			return;
+			break;
 		ap = &ia->ap;
 
-		while (ap->num_folios < cur_pages) {
+		while (pages < cur_pages) {
+			unsigned int folio_pages;
+
 			/*
 			 * This returns a folio with a ref held on it.
 			 * The ref needs to be held until the request is
@@ -963,13 +965,29 @@ static void fuse_readahead(struct readahead_control *rac)
 			 * fuse_try_move_page()) drops the ref after it's
 			 * replaced in the page cache.
 			 */
-			folio = __readahead_folio(rac);
+			if (!folio)
+				folio =  __readahead_folio(rac);
Quoted 8 lines

fs/fuse/file.c static void fuse_readahead(struct readahead_control *rac) · line 974

Quoted 5 lines
fs/fuse/file.c +5 -0
+
+			folio_pages = folio_nr_pages(folio);
+			if (folio_pages > cur_pages - pages)
+				break;
+

Hmm, so let's assume this would be a 2MB folio, but fc->max_pages is limited to 1MB - we not do read-ahead anymore?

Quoted 3 lines

It's hard for me to say without seeing the actual enablement patches, but filesystems are supposed to call mapping_set_folio_order_range to constrain the sizes of the folios that the pagecache requests.

I think large folios do not get enabled ye in this series. Could we have a comment here that folio size is supposed to be restricted to fc->max_pages? And wouldn't that be a case for unlikely()?

Thanks,
Bernd

#23 lore raw

Re: [PATCH v5 09/11] fuse: support large folios for readahead

Quoted 86 lines · Bernd Schubert
Quoted 82 lines · Darrick J. Wong
Quoted 80 lines · Bernd Schubert
Quoted 64 lines · Joanne Koong

Add support for folios larger than one page size for readahead.

---
 fs/fuse/file.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
fs/fuse/file.c +8 -6
index 1d38486fae50..9a31f2a516b9 100644
@@ -876,14 +876,13 @@ static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
    fuse_io_free(ia);
 }
 
-static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
+static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
+                           unsigned int count)
 {
    struct fuse_file *ff = file->private_data;
    struct fuse_mount *fm = ff->fm;
    struct fuse_args_pages *ap = &ia->ap;
    loff_t pos = folio_pos(ap->folios[0]);
-   /* Currently, all folios in FUSE are one page */
-   size_t count = ap->num_folios << PAGE_SHIFT;
    ssize_t res;
    int err;
 
@@ -918,6 +917,7 @@ static void fuse_readahead(struct readahead_control *rac)
    struct inode *inode = rac->mapping->host;
    struct fuse_conn *fc = get_fuse_conn(inode);
    unsigned int max_pages, nr_pages;
+   struct folio *folio = NULL;
 
    if (fuse_is_bad(inode))
            return;
@@ -939,8 +939,8 @@ static void fuse_readahead(struct readahead_control *rac)
    while (nr_pages) {
            struct fuse_io_args *ia;
            struct fuse_args_pages *ap;
-           struct folio *folio;
            unsigned cur_pages = min(max_pages, nr_pages);
+           unsigned int pages = 0;
 
            if (fc->num_background >= fc->congestion_threshold &&
                rac->ra->async_size >= readahead_count(rac))
@@ -952,10 +952,12 @@ static void fuse_readahead(struct readahead_control *rac)
 
            ia = fuse_io_alloc(NULL, cur_pages);
            if (!ia)
-                   return;
+                   break;
            ap = &ia->ap;
 
-           while (ap->num_folios < cur_pages) {
+           while (pages < cur_pages) {
+                   unsigned int folio_pages;
+
                    /*
                     * This returns a folio with a ref held on it.
                     * The ref needs to be held until the request is
@@ -963,13 +965,29 @@ static void fuse_readahead(struct readahead_control *rac)
                     * fuse_try_move_page()) drops the ref after it's

fs/fuse/file.c static void fuse_readahead(struct readahead_control *rac) · line 974

Quoted 10 lines
fs/fuse/file.c +7 -1
                     * replaced in the page cache.
                     */
-                   folio = __readahead_folio(rac);
+                   if (!folio)
+                           folio =  __readahead_folio(rac);
+
+                   folio_pages = folio_nr_pages(folio);
+                   if (folio_pages > cur_pages - pages)
+                           break;
+

Hmm, so let's assume this would be a 2MB folio, but fc->max_pages is limited to 1MB - we not do read-ahead anymore?

Quoted 3 lines
Quoted 3 lines

It's hard for me to say without seeing the actual enablement patches, but filesystems are supposed to call mapping_set_folio_order_range to constrain the sizes of the folios that the pagecache requests.

Yes, exactly. For enabling fuse, I envision adding something like this in fuse_init_file_inode():

max_pages = min(min(fc->max_write, fc->max_read) >> PAGE_SHIFT, fc->max_pages); max_order = ilog2(max_pages); mapping_set_folio_order_range(inode->i_mapping, 0, max_order);

Quoted 4 lines

I think large folios do not get enabled ye in this series. Could we have a comment here that folio size is supposed to be restricted to fc->max_pages? And wouldn't that be a case for unlikely()?

Large folios are not enabled yet in this series. The cover letter explains a bit why,

"This does not yet switch fuse to using large folios. Using large folios in fuse is dependent on adding granular dirty-page tracking. This will be done in a separate patchset that will have fuse use iomap [1]. There also needs to be a followup (also part of future work) for having dirty page balancing not tank performance for unprivileged servers where bdi limits lead to subpar throttling [1], before enabling large folios for fuse."

[1] https://lore.kernel.org/linux-fsdevel/CAJnrk1a38pv3OgFZRfdTiDMXuPWuBgN8KY47XfOsYHj=N2wxAg@mail.gmail.com/#t

I'll add a comment about this in v6.

Thanks,
Joanne

Quoted 4 lines

Thanks,
Bernd

#24 lore raw

PATCH v5 10/11 fuse: optimize direct io large folios processing

In reply to Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

---
 fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +41 -14
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 	}
 
 	while (nbytes < *nbytesp && nr_pages < max_pages) {
-		unsigned nfolios, i;
+		struct folio *prev_folio = NULL;
+		unsigned npages, i;
 		size_t start;
 
 		ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
 		nbytes += ret;
 
-		nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+		npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-		for (i = 0; i < nfolios; i++) {
-			struct folio *folio = page_folio(pages[i]);
-			unsigned int offset = start +
-				(folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-			unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+		/*
+		 * We must check each extracted page. We can't assume every page
+		 * in a large folio is used. For example, userspace may mmap() a
+		 * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+		 * a large folio, in which case the extracted pages could be
+		 *
+		 * folio A page 0
+		 * folio A page 1
+		 * folio B page 0
+		 * folio A page 3
+		 *
+		 * where folio A belongs to the file and folio B is an anonymous
+		 * COW page.
+		 */
+		for (i = 0; i < npages && ret; i++) {
+			struct folio *folio;
+			unsigned int offset;
+			unsigned int len;
+
+			WARN_ON(!pages[i]);
+			folio = page_folio(pages[i]);
+
+			len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+			if (folio == prev_folio && pages[i] != pages[i - 1]) {
+				WARN_ON(ap->folios[ap->num_folios - 1] != folio);
+				ap->descs[ap->num_folios - 1].length += len;
+				WARN_ON(ap->descs[ap->num_folios - 1].length > folio_size(folio));
+			} else {
+				offset = start + (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
+				ap->descs[ap->num_folios].offset = offset;
+				ap->descs[ap->num_folios].length = len;
+				ap->folios[ap->num_folios] = folio;
+				start = 0;
+				ap->num_folios++;
+				prev_folio = folio;
+			}
 
-			ap->descs[ap->num_folios].offset = offset;
-			ap->descs[ap->num_folios].length = len;
-			ap->folios[ap->num_folios] = folio;
-			start = 0;
 			ret -= len;
-			ap->num_folios++;
 		}
-
-		nr_pages += nfolios;
+		nr_pages += npages;
 	}
 	kfree(pages);
 
Signature · 1 line
2.47.1
Reviewed-by on v5 10/11
#25 lore raw

Re: [PATCH v5 10/11] fuse: optimize direct io large folios processing

Quoted 6 lines · Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

Quoted 81 lines
---
 fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +41 -14
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 	}
 
 	while (nbytes < *nbytesp && nr_pages < max_pages) {
-		unsigned nfolios, i;
+		struct folio *prev_folio = NULL;
+		unsigned npages, i;
 		size_t start;
 
 		ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
 		nbytes += ret;
 
-		nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+		npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-		for (i = 0; i < nfolios; i++) {
-			struct folio *folio = page_folio(pages[i]);
-			unsigned int offset = start +
-				(folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-			unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+		/*
+		 * We must check each extracted page. We can't assume every page
+		 * in a large folio is used. For example, userspace may mmap() a
+		 * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+		 * a large folio, in which case the extracted pages could be
+		 *
+		 * folio A page 0
+		 * folio A page 1
+		 * folio B page 0
+		 * folio A page 3
+		 *
+		 * where folio A belongs to the file and folio B is an anonymous
+		 * COW page.
+		 */
+		for (i = 0; i < npages && ret; i++) {
+			struct folio *folio;
+			unsigned int offset;
+			unsigned int len;
+
+			WARN_ON(!pages[i]);
+			folio = page_folio(pages[i]);
+
+			len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+			if (folio == prev_folio && pages[i] != pages[i - 1]) {
+				WARN_ON(ap->folios[ap->num_folios - 1] != folio);
+				ap->descs[ap->num_folios - 1].length += len;
+				WARN_ON(ap->descs[ap->num_folios - 1].length > folio_size(folio));
+			} else {
+				offset = start + (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
+				ap->descs[ap->num_folios].offset = offset;
+				ap->descs[ap->num_folios].length = len;
+				ap->folios[ap->num_folios] = folio;
+				start = 0;
+				ap->num_folios++;
+				prev_folio = folio;
+			}
 
-			ap->descs[ap->num_folios].offset = offset;
-			ap->descs[ap->num_folios].length = len;
-			ap->folios[ap->num_folios] = folio;
-			start = 0;
 			ret -= len;
-			ap->num_folios++;
 		}
-
-		nr_pages += nfolios;
+		nr_pages += npages;
 	}
 	kfree(pages);
 
#26 lore raw

Re: [PATCH v5 10/11] fuse: optimize direct io large folios processing

In reply to Joanne Koong
Quoted 51 lines · Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

---
  fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
  1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +18 -7
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
  	}
 
  	while (nbytes < *nbytesp && nr_pages < max_pages) {
-		unsigned nfolios, i;
+		struct folio *prev_folio = NULL;
+		unsigned npages, i;
  		size_t start;
 
  		ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
  		nbytes += ret;
 
-		nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+		npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-		for (i = 0; i < nfolios; i++) {
-			struct folio *folio = page_folio(pages[i]);
-			unsigned int offset = start +
-				(folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-			unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+		/*
+		 * We must check each extracted page. We can't assume every page
+		 * in a large folio is used. For example, userspace may mmap() a
+		 * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+		 * a large folio, in which case the extracted pages could be
+		 *
+		 * folio A page 0
+		 * folio A page 1
+		 * folio B page 0
+		 * folio A page 3
+		 *
+		 * where folio A belongs to the file and folio B is an anonymous
+		 * COW page.
+		 */
+		for (i = 0; i < npages && ret; i++) {

fs/fuse/file.c static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, · line 1532

Quoted 10 lines
fs/fuse/file.c +10 -0
+			struct folio *folio;
+			unsigned int offset;
+			unsigned int len;
+
+			WARN_ON(!pages[i]);
+			folio = page_folio(pages[i]);
+
+			len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+			if (folio == prev_folio && pages[i] != pages[i - 1]) {

I don't really understand the "pages[i] != pages[i - 1]" part.

Why would you have to equal page pointers in there?

Something that might be simpler to understand and implement would be using

num_pages_contiguous()

from

	https://lore.kernel.org/all/[email protected]/T/#u

and then just making sure that we don't exceed the current folio, if we ever get contiguous pages that cross a folio.

Signature · 3 lines
Cheers,

David / dhildenb
#27 lore raw

Re: [PATCH v5 10/11] fuse: optimize direct io large folios processing

Quoted 66 lines · David Hildenbrand
Quoted 51 lines · Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

---
  fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
  1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +18 -7
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
      }
 
      while (nbytes < *nbytesp && nr_pages < max_pages) {
-             unsigned nfolios, i;
+             struct folio *prev_folio = NULL;
+             unsigned npages, i;
              size_t start;
 
              ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
              nbytes += ret;
 
-             nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+             npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-             for (i = 0; i < nfolios; i++) {
-                     struct folio *folio = page_folio(pages[i]);
-                     unsigned int offset = start +
-                             (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-                     unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+             /*
+              * We must check each extracted page. We can't assume every page
+              * in a large folio is used. For example, userspace may mmap() a
+              * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+              * a large folio, in which case the extracted pages could be
+              *
+              * folio A page 0
+              * folio A page 1
+              * folio B page 0
+              * folio A page 3
+              *
+              * where folio A belongs to the file and folio B is an anonymous
+              * COW page.
+              */
+             for (i = 0; i < npages && ret; i++) {

fs/fuse/file.c static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, · line 1532

Quoted 10 lines
fs/fuse/file.c +10 -0
+                     struct folio *folio;
+                     unsigned int offset;
+                     unsigned int len;
+
+                     WARN_ON(!pages[i]);
+                     folio = page_folio(pages[i]);
+
+                     len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+                     if (folio == prev_folio && pages[i] != pages[i - 1]) {

I don't really understand the "pages[i] != pages[i - 1]" part.

Quoted 2 lines

Why would you have to equal page pointers in there?

The pages extracted are user pages from a userspace iovec. AFAICT, there's the possibility, eg if userspace mmaps() the file with copy-on-write, that the same physical page could back multiple contiguous virtual addresses.

Quoted 11 lines

Something that might be simpler to understand and implement would be using

num_pages_contiguous()

from

        https://lore.kernel.org/all/[email protected]/T/#u

and then just making sure that we don't exceed the current folio, if we ever get contiguous pages that cross a folio.

Thanks for the link. I think here it's common that the pages array would hold pages from multiple different folios, so maybe a new helper num_pages_contiguous_folio() would be useful to return back the number of contiguous pages that are within the scope of the same folio.

Thanks,
Joanne

Quoted 7 lines

--
Cheers,

David / dhildenb

#28 lore raw

Re: [PATCH v5 10/11] fuse: optimize direct io large folios processing

Quoted 67 lines · Joanne Koong
Quoted 66 lines · David Hildenbrand
Quoted 51 lines · Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

---
   fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
   1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +18 -7
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
       }
 
       while (nbytes < *nbytesp && nr_pages < max_pages) {
-             unsigned nfolios, i;
+             struct folio *prev_folio = NULL;
+             unsigned npages, i;
               size_t start;
 
               ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
               nbytes += ret;
 
-             nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+             npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-             for (i = 0; i < nfolios; i++) {
-                     struct folio *folio = page_folio(pages[i]);
-                     unsigned int offset = start +
-                             (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-                     unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+             /*
+              * We must check each extracted page. We can't assume every page
+              * in a large folio is used. For example, userspace may mmap() a
+              * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+              * a large folio, in which case the extracted pages could be
+              *
+              * folio A page 0
+              * folio A page 1
+              * folio B page 0
+              * folio A page 3
+              *
+              * where folio A belongs to the file and folio B is an anonymous
+              * COW page.
+              */
+             for (i = 0; i < npages && ret; i++) {

fs/fuse/file.c static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, · line 1532

Quoted 10 lines
fs/fuse/file.c +10 -0
+                     struct folio *folio;
+                     unsigned int offset;
+                     unsigned int len;
+
+                     WARN_ON(!pages[i]);
+                     folio = page_folio(pages[i]);
+
+                     len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+                     if (folio == prev_folio && pages[i] != pages[i - 1]) {

I don't really understand the "pages[i] != pages[i - 1]" part.

Quoted 7 lines
Quoted 2 lines

Why would you have to equal page pointers in there?

The pages extracted are user pages from a userspace iovec. AFAICT, there's the possibility, eg if userspace mmaps() the file with copy-on-write, that the same physical page could back multiple contiguous virtual addresses.

Yes, I but I was rather curious why that would be a condition we are checking. It's quite the ... corner case :)

Quoted 10 lines
Quoted 10 lines

Something that might be simpler to understand and implement would be using

num_pages_contiguous()

from

         https://lore.kernel.org/all/[email protected]/T/#u
Quoted 7 lines
Quoted 2 lines

and then just making sure that we don't exceed the current folio, if we ever get contiguous pages that cross a folio.

Thanks for the link. I think here it's common that the pages array would hold pages from multiple different folios, so maybe a new helper num_pages_contiguous_folio() would be useful to return back the number of contiguous pages that are within the scope of the same folio.

Yes, something like that can be useful.

Signature · 3 lines
Cheers,

David / dhildenb
#29 lore raw

Re: [PATCH v5 10/11] fuse: optimize direct io large folios processing

Quoted 72 lines · David Hildenbrand
Quoted 70 lines · Joanne Koong
Quoted 68 lines · David Hildenbrand
Quoted 51 lines · Joanne Koong

Optimize processing folios larger than one page size for the direct io case. If contiguous pages are part of the same folio, collate the processing instead of processing each page in the folio separately.

---
   fs/fuse/file.c | 55 +++++++++++++++++++++++++++++++++++++-------------
   1 file changed, 41 insertions(+), 14 deletions(-)
fs/fuse/file.c +18 -7
index 9a31f2a516b9..61eaec1c993b 100644
@@ -1490,7 +1490,8 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
       }
 
       while (nbytes < *nbytesp && nr_pages < max_pages) {
-             unsigned nfolios, i;
+             struct folio *prev_folio = NULL;
+             unsigned npages, i;
               size_t start;
 
               ret = iov_iter_extract_pages(ii, &pages,
@@ -1502,23 +1503,49 @@ static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
 
               nbytes += ret;
 
-             nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
+             npages = DIV_ROUND_UP(ret + start, PAGE_SIZE);
 
-             for (i = 0; i < nfolios; i++) {
-                     struct folio *folio = page_folio(pages[i]);
-                     unsigned int offset = start +
-                             (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
-                     unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
+             /*
+              * We must check each extracted page. We can't assume every page
+              * in a large folio is used. For example, userspace may mmap() a
+              * file PROT_WRITE, MAP_PRIVATE, and then store to the middle of
+              * a large folio, in which case the extracted pages could be
+              *
+              * folio A page 0
+              * folio A page 1
+              * folio B page 0
+              * folio A page 3
+              *
+              * where folio A belongs to the file and folio B is an anonymous
+              * COW page.
+              */
+             for (i = 0; i < npages && ret; i++) {

fs/fuse/file.c static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, · line 1532

Quoted 10 lines
fs/fuse/file.c +10 -0
+                     struct folio *folio;
+                     unsigned int offset;
+                     unsigned int len;
+
+                     WARN_ON(!pages[i]);
+                     folio = page_folio(pages[i]);
+
+                     len = min_t(unsigned int, ret, PAGE_SIZE - start);
+
+                     if (folio == prev_folio && pages[i] != pages[i - 1]) {

I don't really understand the "pages[i] != pages[i - 1]" part.

Why would you have to equal page pointers in there?

Quoted 8 lines
Quoted 4 lines

The pages extracted are user pages from a userspace iovec. AFAICT, there's the possibility, eg if userspace mmaps() the file with copy-on-write, that the same physical page could back multiple contiguous virtual addresses.

Yes, I but I was rather curious why that would be a condition we are checking. It's quite the ... corner case :)

Agreed, definitely the corner case :)

In the fuse code, later on when the buffer gets copied to/from the server, it'll use ap->descs[index].length as the number of bytes to copy. If we don't check for this duplicate page corner case, then it'll copy the wrong offsets in the folio, which may even lead to a page fault if the folio is only one page. This buffer copying logic if you're curious happens in fuse_copy_args() -> fuse_copy_folios() -> fuse_copy_folio().

Quoted 26 lines
Quoted 10 lines
Quoted 10 lines

Something that might be simpler to understand and implement would be using

num_pages_contiguous()

from

         https://lore.kernel.org/all/[email protected]/T/#u
Quoted 7 lines
Quoted 2 lines

and then just making sure that we don't exceed the current folio, if we ever get contiguous pages that cross a folio.

Thanks for the link. I think here it's common that the pages array would hold pages from multiple different folios, so maybe a new helper num_pages_contiguous_folio() would be useful to return back the number of contiguous pages that are within the scope of the same folio.

Yes, something like that can be useful.

--
Cheers,

David / dhildenb

#30 lore raw

PATCH v5 11/11 fuse: support large folios for writeback

In reply to Joanne Koong

Add support for folios larger than one page size for writeback.

---
 fs/fuse/file.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
fs/fuse/file.c +8 -4
index 61eaec1c993b..5e7187446730 100644
@@ -2014,7 +2014,7 @@ static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struc
 
 	ap->folios[folio_index] = folio;
 	ap->descs[folio_index].offset = 0;
-	ap->descs[folio_index].length = PAGE_SIZE;
+	ap->descs[folio_index].length = folio_size(folio);
 
 	inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
 }
@@ -2088,6 +2088,7 @@ struct fuse_fill_wb_data {
 	struct fuse_file *ff;
 	struct inode *inode;
 	unsigned int max_folios;
+	unsigned int nr_pages;
 };
 
 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
@@ -2135,15 +2136,15 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio,
 	WARN_ON(!ap->num_folios);
 
 	/* Reached max pages */
-	if (ap->num_folios == fc->max_pages)
+	if (data->nr_pages + folio_nr_pages(folio) > fc->max_pages)
 		return true;
 
 	/* Reached max write bytes */
-	if ((ap->num_folios + 1) * PAGE_SIZE > fc->max_write)
+	if ((data->nr_pages * PAGE_SIZE) + folio_size(folio) > fc->max_write)
 		return true;
 
 	/* Discontinuity */
-	if (ap->folios[ap->num_folios - 1]->index + 1 != folio_index(folio))
+	if (folio_next_index(ap->folios[ap->num_folios - 1]) != folio_index(folio))
 		return true;
 
 	/* Need to grow the pages array?  If so, did the expansion fail? */
@@ -2174,6 +2175,7 @@ static int fuse_writepages_fill(struct folio *folio,
 	if (wpa && fuse_writepage_need_send(fc, folio, ap, data)) {
 		fuse_writepages_send(data);
 		data->wpa = NULL;
+		data->nr_pages = 0;
 	}
 
 	if (data->wpa == NULL) {
@@ -2188,6 +2190,7 @@ static int fuse_writepages_fill(struct folio *folio,
 	folio_start_writeback(folio);
 
 	fuse_writepage_args_page_fill(wpa, folio, ap->num_folios);
+	data->nr_pages += folio_nr_pages(folio);
 
 	err = 0;
 	ap->num_folios++;
@@ -2218,6 +2221,7 @@ static int fuse_writepages(struct address_space *mapping,
 	data.inode = inode;
 	data.wpa = NULL;
 	data.ff = NULL;
+	data.nr_pages = 0;
 
 	err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
 	if (data.wpa) {
Signature · 1 line
2.47.1