slab_common.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <asm/cacheflush.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/page.h>
  18. #include "slab.h"
  19. enum slab_state slab_state;
  20. LIST_HEAD(slab_caches);
  21. DEFINE_MUTEX(slab_mutex);
  22. /*
  23. * kmem_cache_create - Create a cache.
  24. * @name: A string which is used in /proc/slabinfo to identify this cache.
  25. * @size: The size of objects to be created in this cache.
  26. * @align: The required alignment for the objects.
  27. * @flags: SLAB flags
  28. * @ctor: A constructor for the objects.
  29. *
  30. * Returns a ptr to the cache on success, NULL on failure.
  31. * Cannot be called within a interrupt, but can be interrupted.
  32. * The @ctor is run when new pages are allocated by the cache.
  33. *
  34. * The flags are
  35. *
  36. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  37. * to catch references to uninitialised memory.
  38. *
  39. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  40. * for buffer overruns.
  41. *
  42. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  43. * cacheline. This can be beneficial if you're counting cycles as closely
  44. * as davem.
  45. */
  46. struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
  47. unsigned long flags, void (*ctor)(void *))
  48. {
  49. struct kmem_cache *s = NULL;
  50. get_online_cpus();
  51. mutex_lock(&slab_mutex);
  52. #ifdef CONFIG_DEBUG_VM
  53. if (!name || in_interrupt() || size < sizeof(void *) ||
  54. size > KMALLOC_MAX_SIZE) {
  55. printk(KERN_ERR "kmem_cache_create(%s) integrity check"
  56. " failed\n", name);
  57. goto oops;
  58. }
  59. list_for_each_entry(s, &slab_caches, list) {
  60. char tmp;
  61. int res;
  62. /*
  63. * This happens when the module gets unloaded and doesn't
  64. * destroy its slab cache and no-one else reuses the vmalloc
  65. * area of the module. Print a warning.
  66. */
  67. res = probe_kernel_address(s->name, tmp);
  68. if (res) {
  69. printk(KERN_ERR
  70. "Slab cache with size %d has lost its name\n",
  71. s->object_size);
  72. continue;
  73. }
  74. if (!strcmp(s->name, name)) {
  75. printk(KERN_ERR "kmem_cache_create(%s): Cache name"
  76. " already exists.\n",
  77. name);
  78. dump_stack();
  79. s = NULL;
  80. goto oops;
  81. }
  82. }
  83. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  84. #endif
  85. s = __kmem_cache_create(name, size, align, flags, ctor);
  86. #ifdef CONFIG_DEBUG_VM
  87. oops:
  88. #endif
  89. mutex_unlock(&slab_mutex);
  90. put_online_cpus();
  91. if (!s && (flags & SLAB_PANIC))
  92. panic("kmem_cache_create: Failed to create slab '%s'\n", name);
  93. return s;
  94. }
  95. EXPORT_SYMBOL(kmem_cache_create);
  96. int slab_is_available(void)
  97. {
  98. return slab_state >= UP;
  99. }