1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
4 #include <linux/mmdebug.h>
5 #include <linux/preempt.h>
7 #include <linux/cpumask.h>
9 #include <linux/init.h>
11 #include <asm/percpu.h>
13 /* enough to cover all DEFINE_PER_CPUs in modules */
15 #define PERCPU_MODULE_RESERVE (8 << 10)
17 #define PERCPU_MODULE_RESERVE 0
20 #ifndef PERCPU_ENOUGH_ROOM
21 #define PERCPU_ENOUGH_ROOM \
22 (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
23 PERCPU_MODULE_RESERVE)
26 /* minimum unit size, also is the maximum supported allocation size */
27 #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
30 * Percpu allocator can serve percpu allocations before slab is
31 * initialized which allows slab to depend on the percpu allocator.
32 * The following two parameters decide how much resource to
33 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
34 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
36 #define PERCPU_DYNAMIC_EARLY_SLOTS 128
37 #define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
40 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
41 * back on the first chunk for dynamic percpu allocation if arch is
42 * manually allocating and mapping it for faster access (as a part of
43 * large page mapping for example).
45 * The following values give between one and two pages of free space
46 * after typical minimal boot (2-way SMP, single disk and NIC) with
47 * both defconfig and a distro config on x86_64 and 32. More
48 * intelligent way to determine this would be nice.
50 #if BITS_PER_LONG > 32
51 #define PERCPU_DYNAMIC_RESERVE (20 << 10)
53 #define PERCPU_DYNAMIC_RESERVE (12 << 10)
56 extern void *pcpu_base_addr
;
57 extern const unsigned long *pcpu_unit_offsets
;
59 struct pcpu_group_info
{
60 int nr_units
; /* aligned # of units */
61 unsigned long base_offset
; /* base address offset */
62 unsigned int *cpu_map
; /* unit->cpu map, empty
63 * entries contain NR_CPUS */
66 struct pcpu_alloc_info
{
73 size_t __ai_size
; /* internal, don't use */
74 int nr_groups
; /* 0 if grouping unnecessary */
75 struct pcpu_group_info groups
[];
85 extern const char * const pcpu_fc_names
[PCPU_FC_NR
];
87 extern enum pcpu_fc pcpu_chosen_fc
;
89 typedef void * (*pcpu_fc_alloc_fn_t
)(unsigned int cpu
, size_t size
,
91 typedef void (*pcpu_fc_free_fn_t
)(void *ptr
, size_t size
);
92 typedef void (*pcpu_fc_populate_pte_fn_t
)(unsigned long addr
);
93 typedef int (pcpu_fc_cpu_distance_fn_t
)(unsigned int from
, unsigned int to
);
95 extern struct pcpu_alloc_info
* __init
pcpu_alloc_alloc_info(int nr_groups
,
97 extern void __init
pcpu_free_alloc_info(struct pcpu_alloc_info
*ai
);
99 extern int __init
pcpu_setup_first_chunk(const struct pcpu_alloc_info
*ai
,
102 #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
103 extern int __init
pcpu_embed_first_chunk(size_t reserved_size
, size_t dyn_size
,
105 pcpu_fc_cpu_distance_fn_t cpu_distance_fn
,
106 pcpu_fc_alloc_fn_t alloc_fn
,
107 pcpu_fc_free_fn_t free_fn
);
110 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
111 extern int __init
pcpu_page_first_chunk(size_t reserved_size
,
112 pcpu_fc_alloc_fn_t alloc_fn
,
113 pcpu_fc_free_fn_t free_fn
,
114 pcpu_fc_populate_pte_fn_t populate_pte_fn
);
117 extern void __percpu
*__alloc_reserved_percpu(size_t size
, size_t align
);
118 extern bool is_kernel_percpu_address(unsigned long addr
);
120 #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
121 extern void __init
setup_per_cpu_areas(void);
123 extern void __init
percpu_init_late(void);
125 extern void __percpu
*__alloc_percpu(size_t size
, size_t align
);
126 extern void free_percpu(void __percpu
*__pdata
);
127 extern phys_addr_t
per_cpu_ptr_to_phys(void *addr
);
129 #define alloc_percpu(type) \
130 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
133 * Branching function to split up a function into a set of functions that
134 * are called for different scalar sizes of the objects handled.
137 extern void __bad_size_call_parameter(void);
139 #ifdef CONFIG_DEBUG_PREEMPT
140 extern void __this_cpu_preempt_check(const char *op
);
142 static inline void __this_cpu_preempt_check(const char *op
) { }
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; \
154 __bad_size_call_parameter();break; \
159 #define __pcpu_size_call_return2(stem, variable, ...) \
161 typeof(variable) pscr2_ret__; \
162 __verify_pcpu_ptr(&(variable)); \
163 switch(sizeof(variable)) { \
164 case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break; \
165 case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break; \
166 case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break; \
167 case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break; \
169 __bad_size_call_parameter(); break; \
175 * Special handling for cmpxchg_double. cmpxchg_double is passed two
176 * percpu variables. The first has to be aligned to a double word
177 * boundary and the second has to follow directly thereafter.
178 * We enforce this on all architectures even if they don't support
179 * a double cmpxchg instruction, since it's a cheap requirement, and it
180 * avoids breaking the requirement for architectures with the instruction.
182 #define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...) \
185 __verify_pcpu_ptr(&pcp1); \
186 BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2)); \
187 VM_BUG_ON((unsigned long)(&pcp1) % (2 * sizeof(pcp1))); \
188 VM_BUG_ON((unsigned long)(&pcp2) != \
189 (unsigned long)(&pcp1) + sizeof(pcp1)); \
190 switch(sizeof(pcp1)) { \
191 case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break; \
192 case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break; \
193 case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break; \
194 case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break; \
196 __bad_size_call_parameter(); break; \
201 #define __pcpu_size_call(stem, variable, ...) \
203 __verify_pcpu_ptr(&(variable)); \
204 switch(sizeof(variable)) { \
205 case 1: stem##1(variable, __VA_ARGS__);break; \
206 case 2: stem##2(variable, __VA_ARGS__);break; \
207 case 4: stem##4(variable, __VA_ARGS__);break; \
208 case 8: stem##8(variable, __VA_ARGS__);break; \
210 __bad_size_call_parameter();break; \
215 * this_cpu operations (C) 2008-2013 Christoph Lameter <cl@linux.com>
217 * Optimized manipulation for memory allocated through the per cpu
218 * allocator or for addresses of per cpu variables.
220 * These operation guarantee exclusivity of access for other operations
221 * on the *same* processor. The assumption is that per cpu data is only
222 * accessed by a single processor instance (the current one).
224 * The first group is used for accesses that must be done in a
225 * preemption safe way since we know that the context is not preempt
226 * safe. Interrupts may occur. If the interrupt modifies the variable
227 * too then RMW actions will not be reliable.
229 * The arch code can provide optimized functions in two ways:
231 * 1. Override the function completely. F.e. define this_cpu_add().
232 * The arch must then ensure that the various scalar format passed
233 * are handled correctly.
235 * 2. Provide functions for certain scalar sizes. F.e. provide
236 * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
237 * sized RMW actions. If arch code does not provide operations for
238 * a scalar size then the fallback in the generic code will be
242 #define _this_cpu_generic_read(pcp) \
243 ({ typeof(pcp) ret__; \
245 ret__ = *this_cpu_ptr(&(pcp)); \
250 #ifndef this_cpu_read
251 # ifndef this_cpu_read_1
252 # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
254 # ifndef this_cpu_read_2
255 # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
257 # ifndef this_cpu_read_4
258 # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
260 # ifndef this_cpu_read_8
261 # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
263 # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
266 #define _this_cpu_generic_to_op(pcp, val, op) \
268 unsigned long flags; \
269 raw_local_irq_save(flags); \
270 *raw_cpu_ptr(&(pcp)) op val; \
271 raw_local_irq_restore(flags); \
274 #ifndef this_cpu_write
275 # ifndef this_cpu_write_1
276 # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
278 # ifndef this_cpu_write_2
279 # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
281 # ifndef this_cpu_write_4
282 # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
284 # ifndef this_cpu_write_8
285 # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
287 # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
291 # ifndef this_cpu_add_1
292 # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
294 # ifndef this_cpu_add_2
295 # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
297 # ifndef this_cpu_add_4
298 # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
300 # ifndef this_cpu_add_8
301 # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
303 # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
307 # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(typeof(pcp))(val))
311 # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
315 # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
319 # ifndef this_cpu_and_1
320 # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
322 # ifndef this_cpu_and_2
323 # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
325 # ifndef this_cpu_and_4
326 # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
328 # ifndef this_cpu_and_8
329 # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
331 # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
335 # ifndef this_cpu_or_1
336 # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
338 # ifndef this_cpu_or_2
339 # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
341 # ifndef this_cpu_or_4
342 # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
344 # ifndef this_cpu_or_8
345 # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
347 # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
350 #define _this_cpu_generic_add_return(pcp, val) \
353 unsigned long flags; \
354 raw_local_irq_save(flags); \
355 raw_cpu_add(pcp, val); \
356 ret__ = raw_cpu_read(pcp); \
357 raw_local_irq_restore(flags); \
361 #ifndef this_cpu_add_return
362 # ifndef this_cpu_add_return_1
363 # define this_cpu_add_return_1(pcp, val) _this_cpu_generic_add_return(pcp, val)
365 # ifndef this_cpu_add_return_2
366 # define this_cpu_add_return_2(pcp, val) _this_cpu_generic_add_return(pcp, val)
368 # ifndef this_cpu_add_return_4
369 # define this_cpu_add_return_4(pcp, val) _this_cpu_generic_add_return(pcp, val)
371 # ifndef this_cpu_add_return_8
372 # define this_cpu_add_return_8(pcp, val) _this_cpu_generic_add_return(pcp, val)
374 # define this_cpu_add_return(pcp, val) __pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
377 #define this_cpu_sub_return(pcp, val) this_cpu_add_return(pcp, -(typeof(pcp))(val))
378 #define this_cpu_inc_return(pcp) this_cpu_add_return(pcp, 1)
379 #define this_cpu_dec_return(pcp) this_cpu_add_return(pcp, -1)
381 #define _this_cpu_generic_xchg(pcp, nval) \
382 ({ typeof(pcp) ret__; \
383 unsigned long flags; \
384 raw_local_irq_save(flags); \
385 ret__ = raw_cpu_read(pcp); \
386 raw_cpu_write(pcp, nval); \
387 raw_local_irq_restore(flags); \
391 #ifndef this_cpu_xchg
392 # ifndef this_cpu_xchg_1
393 # define this_cpu_xchg_1(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
395 # ifndef this_cpu_xchg_2
396 # define this_cpu_xchg_2(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
398 # ifndef this_cpu_xchg_4
399 # define this_cpu_xchg_4(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
401 # ifndef this_cpu_xchg_8
402 # define this_cpu_xchg_8(pcp, nval) _this_cpu_generic_xchg(pcp, nval)
404 # define this_cpu_xchg(pcp, nval) \
405 __pcpu_size_call_return2(this_cpu_xchg_, (pcp), nval)
408 #define _this_cpu_generic_cmpxchg(pcp, oval, nval) \
411 unsigned long flags; \
412 raw_local_irq_save(flags); \
413 ret__ = raw_cpu_read(pcp); \
414 if (ret__ == (oval)) \
415 raw_cpu_write(pcp, nval); \
416 raw_local_irq_restore(flags); \
420 #ifndef this_cpu_cmpxchg
421 # ifndef this_cpu_cmpxchg_1
422 # define this_cpu_cmpxchg_1(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
424 # ifndef this_cpu_cmpxchg_2
425 # define this_cpu_cmpxchg_2(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
427 # ifndef this_cpu_cmpxchg_4
428 # define this_cpu_cmpxchg_4(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
430 # ifndef this_cpu_cmpxchg_8
431 # define this_cpu_cmpxchg_8(pcp, oval, nval) _this_cpu_generic_cmpxchg(pcp, oval, nval)
433 # define this_cpu_cmpxchg(pcp, oval, nval) \
434 __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
438 * cmpxchg_double replaces two adjacent scalars at once. The first
439 * two parameters are per cpu variables which have to be of the same
440 * size. A truth value is returned to indicate success or failure
441 * (since a double register result is difficult to handle). There is
442 * very limited hardware support for these operations, so only certain
445 #define _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
448 unsigned long flags; \
449 raw_local_irq_save(flags); \
450 ret__ = raw_cpu_generic_cmpxchg_double(pcp1, pcp2, \
451 oval1, oval2, nval1, nval2); \
452 raw_local_irq_restore(flags); \
456 #ifndef this_cpu_cmpxchg_double
457 # ifndef this_cpu_cmpxchg_double_1
458 # define this_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
459 _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
461 # ifndef this_cpu_cmpxchg_double_2
462 # define this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
463 _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
465 # ifndef this_cpu_cmpxchg_double_4
466 # define this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
467 _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
469 # ifndef this_cpu_cmpxchg_double_8
470 # define this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
471 _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
473 # define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
474 __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
478 * Generic percpu operations for contexts where we do not want to do
479 * any checks for preemptiosn.
481 * If there is no other protection through preempt disable and/or
482 * disabling interupts then one of these RMW operations can show unexpected
483 * behavior because the execution thread was rescheduled on another processor
484 * or an interrupt occurred and the same percpu variable was modified from
485 * the interrupt context.
488 # ifndef raw_cpu_read_1
489 # define raw_cpu_read_1(pcp) (*raw_cpu_ptr(&(pcp)))
491 # ifndef raw_cpu_read_2
492 # define raw_cpu_read_2(pcp) (*raw_cpu_ptr(&(pcp)))
494 # ifndef raw_cpu_read_4
495 # define raw_cpu_read_4(pcp) (*raw_cpu_ptr(&(pcp)))
497 # ifndef raw_cpu_read_8
498 # define raw_cpu_read_8(pcp) (*raw_cpu_ptr(&(pcp)))
500 # define raw_cpu_read(pcp) __pcpu_size_call_return(raw_cpu_read_, (pcp))
503 #define raw_cpu_generic_to_op(pcp, val, op) \
505 *raw_cpu_ptr(&(pcp)) op val; \
509 #ifndef raw_cpu_write
510 # ifndef raw_cpu_write_1
511 # define raw_cpu_write_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
513 # ifndef raw_cpu_write_2
514 # define raw_cpu_write_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
516 # ifndef raw_cpu_write_4
517 # define raw_cpu_write_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
519 # ifndef raw_cpu_write_8
520 # define raw_cpu_write_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), =)
522 # define raw_cpu_write(pcp, val) __pcpu_size_call(raw_cpu_write_, (pcp), (val))
526 # ifndef raw_cpu_add_1
527 # define raw_cpu_add_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
529 # ifndef raw_cpu_add_2
530 # define raw_cpu_add_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
532 # ifndef raw_cpu_add_4
533 # define raw_cpu_add_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
535 # ifndef raw_cpu_add_8
536 # define raw_cpu_add_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), +=)
538 # define raw_cpu_add(pcp, val) __pcpu_size_call(raw_cpu_add_, (pcp), (val))
542 # define raw_cpu_sub(pcp, val) raw_cpu_add((pcp), -(val))
546 # define raw_cpu_inc(pcp) raw_cpu_add((pcp), 1)
550 # define raw_cpu_dec(pcp) raw_cpu_sub((pcp), 1)
554 # ifndef raw_cpu_and_1
555 # define raw_cpu_and_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
557 # ifndef raw_cpu_and_2
558 # define raw_cpu_and_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
560 # ifndef raw_cpu_and_4
561 # define raw_cpu_and_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
563 # ifndef raw_cpu_and_8
564 # define raw_cpu_and_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), &=)
566 # define raw_cpu_and(pcp, val) __pcpu_size_call(raw_cpu_and_, (pcp), (val))
570 # ifndef raw_cpu_or_1
571 # define raw_cpu_or_1(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
573 # ifndef raw_cpu_or_2
574 # define raw_cpu_or_2(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
576 # ifndef raw_cpu_or_4
577 # define raw_cpu_or_4(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
579 # ifndef raw_cpu_or_8
580 # define raw_cpu_or_8(pcp, val) raw_cpu_generic_to_op((pcp), (val), |=)
582 # define raw_cpu_or(pcp, val) __pcpu_size_call(raw_cpu_or_, (pcp), (val))
585 #define raw_cpu_generic_add_return(pcp, val) \
587 raw_cpu_add(pcp, val); \
591 #ifndef raw_cpu_add_return
592 # ifndef raw_cpu_add_return_1
593 # define raw_cpu_add_return_1(pcp, val) raw_cpu_generic_add_return(pcp, val)
595 # ifndef raw_cpu_add_return_2
596 # define raw_cpu_add_return_2(pcp, val) raw_cpu_generic_add_return(pcp, val)
598 # ifndef raw_cpu_add_return_4
599 # define raw_cpu_add_return_4(pcp, val) raw_cpu_generic_add_return(pcp, val)
601 # ifndef raw_cpu_add_return_8
602 # define raw_cpu_add_return_8(pcp, val) raw_cpu_generic_add_return(pcp, val)
604 # define raw_cpu_add_return(pcp, val) \
605 __pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
608 #define raw_cpu_sub_return(pcp, val) raw_cpu_add_return(pcp, -(typeof(pcp))(val))
609 #define raw_cpu_inc_return(pcp) raw_cpu_add_return(pcp, 1)
610 #define raw_cpu_dec_return(pcp) raw_cpu_add_return(pcp, -1)
612 #define raw_cpu_generic_xchg(pcp, nval) \
613 ({ typeof(pcp) ret__; \
614 ret__ = raw_cpu_read(pcp); \
615 raw_cpu_write(pcp, nval); \
620 # ifndef raw_cpu_xchg_1
621 # define raw_cpu_xchg_1(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
623 # ifndef raw_cpu_xchg_2
624 # define raw_cpu_xchg_2(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
626 # ifndef raw_cpu_xchg_4
627 # define raw_cpu_xchg_4(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
629 # ifndef raw_cpu_xchg_8
630 # define raw_cpu_xchg_8(pcp, nval) raw_cpu_generic_xchg(pcp, nval)
632 # define raw_cpu_xchg(pcp, nval) \
633 __pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval)
636 #define raw_cpu_generic_cmpxchg(pcp, oval, nval) \
639 ret__ = raw_cpu_read(pcp); \
640 if (ret__ == (oval)) \
641 raw_cpu_write(pcp, nval); \
645 #ifndef raw_cpu_cmpxchg
646 # ifndef raw_cpu_cmpxchg_1
647 # define raw_cpu_cmpxchg_1(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
649 # ifndef raw_cpu_cmpxchg_2
650 # define raw_cpu_cmpxchg_2(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
652 # ifndef raw_cpu_cmpxchg_4
653 # define raw_cpu_cmpxchg_4(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
655 # ifndef raw_cpu_cmpxchg_8
656 # define raw_cpu_cmpxchg_8(pcp, oval, nval) raw_cpu_generic_cmpxchg(pcp, oval, nval)
658 # define raw_cpu_cmpxchg(pcp, oval, nval) \
659 __pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
662 #define raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
665 if (raw_cpu_read(pcp1) == (oval1) && \
666 raw_cpu_read(pcp2) == (oval2)) { \
667 raw_cpu_write(pcp1, (nval1)); \
668 raw_cpu_write(pcp2, (nval2)); \
674 #ifndef raw_cpu_cmpxchg_double
675 # ifndef raw_cpu_cmpxchg_double_1
676 # define raw_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2) \
677 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
679 # ifndef raw_cpu_cmpxchg_double_2
680 # define raw_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2) \
681 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
683 # ifndef raw_cpu_cmpxchg_double_4
684 # define raw_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2) \
685 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
687 # ifndef raw_cpu_cmpxchg_double_8
688 # define raw_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2) \
689 raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
691 # define raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
692 __pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
696 * Generic percpu operations for context that are safe from preemption/interrupts.
698 #ifndef __this_cpu_read
699 # define __this_cpu_read(pcp) \
700 (__this_cpu_preempt_check("read"),__pcpu_size_call_return(raw_cpu_read_, (pcp)))
703 #ifndef __this_cpu_write
704 # define __this_cpu_write(pcp, val) \
705 do { __this_cpu_preempt_check("write"); \
706 __pcpu_size_call(raw_cpu_write_, (pcp), (val)); \
710 #ifndef __this_cpu_add
711 # define __this_cpu_add(pcp, val) \
712 do { __this_cpu_preempt_check("add"); \
713 __pcpu_size_call(raw_cpu_add_, (pcp), (val)); \
717 #ifndef __this_cpu_sub
718 # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(typeof(pcp))(val))
721 #ifndef __this_cpu_inc
722 # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
725 #ifndef __this_cpu_dec
726 # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
729 #ifndef __this_cpu_and
730 # define __this_cpu_and(pcp, val) \
731 do { __this_cpu_preempt_check("and"); \
732 __pcpu_size_call(raw_cpu_and_, (pcp), (val)); \
737 #ifndef __this_cpu_or
738 # define __this_cpu_or(pcp, val) \
739 do { __this_cpu_preempt_check("or"); \
740 __pcpu_size_call(raw_cpu_or_, (pcp), (val)); \
744 #ifndef __this_cpu_add_return
745 # define __this_cpu_add_return(pcp, val) \
746 (__this_cpu_preempt_check("add_return"),__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val))
749 #define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val))
750 #define __this_cpu_inc_return(pcp) __this_cpu_add_return(pcp, 1)
751 #define __this_cpu_dec_return(pcp) __this_cpu_add_return(pcp, -1)
753 #ifndef __this_cpu_xchg
754 # define __this_cpu_xchg(pcp, nval) \
755 (__this_cpu_preempt_check("xchg"),__pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval))
758 #ifndef __this_cpu_cmpxchg
759 # define __this_cpu_cmpxchg(pcp, oval, nval) \
760 (__this_cpu_preempt_check("cmpxchg"),__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval))
763 #ifndef __this_cpu_cmpxchg_double
764 # define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
765 (__this_cpu_preempt_check("cmpxchg_double"),__pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2)))
768 #endif /* __LINUX_PERCPU_H */