slab_common.c 17 KB

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