slub_def.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SLUB_DEF_H
  3. #define _LINUX_SLUB_DEF_H
  4. /*
  5. * SLUB : A Slab allocator without object queues.
  6. *
  7. * (C) 2007 SGI, Christoph Lameter
  8. */
  9. #include <linux/kfence.h>
  10. #include <linux/kobject.h>
  11. #include <linux/reciprocal_div.h>
  12. enum stat_item {
  13. ALLOC_FASTPATH, /* Allocation from cpu slab */
  14. ALLOC_SLOWPATH, /* Allocation by getting a new cpu slab */
  15. FREE_FASTPATH, /* Free to cpu slab */
  16. FREE_SLOWPATH, /* Freeing not to cpu slab */
  17. FREE_FROZEN, /* Freeing to frozen slab */
  18. FREE_ADD_PARTIAL, /* Freeing moves slab to partial list */
  19. FREE_REMOVE_PARTIAL, /* Freeing removes last object */
  20. ALLOC_FROM_PARTIAL, /* Cpu slab acquired from node partial list */
  21. ALLOC_SLAB, /* Cpu slab acquired from page allocator */
  22. ALLOC_REFILL, /* Refill cpu slab from slab freelist */
  23. ALLOC_NODE_MISMATCH, /* Switching cpu slab */
  24. FREE_SLAB, /* Slab freed to the page allocator */
  25. CPUSLAB_FLUSH, /* Abandoning of the cpu slab */
  26. DEACTIVATE_FULL, /* Cpu slab was full when deactivated */
  27. DEACTIVATE_EMPTY, /* Cpu slab was empty when deactivated */
  28. DEACTIVATE_TO_HEAD, /* Cpu slab was moved to the head of partials */
  29. DEACTIVATE_TO_TAIL, /* Cpu slab was moved to the tail of partials */
  30. DEACTIVATE_REMOTE_FREES,/* Slab contained remotely freed objects */
  31. DEACTIVATE_BYPASS, /* Implicit deactivation */
  32. ORDER_FALLBACK, /* Number of times fallback was necessary */
  33. CMPXCHG_DOUBLE_CPU_FAIL,/* Failure of this_cpu_cmpxchg_double */
  34. CMPXCHG_DOUBLE_FAIL, /* Number of times that cmpxchg double did not match */
  35. CPU_PARTIAL_ALLOC, /* Used cpu partial on alloc */
  36. CPU_PARTIAL_FREE, /* Refill cpu partial on free */
  37. CPU_PARTIAL_NODE, /* Refill cpu partial from node partial */
  38. CPU_PARTIAL_DRAIN, /* Drain cpu partial to node partial */
  39. NR_SLUB_STAT_ITEMS };
  40. struct kmem_cache_cpu {
  41. void **freelist; /* Pointer to next available object */
  42. unsigned long tid; /* Globally unique transaction id */
  43. struct page *page; /* The slab from which we are allocating */
  44. #ifdef CONFIG_SLUB_CPU_PARTIAL
  45. struct page *partial; /* Partially allocated frozen slabs */
  46. #endif
  47. #ifdef CONFIG_SLUB_STATS
  48. unsigned stat[NR_SLUB_STAT_ITEMS];
  49. #endif
  50. };
  51. #ifdef CONFIG_SLUB_CPU_PARTIAL
  52. #define slub_percpu_partial(c) ((c)->partial)
  53. #define slub_set_percpu_partial(c, p) \
  54. ({ \
  55. slub_percpu_partial(c) = (p)->next; \
  56. })
  57. #define slub_percpu_partial_read_once(c) READ_ONCE(slub_percpu_partial(c))
  58. #else
  59. #define slub_percpu_partial(c) NULL
  60. #define slub_set_percpu_partial(c, p)
  61. #define slub_percpu_partial_read_once(c) NULL
  62. #endif // CONFIG_SLUB_CPU_PARTIAL
  63. /*
  64. * Word size structure that can be atomically updated or read and that
  65. * contains both the order and the number of objects that a slab of the
  66. * given order would contain.
  67. */
  68. struct kmem_cache_order_objects {
  69. unsigned int x;
  70. };
  71. /*
  72. * Slab cache management.
  73. */
  74. struct kmem_cache {
  75. struct kmem_cache_cpu __percpu *cpu_slab;
  76. /* Used for retrieving partial slabs, etc. */
  77. slab_flags_t flags;
  78. unsigned long min_partial;
  79. unsigned int size; /* The size of an object including metadata */
  80. unsigned int object_size;/* The size of an object without metadata */
  81. struct reciprocal_value reciprocal_size;
  82. unsigned int offset; /* Free pointer offset */
  83. #ifdef CONFIG_SLUB_CPU_PARTIAL
  84. /* Number of per cpu partial objects to keep around */
  85. unsigned int cpu_partial;
  86. #endif
  87. struct kmem_cache_order_objects oo;
  88. /* Allocation and freeing of slabs */
  89. struct kmem_cache_order_objects max;
  90. struct kmem_cache_order_objects min;
  91. gfp_t allocflags; /* gfp flags to use on each alloc */
  92. int refcount; /* Refcount for slab cache destroy */
  93. void (*ctor)(void *);
  94. unsigned int inuse; /* Offset to metadata */
  95. unsigned int align; /* Alignment */
  96. unsigned int red_left_pad; /* Left redzone padding size */
  97. const char *name; /* Name (only for display!) */
  98. struct list_head list; /* List of slab caches */
  99. #ifdef CONFIG_SYSFS
  100. struct kobject kobj; /* For sysfs */
  101. #endif
  102. #ifdef CONFIG_SLAB_FREELIST_HARDENED
  103. unsigned long random;
  104. #endif
  105. #ifdef CONFIG_NUMA
  106. /*
  107. * Defragmentation by allocating from a remote node.
  108. */
  109. unsigned int remote_node_defrag_ratio;
  110. #endif
  111. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  112. unsigned int *random_seq;
  113. #endif
  114. #ifdef CONFIG_KASAN
  115. struct kasan_cache kasan_info;
  116. #endif
  117. unsigned int useroffset; /* Usercopy region offset */
  118. unsigned int usersize; /* Usercopy region size */
  119. struct kmem_cache_node *node[MAX_NUMNODES];
  120. };
  121. #ifdef CONFIG_SLUB_CPU_PARTIAL
  122. #define slub_cpu_partial(s) ((s)->cpu_partial)
  123. #define slub_set_cpu_partial(s, n) \
  124. ({ \
  125. slub_cpu_partial(s) = (n); \
  126. })
  127. #else
  128. #define slub_cpu_partial(s) (0)
  129. #define slub_set_cpu_partial(s, n)
  130. #endif /* CONFIG_SLUB_CPU_PARTIAL */
  131. #ifdef CONFIG_SYSFS
  132. #define SLAB_SUPPORTS_SYSFS
  133. void sysfs_slab_unlink(struct kmem_cache *);
  134. void sysfs_slab_release(struct kmem_cache *);
  135. #else
  136. static inline void sysfs_slab_unlink(struct kmem_cache *s)
  137. {
  138. }
  139. static inline void sysfs_slab_release(struct kmem_cache *s)
  140. {
  141. }
  142. #endif
  143. void object_err(struct kmem_cache *s, struct page *page,
  144. u8 *object, char *reason);
  145. void *fixup_red_left(struct kmem_cache *s, void *p);
  146. static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
  147. void *x) {
  148. void *object = x - (x - page_address(page)) % cache->size;
  149. void *last_object = page_address(page) +
  150. (page->objects - 1) * cache->size;
  151. void *result = (unlikely(object > last_object)) ? last_object : object;
  152. result = fixup_red_left(cache, result);
  153. return result;
  154. }
  155. /* Determine object index from a given position */
  156. static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
  157. void *addr, void *obj)
  158. {
  159. return reciprocal_divide(kasan_reset_tag(obj) - addr,
  160. cache->reciprocal_size);
  161. }
  162. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  163. const struct page *page, void *obj)
  164. {
  165. if (is_kfence_address(obj))
  166. return 0;
  167. return __obj_to_index(cache, page_address(page), obj);
  168. }
  169. static inline int objs_per_slab_page(const struct kmem_cache *cache,
  170. const struct page *page)
  171. {
  172. return page->objects;
  173. }
  174. #endif /* _LINUX_SLUB_DEF_H */