diff mbox series

[v2] mm/kmemleak: Use explicit cast to cast pointer from percpu to generic AS

Message ID 20240818152634.17443-1-ubizjak@gmail.com (mailing list archive)
State New
Headers show
Series [v2] mm/kmemleak: Use explicit cast to cast pointer from percpu to generic AS | expand

Commit Message

Uros Bizjak Aug. 18, 2024, 3:26 p.m. UTC
Use explicit cast to cast pointer from percpu to generic address space.

The patch will avoid future build errors due to pointer address
space mismatch with enabled strict percpu address space checks.

The patch also fixes following sparse warnings:

kmemleak.c:1063:39: warning: cast removes address space '__percpu' of expression
kmemleak.c:1138:37: warning: cast removes address space '__percpu' of expression

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
---
v2: Use explicit casts instead of IS_ERR_PCPU().
---
 mm/kmemleak.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Uros Bizjak Aug. 18, 2024, 7:51 p.m. UTC | #1
On Sun, Aug 18, 2024 at 5:26 PM Uros Bizjak <ubizjak@gmail.com> wrote:
>
> Use explicit cast to cast pointer from percpu to generic address space.
>
> The patch will avoid future build errors due to pointer address
> space mismatch with enabled strict percpu address space checks.
>
> The patch also fixes following sparse warnings:
>
> kmemleak.c:1063:39: warning: cast removes address space '__percpu' of expression
> kmemleak.c:1138:37: warning: cast removes address space '__percpu' of expression
>
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>

Actually, we can do something around the lines of the attached patch
and still keep the __must_check directive active.

Please disregard this patch, a new (better) approach is in the works.

Uros.

> ---
> v2: Use explicit casts instead of IS_ERR_PCPU().
> ---
>  mm/kmemleak.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index 764b08100570..fc2e09ec48f8 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1059,8 +1059,9 @@ void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
>          * Percpu allocations are only scanned and not reported as leaks
>          * (min_count is set to 0).
>          */
> -       if (kmemleak_enabled && ptr && !IS_ERR(ptr))
> -               create_object_percpu((unsigned long)ptr, size, 0, gfp);
> +       if (kmemleak_enabled && ptr &&
> +           !IS_ERR((const void *)(__force const unsigned long)ptr))
> +               create_object_percpu((__force unsigned long)ptr, size, 0, gfp);
>  }
>  EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
>
> @@ -1134,8 +1135,9 @@ void __ref kmemleak_free_percpu(const void __percpu *ptr)
>  {
>         pr_debug("%s(0x%px)\n", __func__, ptr);
>
> -       if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
> -               delete_object_full((unsigned long)ptr, OBJECT_PERCPU);
> +       if (kmemleak_free_enabled && ptr &&
> +           !IS_ERR((const void *)(__force const unsigned long)ptr))
> +               delete_object_full((__force unsigned long)ptr, OBJECT_PERCPU);
>  }
>  EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
>
> --
> 2.42.0
>
diff --git a/include/linux/err.h b/include/linux/err.h
index b5d9bb2a2349..6dd7f00d422d 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -41,6 +41,8 @@ static inline void * __must_check ERR_PTR(long error)
 	return (void *) error;
 }
 
+#define ERR_PTR_PCPU(ptr) ({ (void __percpu *)(unsigned long)ERR_PTR(ptr); })
+
 /**
  * PTR_ERR - Extract the error code from an error pointer.
  * @ptr: An error pointer.
@@ -51,6 +53,8 @@ static inline long __must_check PTR_ERR(__force const void *ptr)
 	return (long) ptr;
 }
 
+#define PTR_ERR_PCPU(ptr) ({ PTR_ERR((void *)(__force unsigned long)ptr); })
+
 /**
  * IS_ERR - Detect an error pointer.
  * @ptr: The pointer to check.
@@ -61,6 +65,8 @@ static inline bool __must_check IS_ERR(__force const void *ptr)
 	return IS_ERR_VALUE((unsigned long)ptr);
 }
 
+#define IS_ERR_PCPU(ptr) ({ IS_ERR((void *)(__force unsigned long)ptr); })
+
 /**
  * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
  * @ptr: The pointer to check.
diff mbox series

Patch

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 764b08100570..fc2e09ec48f8 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1059,8 +1059,9 @@  void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
 	 * Percpu allocations are only scanned and not reported as leaks
 	 * (min_count is set to 0).
 	 */
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
-		create_object_percpu((unsigned long)ptr, size, 0, gfp);
+	if (kmemleak_enabled && ptr &&
+	    !IS_ERR((const void *)(__force const unsigned long)ptr))
+		create_object_percpu((__force unsigned long)ptr, size, 0, gfp);
 }
 EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
 
@@ -1134,8 +1135,9 @@  void __ref kmemleak_free_percpu(const void __percpu *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
-		delete_object_full((unsigned long)ptr, OBJECT_PERCPU);
+	if (kmemleak_free_enabled && ptr &&
+	    !IS_ERR((const void *)(__force const unsigned long)ptr))
+		delete_object_full((__force unsigned long)ptr, OBJECT_PERCPU);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free_percpu);