slab_common.c 18 KB

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