slab_common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. * Figure out what the alignment of the objects will be given a set of
  67. * flags, a user specified alignment and the size of the objects.
  68. */
  69. unsigned long calculate_alignment(unsigned long flags,
  70. unsigned long align, unsigned long size)
  71. {
  72. /*
  73. * If the user wants hardware cache aligned objects then follow that
  74. * suggestion if the object is sufficiently large.
  75. *
  76. * The hardware cache alignment cannot override the specified
  77. * alignment though. If that is greater then use it.
  78. */
  79. if (flags & SLAB_HWCACHE_ALIGN) {
  80. unsigned long ralign = cache_line_size();
  81. while (size <= ralign / 2)
  82. ralign /= 2;
  83. align = max(align, ralign);
  84. }
  85. if (align < ARCH_SLAB_MINALIGN)
  86. align = ARCH_SLAB_MINALIGN;
  87. return ALIGN(align, sizeof(void *));
  88. }
  89. /*
  90. * kmem_cache_create - Create a cache.
  91. * @name: A string which is used in /proc/slabinfo to identify this cache.
  92. * @size: The size of objects to be created in this cache.
  93. * @align: The required alignment for the objects.
  94. * @flags: SLAB flags
  95. * @ctor: A constructor for the objects.
  96. *
  97. * Returns a ptr to the cache on success, NULL on failure.
  98. * Cannot be called within a interrupt, but can be interrupted.
  99. * The @ctor is run when new pages are allocated by the cache.
  100. *
  101. * The flags are
  102. *
  103. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  104. * to catch references to uninitialised memory.
  105. *
  106. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  107. * for buffer overruns.
  108. *
  109. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  110. * cacheline. This can be beneficial if you're counting cycles as closely
  111. * as davem.
  112. */
  113. struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
  114. unsigned long flags, void (*ctor)(void *))
  115. {
  116. struct kmem_cache *s = NULL;
  117. int err = 0;
  118. get_online_cpus();
  119. mutex_lock(&slab_mutex);
  120. if (!kmem_cache_sanity_check(name, size) == 0)
  121. goto out_locked;
  122. /*
  123. * Some allocators will constraint the set of valid flags to a subset
  124. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  125. * case, and we'll just provide them with a sanitized version of the
  126. * passed flags.
  127. */
  128. flags &= CACHE_CREATE_MASK;
  129. s = __kmem_cache_alias(name, size, align, flags, ctor);
  130. if (s)
  131. goto out_locked;
  132. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  133. if (s) {
  134. s->object_size = s->size = size;
  135. s->align = calculate_alignment(flags, align, size);
  136. s->ctor = ctor;
  137. s->name = kstrdup(name, GFP_KERNEL);
  138. if (!s->name) {
  139. kmem_cache_free(kmem_cache, s);
  140. err = -ENOMEM;
  141. goto out_locked;
  142. }
  143. err = __kmem_cache_create(s, flags);
  144. if (!err) {
  145. s->refcount = 1;
  146. list_add(&s->list, &slab_caches);
  147. } else {
  148. kfree(s->name);
  149. kmem_cache_free(kmem_cache, s);
  150. }
  151. } else
  152. err = -ENOMEM;
  153. out_locked:
  154. mutex_unlock(&slab_mutex);
  155. put_online_cpus();
  156. if (err) {
  157. if (flags & SLAB_PANIC)
  158. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  159. name, err);
  160. else {
  161. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  162. name, err);
  163. dump_stack();
  164. }
  165. return NULL;
  166. }
  167. return s;
  168. }
  169. EXPORT_SYMBOL(kmem_cache_create);
  170. void kmem_cache_destroy(struct kmem_cache *s)
  171. {
  172. get_online_cpus();
  173. mutex_lock(&slab_mutex);
  174. s->refcount--;
  175. if (!s->refcount) {
  176. list_del(&s->list);
  177. if (!__kmem_cache_shutdown(s)) {
  178. mutex_unlock(&slab_mutex);
  179. if (s->flags & SLAB_DESTROY_BY_RCU)
  180. rcu_barrier();
  181. kfree(s->name);
  182. kmem_cache_free(kmem_cache, s);
  183. } else {
  184. list_add(&s->list, &slab_caches);
  185. mutex_unlock(&slab_mutex);
  186. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  187. s->name);
  188. dump_stack();
  189. }
  190. } else {
  191. mutex_unlock(&slab_mutex);
  192. }
  193. put_online_cpus();
  194. }
  195. EXPORT_SYMBOL(kmem_cache_destroy);
  196. int slab_is_available(void)
  197. {
  198. return slab_state >= UP;
  199. }
  200. #ifndef CONFIG_SLOB
  201. /* Create a cache during boot when no slab services are available yet */
  202. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  203. unsigned long flags)
  204. {
  205. int err;
  206. s->name = name;
  207. s->size = s->object_size = size;
  208. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  209. err = __kmem_cache_create(s, flags);
  210. if (err)
  211. panic("Creation of kmalloc slab %s size=%zd failed. Reason %d\n",
  212. name, size, err);
  213. s->refcount = -1; /* Exempt from merging for now */
  214. }
  215. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  216. unsigned long flags)
  217. {
  218. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  219. if (!s)
  220. panic("Out of memory when creating slab %s\n", name);
  221. create_boot_cache(s, name, size, flags);
  222. list_add(&s->list, &slab_caches);
  223. s->refcount = 1;
  224. return s;
  225. }
  226. #endif /* !CONFIG_SLOB */
  227. #ifdef CONFIG_SLABINFO
  228. static void print_slabinfo_header(struct seq_file *m)
  229. {
  230. /*
  231. * Output format version, so at least we can change it
  232. * without _too_ many complaints.
  233. */
  234. #ifdef CONFIG_DEBUG_SLAB
  235. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  236. #else
  237. seq_puts(m, "slabinfo - version: 2.1\n");
  238. #endif
  239. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  240. "<objperslab> <pagesperslab>");
  241. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  242. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  243. #ifdef CONFIG_DEBUG_SLAB
  244. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  245. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  246. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  247. #endif
  248. seq_putc(m, '\n');
  249. }
  250. static void *s_start(struct seq_file *m, loff_t *pos)
  251. {
  252. loff_t n = *pos;
  253. mutex_lock(&slab_mutex);
  254. if (!n)
  255. print_slabinfo_header(m);
  256. return seq_list_start(&slab_caches, *pos);
  257. }
  258. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  259. {
  260. return seq_list_next(p, &slab_caches, pos);
  261. }
  262. static void s_stop(struct seq_file *m, void *p)
  263. {
  264. mutex_unlock(&slab_mutex);
  265. }
  266. static int s_show(struct seq_file *m, void *p)
  267. {
  268. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  269. struct slabinfo sinfo;
  270. memset(&sinfo, 0, sizeof(sinfo));
  271. get_slabinfo(s, &sinfo);
  272. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  273. s->name, sinfo.active_objs, sinfo.num_objs, s->size,
  274. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  275. seq_printf(m, " : tunables %4u %4u %4u",
  276. sinfo.limit, sinfo.batchcount, sinfo.shared);
  277. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  278. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  279. slabinfo_show_stats(m, s);
  280. seq_putc(m, '\n');
  281. return 0;
  282. }
  283. /*
  284. * slabinfo_op - iterator that generates /proc/slabinfo
  285. *
  286. * Output layout:
  287. * cache-name
  288. * num-active-objs
  289. * total-objs
  290. * object size
  291. * num-active-slabs
  292. * total-slabs
  293. * num-pages-per-slab
  294. * + further values on SMP and with statistics enabled
  295. */
  296. static const struct seq_operations slabinfo_op = {
  297. .start = s_start,
  298. .next = s_next,
  299. .stop = s_stop,
  300. .show = s_show,
  301. };
  302. static int slabinfo_open(struct inode *inode, struct file *file)
  303. {
  304. return seq_open(file, &slabinfo_op);
  305. }
  306. static const struct file_operations proc_slabinfo_operations = {
  307. .open = slabinfo_open,
  308. .read = seq_read,
  309. .write = slabinfo_write,
  310. .llseek = seq_lseek,
  311. .release = seq_release,
  312. };
  313. static int __init slab_proc_init(void)
  314. {
  315. proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
  316. return 0;
  317. }
  318. module_init(slab_proc_init);
  319. #endif /* CONFIG_SLABINFO */