percpu.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #ifndef __LINUX_PERCPU_H
  2. #define __LINUX_PERCPU_H
  3. #include <linux/preempt.h>
  4. #include <linux/slab.h> /* For kmalloc() */
  5. #include <linux/smp.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/pfn.h>
  8. #include <asm/percpu.h>
  9. /* enough to cover all DEFINE_PER_CPUs in modules */
  10. #ifdef CONFIG_MODULES
  11. #define PERCPU_MODULE_RESERVE (8 << 10)
  12. #else
  13. #define PERCPU_MODULE_RESERVE 0
  14. #endif
  15. #ifndef PERCPU_ENOUGH_ROOM
  16. #define PERCPU_ENOUGH_ROOM \
  17. (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
  18. PERCPU_MODULE_RESERVE)
  19. #endif
  20. /*
  21. * Must be an lvalue. Since @var must be a simple identifier,
  22. * we force a syntax error here if it isn't.
  23. */
  24. #define get_cpu_var(var) (*({ \
  25. preempt_disable(); \
  26. &__get_cpu_var(var); }))
  27. /*
  28. * The weird & is necessary because sparse considers (void)(var) to be
  29. * a direct dereference of percpu variable (var).
  30. */
  31. #define put_cpu_var(var) do { \
  32. (void)&(var); \
  33. preempt_enable(); \
  34. } while (0)
  35. #ifdef CONFIG_SMP
  36. /* minimum unit size, also is the maximum supported allocation size */
  37. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(64 << 10)
  38. /*
  39. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  40. * back on the first chunk for dynamic percpu allocation if arch is
  41. * manually allocating and mapping it for faster access (as a part of
  42. * large page mapping for example).
  43. *
  44. * The following values give between one and two pages of free space
  45. * after typical minimal boot (2-way SMP, single disk and NIC) with
  46. * both defconfig and a distro config on x86_64 and 32. More
  47. * intelligent way to determine this would be nice.
  48. */
  49. #if BITS_PER_LONG > 32
  50. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  51. #else
  52. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  53. #endif
  54. extern void *pcpu_base_addr;
  55. extern const unsigned long *pcpu_unit_offsets;
  56. struct pcpu_group_info {
  57. int nr_units; /* aligned # of units */
  58. unsigned long base_offset; /* base address offset */
  59. unsigned int *cpu_map; /* unit->cpu map, empty
  60. * entries contain NR_CPUS */
  61. };
  62. struct pcpu_alloc_info {
  63. size_t static_size;
  64. size_t reserved_size;
  65. size_t dyn_size;
  66. size_t unit_size;
  67. size_t atom_size;
  68. size_t alloc_size;
  69. size_t __ai_size; /* internal, don't use */
  70. int nr_groups; /* 0 if grouping unnecessary */
  71. struct pcpu_group_info groups[];
  72. };
  73. enum pcpu_fc {
  74. PCPU_FC_AUTO,
  75. PCPU_FC_EMBED,
  76. PCPU_FC_PAGE,
  77. PCPU_FC_NR,
  78. };
  79. extern const char *pcpu_fc_names[PCPU_FC_NR];
  80. extern enum pcpu_fc pcpu_chosen_fc;
  81. typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
  82. size_t align);
  83. typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
  84. typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
  85. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  86. extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  87. int nr_units);
  88. extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
  89. extern struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  90. size_t reserved_size, ssize_t dyn_size,
  91. size_t atom_size,
  92. pcpu_fc_cpu_distance_fn_t cpu_distance_fn);
  93. extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  94. void *base_addr);
  95. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  96. extern int __init pcpu_embed_first_chunk(size_t reserved_size, ssize_t dyn_size,
  97. size_t atom_size,
  98. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  99. pcpu_fc_alloc_fn_t alloc_fn,
  100. pcpu_fc_free_fn_t free_fn);
  101. #endif
  102. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  103. extern int __init pcpu_page_first_chunk(size_t reserved_size,
  104. pcpu_fc_alloc_fn_t alloc_fn,
  105. pcpu_fc_free_fn_t free_fn,
  106. pcpu_fc_populate_pte_fn_t populate_pte_fn);
  107. #endif
  108. /*
  109. * Use this to get to a cpu's version of the per-cpu object
  110. * dynamically allocated. Non-atomic access to the current CPU's
  111. * version should probably be combined with get_cpu()/put_cpu().
  112. */
  113. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  114. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  115. extern void __percpu *__alloc_percpu(size_t size, size_t align);
  116. extern void free_percpu(void __percpu *__pdata);
  117. extern bool is_kernel_percpu_address(unsigned long addr);
  118. extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
  119. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  120. extern void __init setup_per_cpu_areas(void);
  121. #endif
  122. #else /* CONFIG_SMP */
  123. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  124. static inline void __percpu *__alloc_percpu(size_t size, size_t align)
  125. {
  126. /*
  127. * Can't easily make larger alignment work with kmalloc. WARN
  128. * on it. Larger alignment should only be used for module
  129. * percpu sections on SMP for which this path isn't used.
  130. */
  131. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  132. return kzalloc(size, GFP_KERNEL);
  133. }
  134. static inline void free_percpu(void __percpu *p)
  135. {
  136. kfree(p);
  137. }
  138. /* can't distinguish from other static vars, always false */
  139. static inline bool is_kernel_percpu_address(unsigned long addr)
  140. {
  141. return false;
  142. }
  143. static inline phys_addr_t per_cpu_ptr_to_phys(void *addr)
  144. {
  145. return __pa(addr);
  146. }
  147. static inline void __init setup_per_cpu_areas(void) { }
  148. static inline void *pcpu_lpage_remapped(void *kaddr)
  149. {
  150. return NULL;
  151. }
  152. #endif /* CONFIG_SMP */
  153. #define alloc_percpu(type) \
  154. (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
  155. /*
  156. * Optional methods for optimized non-lvalue per-cpu variable access.
  157. *
  158. * @var can be a percpu variable or a field of it and its size should
  159. * equal char, int or long. percpu_read() evaluates to a lvalue and
  160. * all others to void.
  161. *
  162. * These operations are guaranteed to be atomic w.r.t. preemption.
  163. * The generic versions use plain get/put_cpu_var(). Archs are
  164. * encouraged to implement single-instruction alternatives which don't
  165. * require preemption protection.
  166. */
  167. #ifndef percpu_read
  168. # define percpu_read(var) \
  169. ({ \
  170. typeof(var) *pr_ptr__ = &(var); \
  171. typeof(var) pr_ret__; \
  172. pr_ret__ = get_cpu_var(*pr_ptr__); \
  173. put_cpu_var(*pr_ptr__); \
  174. pr_ret__; \
  175. })
  176. #endif
  177. #define __percpu_generic_to_op(var, val, op) \
  178. do { \
  179. typeof(var) *pgto_ptr__ = &(var); \
  180. get_cpu_var(*pgto_ptr__) op val; \
  181. put_cpu_var(*pgto_ptr__); \
  182. } while (0)
  183. #ifndef percpu_write
  184. # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
  185. #endif
  186. #ifndef percpu_add
  187. # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
  188. #endif
  189. #ifndef percpu_sub
  190. # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
  191. #endif
  192. #ifndef percpu_and
  193. # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
  194. #endif
  195. #ifndef percpu_or
  196. # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
  197. #endif
  198. #ifndef percpu_xor
  199. # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
  200. #endif
  201. /*
  202. * Branching function to split up a function into a set of functions that
  203. * are called for different scalar sizes of the objects handled.
  204. */
  205. extern void __bad_size_call_parameter(void);
  206. #define __pcpu_size_call_return(stem, variable) \
  207. ({ typeof(variable) pscr_ret__; \
  208. __verify_pcpu_ptr(&(variable)); \
  209. switch(sizeof(variable)) { \
  210. case 1: pscr_ret__ = stem##1(variable);break; \
  211. case 2: pscr_ret__ = stem##2(variable);break; \
  212. case 4: pscr_ret__ = stem##4(variable);break; \
  213. case 8: pscr_ret__ = stem##8(variable);break; \
  214. default: \
  215. __bad_size_call_parameter();break; \
  216. } \
  217. pscr_ret__; \
  218. })
  219. #define __pcpu_size_call(stem, variable, ...) \
  220. do { \
  221. __verify_pcpu_ptr(&(variable)); \
  222. switch(sizeof(variable)) { \
  223. case 1: stem##1(variable, __VA_ARGS__);break; \
  224. case 2: stem##2(variable, __VA_ARGS__);break; \
  225. case 4: stem##4(variable, __VA_ARGS__);break; \
  226. case 8: stem##8(variable, __VA_ARGS__);break; \
  227. default: \
  228. __bad_size_call_parameter();break; \
  229. } \
  230. } while (0)
  231. /*
  232. * Optimized manipulation for memory allocated through the per cpu
  233. * allocator or for addresses of per cpu variables.
  234. *
  235. * These operation guarantee exclusivity of access for other operations
  236. * on the *same* processor. The assumption is that per cpu data is only
  237. * accessed by a single processor instance (the current one).
  238. *
  239. * The first group is used for accesses that must be done in a
  240. * preemption safe way since we know that the context is not preempt
  241. * safe. Interrupts may occur. If the interrupt modifies the variable
  242. * too then RMW actions will not be reliable.
  243. *
  244. * The arch code can provide optimized functions in two ways:
  245. *
  246. * 1. Override the function completely. F.e. define this_cpu_add().
  247. * The arch must then ensure that the various scalar format passed
  248. * are handled correctly.
  249. *
  250. * 2. Provide functions for certain scalar sizes. F.e. provide
  251. * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
  252. * sized RMW actions. If arch code does not provide operations for
  253. * a scalar size then the fallback in the generic code will be
  254. * used.
  255. */
  256. #define _this_cpu_generic_read(pcp) \
  257. ({ typeof(pcp) ret__; \
  258. preempt_disable(); \
  259. ret__ = *this_cpu_ptr(&(pcp)); \
  260. preempt_enable(); \
  261. ret__; \
  262. })
  263. #ifndef this_cpu_read
  264. # ifndef this_cpu_read_1
  265. # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
  266. # endif
  267. # ifndef this_cpu_read_2
  268. # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
  269. # endif
  270. # ifndef this_cpu_read_4
  271. # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
  272. # endif
  273. # ifndef this_cpu_read_8
  274. # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
  275. # endif
  276. # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
  277. #endif
  278. #define _this_cpu_generic_to_op(pcp, val, op) \
  279. do { \
  280. preempt_disable(); \
  281. *__this_cpu_ptr(&(pcp)) op val; \
  282. preempt_enable(); \
  283. } while (0)
  284. #ifndef this_cpu_write
  285. # ifndef this_cpu_write_1
  286. # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  287. # endif
  288. # ifndef this_cpu_write_2
  289. # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  290. # endif
  291. # ifndef this_cpu_write_4
  292. # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  293. # endif
  294. # ifndef this_cpu_write_8
  295. # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  296. # endif
  297. # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
  298. #endif
  299. #ifndef this_cpu_add
  300. # ifndef this_cpu_add_1
  301. # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  302. # endif
  303. # ifndef this_cpu_add_2
  304. # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  305. # endif
  306. # ifndef this_cpu_add_4
  307. # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  308. # endif
  309. # ifndef this_cpu_add_8
  310. # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  311. # endif
  312. # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
  313. #endif
  314. #ifndef this_cpu_sub
  315. # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(val))
  316. #endif
  317. #ifndef this_cpu_inc
  318. # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
  319. #endif
  320. #ifndef this_cpu_dec
  321. # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
  322. #endif
  323. #ifndef this_cpu_and
  324. # ifndef this_cpu_and_1
  325. # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  326. # endif
  327. # ifndef this_cpu_and_2
  328. # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  329. # endif
  330. # ifndef this_cpu_and_4
  331. # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  332. # endif
  333. # ifndef this_cpu_and_8
  334. # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  335. # endif
  336. # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
  337. #endif
  338. #ifndef this_cpu_or
  339. # ifndef this_cpu_or_1
  340. # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  341. # endif
  342. # ifndef this_cpu_or_2
  343. # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  344. # endif
  345. # ifndef this_cpu_or_4
  346. # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  347. # endif
  348. # ifndef this_cpu_or_8
  349. # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  350. # endif
  351. # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  352. #endif
  353. #ifndef this_cpu_xor
  354. # ifndef this_cpu_xor_1
  355. # define this_cpu_xor_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  356. # endif
  357. # ifndef this_cpu_xor_2
  358. # define this_cpu_xor_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  359. # endif
  360. # ifndef this_cpu_xor_4
  361. # define this_cpu_xor_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  362. # endif
  363. # ifndef this_cpu_xor_8
  364. # define this_cpu_xor_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  365. # endif
  366. # define this_cpu_xor(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  367. #endif
  368. /*
  369. * Generic percpu operations that do not require preemption handling.
  370. * Either we do not care about races or the caller has the
  371. * responsibility of handling preemptions issues. Arch code can still
  372. * override these instructions since the arch per cpu code may be more
  373. * efficient and may actually get race freeness for free (that is the
  374. * case for x86 for example).
  375. *
  376. * If there is no other protection through preempt disable and/or
  377. * disabling interupts then one of these RMW operations can show unexpected
  378. * behavior because the execution thread was rescheduled on another processor
  379. * or an interrupt occurred and the same percpu variable was modified from
  380. * the interrupt context.
  381. */
  382. #ifndef __this_cpu_read
  383. # ifndef __this_cpu_read_1
  384. # define __this_cpu_read_1(pcp) (*__this_cpu_ptr(&(pcp)))
  385. # endif
  386. # ifndef __this_cpu_read_2
  387. # define __this_cpu_read_2(pcp) (*__this_cpu_ptr(&(pcp)))
  388. # endif
  389. # ifndef __this_cpu_read_4
  390. # define __this_cpu_read_4(pcp) (*__this_cpu_ptr(&(pcp)))
  391. # endif
  392. # ifndef __this_cpu_read_8
  393. # define __this_cpu_read_8(pcp) (*__this_cpu_ptr(&(pcp)))
  394. # endif
  395. # define __this_cpu_read(pcp) __pcpu_size_call_return(__this_cpu_read_, (pcp))
  396. #endif
  397. #define __this_cpu_generic_to_op(pcp, val, op) \
  398. do { \
  399. *__this_cpu_ptr(&(pcp)) op val; \
  400. } while (0)
  401. #ifndef __this_cpu_write
  402. # ifndef __this_cpu_write_1
  403. # define __this_cpu_write_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  404. # endif
  405. # ifndef __this_cpu_write_2
  406. # define __this_cpu_write_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  407. # endif
  408. # ifndef __this_cpu_write_4
  409. # define __this_cpu_write_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  410. # endif
  411. # ifndef __this_cpu_write_8
  412. # define __this_cpu_write_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  413. # endif
  414. # define __this_cpu_write(pcp, val) __pcpu_size_call(__this_cpu_write_, (pcp), (val))
  415. #endif
  416. #ifndef __this_cpu_add
  417. # ifndef __this_cpu_add_1
  418. # define __this_cpu_add_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  419. # endif
  420. # ifndef __this_cpu_add_2
  421. # define __this_cpu_add_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  422. # endif
  423. # ifndef __this_cpu_add_4
  424. # define __this_cpu_add_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  425. # endif
  426. # ifndef __this_cpu_add_8
  427. # define __this_cpu_add_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  428. # endif
  429. # define __this_cpu_add(pcp, val) __pcpu_size_call(__this_cpu_add_, (pcp), (val))
  430. #endif
  431. #ifndef __this_cpu_sub
  432. # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(val))
  433. #endif
  434. #ifndef __this_cpu_inc
  435. # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
  436. #endif
  437. #ifndef __this_cpu_dec
  438. # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
  439. #endif
  440. #ifndef __this_cpu_and
  441. # ifndef __this_cpu_and_1
  442. # define __this_cpu_and_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  443. # endif
  444. # ifndef __this_cpu_and_2
  445. # define __this_cpu_and_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  446. # endif
  447. # ifndef __this_cpu_and_4
  448. # define __this_cpu_and_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  449. # endif
  450. # ifndef __this_cpu_and_8
  451. # define __this_cpu_and_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  452. # endif
  453. # define __this_cpu_and(pcp, val) __pcpu_size_call(__this_cpu_and_, (pcp), (val))
  454. #endif
  455. #ifndef __this_cpu_or
  456. # ifndef __this_cpu_or_1
  457. # define __this_cpu_or_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  458. # endif
  459. # ifndef __this_cpu_or_2
  460. # define __this_cpu_or_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  461. # endif
  462. # ifndef __this_cpu_or_4
  463. # define __this_cpu_or_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  464. # endif
  465. # ifndef __this_cpu_or_8
  466. # define __this_cpu_or_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  467. # endif
  468. # define __this_cpu_or(pcp, val) __pcpu_size_call(__this_cpu_or_, (pcp), (val))
  469. #endif
  470. #ifndef __this_cpu_xor
  471. # ifndef __this_cpu_xor_1
  472. # define __this_cpu_xor_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  473. # endif
  474. # ifndef __this_cpu_xor_2
  475. # define __this_cpu_xor_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  476. # endif
  477. # ifndef __this_cpu_xor_4
  478. # define __this_cpu_xor_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  479. # endif
  480. # ifndef __this_cpu_xor_8
  481. # define __this_cpu_xor_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  482. # endif
  483. # define __this_cpu_xor(pcp, val) __pcpu_size_call(__this_cpu_xor_, (pcp), (val))
  484. #endif
  485. /*
  486. * IRQ safe versions of the per cpu RMW operations. Note that these operations
  487. * are *not* safe against modification of the same variable from another
  488. * processors (which one gets when using regular atomic operations)
  489. . They are guaranteed to be atomic vs. local interrupts and
  490. * preemption only.
  491. */
  492. #define irqsafe_cpu_generic_to_op(pcp, val, op) \
  493. do { \
  494. unsigned long flags; \
  495. local_irq_save(flags); \
  496. *__this_cpu_ptr(&(pcp)) op val; \
  497. local_irq_restore(flags); \
  498. } while (0)
  499. #ifndef irqsafe_cpu_add
  500. # ifndef irqsafe_cpu_add_1
  501. # define irqsafe_cpu_add_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  502. # endif
  503. # ifndef irqsafe_cpu_add_2
  504. # define irqsafe_cpu_add_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  505. # endif
  506. # ifndef irqsafe_cpu_add_4
  507. # define irqsafe_cpu_add_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  508. # endif
  509. # ifndef irqsafe_cpu_add_8
  510. # define irqsafe_cpu_add_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  511. # endif
  512. # define irqsafe_cpu_add(pcp, val) __pcpu_size_call(irqsafe_cpu_add_, (pcp), (val))
  513. #endif
  514. #ifndef irqsafe_cpu_sub
  515. # define irqsafe_cpu_sub(pcp, val) irqsafe_cpu_add((pcp), -(val))
  516. #endif
  517. #ifndef irqsafe_cpu_inc
  518. # define irqsafe_cpu_inc(pcp) irqsafe_cpu_add((pcp), 1)
  519. #endif
  520. #ifndef irqsafe_cpu_dec
  521. # define irqsafe_cpu_dec(pcp) irqsafe_cpu_sub((pcp), 1)
  522. #endif
  523. #ifndef irqsafe_cpu_and
  524. # ifndef irqsafe_cpu_and_1
  525. # define irqsafe_cpu_and_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  526. # endif
  527. # ifndef irqsafe_cpu_and_2
  528. # define irqsafe_cpu_and_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  529. # endif
  530. # ifndef irqsafe_cpu_and_4
  531. # define irqsafe_cpu_and_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  532. # endif
  533. # ifndef irqsafe_cpu_and_8
  534. # define irqsafe_cpu_and_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  535. # endif
  536. # define irqsafe_cpu_and(pcp, val) __pcpu_size_call(irqsafe_cpu_and_, (val))
  537. #endif
  538. #ifndef irqsafe_cpu_or
  539. # ifndef irqsafe_cpu_or_1
  540. # define irqsafe_cpu_or_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  541. # endif
  542. # ifndef irqsafe_cpu_or_2
  543. # define irqsafe_cpu_or_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  544. # endif
  545. # ifndef irqsafe_cpu_or_4
  546. # define irqsafe_cpu_or_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  547. # endif
  548. # ifndef irqsafe_cpu_or_8
  549. # define irqsafe_cpu_or_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  550. # endif
  551. # define irqsafe_cpu_or(pcp, val) __pcpu_size_call(irqsafe_cpu_or_, (val))
  552. #endif
  553. #ifndef irqsafe_cpu_xor
  554. # ifndef irqsafe_cpu_xor_1
  555. # define irqsafe_cpu_xor_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  556. # endif
  557. # ifndef irqsafe_cpu_xor_2
  558. # define irqsafe_cpu_xor_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  559. # endif
  560. # ifndef irqsafe_cpu_xor_4
  561. # define irqsafe_cpu_xor_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  562. # endif
  563. # ifndef irqsafe_cpu_xor_8
  564. # define irqsafe_cpu_xor_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  565. # endif
  566. # define irqsafe_cpu_xor(pcp, val) __pcpu_size_call(irqsafe_cpu_xor_, (val))
  567. #endif
  568. #endif /* __LINUX_PERCPU_H */