percpu.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. #ifndef __LINUX_PERCPU_H
  2. #define __LINUX_PERCPU_H
  3. #include <linux/mmdebug.h>
  4. #include <linux/preempt.h>
  5. #include <linux/smp.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/pfn.h>
  8. #include <linux/init.h>
  9. #include <asm/percpu.h>
  10. /* enough to cover all DEFINE_PER_CPUs in modules */
  11. #ifdef CONFIG_MODULES
  12. #define PERCPU_MODULE_RESERVE (8 << 10)
  13. #else
  14. #define PERCPU_MODULE_RESERVE 0
  15. #endif
  16. #ifndef PERCPU_ENOUGH_ROOM
  17. #define PERCPU_ENOUGH_ROOM \
  18. (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
  19. PERCPU_MODULE_RESERVE)
  20. #endif
  21. /*
  22. * Must be an lvalue. Since @var must be a simple identifier,
  23. * we force a syntax error here if it isn't.
  24. */
  25. #define get_cpu_var(var) (*({ \
  26. preempt_disable(); \
  27. &__get_cpu_var(var); }))
  28. /*
  29. * The weird & is necessary because sparse considers (void)(var) to be
  30. * a direct dereference of percpu variable (var).
  31. */
  32. #define put_cpu_var(var) do { \
  33. (void)&(var); \
  34. preempt_enable(); \
  35. } while (0)
  36. #define get_cpu_ptr(var) ({ \
  37. preempt_disable(); \
  38. this_cpu_ptr(var); })
  39. #define put_cpu_ptr(var) do { \
  40. (void)(var); \
  41. preempt_enable(); \
  42. } while (0)
  43. /* minimum unit size, also is the maximum supported allocation size */
  44. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  45. /*
  46. * Percpu allocator can serve percpu allocations before slab is
  47. * initialized which allows slab to depend on the percpu allocator.
  48. * The following two parameters decide how much resource to
  49. * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
  50. * larger than PERCPU_DYNAMIC_EARLY_SIZE.
  51. */
  52. #define PERCPU_DYNAMIC_EARLY_SLOTS 128
  53. #define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
  54. /*
  55. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  56. * back on the first chunk for dynamic percpu allocation if arch is
  57. * manually allocating and mapping it for faster access (as a part of
  58. * large page mapping for example).
  59. *
  60. * The following values give between one and two pages of free space
  61. * after typical minimal boot (2-way SMP, single disk and NIC) with
  62. * both defconfig and a distro config on x86_64 and 32. More
  63. * intelligent way to determine this would be nice.
  64. */
  65. #if BITS_PER_LONG > 32
  66. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  67. #else
  68. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  69. #endif
  70. extern void *pcpu_base_addr;
  71. extern const unsigned long *pcpu_unit_offsets;
  72. struct pcpu_group_info {
  73. int nr_units; /* aligned # of units */
  74. unsigned long base_offset; /* base address offset */
  75. unsigned int *cpu_map; /* unit->cpu map, empty
  76. * entries contain NR_CPUS */
  77. };
  78. struct pcpu_alloc_info {
  79. size_t static_size;
  80. size_t reserved_size;
  81. size_t dyn_size;
  82. size_t unit_size;
  83. size_t atom_size;
  84. size_t alloc_size;
  85. size_t __ai_size; /* internal, don't use */
  86. int nr_groups; /* 0 if grouping unnecessary */
  87. struct pcpu_group_info groups[];
  88. };
  89. enum pcpu_fc {
  90. PCPU_FC_AUTO,
  91. PCPU_FC_EMBED,
  92. PCPU_FC_PAGE,
  93. PCPU_FC_NR,
  94. };
  95. extern const char * const pcpu_fc_names[PCPU_FC_NR];
  96. extern enum pcpu_fc pcpu_chosen_fc;
  97. typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
  98. size_t align);
  99. typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
  100. typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
  101. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  102. extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  103. int nr_units);
  104. extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
  105. extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  106. void *base_addr);
  107. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  108. extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  109. size_t atom_size,
  110. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  111. pcpu_fc_alloc_fn_t alloc_fn,
  112. pcpu_fc_free_fn_t free_fn);
  113. #endif
  114. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  115. extern int __init pcpu_page_first_chunk(size_t reserved_size,
  116. pcpu_fc_alloc_fn_t alloc_fn,
  117. pcpu_fc_free_fn_t free_fn,
  118. pcpu_fc_populate_pte_fn_t populate_pte_fn);
  119. #endif
  120. /*
  121. * Use this to get to a cpu's version of the per-cpu object
  122. * dynamically allocated. Non-atomic access to the current CPU's
  123. * version should probably be combined with get_cpu()/put_cpu().
  124. */
  125. #ifdef CONFIG_SMP
  126. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  127. #else
  128. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR((ptr)); })
  129. #endif
  130. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
  131. extern bool is_kernel_percpu_address(unsigned long addr);
  132. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  133. extern void __init setup_per_cpu_areas(void);
  134. #endif
  135. extern void __init percpu_init_late(void);
  136. extern void __percpu *__alloc_percpu(size_t size, size_t align);
  137. extern void free_percpu(void __percpu *__pdata);
  138. extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
  139. #define alloc_percpu(type) \
  140. (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
  141. /*
  142. * Branching function to split up a function into a set of functions that
  143. * are called for different scalar sizes of the objects handled.
  144. */
  145. extern void __bad_size_call_parameter(void);
  146. #ifdef CONFIG_DEBUG_PREEMPT
  147. extern void __this_cpu_preempt_check(const char *op);
  148. #else
  149. static inline void __this_cpu_preempt_check(const char *op) { }
  150. #endif
  151. #define __pcpu_size_call_return(stem, variable) \
  152. ({ typeof(variable) pscr_ret__; \
  153. __verify_pcpu_ptr(&(variable)); \
  154. switch(sizeof(variable)) { \
  155. case 1: pscr_ret__ = stem##1(variable);break; \
  156. case 2: pscr_ret__ = stem##2(variable);break; \
  157. case 4: pscr_ret__ = stem##4(variable);break; \
  158. case 8: pscr_ret__ = stem##8(variable);break; \
  159. default: \
  160. __bad_size_call_parameter();break; \
  161. } \
  162. pscr_ret__; \
  163. })
  164. #define __pcpu_size_call_return2(stem, variable, ...) \
  165. ({ \
  166. typeof(variable) pscr2_ret__; \
  167. __verify_pcpu_ptr(&(variable)); \
  168. switch(sizeof(variable)) { \
  169. case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break; \
  170. case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break; \
  171. case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break; \
  172. case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break; \
  173. default: \
  174. __bad_size_call_parameter(); break; \
  175. } \
  176. pscr2_ret__; \
  177. })
  178. /*
  179. * Special handling for cmpxchg_double. cmpxchg_double is passed two
  180. * percpu variables. The first has to be aligned to a double word
  181. * boundary and the second has to follow directly thereafter.
  182. * We enforce this on all architectures even if they don't support
  183. * a double cmpxchg instruction, since it's a cheap requirement, and it
  184. * avoids breaking the requirement for architectures with the instruction.
  185. */
  186. #define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...) \
  187. ({ \
  188. bool pdcrb_ret__; \
  189. __verify_pcpu_ptr(&pcp1); \
  190. BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2)); \
  191. VM_BUG_ON((unsigned long)(&pcp1) % (2 * sizeof(pcp1))); \
  192. VM_BUG_ON((unsigned long)(&pcp2) != \
  193. (unsigned long)(&pcp1) + sizeof(pcp1)); \
  194. switch(sizeof(pcp1)) { \
  195. case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break; \
  196. case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break; \
  197. case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break; \
  198. case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break; \
  199. default: \
  200. __bad_size_call_parameter(); break; \
  201. } \
  202. pdcrb_ret__; \
  203. })
  204. #define __pcpu_size_call(stem, variable, ...) \
  205. do { \
  206. __verify_pcpu_ptr(&(variable)); \
  207. switch(sizeof(variable)) { \
  208. case 1: stem##1(variable, __VA_ARGS__);break; \
  209. case 2: stem##2(variable, __VA_ARGS__);break; \
  210. case 4: stem##4(variable, __VA_ARGS__);break; \
  211. case 8: stem##8(variable, __VA_ARGS__);break; \
  212. default: \
  213. __bad_size_call_parameter();break; \
  214. } \
  215. } while (0)
  216. /*
  217. * this_cpu operations (C) 2008-2013 Christoph Lameter <cl@linux.com>
  218. *
  219. * Optimized manipulation for memory allocated through the per cpu
  220. * allocator or for addresses of per cpu variables.
  221. *
  222. * These operation guarantee exclusivity of access for other operations
  223. * on the *same* processor. The assumption is that per cpu data is only
  224. * accessed by a single processor instance (the current one).
  225. *
  226. * The first group is used for accesses that must be done in a
  227. * preemption safe way since we know that the context is not preempt
  228. * safe. Interrupts may occur. If the interrupt modifies the variable
  229. * too then RMW actions will not be reliable.
  230. *
  231. * The arch code can provide optimized functions in two ways:
  232. *
  233. * 1. Override the function completely. F.e. define this_cpu_add().
  234. * The arch must then ensure that the various scalar format passed
  235. * are handled correctly.
  236. *
  237. * 2. Provide functions for certain scalar sizes. F.e. provide
  238. * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
  239. * sized RMW actions. If arch code does not provide operations for
  240. * a scalar size then the fallback in the generic code will be
  241. * used.
  242. */
  243. #define _this_cpu_generic_read(pcp) \
  244. ({ typeof(pcp) ret__; \
  245. preempt_disable(); \
  246. ret__ = *this_cpu_ptr(&(pcp)); \
  247. preempt_enable(); \
  248. ret__; \
  249. })
  250. #ifndef this_cpu_read
  251. # ifndef this_cpu_read_1
  252. # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
  253. # endif
  254. # ifndef this_cpu_read_2
  255. # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
  256. # endif
  257. # ifndef this_cpu_read_4
  258. # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
  259. # endif
  260. # ifndef this_cpu_read_8
  261. # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
  262. # endif
  263. # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
  264. #endif
  265. #define _this_cpu_generic_to_op(pcp, val, op) \
  266. do { \
  267. unsigned long flags; \
  268. raw_local_irq_save(flags); \
  269. *raw_cpu_ptr(&(pcp)) op val; \
  270. raw_local_irq_restore(flags); \
  271. } while (0)
  272. #ifndef this_cpu_write
  273. # ifndef this_cpu_write_1
  274. # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  275. # endif
  276. # ifndef this_cpu_write_2
  277. # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  278. # endif
  279. # ifndef this_cpu_write_4
  280. # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  281. # endif
  282. # ifndef this_cpu_write_8
  283. # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
  284. # endif
  285. # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
  286. #endif
  287. #ifndef this_cpu_add
  288. # ifndef this_cpu_add_1
  289. # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  290. # endif
  291. # ifndef this_cpu_add_2
  292. # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  293. # endif
  294. # ifndef this_cpu_add_4
  295. # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  296. # endif
  297. # ifndef this_cpu_add_8
  298. # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
  299. # endif
  300. # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
  301. #endif
  302. #ifndef this_cpu_sub
  303. # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(typeof(pcp))(val))
  304. #endif
  305. #ifndef this_cpu_inc
  306. # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
  307. #endif
  308. #ifndef this_cpu_dec
  309. # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
  310. #endif
  311. #ifndef this_cpu_and
  312. # ifndef this_cpu_and_1
  313. # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  314. # endif
  315. # ifndef this_cpu_and_2
  316. # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  317. # endif
  318. # ifndef this_cpu_and_4
  319. # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  320. # endif
  321. # ifndef this_cpu_and_8
  322. # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
  323. # endif
  324. # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
  325. #endif
  326. #ifndef this_cpu_or
  327. # ifndef this_cpu_or_1
  328. # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  329. # endif
  330. # ifndef this_cpu_or_2
  331. # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  332. # endif
  333. # ifndef this_cpu_or_4
  334. # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  335. # endif
  336. # ifndef this_cpu_or_8
  337. # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
  338. # endif
  339. # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
  340. #endif
  341. #define _this_cpu_generic_add_return(pcp, val) \
  342. ({ \
  343. typeof(pcp) ret__; \
  344. unsigned long flags; \
  345. raw_local_irq_save(flags); \
  346. raw_cpu_add(pcp, val); \
  347. ret__ = raw_cpu_read(pcp); \
  348. raw_local_irq_restore(flags); \
  349. ret__; \
  350. })
  351. #ifndef this_cpu_add_return
  352. # ifndef this_cpu_add_return_1
  353. # define this_cpu_add_return_1(pcp, val) _this_cpu_generic_add_return(pcp, val)
  354. # endif
  355. # ifndef this_cpu_add_return_2
  356. # define this_cpu_add_return_2(pcp, val) _this_cpu_generic_add_return(pcp, val)
  357. # endif
  358. # ifndef this_cpu_add_return_4
  359. # define this_cpu_add_return_4(pcp, val) _this_cpu_generic_add_return(pcp, val)
  360. # endif
  361. # ifndef this_cpu_add_return_8
  362. # define this_cpu_add_return_8(pcp, val) _this_cpu_generic_add_return(pcp, val)
  363. # endif
  364. # define this_cpu_add_return(pcp, val) __pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
  365. #endif
  366. #define this_cpu_sub_return(pcp, val) this_cpu_add_return(pcp, -(typeof(pcp))(val))
  367. #define this_cpu_inc_return(pcp) this_cpu_add_return(pcp, 1)
  368. #define this_cpu_dec_return(pcp) this_cpu_add_return(pcp, -1)
  369. #define _this_cpu_generic_xchg(pcp, nval) \
  370. ({ typeof(pcp) ret__; \
  371. unsigned long flags; \
  372. raw_local_irq_save(flags); \
  373. ret__ = raw_cpu_read(pcp); \
  374. raw_cpu_write(pcp, nval); \
  375. raw_local_irq_restore(flags); \
  376. ret__; \
  377. })
  378. #ifndef this_cpu_xchg
  379. # ifndef this_cpu_xchg_1
  380. # define this_cpu_xchg_1(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  381. # endif
  382. # ifndef this_cpu_xchg_2
  383. # define this_cpu_xchg_2(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  384. # endif
  385. # ifndef this_cpu_xchg_4
  386. # define this_cpu_xchg_4(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  387. # endif
  388. # ifndef this_cpu_xchg_8
  389. # define this_cpu_xchg_8(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
  390. # endif
  391. # define this_cpu_xchg(pcp, nval) \
  392. __pcpu_size_call_return2(this_cpu_xchg_, (pcp), nval)
  393. #endif
  394. #define _this_cpu_generic_cmpxchg(pcp, oval, nval) \
  395. ({ \
  396. typeof(pcp) ret__; \
  397. unsigned long flags; \
  398. raw_local_irq_save(flags); \
  399. ret__ = raw_cpu_read(pcp); \
  400. if (ret__ == (oval)) \
  401. raw_cpu_write(pcp, nval); \
  402. raw_local_irq_restore(flags); \
  403. ret__; \
  404. })
  405. #ifndef this_cpu_cmpxchg
  406. # ifndef this_cpu_cmpxchg_1
  407. # define this_cpu_cmpxchg_1(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  408. # endif
  409. # ifndef this_cpu_cmpxchg_2
  410. # define this_cpu_cmpxchg_2(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  411. # endif
  412. # ifndef this_cpu_cmpxchg_4
  413. # define this_cpu_cmpxchg_4(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  414. # endif
  415. # ifndef this_cpu_cmpxchg_8
  416. # define this_cpu_cmpxchg_8(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
  417. # endif
  418. # define this_cpu_cmpxchg(pcp, oval, nval) \
  419. __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
  420. #endif
  421. /*
  422. * cmpxchg_double replaces two adjacent scalars at once. The first
  423. * two parameters are per cpu variables which have to be of the same
  424. * size. A truth value is returned to indicate success or failure
  425. * (since a double register result is difficult to handle). There is
  426. * very limited hardware support for these operations, so only certain
  427. * sizes may work.
  428. */
  429. #define _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  430. ({ \
  431. int ret__; \
  432. unsigned long flags; \
  433. raw_local_irq_save(flags); \
  434. ret__ = raw_cpu_generic_cmpxchg_double(pcp1, pcp2, \
  435. oval1, oval2, nval1, nval2); \
  436. raw_local_irq_restore(flags); \
  437. ret__; \
  438. })
  439. #ifndef this_cpu_cmpxchg_double
  440. # ifndef this_cpu_cmpxchg_double_1
  441. # define this_cpu_cmpxchg_double_1(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_2
  445. # define this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  446. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  447. # endif
  448. # ifndef this_cpu_cmpxchg_double_4
  449. # define this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  450. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  451. # endif
  452. # ifndef this_cpu_cmpxchg_double_8
  453. # define this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  454. _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  455. # endif
  456. # define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  457. __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
  458. #endif
  459. /*
  460. * Generic percpu operations for contexts where we do not want to do
  461. * any checks for preemptiosn.
  462. *
  463. * If there is no other protection through preempt disable and/or
  464. * disabling interupts then one of these RMW operations can show unexpected
  465. * behavior because the execution thread was rescheduled on another processor
  466. * or an interrupt occurred and the same percpu variable was modified from
  467. * the interrupt context.
  468. */
  469. #ifndef raw_cpu_read
  470. # ifndef raw_cpu_read_1
  471. # define raw_cpu_read_1(pcp) (*raw_cpu_ptr(&(pcp)))
  472. # endif
  473. # ifndef raw_cpu_read_2
  474. # define raw_cpu_read_2(pcp) (*raw_cpu_ptr(&(pcp)))
  475. # endif
  476. # ifndef raw_cpu_read_4
  477. # define raw_cpu_read_4(pcp) (*raw_cpu_ptr(&(pcp)))
  478. # endif
  479. # ifndef raw_cpu_read_8
  480. # define raw_cpu_read_8(pcp) (*raw_cpu_ptr(&(pcp)))
  481. # endif
  482. # define raw_cpu_read(pcp) __pcpu_size_call_return(raw_cpu_read_, (pcp))
  483. #endif
  484. #define raw_cpu_generic_to_op(pcp, val, op) \
  485. do { \
  486. *raw_cpu_ptr(&(pcp)) op val; \
  487. } while (0)
  488. #ifndef raw_cpu_write
  489. # ifndef raw_cpu_write_1
  490. # define raw_cpu_write_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
  491. # endif
  492. # ifndef raw_cpu_write_2
  493. # define raw_cpu_write_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
  494. # endif
  495. # ifndef raw_cpu_write_4
  496. # define raw_cpu_write_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
  497. # endif
  498. # ifndef raw_cpu_write_8
  499. # define raw_cpu_write_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
  500. # endif
  501. # define raw_cpu_write(pcp, val) __pcpu_size_call(raw_cpu_write_, (pcp), (val))
  502. #endif
  503. #ifndef raw_cpu_add
  504. # ifndef raw_cpu_add_1
  505. # define raw_cpu_add_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
  506. # endif
  507. # ifndef raw_cpu_add_2
  508. # define raw_cpu_add_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
  509. # endif
  510. # ifndef raw_cpu_add_4
  511. # define raw_cpu_add_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
  512. # endif
  513. # ifndef raw_cpu_add_8
  514. # define raw_cpu_add_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
  515. # endif
  516. # define raw_cpu_add(pcp, val) __pcpu_size_call(raw_cpu_add_, (pcp), (val))
  517. #endif
  518. #ifndef raw_cpu_sub
  519. # define raw_cpu_sub(pcp, val) raw_cpu_add((pcp), -(val))
  520. #endif
  521. #ifndef raw_cpu_inc
  522. # define raw_cpu_inc(pcp) raw_cpu_add((pcp), 1)
  523. #endif
  524. #ifndef raw_cpu_dec
  525. # define raw_cpu_dec(pcp) raw_cpu_sub((pcp), 1)
  526. #endif
  527. #ifndef raw_cpu_and
  528. # ifndef raw_cpu_and_1
  529. # define raw_cpu_and_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
  530. # endif
  531. # ifndef raw_cpu_and_2
  532. # define raw_cpu_and_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
  533. # endif
  534. # ifndef raw_cpu_and_4
  535. # define raw_cpu_and_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
  536. # endif
  537. # ifndef raw_cpu_and_8
  538. # define raw_cpu_and_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
  539. # endif
  540. # define raw_cpu_and(pcp, val) __pcpu_size_call(raw_cpu_and_, (pcp), (val))
  541. #endif
  542. #ifndef raw_cpu_or
  543. # ifndef raw_cpu_or_1
  544. # define raw_cpu_or_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
  545. # endif
  546. # ifndef raw_cpu_or_2
  547. # define raw_cpu_or_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
  548. # endif
  549. # ifndef raw_cpu_or_4
  550. # define raw_cpu_or_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
  551. # endif
  552. # ifndef raw_cpu_or_8
  553. # define raw_cpu_or_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
  554. # endif
  555. # define raw_cpu_or(pcp, val) __pcpu_size_call(raw_cpu_or_, (pcp), (val))
  556. #endif
  557. #define raw_cpu_generic_add_return(pcp, val) \
  558. ({ \
  559. raw_cpu_add(pcp, val); \
  560. raw_cpu_read(pcp); \
  561. })
  562. #ifndef raw_cpu_add_return
  563. # ifndef raw_cpu_add_return_1
  564. # define raw_cpu_add_return_1(pcp, val) raw_cpu_generic_add_return(pcp, val)
  565. # endif
  566. # ifndef raw_cpu_add_return_2
  567. # define raw_cpu_add_return_2(pcp, val) raw_cpu_generic_add_return(pcp, val)
  568. # endif
  569. # ifndef raw_cpu_add_return_4
  570. # define raw_cpu_add_return_4(pcp, val) raw_cpu_generic_add_return(pcp, val)
  571. # endif
  572. # ifndef raw_cpu_add_return_8
  573. # define raw_cpu_add_return_8(pcp, val) raw_cpu_generic_add_return(pcp, val)
  574. # endif
  575. # define raw_cpu_add_return(pcp, val) \
  576. __pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
  577. #endif
  578. #define raw_cpu_sub_return(pcp, val) raw_cpu_add_return(pcp, -(typeof(pcp))(val))
  579. #define raw_cpu_inc_return(pcp) raw_cpu_add_return(pcp, 1)
  580. #define raw_cpu_dec_return(pcp) raw_cpu_add_return(pcp, -1)
  581. #define raw_cpu_generic_xchg(pcp, nval) \
  582. ({ typeof(pcp) ret__; \
  583. ret__ = raw_cpu_read(pcp); \
  584. raw_cpu_write(pcp, nval); \
  585. ret__; \
  586. })
  587. #ifndef raw_cpu_xchg
  588. # ifndef raw_cpu_xchg_1
  589. # define raw_cpu_xchg_1(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
  590. # endif
  591. # ifndef raw_cpu_xchg_2
  592. # define raw_cpu_xchg_2(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
  593. # endif
  594. # ifndef raw_cpu_xchg_4
  595. # define raw_cpu_xchg_4(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
  596. # endif
  597. # ifndef raw_cpu_xchg_8
  598. # define raw_cpu_xchg_8(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
  599. # endif
  600. # define raw_cpu_xchg(pcp, nval) \
  601. __pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval)
  602. #endif
  603. #define raw_cpu_generic_cmpxchg(pcp, oval, nval) \
  604. ({ \
  605. typeof(pcp) ret__; \
  606. ret__ = raw_cpu_read(pcp); \
  607. if (ret__ == (oval)) \
  608. raw_cpu_write(pcp, nval); \
  609. ret__; \
  610. })
  611. #ifndef raw_cpu_cmpxchg
  612. # ifndef raw_cpu_cmpxchg_1
  613. # define raw_cpu_cmpxchg_1(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
  614. # endif
  615. # ifndef raw_cpu_cmpxchg_2
  616. # define raw_cpu_cmpxchg_2(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
  617. # endif
  618. # ifndef raw_cpu_cmpxchg_4
  619. # define raw_cpu_cmpxchg_4(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
  620. # endif
  621. # ifndef raw_cpu_cmpxchg_8
  622. # define raw_cpu_cmpxchg_8(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
  623. # endif
  624. # define raw_cpu_cmpxchg(pcp, oval, nval) \
  625. __pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
  626. #endif
  627. #define raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  628. ({ \
  629. int __ret = 0; \
  630. if (raw_cpu_read(pcp1) == (oval1) && \
  631. raw_cpu_read(pcp2) == (oval2)) { \
  632. raw_cpu_write(pcp1, (nval1)); \
  633. raw_cpu_write(pcp2, (nval2)); \
  634. __ret = 1; \
  635. } \
  636. (__ret); \
  637. })
  638. #ifndef raw_cpu_cmpxchg_double
  639. # ifndef raw_cpu_cmpxchg_double_1
  640. # define raw_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  641. raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  642. # endif
  643. # ifndef raw_cpu_cmpxchg_double_2
  644. # define raw_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  645. raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  646. # endif
  647. # ifndef raw_cpu_cmpxchg_double_4
  648. # define raw_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  649. raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  650. # endif
  651. # ifndef raw_cpu_cmpxchg_double_8
  652. # define raw_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  653. raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  654. # endif
  655. # define raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  656. __pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
  657. #endif
  658. /*
  659. * Generic percpu operations for context that are safe from preemption/interrupts.
  660. */
  661. #ifndef __this_cpu_read
  662. # define __this_cpu_read(pcp) \
  663. (__this_cpu_preempt_check("read"),__pcpu_size_call_return(raw_cpu_read_, (pcp)))
  664. #endif
  665. #ifndef __this_cpu_write
  666. # define __this_cpu_write(pcp, val) \
  667. do { __this_cpu_preempt_check("write"); \
  668. __pcpu_size_call(raw_cpu_write_, (pcp), (val)); \
  669. } while (0)
  670. #endif
  671. #ifndef __this_cpu_add
  672. # define __this_cpu_add(pcp, val) \
  673. do { __this_cpu_preempt_check("add"); \
  674. __pcpu_size_call(raw_cpu_add_, (pcp), (val)); \
  675. } while (0)
  676. #endif
  677. #ifndef __this_cpu_sub
  678. # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(typeof(pcp))(val))
  679. #endif
  680. #ifndef __this_cpu_inc
  681. # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
  682. #endif
  683. #ifndef __this_cpu_dec
  684. # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
  685. #endif
  686. #ifndef __this_cpu_and
  687. # define __this_cpu_and(pcp, val) \
  688. do { __this_cpu_preempt_check("and"); \
  689. __pcpu_size_call(raw_cpu_and_, (pcp), (val)); \
  690. } while (0)
  691. #endif
  692. #ifndef __this_cpu_or
  693. # define __this_cpu_or(pcp, val) \
  694. do { __this_cpu_preempt_check("or"); \
  695. __pcpu_size_call(raw_cpu_or_, (pcp), (val)); \
  696. } while (0)
  697. #endif
  698. #ifndef __this_cpu_add_return
  699. # define __this_cpu_add_return(pcp, val) \
  700. (__this_cpu_preempt_check("add_return"),__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val))
  701. #endif
  702. #define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val))
  703. #define __this_cpu_inc_return(pcp) __this_cpu_add_return(pcp, 1)
  704. #define __this_cpu_dec_return(pcp) __this_cpu_add_return(pcp, -1)
  705. #ifndef __this_cpu_xchg
  706. # define __this_cpu_xchg(pcp, nval) \
  707. (__this_cpu_preempt_check("xchg"),__pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval))
  708. #endif
  709. #ifndef __this_cpu_cmpxchg
  710. # define __this_cpu_cmpxchg(pcp, oval, nval) \
  711. (__this_cpu_preempt_check("cmpxchg"),__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval))
  712. #endif
  713. #ifndef __this_cpu_cmpxchg_double
  714. # define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
  715. (__this_cpu_preempt_check("cmpxchg_double"),__pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2)))
  716. #endif
  717. #endif /* __LINUX_PERCPU_H */