slob.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 (free_slob_pages.next != prev->next)
  290. list_move_tail(&free_slob_pages, prev->next);
  291. break;
  292. }
  293. spin_unlock_irqrestore(&slob_lock, flags);
  294. /* Not enough space: must allocate a new page */
  295. if (!b) {
  296. b = slob_new_page(gfp, 0, node);
  297. if (!b)
  298. return 0;
  299. sp = (struct slob_page *)virt_to_page(b);
  300. set_slob_page(sp);
  301. spin_lock_irqsave(&slob_lock, flags);
  302. sp->units = SLOB_UNITS(PAGE_SIZE);
  303. sp->free = b;
  304. INIT_LIST_HEAD(&sp->list);
  305. set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
  306. set_slob_page_free(sp);
  307. b = slob_page_alloc(sp, size, align);
  308. BUG_ON(!b);
  309. spin_unlock_irqrestore(&slob_lock, flags);
  310. }
  311. if (unlikely((gfp & __GFP_ZERO) && b))
  312. memset(b, 0, size);
  313. return b;
  314. }
  315. /*
  316. * slob_free: entry point into the slob allocator.
  317. */
  318. static void slob_free(void *block, int size)
  319. {
  320. struct slob_page *sp;
  321. slob_t *prev, *next, *b = (slob_t *)block;
  322. slobidx_t units;
  323. unsigned long flags;
  324. if (ZERO_OR_NULL_PTR(block))
  325. return;
  326. BUG_ON(!size);
  327. sp = (struct slob_page *)virt_to_page(block);
  328. units = SLOB_UNITS(size);
  329. spin_lock_irqsave(&slob_lock, flags);
  330. if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  331. /* Go directly to page allocator. Do not pass slob allocator */
  332. if (slob_page_free(sp))
  333. clear_slob_page_free(sp);
  334. clear_slob_page(sp);
  335. free_slob_page(sp);
  336. free_page((unsigned long)b);
  337. goto out;
  338. }
  339. if (!slob_page_free(sp)) {
  340. /* This slob page is about to become partially free. Easy! */
  341. sp->units = units;
  342. sp->free = b;
  343. set_slob(b, units,
  344. (void *)((unsigned long)(b +
  345. SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
  346. set_slob_page_free(sp);
  347. goto out;
  348. }
  349. /*
  350. * Otherwise the page is already partially free, so find reinsertion
  351. * point.
  352. */
  353. sp->units += units;
  354. if (b < sp->free) {
  355. set_slob(b, units, sp->free);
  356. sp->free = b;
  357. } else {
  358. prev = sp->free;
  359. next = slob_next(prev);
  360. while (b > next) {
  361. prev = next;
  362. next = slob_next(prev);
  363. }
  364. if (!slob_last(prev) && b + units == next) {
  365. units += slob_units(next);
  366. set_slob(b, units, slob_next(next));
  367. } else
  368. set_slob(b, units, next);
  369. if (prev + slob_units(prev) == b) {
  370. units = slob_units(b) + slob_units(prev);
  371. set_slob(prev, units, slob_next(b));
  372. } else
  373. set_slob(prev, slob_units(prev), b);
  374. }
  375. out:
  376. spin_unlock_irqrestore(&slob_lock, flags);
  377. }
  378. /*
  379. * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
  380. */
  381. #ifndef ARCH_KMALLOC_MINALIGN
  382. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
  383. #endif
  384. #ifndef ARCH_SLAB_MINALIGN
  385. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
  386. #endif
  387. void *__kmalloc_node(size_t size, gfp_t gfp, int node)
  388. {
  389. unsigned int *m;
  390. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  391. if (size < PAGE_SIZE - align) {
  392. if (!size)
  393. return ZERO_SIZE_PTR;
  394. m = slob_alloc(size + align, gfp, align, node);
  395. if (m)
  396. *m = size;
  397. return (void *)m + align;
  398. } else {
  399. void *ret;
  400. ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node);
  401. if (ret) {
  402. struct page *page;
  403. page = virt_to_page(ret);
  404. page->private = size;
  405. }
  406. return ret;
  407. }
  408. }
  409. EXPORT_SYMBOL(__kmalloc_node);
  410. void kfree(const void *block)
  411. {
  412. struct slob_page *sp;
  413. if (ZERO_OR_NULL_PTR(block))
  414. return;
  415. sp = (struct slob_page *)virt_to_page(block);
  416. if (slob_page(sp)) {
  417. int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  418. unsigned int *m = (unsigned int *)(block - align);
  419. slob_free(m, *m + align);
  420. } else
  421. put_page(&sp->page);
  422. }
  423. EXPORT_SYMBOL(kfree);
  424. /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
  425. size_t ksize(const void *block)
  426. {
  427. struct slob_page *sp;
  428. if (ZERO_OR_NULL_PTR(block))
  429. return 0;
  430. sp = (struct slob_page *)virt_to_page(block);
  431. if (slob_page(sp))
  432. return ((slob_t *)block - 1)->units + SLOB_UNIT;
  433. else
  434. return sp->page.private;
  435. }
  436. struct kmem_cache {
  437. unsigned int size, align;
  438. unsigned long flags;
  439. const char *name;
  440. void (*ctor)(void *, struct kmem_cache *, unsigned long);
  441. };
  442. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  443. size_t align, unsigned long flags,
  444. void (*ctor)(void*, struct kmem_cache *, unsigned long))
  445. {
  446. struct kmem_cache *c;
  447. c = slob_alloc(sizeof(struct kmem_cache), flags, 0, -1);
  448. if (c) {
  449. c->name = name;
  450. c->size = size;
  451. if (flags & SLAB_DESTROY_BY_RCU) {
  452. /* leave room for rcu footer at the end of object */
  453. c->size += sizeof(struct slob_rcu);
  454. }
  455. c->flags = flags;
  456. c->ctor = ctor;
  457. /* ignore alignment unless it's forced */
  458. c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
  459. if (c->align < ARCH_SLAB_MINALIGN)
  460. c->align = ARCH_SLAB_MINALIGN;
  461. if (c->align < align)
  462. c->align = align;
  463. } else if (flags & SLAB_PANIC)
  464. panic("Cannot create slab cache %s\n", name);
  465. return c;
  466. }
  467. EXPORT_SYMBOL(kmem_cache_create);
  468. void kmem_cache_destroy(struct kmem_cache *c)
  469. {
  470. slob_free(c, sizeof(struct kmem_cache));
  471. }
  472. EXPORT_SYMBOL(kmem_cache_destroy);
  473. void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
  474. {
  475. void *b;
  476. if (c->size < PAGE_SIZE)
  477. b = slob_alloc(c->size, flags, c->align, node);
  478. else
  479. b = slob_new_page(flags, get_order(c->size), node);
  480. if (c->ctor)
  481. c->ctor(b, c, 0);
  482. return b;
  483. }
  484. EXPORT_SYMBOL(kmem_cache_alloc_node);
  485. static void __kmem_cache_free(void *b, int size)
  486. {
  487. if (size < PAGE_SIZE)
  488. slob_free(b, size);
  489. else
  490. free_pages((unsigned long)b, get_order(size));
  491. }
  492. static void kmem_rcu_free(struct rcu_head *head)
  493. {
  494. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  495. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  496. __kmem_cache_free(b, slob_rcu->size);
  497. }
  498. void kmem_cache_free(struct kmem_cache *c, void *b)
  499. {
  500. if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  501. struct slob_rcu *slob_rcu;
  502. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  503. INIT_RCU_HEAD(&slob_rcu->head);
  504. slob_rcu->size = c->size;
  505. call_rcu(&slob_rcu->head, kmem_rcu_free);
  506. } else {
  507. __kmem_cache_free(b, c->size);
  508. }
  509. }
  510. EXPORT_SYMBOL(kmem_cache_free);
  511. unsigned int kmem_cache_size(struct kmem_cache *c)
  512. {
  513. return c->size;
  514. }
  515. EXPORT_SYMBOL(kmem_cache_size);
  516. const char *kmem_cache_name(struct kmem_cache *c)
  517. {
  518. return c->name;
  519. }
  520. EXPORT_SYMBOL(kmem_cache_name);
  521. int kmem_cache_shrink(struct kmem_cache *d)
  522. {
  523. return 0;
  524. }
  525. EXPORT_SYMBOL(kmem_cache_shrink);
  526. int kmem_ptr_validate(struct kmem_cache *a, const void *b)
  527. {
  528. return 0;
  529. }
  530. static unsigned int slob_ready __read_mostly;
  531. int slab_is_available(void)
  532. {
  533. return slob_ready;
  534. }
  535. void __init kmem_cache_init(void)
  536. {
  537. slob_ready = 1;
  538. }