slob.c 15 KB

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