btrfs: Convert kmaps to core page calls
Subsystems (6)
cover btrfs: Convert kmaps to core page calls
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use kmap_local_page() which is more efficient in those calls.
I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
There are a lot more kmap->kmap_local_page() conversions but kmap_local_page() requires some care with the unmapping order and so I'm still reviewing those changes because btrfs uses a lot of loops for it's kmaps.
Thanks,
Ira
Ira Weiny (4):
mm/highmem: Lift memcpy_[to|from]_page to core fs/btrfs: Use memcpy_[to|from]_page() fs/btrfs: Use copy_highpage() instead of 2 kmaps() fs/btrfs: Convert to zero_user()
fs/btrfs/compression.c | 11 +++------ fs/btrfs/extent_io.c | 22 ++++------------- fs/btrfs/inode.c | 33 ++++++++----------------- fs/btrfs/lzo.c | 4 ++-- fs/btrfs/raid56.c | 10 +------- fs/btrfs/reflink.c | 12 ++-------- fs/btrfs/send.c | 7 ++---- fs/btrfs/zlib.c | 10 +++----- fs/btrfs/zstd.c | 11 +++------ include/linux/highmem.h | 53 +++++++++++++++++++++++++++++++++++++++++ lib/iov_iter.c | 26 +++----------------- 11 files changed, 86 insertions(+), 113 deletions(-)
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
PATCH 1/4 mm/highmem: Lift memcpy_[to|from]_page to core
From: Ira Weiny <[email protected]>
Working through a conversion to a call kmap_local_page() instead of kmap() revealed many places where the pattern kmap/memcpy/kunmap occurred.
Eric Biggers, Matthew Wilcox, Christoph Hellwig, Dan Williams, and Al Viro all suggested putting this code into helper functions. Al Viro further pointed out that these functions already existed in the iov_iter code.[1]
Various locations for the lifted functions were considered.
Headers like mm.h or string.h seem ok but don't really portray the functionality well. pagemap.h made some sense but is for page cache functionality.[2]
Another alternative would be to create a new header for the promoted memcpy functions, but it masks the fact that these are designed to copy to/from pages using the kernel direct mappings and complicates matters with a new header.
Placing these functions in 'highmem.h' is suboptimal especially with the changes being proposed in the functionality of kmap. From a caller perspective including/using 'highmem.h' implies that the functions defined in that header are only required when highmem is in use which is increasingly not the case with modern processors. However, highmem.h is where all the current functions like this reside (zero_user(), clear_highpage(), clear_user_highpage(), copy_user_highpage(), and copy_highpage()). So it makes the most sense even though it is distasteful for some.[3]
Lift memcpy_to_page() and memcpy_from_page() to pagemap.h.
Remove memzero_page() in favor of zero_user() to zero a page.
Add a memcpy_page(), memmove_page, and memset_page() to cover more kmap/mem*/kunmap. patterns.
Add BUG_ON bounds checks to ensure the newly lifted page memory operations do not result in corrupted data in neighbor pages and to make them consistent with zero_user().[4]
Finally use kmap_local_page() in all the new calls.
[1] https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/#t
https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
---
Chagnes for V4:
Update commit message to say kmap_local_page() since kmap_thread() is no longer valid
Changes for V3:
From Matthew Wilcox
Move calls to highmem.h
Add BUG_ON()
Changes for V2:
From Thomas Gleixner
Change kmap_atomic() to kmap_local_page() after basing on tip/core/mm
From Joonas Lahtinen
Reverse offset/val in memset_page()
--- include/linux/highmem.h | 53 +++++++++++++++++++++++++++++++++++++++++ lib/iov_iter.c | 26 +++----------------- 2 files changed, 56 insertions(+), 23 deletions(-)
include/linux/highmem.h +53 -0
@@ -276,4 +276,57 @@ static inline void copy_highpage(struct page *to, struct page *from)#endif +static inline void memcpy_page (struct page *dst_page,size_t dst_off, +struct page *src_page,size_t src_off, +size_t len) +{ +char *dst =kmap_local_page (dst_page); +char *src =kmap_local_page (src_page); + +BUG_ON (dst_off + len >PAGE_SIZE || src_off + len >PAGE_SIZE ); +memcpy (dst + dst_off, src + src_off, len); +kunmap_local (src); +kunmap_local (dst); +} + +static inline void memmove_page (struct page *dst_page,size_t dst_off, +struct page *src_page,size_t src_off, +size_t len) +{ +char *dst =kmap_local_page (dst_page); +char *src =kmap_local_page (src_page); + +BUG_ON (dst_off + len >PAGE_SIZE || src_off + len >PAGE_SIZE ); +memmove (dst + dst_off, src + src_off, len); +kunmap_local (src); +kunmap_local (dst); +} + +static inline void memcpy_from_page (char *to,struct page *page,size_t offset,size_t len) +{ +char *from =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to, from + offset, len); +kunmap_local (from); +} + +static inline void memcpy_to_page (struct page *page,size_t offset,const char *from,size_t len) +{ +char *to =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to + offset, from, len); +kunmap_local (to); +} + +static inline void memset_page (struct page *page,size_t offset,int val,size_t len) +{ +char *addr =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memset (addr + offset, val, len); +kunmap_local (addr); +} +#endif /* _LINUX_HIGHMEM_H */
lib/iov_iter.c +3 -23
@@ -5,6 +5,7 @@#include <linux/fault-inject-usercopy.h> #include <linux/uio.h> #include <linux/pagemap.h> +#include <linux/highmem.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/splice.h>
@@ -466,27 +467,6 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction, }EXPORT_SYMBOL (iov_iter_init); -static void memcpy_from_page (char *to,struct page *page,size_t offset,size_t len) -{ -char *from =kmap_atomic (page); -memcpy (to, from + offset, len); -kunmap_atomic (from); -} - -static void memcpy_to_page (struct page *page,size_t offset,const char *from,size_t len) -{ -char *to =kmap_atomic (page); -memcpy (to + offset, from, len); -kunmap_atomic (to); -} - -static void memzero_page (struct page *page,size_t offset,size_t len) -{ -char *addr =kmap_atomic (page); -memset (addr + offset,0 , len); -kunmap_atomic (addr); -} -static inline bool allocated (struct pipe_buffer *buf) {return buf->ops == &default_pipe_buf_ops;
@@ -964,7 +944,7 @@ static size_t pipe_zero(size_t bytes, struct iov_iter *i) do { size_t chunk = min_t(size_t, n, PAGE_SIZE - off); - memzero_page(pipe->bufs[i_head & p_mask].page, off, chunk); + zero_user(pipe->bufs[i_head & p_mask].page, off, chunk); i->head = i_head; i->iov_offset = off + chunk; n -= chunk;
@@ -981,7 +961,7 @@ size_t iov_iter_zero(size_t bytes, struct iov_iter *i)return pipe_zero (bytes, i);iterate_and_advance (i, bytes, v,clear_user (v.iov_base, v.iov_len), -memzero_page (v.bv_page, v.bv_offset, v.bv_len), +zero_user (v.bv_page, v.bv_offset, v.bv_len),memset (v.iov_base,0 , v.iov_len) )
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
Re: [PATCH 1/4] mm/highmem: Lift memcpy_[to|from]_page to core
Quoted 4 lines · On 2/5/21 18:35, [email protected] wrote:
include/linux/highmem.h +4
-0+static inline void memmove_page (struct page *dst_page,size_t dst_off, +struct page *src_page,size_t src_off, +size_t len) +{
include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 305
Quoted 10 lines
include/linux/highmem.h +10
-0+char *dst =kmap_local_page (dst_page); +char *src =kmap_local_page (src_page); + +BUG_ON (dst_off + len >PAGE_SIZE || src_off + len >PAGE_SIZE ); +memmove (dst + dst_off, src + src_off, len); +kunmap_local (src); +kunmap_local (dst); +} + +static inline void memcpy_from_page (char *to,struct page *page,size_t offset,size_t len)
How about following ?
static inline void memcpy_from_page(char *to, struct page *page, size_t offset,
size_t len)
include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 314
Quoted 9 lines
include/linux/highmem.h +9
-0+{ +char *from =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to, from + offset, len); +kunmap_local (from); +} + +static inline void memcpy_to_page (struct page *page,size_t offset,const char *from,size_t len)
How about following ?
static inline void memcpy_to_page(struct page *page, size_t offset,
const char *from, size_t len)
include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 323
Quoted 9 lines
include/linux/highmem.h +9
-0+{ +char *to =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to + offset, from, len); +kunmap_local (to); +} + +static inline void memset_page (struct page *page,size_t offset,int val,size_t len)
How about following ?
static inline void memset_page(struct page *page, size_t offset, int val,
size_t len)
Quoted 8 lines
include/linux/highmem.h +8
-0+{ +char *addr =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memset (addr + offset, val, len); +kunmap_local (addr); +} +
Re: [PATCH 1/4] mm/highmem: Lift memcpy_[to|from]_page to core
Quoted 11 lines · Chaitanya Kulkarni
Quoted 10 lines · On 2/5/21 18:35, [email protected] wrote:
include/linux/highmem.h +10
-0+static inline void memmove_page (struct page *dst_page,size_t dst_off, +struct page *src_page,size_t src_off, +size_t len) +{ +char *dst =kmap_local_page (dst_page); +char *src =kmap_local_page (src_page); + +BUG_ON (dst_off + len >PAGE_SIZE || src_off + len >PAGE_SIZE ); +memmove (dst + dst_off, src + src_off, len); +kunmap_local (src);
Quoted 8 lines
include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 305
Quoted 4 lines
include/linux/highmem.h +4
-0+kunmap_local (dst); +} + +static inline void memcpy_from_page (char *to,struct page *page,size_t offset,size_t len)How about following ?
static inline void memcpy_from_page(char *to, struct page *page,size_toffset,
size_tlen)
It is an easy change and It is up to Andrew. But I thought we were making the line length limit longer now.
Ira
Quoted 32 lines
include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 314
Quoted 9 lines
include/linux/highmem.h +9
-0+{ +char *from =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to, from + offset, len); +kunmap_local (from); +} + +static inline void memcpy_to_page (struct page *page,size_t offset,const char *from,size_t len)How about following ?
static inline void memcpy_to_page(struct page *page,size_toffset,
const char *from,size_tlen)include/linux/highmem.h static inline void copy_highpage(struct page *to, struct page *from) · line 323
Quoted 9 lines
include/linux/highmem.h +9
-0+{ +char *to =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memcpy (to + offset, from, len); +kunmap_local (to); +} + +static inline void memset_page (struct page *page,size_t offset,int val,size_t len)How about following ?
static inline void memset_page(struct page *page,size_toffset, int val,
size_tlen)Quoted 8 lines
include/linux/highmem.h +8
-0+{ +char *addr =kmap_local_page (page); + +BUG_ON (offset + len >PAGE_SIZE ); +memset (addr + offset, val, len); +kunmap_local (addr); +} +
Re: [PATCH 1/4] mm/highmem: Lift memcpy_[to|from]_page to core
Quoted 10 lines · Ira Weiny
Quoted 5 lines
Quoted 1 line
+static inline void
memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)How about following ?
static inline void memcpy_from_page(char *to, struct page *page,size_toffset,
size_tlen)It is an easy change and It is up to Andrew. But I thought we were making the line length limit longer now.
Ira
True, not sure what is the right thing going forward especially when new changes
are mixed with the old ones, I'll leave it to the maintainer to decide.
PATCH 2/4 fs/btrfs: Use memcpy_[to|from]_page()
In reply to [email protected]From: Ira Weiny <[email protected]>
Development of this patch was aided by the coccinelle script:
// <smpl>
// SPDX-License-Identifier: GPL-2.0-only
// Find kmap/memcpy/kunmap pattern and replace with memcpy*page calls //
// NOTE: Offsets and other expressions may be more complex than what the script // will automatically generate. Therefore a catchall rule is provided to find // the pattern which then must be evaluated by hand.
//
// Confidence: Low
// Copyright: (C) 2021 Intel Corporation
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options:
//
// simple memcpy version
//
@ memcpy_rule1 @
expression page, T, F, B, Off;
identifier ptr;
type VP;
@@
(
-VP ptr = kmap(page);
|
-ptr = kmap(page);
|
-VP ptr = kmap_atomic(page);
|
-ptr = kmap_atomic(page);
)
<+...
(
-memcpy(ptr + Off, F, B);
+memcpy_to_page(page, Off, F, B);
|
-memcpy(ptr, F, B);
+memcpy_to_page(page, 0, F, B);
|
-memcpy(T, ptr + Off, B);
+memcpy_from_page(T, page, Off, B);
|
-memcpy(T, ptr, B);
+memcpy_from_page(T, page, 0, B);
)
...+>
(
-kunmap(page);
|
-kunmap_atomic(ptr);
)
// Remove any pointers left unused
@
depends on memcpy_rule1
@
identifier memcpy_rule1.ptr;
type VP, VP1;
@@
-VP ptr; ... when != ptr;
? VP1 ptr;
//
// Some callers kmap without a temp pointer
//
@ memcpy_rule2 @
expression page, T, Off, F, B;
@@
<+...
(
-memcpy(kmap(page) + Off, F, B);
+memcpy_to_page(page, Off, F, B);
|
-memcpy(kmap(page), F, B);
+memcpy_to_page(page, 0, F, B);
|
-memcpy(T, kmap(page) + Off, B);
+memcpy_from_page(T, page, Off, B);
|
-memcpy(T, kmap(page), B);
+memcpy_from_page(T, page, 0, B);
)
...+>
-kunmap(page);
// No need for the ptr variable removal
//
// Catch all
//
@ memcpy_rule3 @
expression page;
expression GenTo, GenFrom, GenSize;
identifier ptr;
type VP;
@@
(
-VP ptr = kmap(page);
|
-ptr = kmap(page);
|
-VP ptr = kmap_atomic(page);
|
-ptr = kmap_atomic(page);
)
<+...
(
//
// Some call sites have complex expressions within the memcpy // match a catch all to be evaluated by hand.
//
-memcpy(GenTo, GenFrom, GenSize); +memcpy_to_pageExtra(page, GenTo, GenFrom, GenSize); +memcpy_from_pageExtra(GenTo, page, GenFrom, GenSize); )
...+>
(
-kunmap(page);
|
-kunmap_atomic(ptr);
)
// Remove any pointers left unused
@
depends on memcpy_rule3
@
identifier memcpy_rule3.ptr;
type VP, VP1;
@@
-VP ptr; ... when != ptr;
? VP1 ptr;
// <smpl>
squash: memcpy
--- fs/btrfs/compression.c | 6 ++---- fs/btrfs/lzo.c | 4 ++-- fs/btrfs/reflink.c | 6 +----- fs/btrfs/send.c | 7 ++----- fs/btrfs/zlib.c | 5 ++--- fs/btrfs/zstd.c | 6 ++---- 6 files changed, 11 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +2 -4
@@ -1231,7 +1231,6 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,unsigned long prev_start_byte;unsigned long working_bytes = total_out - buf_start;unsigned long bytes; -char *kaddr;struct bio_vec bvec =bio_iter_iovec (bio, bio->bi_iter);/*
@@ -1262,9 +1261,8 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start, PAGE_SIZE - (buf_offset % PAGE_SIZE)); bytes =min (bytes, working_bytes); - kaddr =kmap_atomic (bvec.bv_page); -memcpy (kaddr + bvec.bv_offset, buf + buf_offset, bytes); -kunmap_atomic (kaddr); +memcpy_to_page (bvec.bv_page, bvec.bv_offset, buf + buf_offset, + bytes);flush_dcache_page (bvec.bv_page); buf_offset += bytes;
fs/btrfs/lzo.c +2 -2
@@ -467,7 +467,7 @@ int lzo_decompress(struct list_head *ws, unsigned char *data_in, destlen = min_t(unsigned long, destlen, PAGE_SIZE); bytes = min_t(unsigned long, destlen, out_len - start_byte); - kaddr =kmap_atomic (dest_page); + kaddr =kmap_local_page (dest_page);memcpy (kaddr, workspace->buf + start_byte, bytes);/*
@@ -477,7 +477,7 @@ int lzo_decompress(struct list_head *ws, unsigned char *data_in,*/ if (bytes < destlen)memset (kaddr+bytes,0 , destlen-bytes); -kunmap_atomic (kaddr); +kunmap_local (kaddr); out:return ret; }
fs/btrfs/reflink.c +1 -5
@@ -103,12 +103,8 @@ static int copy_inline_to_page(struct btrfs_inode *inode,set_bit (BTRFS_INODE_NO_DELALLOC_FLUSH , &inode->runtime_flags);if (comp_type ==BTRFS_COMPRESS_NONE ) { -char *map; - - map =kmap (page); -memcpy (map, data_start, datal); +memcpy_to_page (page,0 , data_start, datal);flush_dcache_page (page); -kunmap (page); }else { ret =btrfs_decompress (comp_type, data_start, page,0 , inline_size, datal);
fs/btrfs/send.c +2 -5
@@ -4948,7 +4948,6 @@ static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)struct btrfs_fs_info *fs_info = root->fs_info;struct inode *inode;struct page *page; -char *addr;pgoff_t index = offset >>PAGE_SHIFT ;pgoff_t last_index;unsigned pg_offset =offset_in_page (offset);
@@ -5001,10 +5000,8 @@ static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) } } - addr =kmap (page); -memcpy (sctx->send_buf + sctx->send_size, addr + pg_offset, - cur_len); -kunmap (page); +memcpy_from_page (sctx->send_buf + sctx->send_size, page, + pg_offset, cur_len);unlock_page (page);put_page (page); index++;
fs/btrfs/zlib.c +2 -3
@@ -432,9 +432,8 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in, PAGE_SIZE - (buf_offset % PAGE_SIZE)); bytes =min (bytes, bytes_left); - kaddr =kmap_atomic (dest_page); -memcpy (kaddr + pg_offset, workspace->buf + buf_offset, bytes); -kunmap_atomic (kaddr); +memcpy_to_page (dest_page, pg_offset, + workspace->buf + buf_offset, bytes); pg_offset += bytes; bytes_left -= bytes;
fs/btrfs/zstd.c +2 -4
@@ -688,10 +688,8 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in, bytes = min_t(unsigned long, destlen - pg_offset, workspace->out_buf.size - buf_offset); - kaddr =kmap_atomic (dest_page); -memcpy (kaddr + pg_offset, workspace->out_buf.dst + buf_offset, - bytes); -kunmap_atomic (kaddr); +memcpy_to_page (dest_page, pg_offset, + workspace->out_buf.dst + buf_offset, bytes); pg_offset += bytes; }
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
Re: [PATCH 2/4] fs/btrfs: Use memcpy_[to|from]_page()
Quoted 1 line · On Fri, Feb 05, 2021 at 03:23:02PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There should be a short explanation what the patch does, eg. "use helper for open coded kmap_atomic/memcpy/kunmap_atomic", although I see there are conversions kmap_atomic -> kmap_local not in the coccinelle script, probably done manually. This should be mentioned too.
Also please use "btrfs:" followed by lowercase in the subject.
PATCH 3/4 fs/btrfs: Use copy_highpage() instead of 2 kmaps()
In reply to [email protected]From: Ira Weiny <[email protected]>
Development of this patch was aided by the following coccinelle script:
// <smpl>
// SPDX-License-Identifier: GPL-2.0-only
// Find kmap/copypage/kunmap pattern and replace with copy_highpage calls //
// NOTE: The expressions in the copy page version of this kmap pattern are // overly complex and so these all need individual attention. //
// Confidence: Low
// Copyright: (C) 2021 Intel Corporation
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options:
//
// Then a copy_page where we have 2 pages involved. //
@ copy_page_rule @
expression page, page2, To, From, Size; identifier ptr, ptr2;
type VP, VP2;
@@
/* kmap */
(
-VP ptr = kmap(page);
...
-VP2 ptr2 = kmap(page2);
|
-VP ptr = kmap_atomic(page);
...
-VP2 ptr2 = kmap_atomic(page2);
|
-ptr = kmap(page);
...
-ptr2 = kmap(page2);
|
-ptr = kmap_atomic(page);
...
-ptr2 = kmap_atomic(page2);
)
// 1 or more copy versions of the entire page
<+...
(
-copy_page(To, From);
+copy_highpage(To, From);
|
-memmove(To, From, Size);
+memmoveExtra(To, From, Size);
)
...+>
/* kunmap */
(
-kunmap(page2);
...
-kunmap(page);
|
-kunmap(page);
...
-kunmap(page2);
|
-kmap_atomic(ptr2);
...
-kmap_atomic(ptr);
)
// Remove any pointers left unused
@
depends on copy_page_rule
@
identifier copy_page_rule.ptr;
identifier copy_page_rule.ptr2;
type VP, VP1;
type VP2, VP21;
@@
-VP ptr; ... when != ptr; ? VP1 ptr; -VP2 ptr2; ... when != ptr2;
? VP21 ptr2;
// </smpl>
--- fs/btrfs/raid56.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
fs/btrfs/raid56.c +1 -9
@@ -250,8 +250,6 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)static void cache_rbio_pages (struct btrfs_raid_bio *rbio) {int i; -char *s; -char *d;int ret; ret =alloc_rbio_pages (rbio);
@@ -262,13 +260,7 @@ static void cache_rbio_pages(struct btrfs_raid_bio *rbio)if (!rbio->bio_pages[i])continue ; - s =kmap (rbio->bio_pages[i]); - d =kmap (rbio->stripe_pages[i]); - -copy_page (d, s); - -kunmap (rbio->bio_pages[i]); -kunmap (rbio->stripe_pages[i]); +copy_highpage (rbio->stripe_pages[i], rbio->bio_pages[i]);SetPageUptodate (rbio->stripe_pages[i]); }set_bit (RBIO_CACHE_READY_BIT , &rbio->flags);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
PATCH 4/4 fs/btrfs: Convert to zero_user()
In reply to [email protected]From: Ira Weiny <[email protected]>
The development of this patch was aided by the following coccinelle script:
// <smpl>
// SPDX-License-Identifier: GPL-2.0-only
// Find kmap/memset/kunmap pattern and replace with memset*page calls //
// NOTE: Offsets and other expressions may be more complex than what the script // will automatically generate. Therefore a catchall rule is provided to find // the pattern which then must be evaluated by hand.
//
// Confidence: Low
// Copyright: (C) 2021 Intel Corporation
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options:
//
// Then the memset pattern
//
@ memset_rule1 @
expression page, V, L, Off;
identifier ptr;
type VP;
@@
(
-VP ptr = kmap(page);
|
-ptr = kmap(page);
|
-VP ptr = kmap_atomic(page);
|
-ptr = kmap_atomic(page);
)
<+...
(
-memset(ptr, 0, L);
+zero_user(page, 0, L);
|
-memset(ptr + Off, 0, L);
+zero_user(page, Off, L);
|
-memset(ptr, V, L);
+memset_page(page, V, 0, L);
|
-memset(ptr + Off, V, L);
+memset_page(page, V, Off, L);
)
...+>
(
-kunmap(page);
|
-kunmap_atomic(ptr);
)
// Remove any pointers left unused
@
depends on memset_rule1
@
identifier memset_rule1.ptr;
type VP, VP1;
@@
-VP ptr; ... when != ptr;
? VP1 ptr;
//
// Catch all
//
@ memset_rule2 @
expression page;
identifier ptr;
expression GenTo, GenSize, GenValue;
type VP;
@@
(
-VP ptr = kmap(page);
|
-ptr = kmap(page);
|
-VP ptr = kmap_atomic(page);
|
-ptr = kmap_atomic(page);
)
<+...
(
//
// Some call sites have complex expressions within the memset/memcpy // The follow are catch alls which need to be evaluated by hand. //
-memset(GenTo, 0, GenSize);
+zero_userExtra(page, GenTo, GenSize);
|
-memset(GenTo, GenValue, GenSize); +memset_pageExtra(page, GenValue, GenTo, GenSize);
)
...+>
(
-kunmap(page);
|
-kunmap_atomic(ptr);
)
// Remove any pointers left unused
@
depends on memset_rule2
@
identifier memset_rule2.ptr;
type VP, VP1;
@@
-VP ptr; ... when != ptr;
? VP1 ptr;
// </smpl>
--- fs/btrfs/compression.c | 5 +---- fs/btrfs/extent_io.c | 22 ++++------------------ fs/btrfs/inode.c | 33 ++++++++++----------------------- fs/btrfs/reflink.c | 6 +----- fs/btrfs/zlib.c | 5 +---- fs/btrfs/zstd.c | 5 +---- 6 files changed, 18 insertions(+), 58 deletions(-)
fs/btrfs/compression.c +1 -4
@@ -567,16 +567,13 @@ static noinline int add_ra_bio_pages(struct inode *inode,free_extent_map (em);if (page->index == end_index) { -char *userpage;size_t zero_offset =offset_in_page (isize);if (zero_offset) {int zeros; zeros =PAGE_SIZE - zero_offset; - userpage =kmap_atomic (page); -memset (userpage + zero_offset,0 , zeros); +zero_user (page, zero_offset, zeros);flush_dcache_page (page); -kunmap_atomic (userpage); } }
fs/btrfs/extent_io.c +4 -18
@@ -3229,15 +3229,12 @@ int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, }if (page->index == last_byte >>PAGE_SHIFT ) { -char *userpage;size_t zero_offset =offset_in_page (last_byte);if (zero_offset) { iosize =PAGE_SIZE - zero_offset; - userpage =kmap_atomic (page); -memset (userpage + zero_offset,0 , iosize); +zero_user (page, zero_offset, iosize);flush_dcache_page (page); -kunmap_atomic (userpage); } }while (cur <= end) {
@@ -3245,14 +3242,11 @@ int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,u64 offset;if (cur >= last_byte) { -char *userpage;struct extent_state *cached =NULL ; iosize =PAGE_SIZE - pg_offset; - userpage =kmap_atomic (page); -memset (userpage + pg_offset,0 , iosize); +zero_user (page, pg_offset, iosize);flush_dcache_page (page); -kunmap_atomic (userpage);set_extent_uptodate (tree, cur, cur + iosize -1 , &cached,GFP_NOFS ); unlock_extent_cached(tree, cur,
@@ -3334,13 +3328,10 @@ int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,/* we've found a hole, just zero and go on */ if (block_start ==EXTENT_MAP_HOLE ) { -char *userpage;struct extent_state *cached =NULL ; - userpage =kmap_atomic (page); -memset (userpage + pg_offset,0 , iosize); +zero_user (page, pg_offset, iosize);flush_dcache_page (page); -kunmap_atomic (userpage);set_extent_uptodate (tree, cur, cur + iosize -1 , &cached,GFP_NOFS );
@@ -3654,12 +3645,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, }if (page->index == end_index) { -char *userpage; - - userpage =kmap_atomic (page); -memset (userpage + pg_offset,0 , -PAGE_SIZE - pg_offset); -kunmap_atomic (userpage); +zero_user (page, pg_offset,PAGE_SIZE - pg_offset);flush_dcache_page (page); }
fs/btrfs/inode.c +10 -23
@@ -640,17 +640,12 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)if (!ret) {unsigned long offset =offset_in_page (total_compressed);struct page *page = pages[nr_pages -1 ]; -char *kaddr;/* zero the tail end of the last page, we might be * sending it down to disk */ -if (offset) { - kaddr =kmap_atomic (page); -memset (kaddr + offset,0 , -PAGE_SIZE - offset); -kunmap_atomic (kaddr); - } +if (offset) +zero_user (page, offset,PAGE_SIZE - offset); will_compress =1 ; } }
@@ -4675,7 +4670,6 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,struct btrfs_ordered_extent *ordered;struct extent_state *cached_state =NULL ;struct extent_changeset *data_reserved =NULL ; -char *kaddr;bool only_release_metadata = false;u32 blocksize = fs_info->sectorsize;pgoff_t index = from >>PAGE_SHIFT ;
@@ -4765,15 +4759,13 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,if (offset != blocksize) {if (!len) len = blocksize - offset; - kaddr =kmap (page);if (front) -memset (kaddr + (block_start -page_offset (page)), -0 , offset); +zero_user (page, (block_start -page_offset (page)), + offset);else -memset (kaddr + (block_start -page_offset (page)) + offset, -0 , len); +zero_user (page, (block_start -page_offset (page)) + offset, + len);flush_dcache_page (page); -kunmap (page); }ClearPageChecked (page);set_page_dirty (page);
@@ -6660,11 +6652,9 @@ static noinline int uncompress_inline(struct btrfs_path *path,* cover that region here. */ -if (max_size + pg_offset <PAGE_SIZE ) { -char *map =kmap (page); -memset (map + pg_offset + max_size,0 ,PAGE_SIZE - max_size - pg_offset); -kunmap (page); - } +if (max_size + pg_offset <PAGE_SIZE ) +zero_user (page, pg_offset + max_size, +PAGE_SIZE - max_size - pg_offset);kfree (tmp);return ret; }
@@ -8303,7 +8293,6 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)struct btrfs_ordered_extent *ordered;struct extent_state *cached_state =NULL ;struct extent_changeset *data_reserved =NULL ; -char *kaddr;unsigned long zero_start;loff_t size;vm_fault_t ret;
@@ -8410,10 +8399,8 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) zero_start =PAGE_SIZE ;if (zero_start !=PAGE_SIZE ) { - kaddr =kmap (page); -memset (kaddr + zero_start,0 ,PAGE_SIZE - zero_start); +zero_user (page, zero_start,PAGE_SIZE - zero_start);flush_dcache_page (page); -kunmap (page); }ClearPageChecked (page);set_page_dirty (page);
fs/btrfs/reflink.c +1 -5
@@ -126,12 +126,8 @@ static int copy_inline_to_page(struct btrfs_inode *inode,* So what's in the range [500, 4095] corresponds to zeroes. */ if (datal < block_size) { -char *map; - - map =kmap (page); -memset (map + datal,0 , block_size - datal); +zero_user (page, datal, block_size - datal);flush_dcache_page (page); -kunmap (page); }SetPageUptodate (page);
fs/btrfs/zlib.c +1 -4
@@ -375,7 +375,6 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,unsigned long bytes_left;unsigned long total_out =0 ;unsigned long pg_offset =0 ; -char *kaddr; destlen = min_t(unsigned long, destlen, PAGE_SIZE); bytes_left = destlen;
@@ -455,9 +454,7 @@ int zlib_decompress(struct list_head *ws, unsigned char *data_in,* end of the inline extent (destlen) to the end of the page */ if (pg_offset < destlen) { - kaddr =kmap_atomic (dest_page); -memset (kaddr + pg_offset,0 , destlen - pg_offset); -kunmap_atomic (kaddr); +zero_user (dest_page, pg_offset, destlen - pg_offset); }return ret; }
fs/btrfs/zstd.c +1 -4
@@ -631,7 +631,6 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in,size_t ret2;unsigned long total_out =0 ;unsigned long pg_offset =0 ; -char *kaddr; stream =ZSTD_initDStream (ZSTD_BTRFS_MAX_INPUT , workspace->mem, workspace->size);
@@ -696,9 +695,7 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in, ret =0 ; finish:if (pg_offset < destlen) { - kaddr =kmap_atomic (dest_page); -memset (kaddr + pg_offset,0 , destlen - pg_offset); -kunmap_atomic (kaddr); +zero_user (dest_page, pg_offset, destlen - pg_offset); }return ret; }
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
Quoted 10 lines · On Fri, Feb 05, 2021 at 03:23:00PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use
kmap_local_page()which is more efficient in those calls.I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
I'd rather take the non-mm patches through my tree so it gets tested the same way as other btrfs changes, straightforward cleanups or not.
This brings the question how to do that as the first patch should go through the MM tree. One option is to posptpone the actual cleanups after the 1st patch is merged but this could take a long delay.
I'd suggest to take the 1st patch within MM tree in the upcoming merge window and then I can prepare a separate pull with just the cleanups. Removing an inter-tree patch dependency was a sufficient reason for Linus in the past for such pull requests.
Quoted 3 lines
There are a lot more
kmap->kmap_local_page()conversions butkmap_local_page()requires some care with the unmapping order and so I'm still reviewing those changes because btrfs uses a lot of loops for it's kmaps.
It sounds to me that converting the kmaps will take some time anyway so exporting the helpers first and then converting the subsystems might be a good option. In case you'd like to get rid of the simple cases in btrfs code now we can do the 2 pull requests.
Quoted 8 lines · David Sterba
Quoted 7 lines · On Fri, Feb 05, 2021 at 03:23:00PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use
kmap_local_page()which is more efficient in those calls.
Quoted 6 lines
Quoted 3 lines
I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
I'd rather take the non-mm patches through my tree so it gets tested the same way as other btrfs changes, straightforward cleanups or not.
True.
Quoted 9 lines
This brings the question how to do that as the first patch should go through the MM tree. One option is to posptpone the actual cleanups after the 1st patch is merged but this could take a long delay.
I'd suggest to take the 1st patch within MM tree in the upcoming merge window and then I can prepare a separate pull with just the cleanups. Removing an inter-tree patch dependency was a sufficient reason for Linus in the past for such pull requests.
There are others how want this base patch too.[1] So I like this option.
Quoted 9 lines
Quoted 4 lines
There are a lot more
kmap->kmap_local_page()conversions butkmap_local_page()requires some care with the unmapping order and so I'm still reviewing those changes because btrfs uses a lot of loops for it's kmaps.It sounds to me that converting the kmaps will take some time anyway so exporting the helpers first and then converting the subsystems might be a good option. In case you'd like to get rid of the simple cases in btrfs code now we can do the 2 pull requests.
I would really like to get the simple case out of the way because the next series has more difficult changes and the simple cases always cause me trouble when grepping/coccinelle'ing for things.
So I would like a follow on pull request if possible. But I'm willing to do what works best for you.
For now I will spin a new version with the changes you've requested ASAP.
Ira
[1] https://lore.kernel.org/linux-f2fs-devel/[email protected]/
Quoted 15 lines · On Tue, 9 Feb 2021 16:11:23 +0100 David Sterba <[email protected]> wrote:
Quoted 10 lines · On Fri, Feb 05, 2021 at 03:23:00PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use
kmap_local_page()which is more efficient in those calls.I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
I'd rather take the non-mm patches through my tree so it gets tested the same way as other btrfs changes, straightforward cleanups or not.
Quoted 8 lines
This brings the question how to do that as the first patch should go through the MM tree. One option is to posptpone the actual cleanups after the 1st patch is merged but this could take a long delay.
I'd suggest to take the 1st patch within MM tree in the upcoming merge window and then I can prepare a separate pull with just the cleanups. Removing an inter-tree patch dependency was a sufficient reason for Linus in the past for such pull requests.
It would be best to merge [1/4] via the btrfs tree. Please add my
Although I think it would be better if [1/4] merely did the code movement. Adding those BUG_ON()s is a semantic/functional change and really shouldn't be bound up with the other things this patch series does. This logically separate change raises questions such as
- What is the impact on overall code size? Not huge, presumably, but every little bit hurts.
- Additional runtime costs of those extra comparisons?
- These impacts could be lessened by using VM_BUG_ON() rather than BUG_ON() - should we do this?
- Linus reeeeeeeally doesn't like new BUG_ON()s. Maybe you can sneak it past him ;)
See what I mean? I do think it would be best to take those assertions out of the patch and to propose them separately, at a later time.
Quoted 28 lines · Andrew Morton
Quoted 15 lines · On Tue, 9 Feb 2021 16:11:23 +0100 David Sterba <[email protected]> wrote:
Quoted 10 lines · On Fri, Feb 05, 2021 at 03:23:00PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use
kmap_local_page()which is more efficient in those calls.I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
I'd rather take the non-mm patches through my tree so it gets tested the same way as other btrfs changes, straightforward cleanups or not.
Quoted 8 lines
This brings the question how to do that as the first patch should go through the MM tree. One option is to posptpone the actual cleanups after the 1st patch is merged but this could take a long delay.
I'd suggest to take the 1st patch within MM tree in the upcoming merge window and then I can prepare a separate pull with just the cleanups. Removing an inter-tree patch dependency was a sufficient reason for Linus in the past for such pull requests.
It would be best to merge [1/4] via the btrfs tree. Please add my
Quoted 7 lines
Acked-by Andrew Morton <[email protected]>Although I think it would be better if [1/4] merely did the code movement. Adding those BUG_ON()s is a semantic/functional change and really shouldn't be bound up with the other things this patch series does.
I proposed this too and was told 'no'...
<quote>
If we put in into a separate patch, someone will suggest backing out the patch which tells us that there's a problem.
</quote>
-- https://lore.kernel.org/lkml/[email protected]/
Quoted 9 lines
This logically separate change raises questions such as
- What is the impact on overall code size? Not huge, presumably, but every little bit hurts.
- Additional runtime costs of those extra comparisons?
- These impacts could be lessened by using
VM_BUG_ON()rather thanBUG_ON()- should we do this?
<sigh> I lost that argument last time around.
<quote>BUG() is our only option here. Both limiting how much we copy or copying the requested amount result in data corruption or leaking information to a process that isn't supposed to see it. </quote>
https://lore.kernel.org/lkml/[email protected]/
CC'ing Matthew because I _really_ don't want to argue this any longer.
Quoted 3 lines
- Linus reeeeeeeally doesn't like new BUG_ON()s. Maybe you can sneak it past him ;)
I'm worried too... :-(
Quoted 2 lines
See what I mean?
Yes I do however ... see above... :-/
Ira
Quoted 3 lines
I do think it would be best to take those assertions out of the patch and to propose them separately, at a later time.
Andrew Morton
maintainerQuoted 37 lines · On Tue, 9 Feb 2021 12:52:49 -0800 Ira Weiny <[email protected]> wrote:
Quoted 28 lines · Andrew Morton
Quoted 15 lines · On Tue, 9 Feb 2021 16:11:23 +0100 David Sterba <[email protected]> wrote:
Quoted 10 lines · On Fri, Feb 05, 2021 at 03:23:00PM -0800, [email protected] wrote:
From: Ira Weiny <[email protected]>
There are many places where kmap/<operation>/kunmap patterns occur. We lift these various patterns to core common functions and use them in the btrfs file system. At the same time we convert those core functions to use
kmap_local_page()which is more efficient in those calls.I think this is best accepted through Andrew's tree as it has the mem*_page functions in it. But I'd like to get an ack from David or one of the other btrfs maintainers before the btrfs patches go through.
I'd rather take the non-mm patches through my tree so it gets tested the same way as other btrfs changes, straightforward cleanups or not.
Quoted 8 lines
This brings the question how to do that as the first patch should go through the MM tree. One option is to posptpone the actual cleanups after the 1st patch is merged but this could take a long delay.
I'd suggest to take the 1st patch within MM tree in the upcoming merge window and then I can prepare a separate pull with just the cleanups. Removing an inter-tree patch dependency was a sufficient reason for Linus in the past for such pull requests.
It would be best to merge [1/4] via the btrfs tree. Please add my
Quoted 7 lines
Acked-by Andrew Morton <[email protected]>Although I think it would be better if [1/4] merely did the code movement. Adding those BUG_ON()s is a semantic/functional change and really shouldn't be bound up with the other things this patch series does.
Quoted 7 lines
I proposed this too and was told 'no'...
<quote>
If we put in into a separate patch, someone will suggest backing out the patch which tells us that there's a problem.
</quote>-- https://lore.kernel.org/lkml/[email protected]/
Yeah, no, please let's not do this. Bundling an offtopic change into [1/4] then making three more patches dependent on the ontopic parts of [1/4] is just rude.
I think the case for adding the BUG_ONs can be clearly made. And that case should at least have been clearly made in the [1/4] changelog!
(Although I expect VM_BUG_ON() would be better - will give us sufficient coverage without the overall impact.)
Let's please queue this up separately.
Quoted 23 lines · Andrew Morton
Quoted 11 lines
Quoted 10 lines
It would be best to merge [1/4] via the btrfs tree. Please add my
Acked-by Andrew Morton <[email protected]>Although I think it would be better if [1/4] merely did the code movement. Adding those BUG_ON()s is a semantic/functional change and really shouldn't be bound up with the other things this patch series does.
Quoted 7 lines
I proposed this too and was told 'no'...
<quote>
If we put in into a separate patch, someone will suggest backing out the patch which tells us that there's a problem.
</quote>-- https://lore.kernel.org/lkml/[email protected]/Yeah, no, please let's not do this. Bundling an offtopic change into [1/4] then making three more patches dependent on the ontopic parts of [1/4] is just rude.
Quoted 5 lines
I think the case for adding the
BUG_ONscan be clearly made. And that case should at least have been clearly made in the [1/4] changelog!(Although I expect
VM_BUG_ON()would be better - will give us sufficient coverage without the overall impact.)
I'm ok with VM_BUG_ON()
Quoted 2 lines
Let's please queue this up separately.
Ok can I retain your Ack on the move part of the patch? Note that it does change kmap_atomic() to kmap_local_page() currently.
Would you prefer a separate change for that as well?
Ira
PS really CC'ing Matthew now...
Andrew Morton
maintainerQuoted 4 lines · On Tue, 9 Feb 2021 13:52:29 -0800 Ira Weiny <[email protected]> wrote:
Quoted 2 lines
Let's please queue this up separately.
Ok can I retain your Ack on the move part of the patch?
I missed that.
Quoted 3 lines
Note that it does change
kmap_atomic()tokmap_local_page()currently.Would you prefer a separate change for that as well?
Really that should be separated out as well, coming after the move, to make it more easily reverted. With a standalone changlog for this.
All a bit of a pain, but it's best in the long run.
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Tue, 9 Feb 2021 13:52:29 -0800 Ira Weiny <[email protected]> wrote:
Quoted 2 lines
Let's please queue this up separately.
Ok can I retain your Ack on the move part of the patch?
I missed that.
Quoted 8 lines
Quoted 3 lines
Note that it does change
kmap_atomic()tokmap_local_page()currently.Would you prefer a separate change for that as well?
Really that should be separated out as well, coming after the move, to make it more easily reverted. With a standalone changlog for this.
All a bit of a pain, but it's best in the long run.
Consider it done.
Ira
BTW does anyone know the reason this thread is not making it to lore? I don't see any of the emails between Andrew and me?
https://lore.kernel.org/lkml/[email protected]/
Andrew Morton
maintainerQuoted 19 lines · On Tue, 9 Feb 2021 14:03:29 -0800 Ira Weiny <[email protected]> wrote:
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Tue, 9 Feb 2021 13:52:29 -0800 Ira Weiny <[email protected]> wrote:
Quoted 2 lines
Let's please queue this up separately.
Ok can I retain your Ack on the move part of the patch?
I missed that.
Quoted 8 lines
Quoted 3 lines
Note that it does change
kmap_atomic()tokmap_local_page()currently.Would you prefer a separate change for that as well?
Really that should be separated out as well, coming after the move, to make it more easily reverted. With a standalone changlog for this.
All a bit of a pain, but it's best in the long run.
Quoted 8 lines
Consider it done.
Ira
BTW does anyone know the reason this thread is not making it to lore? I don't see any of the emails between Andrew and me?
https://lore.kernel.org/lkml/[email protected]/
https://lkml.kernel.org/r/[email protected]
works OK. It found the message in the linux-fsdevel archive.