1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/percpu-defs.h - basic definitions for percpu areas
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * DO NOT INCLUDE DIRECTLY OUTSIDE PERCPU IMPLEMENTATION PROPER.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file is separate from linux/percpu.h to avoid cyclic inclusion
8*4882a593Smuzhiyun * dependency from arch header files. Only to be included from
9*4882a593Smuzhiyun * asm/percpu.h.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This file includes macros necessary to declare percpu sections and
12*4882a593Smuzhiyun * variables, and definitions of percpu accessors and operations. It
13*4882a593Smuzhiyun * should provide enough percpu features to arch header files even when
14*4882a593Smuzhiyun * they can only include asm/percpu.h to avoid cyclic inclusion dependency.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #ifndef _LINUX_PERCPU_DEFS_H
18*4882a593Smuzhiyun #define _LINUX_PERCPU_DEFS_H
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #ifdef CONFIG_SMP
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #ifdef MODULE
23*4882a593Smuzhiyun #define PER_CPU_SHARED_ALIGNED_SECTION ""
24*4882a593Smuzhiyun #define PER_CPU_ALIGNED_SECTION ""
25*4882a593Smuzhiyun #else
26*4882a593Smuzhiyun #define PER_CPU_SHARED_ALIGNED_SECTION "..shared_aligned"
27*4882a593Smuzhiyun #define PER_CPU_ALIGNED_SECTION "..shared_aligned"
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun #define PER_CPU_FIRST_SECTION "..first"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #else
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define PER_CPU_SHARED_ALIGNED_SECTION ""
34*4882a593Smuzhiyun #define PER_CPU_ALIGNED_SECTION "..shared_aligned"
35*4882a593Smuzhiyun #define PER_CPU_FIRST_SECTION ""
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * Base implementations of per-CPU variable declarations and definitions, where
41*4882a593Smuzhiyun * the section in which the variable is to be placed is provided by the
42*4882a593Smuzhiyun * 'sec' argument. This may be used to affect the parameters governing the
43*4882a593Smuzhiyun * variable's storage.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * NOTE! The sections for the DECLARE and for the DEFINE must match, lest
46*4882a593Smuzhiyun * linkage errors occur due the compiler generating the wrong code to access
47*4882a593Smuzhiyun * that section.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun #define __PCPU_ATTRS(sec) \
50*4882a593Smuzhiyun __percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \
51*4882a593Smuzhiyun PER_CPU_ATTRIBUTES
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define __PCPU_DUMMY_ATTRS \
54*4882a593Smuzhiyun __section(".discard") __attribute__((unused))
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * s390 and alpha modules require percpu variables to be defined as
58*4882a593Smuzhiyun * weak to force the compiler to generate GOT based external
59*4882a593Smuzhiyun * references for them. This is necessary because percpu sections
60*4882a593Smuzhiyun * will be located outside of the usually addressable area.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * This definition puts the following two extra restrictions when
63*4882a593Smuzhiyun * defining percpu variables.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * 1. The symbol must be globally unique, even the static ones.
66*4882a593Smuzhiyun * 2. Static percpu variables cannot be defined inside a function.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * Archs which need weak percpu definitions should define
69*4882a593Smuzhiyun * ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * To ensure that the generic code observes the above two
72*4882a593Smuzhiyun * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak
73*4882a593Smuzhiyun * definition is used for all cases.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #if defined(ARCH_NEEDS_WEAK_PER_CPU) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU)
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * __pcpu_scope_* dummy variable is used to enforce scope. It
78*4882a593Smuzhiyun * receives the static modifier when it's used in front of
79*4882a593Smuzhiyun * DEFINE_PER_CPU() and will trigger build failure if
80*4882a593Smuzhiyun * DECLARE_PER_CPU() is used for the same variable.
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * __pcpu_unique_* dummy variable is used to enforce symbol uniqueness
83*4882a593Smuzhiyun * such that hidden weak symbol collision, which will cause unrelated
84*4882a593Smuzhiyun * variables to share the same address, can be detected during build.
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun #define DECLARE_PER_CPU_SECTION(type, name, sec) \
87*4882a593Smuzhiyun extern __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
88*4882a593Smuzhiyun extern __PCPU_ATTRS(sec) __typeof__(type) name
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define DEFINE_PER_CPU_SECTION(type, name, sec) \
91*4882a593Smuzhiyun __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
92*4882a593Smuzhiyun extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
93*4882a593Smuzhiyun __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
94*4882a593Smuzhiyun extern __PCPU_ATTRS(sec) __typeof__(type) name; \
95*4882a593Smuzhiyun __PCPU_ATTRS(sec) __weak __typeof__(type) name
96*4882a593Smuzhiyun #else
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * Normal declaration and definition macros.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun #define DECLARE_PER_CPU_SECTION(type, name, sec) \
101*4882a593Smuzhiyun extern __PCPU_ATTRS(sec) __typeof__(type) name
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun #define DEFINE_PER_CPU_SECTION(type, name, sec) \
104*4882a593Smuzhiyun __PCPU_ATTRS(sec) __typeof__(type) name
105*4882a593Smuzhiyun #endif
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * Variant on the per-CPU variable declaration/definition theme used for
109*4882a593Smuzhiyun * ordinary per-CPU variables.
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun #define DECLARE_PER_CPU(type, name) \
112*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, "")
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun #define DEFINE_PER_CPU(type, name) \
115*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, "")
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * Declaration/definition used for per-CPU variables that must come first in
119*4882a593Smuzhiyun * the set of variables.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun #define DECLARE_PER_CPU_FIRST(type, name) \
122*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #define DEFINE_PER_CPU_FIRST(type, name) \
125*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION)
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Declaration/definition used for per-CPU variables that must be cacheline
129*4882a593Smuzhiyun * aligned under SMP conditions so that, whilst a particular instance of the
130*4882a593Smuzhiyun * data corresponds to a particular CPU, inefficiencies due to direct access by
131*4882a593Smuzhiyun * other CPUs are reduced by preventing the data from unnecessarily spanning
132*4882a593Smuzhiyun * cachelines.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * An example of this would be statistical data, where each CPU's set of data
135*4882a593Smuzhiyun * is updated by that CPU alone, but the data from across all CPUs is collated
136*4882a593Smuzhiyun * by a CPU processing a read from a proc file.
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun #define DECLARE_PER_CPU_SHARED_ALIGNED(type, name) \
139*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
140*4882a593Smuzhiyun ____cacheline_aligned_in_smp
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \
143*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
144*4882a593Smuzhiyun ____cacheline_aligned_in_smp
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun #define DECLARE_PER_CPU_ALIGNED(type, name) \
147*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
148*4882a593Smuzhiyun ____cacheline_aligned
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun #define DEFINE_PER_CPU_ALIGNED(type, name) \
151*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \
152*4882a593Smuzhiyun ____cacheline_aligned
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Declaration/definition used for per-CPU variables that must be page aligned.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun #define DECLARE_PER_CPU_PAGE_ALIGNED(type, name) \
158*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, "..page_aligned") \
159*4882a593Smuzhiyun __aligned(PAGE_SIZE)
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
162*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \
163*4882a593Smuzhiyun __aligned(PAGE_SIZE)
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * Declaration/definition used for per-CPU variables that must be read mostly.
167*4882a593Smuzhiyun */
168*4882a593Smuzhiyun #define DECLARE_PER_CPU_READ_MOSTLY(type, name) \
169*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, "..read_mostly")
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun #define DEFINE_PER_CPU_READ_MOSTLY(type, name) \
172*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, "..read_mostly")
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /*
175*4882a593Smuzhiyun * Declaration/definition used for per-CPU variables that should be accessed
176*4882a593Smuzhiyun * as decrypted when memory encryption is enabled in the guest.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun #ifdef CONFIG_AMD_MEM_ENCRYPT
179*4882a593Smuzhiyun #define DECLARE_PER_CPU_DECRYPTED(type, name) \
180*4882a593Smuzhiyun DECLARE_PER_CPU_SECTION(type, name, "..decrypted")
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun #define DEFINE_PER_CPU_DECRYPTED(type, name) \
183*4882a593Smuzhiyun DEFINE_PER_CPU_SECTION(type, name, "..decrypted")
184*4882a593Smuzhiyun #else
185*4882a593Smuzhiyun #define DEFINE_PER_CPU_DECRYPTED(type, name) DEFINE_PER_CPU(type, name)
186*4882a593Smuzhiyun #endif
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun * Intermodule exports for per-CPU variables. sparse forgets about
190*4882a593Smuzhiyun * address space across EXPORT_SYMBOL(), change EXPORT_SYMBOL() to
191*4882a593Smuzhiyun * noop if __CHECKER__.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun #ifndef __CHECKER__
194*4882a593Smuzhiyun #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(var)
195*4882a593Smuzhiyun #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(var)
196*4882a593Smuzhiyun #else
197*4882a593Smuzhiyun #define EXPORT_PER_CPU_SYMBOL(var)
198*4882a593Smuzhiyun #define EXPORT_PER_CPU_SYMBOL_GPL(var)
199*4882a593Smuzhiyun #endif
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun * Accessors and operations.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun #ifndef __ASSEMBLY__
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun * __verify_pcpu_ptr() verifies @ptr is a percpu pointer without evaluating
208*4882a593Smuzhiyun * @ptr and is invoked once before a percpu area is accessed by all
209*4882a593Smuzhiyun * accessors and operations. This is performed in the generic part of
210*4882a593Smuzhiyun * percpu and arch overrides don't need to worry about it; however, if an
211*4882a593Smuzhiyun * arch wants to implement an arch-specific percpu accessor or operation,
212*4882a593Smuzhiyun * it may use __verify_pcpu_ptr() to verify the parameters.
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * + 0 is required in order to convert the pointer type from a
215*4882a593Smuzhiyun * potential array type to a pointer to a single item of the array.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun #define __verify_pcpu_ptr(ptr) \
218*4882a593Smuzhiyun do { \
219*4882a593Smuzhiyun const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
220*4882a593Smuzhiyun (void)__vpp_verify; \
221*4882a593Smuzhiyun } while (0)
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun #ifdef CONFIG_SMP
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /*
226*4882a593Smuzhiyun * Add an offset to a pointer but keep the pointer as-is. Use RELOC_HIDE()
227*4882a593Smuzhiyun * to prevent the compiler from making incorrect assumptions about the
228*4882a593Smuzhiyun * pointer value. The weird cast keeps both GCC and sparse happy.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun #define SHIFT_PERCPU_PTR(__p, __offset) \
231*4882a593Smuzhiyun RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset))
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun #define per_cpu_ptr(ptr, cpu) \
234*4882a593Smuzhiyun ({ \
235*4882a593Smuzhiyun __verify_pcpu_ptr(ptr); \
236*4882a593Smuzhiyun SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu))); \
237*4882a593Smuzhiyun })
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun #define raw_cpu_ptr(ptr) \
240*4882a593Smuzhiyun ({ \
241*4882a593Smuzhiyun __verify_pcpu_ptr(ptr); \
242*4882a593Smuzhiyun arch_raw_cpu_ptr(ptr); \
243*4882a593Smuzhiyun })
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_PREEMPT
246*4882a593Smuzhiyun #define this_cpu_ptr(ptr) \
247*4882a593Smuzhiyun ({ \
248*4882a593Smuzhiyun __verify_pcpu_ptr(ptr); \
249*4882a593Smuzhiyun SHIFT_PERCPU_PTR(ptr, my_cpu_offset); \
250*4882a593Smuzhiyun })
251*4882a593Smuzhiyun #else
252*4882a593Smuzhiyun #define this_cpu_ptr(ptr) raw_cpu_ptr(ptr)
253*4882a593Smuzhiyun #endif
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun #else /* CONFIG_SMP */
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun #define VERIFY_PERCPU_PTR(__p) \
258*4882a593Smuzhiyun ({ \
259*4882a593Smuzhiyun __verify_pcpu_ptr(__p); \
260*4882a593Smuzhiyun (typeof(*(__p)) __kernel __force *)(__p); \
261*4882a593Smuzhiyun })
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
264*4882a593Smuzhiyun #define raw_cpu_ptr(ptr) per_cpu_ptr(ptr, 0)
265*4882a593Smuzhiyun #define this_cpu_ptr(ptr) raw_cpu_ptr(ptr)
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun #endif /* CONFIG_SMP */
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun #define per_cpu(var, cpu) (*per_cpu_ptr(&(var), cpu))
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /*
272*4882a593Smuzhiyun * Must be an lvalue. Since @var must be a simple identifier,
273*4882a593Smuzhiyun * we force a syntax error here if it isn't.
274*4882a593Smuzhiyun */
275*4882a593Smuzhiyun #define get_cpu_var(var) \
276*4882a593Smuzhiyun (*({ \
277*4882a593Smuzhiyun preempt_disable(); \
278*4882a593Smuzhiyun this_cpu_ptr(&var); \
279*4882a593Smuzhiyun }))
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /*
282*4882a593Smuzhiyun * The weird & is necessary because sparse considers (void)(var) to be
283*4882a593Smuzhiyun * a direct dereference of percpu variable (var).
284*4882a593Smuzhiyun */
285*4882a593Smuzhiyun #define put_cpu_var(var) \
286*4882a593Smuzhiyun do { \
287*4882a593Smuzhiyun (void)&(var); \
288*4882a593Smuzhiyun preempt_enable(); \
289*4882a593Smuzhiyun } while (0)
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun #define get_cpu_ptr(var) \
292*4882a593Smuzhiyun ({ \
293*4882a593Smuzhiyun preempt_disable(); \
294*4882a593Smuzhiyun this_cpu_ptr(var); \
295*4882a593Smuzhiyun })
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun #define put_cpu_ptr(var) \
298*4882a593Smuzhiyun do { \
299*4882a593Smuzhiyun (void)(var); \
300*4882a593Smuzhiyun preempt_enable(); \
301*4882a593Smuzhiyun } while (0)
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * Branching function to split up a function into a set of functions that
305*4882a593Smuzhiyun * are called for different scalar sizes of the objects handled.
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun extern void __bad_size_call_parameter(void);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_PREEMPT
311*4882a593Smuzhiyun extern void __this_cpu_preempt_check(const char *op);
312*4882a593Smuzhiyun #else
__this_cpu_preempt_check(const char * op)313*4882a593Smuzhiyun static inline void __this_cpu_preempt_check(const char *op) { }
314*4882a593Smuzhiyun #endif
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun #define __pcpu_size_call_return(stem, variable) \
317*4882a593Smuzhiyun ({ \
318*4882a593Smuzhiyun typeof(variable) pscr_ret__; \
319*4882a593Smuzhiyun __verify_pcpu_ptr(&(variable)); \
320*4882a593Smuzhiyun switch(sizeof(variable)) { \
321*4882a593Smuzhiyun case 1: pscr_ret__ = stem##1(variable); break; \
322*4882a593Smuzhiyun case 2: pscr_ret__ = stem##2(variable); break; \
323*4882a593Smuzhiyun case 4: pscr_ret__ = stem##4(variable); break; \
324*4882a593Smuzhiyun case 8: pscr_ret__ = stem##8(variable); break; \
325*4882a593Smuzhiyun default: \
326*4882a593Smuzhiyun __bad_size_call_parameter(); break; \
327*4882a593Smuzhiyun } \
328*4882a593Smuzhiyun pscr_ret__; \
329*4882a593Smuzhiyun })
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun #define __pcpu_size_call_return2(stem, variable, ...) \
332*4882a593Smuzhiyun ({ \
333*4882a593Smuzhiyun typeof(variable) pscr2_ret__; \
334*4882a593Smuzhiyun __verify_pcpu_ptr(&(variable)); \
335*4882a593Smuzhiyun switch(sizeof(variable)) { \
336*4882a593Smuzhiyun case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break; \
337*4882a593Smuzhiyun case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break; \
338*4882a593Smuzhiyun case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break; \
339*4882a593Smuzhiyun case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break; \
340*4882a593Smuzhiyun default: \
341*4882a593Smuzhiyun __bad_size_call_parameter(); break; \
342*4882a593Smuzhiyun } \
343*4882a593Smuzhiyun pscr2_ret__; \
344*4882a593Smuzhiyun })
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /*
347*4882a593Smuzhiyun * Special handling for cmpxchg_double. cmpxchg_double is passed two
348*4882a593Smuzhiyun * percpu variables. The first has to be aligned to a double word
349*4882a593Smuzhiyun * boundary and the second has to follow directly thereafter.
350*4882a593Smuzhiyun * We enforce this on all architectures even if they don't support
351*4882a593Smuzhiyun * a double cmpxchg instruction, since it's a cheap requirement, and it
352*4882a593Smuzhiyun * avoids breaking the requirement for architectures with the instruction.
353*4882a593Smuzhiyun */
354*4882a593Smuzhiyun #define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...) \
355*4882a593Smuzhiyun ({ \
356*4882a593Smuzhiyun bool pdcrb_ret__; \
357*4882a593Smuzhiyun __verify_pcpu_ptr(&(pcp1)); \
358*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2)); \
359*4882a593Smuzhiyun VM_BUG_ON((unsigned long)(&(pcp1)) % (2 * sizeof(pcp1))); \
360*4882a593Smuzhiyun VM_BUG_ON((unsigned long)(&(pcp2)) != \
361*4882a593Smuzhiyun (unsigned long)(&(pcp1)) + sizeof(pcp1)); \
362*4882a593Smuzhiyun switch(sizeof(pcp1)) { \
363*4882a593Smuzhiyun case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break; \
364*4882a593Smuzhiyun case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break; \
365*4882a593Smuzhiyun case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break; \
366*4882a593Smuzhiyun case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break; \
367*4882a593Smuzhiyun default: \
368*4882a593Smuzhiyun __bad_size_call_parameter(); break; \
369*4882a593Smuzhiyun } \
370*4882a593Smuzhiyun pdcrb_ret__; \
371*4882a593Smuzhiyun })
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun #define __pcpu_size_call(stem, variable, ...) \
374*4882a593Smuzhiyun do { \
375*4882a593Smuzhiyun __verify_pcpu_ptr(&(variable)); \
376*4882a593Smuzhiyun switch(sizeof(variable)) { \
377*4882a593Smuzhiyun case 1: stem##1(variable, __VA_ARGS__);break; \
378*4882a593Smuzhiyun case 2: stem##2(variable, __VA_ARGS__);break; \
379*4882a593Smuzhiyun case 4: stem##4(variable, __VA_ARGS__);break; \
380*4882a593Smuzhiyun case 8: stem##8(variable, __VA_ARGS__);break; \
381*4882a593Smuzhiyun default: \
382*4882a593Smuzhiyun __bad_size_call_parameter();break; \
383*4882a593Smuzhiyun } \
384*4882a593Smuzhiyun } while (0)
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /*
387*4882a593Smuzhiyun * this_cpu operations (C) 2008-2013 Christoph Lameter <cl@linux.com>
388*4882a593Smuzhiyun *
389*4882a593Smuzhiyun * Optimized manipulation for memory allocated through the per cpu
390*4882a593Smuzhiyun * allocator or for addresses of per cpu variables.
391*4882a593Smuzhiyun *
392*4882a593Smuzhiyun * These operation guarantee exclusivity of access for other operations
393*4882a593Smuzhiyun * on the *same* processor. The assumption is that per cpu data is only
394*4882a593Smuzhiyun * accessed by a single processor instance (the current one).
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * The arch code can provide optimized implementation by defining macros
397*4882a593Smuzhiyun * for certain scalar sizes. F.e. provide this_cpu_add_2() to provide per
398*4882a593Smuzhiyun * cpu atomic operations for 2 byte sized RMW actions. If arch code does
399*4882a593Smuzhiyun * not provide operations for a scalar size then the fallback in the
400*4882a593Smuzhiyun * generic code will be used.
401*4882a593Smuzhiyun *
402*4882a593Smuzhiyun * cmpxchg_double replaces two adjacent scalars at once. The first two
403*4882a593Smuzhiyun * parameters are per cpu variables which have to be of the same size. A
404*4882a593Smuzhiyun * truth value is returned to indicate success or failure (since a double
405*4882a593Smuzhiyun * register result is difficult to handle). There is very limited hardware
406*4882a593Smuzhiyun * support for these operations, so only certain sizes may work.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun * Operations for contexts where we do not want to do any checks for
411*4882a593Smuzhiyun * preemptions. Unless strictly necessary, always use [__]this_cpu_*()
412*4882a593Smuzhiyun * instead.
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * If there is no other protection through preempt disable and/or disabling
415*4882a593Smuzhiyun * interupts then one of these RMW operations can show unexpected behavior
416*4882a593Smuzhiyun * because the execution thread was rescheduled on another processor or an
417*4882a593Smuzhiyun * interrupt occurred and the same percpu variable was modified from the
418*4882a593Smuzhiyun * interrupt context.
419*4882a593Smuzhiyun */
420*4882a593Smuzhiyun #define raw_cpu_read(pcp) __pcpu_size_call_return(raw_cpu_read_, pcp)
421*4882a593Smuzhiyun #define raw_cpu_write(pcp, val) __pcpu_size_call(raw_cpu_write_, pcp, val)
422*4882a593Smuzhiyun #define raw_cpu_add(pcp, val) __pcpu_size_call(raw_cpu_add_, pcp, val)
423*4882a593Smuzhiyun #define raw_cpu_and(pcp, val) __pcpu_size_call(raw_cpu_and_, pcp, val)
424*4882a593Smuzhiyun #define raw_cpu_or(pcp, val) __pcpu_size_call(raw_cpu_or_, pcp, val)
425*4882a593Smuzhiyun #define raw_cpu_add_return(pcp, val) __pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
426*4882a593Smuzhiyun #define raw_cpu_xchg(pcp, nval) __pcpu_size_call_return2(raw_cpu_xchg_, pcp, nval)
427*4882a593Smuzhiyun #define raw_cpu_cmpxchg(pcp, oval, nval) \
428*4882a593Smuzhiyun __pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
429*4882a593Smuzhiyun #define raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
430*4882a593Smuzhiyun __pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, pcp1, pcp2, oval1, oval2, nval1, nval2)
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun #define raw_cpu_sub(pcp, val) raw_cpu_add(pcp, -(val))
433*4882a593Smuzhiyun #define raw_cpu_inc(pcp) raw_cpu_add(pcp, 1)
434*4882a593Smuzhiyun #define raw_cpu_dec(pcp) raw_cpu_sub(pcp, 1)
435*4882a593Smuzhiyun #define raw_cpu_sub_return(pcp, val) raw_cpu_add_return(pcp, -(typeof(pcp))(val))
436*4882a593Smuzhiyun #define raw_cpu_inc_return(pcp) raw_cpu_add_return(pcp, 1)
437*4882a593Smuzhiyun #define raw_cpu_dec_return(pcp) raw_cpu_add_return(pcp, -1)
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /*
440*4882a593Smuzhiyun * Operations for contexts that are safe from preemption/interrupts. These
441*4882a593Smuzhiyun * operations verify that preemption is disabled.
442*4882a593Smuzhiyun */
443*4882a593Smuzhiyun #define __this_cpu_read(pcp) \
444*4882a593Smuzhiyun ({ \
445*4882a593Smuzhiyun __this_cpu_preempt_check("read"); \
446*4882a593Smuzhiyun raw_cpu_read(pcp); \
447*4882a593Smuzhiyun })
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun #define __this_cpu_write(pcp, val) \
450*4882a593Smuzhiyun ({ \
451*4882a593Smuzhiyun __this_cpu_preempt_check("write"); \
452*4882a593Smuzhiyun raw_cpu_write(pcp, val); \
453*4882a593Smuzhiyun })
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun #define __this_cpu_add(pcp, val) \
456*4882a593Smuzhiyun ({ \
457*4882a593Smuzhiyun __this_cpu_preempt_check("add"); \
458*4882a593Smuzhiyun raw_cpu_add(pcp, val); \
459*4882a593Smuzhiyun })
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun #define __this_cpu_and(pcp, val) \
462*4882a593Smuzhiyun ({ \
463*4882a593Smuzhiyun __this_cpu_preempt_check("and"); \
464*4882a593Smuzhiyun raw_cpu_and(pcp, val); \
465*4882a593Smuzhiyun })
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun #define __this_cpu_or(pcp, val) \
468*4882a593Smuzhiyun ({ \
469*4882a593Smuzhiyun __this_cpu_preempt_check("or"); \
470*4882a593Smuzhiyun raw_cpu_or(pcp, val); \
471*4882a593Smuzhiyun })
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun #define __this_cpu_add_return(pcp, val) \
474*4882a593Smuzhiyun ({ \
475*4882a593Smuzhiyun __this_cpu_preempt_check("add_return"); \
476*4882a593Smuzhiyun raw_cpu_add_return(pcp, val); \
477*4882a593Smuzhiyun })
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun #define __this_cpu_xchg(pcp, nval) \
480*4882a593Smuzhiyun ({ \
481*4882a593Smuzhiyun __this_cpu_preempt_check("xchg"); \
482*4882a593Smuzhiyun raw_cpu_xchg(pcp, nval); \
483*4882a593Smuzhiyun })
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun #define __this_cpu_cmpxchg(pcp, oval, nval) \
486*4882a593Smuzhiyun ({ \
487*4882a593Smuzhiyun __this_cpu_preempt_check("cmpxchg"); \
488*4882a593Smuzhiyun raw_cpu_cmpxchg(pcp, oval, nval); \
489*4882a593Smuzhiyun })
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun #define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
492*4882a593Smuzhiyun ({ __this_cpu_preempt_check("cmpxchg_double"); \
493*4882a593Smuzhiyun raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2); \
494*4882a593Smuzhiyun })
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun #define __this_cpu_sub(pcp, val) __this_cpu_add(pcp, -(typeof(pcp))(val))
497*4882a593Smuzhiyun #define __this_cpu_inc(pcp) __this_cpu_add(pcp, 1)
498*4882a593Smuzhiyun #define __this_cpu_dec(pcp) __this_cpu_sub(pcp, 1)
499*4882a593Smuzhiyun #define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val))
500*4882a593Smuzhiyun #define __this_cpu_inc_return(pcp) __this_cpu_add_return(pcp, 1)
501*4882a593Smuzhiyun #define __this_cpu_dec_return(pcp) __this_cpu_add_return(pcp, -1)
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun * Operations with implied preemption/interrupt protection. These
505*4882a593Smuzhiyun * operations can be used without worrying about preemption or interrupt.
506*4882a593Smuzhiyun */
507*4882a593Smuzhiyun #define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, pcp)
508*4882a593Smuzhiyun #define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, pcp, val)
509*4882a593Smuzhiyun #define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, pcp, val)
510*4882a593Smuzhiyun #define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, pcp, val)
511*4882a593Smuzhiyun #define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, pcp, val)
512*4882a593Smuzhiyun #define this_cpu_add_return(pcp, val) __pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
513*4882a593Smuzhiyun #define this_cpu_xchg(pcp, nval) __pcpu_size_call_return2(this_cpu_xchg_, pcp, nval)
514*4882a593Smuzhiyun #define this_cpu_cmpxchg(pcp, oval, nval) \
515*4882a593Smuzhiyun __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
516*4882a593Smuzhiyun #define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
517*4882a593Smuzhiyun __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, pcp1, pcp2, oval1, oval2, nval1, nval2)
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun #define this_cpu_sub(pcp, val) this_cpu_add(pcp, -(typeof(pcp))(val))
520*4882a593Smuzhiyun #define this_cpu_inc(pcp) this_cpu_add(pcp, 1)
521*4882a593Smuzhiyun #define this_cpu_dec(pcp) this_cpu_sub(pcp, 1)
522*4882a593Smuzhiyun #define this_cpu_sub_return(pcp, val) this_cpu_add_return(pcp, -(typeof(pcp))(val))
523*4882a593Smuzhiyun #define this_cpu_inc_return(pcp) this_cpu_add_return(pcp, 1)
524*4882a593Smuzhiyun #define this_cpu_dec_return(pcp) this_cpu_add_return(pcp, -1)
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun #endif /* __ASSEMBLY__ */
527*4882a593Smuzhiyun #endif /* _LINUX_PERCPU_DEFS_H */
528