slob.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_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/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/cache.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/timer.h>
  37. struct slob_block {
  38. int units;
  39. struct slob_block *next;
  40. };
  41. typedef struct slob_block slob_t;
  42. #define SLOB_UNIT sizeof(slob_t)
  43. #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  44. #define SLOB_ALIGN L1_CACHE_BYTES
  45. struct bigblock {
  46. int order;
  47. void *pages;
  48. struct bigblock *next;
  49. };
  50. typedef struct bigblock bigblock_t;
  51. static slob_t arena = { .next = &arena, .units = 1 };
  52. static slob_t *slobfree = &arena;
  53. static bigblock_t *bigblocks;
  54. static DEFINE_SPINLOCK(slob_lock);
  55. static DEFINE_SPINLOCK(block_lock);
  56. static void slob_free(void *b, int size);
  57. static void slob_timer_cbk(void);
  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. void *__kmalloc(size_t size, gfp_t gfp)
  131. {
  132. slob_t *m;
  133. bigblock_t *bb;
  134. unsigned long flags;
  135. if (size < PAGE_SIZE - SLOB_UNIT) {
  136. m = slob_alloc(size + SLOB_UNIT, gfp, 0);
  137. return m ? (void *)(m + 1) : 0;
  138. }
  139. bb = slob_alloc(sizeof(bigblock_t), gfp, 0);
  140. if (!bb)
  141. return 0;
  142. bb->order = get_order(size);
  143. bb->pages = (void *)__get_free_pages(gfp, bb->order);
  144. if (bb->pages) {
  145. spin_lock_irqsave(&block_lock, flags);
  146. bb->next = bigblocks;
  147. bigblocks = bb;
  148. spin_unlock_irqrestore(&block_lock, flags);
  149. return bb->pages;
  150. }
  151. slob_free(bb, sizeof(bigblock_t));
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(__kmalloc);
  155. /**
  156. * krealloc - reallocate memory. The contents will remain unchanged.
  157. *
  158. * @p: object to reallocate memory for.
  159. * @new_size: how many bytes of memory are required.
  160. * @flags: the type of memory to allocate.
  161. *
  162. * The contents of the object pointed to are preserved up to the
  163. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  164. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  165. * %NULL pointer, the object pointed to is freed.
  166. */
  167. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  168. {
  169. void *ret;
  170. if (unlikely(!p))
  171. return kmalloc_track_caller(new_size, flags);
  172. if (unlikely(!new_size)) {
  173. kfree(p);
  174. return NULL;
  175. }
  176. ret = kmalloc_track_caller(new_size, flags);
  177. if (ret) {
  178. memcpy(ret, p, min(new_size, ksize(p)));
  179. kfree(p);
  180. }
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(krealloc);
  184. void kfree(const void *block)
  185. {
  186. bigblock_t *bb, **last = &bigblocks;
  187. unsigned long flags;
  188. if (!block)
  189. return;
  190. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  191. /* might be on the big block list */
  192. spin_lock_irqsave(&block_lock, flags);
  193. for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) {
  194. if (bb->pages == block) {
  195. *last = bb->next;
  196. spin_unlock_irqrestore(&block_lock, flags);
  197. free_pages((unsigned long)block, bb->order);
  198. slob_free(bb, sizeof(bigblock_t));
  199. return;
  200. }
  201. }
  202. spin_unlock_irqrestore(&block_lock, flags);
  203. }
  204. slob_free((slob_t *)block - 1, 0);
  205. return;
  206. }
  207. EXPORT_SYMBOL(kfree);
  208. size_t ksize(const void *block)
  209. {
  210. bigblock_t *bb;
  211. unsigned long flags;
  212. if (!block)
  213. return 0;
  214. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  215. spin_lock_irqsave(&block_lock, flags);
  216. for (bb = bigblocks; bb; bb = bb->next)
  217. if (bb->pages == block) {
  218. spin_unlock_irqrestore(&slob_lock, flags);
  219. return PAGE_SIZE << bb->order;
  220. }
  221. spin_unlock_irqrestore(&block_lock, flags);
  222. }
  223. return ((slob_t *)block - 1)->units * SLOB_UNIT;
  224. }
  225. struct kmem_cache {
  226. unsigned int size, align;
  227. const char *name;
  228. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  229. void (*dtor)(void *, struct kmem_cache *, unsigned long);
  230. };
  231. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  232. size_t align, unsigned long flags,
  233. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  234. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  235. {
  236. struct kmem_cache *c;
  237. c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
  238. if (c) {
  239. c->name = name;
  240. c->size = size;
  241. c->ctor = ctor;
  242. c->dtor = dtor;
  243. /* ignore alignment unless it's forced */
  244. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  245. if (c->align < align)
  246. c->align = align;
  247. } else if (flags & SLAB_PANIC)
  248. panic("Cannot create slab cache %s\n", name);
  249. return c;
  250. }
  251. EXPORT_SYMBOL(kmem_cache_create);
  252. void kmem_cache_destroy(struct kmem_cache *c)
  253. {
  254. slob_free(c, sizeof(struct kmem_cache));
  255. }
  256. EXPORT_SYMBOL(kmem_cache_destroy);
  257. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
  258. {
  259. void *b;
  260. if (c->size < PAGE_SIZE)
  261. b = slob_alloc(c->size, flags, c->align);
  262. else
  263. b = (void *)__get_free_pages(flags, get_order(c->size));
  264. if (c->ctor)
  265. c->ctor(b, c, SLAB_CTOR_CONSTRUCTOR);
  266. return b;
  267. }
  268. EXPORT_SYMBOL(kmem_cache_alloc);
  269. void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
  270. {
  271. void *ret = kmem_cache_alloc(c, flags);
  272. if (ret)
  273. memset(ret, 0, c->size);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL(kmem_cache_zalloc);
  277. void kmem_cache_free(struct kmem_cache *c, void *b)
  278. {
  279. if (c->dtor)
  280. c->dtor(b, c, 0);
  281. if (c->size < PAGE_SIZE)
  282. slob_free(b, c->size);
  283. else
  284. free_pages((unsigned long)b, get_order(c->size));
  285. }
  286. EXPORT_SYMBOL(kmem_cache_free);
  287. unsigned int kmem_cache_size(struct kmem_cache *c)
  288. {
  289. return c->size;
  290. }
  291. EXPORT_SYMBOL(kmem_cache_size);
  292. const char *kmem_cache_name(struct kmem_cache *c)
  293. {
  294. return c->name;
  295. }
  296. EXPORT_SYMBOL(kmem_cache_name);
  297. static struct timer_list slob_timer = TIMER_INITIALIZER(
  298. (void (*)(unsigned long))slob_timer_cbk, 0, 0);
  299. int kmem_cache_shrink(struct kmem_cache *d)
  300. {
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(kmem_cache_shrink);
  304. int kmem_ptr_validate(struct kmem_cache *a, const void *b)
  305. {
  306. return 0;
  307. }
  308. void __init kmem_cache_init(void)
  309. {
  310. slob_timer_cbk();
  311. }
  312. static void slob_timer_cbk(void)
  313. {
  314. void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1);
  315. if (p)
  316. free_page((unsigned long)p);
  317. mod_timer(&slob_timer, jiffies + HZ);
  318. }