slab_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. #include <trace/events/kmem.h>
  22. #include "slab.h"
  23. enum slab_state slab_state;
  24. LIST_HEAD(slab_caches);
  25. DEFINE_MUTEX(slab_mutex);
  26. struct kmem_cache *kmem_cache;
  27. #ifdef CONFIG_DEBUG_VM
  28. static int kmem_cache_sanity_check(struct mem_cgroup *memcg, const char *name,
  29. size_t size)
  30. {
  31. struct kmem_cache *s = NULL;
  32. if (!name || in_interrupt() || size < sizeof(void *) ||
  33. size > KMALLOC_MAX_SIZE) {
  34. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  35. return -EINVAL;
  36. }
  37. list_for_each_entry(s, &slab_caches, list) {
  38. char tmp;
  39. int res;
  40. /*
  41. * This happens when the module gets unloaded and doesn't
  42. * destroy its slab cache and no-one else reuses the vmalloc
  43. * area of the module. Print a warning.
  44. */
  45. res = probe_kernel_address(s->name, tmp);
  46. if (res) {
  47. pr_err("Slab cache with size %d has lost its name\n",
  48. s->object_size);
  49. continue;
  50. }
  51. /*
  52. * For simplicity, we won't check this in the list of memcg
  53. * caches. We have control over memcg naming, and if there
  54. * aren't duplicates in the global list, there won't be any
  55. * duplicates in the memcg lists as well.
  56. */
  57. if (!memcg && !strcmp(s->name, name)) {
  58. pr_err("%s (%s): Cache name already exists.\n",
  59. __func__, name);
  60. dump_stack();
  61. s = NULL;
  62. return -EINVAL;
  63. }
  64. }
  65. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  66. return 0;
  67. }
  68. #else
  69. static inline int kmem_cache_sanity_check(struct mem_cgroup *memcg,
  70. const char *name, size_t size)
  71. {
  72. return 0;
  73. }
  74. #endif
  75. #ifdef CONFIG_MEMCG_KMEM
  76. int memcg_update_all_caches(int num_memcgs)
  77. {
  78. struct kmem_cache *s;
  79. int ret = 0;
  80. mutex_lock(&slab_mutex);
  81. list_for_each_entry(s, &slab_caches, list) {
  82. if (!is_root_cache(s))
  83. continue;
  84. ret = memcg_update_cache_size(s, num_memcgs);
  85. /*
  86. * See comment in memcontrol.c, memcg_update_cache_size:
  87. * Instead of freeing the memory, we'll just leave the caches
  88. * up to this point in an updated state.
  89. */
  90. if (ret)
  91. goto out;
  92. }
  93. memcg_update_array_size(num_memcgs);
  94. out:
  95. mutex_unlock(&slab_mutex);
  96. return ret;
  97. }
  98. #endif
  99. /*
  100. * Figure out what the alignment of the objects will be given a set of
  101. * flags, a user specified alignment and the size of the objects.
  102. */
  103. unsigned long calculate_alignment(unsigned long flags,
  104. unsigned long align, unsigned long size)
  105. {
  106. /*
  107. * If the user wants hardware cache aligned objects then follow that
  108. * suggestion if the object is sufficiently large.
  109. *
  110. * The hardware cache alignment cannot override the specified
  111. * alignment though. If that is greater then use it.
  112. */
  113. if (flags & SLAB_HWCACHE_ALIGN) {
  114. unsigned long ralign = cache_line_size();
  115. while (size <= ralign / 2)
  116. ralign /= 2;
  117. align = max(align, ralign);
  118. }
  119. if (align < ARCH_SLAB_MINALIGN)
  120. align = ARCH_SLAB_MINALIGN;
  121. return ALIGN(align, sizeof(void *));
  122. }
  123. /*
  124. * kmem_cache_create - Create a cache.
  125. * @name: A string which is used in /proc/slabinfo to identify this cache.
  126. * @size: The size of objects to be created in this cache.
  127. * @align: The required alignment for the objects.
  128. * @flags: SLAB flags
  129. * @ctor: A constructor for the objects.
  130. *
  131. * Returns a ptr to the cache on success, NULL on failure.
  132. * Cannot be called within a interrupt, but can be interrupted.
  133. * The @ctor is run when new pages are allocated by the cache.
  134. *
  135. * The flags are
  136. *
  137. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  138. * to catch references to uninitialised memory.
  139. *
  140. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  141. * for buffer overruns.
  142. *
  143. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  144. * cacheline. This can be beneficial if you're counting cycles as closely
  145. * as davem.
  146. */
  147. struct kmem_cache *
  148. kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
  149. size_t align, unsigned long flags, void (*ctor)(void *),
  150. struct kmem_cache *parent_cache)
  151. {
  152. struct kmem_cache *s = NULL;
  153. int err = 0;
  154. get_online_cpus();
  155. mutex_lock(&slab_mutex);
  156. if (!kmem_cache_sanity_check(memcg, name, size) == 0)
  157. goto out_locked;
  158. /*
  159. * Some allocators will constraint the set of valid flags to a subset
  160. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  161. * case, and we'll just provide them with a sanitized version of the
  162. * passed flags.
  163. */
  164. flags &= CACHE_CREATE_MASK;
  165. s = __kmem_cache_alias(memcg, name, size, align, flags, ctor);
  166. if (s)
  167. goto out_locked;
  168. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  169. if (s) {
  170. s->object_size = s->size = size;
  171. s->align = calculate_alignment(flags, align, size);
  172. s->ctor = ctor;
  173. if (memcg_register_cache(memcg, s, parent_cache)) {
  174. kmem_cache_free(kmem_cache, s);
  175. err = -ENOMEM;
  176. goto out_locked;
  177. }
  178. s->name = kstrdup(name, GFP_KERNEL);
  179. if (!s->name) {
  180. kmem_cache_free(kmem_cache, s);
  181. err = -ENOMEM;
  182. goto out_locked;
  183. }
  184. err = __kmem_cache_create(s, flags);
  185. if (!err) {
  186. s->refcount = 1;
  187. list_add(&s->list, &slab_caches);
  188. memcg_cache_list_add(memcg, s);
  189. } else {
  190. kfree(s->name);
  191. kmem_cache_free(kmem_cache, s);
  192. }
  193. } else
  194. err = -ENOMEM;
  195. out_locked:
  196. mutex_unlock(&slab_mutex);
  197. put_online_cpus();
  198. if (err) {
  199. if (flags & SLAB_PANIC)
  200. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  201. name, err);
  202. else {
  203. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  204. name, err);
  205. dump_stack();
  206. }
  207. return NULL;
  208. }
  209. return s;
  210. }
  211. struct kmem_cache *
  212. kmem_cache_create(const char *name, size_t size, size_t align,
  213. unsigned long flags, void (*ctor)(void *))
  214. {
  215. return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor, NULL);
  216. }
  217. EXPORT_SYMBOL(kmem_cache_create);
  218. void kmem_cache_destroy(struct kmem_cache *s)
  219. {
  220. /* Destroy all the children caches if we aren't a memcg cache */
  221. kmem_cache_destroy_memcg_children(s);
  222. get_online_cpus();
  223. mutex_lock(&slab_mutex);
  224. s->refcount--;
  225. if (!s->refcount) {
  226. list_del(&s->list);
  227. if (!__kmem_cache_shutdown(s)) {
  228. mutex_unlock(&slab_mutex);
  229. if (s->flags & SLAB_DESTROY_BY_RCU)
  230. rcu_barrier();
  231. memcg_release_cache(s);
  232. kfree(s->name);
  233. kmem_cache_free(kmem_cache, s);
  234. } else {
  235. list_add(&s->list, &slab_caches);
  236. mutex_unlock(&slab_mutex);
  237. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  238. s->name);
  239. dump_stack();
  240. }
  241. } else {
  242. mutex_unlock(&slab_mutex);
  243. }
  244. put_online_cpus();
  245. }
  246. EXPORT_SYMBOL(kmem_cache_destroy);
  247. int slab_is_available(void)
  248. {
  249. return slab_state >= UP;
  250. }
  251. #ifndef CONFIG_SLOB
  252. /* Create a cache during boot when no slab services are available yet */
  253. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  254. unsigned long flags)
  255. {
  256. int err;
  257. s->name = name;
  258. s->size = s->object_size = size;
  259. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  260. err = __kmem_cache_create(s, flags);
  261. if (err)
  262. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  263. name, size, err);
  264. s->refcount = -1; /* Exempt from merging for now */
  265. }
  266. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  267. unsigned long flags)
  268. {
  269. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  270. if (!s)
  271. panic("Out of memory when creating slab %s\n", name);
  272. create_boot_cache(s, name, size, flags);
  273. list_add(&s->list, &slab_caches);
  274. s->refcount = 1;
  275. return s;
  276. }
  277. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  278. EXPORT_SYMBOL(kmalloc_caches);
  279. #ifdef CONFIG_ZONE_DMA
  280. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  281. EXPORT_SYMBOL(kmalloc_dma_caches);
  282. #endif
  283. /*
  284. * Conversion table for small slabs sizes / 8 to the index in the
  285. * kmalloc array. This is necessary for slabs < 192 since we have non power
  286. * of two cache sizes there. The size of larger slabs can be determined using
  287. * fls.
  288. */
  289. static s8 size_index[24] = {
  290. 3, /* 8 */
  291. 4, /* 16 */
  292. 5, /* 24 */
  293. 5, /* 32 */
  294. 6, /* 40 */
  295. 6, /* 48 */
  296. 6, /* 56 */
  297. 6, /* 64 */
  298. 1, /* 72 */
  299. 1, /* 80 */
  300. 1, /* 88 */
  301. 1, /* 96 */
  302. 7, /* 104 */
  303. 7, /* 112 */
  304. 7, /* 120 */
  305. 7, /* 128 */
  306. 2, /* 136 */
  307. 2, /* 144 */
  308. 2, /* 152 */
  309. 2, /* 160 */
  310. 2, /* 168 */
  311. 2, /* 176 */
  312. 2, /* 184 */
  313. 2 /* 192 */
  314. };
  315. static inline int size_index_elem(size_t bytes)
  316. {
  317. return (bytes - 1) / 8;
  318. }
  319. /*
  320. * Find the kmem_cache structure that serves a given size of
  321. * allocation
  322. */
  323. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  324. {
  325. int index;
  326. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  327. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  328. return NULL;
  329. }
  330. if (size <= 192) {
  331. if (!size)
  332. return ZERO_SIZE_PTR;
  333. index = size_index[size_index_elem(size)];
  334. } else
  335. index = fls(size - 1);
  336. #ifdef CONFIG_ZONE_DMA
  337. if (unlikely((flags & GFP_DMA)))
  338. return kmalloc_dma_caches[index];
  339. #endif
  340. return kmalloc_caches[index];
  341. }
  342. /*
  343. * Create the kmalloc array. Some of the regular kmalloc arrays
  344. * may already have been created because they were needed to
  345. * enable allocations for slab creation.
  346. */
  347. void __init create_kmalloc_caches(unsigned long flags)
  348. {
  349. int i;
  350. /*
  351. * Patch up the size_index table if we have strange large alignment
  352. * requirements for the kmalloc array. This is only the case for
  353. * MIPS it seems. The standard arches will not generate any code here.
  354. *
  355. * Largest permitted alignment is 256 bytes due to the way we
  356. * handle the index determination for the smaller caches.
  357. *
  358. * Make sure that nothing crazy happens if someone starts tinkering
  359. * around with ARCH_KMALLOC_MINALIGN
  360. */
  361. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  362. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  363. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  364. int elem = size_index_elem(i);
  365. if (elem >= ARRAY_SIZE(size_index))
  366. break;
  367. size_index[elem] = KMALLOC_SHIFT_LOW;
  368. }
  369. if (KMALLOC_MIN_SIZE >= 64) {
  370. /*
  371. * The 96 byte size cache is not used if the alignment
  372. * is 64 byte.
  373. */
  374. for (i = 64 + 8; i <= 96; i += 8)
  375. size_index[size_index_elem(i)] = 7;
  376. }
  377. if (KMALLOC_MIN_SIZE >= 128) {
  378. /*
  379. * The 192 byte sized cache is not used if the alignment
  380. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  381. * instead.
  382. */
  383. for (i = 128 + 8; i <= 192; i += 8)
  384. size_index[size_index_elem(i)] = 8;
  385. }
  386. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  387. if (!kmalloc_caches[i]) {
  388. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  389. 1 << i, flags);
  390. }
  391. /*
  392. * Caches that are not of the two-to-the-power-of size.
  393. * These have to be created immediately after the
  394. * earlier power of two caches
  395. */
  396. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  397. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  398. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  399. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  400. }
  401. /* Kmalloc array is now usable */
  402. slab_state = UP;
  403. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  404. struct kmem_cache *s = kmalloc_caches[i];
  405. char *n;
  406. if (s) {
  407. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  408. BUG_ON(!n);
  409. s->name = n;
  410. }
  411. }
  412. #ifdef CONFIG_ZONE_DMA
  413. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  414. struct kmem_cache *s = kmalloc_caches[i];
  415. if (s) {
  416. int size = kmalloc_size(i);
  417. char *n = kasprintf(GFP_NOWAIT,
  418. "dma-kmalloc-%d", size);
  419. BUG_ON(!n);
  420. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  421. size, SLAB_CACHE_DMA | flags);
  422. }
  423. }
  424. #endif
  425. }
  426. #endif /* !CONFIG_SLOB */
  427. #ifdef CONFIG_TRACING
  428. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  429. {
  430. void *ret = kmalloc_order(size, flags, order);
  431. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  432. return ret;
  433. }
  434. EXPORT_SYMBOL(kmalloc_order_trace);
  435. #endif
  436. #ifdef CONFIG_SLABINFO
  437. #ifdef CONFIG_SLAB
  438. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  439. #else
  440. #define SLABINFO_RIGHTS S_IRUSR
  441. #endif
  442. void print_slabinfo_header(struct seq_file *m)
  443. {
  444. /*
  445. * Output format version, so at least we can change it
  446. * without _too_ many complaints.
  447. */
  448. #ifdef CONFIG_DEBUG_SLAB
  449. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  450. #else
  451. seq_puts(m, "slabinfo - version: 2.1\n");
  452. #endif
  453. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  454. "<objperslab> <pagesperslab>");
  455. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  456. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  457. #ifdef CONFIG_DEBUG_SLAB
  458. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  459. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  460. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  461. #endif
  462. seq_putc(m, '\n');
  463. }
  464. static void *s_start(struct seq_file *m, loff_t *pos)
  465. {
  466. loff_t n = *pos;
  467. mutex_lock(&slab_mutex);
  468. if (!n)
  469. print_slabinfo_header(m);
  470. return seq_list_start(&slab_caches, *pos);
  471. }
  472. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  473. {
  474. return seq_list_next(p, &slab_caches, pos);
  475. }
  476. void slab_stop(struct seq_file *m, void *p)
  477. {
  478. mutex_unlock(&slab_mutex);
  479. }
  480. static void
  481. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  482. {
  483. struct kmem_cache *c;
  484. struct slabinfo sinfo;
  485. int i;
  486. if (!is_root_cache(s))
  487. return;
  488. for_each_memcg_cache_index(i) {
  489. c = cache_from_memcg(s, i);
  490. if (!c)
  491. continue;
  492. memset(&sinfo, 0, sizeof(sinfo));
  493. get_slabinfo(c, &sinfo);
  494. info->active_slabs += sinfo.active_slabs;
  495. info->num_slabs += sinfo.num_slabs;
  496. info->shared_avail += sinfo.shared_avail;
  497. info->active_objs += sinfo.active_objs;
  498. info->num_objs += sinfo.num_objs;
  499. }
  500. }
  501. int cache_show(struct kmem_cache *s, struct seq_file *m)
  502. {
  503. struct slabinfo sinfo;
  504. memset(&sinfo, 0, sizeof(sinfo));
  505. get_slabinfo(s, &sinfo);
  506. memcg_accumulate_slabinfo(s, &sinfo);
  507. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  508. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  509. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  510. seq_printf(m, " : tunables %4u %4u %4u",
  511. sinfo.limit, sinfo.batchcount, sinfo.shared);
  512. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  513. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  514. slabinfo_show_stats(m, s);
  515. seq_putc(m, '\n');
  516. return 0;
  517. }
  518. static int s_show(struct seq_file *m, void *p)
  519. {
  520. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  521. if (!is_root_cache(s))
  522. return 0;
  523. return cache_show(s, m);
  524. }
  525. /*
  526. * slabinfo_op - iterator that generates /proc/slabinfo
  527. *
  528. * Output layout:
  529. * cache-name
  530. * num-active-objs
  531. * total-objs
  532. * object size
  533. * num-active-slabs
  534. * total-slabs
  535. * num-pages-per-slab
  536. * + further values on SMP and with statistics enabled
  537. */
  538. static const struct seq_operations slabinfo_op = {
  539. .start = s_start,
  540. .next = slab_next,
  541. .stop = slab_stop,
  542. .show = s_show,
  543. };
  544. static int slabinfo_open(struct inode *inode, struct file *file)
  545. {
  546. return seq_open(file, &slabinfo_op);
  547. }
  548. static const struct file_operations proc_slabinfo_operations = {
  549. .open = slabinfo_open,
  550. .read = seq_read,
  551. .write = slabinfo_write,
  552. .llseek = seq_lseek,
  553. .release = seq_release,
  554. };
  555. static int __init slab_proc_init(void)
  556. {
  557. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  558. &proc_slabinfo_operations);
  559. return 0;
  560. }
  561. module_init(slab_proc_init);
  562. #endif /* CONFIG_SLABINFO */