slab_common.c 11 KB

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