[Devel] [PATCH vz10 15/32] ms/percpu: introduce PERCPU_PTR() macro

Konstantin Khorenko khorenko at virtuozzo.com
Fri Aug 21 19:37:01 MSK 2026


From: Uros Bizjak <ubizjak at gmail.com>

Introduce PERCPU_PTR() macro to cast the percpu pointer from the percpu
address space to a generic (kernel) address space.  Use it in
per_cpu_ptr() and related SHIFT_PERCPU_PTR() macros.

Also remove common knowledge from SHIFT_PERCPU_PTR() comment, "weird cast"
is just a standard way to inform sparse of a cast from the percpu address
space to a generic address space.

Link: https://lkml.kernel.org/r/20241021080856.48746-2-ubizjak@gmail.com
Signed-off-by: Uros Bizjak <ubizjak at gmail.com>
Acked-by: Christoph Lameter <cl at linux.com>
Cc: Dennis Zhou <dennis at kernel.org>
Cc: Tejun Heo <tj at kernel.org>
Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
(cherry picked from commit 001217defda86d0d6a5a9e6cf77a6b813857e7e3)

Needed here because the RHEL10.2 base brought in the inlined
migrate_disable()/migrate_enable() from a later kernel, and the
this_rq_raw() helper they use expands to PERCPU_PTR() for the !SMP case
while the macro itself was never backported, so CONFIG_SMP=n has never
built:

  include/linux/sched.h: error: implicit declaration of function
                         'PERCPU_PTR'

VERIFY_PERCPU_PTR() cannot be used there instead: like this_cpu_ptr() it
needs the complete type of the pointee, and struct rq is deliberately
opaque in that header - which is the reason this_rq_raw() exists at all.

Conflict resolution:

  - !CONFIG_SMP hunk: this tree factors the cast out into
    VERIFY_PERCPU_PTR() instead of open-coding it in per_cpu_ptr(), so
    the conversion went there and per_cpu_ptr() is left alone.

  - dropped the stray trailing semicolon of the upstream macro.  With it,
    VERIFY_PERCPU_PTR() - which uses PERCPU_PTR() inside a statement
    expression - stops evaluating to a pointer and no !SMP build gets
    past percpu-defs.h.  The next upstream commit, dabddd687c9e ("percpu:
    cast percpu pointer in PERCPU_PTR() via unsigned long"), removes the
    semicolon anyway; taking it verbatim here would only make this commit
    unbuildable.

https://virtuozzo.atlassian.net/browse/VSTOR-134732
Feature: fix rh/sched
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 include/linux/percpu-defs.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/include/linux/percpu-defs.h b/include/linux/percpu-defs.h
index 8efce7414fad..02bcd74afc6c 100644
--- a/include/linux/percpu-defs.h
+++ b/include/linux/percpu-defs.h
@@ -220,15 +220,17 @@ do {									\
 	(void)__vpp_verify;						\
 } while (0)
 
+#define PERCPU_PTR(__p)							\
+	(typeof(*(__p)) __force __kernel *)(__p)
+
 #ifdef CONFIG_SMP
 
 /*
- * Add an offset to a pointer but keep the pointer as-is.  Use RELOC_HIDE()
- * to prevent the compiler from making incorrect assumptions about the
- * pointer value.  The weird cast keeps both GCC and sparse happy.
+ * Add an offset to a pointer.  Use RELOC_HIDE() to prevent the compiler
+ * from making incorrect assumptions about the pointer value.
  */
 #define SHIFT_PERCPU_PTR(__p, __offset)					\
-	RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset))
+	RELOC_HIDE(PERCPU_PTR(__p), (__offset))
 
 #define per_cpu_ptr(ptr, cpu)						\
 ({									\
@@ -257,7 +259,7 @@ do {									\
 #define VERIFY_PERCPU_PTR(__p)						\
 ({									\
 	__verify_pcpu_ptr(__p);						\
-	(typeof(*(__p)) __kernel __force *)(__p);			\
+	PERCPU_PTR(__p);						\
 })
 
 #define per_cpu_ptr(ptr, cpu)	({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
-- 
2.47.1



More information about the Devel mailing list