slab_common.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "slab.h"
  21. enum slab_state slab_state;
  22. LIST_HEAD(slab_caches);
  23. DEFINE_MUTEX(slab_mutex);
  24. struct kmem_cache *kmem_cache;
  25. #ifdef CONFIG_DEBUG_VM
  26. static int kmem_cache_sanity_check(const char *name, size_t size)
  27. {
  28. struct kmem_cache *s = NULL;
  29. if (!name || in_interrupt() || size < sizeof(void *) ||
  30. size > KMALLOC_MAX_SIZE) {
  31. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  32. return -EINVAL;
  33. }
  34. list_for_each_entry(s, &slab_caches, list) {
  35. char tmp;
  36. int res;
  37. /*
  38. * This happens when the module gets unloaded and doesn't
  39. * destroy its slab cache and no-one else reuses the vmalloc
  40. * area of the module. Print a warning.
  41. */
  42. res = probe_kernel_address(s->name, tmp);
  43. if (res) {
  44. pr_err("Slab cache with size %d has lost its name\n",
  45. s->object_size);
  46. continue;
  47. }
  48. if (!strcmp(s->name, name)) {
  49. pr_err("%s (%s): Cache name already exists.\n",
  50. __func__, name);
  51. dump_stack();
  52. s = NULL;
  53. return -EINVAL;
  54. }
  55. }
  56. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  57. return 0;
  58. }
  59. #else
  60. static inline int kmem_cache_sanity_check(const char *name, size_t size)
  61. {
  62. return 0;
  63. }
  64. #endif
  65. /*
  66. * kmem_cache_create - Create a cache.
  67. * @name: A string which is used in /proc/slabinfo to identify this cache.
  68. * @size: The size of objects to be created in this cache.
  69. * @align: The required alignment for the objects.
  70. * @flags: SLAB flags
  71. * @ctor: A constructor for the objects.
  72. *
  73. * Returns a ptr to the cache on success, NULL on failure.
  74. * Cannot be called within a interrupt, but can be interrupted.
  75. * The @ctor is run when new pages are allocated by the cache.
  76. *
  77. * The flags are
  78. *
  79. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  80. * to catch references to uninitialised memory.
  81. *
  82. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  83. * for buffer overruns.
  84. *
  85. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  86. * cacheline. This can be beneficial if you're counting cycles as closely
  87. * as davem.
  88. */
  89. struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
  90. unsigned long flags, void (*ctor)(void *))
  91. {
  92. struct kmem_cache *s = NULL;
  93. int err = 0;
  94. get_online_cpus();
  95. mutex_lock(&slab_mutex);
  96. if (!kmem_cache_sanity_check(name, size) == 0)
  97. goto out_locked;
  98. s = __kmem_cache_alias(name, size, align, flags, ctor);
  99. if (s)
  100. goto out_locked;
  101. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  102. if (s) {
  103. s->object_size = s->size = size;
  104. s->align = align;
  105. s->ctor = ctor;
  106. s->name = kstrdup(name, GFP_KERNEL);
  107. if (!s->name) {
  108. kmem_cache_free(kmem_cache, s);
  109. err = -ENOMEM;
  110. goto out_locked;
  111. }
  112. err = __kmem_cache_create(s, flags);
  113. if (!err) {
  114. s->refcount = 1;
  115. list_add(&s->list, &slab_caches);
  116. } else {
  117. kfree(s->name);
  118. kmem_cache_free(kmem_cache, s);
  119. }
  120. } else
  121. err = -ENOMEM;
  122. out_locked:
  123. mutex_unlock(&slab_mutex);
  124. put_online_cpus();
  125. if (err) {
  126. if (flags & SLAB_PANIC)
  127. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  128. name, err);
  129. else {
  130. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  131. name, err);
  132. dump_stack();
  133. }
  134. return NULL;
  135. }
  136. return s;
  137. }
  138. EXPORT_SYMBOL(kmem_cache_create);
  139. void kmem_cache_destroy(struct kmem_cache *s)
  140. {
  141. get_online_cpus();
  142. mutex_lock(&slab_mutex);
  143. s->refcount--;
  144. if (!s->refcount) {
  145. list_del(&s->list);
  146. if (!__kmem_cache_shutdown(s)) {
  147. mutex_unlock(&slab_mutex);
  148. if (s->flags & SLAB_DESTROY_BY_RCU)
  149. rcu_barrier();
  150. kfree(s->name);
  151. kmem_cache_free(kmem_cache, s);
  152. } else {
  153. list_add(&s->list, &slab_caches);
  154. mutex_unlock(&slab_mutex);
  155. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  156. s->name);
  157. dump_stack();
  158. }
  159. } else {
  160. mutex_unlock(&slab_mutex);
  161. }
  162. put_online_cpus();
  163. }
  164. EXPORT_SYMBOL(kmem_cache_destroy);
  165. int slab_is_available(void)
  166. {
  167. return slab_state >= UP;
  168. }
  169. #ifdef CONFIG_SLABINFO
  170. static void print_slabinfo_header(struct seq_file *m)
  171. {
  172. /*
  173. * Output format version, so at least we can change it
  174. * without _too_ many complaints.
  175. */
  176. #ifdef CONFIG_DEBUG_SLAB
  177. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  178. #else
  179. seq_puts(m, "slabinfo - version: 2.1\n");
  180. #endif
  181. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  182. "<objperslab> <pagesperslab>");
  183. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  184. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  185. #ifdef CONFIG_DEBUG_SLAB
  186. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  187. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  188. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  189. #endif
  190. seq_putc(m, '\n');
  191. }
  192. static void *s_start(struct seq_file *m, loff_t *pos)
  193. {
  194. loff_t n = *pos;
  195. mutex_lock(&slab_mutex);
  196. if (!n)
  197. print_slabinfo_header(m);
  198. return seq_list_start(&slab_caches, *pos);
  199. }
  200. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  201. {
  202. return seq_list_next(p, &slab_caches, pos);
  203. }
  204. static void s_stop(struct seq_file *m, void *p)
  205. {
  206. mutex_unlock(&slab_mutex);
  207. }
  208. static int s_show(struct seq_file *m, void *p)
  209. {
  210. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  211. struct slabinfo sinfo;
  212. memset(&sinfo, 0, sizeof(sinfo));
  213. get_slabinfo(s, &sinfo);
  214. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  215. s->name, sinfo.active_objs, sinfo.num_objs, s->size,
  216. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  217. seq_printf(m, " : tunables %4u %4u %4u",
  218. sinfo.limit, sinfo.batchcount, sinfo.shared);
  219. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  220. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  221. slabinfo_show_stats(m, s);
  222. seq_putc(m, '\n');
  223. return 0;
  224. }
  225. /*
  226. * slabinfo_op - iterator that generates /proc/slabinfo
  227. *
  228. * Output layout:
  229. * cache-name
  230. * num-active-objs
  231. * total-objs
  232. * object size
  233. * num-active-slabs
  234. * total-slabs
  235. * num-pages-per-slab
  236. * + further values on SMP and with statistics enabled
  237. */
  238. static const struct seq_operations slabinfo_op = {
  239. .start = s_start,
  240. .next = s_next,
  241. .stop = s_stop,
  242. .show = s_show,
  243. };
  244. static int slabinfo_open(struct inode *inode, struct file *file)
  245. {
  246. return seq_open(file, &slabinfo_op);
  247. }
  248. static const struct file_operations proc_slabinfo_operations = {
  249. .open = slabinfo_open,
  250. .read = seq_read,
  251. .write = slabinfo_write,
  252. .llseek = seq_lseek,
  253. .release = seq_release,
  254. };
  255. static int __init slab_proc_init(void)
  256. {
  257. proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
  258. return 0;
  259. }
  260. module_init(slab_proc_init);
  261. #endif /* CONFIG_SLABINFO */