slab_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. memcg_register_cache(s);
  142. out:
  143. if (err)
  144. return ERR_PTR(err);
  145. return s;
  146. out_free_cache:
  147. memcg_free_cache_params(s);
  148. kfree(s);
  149. goto out;
  150. }
  151. /*
  152. * kmem_cache_create - Create a cache.
  153. * @name: A string which is used in /proc/slabinfo to identify this cache.
  154. * @size: The size of objects to be created in this cache.
  155. * @align: The required alignment for the objects.
  156. * @flags: SLAB flags
  157. * @ctor: A constructor for the objects.
  158. *
  159. * Returns a ptr to the cache on success, NULL on failure.
  160. * Cannot be called within a interrupt, but can be interrupted.
  161. * The @ctor is run when new pages are allocated by the cache.
  162. *
  163. * The flags are
  164. *
  165. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  166. * to catch references to uninitialised memory.
  167. *
  168. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  169. * for buffer overruns.
  170. *
  171. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  172. * cacheline. This can be beneficial if you're counting cycles as closely
  173. * as davem.
  174. */
  175. struct kmem_cache *
  176. kmem_cache_create(const char *name, size_t size, size_t align,
  177. unsigned long flags, void (*ctor)(void *))
  178. {
  179. struct kmem_cache *s;
  180. char *cache_name;
  181. int err;
  182. get_online_cpus();
  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_cpus();
  212. if (err) {
  213. if (flags & SLAB_PANIC)
  214. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  215. name, err);
  216. else {
  217. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  218. name, err);
  219. dump_stack();
  220. }
  221. return NULL;
  222. }
  223. return s;
  224. }
  225. EXPORT_SYMBOL(kmem_cache_create);
  226. #ifdef CONFIG_MEMCG_KMEM
  227. /*
  228. * kmem_cache_create_memcg - Create a cache for a memory cgroup.
  229. * @memcg: The memory cgroup the new cache is for.
  230. * @root_cache: The parent of the new cache.
  231. *
  232. * This function attempts to create a kmem cache that will serve allocation
  233. * requests going from @memcg to @root_cache. The new cache inherits properties
  234. * from its parent.
  235. */
  236. void kmem_cache_create_memcg(struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  237. {
  238. struct kmem_cache *s;
  239. char *cache_name;
  240. get_online_cpus();
  241. mutex_lock(&slab_mutex);
  242. /*
  243. * Since per-memcg caches are created asynchronously on first
  244. * allocation (see memcg_kmem_get_cache()), several threads can try to
  245. * create the same cache, but only one of them may succeed.
  246. */
  247. if (cache_from_memcg_idx(root_cache, memcg_cache_id(memcg)))
  248. goto out_unlock;
  249. cache_name = memcg_create_cache_name(memcg, root_cache);
  250. if (!cache_name)
  251. goto out_unlock;
  252. s = do_kmem_cache_create(cache_name, root_cache->object_size,
  253. root_cache->size, root_cache->align,
  254. root_cache->flags, root_cache->ctor,
  255. memcg, root_cache);
  256. if (IS_ERR(s)) {
  257. kfree(cache_name);
  258. goto out_unlock;
  259. }
  260. s->allocflags |= __GFP_KMEMCG;
  261. out_unlock:
  262. mutex_unlock(&slab_mutex);
  263. put_online_cpus();
  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. mutex_lock(&slab_mutex);
  291. s->refcount--;
  292. if (s->refcount)
  293. goto out_unlock;
  294. if (kmem_cache_destroy_memcg_children(s) != 0)
  295. goto out_unlock;
  296. list_del(&s->list);
  297. memcg_unregister_cache(s);
  298. if (__kmem_cache_shutdown(s) != 0) {
  299. list_add(&s->list, &slab_caches);
  300. memcg_register_cache(s);
  301. printk(KERN_ERR "kmem_cache_destroy %s: "
  302. "Slab cache still has objects\n", s->name);
  303. dump_stack();
  304. goto out_unlock;
  305. }
  306. mutex_unlock(&slab_mutex);
  307. if (s->flags & SLAB_DESTROY_BY_RCU)
  308. rcu_barrier();
  309. memcg_free_cache_params(s);
  310. #ifdef SLAB_SUPPORTS_SYSFS
  311. sysfs_slab_remove(s);
  312. #else
  313. slab_kmem_cache_release(s);
  314. #endif
  315. goto out_put_cpus;
  316. out_unlock:
  317. mutex_unlock(&slab_mutex);
  318. out_put_cpus:
  319. put_online_cpus();
  320. }
  321. EXPORT_SYMBOL(kmem_cache_destroy);
  322. int slab_is_available(void)
  323. {
  324. return slab_state >= UP;
  325. }
  326. #ifndef CONFIG_SLOB
  327. /* Create a cache during boot when no slab services are available yet */
  328. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  329. unsigned long flags)
  330. {
  331. int err;
  332. s->name = name;
  333. s->size = s->object_size = size;
  334. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  335. err = __kmem_cache_create(s, flags);
  336. if (err)
  337. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  338. name, size, err);
  339. s->refcount = -1; /* Exempt from merging for now */
  340. }
  341. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  342. unsigned long flags)
  343. {
  344. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  345. if (!s)
  346. panic("Out of memory when creating slab %s\n", name);
  347. create_boot_cache(s, name, size, flags);
  348. list_add(&s->list, &slab_caches);
  349. s->refcount = 1;
  350. return s;
  351. }
  352. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  353. EXPORT_SYMBOL(kmalloc_caches);
  354. #ifdef CONFIG_ZONE_DMA
  355. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  356. EXPORT_SYMBOL(kmalloc_dma_caches);
  357. #endif
  358. /*
  359. * Conversion table for small slabs sizes / 8 to the index in the
  360. * kmalloc array. This is necessary for slabs < 192 since we have non power
  361. * of two cache sizes there. The size of larger slabs can be determined using
  362. * fls.
  363. */
  364. static s8 size_index[24] = {
  365. 3, /* 8 */
  366. 4, /* 16 */
  367. 5, /* 24 */
  368. 5, /* 32 */
  369. 6, /* 40 */
  370. 6, /* 48 */
  371. 6, /* 56 */
  372. 6, /* 64 */
  373. 1, /* 72 */
  374. 1, /* 80 */
  375. 1, /* 88 */
  376. 1, /* 96 */
  377. 7, /* 104 */
  378. 7, /* 112 */
  379. 7, /* 120 */
  380. 7, /* 128 */
  381. 2, /* 136 */
  382. 2, /* 144 */
  383. 2, /* 152 */
  384. 2, /* 160 */
  385. 2, /* 168 */
  386. 2, /* 176 */
  387. 2, /* 184 */
  388. 2 /* 192 */
  389. };
  390. static inline int size_index_elem(size_t bytes)
  391. {
  392. return (bytes - 1) / 8;
  393. }
  394. /*
  395. * Find the kmem_cache structure that serves a given size of
  396. * allocation
  397. */
  398. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  399. {
  400. int index;
  401. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  402. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  403. return NULL;
  404. }
  405. if (size <= 192) {
  406. if (!size)
  407. return ZERO_SIZE_PTR;
  408. index = size_index[size_index_elem(size)];
  409. } else
  410. index = fls(size - 1);
  411. #ifdef CONFIG_ZONE_DMA
  412. if (unlikely((flags & GFP_DMA)))
  413. return kmalloc_dma_caches[index];
  414. #endif
  415. return kmalloc_caches[index];
  416. }
  417. /*
  418. * Create the kmalloc array. Some of the regular kmalloc arrays
  419. * may already have been created because they were needed to
  420. * enable allocations for slab creation.
  421. */
  422. void __init create_kmalloc_caches(unsigned long flags)
  423. {
  424. int i;
  425. /*
  426. * Patch up the size_index table if we have strange large alignment
  427. * requirements for the kmalloc array. This is only the case for
  428. * MIPS it seems. The standard arches will not generate any code here.
  429. *
  430. * Largest permitted alignment is 256 bytes due to the way we
  431. * handle the index determination for the smaller caches.
  432. *
  433. * Make sure that nothing crazy happens if someone starts tinkering
  434. * around with ARCH_KMALLOC_MINALIGN
  435. */
  436. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  437. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  438. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  439. int elem = size_index_elem(i);
  440. if (elem >= ARRAY_SIZE(size_index))
  441. break;
  442. size_index[elem] = KMALLOC_SHIFT_LOW;
  443. }
  444. if (KMALLOC_MIN_SIZE >= 64) {
  445. /*
  446. * The 96 byte size cache is not used if the alignment
  447. * is 64 byte.
  448. */
  449. for (i = 64 + 8; i <= 96; i += 8)
  450. size_index[size_index_elem(i)] = 7;
  451. }
  452. if (KMALLOC_MIN_SIZE >= 128) {
  453. /*
  454. * The 192 byte sized cache is not used if the alignment
  455. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  456. * instead.
  457. */
  458. for (i = 128 + 8; i <= 192; i += 8)
  459. size_index[size_index_elem(i)] = 8;
  460. }
  461. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  462. if (!kmalloc_caches[i]) {
  463. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  464. 1 << i, flags);
  465. }
  466. /*
  467. * Caches that are not of the two-to-the-power-of size.
  468. * These have to be created immediately after the
  469. * earlier power of two caches
  470. */
  471. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  472. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  473. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  474. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  475. }
  476. /* Kmalloc array is now usable */
  477. slab_state = UP;
  478. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  479. struct kmem_cache *s = kmalloc_caches[i];
  480. char *n;
  481. if (s) {
  482. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  483. BUG_ON(!n);
  484. s->name = n;
  485. }
  486. }
  487. #ifdef CONFIG_ZONE_DMA
  488. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  489. struct kmem_cache *s = kmalloc_caches[i];
  490. if (s) {
  491. int size = kmalloc_size(i);
  492. char *n = kasprintf(GFP_NOWAIT,
  493. "dma-kmalloc-%d", size);
  494. BUG_ON(!n);
  495. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  496. size, SLAB_CACHE_DMA | flags);
  497. }
  498. }
  499. #endif
  500. }
  501. #endif /* !CONFIG_SLOB */
  502. #ifdef CONFIG_TRACING
  503. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  504. {
  505. void *ret = kmalloc_order(size, flags, order);
  506. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  507. return ret;
  508. }
  509. EXPORT_SYMBOL(kmalloc_order_trace);
  510. #endif
  511. #ifdef CONFIG_SLABINFO
  512. #ifdef CONFIG_SLAB
  513. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  514. #else
  515. #define SLABINFO_RIGHTS S_IRUSR
  516. #endif
  517. void print_slabinfo_header(struct seq_file *m)
  518. {
  519. /*
  520. * Output format version, so at least we can change it
  521. * without _too_ many complaints.
  522. */
  523. #ifdef CONFIG_DEBUG_SLAB
  524. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  525. #else
  526. seq_puts(m, "slabinfo - version: 2.1\n");
  527. #endif
  528. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  529. "<objperslab> <pagesperslab>");
  530. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  531. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  532. #ifdef CONFIG_DEBUG_SLAB
  533. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  534. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  535. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  536. #endif
  537. seq_putc(m, '\n');
  538. }
  539. static void *s_start(struct seq_file *m, loff_t *pos)
  540. {
  541. loff_t n = *pos;
  542. mutex_lock(&slab_mutex);
  543. if (!n)
  544. print_slabinfo_header(m);
  545. return seq_list_start(&slab_caches, *pos);
  546. }
  547. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  548. {
  549. return seq_list_next(p, &slab_caches, pos);
  550. }
  551. void slab_stop(struct seq_file *m, void *p)
  552. {
  553. mutex_unlock(&slab_mutex);
  554. }
  555. static void
  556. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  557. {
  558. struct kmem_cache *c;
  559. struct slabinfo sinfo;
  560. int i;
  561. if (!is_root_cache(s))
  562. return;
  563. for_each_memcg_cache_index(i) {
  564. c = cache_from_memcg_idx(s, i);
  565. if (!c)
  566. continue;
  567. memset(&sinfo, 0, sizeof(sinfo));
  568. get_slabinfo(c, &sinfo);
  569. info->active_slabs += sinfo.active_slabs;
  570. info->num_slabs += sinfo.num_slabs;
  571. info->shared_avail += sinfo.shared_avail;
  572. info->active_objs += sinfo.active_objs;
  573. info->num_objs += sinfo.num_objs;
  574. }
  575. }
  576. int cache_show(struct kmem_cache *s, struct seq_file *m)
  577. {
  578. struct slabinfo sinfo;
  579. memset(&sinfo, 0, sizeof(sinfo));
  580. get_slabinfo(s, &sinfo);
  581. memcg_accumulate_slabinfo(s, &sinfo);
  582. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  583. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  584. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  585. seq_printf(m, " : tunables %4u %4u %4u",
  586. sinfo.limit, sinfo.batchcount, sinfo.shared);
  587. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  588. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  589. slabinfo_show_stats(m, s);
  590. seq_putc(m, '\n');
  591. return 0;
  592. }
  593. static int s_show(struct seq_file *m, void *p)
  594. {
  595. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  596. if (!is_root_cache(s))
  597. return 0;
  598. return cache_show(s, m);
  599. }
  600. /*
  601. * slabinfo_op - iterator that generates /proc/slabinfo
  602. *
  603. * Output layout:
  604. * cache-name
  605. * num-active-objs
  606. * total-objs
  607. * object size
  608. * num-active-slabs
  609. * total-slabs
  610. * num-pages-per-slab
  611. * + further values on SMP and with statistics enabled
  612. */
  613. static const struct seq_operations slabinfo_op = {
  614. .start = s_start,
  615. .next = slab_next,
  616. .stop = slab_stop,
  617. .show = s_show,
  618. };
  619. static int slabinfo_open(struct inode *inode, struct file *file)
  620. {
  621. return seq_open(file, &slabinfo_op);
  622. }
  623. static const struct file_operations proc_slabinfo_operations = {
  624. .open = slabinfo_open,
  625. .read = seq_read,
  626. .write = slabinfo_write,
  627. .llseek = seq_lseek,
  628. .release = seq_release,
  629. };
  630. static int __init slab_proc_init(void)
  631. {
  632. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  633. &proc_slabinfo_operations);
  634. return 0;
  635. }
  636. module_init(slab_proc_init);
  637. #endif /* CONFIG_SLABINFO */