slab.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef MM_SLAB_H
  3. #define MM_SLAB_H
  4. #include <linux/reciprocal_div.h>
  5. #include <linux/list_lru.h>
  6. #include <linux/local_lock.h>
  7. #include <linux/random.h>
  8. #include <linux/kobject.h>
  9. #include <linux/sched/mm.h>
  10. #include <linux/memcontrol.h>
  11. #include <linux/kfence.h>
  12. #include <linux/kasan.h>
  13. /*
  14. * Internal slab definitions
  15. */
  16. #ifdef CONFIG_64BIT
  17. # ifdef system_has_cmpxchg128
  18. # define system_has_freelist_aba() system_has_cmpxchg128()
  19. # define try_cmpxchg_freelist try_cmpxchg128
  20. # endif
  21. #define this_cpu_try_cmpxchg_freelist this_cpu_try_cmpxchg128
  22. typedef u128 freelist_full_t;
  23. #else /* CONFIG_64BIT */
  24. # ifdef system_has_cmpxchg64
  25. # define system_has_freelist_aba() system_has_cmpxchg64()
  26. # define try_cmpxchg_freelist try_cmpxchg64
  27. # endif
  28. #define this_cpu_try_cmpxchg_freelist this_cpu_try_cmpxchg64
  29. typedef u64 freelist_full_t;
  30. #endif /* CONFIG_64BIT */
  31. #if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  32. #undef system_has_freelist_aba
  33. #endif
  34. /*
  35. * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
  36. * problems with cmpxchg of just a pointer.
  37. */
  38. typedef union {
  39. struct {
  40. void *freelist;
  41. unsigned long counter;
  42. };
  43. freelist_full_t full;
  44. } freelist_aba_t;
  45. /* Reuses the bits in struct page */
  46. struct slab {
  47. unsigned long __page_flags;
  48. struct kmem_cache *slab_cache;
  49. union {
  50. struct {
  51. union {
  52. struct list_head slab_list;
  53. #ifdef CONFIG_SLUB_CPU_PARTIAL
  54. struct {
  55. struct slab *next;
  56. int slabs; /* Nr of slabs left */
  57. };
  58. #endif
  59. };
  60. /* Double-word boundary */
  61. union {
  62. struct {
  63. void *freelist; /* first free object */
  64. union {
  65. unsigned long counters;
  66. struct {
  67. unsigned inuse:16;
  68. unsigned objects:15;
  69. /*
  70. * If slab debugging is enabled then the
  71. * frozen bit can be reused to indicate
  72. * that the slab was corrupted
  73. */
  74. unsigned frozen:1;
  75. };
  76. };
  77. };
  78. #ifdef system_has_freelist_aba
  79. freelist_aba_t freelist_counter;
  80. #endif
  81. };
  82. };
  83. struct rcu_head rcu_head;
  84. };
  85. unsigned int __page_type;
  86. atomic_t __page_refcount;
  87. #ifdef CONFIG_SLAB_OBJ_EXT
  88. unsigned long obj_exts;
  89. #endif
  90. };
  91. #define SLAB_MATCH(pg, sl) \
  92. static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
  93. SLAB_MATCH(flags, __page_flags);
  94. SLAB_MATCH(compound_head, slab_cache); /* Ensure bit 0 is clear */
  95. SLAB_MATCH(_refcount, __page_refcount);
  96. #ifdef CONFIG_MEMCG
  97. SLAB_MATCH(memcg_data, obj_exts);
  98. #elif defined(CONFIG_SLAB_OBJ_EXT)
  99. SLAB_MATCH(_unused_slab_obj_exts, obj_exts);
  100. #endif
  101. #undef SLAB_MATCH
  102. static_assert(sizeof(struct slab) <= sizeof(struct page));
  103. #if defined(system_has_freelist_aba)
  104. static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(freelist_aba_t)));
  105. #endif
  106. /**
  107. * folio_slab - Converts from folio to slab.
  108. * @folio: The folio.
  109. *
  110. * Currently struct slab is a different representation of a folio where
  111. * folio_test_slab() is true.
  112. *
  113. * Return: The slab which contains this folio.
  114. */
  115. #define folio_slab(folio) (_Generic((folio), \
  116. const struct folio *: (const struct slab *)(folio), \
  117. struct folio *: (struct slab *)(folio)))
  118. /**
  119. * slab_folio - The folio allocated for a slab
  120. * @slab: The slab.
  121. *
  122. * Slabs are allocated as folios that contain the individual objects and are
  123. * using some fields in the first struct page of the folio - those fields are
  124. * now accessed by struct slab. It is occasionally necessary to convert back to
  125. * a folio in order to communicate with the rest of the mm. Please use this
  126. * helper function instead of casting yourself, as the implementation may change
  127. * in the future.
  128. */
  129. #define slab_folio(s) (_Generic((s), \
  130. const struct slab *: (const struct folio *)s, \
  131. struct slab *: (struct folio *)s))
  132. /**
  133. * page_slab - Converts from first struct page to slab.
  134. * @p: The first (either head of compound or single) page of slab.
  135. *
  136. * A temporary wrapper to convert struct page to struct slab in situations where
  137. * we know the page is the compound head, or single order-0 page.
  138. *
  139. * Long-term ideally everything would work with struct slab directly or go
  140. * through folio to struct slab.
  141. *
  142. * Return: The slab which contains this page
  143. */
  144. #define page_slab(p) (_Generic((p), \
  145. const struct page *: (const struct slab *)(p), \
  146. struct page *: (struct slab *)(p)))
  147. /**
  148. * slab_page - The first struct page allocated for a slab
  149. * @slab: The slab.
  150. *
  151. * A convenience wrapper for converting slab to the first struct page of the
  152. * underlying folio, to communicate with code not yet converted to folio or
  153. * struct slab.
  154. */
  155. #define slab_page(s) folio_page(slab_folio(s), 0)
  156. /*
  157. * If network-based swap is enabled, sl*b must keep track of whether pages
  158. * were allocated from pfmemalloc reserves.
  159. */
  160. static inline bool slab_test_pfmemalloc(const struct slab *slab)
  161. {
  162. return folio_test_active(slab_folio(slab));
  163. }
  164. static inline void slab_set_pfmemalloc(struct slab *slab)
  165. {
  166. folio_set_active(slab_folio(slab));
  167. }
  168. static inline void slab_clear_pfmemalloc(struct slab *slab)
  169. {
  170. folio_clear_active(slab_folio(slab));
  171. }
  172. static inline void __slab_clear_pfmemalloc(struct slab *slab)
  173. {
  174. __folio_clear_active(slab_folio(slab));
  175. }
  176. static inline void *slab_address(const struct slab *slab)
  177. {
  178. return folio_address(slab_folio(slab));
  179. }
  180. static inline int slab_nid(const struct slab *slab)
  181. {
  182. return folio_nid(slab_folio(slab));
  183. }
  184. static inline pg_data_t *slab_pgdat(const struct slab *slab)
  185. {
  186. return folio_pgdat(slab_folio(slab));
  187. }
  188. static inline struct slab *virt_to_slab(const void *addr)
  189. {
  190. struct folio *folio = virt_to_folio(addr);
  191. if (!folio_test_slab(folio))
  192. return NULL;
  193. return folio_slab(folio);
  194. }
  195. static inline int slab_order(const struct slab *slab)
  196. {
  197. return folio_order(slab_folio(slab));
  198. }
  199. static inline size_t slab_size(const struct slab *slab)
  200. {
  201. return PAGE_SIZE << slab_order(slab);
  202. }
  203. #ifdef CONFIG_SLUB_CPU_PARTIAL
  204. #define slub_percpu_partial(c) ((c)->partial)
  205. #define slub_set_percpu_partial(c, p) \
  206. ({ \
  207. slub_percpu_partial(c) = (p)->next; \
  208. })
  209. #define slub_percpu_partial_read_once(c) READ_ONCE(slub_percpu_partial(c))
  210. #else
  211. #define slub_percpu_partial(c) NULL
  212. #define slub_set_percpu_partial(c, p)
  213. #define slub_percpu_partial_read_once(c) NULL
  214. #endif // CONFIG_SLUB_CPU_PARTIAL
  215. /*
  216. * Word size structure that can be atomically updated or read and that
  217. * contains both the order and the number of objects that a slab of the
  218. * given order would contain.
  219. */
  220. struct kmem_cache_order_objects {
  221. unsigned int x;
  222. };
  223. /*
  224. * Slab cache management.
  225. */
  226. struct kmem_cache {
  227. #ifndef CONFIG_SLUB_TINY
  228. struct kmem_cache_cpu __percpu *cpu_slab;
  229. #endif
  230. /* Used for retrieving partial slabs, etc. */
  231. slab_flags_t flags;
  232. unsigned long min_partial;
  233. unsigned int size; /* Object size including metadata */
  234. unsigned int object_size; /* Object size without metadata */
  235. struct reciprocal_value reciprocal_size;
  236. unsigned int offset; /* Free pointer offset */
  237. #ifdef CONFIG_SLUB_CPU_PARTIAL
  238. /* Number of per cpu partial objects to keep around */
  239. unsigned int cpu_partial;
  240. /* Number of per cpu partial slabs to keep around */
  241. unsigned int cpu_partial_slabs;
  242. #endif
  243. struct kmem_cache_order_objects oo;
  244. /* Allocation and freeing of slabs */
  245. struct kmem_cache_order_objects min;
  246. gfp_t allocflags; /* gfp flags to use on each alloc */
  247. int refcount; /* Refcount for slab cache destroy */
  248. void (*ctor)(void *object); /* Object constructor */
  249. unsigned int inuse; /* Offset to metadata */
  250. unsigned int align; /* Alignment */
  251. unsigned int red_left_pad; /* Left redzone padding size */
  252. const char *name; /* Name (only for display!) */
  253. struct list_head list; /* List of slab caches */
  254. #ifdef CONFIG_SYSFS
  255. struct kobject kobj; /* For sysfs */
  256. #endif
  257. #ifdef CONFIG_SLAB_FREELIST_HARDENED
  258. unsigned long random;
  259. #endif
  260. #ifdef CONFIG_NUMA
  261. /*
  262. * Defragmentation by allocating from a remote node.
  263. */
  264. unsigned int remote_node_defrag_ratio;
  265. #endif
  266. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  267. unsigned int *random_seq;
  268. #endif
  269. #ifdef CONFIG_KASAN_GENERIC
  270. struct kasan_cache kasan_info;
  271. #endif
  272. #ifdef CONFIG_HARDENED_USERCOPY
  273. unsigned int useroffset; /* Usercopy region offset */
  274. unsigned int usersize; /* Usercopy region size */
  275. #endif
  276. struct kmem_cache_node *node[MAX_NUMNODES];
  277. };
  278. #if defined(CONFIG_SYSFS) && !defined(CONFIG_SLUB_TINY)
  279. #define SLAB_SUPPORTS_SYSFS 1
  280. void sysfs_slab_unlink(struct kmem_cache *s);
  281. void sysfs_slab_release(struct kmem_cache *s);
  282. #else
  283. static inline void sysfs_slab_unlink(struct kmem_cache *s) { }
  284. static inline void sysfs_slab_release(struct kmem_cache *s) { }
  285. #endif
  286. void *fixup_red_left(struct kmem_cache *s, void *p);
  287. static inline void *nearest_obj(struct kmem_cache *cache,
  288. const struct slab *slab, void *x)
  289. {
  290. void *object = x - (x - slab_address(slab)) % cache->size;
  291. void *last_object = slab_address(slab) +
  292. (slab->objects - 1) * cache->size;
  293. void *result = (unlikely(object > last_object)) ? last_object : object;
  294. result = fixup_red_left(cache, result);
  295. return result;
  296. }
  297. /* Determine object index from a given position */
  298. static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
  299. void *addr, void *obj)
  300. {
  301. return reciprocal_divide(kasan_reset_tag(obj) - addr,
  302. cache->reciprocal_size);
  303. }
  304. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  305. const struct slab *slab, void *obj)
  306. {
  307. if (is_kfence_address(obj))
  308. return 0;
  309. return __obj_to_index(cache, slab_address(slab), obj);
  310. }
  311. static inline int objs_per_slab(const struct kmem_cache *cache,
  312. const struct slab *slab)
  313. {
  314. return slab->objects;
  315. }
  316. /*
  317. * State of the slab allocator.
  318. *
  319. * This is used to describe the states of the allocator during bootup.
  320. * Allocators use this to gradually bootstrap themselves. Most allocators
  321. * have the problem that the structures used for managing slab caches are
  322. * allocated from slab caches themselves.
  323. */
  324. enum slab_state {
  325. DOWN, /* No slab functionality yet */
  326. PARTIAL, /* SLUB: kmem_cache_node available */
  327. UP, /* Slab caches usable but not all extras yet */
  328. FULL /* Everything is working */
  329. };
  330. extern enum slab_state slab_state;
  331. /* The slab cache mutex protects the management structures during changes */
  332. extern struct mutex slab_mutex;
  333. /* The list of all slab caches on the system */
  334. extern struct list_head slab_caches;
  335. /* The slab cache that manages slab cache information */
  336. extern struct kmem_cache *kmem_cache;
  337. /* A table of kmalloc cache names and sizes */
  338. extern const struct kmalloc_info_struct {
  339. const char *name[NR_KMALLOC_TYPES];
  340. unsigned int size;
  341. } kmalloc_info[];
  342. /* Kmalloc array related functions */
  343. void setup_kmalloc_cache_index_table(void);
  344. void create_kmalloc_caches(void);
  345. extern u8 kmalloc_size_index[24];
  346. static inline unsigned int size_index_elem(unsigned int bytes)
  347. {
  348. return (bytes - 1) / 8;
  349. }
  350. /*
  351. * Find the kmem_cache structure that serves a given size of
  352. * allocation
  353. *
  354. * This assumes size is larger than zero and not larger than
  355. * KMALLOC_MAX_CACHE_SIZE and the caller must check that.
  356. */
  357. static inline struct kmem_cache *
  358. kmalloc_slab(size_t size, kmem_buckets *b, gfp_t flags, unsigned long caller)
  359. {
  360. unsigned int index;
  361. if (!b)
  362. b = &kmalloc_caches[kmalloc_type(flags, caller)];
  363. if (size <= 192)
  364. index = kmalloc_size_index[size_index_elem(size)];
  365. else
  366. index = fls(size - 1);
  367. return (*b)[index];
  368. }
  369. gfp_t kmalloc_fix_flags(gfp_t flags);
  370. /* Functions provided by the slab allocators */
  371. int do_kmem_cache_create(struct kmem_cache *s, const char *name,
  372. unsigned int size, struct kmem_cache_args *args,
  373. slab_flags_t flags);
  374. void __init kmem_cache_init(void);
  375. extern void create_boot_cache(struct kmem_cache *, const char *name,
  376. unsigned int size, slab_flags_t flags,
  377. unsigned int useroffset, unsigned int usersize);
  378. int slab_unmergeable(struct kmem_cache *s);
  379. struct kmem_cache *find_mergeable(unsigned size, unsigned align,
  380. slab_flags_t flags, const char *name, void (*ctor)(void *));
  381. struct kmem_cache *
  382. __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
  383. slab_flags_t flags, void (*ctor)(void *));
  384. slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name);
  385. static inline bool is_kmalloc_cache(struct kmem_cache *s)
  386. {
  387. return (s->flags & SLAB_KMALLOC);
  388. }
  389. static inline bool is_kmalloc_normal(struct kmem_cache *s)
  390. {
  391. if (!is_kmalloc_cache(s))
  392. return false;
  393. return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
  394. }
  395. /* Legal flag mask for kmem_cache_create(), for various configurations */
  396. #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
  397. SLAB_CACHE_DMA32 | SLAB_PANIC | \
  398. SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
  399. #ifdef CONFIG_SLUB_DEBUG
  400. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  401. SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
  402. #else
  403. #define SLAB_DEBUG_FLAGS (0)
  404. #endif
  405. #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
  406. SLAB_TEMPORARY | SLAB_ACCOUNT | \
  407. SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
  408. /* Common flags available with current configuration */
  409. #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
  410. /* Common flags permitted for kmem_cache_create */
  411. #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
  412. SLAB_RED_ZONE | \
  413. SLAB_POISON | \
  414. SLAB_STORE_USER | \
  415. SLAB_TRACE | \
  416. SLAB_CONSISTENCY_CHECKS | \
  417. SLAB_NOLEAKTRACE | \
  418. SLAB_RECLAIM_ACCOUNT | \
  419. SLAB_TEMPORARY | \
  420. SLAB_ACCOUNT | \
  421. SLAB_KMALLOC | \
  422. SLAB_NO_MERGE | \
  423. SLAB_NO_USER_FLAGS)
  424. bool __kmem_cache_empty(struct kmem_cache *);
  425. int __kmem_cache_shutdown(struct kmem_cache *);
  426. void __kmem_cache_release(struct kmem_cache *);
  427. int __kmem_cache_shrink(struct kmem_cache *);
  428. void slab_kmem_cache_release(struct kmem_cache *);
  429. struct seq_file;
  430. struct file;
  431. struct slabinfo {
  432. unsigned long active_objs;
  433. unsigned long num_objs;
  434. unsigned long active_slabs;
  435. unsigned long num_slabs;
  436. unsigned long shared_avail;
  437. unsigned int limit;
  438. unsigned int batchcount;
  439. unsigned int shared;
  440. unsigned int objects_per_slab;
  441. unsigned int cache_order;
  442. };
  443. void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
  444. #ifdef CONFIG_SLUB_DEBUG
  445. #ifdef CONFIG_SLUB_DEBUG_ON
  446. DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
  447. #else
  448. DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
  449. #endif
  450. extern void print_tracking(struct kmem_cache *s, void *object);
  451. long validate_slab_cache(struct kmem_cache *s);
  452. static inline bool __slub_debug_enabled(void)
  453. {
  454. return static_branch_unlikely(&slub_debug_enabled);
  455. }
  456. #else
  457. static inline void print_tracking(struct kmem_cache *s, void *object)
  458. {
  459. }
  460. static inline bool __slub_debug_enabled(void)
  461. {
  462. return false;
  463. }
  464. #endif
  465. /*
  466. * Returns true if any of the specified slab_debug flags is enabled for the
  467. * cache. Use only for flags parsed by setup_slub_debug() as it also enables
  468. * the static key.
  469. */
  470. static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
  471. {
  472. if (IS_ENABLED(CONFIG_SLUB_DEBUG))
  473. VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
  474. if (__slub_debug_enabled())
  475. return s->flags & flags;
  476. return false;
  477. }
  478. #if IS_ENABLED(CONFIG_SLUB_DEBUG) && IS_ENABLED(CONFIG_KUNIT)
  479. bool slab_in_kunit_test(void);
  480. #else
  481. static inline bool slab_in_kunit_test(void) { return false; }
  482. #endif
  483. #ifdef CONFIG_SLAB_OBJ_EXT
  484. /*
  485. * slab_obj_exts - get the pointer to the slab object extension vector
  486. * associated with a slab.
  487. * @slab: a pointer to the slab struct
  488. *
  489. * Returns a pointer to the object extension vector associated with the slab,
  490. * or NULL if no such vector has been associated yet.
  491. */
  492. static inline struct slabobj_ext *slab_obj_exts(struct slab *slab)
  493. {
  494. unsigned long obj_exts = READ_ONCE(slab->obj_exts);
  495. #ifdef CONFIG_MEMCG
  496. VM_BUG_ON_PAGE(obj_exts && !(obj_exts & MEMCG_DATA_OBJEXTS),
  497. slab_page(slab));
  498. VM_BUG_ON_PAGE(obj_exts & MEMCG_DATA_KMEM, slab_page(slab));
  499. #endif
  500. return (struct slabobj_ext *)(obj_exts & ~OBJEXTS_FLAGS_MASK);
  501. }
  502. int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
  503. gfp_t gfp, bool new_slab);
  504. #else /* CONFIG_SLAB_OBJ_EXT */
  505. static inline struct slabobj_ext *slab_obj_exts(struct slab *slab)
  506. {
  507. return NULL;
  508. }
  509. #endif /* CONFIG_SLAB_OBJ_EXT */
  510. static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
  511. {
  512. return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  513. NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
  514. }
  515. #ifdef CONFIG_MEMCG
  516. bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,
  517. gfp_t flags, size_t size, void **p);
  518. void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
  519. void **p, int objects, struct slabobj_ext *obj_exts);
  520. #endif
  521. size_t __ksize(const void *objp);
  522. static inline size_t slab_ksize(const struct kmem_cache *s)
  523. {
  524. #ifdef CONFIG_SLUB_DEBUG
  525. /*
  526. * Debugging requires use of the padding between object
  527. * and whatever may come after it.
  528. */
  529. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  530. return s->object_size;
  531. #endif
  532. if (s->flags & SLAB_KASAN)
  533. return s->object_size;
  534. /*
  535. * If we have the need to store the freelist pointer
  536. * back there or track user information then we can
  537. * only use the space before that information.
  538. */
  539. if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
  540. return s->inuse;
  541. /*
  542. * Else we can use all the padding etc for the allocation
  543. */
  544. return s->size;
  545. }
  546. #ifdef CONFIG_SLUB_DEBUG
  547. void dump_unreclaimable_slab(void);
  548. #else
  549. static inline void dump_unreclaimable_slab(void)
  550. {
  551. }
  552. #endif
  553. void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
  554. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  555. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  556. gfp_t gfp);
  557. void cache_random_seq_destroy(struct kmem_cache *cachep);
  558. #else
  559. static inline int cache_random_seq_create(struct kmem_cache *cachep,
  560. unsigned int count, gfp_t gfp)
  561. {
  562. return 0;
  563. }
  564. static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
  565. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  566. static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
  567. {
  568. if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
  569. &init_on_alloc)) {
  570. if (c->ctor)
  571. return false;
  572. if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
  573. return flags & __GFP_ZERO;
  574. return true;
  575. }
  576. return flags & __GFP_ZERO;
  577. }
  578. static inline bool slab_want_init_on_free(struct kmem_cache *c)
  579. {
  580. if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
  581. &init_on_free))
  582. return !(c->ctor ||
  583. (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
  584. return false;
  585. }
  586. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
  587. void debugfs_slab_release(struct kmem_cache *);
  588. #else
  589. static inline void debugfs_slab_release(struct kmem_cache *s) { }
  590. #endif
  591. #ifdef CONFIG_PRINTK
  592. #define KS_ADDRS_COUNT 16
  593. struct kmem_obj_info {
  594. void *kp_ptr;
  595. struct slab *kp_slab;
  596. void *kp_objp;
  597. unsigned long kp_data_offset;
  598. struct kmem_cache *kp_slab_cache;
  599. void *kp_ret;
  600. void *kp_stack[KS_ADDRS_COUNT];
  601. void *kp_free_stack[KS_ADDRS_COUNT];
  602. };
  603. void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
  604. #endif
  605. void __check_heap_object(const void *ptr, unsigned long n,
  606. const struct slab *slab, bool to_user);
  607. #ifdef CONFIG_SLUB_DEBUG
  608. void skip_orig_size_check(struct kmem_cache *s, const void *object);
  609. #endif
  610. #endif /* MM_SLAB_H */