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) || !defined(CONFIG_SLUB_DEBUG_ON)
  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. * kmem_cache_create_memcg - 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 *kmem_cache_create_memcg(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 kmem_cache_destroy_memcg_children(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 = __kmem_cache_destroy_memcg_children(s);
  273. mutex_lock(&slab_mutex);
  274. return rc;
  275. }
  276. #else
  277. static int kmem_cache_destroy_memcg_children(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 (kmem_cache_destroy_memcg_children(s) != 0)
  296. goto out_unlock;
  297. list_del(&s->list);
  298. if (__kmem_cache_shutdown(s) != 0) {
  299. list_add(&s->list, &slab_caches);
  300. printk(KERN_ERR "kmem_cache_destroy %s: "
  301. "Slab cache still has objects\n", s->name);
  302. dump_stack();
  303. goto out_unlock;
  304. }
  305. mutex_unlock(&slab_mutex);
  306. if (s->flags & SLAB_DESTROY_BY_RCU)
  307. rcu_barrier();
  308. memcg_free_cache_params(s);
  309. #ifdef SLAB_SUPPORTS_SYSFS
  310. sysfs_slab_remove(s);
  311. #else
  312. slab_kmem_cache_release(s);
  313. #endif
  314. goto out;
  315. out_unlock:
  316. mutex_unlock(&slab_mutex);
  317. out:
  318. put_online_mems();
  319. put_online_cpus();
  320. }
  321. EXPORT_SYMBOL(kmem_cache_destroy);
  322. /**
  323. * kmem_cache_shrink - Shrink a cache.
  324. * @cachep: The cache to shrink.
  325. *
  326. * Releases as many slabs as possible for a cache.
  327. * To help debugging, a zero exit status indicates all slabs were released.
  328. */
  329. int kmem_cache_shrink(struct kmem_cache *cachep)
  330. {
  331. int ret;
  332. get_online_cpus();
  333. get_online_mems();
  334. ret = __kmem_cache_shrink(cachep);
  335. put_online_mems();
  336. put_online_cpus();
  337. return ret;
  338. }
  339. EXPORT_SYMBOL(kmem_cache_shrink);
  340. int slab_is_available(void)
  341. {
  342. return slab_state >= UP;
  343. }
  344. #ifndef CONFIG_SLOB
  345. /* Create a cache during boot when no slab services are available yet */
  346. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  347. unsigned long flags)
  348. {
  349. int err;
  350. s->name = name;
  351. s->size = s->object_size = size;
  352. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  353. err = __kmem_cache_create(s, flags);
  354. if (err)
  355. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  356. name, size, err);
  357. s->refcount = -1; /* Exempt from merging for now */
  358. }
  359. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  360. unsigned long flags)
  361. {
  362. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  363. if (!s)
  364. panic("Out of memory when creating slab %s\n", name);
  365. create_boot_cache(s, name, size, flags);
  366. list_add(&s->list, &slab_caches);
  367. s->refcount = 1;
  368. return s;
  369. }
  370. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  371. EXPORT_SYMBOL(kmalloc_caches);
  372. #ifdef CONFIG_ZONE_DMA
  373. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  374. EXPORT_SYMBOL(kmalloc_dma_caches);
  375. #endif
  376. /*
  377. * Conversion table for small slabs sizes / 8 to the index in the
  378. * kmalloc array. This is necessary for slabs < 192 since we have non power
  379. * of two cache sizes there. The size of larger slabs can be determined using
  380. * fls.
  381. */
  382. static s8 size_index[24] = {
  383. 3, /* 8 */
  384. 4, /* 16 */
  385. 5, /* 24 */
  386. 5, /* 32 */
  387. 6, /* 40 */
  388. 6, /* 48 */
  389. 6, /* 56 */
  390. 6, /* 64 */
  391. 1, /* 72 */
  392. 1, /* 80 */
  393. 1, /* 88 */
  394. 1, /* 96 */
  395. 7, /* 104 */
  396. 7, /* 112 */
  397. 7, /* 120 */
  398. 7, /* 128 */
  399. 2, /* 136 */
  400. 2, /* 144 */
  401. 2, /* 152 */
  402. 2, /* 160 */
  403. 2, /* 168 */
  404. 2, /* 176 */
  405. 2, /* 184 */
  406. 2 /* 192 */
  407. };
  408. static inline int size_index_elem(size_t bytes)
  409. {
  410. return (bytes - 1) / 8;
  411. }
  412. /*
  413. * Find the kmem_cache structure that serves a given size of
  414. * allocation
  415. */
  416. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  417. {
  418. int index;
  419. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  420. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  421. return NULL;
  422. }
  423. if (size <= 192) {
  424. if (!size)
  425. return ZERO_SIZE_PTR;
  426. index = size_index[size_index_elem(size)];
  427. } else
  428. index = fls(size - 1);
  429. #ifdef CONFIG_ZONE_DMA
  430. if (unlikely((flags & GFP_DMA)))
  431. return kmalloc_dma_caches[index];
  432. #endif
  433. return kmalloc_caches[index];
  434. }
  435. /*
  436. * Create the kmalloc array. Some of the regular kmalloc arrays
  437. * may already have been created because they were needed to
  438. * enable allocations for slab creation.
  439. */
  440. void __init create_kmalloc_caches(unsigned long flags)
  441. {
  442. int i;
  443. /*
  444. * Patch up the size_index table if we have strange large alignment
  445. * requirements for the kmalloc array. This is only the case for
  446. * MIPS it seems. The standard arches will not generate any code here.
  447. *
  448. * Largest permitted alignment is 256 bytes due to the way we
  449. * handle the index determination for the smaller caches.
  450. *
  451. * Make sure that nothing crazy happens if someone starts tinkering
  452. * around with ARCH_KMALLOC_MINALIGN
  453. */
  454. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  455. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  456. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  457. int elem = size_index_elem(i);
  458. if (elem >= ARRAY_SIZE(size_index))
  459. break;
  460. size_index[elem] = KMALLOC_SHIFT_LOW;
  461. }
  462. if (KMALLOC_MIN_SIZE >= 64) {
  463. /*
  464. * The 96 byte size cache is not used if the alignment
  465. * is 64 byte.
  466. */
  467. for (i = 64 + 8; i <= 96; i += 8)
  468. size_index[size_index_elem(i)] = 7;
  469. }
  470. if (KMALLOC_MIN_SIZE >= 128) {
  471. /*
  472. * The 192 byte sized cache is not used if the alignment
  473. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  474. * instead.
  475. */
  476. for (i = 128 + 8; i <= 192; i += 8)
  477. size_index[size_index_elem(i)] = 8;
  478. }
  479. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  480. if (!kmalloc_caches[i]) {
  481. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  482. 1 << i, flags);
  483. }
  484. /*
  485. * Caches that are not of the two-to-the-power-of size.
  486. * These have to be created immediately after the
  487. * earlier power of two caches
  488. */
  489. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  490. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  491. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  492. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  493. }
  494. /* Kmalloc array is now usable */
  495. slab_state = UP;
  496. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  497. struct kmem_cache *s = kmalloc_caches[i];
  498. char *n;
  499. if (s) {
  500. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  501. BUG_ON(!n);
  502. s->name = n;
  503. }
  504. }
  505. #ifdef CONFIG_ZONE_DMA
  506. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  507. struct kmem_cache *s = kmalloc_caches[i];
  508. if (s) {
  509. int size = kmalloc_size(i);
  510. char *n = kasprintf(GFP_NOWAIT,
  511. "dma-kmalloc-%d", size);
  512. BUG_ON(!n);
  513. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  514. size, SLAB_CACHE_DMA | flags);
  515. }
  516. }
  517. #endif
  518. }
  519. #endif /* !CONFIG_SLOB */
  520. /*
  521. * To avoid unnecessary overhead, we pass through large allocation requests
  522. * directly to the page allocator. We use __GFP_COMP, because we will need to
  523. * know the allocation order to free the pages properly in kfree.
  524. */
  525. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  526. {
  527. void *ret;
  528. struct page *page;
  529. flags |= __GFP_COMP;
  530. page = alloc_kmem_pages(flags, order);
  531. ret = page ? page_address(page) : NULL;
  532. kmemleak_alloc(ret, size, 1, flags);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL(kmalloc_order);
  536. #ifdef CONFIG_TRACING
  537. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  538. {
  539. void *ret = kmalloc_order(size, flags, order);
  540. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL(kmalloc_order_trace);
  544. #endif
  545. #ifdef CONFIG_SLABINFO
  546. #ifdef CONFIG_SLAB
  547. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  548. #else
  549. #define SLABINFO_RIGHTS S_IRUSR
  550. #endif
  551. void print_slabinfo_header(struct seq_file *m)
  552. {
  553. /*
  554. * Output format version, so at least we can change it
  555. * without _too_ many complaints.
  556. */
  557. #ifdef CONFIG_DEBUG_SLAB
  558. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  559. #else
  560. seq_puts(m, "slabinfo - version: 2.1\n");
  561. #endif
  562. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  563. "<objperslab> <pagesperslab>");
  564. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  565. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  566. #ifdef CONFIG_DEBUG_SLAB
  567. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  568. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  569. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  570. #endif
  571. seq_putc(m, '\n');
  572. }
  573. static void *s_start(struct seq_file *m, loff_t *pos)
  574. {
  575. loff_t n = *pos;
  576. mutex_lock(&slab_mutex);
  577. if (!n)
  578. print_slabinfo_header(m);
  579. return seq_list_start(&slab_caches, *pos);
  580. }
  581. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  582. {
  583. return seq_list_next(p, &slab_caches, pos);
  584. }
  585. void slab_stop(struct seq_file *m, void *p)
  586. {
  587. mutex_unlock(&slab_mutex);
  588. }
  589. static void
  590. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  591. {
  592. struct kmem_cache *c;
  593. struct slabinfo sinfo;
  594. int i;
  595. if (!is_root_cache(s))
  596. return;
  597. for_each_memcg_cache_index(i) {
  598. c = cache_from_memcg_idx(s, i);
  599. if (!c)
  600. continue;
  601. memset(&sinfo, 0, sizeof(sinfo));
  602. get_slabinfo(c, &sinfo);
  603. info->active_slabs += sinfo.active_slabs;
  604. info->num_slabs += sinfo.num_slabs;
  605. info->shared_avail += sinfo.shared_avail;
  606. info->active_objs += sinfo.active_objs;
  607. info->num_objs += sinfo.num_objs;
  608. }
  609. }
  610. int cache_show(struct kmem_cache *s, struct seq_file *m)
  611. {
  612. struct slabinfo sinfo;
  613. memset(&sinfo, 0, sizeof(sinfo));
  614. get_slabinfo(s, &sinfo);
  615. memcg_accumulate_slabinfo(s, &sinfo);
  616. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  617. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  618. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  619. seq_printf(m, " : tunables %4u %4u %4u",
  620. sinfo.limit, sinfo.batchcount, sinfo.shared);
  621. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  622. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  623. slabinfo_show_stats(m, s);
  624. seq_putc(m, '\n');
  625. return 0;
  626. }
  627. static int s_show(struct seq_file *m, void *p)
  628. {
  629. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  630. if (!is_root_cache(s))
  631. return 0;
  632. return cache_show(s, m);
  633. }
  634. /*
  635. * slabinfo_op - iterator that generates /proc/slabinfo
  636. *
  637. * Output layout:
  638. * cache-name
  639. * num-active-objs
  640. * total-objs
  641. * object size
  642. * num-active-slabs
  643. * total-slabs
  644. * num-pages-per-slab
  645. * + further values on SMP and with statistics enabled
  646. */
  647. static const struct seq_operations slabinfo_op = {
  648. .start = s_start,
  649. .next = slab_next,
  650. .stop = slab_stop,
  651. .show = s_show,
  652. };
  653. static int slabinfo_open(struct inode *inode, struct file *file)
  654. {
  655. return seq_open(file, &slabinfo_op);
  656. }
  657. static const struct file_operations proc_slabinfo_operations = {
  658. .open = slabinfo_open,
  659. .read = seq_read,
  660. .write = slabinfo_write,
  661. .llseek = seq_lseek,
  662. .release = seq_release,
  663. };
  664. static int __init slab_proc_init(void)
  665. {
  666. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  667. &proc_slabinfo_operations);
  668. return 0;
  669. }
  670. module_init(slab_proc_init);
  671. #endif /* CONFIG_SLABINFO */