percpu.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. #define get_cpu_ptr(var) ({ \
  36. preempt_disable(); \
  37. this_cpu_ptr(var); })
  38. #define put_cpu_ptr(var) do { \
  39. (void)(var); \
  40. preempt_enable(); \
  41. } while (0)
  42. /* minimum unit size, also is the maximum supported allocation size */
  43. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  44. /*
  45. * Percpu allocator can serve percpu allocations before slab is
  46. * initialized which allows slab to depend on the percpu allocator.
  47. * The following two parameters decide how much resource to
  48. * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
  49. * larger than PERCPU_DYNAMIC_EARLY_SIZE.
  50. */
  51. #define PERCPU_DYNAMIC_EARLY_SLOTS 128
  52. #define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
  53. /*
  54. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  55. * back on the first chunk for dynamic percpu allocation if arch is
  56. * manually allocating and mapping it for faster access (as a part of
  57. * large page mapping for example).
  58. *
  59. * The following values give between one and two pages of free space
  60. * after typical minimal boot (2-way SMP, single disk and NIC) with
  61. * both defconfig and a distro config on x86_64 and 32. More
  62. * intelligent way to determine this would be nice.
  63. */
  64. #if BITS_PER_LONG > 32
  65. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  66. #else
  67. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  68. #endif
  69. extern void *pcpu_base_addr;
  70. extern const unsigned long *pcpu_unit_offsets;
  71. struct pcpu_group_info {
  72. int nr_units; /* aligned # of units */
  73. unsigned long base_offset; /* base address offset */
  74. unsigned int *cpu_map; /* unit->cpu map, empty
  75. * entries contain NR_CPUS */
  76. };
  77. struct pcpu_alloc_info {
  78. size_t static_size;
  79. size_t reserved_size;
  80. size_t dyn_size;
  81. size_t unit_size;
  82. size_t atom_size;
  83. size_t alloc_size;
  84. size_t __ai_size; /* internal, don't use */
  85. int nr_groups; /* 0 if grouping unnecessary */
  86. struct pcpu_group_info groups[];
  87. };
  88. enum pcpu_fc {
  89. PCPU_FC_AUTO,
  90. PCPU_FC_EMBED,
  91. PCPU_FC_PAGE,
  92. PCPU_FC_NR,
  93. };
  94. extern const char * const pcpu_fc_names[PCPU_FC_NR];
  95. extern enum pcpu_fc pcpu_chosen_fc;
  96. typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
  97. size_t align);
  98. typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
  99. typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
  100. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  101. extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  102. int nr_units);
  103. extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
  104. extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  105. void *base_addr);
  106. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  107. extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  108. size_t atom_size,
  109. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  110. pcpu_fc_alloc_fn_t alloc_fn,
  111. pcpu_fc_free_fn_t free_fn);
  112. #endif
  113. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  114. extern int __init pcpu_page_first_chunk(size_t reserved_size,
  115. pcpu_fc_alloc_fn_t alloc_fn,
  116. pcpu_fc_free_fn_t free_fn,
  117. pcpu_fc_populate_pte_fn_t populate_pte_fn);
  118. #endif
  119. /*
  120. * Use this to get to a cpu's version of the per-cpu object
  121. * dynamically allocated. Non-atomic access to the current CPU's
  122. * version should probably be combined with get_cpu()/put_cpu().
  123. */
  124. #ifdef CONFIG_SMP
  125. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  126. #else
  127. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR((ptr)); })
  128. #endif
  129. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  130. extern bool is_kernel_percpu_address(unsigned long addr);
  131. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  132. extern void __init setup_per_cpu_areas(void);
  133. #endif
  134. extern void __init percpu_init_late(void);
  135. extern void __percpu *__alloc_percpu(size_t size, size_t align);
  136. extern void free_percpu(void __percpu *__pdata);
  137. extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
  138. #define alloc_percpu(type) \
  139. (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
  140. /*
  141. * Branching function to split up a function into a set of functions that
  142. * are called for different scalar sizes of the objects handled.
  143. */
  144. extern void __bad_size_call_parameter(void);
  145. #define __pcpu_size_call_return(stem, variable) \
  146. ({ typeof(variable) pscr_ret__; \
  147. __verify_pcpu_ptr(&(variable)); \
  148. switch(sizeof(variable)) { \
  149. case 1: pscr_ret__ = stem##1(variable);break; \
  150. case 2: pscr_ret__ = stem##2(variable);break; \
  151. case 4: pscr_ret__ = stem##4(variable);break; \
  152. case 8: pscr_ret__ = stem##8(variable);break; \
  153. default: \
  154. __bad_size_call_parameter();break; \
  155. } \
  156. pscr_ret__; \
  157. })
  158. #define __pcpu_size_call_return2(stem, variable, ...) \
  159. ({ \
  160. typeof(variable) pscr2_ret__; \
  161. __verify_pcpu_ptr(&(variable)); \
  162. switch(sizeof(variable)) { \
  163. case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break; \
  164. case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break; \
  165. case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break; \
  166. case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break; \
  167. default: \
  168. __bad_size_call_parameter(); break; \
  169. } \
  170. pscr2_ret__; \
  171. })
  172. /*
  173. * Special handling for cmpxchg_double. cmpxchg_double is passed two
  174. * percpu variables. The first has to be aligned to a double word
  175. * boundary and the second has to follow directly thereafter.
  176. * We enforce this on all architectures even if they don't support
  177. * a double cmpxchg instruction, since it's a cheap requirement, and it
  178. * avoids breaking the requirement for architectures with the instruction.
  179. */
  180. #define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...) \
  181. ({ \
  182. bool pdcrb_ret__; \
  183. __verify_pcpu_ptr(&pcp1); \
  184. BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2)); \
  185. VM_BUG_ON((unsigned long)(&pcp1) % (2 * sizeof(pcp1))); \
  186. VM_BUG_ON((unsigned long)(&pcp2) != \
  187. (unsigned long)(&pcp1) + sizeof(pcp1)); \
  188. switch(sizeof(pcp1)) { \
  189. case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break; \
  190. case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break; \
  191. case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break; \
  192. case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break; \
  193. default: \
  194. __bad_size_call_parameter(); break; \
  195. } \
  196. pdcrb_ret__; \
  197. })
  198. #define __pcpu_size_call(stem, variable, ...) \
  199. do { \
  200. __verify_pcpu_ptr(&(variable)); \
  201. switch(sizeof(variable)) { \
  202. case 1: stem##1(variable, __VA_ARGS__);break; \
  203. case 2: stem##2(variable, __VA_ARGS__);break; \
  204. case 4: stem##4(variable, __VA_ARGS__);break; \
  205. case 8: stem##8(variable, __VA_ARGS__);break; \
  206. default: \
  207. __bad_size_call_parameter();break; \
  208. } \
  209. } while (0)
  210. /*
  211. * Optimized manipulation for memory allocated through the per cpu
  212. * allocator or for addresses of per cpu variables.
  213. *
  214. * These operation guarantee exclusivity of access for other operations
  215. * on the *same* processor. The assumption is that per cpu data is only
  216. * accessed by a single processor instance (the current one).
  217. *
  218. * The first group is used for accesses that must be done in a
  219. * preemption safe way since we know that the context is not preempt
  220. * safe. Interrupts may occur. If the interrupt modifies the variable
  221. * too then RMW actions will not be reliable.
  222. *
  223. * The arch code can provide optimized functions in two ways:
  224. *
  225. * 1. Override the function completely. F.e. define this_cpu_add().
  226. * The arch must then ensure that the various scalar format passed
  227. * are handled correctly.
  228. *
  229. * 2. Provide functions for certain scalar sizes. F.e. provide
  230. * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
  231. * sized RMW actions. If arch code does not provide operations for
  232. * a scalar size then the fallback in the generic code will be
  233. * used.
  234. */
  235. #define _this_cpu_generic_read(pcp) \
  236. ({ typeof(pcp) ret__; \
  237. preempt_disable(); \
  238. ret__ = *this_cpu_ptr(&(pcp)); \
  239. preempt_enable(); \
  240. ret__; \
  241. })
  242. #ifndef this_cpu_read
  243. # ifndef this_cpu_read_1
  244. # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
  245. # endif
  246. # ifndef this_cpu_read_2
  247. # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
  248. # endif
  249. # ifndef this_cpu_read_4
  250. # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
  251. # endif
  252. # ifndef this_cpu_read_8
  253. # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
  254. # endif
  255. # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
  256. #endif
  257. #define _this_cpu_generic_to_op(pcp, val, op) \
  258. do { \
  259. unsigned long flags; \
  260. raw_local_irq_save(flags); \
  261. *__this_cpu_ptr(&(pcp)) op val; \
  262. raw_local_irq_restore(flags); \
  263. } while (0)
  264. #ifndef this_cpu_write
  265. # ifndef this_cpu_write_1
  266. # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  267. # endif
  268. # ifndef this_cpu_write_2
  269. # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  270. # endif
  271. # ifndef this_cpu_write_4
  272. # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  273. # endif
  274. # ifndef this_cpu_write_8
  275. # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  276. # endif
  277. # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
  278. #endif
  279. #ifndef this_cpu_add
  280. # ifndef this_cpu_add_1
  281. # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  282. # endif
  283. # ifndef this_cpu_add_2
  284. # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  285. # endif
  286. # ifndef this_cpu_add_4
  287. # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  288. # endif
  289. # ifndef this_cpu_add_8
  290. # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  291. # endif
  292. # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
  293. #endif
  294. #ifndef this_cpu_sub
  295. # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(val))
  296. #endif
  297. #ifndef this_cpu_inc
  298. # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
  299. #endif
  300. #ifndef this_cpu_dec
  301. # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
  302. #endif
  303. #ifndef this_cpu_and
  304. # ifndef this_cpu_and_1
  305. # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  306. # endif
  307. # ifndef this_cpu_and_2
  308. # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  309. # endif
  310. # ifndef this_cpu_and_4
  311. # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  312. # endif
  313. # ifndef this_cpu_and_8
  314. # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  315. # endif
  316. # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
  317. #endif
  318. #ifndef this_cpu_or
  319. # ifndef this_cpu_or_1
  320. # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  321. # endif
  322. # ifndef this_cpu_or_2
  323. # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  324. # endif
  325. # ifndef this_cpu_or_4
  326. # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  327. # endif
  328. # ifndef this_cpu_or_8
  329. # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  330. # endif
  331. # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  332. #endif
  333. #define _this_cpu_generic_add_return(pcp, val) \
  334. ({ \
  335. typeof(pcp) ret__; \
  336. unsigned long flags; \
  337. raw_local_irq_save(flags); \
  338. __this_cpu_add(pcp, val); \
  339. ret__ = __this_cpu_read(pcp); \
  340. raw_local_irq_restore(flags); \
  341. ret__; \
  342. })
  343. #ifndef this_cpu_add_return
  344. # ifndef this_cpu_add_return_1
  345. # define this_cpu_add_return_1(pcp, val) _this_cpu_generic_add_return(pcp, val)
  346. # endif
  347. # ifndef this_cpu_add_return_2
  348. # define this_cpu_add_return_2(pcp, val) _this_cpu_generic_add_return(pcp, val)
  349. # endif
  350. # ifndef this_cpu_add_return_4
  351. # define this_cpu_add_return_4(pcp, val) _this_cpu_generic_add_return(pcp, val)
  352. # endif
  353. # ifndef this_cpu_add_return_8
  354. # define this_cpu_add_return_8(pcp, val) _this_cpu_generic_add_return(pcp, val)
  355. # endif
  356. # define this_cpu_add_return(pcp, val) __pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
  357. #endif
  358. #define this_cpu_sub_return(pcp, val) this_cpu_add_return(pcp, -(val))
  359. #define this_cpu_inc_return(pcp) this_cpu_add_return(pcp, 1)
  360. #define this_cpu_dec_return(pcp) this_cpu_add_return(pcp, -1)
  361. #define _this_cpu_generic_xchg(pcp, nval) \
  362. ({ typeof(pcp) ret__; \
  363. unsigned long flags; \
  364. raw_local_irq_save(flags); \
  365. ret__ = __this_cpu_read(pcp); \
  366. __this_cpu_write(pcp, nval); \
  367. raw_local_irq_restore(flags); \
  368. ret__; \
  369. })
  370. #ifndef this_cpu_xchg
  371. # ifndef this_cpu_xchg_1
  372. # define this_cpu_xchg_1(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  373. # endif
  374. # ifndef this_cpu_xchg_2
  375. # define this_cpu_xchg_2(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  376. # endif
  377. # ifndef this_cpu_xchg_4
  378. # define this_cpu_xchg_4(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  379. # endif
  380. # ifndef this_cpu_xchg_8
  381. # define this_cpu_xchg_8(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  382. # endif
  383. # define this_cpu_xchg(pcp, nval) \
  384. __pcpu_size_call_return2(this_cpu_xchg_, (pcp), nval)
  385. #endif
  386. #define _this_cpu_generic_cmpxchg(pcp, oval, nval) \
  387. ({ \
  388. typeof(pcp) ret__; \
  389. unsigned long flags; \
  390. raw_local_irq_save(flags); \
  391. ret__ = __this_cpu_read(pcp); \
  392. if (ret__ == (oval)) \
  393. __this_cpu_write(pcp, nval); \
  394. raw_local_irq_restore(flags); \
  395. ret__; \
  396. })
  397. #ifndef this_cpu_cmpxchg
  398. # ifndef this_cpu_cmpxchg_1
  399. # define this_cpu_cmpxchg_1(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  400. # endif
  401. # ifndef this_cpu_cmpxchg_2
  402. # define this_cpu_cmpxchg_2(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  403. # endif
  404. # ifndef this_cpu_cmpxchg_4
  405. # define this_cpu_cmpxchg_4(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  406. # endif
  407. # ifndef this_cpu_cmpxchg_8
  408. # define this_cpu_cmpxchg_8(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  409. # endif
  410. # define this_cpu_cmpxchg(pcp, oval, nval) \
  411. __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
  412. #endif
  413. /*
  414. * cmpxchg_double replaces two adjacent scalars at once. The first
  415. * two parameters are per cpu variables which have to be of the same
  416. * size. A truth value is returned to indicate success or failure
  417. * (since a double register result is difficult to handle). There is
  418. * very limited hardware support for these operations, so only certain
  419. * sizes may work.
  420. */
  421. #define _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  422. ({ \
  423. int ret__; \
  424. unsigned long flags; \
  425. raw_local_irq_save(flags); \
  426. ret__ = __this_cpu_generic_cmpxchg_double(pcp1, pcp2, \
  427. oval1, oval2, nval1, nval2); \
  428. raw_local_irq_restore(flags); \
  429. ret__; \
  430. })
  431. #ifndef this_cpu_cmpxchg_double
  432. # ifndef this_cpu_cmpxchg_double_1
  433. # define this_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  434. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  435. # endif
  436. # ifndef this_cpu_cmpxchg_double_2
  437. # define this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  438. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  439. # endif
  440. # ifndef this_cpu_cmpxchg_double_4
  441. # define this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  442. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  443. # endif
  444. # ifndef this_cpu_cmpxchg_double_8
  445. # define this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  446. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  447. # endif
  448. # define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  449. __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
  450. #endif
  451. /*
  452. * Generic percpu operations for context that are safe from preemption/interrupts.
  453. * Either we do not care about races or the caller has the
  454. * responsibility of handling preemption/interrupt issues. Arch code can still
  455. * override these instructions since the arch per cpu code may be more
  456. * efficient and may actually get race freeness for free (that is the
  457. * case for x86 for example).
  458. *
  459. * If there is no other protection through preempt disable and/or
  460. * disabling interupts then one of these RMW operations can show unexpected
  461. * behavior because the execution thread was rescheduled on another processor
  462. * or an interrupt occurred and the same percpu variable was modified from
  463. * the interrupt context.
  464. */
  465. #ifndef __this_cpu_read
  466. # ifndef __this_cpu_read_1
  467. # define __this_cpu_read_1(pcp) (*__this_cpu_ptr(&(pcp)))
  468. # endif
  469. # ifndef __this_cpu_read_2
  470. # define __this_cpu_read_2(pcp) (*__this_cpu_ptr(&(pcp)))
  471. # endif
  472. # ifndef __this_cpu_read_4
  473. # define __this_cpu_read_4(pcp) (*__this_cpu_ptr(&(pcp)))
  474. # endif
  475. # ifndef __this_cpu_read_8
  476. # define __this_cpu_read_8(pcp) (*__this_cpu_ptr(&(pcp)))
  477. # endif
  478. # define __this_cpu_read(pcp) __pcpu_size_call_return(__this_cpu_read_, (pcp))
  479. #endif
  480. #define __this_cpu_generic_to_op(pcp, val, op) \
  481. do { \
  482. *__this_cpu_ptr(&(pcp)) op val; \
  483. } while (0)
  484. #ifndef __this_cpu_write
  485. # ifndef __this_cpu_write_1
  486. # define __this_cpu_write_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  487. # endif
  488. # ifndef __this_cpu_write_2
  489. # define __this_cpu_write_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  490. # endif
  491. # ifndef __this_cpu_write_4
  492. # define __this_cpu_write_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  493. # endif
  494. # ifndef __this_cpu_write_8
  495. # define __this_cpu_write_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
  496. # endif
  497. # define __this_cpu_write(pcp, val) __pcpu_size_call(__this_cpu_write_, (pcp), (val))
  498. #endif
  499. #ifndef __this_cpu_add
  500. # ifndef __this_cpu_add_1
  501. # define __this_cpu_add_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  502. # endif
  503. # ifndef __this_cpu_add_2
  504. # define __this_cpu_add_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  505. # endif
  506. # ifndef __this_cpu_add_4
  507. # define __this_cpu_add_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  508. # endif
  509. # ifndef __this_cpu_add_8
  510. # define __this_cpu_add_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
  511. # endif
  512. # define __this_cpu_add(pcp, val) __pcpu_size_call(__this_cpu_add_, (pcp), (val))
  513. #endif
  514. #ifndef __this_cpu_sub
  515. # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(val))
  516. #endif
  517. #ifndef __this_cpu_inc
  518. # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
  519. #endif
  520. #ifndef __this_cpu_dec
  521. # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
  522. #endif
  523. #ifndef __this_cpu_and
  524. # ifndef __this_cpu_and_1
  525. # define __this_cpu_and_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  526. # endif
  527. # ifndef __this_cpu_and_2
  528. # define __this_cpu_and_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  529. # endif
  530. # ifndef __this_cpu_and_4
  531. # define __this_cpu_and_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  532. # endif
  533. # ifndef __this_cpu_and_8
  534. # define __this_cpu_and_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
  535. # endif
  536. # define __this_cpu_and(pcp, val) __pcpu_size_call(__this_cpu_and_, (pcp), (val))
  537. #endif
  538. #ifndef __this_cpu_or
  539. # ifndef __this_cpu_or_1
  540. # define __this_cpu_or_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  541. # endif
  542. # ifndef __this_cpu_or_2
  543. # define __this_cpu_or_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  544. # endif
  545. # ifndef __this_cpu_or_4
  546. # define __this_cpu_or_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  547. # endif
  548. # ifndef __this_cpu_or_8
  549. # define __this_cpu_or_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
  550. # endif
  551. # define __this_cpu_or(pcp, val) __pcpu_size_call(__this_cpu_or_, (pcp), (val))
  552. #endif
  553. #define __this_cpu_generic_add_return(pcp, val) \
  554. ({ \
  555. __this_cpu_add(pcp, val); \
  556. __this_cpu_read(pcp); \
  557. })
  558. #ifndef __this_cpu_add_return
  559. # ifndef __this_cpu_add_return_1
  560. # define __this_cpu_add_return_1(pcp, val) __this_cpu_generic_add_return(pcp, val)
  561. # endif
  562. # ifndef __this_cpu_add_return_2
  563. # define __this_cpu_add_return_2(pcp, val) __this_cpu_generic_add_return(pcp, val)
  564. # endif
  565. # ifndef __this_cpu_add_return_4
  566. # define __this_cpu_add_return_4(pcp, val) __this_cpu_generic_add_return(pcp, val)
  567. # endif
  568. # ifndef __this_cpu_add_return_8
  569. # define __this_cpu_add_return_8(pcp, val) __this_cpu_generic_add_return(pcp, val)
  570. # endif
  571. # define __this_cpu_add_return(pcp, val) \
  572. __pcpu_size_call_return2(__this_cpu_add_return_, pcp, val)
  573. #endif
  574. #define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(val))
  575. #define __this_cpu_inc_return(pcp) __this_cpu_add_return(pcp, 1)
  576. #define __this_cpu_dec_return(pcp) __this_cpu_add_return(pcp, -1)
  577. #define __this_cpu_generic_xchg(pcp, nval) \
  578. ({ typeof(pcp) ret__; \
  579. ret__ = __this_cpu_read(pcp); \
  580. __this_cpu_write(pcp, nval); \
  581. ret__; \
  582. })
  583. #ifndef __this_cpu_xchg
  584. # ifndef __this_cpu_xchg_1
  585. # define __this_cpu_xchg_1(pcp, nval) __this_cpu_generic_xchg(pcp, nval)
  586. # endif
  587. # ifndef __this_cpu_xchg_2
  588. # define __this_cpu_xchg_2(pcp, nval) __this_cpu_generic_xchg(pcp, nval)
  589. # endif
  590. # ifndef __this_cpu_xchg_4
  591. # define __this_cpu_xchg_4(pcp, nval) __this_cpu_generic_xchg(pcp, nval)
  592. # endif
  593. # ifndef __this_cpu_xchg_8
  594. # define __this_cpu_xchg_8(pcp, nval) __this_cpu_generic_xchg(pcp, nval)
  595. # endif
  596. # define __this_cpu_xchg(pcp, nval) \
  597. __pcpu_size_call_return2(__this_cpu_xchg_, (pcp), nval)
  598. #endif
  599. #define __this_cpu_generic_cmpxchg(pcp, oval, nval) \
  600. ({ \
  601. typeof(pcp) ret__; \
  602. ret__ = __this_cpu_read(pcp); \
  603. if (ret__ == (oval)) \
  604. __this_cpu_write(pcp, nval); \
  605. ret__; \
  606. })
  607. #ifndef __this_cpu_cmpxchg
  608. # ifndef __this_cpu_cmpxchg_1
  609. # define __this_cpu_cmpxchg_1(pcp, oval, nval) __this_cpu_generic_cmpxchg(pcp, oval, nval)
  610. # endif
  611. # ifndef __this_cpu_cmpxchg_2
  612. # define __this_cpu_cmpxchg_2(pcp, oval, nval) __this_cpu_generic_cmpxchg(pcp, oval, nval)
  613. # endif
  614. # ifndef __this_cpu_cmpxchg_4
  615. # define __this_cpu_cmpxchg_4(pcp, oval, nval) __this_cpu_generic_cmpxchg(pcp, oval, nval)
  616. # endif
  617. # ifndef __this_cpu_cmpxchg_8
  618. # define __this_cpu_cmpxchg_8(pcp, oval, nval) __this_cpu_generic_cmpxchg(pcp, oval, nval)
  619. # endif
  620. # define __this_cpu_cmpxchg(pcp, oval, nval) \
  621. __pcpu_size_call_return2(__this_cpu_cmpxchg_, pcp, oval, nval)
  622. #endif
  623. #define __this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  624. ({ \
  625. int __ret = 0; \
  626. if (__this_cpu_read(pcp1) == (oval1) && \
  627. __this_cpu_read(pcp2) == (oval2)) { \
  628. __this_cpu_write(pcp1, (nval1)); \
  629. __this_cpu_write(pcp2, (nval2)); \
  630. __ret = 1; \
  631. } \
  632. (__ret); \
  633. })
  634. #ifndef __this_cpu_cmpxchg_double
  635. # ifndef __this_cpu_cmpxchg_double_1
  636. # define __this_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  637. __this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  638. # endif
  639. # ifndef __this_cpu_cmpxchg_double_2
  640. # define __this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  641. __this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  642. # endif
  643. # ifndef __this_cpu_cmpxchg_double_4
  644. # define __this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  645. __this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  646. # endif
  647. # ifndef __this_cpu_cmpxchg_double_8
  648. # define __this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  649. __this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  650. # endif
  651. # define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  652. __pcpu_double_call_return_bool(__this_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
  653. #endif
  654. #endif /* __LINUX_PERCPU_H */