xref: /linux-6.15/include/linux/percpu-defs.h (revision 6a39fe05)
1457c8996SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
262fde541STejun Heo /*
362fde541STejun Heo  * linux/percpu-defs.h - basic definitions for percpu areas
462fde541STejun Heo  *
562fde541STejun Heo  * DO NOT INCLUDE DIRECTLY OUTSIDE PERCPU IMPLEMENTATION PROPER.
662fde541STejun Heo  *
762fde541STejun Heo  * This file is separate from linux/percpu.h to avoid cyclic inclusion
862fde541STejun Heo  * dependency from arch header files.  Only to be included from
962fde541STejun Heo  * asm/percpu.h.
1062fde541STejun Heo  *
1162fde541STejun Heo  * This file includes macros necessary to declare percpu sections and
1262fde541STejun Heo  * variables, and definitions of percpu accessors and operations.  It
1362fde541STejun Heo  * should provide enough percpu features to arch header files even when
1462fde541STejun Heo  * they can only include asm/percpu.h to avoid cyclic inclusion dependency.
1562fde541STejun Heo  */
1662fde541STejun Heo 
175028eaa9SDavid Howells #ifndef _LINUX_PERCPU_DEFS_H
185028eaa9SDavid Howells #define _LINUX_PERCPU_DEFS_H
195028eaa9SDavid Howells 
2062fde541STejun Heo #ifdef CONFIG_SMP
2162fde541STejun Heo 
2262fde541STejun Heo #ifdef MODULE
2362fde541STejun Heo #define PER_CPU_SHARED_ALIGNED_SECTION ""
2462fde541STejun Heo #define PER_CPU_ALIGNED_SECTION ""
2562fde541STejun Heo #else
2662fde541STejun Heo #define PER_CPU_SHARED_ALIGNED_SECTION "..shared_aligned"
2762fde541STejun Heo #define PER_CPU_ALIGNED_SECTION "..shared_aligned"
2862fde541STejun Heo #endif
2962fde541STejun Heo 
3062fde541STejun Heo #else
3162fde541STejun Heo 
3262fde541STejun Heo #define PER_CPU_SHARED_ALIGNED_SECTION ""
3362fde541STejun Heo #define PER_CPU_ALIGNED_SECTION "..shared_aligned"
3462fde541STejun Heo 
3562fde541STejun Heo #endif
3662fde541STejun Heo 
3762fde541STejun Heo /*
3862fde541STejun Heo  * Base implementations of per-CPU variable declarations and definitions, where
395028eaa9SDavid Howells  * the section in which the variable is to be placed is provided by the
405028eaa9SDavid Howells  * 'sec' argument.  This may be used to affect the parameters governing the
415028eaa9SDavid Howells  * variable's storage.
427c756e6eSTejun Heo  *
435028eaa9SDavid Howells  * NOTE!  The sections for the DECLARE and for the DEFINE must match, lest
445028eaa9SDavid Howells  * linkage errors occur due the compiler generating the wrong code to access
455028eaa9SDavid Howells  * that section.
465028eaa9SDavid Howells  */
475028eaa9SDavid Howells #define __PCPU_ATTRS(sec)						\
485028eaa9SDavid Howells 	__percpu __attribute__((section(PER_CPU_BASE_SECTION sec)))	\
497c756e6eSTejun Heo 	PER_CPU_ATTRIBUTES
50e0fdb0e0SRusty Russell 
517c756e6eSTejun Heo #define __PCPU_DUMMY_ATTRS						\
525028eaa9SDavid Howells 	__section(".discard") __attribute__((unused))
537c756e6eSTejun Heo 
5433def849SJoe Perches /*
557c756e6eSTejun Heo  * s390 and alpha modules require percpu variables to be defined as
567c756e6eSTejun Heo  * weak to force the compiler to generate GOT based external
577c756e6eSTejun Heo  * references for them.  This is necessary because percpu sections
587c756e6eSTejun Heo  * will be located outside of the usually addressable area.
597c756e6eSTejun Heo  *
607c756e6eSTejun Heo  * This definition puts the following two extra restrictions when
617c756e6eSTejun Heo  * defining percpu variables.
627c756e6eSTejun Heo  *
637c756e6eSTejun Heo  * 1. The symbol must be globally unique, even the static ones.
647c756e6eSTejun Heo  * 2. Static percpu variables cannot be defined inside a function.
657c756e6eSTejun Heo  *
667c756e6eSTejun Heo  * Archs which need weak percpu definitions should define
677c756e6eSTejun Heo  * ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary.
687c756e6eSTejun Heo  *
697c756e6eSTejun Heo  * To ensure that the generic code observes the above two
707c756e6eSTejun Heo  * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak
717c756e6eSTejun Heo  * definition is used for all cases.
727c756e6eSTejun Heo  */
737c756e6eSTejun Heo #if defined(ARCH_NEEDS_WEAK_PER_CPU) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU)
747c756e6eSTejun Heo /*
757c756e6eSTejun Heo  * __pcpu_scope_* dummy variable is used to enforce scope.  It
767c756e6eSTejun Heo  * receives the static modifier when it's used in front of
777c756e6eSTejun Heo  * DEFINE_PER_CPU() and will trigger build failure if
787c756e6eSTejun Heo  * DECLARE_PER_CPU() is used for the same variable.
797c756e6eSTejun Heo  *
807c756e6eSTejun Heo  * __pcpu_unique_* dummy variable is used to enforce symbol uniqueness
817c756e6eSTejun Heo  * such that hidden weak symbol collision, which will cause unrelated
827c756e6eSTejun Heo  * variables to share the same address, can be detected during build.
837c756e6eSTejun Heo  */
847c756e6eSTejun Heo #define DECLARE_PER_CPU_SECTION(type, name, sec)			\
857c756e6eSTejun Heo 	extern __PCPU_DUMMY_ATTRS char __pcpu_scope_##name;		\
867c756e6eSTejun Heo 	extern __PCPU_ATTRS(sec) __typeof__(type) name
877c756e6eSTejun Heo 
88dd17c8f7SRusty Russell #define DEFINE_PER_CPU_SECTION(type, name, sec)				\
897c756e6eSTejun Heo 	__PCPU_DUMMY_ATTRS char __pcpu_scope_##name;			\
907c756e6eSTejun Heo 	extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name;		\
917c756e6eSTejun Heo 	__PCPU_DUMMY_ATTRS char __pcpu_unique_##name;			\
920f5e4816STejun Heo 	extern __PCPU_ATTRS(sec) __typeof__(type) name;			\
937c756e6eSTejun Heo 	__PCPU_ATTRS(sec) __weak __typeof__(type) name
94b1a0fbfdSTejun Heo #else
9569a60bc7SAlexander Pateenok /*
967c756e6eSTejun Heo  * Normal declaration and definition macros.
977c756e6eSTejun Heo  */
987c756e6eSTejun Heo #define DECLARE_PER_CPU_SECTION(type, name, sec)			\
997c756e6eSTejun Heo 	extern __PCPU_ATTRS(sec) __typeof__(type) name
1007c756e6eSTejun Heo 
101dd17c8f7SRusty Russell #define DEFINE_PER_CPU_SECTION(type, name, sec)				\
1027c756e6eSTejun Heo 	__PCPU_ATTRS(sec) __typeof__(type) name
1037c756e6eSTejun Heo #endif
10469a60bc7SAlexander Pateenok 
1057c756e6eSTejun Heo /*
1065028eaa9SDavid Howells  * Variant on the per-CPU variable declaration/definition theme used for
1075028eaa9SDavid Howells  * ordinary per-CPU variables.
1085028eaa9SDavid Howells  */
1095028eaa9SDavid Howells #define DECLARE_PER_CPU(type, name)					\
1105028eaa9SDavid Howells 	DECLARE_PER_CPU_SECTION(type, name, "")
1115028eaa9SDavid Howells 
1125028eaa9SDavid Howells #define DEFINE_PER_CPU(type, name)					\
1135028eaa9SDavid Howells 	DEFINE_PER_CPU_SECTION(type, name, "")
1145028eaa9SDavid Howells 
1155028eaa9SDavid Howells /*
1165028eaa9SDavid Howells  * Declaration/definition used for per-CPU variables that are frequently
1175028eaa9SDavid Howells  * accessed and should be in a single cacheline.
1185028eaa9SDavid Howells  *
1195028eaa9SDavid Howells  * For use only by architecture and core code.  Only use scalar or pointer
1205028eaa9SDavid Howells  * types to maximize density.
1215028eaa9SDavid Howells  */
1225028eaa9SDavid Howells #define DECLARE_PER_CPU_CACHE_HOT(type, name)				\
1235028eaa9SDavid Howells 	DECLARE_PER_CPU_SECTION(type, name, "..hot.." #name)
1245028eaa9SDavid Howells 
1255028eaa9SDavid Howells #define DEFINE_PER_CPU_CACHE_HOT(type, name)				\
1265028eaa9SDavid Howells 	DEFINE_PER_CPU_SECTION(type, name, "..hot.." #name)
1275028eaa9SDavid Howells 
1285028eaa9SDavid Howells /*
1295028eaa9SDavid Howells  * Declaration/definition used for per-CPU variables that must be cacheline
1305028eaa9SDavid Howells  * aligned under SMP conditions so that, whilst a particular instance of the
1315028eaa9SDavid Howells  * data corresponds to a particular CPU, inefficiencies due to direct access by
1325028eaa9SDavid Howells  * other CPUs are reduced by preventing the data from unnecessarily spanning
1335028eaa9SDavid Howells  * cachelines.
1345028eaa9SDavid Howells  *
1355028eaa9SDavid Howells  * An example of this would be statistical data, where each CPU's set of data
1365028eaa9SDavid Howells  * is updated by that CPU alone, but the data from across all CPUs is collated
1375028eaa9SDavid Howells  * by a CPU processing a read from a proc file.
1385028eaa9SDavid Howells  */
1395028eaa9SDavid Howells #define DECLARE_PER_CPU_SHARED_ALIGNED(type, name)			\
1405028eaa9SDavid Howells 	DECLARE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
1415028eaa9SDavid Howells 	____cacheline_aligned_in_smp
1425028eaa9SDavid Howells 
1435028eaa9SDavid Howells #define DEFINE_PER_CPU_SHARED_ALIGNED(type, name)			\
1445028eaa9SDavid Howells 	DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \
1455028eaa9SDavid Howells 	____cacheline_aligned_in_smp
14653f82452SJeremy Fitzhardinge 
14753f82452SJeremy Fitzhardinge #define DECLARE_PER_CPU_ALIGNED(type, name)				\
14853f82452SJeremy Fitzhardinge 	DECLARE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION)	\
14953f82452SJeremy Fitzhardinge 	____cacheline_aligned
15053f82452SJeremy Fitzhardinge 
15153f82452SJeremy Fitzhardinge #define DEFINE_PER_CPU_ALIGNED(type, name)				\
15253f82452SJeremy Fitzhardinge 	DEFINE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION)	\
15353f82452SJeremy Fitzhardinge 	____cacheline_aligned
1545028eaa9SDavid Howells 
1555028eaa9SDavid Howells /*
1565028eaa9SDavid Howells  * Declaration/definition used for per-CPU variables that must be page aligned.
1575028eaa9SDavid Howells  */
1583d9a854cSDenys Vlasenko #define DECLARE_PER_CPU_PAGE_ALIGNED(type, name)			\
1593e352aa8STejun Heo 	DECLARE_PER_CPU_SECTION(type, name, "..page_aligned")		\
1605028eaa9SDavid Howells 	__aligned(PAGE_SIZE)
1615028eaa9SDavid Howells 
1623d9a854cSDenys Vlasenko #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name)				\
1633e352aa8STejun Heo 	DEFINE_PER_CPU_SECTION(type, name, "..page_aligned")		\
1645028eaa9SDavid Howells 	__aligned(PAGE_SIZE)
1655028eaa9SDavid Howells 
166c957ef2cSShaohua Li /*
167c957ef2cSShaohua Li  * Declaration/definition used for per-CPU variables that must be read mostly.
168c957ef2cSShaohua Li  */
169330d2822SZhengyu He #define DECLARE_PER_CPU_READ_MOSTLY(type, name)			\
170c957ef2cSShaohua Li 	DECLARE_PER_CPU_SECTION(type, name, "..read_mostly")
171c957ef2cSShaohua Li 
172330d2822SZhengyu He #define DEFINE_PER_CPU_READ_MOSTLY(type, name)				\
173c957ef2cSShaohua Li 	DEFINE_PER_CPU_SECTION(type, name, "..read_mostly")
174c957ef2cSShaohua Li 
175ac26963aSBrijesh Singh /*
176ac26963aSBrijesh Singh  * Declaration/definition used for per-CPU variables that should be accessed
177ac26963aSBrijesh Singh  * as decrypted when memory encryption is enabled in the guest.
178264b0d2bSErdem Aktas  */
179ac26963aSBrijesh Singh #ifdef CONFIG_AMD_MEM_ENCRYPT
180ac26963aSBrijesh Singh #define DECLARE_PER_CPU_DECRYPTED(type, name)				\
181ac26963aSBrijesh Singh 	DECLARE_PER_CPU_SECTION(type, name, "..decrypted")
182ac26963aSBrijesh Singh 
183ac26963aSBrijesh Singh #define DEFINE_PER_CPU_DECRYPTED(type, name)				\
184ac26963aSBrijesh Singh 	DEFINE_PER_CPU_SECTION(type, name, "..decrypted")
185ac26963aSBrijesh Singh #else
186ac26963aSBrijesh Singh #define DEFINE_PER_CPU_DECRYPTED(type, name)	DEFINE_PER_CPU(type, name)
187ac26963aSBrijesh Singh #endif
188ac26963aSBrijesh Singh 
189545695fbSTejun Heo /*
190545695fbSTejun Heo  * Intermodule exports for per-CPU variables.  sparse forgets about
191545695fbSTejun Heo  * address space across EXPORT_SYMBOL(), change EXPORT_SYMBOL() to
1925028eaa9SDavid Howells  * noop if __CHECKER__.
193545695fbSTejun Heo  */
194dd17c8f7SRusty Russell #ifndef __CHECKER__
195dd17c8f7SRusty Russell #define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(var)
196545695fbSTejun Heo #define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(var)
197545695fbSTejun Heo #else
198545695fbSTejun Heo #define EXPORT_PER_CPU_SYMBOL(var)
199545695fbSTejun Heo #define EXPORT_PER_CPU_SYMBOL_GPL(var)
2005028eaa9SDavid Howells #endif
20162fde541STejun Heo 
20262fde541STejun Heo /*
20362fde541STejun Heo  * Accessors and operations.
20462fde541STejun Heo  */
20562fde541STejun Heo #ifndef __ASSEMBLY__
2069c28278aSTejun Heo 
2076fbc07bbSTejun Heo /*
2086fbc07bbSTejun Heo  * __verify_pcpu_ptr() verifies @ptr is a percpu pointer without evaluating
2096fbc07bbSTejun Heo  * @ptr and is invoked once before a percpu area is accessed by all
2106fbc07bbSTejun Heo  * accessors and operations.  This is performed in the generic part of
2116fbc07bbSTejun Heo  * percpu and arch overrides don't need to worry about it; however, if an
2126fbc07bbSTejun Heo  * arch wants to implement an arch-specific percpu accessor or operation,
2139c28278aSTejun Heo  * it may use __verify_pcpu_ptr() to verify the parameters.
2149c28278aSTejun Heo  *
2159c28278aSTejun Heo  * + 0 is required in order to convert the pointer type from a
2169c28278aSTejun Heo  * potential array type to a pointer to a single item of the array.
217eba11788STejun Heo  */
218eba11788STejun Heo #define __verify_pcpu_ptr(ptr)						\
2199c28278aSTejun Heo do {									\
2209c28278aSTejun Heo 	const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL;	\
2219c28278aSTejun Heo 	(void)__vpp_verify;						\
2229c28278aSTejun Heo } while (0)
223001217deSUros Bizjak 
224*6a39fe05SUros Bizjak #define PERCPU_PTR(__p)							\
225001217deSUros Bizjak 	(TYPEOF_UNQUAL(*(__p)) __force __kernel *)((__force unsigned long)(__p))
22662fde541STejun Heo 
22762fde541STejun Heo #ifdef CONFIG_SMP
22862fde541STejun Heo 
229001217deSUros Bizjak /*
230001217deSUros Bizjak  * Add an offset to a pointer.  Use RELOC_HIDE() to prevent the compiler
23162fde541STejun Heo  * from making incorrect assumptions about the pointer value.
232eba11788STejun Heo  */
233001217deSUros Bizjak #define SHIFT_PERCPU_PTR(__p, __offset)					\
2346fbc07bbSTejun Heo 	RELOC_HIDE(PERCPU_PTR(__p), (__offset))
2356fbc07bbSTejun Heo 
236eba11788STejun Heo #define per_cpu_ptr(ptr, cpu)						\
2376fbc07bbSTejun Heo ({									\
2386fbc07bbSTejun Heo 	__verify_pcpu_ptr(ptr);						\
23962fde541STejun Heo 	SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)));			\
24062fde541STejun Heo })
2416fbc07bbSTejun Heo 
2426fbc07bbSTejun Heo #define raw_cpu_ptr(ptr)						\
2436fbc07bbSTejun Heo ({									\
2446fbc07bbSTejun Heo 	__verify_pcpu_ptr(ptr);						\
2456fbc07bbSTejun Heo 	arch_raw_cpu_ptr(ptr);						\
24662fde541STejun Heo })
24762fde541STejun Heo 
2486fbc07bbSTejun Heo #ifdef CONFIG_DEBUG_PREEMPT
2496fbc07bbSTejun Heo #define this_cpu_ptr(ptr)						\
2506fbc07bbSTejun Heo ({									\
2516fbc07bbSTejun Heo 	__verify_pcpu_ptr(ptr);						\
2526fbc07bbSTejun Heo 	SHIFT_PERCPU_PTR(ptr, my_cpu_offset);				\
25362fde541STejun Heo })
25462fde541STejun Heo #else
25562fde541STejun Heo #define this_cpu_ptr(ptr) raw_cpu_ptr(ptr)
25662fde541STejun Heo #endif
25762fde541STejun Heo 
25862fde541STejun Heo #else	/* CONFIG_SMP */
25974ef070eSUros Bizjak 
260eba11788STejun Heo #define per_cpu_ptr(ptr, cpu)						\
26174ef070eSUros Bizjak ({									\
26274ef070eSUros Bizjak 	(void)(cpu);							\
263001217deSUros Bizjak 	__verify_pcpu_ptr(ptr);						\
26462fde541STejun Heo 	PERCPU_PTR(ptr);						\
26562fde541STejun Heo })
2663b8ed91dSTejun Heo 
2673b8ed91dSTejun Heo #define raw_cpu_ptr(ptr)	per_cpu_ptr(ptr, 0)
26862fde541STejun Heo #define this_cpu_ptr(ptr)	raw_cpu_ptr(ptr)
26962fde541STejun Heo 
27062fde541STejun Heo #endif	/* CONFIG_SMP */
2713b8ed91dSTejun Heo 
2723b8ed91dSTejun Heo #define per_cpu(var, cpu)	(*per_cpu_ptr(&(var), cpu))
2739defda18STejun Heo 
2749defda18STejun Heo /*
2759defda18STejun Heo  * Must be an lvalue. Since @var must be a simple identifier,
2769defda18STejun Heo  * we force a syntax error here if it isn't.
277eba11788STejun Heo  */
278eba11788STejun Heo #define get_cpu_var(var)						\
2799defda18STejun Heo (*({									\
280eba11788STejun Heo 	preempt_disable();						\
281eba11788STejun Heo 	this_cpu_ptr(&var);						\
2829defda18STejun Heo }))
2839defda18STejun Heo 
2849defda18STejun Heo /*
2859defda18STejun Heo  * The weird & is necessary because sparse considers (void)(var) to be
2869defda18STejun Heo  * a direct dereference of percpu variable (var).
287eba11788STejun Heo  */
288eba11788STejun Heo #define put_cpu_var(var)						\
2899defda18STejun Heo do {									\
2909defda18STejun Heo 	(void)&(var);							\
2919defda18STejun Heo 	preempt_enable();						\
2929defda18STejun Heo } while (0)
293eba11788STejun Heo 
294eba11788STejun Heo #define get_cpu_ptr(var)						\
2959defda18STejun Heo ({									\
296eba11788STejun Heo 	preempt_disable();						\
297eba11788STejun Heo 	this_cpu_ptr(var);						\
2989defda18STejun Heo })
299eba11788STejun Heo 
300eba11788STejun Heo #define put_cpu_ptr(var)						\
3019defda18STejun Heo do {									\
3029defda18STejun Heo 	(void)(var);							\
3039defda18STejun Heo 	preempt_enable();						\
3049defda18STejun Heo } while (0)
305a32f8d8eSTejun Heo 
306a32f8d8eSTejun Heo /*
307a32f8d8eSTejun Heo  * Branching function to split up a function into a set of functions that
308a32f8d8eSTejun Heo  * are called for different scalar sizes of the objects handled.
309a32f8d8eSTejun Heo  */
310a32f8d8eSTejun Heo 
311a32f8d8eSTejun Heo extern void __bad_size_call_parameter(void);
312a32f8d8eSTejun Heo 
313a32f8d8eSTejun Heo #ifdef CONFIG_DEBUG_PREEMPT
314a32f8d8eSTejun Heo extern void __this_cpu_preempt_check(const char *op);
315f176d4ccSPeter Zijlstra #else
__this_cpu_preempt_check(const char * op)316a32f8d8eSTejun Heo static __always_inline void __this_cpu_preempt_check(const char *op) { }
317a32f8d8eSTejun Heo #endif
318a32f8d8eSTejun Heo 
319eba11788STejun Heo #define __pcpu_size_call_return(stem, variable)				\
3208a3c3923SUros Bizjak ({									\
321a32f8d8eSTejun Heo 	TYPEOF_UNQUAL(variable) pscr_ret__;				\
322a32f8d8eSTejun Heo 	__verify_pcpu_ptr(&(variable));					\
323a32f8d8eSTejun Heo 	switch(sizeof(variable)) {					\
324a32f8d8eSTejun Heo 	case 1: pscr_ret__ = stem##1(variable); break;			\
325a32f8d8eSTejun Heo 	case 2: pscr_ret__ = stem##2(variable); break;			\
326a32f8d8eSTejun Heo 	case 4: pscr_ret__ = stem##4(variable); break;			\
327a32f8d8eSTejun Heo 	case 8: pscr_ret__ = stem##8(variable); break;			\
328a32f8d8eSTejun Heo 	default:							\
329a32f8d8eSTejun Heo 		__bad_size_call_parameter(); break;			\
330a32f8d8eSTejun Heo 	}								\
331a32f8d8eSTejun Heo 	pscr_ret__;							\
332a32f8d8eSTejun Heo })
333a32f8d8eSTejun Heo 
334a32f8d8eSTejun Heo #define __pcpu_size_call_return2(stem, variable, ...)			\
3358a3c3923SUros Bizjak ({									\
336a32f8d8eSTejun Heo 	TYPEOF_UNQUAL(variable) pscr2_ret__;				\
337a32f8d8eSTejun Heo 	__verify_pcpu_ptr(&(variable));					\
338a32f8d8eSTejun Heo 	switch(sizeof(variable)) {					\
339a32f8d8eSTejun Heo 	case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break;	\
340a32f8d8eSTejun Heo 	case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break;	\
341a32f8d8eSTejun Heo 	case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break;	\
342a32f8d8eSTejun Heo 	case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break;	\
343a32f8d8eSTejun Heo 	default:							\
344a32f8d8eSTejun Heo 		__bad_size_call_parameter(); break;			\
345a32f8d8eSTejun Heo 	}								\
346a32f8d8eSTejun Heo 	pscr2_ret__;							\
347a32f8d8eSTejun Heo })
348c5c0ba95SPeter Zijlstra 
349c5c0ba95SPeter Zijlstra #define __pcpu_size_call_return2bool(stem, variable, ...)		\
350c5c0ba95SPeter Zijlstra ({									\
351c5c0ba95SPeter Zijlstra 	bool pscr2_ret__;						\
352c5c0ba95SPeter Zijlstra 	__verify_pcpu_ptr(&(variable));					\
353c5c0ba95SPeter Zijlstra 	switch(sizeof(variable)) {					\
354c5c0ba95SPeter Zijlstra 	case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break;	\
355c5c0ba95SPeter Zijlstra 	case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break;	\
356c5c0ba95SPeter Zijlstra 	case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break;	\
357c5c0ba95SPeter Zijlstra 	case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break;	\
358c5c0ba95SPeter Zijlstra 	default:							\
359c5c0ba95SPeter Zijlstra 		__bad_size_call_parameter(); break;			\
360c5c0ba95SPeter Zijlstra 	}								\
361c5c0ba95SPeter Zijlstra 	pscr2_ret__;							\
362c5c0ba95SPeter Zijlstra })
363a32f8d8eSTejun Heo 
364a32f8d8eSTejun Heo #define __pcpu_size_call(stem, variable, ...)				\
365a32f8d8eSTejun Heo do {									\
366a32f8d8eSTejun Heo 	__verify_pcpu_ptr(&(variable));					\
367a32f8d8eSTejun Heo 	switch(sizeof(variable)) {					\
368a32f8d8eSTejun Heo 		case 1: stem##1(variable, __VA_ARGS__);break;		\
369a32f8d8eSTejun Heo 		case 2: stem##2(variable, __VA_ARGS__);break;		\
370a32f8d8eSTejun Heo 		case 4: stem##4(variable, __VA_ARGS__);break;		\
371a32f8d8eSTejun Heo 		case 8: stem##8(variable, __VA_ARGS__);break;		\
372a32f8d8eSTejun Heo 		default: 						\
373a32f8d8eSTejun Heo 			__bad_size_call_parameter();break;		\
374a32f8d8eSTejun Heo 	}								\
375a32f8d8eSTejun Heo } while (0)
376a32f8d8eSTejun Heo 
377a32f8d8eSTejun Heo /*
378a32f8d8eSTejun Heo  * this_cpu operations (C) 2008-2013 Christoph Lameter <[email protected]>
379a32f8d8eSTejun Heo  *
380a32f8d8eSTejun Heo  * Optimized manipulation for memory allocated through the per cpu
381a32f8d8eSTejun Heo  * allocator or for addresses of per cpu variables.
382a32f8d8eSTejun Heo  *
383a32f8d8eSTejun Heo  * These operation guarantee exclusivity of access for other operations
384a32f8d8eSTejun Heo  * on the *same* processor. The assumption is that per cpu data is only
385a32f8d8eSTejun Heo  * accessed by a single processor instance (the current one).
386a32f8d8eSTejun Heo  *
387a32f8d8eSTejun Heo  * The arch code can provide optimized implementation by defining macros
388a32f8d8eSTejun Heo  * for certain scalar sizes. F.e. provide this_cpu_add_2() to provide per
389a32f8d8eSTejun Heo  * cpu atomic operations for 2 byte sized RMW actions. If arch code does
390a32f8d8eSTejun Heo  * not provide operations for a scalar size then the fallback in the
391eba11788STejun Heo  * generic code will be used.
392eba11788STejun Heo  *
393eba11788STejun Heo  * cmpxchg_double replaces two adjacent scalars at once.  The first two
394eba11788STejun Heo  * parameters are per cpu variables which have to be of the same size.  A
395eba11788STejun Heo  * truth value is returned to indicate success or failure (since a double
396eba11788STejun Heo  * register result is difficult to handle).  There is very limited hardware
397a32f8d8eSTejun Heo  * support for these operations, so only certain sizes may work.
398a32f8d8eSTejun Heo  */
399a32f8d8eSTejun Heo 
400eba11788STejun Heo /*
401eba11788STejun Heo  * Operations for contexts where we do not want to do any checks for
402eba11788STejun Heo  * preemptions.  Unless strictly necessary, always use [__]this_cpu_*()
403a32f8d8eSTejun Heo  * instead.
404eba11788STejun Heo  *
40506c88398SZhen Lei  * If there is no other protection through preempt disable and/or disabling
406eba11788STejun Heo  * interrupts then one of these RMW operations can show unexpected behavior
407eba11788STejun Heo  * because the execution thread was rescheduled on another processor or an
408eba11788STejun Heo  * interrupt occurred and the same percpu variable was modified from the
409a32f8d8eSTejun Heo  * interrupt context.
410eba11788STejun Heo  */
411eba11788STejun Heo #define raw_cpu_read(pcp)		__pcpu_size_call_return(raw_cpu_read_, pcp)
412eba11788STejun Heo #define raw_cpu_write(pcp, val)		__pcpu_size_call(raw_cpu_write_, pcp, val)
413eba11788STejun Heo #define raw_cpu_add(pcp, val)		__pcpu_size_call(raw_cpu_add_, pcp, val)
414eba11788STejun Heo #define raw_cpu_and(pcp, val)		__pcpu_size_call(raw_cpu_and_, pcp, val)
415eba11788STejun Heo #define raw_cpu_or(pcp, val)		__pcpu_size_call(raw_cpu_or_, pcp, val)
416eba11788STejun Heo #define raw_cpu_add_return(pcp, val)	__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
417a32f8d8eSTejun Heo #define raw_cpu_xchg(pcp, nval)		__pcpu_size_call_return2(raw_cpu_xchg_, pcp, nval)
418a32f8d8eSTejun Heo #define raw_cpu_cmpxchg(pcp, oval, nval) \
419c5c0ba95SPeter Zijlstra 	__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
420c5c0ba95SPeter Zijlstra #define raw_cpu_try_cmpxchg(pcp, ovalp, nval) \
421eba11788STejun Heo 	__pcpu_size_call_return2bool(raw_cpu_try_cmpxchg_, pcp, ovalp, nval)
422eba11788STejun Heo #define raw_cpu_sub(pcp, val)		raw_cpu_add(pcp, -(val))
423eba11788STejun Heo #define raw_cpu_inc(pcp)		raw_cpu_add(pcp, 1)
424eba11788STejun Heo #define raw_cpu_dec(pcp)		raw_cpu_sub(pcp, 1)
425eba11788STejun Heo #define raw_cpu_sub_return(pcp, val)	raw_cpu_add_return(pcp, -(typeof(pcp))(val))
426eba11788STejun Heo #define raw_cpu_inc_return(pcp)		raw_cpu_add_return(pcp, 1)
427a32f8d8eSTejun Heo #define raw_cpu_dec_return(pcp)		raw_cpu_add_return(pcp, -1)
428a32f8d8eSTejun Heo 
429eba11788STejun Heo /*
430eba11788STejun Heo  * Operations for contexts that are safe from preemption/interrupts.  These
431a32f8d8eSTejun Heo  * operations verify that preemption is disabled.
432a32f8d8eSTejun Heo  */
433eba11788STejun Heo #define __this_cpu_read(pcp)						\
434eba11788STejun Heo ({									\
435eba11788STejun Heo 	__this_cpu_preempt_check("read");				\
436eba11788STejun Heo 	raw_cpu_read(pcp);						\
437a32f8d8eSTejun Heo })
438a32f8d8eSTejun Heo 
439eba11788STejun Heo #define __this_cpu_write(pcp, val)					\
440eba11788STejun Heo ({									\
441cadb1c4dSTejun Heo 	__this_cpu_preempt_check("write");				\
442eba11788STejun Heo 	raw_cpu_write(pcp, val);					\
443a32f8d8eSTejun Heo })
444a32f8d8eSTejun Heo 
445eba11788STejun Heo #define __this_cpu_add(pcp, val)					\
446eba11788STejun Heo ({									\
447cadb1c4dSTejun Heo 	__this_cpu_preempt_check("add");				\
448eba11788STejun Heo 	raw_cpu_add(pcp, val);						\
449a32f8d8eSTejun Heo })
450a32f8d8eSTejun Heo 
451eba11788STejun Heo #define __this_cpu_and(pcp, val)					\
452eba11788STejun Heo ({									\
453cadb1c4dSTejun Heo 	__this_cpu_preempt_check("and");				\
454eba11788STejun Heo 	raw_cpu_and(pcp, val);						\
455a32f8d8eSTejun Heo })
456a32f8d8eSTejun Heo 
457eba11788STejun Heo #define __this_cpu_or(pcp, val)						\
458eba11788STejun Heo ({									\
459cadb1c4dSTejun Heo 	__this_cpu_preempt_check("or");					\
460eba11788STejun Heo 	raw_cpu_or(pcp, val);						\
461a32f8d8eSTejun Heo })
462a32f8d8eSTejun Heo 
463eba11788STejun Heo #define __this_cpu_add_return(pcp, val)					\
464eba11788STejun Heo ({									\
465eba11788STejun Heo 	__this_cpu_preempt_check("add_return");				\
466eba11788STejun Heo 	raw_cpu_add_return(pcp, val);					\
467a32f8d8eSTejun Heo })
468eba11788STejun Heo 
469eba11788STejun Heo #define __this_cpu_xchg(pcp, nval)					\
470eba11788STejun Heo ({									\
471eba11788STejun Heo 	__this_cpu_preempt_check("xchg");				\
472eba11788STejun Heo 	raw_cpu_xchg(pcp, nval);					\
473eba11788STejun Heo })
474eba11788STejun Heo 
475eba11788STejun Heo #define __this_cpu_cmpxchg(pcp, oval, nval)				\
476eba11788STejun Heo ({									\
477eba11788STejun Heo 	__this_cpu_preempt_check("cmpxchg");				\
478eba11788STejun Heo 	raw_cpu_cmpxchg(pcp, oval, nval);				\
479eba11788STejun Heo })
48021664442SUros Bizjak 
48121664442SUros Bizjak #define __this_cpu_try_cmpxchg(pcp, ovalp, nval)			\
48221664442SUros Bizjak ({									\
48321664442SUros Bizjak 	__this_cpu_preempt_check("try_cmpxchg");			\
48421664442SUros Bizjak 	raw_cpu_try_cmpxchg(pcp, ovalp, nval);				\
48521664442SUros Bizjak })
486eba11788STejun Heo 
487eba11788STejun Heo #define __this_cpu_sub(pcp, val)	__this_cpu_add(pcp, -(typeof(pcp))(val))
488eba11788STejun Heo #define __this_cpu_inc(pcp)		__this_cpu_add(pcp, 1)
489a32f8d8eSTejun Heo #define __this_cpu_dec(pcp)		__this_cpu_sub(pcp, 1)
490a32f8d8eSTejun Heo #define __this_cpu_sub_return(pcp, val)	__this_cpu_add_return(pcp, -(typeof(pcp))(val))
491a32f8d8eSTejun Heo #define __this_cpu_inc_return(pcp)	__this_cpu_add_return(pcp, 1)
492a32f8d8eSTejun Heo #define __this_cpu_dec_return(pcp)	__this_cpu_add_return(pcp, -1)
493a32f8d8eSTejun Heo 
49483cb8557STejun Heo /*
49583cb8557STejun Heo  * Operations with implied preemption/interrupt protection.  These
496a32f8d8eSTejun Heo  * operations can be used without worrying about preemption or interrupt.
497eba11788STejun Heo  */
498eba11788STejun Heo #define this_cpu_read(pcp)		__pcpu_size_call_return(this_cpu_read_, pcp)
499eba11788STejun Heo #define this_cpu_write(pcp, val)	__pcpu_size_call(this_cpu_write_, pcp, val)
500eba11788STejun Heo #define this_cpu_add(pcp, val)		__pcpu_size_call(this_cpu_add_, pcp, val)
501eba11788STejun Heo #define this_cpu_and(pcp, val)		__pcpu_size_call(this_cpu_and_, pcp, val)
502a32f8d8eSTejun Heo #define this_cpu_or(pcp, val)		__pcpu_size_call(this_cpu_or_, pcp, val)
503eba11788STejun Heo #define this_cpu_add_return(pcp, val)	__pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
504eba11788STejun Heo #define this_cpu_xchg(pcp, nval)	__pcpu_size_call_return2(this_cpu_xchg_, pcp, nval)
505eba11788STejun Heo #define this_cpu_cmpxchg(pcp, oval, nval) \
506c5c0ba95SPeter Zijlstra 	__pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
507c5c0ba95SPeter Zijlstra #define this_cpu_try_cmpxchg(pcp, ovalp, nval) \
508eba11788STejun Heo 	__pcpu_size_call_return2bool(this_cpu_try_cmpxchg_, pcp, ovalp, nval)
509eba11788STejun Heo #define this_cpu_sub(pcp, val)		this_cpu_add(pcp, -(typeof(pcp))(val))
510eba11788STejun Heo #define this_cpu_inc(pcp)		this_cpu_add(pcp, 1)
511a32f8d8eSTejun Heo #define this_cpu_dec(pcp)		this_cpu_sub(pcp, 1)
512a32f8d8eSTejun Heo #define this_cpu_sub_return(pcp, val)	this_cpu_add_return(pcp, -(typeof(pcp))(val))
513a32f8d8eSTejun Heo #define this_cpu_inc_return(pcp)	this_cpu_add_return(pcp, 1)
514a32f8d8eSTejun Heo #define this_cpu_dec_return(pcp)	this_cpu_add_return(pcp, -1)
51562fde541STejun Heo 
5165028eaa9SDavid Howells #endif /* __ASSEMBLY__ */
517 #endif /* _LINUX_PERCPU_DEFS_H */
518