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