Message ID | 20231107212643.3490372-2-willy@infradead.org |
---|---|
State | Not Applicable |
Headers | show |
Series | Add folio_zero_tail() and folio_fill_tail() | expand |
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; > +} > +
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
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?
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".
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
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.
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(-)