slab_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. struct kmem_cache *kmem_cache;
  23. #ifdef CONFIG_DEBUG_VM
  24. static int kmem_cache_sanity_check(const char *name, size_t size)
  25. {
  26. struct kmem_cache *s = NULL;
  27. if (!name || in_interrupt() || size < sizeof(void *) ||
  28. size > KMALLOC_MAX_SIZE) {
  29. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  30. return -EINVAL;
  31. }
  32. list_for_each_entry(s, &slab_caches, list) {
  33. char tmp;
  34. int res;
  35. /*
  36. * This happens when the module gets unloaded and doesn't
  37. * destroy its slab cache and no-one else reuses the vmalloc
  38. * area of the module. Print a warning.
  39. */
  40. res = probe_kernel_address(s->name, tmp);
  41. if (res) {
  42. pr_err("Slab cache with size %d has lost its name\n",
  43. s->object_size);
  44. continue;
  45. }
  46. if (!strcmp(s->name, name)) {
  47. pr_err("%s (%s): Cache name already exists.\n",
  48. __func__, name);
  49. dump_stack();
  50. s = NULL;
  51. return -EINVAL;
  52. }
  53. }
  54. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  55. return 0;
  56. }
  57. #else
  58. static inline int kmem_cache_sanity_check(const char *name, size_t size)
  59. {
  60. return 0;
  61. }
  62. #endif
  63. /*
  64. * kmem_cache_create - Create a cache.
  65. * @name: A string which is used in /proc/slabinfo to identify this cache.
  66. * @size: The size of objects to be created in this cache.
  67. * @align: The required alignment for the objects.
  68. * @flags: SLAB flags
  69. * @ctor: A constructor for the objects.
  70. *
  71. * Returns a ptr to the cache on success, NULL on failure.
  72. * Cannot be called within a interrupt, but can be interrupted.
  73. * The @ctor is run when new pages are allocated by the cache.
  74. *
  75. * The flags are
  76. *
  77. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  78. * to catch references to uninitialised memory.
  79. *
  80. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  81. * for buffer overruns.
  82. *
  83. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  84. * cacheline. This can be beneficial if you're counting cycles as closely
  85. * as davem.
  86. */
  87. struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
  88. unsigned long flags, void (*ctor)(void *))
  89. {
  90. struct kmem_cache *s = NULL;
  91. int err = 0;
  92. get_online_cpus();
  93. mutex_lock(&slab_mutex);
  94. if (!kmem_cache_sanity_check(name, size) == 0)
  95. goto out_locked;
  96. s = __kmem_cache_alias(name, size, align, flags, ctor);
  97. if (s)
  98. goto out_locked;
  99. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  100. if (s) {
  101. s->object_size = s->size = size;
  102. s->align = align;
  103. s->ctor = ctor;
  104. s->name = kstrdup(name, GFP_KERNEL);
  105. if (!s->name) {
  106. kmem_cache_free(kmem_cache, s);
  107. err = -ENOMEM;
  108. goto out_locked;
  109. }
  110. err = __kmem_cache_create(s, flags);
  111. if (!err) {
  112. s->refcount = 1;
  113. list_add(&s->list, &slab_caches);
  114. } else {
  115. kfree(s->name);
  116. kmem_cache_free(kmem_cache, s);
  117. }
  118. } else
  119. err = -ENOMEM;
  120. out_locked:
  121. mutex_unlock(&slab_mutex);
  122. put_online_cpus();
  123. if (err) {
  124. if (flags & SLAB_PANIC)
  125. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  126. name, err);
  127. else {
  128. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  129. name, err);
  130. dump_stack();
  131. }
  132. return NULL;
  133. }
  134. return s;
  135. }
  136. EXPORT_SYMBOL(kmem_cache_create);
  137. void kmem_cache_destroy(struct kmem_cache *s)
  138. {
  139. get_online_cpus();
  140. mutex_lock(&slab_mutex);
  141. s->refcount--;
  142. if (!s->refcount) {
  143. list_del(&s->list);
  144. if (!__kmem_cache_shutdown(s)) {
  145. mutex_unlock(&slab_mutex);
  146. if (s->flags & SLAB_DESTROY_BY_RCU)
  147. rcu_barrier();
  148. kfree(s->name);
  149. kmem_cache_free(kmem_cache, s);
  150. } else {
  151. list_add(&s->list, &slab_caches);
  152. mutex_unlock(&slab_mutex);
  153. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  154. s->name);
  155. dump_stack();
  156. }
  157. } else {
  158. mutex_unlock(&slab_mutex);
  159. }
  160. put_online_cpus();
  161. }
  162. EXPORT_SYMBOL(kmem_cache_destroy);
  163. int slab_is_available(void)
  164. {
  165. return slab_state >= UP;
  166. }