slab_common.c 15 KB

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