percpu.h 20 KB

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