slob.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SLOB Allocator: Simple List Of Blocks
  4. *
  5. * Matt Mackall <[email protected]> 12/30/03
  6. *
  7. * NUMA support by Paul Mundt, 2007.
  8. *
  9. * How SLOB works:
  10. *
  11. * The core of SLOB is a traditional K&R style heap allocator, with
  12. * support for returning aligned objects. The granularity of this
  13. * allocator is as little as 2 bytes, however typically most architectures
  14. * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
  15. *
  16. * The slob heap is a set of linked list of pages from alloc_pages(),
  17. * and within each page, there is a singly-linked list of free blocks
  18. * (slob_t). The heap is grown on demand. To reduce fragmentation,
  19. * heap pages are segregated into three lists, with objects less than
  20. * 256 bytes, objects less than 1024 bytes, and all other objects.
  21. *
  22. * Allocation from heap involves first searching for a page with
  23. * sufficient free blocks (using a next-fit-like approach) followed by
  24. * a first-fit scan of the page. Deallocation inserts objects back
  25. * into the free list in address order, so this is effectively an
  26. * address-ordered first fit.
  27. *
  28. * Above this is an implementation of kmalloc/kfree. Blocks returned
  29. * from kmalloc are prepended with a 4-byte header with the kmalloc size.
  30. * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
  31. * alloc_pages() directly, allocating compound pages so the page order
  32. * does not have to be separately tracked.
  33. * These objects are detected in kfree() because folio_test_slab()
  34. * is false for them.
  35. *
  36. * SLAB is emulated on top of SLOB by simply calling constructors and
  37. * destructors for every SLAB allocation. Objects are returned with the
  38. * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
  39. * case the low-level allocator will fragment blocks to create the proper
  40. * alignment. Again, objects of page-size or greater are allocated by
  41. * calling alloc_pages(). As SLAB objects know their size, no separate
  42. * size bookkeeping is necessary and there is essentially no allocation
  43. * space overhead, and compound pages aren't needed for multi-page
  44. * allocations.
  45. *
  46. * NUMA support in SLOB is fairly simplistic, pushing most of the real
  47. * logic down to the page allocator, and simply doing the node accounting
  48. * on the upper levels. In the event that a node id is explicitly
  49. * provided, __alloc_pages_node() with the specified node id is used
  50. * instead. The common case (or when the node id isn't explicitly provided)
  51. * will default to the current node, as per numa_node_id().
  52. *
  53. * Node aware pages are still inserted in to the global freelist, and
  54. * these are scanned for by matching against the node id encoded in the
  55. * page flags. As a result, block allocations that can be satisfied from
  56. * the freelist will only be done so on pages residing on the same node,
  57. * in order to prevent random node placement.
  58. */
  59. #include <linux/kernel.h>
  60. #include <linux/slab.h>
  61. #include <linux/mm.h>
  62. #include <linux/swap.h> /* struct reclaim_state */
  63. #include <linux/cache.h>
  64. #include <linux/init.h>
  65. #include <linux/export.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/list.h>
  68. #include <linux/kmemleak.h>
  69. #include <trace/events/kmem.h>
  70. #include <linux/atomic.h>
  71. #include "slab.h"
  72. /*
  73. * slob_block has a field 'units', which indicates size of block if +ve,
  74. * or offset of next block if -ve (in SLOB_UNITs).
  75. *
  76. * Free blocks of size 1 unit simply contain the offset of the next block.
  77. * Those with larger size contain their size in the first SLOB_UNIT of
  78. * memory, and the offset of the next free block in the second SLOB_UNIT.
  79. */
  80. #if PAGE_SIZE <= (32767 * 2)
  81. typedef s16 slobidx_t;
  82. #else
  83. typedef s32 slobidx_t;
  84. #endif
  85. struct slob_block {
  86. slobidx_t units;
  87. };
  88. typedef struct slob_block slob_t;
  89. /*
  90. * All partially free slob pages go on these lists.
  91. */
  92. #define SLOB_BREAK1 256
  93. #define SLOB_BREAK2 1024
  94. static LIST_HEAD(free_slob_small);
  95. static LIST_HEAD(free_slob_medium);
  96. static LIST_HEAD(free_slob_large);
  97. /*
  98. * slob_page_free: true for pages on free_slob_pages list.
  99. */
  100. static inline int slob_page_free(struct slab *slab)
  101. {
  102. return PageSlobFree(slab_page(slab));
  103. }
  104. static void set_slob_page_free(struct slab *slab, struct list_head *list)
  105. {
  106. list_add(&slab->slab_list, list);
  107. __SetPageSlobFree(slab_page(slab));
  108. }
  109. static inline void clear_slob_page_free(struct slab *slab)
  110. {
  111. list_del(&slab->slab_list);
  112. __ClearPageSlobFree(slab_page(slab));
  113. }
  114. #define SLOB_UNIT sizeof(slob_t)
  115. #define SLOB_UNITS(size) DIV_ROUND_UP(size, SLOB_UNIT)
  116. /*
  117. * struct slob_rcu is inserted at the tail of allocated slob blocks, which
  118. * were created with a SLAB_TYPESAFE_BY_RCU slab. slob_rcu is used to free
  119. * the block using call_rcu.
  120. */
  121. struct slob_rcu {
  122. struct rcu_head head;
  123. int size;
  124. };
  125. /*
  126. * slob_lock protects all slob allocator structures.
  127. */
  128. static DEFINE_SPINLOCK(slob_lock);
  129. /*
  130. * Encode the given size and next info into a free slob block s.
  131. */
  132. static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
  133. {
  134. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  135. slobidx_t offset = next - base;
  136. if (size > 1) {
  137. s[0].units = size;
  138. s[1].units = offset;
  139. } else
  140. s[0].units = -offset;
  141. }
  142. /*
  143. * Return the size of a slob block.
  144. */
  145. static slobidx_t slob_units(slob_t *s)
  146. {
  147. if (s->units > 0)
  148. return s->units;
  149. return 1;
  150. }
  151. /*
  152. * Return the next free slob block pointer after this one.
  153. */
  154. static slob_t *slob_next(slob_t *s)
  155. {
  156. slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  157. slobidx_t next;
  158. if (s[0].units < 0)
  159. next = -s[0].units;
  160. else
  161. next = s[1].units;
  162. return base+next;
  163. }
  164. /*
  165. * Returns true if s is the last free block in its page.
  166. */
  167. static int slob_last(slob_t *s)
  168. {
  169. return !((unsigned long)slob_next(s) & ~PAGE_MASK);
  170. }
  171. static void *slob_new_pages(gfp_t gfp, int order, int node)
  172. {
  173. struct page *page;
  174. #ifdef CONFIG_NUMA
  175. if (node != NUMA_NO_NODE)
  176. page = __alloc_pages_node(node, gfp, order);
  177. else
  178. #endif
  179. page = alloc_pages(gfp, order);
  180. if (!page)
  181. return NULL;
  182. mod_node_page_state(page_pgdat(page), NR_SLAB_UNRECLAIMABLE_B,
  183. PAGE_SIZE << order);
  184. return page_address(page);
  185. }
  186. static void slob_free_pages(void *b, int order)
  187. {
  188. struct page *sp = virt_to_page(b);
  189. if (current->reclaim_state)
  190. current->reclaim_state->reclaimed_slab += 1 << order;
  191. mod_node_page_state(page_pgdat(sp), NR_SLAB_UNRECLAIMABLE_B,
  192. -(PAGE_SIZE << order));
  193. __free_pages(sp, order);
  194. }
  195. /*
  196. * slob_page_alloc() - Allocate a slob block within a given slob_page sp.
  197. * @sp: Page to look in.
  198. * @size: Size of the allocation.
  199. * @align: Allocation alignment.
  200. * @align_offset: Offset in the allocated block that will be aligned.
  201. * @page_removed_from_list: Return parameter.
  202. *
  203. * Tries to find a chunk of memory at least @size bytes big within @page.
  204. *
  205. * Return: Pointer to memory if allocated, %NULL otherwise. If the
  206. * allocation fills up @page then the page is removed from the
  207. * freelist, in this case @page_removed_from_list will be set to
  208. * true (set to false otherwise).
  209. */
  210. static void *slob_page_alloc(struct slab *sp, size_t size, int align,
  211. int align_offset, bool *page_removed_from_list)
  212. {
  213. slob_t *prev, *cur, *aligned = NULL;
  214. int delta = 0, units = SLOB_UNITS(size);
  215. *page_removed_from_list = false;
  216. for (prev = NULL, cur = sp->freelist; ; prev = cur, cur = slob_next(cur)) {
  217. slobidx_t avail = slob_units(cur);
  218. /*
  219. * 'aligned' will hold the address of the slob block so that the
  220. * address 'aligned'+'align_offset' is aligned according to the
  221. * 'align' parameter. This is for kmalloc() which prepends the
  222. * allocated block with its size, so that the block itself is
  223. * aligned when needed.
  224. */
  225. if (align) {
  226. aligned = (slob_t *)
  227. (ALIGN((unsigned long)cur + align_offset, align)
  228. - align_offset);
  229. delta = aligned - cur;
  230. }
  231. if (avail >= units + delta) { /* room enough? */
  232. slob_t *next;
  233. if (delta) { /* need to fragment head to align? */
  234. next = slob_next(cur);
  235. set_slob(aligned, avail - delta, next);
  236. set_slob(cur, delta, aligned);
  237. prev = cur;
  238. cur = aligned;
  239. avail = slob_units(cur);
  240. }
  241. next = slob_next(cur);
  242. if (avail == units) { /* exact fit? unlink. */
  243. if (prev)
  244. set_slob(prev, slob_units(prev), next);
  245. else
  246. sp->freelist = next;
  247. } else { /* fragment */
  248. if (prev)
  249. set_slob(prev, slob_units(prev), cur + units);
  250. else
  251. sp->freelist = cur + units;
  252. set_slob(cur + units, avail - units, next);
  253. }
  254. sp->units -= units;
  255. if (!sp->units) {
  256. clear_slob_page_free(sp);
  257. *page_removed_from_list = true;
  258. }
  259. return cur;
  260. }
  261. if (slob_last(cur))
  262. return NULL;
  263. }
  264. }
  265. /*
  266. * slob_alloc: entry point into the slob allocator.
  267. */
  268. static void *slob_alloc(size_t size, gfp_t gfp, int align, int node,
  269. int align_offset)
  270. {
  271. struct folio *folio;
  272. struct slab *sp;
  273. struct list_head *slob_list;
  274. slob_t *b = NULL;
  275. unsigned long flags;
  276. bool _unused;
  277. if (size < SLOB_BREAK1)
  278. slob_list = &free_slob_small;
  279. else if (size < SLOB_BREAK2)
  280. slob_list = &free_slob_medium;
  281. else
  282. slob_list = &free_slob_large;
  283. spin_lock_irqsave(&slob_lock, flags);
  284. /* Iterate through each partially free page, try to find room */
  285. list_for_each_entry(sp, slob_list, slab_list) {
  286. bool page_removed_from_list = false;
  287. #ifdef CONFIG_NUMA
  288. /*
  289. * If there's a node specification, search for a partial
  290. * page with a matching node id in the freelist.
  291. */
  292. if (node != NUMA_NO_NODE && slab_nid(sp) != node)
  293. continue;
  294. #endif
  295. /* Enough room on this page? */
  296. if (sp->units < SLOB_UNITS(size))
  297. continue;
  298. b = slob_page_alloc(sp, size, align, align_offset, &page_removed_from_list);
  299. if (!b)
  300. continue;
  301. /*
  302. * If slob_page_alloc() removed sp from the list then we
  303. * cannot call list functions on sp. If so allocation
  304. * did not fragment the page anyway so optimisation is
  305. * unnecessary.
  306. */
  307. if (!page_removed_from_list) {
  308. /*
  309. * Improve fragment distribution and reduce our average
  310. * search time by starting our next search here. (see
  311. * Knuth vol 1, sec 2.5, pg 449)
  312. */
  313. if (!list_is_first(&sp->slab_list, slob_list))
  314. list_rotate_to_front(&sp->slab_list, slob_list);
  315. }
  316. break;
  317. }
  318. spin_unlock_irqrestore(&slob_lock, flags);
  319. /* Not enough space: must allocate a new page */
  320. if (!b) {
  321. b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);
  322. if (!b)
  323. return NULL;
  324. folio = virt_to_folio(b);
  325. __folio_set_slab(folio);
  326. sp = folio_slab(folio);
  327. spin_lock_irqsave(&slob_lock, flags);
  328. sp->units = SLOB_UNITS(PAGE_SIZE);
  329. sp->freelist = b;
  330. INIT_LIST_HEAD(&sp->slab_list);
  331. set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
  332. set_slob_page_free(sp, slob_list);
  333. b = slob_page_alloc(sp, size, align, align_offset, &_unused);
  334. BUG_ON(!b);
  335. spin_unlock_irqrestore(&slob_lock, flags);
  336. }
  337. if (unlikely(gfp & __GFP_ZERO))
  338. memset(b, 0, size);
  339. return b;
  340. }
  341. /*
  342. * slob_free: entry point into the slob allocator.
  343. */
  344. static void slob_free(void *block, int size)
  345. {
  346. struct slab *sp;
  347. slob_t *prev, *next, *b = (slob_t *)block;
  348. slobidx_t units;
  349. unsigned long flags;
  350. struct list_head *slob_list;
  351. if (unlikely(ZERO_OR_NULL_PTR(block)))
  352. return;
  353. BUG_ON(!size);
  354. sp = virt_to_slab(block);
  355. units = SLOB_UNITS(size);
  356. spin_lock_irqsave(&slob_lock, flags);
  357. if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  358. /* Go directly to page allocator. Do not pass slob allocator */
  359. if (slob_page_free(sp))
  360. clear_slob_page_free(sp);
  361. spin_unlock_irqrestore(&slob_lock, flags);
  362. __folio_clear_slab(slab_folio(sp));
  363. slob_free_pages(b, 0);
  364. return;
  365. }
  366. if (!slob_page_free(sp)) {
  367. /* This slob page is about to become partially free. Easy! */
  368. sp->units = units;
  369. sp->freelist = b;
  370. set_slob(b, units,
  371. (void *)((unsigned long)(b +
  372. SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
  373. if (size < SLOB_BREAK1)
  374. slob_list = &free_slob_small;
  375. else if (size < SLOB_BREAK2)
  376. slob_list = &free_slob_medium;
  377. else
  378. slob_list = &free_slob_large;
  379. set_slob_page_free(sp, slob_list);
  380. goto out;
  381. }
  382. /*
  383. * Otherwise the page is already partially free, so find reinsertion
  384. * point.
  385. */
  386. sp->units += units;
  387. if (b < (slob_t *)sp->freelist) {
  388. if (b + units == sp->freelist) {
  389. units += slob_units(sp->freelist);
  390. sp->freelist = slob_next(sp->freelist);
  391. }
  392. set_slob(b, units, sp->freelist);
  393. sp->freelist = b;
  394. } else {
  395. prev = sp->freelist;
  396. next = slob_next(prev);
  397. while (b > next) {
  398. prev = next;
  399. next = slob_next(prev);
  400. }
  401. if (!slob_last(prev) && b + units == next) {
  402. units += slob_units(next);
  403. set_slob(b, units, slob_next(next));
  404. } else
  405. set_slob(b, units, next);
  406. if (prev + slob_units(prev) == b) {
  407. units = slob_units(b) + slob_units(prev);
  408. set_slob(prev, units, slob_next(b));
  409. } else
  410. set_slob(prev, slob_units(prev), b);
  411. }
  412. out:
  413. spin_unlock_irqrestore(&slob_lock, flags);
  414. }
  415. #ifdef CONFIG_PRINTK
  416. void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
  417. {
  418. kpp->kp_ptr = object;
  419. kpp->kp_slab = slab;
  420. }
  421. #endif
  422. /*
  423. * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
  424. */
  425. static __always_inline void *
  426. __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller)
  427. {
  428. unsigned int *m;
  429. unsigned int minalign;
  430. void *ret;
  431. minalign = max_t(unsigned int, ARCH_KMALLOC_MINALIGN,
  432. arch_slab_minalign());
  433. gfp &= gfp_allowed_mask;
  434. might_alloc(gfp);
  435. if (size < PAGE_SIZE - minalign) {
  436. int align = minalign;
  437. /*
  438. * For power of two sizes, guarantee natural alignment for
  439. * kmalloc()'d objects.
  440. */
  441. if (is_power_of_2(size))
  442. align = max_t(unsigned int, minalign, size);
  443. if (!size)
  444. return ZERO_SIZE_PTR;
  445. m = slob_alloc(size + minalign, gfp, align, node, minalign);
  446. if (!m)
  447. return NULL;
  448. *m = size;
  449. ret = (void *)m + minalign;
  450. trace_kmalloc(caller, ret, size, size + minalign, gfp, node);
  451. } else {
  452. unsigned int order = get_order(size);
  453. if (likely(order))
  454. gfp |= __GFP_COMP;
  455. ret = slob_new_pages(gfp, order, node);
  456. trace_kmalloc(caller, ret, size, PAGE_SIZE << order, gfp, node);
  457. }
  458. kmemleak_alloc(ret, size, 1, gfp);
  459. return ret;
  460. }
  461. void *__kmalloc(size_t size, gfp_t gfp)
  462. {
  463. return __do_kmalloc_node(size, gfp, NUMA_NO_NODE, _RET_IP_);
  464. }
  465. EXPORT_SYMBOL(__kmalloc);
  466. void *__kmalloc_node_track_caller(size_t size, gfp_t gfp,
  467. int node, unsigned long caller)
  468. {
  469. return __do_kmalloc_node(size, gfp, node, caller);
  470. }
  471. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  472. void kfree(const void *block)
  473. {
  474. struct folio *sp;
  475. trace_kfree(_RET_IP_, block);
  476. if (unlikely(ZERO_OR_NULL_PTR(block)))
  477. return;
  478. kmemleak_free(block);
  479. sp = virt_to_folio(block);
  480. if (folio_test_slab(sp)) {
  481. unsigned int align = max_t(unsigned int,
  482. ARCH_KMALLOC_MINALIGN,
  483. arch_slab_minalign());
  484. unsigned int *m = (unsigned int *)(block - align);
  485. slob_free(m, *m + align);
  486. } else {
  487. unsigned int order = folio_order(sp);
  488. mod_node_page_state(folio_pgdat(sp), NR_SLAB_UNRECLAIMABLE_B,
  489. -(PAGE_SIZE << order));
  490. __free_pages(folio_page(sp, 0), order);
  491. }
  492. }
  493. EXPORT_SYMBOL(kfree);
  494. size_t kmalloc_size_roundup(size_t size)
  495. {
  496. /* Short-circuit the 0 size case. */
  497. if (unlikely(size == 0))
  498. return 0;
  499. /* Short-circuit saturated "too-large" case. */
  500. if (unlikely(size == SIZE_MAX))
  501. return SIZE_MAX;
  502. return ALIGN(size, ARCH_KMALLOC_MINALIGN);
  503. }
  504. EXPORT_SYMBOL(kmalloc_size_roundup);
  505. /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
  506. size_t __ksize(const void *block)
  507. {
  508. struct folio *folio;
  509. unsigned int align;
  510. unsigned int *m;
  511. BUG_ON(!block);
  512. if (unlikely(block == ZERO_SIZE_PTR))
  513. return 0;
  514. folio = virt_to_folio(block);
  515. if (unlikely(!folio_test_slab(folio)))
  516. return folio_size(folio);
  517. align = max_t(unsigned int, ARCH_KMALLOC_MINALIGN,
  518. arch_slab_minalign());
  519. m = (unsigned int *)(block - align);
  520. return SLOB_UNITS(*m) * SLOB_UNIT;
  521. }
  522. int __kmem_cache_create(struct kmem_cache *c, slab_flags_t flags)
  523. {
  524. if (flags & SLAB_TYPESAFE_BY_RCU) {
  525. /* leave room for rcu footer at the end of object */
  526. c->size += sizeof(struct slob_rcu);
  527. }
  528. /* Actual size allocated */
  529. c->size = SLOB_UNITS(c->size) * SLOB_UNIT;
  530. c->flags = flags;
  531. return 0;
  532. }
  533. static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
  534. {
  535. void *b;
  536. flags &= gfp_allowed_mask;
  537. might_alloc(flags);
  538. if (c->size < PAGE_SIZE) {
  539. b = slob_alloc(c->size, flags, c->align, node, 0);
  540. trace_kmem_cache_alloc(_RET_IP_, b, c, flags, node);
  541. } else {
  542. b = slob_new_pages(flags, get_order(c->size), node);
  543. trace_kmem_cache_alloc(_RET_IP_, b, c, flags, node);
  544. }
  545. if (b && c->ctor) {
  546. WARN_ON_ONCE(flags & __GFP_ZERO);
  547. c->ctor(b);
  548. }
  549. kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags);
  550. return b;
  551. }
  552. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  553. {
  554. return slob_alloc_node(cachep, flags, NUMA_NO_NODE);
  555. }
  556. EXPORT_SYMBOL(kmem_cache_alloc);
  557. void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, gfp_t flags)
  558. {
  559. return slob_alloc_node(cachep, flags, NUMA_NO_NODE);
  560. }
  561. EXPORT_SYMBOL(kmem_cache_alloc_lru);
  562. void *__kmalloc_node(size_t size, gfp_t gfp, int node)
  563. {
  564. return __do_kmalloc_node(size, gfp, node, _RET_IP_);
  565. }
  566. EXPORT_SYMBOL(__kmalloc_node);
  567. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t gfp, int node)
  568. {
  569. return slob_alloc_node(cachep, gfp, node);
  570. }
  571. EXPORT_SYMBOL(kmem_cache_alloc_node);
  572. static void __kmem_cache_free(void *b, int size)
  573. {
  574. if (size < PAGE_SIZE)
  575. slob_free(b, size);
  576. else
  577. slob_free_pages(b, get_order(size));
  578. }
  579. static void kmem_rcu_free(struct rcu_head *head)
  580. {
  581. struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  582. void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  583. __kmem_cache_free(b, slob_rcu->size);
  584. }
  585. void kmem_cache_free(struct kmem_cache *c, void *b)
  586. {
  587. kmemleak_free_recursive(b, c->flags);
  588. trace_kmem_cache_free(_RET_IP_, b, c);
  589. if (unlikely(c->flags & SLAB_TYPESAFE_BY_RCU)) {
  590. struct slob_rcu *slob_rcu;
  591. slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  592. slob_rcu->size = c->size;
  593. call_rcu(&slob_rcu->head, kmem_rcu_free);
  594. } else {
  595. __kmem_cache_free(b, c->size);
  596. }
  597. }
  598. EXPORT_SYMBOL(kmem_cache_free);
  599. void kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
  600. {
  601. size_t i;
  602. for (i = 0; i < nr; i++) {
  603. if (s)
  604. kmem_cache_free(s, p[i]);
  605. else
  606. kfree(p[i]);
  607. }
  608. }
  609. EXPORT_SYMBOL(kmem_cache_free_bulk);
  610. int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
  611. void **p)
  612. {
  613. size_t i;
  614. for (i = 0; i < nr; i++) {
  615. void *x = p[i] = kmem_cache_alloc(s, flags);
  616. if (!x) {
  617. kmem_cache_free_bulk(s, i, p);
  618. return 0;
  619. }
  620. }
  621. return i;
  622. }
  623. EXPORT_SYMBOL(kmem_cache_alloc_bulk);
  624. int __kmem_cache_shutdown(struct kmem_cache *c)
  625. {
  626. /* No way to check for remaining objects */
  627. return 0;
  628. }
  629. void __kmem_cache_release(struct kmem_cache *c)
  630. {
  631. }
  632. int __kmem_cache_shrink(struct kmem_cache *d)
  633. {
  634. return 0;
  635. }
  636. static struct kmem_cache kmem_cache_boot = {
  637. .name = "kmem_cache",
  638. .size = sizeof(struct kmem_cache),
  639. .flags = SLAB_PANIC,
  640. .align = ARCH_KMALLOC_MINALIGN,
  641. };
  642. void __init kmem_cache_init(void)
  643. {
  644. kmem_cache = &kmem_cache_boot;
  645. slab_state = UP;
  646. }
  647. void __init kmem_cache_init_late(void)
  648. {
  649. slab_state = FULL;
  650. }