Skip to contentSkip to conversation

x86/percpu: Use __force to cast from __percpu address space

PATCH In review Uros Bizjak 2 participants 4 messages May 14, 2024 – May 15, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/percpu: Use __force to cast from __percpu address space

commit a55c1fdad5f61b4bfe42319694b23671a758cb28 upstream.

Fix Sparse warning when casting from __percpu address space by using __force in the cast. x86 named address spaces are not considered to be subspaces of the generic (flat) address space, so explicit casts are required to convert pointers between these address spaces and the generic address space (the application should cast to uintptr_t and apply the segment base offset). The cast to uintptr_t removes __percpu address space tag and Sparse reports:

warning: cast removes address space '__percpu' of expression

Use __force to inform Sparse that the cast is intentional.

The patch deviates from upstream commit due to the unification of arch_raw_cpu_ptr() defines in the commit:

4e5b0e8003df ("x86/percpu: Unify arch_raw_cpu_ptr() defines").

---
 arch/x86/include/asm/percpu.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/percpu.h +4 -4
index 44958ebaf626..66ed36b8cdb4 100644
@@ -70,7 +70,7 @@
 	unsigned long tcp_ptr__;				\
 	tcp_ptr__ = __raw_cpu_read(, this_cpu_off);		\
 								\
-	tcp_ptr__ += (unsigned long)(ptr);			\
+	tcp_ptr__ += (__force unsigned long)(ptr);		\
 	(typeof(*(ptr)) __kernel __force *)tcp_ptr__;		\
 })
 #else /* CONFIG_USE_X86_SEG_SUPPORT */
@@ -85,7 +85,7 @@
 	     : "=r" (tcp_ptr__)					\
 	     : "m" (__my_cpu_var(this_cpu_off)));		\
 								\
-	tcp_ptr__ += (unsigned long)(ptr);			\
+	tcp_ptr__ += (__force unsigned long)(ptr);		\
 	(typeof(*(ptr)) __kernel __force *)tcp_ptr__;		\
 })
 #endif /* CONFIG_USE_X86_SEG_SUPPORT */
@@ -102,8 +102,8 @@
 #endif /* CONFIG_SMP */
 
 #define __my_cpu_type(var)	typeof(var) __percpu_seg_override
-#define __my_cpu_ptr(ptr)	(__my_cpu_type(*ptr) *)(uintptr_t)(ptr)
-#define __my_cpu_var(var)	(*__my_cpu_ptr(&var))
+#define __my_cpu_ptr(ptr)	(__my_cpu_type(*(ptr))*)(__force uintptr_t)(ptr)
+#define __my_cpu_var(var)	(*__my_cpu_ptr(&(var)))
 #define __percpu_arg(x)		__percpu_prefix "%" #x
 #define __force_percpu_arg(x)	__force_percpu_prefix "%" #x
 
Signature · 1 line
2.45.0
maintainer
#1 lore raw
Quoted 10 lines · Uros Bizjak

commit a55c1fdad5f61b4bfe42319694b23671a758cb28 upstream.

Fix Sparse warning when casting from __percpu address space by using __force in the cast. x86 named address spaces are not considered to be subspaces of the generic (flat) address space, so explicit casts are required to convert pointers between these address spaces and the generic address space (the application should cast to uintptr_t and apply the segment base offset). The cast to uintptr_t removes __percpu address space tag and Sparse reports:

Quoted 3 lines

warning: cast removes address space '__percpu' of expression

Use __force to inform Sparse that the cast is intentional.

Why is a fix for sparse required for stable kernels?

Quoted 11 lines

The patch deviates from upstream commit due to the unification of arch_raw_cpu_ptr() defines in the commit:

4e5b0e8003df ("x86/percpu: Unify arch_raw_cpu_ptr() defines").

Quoted 3 lines
---
 arch/x86/include/asm/percpu.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

And also, what kernel version(s) is this for?

I don't see this in any released kernels yet either, is that intentional?

thanks,

greg k-h

#2 lore raw
Quoted 12 lines · Greg KH
Quoted 10 lines · Uros Bizjak

commit a55c1fdad5f61b4bfe42319694b23671a758cb28 upstream.

Fix Sparse warning when casting from __percpu address space by using __force in the cast. x86 named address spaces are not considered to be subspaces of the generic (flat) address space, so explicit casts are required to convert pointers between these address spaces and the generic address space (the application should cast to uintptr_t and apply the segment base offset). The cast to uintptr_t removes __percpu address space tag and Sparse reports:

Quoted 5 lines
Quoted 3 lines

warning: cast removes address space '__percpu' of expression

Use __force to inform Sparse that the cast is intentional.

Why is a fix for sparse required for stable kernels?

Named address spaces is a new feature in the 6.8 kernel. When someone compiles this version with Sparse (and certain sparse parameters), it will spew many sparse warnings. We have fixed this in the tip tree (so, the fix will be in v6.9), but the tip tree diverted from the mainline in this area, so it was not possible to fix the issue in 6.8 via "urgent" tip branches.

I thought that the fix falls into "some “oh, that’s not good” issue" category (due to many sparse warnings). Also, the fix is straightforward with a low possibility of breaking something.

Quoted 15 lines
Quoted 11 lines

The patch deviates from upstream commit due to the unification of arch_raw_cpu_ptr() defines in the commit:

4e5b0e8003df ("x86/percpu: Unify arch_raw_cpu_ptr() defines").

Quoted 3 lines
---
 arch/x86/include/asm/percpu.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 1 line

And also, what kernel version(s) is this for?

As instructed in the "Procedure for submitting patches to the -stable tree", it is stated at:

Quoted 3 lines

I don't see this in any released kernels yet either, is that intentional?

The original fix was committed to the current mainline and will be in v6.9, but please also see the above reasoning. However, it is your call, so if you think that this issue is not problematic for a stable kernel, it's also OK with me.

Thanks,
Uros.

#3 lore raw
Quoted 46 lines · Uros Bizjak
Quoted 12 lines · Greg KH
Quoted 10 lines · Uros Bizjak

commit a55c1fdad5f61b4bfe42319694b23671a758cb28 upstream.

Fix Sparse warning when casting from __percpu address space by using __force in the cast. x86 named address spaces are not considered to be subspaces of the generic (flat) address space, so explicit casts are required to convert pointers between these address spaces and the generic address space (the application should cast to uintptr_t and apply the segment base offset). The cast to uintptr_t removes __percpu address space tag and Sparse reports:

Quoted 5 lines
Quoted 3 lines

warning: cast removes address space '__percpu' of expression

Use __force to inform Sparse that the cast is intentional.

Why is a fix for sparse required for stable kernels?

Named address spaces is a new feature in the 6.8 kernel. When someone compiles this version with Sparse (and certain sparse parameters), it will spew many sparse warnings. We have fixed this in the tip tree (so, the fix will be in v6.9), but the tip tree diverted from the mainline in this area, so it was not possible to fix the issue in 6.8 via "urgent" tip branches.

I thought that the fix falls into "some “oh, that’s not good” issue" category (due to many sparse warnings). Also, the fix is straightforward with a low possibility of breaking something.

Quoted 15 lines
Quoted 14 lines

The patch deviates from upstream commit due to the unification of arch_raw_cpu_ptr() defines in the commit:

4e5b0e8003df ("x86/percpu: Unify arch_raw_cpu_ptr() defines").

---
 arch/x86/include/asm/percpu.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 1 line

And also, what kernel version(s) is this for?

As instructed in the "Procedure for submitting patches to the -stable tree", it is stated at:

Oh, I mixed up kernel versions... this should apply to just released v6.9.

Quoted 6 lines
Quoted 3 lines

I don't see this in any released kernels yet either, is that intentional?

The original fix was committed to the current mainline and will be in v6.9, but please also see the above reasoning. However, it is your

The original fix will be in v6.10, I mixed up the number ...

Quoted 2 lines

call, so if you think that this issue is not problematic for a stable kernel, it's also OK with me.

Thanks,
Uros.