slob.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. * is_slob_page: True for all slob pages (false for bigblock pages)
  123. */
  124. static inline int is_slob_page(struct slob_page *sp)
  125. {
  126. return PageSlobPage((struct page *)sp);
  127. }
  128. static inline void set_slob_page(struct slob_page *sp)
  129. {
  130. __SetPageSlobPage((struct page *)sp);
  131. }
  132. static inline void clear_slob_page(struct slob_page *sp)
  133. {
  134. __ClearPageSlobPage((struct page *)sp);
  135. }
  136. static inline struct slob_page *slob_page(const void *addr)
  137. {
  138. return (struct slob_page *)virt_to_page(addr);
  139. }
  140. /*
  141. * slob_page_free: true for pages on free_slob_pages list.
  142. */
  143. static inline int slob_page_free(struct slob_page *sp)
  144. {
  145. return PageSlobFree((struct page *)sp);
  146. }
  147. static void set_slob_page_free(struct slob_page *sp, struct list_head *list)
  148. {
  149. list_add(&sp->list, list);
  150. __SetPageSlobFree((struct page *)sp);
  151. }
  152. static inline void clear_slob_page_free(struct slob_page *sp)
  153. {
  154. list_del(&sp->list);
  155. __ClearPageSlobFree((struct page *)sp);
  156. }
  157. #define SLOB_UNIT sizeof(slob_t)
  158. #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  159. #define SLOB_ALIGN L1_CACHE_BYTES
  160. /*
  161. * struct slob_rcu is inserted at the tail of allocated slob blocks, which
  162. * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
  163. * the block using call_rcu.
  164. */
  165. struct slob_rcu {
  166. struct rcu_head head;
  167. int size;
  168. };
  169. /*
  170. * slob_lock protects all slob allocator structures.
  171. */
  172. static DEFINE_SPINLOCK(slob_lock);
  173. /*
  174. * Encode the given size and next info into a free slob block s.
  175. */
  176. static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
  177. {
  178. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  179. slobidx_t offset = next - base;
  180. if (size > 1) {
  181. s[0].units = size;
  182. s[1].units = offset;
  183. } else
  184. s[0].units = -offset;
  185. }
  186. /*
  187. * Return the size of a slob block.
  188. */
  189. static slobidx_t slob_units(slob_t *s)
  190. {
  191. if (s->units > 0)
  192. return s->units;
  193. return 1;
  194. }
  195. /*
  196. * Return the next free slob block pointer after this one.
  197. */
  198. static slob_t *slob_next(slob_t *s)
  199. {
  200. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  201. slobidx_t next;
  202. if (s[0].units < 0)
  203. next = -s[0].units;
  204. else
  205. next = s[1].units;
  206. return base+next;
  207. }
  208. /*
  209. * Returns true if s is the last free block in its page.
  210. */
  211. static int slob_last(slob_t *s)
  212. {
  213. return !((unsigned long)slob_next(s) & ~PAGE_MASK);
  214. }
  215. static void *slob_new_pages(gfp_t gfp, int order, int node)
  216. {
  217. void *page;
  218. #ifdef CONFIG_NUMA
  219. if (node != -1)
  220. page = alloc_pages_node(node, gfp, order);
  221. else
  222. #endif
  223. page = alloc_pages(gfp, order);
  224. if (!page)
  225. return NULL;
  226. return page_address(page);
  227. }
  228. static void slob_free_pages(void *b, int order)
  229. {
  230. free_pages((unsigned long)b, order);
  231. }
  232. /*
  233. * Allocate a slob block within a given slob_page sp.
  234. */
  235. static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
  236. {
  237. slob_t *prev, *cur, *aligned = NULL;
  238. int delta = 0, units = SLOB_UNITS(size);
  239. for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
  240. slobidx_t avail = slob_units(cur);
  241. if (align) {
  242. aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  243. delta = aligned - cur;
  244. }
  245. if (avail >= units + delta) { /* room enough? */
  246. slob_t *next;
  247. if (delta) { /* need to fragment head to align? */
  248. next = slob_next(cur);
  249. set_slob(aligned, avail - delta, next);
  250. set_slob(cur, delta, aligned);
  251. prev = cur;
  252. cur = aligned;
  253. avail = slob_units(cur);
  254. }
  255. next = slob_next(cur);
  256. if (avail == units) { /* exact fit? unlink. */
  257. if (prev)
  258. set_slob(prev, slob_units(prev), next);
  259. else
  260. sp->free = next;
  261. } else { /* fragment */
  262. if (prev)
  263. set_slob(prev, slob_units(prev), cur + units);
  264. else
  265. sp->free = cur + units;
  266. set_slob(cur + units, avail - units, next);
  267. }
  268. sp->units -= units;
  269. if (!sp->units)
  270. clear_slob_page_free(sp);
  271. return cur;
  272. }
  273. if (slob_last(cur))
  274. return NULL;
  275. }
  276. }
  277. /*
  278. * slob_alloc: entry point into the slob allocator.
  279. */
  280. static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
  281. {
  282. struct slob_page *sp;
  283. struct list_head *prev;
  284. struct list_head *slob_list;
  285. slob_t *b = NULL;
  286. unsigned long flags;
  287. if (size < SLOB_BREAK1)
  288. slob_list = &free_slob_small;
  289. else if (size < SLOB_BREAK2)
  290. slob_list = &free_slob_medium;
  291. else
  292. slob_list = &free_slob_large;
  293. spin_lock_irqsave(&slob_lock, flags);
  294. /* Iterate through each partially free page, try to find room */
  295. list_for_each_entry(sp, slob_list, list) {
  296. #ifdef CONFIG_NUMA
  297. /*
  298. * If there's a node specification, search for a partial
  299. * page with a matching node id in the freelist.
  300. */
  301. if (node != -1 && page_to_nid(&sp->page) != node)
  302. continue;
  303. #endif
  304. /* Enough room on this page? */
  305. if (sp->units < SLOB_UNITS(size))
  306. continue;
  307. /* Attempt to alloc */
  308. prev = sp->list.prev;
  309. b = slob_page_alloc(sp, size, align);
  310. if (!b)
  311. continue;
  312. /* Improve fragment distribution and reduce our average
  313. * search time by starting our next search here. (see
  314. * Knuth vol 1, sec 2.5, pg 449) */
  315. if (prev != slob_list->prev &&
  316. slob_list->next != prev->next)
  317. list_move_tail(slob_list, prev->next);
  318. break;
  319. }
  320. spin_unlock_irqrestore(&slob_lock, flags);
  321. /* Not enough space: must allocate a new page */
  322. if (!b) {
  323. b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);
  324. if (!b)
  325. return NULL;
  326. sp = slob_page(b);
  327. set_slob_page(sp);
  328. spin_lock_irqsave(&slob_lock, flags);
  329. sp->units = SLOB_UNITS(PAGE_SIZE);
  330. sp->free = b;
  331. INIT_LIST_HEAD(&sp->list);
  332. set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
  333. set_slob_page_free(sp, slob_list);
  334. b = slob_page_alloc(sp, size, align);
  335. BUG_ON(!b);
  336. spin_unlock_irqrestore(&slob_lock, flags);
  337. }
  338. if (unlikely((gfp & __GFP_ZERO) && b))
  339. memset(b, 0, size);
  340. return b;
  341. }
  342. /*
  343. * slob_free: entry point into the slob allocator.
  344. */
  345. static void slob_free(void *block, int size)
  346. {
  347. struct slob_page *sp;
  348. slob_t *prev, *next, *b = (slob_t *)block;
  349. slobidx_t units;
  350. unsigned long flags;
  351. if (unlikely(ZERO_OR_NULL_PTR(block)))
  352. return;
  353. BUG_ON(!size);
  354. sp = slob_page(block);
  355. units = SLOB_UNITS(size);
  356. spin_lock_irqsave(&slob_lock, flags);
  357. if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  358. /* Go directly to page allocator. Do not pass slob allocator */
  359. if (slob_page_free(sp))
  360. clear_slob_page_free(sp);
  361. spin_unlock_irqrestore(&slob_lock, flags);
  362. clear_slob_page(sp);
  363. free_slob_page(sp);
  364. free_page((unsigned long)b);
  365. return;
  366. }
  367. if (!slob_page_free(sp)) {
  368. /* This slob page is about to become partially free. Easy! */
  369. sp->units = units;
  370. sp->free = b;
  371. set_slob(b, units,
  372. (void *)((unsigned long)(b +
  373. SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
  374. set_slob_page_free(sp, &free_slob_small);
  375. goto out;
  376. }
  377. /*
  378. * Otherwise the page is already partially free, so find reinsertion
  379. * point.
  380. */
  381. sp->units += units;
  382. if (b < sp->free) {
  383. if (b + units == sp->free) {
  384. units += slob_units(sp->free);
  385. sp->free = slob_next(sp->free);
  386. }
  387. set_slob(b, units, sp->free);
  388. sp->free = b;
  389. } else {
  390. prev = sp->free;
  391. next = slob_next(prev);
  392. while (b > next) {
  393. prev = next;
  394. next = slob_next(prev);
  395. }
  396. if (!slob_last(prev) && b + units == next) {
  397. units += slob_units(next);
  398. set_slob(b, units, slob_next(next));
  399. } else
  400. set_slob(b, units, next);
  401. if (prev + slob_units(prev) == b) {
  402. units = slob_units(b) + slob_units(prev);
  403. set_slob(prev, units, slob_next(b));
  404. } else
  405. set_slob(prev, slob_units(prev), b);
  406. }
  407. out:
  408. spin_unlock_irqrestore(&slob_lock, flags);
  409. }
  410. /*
  411. * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
  412. */
  413. #ifndef ARCH_KMALLOC_MINALIGN
  414. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
  415. #endif
  416. #ifndef ARCH_SLAB_MINALIGN
  417. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
  418. #endif
  419. void *__kmalloc_node(size_t size, gfp_t gfp, int node)
  420. {
  421. unsigned int *m;
  422. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  423. if (size < PAGE_SIZE - align) {
  424. if (!size)
  425. return ZERO_SIZE_PTR;
  426. m = slob_alloc(size + align, gfp, align, node);
  427. if (!m)
  428. return NULL;
  429. *m = size;
  430. return (void *)m + align;
  431. } else {
  432. void *ret;
  433. ret = slob_new_pages(gfp | __GFP_COMP, get_order(size), node);
  434. if (ret) {
  435. struct page *page;
  436. page = virt_to_page(ret);
  437. page->private = size;
  438. }
  439. return ret;
  440. }
  441. }
  442. EXPORT_SYMBOL(__kmalloc_node);
  443. void kfree(const void *block)
  444. {
  445. struct slob_page *sp;
  446. if (unlikely(ZERO_OR_NULL_PTR(block)))
  447. return;
  448. sp = slob_page(block);
  449. if (is_slob_page(sp)) {
  450. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  451. unsigned int *m = (unsigned int *)(block - align);
  452. slob_free(m, *m + align);
  453. } else
  454. put_page(&sp->page);
  455. }
  456. EXPORT_SYMBOL(kfree);
  457. /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
  458. size_t ksize(const void *block)
  459. {
  460. struct slob_page *sp;
  461. BUG_ON(!block);
  462. if (unlikely(block == ZERO_SIZE_PTR))
  463. return 0;
  464. sp = slob_page(block);
  465. if (is_slob_page(sp)) {
  466. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  467. unsigned int *m = (unsigned int *)(block - align);
  468. return SLOB_UNITS(*m) * SLOB_UNIT;
  469. } else
  470. return sp->page.private;
  471. }
  472. EXPORT_SYMBOL(ksize);
  473. struct kmem_cache {
  474. unsigned int size, align;
  475. unsigned long flags;
  476. const char *name;
  477. void (*ctor)(void *);
  478. };
  479. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  480. size_t align, unsigned long flags, void (*ctor)(void *))
  481. {
  482. struct kmem_cache *c;
  483. c = slob_alloc(sizeof(struct kmem_cache),
  484. GFP_KERNEL, ARCH_KMALLOC_MINALIGN, -1);
  485. if (c) {
  486. c->name = name;
  487. c->size = size;
  488. if (flags & SLAB_DESTROY_BY_RCU) {
  489. /* leave room for rcu footer at the end of object */
  490. c->size += sizeof(struct slob_rcu);
  491. }
  492. c->flags = flags;
  493. c->ctor = ctor;
  494. /* ignore alignment unless it's forced */
  495. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  496. if (c->align < ARCH_SLAB_MINALIGN)
  497. c->align = ARCH_SLAB_MINALIGN;
  498. if (c->align < align)
  499. c->align = align;
  500. } else if (flags & SLAB_PANIC)
  501. panic("Cannot create slab cache %s\n", name);
  502. return c;
  503. }
  504. EXPORT_SYMBOL(kmem_cache_create);
  505. void kmem_cache_destroy(struct kmem_cache *c)
  506. {
  507. slob_free(c, sizeof(struct kmem_cache));
  508. }
  509. EXPORT_SYMBOL(kmem_cache_destroy);
  510. void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
  511. {
  512. void *b;
  513. if (c->size < PAGE_SIZE)
  514. b = slob_alloc(c->size, flags, c->align, node);
  515. else
  516. b = slob_new_pages(flags, get_order(c->size), node);
  517. if (c->ctor)
  518. c->ctor(b);
  519. return b;
  520. }
  521. EXPORT_SYMBOL(kmem_cache_alloc_node);
  522. static void __kmem_cache_free(void *b, int size)
  523. {
  524. if (size < PAGE_SIZE)
  525. slob_free(b, size);
  526. else
  527. slob_free_pages(b, get_order(size));
  528. }
  529. static void kmem_rcu_free(struct rcu_head *head)
  530. {
  531. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  532. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  533. __kmem_cache_free(b, slob_rcu->size);
  534. }
  535. void kmem_cache_free(struct kmem_cache *c, void *b)
  536. {
  537. if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  538. struct slob_rcu *slob_rcu;
  539. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  540. INIT_RCU_HEAD(&slob_rcu->head);
  541. slob_rcu->size = c->size;
  542. call_rcu(&slob_rcu->head, kmem_rcu_free);
  543. } else {
  544. __kmem_cache_free(b, c->size);
  545. }
  546. }
  547. EXPORT_SYMBOL(kmem_cache_free);
  548. unsigned int kmem_cache_size(struct kmem_cache *c)
  549. {
  550. return c->size;
  551. }
  552. EXPORT_SYMBOL(kmem_cache_size);
  553. const char *kmem_cache_name(struct kmem_cache *c)
  554. {
  555. return c->name;
  556. }
  557. EXPORT_SYMBOL(kmem_cache_name);
  558. int kmem_cache_shrink(struct kmem_cache *d)
  559. {
  560. return 0;
  561. }
  562. EXPORT_SYMBOL(kmem_cache_shrink);
  563. int kmem_ptr_validate(struct kmem_cache *a, const void *b)
  564. {
  565. return 0;
  566. }
  567. static unsigned int slob_ready __read_mostly;
  568. int slab_is_available(void)
  569. {
  570. return slob_ready;
  571. }
  572. void __init kmem_cache_init(void)
  573. {
  574. slob_ready = 1;
  575. }