slab_common.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * Slab allocator functions that are independent of the allocator strategy
  3. *
  4. * (C) 2012 Christoph Lameter <cl@linux.com>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/mm.h>
  8. #include <linux/poison.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/memory.h>
  11. #include <linux/compiler.h>
  12. #include <linux/module.h>
  13. #include <linux/cpu.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/proc_fs.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/page.h>
  20. #include <linux/memcontrol.h>
  21. #define CREATE_TRACE_POINTS
  22. #include <trace/events/kmem.h>
  23. #include "slab.h"
  24. enum slab_state slab_state;
  25. LIST_HEAD(slab_caches);
  26. DEFINE_MUTEX(slab_mutex);
  27. struct kmem_cache *kmem_cache;
  28. /*
  29. * Set of flags that will prevent slab merging
  30. */
  31. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  32. SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
  33. SLAB_FAILSLAB)
  34. #define SLAB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  35. SLAB_CACHE_DMA | SLAB_NOTRACK)
  36. /*
  37. * Merge control. If this is set then no merging of slab caches will occur.
  38. * (Could be removed. This was introduced to pacify the merge skeptics.)
  39. */
  40. static int slab_nomerge;
  41. static int __init setup_slab_nomerge(char *str)
  42. {
  43. slab_nomerge = 1;
  44. return 1;
  45. }
  46. #ifdef CONFIG_SLUB
  47. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  48. #endif
  49. __setup("slab_nomerge", setup_slab_nomerge);
  50. /*
  51. * Determine the size of a slab object
  52. */
  53. unsigned int kmem_cache_size(struct kmem_cache *s)
  54. {
  55. return s->object_size;
  56. }
  57. EXPORT_SYMBOL(kmem_cache_size);
  58. #ifdef CONFIG_DEBUG_VM
  59. static int kmem_cache_sanity_check(const char *name, size_t size)
  60. {
  61. struct kmem_cache *s = NULL;
  62. if (!name || in_interrupt() || size < sizeof(void *) ||
  63. size > KMALLOC_MAX_SIZE) {
  64. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  65. return -EINVAL;
  66. }
  67. list_for_each_entry(s, &slab_caches, list) {
  68. char tmp;
  69. int res;
  70. /*
  71. * This happens when the module gets unloaded and doesn't
  72. * destroy its slab cache and no-one else reuses the vmalloc
  73. * area of the module. Print a warning.
  74. */
  75. res = probe_kernel_address(s->name, tmp);
  76. if (res) {
  77. pr_err("Slab cache with size %d has lost its name\n",
  78. s->object_size);
  79. continue;
  80. }
  81. }
  82. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  83. return 0;
  84. }
  85. #else
  86. static inline int kmem_cache_sanity_check(const char *name, size_t size)
  87. {
  88. return 0;
  89. }
  90. #endif
  91. #ifdef CONFIG_MEMCG_KMEM
  92. static int memcg_alloc_cache_params(struct mem_cgroup *memcg,
  93. struct kmem_cache *s, struct kmem_cache *root_cache)
  94. {
  95. size_t size;
  96. if (!memcg_kmem_enabled())
  97. return 0;
  98. if (!memcg) {
  99. size = offsetof(struct memcg_cache_params, memcg_caches);
  100. size += memcg_limited_groups_array_size * sizeof(void *);
  101. } else
  102. size = sizeof(struct memcg_cache_params);
  103. s->memcg_params = kzalloc(size, GFP_KERNEL);
  104. if (!s->memcg_params)
  105. return -ENOMEM;
  106. if (memcg) {
  107. s->memcg_params->memcg = memcg;
  108. s->memcg_params->root_cache = root_cache;
  109. } else
  110. s->memcg_params->is_root_cache = true;
  111. return 0;
  112. }
  113. static void memcg_free_cache_params(struct kmem_cache *s)
  114. {
  115. kfree(s->memcg_params);
  116. }
  117. static int memcg_update_cache_params(struct kmem_cache *s, int num_memcgs)
  118. {
  119. int size;
  120. struct memcg_cache_params *new_params, *cur_params;
  121. BUG_ON(!is_root_cache(s));
  122. size = offsetof(struct memcg_cache_params, memcg_caches);
  123. size += num_memcgs * sizeof(void *);
  124. new_params = kzalloc(size, GFP_KERNEL);
  125. if (!new_params)
  126. return -ENOMEM;
  127. cur_params = s->memcg_params;
  128. memcpy(new_params->memcg_caches, cur_params->memcg_caches,
  129. memcg_limited_groups_array_size * sizeof(void *));
  130. new_params->is_root_cache = true;
  131. rcu_assign_pointer(s->memcg_params, new_params);
  132. if (cur_params)
  133. kfree_rcu(cur_params, rcu_head);
  134. return 0;
  135. }
  136. int memcg_update_all_caches(int num_memcgs)
  137. {
  138. struct kmem_cache *s;
  139. int ret = 0;
  140. mutex_lock(&slab_mutex);
  141. list_for_each_entry(s, &slab_caches, list) {
  142. if (!is_root_cache(s))
  143. continue;
  144. ret = memcg_update_cache_params(s, num_memcgs);
  145. /*
  146. * Instead of freeing the memory, we'll just leave the caches
  147. * up to this point in an updated state.
  148. */
  149. if (ret)
  150. goto out;
  151. }
  152. memcg_update_array_size(num_memcgs);
  153. out:
  154. mutex_unlock(&slab_mutex);
  155. return ret;
  156. }
  157. #else
  158. static inline int memcg_alloc_cache_params(struct mem_cgroup *memcg,
  159. struct kmem_cache *s, struct kmem_cache *root_cache)
  160. {
  161. return 0;
  162. }
  163. static inline void memcg_free_cache_params(struct kmem_cache *s)
  164. {
  165. }
  166. #endif /* CONFIG_MEMCG_KMEM */
  167. /*
  168. * Find a mergeable slab cache
  169. */
  170. int slab_unmergeable(struct kmem_cache *s)
  171. {
  172. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  173. return 1;
  174. if (!is_root_cache(s))
  175. return 1;
  176. if (s->ctor)
  177. return 1;
  178. /*
  179. * We may have set a slab to be unmergeable during bootstrap.
  180. */
  181. if (s->refcount < 0)
  182. return 1;
  183. return 0;
  184. }
  185. struct kmem_cache *find_mergeable(size_t size, size_t align,
  186. unsigned long flags, const char *name, void (*ctor)(void *))
  187. {
  188. struct kmem_cache *s;
  189. if (slab_nomerge || (flags & SLAB_NEVER_MERGE))
  190. return NULL;
  191. if (ctor)
  192. return NULL;
  193. size = ALIGN(size, sizeof(void *));
  194. align = calculate_alignment(flags, align, size);
  195. size = ALIGN(size, align);
  196. flags = kmem_cache_flags(size, flags, name, NULL);
  197. list_for_each_entry(s, &slab_caches, list) {
  198. if (slab_unmergeable(s))
  199. continue;
  200. if (size > s->size)
  201. continue;
  202. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  203. continue;
  204. /*
  205. * Check if alignment is compatible.
  206. * Courtesy of Adrian Drzewiecki
  207. */
  208. if ((s->size & ~(align - 1)) != s->size)
  209. continue;
  210. if (s->size - size >= sizeof(void *))
  211. continue;
  212. if (IS_ENABLED(CONFIG_SLAB) && align &&
  213. (align > s->align || s->align % align))
  214. continue;
  215. return s;
  216. }
  217. return NULL;
  218. }
  219. /*
  220. * Figure out what the alignment of the objects will be given a set of
  221. * flags, a user specified alignment and the size of the objects.
  222. */
  223. unsigned long calculate_alignment(unsigned long flags,
  224. unsigned long align, unsigned long size)
  225. {
  226. /*
  227. * If the user wants hardware cache aligned objects then follow that
  228. * suggestion if the object is sufficiently large.
  229. *
  230. * The hardware cache alignment cannot override the specified
  231. * alignment though. If that is greater then use it.
  232. */
  233. if (flags & SLAB_HWCACHE_ALIGN) {
  234. unsigned long ralign = cache_line_size();
  235. while (size <= ralign / 2)
  236. ralign /= 2;
  237. align = max(align, ralign);
  238. }
  239. if (align < ARCH_SLAB_MINALIGN)
  240. align = ARCH_SLAB_MINALIGN;
  241. return ALIGN(align, sizeof(void *));
  242. }
  243. static struct kmem_cache *
  244. do_kmem_cache_create(char *name, size_t object_size, size_t size, size_t align,
  245. unsigned long flags, void (*ctor)(void *),
  246. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  247. {
  248. struct kmem_cache *s;
  249. int err;
  250. err = -ENOMEM;
  251. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  252. if (!s)
  253. goto out;
  254. s->name = name;
  255. s->object_size = object_size;
  256. s->size = size;
  257. s->align = align;
  258. s->ctor = ctor;
  259. err = memcg_alloc_cache_params(memcg, s, root_cache);
  260. if (err)
  261. goto out_free_cache;
  262. err = __kmem_cache_create(s, flags);
  263. if (err)
  264. goto out_free_cache;
  265. s->refcount = 1;
  266. list_add(&s->list, &slab_caches);
  267. out:
  268. if (err)
  269. return ERR_PTR(err);
  270. return s;
  271. out_free_cache:
  272. memcg_free_cache_params(s);
  273. kfree(s);
  274. goto out;
  275. }
  276. /*
  277. * kmem_cache_create - Create a cache.
  278. * @name: A string which is used in /proc/slabinfo to identify this cache.
  279. * @size: The size of objects to be created in this cache.
  280. * @align: The required alignment for the objects.
  281. * @flags: SLAB flags
  282. * @ctor: A constructor for the objects.
  283. *
  284. * Returns a ptr to the cache on success, NULL on failure.
  285. * Cannot be called within a interrupt, but can be interrupted.
  286. * The @ctor is run when new pages are allocated by the cache.
  287. *
  288. * The flags are
  289. *
  290. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  291. * to catch references to uninitialised memory.
  292. *
  293. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  294. * for buffer overruns.
  295. *
  296. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  297. * cacheline. This can be beneficial if you're counting cycles as closely
  298. * as davem.
  299. */
  300. struct kmem_cache *
  301. kmem_cache_create(const char *name, size_t size, size_t align,
  302. unsigned long flags, void (*ctor)(void *))
  303. {
  304. struct kmem_cache *s;
  305. char *cache_name;
  306. int err;
  307. get_online_cpus();
  308. get_online_mems();
  309. mutex_lock(&slab_mutex);
  310. err = kmem_cache_sanity_check(name, size);
  311. if (err) {
  312. s = NULL; /* suppress uninit var warning */
  313. goto out_unlock;
  314. }
  315. /*
  316. * Some allocators will constraint the set of valid flags to a subset
  317. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  318. * case, and we'll just provide them with a sanitized version of the
  319. * passed flags.
  320. */
  321. flags &= CACHE_CREATE_MASK;
  322. s = __kmem_cache_alias(name, size, align, flags, ctor);
  323. if (s)
  324. goto out_unlock;
  325. cache_name = kstrdup(name, GFP_KERNEL);
  326. if (!cache_name) {
  327. err = -ENOMEM;
  328. goto out_unlock;
  329. }
  330. s = do_kmem_cache_create(cache_name, size, size,
  331. calculate_alignment(flags, align, size),
  332. flags, ctor, NULL, NULL);
  333. if (IS_ERR(s)) {
  334. err = PTR_ERR(s);
  335. kfree(cache_name);
  336. }
  337. out_unlock:
  338. mutex_unlock(&slab_mutex);
  339. put_online_mems();
  340. put_online_cpus();
  341. if (err) {
  342. if (flags & SLAB_PANIC)
  343. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  344. name, err);
  345. else {
  346. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  347. name, err);
  348. dump_stack();
  349. }
  350. return NULL;
  351. }
  352. return s;
  353. }
  354. EXPORT_SYMBOL(kmem_cache_create);
  355. #ifdef CONFIG_MEMCG_KMEM
  356. /*
  357. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  358. * @memcg: The memory cgroup the new cache is for.
  359. * @root_cache: The parent of the new cache.
  360. * @memcg_name: The name of the memory cgroup (used for naming the new cache).
  361. *
  362. * This function attempts to create a kmem cache that will serve allocation
  363. * requests going from @memcg to @root_cache. The new cache inherits properties
  364. * from its parent.
  365. */
  366. struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
  367. struct kmem_cache *root_cache,
  368. const char *memcg_name)
  369. {
  370. struct kmem_cache *s = NULL;
  371. char *cache_name;
  372. get_online_cpus();
  373. get_online_mems();
  374. mutex_lock(&slab_mutex);
  375. cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
  376. memcg_cache_id(memcg), memcg_name);
  377. if (!cache_name)
  378. goto out_unlock;
  379. s = do_kmem_cache_create(cache_name, root_cache->object_size,
  380. root_cache->size, root_cache->align,
  381. root_cache->flags, root_cache->ctor,
  382. memcg, root_cache);
  383. if (IS_ERR(s)) {
  384. kfree(cache_name);
  385. s = NULL;
  386. }
  387. out_unlock:
  388. mutex_unlock(&slab_mutex);
  389. put_online_mems();
  390. put_online_cpus();
  391. return s;
  392. }
  393. static int memcg_cleanup_cache_params(struct kmem_cache *s)
  394. {
  395. int rc;
  396. if (!s->memcg_params ||
  397. !s->memcg_params->is_root_cache)
  398. return 0;
  399. mutex_unlock(&slab_mutex);
  400. rc = __memcg_cleanup_cache_params(s);
  401. mutex_lock(&slab_mutex);
  402. return rc;
  403. }
  404. #else
  405. static int memcg_cleanup_cache_params(struct kmem_cache *s)
  406. {
  407. return 0;
  408. }
  409. #endif /* CONFIG_MEMCG_KMEM */
  410. void slab_kmem_cache_release(struct kmem_cache *s)
  411. {
  412. kfree(s->name);
  413. kmem_cache_free(kmem_cache, s);
  414. }
  415. void kmem_cache_destroy(struct kmem_cache *s)
  416. {
  417. get_online_cpus();
  418. get_online_mems();
  419. mutex_lock(&slab_mutex);
  420. s->refcount--;
  421. if (s->refcount)
  422. goto out_unlock;
  423. if (memcg_cleanup_cache_params(s) != 0)
  424. goto out_unlock;
  425. if (__kmem_cache_shutdown(s) != 0) {
  426. printk(KERN_ERR "kmem_cache_destroy %s: "
  427. "Slab cache still has objects\n", s->name);
  428. dump_stack();
  429. goto out_unlock;
  430. }
  431. list_del(&s->list);
  432. mutex_unlock(&slab_mutex);
  433. if (s->flags & SLAB_DESTROY_BY_RCU)
  434. rcu_barrier();
  435. memcg_free_cache_params(s);
  436. #ifdef SLAB_SUPPORTS_SYSFS
  437. sysfs_slab_remove(s);
  438. #else
  439. slab_kmem_cache_release(s);
  440. #endif
  441. goto out;
  442. out_unlock:
  443. mutex_unlock(&slab_mutex);
  444. out:
  445. put_online_mems();
  446. put_online_cpus();
  447. }
  448. EXPORT_SYMBOL(kmem_cache_destroy);
  449. /**
  450. * kmem_cache_shrink - Shrink a cache.
  451. * @cachep: The cache to shrink.
  452. *
  453. * Releases as many slabs as possible for a cache.
  454. * To help debugging, a zero exit status indicates all slabs were released.
  455. */
  456. int kmem_cache_shrink(struct kmem_cache *cachep)
  457. {
  458. int ret;
  459. get_online_cpus();
  460. get_online_mems();
  461. ret = __kmem_cache_shrink(cachep);
  462. put_online_mems();
  463. put_online_cpus();
  464. return ret;
  465. }
  466. EXPORT_SYMBOL(kmem_cache_shrink);
  467. int slab_is_available(void)
  468. {
  469. return slab_state >= UP;
  470. }
  471. #ifndef CONFIG_SLOB
  472. /* Create a cache during boot when no slab services are available yet */
  473. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  474. unsigned long flags)
  475. {
  476. int err;
  477. s->name = name;
  478. s->size = s->object_size = size;
  479. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  480. err = __kmem_cache_create(s, flags);
  481. if (err)
  482. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  483. name, size, err);
  484. s->refcount = -1; /* Exempt from merging for now */
  485. }
  486. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  487. unsigned long flags)
  488. {
  489. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  490. if (!s)
  491. panic("Out of memory when creating slab %s\n", name);
  492. create_boot_cache(s, name, size, flags);
  493. list_add(&s->list, &slab_caches);
  494. s->refcount = 1;
  495. return s;
  496. }
  497. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  498. EXPORT_SYMBOL(kmalloc_caches);
  499. #ifdef CONFIG_ZONE_DMA
  500. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  501. EXPORT_SYMBOL(kmalloc_dma_caches);
  502. #endif
  503. /*
  504. * Conversion table for small slabs sizes / 8 to the index in the
  505. * kmalloc array. This is necessary for slabs < 192 since we have non power
  506. * of two cache sizes there. The size of larger slabs can be determined using
  507. * fls.
  508. */
  509. static s8 size_index[24] = {
  510. 3, /* 8 */
  511. 4, /* 16 */
  512. 5, /* 24 */
  513. 5, /* 32 */
  514. 6, /* 40 */
  515. 6, /* 48 */
  516. 6, /* 56 */
  517. 6, /* 64 */
  518. 1, /* 72 */
  519. 1, /* 80 */
  520. 1, /* 88 */
  521. 1, /* 96 */
  522. 7, /* 104 */
  523. 7, /* 112 */
  524. 7, /* 120 */
  525. 7, /* 128 */
  526. 2, /* 136 */
  527. 2, /* 144 */
  528. 2, /* 152 */
  529. 2, /* 160 */
  530. 2, /* 168 */
  531. 2, /* 176 */
  532. 2, /* 184 */
  533. 2 /* 192 */
  534. };
  535. static inline int size_index_elem(size_t bytes)
  536. {
  537. return (bytes - 1) / 8;
  538. }
  539. /*
  540. * Find the kmem_cache structure that serves a given size of
  541. * allocation
  542. */
  543. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  544. {
  545. int index;
  546. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  547. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  548. return NULL;
  549. }
  550. if (size <= 192) {
  551. if (!size)
  552. return ZERO_SIZE_PTR;
  553. index = size_index[size_index_elem(size)];
  554. } else
  555. index = fls(size - 1);
  556. #ifdef CONFIG_ZONE_DMA
  557. if (unlikely((flags & GFP_DMA)))
  558. return kmalloc_dma_caches[index];
  559. #endif
  560. return kmalloc_caches[index];
  561. }
  562. /*
  563. * Create the kmalloc array. Some of the regular kmalloc arrays
  564. * may already have been created because they were needed to
  565. * enable allocations for slab creation.
  566. */
  567. void __init create_kmalloc_caches(unsigned long flags)
  568. {
  569. int i;
  570. /*
  571. * Patch up the size_index table if we have strange large alignment
  572. * requirements for the kmalloc array. This is only the case for
  573. * MIPS it seems. The standard arches will not generate any code here.
  574. *
  575. * Largest permitted alignment is 256 bytes due to the way we
  576. * handle the index determination for the smaller caches.
  577. *
  578. * Make sure that nothing crazy happens if someone starts tinkering
  579. * around with ARCH_KMALLOC_MINALIGN
  580. */
  581. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  582. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  583. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  584. int elem = size_index_elem(i);
  585. if (elem >= ARRAY_SIZE(size_index))
  586. break;
  587. size_index[elem] = KMALLOC_SHIFT_LOW;
  588. }
  589. if (KMALLOC_MIN_SIZE >= 64) {
  590. /*
  591. * The 96 byte size cache is not used if the alignment
  592. * is 64 byte.
  593. */
  594. for (i = 64 + 8; i <= 96; i += 8)
  595. size_index[size_index_elem(i)] = 7;
  596. }
  597. if (KMALLOC_MIN_SIZE >= 128) {
  598. /*
  599. * The 192 byte sized cache is not used if the alignment
  600. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  601. * instead.
  602. */
  603. for (i = 128 + 8; i <= 192; i += 8)
  604. size_index[size_index_elem(i)] = 8;
  605. }
  606. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  607. if (!kmalloc_caches[i]) {
  608. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  609. 1 << i, flags);
  610. }
  611. /*
  612. * Caches that are not of the two-to-the-power-of size.
  613. * These have to be created immediately after the
  614. * earlier power of two caches
  615. */
  616. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  617. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  618. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  619. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  620. }
  621. /* Kmalloc array is now usable */
  622. slab_state = UP;
  623. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  624. struct kmem_cache *s = kmalloc_caches[i];
  625. char *n;
  626. if (s) {
  627. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  628. BUG_ON(!n);
  629. s->name = n;
  630. }
  631. }
  632. #ifdef CONFIG_ZONE_DMA
  633. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  634. struct kmem_cache *s = kmalloc_caches[i];
  635. if (s) {
  636. int size = kmalloc_size(i);
  637. char *n = kasprintf(GFP_NOWAIT,
  638. "dma-kmalloc-%d", size);
  639. BUG_ON(!n);
  640. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  641. size, SLAB_CACHE_DMA | flags);
  642. }
  643. }
  644. #endif
  645. }
  646. #endif /* !CONFIG_SLOB */
  647. /*
  648. * To avoid unnecessary overhead, we pass through large allocation requests
  649. * directly to the page allocator. We use __GFP_COMP, because we will need to
  650. * know the allocation order to free the pages properly in kfree.
  651. */
  652. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  653. {
  654. void *ret;
  655. struct page *page;
  656. flags |= __GFP_COMP;
  657. page = alloc_kmem_pages(flags, order);
  658. ret = page ? page_address(page) : NULL;
  659. kmemleak_alloc(ret, size, 1, flags);
  660. return ret;
  661. }
  662. EXPORT_SYMBOL(kmalloc_order);
  663. #ifdef CONFIG_TRACING
  664. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  665. {
  666. void *ret = kmalloc_order(size, flags, order);
  667. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  668. return ret;
  669. }
  670. EXPORT_SYMBOL(kmalloc_order_trace);
  671. #endif
  672. #ifdef CONFIG_SLABINFO
  673. #ifdef CONFIG_SLAB
  674. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  675. #else
  676. #define SLABINFO_RIGHTS S_IRUSR
  677. #endif
  678. void print_slabinfo_header(struct seq_file *m)
  679. {
  680. /*
  681. * Output format version, so at least we can change it
  682. * without _too_ many complaints.
  683. */
  684. #ifdef CONFIG_DEBUG_SLAB
  685. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  686. #else
  687. seq_puts(m, "slabinfo - version: 2.1\n");
  688. #endif
  689. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  690. "<objperslab> <pagesperslab>");
  691. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  692. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  693. #ifdef CONFIG_DEBUG_SLAB
  694. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  695. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  696. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  697. #endif
  698. seq_putc(m, '\n');
  699. }
  700. static void *s_start(struct seq_file *m, loff_t *pos)
  701. {
  702. loff_t n = *pos;
  703. mutex_lock(&slab_mutex);
  704. if (!n)
  705. print_slabinfo_header(m);
  706. return seq_list_start(&slab_caches, *pos);
  707. }
  708. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  709. {
  710. return seq_list_next(p, &slab_caches, pos);
  711. }
  712. void slab_stop(struct seq_file *m, void *p)
  713. {
  714. mutex_unlock(&slab_mutex);
  715. }
  716. static void
  717. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  718. {
  719. struct kmem_cache *c;
  720. struct slabinfo sinfo;
  721. int i;
  722. if (!is_root_cache(s))
  723. return;
  724. for_each_memcg_cache_index(i) {
  725. c = cache_from_memcg_idx(s, i);
  726. if (!c)
  727. continue;
  728. memset(&sinfo, 0, sizeof(sinfo));
  729. get_slabinfo(c, &sinfo);
  730. info->active_slabs += sinfo.active_slabs;
  731. info->num_slabs += sinfo.num_slabs;
  732. info->shared_avail += sinfo.shared_avail;
  733. info->active_objs += sinfo.active_objs;
  734. info->num_objs += sinfo.num_objs;
  735. }
  736. }
  737. int cache_show(struct kmem_cache *s, struct seq_file *m)
  738. {
  739. struct slabinfo sinfo;
  740. memset(&sinfo, 0, sizeof(sinfo));
  741. get_slabinfo(s, &sinfo);
  742. memcg_accumulate_slabinfo(s, &sinfo);
  743. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  744. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  745. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  746. seq_printf(m, " : tunables %4u %4u %4u",
  747. sinfo.limit, sinfo.batchcount, sinfo.shared);
  748. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  749. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  750. slabinfo_show_stats(m, s);
  751. seq_putc(m, '\n');
  752. return 0;
  753. }
  754. static int s_show(struct seq_file *m, void *p)
  755. {
  756. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  757. if (!is_root_cache(s))
  758. return 0;
  759. return cache_show(s, m);
  760. }
  761. /*
  762. * slabinfo_op - iterator that generates /proc/slabinfo
  763. *
  764. * Output layout:
  765. * cache-name
  766. * num-active-objs
  767. * total-objs
  768. * object size
  769. * num-active-slabs
  770. * total-slabs
  771. * num-pages-per-slab
  772. * + further values on SMP and with statistics enabled
  773. */
  774. static const struct seq_operations slabinfo_op = {
  775. .start = s_start,
  776. .next = slab_next,
  777. .stop = slab_stop,
  778. .show = s_show,
  779. };
  780. static int slabinfo_open(struct inode *inode, struct file *file)
  781. {
  782. return seq_open(file, &slabinfo_op);
  783. }
  784. static const struct file_operations proc_slabinfo_operations = {
  785. .open = slabinfo_open,
  786. .read = seq_read,
  787. .write = slabinfo_write,
  788. .llseek = seq_lseek,
  789. .release = seq_release,
  790. };
  791. static int __init slab_proc_init(void)
  792. {
  793. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  794. &proc_slabinfo_operations);
  795. return 0;
  796. }
  797. module_init(slab_proc_init);
  798. #endif /* CONFIG_SLABINFO */
  799. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  800. gfp_t flags)
  801. {
  802. void *ret;
  803. size_t ks = 0;
  804. if (p)
  805. ks = ksize(p);
  806. if (ks >= new_size)
  807. return (void *)p;
  808. ret = kmalloc_track_caller(new_size, flags);
  809. if (ret && p)
  810. memcpy(ret, p, ks);
  811. return ret;
  812. }
  813. /**
  814. * __krealloc - like krealloc() but don't free @p.
  815. * @p: object to reallocate memory for.
  816. * @new_size: how many bytes of memory are required.
  817. * @flags: the type of memory to allocate.
  818. *
  819. * This function is like krealloc() except it never frees the originally
  820. * allocated buffer. Use this if you don't want to free the buffer immediately
  821. * like, for example, with RCU.
  822. */
  823. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  824. {
  825. if (unlikely(!new_size))
  826. return ZERO_SIZE_PTR;
  827. return __do_krealloc(p, new_size, flags);
  828. }
  829. EXPORT_SYMBOL(__krealloc);
  830. /**
  831. * krealloc - reallocate memory. The contents will remain unchanged.
  832. * @p: object to reallocate memory for.
  833. * @new_size: how many bytes of memory are required.
  834. * @flags: the type of memory to allocate.
  835. *
  836. * The contents of the object pointed to are preserved up to the
  837. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  838. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  839. * %NULL pointer, the object pointed to is freed.
  840. */
  841. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  842. {
  843. void *ret;
  844. if (unlikely(!new_size)) {
  845. kfree(p);
  846. return ZERO_SIZE_PTR;
  847. }
  848. ret = __do_krealloc(p, new_size, flags);
  849. if (ret && p != ret)
  850. kfree(p);
  851. return ret;
  852. }
  853. EXPORT_SYMBOL(krealloc);
  854. /**
  855. * kzfree - like kfree but zero memory
  856. * @p: object to free memory of
  857. *
  858. * The memory of the object @p points to is zeroed before freed.
  859. * If @p is %NULL, kzfree() does nothing.
  860. *
  861. * Note: this function zeroes the whole allocated buffer which can be a good
  862. * deal bigger than the requested buffer size passed to kmalloc(). So be
  863. * careful when using this function in performance sensitive code.
  864. */
  865. void kzfree(const void *p)
  866. {
  867. size_t ks;
  868. void *mem = (void *)p;
  869. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  870. return;
  871. ks = ksize(mem);
  872. memset(mem, 0, ks);
  873. kfree(mem);
  874. }
  875. EXPORT_SYMBOL(kzfree);
  876. /* Tracepoints definitions. */
  877. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  878. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  879. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  880. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  881. EXPORT_TRACEPOINT_SYMBOL(kfree);
  882. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);