slab.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_SLAB_H
  3. #define _TOOLS_SLAB_H
  4. #include <linux/types.h>
  5. #include <linux/gfp.h>
  6. #include <pthread.h>
  7. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  8. #define kzalloc_node(size, flags, node) kmalloc(size, flags)
  9. enum _slab_flag_bits {
  10. _SLAB_KMALLOC,
  11. _SLAB_HWCACHE_ALIGN,
  12. _SLAB_PANIC,
  13. _SLAB_TYPESAFE_BY_RCU,
  14. _SLAB_ACCOUNT,
  15. _SLAB_FLAGS_LAST_BIT
  16. };
  17. #define __SLAB_FLAG_BIT(nr) ((unsigned int __force)(1U << (nr)))
  18. #define __SLAB_FLAG_UNUSED ((unsigned int __force)(0U))
  19. #define SLAB_HWCACHE_ALIGN __SLAB_FLAG_BIT(_SLAB_HWCACHE_ALIGN)
  20. #define SLAB_PANIC __SLAB_FLAG_BIT(_SLAB_PANIC)
  21. #define SLAB_TYPESAFE_BY_RCU __SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
  22. #ifdef CONFIG_MEMCG
  23. # define SLAB_ACCOUNT __SLAB_FLAG_BIT(_SLAB_ACCOUNT)
  24. #else
  25. # define SLAB_ACCOUNT __SLAB_FLAG_UNUSED
  26. #endif
  27. void *kmalloc(size_t size, gfp_t gfp);
  28. void kfree(void *p);
  29. void *kmalloc_array(size_t n, size_t size, gfp_t gfp);
  30. bool slab_is_available(void);
  31. enum slab_state {
  32. DOWN,
  33. PARTIAL,
  34. UP,
  35. FULL
  36. };
  37. struct kmem_cache {
  38. pthread_mutex_t lock;
  39. unsigned int size;
  40. unsigned int align;
  41. unsigned int sheaf_capacity;
  42. int nr_objs;
  43. void *objs;
  44. void (*ctor)(void *);
  45. bool non_kernel_enabled;
  46. unsigned int non_kernel;
  47. unsigned long nr_allocated;
  48. unsigned long nr_tallocated;
  49. bool exec_callback;
  50. void (*callback)(void *);
  51. void *private;
  52. };
  53. struct kmem_cache_args {
  54. /**
  55. * @align: The required alignment for the objects.
  56. *
  57. * %0 means no specific alignment is requested.
  58. */
  59. unsigned int align;
  60. /**
  61. * @sheaf_capacity: The maximum size of the sheaf.
  62. */
  63. unsigned int sheaf_capacity;
  64. /**
  65. * @useroffset: Usercopy region offset.
  66. *
  67. * %0 is a valid offset, when @usersize is non-%0
  68. */
  69. unsigned int useroffset;
  70. /**
  71. * @usersize: Usercopy region size.
  72. *
  73. * %0 means no usercopy region is specified.
  74. */
  75. unsigned int usersize;
  76. /**
  77. * @freeptr_offset: Custom offset for the free pointer
  78. * in &SLAB_TYPESAFE_BY_RCU caches
  79. *
  80. * By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer
  81. * outside of the object. This might cause the object to grow in size.
  82. * Cache creators that have a reason to avoid this can specify a custom
  83. * free pointer offset in their struct where the free pointer will be
  84. * placed.
  85. *
  86. * Note that placing the free pointer inside the object requires the
  87. * caller to ensure that no fields are invalidated that are required to
  88. * guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for
  89. * details).
  90. *
  91. * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
  92. * is specified, %use_freeptr_offset must be set %true.
  93. *
  94. * Note that @ctor currently isn't supported with custom free pointers
  95. * as a @ctor requires an external free pointer.
  96. */
  97. unsigned int freeptr_offset;
  98. /**
  99. * @use_freeptr_offset: Whether a @freeptr_offset is used.
  100. */
  101. bool use_freeptr_offset;
  102. /**
  103. * @ctor: A constructor for the objects.
  104. *
  105. * The constructor is invoked for each object in a newly allocated slab
  106. * page. It is the cache user's responsibility to free object in the
  107. * same state as after calling the constructor, or deal appropriately
  108. * with any differences between a freshly constructed and a reallocated
  109. * object.
  110. *
  111. * %NULL means no constructor.
  112. */
  113. void (*ctor)(void *);
  114. };
  115. struct slab_sheaf {
  116. union {
  117. struct list_head barn_list;
  118. /* only used for prefilled sheafs */
  119. unsigned int capacity;
  120. };
  121. struct kmem_cache *cache;
  122. unsigned int size;
  123. int node; /* only used for rcu_sheaf */
  124. void *objects[];
  125. };
  126. static inline void *kzalloc(size_t size, gfp_t gfp)
  127. {
  128. return kmalloc(size, gfp | __GFP_ZERO);
  129. }
  130. struct list_lru;
  131. void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *, int flags);
  132. static inline void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
  133. {
  134. return kmem_cache_alloc_lru(cachep, NULL, flags);
  135. }
  136. void kmem_cache_free(struct kmem_cache *cachep, void *objp);
  137. struct kmem_cache *
  138. __kmem_cache_create_args(const char *name, unsigned int size,
  139. struct kmem_cache_args *args, unsigned int flags);
  140. /* If NULL is passed for @args, use this variant with default arguments. */
  141. static inline struct kmem_cache *
  142. __kmem_cache_default_args(const char *name, unsigned int size,
  143. struct kmem_cache_args *args, unsigned int flags)
  144. {
  145. struct kmem_cache_args kmem_default_args = {};
  146. return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
  147. }
  148. static inline struct kmem_cache *
  149. __kmem_cache_create(const char *name, unsigned int size, unsigned int align,
  150. unsigned int flags, void (*ctor)(void *))
  151. {
  152. struct kmem_cache_args kmem_args = {
  153. .align = align,
  154. .ctor = ctor,
  155. };
  156. return __kmem_cache_create_args(name, size, &kmem_args, flags);
  157. }
  158. #define kmem_cache_create(__name, __object_size, __args, ...) \
  159. _Generic((__args), \
  160. struct kmem_cache_args *: __kmem_cache_create_args, \
  161. void *: __kmem_cache_default_args, \
  162. default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__)
  163. void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list);
  164. int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
  165. void **list);
  166. struct slab_sheaf *
  167. kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size);
  168. void *
  169. kmem_cache_alloc_from_sheaf(struct kmem_cache *s, gfp_t gfp,
  170. struct slab_sheaf *sheaf);
  171. void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
  172. struct slab_sheaf *sheaf);
  173. int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp,
  174. struct slab_sheaf **sheafp, unsigned int size);
  175. static inline unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf)
  176. {
  177. return sheaf->size;
  178. }
  179. #define __alloc_objs(KMALLOC, GFP, TYPE, COUNT) \
  180. ({ \
  181. const size_t __obj_size = size_mul(sizeof(TYPE), COUNT); \
  182. (TYPE *)KMALLOC(__obj_size, GFP); \
  183. })
  184. #define kzalloc_obj(P, ...) \
  185. __alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), 1)
  186. #endif /* _TOOLS_SLAB_H */