btrfs: Convert zlib_compress_pages() to use kmap_local_page()
PATCH 1/1 btrfs: Convert zlib_compress_pages() to use kmap_local_page()
The use of kmap() is being deprecated in favor of kmap_local_page(). With kmap_local_page(), the mapping is per thread, CPU local and not globally visible.
Therefore, use kmap_local_page() / kunmap_local() in zlib_compress_pages() because in this function the mappings are per thread and are not visible in other contexts. Furthermore, drop the mappings of "out_page" which is allocated within zlib_compress_pages() with alloc_page(GFP_NOFS) and use page_address() (thanks to David Sterba).
Tested with xfstests on a QEMU + KVM 32-bits VM with 4GB of RAM booting a kernel with HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
--- fs/btrfs/zlib.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)
fs/btrfs/zlib.c +13 -19
@@ -97,7 +97,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, { struct workspace *workspace = list_entry(ws, struct workspace, list);int ret; -char *data_in; +char *data_in =NULL ;char *cpage_out;int nr_pages =0 ;struct page *in_page =NULL ;
@@ -126,7 +126,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[0 ] = out_page; nr_pages =1 ;
@@ -148,26 +148,26 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,int i;for (i =0 ; i < in_buf_pages; i++) { -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } in_page =find_get_page (mapping, start >>PAGE_SHIFT ); - data_in =kmap (in_page); + data_in =kmap_local_page (in_page);memcpy (workspace->buf + i *PAGE_SIZE , data_in,PAGE_SIZE ); start +=PAGE_SIZE ; } workspace->strm.next_in = workspace->buf; }else { -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } in_page =find_get_page (mapping, start >>PAGE_SHIFT ); - data_in =kmap (in_page); + data_in =kmap_local_page (in_page); start +=PAGE_SIZE ; workspace->strm.next_in = data_in; }
@@ -196,9 +196,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,* the stream end if required */ if (workspace->strm.avail_out ==0 ) { -kunmap (out_page);if (nr_pages == nr_dest_pages) { - out_page =NULL ; ret = -E2BIG ; goto out; }
@@ -207,7 +205,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[nr_pages] = out_page; nr_pages++; workspace->strm.avail_out =PAGE_SIZE ;
@@ -234,9 +232,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, goto out; }else if (workspace->strm.avail_out ==0 ) {/* get another page for the stream end */ -kunmap (out_page);if (nr_pages == nr_dest_pages) { - out_page =NULL ; ret = -E2BIG ; goto out; }
@@ -245,7 +241,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[nr_pages] = out_page; nr_pages++; workspace->strm.avail_out =PAGE_SIZE ;
@@ -264,13 +260,11 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, *total_in = workspace->strm.total_in; out: *out_pages = nr_pages; -if (out_page) -kunmap (out_page); - -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } +return ret; }
Signature · 1 line
2.36.1
Quoted 10 lines · Fabio M. De Francesco
The use of
kmap()is being deprecated in favor ofkmap_local_page(). Withkmap_local_page(), the mapping is per thread, CPU local and not globally visible.Therefore, use
kmap_local_page()/kunmap_local()inzlib_compress_pages()because in this function the mappings are per thread and are not visible in other contexts. Furthermore, drop the mappings of "out_page" which is allocated withinzlib_compress_pages()withalloc_page(GFP_NOFS)and usepage_address()(thanks to David Sterba).
Quoted 7 lines
Tested with xfstests on a QEMU + KVM 32-bits VM with 4GB of RAM booting a kernel with HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
Cc Qu Wenruo <[email protected]>Suggested-by David Sterba <[email protected]>Suggested-by Ira Weiny <[email protected]>
Quoted 118 lines
Signed-off-by Fabio M. De Francesco <[email protected]>--- fs/btrfs/zlib.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)fs/btrfs/zlib.c +13
-19@@ -97,7 +97,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, { struct workspace *workspace = list_entry(ws, struct workspace, list);int ret; -char *data_in; +char *data_in =NULL ;char *cpage_out;int nr_pages =0 ;struct page *in_page =NULL ;@@ -126,7 +126,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[0 ] = out_page; nr_pages =1 ;@@ -148,26 +148,26 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,int i;for (i =0 ; i < in_buf_pages; i++) { -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } in_page =find_get_page (mapping, start >>PAGE_SHIFT ); - data_in =kmap (in_page); + data_in =kmap_local_page (in_page);memcpy (workspace->buf + i *PAGE_SIZE , data_in,PAGE_SIZE ); start +=PAGE_SIZE ; } workspace->strm.next_in = workspace->buf; }else { -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } in_page =find_get_page (mapping, start >>PAGE_SHIFT ); - data_in =kmap (in_page); + data_in =kmap_local_page (in_page); start +=PAGE_SIZE ; workspace->strm.next_in = data_in; }@@ -196,9 +196,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,* the stream end if required */ if (workspace->strm.avail_out ==0 ) { -kunmap (out_page);if (nr_pages == nr_dest_pages) { - out_page =NULL ; ret = -E2BIG ; goto out; }@@ -207,7 +205,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[nr_pages] = out_page; nr_pages++; workspace->strm.avail_out =PAGE_SIZE ;@@ -234,9 +232,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, goto out; }else if (workspace->strm.avail_out ==0 ) {/* get another page for the stream end */ -kunmap (out_page);if (nr_pages == nr_dest_pages) { - out_page =NULL ; ret = -E2BIG ; goto out; }@@ -245,7 +241,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, ret = -ENOMEM ; goto out; } - cpage_out =kmap (out_page); + cpage_out =page_address (out_page); pages[nr_pages] = out_page; nr_pages++; workspace->strm.avail_out =PAGE_SIZE ;@@ -264,13 +260,11 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, *total_in = workspace->strm.total_in; out: *out_pages = nr_pages; -if (out_page) -kunmap (out_page); - -if (in_page) { -kunmap (in_page); +if (data_in) { +kunmap_local (data_in);put_page (in_page); } +return ret; }Signature · 1 line
2.36.1
Quoted 10 lines · Fabio M. De Francesco
The use of
kmap()is being deprecated in favor ofkmap_local_page(). Withkmap_local_page(), the mapping is per thread, CPU local and not globally visible.Therefore, use
kmap_local_page()/kunmap_local()inzlib_compress_pages()because in this function the mappings are per thread and are not visible in other contexts. Furthermore, drop the mappings of "out_page" which is allocated withinzlib_compress_pages()withalloc_page(GFP_NOFS)and usepage_address()(thanks to David Sterba).
Quoted 8 lines
Tested with xfstests on a QEMU + KVM 32-bits VM with 4GB of RAM booting a kernel with HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
Cc Qu Wenruo <[email protected]>Suggested-by David Sterba <[email protected]>Suggested-by Ira Weiny <[email protected]>Signed-off-by Fabio M. De Francesco <[email protected]>
Added to the kmap patch queue, thanks.