linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Add folio_zero_tail() and folio_fill_tail()
@ 2023-11-07 21:26 Matthew Wilcox (Oracle)
  2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Matthew Wilcox (Oracle) @ 2023-11-07 21:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2, linux-fsdevel,
	linux-xfs, Darrick J . Wong, linux-erofs, Theodore Ts'o,
	Andreas Dilger, Andreas Gruenbacher

I'm trying to make it easier for filesystems with tailpacking /
stuffing / inline data to use folios.  The primary function here is
folio_fill_tail().  You give it a pointer to memory where the data
currently is, and it takes care of copying it into the folio at that
offset.  That works for gfs2 & iomap.  Then There's Ext4.  Rather than
gin up some kind of specialist "Here's a two pointers to two blocks
of memory" routine, just let it do its current thing, and let it call
folio_zero_tail(), which is also called by folio_fill_tail().

Other filesystems can be converted later; these ones seemed like good
examples as they're already partly or completely converted to folios.

Matthew Wilcox (Oracle) (3):
  mm: Add folio_zero_tail() and use it in ext4
  mm: Add folio_fill_tail() and use it in iomap
  gfs2: Convert stuffed_readpage() to stuffed_read_folio()

 fs/ext4/inline.c        |  3 +-
 fs/gfs2/aops.c          | 37 +++++++++-----------
 fs/iomap/buffered-io.c  | 14 ++------
 include/linux/highmem.h | 76 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 34 deletions(-)

-- 
2.42.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-07 21:26 [PATCH 0/3] Add folio_zero_tail() and folio_fill_tail() Matthew Wilcox (Oracle)
@ 2023-11-07 21:26 ` Matthew Wilcox (Oracle)
  2023-11-08 23:06   ` Andrew Morton
  2023-11-09 21:50   ` Andreas Gruenbacher
  2023-11-07 21:26 ` [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap Matthew Wilcox (Oracle)
  2023-11-07 21:26 ` [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio() Matthew Wilcox (Oracle)
  2 siblings, 2 replies; 14+ messages in thread
From: Matthew Wilcox (Oracle) @ 2023-11-07 21:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2, linux-fsdevel,
	linux-xfs, Darrick J . Wong, linux-erofs, Theodore Ts'o,
	Andreas Dilger, Andreas Gruenbacher

Instead of unmapping the folio after copying the data to it, then mapping
it again to zero the tail, provide folio_zero_tail() to zero the tail
of an already-mapped folio.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 fs/ext4/inline.c        |  3 +--
 include/linux/highmem.h | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index 9a84a5f9fef4..d5bd1e3a5d36 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -502,9 +502,8 @@ static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
 	BUG_ON(len > PAGE_SIZE);
 	kaddr = kmap_local_folio(folio, 0);
 	ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
-	flush_dcache_folio(folio);
+	kaddr = folio_zero_tail(folio, len, kaddr + len);
 	kunmap_local(kaddr);
-	folio_zero_segment(folio, len, folio_size(folio));
 	folio_mark_uptodate(folio);
 	brelse(iloc.bh);
 
diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 4cacc0e43b51..1b81416196dd 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -483,6 +483,44 @@ static inline void memcpy_to_folio(struct folio *folio, size_t offset,
 	flush_dcache_folio(folio);
 }
 
+/**
+ * folio_zero_tail - Zero the tail of a folio.
+ * @folio: The folio to zero.
+ * @kaddr: The address the folio is currently mapped to.
+ * @offset: The byte offset in the folio to start zeroing at.
+ *
+ * If you have already used kmap_local_folio() to map a folio, written
+ * some data to it and now need to zero the end of the folio (and flush
+ * the dcache), you can use this function.  If you do not have the
+ * folio kmapped (eg the folio has been partially populated by DMA),
+ * use folio_zero_range() or folio_zero_segment() instead.
+ *
+ * Return: An address which can be passed to kunmap_local().
+ */
+static inline __must_check void *folio_zero_tail(struct folio *folio,
+		size_t offset, void *kaddr)
+{
+	size_t len = folio_size(folio) - offset;
+
+	if (folio_test_highmem(folio)) {
+		size_t max = PAGE_SIZE - offset_in_page(offset);
+
+		while (len > max) {
+			memset(kaddr, 0, max);
+			kunmap_local(kaddr);
+			len -= max;
+			offset += max;
+			max = PAGE_SIZE;
+			kaddr = kmap_local_folio(folio, offset);
+		}
+	}
+
+	memset(kaddr, 0, len);
+	flush_dcache_folio(folio);
+
+	return kaddr;
+}
+
 /**
  * memcpy_from_file_folio - Copy some bytes from a file folio.
  * @to: The destination buffer.
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap
  2023-11-07 21:26 [PATCH 0/3] Add folio_zero_tail() and folio_fill_tail() Matthew Wilcox (Oracle)
  2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
@ 2023-11-07 21:26 ` Matthew Wilcox (Oracle)
  2023-11-09 21:50   ` Andreas Gruenbacher
  2023-11-07 21:26 ` [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio() Matthew Wilcox (Oracle)
  2 siblings, 1 reply; 14+ messages in thread
From: Matthew Wilcox (Oracle) @ 2023-11-07 21:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2, linux-fsdevel,
	linux-xfs, Darrick J . Wong, linux-erofs, Theodore Ts'o,
	Andreas Dilger, Andreas Gruenbacher

The iomap code was limited to PAGE_SIZE bytes; generalise it to cover
an arbitrary-sized folio, and move it to be a common helper.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 fs/iomap/buffered-io.c  | 14 ++------------
 include/linux/highmem.h | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index f72df2babe56..093c4515b22a 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -305,28 +305,18 @@ static int iomap_read_inline_data(const struct iomap_iter *iter,
 {
 	const struct iomap *iomap = iomap_iter_srcmap(iter);
 	size_t size = i_size_read(iter->inode) - iomap->offset;
-	size_t poff = offset_in_page(iomap->offset);
 	size_t offset = offset_in_folio(folio, iomap->offset);
-	void *addr;
 
 	if (folio_test_uptodate(folio))
 		return 0;
 
-	if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
-		return -EIO;
-	if (WARN_ON_ONCE(size > PAGE_SIZE -
-			 offset_in_page(iomap->inline_data)))
-		return -EIO;
 	if (WARN_ON_ONCE(size > iomap->length))
 		return -EIO;
 	if (offset > 0)
 		ifs_alloc(iter->inode, folio, iter->flags);
 
-	addr = kmap_local_folio(folio, offset);
-	memcpy(addr, iomap->inline_data, size);
-	memset(addr + size, 0, PAGE_SIZE - poff - size);
-	kunmap_local(addr);
-	iomap_set_range_uptodate(folio, offset, PAGE_SIZE - poff);
+	folio_fill_tail(folio, offset, iomap->inline_data, size);
+	iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
 	return 0;
 }
 
diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 1b81416196dd..0fbb60ffefc9 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -521,6 +521,44 @@ static inline __must_check void *folio_zero_tail(struct folio *folio,
 	return kaddr;
 }
 
+/**
+ * folio_fill_tail - Copy some data to a folio and pad with zeroes.
+ * @folio: The destination folio.
+ * @offset: The offset into @folio at which to start copying.
+ * @from: The data to copy.
+ * @len: How many bytes of data to copy.
+ *
+ * This function is most useful for filesystems which support inline data.
+ * When they want to copy data from the inode into the page cache, this
+ * function does everything for them.  It supports large folios even on
+ * HIGHMEM configurations.
+ */
+static inline void folio_fill_tail(struct folio *folio, size_t offset,
+		const char *from, size_t len)
+{
+	char *to = kmap_local_folio(folio, offset);
+
+	VM_BUG_ON(offset + len > folio_size(folio));
+
+	if (folio_test_highmem(folio)) {
+		size_t max = PAGE_SIZE - offset_in_page(offset);
+
+		while (len > max) {
+			memcpy(to, from, max);
+			kunmap_local(to);
+			len -= max;
+			from += max;
+			offset += max;
+			max = PAGE_SIZE;
+			to = kmap_local_folio(folio, offset);
+		}
+	}
+
+	memcpy(to, from, len);
+	to = folio_zero_tail(folio, offset, to);
+	kunmap_local(to);
+}
+
 /**
  * memcpy_from_file_folio - Copy some bytes from a file folio.
  * @to: The destination buffer.
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio()
  2023-11-07 21:26 [PATCH 0/3] Add folio_zero_tail() and folio_fill_tail() Matthew Wilcox (Oracle)
  2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
  2023-11-07 21:26 ` [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap Matthew Wilcox (Oracle)
@ 2023-11-07 21:26 ` Matthew Wilcox (Oracle)
  2023-11-09 21:52   ` Andreas Gruenbacher
  2 siblings, 1 reply; 14+ messages in thread
From: Matthew Wilcox (Oracle) @ 2023-11-07 21:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2, linux-fsdevel,
	linux-xfs, Darrick J . Wong, linux-erofs, Theodore Ts'o,
	Andreas Dilger, Andreas Gruenbacher

Use folio_fill_tail() to implement the unstuffing and folio_end_read()
to simultaneously mark the folio uptodate and unlock it.  Unifies a
couple of code paths.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 fs/gfs2/aops.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
index 9611bfceda4b..ba8742dc91f8 100644
--- a/fs/gfs2/aops.c
+++ b/fs/gfs2/aops.c
@@ -403,18 +403,18 @@ static int gfs2_jdata_writepages(struct address_space *mapping,
 }
 
 /**
- * stuffed_readpage - Fill in a Linux folio with stuffed file data
+ * stuffed_read_folio - Fill in a Linux folio with stuffed file data
  * @ip: the inode
  * @folio: the folio
  *
  * Returns: errno
  */
-static int stuffed_readpage(struct gfs2_inode *ip, struct folio *folio)
+static int stuffed_read_folio(struct gfs2_inode *ip, struct folio *folio)
 {
-	struct buffer_head *dibh;
-	size_t i_size = i_size_read(&ip->i_inode);
-	void *data;
-	int error;
+	struct buffer_head *dibh = NULL;
+	size_t dsize = i_size_read(&ip->i_inode);
+	void *from = NULL;
+	int error = 0;
 
 	/*
 	 * Due to the order of unstuffing files and ->fault(), we can be
@@ -422,22 +422,20 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct folio *folio)
 	 * so we need to supply one here. It doesn't happen often.
 	 */
 	if (unlikely(folio->index)) {
-		folio_zero_range(folio, 0, folio_size(folio));
-		folio_mark_uptodate(folio);
-		return 0;
+		dsize = 0;
+	} else {
+		error = gfs2_meta_inode_buffer(ip, &dibh);
+		if (error)
+			goto out;
+		from = dibh->b_data + sizeof(struct gfs2_dinode);
 	}
 
-	error = gfs2_meta_inode_buffer(ip, &dibh);
-	if (error)
-		return error;
-
-	data = dibh->b_data + sizeof(struct gfs2_dinode);
-	memcpy_to_folio(folio, 0, data, i_size);
-	folio_zero_range(folio, i_size, folio_size(folio) - i_size);
+	folio_fill_tail(folio, 0, from, dsize);
 	brelse(dibh);
-	folio_mark_uptodate(folio);
+out:
+	folio_end_read(folio, error == 0);
 
-	return 0;
+	return error;
 }
 
 /**
@@ -456,8 +454,7 @@ static int gfs2_read_folio(struct file *file, struct folio *folio)
 	    (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
 		error = iomap_read_folio(folio, &gfs2_iomap_ops);
 	} else if (gfs2_is_stuffed(ip)) {
-		error = stuffed_readpage(ip, folio);
-		folio_unlock(folio);
+		error = stuffed_read_folio(ip, folio);
 	} else {
 		error = mpage_read_folio(folio, gfs2_block_map);
 	}
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
@ 2023-11-08 23:06   ` Andrew Morton
  2023-11-09  0:12     ` Andreas Grünbacher
  2023-11-09 17:37     ` Matthew Wilcox
  2023-11-09 21:50   ` Andreas Gruenbacher
  1 sibling, 2 replies; 14+ messages in thread
From: Andrew Morton @ 2023-11-08 23:06 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: linux-ext4, gfs2, linux-fsdevel, linux-xfs, Darrick J . Wong,
	linux-erofs, Theodore Ts'o, Andreas Dilger,
	Andreas Gruenbacher

On Tue,  7 Nov 2023 21:26:40 +0000 "Matthew Wilcox (Oracle)" <willy@infradead.org> wrote:

> Instead of unmapping the folio after copying the data to it, then mapping
> it again to zero the tail, provide folio_zero_tail() to zero the tail
> of an already-mapped folio.
> 
> ...
>
> --- a/include/linux/highmem.h
> +++ b/include/linux/highmem.h
> @@ -483,6 +483,44 @@ static inline void memcpy_to_folio(struct folio *folio, size_t offset,
>  	flush_dcache_folio(folio);
>  }
>  
> +/**
> + * folio_zero_tail - Zero the tail of a folio.
> + * @folio: The folio to zero.
> + * @kaddr: The address the folio is currently mapped to.
> + * @offset: The byte offset in the folio to start zeroing at.

That's the argument ordering I would expect.

> + * If you have already used kmap_local_folio() to map a folio, written
> + * some data to it and now need to zero the end of the folio (and flush
> + * the dcache), you can use this function.  If you do not have the
> + * folio kmapped (eg the folio has been partially populated by DMA),
> + * use folio_zero_range() or folio_zero_segment() instead.
> + *
> + * Return: An address which can be passed to kunmap_local().
> + */
> +static inline __must_check void *folio_zero_tail(struct folio *folio,
> +		size_t offset, void *kaddr)

While that is not.  addr,len is far more common that len,addr?

> +{
> +	size_t len = folio_size(folio) - offset;

Calling it `remaining' would be more clear.

> +
> +	if (folio_test_highmem(folio)) {
> +		size_t max = PAGE_SIZE - offset_in_page(offset);
> +
> +		while (len > max) {

Shouldn't this be `while (len)'?  AFAICT this code can fail to clear
the final page.

> +			memset(kaddr, 0, max);
> +			kunmap_local(kaddr);
> +			len -= max;
> +			offset += max;
> +			max = PAGE_SIZE;
> +			kaddr = kmap_local_folio(folio, offset);
> +		}
> +	}
> +
> +	memset(kaddr, 0, len);
> +	flush_dcache_folio(folio);
> +
> +	return kaddr;
> +}
> +


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-08 23:06   ` Andrew Morton
@ 2023-11-09  0:12     ` Andreas Grünbacher
  2023-11-09 17:27       ` Andrew Morton
  2023-11-09 17:37     ` Matthew Wilcox
  1 sibling, 1 reply; 14+ messages in thread
From: Andreas Grünbacher @ 2023-11-09  0:12 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2,
	Linux FS-devel Mailing List, linux-xfs, Darrick J . Wong,
	linux-erofs, Theodore Ts'o, Andreas Dilger,
	Andreas Gruenbacher

Andrew,

Andrew Morton <akpm@linux-foundation.org> schrieb am Do., 9. Nov. 2023, 00:06:
> > +
> > +     if (folio_test_highmem(folio)) {
> > +             size_t max = PAGE_SIZE - offset_in_page(offset);
> > +
> > +             while (len > max) {
>
> Shouldn't this be `while (len)'?  AFAICT this code can fail to clear
> the final page.

not sure what you're seeing there, but this looks fine to me.

Thanks,
Andreas

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-09  0:12     ` Andreas Grünbacher
@ 2023-11-09 17:27       ` Andrew Morton
  0 siblings, 0 replies; 14+ messages in thread
From: Andrew Morton @ 2023-11-09 17:27 UTC (permalink / raw)
  To: Andreas Grünbacher
  Cc: Matthew Wilcox (Oracle), linux-ext4, gfs2,
	Linux FS-devel Mailing List, linux-xfs, Darrick J . Wong,
	linux-erofs, Theodore Ts'o, Andreas Dilger,
	Andreas Gruenbacher

On Thu, 9 Nov 2023 01:12:15 +0100 Andreas Grünbacher <andreas.gruenbacher@gmail.com> wrote:

> Andrew,
> 
> Andrew Morton <akpm@linux-foundation.org> schrieb am Do., 9. Nov. 2023, 00:06:
> > > +
> > > +     if (folio_test_highmem(folio)) {
> > > +             size_t max = PAGE_SIZE - offset_in_page(offset);
> > > +
> > > +             while (len > max) {
> >
> > Shouldn't this be `while (len)'?  AFAICT this code can fail to clear
> > the final page.
> 
> not sure what you're seeing there, but this looks fine to me.

I was right!  This code does fail to handle the final page.

: static inline void folio_fill_tail(struct folio *folio, size_t offset,
: 		const char *from, size_t len)
: {
: 	char *to = kmap_local_folio(folio, offset);
: 
: 	VM_BUG_ON(offset + len > folio_size(folio));
: 
: 	if (folio_test_highmem(folio)) {
: 		size_t max = PAGE_SIZE - offset_in_page(offset);
: 
: 		while (len > max) {
: 			memcpy(to, from, max);
: 			kunmap_local(to);
: 			len -= max;
: 			from += max;
: 			offset += max;
: 			max = PAGE_SIZE;
: 			to = kmap_local_folio(folio, offset);
: 		}
: 	}
: 
: 	memcpy(to, from, len);

This code down here handles it, doh.

: 	to = folio_zero_tail(folio, offset, to);
: 	kunmap_local(to);
: }

Implementation seems less straightforward than it might be?  Oh well.

Has it been runtime tested?

Anyway, let's please change the function argument ordering and remember
to cc linux-mm on v2?

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-08 23:06   ` Andrew Morton
  2023-11-09  0:12     ` Andreas Grünbacher
@ 2023-11-09 17:37     ` Matthew Wilcox
  1 sibling, 0 replies; 14+ messages in thread
From: Matthew Wilcox @ 2023-11-09 17:37 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-ext4, gfs2, linux-fsdevel, linux-xfs, Darrick J . Wong,
	linux-erofs, Theodore Ts'o, Andreas Dilger,
	Andreas Gruenbacher

On Wed, Nov 08, 2023 at 03:06:06PM -0800, Andrew Morton wrote:
> >  
> > +/**
> > + * folio_zero_tail - Zero the tail of a folio.
> > + * @folio: The folio to zero.
> > + * @kaddr: The address the folio is currently mapped to.
> > + * @offset: The byte offset in the folio to start zeroing at.
> 
> That's the argument ordering I would expect.
> 
> > + * If you have already used kmap_local_folio() to map a folio, written
> > + * some data to it and now need to zero the end of the folio (and flush
> > + * the dcache), you can use this function.  If you do not have the
> > + * folio kmapped (eg the folio has been partially populated by DMA),
> > + * use folio_zero_range() or folio_zero_segment() instead.
> > + *
> > + * Return: An address which can be passed to kunmap_local().
> > + */
> > +static inline __must_check void *folio_zero_tail(struct folio *folio,
> > +		size_t offset, void *kaddr)
> 
> While that is not.  addr,len is far more common that len,addr?

But that's not len!  That's offset-in-the-folio.  ie we're doing:

memset(folio_address(folio) + offset, 0, folio_size(folio) - offset);

If we were doing:

memset(folio_address(folio), 0, len);

then yes, your suggestion is the right order.

Indeed, having the arguments in the current order would hopefully make
filesystem authors realise that this _isn't_ "len".

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4
  2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
  2023-11-08 23:06   ` Andrew Morton
@ 2023-11-09 21:50   ` Andreas Gruenbacher
  1 sibling, 0 replies; 14+ messages in thread
From: Andreas Gruenbacher @ 2023-11-09 21:50 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: Andrew Morton, linux-ext4, gfs2, linux-fsdevel, linux-xfs,
	Darrick J . Wong, linux-erofs, Theodore Ts'o, Andreas Dilger

Hi Willy,

On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
> Instead of unmapping the folio after copying the data to it, then mapping
> it again to zero the tail, provide folio_zero_tail() to zero the tail
> of an already-mapped folio.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  fs/ext4/inline.c        |  3 +--
>  include/linux/highmem.h | 38 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 39 insertions(+), 2 deletions(-)
>
> diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
> index 9a84a5f9fef4..d5bd1e3a5d36 100644
> --- a/fs/ext4/inline.c
> +++ b/fs/ext4/inline.c
> @@ -502,9 +502,8 @@ static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
>         BUG_ON(len > PAGE_SIZE);
>         kaddr = kmap_local_folio(folio, 0);
>         ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
> -       flush_dcache_folio(folio);
> +       kaddr = folio_zero_tail(folio, len, kaddr + len);
>         kunmap_local(kaddr);
> -       folio_zero_segment(folio, len, folio_size(folio));
>         folio_mark_uptodate(folio);
>         brelse(iloc.bh);
>
> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
> index 4cacc0e43b51..1b81416196dd 100644
> --- a/include/linux/highmem.h
> +++ b/include/linux/highmem.h
> @@ -483,6 +483,44 @@ static inline void memcpy_to_folio(struct folio *folio, size_t offset,
>         flush_dcache_folio(folio);
>  }
>
> +/**
> + * folio_zero_tail - Zero the tail of a folio.
> + * @folio: The folio to zero.
> + * @kaddr: The address the folio is currently mapped to.
> + * @offset: The byte offset in the folio to start zeroing at.
> + *

As Andrew has pointed out, the order of the arguments in the
description doesn't match the order in the function definition. Other
than that, this patch looks good, so

Reviewed-by: Andreas Gruenbacher <agruenba@redhat.com>

> + * If you have already used kmap_local_folio() to map a folio, written
> + * some data to it and now need to zero the end of the folio (and flush
> + * the dcache), you can use this function.  If you do not have the
> + * folio kmapped (eg the folio has been partially populated by DMA),
> + * use folio_zero_range() or folio_zero_segment() instead.
> + *
> + * Return: An address which can be passed to kunmap_local().
> + */
> +static inline __must_check void *folio_zero_tail(struct folio *folio,
> +               size_t offset, void *kaddr)
> +{
> +       size_t len = folio_size(folio) - offset;
> +
> +       if (folio_test_highmem(folio)) {
> +               size_t max = PAGE_SIZE - offset_in_page(offset);
> +
> +               while (len > max) {
> +                       memset(kaddr, 0, max);
> +                       kunmap_local(kaddr);
> +                       len -= max;
> +                       offset += max;
> +                       max = PAGE_SIZE;
> +                       kaddr = kmap_local_folio(folio, offset);
> +               }
> +       }
> +
> +       memset(kaddr, 0, len);
> +       flush_dcache_folio(folio);
> +
> +       return kaddr;
> +}
> +
>  /**
>   * memcpy_from_file_folio - Copy some bytes from a file folio.
>   * @to: The destination buffer.
> --
> 2.42.0
>

Thanks,
Andreas


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap
  2023-11-07 21:26 ` [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap Matthew Wilcox (Oracle)
@ 2023-11-09 21:50   ` Andreas Gruenbacher
  2023-11-10 17:09     ` Matthew Wilcox
  0 siblings, 1 reply; 14+ messages in thread
From: Andreas Gruenbacher @ 2023-11-09 21:50 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: Andrew Morton, linux-ext4, gfs2, linux-fsdevel, linux-xfs,
	Darrick J . Wong, linux-erofs, Theodore Ts'o, Andreas Dilger

On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
> The iomap code was limited to PAGE_SIZE bytes; generalise it to cover
> an arbitrary-sized folio, and move it to be a common helper.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  fs/iomap/buffered-io.c  | 14 ++------------
>  include/linux/highmem.h | 38 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 40 insertions(+), 12 deletions(-)
>
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index f72df2babe56..093c4515b22a 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -305,28 +305,18 @@ static int iomap_read_inline_data(const struct iomap_iter *iter,
>  {
>         const struct iomap *iomap = iomap_iter_srcmap(iter);
>         size_t size = i_size_read(iter->inode) - iomap->offset;
> -       size_t poff = offset_in_page(iomap->offset);
>         size_t offset = offset_in_folio(folio, iomap->offset);
> -       void *addr;
>
>         if (folio_test_uptodate(folio))
>                 return 0;
>
> -       if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
> -               return -EIO;
> -       if (WARN_ON_ONCE(size > PAGE_SIZE -
> -                        offset_in_page(iomap->inline_data)))
> -               return -EIO;
>         if (WARN_ON_ONCE(size > iomap->length))
>                 return -EIO;
>         if (offset > 0)
>                 ifs_alloc(iter->inode, folio, iter->flags);
>
> -       addr = kmap_local_folio(folio, offset);
> -       memcpy(addr, iomap->inline_data, size);
> -       memset(addr + size, 0, PAGE_SIZE - poff - size);
> -       kunmap_local(addr);
> -       iomap_set_range_uptodate(folio, offset, PAGE_SIZE - poff);
> +       folio_fill_tail(folio, offset, iomap->inline_data, size);
> +       iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
>         return 0;
>  }
>
> diff --git a/include/linux/highmem.h b/include/linux/highmem.h
> index 1b81416196dd..0fbb60ffefc9 100644
> --- a/include/linux/highmem.h
> +++ b/include/linux/highmem.h
> @@ -521,6 +521,44 @@ static inline __must_check void *folio_zero_tail(struct folio *folio,
>         return kaddr;
>  }
>
> +/**
> + * folio_fill_tail - Copy some data to a folio and pad with zeroes.
> + * @folio: The destination folio.
> + * @offset: The offset into @folio at which to start copying.
> + * @from: The data to copy.
> + * @len: How many bytes of data to copy.
> + *
> + * This function is most useful for filesystems which support inline data.
> + * When they want to copy data from the inode into the page cache, this
> + * function does everything for them.  It supports large folios even on
> + * HIGHMEM configurations.
> + */
> +static inline void folio_fill_tail(struct folio *folio, size_t offset,
> +               const char *from, size_t len)
> +{
> +       char *to = kmap_local_folio(folio, offset);
> +
> +       VM_BUG_ON(offset + len > folio_size(folio));
> +
> +       if (folio_test_highmem(folio)) {
> +               size_t max = PAGE_SIZE - offset_in_page(offset);
> +
> +               while (len > max) {
> +                       memcpy(to, from, max);
> +                       kunmap_local(to);
> +                       len -= max;
> +                       from += max;
> +                       offset += max;
> +                       max = PAGE_SIZE;
> +                       to = kmap_local_folio(folio, offset);
> +               }
> +       }
> +
> +       memcpy(to, from, len);
> +       to = folio_zero_tail(folio, offset, to);

This needs to be:

to = folio_zero_tail(folio, offset  + len, to + len);

> +       kunmap_local(to);
> +}
> +
>  /**
>   * memcpy_from_file_folio - Copy some bytes from a file folio.
>   * @to: The destination buffer.
> --
> 2.42.0
>

Reviewed-by: Andreas Gruenbacher <agruenba@redhat.com>

Thanks,
Andreas


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio()
  2023-11-07 21:26 ` [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio() Matthew Wilcox (Oracle)
@ 2023-11-09 21:52   ` Andreas Gruenbacher
  0 siblings, 0 replies; 14+ messages in thread
From: Andreas Gruenbacher @ 2023-11-09 21:52 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: Andrew Morton, linux-ext4, gfs2, linux-fsdevel, linux-xfs,
	Darrick J . Wong, linux-erofs, Theodore Ts'o, Andreas Dilger

On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
> Use folio_fill_tail() to implement the unstuffing and folio_end_read()
> to simultaneously mark the folio uptodate and unlock it.  Unifies a
> couple of code paths.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  fs/gfs2/aops.c | 37 +++++++++++++++++--------------------
>  1 file changed, 17 insertions(+), 20 deletions(-)
>
> diff --git a/fs/gfs2/aops.c b/fs/gfs2/aops.c
> index 9611bfceda4b..ba8742dc91f8 100644
> --- a/fs/gfs2/aops.c
> +++ b/fs/gfs2/aops.c
> @@ -403,18 +403,18 @@ static int gfs2_jdata_writepages(struct address_space *mapping,
>  }
>
>  /**
> - * stuffed_readpage - Fill in a Linux folio with stuffed file data
> + * stuffed_read_folio - Fill in a Linux folio with stuffed file data
>   * @ip: the inode
>   * @folio: the folio
>   *
>   * Returns: errno
>   */
> -static int stuffed_readpage(struct gfs2_inode *ip, struct folio *folio)
> +static int stuffed_read_folio(struct gfs2_inode *ip, struct folio *folio)
>  {
> -       struct buffer_head *dibh;
> -       size_t i_size = i_size_read(&ip->i_inode);
> -       void *data;
> -       int error;
> +       struct buffer_head *dibh = NULL;
> +       size_t dsize = i_size_read(&ip->i_inode);
> +       void *from = NULL;
> +       int error = 0;
>
>         /*
>          * Due to the order of unstuffing files and ->fault(), we can be
> @@ -422,22 +422,20 @@ static int stuffed_readpage(struct gfs2_inode *ip, struct folio *folio)
>          * so we need to supply one here. It doesn't happen often.
>          */
>         if (unlikely(folio->index)) {
> -               folio_zero_range(folio, 0, folio_size(folio));
> -               folio_mark_uptodate(folio);
> -               return 0;
> +               dsize = 0;
> +       } else {
> +               error = gfs2_meta_inode_buffer(ip, &dibh);
> +               if (error)
> +                       goto out;
> +               from = dibh->b_data + sizeof(struct gfs2_dinode);
>         }
>
> -       error = gfs2_meta_inode_buffer(ip, &dibh);
> -       if (error)
> -               return error;
> -
> -       data = dibh->b_data + sizeof(struct gfs2_dinode);
> -       memcpy_to_folio(folio, 0, data, i_size);
> -       folio_zero_range(folio, i_size, folio_size(folio) - i_size);
> +       folio_fill_tail(folio, 0, from, dsize);
>         brelse(dibh);
> -       folio_mark_uptodate(folio);
> +out:
> +       folio_end_read(folio, error == 0);
>
> -       return 0;
> +       return error;
>  }
>
>  /**
> @@ -456,8 +454,7 @@ static int gfs2_read_folio(struct file *file, struct folio *folio)
>             (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
>                 error = iomap_read_folio(folio, &gfs2_iomap_ops);
>         } else if (gfs2_is_stuffed(ip)) {
> -               error = stuffed_readpage(ip, folio);
> -               folio_unlock(folio);
> +               error = stuffed_read_folio(ip, folio);
>         } else {
>                 error = mpage_read_folio(folio, gfs2_block_map);
>         }
> --
> 2.42.0
>

Reviewed-by: Andreas Gruenbacher <agruenba@redhat.com>

Thanks,
Andreas


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap
  2023-11-09 21:50   ` Andreas Gruenbacher
@ 2023-11-10 17:09     ` Matthew Wilcox
  2023-11-10 17:50       ` Andreas Grünbacher
  2023-11-11  4:02       ` Gao Xiang
  0 siblings, 2 replies; 14+ messages in thread
From: Matthew Wilcox @ 2023-11-10 17:09 UTC (permalink / raw)
  To: Andreas Gruenbacher
  Cc: Andrew Morton, linux-ext4, gfs2, linux-fsdevel, linux-xfs,
	Darrick J . Wong, linux-erofs, Theodore Ts'o, Andreas Dilger

On Thu, Nov 09, 2023 at 10:50:45PM +0100, Andreas Gruenbacher wrote:
> On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
> <willy@infradead.org> wrote:
> > +static inline void folio_fill_tail(struct folio *folio, size_t offset,
> > +               const char *from, size_t len)
> > +{
> > +       char *to = kmap_local_folio(folio, offset);
> > +
> > +       VM_BUG_ON(offset + len > folio_size(folio));
> > +
> > +       if (folio_test_highmem(folio)) {
> > +               size_t max = PAGE_SIZE - offset_in_page(offset);
> > +
> > +               while (len > max) {
> > +                       memcpy(to, from, max);
> > +                       kunmap_local(to);
> > +                       len -= max;
> > +                       from += max;
> > +                       offset += max;
> > +                       max = PAGE_SIZE;
> > +                       to = kmap_local_folio(folio, offset);
> > +               }
> > +       }
> > +
> > +       memcpy(to, from, len);
> > +       to = folio_zero_tail(folio, offset, to);
> 
> This needs to be:
> 
> to = folio_zero_tail(folio, offset  + len, to + len);

Oh, wow, that was stupid of me.  I only ran an xfstests against ext4,
which doesn't exercise this code, not gfs2 or erofs.  Thanks for
fixing this up.

I was wondering about adding the assertion:

	VM_BUG_ON((kaddr - offset) % PAGE_SIZE);

to catch the possible mistake of calling kmap_local_folio(folio, 0)
instead of kmap_local_folio(folio, offset).  But maybe that's
sufficiently unlikely a mistake to bother adding a runtime check for.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap
  2023-11-10 17:09     ` Matthew Wilcox
@ 2023-11-10 17:50       ` Andreas Grünbacher
  2023-11-11  4:02       ` Gao Xiang
  1 sibling, 0 replies; 14+ messages in thread
From: Andreas Grünbacher @ 2023-11-10 17:50 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Andreas Gruenbacher, Andrew Morton, linux-ext4, gfs2,
	linux-fsdevel, linux-xfs, Darrick J . Wong, linux-erofs,
	Theodore Ts'o, Andreas Dilger

Am Fr., 10. Nov. 2023 um 18:09 Uhr schrieb Matthew Wilcox <willy@infradead.org>:
> On Thu, Nov 09, 2023 at 10:50:45PM +0100, Andreas Gruenbacher wrote:
> > On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
> > <willy@infradead.org> wrote:
> > > +static inline void folio_fill_tail(struct folio *folio, size_t offset,
> > > +               const char *from, size_t len)
> > > +{
> > > +       char *to = kmap_local_folio(folio, offset);
> > > +
> > > +       VM_BUG_ON(offset + len > folio_size(folio));
> > > +
> > > +       if (folio_test_highmem(folio)) {
> > > +               size_t max = PAGE_SIZE - offset_in_page(offset);
> > > +
> > > +               while (len > max) {
> > > +                       memcpy(to, from, max);
> > > +                       kunmap_local(to);
> > > +                       len -= max;
> > > +                       from += max;
> > > +                       offset += max;
> > > +                       max = PAGE_SIZE;
> > > +                       to = kmap_local_folio(folio, offset);
> > > +               }
> > > +       }
> > > +
> > > +       memcpy(to, from, len);
> > > +       to = folio_zero_tail(folio, offset, to);
> >
> > This needs to be:
> >
> > to = folio_zero_tail(folio, offset  + len, to + len);
>
> Oh, wow, that was stupid of me.  I only ran an xfstests against ext4,
> which doesn't exercise this code, not gfs2 or erofs.  Thanks for
> fixing this up.
>
> I was wondering about adding the assertion:
>
>         VM_BUG_ON((kaddr - offset) % PAGE_SIZE);
>
> to catch the possible mistake of calling kmap_local_folio(folio, 0)
> instead of kmap_local_folio(folio, offset).  But maybe that's
> sufficiently unlikely a mistake to bother adding a runtime check for.

folio_zero_tail() is a bit of an obscure function, so I'm not sure if
there will be additional callers. The parameters are described as:

 * @offset: The byte offset in the folio to start zeroing at.
 * @kaddr: The address the folio is currently mapped to.

What about changing the @kaddr description to 'the (mapped) address
within the folio to start zeroing at' or similar?

Andreas

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap
  2023-11-10 17:09     ` Matthew Wilcox
  2023-11-10 17:50       ` Andreas Grünbacher
@ 2023-11-11  4:02       ` Gao Xiang
  1 sibling, 0 replies; 14+ messages in thread
From: Gao Xiang @ 2023-11-11  4:02 UTC (permalink / raw)
  To: Matthew Wilcox, Andreas Gruenbacher
  Cc: linux-xfs, Theodore Ts'o, Darrick J . Wong, gfs2,
	Andreas Dilger, linux-fsdevel, Andrew Morton, linux-ext4,
	linux-erofs



On 2023/11/11 01:09, Matthew Wilcox wrote:
> On Thu, Nov 09, 2023 at 10:50:45PM +0100, Andreas Gruenbacher wrote:
>> On Tue, Nov 7, 2023 at 10:27 PM Matthew Wilcox (Oracle)
>> <willy@infradead.org> wrote:
>>> +static inline void folio_fill_tail(struct folio *folio, size_t offset,
>>> +               const char *from, size_t len)
>>> +{
>>> +       char *to = kmap_local_folio(folio, offset);
>>> +
>>> +       VM_BUG_ON(offset + len > folio_size(folio));
>>> +
>>> +       if (folio_test_highmem(folio)) {
>>> +               size_t max = PAGE_SIZE - offset_in_page(offset);
>>> +
>>> +               while (len > max) {
>>> +                       memcpy(to, from, max);
>>> +                       kunmap_local(to);
>>> +                       len -= max;
>>> +                       from += max;
>>> +                       offset += max;
>>> +                       max = PAGE_SIZE;
>>> +                       to = kmap_local_folio(folio, offset);
>>> +               }
>>> +       }
>>> +
>>> +       memcpy(to, from, len);
>>> +       to = folio_zero_tail(folio, offset, to);
>>
>> This needs to be:
>>
>> to = folio_zero_tail(folio, offset  + len, to + len);
> 
> Oh, wow, that was stupid of me.  I only ran an xfstests against ext4,
> which doesn't exercise this code, not gfs2 or erofs.  Thanks for
> fixing this up.

Assuming that is for the next cycle (no rush), I will also test
this patch and feedback later since I'm now working on other
stuffs.

Thanks,
Gao Xiang

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-11-11  4:02 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-07 21:26 [PATCH 0/3] Add folio_zero_tail() and folio_fill_tail() Matthew Wilcox (Oracle)
2023-11-07 21:26 ` [PATCH 1/3] mm: Add folio_zero_tail() and use it in ext4 Matthew Wilcox (Oracle)
2023-11-08 23:06   ` Andrew Morton
2023-11-09  0:12     ` Andreas Grünbacher
2023-11-09 17:27       ` Andrew Morton
2023-11-09 17:37     ` Matthew Wilcox
2023-11-09 21:50   ` Andreas Gruenbacher
2023-11-07 21:26 ` [PATCH 2/3] mm: Add folio_fill_tail() and use it in iomap Matthew Wilcox (Oracle)
2023-11-09 21:50   ` Andreas Gruenbacher
2023-11-10 17:09     ` Matthew Wilcox
2023-11-10 17:50       ` Andreas Grünbacher
2023-11-11  4:02       ` Gao Xiang
2023-11-07 21:26 ` [PATCH 3/3] gfs2: Convert stuffed_readpage() to stuffed_read_folio() Matthew Wilcox (Oracle)
2023-11-09 21:52   ` Andreas Gruenbacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).