slab_common.c 24 KB

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