percpu-internal.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _MM_PERCPU_INTERNAL_H
  3. #define _MM_PERCPU_INTERNAL_H
  4. #include <linux/types.h>
  5. #include <linux/percpu.h>
  6. #include <linux/memcontrol.h>
  7. /*
  8. * pcpu_block_md is the metadata block struct.
  9. * Each chunk's bitmap is split into a number of full blocks.
  10. * All units are in terms of bits.
  11. *
  12. * The scan hint is the largest known contiguous area before the contig hint.
  13. * It is not necessarily the actual largest contig hint though. There is an
  14. * invariant that the scan_hint_start > contig_hint_start iff
  15. * scan_hint == contig_hint. This is necessary because when scanning forward,
  16. * we don't know if a new contig hint would be better than the current one.
  17. */
  18. struct pcpu_block_md {
  19. int scan_hint; /* scan hint for block */
  20. int scan_hint_start; /* block relative starting
  21. position of the scan hint */
  22. int contig_hint; /* contig hint for block */
  23. int contig_hint_start; /* block relative starting
  24. position of the contig hint */
  25. int left_free; /* size of free space along
  26. the left side of the block */
  27. int right_free; /* size of free space along
  28. the right side of the block */
  29. int first_free; /* block position of first free */
  30. int nr_bits; /* total bits responsible for */
  31. };
  32. struct pcpuobj_ext {
  33. #ifdef CONFIG_MEMCG
  34. struct obj_cgroup *cgroup;
  35. #endif
  36. #ifdef CONFIG_MEM_ALLOC_PROFILING
  37. union codetag_ref tag;
  38. #endif
  39. };
  40. #if defined(CONFIG_MEMCG) || defined(CONFIG_MEM_ALLOC_PROFILING)
  41. #define NEED_PCPUOBJ_EXT
  42. #endif
  43. struct pcpu_chunk {
  44. #ifdef CONFIG_PERCPU_STATS
  45. int nr_alloc; /* # of allocations */
  46. size_t max_alloc_size; /* largest allocation size */
  47. #endif
  48. struct list_head list; /* linked to pcpu_slot lists */
  49. int free_bytes; /* free bytes in the chunk */
  50. struct pcpu_block_md chunk_md;
  51. unsigned long *bound_map; /* boundary map */
  52. /*
  53. * base_addr is the base address of this chunk.
  54. * To reduce false sharing, current layout is optimized to make sure
  55. * base_addr locate in the different cacheline with free_bytes and
  56. * chunk_md.
  57. */
  58. void *base_addr ____cacheline_aligned_in_smp;
  59. unsigned long *alloc_map; /* allocation map */
  60. struct pcpu_block_md *md_blocks; /* metadata blocks */
  61. void *data; /* chunk data */
  62. bool immutable; /* no [de]population allowed */
  63. bool isolated; /* isolated from active chunk
  64. slots */
  65. int start_offset; /* the overlap with the previous
  66. region to have a page aligned
  67. base_addr */
  68. int end_offset; /* additional area required to
  69. have the region end page
  70. aligned */
  71. #ifdef NEED_PCPUOBJ_EXT
  72. struct pcpuobj_ext *obj_exts; /* vector of object cgroups */
  73. #endif
  74. int nr_pages; /* # of pages served by this chunk */
  75. int nr_populated; /* # of populated pages */
  76. int nr_empty_pop_pages; /* # of empty populated pages */
  77. unsigned long populated[]; /* populated bitmap */
  78. };
  79. static inline bool need_pcpuobj_ext(void)
  80. {
  81. if (IS_ENABLED(CONFIG_MEM_ALLOC_PROFILING))
  82. return true;
  83. if (!mem_cgroup_kmem_disabled())
  84. return true;
  85. return false;
  86. }
  87. extern spinlock_t pcpu_lock;
  88. extern struct list_head *pcpu_chunk_lists;
  89. extern int pcpu_nr_slots;
  90. extern int pcpu_sidelined_slot;
  91. extern int pcpu_to_depopulate_slot;
  92. extern int pcpu_nr_empty_pop_pages;
  93. extern struct pcpu_chunk *pcpu_first_chunk;
  94. extern struct pcpu_chunk *pcpu_reserved_chunk;
  95. /**
  96. * pcpu_chunk_nr_blocks - converts nr_pages to # of md_blocks
  97. * @chunk: chunk of interest
  98. *
  99. * This conversion is from the number of physical pages that the chunk
  100. * serves to the number of bitmap blocks used.
  101. */
  102. static inline int pcpu_chunk_nr_blocks(struct pcpu_chunk *chunk)
  103. {
  104. return chunk->nr_pages * PAGE_SIZE / PCPU_BITMAP_BLOCK_SIZE;
  105. }
  106. /**
  107. * pcpu_nr_pages_to_map_bits - converts the pages to size of bitmap
  108. * @pages: number of physical pages
  109. *
  110. * This conversion is from physical pages to the number of bits
  111. * required in the bitmap.
  112. */
  113. static inline int pcpu_nr_pages_to_map_bits(int pages)
  114. {
  115. return pages * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE;
  116. }
  117. /**
  118. * pcpu_chunk_map_bits - helper to convert nr_pages to size of bitmap
  119. * @chunk: chunk of interest
  120. *
  121. * This conversion is from the number of physical pages that the chunk
  122. * serves to the number of bits in the bitmap.
  123. */
  124. static inline int pcpu_chunk_map_bits(struct pcpu_chunk *chunk)
  125. {
  126. return pcpu_nr_pages_to_map_bits(chunk->nr_pages);
  127. }
  128. /**
  129. * pcpu_obj_full_size - helper to calculate size of each accounted object
  130. * @size: size of area to allocate in bytes
  131. *
  132. * For each accounted object there is an extra space which is used to store
  133. * obj_cgroup membership if kmemcg is not disabled. Charge it too.
  134. */
  135. static inline size_t pcpu_obj_full_size(size_t size)
  136. {
  137. size_t extra_size = 0;
  138. #ifdef CONFIG_MEMCG
  139. if (!mem_cgroup_kmem_disabled())
  140. extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
  141. #endif
  142. return size * num_possible_cpus() + extra_size;
  143. }
  144. #ifdef CONFIG_PERCPU_STATS
  145. #include <linux/spinlock.h>
  146. struct percpu_stats {
  147. u64 nr_alloc; /* lifetime # of allocations */
  148. u64 nr_dealloc; /* lifetime # of deallocations */
  149. u64 nr_cur_alloc; /* current # of allocations */
  150. u64 nr_max_alloc; /* max # of live allocations */
  151. u32 nr_chunks; /* current # of live chunks */
  152. u32 nr_max_chunks; /* max # of live chunks */
  153. size_t min_alloc_size; /* min allocation size */
  154. size_t max_alloc_size; /* max allocation size */
  155. };
  156. extern struct percpu_stats pcpu_stats;
  157. extern struct pcpu_alloc_info pcpu_stats_ai;
  158. /*
  159. * For debug purposes. We don't care about the flexible array.
  160. */
  161. static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
  162. {
  163. memcpy(&pcpu_stats_ai, ai, sizeof(struct pcpu_alloc_info));
  164. /* initialize min_alloc_size to unit_size */
  165. pcpu_stats.min_alloc_size = pcpu_stats_ai.unit_size;
  166. }
  167. /*
  168. * pcpu_stats_area_alloc - increment area allocation stats
  169. * @chunk: the location of the area being allocated
  170. * @size: size of area to allocate in bytes
  171. *
  172. * CONTEXT:
  173. * pcpu_lock.
  174. */
  175. static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
  176. {
  177. lockdep_assert_held(&pcpu_lock);
  178. pcpu_stats.nr_alloc++;
  179. pcpu_stats.nr_cur_alloc++;
  180. pcpu_stats.nr_max_alloc =
  181. max(pcpu_stats.nr_max_alloc, pcpu_stats.nr_cur_alloc);
  182. pcpu_stats.min_alloc_size =
  183. min(pcpu_stats.min_alloc_size, size);
  184. pcpu_stats.max_alloc_size =
  185. max(pcpu_stats.max_alloc_size, size);
  186. chunk->nr_alloc++;
  187. chunk->max_alloc_size = max(chunk->max_alloc_size, size);
  188. }
  189. /*
  190. * pcpu_stats_area_dealloc - decrement allocation stats
  191. * @chunk: the location of the area being deallocated
  192. *
  193. * CONTEXT:
  194. * pcpu_lock.
  195. */
  196. static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
  197. {
  198. lockdep_assert_held(&pcpu_lock);
  199. pcpu_stats.nr_dealloc++;
  200. pcpu_stats.nr_cur_alloc--;
  201. chunk->nr_alloc--;
  202. }
  203. /*
  204. * pcpu_stats_chunk_alloc - increment chunk stats
  205. */
  206. static inline void pcpu_stats_chunk_alloc(void)
  207. {
  208. unsigned long flags;
  209. spin_lock_irqsave(&pcpu_lock, flags);
  210. pcpu_stats.nr_chunks++;
  211. pcpu_stats.nr_max_chunks =
  212. max(pcpu_stats.nr_max_chunks, pcpu_stats.nr_chunks);
  213. spin_unlock_irqrestore(&pcpu_lock, flags);
  214. }
  215. /*
  216. * pcpu_stats_chunk_dealloc - decrement chunk stats
  217. */
  218. static inline void pcpu_stats_chunk_dealloc(void)
  219. {
  220. unsigned long flags;
  221. spin_lock_irqsave(&pcpu_lock, flags);
  222. pcpu_stats.nr_chunks--;
  223. spin_unlock_irqrestore(&pcpu_lock, flags);
  224. }
  225. #else
  226. static inline void pcpu_stats_save_ai(const struct pcpu_alloc_info *ai)
  227. {
  228. }
  229. static inline void pcpu_stats_area_alloc(struct pcpu_chunk *chunk, size_t size)
  230. {
  231. }
  232. static inline void pcpu_stats_area_dealloc(struct pcpu_chunk *chunk)
  233. {
  234. }
  235. static inline void pcpu_stats_chunk_alloc(void)
  236. {
  237. }
  238. static inline void pcpu_stats_chunk_dealloc(void)
  239. {
  240. }
  241. #endif /* !CONFIG_PERCPU_STATS */
  242. #endif