slab_common.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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 <cl@linux.com>
  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/dma-mapping.h>
  20. #include <linux/swiotlb.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/kmemleak.h>
  24. #include <linux/kasan.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/page.h>
  28. #include <linux/memcontrol.h>
  29. #include <linux/stackdepot.h>
  30. #include "internal.h"
  31. #include "slab.h"
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/kmem.h>
  34. enum slab_state slab_state;
  35. LIST_HEAD(slab_caches);
  36. DEFINE_MUTEX(slab_mutex);
  37. struct kmem_cache *kmem_cache;
  38. /*
  39. * Set of flags that will prevent slab merging
  40. */
  41. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  42. SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
  43. SLAB_FAILSLAB | SLAB_NO_MERGE)
  44. #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
  45. SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
  46. /*
  47. * Merge control. If this is set then no merging of slab caches will occur.
  48. */
  49. static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
  50. static int __init setup_slab_nomerge(char *str)
  51. {
  52. slab_nomerge = true;
  53. return 1;
  54. }
  55. static int __init setup_slab_merge(char *str)
  56. {
  57. slab_nomerge = false;
  58. return 1;
  59. }
  60. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  61. __setup_param("slub_merge", slub_merge, setup_slab_merge, 0);
  62. __setup("slab_nomerge", setup_slab_nomerge);
  63. __setup("slab_merge", setup_slab_merge);
  64. /*
  65. * Determine the size of a slab object
  66. */
  67. unsigned int kmem_cache_size(struct kmem_cache *s)
  68. {
  69. return s->object_size;
  70. }
  71. EXPORT_SYMBOL(kmem_cache_size);
  72. #ifdef CONFIG_DEBUG_VM
  73. static bool kmem_cache_is_duplicate_name(const char *name)
  74. {
  75. struct kmem_cache *s;
  76. list_for_each_entry(s, &slab_caches, list) {
  77. if (!strcmp(s->name, name))
  78. return true;
  79. }
  80. return false;
  81. }
  82. static int kmem_cache_sanity_check(const char *name, unsigned int size)
  83. {
  84. if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
  85. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  86. return -EINVAL;
  87. }
  88. /* Duplicate names will confuse slabtop, et al */
  89. WARN(kmem_cache_is_duplicate_name(name),
  90. "kmem_cache of name '%s' already exists\n", name);
  91. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  92. return 0;
  93. }
  94. #else
  95. static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
  96. {
  97. return 0;
  98. }
  99. #endif
  100. /*
  101. * Figure out what the alignment of the objects will be given a set of
  102. * flags, a user specified alignment and the size of the objects.
  103. */
  104. static unsigned int calculate_alignment(slab_flags_t flags,
  105. unsigned int align, unsigned int size)
  106. {
  107. /*
  108. * If the user wants hardware cache aligned objects then follow that
  109. * suggestion if the object is sufficiently large.
  110. *
  111. * The hardware cache alignment cannot override the specified
  112. * alignment though. If that is greater then use it.
  113. */
  114. if (flags & SLAB_HWCACHE_ALIGN) {
  115. unsigned int ralign;
  116. ralign = cache_line_size();
  117. while (size <= ralign / 2)
  118. ralign /= 2;
  119. align = max(align, ralign);
  120. }
  121. align = max(align, arch_slab_minalign());
  122. return ALIGN(align, sizeof(void *));
  123. }
  124. /*
  125. * Find a mergeable slab cache
  126. */
  127. int slab_unmergeable(struct kmem_cache *s)
  128. {
  129. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  130. return 1;
  131. if (s->ctor)
  132. return 1;
  133. #ifdef CONFIG_HARDENED_USERCOPY
  134. if (s->usersize)
  135. return 1;
  136. #endif
  137. /*
  138. * We may have set a slab to be unmergeable during bootstrap.
  139. */
  140. if (s->refcount < 0)
  141. return 1;
  142. return 0;
  143. }
  144. struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
  145. slab_flags_t flags, const char *name, void (*ctor)(void *))
  146. {
  147. struct kmem_cache *s;
  148. if (slab_nomerge)
  149. return NULL;
  150. if (ctor)
  151. return NULL;
  152. flags = kmem_cache_flags(flags, name);
  153. if (flags & SLAB_NEVER_MERGE)
  154. return NULL;
  155. size = ALIGN(size, sizeof(void *));
  156. align = calculate_alignment(flags, align, size);
  157. size = ALIGN(size, align);
  158. list_for_each_entry_reverse(s, &slab_caches, list) {
  159. if (slab_unmergeable(s))
  160. continue;
  161. if (size > s->size)
  162. continue;
  163. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  164. continue;
  165. /*
  166. * Check if alignment is compatible.
  167. * Courtesy of Adrian Drzewiecki
  168. */
  169. if ((s->size & ~(align - 1)) != s->size)
  170. continue;
  171. if (s->size - size >= sizeof(void *))
  172. continue;
  173. return s;
  174. }
  175. return NULL;
  176. }
  177. static struct kmem_cache *create_cache(const char *name,
  178. unsigned int object_size,
  179. struct kmem_cache_args *args,
  180. slab_flags_t flags)
  181. {
  182. struct kmem_cache *s;
  183. int err;
  184. if (WARN_ON(args->useroffset + args->usersize > object_size))
  185. args->useroffset = args->usersize = 0;
  186. /* If a custom freelist pointer is requested make sure it's sane. */
  187. err = -EINVAL;
  188. if (args->use_freeptr_offset &&
  189. (args->freeptr_offset >= object_size ||
  190. !(flags & SLAB_TYPESAFE_BY_RCU) ||
  191. !IS_ALIGNED(args->freeptr_offset, __alignof__(freeptr_t))))
  192. goto out;
  193. err = -ENOMEM;
  194. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  195. if (!s)
  196. goto out;
  197. err = do_kmem_cache_create(s, name, object_size, args, flags);
  198. if (err)
  199. goto out_free_cache;
  200. s->refcount = 1;
  201. list_add(&s->list, &slab_caches);
  202. return s;
  203. out_free_cache:
  204. kmem_cache_free(kmem_cache, s);
  205. out:
  206. return ERR_PTR(err);
  207. }
  208. /**
  209. * __kmem_cache_create_args - Create a kmem cache.
  210. * @name: A string which is used in /proc/slabinfo to identify this cache.
  211. * @object_size: The size of objects to be created in this cache.
  212. * @args: Additional arguments for the cache creation (see
  213. * &struct kmem_cache_args).
  214. * @flags: See %SLAB_* flags for an explanation of individual @flags.
  215. *
  216. * Not to be called directly, use the kmem_cache_create() wrapper with the same
  217. * parameters.
  218. *
  219. * Context: Cannot be called within a interrupt, but can be interrupted.
  220. *
  221. * Return: a pointer to the cache on success, NULL on failure.
  222. */
  223. struct kmem_cache *__kmem_cache_create_args(const char *name,
  224. unsigned int object_size,
  225. struct kmem_cache_args *args,
  226. slab_flags_t flags)
  227. {
  228. struct kmem_cache *s = NULL;
  229. const char *cache_name;
  230. int err;
  231. #ifdef CONFIG_SLUB_DEBUG
  232. /*
  233. * If no slab_debug was enabled globally, the static key is not yet
  234. * enabled by setup_slub_debug(). Enable it if the cache is being
  235. * created with any of the debugging flags passed explicitly.
  236. * It's also possible that this is the first cache created with
  237. * SLAB_STORE_USER and we should init stack_depot for it.
  238. */
  239. if (flags & SLAB_DEBUG_FLAGS)
  240. static_branch_enable(&slub_debug_enabled);
  241. if (flags & SLAB_STORE_USER)
  242. stack_depot_init();
  243. #endif
  244. mutex_lock(&slab_mutex);
  245. err = kmem_cache_sanity_check(name, object_size);
  246. if (err) {
  247. goto out_unlock;
  248. }
  249. /* Refuse requests with allocator specific flags */
  250. if (flags & ~SLAB_FLAGS_PERMITTED) {
  251. err = -EINVAL;
  252. goto out_unlock;
  253. }
  254. /*
  255. * Some allocators will constraint the set of valid flags to a subset
  256. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  257. * case, and we'll just provide them with a sanitized version of the
  258. * passed flags.
  259. */
  260. flags &= CACHE_CREATE_MASK;
  261. /* Fail closed on bad usersize of useroffset values. */
  262. if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) ||
  263. WARN_ON(!args->usersize && args->useroffset) ||
  264. WARN_ON(object_size < args->usersize ||
  265. object_size - args->usersize < args->useroffset))
  266. args->usersize = args->useroffset = 0;
  267. if (!args->usersize)
  268. s = __kmem_cache_alias(name, object_size, args->align, flags,
  269. args->ctor);
  270. if (s)
  271. goto out_unlock;
  272. cache_name = kstrdup_const(name, GFP_KERNEL);
  273. if (!cache_name) {
  274. err = -ENOMEM;
  275. goto out_unlock;
  276. }
  277. args->align = calculate_alignment(flags, args->align, object_size);
  278. s = create_cache(cache_name, object_size, args, flags);
  279. if (IS_ERR(s)) {
  280. err = PTR_ERR(s);
  281. kfree_const(cache_name);
  282. }
  283. out_unlock:
  284. mutex_unlock(&slab_mutex);
  285. if (err) {
  286. if (flags & SLAB_PANIC)
  287. panic("%s: Failed to create slab '%s'. Error %d\n",
  288. __func__, name, err);
  289. else {
  290. pr_warn("%s(%s) failed with error %d\n",
  291. __func__, name, err);
  292. dump_stack();
  293. }
  294. return NULL;
  295. }
  296. return s;
  297. }
  298. EXPORT_SYMBOL(__kmem_cache_create_args);
  299. static struct kmem_cache *kmem_buckets_cache __ro_after_init;
  300. /**
  301. * kmem_buckets_create - Create a set of caches that handle dynamic sized
  302. * allocations via kmem_buckets_alloc()
  303. * @name: A prefix string which is used in /proc/slabinfo to identify this
  304. * cache. The individual caches with have their sizes as the suffix.
  305. * @flags: SLAB flags (see kmem_cache_create() for details).
  306. * @useroffset: Starting offset within an allocation that may be copied
  307. * to/from userspace.
  308. * @usersize: How many bytes, starting at @useroffset, may be copied
  309. * to/from userspace.
  310. * @ctor: A constructor for the objects, run when new allocations are made.
  311. *
  312. * Cannot be called within an interrupt, but can be interrupted.
  313. *
  314. * Return: a pointer to the cache on success, NULL on failure. When
  315. * CONFIG_SLAB_BUCKETS is not enabled, ZERO_SIZE_PTR is returned, and
  316. * subsequent calls to kmem_buckets_alloc() will fall back to kmalloc().
  317. * (i.e. callers only need to check for NULL on failure.)
  318. */
  319. kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
  320. unsigned int useroffset,
  321. unsigned int usersize,
  322. void (*ctor)(void *))
  323. {
  324. unsigned long mask = 0;
  325. unsigned int idx;
  326. kmem_buckets *b;
  327. BUILD_BUG_ON(ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]) > BITS_PER_LONG);
  328. /*
  329. * When the separate buckets API is not built in, just return
  330. * a non-NULL value for the kmem_buckets pointer, which will be
  331. * unused when performing allocations.
  332. */
  333. if (!IS_ENABLED(CONFIG_SLAB_BUCKETS))
  334. return ZERO_SIZE_PTR;
  335. if (WARN_ON(!kmem_buckets_cache))
  336. return NULL;
  337. b = kmem_cache_alloc(kmem_buckets_cache, GFP_KERNEL|__GFP_ZERO);
  338. if (WARN_ON(!b))
  339. return NULL;
  340. flags |= SLAB_NO_MERGE;
  341. for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) {
  342. char *short_size, *cache_name;
  343. unsigned int cache_useroffset, cache_usersize;
  344. unsigned int size, aligned_idx;
  345. if (!kmalloc_caches[KMALLOC_NORMAL][idx])
  346. continue;
  347. size = kmalloc_caches[KMALLOC_NORMAL][idx]->object_size;
  348. if (!size)
  349. continue;
  350. short_size = strchr(kmalloc_caches[KMALLOC_NORMAL][idx]->name, '-');
  351. if (WARN_ON(!short_size))
  352. goto fail;
  353. if (useroffset >= size) {
  354. cache_useroffset = 0;
  355. cache_usersize = 0;
  356. } else {
  357. cache_useroffset = useroffset;
  358. cache_usersize = min(size - cache_useroffset, usersize);
  359. }
  360. aligned_idx = __kmalloc_index(size, false);
  361. if (!(*b)[aligned_idx]) {
  362. cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1);
  363. if (WARN_ON(!cache_name))
  364. goto fail;
  365. (*b)[aligned_idx] = kmem_cache_create_usercopy(cache_name, size,
  366. 0, flags, cache_useroffset,
  367. cache_usersize, ctor);
  368. kfree(cache_name);
  369. if (WARN_ON(!(*b)[aligned_idx]))
  370. goto fail;
  371. set_bit(aligned_idx, &mask);
  372. }
  373. if (idx != aligned_idx)
  374. (*b)[idx] = (*b)[aligned_idx];
  375. }
  376. return b;
  377. fail:
  378. for_each_set_bit(idx, &mask, ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]))
  379. kmem_cache_destroy((*b)[idx]);
  380. kmem_cache_free(kmem_buckets_cache, b);
  381. return NULL;
  382. }
  383. EXPORT_SYMBOL(kmem_buckets_create);
  384. /*
  385. * For a given kmem_cache, kmem_cache_destroy() should only be called
  386. * once or there will be a use-after-free problem. The actual deletion
  387. * and release of the kobject does not need slab_mutex or cpu_hotplug_lock
  388. * protection. So they are now done without holding those locks.
  389. */
  390. static void kmem_cache_release(struct kmem_cache *s)
  391. {
  392. kfence_shutdown_cache(s);
  393. if (__is_defined(SLAB_SUPPORTS_SYSFS) && slab_state >= FULL)
  394. sysfs_slab_release(s);
  395. else
  396. slab_kmem_cache_release(s);
  397. }
  398. void slab_kmem_cache_release(struct kmem_cache *s)
  399. {
  400. __kmem_cache_release(s);
  401. kfree_const(s->name);
  402. kmem_cache_free(kmem_cache, s);
  403. }
  404. void kmem_cache_destroy(struct kmem_cache *s)
  405. {
  406. int err;
  407. if (unlikely(!s) || !kasan_check_byte(s))
  408. return;
  409. /* in-flight kfree_rcu()'s may include objects from our cache */
  410. kvfree_rcu_barrier();
  411. if (IS_ENABLED(CONFIG_SLUB_RCU_DEBUG) &&
  412. (s->flags & SLAB_TYPESAFE_BY_RCU)) {
  413. /*
  414. * Under CONFIG_SLUB_RCU_DEBUG, when objects in a
  415. * SLAB_TYPESAFE_BY_RCU slab are freed, SLUB will internally
  416. * defer their freeing with call_rcu().
  417. * Wait for such call_rcu() invocations here before actually
  418. * destroying the cache.
  419. *
  420. * It doesn't matter that we haven't looked at the slab refcount
  421. * yet - slabs with SLAB_TYPESAFE_BY_RCU can't be merged, so
  422. * the refcount should be 1 here.
  423. */
  424. rcu_barrier();
  425. }
  426. cpus_read_lock();
  427. mutex_lock(&slab_mutex);
  428. s->refcount--;
  429. if (s->refcount) {
  430. mutex_unlock(&slab_mutex);
  431. cpus_read_unlock();
  432. return;
  433. }
  434. /* free asan quarantined objects */
  435. kasan_cache_shutdown(s);
  436. err = __kmem_cache_shutdown(s);
  437. if (!slab_in_kunit_test())
  438. WARN(err, "%s %s: Slab cache still has objects when called from %pS",
  439. __func__, s->name, (void *)_RET_IP_);
  440. list_del(&s->list);
  441. mutex_unlock(&slab_mutex);
  442. cpus_read_unlock();
  443. if (slab_state >= FULL)
  444. sysfs_slab_unlink(s);
  445. debugfs_slab_release(s);
  446. if (err)
  447. return;
  448. if (s->flags & SLAB_TYPESAFE_BY_RCU)
  449. rcu_barrier();
  450. kmem_cache_release(s);
  451. }
  452. EXPORT_SYMBOL(kmem_cache_destroy);
  453. /**
  454. * kmem_cache_shrink - Shrink a cache.
  455. * @cachep: The cache to shrink.
  456. *
  457. * Releases as many slabs as possible for a cache.
  458. * To help debugging, a zero exit status indicates all slabs were released.
  459. *
  460. * Return: %0 if all slabs were released, non-zero otherwise
  461. */
  462. int kmem_cache_shrink(struct kmem_cache *cachep)
  463. {
  464. kasan_cache_shrink(cachep);
  465. return __kmem_cache_shrink(cachep);
  466. }
  467. EXPORT_SYMBOL(kmem_cache_shrink);
  468. bool slab_is_available(void)
  469. {
  470. return slab_state >= UP;
  471. }
  472. #ifdef CONFIG_PRINTK
  473. static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
  474. {
  475. if (__kfence_obj_info(kpp, object, slab))
  476. return;
  477. __kmem_obj_info(kpp, object, slab);
  478. }
  479. /**
  480. * kmem_dump_obj - Print available slab provenance information
  481. * @object: slab object for which to find provenance information.
  482. *
  483. * This function uses pr_cont(), so that the caller is expected to have
  484. * printed out whatever preamble is appropriate. The provenance information
  485. * depends on the type of object and on how much debugging is enabled.
  486. * For a slab-cache object, the fact that it is a slab object is printed,
  487. * and, if available, the slab name, return address, and stack trace from
  488. * the allocation and last free path of that object.
  489. *
  490. * Return: %true if the pointer is to a not-yet-freed object from
  491. * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
  492. * is to an already-freed object, and %false otherwise.
  493. */
  494. bool kmem_dump_obj(void *object)
  495. {
  496. char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc";
  497. int i;
  498. struct slab *slab;
  499. unsigned long ptroffset;
  500. struct kmem_obj_info kp = { };
  501. /* Some arches consider ZERO_SIZE_PTR to be a valid address. */
  502. if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
  503. return false;
  504. slab = virt_to_slab(object);
  505. if (!slab)
  506. return false;
  507. kmem_obj_info(&kp, object, slab);
  508. if (kp.kp_slab_cache)
  509. pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name);
  510. else
  511. pr_cont(" slab%s", cp);
  512. if (is_kfence_address(object))
  513. pr_cont(" (kfence)");
  514. if (kp.kp_objp)
  515. pr_cont(" start %px", kp.kp_objp);
  516. if (kp.kp_data_offset)
  517. pr_cont(" data offset %lu", kp.kp_data_offset);
  518. if (kp.kp_objp) {
  519. ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset;
  520. pr_cont(" pointer offset %lu", ptroffset);
  521. }
  522. if (kp.kp_slab_cache && kp.kp_slab_cache->object_size)
  523. pr_cont(" size %u", kp.kp_slab_cache->object_size);
  524. if (kp.kp_ret)
  525. pr_cont(" allocated at %pS\n", kp.kp_ret);
  526. else
  527. pr_cont("\n");
  528. for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) {
  529. if (!kp.kp_stack[i])
  530. break;
  531. pr_info(" %pS\n", kp.kp_stack[i]);
  532. }
  533. if (kp.kp_free_stack[0])
  534. pr_cont(" Free path:\n");
  535. for (i = 0; i < ARRAY_SIZE(kp.kp_free_stack); i++) {
  536. if (!kp.kp_free_stack[i])
  537. break;
  538. pr_info(" %pS\n", kp.kp_free_stack[i]);
  539. }
  540. return true;
  541. }
  542. EXPORT_SYMBOL_GPL(kmem_dump_obj);
  543. #endif
  544. /* Create a cache during boot when no slab services are available yet */
  545. void __init create_boot_cache(struct kmem_cache *s, const char *name,
  546. unsigned int size, slab_flags_t flags,
  547. unsigned int useroffset, unsigned int usersize)
  548. {
  549. int err;
  550. unsigned int align = ARCH_KMALLOC_MINALIGN;
  551. struct kmem_cache_args kmem_args = {};
  552. /*
  553. * kmalloc caches guarantee alignment of at least the largest
  554. * power-of-two divisor of the size. For power-of-two sizes,
  555. * it is the size itself.
  556. */
  557. if (flags & SLAB_KMALLOC)
  558. align = max(align, 1U << (ffs(size) - 1));
  559. kmem_args.align = calculate_alignment(flags, align, size);
  560. #ifdef CONFIG_HARDENED_USERCOPY
  561. kmem_args.useroffset = useroffset;
  562. kmem_args.usersize = usersize;
  563. #endif
  564. err = do_kmem_cache_create(s, name, size, &kmem_args, flags);
  565. if (err)
  566. panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
  567. name, size, err);
  568. s->refcount = -1; /* Exempt from merging for now */
  569. }
  570. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  571. unsigned int size,
  572. slab_flags_t flags)
  573. {
  574. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  575. if (!s)
  576. panic("Out of memory when creating slab %s\n", name);
  577. create_boot_cache(s, name, size, flags | SLAB_KMALLOC, 0, size);
  578. list_add(&s->list, &slab_caches);
  579. s->refcount = 1;
  580. return s;
  581. }
  582. kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES] __ro_after_init =
  583. { /* initialization for https://llvm.org/pr42570 */ };
  584. EXPORT_SYMBOL(kmalloc_caches);
  585. #ifdef CONFIG_RANDOM_KMALLOC_CACHES
  586. unsigned long random_kmalloc_seed __ro_after_init;
  587. EXPORT_SYMBOL(random_kmalloc_seed);
  588. #endif
  589. /*
  590. * Conversion table for small slabs sizes / 8 to the index in the
  591. * kmalloc array. This is necessary for slabs < 192 since we have non power
  592. * of two cache sizes there. The size of larger slabs can be determined using
  593. * fls.
  594. */
  595. u8 kmalloc_size_index[24] __ro_after_init = {
  596. 3, /* 8 */
  597. 4, /* 16 */
  598. 5, /* 24 */
  599. 5, /* 32 */
  600. 6, /* 40 */
  601. 6, /* 48 */
  602. 6, /* 56 */
  603. 6, /* 64 */
  604. 1, /* 72 */
  605. 1, /* 80 */
  606. 1, /* 88 */
  607. 1, /* 96 */
  608. 7, /* 104 */
  609. 7, /* 112 */
  610. 7, /* 120 */
  611. 7, /* 128 */
  612. 2, /* 136 */
  613. 2, /* 144 */
  614. 2, /* 152 */
  615. 2, /* 160 */
  616. 2, /* 168 */
  617. 2, /* 176 */
  618. 2, /* 184 */
  619. 2 /* 192 */
  620. };
  621. size_t kmalloc_size_roundup(size_t size)
  622. {
  623. if (size && size <= KMALLOC_MAX_CACHE_SIZE) {
  624. /*
  625. * The flags don't matter since size_index is common to all.
  626. * Neither does the caller for just getting ->object_size.
  627. */
  628. return kmalloc_slab(size, NULL, GFP_KERNEL, 0)->object_size;
  629. }
  630. /* Above the smaller buckets, size is a multiple of page size. */
  631. if (size && size <= KMALLOC_MAX_SIZE)
  632. return PAGE_SIZE << get_order(size);
  633. /*
  634. * Return 'size' for 0 - kmalloc() returns ZERO_SIZE_PTR
  635. * and very large size - kmalloc() may fail.
  636. */
  637. return size;
  638. }
  639. EXPORT_SYMBOL(kmalloc_size_roundup);
  640. #ifdef CONFIG_ZONE_DMA
  641. #define KMALLOC_DMA_NAME(sz) .name[KMALLOC_DMA] = "dma-kmalloc-" #sz,
  642. #else
  643. #define KMALLOC_DMA_NAME(sz)
  644. #endif
  645. #ifdef CONFIG_MEMCG
  646. #define KMALLOC_CGROUP_NAME(sz) .name[KMALLOC_CGROUP] = "kmalloc-cg-" #sz,
  647. #else
  648. #define KMALLOC_CGROUP_NAME(sz)
  649. #endif
  650. #ifndef CONFIG_SLUB_TINY
  651. #define KMALLOC_RCL_NAME(sz) .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #sz,
  652. #else
  653. #define KMALLOC_RCL_NAME(sz)
  654. #endif
  655. #ifdef CONFIG_RANDOM_KMALLOC_CACHES
  656. #define __KMALLOC_RANDOM_CONCAT(a, b) a ## b
  657. #define KMALLOC_RANDOM_NAME(N, sz) __KMALLOC_RANDOM_CONCAT(KMA_RAND_, N)(sz)
  658. #define KMA_RAND_1(sz) .name[KMALLOC_RANDOM_START + 1] = "kmalloc-rnd-01-" #sz,
  659. #define KMA_RAND_2(sz) KMA_RAND_1(sz) .name[KMALLOC_RANDOM_START + 2] = "kmalloc-rnd-02-" #sz,
  660. #define KMA_RAND_3(sz) KMA_RAND_2(sz) .name[KMALLOC_RANDOM_START + 3] = "kmalloc-rnd-03-" #sz,
  661. #define KMA_RAND_4(sz) KMA_RAND_3(sz) .name[KMALLOC_RANDOM_START + 4] = "kmalloc-rnd-04-" #sz,
  662. #define KMA_RAND_5(sz) KMA_RAND_4(sz) .name[KMALLOC_RANDOM_START + 5] = "kmalloc-rnd-05-" #sz,
  663. #define KMA_RAND_6(sz) KMA_RAND_5(sz) .name[KMALLOC_RANDOM_START + 6] = "kmalloc-rnd-06-" #sz,
  664. #define KMA_RAND_7(sz) KMA_RAND_6(sz) .name[KMALLOC_RANDOM_START + 7] = "kmalloc-rnd-07-" #sz,
  665. #define KMA_RAND_8(sz) KMA_RAND_7(sz) .name[KMALLOC_RANDOM_START + 8] = "kmalloc-rnd-08-" #sz,
  666. #define KMA_RAND_9(sz) KMA_RAND_8(sz) .name[KMALLOC_RANDOM_START + 9] = "kmalloc-rnd-09-" #sz,
  667. #define KMA_RAND_10(sz) KMA_RAND_9(sz) .name[KMALLOC_RANDOM_START + 10] = "kmalloc-rnd-10-" #sz,
  668. #define KMA_RAND_11(sz) KMA_RAND_10(sz) .name[KMALLOC_RANDOM_START + 11] = "kmalloc-rnd-11-" #sz,
  669. #define KMA_RAND_12(sz) KMA_RAND_11(sz) .name[KMALLOC_RANDOM_START + 12] = "kmalloc-rnd-12-" #sz,
  670. #define KMA_RAND_13(sz) KMA_RAND_12(sz) .name[KMALLOC_RANDOM_START + 13] = "kmalloc-rnd-13-" #sz,
  671. #define KMA_RAND_14(sz) KMA_RAND_13(sz) .name[KMALLOC_RANDOM_START + 14] = "kmalloc-rnd-14-" #sz,
  672. #define KMA_RAND_15(sz) KMA_RAND_14(sz) .name[KMALLOC_RANDOM_START + 15] = "kmalloc-rnd-15-" #sz,
  673. #else // CONFIG_RANDOM_KMALLOC_CACHES
  674. #define KMALLOC_RANDOM_NAME(N, sz)
  675. #endif
  676. #define INIT_KMALLOC_INFO(__size, __short_size) \
  677. { \
  678. .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
  679. KMALLOC_RCL_NAME(__short_size) \
  680. KMALLOC_CGROUP_NAME(__short_size) \
  681. KMALLOC_DMA_NAME(__short_size) \
  682. KMALLOC_RANDOM_NAME(RANDOM_KMALLOC_CACHES_NR, __short_size) \
  683. .size = __size, \
  684. }
  685. /*
  686. * kmalloc_info[] is to make slab_debug=,kmalloc-xx option work at boot time.
  687. * kmalloc_index() supports up to 2^21=2MB, so the final entry of the table is
  688. * kmalloc-2M.
  689. */
  690. const struct kmalloc_info_struct kmalloc_info[] __initconst = {
  691. INIT_KMALLOC_INFO(0, 0),
  692. INIT_KMALLOC_INFO(96, 96),
  693. INIT_KMALLOC_INFO(192, 192),
  694. INIT_KMALLOC_INFO(8, 8),
  695. INIT_KMALLOC_INFO(16, 16),
  696. INIT_KMALLOC_INFO(32, 32),
  697. INIT_KMALLOC_INFO(64, 64),
  698. INIT_KMALLOC_INFO(128, 128),
  699. INIT_KMALLOC_INFO(256, 256),
  700. INIT_KMALLOC_INFO(512, 512),
  701. INIT_KMALLOC_INFO(1024, 1k),
  702. INIT_KMALLOC_INFO(2048, 2k),
  703. INIT_KMALLOC_INFO(4096, 4k),
  704. INIT_KMALLOC_INFO(8192, 8k),
  705. INIT_KMALLOC_INFO(16384, 16k),
  706. INIT_KMALLOC_INFO(32768, 32k),
  707. INIT_KMALLOC_INFO(65536, 64k),
  708. INIT_KMALLOC_INFO(131072, 128k),
  709. INIT_KMALLOC_INFO(262144, 256k),
  710. INIT_KMALLOC_INFO(524288, 512k),
  711. INIT_KMALLOC_INFO(1048576, 1M),
  712. INIT_KMALLOC_INFO(2097152, 2M)
  713. };
  714. /*
  715. * Patch up the size_index table if we have strange large alignment
  716. * requirements for the kmalloc array. This is only the case for
  717. * MIPS it seems. The standard arches will not generate any code here.
  718. *
  719. * Largest permitted alignment is 256 bytes due to the way we
  720. * handle the index determination for the smaller caches.
  721. *
  722. * Make sure that nothing crazy happens if someone starts tinkering
  723. * around with ARCH_KMALLOC_MINALIGN
  724. */
  725. void __init setup_kmalloc_cache_index_table(void)
  726. {
  727. unsigned int i;
  728. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  729. !is_power_of_2(KMALLOC_MIN_SIZE));
  730. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  731. unsigned int elem = size_index_elem(i);
  732. if (elem >= ARRAY_SIZE(kmalloc_size_index))
  733. break;
  734. kmalloc_size_index[elem] = KMALLOC_SHIFT_LOW;
  735. }
  736. if (KMALLOC_MIN_SIZE >= 64) {
  737. /*
  738. * The 96 byte sized cache is not used if the alignment
  739. * is 64 byte.
  740. */
  741. for (i = 64 + 8; i <= 96; i += 8)
  742. kmalloc_size_index[size_index_elem(i)] = 7;
  743. }
  744. if (KMALLOC_MIN_SIZE >= 128) {
  745. /*
  746. * The 192 byte sized cache is not used if the alignment
  747. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  748. * instead.
  749. */
  750. for (i = 128 + 8; i <= 192; i += 8)
  751. kmalloc_size_index[size_index_elem(i)] = 8;
  752. }
  753. }
  754. static unsigned int __kmalloc_minalign(void)
  755. {
  756. unsigned int minalign = dma_get_cache_alignment();
  757. if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) &&
  758. is_swiotlb_allocated())
  759. minalign = ARCH_KMALLOC_MINALIGN;
  760. return max(minalign, arch_slab_minalign());
  761. }
  762. static void __init
  763. new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
  764. {
  765. slab_flags_t flags = 0;
  766. unsigned int minalign = __kmalloc_minalign();
  767. unsigned int aligned_size = kmalloc_info[idx].size;
  768. int aligned_idx = idx;
  769. if ((KMALLOC_RECLAIM != KMALLOC_NORMAL) && (type == KMALLOC_RECLAIM)) {
  770. flags |= SLAB_RECLAIM_ACCOUNT;
  771. } else if (IS_ENABLED(CONFIG_MEMCG) && (type == KMALLOC_CGROUP)) {
  772. if (mem_cgroup_kmem_disabled()) {
  773. kmalloc_caches[type][idx] = kmalloc_caches[KMALLOC_NORMAL][idx];
  774. return;
  775. }
  776. flags |= SLAB_ACCOUNT;
  777. } else if (IS_ENABLED(CONFIG_ZONE_DMA) && (type == KMALLOC_DMA)) {
  778. flags |= SLAB_CACHE_DMA;
  779. }
  780. #ifdef CONFIG_RANDOM_KMALLOC_CACHES
  781. if (type >= KMALLOC_RANDOM_START && type <= KMALLOC_RANDOM_END)
  782. flags |= SLAB_NO_MERGE;
  783. #endif
  784. /*
  785. * If CONFIG_MEMCG is enabled, disable cache merging for
  786. * KMALLOC_NORMAL caches.
  787. */
  788. if (IS_ENABLED(CONFIG_MEMCG) && (type == KMALLOC_NORMAL))
  789. flags |= SLAB_NO_MERGE;
  790. if (minalign > ARCH_KMALLOC_MINALIGN) {
  791. aligned_size = ALIGN(aligned_size, minalign);
  792. aligned_idx = __kmalloc_index(aligned_size, false);
  793. }
  794. if (!kmalloc_caches[type][aligned_idx])
  795. kmalloc_caches[type][aligned_idx] = create_kmalloc_cache(
  796. kmalloc_info[aligned_idx].name[type],
  797. aligned_size, flags);
  798. if (idx != aligned_idx)
  799. kmalloc_caches[type][idx] = kmalloc_caches[type][aligned_idx];
  800. }
  801. /*
  802. * Create the kmalloc array. Some of the regular kmalloc arrays
  803. * may already have been created because they were needed to
  804. * enable allocations for slab creation.
  805. */
  806. void __init create_kmalloc_caches(void)
  807. {
  808. int i;
  809. enum kmalloc_cache_type type;
  810. /*
  811. * Including KMALLOC_CGROUP if CONFIG_MEMCG defined
  812. */
  813. for (type = KMALLOC_NORMAL; type < NR_KMALLOC_TYPES; type++) {
  814. /* Caches that are NOT of the two-to-the-power-of size. */
  815. if (KMALLOC_MIN_SIZE <= 32)
  816. new_kmalloc_cache(1, type);
  817. if (KMALLOC_MIN_SIZE <= 64)
  818. new_kmalloc_cache(2, type);
  819. /* Caches that are of the two-to-the-power-of size. */
  820. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
  821. new_kmalloc_cache(i, type);
  822. }
  823. #ifdef CONFIG_RANDOM_KMALLOC_CACHES
  824. random_kmalloc_seed = get_random_u64();
  825. #endif
  826. /* Kmalloc array is now usable */
  827. slab_state = UP;
  828. if (IS_ENABLED(CONFIG_SLAB_BUCKETS))
  829. kmem_buckets_cache = kmem_cache_create("kmalloc_buckets",
  830. sizeof(kmem_buckets),
  831. 0, SLAB_NO_MERGE, NULL);
  832. }
  833. /**
  834. * __ksize -- Report full size of underlying allocation
  835. * @object: pointer to the object
  836. *
  837. * This should only be used internally to query the true size of allocations.
  838. * It is not meant to be a way to discover the usable size of an allocation
  839. * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond
  840. * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS,
  841. * and/or FORTIFY_SOURCE.
  842. *
  843. * Return: size of the actual memory used by @object in bytes
  844. */
  845. size_t __ksize(const void *object)
  846. {
  847. struct folio *folio;
  848. if (unlikely(object == ZERO_SIZE_PTR))
  849. return 0;
  850. folio = virt_to_folio(object);
  851. if (unlikely(!folio_test_slab(folio))) {
  852. if (WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE))
  853. return 0;
  854. if (WARN_ON(object != folio_address(folio)))
  855. return 0;
  856. return folio_size(folio);
  857. }
  858. #ifdef CONFIG_SLUB_DEBUG
  859. skip_orig_size_check(folio_slab(folio)->slab_cache, object);
  860. #endif
  861. return slab_ksize(folio_slab(folio)->slab_cache);
  862. }
  863. gfp_t kmalloc_fix_flags(gfp_t flags)
  864. {
  865. gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
  866. flags &= ~GFP_SLAB_BUG_MASK;
  867. pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
  868. invalid_mask, &invalid_mask, flags, &flags);
  869. dump_stack();
  870. return flags;
  871. }
  872. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  873. /* Randomize a generic freelist */
  874. static void freelist_randomize(unsigned int *list,
  875. unsigned int count)
  876. {
  877. unsigned int rand;
  878. unsigned int i;
  879. for (i = 0; i < count; i++)
  880. list[i] = i;
  881. /* Fisher-Yates shuffle */
  882. for (i = count - 1; i > 0; i--) {
  883. rand = get_random_u32_below(i + 1);
  884. swap(list[i], list[rand]);
  885. }
  886. }
  887. /* Create a random sequence per cache */
  888. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  889. gfp_t gfp)
  890. {
  891. if (count < 2 || cachep->random_seq)
  892. return 0;
  893. cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
  894. if (!cachep->random_seq)
  895. return -ENOMEM;
  896. freelist_randomize(cachep->random_seq, count);
  897. return 0;
  898. }
  899. /* Destroy the per-cache random freelist sequence */
  900. void cache_random_seq_destroy(struct kmem_cache *cachep)
  901. {
  902. kfree(cachep->random_seq);
  903. cachep->random_seq = NULL;
  904. }
  905. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  906. #ifdef CONFIG_SLUB_DEBUG
  907. #define SLABINFO_RIGHTS (0400)
  908. static void print_slabinfo_header(struct seq_file *m)
  909. {
  910. /*
  911. * Output format version, so at least we can change it
  912. * without _too_ many complaints.
  913. */
  914. seq_puts(m, "slabinfo - version: 2.1\n");
  915. seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  916. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  917. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  918. seq_putc(m, '\n');
  919. }
  920. static void *slab_start(struct seq_file *m, loff_t *pos)
  921. {
  922. mutex_lock(&slab_mutex);
  923. return seq_list_start(&slab_caches, *pos);
  924. }
  925. static void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  926. {
  927. return seq_list_next(p, &slab_caches, pos);
  928. }
  929. static void slab_stop(struct seq_file *m, void *p)
  930. {
  931. mutex_unlock(&slab_mutex);
  932. }
  933. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  934. {
  935. struct slabinfo sinfo;
  936. memset(&sinfo, 0, sizeof(sinfo));
  937. get_slabinfo(s, &sinfo);
  938. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  939. s->name, sinfo.active_objs, sinfo.num_objs, s->size,
  940. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  941. seq_printf(m, " : tunables %4u %4u %4u",
  942. sinfo.limit, sinfo.batchcount, sinfo.shared);
  943. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  944. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  945. seq_putc(m, '\n');
  946. }
  947. static int slab_show(struct seq_file *m, void *p)
  948. {
  949. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  950. if (p == slab_caches.next)
  951. print_slabinfo_header(m);
  952. cache_show(s, m);
  953. return 0;
  954. }
  955. void dump_unreclaimable_slab(void)
  956. {
  957. struct kmem_cache *s;
  958. struct slabinfo sinfo;
  959. /*
  960. * Here acquiring slab_mutex is risky since we don't prefer to get
  961. * sleep in oom path. But, without mutex hold, it may introduce a
  962. * risk of crash.
  963. * Use mutex_trylock to protect the list traverse, dump nothing
  964. * without acquiring the mutex.
  965. */
  966. if (!mutex_trylock(&slab_mutex)) {
  967. pr_warn("excessive unreclaimable slab but cannot dump stats\n");
  968. return;
  969. }
  970. pr_info("Unreclaimable slab info:\n");
  971. pr_info("Name Used Total\n");
  972. list_for_each_entry(s, &slab_caches, list) {
  973. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  974. continue;
  975. get_slabinfo(s, &sinfo);
  976. if (sinfo.num_objs > 0)
  977. pr_info("%-17s %10luKB %10luKB\n", s->name,
  978. (sinfo.active_objs * s->size) / 1024,
  979. (sinfo.num_objs * s->size) / 1024);
  980. }
  981. mutex_unlock(&slab_mutex);
  982. }
  983. /*
  984. * slabinfo_op - iterator that generates /proc/slabinfo
  985. *
  986. * Output layout:
  987. * cache-name
  988. * num-active-objs
  989. * total-objs
  990. * object size
  991. * num-active-slabs
  992. * total-slabs
  993. * num-pages-per-slab
  994. * + further values on SMP and with statistics enabled
  995. */
  996. static const struct seq_operations slabinfo_op = {
  997. .start = slab_start,
  998. .next = slab_next,
  999. .stop = slab_stop,
  1000. .show = slab_show,
  1001. };
  1002. static int slabinfo_open(struct inode *inode, struct file *file)
  1003. {
  1004. return seq_open(file, &slabinfo_op);
  1005. }
  1006. static const struct proc_ops slabinfo_proc_ops = {
  1007. .proc_flags = PROC_ENTRY_PERMANENT,
  1008. .proc_open = slabinfo_open,
  1009. .proc_read = seq_read,
  1010. .proc_lseek = seq_lseek,
  1011. .proc_release = seq_release,
  1012. };
  1013. static int __init slab_proc_init(void)
  1014. {
  1015. proc_create("slabinfo", SLABINFO_RIGHTS, NULL, &slabinfo_proc_ops);
  1016. return 0;
  1017. }
  1018. module_init(slab_proc_init);
  1019. #endif /* CONFIG_SLUB_DEBUG */
  1020. static __always_inline __realloc_size(2) void *
  1021. __do_krealloc(const void *p, size_t new_size, gfp_t flags)
  1022. {
  1023. void *ret;
  1024. size_t ks;
  1025. /* Check for double-free before calling ksize. */
  1026. if (likely(!ZERO_OR_NULL_PTR(p))) {
  1027. if (!kasan_check_byte(p))
  1028. return NULL;
  1029. ks = ksize(p);
  1030. } else
  1031. ks = 0;
  1032. /* If the object still fits, repoison it precisely. */
  1033. if (ks >= new_size) {
  1034. /* Zero out spare memory. */
  1035. if (want_init_on_alloc(flags)) {
  1036. kasan_disable_current();
  1037. memset(kasan_reset_tag(p) + new_size, 0, ks - new_size);
  1038. kasan_enable_current();
  1039. }
  1040. p = kasan_krealloc((void *)p, new_size, flags);
  1041. return (void *)p;
  1042. }
  1043. ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
  1044. if (ret && p) {
  1045. /* Disable KASAN checks as the object's redzone is accessed. */
  1046. kasan_disable_current();
  1047. memcpy(ret, kasan_reset_tag(p), ks);
  1048. kasan_enable_current();
  1049. }
  1050. return ret;
  1051. }
  1052. /**
  1053. * krealloc - reallocate memory. The contents will remain unchanged.
  1054. * @p: object to reallocate memory for.
  1055. * @new_size: how many bytes of memory are required.
  1056. * @flags: the type of memory to allocate.
  1057. *
  1058. * If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size
  1059. * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
  1060. *
  1061. * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
  1062. * initial memory allocation, every subsequent call to this API for the same
  1063. * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
  1064. * __GFP_ZERO is not fully honored by this API.
  1065. *
  1066. * This is the case, since krealloc() only knows about the bucket size of an
  1067. * allocation (but not the exact size it was allocated with) and hence
  1068. * implements the following semantics for shrinking and growing buffers with
  1069. * __GFP_ZERO.
  1070. *
  1071. * new bucket
  1072. * 0 size size
  1073. * |--------|----------------|
  1074. * | keep | zero |
  1075. *
  1076. * In any case, the contents of the object pointed to are preserved up to the
  1077. * lesser of the new and old sizes.
  1078. *
  1079. * Return: pointer to the allocated memory or %NULL in case of error
  1080. */
  1081. void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
  1082. {
  1083. void *ret;
  1084. if (unlikely(!new_size)) {
  1085. kfree(p);
  1086. return ZERO_SIZE_PTR;
  1087. }
  1088. ret = __do_krealloc(p, new_size, flags);
  1089. if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
  1090. kfree(p);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL(krealloc_noprof);
  1094. /**
  1095. * kfree_sensitive - Clear sensitive information in memory before freeing
  1096. * @p: object to free memory of
  1097. *
  1098. * The memory of the object @p points to is zeroed before freed.
  1099. * If @p is %NULL, kfree_sensitive() does nothing.
  1100. *
  1101. * Note: this function zeroes the whole allocated buffer which can be a good
  1102. * deal bigger than the requested buffer size passed to kmalloc(). So be
  1103. * careful when using this function in performance sensitive code.
  1104. */
  1105. void kfree_sensitive(const void *p)
  1106. {
  1107. size_t ks;
  1108. void *mem = (void *)p;
  1109. ks = ksize(mem);
  1110. if (ks) {
  1111. kasan_unpoison_range(mem, ks);
  1112. memzero_explicit(mem, ks);
  1113. }
  1114. kfree(mem);
  1115. }
  1116. EXPORT_SYMBOL(kfree_sensitive);
  1117. size_t ksize(const void *objp)
  1118. {
  1119. /*
  1120. * We need to first check that the pointer to the object is valid.
  1121. * The KASAN report printed from ksize() is more useful, then when
  1122. * it's printed later when the behaviour could be undefined due to
  1123. * a potential use-after-free or double-free.
  1124. *
  1125. * We use kasan_check_byte(), which is supported for the hardware
  1126. * tag-based KASAN mode, unlike kasan_check_read/write().
  1127. *
  1128. * If the pointed to memory is invalid, we return 0 to avoid users of
  1129. * ksize() writing to and potentially corrupting the memory region.
  1130. *
  1131. * We want to perform the check before __ksize(), to avoid potentially
  1132. * crashing in __ksize() due to accessing invalid metadata.
  1133. */
  1134. if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp))
  1135. return 0;
  1136. return kfence_ksize(objp) ?: __ksize(objp);
  1137. }
  1138. EXPORT_SYMBOL(ksize);
  1139. /* Tracepoints definitions. */
  1140. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1141. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1142. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1143. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);