xref: /linux/arch/x86/include/asm/percpu.h (revision 4127e13c9302f6892f73793f133ea4b4fffb2964)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PERCPU_H
3 #define _ASM_X86_PERCPU_H
4 
5 #ifdef CONFIG_X86_64
6 # define __percpu_seg		gs
7 # define __percpu_rel		(%rip)
8 #else
9 # define __percpu_seg		fs
10 # define __percpu_rel
11 #endif
12 
13 #ifdef __ASSEMBLY__
14 
15 #ifdef CONFIG_SMP
16 # define __percpu		%__percpu_seg:
17 #else
18 # define __percpu
19 #endif
20 
21 #define PER_CPU_VAR(var)	__percpu(var)__percpu_rel
22 
23 #ifdef CONFIG_X86_64_SMP
24 # define INIT_PER_CPU_VAR(var)  init_per_cpu__##var
25 #else
26 # define INIT_PER_CPU_VAR(var)  var
27 #endif
28 
29 #else /* !__ASSEMBLY__: */
30 
31 #include <linux/build_bug.h>
32 #include <linux/stringify.h>
33 #include <asm/asm.h>
34 
35 #ifdef CONFIG_SMP
36 
37 #ifdef CONFIG_CC_HAS_NAMED_AS
38 
39 #ifdef __CHECKER__
40 # define __seg_gs		__attribute__((address_space(__seg_gs)))
41 # define __seg_fs		__attribute__((address_space(__seg_fs)))
42 #endif
43 
44 #ifdef CONFIG_X86_64
45 # define __percpu_seg_override	__seg_gs
46 #else
47 # define __percpu_seg_override	__seg_fs
48 #endif
49 
50 #define __percpu_prefix		""
51 
52 #else /* !CONFIG_CC_HAS_NAMED_AS: */
53 
54 #define __percpu_seg_override
55 #define __percpu_prefix		"%%"__stringify(__percpu_seg)":"
56 
57 #endif /* CONFIG_CC_HAS_NAMED_AS */
58 
59 #define __force_percpu_prefix	"%%"__stringify(__percpu_seg)":"
60 #define __my_cpu_offset		this_cpu_read(this_cpu_off)
61 
62 /*
63  * Compared to the generic __my_cpu_offset version, the following
64  * saves one instruction and avoids clobbering a temp register.
65  *
66  * arch_raw_cpu_ptr should not be used in 32-bit VDSO for a 64-bit
67  * kernel, because games are played with CONFIG_X86_64 there and
68  * sizeof(this_cpu_off) becames 4.
69  */
70 #ifndef BUILD_VDSO32_64
71 #define arch_raw_cpu_ptr(_ptr)						\
72 ({									\
73 	unsigned long tcp_ptr__ = raw_cpu_read_long(this_cpu_off);	\
74 									\
75 	tcp_ptr__ += (__force unsigned long)(_ptr);			\
76 	(TYPEOF_UNQUAL(*(_ptr)) __force __kernel *)tcp_ptr__;		\
77 })
78 #else
79 #define arch_raw_cpu_ptr(_ptr)						\
80 ({									\
81 	BUILD_BUG();							\
82 	(TYPEOF_UNQUAL(*(_ptr)) __force __kernel *)0;			\
83 })
84 #endif
85 
86 #define PER_CPU_VAR(var)	%__percpu_seg:(var)__percpu_rel
87 
88 #else /* !CONFIG_SMP: */
89 
90 #define __percpu_seg_override
91 #define __percpu_prefix		""
92 #define __force_percpu_prefix	""
93 
94 #define PER_CPU_VAR(var)	(var)__percpu_rel
95 
96 #endif /* CONFIG_SMP */
97 
98 #if defined(CONFIG_USE_X86_SEG_SUPPORT) && defined(USE_TYPEOF_UNQUAL)
99 # define __my_cpu_type(var)	typeof(var)
100 # define __my_cpu_ptr(ptr)	(ptr)
101 # define __my_cpu_var(var)	(var)
102 
103 # define __percpu_qual		__percpu_seg_override
104 #else
105 # define __my_cpu_type(var)	typeof(var) __percpu_seg_override
106 # define __my_cpu_ptr(ptr)	(__my_cpu_type(*(ptr))*)(__force uintptr_t)(ptr)
107 # define __my_cpu_var(var)	(*__my_cpu_ptr(&(var)))
108 #endif
109 
110 #define __percpu_arg(x)		__percpu_prefix "%" #x
111 #define __force_percpu_arg(x)	__force_percpu_prefix "%" #x
112 
113 /*
114  * Initialized pointers to per-CPU variables needed for the boot
115  * processor need to use these macros to get the proper address
116  * offset from __per_cpu_load on SMP.
117  *
118  * There also must be an entry in vmlinux_64.lds.S
119  */
120 #define DECLARE_INIT_PER_CPU(var) \
121        extern typeof(var) init_per_cpu_var(var)
122 
123 #ifdef CONFIG_X86_64_SMP
124 # define init_per_cpu_var(var)  init_per_cpu__##var
125 #else
126 # define init_per_cpu_var(var)  var
127 #endif
128 
129 /*
130  * For arch-specific code, we can use direct single-insn ops (they
131  * don't give an lvalue though).
132  */
133 
134 #define __pcpu_type_1		u8
135 #define __pcpu_type_2		u16
136 #define __pcpu_type_4		u32
137 #define __pcpu_type_8		u64
138 
139 #define __pcpu_cast_1(val)	((u8)(((unsigned long) val) & 0xff))
140 #define __pcpu_cast_2(val)	((u16)(((unsigned long) val) & 0xffff))
141 #define __pcpu_cast_4(val)	((u32)(((unsigned long) val) & 0xffffffff))
142 #define __pcpu_cast_8(val)	((u64)(val))
143 
144 #define __pcpu_op1_1(op, dst)	op "b " dst
145 #define __pcpu_op1_2(op, dst)	op "w " dst
146 #define __pcpu_op1_4(op, dst)	op "l " dst
147 #define __pcpu_op1_8(op, dst)	op "q " dst
148 
149 #define __pcpu_op2_1(op, src, dst) op "b " src ", " dst
150 #define __pcpu_op2_2(op, src, dst) op "w " src ", " dst
151 #define __pcpu_op2_4(op, src, dst) op "l " src ", " dst
152 #define __pcpu_op2_8(op, src, dst) op "q " src ", " dst
153 
154 #define __pcpu_reg_1(mod, x)	mod "q" (x)
155 #define __pcpu_reg_2(mod, x)	mod "r" (x)
156 #define __pcpu_reg_4(mod, x)	mod "r" (x)
157 #define __pcpu_reg_8(mod, x)	mod "r" (x)
158 
159 #define __pcpu_reg_imm_1(x)	"qi" (x)
160 #define __pcpu_reg_imm_2(x)	"ri" (x)
161 #define __pcpu_reg_imm_4(x)	"ri" (x)
162 #define __pcpu_reg_imm_8(x)	"re" (x)
163 
164 #ifdef CONFIG_USE_X86_SEG_SUPPORT
165 
166 #define __raw_cpu_read(size, qual, pcp)					\
167 ({									\
168 	*(qual __my_cpu_type(pcp) *)__my_cpu_ptr(&(pcp));		\
169 })
170 
171 #define __raw_cpu_write(size, qual, pcp, val)				\
172 do {									\
173 	*(qual __my_cpu_type(pcp) *)__my_cpu_ptr(&(pcp)) = (val);	\
174 } while (0)
175 
176 #define __raw_cpu_read_const(pcp)	__raw_cpu_read(, , pcp)
177 
178 #else /* !CONFIG_USE_X86_SEG_SUPPORT: */
179 
180 #define __raw_cpu_read(size, qual, _var)				\
181 ({									\
182 	__pcpu_type_##size pfo_val__;					\
183 									\
184 	asm qual (__pcpu_op2_##size("mov", __percpu_arg([var]), "%[val]") \
185 	    : [val] __pcpu_reg_##size("=", pfo_val__)			\
186 	    : [var] "m" (__my_cpu_var(_var)));				\
187 									\
188 	(typeof(_var))(unsigned long) pfo_val__;			\
189 })
190 
191 #define __raw_cpu_write(size, qual, _var, _val)				\
192 do {									\
193 	__pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val);	\
194 									\
195 	if (0) {		                                        \
196 		TYPEOF_UNQUAL(_var) pto_tmp__;				\
197 		pto_tmp__ = (_val);					\
198 		(void)pto_tmp__;					\
199 	}								\
200 	asm qual(__pcpu_op2_##size("mov", "%[val]", __percpu_arg([var])) \
201 	    : [var] "=m" (__my_cpu_var(_var))				\
202 	    : [val] __pcpu_reg_imm_##size(pto_val__));			\
203 } while (0)
204 
205 /*
206  * The generic per-CPU infrastrucutre is not suitable for
207  * reading const-qualified variables.
208  */
209 #define __raw_cpu_read_const(pcp)	({ BUILD_BUG(); (typeof(pcp))0; })
210 
211 #endif /* CONFIG_USE_X86_SEG_SUPPORT */
212 
213 #define __raw_cpu_read_stable(size, _var)				\
214 ({									\
215 	__pcpu_type_##size pfo_val__;					\
216 									\
217 	asm(__pcpu_op2_##size("mov", __force_percpu_arg(a[var]), "%[val]") \
218 	    : [val] __pcpu_reg_##size("=", pfo_val__)			\
219 	    : [var] "i" (&(_var)));					\
220 									\
221 	(typeof(_var))(unsigned long) pfo_val__;			\
222 })
223 
224 #define percpu_unary_op(size, qual, op, _var)				\
225 ({									\
226 	asm qual (__pcpu_op1_##size(op, __percpu_arg([var]))		\
227 	    : [var] "+m" (__my_cpu_var(_var)));				\
228 })
229 
230 #define percpu_binary_op(size, qual, op, _var, _val)			\
231 do {									\
232 	__pcpu_type_##size pto_val__ = __pcpu_cast_##size(_val);	\
233 									\
234 	if (0) {		                                        \
235 		TYPEOF_UNQUAL(_var) pto_tmp__;				\
236 		pto_tmp__ = (_val);					\
237 		(void)pto_tmp__;					\
238 	}								\
239 	asm qual(__pcpu_op2_##size(op, "%[val]", __percpu_arg([var]))	\
240 	    : [var] "+m" (__my_cpu_var(_var))				\
241 	    : [val] __pcpu_reg_imm_##size(pto_val__));			\
242 } while (0)
243 
244 /*
245  * Generate a per-CPU add to memory instruction and optimize code
246  * if one is added or subtracted.
247  */
248 #define percpu_add_op(size, qual, var, val)				\
249 do {									\
250 	const int pao_ID__ =						\
251 		(__builtin_constant_p(val) &&				\
252 			((val) == 1 ||					\
253 			 (val) == (typeof(val))-1)) ? (int)(val) : 0;	\
254 									\
255 	if (0) {							\
256 		TYPEOF_UNQUAL(var) pao_tmp__;				\
257 		pao_tmp__ = (val);					\
258 		(void)pao_tmp__;					\
259 	}								\
260 	if (pao_ID__ == 1)						\
261 		percpu_unary_op(size, qual, "inc", var);		\
262 	else if (pao_ID__ == -1)					\
263 		percpu_unary_op(size, qual, "dec", var);		\
264 	else								\
265 		percpu_binary_op(size, qual, "add", var, val);		\
266 } while (0)
267 
268 /*
269  * Add return operation
270  */
271 #define percpu_add_return_op(size, qual, _var, _val)			\
272 ({									\
273 	__pcpu_type_##size paro_tmp__ = __pcpu_cast_##size(_val);	\
274 									\
275 	asm qual (__pcpu_op2_##size("xadd", "%[tmp]",			\
276 				     __percpu_arg([var]))		\
277 		  : [tmp] __pcpu_reg_##size("+", paro_tmp__),		\
278 		    [var] "+m" (__my_cpu_var(_var))			\
279 		  : : "memory");					\
280 	(typeof(_var))(unsigned long) (paro_tmp__ + _val);		\
281 })
282 
283 /*
284  * raw_cpu_xchg() can use a load-store since
285  * it is not required to be IRQ-safe.
286  */
287 #define raw_percpu_xchg_op(_var, _nval)					\
288 ({									\
289 	TYPEOF_UNQUAL(_var) pxo_old__ = raw_cpu_read(_var);		\
290 									\
291 	raw_cpu_write(_var, _nval);					\
292 									\
293 	pxo_old__;							\
294 })
295 
296 /*
297  * this_cpu_xchg() is implemented using CMPXCHG without a LOCK prefix.
298  * XCHG is expensive due to the implied LOCK prefix. The processor
299  * cannot prefetch cachelines if XCHG is used.
300  */
301 #define this_percpu_xchg_op(_var, _nval)				\
302 ({									\
303 	TYPEOF_UNQUAL(_var) pxo_old__ = this_cpu_read(_var);		\
304 									\
305 	do { } while (!this_cpu_try_cmpxchg(_var, &pxo_old__, _nval));	\
306 									\
307 	pxo_old__;							\
308 })
309 
310 /*
311  * CMPXCHG has no such implied lock semantics as a result it is much
312  * more efficient for CPU-local operations.
313  */
314 #define percpu_cmpxchg_op(size, qual, _var, _oval, _nval)		\
315 ({									\
316 	__pcpu_type_##size pco_old__ = __pcpu_cast_##size(_oval);	\
317 	__pcpu_type_##size pco_new__ = __pcpu_cast_##size(_nval);	\
318 									\
319 	asm qual (__pcpu_op2_##size("cmpxchg", "%[nval]",		\
320 				    __percpu_arg([var]))		\
321 		  : [oval] "+a" (pco_old__),				\
322 		    [var] "+m" (__my_cpu_var(_var))			\
323 		  : [nval] __pcpu_reg_##size(, pco_new__)		\
324 		  : "memory");						\
325 									\
326 	(typeof(_var))(unsigned long) pco_old__;			\
327 })
328 
329 #define percpu_try_cmpxchg_op(size, qual, _var, _ovalp, _nval)		\
330 ({									\
331 	bool success;							\
332 	__pcpu_type_##size *pco_oval__ = (__pcpu_type_##size *)(_ovalp); \
333 	__pcpu_type_##size pco_old__ = *pco_oval__;			\
334 	__pcpu_type_##size pco_new__ = __pcpu_cast_##size(_nval);	\
335 									\
336 	asm qual (__pcpu_op2_##size("cmpxchg", "%[nval]",		\
337 				    __percpu_arg([var]))		\
338 		  CC_SET(z)						\
339 		  : CC_OUT(z) (success),				\
340 		    [oval] "+a" (pco_old__),				\
341 		    [var] "+m" (__my_cpu_var(_var))			\
342 		  : [nval] __pcpu_reg_##size(, pco_new__)		\
343 		  : "memory");						\
344 	if (unlikely(!success))						\
345 		*pco_oval__ = pco_old__;				\
346 									\
347 	likely(success);						\
348 })
349 
350 #if defined(CONFIG_X86_32) && !defined(CONFIG_UML)
351 
352 #define percpu_cmpxchg64_op(size, qual, _var, _oval, _nval)		\
353 ({									\
354 	union {								\
355 		u64 var;						\
356 		struct {						\
357 			u32 low, high;					\
358 		};							\
359 	} old__, new__;							\
360 									\
361 	old__.var = _oval;						\
362 	new__.var = _nval;						\
363 									\
364 	asm qual (ALTERNATIVE("call this_cpu_cmpxchg8b_emu",		\
365 			      "cmpxchg8b " __percpu_arg([var]), X86_FEATURE_CX8) \
366 		  : [var] "+m" (__my_cpu_var(_var)),			\
367 		    "+a" (old__.low),					\
368 		    "+d" (old__.high)					\
369 		  : "b" (new__.low),					\
370 		    "c" (new__.high),					\
371 		    "S" (&(_var))					\
372 		  : "memory");						\
373 									\
374 	old__.var;							\
375 })
376 
377 #define raw_cpu_cmpxchg64(pcp, oval, nval)		percpu_cmpxchg64_op(8,         , pcp, oval, nval)
378 #define this_cpu_cmpxchg64(pcp, oval, nval)		percpu_cmpxchg64_op(8, volatile, pcp, oval, nval)
379 
380 #define percpu_try_cmpxchg64_op(size, qual, _var, _ovalp, _nval)	\
381 ({									\
382 	bool success;							\
383 	u64 *_oval = (u64 *)(_ovalp);					\
384 	union {								\
385 		u64 var;						\
386 		struct {						\
387 			u32 low, high;					\
388 		};							\
389 	} old__, new__;							\
390 									\
391 	old__.var = *_oval;						\
392 	new__.var = _nval;						\
393 									\
394 	asm qual (ALTERNATIVE("call this_cpu_cmpxchg8b_emu",		\
395 			      "cmpxchg8b " __percpu_arg([var]), X86_FEATURE_CX8) \
396 		  CC_SET(z)						\
397 		  : CC_OUT(z) (success),				\
398 		    [var] "+m" (__my_cpu_var(_var)),			\
399 		    "+a" (old__.low),					\
400 		    "+d" (old__.high)					\
401 		  : "b" (new__.low),					\
402 		    "c" (new__.high),					\
403 		    "S" (&(_var))					\
404 		  : "memory");						\
405 	if (unlikely(!success))						\
406 		*_oval = old__.var;					\
407 									\
408 	likely(success);						\
409 })
410 
411 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval)		percpu_try_cmpxchg64_op(8,         , pcp, ovalp, nval)
412 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval)	percpu_try_cmpxchg64_op(8, volatile, pcp, ovalp, nval)
413 
414 #endif /* defined(CONFIG_X86_32) && !defined(CONFIG_UML) */
415 
416 #ifdef CONFIG_X86_64
417 #define raw_cpu_cmpxchg64(pcp, oval, nval)		percpu_cmpxchg_op(8,         , pcp, oval, nval);
418 #define this_cpu_cmpxchg64(pcp, oval, nval)		percpu_cmpxchg_op(8, volatile, pcp, oval, nval);
419 
420 #define raw_cpu_try_cmpxchg64(pcp, ovalp, nval)		percpu_try_cmpxchg_op(8,         , pcp, ovalp, nval);
421 #define this_cpu_try_cmpxchg64(pcp, ovalp, nval)	percpu_try_cmpxchg_op(8, volatile, pcp, ovalp, nval);
422 
423 #define percpu_cmpxchg128_op(size, qual, _var, _oval, _nval)		\
424 ({									\
425 	union {								\
426 		u128 var;						\
427 		struct {						\
428 			u64 low, high;					\
429 		};							\
430 	} old__, new__;							\
431 									\
432 	old__.var = _oval;						\
433 	new__.var = _nval;						\
434 									\
435 	asm qual (ALTERNATIVE("call this_cpu_cmpxchg16b_emu",		\
436 			      "cmpxchg16b " __percpu_arg([var]), X86_FEATURE_CX16) \
437 		  : [var] "+m" (__my_cpu_var(_var)),			\
438 		    "+a" (old__.low),					\
439 		    "+d" (old__.high)					\
440 		  : "b" (new__.low),					\
441 		    "c" (new__.high),					\
442 		    "S" (&(_var))					\
443 		  : "memory");						\
444 									\
445 	old__.var;							\
446 })
447 
448 #define raw_cpu_cmpxchg128(pcp, oval, nval)		percpu_cmpxchg128_op(16,         , pcp, oval, nval)
449 #define this_cpu_cmpxchg128(pcp, oval, nval)		percpu_cmpxchg128_op(16, volatile, pcp, oval, nval)
450 
451 #define percpu_try_cmpxchg128_op(size, qual, _var, _ovalp, _nval)	\
452 ({									\
453 	bool success;							\
454 	u128 *_oval = (u128 *)(_ovalp);					\
455 	union {								\
456 		u128 var;						\
457 		struct {						\
458 			u64 low, high;					\
459 		};							\
460 	} old__, new__;							\
461 									\
462 	old__.var = *_oval;						\
463 	new__.var = _nval;						\
464 									\
465 	asm qual (ALTERNATIVE("call this_cpu_cmpxchg16b_emu",		\
466 			      "cmpxchg16b " __percpu_arg([var]), X86_FEATURE_CX16) \
467 		  CC_SET(z)						\
468 		  : CC_OUT(z) (success),				\
469 		    [var] "+m" (__my_cpu_var(_var)),			\
470 		    "+a" (old__.low),					\
471 		    "+d" (old__.high)					\
472 		  : "b" (new__.low),					\
473 		    "c" (new__.high),					\
474 		    "S" (&(_var))					\
475 		  : "memory");						\
476 	if (unlikely(!success))						\
477 		*_oval = old__.var;					\
478 	likely(success);						\
479 })
480 
481 #define raw_cpu_try_cmpxchg128(pcp, ovalp, nval)	percpu_try_cmpxchg128_op(16,         , pcp, ovalp, nval)
482 #define this_cpu_try_cmpxchg128(pcp, ovalp, nval)	percpu_try_cmpxchg128_op(16, volatile, pcp, ovalp, nval)
483 
484 #endif /* CONFIG_X86_64 */
485 
486 #define raw_cpu_read_1(pcp)				__raw_cpu_read(1, , pcp)
487 #define raw_cpu_read_2(pcp)				__raw_cpu_read(2, , pcp)
488 #define raw_cpu_read_4(pcp)				__raw_cpu_read(4, , pcp)
489 #define raw_cpu_write_1(pcp, val)			__raw_cpu_write(1, , pcp, val)
490 #define raw_cpu_write_2(pcp, val)			__raw_cpu_write(2, , pcp, val)
491 #define raw_cpu_write_4(pcp, val)			__raw_cpu_write(4, , pcp, val)
492 
493 #define this_cpu_read_1(pcp)				__raw_cpu_read(1, volatile, pcp)
494 #define this_cpu_read_2(pcp)				__raw_cpu_read(2, volatile, pcp)
495 #define this_cpu_read_4(pcp)				__raw_cpu_read(4, volatile, pcp)
496 #define this_cpu_write_1(pcp, val)			__raw_cpu_write(1, volatile, pcp, val)
497 #define this_cpu_write_2(pcp, val)			__raw_cpu_write(2, volatile, pcp, val)
498 #define this_cpu_write_4(pcp, val)			__raw_cpu_write(4, volatile, pcp, val)
499 
500 #define this_cpu_read_stable_1(pcp)			__raw_cpu_read_stable(1, pcp)
501 #define this_cpu_read_stable_2(pcp)			__raw_cpu_read_stable(2, pcp)
502 #define this_cpu_read_stable_4(pcp)			__raw_cpu_read_stable(4, pcp)
503 
504 #define raw_cpu_add_1(pcp, val)				percpu_add_op(1, , (pcp), val)
505 #define raw_cpu_add_2(pcp, val)				percpu_add_op(2, , (pcp), val)
506 #define raw_cpu_add_4(pcp, val)				percpu_add_op(4, , (pcp), val)
507 #define raw_cpu_and_1(pcp, val)				percpu_binary_op(1, , "and", (pcp), val)
508 #define raw_cpu_and_2(pcp, val)				percpu_binary_op(2, , "and", (pcp), val)
509 #define raw_cpu_and_4(pcp, val)				percpu_binary_op(4, , "and", (pcp), val)
510 #define raw_cpu_or_1(pcp, val)				percpu_binary_op(1, , "or", (pcp), val)
511 #define raw_cpu_or_2(pcp, val)				percpu_binary_op(2, , "or", (pcp), val)
512 #define raw_cpu_or_4(pcp, val)				percpu_binary_op(4, , "or", (pcp), val)
513 #define raw_cpu_xchg_1(pcp, val)			raw_percpu_xchg_op(pcp, val)
514 #define raw_cpu_xchg_2(pcp, val)			raw_percpu_xchg_op(pcp, val)
515 #define raw_cpu_xchg_4(pcp, val)			raw_percpu_xchg_op(pcp, val)
516 
517 #define this_cpu_add_1(pcp, val)			percpu_add_op(1, volatile, (pcp), val)
518 #define this_cpu_add_2(pcp, val)			percpu_add_op(2, volatile, (pcp), val)
519 #define this_cpu_add_4(pcp, val)			percpu_add_op(4, volatile, (pcp), val)
520 #define this_cpu_and_1(pcp, val)			percpu_binary_op(1, volatile, "and", (pcp), val)
521 #define this_cpu_and_2(pcp, val)			percpu_binary_op(2, volatile, "and", (pcp), val)
522 #define this_cpu_and_4(pcp, val)			percpu_binary_op(4, volatile, "and", (pcp), val)
523 #define this_cpu_or_1(pcp, val)				percpu_binary_op(1, volatile, "or", (pcp), val)
524 #define this_cpu_or_2(pcp, val)				percpu_binary_op(2, volatile, "or", (pcp), val)
525 #define this_cpu_or_4(pcp, val)				percpu_binary_op(4, volatile, "or", (pcp), val)
526 #define this_cpu_xchg_1(pcp, nval)			this_percpu_xchg_op(pcp, nval)
527 #define this_cpu_xchg_2(pcp, nval)			this_percpu_xchg_op(pcp, nval)
528 #define this_cpu_xchg_4(pcp, nval)			this_percpu_xchg_op(pcp, nval)
529 
530 #define raw_cpu_add_return_1(pcp, val)			percpu_add_return_op(1, , pcp, val)
531 #define raw_cpu_add_return_2(pcp, val)			percpu_add_return_op(2, , pcp, val)
532 #define raw_cpu_add_return_4(pcp, val)			percpu_add_return_op(4, , pcp, val)
533 #define raw_cpu_cmpxchg_1(pcp, oval, nval)		percpu_cmpxchg_op(1, , pcp, oval, nval)
534 #define raw_cpu_cmpxchg_2(pcp, oval, nval)		percpu_cmpxchg_op(2, , pcp, oval, nval)
535 #define raw_cpu_cmpxchg_4(pcp, oval, nval)		percpu_cmpxchg_op(4, , pcp, oval, nval)
536 #define raw_cpu_try_cmpxchg_1(pcp, ovalp, nval)		percpu_try_cmpxchg_op(1, , pcp, ovalp, nval)
537 #define raw_cpu_try_cmpxchg_2(pcp, ovalp, nval)		percpu_try_cmpxchg_op(2, , pcp, ovalp, nval)
538 #define raw_cpu_try_cmpxchg_4(pcp, ovalp, nval)		percpu_try_cmpxchg_op(4, , pcp, ovalp, nval)
539 
540 #define this_cpu_add_return_1(pcp, val)			percpu_add_return_op(1, volatile, pcp, val)
541 #define this_cpu_add_return_2(pcp, val)			percpu_add_return_op(2, volatile, pcp, val)
542 #define this_cpu_add_return_4(pcp, val)			percpu_add_return_op(4, volatile, pcp, val)
543 #define this_cpu_cmpxchg_1(pcp, oval, nval)		percpu_cmpxchg_op(1, volatile, pcp, oval, nval)
544 #define this_cpu_cmpxchg_2(pcp, oval, nval)		percpu_cmpxchg_op(2, volatile, pcp, oval, nval)
545 #define this_cpu_cmpxchg_4(pcp, oval, nval)		percpu_cmpxchg_op(4, volatile, pcp, oval, nval)
546 #define this_cpu_try_cmpxchg_1(pcp, ovalp, nval)	percpu_try_cmpxchg_op(1, volatile, pcp, ovalp, nval)
547 #define this_cpu_try_cmpxchg_2(pcp, ovalp, nval)	percpu_try_cmpxchg_op(2, volatile, pcp, ovalp, nval)
548 #define this_cpu_try_cmpxchg_4(pcp, ovalp, nval)	percpu_try_cmpxchg_op(4, volatile, pcp, ovalp, nval)
549 
550 /*
551  * Per-CPU atomic 64-bit operations are only available under 64-bit kernels.
552  * 32-bit kernels must fall back to generic operations.
553  */
554 #ifdef CONFIG_X86_64
555 
556 #define raw_cpu_read_8(pcp)				__raw_cpu_read(8, , pcp)
557 #define raw_cpu_write_8(pcp, val)			__raw_cpu_write(8, , pcp, val)
558 
559 #define this_cpu_read_8(pcp)				__raw_cpu_read(8, volatile, pcp)
560 #define this_cpu_write_8(pcp, val)			__raw_cpu_write(8, volatile, pcp, val)
561 
562 #define this_cpu_read_stable_8(pcp)			__raw_cpu_read_stable(8, pcp)
563 
564 #define raw_cpu_add_8(pcp, val)				percpu_add_op(8, , (pcp), val)
565 #define raw_cpu_and_8(pcp, val)				percpu_binary_op(8, , "and", (pcp), val)
566 #define raw_cpu_or_8(pcp, val)				percpu_binary_op(8, , "or", (pcp), val)
567 #define raw_cpu_add_return_8(pcp, val)			percpu_add_return_op(8, , pcp, val)
568 #define raw_cpu_xchg_8(pcp, nval)			raw_percpu_xchg_op(pcp, nval)
569 #define raw_cpu_cmpxchg_8(pcp, oval, nval)		percpu_cmpxchg_op(8, , pcp, oval, nval)
570 #define raw_cpu_try_cmpxchg_8(pcp, ovalp, nval)		percpu_try_cmpxchg_op(8, , pcp, ovalp, nval)
571 
572 #define this_cpu_add_8(pcp, val)			percpu_add_op(8, volatile, (pcp), val)
573 #define this_cpu_and_8(pcp, val)			percpu_binary_op(8, volatile, "and", (pcp), val)
574 #define this_cpu_or_8(pcp, val)				percpu_binary_op(8, volatile, "or", (pcp), val)
575 #define this_cpu_add_return_8(pcp, val)			percpu_add_return_op(8, volatile, pcp, val)
576 #define this_cpu_xchg_8(pcp, nval)			this_percpu_xchg_op(pcp, nval)
577 #define this_cpu_cmpxchg_8(pcp, oval, nval)		percpu_cmpxchg_op(8, volatile, pcp, oval, nval)
578 #define this_cpu_try_cmpxchg_8(pcp, ovalp, nval)	percpu_try_cmpxchg_op(8, volatile, pcp, ovalp, nval)
579 
580 #define raw_cpu_read_long(pcp)				raw_cpu_read_8(pcp)
581 
582 #else /* !CONFIG_X86_64: */
583 
584 /* There is no generic 64-bit read stable operation for 32-bit targets. */
585 #define this_cpu_read_stable_8(pcp)			({ BUILD_BUG(); (typeof(pcp))0; })
586 
587 #define raw_cpu_read_long(pcp)				raw_cpu_read_4(pcp)
588 
589 #endif /* CONFIG_X86_64 */
590 
591 #define this_cpu_read_const(pcp)			__raw_cpu_read_const(pcp)
592 
593 /*
594  * this_cpu_read() makes the compiler load the per-CPU variable every time
595  * it is accessed while this_cpu_read_stable() allows the value to be cached.
596  * this_cpu_read_stable() is more efficient and can be used if its value
597  * is guaranteed to be valid across CPUs.  The current users include
598  * pcpu_hot.current_task and pcpu_hot.top_of_stack, both of which are
599  * actually per-thread variables implemented as per-CPU variables and
600  * thus stable for the duration of the respective task.
601  */
602 #define this_cpu_read_stable(pcp)			__pcpu_size_call_return(this_cpu_read_stable_, pcp)
603 
604 #define x86_this_cpu_constant_test_bit(_nr, _var)			\
605 ({									\
606 	unsigned long __percpu *addr__ =				\
607 		(unsigned long __percpu *)&(_var) + ((_nr) / BITS_PER_LONG); \
608 									\
609 	!!((1UL << ((_nr) % BITS_PER_LONG)) & raw_cpu_read(*addr__));	\
610 })
611 
612 #define x86_this_cpu_variable_test_bit(_nr, _var)			\
613 ({									\
614 	bool oldbit;							\
615 									\
616 	asm volatile("btl %[nr], " __percpu_arg([var])			\
617 		     CC_SET(c)						\
618 		     : CC_OUT(c) (oldbit)				\
619 		     : [var] "m" (__my_cpu_var(_var)),			\
620 		       [nr] "rI" (_nr));				\
621 	oldbit;								\
622 })
623 
624 #define x86_this_cpu_test_bit(_nr, _var)				\
625 	(__builtin_constant_p(_nr)					\
626 	 ? x86_this_cpu_constant_test_bit(_nr, _var)			\
627 	 : x86_this_cpu_variable_test_bit(_nr, _var))
628 
629 
630 #include <asm-generic/percpu.h>
631 
632 /* We can use this directly for local CPU (faster). */
633 DECLARE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off);
634 
635 #endif /* !__ASSEMBLY__ */
636 
637 #ifdef CONFIG_SMP
638 
639 /*
640  * Define the "EARLY_PER_CPU" macros.  These are used for some per_cpu
641  * variables that are initialized and accessed before there are per_cpu
642  * areas allocated.
643  */
644 
645 #define	DEFINE_EARLY_PER_CPU(_type, _name, _initvalue)			\
646 	DEFINE_PER_CPU(_type, _name) = _initvalue;			\
647 	__typeof__(_type) _name##_early_map[NR_CPUS] __initdata =	\
648 				{ [0 ... NR_CPUS-1] = _initvalue };	\
649 	__typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
650 
651 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue)	\
652 	DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue;		\
653 	__typeof__(_type) _name##_early_map[NR_CPUS] __initdata =	\
654 				{ [0 ... NR_CPUS-1] = _initvalue };	\
655 	__typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
656 
657 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name)				\
658 	EXPORT_PER_CPU_SYMBOL(_name)
659 
660 #define DECLARE_EARLY_PER_CPU(_type, _name)				\
661 	DECLARE_PER_CPU(_type, _name);					\
662 	extern __typeof__(_type) *_name##_early_ptr;			\
663 	extern __typeof__(_type)  _name##_early_map[]
664 
665 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name)			\
666 	DECLARE_PER_CPU_READ_MOSTLY(_type, _name);			\
667 	extern __typeof__(_type) *_name##_early_ptr;			\
668 	extern __typeof__(_type)  _name##_early_map[]
669 
670 #define	early_per_cpu_ptr(_name)			(_name##_early_ptr)
671 #define	early_per_cpu_map(_name, _idx)			(_name##_early_map[_idx])
672 
673 #define	early_per_cpu(_name, _cpu)					\
674 	*(early_per_cpu_ptr(_name) ?					\
675 		&early_per_cpu_ptr(_name)[_cpu] :			\
676 		&per_cpu(_name, _cpu))
677 
678 #else /* !CONFIG_SMP: */
679 #define	DEFINE_EARLY_PER_CPU(_type, _name, _initvalue)			\
680 	DEFINE_PER_CPU(_type, _name) = _initvalue
681 
682 #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue)	\
683 	DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue
684 
685 #define EXPORT_EARLY_PER_CPU_SYMBOL(_name)				\
686 	EXPORT_PER_CPU_SYMBOL(_name)
687 
688 #define DECLARE_EARLY_PER_CPU(_type, _name)				\
689 	DECLARE_PER_CPU(_type, _name)
690 
691 #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name)			\
692 	DECLARE_PER_CPU_READ_MOSTLY(_type, _name)
693 
694 #define	early_per_cpu(_name, _cpu)			per_cpu(_name, _cpu)
695 #define	early_per_cpu_ptr(_name)			NULL
696 /* no early_per_cpu_map() */
697 
698 #endif /* !CONFIG_SMP */
699 
700 #endif /* _ASM_X86_PERCPU_H */
701