slab_common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /* Embrace davem */
  130. flags |= SLAB_HWCACHE_ALIGN;
  131. s = __kmem_cache_alias(name, size, align, flags, ctor);
  132. if (s)
  133. goto out_locked;
  134. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  135. if (s) {
  136. s->object_size = s->size = size;
  137. s->align = calculate_alignment(flags, align, size);
  138. s->ctor = ctor;
  139. s->name = kstrdup(name, GFP_KERNEL);
  140. if (!s->name) {
  141. kmem_cache_free(kmem_cache, s);
  142. err = -ENOMEM;
  143. goto out_locked;
  144. }
  145. err = __kmem_cache_create(s, flags);
  146. if (!err) {
  147. s->refcount = 1;
  148. list_add(&s->list, &slab_caches);
  149. } else {
  150. kfree(s->name);
  151. kmem_cache_free(kmem_cache, s);
  152. }
  153. } else
  154. err = -ENOMEM;
  155. out_locked:
  156. mutex_unlock(&slab_mutex);
  157. put_online_cpus();
  158. if (err) {
  159. if (flags & SLAB_PANIC)
  160. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  161. name, err);
  162. else {
  163. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  164. name, err);
  165. dump_stack();
  166. }
  167. return NULL;
  168. }
  169. return s;
  170. }
  171. EXPORT_SYMBOL(kmem_cache_create);
  172. void kmem_cache_destroy(struct kmem_cache *s)
  173. {
  174. get_online_cpus();
  175. mutex_lock(&slab_mutex);
  176. s->refcount--;
  177. if (!s->refcount) {
  178. list_del(&s->list);
  179. if (!__kmem_cache_shutdown(s)) {
  180. mutex_unlock(&slab_mutex);
  181. if (s->flags & SLAB_DESTROY_BY_RCU)
  182. rcu_barrier();
  183. kfree(s->name);
  184. kmem_cache_free(kmem_cache, s);
  185. } else {
  186. list_add(&s->list, &slab_caches);
  187. mutex_unlock(&slab_mutex);
  188. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  189. s->name);
  190. dump_stack();
  191. }
  192. } else {
  193. mutex_unlock(&slab_mutex);
  194. }
  195. put_online_cpus();
  196. }
  197. EXPORT_SYMBOL(kmem_cache_destroy);
  198. int slab_is_available(void)
  199. {
  200. return slab_state >= UP;
  201. }
  202. #ifndef CONFIG_SLOB
  203. /* Create a cache during boot when no slab services are available yet */
  204. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  205. unsigned long flags)
  206. {
  207. int err;
  208. s->name = name;
  209. s->size = s->object_size = size;
  210. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  211. err = __kmem_cache_create(s, flags);
  212. if (err)
  213. panic("Creation of kmalloc slab %s size=%zd failed. Reason %d\n",
  214. name, size, err);
  215. s->refcount = -1; /* Exempt from merging for now */
  216. }
  217. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  218. unsigned long flags)
  219. {
  220. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  221. if (!s)
  222. panic("Out of memory when creating slab %s\n", name);
  223. create_boot_cache(s, name, size, flags);
  224. list_add(&s->list, &slab_caches);
  225. s->refcount = 1;
  226. return s;
  227. }
  228. #endif /* !CONFIG_SLOB */
  229. #ifdef CONFIG_SLABINFO
  230. static void print_slabinfo_header(struct seq_file *m)
  231. {
  232. /*
  233. * Output format version, so at least we can change it
  234. * without _too_ many complaints.
  235. */
  236. #ifdef CONFIG_DEBUG_SLAB
  237. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  238. #else
  239. seq_puts(m, "slabinfo - version: 2.1\n");
  240. #endif
  241. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  242. "<objperslab> <pagesperslab>");
  243. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  244. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  245. #ifdef CONFIG_DEBUG_SLAB
  246. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  247. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  248. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  249. #endif
  250. seq_putc(m, '\n');
  251. }
  252. static void *s_start(struct seq_file *m, loff_t *pos)
  253. {
  254. loff_t n = *pos;
  255. mutex_lock(&slab_mutex);
  256. if (!n)
  257. print_slabinfo_header(m);
  258. return seq_list_start(&slab_caches, *pos);
  259. }
  260. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  261. {
  262. return seq_list_next(p, &slab_caches, pos);
  263. }
  264. static void s_stop(struct seq_file *m, void *p)
  265. {
  266. mutex_unlock(&slab_mutex);
  267. }
  268. static int s_show(struct seq_file *m, void *p)
  269. {
  270. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  271. struct slabinfo sinfo;
  272. memset(&sinfo, 0, sizeof(sinfo));
  273. get_slabinfo(s, &sinfo);
  274. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  275. s->name, sinfo.active_objs, sinfo.num_objs, s->size,
  276. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  277. seq_printf(m, " : tunables %4u %4u %4u",
  278. sinfo.limit, sinfo.batchcount, sinfo.shared);
  279. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  280. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  281. slabinfo_show_stats(m, s);
  282. seq_putc(m, '\n');
  283. return 0;
  284. }
  285. /*
  286. * slabinfo_op - iterator that generates /proc/slabinfo
  287. *
  288. * Output layout:
  289. * cache-name
  290. * num-active-objs
  291. * total-objs
  292. * object size
  293. * num-active-slabs
  294. * total-slabs
  295. * num-pages-per-slab
  296. * + further values on SMP and with statistics enabled
  297. */
  298. static const struct seq_operations slabinfo_op = {
  299. .start = s_start,
  300. .next = s_next,
  301. .stop = s_stop,
  302. .show = s_show,
  303. };
  304. static int slabinfo_open(struct inode *inode, struct file *file)
  305. {
  306. return seq_open(file, &slabinfo_op);
  307. }
  308. static const struct file_operations proc_slabinfo_operations = {
  309. .open = slabinfo_open,
  310. .read = seq_read,
  311. .write = slabinfo_write,
  312. .llseek = seq_lseek,
  313. .release = seq_release,
  314. };
  315. static int __init slab_proc_init(void)
  316. {
  317. proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
  318. return 0;
  319. }
  320. module_init(slab_proc_init);
  321. #endif /* CONFIG_SLABINFO */