Skip to content

Commit e0fdb0e

Browse files
rustyrussellhtejun
authored andcommitted
percpu: add __percpu for sparse.
We have to make __kernel "__attribute__((address_space(0)))" so we can cast to it. tj: * put_cpu_var() update. * Annotations added to dynamic allocator interface. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent f7b64fe commit e0fdb0e

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎include/asm-generic/percpu.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ extern unsigned long __per_cpu_offset[NR_CPUS];
4141
* Only S390 provides its own means of moving the pointer.
4242
*/
4343
#ifndef SHIFT_PERCPU_PTR
44-
#define SHIFT_PERCPU_PTR(__p, __offset) RELOC_HIDE((__p), (__offset))
44+
/* Weird cast keeps both GCC and sparse happy. */
45+
#define SHIFT_PERCPU_PTR(__p, __offset) \
46+
RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset))
4547
#endif
4648

4749
/*

‎include/linux/compiler.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#ifdef __CHECKER__
77
# define __user __attribute__((noderef, address_space(1)))
8-
# define __kernel /* default address space */
8+
# define __kernel __attribute__((address_space(0)))
99
# define __safe __attribute__((safe))
1010
# define __force __attribute__((force))
1111
# define __nocast __attribute__((nocast))
@@ -15,6 +15,7 @@
1515
# define __acquire(x) __context__(x,1)
1616
# define __release(x) __context__(x,-1)
1717
# define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
18+
# define __percpu __attribute__((noderef, address_space(3)))
1819
extern void __chk_user_ptr(const volatile void __user *);
1920
extern void __chk_io_ptr(const volatile void __iomem *);
2021
#else
@@ -32,6 +33,7 @@ extern void __chk_io_ptr(const volatile void __iomem *);
3233
# define __acquire(x) (void)0
3334
# define __release(x) (void)0
3435
# define __cond_lock(x,c) (c)
36+
# define __percpu
3537
#endif
3638

3739
#ifdef __KERNEL__

‎include/linux/percpu-defs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* that section.
1313
*/
1414
#define __PCPU_ATTRS(sec) \
15-
__attribute__((section(PER_CPU_BASE_SECTION sec))) \
15+
__percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \
1616
PER_CPU_ATTRIBUTES
1717

1818
#define __PCPU_DUMMY_ATTRS \

‎include/linux/percpu.h‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
preempt_disable(); \
3131
&__get_cpu_var(var); }))
3232

33+
/*
34+
* The weird & is necessary because sparse considers (void)(var) to be
35+
* a direct dereference of percpu variable (var).
36+
*/
3337
#define put_cpu_var(var) do { \
34-
(void)(var); \
38+
(void)&(var); \
3539
preempt_enable(); \
3640
} while (0)
3741

@@ -130,9 +134,9 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,
130134
*/
131135
#define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
132136

133-
extern void *__alloc_reserved_percpu(size_t size, size_t align);
134-
extern void *__alloc_percpu(size_t size, size_t align);
135-
extern void free_percpu(void *__pdata);
137+
extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
138+
extern void __percpu *__alloc_percpu(size_t size, size_t align);
139+
extern void free_percpu(void __percpu *__pdata);
136140

137141
#ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
138142
extern void __init setup_per_cpu_areas(void);
@@ -142,7 +146,7 @@ extern void __init setup_per_cpu_areas(void);
142146

143147
#define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
144148

145-
static inline void *__alloc_percpu(size_t size, size_t align)
149+
static inline void __percpu *__alloc_percpu(size_t size, size_t align)
146150
{
147151
/*
148152
* Can't easily make larger alignment work with kmalloc. WARN
@@ -153,7 +157,7 @@ static inline void *__alloc_percpu(size_t size, size_t align)
153157
return kzalloc(size, GFP_KERNEL);
154158
}
155159

156-
static inline void free_percpu(void *p)
160+
static inline void free_percpu(void __percpu *p)
157161
{
158162
kfree(p);
159163
}
@@ -168,7 +172,7 @@ static inline void *pcpu_lpage_remapped(void *kaddr)
168172
#endif /* CONFIG_SMP */
169173

170174
#define alloc_percpu(type) \
171-
(typeof(type) *)__alloc_percpu(sizeof(type), __alignof__(type))
175+
(typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
172176

173177
/*
174178
* Optional methods for optimized non-lvalue per-cpu variable access.

0 commit comments

Comments
 (0)