slab.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef MM_SLAB_H
  3. #define MM_SLAB_H
  4. /*
  5. * Internal slab definitions
  6. */
  7. /* Reuses the bits in struct page */
  8. struct slab {
  9. unsigned long __page_flags;
  10. #if defined(CONFIG_SLAB)
  11. union {
  12. struct list_head slab_list;
  13. struct rcu_head rcu_head;
  14. };
  15. struct kmem_cache *slab_cache;
  16. void *freelist; /* array of free object indexes */
  17. void *s_mem; /* first object */
  18. unsigned int active;
  19. #elif defined(CONFIG_SLUB)
  20. union {
  21. struct list_head slab_list;
  22. struct rcu_head rcu_head;
  23. #ifdef CONFIG_SLUB_CPU_PARTIAL
  24. struct {
  25. struct slab *next;
  26. int slabs; /* Nr of slabs left */
  27. };
  28. #endif
  29. };
  30. struct kmem_cache *slab_cache;
  31. /* Double-word boundary */
  32. void *freelist; /* first free object */
  33. union {
  34. unsigned long counters;
  35. struct {
  36. unsigned inuse:16;
  37. unsigned objects:15;
  38. unsigned frozen:1;
  39. };
  40. };
  41. unsigned int __unused;
  42. #elif defined(CONFIG_SLOB)
  43. struct list_head slab_list;
  44. void *__unused_1;
  45. void *freelist; /* first free block */
  46. long units;
  47. unsigned int __unused_2;
  48. #else
  49. #error "Unexpected slab allocator configured"
  50. #endif
  51. atomic_t __page_refcount;
  52. #ifdef CONFIG_MEMCG
  53. unsigned long memcg_data;
  54. #endif
  55. };
  56. #define SLAB_MATCH(pg, sl) \
  57. static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl))
  58. SLAB_MATCH(flags, __page_flags);
  59. SLAB_MATCH(compound_head, slab_list); /* Ensure bit 0 is clear */
  60. #ifndef CONFIG_SLOB
  61. SLAB_MATCH(rcu_head, rcu_head);
  62. #endif
  63. SLAB_MATCH(_refcount, __page_refcount);
  64. #ifdef CONFIG_MEMCG
  65. SLAB_MATCH(memcg_data, memcg_data);
  66. #endif
  67. #undef SLAB_MATCH
  68. static_assert(sizeof(struct slab) <= sizeof(struct page));
  69. /**
  70. * folio_slab - Converts from folio to slab.
  71. * @folio: The folio.
  72. *
  73. * Currently struct slab is a different representation of a folio where
  74. * folio_test_slab() is true.
  75. *
  76. * Return: The slab which contains this folio.
  77. */
  78. #define folio_slab(folio) (_Generic((folio), \
  79. const struct folio *: (const struct slab *)(folio), \
  80. struct folio *: (struct slab *)(folio)))
  81. /**
  82. * slab_folio - The folio allocated for a slab
  83. * @slab: The slab.
  84. *
  85. * Slabs are allocated as folios that contain the individual objects and are
  86. * using some fields in the first struct page of the folio - those fields are
  87. * now accessed by struct slab. It is occasionally necessary to convert back to
  88. * a folio in order to communicate with the rest of the mm. Please use this
  89. * helper function instead of casting yourself, as the implementation may change
  90. * in the future.
  91. */
  92. #define slab_folio(s) (_Generic((s), \
  93. const struct slab *: (const struct folio *)s, \
  94. struct slab *: (struct folio *)s))
  95. /**
  96. * page_slab - Converts from first struct page to slab.
  97. * @p: The first (either head of compound or single) page of slab.
  98. *
  99. * A temporary wrapper to convert struct page to struct slab in situations where
  100. * we know the page is the compound head, or single order-0 page.
  101. *
  102. * Long-term ideally everything would work with struct slab directly or go
  103. * through folio to struct slab.
  104. *
  105. * Return: The slab which contains this page
  106. */
  107. #define page_slab(p) (_Generic((p), \
  108. const struct page *: (const struct slab *)(p), \
  109. struct page *: (struct slab *)(p)))
  110. /**
  111. * slab_page - The first struct page allocated for a slab
  112. * @slab: The slab.
  113. *
  114. * A convenience wrapper for converting slab to the first struct page of the
  115. * underlying folio, to communicate with code not yet converted to folio or
  116. * struct slab.
  117. */
  118. #define slab_page(s) folio_page(slab_folio(s), 0)
  119. /*
  120. * If network-based swap is enabled, sl*b must keep track of whether pages
  121. * were allocated from pfmemalloc reserves.
  122. */
  123. static inline bool slab_test_pfmemalloc(const struct slab *slab)
  124. {
  125. return folio_test_active((struct folio *)slab_folio(slab));
  126. }
  127. static inline void slab_set_pfmemalloc(struct slab *slab)
  128. {
  129. folio_set_active(slab_folio(slab));
  130. }
  131. static inline void slab_clear_pfmemalloc(struct slab *slab)
  132. {
  133. folio_clear_active(slab_folio(slab));
  134. }
  135. static inline void __slab_clear_pfmemalloc(struct slab *slab)
  136. {
  137. __folio_clear_active(slab_folio(slab));
  138. }
  139. static inline void *slab_address(const struct slab *slab)
  140. {
  141. return folio_address(slab_folio(slab));
  142. }
  143. static inline int slab_nid(const struct slab *slab)
  144. {
  145. return folio_nid(slab_folio(slab));
  146. }
  147. static inline pg_data_t *slab_pgdat(const struct slab *slab)
  148. {
  149. return folio_pgdat(slab_folio(slab));
  150. }
  151. static inline struct slab *virt_to_slab(const void *addr)
  152. {
  153. struct folio *folio = virt_to_folio(addr);
  154. if (!folio_test_slab(folio))
  155. return NULL;
  156. return folio_slab(folio);
  157. }
  158. static inline int slab_order(const struct slab *slab)
  159. {
  160. return folio_order((struct folio *)slab_folio(slab));
  161. }
  162. static inline size_t slab_size(const struct slab *slab)
  163. {
  164. return PAGE_SIZE << slab_order(slab);
  165. }
  166. #ifdef CONFIG_SLOB
  167. /*
  168. * Common fields provided in kmem_cache by all slab allocators
  169. * This struct is either used directly by the allocator (SLOB)
  170. * or the allocator must include definitions for all fields
  171. * provided in kmem_cache_common in their definition of kmem_cache.
  172. *
  173. * Once we can do anonymous structs (C11 standard) we could put a
  174. * anonymous struct definition in these allocators so that the
  175. * separate allocations in the kmem_cache structure of SLAB and
  176. * SLUB is no longer needed.
  177. */
  178. struct kmem_cache {
  179. unsigned int object_size;/* The original size of the object */
  180. unsigned int size; /* The aligned/padded/added on size */
  181. unsigned int align; /* Alignment as calculated */
  182. slab_flags_t flags; /* Active flags on the slab */
  183. unsigned int useroffset;/* Usercopy region offset */
  184. unsigned int usersize; /* Usercopy region size */
  185. const char *name; /* Slab name for sysfs */
  186. int refcount; /* Use counter */
  187. void (*ctor)(void *); /* Called on object slot creation */
  188. struct list_head list; /* List of all slab caches on the system */
  189. };
  190. #endif /* CONFIG_SLOB */
  191. #ifdef CONFIG_SLAB
  192. #include <linux/slab_def.h>
  193. #endif
  194. #ifdef CONFIG_SLUB
  195. #include <linux/slub_def.h>
  196. #endif
  197. #include <linux/memcontrol.h>
  198. #include <linux/fault-inject.h>
  199. #include <linux/kasan.h>
  200. #include <linux/kmemleak.h>
  201. #include <linux/random.h>
  202. #include <linux/sched/mm.h>
  203. #include <linux/list_lru.h>
  204. #ifdef CONFIG_KDP
  205. #include <linux/kdp.h>
  206. #endif
  207. /*
  208. * State of the slab allocator.
  209. *
  210. * This is used to describe the states of the allocator during bootup.
  211. * Allocators use this to gradually bootstrap themselves. Most allocators
  212. * have the problem that the structures used for managing slab caches are
  213. * allocated from slab caches themselves.
  214. */
  215. enum slab_state {
  216. DOWN, /* No slab functionality yet */
  217. PARTIAL, /* SLUB: kmem_cache_node available */
  218. PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
  219. UP, /* Slab caches usable but not all extras yet */
  220. FULL /* Everything is working */
  221. };
  222. extern enum slab_state slab_state;
  223. /* The slab cache mutex protects the management structures during changes */
  224. extern struct mutex slab_mutex;
  225. /* The list of all slab caches on the system */
  226. extern struct list_head slab_caches;
  227. /* The slab cache that manages slab cache information */
  228. extern struct kmem_cache *kmem_cache;
  229. /* A table of kmalloc cache names and sizes */
  230. extern const struct kmalloc_info_struct {
  231. const char *name[NR_KMALLOC_TYPES];
  232. unsigned int size;
  233. } kmalloc_info[];
  234. #ifndef CONFIG_SLOB
  235. /* Kmalloc array related functions */
  236. void setup_kmalloc_cache_index_table(void);
  237. void create_kmalloc_caches(slab_flags_t);
  238. /* Find the kmalloc slab corresponding for a certain size */
  239. struct kmem_cache *kmalloc_slab(size_t, gfp_t);
  240. void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
  241. int node, size_t orig_size,
  242. unsigned long caller);
  243. void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller);
  244. #endif
  245. gfp_t kmalloc_fix_flags(gfp_t flags);
  246. #ifdef CONFIG_SLUB
  247. /*
  248. * Tracking user of a slab.
  249. */
  250. #define TRACK_ADDRS_COUNT 16
  251. struct track {
  252. unsigned long addr; /* Called from address */
  253. #ifdef CONFIG_STACKDEPOT
  254. depot_stack_handle_t handle;
  255. #endif
  256. int cpu; /* Was running on cpu */
  257. int pid; /* Pid context */
  258. unsigned long when; /* When did the operation occur */
  259. };
  260. enum track_item { TRACK_ALLOC, TRACK_FREE };
  261. #endif
  262. /* Functions provided by the slab allocators */
  263. int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
  264. struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
  265. slab_flags_t flags, unsigned int useroffset,
  266. unsigned int usersize);
  267. extern void create_boot_cache(struct kmem_cache *, const char *name,
  268. unsigned int size, slab_flags_t flags,
  269. unsigned int useroffset, unsigned int usersize);
  270. int slab_unmergeable(struct kmem_cache *s);
  271. struct kmem_cache *find_mergeable(unsigned size, unsigned align,
  272. slab_flags_t flags, const char *name, void (*ctor)(void *));
  273. #ifndef CONFIG_SLOB
  274. struct kmem_cache *
  275. __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
  276. slab_flags_t flags, void (*ctor)(void *));
  277. slab_flags_t kmem_cache_flags(unsigned int object_size,
  278. slab_flags_t flags, const char *name);
  279. #else
  280. static inline struct kmem_cache *
  281. __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
  282. slab_flags_t flags, void (*ctor)(void *))
  283. { return NULL; }
  284. static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
  285. slab_flags_t flags, const char *name)
  286. {
  287. return flags;
  288. }
  289. #endif
  290. /* Legal flag mask for kmem_cache_create(), for various configurations */
  291. #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
  292. SLAB_CACHE_DMA32 | SLAB_PANIC | \
  293. SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
  294. #if defined(CONFIG_DEBUG_SLAB)
  295. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  296. #elif defined(CONFIG_SLUB_DEBUG)
  297. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  298. SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
  299. #else
  300. #define SLAB_DEBUG_FLAGS (0)
  301. #endif
  302. #if defined(CONFIG_SLAB)
  303. #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
  304. SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
  305. SLAB_ACCOUNT)
  306. #elif defined(CONFIG_SLUB)
  307. #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
  308. SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_USER_FLAGS)
  309. #else
  310. #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
  311. #endif
  312. /* Common flags available with current configuration */
  313. #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
  314. /* Common flags permitted for kmem_cache_create */
  315. #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
  316. SLAB_RED_ZONE | \
  317. SLAB_POISON | \
  318. SLAB_STORE_USER | \
  319. SLAB_TRACE | \
  320. SLAB_CONSISTENCY_CHECKS | \
  321. SLAB_MEM_SPREAD | \
  322. SLAB_NOLEAKTRACE | \
  323. SLAB_RECLAIM_ACCOUNT | \
  324. SLAB_TEMPORARY | \
  325. SLAB_ACCOUNT | \
  326. SLAB_NO_USER_FLAGS)
  327. bool __kmem_cache_empty(struct kmem_cache *);
  328. int __kmem_cache_shutdown(struct kmem_cache *);
  329. void __kmem_cache_release(struct kmem_cache *);
  330. int __kmem_cache_shrink(struct kmem_cache *);
  331. void slab_kmem_cache_release(struct kmem_cache *);
  332. struct seq_file;
  333. struct file;
  334. struct slabinfo {
  335. unsigned long active_objs;
  336. unsigned long num_objs;
  337. unsigned long active_slabs;
  338. unsigned long num_slabs;
  339. unsigned long shared_avail;
  340. unsigned int limit;
  341. unsigned int batchcount;
  342. unsigned int shared;
  343. unsigned int objects_per_slab;
  344. unsigned int cache_order;
  345. };
  346. void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
  347. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
  348. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  349. size_t count, loff_t *ppos);
  350. static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s)
  351. {
  352. return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  353. NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
  354. }
  355. #ifdef CONFIG_SLUB_DEBUG
  356. #ifdef CONFIG_SLUB_DEBUG_ON
  357. DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
  358. #else
  359. DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
  360. #endif
  361. extern void print_tracking(struct kmem_cache *s, void *object);
  362. long validate_slab_cache(struct kmem_cache *s);
  363. extern unsigned long get_each_object_track(struct kmem_cache *s,
  364. struct page *page, enum track_item alloc,
  365. int (*fn)(const struct kmem_cache *, const void *,
  366. const struct track *, void *), void *private);
  367. static inline bool __slub_debug_enabled(void)
  368. {
  369. return static_branch_unlikely(&slub_debug_enabled);
  370. }
  371. #else
  372. static inline void print_tracking(struct kmem_cache *s, void *object)
  373. {
  374. }
  375. #ifdef CONFIG_SLUB
  376. static inline unsigned long get_each_object_track(struct kmem_cache *s,
  377. struct page *page, enum track_item alloc,
  378. int (*fn)(const struct kmem_cache *, const void *,
  379. const struct track *, void *), void *private)
  380. {
  381. return 0;
  382. }
  383. #endif
  384. static inline bool __slub_debug_enabled(void)
  385. {
  386. return false;
  387. }
  388. #endif
  389. /*
  390. * Returns true if any of the specified slub_debug flags is enabled for the
  391. * cache. Use only for flags parsed by setup_slub_debug() as it also enables
  392. * the static key.
  393. */
  394. static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
  395. {
  396. if (IS_ENABLED(CONFIG_SLUB_DEBUG))
  397. VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
  398. if (__slub_debug_enabled())
  399. return s->flags & flags;
  400. return false;
  401. }
  402. #ifdef CONFIG_MEMCG_KMEM
  403. /*
  404. * slab_objcgs - get the object cgroups vector associated with a slab
  405. * @slab: a pointer to the slab struct
  406. *
  407. * Returns a pointer to the object cgroups vector associated with the slab,
  408. * or NULL if no such vector has been associated yet.
  409. */
  410. static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
  411. {
  412. unsigned long memcg_data = READ_ONCE(slab->memcg_data);
  413. VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS),
  414. slab_page(slab));
  415. VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab));
  416. return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
  417. }
  418. int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
  419. gfp_t gfp, bool new_slab);
  420. void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
  421. enum node_stat_item idx, int nr);
  422. static inline void memcg_free_slab_cgroups(struct slab *slab)
  423. {
  424. kfree(slab_objcgs(slab));
  425. slab->memcg_data = 0;
  426. }
  427. static inline size_t obj_full_size(struct kmem_cache *s)
  428. {
  429. /*
  430. * For each accounted object there is an extra space which is used
  431. * to store obj_cgroup membership. Charge it too.
  432. */
  433. return s->size + sizeof(struct obj_cgroup *);
  434. }
  435. /*
  436. * Returns false if the allocation should fail.
  437. */
  438. static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
  439. struct list_lru *lru,
  440. struct obj_cgroup **objcgp,
  441. size_t objects, gfp_t flags)
  442. {
  443. struct obj_cgroup *objcg;
  444. if (!memcg_kmem_enabled())
  445. return true;
  446. if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
  447. return true;
  448. objcg = get_obj_cgroup_from_current();
  449. if (!objcg)
  450. return true;
  451. if (lru) {
  452. int ret;
  453. struct mem_cgroup *memcg;
  454. memcg = get_mem_cgroup_from_objcg(objcg);
  455. ret = memcg_list_lru_alloc(memcg, lru, flags);
  456. css_put(&memcg->css);
  457. if (ret)
  458. goto out;
  459. }
  460. if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s)))
  461. goto out;
  462. *objcgp = objcg;
  463. return true;
  464. out:
  465. obj_cgroup_put(objcg);
  466. return false;
  467. }
  468. static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
  469. struct obj_cgroup *objcg,
  470. gfp_t flags, size_t size,
  471. void **p)
  472. {
  473. struct slab *slab;
  474. unsigned long off;
  475. size_t i;
  476. if (!memcg_kmem_enabled() || !objcg)
  477. return;
  478. for (i = 0; i < size; i++) {
  479. if (likely(p[i])) {
  480. slab = virt_to_slab(p[i]);
  481. if (!slab_objcgs(slab) &&
  482. memcg_alloc_slab_cgroups(slab, s, flags,
  483. false)) {
  484. obj_cgroup_uncharge(objcg, obj_full_size(s));
  485. continue;
  486. }
  487. off = obj_to_index(s, slab, p[i]);
  488. obj_cgroup_get(objcg);
  489. slab_objcgs(slab)[off] = objcg;
  490. mod_objcg_state(objcg, slab_pgdat(slab),
  491. cache_vmstat_idx(s), obj_full_size(s));
  492. } else {
  493. obj_cgroup_uncharge(objcg, obj_full_size(s));
  494. }
  495. }
  496. obj_cgroup_put(objcg);
  497. }
  498. static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
  499. void **p, int objects)
  500. {
  501. struct obj_cgroup **objcgs;
  502. int i;
  503. if (!memcg_kmem_enabled())
  504. return;
  505. objcgs = slab_objcgs(slab);
  506. if (!objcgs)
  507. return;
  508. for (i = 0; i < objects; i++) {
  509. struct obj_cgroup *objcg;
  510. unsigned int off;
  511. off = obj_to_index(s, slab, p[i]);
  512. objcg = objcgs[off];
  513. if (!objcg)
  514. continue;
  515. objcgs[off] = NULL;
  516. obj_cgroup_uncharge(objcg, obj_full_size(s));
  517. mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s),
  518. -obj_full_size(s));
  519. obj_cgroup_put(objcg);
  520. }
  521. }
  522. #else /* CONFIG_MEMCG_KMEM */
  523. static inline struct obj_cgroup **slab_objcgs(struct slab *slab)
  524. {
  525. return NULL;
  526. }
  527. static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
  528. {
  529. return NULL;
  530. }
  531. static inline int memcg_alloc_slab_cgroups(struct slab *slab,
  532. struct kmem_cache *s, gfp_t gfp,
  533. bool new_slab)
  534. {
  535. return 0;
  536. }
  537. static inline void memcg_free_slab_cgroups(struct slab *slab)
  538. {
  539. }
  540. static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
  541. struct list_lru *lru,
  542. struct obj_cgroup **objcgp,
  543. size_t objects, gfp_t flags)
  544. {
  545. return true;
  546. }
  547. static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
  548. struct obj_cgroup *objcg,
  549. gfp_t flags, size_t size,
  550. void **p)
  551. {
  552. }
  553. static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
  554. void **p, int objects)
  555. {
  556. }
  557. #endif /* CONFIG_MEMCG_KMEM */
  558. #ifndef CONFIG_SLOB
  559. static inline struct kmem_cache *virt_to_cache(const void *obj)
  560. {
  561. struct slab *slab;
  562. slab = virt_to_slab(obj);
  563. if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n",
  564. __func__))
  565. return NULL;
  566. return slab->slab_cache;
  567. }
  568. static __always_inline void account_slab(struct slab *slab, int order,
  569. struct kmem_cache *s, gfp_t gfp)
  570. {
  571. if (memcg_kmem_enabled() && (s->flags & SLAB_ACCOUNT))
  572. memcg_alloc_slab_cgroups(slab, s, gfp, true);
  573. mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
  574. PAGE_SIZE << order);
  575. }
  576. static __always_inline void unaccount_slab(struct slab *slab, int order,
  577. struct kmem_cache *s)
  578. {
  579. if (memcg_kmem_enabled())
  580. memcg_free_slab_cgroups(slab);
  581. mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
  582. -(PAGE_SIZE << order));
  583. }
  584. static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  585. {
  586. struct kmem_cache *cachep;
  587. if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
  588. !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
  589. return s;
  590. cachep = virt_to_cache(x);
  591. if (WARN(cachep && cachep != s,
  592. "%s: Wrong slab cache. %s but object is from %s\n",
  593. __func__, s->name, cachep->name))
  594. print_tracking(cachep, x);
  595. return cachep;
  596. }
  597. void free_large_kmalloc(struct folio *folio, void *object);
  598. #endif /* CONFIG_SLOB */
  599. size_t __ksize(const void *objp);
  600. static inline size_t slab_ksize(const struct kmem_cache *s)
  601. {
  602. #ifndef CONFIG_SLUB
  603. return s->object_size;
  604. #else /* CONFIG_SLUB */
  605. # ifdef CONFIG_SLUB_DEBUG
  606. /*
  607. * Debugging requires use of the padding between object
  608. * and whatever may come after it.
  609. */
  610. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  611. return s->object_size;
  612. # endif
  613. if (s->flags & SLAB_KASAN)
  614. return s->object_size;
  615. /*
  616. * If we have the need to store the freelist pointer
  617. * back there or track user information then we can
  618. * only use the space before that information.
  619. */
  620. if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
  621. return s->inuse;
  622. /*
  623. * Else we can use all the padding etc for the allocation
  624. */
  625. return s->size;
  626. #endif
  627. }
  628. static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
  629. struct list_lru *lru,
  630. struct obj_cgroup **objcgp,
  631. size_t size, gfp_t flags)
  632. {
  633. flags &= gfp_allowed_mask;
  634. might_alloc(flags);
  635. if (should_failslab(s, flags))
  636. return NULL;
  637. #ifdef CONFIG_KDP
  638. if (is_kdp_kmem_cache(s))
  639. return s;
  640. #endif
  641. if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags))
  642. return NULL;
  643. return s;
  644. }
  645. static inline void slab_post_alloc_hook(struct kmem_cache *s,
  646. struct obj_cgroup *objcg, gfp_t flags,
  647. size_t size, void **p, bool init)
  648. {
  649. size_t i;
  650. flags &= gfp_allowed_mask;
  651. /*
  652. * As memory initialization might be integrated into KASAN,
  653. * kasan_slab_alloc and initialization memset must be
  654. * kept together to avoid discrepancies in behavior.
  655. *
  656. * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
  657. */
  658. for (i = 0; i < size; i++) {
  659. p[i] = kasan_slab_alloc(s, p[i], flags, init);
  660. if (p[i] && init && !kasan_has_integrated_init())
  661. memset(p[i], 0, s->object_size);
  662. kmemleak_alloc_recursive(p[i], s->object_size, 1,
  663. s->flags, flags);
  664. kmsan_slab_alloc(s, p[i], flags);
  665. }
  666. memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
  667. }
  668. #ifndef CONFIG_SLOB
  669. /*
  670. * The slab lists for all objects.
  671. */
  672. struct kmem_cache_node {
  673. spinlock_t list_lock;
  674. #ifdef CONFIG_SLAB
  675. struct list_head slabs_partial; /* partial list first, better asm code */
  676. struct list_head slabs_full;
  677. struct list_head slabs_free;
  678. unsigned long total_slabs; /* length of all slab lists */
  679. unsigned long free_slabs; /* length of free slab list only */
  680. unsigned long free_objects;
  681. unsigned int free_limit;
  682. unsigned int colour_next; /* Per-node cache coloring */
  683. struct array_cache *shared; /* shared per node */
  684. struct alien_cache **alien; /* on other nodes */
  685. unsigned long next_reap; /* updated without locking */
  686. int free_touched; /* updated without locking */
  687. #endif
  688. #ifdef CONFIG_SLUB
  689. unsigned long nr_partial;
  690. struct list_head partial;
  691. #ifdef CONFIG_SLUB_DEBUG
  692. atomic_long_t nr_slabs;
  693. atomic_long_t total_objects;
  694. struct list_head full;
  695. #endif
  696. #endif
  697. };
  698. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  699. {
  700. return s->node[node];
  701. }
  702. /*
  703. * Iterator over all nodes. The body will be executed for each node that has
  704. * a kmem_cache_node structure allocated (which is true for all online nodes)
  705. */
  706. #define for_each_kmem_cache_node(__s, __node, __n) \
  707. for (__node = 0; __node < nr_node_ids; __node++) \
  708. if ((__n = get_node(__s, __node)))
  709. #endif
  710. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
  711. void dump_unreclaimable_slab(void);
  712. #else
  713. static inline void dump_unreclaimable_slab(void)
  714. {
  715. }
  716. #endif
  717. void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
  718. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  719. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  720. gfp_t gfp);
  721. void cache_random_seq_destroy(struct kmem_cache *cachep);
  722. #else
  723. static inline int cache_random_seq_create(struct kmem_cache *cachep,
  724. unsigned int count, gfp_t gfp)
  725. {
  726. return 0;
  727. }
  728. static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
  729. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  730. static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
  731. {
  732. if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
  733. &init_on_alloc)) {
  734. if (c->ctor)
  735. return false;
  736. if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
  737. return flags & __GFP_ZERO;
  738. return true;
  739. }
  740. return flags & __GFP_ZERO;
  741. }
  742. static inline bool slab_want_init_on_free(struct kmem_cache *c)
  743. {
  744. if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
  745. &init_on_free))
  746. return !(c->ctor ||
  747. (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
  748. return false;
  749. }
  750. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
  751. void debugfs_slab_release(struct kmem_cache *);
  752. #else
  753. static inline void debugfs_slab_release(struct kmem_cache *s) { }
  754. #endif
  755. #ifdef CONFIG_PRINTK
  756. #define KS_ADDRS_COUNT 16
  757. struct kmem_obj_info {
  758. void *kp_ptr;
  759. struct slab *kp_slab;
  760. void *kp_objp;
  761. unsigned long kp_data_offset;
  762. struct kmem_cache *kp_slab_cache;
  763. void *kp_ret;
  764. void *kp_stack[KS_ADDRS_COUNT];
  765. void *kp_free_stack[KS_ADDRS_COUNT];
  766. };
  767. void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab);
  768. #endif
  769. #ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR
  770. void __check_heap_object(const void *ptr, unsigned long n,
  771. const struct slab *slab, bool to_user);
  772. #else
  773. static inline
  774. void __check_heap_object(const void *ptr, unsigned long n,
  775. const struct slab *slab, bool to_user)
  776. {
  777. }
  778. #endif
  779. #endif /* MM_SLAB_H */