slob.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #include <linux/rcupdate.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. /*
  53. * struct slob_rcu is inserted at the tail of allocated slob blocks, which
  54. * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
  55. * the block using call_rcu.
  56. */
  57. struct slob_rcu {
  58. struct rcu_head head;
  59. int size;
  60. };
  61. static slob_t arena = { .next = &arena, .units = 1 };
  62. static slob_t *slobfree = &arena;
  63. static bigblock_t *bigblocks;
  64. static DEFINE_SPINLOCK(slob_lock);
  65. static DEFINE_SPINLOCK(block_lock);
  66. static void slob_free(void *b, int size);
  67. static void slob_timer_cbk(void);
  68. static void *slob_alloc(size_t size, gfp_t gfp, int align)
  69. {
  70. slob_t *prev, *cur, *aligned = 0;
  71. int delta = 0, units = SLOB_UNITS(size);
  72. unsigned long flags;
  73. spin_lock_irqsave(&slob_lock, flags);
  74. prev = slobfree;
  75. for (cur = prev->next; ; prev = cur, cur = cur->next) {
  76. if (align) {
  77. aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  78. delta = aligned - cur;
  79. }
  80. if (cur->units >= units + delta) { /* room enough? */
  81. if (delta) { /* need to fragment head to align? */
  82. aligned->units = cur->units - delta;
  83. aligned->next = cur->next;
  84. cur->next = aligned;
  85. cur->units = delta;
  86. prev = cur;
  87. cur = aligned;
  88. }
  89. if (cur->units == units) /* exact fit? */
  90. prev->next = cur->next; /* unlink */
  91. else { /* fragment */
  92. prev->next = cur + units;
  93. prev->next->units = cur->units - units;
  94. prev->next->next = cur->next;
  95. cur->units = units;
  96. }
  97. slobfree = prev;
  98. spin_unlock_irqrestore(&slob_lock, flags);
  99. return cur;
  100. }
  101. if (cur == slobfree) {
  102. spin_unlock_irqrestore(&slob_lock, flags);
  103. if (size == PAGE_SIZE) /* trying to shrink arena? */
  104. return 0;
  105. cur = (slob_t *)__get_free_page(gfp);
  106. if (!cur)
  107. return 0;
  108. slob_free(cur, PAGE_SIZE);
  109. spin_lock_irqsave(&slob_lock, flags);
  110. cur = slobfree;
  111. }
  112. }
  113. }
  114. static void slob_free(void *block, int size)
  115. {
  116. slob_t *cur, *b = (slob_t *)block;
  117. unsigned long flags;
  118. if (!block)
  119. return;
  120. if (size)
  121. b->units = SLOB_UNITS(size);
  122. /* Find reinsertion point */
  123. spin_lock_irqsave(&slob_lock, flags);
  124. for (cur = slobfree; !(b > cur && b < cur->next); cur = cur->next)
  125. if (cur >= cur->next && (b > cur || b < cur->next))
  126. break;
  127. if (b + b->units == cur->next) {
  128. b->units += cur->next->units;
  129. b->next = cur->next->next;
  130. } else
  131. b->next = cur->next;
  132. if (cur + cur->units == b) {
  133. cur->units += b->units;
  134. cur->next = b->next;
  135. } else
  136. cur->next = b;
  137. slobfree = cur;
  138. spin_unlock_irqrestore(&slob_lock, flags);
  139. }
  140. void *__kmalloc(size_t size, gfp_t gfp)
  141. {
  142. slob_t *m;
  143. bigblock_t *bb;
  144. unsigned long flags;
  145. if (size < PAGE_SIZE - SLOB_UNIT) {
  146. m = slob_alloc(size + SLOB_UNIT, gfp, 0);
  147. return m ? (void *)(m + 1) : 0;
  148. }
  149. bb = slob_alloc(sizeof(bigblock_t), gfp, 0);
  150. if (!bb)
  151. return 0;
  152. bb->order = get_order(size);
  153. bb->pages = (void *)__get_free_pages(gfp, bb->order);
  154. if (bb->pages) {
  155. spin_lock_irqsave(&block_lock, flags);
  156. bb->next = bigblocks;
  157. bigblocks = bb;
  158. spin_unlock_irqrestore(&block_lock, flags);
  159. return bb->pages;
  160. }
  161. slob_free(bb, sizeof(bigblock_t));
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(__kmalloc);
  165. /**
  166. * krealloc - reallocate memory. The contents will remain unchanged.
  167. *
  168. * @p: object to reallocate memory for.
  169. * @new_size: how many bytes of memory are required.
  170. * @flags: the type of memory to allocate.
  171. *
  172. * The contents of the object pointed to are preserved up to the
  173. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  174. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  175. * %NULL pointer, the object pointed to is freed.
  176. */
  177. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  178. {
  179. void *ret;
  180. if (unlikely(!p))
  181. return kmalloc_track_caller(new_size, flags);
  182. if (unlikely(!new_size)) {
  183. kfree(p);
  184. return NULL;
  185. }
  186. ret = kmalloc_track_caller(new_size, flags);
  187. if (ret) {
  188. memcpy(ret, p, min(new_size, ksize(p)));
  189. kfree(p);
  190. }
  191. return ret;
  192. }
  193. EXPORT_SYMBOL(krealloc);
  194. void kfree(const void *block)
  195. {
  196. bigblock_t *bb, **last = &bigblocks;
  197. unsigned long flags;
  198. if (!block)
  199. return;
  200. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  201. /* might be on the big block list */
  202. spin_lock_irqsave(&block_lock, flags);
  203. for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) {
  204. if (bb->pages == block) {
  205. *last = bb->next;
  206. spin_unlock_irqrestore(&block_lock, flags);
  207. free_pages((unsigned long)block, bb->order);
  208. slob_free(bb, sizeof(bigblock_t));
  209. return;
  210. }
  211. }
  212. spin_unlock_irqrestore(&block_lock, flags);
  213. }
  214. slob_free((slob_t *)block - 1, 0);
  215. return;
  216. }
  217. EXPORT_SYMBOL(kfree);
  218. size_t ksize(const void *block)
  219. {
  220. bigblock_t *bb;
  221. unsigned long flags;
  222. if (!block)
  223. return 0;
  224. if (!((unsigned long)block & (PAGE_SIZE-1))) {
  225. spin_lock_irqsave(&block_lock, flags);
  226. for (bb = bigblocks; bb; bb = bb->next)
  227. if (bb->pages == block) {
  228. spin_unlock_irqrestore(&slob_lock, flags);
  229. return PAGE_SIZE << bb->order;
  230. }
  231. spin_unlock_irqrestore(&block_lock, flags);
  232. }
  233. return ((slob_t *)block - 1)->units * SLOB_UNIT;
  234. }
  235. struct kmem_cache {
  236. unsigned int size, align;
  237. unsigned long flags;
  238. const char *name;
  239. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  240. };
  241. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  242. size_t align, unsigned long flags,
  243. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  244. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  245. {
  246. struct kmem_cache *c;
  247. c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
  248. if (c) {
  249. c->name = name;
  250. c->size = size;
  251. if (flags & SLAB_DESTROY_BY_RCU) {
  252. /* leave room for rcu footer at the end of object */
  253. c->size += sizeof(struct slob_rcu);
  254. }
  255. c->flags = flags;
  256. c->ctor = ctor;
  257. /* ignore alignment unless it's forced */
  258. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  259. if (c->align < align)
  260. c->align = align;
  261. } else if (flags & SLAB_PANIC)
  262. panic("Cannot create slab cache %s\n", name);
  263. return c;
  264. }
  265. EXPORT_SYMBOL(kmem_cache_create);
  266. void kmem_cache_destroy(struct kmem_cache *c)
  267. {
  268. slob_free(c, sizeof(struct kmem_cache));
  269. }
  270. EXPORT_SYMBOL(kmem_cache_destroy);
  271. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
  272. {
  273. void *b;
  274. if (c->size < PAGE_SIZE)
  275. b = slob_alloc(c->size, flags, c->align);
  276. else
  277. b = (void *)__get_free_pages(flags, get_order(c->size));
  278. if (c->ctor)
  279. c->ctor(b, c, 0);
  280. return b;
  281. }
  282. EXPORT_SYMBOL(kmem_cache_alloc);
  283. void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
  284. {
  285. void *ret = kmem_cache_alloc(c, flags);
  286. if (ret)
  287. memset(ret, 0, c->size);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(kmem_cache_zalloc);
  291. static void __kmem_cache_free(void *b, int size)
  292. {
  293. if (size < PAGE_SIZE)
  294. slob_free(b, size);
  295. else
  296. free_pages((unsigned long)b, get_order(size));
  297. }
  298. static void kmem_rcu_free(struct rcu_head *head)
  299. {
  300. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  301. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  302. __kmem_cache_free(b, slob_rcu->size);
  303. }
  304. void kmem_cache_free(struct kmem_cache *c, void *b)
  305. {
  306. if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  307. struct slob_rcu *slob_rcu;
  308. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  309. INIT_RCU_HEAD(&slob_rcu->head);
  310. slob_rcu->size = c->size;
  311. call_rcu(&slob_rcu->head, kmem_rcu_free);
  312. } else {
  313. __kmem_cache_free(b, c->size);
  314. }
  315. }
  316. EXPORT_SYMBOL(kmem_cache_free);
  317. unsigned int kmem_cache_size(struct kmem_cache *c)
  318. {
  319. return c->size;
  320. }
  321. EXPORT_SYMBOL(kmem_cache_size);
  322. const char *kmem_cache_name(struct kmem_cache *c)
  323. {
  324. return c->name;
  325. }
  326. EXPORT_SYMBOL(kmem_cache_name);
  327. static struct timer_list slob_timer = TIMER_INITIALIZER(
  328. (void (*)(unsigned long))slob_timer_cbk, 0, 0);
  329. int kmem_cache_shrink(struct kmem_cache *d)
  330. {
  331. return 0;
  332. }
  333. EXPORT_SYMBOL(kmem_cache_shrink);
  334. int kmem_ptr_validate(struct kmem_cache *a, const void *b)
  335. {
  336. return 0;
  337. }
  338. void __init kmem_cache_init(void)
  339. {
  340. slob_timer_cbk();
  341. }
  342. static void slob_timer_cbk(void)
  343. {
  344. void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1);
  345. if (p)
  346. free_page((unsigned long)p);
  347. mod_timer(&slob_timer, jiffies + HZ);
  348. }