slob.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 as little as 2 bytes, however typically most architectures
  11. * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
  12. *
  13. * The slob heap is a linked list of pages from __get_free_page, and
  14. * within each page, there is a singly-linked list of free blocks (slob_t).
  15. * The heap is grown on demand and allocation from the heap is currently
  16. * first-fit.
  17. *
  18. * Above this is an implementation of kmalloc/kfree. Blocks returned
  19. * from kmalloc are prepended with a 4-byte header with the kmalloc size.
  20. * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
  21. * __get_free_pages directly, allocating compound pages so the page order
  22. * does not have to be separately tracked, and also stores the exact
  23. * allocation size in page->private so that it can be used to accurately
  24. * provide ksize(). These objects are detected in kfree() because slob_page()
  25. * is false for them.
  26. *
  27. * SLAB is emulated on top of SLOB by simply calling constructors and
  28. * destructors for every SLAB allocation. Objects are returned with the
  29. * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
  30. * case the low-level allocator will fragment blocks to create the proper
  31. * alignment. Again, objects of page-size or greater are allocated by
  32. * calling __get_free_pages. As SLAB objects know their size, no separate
  33. * size bookkeeping is necessary and there is essentially no allocation
  34. * space overhead, and compound pages aren't needed for multi-page
  35. * allocations.
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/slab.h>
  39. #include <linux/mm.h>
  40. #include <linux/cache.h>
  41. #include <linux/init.h>
  42. #include <linux/module.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/list.h>
  45. #include <asm/atomic.h>
  46. /*
  47. * slob_block has a field 'units', which indicates size of block if +ve,
  48. * or offset of next block if -ve (in SLOB_UNITs).
  49. *
  50. * Free blocks of size 1 unit simply contain the offset of the next block.
  51. * Those with larger size contain their size in the first SLOB_UNIT of
  52. * memory, and the offset of the next free block in the second SLOB_UNIT.
  53. */
  54. #if PAGE_SIZE <= (32767 * 2)
  55. typedef s16 slobidx_t;
  56. #else
  57. typedef s32 slobidx_t;
  58. #endif
  59. struct slob_block {
  60. slobidx_t units;
  61. };
  62. typedef struct slob_block slob_t;
  63. /*
  64. * We use struct page fields to manage some slob allocation aspects,
  65. * however to avoid the horrible mess in include/linux/mm_types.h, we'll
  66. * just define our own struct page type variant here.
  67. */
  68. struct slob_page {
  69. union {
  70. struct {
  71. unsigned long flags; /* mandatory */
  72. atomic_t _count; /* mandatory */
  73. slobidx_t units; /* free units left in page */
  74. unsigned long pad[2];
  75. slob_t *free; /* first free slob_t in page */
  76. struct list_head list; /* linked list of free pages */
  77. };
  78. struct page page;
  79. };
  80. };
  81. static inline void struct_slob_page_wrong_size(void)
  82. { BUILD_BUG_ON(sizeof(struct slob_page) != sizeof(struct page)); }
  83. /*
  84. * free_slob_page: call before a slob_page is returned to the page allocator.
  85. */
  86. static inline void free_slob_page(struct slob_page *sp)
  87. {
  88. reset_page_mapcount(&sp->page);
  89. sp->page.mapping = NULL;
  90. }
  91. /*
  92. * All (partially) free slob pages go on this list.
  93. */
  94. static LIST_HEAD(free_slob_pages);
  95. /*
  96. * slob_page: True for all slob pages (false for bigblock pages)
  97. */
  98. static inline int slob_page(struct slob_page *sp)
  99. {
  100. return test_bit(PG_active, &sp->flags);
  101. }
  102. static inline void set_slob_page(struct slob_page *sp)
  103. {
  104. __set_bit(PG_active, &sp->flags);
  105. }
  106. static inline void clear_slob_page(struct slob_page *sp)
  107. {
  108. __clear_bit(PG_active, &sp->flags);
  109. }
  110. /*
  111. * slob_page_free: true for pages on free_slob_pages list.
  112. */
  113. static inline int slob_page_free(struct slob_page *sp)
  114. {
  115. return test_bit(PG_private, &sp->flags);
  116. }
  117. static inline void set_slob_page_free(struct slob_page *sp)
  118. {
  119. list_add(&sp->list, &free_slob_pages);
  120. __set_bit(PG_private, &sp->flags);
  121. }
  122. static inline void clear_slob_page_free(struct slob_page *sp)
  123. {
  124. list_del(&sp->list);
  125. __clear_bit(PG_private, &sp->flags);
  126. }
  127. #define SLOB_UNIT sizeof(slob_t)
  128. #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  129. #define SLOB_ALIGN L1_CACHE_BYTES
  130. /*
  131. * struct slob_rcu is inserted at the tail of allocated slob blocks, which
  132. * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
  133. * the block using call_rcu.
  134. */
  135. struct slob_rcu {
  136. struct rcu_head head;
  137. int size;
  138. };
  139. /*
  140. * slob_lock protects all slob allocator structures.
  141. */
  142. static DEFINE_SPINLOCK(slob_lock);
  143. /*
  144. * Encode the given size and next info into a free slob block s.
  145. */
  146. static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
  147. {
  148. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  149. slobidx_t offset = next - base;
  150. if (size > 1) {
  151. s[0].units = size;
  152. s[1].units = offset;
  153. } else
  154. s[0].units = -offset;
  155. }
  156. /*
  157. * Return the size of a slob block.
  158. */
  159. static slobidx_t slob_units(slob_t *s)
  160. {
  161. if (s->units > 0)
  162. return s->units;
  163. return 1;
  164. }
  165. /*
  166. * Return the next free slob block pointer after this one.
  167. */
  168. static slob_t *slob_next(slob_t *s)
  169. {
  170. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  171. slobidx_t next;
  172. if (s[0].units < 0)
  173. next = -s[0].units;
  174. else
  175. next = s[1].units;
  176. return base+next;
  177. }
  178. /*
  179. * Returns true if s is the last free block in its page.
  180. */
  181. static int slob_last(slob_t *s)
  182. {
  183. return !((unsigned long)slob_next(s) & ~PAGE_MASK);
  184. }
  185. /*
  186. * Allocate a slob block within a given slob_page sp.
  187. */
  188. static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
  189. {
  190. slob_t *prev, *cur, *aligned = 0;
  191. int delta = 0, units = SLOB_UNITS(size);
  192. for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
  193. slobidx_t avail = slob_units(cur);
  194. if (align) {
  195. aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  196. delta = aligned - cur;
  197. }
  198. if (avail >= units + delta) { /* room enough? */
  199. slob_t *next;
  200. if (delta) { /* need to fragment head to align? */
  201. next = slob_next(cur);
  202. set_slob(aligned, avail - delta, next);
  203. set_slob(cur, delta, aligned);
  204. prev = cur;
  205. cur = aligned;
  206. avail = slob_units(cur);
  207. }
  208. next = slob_next(cur);
  209. if (avail == units) { /* exact fit? unlink. */
  210. if (prev)
  211. set_slob(prev, slob_units(prev), next);
  212. else
  213. sp->free = next;
  214. } else { /* fragment */
  215. if (prev)
  216. set_slob(prev, slob_units(prev), cur + units);
  217. else
  218. sp->free = cur + units;
  219. set_slob(cur + units, avail - units, next);
  220. }
  221. sp->units -= units;
  222. if (!sp->units)
  223. clear_slob_page_free(sp);
  224. return cur;
  225. }
  226. if (slob_last(cur))
  227. return NULL;
  228. }
  229. }
  230. /*
  231. * slob_alloc: entry point into the slob allocator.
  232. */
  233. static void *slob_alloc(size_t size, gfp_t gfp, int align)
  234. {
  235. struct slob_page *sp;
  236. slob_t *b = NULL;
  237. unsigned long flags;
  238. spin_lock_irqsave(&slob_lock, flags);
  239. /* Iterate through each partially free page, try to find room */
  240. list_for_each_entry(sp, &free_slob_pages, list) {
  241. if (sp->units >= SLOB_UNITS(size)) {
  242. b = slob_page_alloc(sp, size, align);
  243. if (b)
  244. break;
  245. }
  246. }
  247. spin_unlock_irqrestore(&slob_lock, flags);
  248. /* Not enough space: must allocate a new page */
  249. if (!b) {
  250. b = (slob_t *)__get_free_page(gfp);
  251. if (!b)
  252. return 0;
  253. sp = (struct slob_page *)virt_to_page(b);
  254. set_slob_page(sp);
  255. spin_lock_irqsave(&slob_lock, flags);
  256. sp->units = SLOB_UNITS(PAGE_SIZE);
  257. sp->free = b;
  258. INIT_LIST_HEAD(&sp->list);
  259. set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
  260. set_slob_page_free(sp);
  261. b = slob_page_alloc(sp, size, align);
  262. BUG_ON(!b);
  263. spin_unlock_irqrestore(&slob_lock, flags);
  264. }
  265. return b;
  266. }
  267. /*
  268. * slob_free: entry point into the slob allocator.
  269. */
  270. static void slob_free(void *block, int size)
  271. {
  272. struct slob_page *sp;
  273. slob_t *prev, *next, *b = (slob_t *)block;
  274. slobidx_t units;
  275. unsigned long flags;
  276. if (!block)
  277. return;
  278. BUG_ON(!size);
  279. sp = (struct slob_page *)virt_to_page(block);
  280. units = SLOB_UNITS(size);
  281. spin_lock_irqsave(&slob_lock, flags);
  282. if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  283. /* Go directly to page allocator. Do not pass slob allocator */
  284. if (slob_page_free(sp))
  285. clear_slob_page_free(sp);
  286. clear_slob_page(sp);
  287. free_slob_page(sp);
  288. free_page((unsigned long)b);
  289. goto out;
  290. }
  291. if (!slob_page_free(sp)) {
  292. /* This slob page is about to become partially free. Easy! */
  293. sp->units = units;
  294. sp->free = b;
  295. set_slob(b, units,
  296. (void *)((unsigned long)(b +
  297. SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
  298. set_slob_page_free(sp);
  299. goto out;
  300. }
  301. /*
  302. * Otherwise the page is already partially free, so find reinsertion
  303. * point.
  304. */
  305. sp->units += units;
  306. if (b < sp->free) {
  307. set_slob(b, units, sp->free);
  308. sp->free = b;
  309. } else {
  310. prev = sp->free;
  311. next = slob_next(prev);
  312. while (b > next) {
  313. prev = next;
  314. next = slob_next(prev);
  315. }
  316. if (!slob_last(prev) && b + units == next) {
  317. units += slob_units(next);
  318. set_slob(b, units, slob_next(next));
  319. } else
  320. set_slob(b, units, next);
  321. if (prev + slob_units(prev) == b) {
  322. units = slob_units(b) + slob_units(prev);
  323. set_slob(prev, units, slob_next(b));
  324. } else
  325. set_slob(prev, slob_units(prev), b);
  326. }
  327. out:
  328. spin_unlock_irqrestore(&slob_lock, flags);
  329. }
  330. /*
  331. * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
  332. */
  333. #ifndef ARCH_KMALLOC_MINALIGN
  334. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
  335. #endif
  336. #ifndef ARCH_SLAB_MINALIGN
  337. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
  338. #endif
  339. void *__kmalloc(size_t size, gfp_t gfp)
  340. {
  341. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  342. if (size < PAGE_SIZE - align) {
  343. unsigned int *m;
  344. m = slob_alloc(size + align, gfp, align);
  345. if (m)
  346. *m = size;
  347. return (void *)m + align;
  348. } else {
  349. void *ret;
  350. ret = (void *) __get_free_pages(gfp | __GFP_COMP,
  351. get_order(size));
  352. if (ret) {
  353. struct page *page;
  354. page = virt_to_page(ret);
  355. page->private = size;
  356. }
  357. return ret;
  358. }
  359. }
  360. EXPORT_SYMBOL(__kmalloc);
  361. /**
  362. * krealloc - reallocate memory. The contents will remain unchanged.
  363. *
  364. * @p: object to reallocate memory for.
  365. * @new_size: how many bytes of memory are required.
  366. * @flags: the type of memory to allocate.
  367. *
  368. * The contents of the object pointed to are preserved up to the
  369. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  370. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  371. * %NULL pointer, the object pointed to is freed.
  372. */
  373. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  374. {
  375. void *ret;
  376. if (unlikely(!p))
  377. return kmalloc_track_caller(new_size, flags);
  378. if (unlikely(!new_size)) {
  379. kfree(p);
  380. return NULL;
  381. }
  382. ret = kmalloc_track_caller(new_size, flags);
  383. if (ret) {
  384. memcpy(ret, p, min(new_size, ksize(p)));
  385. kfree(p);
  386. }
  387. return ret;
  388. }
  389. EXPORT_SYMBOL(krealloc);
  390. void kfree(const void *block)
  391. {
  392. struct slob_page *sp;
  393. if (!block)
  394. return;
  395. sp = (struct slob_page *)virt_to_page(block);
  396. if (slob_page(sp)) {
  397. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  398. unsigned int *m = (unsigned int *)(block - align);
  399. slob_free(m, *m + align);
  400. } else
  401. put_page(&sp->page);
  402. }
  403. EXPORT_SYMBOL(kfree);
  404. /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
  405. size_t ksize(const void *block)
  406. {
  407. struct slob_page *sp;
  408. if (!block)
  409. return 0;
  410. sp = (struct slob_page *)virt_to_page(block);
  411. if (slob_page(sp))
  412. return ((slob_t *)block - 1)->units + SLOB_UNIT;
  413. else
  414. return sp->page.private;
  415. }
  416. struct kmem_cache {
  417. unsigned int size, align;
  418. unsigned long flags;
  419. const char *name;
  420. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  421. };
  422. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  423. size_t align, unsigned long flags,
  424. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  425. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  426. {
  427. struct kmem_cache *c;
  428. c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
  429. if (c) {
  430. c->name = name;
  431. c->size = size;
  432. if (flags & SLAB_DESTROY_BY_RCU) {
  433. /* leave room for rcu footer at the end of object */
  434. c->size += sizeof(struct slob_rcu);
  435. }
  436. c->flags = flags;
  437. c->ctor = ctor;
  438. /* ignore alignment unless it's forced */
  439. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  440. if (c->align < ARCH_SLAB_MINALIGN)
  441. c->align = ARCH_SLAB_MINALIGN;
  442. if (c->align < align)
  443. c->align = align;
  444. } else if (flags & SLAB_PANIC)
  445. panic("Cannot create slab cache %s\n", name);
  446. return c;
  447. }
  448. EXPORT_SYMBOL(kmem_cache_create);
  449. void kmem_cache_destroy(struct kmem_cache *c)
  450. {
  451. slob_free(c, sizeof(struct kmem_cache));
  452. }
  453. EXPORT_SYMBOL(kmem_cache_destroy);
  454. void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
  455. {
  456. void *b;
  457. if (c->size < PAGE_SIZE)
  458. b = slob_alloc(c->size, flags, c->align);
  459. else
  460. b = (void *)__get_free_pages(flags, get_order(c->size));
  461. if (c->ctor)
  462. c->ctor(b, c, 0);
  463. return b;
  464. }
  465. EXPORT_SYMBOL(kmem_cache_alloc);
  466. void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
  467. {
  468. void *ret = kmem_cache_alloc(c, flags);
  469. if (ret)
  470. memset(ret, 0, c->size);
  471. return ret;
  472. }
  473. EXPORT_SYMBOL(kmem_cache_zalloc);
  474. static void __kmem_cache_free(void *b, int size)
  475. {
  476. if (size < PAGE_SIZE)
  477. slob_free(b, size);
  478. else
  479. free_pages((unsigned long)b, get_order(size));
  480. }
  481. static void kmem_rcu_free(struct rcu_head *head)
  482. {
  483. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  484. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  485. __kmem_cache_free(b, slob_rcu->size);
  486. }
  487. void kmem_cache_free(struct kmem_cache *c, void *b)
  488. {
  489. if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  490. struct slob_rcu *slob_rcu;
  491. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  492. INIT_RCU_HEAD(&slob_rcu->head);
  493. slob_rcu->size = c->size;
  494. call_rcu(&slob_rcu->head, kmem_rcu_free);
  495. } else {
  496. __kmem_cache_free(b, c->size);
  497. }
  498. }
  499. EXPORT_SYMBOL(kmem_cache_free);
  500. unsigned int kmem_cache_size(struct kmem_cache *c)
  501. {
  502. return c->size;
  503. }
  504. EXPORT_SYMBOL(kmem_cache_size);
  505. const char *kmem_cache_name(struct kmem_cache *c)
  506. {
  507. return c->name;
  508. }
  509. EXPORT_SYMBOL(kmem_cache_name);
  510. int kmem_cache_shrink(struct kmem_cache *d)
  511. {
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(kmem_cache_shrink);
  515. int kmem_ptr_validate(struct kmem_cache *a, const void *b)
  516. {
  517. return 0;
  518. }
  519. void __init kmem_cache_init(void)
  520. {
  521. }