slab.h 20 KB

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