slob.c 16 KB

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