slab_common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifdef CONFIG_DEBUG_VM
  51. if (!name || in_interrupt() || size < sizeof(void *) ||
  52. size > KMALLOC_MAX_SIZE) {
  53. printk(KERN_ERR "kmem_cache_create(%s) integrity check"
  54. " failed\n", name);
  55. goto out;
  56. }
  57. #endif
  58. get_online_cpus();
  59. mutex_lock(&slab_mutex);
  60. #ifdef CONFIG_DEBUG_VM
  61. list_for_each_entry(s, &slab_caches, list) {
  62. char tmp;
  63. int res;
  64. /*
  65. * This happens when the module gets unloaded and doesn't
  66. * destroy its slab cache and no-one else reuses the vmalloc
  67. * area of the module. Print a warning.
  68. */
  69. res = probe_kernel_address(s->name, tmp);
  70. if (res) {
  71. printk(KERN_ERR
  72. "Slab cache with size %d has lost its name\n",
  73. s->object_size);
  74. continue;
  75. }
  76. if (!strcmp(s->name, name)) {
  77. printk(KERN_ERR "kmem_cache_create(%s): Cache name"
  78. " already exists.\n",
  79. name);
  80. dump_stack();
  81. s = NULL;
  82. goto oops;
  83. }
  84. }
  85. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  86. #endif
  87. s = __kmem_cache_create(name, size, align, flags, ctor);
  88. #ifdef CONFIG_DEBUG_VM
  89. oops:
  90. #endif
  91. mutex_unlock(&slab_mutex);
  92. put_online_cpus();
  93. #ifdef CONFIG_DEBUG_VM
  94. out:
  95. #endif
  96. if (!s && (flags & SLAB_PANIC))
  97. panic("kmem_cache_create: Failed to create slab '%s'\n", name);
  98. return s;
  99. }
  100. EXPORT_SYMBOL(kmem_cache_create);
  101. int slab_is_available(void)
  102. {
  103. return slab_state >= UP;
  104. }