linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
@ 2022-06-18  9:27 Fabio M. De Francesco
  2022-06-25 14:41 ` Fabio M. De Francesco
  0 siblings, 1 reply; 8+ messages in thread
From: Fabio M. De Francesco @ 2022-06-18  9:27 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Filipe Manana, Qu Wenruo, Nikolay Borisov, Gabriel Niebler,
	linux-btrfs, linux-kernel
  Cc: Fabio M. De Francesco, Ira Weiny

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.

Tested with xfstests on QEMU + KVM 32-bit VM with 4GB of RAM and
HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".

Cc: Qu Wenruo <wqu@suse.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
---

This patch builds only on top of
"[PATCH] btrfs: zlib: refactor how we prepare the input buffer" by Qu Wenruo".
https://lore.kernel.org/linux-btrfs/d0bfc791b5509df7b9ad44e41ada197d1b3149b3.1655519730.git.wqu@suse.com/

 fs/btrfs/zlib.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/zlib.c b/fs/btrfs/zlib.c
index 966e17cea981..4496dd30bd71 100644
--- a/fs/btrfs/zlib.c
+++ b/fs/btrfs/zlib.c
@@ -160,7 +160,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
 		ret = -ENOMEM;
 		goto out;
 	}
-	cpage_out = kmap(out_page);
+	cpage_out = kmap_local_page(out_page);
 	pages[0] = out_page;
 	nr_pages = 1;
 
@@ -198,9 +198,9 @@ 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);
+			kunmap_local(cpage_out);
 			if (nr_pages == nr_dest_pages) {
-				out_page = NULL;
+				cpage_out = NULL;
 				ret = -E2BIG;
 				goto out;
 			}
@@ -209,7 +209,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
 				ret = -ENOMEM;
 				goto out;
 			}
-			cpage_out = kmap(out_page);
+			cpage_out = kmap_local_page(out_page);
 			pages[nr_pages] = out_page;
 			nr_pages++;
 			workspace->strm.avail_out = PAGE_SIZE;
@@ -236,9 +236,9 @@ 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);
+			kunmap_local(cpage_out);
 			if (nr_pages == nr_dest_pages) {
-				out_page = NULL;
+				cpage_out = NULL;
 				ret = -E2BIG;
 				goto out;
 			}
@@ -247,7 +247,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
 				ret = -ENOMEM;
 				goto out;
 			}
-			cpage_out = kmap(out_page);
+			cpage_out = kmap_local_page(out_page);
 			pages[nr_pages] = out_page;
 			nr_pages++;
 			workspace->strm.avail_out = PAGE_SIZE;
@@ -266,8 +266,8 @@ 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 (cpage_out)
+		kunmap_local(cpage_out);
 	return ret;
 }
 
-- 
2.36.1


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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-18  9:27 [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page() Fabio M. De Francesco
@ 2022-06-25 14:41 ` Fabio M. De Francesco
  2022-06-25 23:03   ` Qu Wenruo
  0 siblings, 1 reply; 8+ messages in thread
From: Fabio M. De Francesco @ 2022-06-25 14:41 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Filipe Manana, Qu Wenruo, Nikolay Borisov, Gabriel Niebler,
	linux-btrfs, linux-kernel
  Cc: Ira Weiny

On sabato 18 giugno 2022 11:27:52 CEST Fabio M. De Francesco wrote:
> 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.
> 
> Tested with xfstests on QEMU + KVM 32-bit VM with 4GB of RAM and
> HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
> 
> Cc: Qu Wenruo <wqu@suse.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
> ---
> 
> This patch builds only on top of
> "[PATCH] btrfs: zlib: refactor how we prepare the input buffer" by Qu 
Wenruo".
> https://lore.kernel.org/linux-btrfs/
d0bfc791b5509df7b9ad44e41ada197d1b3149b3.1655519730.git.wqu@suse.com/
> 

I've seen that Qu sent a v2 of the above patch. However David thinks that 
it is better not to map pages allocated in zlib.c for output (out_page) 
since they cannot come from highmem because of "alloc_page(GFP_NOFS);".

@David:

I suppose that, since it builds _only_ on top of the refactor submitted by 
Qu, I'll have to wait and see what you decide. 

If you don't want kmap_local_page() and prefer using page_address() on 
"out_page", please drop this patch and let me know, so that I can send a 
new patch which will be in accordance to your preference.

Thanks,

Fabio



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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-25 14:41 ` Fabio M. De Francesco
@ 2022-06-25 23:03   ` Qu Wenruo
  2022-06-26 11:12     ` Fabio M. De Francesco
  2022-06-27 16:41     ` Fabio M. De Francesco
  0 siblings, 2 replies; 8+ messages in thread
From: Qu Wenruo @ 2022-06-25 23:03 UTC (permalink / raw)
  To: Fabio M. De Francesco, Chris Mason, Josef Bacik, David Sterba,
	Nick Terrell, Chris Down, Filipe Manana, Nikolay Borisov,
	Gabriel Niebler, linux-btrfs, linux-kernel
  Cc: Ira Weiny



On 2022/6/25 22:41, Fabio M. De Francesco wrote:
> On sabato 18 giugno 2022 11:27:52 CEST Fabio M. De Francesco wrote:
>> 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.
>>
>> Tested with xfstests on QEMU + KVM 32-bit VM with 4GB of RAM and
>> HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
>>
>> Cc: Qu Wenruo <wqu@suse.com>
>> Suggested-by: Ira Weiny <ira.weiny@intel.com>
>> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
>> ---
>>
>> This patch builds only on top of
>> "[PATCH] btrfs: zlib: refactor how we prepare the input buffer" by Qu
> Wenruo".
>> https://lore.kernel.org/linux-btrfs/
> d0bfc791b5509df7b9ad44e41ada197d1b3149b3.1655519730.git.wqu@suse.com/
>>
> 
> I've seen that Qu sent a v2 of the above patch. However David thinks that
> it is better not to map pages allocated in zlib.c for output (out_page)
> since they cannot come from highmem because of "alloc_page(GFP_NOFS);".
> 
> @David:
> 
> I suppose that, since it builds _only_ on top of the refactor submitted by
> Qu, I'll have to wait and see what you decide.
> 
> If you don't want kmap_local_page() and prefer using page_address() on
> "out_page", please drop this patch and let me know, so that I can send a
> new patch which will be in accordance to your preference.

And that would also make the convert much easier for kmap_local_page() 
of input pages.

I'll hold the refactor patch after all the kmap code is converted.

Thanks,
Qu

> 
> Thanks,
> 
> Fabio
> 
> 

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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-25 23:03   ` Qu Wenruo
@ 2022-06-26 11:12     ` Fabio M. De Francesco
  2022-06-27 16:41     ` Fabio M. De Francesco
  1 sibling, 0 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2022-06-26 11:12 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Filipe Manana, Nikolay Borisov, Gabriel Niebler, linux-btrfs,
	linux-kernel, Qu Wenruo
  Cc: Ira Weiny

On domenica 26 giugno 2022 01:03:55 CEST Qu Wenruo wrote:
> 
> On 2022/6/25 22:41, Fabio M. De Francesco wrote:
> > On sabato 18 giugno 2022 11:27:52 CEST Fabio M. De Francesco wrote:
> >> 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.
> >>
> >> Tested with xfstests on QEMU + KVM 32-bit VM with 4GB of RAM and
> >> HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
> >>
> >> Cc: Qu Wenruo <wqu@suse.com>
> >> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> >> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
> >> ---
> >>
> >> This patch builds only on top of
> >> "[PATCH] btrfs: zlib: refactor how we prepare the input buffer" by Qu
> >> Wenruo".
> >> https://lore.kernel.org/linux-btrfs/
d0bfc791b5509df7b9ad44e41ada197d1b3149b3.1655519730.git.wqu@suse.com/
> >>
> > 
> > I've seen that Qu sent a v2 of the above patch. However David thinks 
> > that it is better not to map pages allocated in zlib.c for output 
> > (out_page) since they cannot come from highmem because of 
> > "alloc_page(GFP_NOFS);".
> > 
> > @David:
> > 
> > I suppose that, since it builds _only_ on top of the refactor submitted 
> > by
> > Qu, I'll have to wait and see what you decide.
> > 
> > If you don't want kmap_local_page() and prefer using page_address() on
> > "out_page", please drop this patch and let me know, so that I can send 
a
> > new patch which will be in accordance to your preference.
> 
> And that would also make the convert much easier for kmap_local_page() 
> of input pages.
> 
> I'll hold the refactor patch after all the kmap code is converted.

Thanks Qu,

I have already made a patch to zlib_compress_pages() in accordance to what 
David asked for, but I cannot compile and test it. 

With last week update of Tumbleweed, openSUSE dropped GCC-10 for x86_32, so 
we've been left only with GCC-7. With GCC-7 I cannot any longer build 
5.19.rc3 because it fails somewhere in drm/i915 (where code has not changed 
since April 2022).

I suppose it's just a mistake which they will fix within few days. 

I'm pretty sure that my patch works properly, however I'm not comfortable 
to submit it with no successful build and tests.

Again thanks,

Fabio




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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-25 23:03   ` Qu Wenruo
  2022-06-26 11:12     ` Fabio M. De Francesco
@ 2022-06-27 16:41     ` Fabio M. De Francesco
  1 sibling, 0 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2022-06-27 16:41 UTC (permalink / raw)
  To: Qu Wenruo
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Filipe Manana, Nikolay Borisov, Gabriel Niebler, linux-btrfs,
	linux-kernel, Ira Weiny

On domenica 26 giugno 2022 01:03:55 CEST Qu Wenruo wrote:
> 
> On 2022/6/25 22:41, Fabio M. De Francesco wrote:
> > On sabato 18 giugno 2022 11:27:52 CEST Fabio M. De Francesco wrote:
> >> 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.
> >>
> >> Tested with xfstests on QEMU + KVM 32-bit VM with 4GB of RAM and
> >> HIGHMEM64G enabled. This patch passes 26/26 tests of group "compress".
> >>
> >> Cc: Qu Wenruo <wqu@suse.com>
> >> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> >> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
> >> ---
> >>
> >> This patch builds only on top of
> >> "[PATCH] btrfs: zlib: refactor how we prepare the input buffer" by Qu
> > Wenruo".
> >> https://lore.kernel.org/linux-btrfs/
> > d0bfc791b5509df7b9ad44e41ada197d1b3149b3.1655519730.git.wqu@suse.com/
> >>
> > 
> > I've seen that Qu sent a v2 of the above patch. However David thinks 
that
> > it is better not to map pages allocated in zlib.c for output (out_page)
> > since they cannot come from highmem because of "alloc_page(GFP_NOFS);".
> > 
> > @David:
> > 
> > I suppose that, since it builds _only_ on top of the refactor submitted 
by
> > Qu, I'll have to wait and see what you decide.
> > 
> > If you don't want kmap_local_page() and prefer using page_address() on
> > "out_page", please drop this patch and let me know, so that I can send 
a
> > new patch which will be in accordance to your preference.
> 
> And that would also make the convert much easier for kmap_local_page() 
> of input pages.
> 
> I'll hold the refactor patch after all the kmap code is converted.
> 
> Thanks,
> Qu

Thanks for holding the refactor patch for the time I needed.

Now zlib.c is converted to kmap_local_page() and tested:
https://lore.kernel.org/lkml/20220627163305.24116-1-fmdefrancesco@gmail.com/

Again thanks,

Fabio




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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-27 16:33 Fabio M. De Francesco
  2022-06-30 14:51 ` Ira Weiny
@ 2022-07-07 21:17 ` David Sterba
  1 sibling, 0 replies; 8+ messages in thread
From: David Sterba @ 2022-07-07 21:17 UTC (permalink / raw)
  To: Fabio M. De Francesco
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Qu Wenruo, Filipe Manana, Gabriel Niebler, linux-btrfs,
	linux-kernel, Ira Weiny

On Mon, Jun 27, 2022 at 06:33:05PM +0200, Fabio M. De Francesco wrote:
> 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".
> 
> Cc: Qu Wenruo <wqu@suse.com>
> Suggested-by: David Sterba <dsterba@suse.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>

Added to the kmap patch queue, thanks.

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

* Re: [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
  2022-06-27 16:33 Fabio M. De Francesco
@ 2022-06-30 14:51 ` Ira Weiny
  2022-07-07 21:17 ` David Sterba
  1 sibling, 0 replies; 8+ messages in thread
From: Ira Weiny @ 2022-06-30 14:51 UTC (permalink / raw)
  To: Fabio M. De Francesco
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Qu Wenruo, Filipe Manana, Gabriel Niebler, linux-btrfs,
	linux-kernel

On Mon, Jun 27, 2022 at 06:33:05PM +0200, Fabio M. De Francesco wrote:
> 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".
> 
> Cc: Qu Wenruo <wqu@suse.com>
> Suggested-by: David Sterba <dsterba@suse.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>

Reviewed-by: Ira Weiny <ira.weiny@intel.com>

> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
> ---
>  fs/btrfs/zlib.c | 32 +++++++++++++-------------------
>  1 file changed, 13 insertions(+), 19 deletions(-)
> 
> diff --git a/fs/btrfs/zlib.c b/fs/btrfs/zlib.c
> index 770c4c6bbaef..b4f44662cda7 100644
> --- a/fs/btrfs/zlib.c
> +++ b/fs/btrfs/zlib.c
> @@ -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;
>  }
>  
> -- 
> 2.36.1
> 

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

* [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page()
@ 2022-06-27 16:33 Fabio M. De Francesco
  2022-06-30 14:51 ` Ira Weiny
  2022-07-07 21:17 ` David Sterba
  0 siblings, 2 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2022-06-27 16:33 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, Chris Down,
	Qu Wenruo, Filipe Manana, Gabriel Niebler, linux-btrfs,
	linux-kernel
  Cc: Fabio M. De Francesco, Ira Weiny

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".

Cc: Qu Wenruo <wqu@suse.com>
Suggested-by: David Sterba <dsterba@suse.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
---
 fs/btrfs/zlib.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)

diff --git a/fs/btrfs/zlib.c b/fs/btrfs/zlib.c
index 770c4c6bbaef..b4f44662cda7 100644
--- a/fs/btrfs/zlib.c
+++ b/fs/btrfs/zlib.c
@@ -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;
 }
 
-- 
2.36.1


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

end of thread, other threads:[~2022-07-07 21:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-18  9:27 [PATCH] btrfs: Convert zlib_compress_pages() to use kmap_local_page() Fabio M. De Francesco
2022-06-25 14:41 ` Fabio M. De Francesco
2022-06-25 23:03   ` Qu Wenruo
2022-06-26 11:12     ` Fabio M. De Francesco
2022-06-27 16:41     ` Fabio M. De Francesco
2022-06-27 16:33 Fabio M. De Francesco
2022-06-30 14:51 ` Ira Weiny
2022-07-07 21:17 ` David Sterba

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).