slab_common.c 24 KB

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