slab_common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_create(n, size, align, flags, ctor);
  106. if (s) {
  107. /*
  108. * Check if the slab has actually been created and if it was a
  109. * real instatiation. Aliases do not belong on the list
  110. */
  111. if (s->refcount == 1)
  112. list_add(&s->list, &slab_caches);
  113. } else {
  114. kfree(n);
  115. err = -ENOSYS; /* Until __kmem_cache_create returns code */
  116. }
  117. out_locked:
  118. mutex_unlock(&slab_mutex);
  119. put_online_cpus();
  120. if (err) {
  121. if (flags & SLAB_PANIC)
  122. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  123. name, err);
  124. else {
  125. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  126. name, err);
  127. dump_stack();
  128. }
  129. return NULL;
  130. }
  131. return s;
  132. }
  133. EXPORT_SYMBOL(kmem_cache_create);
  134. void kmem_cache_destroy(struct kmem_cache *s)
  135. {
  136. get_online_cpus();
  137. mutex_lock(&slab_mutex);
  138. s->refcount--;
  139. if (!s->refcount) {
  140. list_del(&s->list);
  141. if (!__kmem_cache_shutdown(s)) {
  142. if (s->flags & SLAB_DESTROY_BY_RCU)
  143. rcu_barrier();
  144. kfree(s->name);
  145. kmem_cache_free(kmem_cache, s);
  146. } else {
  147. list_add(&s->list, &slab_caches);
  148. printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
  149. s->name);
  150. dump_stack();
  151. }
  152. }
  153. mutex_unlock(&slab_mutex);
  154. put_online_cpus();
  155. }
  156. EXPORT_SYMBOL(kmem_cache_destroy);
  157. int slab_is_available(void)
  158. {
  159. return slab_state >= UP;
  160. }