slob.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * SLOB Allocator: Simple List Of Blocks
  3. *
  4. * Matt Mackall <mpm@selenic.com> 12/30/03
  5. *
  6. * How SLOB works:
  7. *
  8. * The core of SLOB is a traditional K&R style heap allocator, with
  9. * support for returning aligned objects. The granularity of this
  10. * allocator is 8 bytes on x86, though it's perhaps possible to reduce
  11. * this to 4 if it's deemed worth the effort. The slob heap is a
  12. * singly-linked list of pages from __get_free_page, grown on demand
  13. * and allocation from the heap is currently first-fit.
  14. *
  15. * Above this is an implementation of kmalloc/kfree. Blocks returned
  16. * from kmalloc are 8-byte aligned and prepended with a 8-byte header.
  17. * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
  18. * __get_free_pages directly so that it can return page-aligned blocks
  19. * and keeps a linked list of such pages and their orders. These
  20. * objects are detected in kfree() by their page alignment.
  21. *
  22. * SLAB is emulated on top of SLOB by simply calling constructors and
  23. * destructors for every SLAB allocation. Objects are returned with
  24. * the 8-byte alignment unless the SLAB_MUST_HWCACHE_ALIGN flag is
  25. * set, in which case the low-level allocator will fragment blocks to
  26. * create the proper alignment. Again, objects of page-size or greater
  27. * are allocated by calling __get_free_pages. As SLAB objects know
  28. * their size, no separate size bookkeeping is necessary and there is
  29. * essentially no allocation space overhead.
  30. */
  31. #include <linux/config.h>
  32. #include <linux/slab.h>
  33. #include <linux/mm.h>
  34. #include <linux/cache.h>
  35. #include <linux/init.h>
  36. #include <linux/module.h>
  37. #include <linux/timer.h>
  38. struct slob_block {
  39. int units;
  40. struct slob_block *next;
  41. };
  42. typedef struct slob_block slob_t;
  43. #define SLOB_UNIT sizeof(slob_t)
  44. #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  45. #define SLOB_ALIGN L1_CACHE_BYTES
  46. struct bigblock {
  47. int order;
  48. void *pages;
  49. struct bigblock *next;
  50. };
  51. typedef struct bigblock bigblock_t;
  52. static slob_t arena = { .next = &arena, .units = 1 };
  53. static slob_t *slobfree = &arena;
  54. static bigblock_t *bigblocks;
  55. static DEFINE_SPINLOCK(slob_lock);
  56. static DEFINE_SPINLOCK(block_lock);
  57. static void slob_free(void *b, int size);
  58. static void *slob_alloc(size_t size, gfp_t gfp, int align)
  59. {
  60. slob_t *prev, *cur, *aligned = 0;
  61. int delta = 0, units = SLOB_UNITS(size);
  62. unsigned long flags;
  63. spin_lock_irqsave(&slob_lock, flags);
  64. prev = slobfree;
  65. for (cur = prev->next; ; prev = cur, cur = cur->next) {
  66. if (align) {
  67. aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  68. delta = aligned - cur;
  69. }
  70. if (cur->units >= units + delta) { /* room enough? */
  71. if (delta) { /* need to fragment head to align? */
  72. aligned->units = cur->units - delta;
  73. aligned->next = cur->next;
  74. cur->next = aligned;
  75. cur->units = delta;
  76. prev = cur;
  77. cur = aligned;
  78. }
  79. if (cur->units == units) /* exact fit? */
  80. prev->next = cur->next; /* unlink */
  81. else { /* fragment */
  82. prev->next = cur + units;
  83. prev->next->units = cur->units - units;
  84. prev->next->next = cur->next;
  85. cur->units = units;
  86. }
  87. slobfree = prev;
  88. spin_unlock_irqrestore(&slob_lock, flags);
  89. return cur;
  90. }
  91. if (cur == slobfree) {
  92. spin_unlock_irqrestore(&slob_lock, flags);
  93. if (size == PAGE_SIZE) /* trying to shrink arena? */
  94. return 0;
  95. cur = (slob_t *)__get_free_page(gfp);
  96. if (!cur)
  97. return 0;
  98. slob_free(cur, PAGE_SIZE);
  99. spin_lock_irqsave(&slob_lock, flags);
  100. cur = slobfree;
  101. }
  102. }
  103. }
  104. static void slob_free(void *block, int size)
  105. {
  106. slob_t *cur, *b = (slob_t *)block;
  107. unsigned long flags;
  108. if (!block)
  109. return;
  110. if (size)
  111. b->units = SLOB_UNITS(size);
  112. /* Find reinsertion point */
  113. spin_lock_irqsave(&slob_lock, flags);
  114. for (cur = slobfree; !(b > cur && b < cur->next); cur = cur->next)
  115. if (cur >= cur->next && (b > cur || b < cur->next))
  116. break;
  117. if (b + b->units == cur->next) {
  118. b->units += cur->next->units;
  119. b->next = cur->next->next;
  120. } else
  121. b->next = cur->next;
  122. if (cur + cur->units == b) {
  123. cur->units += b->units;
  124. cur->next = b->next;
  125. } else
  126. cur->next = b;
  127. slobfree = cur;
  128. spin_unlock_irqrestore(&slob_lock, flags);
  129. }
  130. static int FASTCALL(find_order(int size));
  131. static int fastcall find_order(int size)
  132. {
  133. int order = 0;
  134. for ( ; size > 4096 ; size >>=1)
  135. order++;
  136. return order;
  137. }
  138. void *kmalloc(size_t size, gfp_t gfp)
  139. {
  140. slob_t *m;
  141. bigblock_t *bb;
  142. unsigned long flags;
  143. if (size < PAGE_SIZE - SLOB_UNIT) {
  144. m = slob_alloc(size + SLOB_UNIT, gfp, 0);
  145. return m ? (void *)(m + 1) : 0;
  146. }
  147. bb = slob_alloc(sizeof(bigblock_t), gfp, 0);
  148. if (!bb)
  149. return 0;
  150. bb->order = find_order(size);
  151. bb->pages = (void *)__get_free_pages(gfp, bb->order);
  152. if (bb->pages) {
  153. spin_lock_irqsave(&block_lock, flags);
  154. bb->next = bigblocks;
  155. bigblocks = bb;
  156. spin_unlock_irqrestore(&block_lock, flags);
  157. return bb->pages;
  158. }
  159. slob_free(bb, sizeof(bigblock_t));
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(kmalloc);
  163. void kfree(const void *block)
  164. {
  165. bigblock_t *bb, **last = &bigblocks;
  166. unsigned long flags;
  167. if (!block)
  168. return;
  169. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  170. /* might be on the big block list */
  171. spin_lock_irqsave(&block_lock, flags);
  172. for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) {
  173. if (bb->pages == block) {
  174. *last = bb->next;
  175. spin_unlock_irqrestore(&block_lock, flags);
  176. free_pages((unsigned long)block, bb->order);
  177. slob_free(bb, sizeof(bigblock_t));
  178. return;
  179. }
  180. }
  181. spin_unlock_irqrestore(&block_lock, flags);
  182. }
  183. slob_free((slob_t *)block - 1, 0);
  184. return;
  185. }
  186. EXPORT_SYMBOL(kfree);
  187. unsigned int ksize(const void *block)
  188. {
  189. bigblock_t *bb;
  190. unsigned long flags;
  191. if (!block)
  192. return 0;
  193. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  194. spin_lock_irqsave(&block_lock, flags);
  195. for (bb = bigblocks; bb; bb = bb->next)
  196. if (bb->pages == block) {
  197. spin_unlock_irqrestore(&slob_lock, flags);
  198. return PAGE_SIZE << bb->order;
  199. }
  200. spin_unlock_irqrestore(&block_lock, flags);
  201. }
  202. return ((slob_t *)block - 1)->units * SLOB_UNIT;
  203. }
  204. struct kmem_cache {
  205. unsigned int size, align;
  206. const char *name;
  207. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  208. void (*dtor)(void *, struct kmem_cache *, unsigned long);
  209. };
  210. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  211. size_t align, unsigned long flags,
  212. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  213. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  214. {
  215. struct kmem_cache *c;
  216. c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
  217. if (c) {
  218. c->name = name;
  219. c->size = size;
  220. c->ctor = ctor;
  221. c->dtor = dtor;
  222. /* ignore alignment unless it's forced */
  223. c->align = (flags & SLAB_MUST_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  224. if (c->align < align)
  225. c->align = align;
  226. }
  227. return c;
  228. }
  229. EXPORT_SYMBOL(kmem_cache_create);
  230. int kmem_cache_destroy(struct kmem_cache *c)
  231. {
  232. slob_free(c, sizeof(struct kmem_cache));
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(kmem_cache_destroy);
  236. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
  237. {
  238. void *b;
  239. if (c->size < PAGE_SIZE)
  240. b = slob_alloc(c->size, flags, c->align);
  241. else
  242. b = (void *)__get_free_pages(flags, find_order(c->size));
  243. if (c->ctor)
  244. c->ctor(b, c, SLAB_CTOR_CONSTRUCTOR);
  245. return b;
  246. }
  247. EXPORT_SYMBOL(kmem_cache_alloc);
  248. void kmem_cache_free(struct kmem_cache *c, void *b)
  249. {
  250. if (c->dtor)
  251. c->dtor(b, c, 0);
  252. if (c->size < PAGE_SIZE)
  253. slob_free(b, c->size);
  254. else
  255. free_pages((unsigned long)b, find_order(c->size));
  256. }
  257. EXPORT_SYMBOL(kmem_cache_free);
  258. unsigned int kmem_cache_size(struct kmem_cache *c)
  259. {
  260. return c->size;
  261. }
  262. EXPORT_SYMBOL(kmem_cache_size);
  263. const char *kmem_cache_name(struct kmem_cache *c)
  264. {
  265. return c->name;
  266. }
  267. EXPORT_SYMBOL(kmem_cache_name);
  268. static struct timer_list slob_timer = TIMER_INITIALIZER(
  269. (void (*)(unsigned long))kmem_cache_init, 0, 0);
  270. void kmem_cache_init(void)
  271. {
  272. void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1);
  273. if (p)
  274. free_page((unsigned long)p);
  275. mod_timer(&slob_timer, jiffies + HZ);
  276. }
  277. atomic_t slab_reclaim_pages = ATOMIC_INIT(0);
  278. EXPORT_SYMBOL(slab_reclaim_pages);
  279. #ifdef CONFIG_SMP
  280. void *__alloc_percpu(size_t size, size_t align)
  281. {
  282. int i;
  283. struct percpu_data *pdata = kmalloc(sizeof (*pdata), GFP_KERNEL);
  284. if (!pdata)
  285. return NULL;
  286. for (i = 0; i < NR_CPUS; i++) {
  287. if (!cpu_possible(i))
  288. continue;
  289. pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
  290. if (!pdata->ptrs[i])
  291. goto unwind_oom;
  292. memset(pdata->ptrs[i], 0, size);
  293. }
  294. /* Catch derefs w/o wrappers */
  295. return (void *) (~(unsigned long) pdata);
  296. unwind_oom:
  297. while (--i >= 0) {
  298. if (!cpu_possible(i))
  299. continue;
  300. kfree(pdata->ptrs[i]);
  301. }
  302. kfree(pdata);
  303. return NULL;
  304. }
  305. EXPORT_SYMBOL(__alloc_percpu);
  306. void
  307. free_percpu(const void *objp)
  308. {
  309. int i;
  310. struct percpu_data *p = (struct percpu_data *) (~(unsigned long) objp);
  311. for (i = 0; i < NR_CPUS; i++) {
  312. if (!cpu_possible(i))
  313. continue;
  314. kfree(p->ptrs[i]);
  315. }
  316. kfree(p);
  317. }
  318. EXPORT_SYMBOL(free_percpu);
  319. #endif