[v3,3/6] gfs2: Replace kmap() by kmap_local_page() in gfs2_unstuffer_page

Message ID 063721a02d5e226d1e9e9782f76ce94c16d73e93.1688073459.git.drv@mailo.com
State New
Headers
Series gfs2: kmap{_atomic} conversion to kmap_local_{page/folio} |

Commit Message

Deepak R Varma June 29, 2023, 9:50 p.m. UTC
  The use of kmap() is being deprecated in favor of kmap_local_page().

There are two main problems with kmap(): (1) It comes with an overhead as
the mapping space is restricted and protected by a global lock for
synchronization and (2) it also requires global TLB invalidation when the
kmap’s pool wraps and it might block when the mapping space is fully
utilized until a slot becomes available.

With kmap_local_page() the mappings are per thread, CPU local, can take
page faults, and can be called from any context (including interrupts).
It is faster than kmap() in kernels with HIGHMEM enabled. Furthermore,
the tasks can be preempted and, when they are scheduled to run again, the
kernel virtual addresses are restored and still valid.

Therefore, replace kmap() with kmap_local_page() in gfs2_unstuffer_page().

Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
Signed-off-by: Deepak R Varma <drv@mailo.com>
---
Changes in v3:
   - Patch included in the patch series

Changes in v2:
   - None


 fs/gfs2/bmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Fabio M. De Francesco July 1, 2023, 1:01 p.m. UTC | #1
On giovedì 29 giugno 2023 23:50:43 CEST Deepak R Varma wrote:
> The use of kmap() is being deprecated in favor of kmap_local_page().
> 
> There are two main problems with kmap(): (1) It comes with an overhead as
> the mapping space is restricted and protected by a global lock for
> synchronization and (2) it also requires global TLB invalidation when the
> kmap’s pool wraps and it might block when the mapping space is fully
> utilized until a slot becomes available.
> 
> With kmap_local_page() the mappings are per thread, CPU local, can take
> page faults, and can be called from any context (including interrupts).
> It is faster than kmap() in kernels with HIGHMEM enabled. Furthermore,
> the tasks can be preempted and, when they are scheduled to run again, the
> kernel virtual addresses are restored and still valid.
> 
> Therefore, replace kmap() with kmap_local_page() in gfs2_unstuffer_page().
> 
> Suggested-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
> Signed-off-by: Deepak R Varma <drv@mailo.com>
> ---

Deepak,

Would you please cite the author of this boiler-plate commit message? I think 
that you are not required by any stated formal rule, however it would be much 
appreciated (by me, at least :-)).

> Changes in v3:
>    - Patch included in the patch series
> 
> Changes in v2:
>    - None
> 
> 
>  fs/gfs2/bmap.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
> index 8d611fbcf0bd..6b850e2ba5c8 100644
> --- a/fs/gfs2/bmap.c
> +++ b/fs/gfs2/bmap.c
> @@ -58,12 +58,12 @@ static int gfs2_unstuffer_page(struct gfs2_inode *ip,
> struct buffer_head *dibh, struct inode *inode = &ip->i_inode;
> 
>  	if (!PageUptodate(page)) {
> -		void *kaddr = kmap(page);
> +		void *kaddr = kmap_local_page(page);
>  		u64 dsize = i_size_read(inode);

As a general rule, we should take the mappings the shorter time it is possible 
(to avoid to disable migration for too long). I'm not sure why the "dsize" 
assignment is made between mapping and un-mapping. Can you please explain why?

Thanks,

Fabio

>  		memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), 
dsize);
>  		memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
> -		kunmap(page);
> +		kunmap_local(kaddr);
> 
>  		SetPageUptodate(page);
>  	}
> --
> 2.34.1
  

Patch

diff --git a/fs/gfs2/bmap.c b/fs/gfs2/bmap.c
index 8d611fbcf0bd..6b850e2ba5c8 100644
--- a/fs/gfs2/bmap.c
+++ b/fs/gfs2/bmap.c
@@ -58,12 +58,12 @@  static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
 	struct inode *inode = &ip->i_inode;
 
 	if (!PageUptodate(page)) {
-		void *kaddr = kmap(page);
+		void *kaddr = kmap_local_page(page);
 		u64 dsize = i_size_read(inode);
  
 		memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
 		memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
-		kunmap(page);
+		kunmap_local(kaddr);
 
 		SetPageUptodate(page);
 	}