diff mbox series

[v2] drm/i915/gt: Replace kmap with its safer kmap_local_page counterpart

Message ID 20250214003437.1311476-1-andi.shyti@linux.intel.com (mailing list archive)
State New
Headers show
Series [v2] drm/i915/gt: Replace kmap with its safer kmap_local_page counterpart | expand

Commit Message

Andi Shyti Feb. 14, 2025, 12:34 a.m. UTC
kmap_local_page(), unlike kmap(), performs a contextualized
mapping of pages. This means the pages are mapped locally to the
thread that created them, making them invisible outside the
thread and safer to use.

Replace kmap() and kunmap() with kmap_local_page() and
kunmap_local() counterparts for improved safety.

Signed-off-by: Andi Shyti <andi.shyti@linux.intel.com>
Reviewed-by: Krzysztof Karas <krzysztof.karas@intel.com>
---
Cc: Nitin Gote <nitin.r.gote@intel.com>

v1 -> v2:
 - replaced kmap with the _local version also in the
   intel_ggtt_fencing.c file. (Thanks Nitin)

 drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c | 4 ++--
 drivers/gpu/drm/i915/gt/shmem_utils.c        | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

Comments

Gote, Nitin R Feb. 17, 2025, 4:30 a.m. UTC | #1
Hi Andi, 

> -----Original Message-----
> From: Andi Shyti <andi.shyti@linux.intel.com>
> Sent: Friday, February 14, 2025 6:05 AM
> To: intel-gfx <intel-gfx@lists.freedesktop.org>; dri-devel <dri-
> devel@lists.freedesktop.org>
> Cc: Andi Shyti <andi.shyti@linux.intel.com>; Karas, Krzysztof
> <krzysztof.karas@intel.com>; Gote, Nitin R <nitin.r.gote@intel.com>
> Subject: [PATCH v2] drm/i915/gt: Replace kmap with its safer kmap_local_page
> counterpart
> 
> kmap_local_page(), unlike kmap(), performs a contextualized mapping of pages.
> This means the pages are mapped locally to the thread that created them, making
> them invisible outside the thread and safer to use.
> 
> Replace kmap() and kunmap() with kmap_local_page() and
> kunmap_local() counterparts for improved safety.
> 
> Signed-off-by: Andi Shyti <andi.shyti@linux.intel.com>
> Reviewed-by: Krzysztof Karas <krzysztof.karas@intel.com>
> ---
> Cc: Nitin Gote <nitin.r.gote@intel.com>
> 
> v1 -> v2:
>  - replaced kmap with the _local version also in the
>    intel_ggtt_fencing.c file. (Thanks Nitin)
> 
>  drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c | 4 ++--
>  drivers/gpu/drm/i915/gt/shmem_utils.c        | 8 ++++----
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
> b/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
> index 0ffba50981e3..00f7cd6debf3 100644
> --- a/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
> +++ b/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
> @@ -749,7 +749,7 @@ static void swizzle_page(struct page *page)
>  	char *vaddr;
>  	int i;
> 
> -	vaddr = kmap(page);
> +	vaddr = kmap_local_page(page);
> 
>  	for (i = 0; i < PAGE_SIZE; i += 128) {
>  		memcpy(temp, &vaddr[i], 64);
> @@ -757,7 +757,7 @@ static void swizzle_page(struct page *page)
>  		memcpy(&vaddr[i + 64], temp, 64);
>  	}
> 
> -	kunmap(page);
> +	kunmap_local(vaddr);
>  }
> 
>  /**
> diff --git a/drivers/gpu/drm/i915/gt/shmem_utils.c
> b/drivers/gpu/drm/i915/gt/shmem_utils.c
> index bb696b29ee2c..365c4b8b04f4 100644
> --- a/drivers/gpu/drm/i915/gt/shmem_utils.c
> +++ b/drivers/gpu/drm/i915/gt/shmem_utils.c
> @@ -108,7 +108,7 @@ static int __shmem_rw(struct file *file, loff_t off,
>  		if (IS_ERR(page))
>  			return PTR_ERR(page);
> 
> -		vaddr = kmap(page);
> +		vaddr = kmap_local_page(page);
>  		if (write) {
>  			memcpy(vaddr + offset_in_page(off), ptr, this);
>  			set_page_dirty(page);
> @@ -116,7 +116,7 @@ static int __shmem_rw(struct file *file, loff_t off,
>  			memcpy(ptr, vaddr + offset_in_page(off), this);
>  		}
>  		mark_page_accessed(page);
> -		kunmap(page);
> +		kunmap_local(vaddr);
>  		put_page(page);
> 
>  		len -= this;
> @@ -143,11 +143,11 @@ int shmem_read_to_iosys_map(struct file *file, loff_t
> off,
>  		if (IS_ERR(page))
>  			return PTR_ERR(page);
> 
> -		vaddr = kmap(page);
> +		vaddr = kmap_local_page(page);
>  		iosys_map_memcpy_to(map, map_off, vaddr +
> offset_in_page(off),
>  				    this);
>  		mark_page_accessed(page);
> -		kunmap(page);
> +		kunmap_local(vaddr);
>  		put_page(page);
> 
>  		len -= this;
> --
> 2.47.2

LGTM.
Reviewed-by: Nitin Gote <nitin.r.gote@intel.com>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c b/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
index 0ffba50981e3..00f7cd6debf3 100644
--- a/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
+++ b/drivers/gpu/drm/i915/gt/intel_ggtt_fencing.c
@@ -749,7 +749,7 @@  static void swizzle_page(struct page *page)
 	char *vaddr;
 	int i;
 
-	vaddr = kmap(page);
+	vaddr = kmap_local_page(page);
 
 	for (i = 0; i < PAGE_SIZE; i += 128) {
 		memcpy(temp, &vaddr[i], 64);
@@ -757,7 +757,7 @@  static void swizzle_page(struct page *page)
 		memcpy(&vaddr[i + 64], temp, 64);
 	}
 
-	kunmap(page);
+	kunmap_local(vaddr);
 }
 
 /**
diff --git a/drivers/gpu/drm/i915/gt/shmem_utils.c b/drivers/gpu/drm/i915/gt/shmem_utils.c
index bb696b29ee2c..365c4b8b04f4 100644
--- a/drivers/gpu/drm/i915/gt/shmem_utils.c
+++ b/drivers/gpu/drm/i915/gt/shmem_utils.c
@@ -108,7 +108,7 @@  static int __shmem_rw(struct file *file, loff_t off,
 		if (IS_ERR(page))
 			return PTR_ERR(page);
 
-		vaddr = kmap(page);
+		vaddr = kmap_local_page(page);
 		if (write) {
 			memcpy(vaddr + offset_in_page(off), ptr, this);
 			set_page_dirty(page);
@@ -116,7 +116,7 @@  static int __shmem_rw(struct file *file, loff_t off,
 			memcpy(ptr, vaddr + offset_in_page(off), this);
 		}
 		mark_page_accessed(page);
-		kunmap(page);
+		kunmap_local(vaddr);
 		put_page(page);
 
 		len -= this;
@@ -143,11 +143,11 @@  int shmem_read_to_iosys_map(struct file *file, loff_t off,
 		if (IS_ERR(page))
 			return PTR_ERR(page);
 
-		vaddr = kmap(page);
+		vaddr = kmap_local_page(page);
 		iosys_map_memcpy_to(map, map_off, vaddr + offset_in_page(off),
 				    this);
 		mark_page_accessed(page);
-		kunmap(page);
+		kunmap_local(vaddr);
 		put_page(page);
 
 		len -= this;