1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
4 #include <linux/preempt.h>
5 #include <linux/slab.h> /* For kmalloc() */
7 #include <linux/cpumask.h>
10 #include <asm/percpu.h>
12 /* enough to cover all DEFINE_PER_CPUs in modules */
14 #define PERCPU_MODULE_RESERVE (8 << 10)
16 #define PERCPU_MODULE_RESERVE 0
19 #ifndef PERCPU_ENOUGH_ROOM
20 #define PERCPU_ENOUGH_ROOM \
21 (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
22 PERCPU_MODULE_RESERVE)
26 * Must be an lvalue. Since @var must be a simple identifier,
27 * we force a syntax error here if it isn't.
29 #define get_cpu_var(var) (*({ \
31 &__get_cpu_var(var); }))
34 * The weird & is necessary because sparse considers (void)(var) to be
35 * a direct dereference of percpu variable (var).
37 #define put_cpu_var(var) do { \
44 /* minimum unit size, also is the maximum supported allocation size */
45 #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(64 << 10)
48 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
49 * back on the first chunk for dynamic percpu allocation if arch is
50 * manually allocating and mapping it for faster access (as a part of
51 * large page mapping for example).
53 * The following values give between one and two pages of free space
54 * after typical minimal boot (2-way SMP, single disk and NIC) with
55 * both defconfig and a distro config on x86_64 and 32. More
56 * intelligent way to determine this would be nice.
58 #if BITS_PER_LONG > 32
59 #define PERCPU_DYNAMIC_RESERVE (20 << 10)
61 #define PERCPU_DYNAMIC_RESERVE (12 << 10)
64 extern void *pcpu_base_addr;
65 extern const unsigned long *pcpu_unit_offsets;
67 struct pcpu_group_info {
68 int nr_units; /* aligned # of units */
69 unsigned long base_offset; /* base address offset */
70 unsigned int *cpu_map; /* unit->cpu map, empty
71 * entries contain NR_CPUS */
74 struct pcpu_alloc_info {
81 size_t __ai_size; /* internal, don't use */
82 int nr_groups; /* 0 if grouping unnecessary */
83 struct pcpu_group_info groups[];
93 extern const char *pcpu_fc_names[PCPU_FC_NR];
95 extern enum pcpu_fc pcpu_chosen_fc;
97 typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
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);
103 extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
105 extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
107 extern struct pcpu_alloc_info * __init pcpu_build_alloc_info(
108 size_t reserved_size, ssize_t dyn_size,
110 pcpu_fc_cpu_distance_fn_t cpu_distance_fn);
112 extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
115 #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
116 extern int __init pcpu_embed_first_chunk(size_t reserved_size, ssize_t dyn_size,
118 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
119 pcpu_fc_alloc_fn_t alloc_fn,
120 pcpu_fc_free_fn_t free_fn);
123 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
124 extern int __init pcpu_page_first_chunk(size_t reserved_size,
125 pcpu_fc_alloc_fn_t alloc_fn,
126 pcpu_fc_free_fn_t free_fn,
127 pcpu_fc_populate_pte_fn_t populate_pte_fn);
131 * Use this to get to a cpu's version of the per-cpu object
132 * dynamically allocated. Non-atomic access to the current CPU's
133 * version should probably be combined with get_cpu()/put_cpu().
135 #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
137 extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
138 extern void __percpu *__alloc_percpu(size_t size, size_t align);
139 extern void free_percpu(void __percpu *__pdata);
140 extern bool is_kernel_percpu_address(unsigned long addr);
141 extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
143 #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
144 extern void __init setup_per_cpu_areas(void);
147 #else /* CONFIG_SMP */
149 #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
151 static inline void __percpu *__alloc_percpu(size_t size, size_t align)
154 * Can't easily make larger alignment work with kmalloc. WARN
155 * on it. Larger alignment should only be used for module
156 * percpu sections on SMP for which this path isn't used.
158 WARN_ON_ONCE(align > SMP_CACHE_BYTES);
159 return kzalloc(size, GFP_KERNEL);
162 static inline void free_percpu(void __percpu *p)
167 /* can't distinguish from other static vars, always false */
168 static inline bool is_kernel_percpu_address(unsigned long addr)
173 static inline phys_addr_t per_cpu_ptr_to_phys(void *addr)
178 static inline void __init setup_per_cpu_areas(void) { }
180 static inline void *pcpu_lpage_remapped(void *kaddr)
185 #endif /* CONFIG_SMP */
187 #define alloc_percpu(type) \
188 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
191 * Optional methods for optimized non-lvalue per-cpu variable access.
193 * @var can be a percpu variable or a field of it and its size should
194 * equal char, int or long. percpu_read() evaluates to a lvalue and
195 * all others to void.
197 * These operations are guaranteed to be atomic w.r.t. preemption.
198 * The generic versions use plain get/put_cpu_var(). Archs are
199 * encouraged to implement single-instruction alternatives which don't
200 * require preemption protection.
203 # define percpu_read(var) \
205 typeof(var) *pr_ptr__ = &(var); \
206 typeof(var) pr_ret__; \
207 pr_ret__ = get_cpu_var(*pr_ptr__); \
208 put_cpu_var(*pr_ptr__); \
213 #define __percpu_generic_to_op(var, val, op) \
215 typeof(var) *pgto_ptr__ = &(var); \
216 get_cpu_var(*pgto_ptr__) op val; \
217 put_cpu_var(*pgto_ptr__); \
221 # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
225 # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
229 # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
233 # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
237 # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
241 # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
245 * Branching function to split up a function into a set of functions that
246 * are called for different scalar sizes of the objects handled.
249 extern void __bad_size_call_parameter(void);
251 #define __pcpu_size_call_return(stem, variable) \
252 ({ typeof(variable) pscr_ret__; \
253 __verify_pcpu_ptr(&(variable)); \
254 switch(sizeof(variable)) { \
255 case 1: pscr_ret__ = stem##1(variable);break; \
256 case 2: pscr_ret__ = stem##2(variable);break; \
257 case 4: pscr_ret__ = stem##4(variable);break; \
258 case 8: pscr_ret__ = stem##8(variable);break; \
260 __bad_size_call_parameter();break; \
265 #define __pcpu_size_call(stem, variable, ...) \
267 __verify_pcpu_ptr(&(variable)); \
268 switch(sizeof(variable)) { \
269 case 1: stem##1(variable, __VA_ARGS__);break; \
270 case 2: stem##2(variable, __VA_ARGS__);break; \
271 case 4: stem##4(variable, __VA_ARGS__);break; \
272 case 8: stem##8(variable, __VA_ARGS__);break; \
274 __bad_size_call_parameter();break; \
279 * Optimized manipulation for memory allocated through the per cpu
280 * allocator or for addresses of per cpu variables.
282 * These operation guarantee exclusivity of access for other operations
283 * on the *same* processor. The assumption is that per cpu data is only
284 * accessed by a single processor instance (the current one).
286 * The first group is used for accesses that must be done in a
287 * preemption safe way since we know that the context is not preempt
288 * safe. Interrupts may occur. If the interrupt modifies the variable
289 * too then RMW actions will not be reliable.
291 * The arch code can provide optimized functions in two ways:
293 * 1. Override the function completely. F.e. define this_cpu_add().
294 * The arch must then ensure that the various scalar format passed
295 * are handled correctly.
297 * 2. Provide functions for certain scalar sizes. F.e. provide
298 * this_cpu_add_2() to provide per cpu atomic operations for 2 byte
299 * sized RMW actions. If arch code does not provide operations for
300 * a scalar size then the fallback in the generic code will be
304 #define _this_cpu_generic_read(pcp) \
305 ({ typeof(pcp) ret__; \
307 ret__ = *this_cpu_ptr(&(pcp)); \
312 #ifndef this_cpu_read
313 # ifndef this_cpu_read_1
314 # define this_cpu_read_1(pcp) _this_cpu_generic_read(pcp)
316 # ifndef this_cpu_read_2
317 # define this_cpu_read_2(pcp) _this_cpu_generic_read(pcp)
319 # ifndef this_cpu_read_4
320 # define this_cpu_read_4(pcp) _this_cpu_generic_read(pcp)
322 # ifndef this_cpu_read_8
323 # define this_cpu_read_8(pcp) _this_cpu_generic_read(pcp)
325 # define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
328 #define _this_cpu_generic_to_op(pcp, val, op) \
331 *__this_cpu_ptr(&(pcp)) op val; \
335 #ifndef this_cpu_write
336 # ifndef this_cpu_write_1
337 # define this_cpu_write_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
339 # ifndef this_cpu_write_2
340 # define this_cpu_write_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
342 # ifndef this_cpu_write_4
343 # define this_cpu_write_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
345 # ifndef this_cpu_write_8
346 # define this_cpu_write_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), =)
348 # define this_cpu_write(pcp, val) __pcpu_size_call(this_cpu_write_, (pcp), (val))
352 # ifndef this_cpu_add_1
353 # define this_cpu_add_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
355 # ifndef this_cpu_add_2
356 # define this_cpu_add_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
358 # ifndef this_cpu_add_4
359 # define this_cpu_add_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
361 # ifndef this_cpu_add_8
362 # define this_cpu_add_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), +=)
364 # define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, (pcp), (val))
368 # define this_cpu_sub(pcp, val) this_cpu_add((pcp), -(val))
372 # define this_cpu_inc(pcp) this_cpu_add((pcp), 1)
376 # define this_cpu_dec(pcp) this_cpu_sub((pcp), 1)
380 # ifndef this_cpu_and_1
381 # define this_cpu_and_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
383 # ifndef this_cpu_and_2
384 # define this_cpu_and_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
386 # ifndef this_cpu_and_4
387 # define this_cpu_and_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
389 # ifndef this_cpu_and_8
390 # define this_cpu_and_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), &=)
392 # define this_cpu_and(pcp, val) __pcpu_size_call(this_cpu_and_, (pcp), (val))
396 # ifndef this_cpu_or_1
397 # define this_cpu_or_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
399 # ifndef this_cpu_or_2
400 # define this_cpu_or_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
402 # ifndef this_cpu_or_4
403 # define this_cpu_or_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
405 # ifndef this_cpu_or_8
406 # define this_cpu_or_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), |=)
408 # define this_cpu_or(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
412 # ifndef this_cpu_xor_1
413 # define this_cpu_xor_1(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
415 # ifndef this_cpu_xor_2
416 # define this_cpu_xor_2(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
418 # ifndef this_cpu_xor_4
419 # define this_cpu_xor_4(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
421 # ifndef this_cpu_xor_8
422 # define this_cpu_xor_8(pcp, val) _this_cpu_generic_to_op((pcp), (val), ^=)
424 # define this_cpu_xor(pcp, val) __pcpu_size_call(this_cpu_or_, (pcp), (val))
428 * Generic percpu operations that do not require preemption handling.
429 * Either we do not care about races or the caller has the
430 * responsibility of handling preemptions issues. Arch code can still
431 * override these instructions since the arch per cpu code may be more
432 * efficient and may actually get race freeness for free (that is the
433 * case for x86 for example).
435 * If there is no other protection through preempt disable and/or
436 * disabling interupts then one of these RMW operations can show unexpected
437 * behavior because the execution thread was rescheduled on another processor
438 * or an interrupt occurred and the same percpu variable was modified from
439 * the interrupt context.
441 #ifndef __this_cpu_read
442 # ifndef __this_cpu_read_1
443 # define __this_cpu_read_1(pcp) (*__this_cpu_ptr(&(pcp)))
445 # ifndef __this_cpu_read_2
446 # define __this_cpu_read_2(pcp) (*__this_cpu_ptr(&(pcp)))
448 # ifndef __this_cpu_read_4
449 # define __this_cpu_read_4(pcp) (*__this_cpu_ptr(&(pcp)))
451 # ifndef __this_cpu_read_8
452 # define __this_cpu_read_8(pcp) (*__this_cpu_ptr(&(pcp)))
454 # define __this_cpu_read(pcp) __pcpu_size_call_return(__this_cpu_read_, (pcp))
457 #define __this_cpu_generic_to_op(pcp, val, op) \
459 *__this_cpu_ptr(&(pcp)) op val; \
462 #ifndef __this_cpu_write
463 # ifndef __this_cpu_write_1
464 # define __this_cpu_write_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
466 # ifndef __this_cpu_write_2
467 # define __this_cpu_write_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
469 # ifndef __this_cpu_write_4
470 # define __this_cpu_write_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
472 # ifndef __this_cpu_write_8
473 # define __this_cpu_write_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), =)
475 # define __this_cpu_write(pcp, val) __pcpu_size_call(__this_cpu_write_, (pcp), (val))
478 #ifndef __this_cpu_add
479 # ifndef __this_cpu_add_1
480 # define __this_cpu_add_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
482 # ifndef __this_cpu_add_2
483 # define __this_cpu_add_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
485 # ifndef __this_cpu_add_4
486 # define __this_cpu_add_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
488 # ifndef __this_cpu_add_8
489 # define __this_cpu_add_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), +=)
491 # define __this_cpu_add(pcp, val) __pcpu_size_call(__this_cpu_add_, (pcp), (val))
494 #ifndef __this_cpu_sub
495 # define __this_cpu_sub(pcp, val) __this_cpu_add((pcp), -(val))
498 #ifndef __this_cpu_inc
499 # define __this_cpu_inc(pcp) __this_cpu_add((pcp), 1)
502 #ifndef __this_cpu_dec
503 # define __this_cpu_dec(pcp) __this_cpu_sub((pcp), 1)
506 #ifndef __this_cpu_and
507 # ifndef __this_cpu_and_1
508 # define __this_cpu_and_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
510 # ifndef __this_cpu_and_2
511 # define __this_cpu_and_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
513 # ifndef __this_cpu_and_4
514 # define __this_cpu_and_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
516 # ifndef __this_cpu_and_8
517 # define __this_cpu_and_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), &=)
519 # define __this_cpu_and(pcp, val) __pcpu_size_call(__this_cpu_and_, (pcp), (val))
522 #ifndef __this_cpu_or
523 # ifndef __this_cpu_or_1
524 # define __this_cpu_or_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
526 # ifndef __this_cpu_or_2
527 # define __this_cpu_or_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
529 # ifndef __this_cpu_or_4
530 # define __this_cpu_or_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
532 # ifndef __this_cpu_or_8
533 # define __this_cpu_or_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), |=)
535 # define __this_cpu_or(pcp, val) __pcpu_size_call(__this_cpu_or_, (pcp), (val))
538 #ifndef __this_cpu_xor
539 # ifndef __this_cpu_xor_1
540 # define __this_cpu_xor_1(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
542 # ifndef __this_cpu_xor_2
543 # define __this_cpu_xor_2(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
545 # ifndef __this_cpu_xor_4
546 # define __this_cpu_xor_4(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
548 # ifndef __this_cpu_xor_8
549 # define __this_cpu_xor_8(pcp, val) __this_cpu_generic_to_op((pcp), (val), ^=)
551 # define __this_cpu_xor(pcp, val) __pcpu_size_call(__this_cpu_xor_, (pcp), (val))
555 * IRQ safe versions of the per cpu RMW operations. Note that these operations
556 * are *not* safe against modification of the same variable from another
557 * processors (which one gets when using regular atomic operations)
558 . They are guaranteed to be atomic vs. local interrupts and
561 #define irqsafe_cpu_generic_to_op(pcp, val, op) \
563 unsigned long flags; \
564 local_irq_save(flags); \
565 *__this_cpu_ptr(&(pcp)) op val; \
566 local_irq_restore(flags); \
569 #ifndef irqsafe_cpu_add
570 # ifndef irqsafe_cpu_add_1
571 # define irqsafe_cpu_add_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
573 # ifndef irqsafe_cpu_add_2
574 # define irqsafe_cpu_add_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
576 # ifndef irqsafe_cpu_add_4
577 # define irqsafe_cpu_add_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
579 # ifndef irqsafe_cpu_add_8
580 # define irqsafe_cpu_add_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), +=)
582 # define irqsafe_cpu_add(pcp, val) __pcpu_size_call(irqsafe_cpu_add_, (pcp), (val))
585 #ifndef irqsafe_cpu_sub
586 # define irqsafe_cpu_sub(pcp, val) irqsafe_cpu_add((pcp), -(val))
589 #ifndef irqsafe_cpu_inc
590 # define irqsafe_cpu_inc(pcp) irqsafe_cpu_add((pcp), 1)
593 #ifndef irqsafe_cpu_dec
594 # define irqsafe_cpu_dec(pcp) irqsafe_cpu_sub((pcp), 1)
597 #ifndef irqsafe_cpu_and
598 # ifndef irqsafe_cpu_and_1
599 # define irqsafe_cpu_and_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
601 # ifndef irqsafe_cpu_and_2
602 # define irqsafe_cpu_and_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
604 # ifndef irqsafe_cpu_and_4
605 # define irqsafe_cpu_and_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
607 # ifndef irqsafe_cpu_and_8
608 # define irqsafe_cpu_and_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), &=)
610 # define irqsafe_cpu_and(pcp, val) __pcpu_size_call(irqsafe_cpu_and_, (val))
613 #ifndef irqsafe_cpu_or
614 # ifndef irqsafe_cpu_or_1
615 # define irqsafe_cpu_or_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
617 # ifndef irqsafe_cpu_or_2
618 # define irqsafe_cpu_or_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
620 # ifndef irqsafe_cpu_or_4
621 # define irqsafe_cpu_or_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
623 # ifndef irqsafe_cpu_or_8
624 # define irqsafe_cpu_or_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), |=)
626 # define irqsafe_cpu_or(pcp, val) __pcpu_size_call(irqsafe_cpu_or_, (val))
629 #ifndef irqsafe_cpu_xor
630 # ifndef irqsafe_cpu_xor_1
631 # define irqsafe_cpu_xor_1(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
633 # ifndef irqsafe_cpu_xor_2
634 # define irqsafe_cpu_xor_2(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
636 # ifndef irqsafe_cpu_xor_4
637 # define irqsafe_cpu_xor_4(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
639 # ifndef irqsafe_cpu_xor_8
640 # define irqsafe_cpu_xor_8(pcp, val) irqsafe_cpu_generic_to_op((pcp), (val), ^=)
642 # define irqsafe_cpu_xor(pcp, val) __pcpu_size_call(irqsafe_cpu_xor_, (val))
645 #endif /* __LINUX_PERCPU_H */