slab_common.c 3.0 KB

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