slob.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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_exact_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 "slab.h"
  62. #include <linux/mm.h>
  63. #include <linux/swap.h> /* struct reclaim_state */
  64. #include <linux/cache.h>
  65. #include <linux/init.h>
  66. #include <linux/export.h>
  67. #include <linux/rcupdate.h>
  68. #include <linux/list.h>
  69. #include <linux/kmemleak.h>
  70. #include <trace/events/kmem.h>
  71. #include <linux/atomic.h>
  72. /*
  73. * slob_block has a field 'units', which indicates size of block if +ve,
  74. * or offset of next block if -ve (in SLOB_UNITs).
  75. *
  76. * Free blocks of size 1 unit simply contain the offset of the next block.
  77. * Those with larger size contain their size in the first SLOB_UNIT of
  78. * memory, and the offset of the next free block in the second SLOB_UNIT.
  79. */
  80. #if PAGE_SIZE <= (32767 * 2)
  81. typedef s16 slobidx_t;
  82. #else
  83. typedef s32 slobidx_t;
  84. #endif
  85. struct slob_block {
  86. slobidx_t units;
  87. };
  88. typedef struct slob_block slob_t;
  89. /*
  90. * All partially free slob pages go on these lists.
  91. */
  92. #define SLOB_BREAK1 256
  93. #define SLOB_BREAK2 1024
  94. static LIST_HEAD(free_slob_small);
  95. static LIST_HEAD(free_slob_medium);
  96. static LIST_HEAD(free_slob_large);
  97. /*
  98. * slob_page_free: true for pages on free_slob_pages list.
  99. */
  100. static inline int slob_page_free(struct page *sp)
  101. {
  102. return PageSlobFree(sp);
  103. }
  104. static void set_slob_page_free(struct page *sp, struct list_head *list)
  105. {
  106. list_add(&sp->list, list);
  107. __SetPageSlobFree(sp);
  108. }
  109. static inline void clear_slob_page_free(struct page *sp)
  110. {
  111. list_del(&sp->list);
  112. __ClearPageSlobFree(sp);
  113. }
  114. #define SLOB_UNIT sizeof(slob_t)
  115. #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  116. #define SLOB_ALIGN L1_CACHE_BYTES
  117. /*
  118. * struct slob_rcu is inserted at the tail of allocated slob blocks, which
  119. * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
  120. * the block using call_rcu.
  121. */
  122. struct slob_rcu {
  123. struct rcu_head head;
  124. int size;
  125. };
  126. /*
  127. * slob_lock protects all slob allocator structures.
  128. */
  129. static DEFINE_SPINLOCK(slob_lock);
  130. /*
  131. * Encode the given size and next info into a free slob block s.
  132. */
  133. static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
  134. {
  135. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  136. slobidx_t offset = next - base;
  137. if (size > 1) {
  138. s[0].units = size;
  139. s[1].units = offset;
  140. } else
  141. s[0].units = -offset;
  142. }
  143. /*
  144. * Return the size of a slob block.
  145. */
  146. static slobidx_t slob_units(slob_t *s)
  147. {
  148. if (s->units > 0)
  149. return s->units;
  150. return 1;
  151. }
  152. /*
  153. * Return the next free slob block pointer after this one.
  154. */
  155. static slob_t *slob_next(slob_t *s)
  156. {
  157. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  158. slobidx_t next;
  159. if (s[0].units < 0)
  160. next = -s[0].units;
  161. else
  162. next = s[1].units;
  163. return base+next;
  164. }
  165. /*
  166. * Returns true if s is the last free block in its page.
  167. */
  168. static int slob_last(slob_t *s)
  169. {
  170. return !((unsigned long)slob_next(s) & ~PAGE_MASK);
  171. }
  172. static void *slob_new_pages(gfp_t gfp, int order, int node)
  173. {
  174. void *page;
  175. #ifdef CONFIG_NUMA
  176. if (node != NUMA_NO_NODE)
  177. page = alloc_pages_exact_node(node, gfp, order);
  178. else
  179. #endif
  180. page = alloc_pages(gfp, order);
  181. if (!page)
  182. return NULL;
  183. return page_address(page);
  184. }
  185. static void slob_free_pages(void *b, int order)
  186. {
  187. if (current->reclaim_state)
  188. current->reclaim_state->reclaimed_slab += 1 << order;
  189. free_pages((unsigned long)b, order);
  190. }
  191. /*
  192. * Allocate a slob block within a given slob_page sp.
  193. */
  194. static void *slob_page_alloc(struct page *sp, size_t size, int align)
  195. {
  196. slob_t *prev, *cur, *aligned = NULL;
  197. int delta = 0, units = SLOB_UNITS(size);
  198. for (prev = NULL, cur = sp->freelist; ; prev = cur, cur = slob_next(cur)) {
  199. slobidx_t avail = slob_units(cur);
  200. if (align) {
  201. aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  202. delta = aligned - cur;
  203. }
  204. if (avail >= units + delta) { /* room enough? */
  205. slob_t *next;
  206. if (delta) { /* need to fragment head to align? */
  207. next = slob_next(cur);
  208. set_slob(aligned, avail - delta, next);
  209. set_slob(cur, delta, aligned);
  210. prev = cur;
  211. cur = aligned;
  212. avail = slob_units(cur);
  213. }
  214. next = slob_next(cur);
  215. if (avail == units) { /* exact fit? unlink. */
  216. if (prev)
  217. set_slob(prev, slob_units(prev), next);
  218. else
  219. sp->freelist = next;
  220. } else { /* fragment */
  221. if (prev)
  222. set_slob(prev, slob_units(prev), cur + units);
  223. else
  224. sp->freelist = cur + units;
  225. set_slob(cur + units, avail - units, next);
  226. }
  227. sp->units -= units;
  228. if (!sp->units)
  229. clear_slob_page_free(sp);
  230. return cur;
  231. }
  232. if (slob_last(cur))
  233. return NULL;
  234. }
  235. }
  236. /*
  237. * slob_alloc: entry point into the slob allocator.
  238. */
  239. static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
  240. {
  241. struct page *sp;
  242. struct list_head *prev;
  243. struct list_head *slob_list;
  244. slob_t *b = NULL;
  245. unsigned long flags;
  246. if (size < SLOB_BREAK1)
  247. slob_list = &free_slob_small;
  248. else if (size < SLOB_BREAK2)
  249. slob_list = &free_slob_medium;
  250. else
  251. slob_list = &free_slob_large;
  252. spin_lock_irqsave(&slob_lock, flags);
  253. /* Iterate through each partially free page, try to find room */
  254. list_for_each_entry(sp, slob_list, list) {
  255. #ifdef CONFIG_NUMA
  256. /*
  257. * If there's a node specification, search for a partial
  258. * page with a matching node id in the freelist.
  259. */
  260. if (node != NUMA_NO_NODE && page_to_nid(sp) != node)
  261. continue;
  262. #endif
  263. /* Enough room on this page? */
  264. if (sp->units < SLOB_UNITS(size))
  265. continue;
  266. /* Attempt to alloc */
  267. prev = sp->list.prev;
  268. b = slob_page_alloc(sp, size, align);
  269. if (!b)
  270. continue;
  271. /* Improve fragment distribution and reduce our average
  272. * search time by starting our next search here. (see
  273. * Knuth vol 1, sec 2.5, pg 449) */
  274. if (prev != slob_list->prev &&
  275. slob_list->next != prev->next)
  276. list_move_tail(slob_list, prev->next);
  277. break;
  278. }
  279. spin_unlock_irqrestore(&slob_lock, flags);
  280. /* Not enough space: must allocate a new page */
  281. if (!b) {
  282. b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);
  283. if (!b)
  284. return NULL;
  285. sp = virt_to_page(b);
  286. __SetPageSlab(sp);
  287. spin_lock_irqsave(&slob_lock, flags);
  288. sp->units = SLOB_UNITS(PAGE_SIZE);
  289. sp->freelist = b;
  290. INIT_LIST_HEAD(&sp->list);
  291. set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
  292. set_slob_page_free(sp, slob_list);
  293. b = slob_page_alloc(sp, size, align);
  294. BUG_ON(!b);
  295. spin_unlock_irqrestore(&slob_lock, flags);
  296. }
  297. if (unlikely((gfp & __GFP_ZERO) && b))
  298. memset(b, 0, size);
  299. return b;
  300. }
  301. /*
  302. * slob_free: entry point into the slob allocator.
  303. */
  304. static void slob_free(void *block, int size)
  305. {
  306. struct page *sp;
  307. slob_t *prev, *next, *b = (slob_t *)block;
  308. slobidx_t units;
  309. unsigned long flags;
  310. struct list_head *slob_list;
  311. if (unlikely(ZERO_OR_NULL_PTR(block)))
  312. return;
  313. BUG_ON(!size);
  314. sp = virt_to_page(block);
  315. units = SLOB_UNITS(size);
  316. spin_lock_irqsave(&slob_lock, flags);
  317. if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  318. /* Go directly to page allocator. Do not pass slob allocator */
  319. if (slob_page_free(sp))
  320. clear_slob_page_free(sp);
  321. spin_unlock_irqrestore(&slob_lock, flags);
  322. __ClearPageSlab(sp);
  323. reset_page_mapcount(sp);
  324. slob_free_pages(b, 0);
  325. return;
  326. }
  327. if (!slob_page_free(sp)) {
  328. /* This slob page is about to become partially free. Easy! */
  329. sp->units = units;
  330. sp->freelist = b;
  331. set_slob(b, units,
  332. (void *)((unsigned long)(b +
  333. SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
  334. if (size < SLOB_BREAK1)
  335. slob_list = &free_slob_small;
  336. else if (size < SLOB_BREAK2)
  337. slob_list = &free_slob_medium;
  338. else
  339. slob_list = &free_slob_large;
  340. set_slob_page_free(sp, slob_list);
  341. goto out;
  342. }
  343. /*
  344. * Otherwise the page is already partially free, so find reinsertion
  345. * point.
  346. */
  347. sp->units += units;
  348. if (b < (slob_t *)sp->freelist) {
  349. if (b + units == sp->freelist) {
  350. units += slob_units(sp->freelist);
  351. sp->freelist = slob_next(sp->freelist);
  352. }
  353. set_slob(b, units, sp->freelist);
  354. sp->freelist = b;
  355. } else {
  356. prev = sp->freelist;
  357. next = slob_next(prev);
  358. while (b > next) {
  359. prev = next;
  360. next = slob_next(prev);
  361. }
  362. if (!slob_last(prev) && b + units == next) {
  363. units += slob_units(next);
  364. set_slob(b, units, slob_next(next));
  365. } else
  366. set_slob(b, units, next);
  367. if (prev + slob_units(prev) == b) {
  368. units = slob_units(b) + slob_units(prev);
  369. set_slob(prev, units, slob_next(b));
  370. } else
  371. set_slob(prev, slob_units(prev), b);
  372. }
  373. out:
  374. spin_unlock_irqrestore(&slob_lock, flags);
  375. }
  376. /*
  377. * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
  378. */
  379. static __always_inline void *
  380. __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller)
  381. {
  382. unsigned int *m;
  383. int align = max_t(size_t, ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  384. void *ret;
  385. gfp &= gfp_allowed_mask;
  386. lockdep_trace_alloc(gfp);
  387. if (size < PAGE_SIZE - align) {
  388. if (!size)
  389. return ZERO_SIZE_PTR;
  390. m = slob_alloc(size + align, gfp, align, node);
  391. if (!m)
  392. return NULL;
  393. *m = size;
  394. ret = (void *)m + align;
  395. trace_kmalloc_node(caller, ret,
  396. size, size + align, gfp, node);
  397. } else {
  398. unsigned int order = get_order(size);
  399. if (likely(order))
  400. gfp |= __GFP_COMP;
  401. ret = slob_new_pages(gfp, order, node);
  402. if (ret) {
  403. struct page *page;
  404. page = virt_to_page(ret);
  405. page->private = size;
  406. }
  407. trace_kmalloc_node(caller, ret,
  408. size, PAGE_SIZE << order, gfp, node);
  409. }
  410. kmemleak_alloc(ret, size, 1, gfp);
  411. return ret;
  412. }
  413. void *__kmalloc_node(size_t size, gfp_t gfp, int node)
  414. {
  415. return __do_kmalloc_node(size, gfp, node, _RET_IP_);
  416. }
  417. EXPORT_SYMBOL(__kmalloc_node);
  418. #ifdef CONFIG_TRACING
  419. void *__kmalloc_track_caller(size_t size, gfp_t gfp, unsigned long caller)
  420. {
  421. return __do_kmalloc_node(size, gfp, NUMA_NO_NODE, caller);
  422. }
  423. #ifdef CONFIG_NUMA
  424. void *__kmalloc_node_track_caller(size_t size, gfp_t gfp,
  425. int node, unsigned long caller)
  426. {
  427. return __do_kmalloc_node(size, gfp, node, caller);
  428. }
  429. #endif
  430. #endif
  431. void kfree(const void *block)
  432. {
  433. struct page *sp;
  434. trace_kfree(_RET_IP_, block);
  435. if (unlikely(ZERO_OR_NULL_PTR(block)))
  436. return;
  437. kmemleak_free(block);
  438. sp = virt_to_page(block);
  439. if (PageSlab(sp)) {
  440. int align = max_t(size_t, ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  441. unsigned int *m = (unsigned int *)(block - align);
  442. slob_free(m, *m + align);
  443. } else
  444. put_page(sp);
  445. }
  446. EXPORT_SYMBOL(kfree);
  447. /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
  448. size_t ksize(const void *block)
  449. {
  450. struct page *sp;
  451. BUG_ON(!block);
  452. if (unlikely(block == ZERO_SIZE_PTR))
  453. return 0;
  454. sp = virt_to_page(block);
  455. if (PageSlab(sp)) {
  456. int align = max_t(size_t, ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  457. unsigned int *m = (unsigned int *)(block - align);
  458. return SLOB_UNITS(*m) * SLOB_UNIT;
  459. } else
  460. return sp->private;
  461. }
  462. EXPORT_SYMBOL(ksize);
  463. int __kmem_cache_create(struct kmem_cache *c, unsigned long flags)
  464. {
  465. size_t align = c->size;
  466. if (flags & SLAB_DESTROY_BY_RCU) {
  467. /* leave room for rcu footer at the end of object */
  468. c->size += sizeof(struct slob_rcu);
  469. }
  470. c->flags = flags;
  471. /* ignore alignment unless it's forced */
  472. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  473. if (c->align < ARCH_SLAB_MINALIGN)
  474. c->align = ARCH_SLAB_MINALIGN;
  475. if (c->align < align)
  476. c->align = align;
  477. return 0;
  478. }
  479. void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
  480. {
  481. void *b;
  482. flags &= gfp_allowed_mask;
  483. lockdep_trace_alloc(flags);
  484. if (c->size < PAGE_SIZE) {
  485. b = slob_alloc(c->size, flags, c->align, node);
  486. trace_kmem_cache_alloc_node(_RET_IP_, b, c->size,
  487. SLOB_UNITS(c->size) * SLOB_UNIT,
  488. flags, node);
  489. } else {
  490. b = slob_new_pages(flags, get_order(c->size), node);
  491. trace_kmem_cache_alloc_node(_RET_IP_, b, c->size,
  492. PAGE_SIZE << get_order(c->size),
  493. flags, node);
  494. }
  495. if (c->ctor)
  496. c->ctor(b);
  497. kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags);
  498. return b;
  499. }
  500. EXPORT_SYMBOL(kmem_cache_alloc_node);
  501. static void __kmem_cache_free(void *b, int size)
  502. {
  503. if (size < PAGE_SIZE)
  504. slob_free(b, size);
  505. else
  506. slob_free_pages(b, get_order(size));
  507. }
  508. static void kmem_rcu_free(struct rcu_head *head)
  509. {
  510. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  511. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  512. __kmem_cache_free(b, slob_rcu->size);
  513. }
  514. void kmem_cache_free(struct kmem_cache *c, void *b)
  515. {
  516. kmemleak_free_recursive(b, c->flags);
  517. if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  518. struct slob_rcu *slob_rcu;
  519. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  520. slob_rcu->size = c->size;
  521. call_rcu(&slob_rcu->head, kmem_rcu_free);
  522. } else {
  523. __kmem_cache_free(b, c->size);
  524. }
  525. trace_kmem_cache_free(_RET_IP_, b);
  526. }
  527. EXPORT_SYMBOL(kmem_cache_free);
  528. unsigned int kmem_cache_size(struct kmem_cache *c)
  529. {
  530. return c->size;
  531. }
  532. EXPORT_SYMBOL(kmem_cache_size);
  533. int __kmem_cache_shutdown(struct kmem_cache *c)
  534. {
  535. /* No way to check for remaining objects */
  536. return 0;
  537. }
  538. int kmem_cache_shrink(struct kmem_cache *d)
  539. {
  540. return 0;
  541. }
  542. EXPORT_SYMBOL(kmem_cache_shrink);
  543. struct kmem_cache kmem_cache_boot = {
  544. .name = "kmem_cache",
  545. .size = sizeof(struct kmem_cache),
  546. .flags = SLAB_PANIC,
  547. .align = ARCH_KMALLOC_MINALIGN,
  548. };
  549. void __init kmem_cache_init(void)
  550. {
  551. kmem_cache = &kmem_cache_boot;
  552. slab_state = UP;
  553. }
  554. void __init kmem_cache_init_late(void)
  555. {
  556. slab_state = FULL;
  557. }