slab_common.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Slab allocator functions that are independent of the allocator strategy
  4. *
  5. * (C) 2012 Christoph Lameter <[email protected]>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/mm.h>
  9. #include <linux/poison.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/memory.h>
  12. #include <linux/cache.h>
  13. #include <linux/compiler.h>
  14. #include <linux/kfence.h>
  15. #include <linux/module.h>
  16. #include <linux/cpu.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/kasan.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/page.h>
  25. #include <linux/memcontrol.h>
  26. #include <linux/stackdepot.h>
  27. #include "internal.h"
  28. #include "slab.h"
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/kmem.h>
  31. #undef CREATE_TRACE_POINTS
  32. #include <trace/hooks/mm.h>
  33. enum slab_state slab_state;
  34. LIST_HEAD(slab_caches);
  35. DEFINE_MUTEX(slab_mutex);
  36. struct kmem_cache *kmem_cache;
  37. static LIST_HEAD(slab_caches_to_rcu_destroy);
  38. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
  39. static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
  40. slab_caches_to_rcu_destroy_workfn);
  41. /*
  42. * Set of flags that will prevent slab merging
  43. */
  44. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  45. SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
  46. SLAB_FAILSLAB | kasan_never_merge())
  47. #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
  48. SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
  49. /*
  50. * Merge control. If this is set then no merging of slab caches will occur.
  51. */
  52. static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
  53. static int __init setup_slab_nomerge(char *str)
  54. {
  55. slab_nomerge = true;
  56. return 1;
  57. }
  58. static int __init setup_slab_merge(char *str)
  59. {
  60. slab_nomerge = false;
  61. return 1;
  62. }
  63. #ifdef CONFIG_SLUB
  64. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  65. __setup_param("slub_merge", slub_merge, setup_slab_merge, 0);
  66. #endif
  67. __setup("slab_nomerge", setup_slab_nomerge);
  68. __setup("slab_merge", setup_slab_merge);
  69. /*
  70. * Determine the size of a slab object
  71. */
  72. unsigned int kmem_cache_size(struct kmem_cache *s)
  73. {
  74. return s->object_size;
  75. }
  76. EXPORT_SYMBOL(kmem_cache_size);
  77. #ifdef CONFIG_DEBUG_VM
  78. static int kmem_cache_sanity_check(const char *name, unsigned int size)
  79. {
  80. if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
  81. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  82. return -EINVAL;
  83. }
  84. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  85. return 0;
  86. }
  87. #else
  88. static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
  89. {
  90. return 0;
  91. }
  92. #endif
  93. /*
  94. * Figure out what the alignment of the objects will be given a set of
  95. * flags, a user specified alignment and the size of the objects.
  96. */
  97. static unsigned int calculate_alignment(slab_flags_t flags,
  98. unsigned int align, unsigned int size)
  99. {
  100. /*
  101. * If the user wants hardware cache aligned objects then follow that
  102. * suggestion if the object is sufficiently large.
  103. *
  104. * The hardware cache alignment cannot override the specified
  105. * alignment though. If that is greater then use it.
  106. */
  107. if (flags & SLAB_HWCACHE_ALIGN) {
  108. unsigned int ralign;
  109. ralign = cache_line_size();
  110. while (size <= ralign / 2)
  111. ralign /= 2;
  112. align = max(align, ralign);
  113. }
  114. align = max(align, arch_slab_minalign());
  115. return ALIGN(align, sizeof(void *));
  116. }
  117. /*
  118. * Find a mergeable slab cache
  119. */
  120. int slab_unmergeable(struct kmem_cache *s)
  121. {
  122. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  123. return 1;
  124. if (s->ctor)
  125. return 1;
  126. if (s->usersize)
  127. return 1;
  128. /*
  129. * We may have set a slab to be unmergeable during bootstrap.
  130. */
  131. if (s->refcount < 0)
  132. return 1;
  133. return 0;
  134. }
  135. struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
  136. slab_flags_t flags, const char *name, void (*ctor)(void *))
  137. {
  138. struct kmem_cache *s;
  139. if (slab_nomerge)
  140. return NULL;
  141. if (ctor)
  142. return NULL;
  143. size = ALIGN(size, sizeof(void *));
  144. align = calculate_alignment(flags, align, size);
  145. size = ALIGN(size, align);
  146. flags = kmem_cache_flags(size, flags, name);
  147. if (flags & SLAB_NEVER_MERGE)
  148. return NULL;
  149. list_for_each_entry_reverse(s, &slab_caches, list) {
  150. if (slab_unmergeable(s))
  151. continue;
  152. if (size > s->size)
  153. continue;
  154. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  155. continue;
  156. /*
  157. * Check if alignment is compatible.
  158. * Courtesy of Adrian Drzewiecki
  159. */
  160. if ((s->size & ~(align - 1)) != s->size)
  161. continue;
  162. if (s->size - size >= sizeof(void *))
  163. continue;
  164. if (IS_ENABLED(CONFIG_SLAB) && align &&
  165. (align > s->align || s->align % align))
  166. continue;
  167. return s;
  168. }
  169. return NULL;
  170. }
  171. static struct kmem_cache *create_cache(const char *name,
  172. unsigned int object_size, unsigned int align,
  173. slab_flags_t flags, unsigned int useroffset,
  174. unsigned int usersize, void (*ctor)(void *),
  175. struct kmem_cache *root_cache)
  176. {
  177. struct kmem_cache *s;
  178. int err;
  179. if (WARN_ON(useroffset + usersize > object_size))
  180. useroffset = usersize = 0;
  181. err = -ENOMEM;
  182. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  183. if (!s)
  184. goto out;
  185. s->name = name;
  186. s->size = s->object_size = object_size;
  187. s->align = align;
  188. s->ctor = ctor;
  189. s->useroffset = useroffset;
  190. s->usersize = usersize;
  191. err = __kmem_cache_create(s, flags);
  192. if (err)
  193. goto out_free_cache;
  194. s->refcount = 1;
  195. list_add(&s->list, &slab_caches);
  196. out:
  197. if (err)
  198. return ERR_PTR(err);
  199. return s;
  200. out_free_cache:
  201. kmem_cache_free(kmem_cache, s);
  202. goto out;
  203. }
  204. /**
  205. * kmem_cache_create_usercopy - Create a cache with a region suitable
  206. * for copying to userspace
  207. * @name: A string which is used in /proc/slabinfo to identify this cache.
  208. * @size: The size of objects to be created in this cache.
  209. * @align: The required alignment for the objects.
  210. * @flags: SLAB flags
  211. * @useroffset: Usercopy region offset
  212. * @usersize: Usercopy region size
  213. * @ctor: A constructor for the objects.
  214. *
  215. * Cannot be called within a interrupt, but can be interrupted.
  216. * The @ctor is run when new pages are allocated by the cache.
  217. *
  218. * The flags are
  219. *
  220. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  221. * to catch references to uninitialised memory.
  222. *
  223. * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
  224. * for buffer overruns.
  225. *
  226. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  227. * cacheline. This can be beneficial if you're counting cycles as closely
  228. * as davem.
  229. *
  230. * Return: a pointer to the cache on success, NULL on failure.
  231. */
  232. struct kmem_cache *
  233. kmem_cache_create_usercopy(const char *name,
  234. unsigned int size, unsigned int align,
  235. slab_flags_t flags,
  236. unsigned int useroffset, unsigned int usersize,
  237. void (*ctor)(void *))
  238. {
  239. struct kmem_cache *s = NULL;
  240. const char *cache_name;
  241. int err;
  242. #ifdef CONFIG_SLUB_DEBUG
  243. /*
  244. * If no slub_debug was enabled globally, the static key is not yet
  245. * enabled by setup_slub_debug(). Enable it if the cache is being
  246. * created with any of the debugging flags passed explicitly.
  247. * It's also possible that this is the first cache created with
  248. * SLAB_STORE_USER and we should init stack_depot for it.
  249. */
  250. if (flags & SLAB_DEBUG_FLAGS)
  251. static_branch_enable(&slub_debug_enabled);
  252. if (flags & SLAB_STORE_USER)
  253. stack_depot_init();
  254. #endif
  255. mutex_lock(&slab_mutex);
  256. err = kmem_cache_sanity_check(name, size);
  257. if (err) {
  258. goto out_unlock;
  259. }
  260. /* Refuse requests with allocator specific flags */
  261. if (flags & ~SLAB_FLAGS_PERMITTED) {
  262. err = -EINVAL;
  263. goto out_unlock;
  264. }
  265. /*
  266. * Some allocators will constraint the set of valid flags to a subset
  267. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  268. * case, and we'll just provide them with a sanitized version of the
  269. * passed flags.
  270. */
  271. flags &= CACHE_CREATE_MASK;
  272. /* Fail closed on bad usersize of useroffset values. */
  273. if (WARN_ON(!usersize && useroffset) ||
  274. WARN_ON(size < usersize || size - usersize < useroffset))
  275. usersize = useroffset = 0;
  276. if (!usersize)
  277. s = __kmem_cache_alias(name, size, align, flags, ctor);
  278. if (s)
  279. goto out_unlock;
  280. cache_name = kstrdup_const(name, GFP_KERNEL);
  281. if (!cache_name) {
  282. err = -ENOMEM;
  283. goto out_unlock;
  284. }
  285. s = create_cache(cache_name, size,
  286. calculate_alignment(flags, align, size),
  287. flags, useroffset, usersize, ctor, NULL);
  288. if (IS_ERR(s)) {
  289. err = PTR_ERR(s);
  290. kfree_const(cache_name);
  291. }
  292. out_unlock:
  293. mutex_unlock(&slab_mutex);
  294. if (err) {
  295. if (flags & SLAB_PANIC)
  296. panic("%s: Failed to create slab '%s'. Error %d\n",
  297. __func__, name, err);
  298. else {
  299. pr_warn("%s(%s) failed with error %d\n",
  300. __func__, name, err);
  301. dump_stack();
  302. }
  303. return NULL;
  304. }
  305. return s;
  306. }
  307. EXPORT_SYMBOL(kmem_cache_create_usercopy);
  308. /**
  309. * kmem_cache_create - Create a cache.
  310. * @name: A string which is used in /proc/slabinfo to identify this cache.
  311. * @size: The size of objects to be created in this cache.
  312. * @align: The required alignment for the objects.
  313. * @flags: SLAB flags
  314. * @ctor: A constructor for the objects.
  315. *
  316. * Cannot be called within a interrupt, but can be interrupted.
  317. * The @ctor is run when new pages are allocated by the cache.
  318. *
  319. * The flags are
  320. *
  321. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  322. * to catch references to uninitialised memory.
  323. *
  324. * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
  325. * for buffer overruns.
  326. *
  327. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  328. * cacheline. This can be beneficial if you're counting cycles as closely
  329. * as davem.
  330. *
  331. * Return: a pointer to the cache on success, NULL on failure.
  332. */
  333. struct kmem_cache *
  334. kmem_cache_create(const char *name, unsigned int size, unsigned int align,
  335. slab_flags_t flags, void (*ctor)(void *))
  336. {
  337. return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
  338. ctor);
  339. }
  340. EXPORT_SYMBOL(kmem_cache_create);
  341. #ifdef SLAB_SUPPORTS_SYSFS
  342. /*
  343. * For a given kmem_cache, kmem_cache_destroy() should only be called
  344. * once or there will be a use-after-free problem. The actual deletion
  345. * and release of the kobject does not need slab_mutex or cpu_hotplug_lock
  346. * protection. So they are now done without holding those locks.
  347. *
  348. * Note that there will be a slight delay in the deletion of sysfs files
  349. * if kmem_cache_release() is called indrectly from a work function.
  350. */
  351. static void kmem_cache_release(struct kmem_cache *s)
  352. {
  353. sysfs_slab_unlink(s);
  354. sysfs_slab_release(s);
  355. }
  356. #else
  357. static void kmem_cache_release(struct kmem_cache *s)
  358. {
  359. slab_kmem_cache_release(s);
  360. }
  361. #endif
  362. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
  363. {
  364. LIST_HEAD(to_destroy);
  365. struct kmem_cache *s, *s2;
  366. /*
  367. * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
  368. * @slab_caches_to_rcu_destroy list. The slab pages are freed
  369. * through RCU and the associated kmem_cache are dereferenced
  370. * while freeing the pages, so the kmem_caches should be freed only
  371. * after the pending RCU operations are finished. As rcu_barrier()
  372. * is a pretty slow operation, we batch all pending destructions
  373. * asynchronously.
  374. */
  375. mutex_lock(&slab_mutex);
  376. list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy);
  377. mutex_unlock(&slab_mutex);
  378. if (list_empty(&to_destroy))
  379. return;
  380. rcu_barrier();
  381. list_for_each_entry_safe(s, s2, &to_destroy, list) {
  382. debugfs_slab_release(s);
  383. kfence_shutdown_cache(s);
  384. kmem_cache_release(s);
  385. }
  386. }
  387. static int shutdown_cache(struct kmem_cache *s)
  388. {
  389. /* free asan quarantined objects */
  390. kasan_cache_shutdown(s);
  391. if (__kmem_cache_shutdown(s) != 0)
  392. return -EBUSY;
  393. list_del(&s->list);
  394. if (s->flags & SLAB_TYPESAFE_BY_RCU) {
  395. list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
  396. schedule_work(&slab_caches_to_rcu_destroy_work);
  397. } else {
  398. kfence_shutdown_cache(s);
  399. debugfs_slab_release(s);
  400. }
  401. return 0;
  402. }
  403. void slab_kmem_cache_release(struct kmem_cache *s)
  404. {
  405. __kmem_cache_release(s);
  406. kfree_const(s->name);
  407. kmem_cache_free(kmem_cache, s);
  408. }
  409. void kmem_cache_destroy(struct kmem_cache *s)
  410. {
  411. int err = -EBUSY;
  412. bool rcu_set;
  413. if (unlikely(!s) || !kasan_check_byte(s))
  414. return;
  415. cpus_read_lock();
  416. mutex_lock(&slab_mutex);
  417. rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;
  418. s->refcount--;
  419. if (s->refcount)
  420. goto out_unlock;
  421. err = shutdown_cache(s);
  422. WARN(err, "%s %s: Slab cache still has objects when called from %pS",
  423. __func__, s->name, (void *)_RET_IP_);
  424. out_unlock:
  425. mutex_unlock(&slab_mutex);
  426. cpus_read_unlock();
  427. if (!err && !rcu_set)
  428. kmem_cache_release(s);
  429. }
  430. EXPORT_SYMBOL(kmem_cache_destroy);
  431. /**
  432. * kmem_cache_shrink - Shrink a cache.
  433. * @cachep: The cache to shrink.
  434. *
  435. * Releases as many slabs as possible for a cache.
  436. * To help debugging, a zero exit status indicates all slabs were released.
  437. *
  438. * Return: %0 if all slabs were released, non-zero otherwise
  439. */
  440. int kmem_cache_shrink(struct kmem_cache *cachep)
  441. {
  442. kasan_cache_shrink(cachep);
  443. return __kmem_cache_shrink(cachep);
  444. }
  445. EXPORT_SYMBOL(kmem_cache_shrink);
  446. bool slab_is_available(void)
  447. {
  448. return slab_state >= UP;
  449. }
  450. #ifdef CONFIG_PRINTK
  451. /**
  452. * kmem_valid_obj - does the pointer reference a valid slab object?
  453. * @object: pointer to query.
  454. *
  455. * Return: %true if the pointer is to a not-yet-freed object from
  456. * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
  457. * is to an already-freed object, and %false otherwise.
  458. */
  459. bool kmem_valid_obj(void *object)
  460. {
  461. struct folio *folio;
  462. /* Some arches consider ZERO_SIZE_PTR to be a valid address. */
  463. if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
  464. return false;
  465. folio = virt_to_folio(object);
  466. return folio_test_slab(folio);
  467. }
  468. EXPORT_SYMBOL_GPL(kmem_valid_obj);
  469. static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
  470. {
  471. if (__kfence_obj_info(kpp, object, slab))
  472. return;
  473. __kmem_obj_info(kpp, object, slab);
  474. }
  475. /**
  476. * kmem_dump_obj - Print available slab provenance information
  477. * @object: slab object for which to find provenance information.
  478. *
  479. * This function uses pr_cont(), so that the caller is expected to have
  480. * printed out whatever preamble is appropriate. The provenance information
  481. * depends on the type of object and on how much debugging is enabled.
  482. * For a slab-cache object, the fact that it is a slab object is printed,
  483. * and, if available, the slab name, return address, and stack trace from
  484. * the allocation and last free path of that object.
  485. *
  486. * This function will splat if passed a pointer to a non-slab object.
  487. * If you are not sure what type of object you have, you should instead
  488. * use mem_dump_obj().
  489. */
  490. void kmem_dump_obj(void *object)
  491. {
  492. char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc";
  493. int i;
  494. struct slab *slab;
  495. unsigned long ptroffset;
  496. struct kmem_obj_info kp = { };
  497. if (WARN_ON_ONCE(!virt_addr_valid(object)))
  498. return;
  499. slab = virt_to_slab(object);
  500. if (WARN_ON_ONCE(!slab)) {
  501. pr_cont(" non-slab memory.\n");
  502. return;
  503. }
  504. kmem_obj_info(&kp, object, slab);
  505. if (kp.kp_slab_cache)
  506. pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name);
  507. else
  508. pr_cont(" slab%s", cp);
  509. if (is_kfence_address(object))
  510. pr_cont(" (kfence)");
  511. if (kp.kp_objp)
  512. pr_cont(" start %px", kp.kp_objp);
  513. if (kp.kp_data_offset)
  514. pr_cont(" data offset %lu", kp.kp_data_offset);
  515. if (kp.kp_objp) {
  516. ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset;
  517. pr_cont(" pointer offset %lu", ptroffset);
  518. }
  519. if (kp.kp_slab_cache && kp.kp_slab_cache->usersize)
  520. pr_cont(" size %u", kp.kp_slab_cache->usersize);
  521. if (kp.kp_ret)
  522. pr_cont(" allocated at %pS\n", kp.kp_ret);
  523. else
  524. pr_cont("\n");
  525. for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) {
  526. if (!kp.kp_stack[i])
  527. break;
  528. pr_info(" %pS\n", kp.kp_stack[i]);
  529. }
  530. if (kp.kp_free_stack[0])
  531. pr_cont(" Free path:\n");
  532. for (i = 0; i < ARRAY_SIZE(kp.kp_free_stack); i++) {
  533. if (!kp.kp_free_stack[i])
  534. break;
  535. pr_info(" %pS\n", kp.kp_free_stack[i]);
  536. }
  537. }
  538. EXPORT_SYMBOL_GPL(kmem_dump_obj);
  539. #endif
  540. #ifndef CONFIG_SLOB
  541. /* Create a cache during boot when no slab services are available yet */
  542. void __init create_boot_cache(struct kmem_cache *s, const char *name,
  543. unsigned int size, slab_flags_t flags,
  544. unsigned int useroffset, unsigned int usersize)
  545. {
  546. int err;
  547. unsigned int align = ARCH_KMALLOC_MINALIGN;
  548. s->name = name;
  549. s->size = s->object_size = size;
  550. /*
  551. * For power of two sizes, guarantee natural alignment for kmalloc
  552. * caches, regardless of SL*B debugging options.
  553. */
  554. if (is_power_of_2(size))
  555. align = max(align, size);
  556. s->align = calculate_alignment(flags, align, size);
  557. s->useroffset = useroffset;
  558. s->usersize = usersize;
  559. err = __kmem_cache_create(s, flags);
  560. if (err)
  561. panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
  562. name, size, err);
  563. s->refcount = -1; /* Exempt from merging for now */
  564. }
  565. struct kmem_cache *__init create_kmalloc_cache(const char *name,
  566. unsigned int size, slab_flags_t flags,
  567. unsigned int useroffset, unsigned int usersize)
  568. {
  569. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  570. if (!s)
  571. panic("Out of memory when creating slab %s\n", name);
  572. create_boot_cache(s, name, size, flags | SLAB_KMALLOC, useroffset,
  573. usersize);
  574. kasan_cache_create_kmalloc(s);
  575. list_add(&s->list, &slab_caches);
  576. s->refcount = 1;
  577. return s;
  578. }
  579. struct kmem_cache *
  580. kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1] __ro_after_init =
  581. { /* initialization for https://bugs.llvm.org/show_bug.cgi?id=42570 */ };
  582. EXPORT_SYMBOL(kmalloc_caches);
  583. /*
  584. * Conversion table for small slabs sizes / 8 to the index in the
  585. * kmalloc array. This is necessary for slabs < 192 since we have non power
  586. * of two cache sizes there. The size of larger slabs can be determined using
  587. * fls.
  588. */
  589. static u8 size_index[24] __ro_after_init = {
  590. 3, /* 8 */
  591. 4, /* 16 */
  592. 5, /* 24 */
  593. 5, /* 32 */
  594. 6, /* 40 */
  595. 6, /* 48 */
  596. 6, /* 56 */
  597. 6, /* 64 */
  598. 1, /* 72 */
  599. 1, /* 80 */
  600. 1, /* 88 */
  601. 1, /* 96 */
  602. 7, /* 104 */
  603. 7, /* 112 */
  604. 7, /* 120 */
  605. 7, /* 128 */
  606. 2, /* 136 */
  607. 2, /* 144 */
  608. 2, /* 152 */
  609. 2, /* 160 */
  610. 2, /* 168 */
  611. 2, /* 176 */
  612. 2, /* 184 */
  613. 2 /* 192 */
  614. };
  615. static inline unsigned int size_index_elem(unsigned int bytes)
  616. {
  617. return (bytes - 1) / 8;
  618. }
  619. /*
  620. * Find the kmem_cache structure that serves a given size of
  621. * allocation
  622. */
  623. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  624. {
  625. unsigned int index;
  626. if (size <= 192) {
  627. if (!size)
  628. return ZERO_SIZE_PTR;
  629. index = size_index[size_index_elem(size)];
  630. } else {
  631. if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE))
  632. return NULL;
  633. index = fls(size - 1);
  634. }
  635. return kmalloc_caches[kmalloc_type(flags)][index];
  636. }
  637. size_t kmalloc_size_roundup(size_t size)
  638. {
  639. struct kmem_cache *c;
  640. /* Short-circuit the 0 size case. */
  641. if (unlikely(size == 0))
  642. return 0;
  643. /* Short-circuit saturated "too-large" case. */
  644. if (unlikely(size == SIZE_MAX))
  645. return SIZE_MAX;
  646. /* Above the smaller buckets, size is a multiple of page size. */
  647. if (size > KMALLOC_MAX_CACHE_SIZE)
  648. return PAGE_SIZE << get_order(size);
  649. /* The flags don't matter since size_index is common to all. */
  650. c = kmalloc_slab(size, GFP_KERNEL);
  651. return c ? c->object_size : 0;
  652. }
  653. EXPORT_SYMBOL(kmalloc_size_roundup);
  654. #ifdef CONFIG_ZONE_DMA
  655. #define KMALLOC_DMA_NAME(sz) .name[KMALLOC_DMA] = "dma-kmalloc-" #sz,
  656. #else
  657. #define KMALLOC_DMA_NAME(sz)
  658. #endif
  659. #ifdef CONFIG_MEMCG_KMEM
  660. #define KMALLOC_CGROUP_NAME(sz) .name[KMALLOC_CGROUP] = "kmalloc-cg-" #sz,
  661. #else
  662. #define KMALLOC_CGROUP_NAME(sz)
  663. #endif
  664. #define INIT_KMALLOC_INFO(__size, __short_size) \
  665. { \
  666. .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
  667. .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
  668. KMALLOC_CGROUP_NAME(__short_size) \
  669. KMALLOC_DMA_NAME(__short_size) \
  670. .size = __size, \
  671. }
  672. /*
  673. * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
  674. * kmalloc_index() supports up to 2^21=2MB, so the final entry of the table is
  675. * kmalloc-2M.
  676. */
  677. const struct kmalloc_info_struct kmalloc_info[] __initconst = {
  678. INIT_KMALLOC_INFO(0, 0),
  679. INIT_KMALLOC_INFO(96, 96),
  680. INIT_KMALLOC_INFO(192, 192),
  681. INIT_KMALLOC_INFO(8, 8),
  682. INIT_KMALLOC_INFO(16, 16),
  683. INIT_KMALLOC_INFO(32, 32),
  684. INIT_KMALLOC_INFO(64, 64),
  685. INIT_KMALLOC_INFO(128, 128),
  686. INIT_KMALLOC_INFO(256, 256),
  687. INIT_KMALLOC_INFO(512, 512),
  688. INIT_KMALLOC_INFO(1024, 1k),
  689. INIT_KMALLOC_INFO(2048, 2k),
  690. INIT_KMALLOC_INFO(4096, 4k),
  691. INIT_KMALLOC_INFO(8192, 8k),
  692. INIT_KMALLOC_INFO(16384, 16k),
  693. INIT_KMALLOC_INFO(32768, 32k),
  694. INIT_KMALLOC_INFO(65536, 64k),
  695. INIT_KMALLOC_INFO(131072, 128k),
  696. INIT_KMALLOC_INFO(262144, 256k),
  697. INIT_KMALLOC_INFO(524288, 512k),
  698. INIT_KMALLOC_INFO(1048576, 1M),
  699. INIT_KMALLOC_INFO(2097152, 2M)
  700. };
  701. /*
  702. * Patch up the size_index table if we have strange large alignment
  703. * requirements for the kmalloc array. This is only the case for
  704. * MIPS it seems. The standard arches will not generate any code here.
  705. *
  706. * Largest permitted alignment is 256 bytes due to the way we
  707. * handle the index determination for the smaller caches.
  708. *
  709. * Make sure that nothing crazy happens if someone starts tinkering
  710. * around with ARCH_KMALLOC_MINALIGN
  711. */
  712. void __init setup_kmalloc_cache_index_table(void)
  713. {
  714. unsigned int i;
  715. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  716. !is_power_of_2(KMALLOC_MIN_SIZE));
  717. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  718. unsigned int elem = size_index_elem(i);
  719. if (elem >= ARRAY_SIZE(size_index))
  720. break;
  721. size_index[elem] = KMALLOC_SHIFT_LOW;
  722. }
  723. if (KMALLOC_MIN_SIZE >= 64) {
  724. /*
  725. * The 96 byte sized cache is not used if the alignment
  726. * is 64 byte.
  727. */
  728. for (i = 64 + 8; i <= 96; i += 8)
  729. size_index[size_index_elem(i)] = 7;
  730. }
  731. if (KMALLOC_MIN_SIZE >= 128) {
  732. /*
  733. * The 192 byte sized cache is not used if the alignment
  734. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  735. * instead.
  736. */
  737. for (i = 128 + 8; i <= 192; i += 8)
  738. size_index[size_index_elem(i)] = 8;
  739. }
  740. }
  741. static void __init
  742. new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
  743. {
  744. if (type == KMALLOC_RECLAIM) {
  745. flags |= SLAB_RECLAIM_ACCOUNT;
  746. } else if (IS_ENABLED(CONFIG_MEMCG_KMEM) && (type == KMALLOC_CGROUP)) {
  747. if (mem_cgroup_kmem_disabled()) {
  748. kmalloc_caches[type][idx] = kmalloc_caches[KMALLOC_NORMAL][idx];
  749. return;
  750. }
  751. flags |= SLAB_ACCOUNT;
  752. } else if (IS_ENABLED(CONFIG_ZONE_DMA) && (type == KMALLOC_DMA)) {
  753. flags |= SLAB_CACHE_DMA;
  754. }
  755. kmalloc_caches[type][idx] = create_kmalloc_cache(
  756. kmalloc_info[idx].name[type],
  757. kmalloc_info[idx].size, flags, 0,
  758. kmalloc_info[idx].size);
  759. /*
  760. * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for
  761. * KMALLOC_NORMAL caches.
  762. */
  763. if (IS_ENABLED(CONFIG_MEMCG_KMEM) && (type == KMALLOC_NORMAL))
  764. kmalloc_caches[type][idx]->refcount = -1;
  765. }
  766. /*
  767. * Create the kmalloc array. Some of the regular kmalloc arrays
  768. * may already have been created because they were needed to
  769. * enable allocations for slab creation.
  770. */
  771. void __init create_kmalloc_caches(slab_flags_t flags)
  772. {
  773. int i;
  774. enum kmalloc_cache_type type;
  775. /*
  776. * Including KMALLOC_CGROUP if CONFIG_MEMCG_KMEM defined
  777. */
  778. for (type = KMALLOC_NORMAL; type < NR_KMALLOC_TYPES; type++) {
  779. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  780. if (!kmalloc_caches[type][i])
  781. new_kmalloc_cache(i, type, flags);
  782. /*
  783. * Caches that are not of the two-to-the-power-of size.
  784. * These have to be created immediately after the
  785. * earlier power of two caches
  786. */
  787. if (KMALLOC_MIN_SIZE <= 32 && i == 6 &&
  788. !kmalloc_caches[type][1])
  789. new_kmalloc_cache(1, type, flags);
  790. if (KMALLOC_MIN_SIZE <= 64 && i == 7 &&
  791. !kmalloc_caches[type][2])
  792. new_kmalloc_cache(2, type, flags);
  793. }
  794. }
  795. /* Kmalloc array is now usable */
  796. slab_state = UP;
  797. }
  798. void free_large_kmalloc(struct folio *folio, void *object)
  799. {
  800. unsigned int order = folio_order(folio);
  801. if (WARN_ON_ONCE(order == 0))
  802. pr_warn_once("object pointer: 0x%p\n", object);
  803. kmemleak_free(object);
  804. kasan_kfree_large(object);
  805. kmsan_kfree_large(object);
  806. mod_lruvec_page_state(folio_page(folio, 0), NR_SLAB_UNRECLAIMABLE_B,
  807. -(PAGE_SIZE << order));
  808. __free_pages(folio_page(folio, 0), order);
  809. }
  810. static void *__kmalloc_large_node(size_t size, gfp_t flags, int node);
  811. static __always_inline
  812. void *__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
  813. {
  814. struct kmem_cache *s;
  815. void *ret;
  816. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
  817. ret = __kmalloc_large_node(size, flags, node);
  818. trace_kmalloc(caller, ret, size,
  819. PAGE_SIZE << get_order(size), flags, node);
  820. return ret;
  821. }
  822. s = kmalloc_slab(size, flags);
  823. if (unlikely(ZERO_OR_NULL_PTR(s)))
  824. return s;
  825. ret = __kmem_cache_alloc_node(s, flags, node, size, caller);
  826. ret = kasan_kmalloc(s, ret, size, flags);
  827. trace_kmalloc(caller, ret, size, s->size, flags, node);
  828. return ret;
  829. }
  830. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  831. {
  832. return __do_kmalloc_node(size, flags, node, _RET_IP_);
  833. }
  834. EXPORT_SYMBOL(__kmalloc_node);
  835. void *__kmalloc(size_t size, gfp_t flags)
  836. {
  837. return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_);
  838. }
  839. EXPORT_SYMBOL(__kmalloc);
  840. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  841. int node, unsigned long caller)
  842. {
  843. return __do_kmalloc_node(size, flags, node, caller);
  844. }
  845. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  846. /**
  847. * kfree - free previously allocated memory
  848. * @object: pointer returned by kmalloc.
  849. *
  850. * If @object is NULL, no operation is performed.
  851. *
  852. * Don't free memory not originally allocated by kmalloc()
  853. * or you will run into trouble.
  854. */
  855. void kfree(const void *object)
  856. {
  857. struct folio *folio;
  858. struct slab *slab;
  859. struct kmem_cache *s;
  860. trace_kfree(_RET_IP_, object);
  861. if (unlikely(ZERO_OR_NULL_PTR(object)))
  862. return;
  863. folio = virt_to_folio(object);
  864. if (unlikely(!folio_test_slab(folio))) {
  865. free_large_kmalloc(folio, (void *)object);
  866. return;
  867. }
  868. slab = folio_slab(folio);
  869. s = slab->slab_cache;
  870. __kmem_cache_free(s, (void *)object, _RET_IP_);
  871. }
  872. EXPORT_SYMBOL(kfree);
  873. /**
  874. * __ksize -- Report full size of underlying allocation
  875. * @object: pointer to the object
  876. *
  877. * This should only be used internally to query the true size of allocations.
  878. * It is not meant to be a way to discover the usable size of an allocation
  879. * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond
  880. * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS,
  881. * and/or FORTIFY_SOURCE.
  882. *
  883. * Return: size of the actual memory used by @object in bytes
  884. */
  885. size_t __ksize(const void *object)
  886. {
  887. struct folio *folio;
  888. if (unlikely(object == ZERO_SIZE_PTR))
  889. return 0;
  890. folio = virt_to_folio(object);
  891. if (unlikely(!folio_test_slab(folio))) {
  892. if (WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE))
  893. return 0;
  894. if (WARN_ON(object != folio_address(folio)))
  895. return 0;
  896. return folio_size(folio);
  897. }
  898. return slab_ksize(folio_slab(folio)->slab_cache);
  899. }
  900. void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  901. {
  902. void *ret = __kmem_cache_alloc_node(s, gfpflags, NUMA_NO_NODE,
  903. size, _RET_IP_);
  904. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE);
  905. ret = kasan_kmalloc(s, ret, size, gfpflags);
  906. return ret;
  907. }
  908. EXPORT_SYMBOL(kmalloc_trace);
  909. void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
  910. int node, size_t size)
  911. {
  912. void *ret = __kmem_cache_alloc_node(s, gfpflags, node, size, _RET_IP_);
  913. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, node);
  914. ret = kasan_kmalloc(s, ret, size, gfpflags);
  915. return ret;
  916. }
  917. EXPORT_SYMBOL(kmalloc_node_trace);
  918. #endif /* !CONFIG_SLOB */
  919. gfp_t kmalloc_fix_flags(gfp_t flags)
  920. {
  921. gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
  922. flags &= ~GFP_SLAB_BUG_MASK;
  923. pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
  924. invalid_mask, &invalid_mask, flags, &flags);
  925. dump_stack();
  926. return flags;
  927. }
  928. /*
  929. * To avoid unnecessary overhead, we pass through large allocation requests
  930. * directly to the page allocator. We use __GFP_COMP, because we will need to
  931. * know the allocation order to free the pages properly in kfree.
  932. */
  933. static void *__kmalloc_large_node(size_t size, gfp_t flags, int node)
  934. {
  935. struct page *page;
  936. void *ptr = NULL;
  937. unsigned int order = get_order(size);
  938. if (unlikely(flags & GFP_SLAB_BUG_MASK))
  939. flags = kmalloc_fix_flags(flags);
  940. flags |= __GFP_COMP;
  941. page = alloc_pages_node(node, flags, order);
  942. if (page) {
  943. ptr = page_address(page);
  944. mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
  945. PAGE_SIZE << order);
  946. }
  947. trace_android_vh_kmalloc_large_alloced(page, order, flags);
  948. ptr = kasan_kmalloc_large(ptr, size, flags);
  949. /* As ptr might get tagged, call kmemleak hook after KASAN. */
  950. kmemleak_alloc(ptr, size, 1, flags);
  951. kmsan_kmalloc_large(ptr, size, flags);
  952. return ptr;
  953. }
  954. void *kmalloc_large(size_t size, gfp_t flags)
  955. {
  956. void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE);
  957. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size),
  958. flags, NUMA_NO_NODE);
  959. return ret;
  960. }
  961. EXPORT_SYMBOL(kmalloc_large);
  962. void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  963. {
  964. void *ret = __kmalloc_large_node(size, flags, node);
  965. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size),
  966. flags, node);
  967. return ret;
  968. }
  969. EXPORT_SYMBOL(kmalloc_large_node);
  970. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  971. /* Randomize a generic freelist */
  972. static void freelist_randomize(struct rnd_state *state, unsigned int *list,
  973. unsigned int count)
  974. {
  975. unsigned int rand;
  976. unsigned int i;
  977. for (i = 0; i < count; i++)
  978. list[i] = i;
  979. /* Fisher-Yates shuffle */
  980. for (i = count - 1; i > 0; i--) {
  981. rand = prandom_u32_state(state);
  982. rand %= (i + 1);
  983. swap(list[i], list[rand]);
  984. }
  985. }
  986. /* Create a random sequence per cache */
  987. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  988. gfp_t gfp)
  989. {
  990. struct rnd_state state;
  991. if (count < 2 || cachep->random_seq)
  992. return 0;
  993. cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
  994. if (!cachep->random_seq)
  995. return -ENOMEM;
  996. /* Get best entropy at this stage of boot */
  997. prandom_seed_state(&state, get_random_long());
  998. freelist_randomize(&state, cachep->random_seq, count);
  999. return 0;
  1000. }
  1001. /* Destroy the per-cache random freelist sequence */
  1002. void cache_random_seq_destroy(struct kmem_cache *cachep)
  1003. {
  1004. kfree(cachep->random_seq);
  1005. cachep->random_seq = NULL;
  1006. }
  1007. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  1008. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
  1009. #ifdef CONFIG_SLAB
  1010. #define SLABINFO_RIGHTS (0600)
  1011. #else
  1012. #define SLABINFO_RIGHTS (0400)
  1013. #endif
  1014. static void print_slabinfo_header(struct seq_file *m)
  1015. {
  1016. /*
  1017. * Output format version, so at least we can change it
  1018. * without _too_ many complaints.
  1019. */
  1020. #ifdef CONFIG_DEBUG_SLAB
  1021. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  1022. #else
  1023. seq_puts(m, "slabinfo - version: 2.1\n");
  1024. #endif
  1025. seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  1026. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  1027. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  1028. #ifdef CONFIG_DEBUG_SLAB
  1029. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  1030. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  1031. #endif
  1032. trace_android_vh_print_slabinfo_header(m);
  1033. seq_putc(m, '\n');
  1034. }
  1035. static void *slab_start(struct seq_file *m, loff_t *pos)
  1036. {
  1037. mutex_lock(&slab_mutex);
  1038. return seq_list_start(&slab_caches, *pos);
  1039. }
  1040. static void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  1041. {
  1042. return seq_list_next(p, &slab_caches, pos);
  1043. }
  1044. static void slab_stop(struct seq_file *m, void *p)
  1045. {
  1046. mutex_unlock(&slab_mutex);
  1047. }
  1048. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  1049. {
  1050. struct slabinfo sinfo;
  1051. memset(&sinfo, 0, sizeof(sinfo));
  1052. get_slabinfo(s, &sinfo);
  1053. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  1054. s->name, sinfo.active_objs, sinfo.num_objs, s->size,
  1055. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  1056. seq_printf(m, " : tunables %4u %4u %4u",
  1057. sinfo.limit, sinfo.batchcount, sinfo.shared);
  1058. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  1059. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  1060. slabinfo_show_stats(m, s);
  1061. trace_android_vh_cache_show(m, &sinfo, s);
  1062. seq_putc(m, '\n');
  1063. }
  1064. static int slab_show(struct seq_file *m, void *p)
  1065. {
  1066. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  1067. if (p == slab_caches.next)
  1068. print_slabinfo_header(m);
  1069. cache_show(s, m);
  1070. return 0;
  1071. }
  1072. void dump_unreclaimable_slab(void)
  1073. {
  1074. struct kmem_cache *s;
  1075. struct slabinfo sinfo;
  1076. /*
  1077. * Here acquiring slab_mutex is risky since we don't prefer to get
  1078. * sleep in oom path. But, without mutex hold, it may introduce a
  1079. * risk of crash.
  1080. * Use mutex_trylock to protect the list traverse, dump nothing
  1081. * without acquiring the mutex.
  1082. */
  1083. if (!mutex_trylock(&slab_mutex)) {
  1084. pr_warn("excessive unreclaimable slab but cannot dump stats\n");
  1085. return;
  1086. }
  1087. pr_info("Unreclaimable slab info:\n");
  1088. pr_info("Name Used Total\n");
  1089. list_for_each_entry(s, &slab_caches, list) {
  1090. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  1091. continue;
  1092. get_slabinfo(s, &sinfo);
  1093. if (sinfo.num_objs > 0)
  1094. pr_info("%-17s %10luKB %10luKB\n", s->name,
  1095. (sinfo.active_objs * s->size) / 1024,
  1096. (sinfo.num_objs * s->size) / 1024);
  1097. }
  1098. mutex_unlock(&slab_mutex);
  1099. }
  1100. /*
  1101. * slabinfo_op - iterator that generates /proc/slabinfo
  1102. *
  1103. * Output layout:
  1104. * cache-name
  1105. * num-active-objs
  1106. * total-objs
  1107. * object size
  1108. * num-active-slabs
  1109. * total-slabs
  1110. * num-pages-per-slab
  1111. * + further values on SMP and with statistics enabled
  1112. */
  1113. static const struct seq_operations slabinfo_op = {
  1114. .start = slab_start,
  1115. .next = slab_next,
  1116. .stop = slab_stop,
  1117. .show = slab_show,
  1118. };
  1119. static int slabinfo_open(struct inode *inode, struct file *file)
  1120. {
  1121. return seq_open(file, &slabinfo_op);
  1122. }
  1123. static const struct proc_ops slabinfo_proc_ops = {
  1124. .proc_flags = PROC_ENTRY_PERMANENT,
  1125. .proc_open = slabinfo_open,
  1126. .proc_read = seq_read,
  1127. .proc_write = slabinfo_write,
  1128. .proc_lseek = seq_lseek,
  1129. .proc_release = seq_release,
  1130. };
  1131. static int __init slab_proc_init(void)
  1132. {
  1133. proc_create("slabinfo", SLABINFO_RIGHTS, NULL, &slabinfo_proc_ops);
  1134. return 0;
  1135. }
  1136. module_init(slab_proc_init);
  1137. #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
  1138. static __always_inline __realloc_size(2) void *
  1139. __do_krealloc(const void *p, size_t new_size, gfp_t flags)
  1140. {
  1141. void *ret;
  1142. size_t ks;
  1143. /* Don't use instrumented ksize to allow precise KASAN poisoning. */
  1144. if (likely(!ZERO_OR_NULL_PTR(p))) {
  1145. if (!kasan_check_byte(p))
  1146. return NULL;
  1147. ks = kfence_ksize(p) ?: __ksize(p);
  1148. } else
  1149. ks = 0;
  1150. /* If the object still fits, repoison it precisely. */
  1151. if (ks >= new_size) {
  1152. p = kasan_krealloc((void *)p, new_size, flags);
  1153. return (void *)p;
  1154. }
  1155. ret = kmalloc_track_caller(new_size, flags);
  1156. if (ret && p) {
  1157. /* Disable KASAN checks as the object's redzone is accessed. */
  1158. kasan_disable_current();
  1159. memcpy(ret, kasan_reset_tag(p), ks);
  1160. kasan_enable_current();
  1161. }
  1162. return ret;
  1163. }
  1164. /**
  1165. * krealloc - reallocate memory. The contents will remain unchanged.
  1166. * @p: object to reallocate memory for.
  1167. * @new_size: how many bytes of memory are required.
  1168. * @flags: the type of memory to allocate.
  1169. *
  1170. * The contents of the object pointed to are preserved up to the
  1171. * lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored).
  1172. * If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size
  1173. * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
  1174. *
  1175. * Return: pointer to the allocated memory or %NULL in case of error
  1176. */
  1177. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  1178. {
  1179. void *ret;
  1180. if (unlikely(!new_size)) {
  1181. kfree(p);
  1182. return ZERO_SIZE_PTR;
  1183. }
  1184. ret = __do_krealloc(p, new_size, flags);
  1185. if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
  1186. kfree(p);
  1187. return ret;
  1188. }
  1189. EXPORT_SYMBOL(krealloc);
  1190. /**
  1191. * kfree_sensitive - Clear sensitive information in memory before freeing
  1192. * @p: object to free memory of
  1193. *
  1194. * The memory of the object @p points to is zeroed before freed.
  1195. * If @p is %NULL, kfree_sensitive() does nothing.
  1196. *
  1197. * Note: this function zeroes the whole allocated buffer which can be a good
  1198. * deal bigger than the requested buffer size passed to kmalloc(). So be
  1199. * careful when using this function in performance sensitive code.
  1200. */
  1201. void kfree_sensitive(const void *p)
  1202. {
  1203. size_t ks;
  1204. void *mem = (void *)p;
  1205. ks = ksize(mem);
  1206. if (ks)
  1207. memzero_explicit(mem, ks);
  1208. kfree(mem);
  1209. }
  1210. EXPORT_SYMBOL(kfree_sensitive);
  1211. size_t ksize(const void *objp)
  1212. {
  1213. size_t size;
  1214. /*
  1215. * We need to first check that the pointer to the object is valid, and
  1216. * only then unpoison the memory. The report printed from ksize() is
  1217. * more useful, then when it's printed later when the behaviour could
  1218. * be undefined due to a potential use-after-free or double-free.
  1219. *
  1220. * We use kasan_check_byte(), which is supported for the hardware
  1221. * tag-based KASAN mode, unlike kasan_check_read/write().
  1222. *
  1223. * If the pointed to memory is invalid, we return 0 to avoid users of
  1224. * ksize() writing to and potentially corrupting the memory region.
  1225. *
  1226. * We want to perform the check before __ksize(), to avoid potentially
  1227. * crashing in __ksize() due to accessing invalid metadata.
  1228. */
  1229. if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp))
  1230. return 0;
  1231. size = kfence_ksize(objp) ?: __ksize(objp);
  1232. /*
  1233. * We assume that ksize callers could use whole allocated area,
  1234. * so we need to unpoison this area.
  1235. */
  1236. kasan_unpoison_range(objp, size);
  1237. return size;
  1238. }
  1239. EXPORT_SYMBOL(ksize);
  1240. /* Tracepoints definitions. */
  1241. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1242. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1243. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1244. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
  1245. int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
  1246. {
  1247. if (__should_failslab(s, gfpflags))
  1248. return -ENOMEM;
  1249. return 0;
  1250. }
  1251. ALLOW_ERROR_INJECTION(should_failslab, ERRNO);