Skip to content

Commit 632bbfe

Browse files
Jan BlunckLinus Torvalds
authored andcommitted
[PATCH] trigger a syntax error if percpu macros are incorrectly used
get_cpu_var()/per_cpu()/__get_cpu_var() arguments must be simple identifiers. Otherwise the arch dependent implementations might break. This patch enforces the correct usage of the macros by producing a syntax error if the variable is not a simple identifier. Signed-off-by: Jan Blunck <jblunck@suse.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 0a2966b commit 632bbfe

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎include/asm-generic/percpu.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ extern unsigned long __per_cpu_offset[NR_CPUS];
1414
__attribute__((__section__(".data.percpu"))) __typeof__(type) per_cpu__##name
1515

1616
/* var is in discarded region: offset to particular copy we want */
17-
#define per_cpu(var, cpu) (*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))
17+
#define per_cpu(var, cpu) (*({ \
18+
extern int simple_indentifier_##var(void); \
19+
RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]); }))
1820
#define __get_cpu_var(var) per_cpu(var, smp_processor_id())
1921
#define __raw_get_cpu_var(var) per_cpu(var, raw_smp_processor_id())
2022

‎include/asm-s390/percpu.h‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
*/
1616
#if defined(__s390x__) && defined(MODULE)
1717

18-
#define __reloc_hide(var,offset) \
19-
(*({ unsigned long *__ptr; \
20-
asm ( "larl %0,per_cpu__"#var"@GOTENT" \
21-
: "=a" (__ptr) : "X" (per_cpu__##var) ); \
22-
(typeof(&per_cpu__##var))((*__ptr) + (offset)); }))
18+
#define __reloc_hide(var,offset) (*({ \
19+
extern int simple_indentifier_##var(void); \
20+
unsigned long *__ptr; \
21+
asm ( "larl %0,per_cpu__"#var"@GOTENT" \
22+
: "=a" (__ptr) : "X" (per_cpu__##var) ); \
23+
(typeof(&per_cpu__##var))((*__ptr) + (offset)); }))
2324

2425
#else
2526

26-
#define __reloc_hide(var, offset) \
27-
(*({ unsigned long __ptr; \
28-
asm ( "" : "=a" (__ptr) : "0" (&per_cpu__##var) ); \
29-
(typeof(&per_cpu__##var)) (__ptr + (offset)); }))
27+
#define __reloc_hide(var, offset) (*({ \
28+
extern int simple_indentifier_##var(void); \
29+
unsigned long __ptr; \
30+
asm ( "" : "=a" (__ptr) : "0" (&per_cpu__##var) ); \
31+
(typeof(&per_cpu__##var)) (__ptr + (offset)); }))
3032

3133
#endif
3234

‎include/asm-x86_64/percpu.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
__attribute__((__section__(".data.percpu"))) __typeof__(type) per_cpu__##name
2222

2323
/* var is in discarded region: offset to particular copy we want */
24-
#define per_cpu(var, cpu) (*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset(cpu)))
25-
#define __get_cpu_var(var) (*RELOC_HIDE(&per_cpu__##var, __my_cpu_offset()))
26-
#define __raw_get_cpu_var(var) (*RELOC_HIDE(&per_cpu__##var, __my_cpu_offset()))
24+
#define per_cpu(var, cpu) (*({ \
25+
extern int simple_indentifier_##var(void); \
26+
RELOC_HIDE(&per_cpu__##var, __per_cpu_offset(cpu)); }))
27+
#define __get_cpu_var(var) (*({ \
28+
extern int simple_indentifier_##var(void); \
29+
RELOC_HIDE(&per_cpu__##var, __my_cpu_offset()); }))
30+
#define __raw_get_cpu_var(var) (*({ \
31+
extern int simple_indentifier_##var(void); \
32+
RELOC_HIDE(&per_cpu__##var, __my_cpu_offset()); }))
2733

2834
/* A macro to avoid #include hell... */
2935
#define percpu_modcopy(pcpudst, src, size) \

‎include/linux/percpu.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
#define PERCPU_ENOUGH_ROOM 32768
1212
#endif
1313

14-
/* Must be an lvalue. */
15-
#define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); }))
14+
/*
15+
* Must be an lvalue. Since @var must be a simple identifier,
16+
* we force a syntax error here if it isn't.
17+
*/
18+
#define get_cpu_var(var) (*({ \
19+
extern int simple_indentifier_##var(void); \
20+
preempt_disable(); \
21+
&__get_cpu_var(var); }))
1622
#define put_cpu_var(var) preempt_enable()
1723

1824
#ifdef CONFIG_SMP

0 commit comments

Comments
 (0)