Skip to content

Commit 188a814

Browse files
Christoph Lametertorvalds
authored andcommitted
percpu: add preemption checks to __this_cpu ops
We define a check function in order to avoid trouble with the include files. Then the higher level __this_cpu macros are modified to invoke the preemption check. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Christoph Lameter <cl@linux.com> Acked-by: Ingo Molnar <mingo@kernel.org> Cc: Tejun Heo <tj@kernel.org> Tested-by: Grygorii Strashko <grygorii.strashko@ti.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 293b6a4 commit 188a814

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

‎include/linux/percpu.h‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
173173

174174
extern void __bad_size_call_parameter(void);
175175

176+
#ifdef CONFIG_DEBUG_PREEMPT
177+
extern void __this_cpu_preempt_check(const char *op);
178+
#else
179+
static inline void __this_cpu_preempt_check(const char *op) { }
180+
#endif
181+
176182
#define __pcpu_size_call_return(stem, variable) \
177183
({ typeof(variable) pscr_ret__; \
178184
__verify_pcpu_ptr(&(variable)); \
@@ -725,18 +731,24 @@ do { \
725731

726732
/*
727733
* Generic percpu operations for context that are safe from preemption/interrupts.
728-
* Checks will be added here soon.
729734
*/
730735
#ifndef __this_cpu_read
731-
# define __this_cpu_read(pcp) __pcpu_size_call_return(raw_cpu_read_, (pcp))
736+
# define __this_cpu_read(pcp) \
737+
(__this_cpu_preempt_check("read"),__pcpu_size_call_return(raw_cpu_read_, (pcp)))
732738
#endif
733739

734740
#ifndef __this_cpu_write
735-
# define __this_cpu_write(pcp, val) __pcpu_size_call(raw_cpu_write_, (pcp), (val))
741+
# define __this_cpu_write(pcp, val) \
742+
do { __this_cpu_preempt_check("write"); \
743+
__pcpu_size_call(raw_cpu_write_, (pcp), (val)); \
744+
} while (0)
736745
#endif
737746

738747
#ifndef __this_cpu_add
739-
# define __this_cpu_add(pcp, val) __pcpu_size_call(raw_cpu_add_, (pcp), (val))
748+
# define __this_cpu_add(pcp, val) \
749+
do { __this_cpu_preempt_check("add"); \
750+
__pcpu_size_call(raw_cpu_add_, (pcp), (val)); \
751+
} while (0)
740752
#endif
741753

742754
#ifndef __this_cpu_sub
@@ -752,16 +764,23 @@ do { \
752764
#endif
753765

754766
#ifndef __this_cpu_and
755-
# define __this_cpu_and(pcp, val) __pcpu_size_call(raw_cpu_and_, (pcp), (val))
767+
# define __this_cpu_and(pcp, val) \
768+
do { __this_cpu_preempt_check("and"); \
769+
__pcpu_size_call(raw_cpu_and_, (pcp), (val)); \
770+
} while (0)
771+
756772
#endif
757773

758774
#ifndef __this_cpu_or
759-
# define __this_cpu_or(pcp, val) __pcpu_size_call(raw_cpu_or_, (pcp), (val))
775+
# define __this_cpu_or(pcp, val) \
776+
do { __this_cpu_preempt_check("or"); \
777+
__pcpu_size_call(raw_cpu_or_, (pcp), (val)); \
778+
} while (0)
760779
#endif
761780

762781
#ifndef __this_cpu_add_return
763782
# define __this_cpu_add_return(pcp, val) \
764-
__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
783+
(__this_cpu_preempt_check("add_return"),__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val))
765784
#endif
766785

767786
#define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val))
@@ -770,17 +789,17 @@ do { \
770789

771790
#ifndef __this_cpu_xchg
772791
# define __this_cpu_xchg(pcp, nval) \
773-
__pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval)
792+
(__this_cpu_preempt_check("xchg"),__pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval))
774793
#endif
775794

776795
#ifndef __this_cpu_cmpxchg
777796
# define __this_cpu_cmpxchg(pcp, oval, nval) \
778-
__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
797+
(__this_cpu_preempt_check("cmpxchg"),__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval))
779798
#endif
780799

781800
#ifndef __this_cpu_cmpxchg_double
782801
# define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
783-
__pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
802+
(__this_cpu_preempt_check("cmpxchg_double"),__pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2)))
784803
#endif
785804

786805
#endif /* __LINUX_PERCPU_H */

‎lib/smp_processor_id.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <linux/kallsyms.h>
88
#include <linux/sched.h>
99

10-
notrace unsigned int debug_smp_processor_id(void)
10+
notrace static unsigned int check_preemption_disabled(const char *what1,
11+
const char *what2)
1112
{
1213
int this_cpu = raw_smp_processor_id();
1314

@@ -38,9 +39,9 @@ notrace unsigned int debug_smp_processor_id(void)
3839
if (!printk_ratelimit())
3940
goto out_enable;
4041

41-
printk(KERN_ERR "BUG: using smp_processor_id() in preemptible [%08x] "
42-
"code: %s/%d\n",
43-
preempt_count() - 1, current->comm, current->pid);
42+
printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
43+
what1, what2, preempt_count() - 1, current->comm, current->pid);
44+
4445
print_symbol("caller is %s\n", (long)__builtin_return_address(0));
4546
dump_stack();
4647

@@ -50,5 +51,14 @@ notrace unsigned int debug_smp_processor_id(void)
5051
return this_cpu;
5152
}
5253

54+
notrace unsigned int debug_smp_processor_id(void)
55+
{
56+
return check_preemption_disabled("smp_processor_id", "");
57+
}
5358
EXPORT_SYMBOL(debug_smp_processor_id);
5459

60+
notrace void __this_cpu_preempt_check(const char *op)
61+
{
62+
check_preemption_disabled("__this_cpu_", op);
63+
}
64+
EXPORT_SYMBOL(__this_cpu_preempt_check);

0 commit comments

Comments
 (0)