percpu.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_PERCPU_H
  3. #define __LINUX_PERCPU_H
  4. #include <linux/mmdebug.h>
  5. #include <linux/preempt.h>
  6. #include <linux/smp.h>
  7. #include <linux/cpumask.h>
  8. #include <linux/pfn.h>
  9. #include <linux/init.h>
  10. #include <asm/percpu.h>
  11. /* enough to cover all DEFINE_PER_CPUs in modules */
  12. #ifdef CONFIG_MODULES
  13. #define PERCPU_MODULE_RESERVE (8 << 10)
  14. #else
  15. #define PERCPU_MODULE_RESERVE 0
  16. #endif
  17. /* minimum unit size, also is the maximum supported allocation size */
  18. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  19. /* minimum allocation size and shift in bytes */
  20. #define PCPU_MIN_ALLOC_SHIFT 2
  21. #define PCPU_MIN_ALLOC_SIZE (1 << PCPU_MIN_ALLOC_SHIFT)
  22. /*
  23. * The PCPU_BITMAP_BLOCK_SIZE must be the same size as PAGE_SIZE as the
  24. * updating of hints is used to manage the nr_empty_pop_pages in both
  25. * the chunk and globally.
  26. */
  27. #define PCPU_BITMAP_BLOCK_SIZE PAGE_SIZE
  28. #define PCPU_BITMAP_BLOCK_BITS (PCPU_BITMAP_BLOCK_SIZE >> \
  29. PCPU_MIN_ALLOC_SHIFT)
  30. /*
  31. * Percpu allocator can serve percpu allocations before slab is
  32. * initialized which allows slab to depend on the percpu allocator.
  33. * The following two parameters decide how much resource to
  34. * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
  35. * larger than PERCPU_DYNAMIC_EARLY_SIZE.
  36. */
  37. #define PERCPU_DYNAMIC_EARLY_SLOTS 128
  38. #define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
  39. /*
  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).
  44. *
  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.
  49. */
  50. #if BITS_PER_LONG > 32
  51. #define PERCPU_DYNAMIC_RESERVE (28 << 10)
  52. #else
  53. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  54. #endif
  55. extern void *pcpu_base_addr;
  56. extern const unsigned long *pcpu_unit_offsets;
  57. struct pcpu_group_info {
  58. int nr_units; /* aligned # of units */
  59. unsigned long base_offset; /* base address offset */
  60. unsigned int *cpu_map; /* unit->cpu map, empty
  61. * entries contain NR_CPUS */
  62. };
  63. struct pcpu_alloc_info {
  64. size_t static_size;
  65. size_t reserved_size;
  66. size_t dyn_size;
  67. size_t unit_size;
  68. size_t atom_size;
  69. size_t alloc_size;
  70. size_t __ai_size; /* internal, don't use */
  71. int nr_groups; /* 0 if grouping unnecessary */
  72. struct pcpu_group_info groups[];
  73. };
  74. enum pcpu_fc {
  75. PCPU_FC_AUTO,
  76. PCPU_FC_EMBED,
  77. PCPU_FC_PAGE,
  78. PCPU_FC_NR,
  79. };
  80. extern const char * const pcpu_fc_names[PCPU_FC_NR];
  81. extern enum pcpu_fc pcpu_chosen_fc;
  82. typedef int (pcpu_fc_cpu_to_node_fn_t)(int cpu);
  83. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  84. extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  85. int nr_units);
  86. extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
  87. extern void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  88. void *base_addr);
  89. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  90. extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
  91. size_t atom_size,
  92. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  93. pcpu_fc_cpu_to_node_fn_t cpu_to_nd_fn);
  94. #endif
  95. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  96. void __init pcpu_populate_pte(unsigned long addr);
  97. extern int __init pcpu_page_first_chunk(size_t reserved_size,
  98. pcpu_fc_cpu_to_node_fn_t cpu_to_nd_fn);
  99. #endif
  100. extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align) __alloc_size(1);
  101. extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
  102. extern bool is_kernel_percpu_address(unsigned long addr);
  103. #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
  104. extern void __init setup_per_cpu_areas(void);
  105. #endif
  106. extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1);
  107. extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1);
  108. extern void free_percpu(void __percpu *__pdata);
  109. extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
  110. #define alloc_percpu_gfp(type, gfp) \
  111. (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
  112. __alignof__(type), gfp)
  113. #define alloc_percpu(type) \
  114. (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \
  115. __alignof__(type))
  116. extern unsigned long pcpu_nr_pages(void);
  117. #endif /* __LINUX_PERCPU_H */