slob.c 15 KB

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