percpu.h 19 KB

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