slab_common.c 9.7 KB

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