percpu.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 phys_addr_t per_cpu_ptr_to_phys(void *addr);
  118. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  119. extern void __init setup_per_cpu_areas(void);
  120. #endif
  121. #else /* CONFIG_SMP */
  122. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  123. static inline void __percpu *__alloc_percpu(size_t size, size_t align)
  124. {
  125. /*
  126. * Can't easily make larger alignment work with kmalloc. WARN
  127. * on it. Larger alignment should only be used for module
  128. * percpu sections on SMP for which this path isn't used.
  129. */
  130. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  131. return kzalloc(size, GFP_KERNEL);
  132. }
  133. static inline void free_percpu(void __percpu *p)
  134. {
  135. kfree(p);
  136. }
  137. static inline phys_addr_t per_cpu_ptr_to_phys(void *addr)
  138. {
  139. return __pa(addr);
  140. }
  141. static inline void __init setup_per_cpu_areas(void) { }
  142. static inline void *pcpu_lpage_remapped(void *kaddr)
  143. {
  144. return NULL;
  145. }
  146. #endif /* CONFIG_SMP */
  147. #define alloc_percpu(type) \
  148. (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
  149. /*
  150. * Optional methods for optimized non-lvalue per-cpu variable access.
  151. *
  152. * @var can be a percpu variable or a field of it and its size should
  153. * equal char, int or long. percpu_read() evaluates to a lvalue and
  154. * all others to void.
  155. *
  156. * These operations are guaranteed to be atomic w.r.t. preemption.
  157. * The generic versions use plain get/put_cpu_var(). Archs are
  158. * encouraged to implement single-instruction alternatives which don't
  159. * require preemption protection.
  160. */
  161. #ifndef percpu_read
  162. # define percpu_read(var) \
  163. ({ \
  164. typeof(var) *pr_ptr__ = &(var); \
  165. typeof(var) pr_ret__; \
  166. pr_ret__ = get_cpu_var(*pr_ptr__); \
  167. put_cpu_var(*pr_ptr__); \
  168. pr_ret__; \
  169. })
  170. #endif
  171. #define __percpu_generic_to_op(var, val, op) \
  172. do { \
  173. typeof(var) *pgto_ptr__ = &(var); \
  174. get_cpu_var(*pgto_ptr__) op val; \
  175. put_cpu_var(*pgto_ptr__); \
  176. } while (0)
  177. #ifndef percpu_write
  178. # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
  179. #endif
  180. #ifndef percpu_add
  181. # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
  182. #endif
  183. #ifndef percpu_sub
  184. # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
  185. #endif
  186. #ifndef percpu_and
  187. # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
  188. #endif
  189. #ifndef percpu_or
  190. # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
  191. #endif
  192. #ifndef percpu_xor
  193. # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
  194. #endif
  195. /*
  196. * Branching function to split up a function into a set of functions that
  197. * are called for different scalar sizes of the objects handled.
  198. */
  199. extern void __bad_size_call_parameter(void);
  200. #define __pcpu_size_call_return(stem, variable) \
  201. ({ typeof(variable) pscr_ret__; \
  202. __verify_pcpu_ptr(&(variable)); \
  203. switch(sizeof(variable)) { \
  204. case 1: pscr_ret__ = stem##1(variable);break; \
  205. case 2: pscr_ret__ = stem##2(variable);break; \
  206. case 4: pscr_ret__ = stem##4(variable);break; \
  207. case 8: pscr_ret__ = stem##8(variable);break; \
  208. default: \
  209. __bad_size_call_parameter();break; \
  210. } \
  211. pscr_ret__; \
  212. })
  213. #define __pcpu_size_call(stem, variable, ...) \
  214. do { \
  215. __verify_pcpu_ptr(&(variable)); \
  216. switch(sizeof(variable)) { \
  217. case 1: stem##1(variable, __VA_ARGS__);break; \
  218. case 2: stem##2(variable, __VA_ARGS__);break; \
  219. case 4: stem##4(variable, __VA_ARGS__);break; \
  220. case 8: stem##8(variable, __VA_ARGS__);break; \
  221. default: \
  222. __bad_size_call_parameter();break; \
  223. } \
  224. } while (0)
  225. /*
  226. * Optimized manipulation for memory allocated through the per cpu
  227. * allocator or for addresses of per cpu variables.
  228. *
  229. * These operation guarantee exclusivity of access for other operations
  230. * on the *same* processor. The assumption is that per cpu data is only
  231. * accessed by a single processor instance (the current one).
  232. *
  233. * The first group is used for accesses that must be done in a
  234. * preemption safe way since we know that the context is not preempt
  235. * safe. Interrupts may occur. If the interrupt modifies the variable
  236. * too then RMW actions will not be reliable.
  237. *
  238. * The arch code can provide optimized functions in two ways:
  239. *
  240. * 1. Override the function completely. F.e. define this_cpu_add().
  241. * The arch must then ensure that the various scalar format passed
  242. * are handled correctly.
  243. *
  244. * 2. Provide functions for certain scalar sizes. F.e. provide
  245. * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
  246. * sized RMW actions. If arch code does not provide operations for
  247. * a scalar size then the fallback in the generic code will be
  248. * used.
  249. */
  250. #define _this_cpu_generic_read(pcp) \
  251. ({ typeof(pcp) ret__; \
  252. preempt_disable(); \
  253. ret__ = *this_cpu_ptr(&(pcp)); \
  254. preempt_enable(); \
  255. ret__; \
  256. })
  257. #ifndef this_cpu_read
  258. # ifndef this_cpu_read_1
  259. # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
  260. # endif
  261. # ifndef this_cpu_read_2
  262. # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
  263. # endif
  264. # ifndef this_cpu_read_4
  265. # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
  266. # endif
  267. # ifndef this_cpu_read_8
  268. # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
  269. # endif
  270. # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
  271. #endif
  272. #define _this_cpu_generic_to_op(pcp, val, op) \
  273. do { \
  274. preempt_disable(); \
  275. *__this_cpu_ptr(&(pcp)) op val; \
  276. preempt_enable(); \
  277. } while (0)
  278. #ifndef this_cpu_write
  279. # ifndef this_cpu_write_1
  280. # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  281. # endif
  282. # ifndef this_cpu_write_2
  283. # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  284. # endif
  285. # ifndef this_cpu_write_4
  286. # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  287. # endif
  288. # ifndef this_cpu_write_8
  289. # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  290. # endif
  291. # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
  292. #endif
  293. #ifndef this_cpu_add
  294. # ifndef this_cpu_add_1
  295. # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  296. # endif
  297. # ifndef this_cpu_add_2
  298. # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  299. # endif
  300. # ifndef this_cpu_add_4
  301. # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  302. # endif
  303. # ifndef this_cpu_add_8
  304. # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  305. # endif
  306. # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
  307. #endif
  308. #ifndef this_cpu_sub
  309. # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(val))
  310. #endif
  311. #ifndef this_cpu_inc
  312. # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
  313. #endif
  314. #ifndef this_cpu_dec
  315. # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
  316. #endif
  317. #ifndef this_cpu_and
  318. # ifndef this_cpu_and_1
  319. # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  320. # endif
  321. # ifndef this_cpu_and_2
  322. # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  323. # endif
  324. # ifndef this_cpu_and_4
  325. # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  326. # endif
  327. # ifndef this_cpu_and_8
  328. # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  329. # endif
  330. # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
  331. #endif
  332. #ifndef this_cpu_or
  333. # ifndef this_cpu_or_1
  334. # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  335. # endif
  336. # ifndef this_cpu_or_2
  337. # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  338. # endif
  339. # ifndef this_cpu_or_4
  340. # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  341. # endif
  342. # ifndef this_cpu_or_8
  343. # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  344. # endif
  345. # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  346. #endif
  347. #ifndef this_cpu_xor
  348. # ifndef this_cpu_xor_1
  349. # define this_cpu_xor_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  350. # endif
  351. # ifndef this_cpu_xor_2
  352. # define this_cpu_xor_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  353. # endif
  354. # ifndef this_cpu_xor_4
  355. # define this_cpu_xor_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  356. # endif
  357. # ifndef this_cpu_xor_8
  358. # define this_cpu_xor_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
  359. # endif
  360. # define this_cpu_xor(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  361. #endif
  362. /*
  363. * Generic percpu operations that do not require preemption handling.
  364. * Either we do not care about races or the caller has the
  365. * responsibility of handling preemptions issues. Arch code can still
  366. * override these instructions since the arch per cpu code may be more
  367. * efficient and may actually get race freeness for free (that is the
  368. * case for x86 for example).
  369. *
  370. * If there is no other protection through preempt disable and/or
  371. * disabling interupts then one of these RMW operations can show unexpected
  372. * behavior because the execution thread was rescheduled on another processor
  373. * or an interrupt occurred and the same percpu variable was modified from
  374. * the interrupt context.
  375. */
  376. #ifndef __this_cpu_read
  377. # ifndef __this_cpu_read_1
  378. # define __this_cpu_read_1(pcp) (*__this_cpu_ptr(&(pcp)))
  379. # endif
  380. # ifndef __this_cpu_read_2
  381. # define __this_cpu_read_2(pcp) (*__this_cpu_ptr(&(pcp)))
  382. # endif
  383. # ifndef __this_cpu_read_4
  384. # define __this_cpu_read_4(pcp) (*__this_cpu_ptr(&(pcp)))
  385. # endif
  386. # ifndef __this_cpu_read_8
  387. # define __this_cpu_read_8(pcp) (*__this_cpu_ptr(&(pcp)))
  388. # endif
  389. # define __this_cpu_read(pcp) __pcpu_size_call_return(__this_cpu_read_, (pcp))
  390. #endif
  391. #define __this_cpu_generic_to_op(pcp, val, op) \
  392. do { \
  393. *__this_cpu_ptr(&(pcp)) op val; \
  394. } while (0)
  395. #ifndef __this_cpu_write
  396. # ifndef __this_cpu_write_1
  397. # define __this_cpu_write_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  398. # endif
  399. # ifndef __this_cpu_write_2
  400. # define __this_cpu_write_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  401. # endif
  402. # ifndef __this_cpu_write_4
  403. # define __this_cpu_write_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  404. # endif
  405. # ifndef __this_cpu_write_8
  406. # define __this_cpu_write_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  407. # endif
  408. # define __this_cpu_write(pcp, val) __pcpu_size_call(__this_cpu_write_, (pcp), (val))
  409. #endif
  410. #ifndef __this_cpu_add
  411. # ifndef __this_cpu_add_1
  412. # define __this_cpu_add_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  413. # endif
  414. # ifndef __this_cpu_add_2
  415. # define __this_cpu_add_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  416. # endif
  417. # ifndef __this_cpu_add_4
  418. # define __this_cpu_add_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  419. # endif
  420. # ifndef __this_cpu_add_8
  421. # define __this_cpu_add_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  422. # endif
  423. # define __this_cpu_add(pcp, val) __pcpu_size_call(__this_cpu_add_, (pcp), (val))
  424. #endif
  425. #ifndef __this_cpu_sub
  426. # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(val))
  427. #endif
  428. #ifndef __this_cpu_inc
  429. # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
  430. #endif
  431. #ifndef __this_cpu_dec
  432. # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
  433. #endif
  434. #ifndef __this_cpu_and
  435. # ifndef __this_cpu_and_1
  436. # define __this_cpu_and_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  437. # endif
  438. # ifndef __this_cpu_and_2
  439. # define __this_cpu_and_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  440. # endif
  441. # ifndef __this_cpu_and_4
  442. # define __this_cpu_and_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  443. # endif
  444. # ifndef __this_cpu_and_8
  445. # define __this_cpu_and_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  446. # endif
  447. # define __this_cpu_and(pcp, val) __pcpu_size_call(__this_cpu_and_, (pcp), (val))
  448. #endif
  449. #ifndef __this_cpu_or
  450. # ifndef __this_cpu_or_1
  451. # define __this_cpu_or_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  452. # endif
  453. # ifndef __this_cpu_or_2
  454. # define __this_cpu_or_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  455. # endif
  456. # ifndef __this_cpu_or_4
  457. # define __this_cpu_or_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  458. # endif
  459. # ifndef __this_cpu_or_8
  460. # define __this_cpu_or_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  461. # endif
  462. # define __this_cpu_or(pcp, val) __pcpu_size_call(__this_cpu_or_, (pcp), (val))
  463. #endif
  464. #ifndef __this_cpu_xor
  465. # ifndef __this_cpu_xor_1
  466. # define __this_cpu_xor_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  467. # endif
  468. # ifndef __this_cpu_xor_2
  469. # define __this_cpu_xor_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  470. # endif
  471. # ifndef __this_cpu_xor_4
  472. # define __this_cpu_xor_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  473. # endif
  474. # ifndef __this_cpu_xor_8
  475. # define __this_cpu_xor_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
  476. # endif
  477. # define __this_cpu_xor(pcp, val) __pcpu_size_call(__this_cpu_xor_, (pcp), (val))
  478. #endif
  479. /*
  480. * IRQ safe versions of the per cpu RMW operations. Note that these operations
  481. * are *not* safe against modification of the same variable from another
  482. * processors (which one gets when using regular atomic operations)
  483. . They are guaranteed to be atomic vs. local interrupts and
  484. * preemption only.
  485. */
  486. #define irqsafe_cpu_generic_to_op(pcp, val, op) \
  487. do { \
  488. unsigned long flags; \
  489. local_irq_save(flags); \
  490. *__this_cpu_ptr(&(pcp)) op val; \
  491. local_irq_restore(flags); \
  492. } while (0)
  493. #ifndef irqsafe_cpu_add
  494. # ifndef irqsafe_cpu_add_1
  495. # define irqsafe_cpu_add_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  496. # endif
  497. # ifndef irqsafe_cpu_add_2
  498. # define irqsafe_cpu_add_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  499. # endif
  500. # ifndef irqsafe_cpu_add_4
  501. # define irqsafe_cpu_add_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  502. # endif
  503. # ifndef irqsafe_cpu_add_8
  504. # define irqsafe_cpu_add_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
  505. # endif
  506. # define irqsafe_cpu_add(pcp, val) __pcpu_size_call(irqsafe_cpu_add_, (pcp), (val))
  507. #endif
  508. #ifndef irqsafe_cpu_sub
  509. # define irqsafe_cpu_sub(pcp, val) irqsafe_cpu_add((pcp), -(val))
  510. #endif
  511. #ifndef irqsafe_cpu_inc
  512. # define irqsafe_cpu_inc(pcp) irqsafe_cpu_add((pcp), 1)
  513. #endif
  514. #ifndef irqsafe_cpu_dec
  515. # define irqsafe_cpu_dec(pcp) irqsafe_cpu_sub((pcp), 1)
  516. #endif
  517. #ifndef irqsafe_cpu_and
  518. # ifndef irqsafe_cpu_and_1
  519. # define irqsafe_cpu_and_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  520. # endif
  521. # ifndef irqsafe_cpu_and_2
  522. # define irqsafe_cpu_and_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  523. # endif
  524. # ifndef irqsafe_cpu_and_4
  525. # define irqsafe_cpu_and_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  526. # endif
  527. # ifndef irqsafe_cpu_and_8
  528. # define irqsafe_cpu_and_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
  529. # endif
  530. # define irqsafe_cpu_and(pcp, val) __pcpu_size_call(irqsafe_cpu_and_, (val))
  531. #endif
  532. #ifndef irqsafe_cpu_or
  533. # ifndef irqsafe_cpu_or_1
  534. # define irqsafe_cpu_or_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  535. # endif
  536. # ifndef irqsafe_cpu_or_2
  537. # define irqsafe_cpu_or_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  538. # endif
  539. # ifndef irqsafe_cpu_or_4
  540. # define irqsafe_cpu_or_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  541. # endif
  542. # ifndef irqsafe_cpu_or_8
  543. # define irqsafe_cpu_or_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
  544. # endif
  545. # define irqsafe_cpu_or(pcp, val) __pcpu_size_call(irqsafe_cpu_or_, (val))
  546. #endif
  547. #ifndef irqsafe_cpu_xor
  548. # ifndef irqsafe_cpu_xor_1
  549. # define irqsafe_cpu_xor_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  550. # endif
  551. # ifndef irqsafe_cpu_xor_2
  552. # define irqsafe_cpu_xor_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  553. # endif
  554. # ifndef irqsafe_cpu_xor_4
  555. # define irqsafe_cpu_xor_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  556. # endif
  557. # ifndef irqsafe_cpu_xor_8
  558. # define irqsafe_cpu_xor_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
  559. # endif
  560. # define irqsafe_cpu_xor(pcp, val) __pcpu_size_call(irqsafe_cpu_xor_, (val))
  561. #endif
  562. #endif /* __LINUX_PERCPU_H */