slab_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. char *n;
  93. get_online_cpus();
  94. mutex_lock(&slab_mutex);
  95. if (!kmem_cache_sanity_check(name, size) == 0)
  96. goto out_locked;
  97. n = kstrdup(name, GFP_KERNEL);
  98. if (!n) {
  99. err = -ENOMEM;
  100. goto out_locked;
  101. }
  102. s = __kmem_cache_alias(name, size, align, flags, ctor);
  103. if (s)
  104. goto out_locked;
  105. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  106. if (s) {
  107. err = __kmem_cache_create(s, n, size, align, flags, ctor);
  108. if (!err)
  109. list_add(&s->list, &slab_caches);
  110. else {
  111. kfree(n);
  112. kmem_cache_free(kmem_cache, s);
  113. }
  114. } else {
  115. kfree(n);
  116. err = -ENOMEM;
  117. }
  118. out_locked:
  119. mutex_unlock(&slab_mutex);
  120. put_online_cpus();
  121. if (err) {
  122. if (flags & SLAB_PANIC)
  123. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  124. name, err);
  125. else {
  126. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  127. name, err);
  128. dump_stack();
  129. }
  130. return NULL;
  131. }
  132. if (s->refcount == 1) {
  133. err = sysfs_slab_add(s);
  134. if (err)
  135. printk(KERN_WARNING "kmem_cache_create(%s) failed to"
  136. " create sysfs entry. Error %d\n",
  137. name, err);
  138. }
  139. return s;
  140. }
  141. EXPORT_SYMBOL(kmem_cache_create);
  142. void kmem_cache_destroy(struct kmem_cache *s)
  143. {
  144. get_online_cpus();
  145. mutex_lock(&slab_mutex);
  146. s->refcount--;
  147. if (!s->refcount) {
  148. list_del(&s->list);
  149. if (!__kmem_cache_shutdown(s)) {
  150. if (s->flags & SLAB_DESTROY_BY_RCU)
  151. rcu_barrier();
  152. kfree(s->name);
  153. kmem_cache_free(kmem_cache, s);
  154. } else {
  155. list_add(&s->list, &slab_caches);
  156. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  157. s->name);
  158. dump_stack();
  159. }
  160. }
  161. mutex_unlock(&slab_mutex);
  162. put_online_cpus();
  163. }
  164. EXPORT_SYMBOL(kmem_cache_destroy);
  165. int slab_is_available(void)
  166. {
  167. return slab_state >= UP;
  168. }