slob.c 16 KB

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