Skip to content

Commit a3f8a3a

Browse files
ubizjakIngo Molnar
authored andcommitted
x86/percpu: Rewrite x86_this_cpu_test_bit() and friends as macros
Rewrite the whole family of x86_this_cpu_test_bit() functions as macros, so standard __my_cpu_var() and raw_cpu_read() macros can be used on percpu variables. This approach considerably simplifies implementation of functions and also introduces standard checks on accessed percpu variables. No functional changes intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4c3677c commit a3f8a3a

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

arch/um/include/asm/cpufeature.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
3838

3939
#define this_cpu_has(bit) \
4040
(__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
41-
x86_this_cpu_test_bit(bit, \
42-
(unsigned long __percpu *)&cpu_info.x86_capability))
41+
x86_this_cpu_test_bit(bit, cpu_info.x86_capability))
4342

4443
/*
4544
* This macro is for detection of features which need kernel

arch/x86/include/asm/cpufeature.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
127127

128128
#define this_cpu_has(bit) \
129129
(__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
130-
x86_this_cpu_test_bit(bit, \
131-
(unsigned long __percpu *)&cpu_info.x86_capability))
130+
x86_this_cpu_test_bit(bit, cpu_info.x86_capability))
132131

133132
/*
134133
* This macro is for detection of features which need kernel

arch/x86/include/asm/percpu.h

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
#endif /* CONFIG_SMP */
9797

9898
#define __my_cpu_type(var) typeof(var) __percpu_seg_override
99-
#define __my_cpu_ptr(ptr) (__my_cpu_type(*ptr)*)(__force uintptr_t)(ptr)
99+
#define __my_cpu_ptr(ptr) (__my_cpu_type(*(ptr))*)(__force uintptr_t)(ptr)
100100
#define __my_cpu_var(var) (*__my_cpu_ptr(&(var)))
101101
#define __percpu_arg(x) __percpu_prefix "%" #x
102102
#define __force_percpu_arg(x) __force_percpu_prefix "%" #x
@@ -568,37 +568,29 @@ do { \
568568
#define this_cpu_read_stable_8(pcp) ({ BUILD_BUG(); (typeof(pcp))0; })
569569
#endif
570570

571-
static __always_inline bool x86_this_cpu_constant_test_bit(unsigned int nr,
572-
const unsigned long __percpu *addr)
573-
{
574-
unsigned long __percpu *a =
575-
(unsigned long __percpu *)addr + nr / BITS_PER_LONG;
571+
#define x86_this_cpu_constant_test_bit(_nr, _var) \
572+
({ \
573+
unsigned long __percpu *addr__ = \
574+
(unsigned long __percpu *)&(_var) + ((_nr) / BITS_PER_LONG); \
575+
!!((1UL << ((_nr) % BITS_PER_LONG)) & raw_cpu_read(*addr__)); \
576+
})
576577

577-
#ifdef CONFIG_X86_64
578-
return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_8(*a)) != 0;
579-
#else
580-
return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_4(*a)) != 0;
581-
#endif
582-
}
583-
584-
static inline bool x86_this_cpu_variable_test_bit(int nr,
585-
const unsigned long __percpu *addr)
586-
{
587-
bool oldbit;
588-
589-
asm volatile("btl %[nr], " __percpu_arg([var])
590-
CC_SET(c)
591-
: CC_OUT(c) (oldbit)
592-
: [var] "m" (*__my_cpu_ptr((unsigned long __percpu *)(addr))),
593-
[nr] "Ir" (nr));
594-
595-
return oldbit;
596-
}
597-
598-
#define x86_this_cpu_test_bit(nr, addr) \
599-
(__builtin_constant_p((nr)) \
600-
? x86_this_cpu_constant_test_bit((nr), (addr)) \
601-
: x86_this_cpu_variable_test_bit((nr), (addr)))
578+
#define x86_this_cpu_variable_test_bit(_nr, _var) \
579+
({ \
580+
bool oldbit; \
581+
\
582+
asm volatile("btl %[nr], " __percpu_arg([var]) \
583+
CC_SET(c) \
584+
: CC_OUT(c) (oldbit) \
585+
: [var] "m" (__my_cpu_var(_var)), \
586+
[nr] "rI" (_nr)); \
587+
oldbit; \
588+
})
589+
590+
#define x86_this_cpu_test_bit(_nr, _var) \
591+
(__builtin_constant_p(_nr) \
592+
? x86_this_cpu_constant_test_bit(_nr, _var) \
593+
: x86_this_cpu_variable_test_bit(_nr, _var))
602594

603595

604596
#include <asm-generic/percpu.h>

0 commit comments

Comments
 (0)