18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/percpu-defs.h - basic definitions for percpu areas 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * DO NOT INCLUDE DIRECTLY OUTSIDE PERCPU IMPLEMENTATION PROPER. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file is separate from linux/percpu.h to avoid cyclic inclusion 88c2ecf20Sopenharmony_ci * dependency from arch header files. Only to be included from 98c2ecf20Sopenharmony_ci * asm/percpu.h. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This file includes macros necessary to declare percpu sections and 128c2ecf20Sopenharmony_ci * variables, and definitions of percpu accessors and operations. It 138c2ecf20Sopenharmony_ci * should provide enough percpu features to arch header files even when 148c2ecf20Sopenharmony_ci * they can only include asm/percpu.h to avoid cyclic inclusion dependency. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#ifndef _LINUX_PERCPU_DEFS_H 188c2ecf20Sopenharmony_ci#define _LINUX_PERCPU_DEFS_H 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#ifdef MODULE 238c2ecf20Sopenharmony_ci#define PER_CPU_SHARED_ALIGNED_SECTION "" 248c2ecf20Sopenharmony_ci#define PER_CPU_ALIGNED_SECTION "" 258c2ecf20Sopenharmony_ci#else 268c2ecf20Sopenharmony_ci#define PER_CPU_SHARED_ALIGNED_SECTION "..shared_aligned" 278c2ecf20Sopenharmony_ci#define PER_CPU_ALIGNED_SECTION "..shared_aligned" 288c2ecf20Sopenharmony_ci#endif 298c2ecf20Sopenharmony_ci#define PER_CPU_FIRST_SECTION "..first" 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#else 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define PER_CPU_SHARED_ALIGNED_SECTION "" 348c2ecf20Sopenharmony_ci#define PER_CPU_ALIGNED_SECTION "..shared_aligned" 358c2ecf20Sopenharmony_ci#define PER_CPU_FIRST_SECTION "" 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#endif 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* 408c2ecf20Sopenharmony_ci * Base implementations of per-CPU variable declarations and definitions, where 418c2ecf20Sopenharmony_ci * the section in which the variable is to be placed is provided by the 428c2ecf20Sopenharmony_ci * 'sec' argument. This may be used to affect the parameters governing the 438c2ecf20Sopenharmony_ci * variable's storage. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * NOTE! The sections for the DECLARE and for the DEFINE must match, lest 468c2ecf20Sopenharmony_ci * linkage errors occur due the compiler generating the wrong code to access 478c2ecf20Sopenharmony_ci * that section. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci#define __PCPU_ATTRS(sec) \ 508c2ecf20Sopenharmony_ci __percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \ 518c2ecf20Sopenharmony_ci PER_CPU_ATTRIBUTES 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define __PCPU_DUMMY_ATTRS \ 548c2ecf20Sopenharmony_ci __section(".discard") __attribute__((unused)) 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * s390 and alpha modules require percpu variables to be defined as 588c2ecf20Sopenharmony_ci * weak to force the compiler to generate GOT based external 598c2ecf20Sopenharmony_ci * references for them. This is necessary because percpu sections 608c2ecf20Sopenharmony_ci * will be located outside of the usually addressable area. 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * This definition puts the following two extra restrictions when 638c2ecf20Sopenharmony_ci * defining percpu variables. 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * 1. The symbol must be globally unique, even the static ones. 668c2ecf20Sopenharmony_ci * 2. Static percpu variables cannot be defined inside a function. 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * Archs which need weak percpu definitions should define 698c2ecf20Sopenharmony_ci * ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary. 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * To ensure that the generic code observes the above two 728c2ecf20Sopenharmony_ci * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak 738c2ecf20Sopenharmony_ci * definition is used for all cases. 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_ci#if defined(ARCH_NEEDS_WEAK_PER_CPU) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU) 768c2ecf20Sopenharmony_ci/* 778c2ecf20Sopenharmony_ci * __pcpu_scope_* dummy variable is used to enforce scope. It 788c2ecf20Sopenharmony_ci * receives the static modifier when it's used in front of 798c2ecf20Sopenharmony_ci * DEFINE_PER_CPU() and will trigger build failure if 808c2ecf20Sopenharmony_ci * DECLARE_PER_CPU() is used for the same variable. 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * __pcpu_unique_* dummy variable is used to enforce symbol uniqueness 838c2ecf20Sopenharmony_ci * such that hidden weak symbol collision, which will cause unrelated 848c2ecf20Sopenharmony_ci * variables to share the same address, can be detected during build. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_SECTION(type, name, sec) \ 878c2ecf20Sopenharmony_ci extern __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \ 888c2ecf20Sopenharmony_ci extern __PCPU_ATTRS(sec) __typeof__(type) name 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_SECTION(type, name, sec) \ 918c2ecf20Sopenharmony_ci __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \ 928c2ecf20Sopenharmony_ci extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \ 938c2ecf20Sopenharmony_ci __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \ 948c2ecf20Sopenharmony_ci extern __PCPU_ATTRS(sec) __typeof__(type) name; \ 958c2ecf20Sopenharmony_ci __PCPU_ATTRS(sec) __weak __typeof__(type) name 968c2ecf20Sopenharmony_ci#else 978c2ecf20Sopenharmony_ci/* 988c2ecf20Sopenharmony_ci * Normal declaration and definition macros. 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_SECTION(type, name, sec) \ 1018c2ecf20Sopenharmony_ci extern __PCPU_ATTRS(sec) __typeof__(type) name 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_SECTION(type, name, sec) \ 1048c2ecf20Sopenharmony_ci __PCPU_ATTRS(sec) __typeof__(type) name 1058c2ecf20Sopenharmony_ci#endif 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/* 1088c2ecf20Sopenharmony_ci * Variant on the per-CPU variable declaration/definition theme used for 1098c2ecf20Sopenharmony_ci * ordinary per-CPU variables. 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU(type, name) \ 1128c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, "") 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU(type, name) \ 1158c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, "") 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* 1188c2ecf20Sopenharmony_ci * Declaration/definition used for per-CPU variables that must come first in 1198c2ecf20Sopenharmony_ci * the set of variables. 1208c2ecf20Sopenharmony_ci */ 1218c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_FIRST(type, name) \ 1228c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION) 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_FIRST(type, name) \ 1258c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION) 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/* 1288c2ecf20Sopenharmony_ci * Declaration/definition used for per-CPU variables that must be cacheline 1298c2ecf20Sopenharmony_ci * aligned under SMP conditions so that, whilst a particular instance of the 1308c2ecf20Sopenharmony_ci * data corresponds to a particular CPU, inefficiencies due to direct access by 1318c2ecf20Sopenharmony_ci * other CPUs are reduced by preventing the data from unnecessarily spanning 1328c2ecf20Sopenharmony_ci * cachelines. 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * An example of this would be statistical data, where each CPU's set of data 1358c2ecf20Sopenharmony_ci * is updated by that CPU alone, but the data from across all CPUs is collated 1368c2ecf20Sopenharmony_ci * by a CPU processing a read from a proc file. 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_SHARED_ALIGNED(type, name) \ 1398c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \ 1408c2ecf20Sopenharmony_ci ____cacheline_aligned_in_smp 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_SHARED_ALIGNED(type, name) \ 1438c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, PER_CPU_SHARED_ALIGNED_SECTION) \ 1448c2ecf20Sopenharmony_ci ____cacheline_aligned_in_smp 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_ALIGNED(type, name) \ 1478c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \ 1488c2ecf20Sopenharmony_ci ____cacheline_aligned 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_ALIGNED(type, name) \ 1518c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, PER_CPU_ALIGNED_SECTION) \ 1528c2ecf20Sopenharmony_ci ____cacheline_aligned 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* 1558c2ecf20Sopenharmony_ci * Declaration/definition used for per-CPU variables that must be page aligned. 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_PAGE_ALIGNED(type, name) \ 1588c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, "..page_aligned") \ 1598c2ecf20Sopenharmony_ci __aligned(PAGE_SIZE) 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \ 1628c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \ 1638c2ecf20Sopenharmony_ci __aligned(PAGE_SIZE) 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* 1668c2ecf20Sopenharmony_ci * Declaration/definition used for per-CPU variables that must be read mostly. 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_READ_MOSTLY(type, name) \ 1698c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, "..read_mostly") 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_READ_MOSTLY(type, name) \ 1728c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, "..read_mostly") 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci/* 1758c2ecf20Sopenharmony_ci * Declaration/definition used for per-CPU variables that should be accessed 1768c2ecf20Sopenharmony_ci * as decrypted when memory encryption is enabled in the guest. 1778c2ecf20Sopenharmony_ci */ 1788c2ecf20Sopenharmony_ci#ifdef CONFIG_AMD_MEM_ENCRYPT 1798c2ecf20Sopenharmony_ci#define DECLARE_PER_CPU_DECRYPTED(type, name) \ 1808c2ecf20Sopenharmony_ci DECLARE_PER_CPU_SECTION(type, name, "..decrypted") 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_DECRYPTED(type, name) \ 1838c2ecf20Sopenharmony_ci DEFINE_PER_CPU_SECTION(type, name, "..decrypted") 1848c2ecf20Sopenharmony_ci#else 1858c2ecf20Sopenharmony_ci#define DEFINE_PER_CPU_DECRYPTED(type, name) DEFINE_PER_CPU(type, name) 1868c2ecf20Sopenharmony_ci#endif 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci/* 1898c2ecf20Sopenharmony_ci * Intermodule exports for per-CPU variables. sparse forgets about 1908c2ecf20Sopenharmony_ci * address space across EXPORT_SYMBOL(), change EXPORT_SYMBOL() to 1918c2ecf20Sopenharmony_ci * noop if __CHECKER__. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_ci#ifndef __CHECKER__ 1948c2ecf20Sopenharmony_ci#define EXPORT_PER_CPU_SYMBOL(var) EXPORT_SYMBOL(var) 1958c2ecf20Sopenharmony_ci#define EXPORT_PER_CPU_SYMBOL_GPL(var) EXPORT_SYMBOL_GPL(var) 1968c2ecf20Sopenharmony_ci#else 1978c2ecf20Sopenharmony_ci#define EXPORT_PER_CPU_SYMBOL(var) 1988c2ecf20Sopenharmony_ci#define EXPORT_PER_CPU_SYMBOL_GPL(var) 1998c2ecf20Sopenharmony_ci#endif 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* 2028c2ecf20Sopenharmony_ci * Accessors and operations. 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci/* 2078c2ecf20Sopenharmony_ci * __verify_pcpu_ptr() verifies @ptr is a percpu pointer without evaluating 2088c2ecf20Sopenharmony_ci * @ptr and is invoked once before a percpu area is accessed by all 2098c2ecf20Sopenharmony_ci * accessors and operations. This is performed in the generic part of 2108c2ecf20Sopenharmony_ci * percpu and arch overrides don't need to worry about it; however, if an 2118c2ecf20Sopenharmony_ci * arch wants to implement an arch-specific percpu accessor or operation, 2128c2ecf20Sopenharmony_ci * it may use __verify_pcpu_ptr() to verify the parameters. 2138c2ecf20Sopenharmony_ci * 2148c2ecf20Sopenharmony_ci * + 0 is required in order to convert the pointer type from a 2158c2ecf20Sopenharmony_ci * potential array type to a pointer to a single item of the array. 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_ci#define __verify_pcpu_ptr(ptr) \ 2188c2ecf20Sopenharmony_cido { \ 2198c2ecf20Sopenharmony_ci const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \ 2208c2ecf20Sopenharmony_ci (void)__vpp_verify; \ 2218c2ecf20Sopenharmony_ci} while (0) 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/* 2268c2ecf20Sopenharmony_ci * Add an offset to a pointer but keep the pointer as-is. Use RELOC_HIDE() 2278c2ecf20Sopenharmony_ci * to prevent the compiler from making incorrect assumptions about the 2288c2ecf20Sopenharmony_ci * pointer value. The weird cast keeps both GCC and sparse happy. 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci#define SHIFT_PERCPU_PTR(__p, __offset) \ 2318c2ecf20Sopenharmony_ci RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset)) 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci#define per_cpu_ptr(ptr, cpu) \ 2348c2ecf20Sopenharmony_ci({ \ 2358c2ecf20Sopenharmony_ci __verify_pcpu_ptr(ptr); \ 2368c2ecf20Sopenharmony_ci SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu))); \ 2378c2ecf20Sopenharmony_ci}) 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci#define raw_cpu_ptr(ptr) \ 2408c2ecf20Sopenharmony_ci({ \ 2418c2ecf20Sopenharmony_ci __verify_pcpu_ptr(ptr); \ 2428c2ecf20Sopenharmony_ci arch_raw_cpu_ptr(ptr); \ 2438c2ecf20Sopenharmony_ci}) 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_PREEMPT 2468c2ecf20Sopenharmony_ci#define this_cpu_ptr(ptr) \ 2478c2ecf20Sopenharmony_ci({ \ 2488c2ecf20Sopenharmony_ci __verify_pcpu_ptr(ptr); \ 2498c2ecf20Sopenharmony_ci SHIFT_PERCPU_PTR(ptr, my_cpu_offset); \ 2508c2ecf20Sopenharmony_ci}) 2518c2ecf20Sopenharmony_ci#else 2528c2ecf20Sopenharmony_ci#define this_cpu_ptr(ptr) raw_cpu_ptr(ptr) 2538c2ecf20Sopenharmony_ci#endif 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci#else /* CONFIG_SMP */ 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci#define VERIFY_PERCPU_PTR(__p) \ 2588c2ecf20Sopenharmony_ci({ \ 2598c2ecf20Sopenharmony_ci __verify_pcpu_ptr(__p); \ 2608c2ecf20Sopenharmony_ci (typeof(*(__p)) __kernel __force *)(__p); \ 2618c2ecf20Sopenharmony_ci}) 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci#define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); }) 2648c2ecf20Sopenharmony_ci#define raw_cpu_ptr(ptr) per_cpu_ptr(ptr, 0) 2658c2ecf20Sopenharmony_ci#define this_cpu_ptr(ptr) raw_cpu_ptr(ptr) 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci#endif /* CONFIG_SMP */ 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci#define per_cpu(var, cpu) (*per_cpu_ptr(&(var), cpu)) 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci/* 2728c2ecf20Sopenharmony_ci * Must be an lvalue. Since @var must be a simple identifier, 2738c2ecf20Sopenharmony_ci * we force a syntax error here if it isn't. 2748c2ecf20Sopenharmony_ci */ 2758c2ecf20Sopenharmony_ci#define get_cpu_var(var) \ 2768c2ecf20Sopenharmony_ci(*({ \ 2778c2ecf20Sopenharmony_ci preempt_disable(); \ 2788c2ecf20Sopenharmony_ci this_cpu_ptr(&var); \ 2798c2ecf20Sopenharmony_ci})) 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci/* 2828c2ecf20Sopenharmony_ci * The weird & is necessary because sparse considers (void)(var) to be 2838c2ecf20Sopenharmony_ci * a direct dereference of percpu variable (var). 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_ci#define put_cpu_var(var) \ 2868c2ecf20Sopenharmony_cido { \ 2878c2ecf20Sopenharmony_ci (void)&(var); \ 2888c2ecf20Sopenharmony_ci preempt_enable(); \ 2898c2ecf20Sopenharmony_ci} while (0) 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci#define get_cpu_ptr(var) \ 2928c2ecf20Sopenharmony_ci({ \ 2938c2ecf20Sopenharmony_ci preempt_disable(); \ 2948c2ecf20Sopenharmony_ci this_cpu_ptr(var); \ 2958c2ecf20Sopenharmony_ci}) 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci#define put_cpu_ptr(var) \ 2988c2ecf20Sopenharmony_cido { \ 2998c2ecf20Sopenharmony_ci (void)(var); \ 3008c2ecf20Sopenharmony_ci preempt_enable(); \ 3018c2ecf20Sopenharmony_ci} while (0) 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci/* 3048c2ecf20Sopenharmony_ci * Branching function to split up a function into a set of functions that 3058c2ecf20Sopenharmony_ci * are called for different scalar sizes of the objects handled. 3068c2ecf20Sopenharmony_ci */ 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ciextern void __bad_size_call_parameter(void); 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_PREEMPT 3118c2ecf20Sopenharmony_ciextern void __this_cpu_preempt_check(const char *op); 3128c2ecf20Sopenharmony_ci#else 3138c2ecf20Sopenharmony_cistatic inline void __this_cpu_preempt_check(const char *op) { } 3148c2ecf20Sopenharmony_ci#endif 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci#define __pcpu_size_call_return(stem, variable) \ 3178c2ecf20Sopenharmony_ci({ \ 3188c2ecf20Sopenharmony_ci typeof(variable) pscr_ret__; \ 3198c2ecf20Sopenharmony_ci __verify_pcpu_ptr(&(variable)); \ 3208c2ecf20Sopenharmony_ci switch(sizeof(variable)) { \ 3218c2ecf20Sopenharmony_ci case 1: pscr_ret__ = stem##1(variable); break; \ 3228c2ecf20Sopenharmony_ci case 2: pscr_ret__ = stem##2(variable); break; \ 3238c2ecf20Sopenharmony_ci case 4: pscr_ret__ = stem##4(variable); break; \ 3248c2ecf20Sopenharmony_ci case 8: pscr_ret__ = stem##8(variable); break; \ 3258c2ecf20Sopenharmony_ci default: \ 3268c2ecf20Sopenharmony_ci __bad_size_call_parameter(); break; \ 3278c2ecf20Sopenharmony_ci } \ 3288c2ecf20Sopenharmony_ci pscr_ret__; \ 3298c2ecf20Sopenharmony_ci}) 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci#define __pcpu_size_call_return2(stem, variable, ...) \ 3328c2ecf20Sopenharmony_ci({ \ 3338c2ecf20Sopenharmony_ci typeof(variable) pscr2_ret__; \ 3348c2ecf20Sopenharmony_ci __verify_pcpu_ptr(&(variable)); \ 3358c2ecf20Sopenharmony_ci switch(sizeof(variable)) { \ 3368c2ecf20Sopenharmony_ci case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break; \ 3378c2ecf20Sopenharmony_ci case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break; \ 3388c2ecf20Sopenharmony_ci case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break; \ 3398c2ecf20Sopenharmony_ci case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break; \ 3408c2ecf20Sopenharmony_ci default: \ 3418c2ecf20Sopenharmony_ci __bad_size_call_parameter(); break; \ 3428c2ecf20Sopenharmony_ci } \ 3438c2ecf20Sopenharmony_ci pscr2_ret__; \ 3448c2ecf20Sopenharmony_ci}) 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci/* 3478c2ecf20Sopenharmony_ci * Special handling for cmpxchg_double. cmpxchg_double is passed two 3488c2ecf20Sopenharmony_ci * percpu variables. The first has to be aligned to a double word 3498c2ecf20Sopenharmony_ci * boundary and the second has to follow directly thereafter. 3508c2ecf20Sopenharmony_ci * We enforce this on all architectures even if they don't support 3518c2ecf20Sopenharmony_ci * a double cmpxchg instruction, since it's a cheap requirement, and it 3528c2ecf20Sopenharmony_ci * avoids breaking the requirement for architectures with the instruction. 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_ci#define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...) \ 3558c2ecf20Sopenharmony_ci({ \ 3568c2ecf20Sopenharmony_ci bool pdcrb_ret__; \ 3578c2ecf20Sopenharmony_ci __verify_pcpu_ptr(&(pcp1)); \ 3588c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2)); \ 3598c2ecf20Sopenharmony_ci VM_BUG_ON((unsigned long)(&(pcp1)) % (2 * sizeof(pcp1))); \ 3608c2ecf20Sopenharmony_ci VM_BUG_ON((unsigned long)(&(pcp2)) != \ 3618c2ecf20Sopenharmony_ci (unsigned long)(&(pcp1)) + sizeof(pcp1)); \ 3628c2ecf20Sopenharmony_ci switch(sizeof(pcp1)) { \ 3638c2ecf20Sopenharmony_ci case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break; \ 3648c2ecf20Sopenharmony_ci case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break; \ 3658c2ecf20Sopenharmony_ci case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break; \ 3668c2ecf20Sopenharmony_ci case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break; \ 3678c2ecf20Sopenharmony_ci default: \ 3688c2ecf20Sopenharmony_ci __bad_size_call_parameter(); break; \ 3698c2ecf20Sopenharmony_ci } \ 3708c2ecf20Sopenharmony_ci pdcrb_ret__; \ 3718c2ecf20Sopenharmony_ci}) 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci#define __pcpu_size_call(stem, variable, ...) \ 3748c2ecf20Sopenharmony_cido { \ 3758c2ecf20Sopenharmony_ci __verify_pcpu_ptr(&(variable)); \ 3768c2ecf20Sopenharmony_ci switch(sizeof(variable)) { \ 3778c2ecf20Sopenharmony_ci case 1: stem##1(variable, __VA_ARGS__);break; \ 3788c2ecf20Sopenharmony_ci case 2: stem##2(variable, __VA_ARGS__);break; \ 3798c2ecf20Sopenharmony_ci case 4: stem##4(variable, __VA_ARGS__);break; \ 3808c2ecf20Sopenharmony_ci case 8: stem##8(variable, __VA_ARGS__);break; \ 3818c2ecf20Sopenharmony_ci default: \ 3828c2ecf20Sopenharmony_ci __bad_size_call_parameter();break; \ 3838c2ecf20Sopenharmony_ci } \ 3848c2ecf20Sopenharmony_ci} while (0) 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci/* 3878c2ecf20Sopenharmony_ci * this_cpu operations (C) 2008-2013 Christoph Lameter <cl@linux.com> 3888c2ecf20Sopenharmony_ci * 3898c2ecf20Sopenharmony_ci * Optimized manipulation for memory allocated through the per cpu 3908c2ecf20Sopenharmony_ci * allocator or for addresses of per cpu variables. 3918c2ecf20Sopenharmony_ci * 3928c2ecf20Sopenharmony_ci * These operation guarantee exclusivity of access for other operations 3938c2ecf20Sopenharmony_ci * on the *same* processor. The assumption is that per cpu data is only 3948c2ecf20Sopenharmony_ci * accessed by a single processor instance (the current one). 3958c2ecf20Sopenharmony_ci * 3968c2ecf20Sopenharmony_ci * The arch code can provide optimized implementation by defining macros 3978c2ecf20Sopenharmony_ci * for certain scalar sizes. F.e. provide this_cpu_add_2() to provide per 3988c2ecf20Sopenharmony_ci * cpu atomic operations for 2 byte sized RMW actions. If arch code does 3998c2ecf20Sopenharmony_ci * not provide operations for a scalar size then the fallback in the 4008c2ecf20Sopenharmony_ci * generic code will be used. 4018c2ecf20Sopenharmony_ci * 4028c2ecf20Sopenharmony_ci * cmpxchg_double replaces two adjacent scalars at once. The first two 4038c2ecf20Sopenharmony_ci * parameters are per cpu variables which have to be of the same size. A 4048c2ecf20Sopenharmony_ci * truth value is returned to indicate success or failure (since a double 4058c2ecf20Sopenharmony_ci * register result is difficult to handle). There is very limited hardware 4068c2ecf20Sopenharmony_ci * support for these operations, so only certain sizes may work. 4078c2ecf20Sopenharmony_ci */ 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci/* 4108c2ecf20Sopenharmony_ci * Operations for contexts where we do not want to do any checks for 4118c2ecf20Sopenharmony_ci * preemptions. Unless strictly necessary, always use [__]this_cpu_*() 4128c2ecf20Sopenharmony_ci * instead. 4138c2ecf20Sopenharmony_ci * 4148c2ecf20Sopenharmony_ci * If there is no other protection through preempt disable and/or disabling 4158c2ecf20Sopenharmony_ci * interupts then one of these RMW operations can show unexpected behavior 4168c2ecf20Sopenharmony_ci * because the execution thread was rescheduled on another processor or an 4178c2ecf20Sopenharmony_ci * interrupt occurred and the same percpu variable was modified from the 4188c2ecf20Sopenharmony_ci * interrupt context. 4198c2ecf20Sopenharmony_ci */ 4208c2ecf20Sopenharmony_ci#define raw_cpu_read(pcp) __pcpu_size_call_return(raw_cpu_read_, pcp) 4218c2ecf20Sopenharmony_ci#define raw_cpu_write(pcp, val) __pcpu_size_call(raw_cpu_write_, pcp, val) 4228c2ecf20Sopenharmony_ci#define raw_cpu_add(pcp, val) __pcpu_size_call(raw_cpu_add_, pcp, val) 4238c2ecf20Sopenharmony_ci#define raw_cpu_and(pcp, val) __pcpu_size_call(raw_cpu_and_, pcp, val) 4248c2ecf20Sopenharmony_ci#define raw_cpu_or(pcp, val) __pcpu_size_call(raw_cpu_or_, pcp, val) 4258c2ecf20Sopenharmony_ci#define raw_cpu_add_return(pcp, val) __pcpu_size_call_return2(raw_cpu_add_return_, pcp, val) 4268c2ecf20Sopenharmony_ci#define raw_cpu_xchg(pcp, nval) __pcpu_size_call_return2(raw_cpu_xchg_, pcp, nval) 4278c2ecf20Sopenharmony_ci#define raw_cpu_cmpxchg(pcp, oval, nval) \ 4288c2ecf20Sopenharmony_ci __pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval) 4298c2ecf20Sopenharmony_ci#define raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \ 4308c2ecf20Sopenharmony_ci __pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, pcp1, pcp2, oval1, oval2, nval1, nval2) 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci#define raw_cpu_sub(pcp, val) raw_cpu_add(pcp, -(val)) 4338c2ecf20Sopenharmony_ci#define raw_cpu_inc(pcp) raw_cpu_add(pcp, 1) 4348c2ecf20Sopenharmony_ci#define raw_cpu_dec(pcp) raw_cpu_sub(pcp, 1) 4358c2ecf20Sopenharmony_ci#define raw_cpu_sub_return(pcp, val) raw_cpu_add_return(pcp, -(typeof(pcp))(val)) 4368c2ecf20Sopenharmony_ci#define raw_cpu_inc_return(pcp) raw_cpu_add_return(pcp, 1) 4378c2ecf20Sopenharmony_ci#define raw_cpu_dec_return(pcp) raw_cpu_add_return(pcp, -1) 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci/* 4408c2ecf20Sopenharmony_ci * Operations for contexts that are safe from preemption/interrupts. These 4418c2ecf20Sopenharmony_ci * operations verify that preemption is disabled. 4428c2ecf20Sopenharmony_ci */ 4438c2ecf20Sopenharmony_ci#define __this_cpu_read(pcp) \ 4448c2ecf20Sopenharmony_ci({ \ 4458c2ecf20Sopenharmony_ci __this_cpu_preempt_check("read"); \ 4468c2ecf20Sopenharmony_ci raw_cpu_read(pcp); \ 4478c2ecf20Sopenharmony_ci}) 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci#define __this_cpu_write(pcp, val) \ 4508c2ecf20Sopenharmony_ci({ \ 4518c2ecf20Sopenharmony_ci __this_cpu_preempt_check("write"); \ 4528c2ecf20Sopenharmony_ci raw_cpu_write(pcp, val); \ 4538c2ecf20Sopenharmony_ci}) 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci#define __this_cpu_add(pcp, val) \ 4568c2ecf20Sopenharmony_ci({ \ 4578c2ecf20Sopenharmony_ci __this_cpu_preempt_check("add"); \ 4588c2ecf20Sopenharmony_ci raw_cpu_add(pcp, val); \ 4598c2ecf20Sopenharmony_ci}) 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci#define __this_cpu_and(pcp, val) \ 4628c2ecf20Sopenharmony_ci({ \ 4638c2ecf20Sopenharmony_ci __this_cpu_preempt_check("and"); \ 4648c2ecf20Sopenharmony_ci raw_cpu_and(pcp, val); \ 4658c2ecf20Sopenharmony_ci}) 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci#define __this_cpu_or(pcp, val) \ 4688c2ecf20Sopenharmony_ci({ \ 4698c2ecf20Sopenharmony_ci __this_cpu_preempt_check("or"); \ 4708c2ecf20Sopenharmony_ci raw_cpu_or(pcp, val); \ 4718c2ecf20Sopenharmony_ci}) 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci#define __this_cpu_add_return(pcp, val) \ 4748c2ecf20Sopenharmony_ci({ \ 4758c2ecf20Sopenharmony_ci __this_cpu_preempt_check("add_return"); \ 4768c2ecf20Sopenharmony_ci raw_cpu_add_return(pcp, val); \ 4778c2ecf20Sopenharmony_ci}) 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci#define __this_cpu_xchg(pcp, nval) \ 4808c2ecf20Sopenharmony_ci({ \ 4818c2ecf20Sopenharmony_ci __this_cpu_preempt_check("xchg"); \ 4828c2ecf20Sopenharmony_ci raw_cpu_xchg(pcp, nval); \ 4838c2ecf20Sopenharmony_ci}) 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci#define __this_cpu_cmpxchg(pcp, oval, nval) \ 4868c2ecf20Sopenharmony_ci({ \ 4878c2ecf20Sopenharmony_ci __this_cpu_preempt_check("cmpxchg"); \ 4888c2ecf20Sopenharmony_ci raw_cpu_cmpxchg(pcp, oval, nval); \ 4898c2ecf20Sopenharmony_ci}) 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci#define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \ 4928c2ecf20Sopenharmony_ci({ __this_cpu_preempt_check("cmpxchg_double"); \ 4938c2ecf20Sopenharmony_ci raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2); \ 4948c2ecf20Sopenharmony_ci}) 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci#define __this_cpu_sub(pcp, val) __this_cpu_add(pcp, -(typeof(pcp))(val)) 4978c2ecf20Sopenharmony_ci#define __this_cpu_inc(pcp) __this_cpu_add(pcp, 1) 4988c2ecf20Sopenharmony_ci#define __this_cpu_dec(pcp) __this_cpu_sub(pcp, 1) 4998c2ecf20Sopenharmony_ci#define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val)) 5008c2ecf20Sopenharmony_ci#define __this_cpu_inc_return(pcp) __this_cpu_add_return(pcp, 1) 5018c2ecf20Sopenharmony_ci#define __this_cpu_dec_return(pcp) __this_cpu_add_return(pcp, -1) 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci/* 5048c2ecf20Sopenharmony_ci * Operations with implied preemption/interrupt protection. These 5058c2ecf20Sopenharmony_ci * operations can be used without worrying about preemption or interrupt. 5068c2ecf20Sopenharmony_ci */ 5078c2ecf20Sopenharmony_ci#define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, pcp) 5088c2ecf20Sopenharmony_ci#define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, pcp, val) 5098c2ecf20Sopenharmony_ci#define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, pcp, val) 5108c2ecf20Sopenharmony_ci#define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, pcp, val) 5118c2ecf20Sopenharmony_ci#define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, pcp, val) 5128c2ecf20Sopenharmony_ci#define this_cpu_add_return(pcp, val) __pcpu_size_call_return2(this_cpu_add_return_, pcp, val) 5138c2ecf20Sopenharmony_ci#define this_cpu_xchg(pcp, nval) __pcpu_size_call_return2(this_cpu_xchg_, pcp, nval) 5148c2ecf20Sopenharmony_ci#define this_cpu_cmpxchg(pcp, oval, nval) \ 5158c2ecf20Sopenharmony_ci __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval) 5168c2ecf20Sopenharmony_ci#define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \ 5178c2ecf20Sopenharmony_ci __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, pcp1, pcp2, oval1, oval2, nval1, nval2) 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci#define this_cpu_sub(pcp, val) this_cpu_add(pcp, -(typeof(pcp))(val)) 5208c2ecf20Sopenharmony_ci#define this_cpu_inc(pcp) this_cpu_add(pcp, 1) 5218c2ecf20Sopenharmony_ci#define this_cpu_dec(pcp) this_cpu_sub(pcp, 1) 5228c2ecf20Sopenharmony_ci#define this_cpu_sub_return(pcp, val) this_cpu_add_return(pcp, -(typeof(pcp))(val)) 5238c2ecf20Sopenharmony_ci#define this_cpu_inc_return(pcp) this_cpu_add_return(pcp, 1) 5248c2ecf20Sopenharmony_ci#define this_cpu_dec_return(pcp) this_cpu_add_return(pcp, -1) 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */ 5278c2ecf20Sopenharmony_ci#endif /* _LINUX_PERCPU_DEFS_H */ 528