slob.c 15 KB

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