slab_common.c 4.0 KB

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