z3fold.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * z3fold.c
  3. *
  4. * Author: Vitaly Wool <vitaly.wool@konsulko.com>
  5. * Copyright (C) 2016, Sony Mobile Communications Inc.
  6. *
  7. * This implementation is based on zbud written by Seth Jennings.
  8. *
  9. * z3fold is an special purpose allocator for storing compressed pages. It
  10. * can store up to three compressed pages per page which improves the
  11. * compression ratio of zbud while retaining its main concepts (e. g. always
  12. * storing an integral number of objects per page) and simplicity.
  13. * It still has simple and deterministic reclaim properties that make it
  14. * preferable to a higher density approach (with no requirement on integral
  15. * number of object per page) when reclaim is used.
  16. *
  17. * As in zbud, pages are divided into "chunks". The size of the chunks is
  18. * fixed at compile time and is determined by NCHUNKS_ORDER below.
  19. *
  20. * z3fold doesn't export any API and is meant to be used via zpool API.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/atomic.h>
  24. #include <linux/sched.h>
  25. #include <linux/list.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/percpu.h>
  29. #include <linux/preempt.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/zpool.h>
  34. /*****************
  35. * Structures
  36. *****************/
  37. struct z3fold_pool;
  38. struct z3fold_ops {
  39. int (*evict)(struct z3fold_pool *pool, unsigned long handle);
  40. };
  41. enum buddy {
  42. HEADLESS = 0,
  43. FIRST,
  44. MIDDLE,
  45. LAST,
  46. BUDDIES_MAX
  47. };
  48. /*
  49. * struct z3fold_header - z3fold page metadata occupying first chunks of each
  50. * z3fold page, except for HEADLESS pages
  51. * @buddy: links the z3fold page into the relevant list in the
  52. * pool
  53. * @page_lock: per-page lock
  54. * @refcount: reference count for the z3fold page
  55. * @work: work_struct for page layout optimization
  56. * @pool: pointer to the pool which this page belongs to
  57. * @cpu: CPU which this page "belongs" to
  58. * @first_chunks: the size of the first buddy in chunks, 0 if free
  59. * @middle_chunks: the size of the middle buddy in chunks, 0 if free
  60. * @last_chunks: the size of the last buddy in chunks, 0 if free
  61. * @first_num: the starting number (for the first handle)
  62. */
  63. struct z3fold_header {
  64. struct list_head buddy;
  65. spinlock_t page_lock;
  66. struct kref refcount;
  67. struct work_struct work;
  68. struct z3fold_pool *pool;
  69. short cpu;
  70. unsigned short first_chunks;
  71. unsigned short middle_chunks;
  72. unsigned short last_chunks;
  73. unsigned short start_middle;
  74. unsigned short first_num:2;
  75. };
  76. /*
  77. * NCHUNKS_ORDER determines the internal allocation granularity, effectively
  78. * adjusting internal fragmentation. It also determines the number of
  79. * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
  80. * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
  81. * in the beginning of an allocated page are occupied by z3fold header, so
  82. * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
  83. * which shows the max number of free chunks in z3fold page, also there will
  84. * be 63, or 62, respectively, freelists per pool.
  85. */
  86. #define NCHUNKS_ORDER 6
  87. #define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
  88. #define CHUNK_SIZE (1 << CHUNK_SHIFT)
  89. #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
  90. #define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
  91. #define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
  92. #define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
  93. #define BUDDY_MASK (0x3)
  94. #define BUDDY_SHIFT 2
  95. /**
  96. * struct z3fold_pool - stores metadata for each z3fold pool
  97. * @name: pool name
  98. * @lock: protects pool unbuddied/lru lists
  99. * @stale_lock: protects pool stale page list
  100. * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2-
  101. * buddies; the list each z3fold page is added to depends on
  102. * the size of its free region.
  103. * @lru: list tracking the z3fold pages in LRU order by most recently
  104. * added buddy.
  105. * @stale: list of pages marked for freeing
  106. * @pages_nr: number of z3fold pages in the pool.
  107. * @ops: pointer to a structure of user defined operations specified at
  108. * pool creation time.
  109. * @compact_wq: workqueue for page layout background optimization
  110. * @release_wq: workqueue for safe page release
  111. * @work: work_struct for safe page release
  112. *
  113. * This structure is allocated at pool creation time and maintains metadata
  114. * pertaining to a particular z3fold pool.
  115. */
  116. struct z3fold_pool {
  117. const char *name;
  118. spinlock_t lock;
  119. spinlock_t stale_lock;
  120. struct list_head *unbuddied;
  121. struct list_head lru;
  122. struct list_head stale;
  123. atomic64_t pages_nr;
  124. const struct z3fold_ops *ops;
  125. struct zpool *zpool;
  126. const struct zpool_ops *zpool_ops;
  127. struct workqueue_struct *compact_wq;
  128. struct workqueue_struct *release_wq;
  129. struct work_struct work;
  130. };
  131. /*
  132. * Internal z3fold page flags
  133. */
  134. enum z3fold_page_flags {
  135. PAGE_HEADLESS = 0,
  136. MIDDLE_CHUNK_MAPPED,
  137. NEEDS_COMPACTING,
  138. PAGE_STALE,
  139. PAGE_CLAIMED, /* by either reclaim or free */
  140. };
  141. /*****************
  142. * Helpers
  143. *****************/
  144. /* Converts an allocation size in bytes to size in z3fold chunks */
  145. static int size_to_chunks(size_t size)
  146. {
  147. return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
  148. }
  149. #define for_each_unbuddied_list(_iter, _begin) \
  150. for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
  151. static void compact_page_work(struct work_struct *w);
  152. /* Initializes the z3fold header of a newly allocated z3fold page */
  153. static struct z3fold_header *init_z3fold_page(struct page *page,
  154. struct z3fold_pool *pool)
  155. {
  156. struct z3fold_header *zhdr = page_address(page);
  157. INIT_LIST_HEAD(&page->lru);
  158. clear_bit(PAGE_HEADLESS, &page->private);
  159. clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  160. clear_bit(NEEDS_COMPACTING, &page->private);
  161. clear_bit(PAGE_STALE, &page->private);
  162. clear_bit(PAGE_CLAIMED, &page->private);
  163. spin_lock_init(&zhdr->page_lock);
  164. kref_init(&zhdr->refcount);
  165. zhdr->first_chunks = 0;
  166. zhdr->middle_chunks = 0;
  167. zhdr->last_chunks = 0;
  168. zhdr->first_num = 0;
  169. zhdr->start_middle = 0;
  170. zhdr->cpu = -1;
  171. zhdr->pool = pool;
  172. INIT_LIST_HEAD(&zhdr->buddy);
  173. INIT_WORK(&zhdr->work, compact_page_work);
  174. return zhdr;
  175. }
  176. /* Resets the struct page fields and frees the page */
  177. static void free_z3fold_page(struct page *page)
  178. {
  179. __free_page(page);
  180. }
  181. /* Lock a z3fold page */
  182. static inline void z3fold_page_lock(struct z3fold_header *zhdr)
  183. {
  184. spin_lock(&zhdr->page_lock);
  185. }
  186. /* Try to lock a z3fold page */
  187. static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
  188. {
  189. return spin_trylock(&zhdr->page_lock);
  190. }
  191. /* Unlock a z3fold page */
  192. static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
  193. {
  194. spin_unlock(&zhdr->page_lock);
  195. }
  196. /*
  197. * Encodes the handle of a particular buddy within a z3fold page
  198. * Pool lock should be held as this function accesses first_num
  199. */
  200. static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
  201. {
  202. unsigned long handle;
  203. handle = (unsigned long)zhdr;
  204. if (bud != HEADLESS) {
  205. handle |= (bud + zhdr->first_num) & BUDDY_MASK;
  206. if (bud == LAST)
  207. handle |= (zhdr->last_chunks << BUDDY_SHIFT);
  208. }
  209. return handle;
  210. }
  211. /* Returns the z3fold page where a given handle is stored */
  212. static struct z3fold_header *handle_to_z3fold_header(unsigned long handle)
  213. {
  214. return (struct z3fold_header *)(handle & PAGE_MASK);
  215. }
  216. /* only for LAST bud, returns zero otherwise */
  217. static unsigned short handle_to_chunks(unsigned long handle)
  218. {
  219. return (handle & ~PAGE_MASK) >> BUDDY_SHIFT;
  220. }
  221. /*
  222. * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
  223. * but that doesn't matter. because the masking will result in the
  224. * correct buddy number.
  225. */
  226. static enum buddy handle_to_buddy(unsigned long handle)
  227. {
  228. struct z3fold_header *zhdr = handle_to_z3fold_header(handle);
  229. return (handle - zhdr->first_num) & BUDDY_MASK;
  230. }
  231. static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked)
  232. {
  233. struct page *page = virt_to_page(zhdr);
  234. struct z3fold_pool *pool = zhdr->pool;
  235. WARN_ON(!list_empty(&zhdr->buddy));
  236. set_bit(PAGE_STALE, &page->private);
  237. clear_bit(NEEDS_COMPACTING, &page->private);
  238. spin_lock(&pool->lock);
  239. if (!list_empty(&page->lru))
  240. list_del(&page->lru);
  241. spin_unlock(&pool->lock);
  242. if (locked)
  243. z3fold_page_unlock(zhdr);
  244. spin_lock(&pool->stale_lock);
  245. list_add(&zhdr->buddy, &pool->stale);
  246. queue_work(pool->release_wq, &pool->work);
  247. spin_unlock(&pool->stale_lock);
  248. }
  249. static void __attribute__((__unused__))
  250. release_z3fold_page(struct kref *ref)
  251. {
  252. struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
  253. refcount);
  254. __release_z3fold_page(zhdr, false);
  255. }
  256. static void release_z3fold_page_locked(struct kref *ref)
  257. {
  258. struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
  259. refcount);
  260. WARN_ON(z3fold_page_trylock(zhdr));
  261. __release_z3fold_page(zhdr, true);
  262. }
  263. static void release_z3fold_page_locked_list(struct kref *ref)
  264. {
  265. struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
  266. refcount);
  267. spin_lock(&zhdr->pool->lock);
  268. list_del_init(&zhdr->buddy);
  269. spin_unlock(&zhdr->pool->lock);
  270. WARN_ON(z3fold_page_trylock(zhdr));
  271. __release_z3fold_page(zhdr, true);
  272. }
  273. static void free_pages_work(struct work_struct *w)
  274. {
  275. struct z3fold_pool *pool = container_of(w, struct z3fold_pool, work);
  276. spin_lock(&pool->stale_lock);
  277. while (!list_empty(&pool->stale)) {
  278. struct z3fold_header *zhdr = list_first_entry(&pool->stale,
  279. struct z3fold_header, buddy);
  280. struct page *page = virt_to_page(zhdr);
  281. list_del(&zhdr->buddy);
  282. if (WARN_ON(!test_bit(PAGE_STALE, &page->private)))
  283. continue;
  284. spin_unlock(&pool->stale_lock);
  285. cancel_work_sync(&zhdr->work);
  286. free_z3fold_page(page);
  287. cond_resched();
  288. spin_lock(&pool->stale_lock);
  289. }
  290. spin_unlock(&pool->stale_lock);
  291. }
  292. /*
  293. * Returns the number of free chunks in a z3fold page.
  294. * NB: can't be used with HEADLESS pages.
  295. */
  296. static int num_free_chunks(struct z3fold_header *zhdr)
  297. {
  298. int nfree;
  299. /*
  300. * If there is a middle object, pick up the bigger free space
  301. * either before or after it. Otherwise just subtract the number
  302. * of chunks occupied by the first and the last objects.
  303. */
  304. if (zhdr->middle_chunks != 0) {
  305. int nfree_before = zhdr->first_chunks ?
  306. 0 : zhdr->start_middle - ZHDR_CHUNKS;
  307. int nfree_after = zhdr->last_chunks ?
  308. 0 : TOTAL_CHUNKS -
  309. (zhdr->start_middle + zhdr->middle_chunks);
  310. nfree = max(nfree_before, nfree_after);
  311. } else
  312. nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
  313. return nfree;
  314. }
  315. static inline void *mchunk_memmove(struct z3fold_header *zhdr,
  316. unsigned short dst_chunk)
  317. {
  318. void *beg = zhdr;
  319. return memmove(beg + (dst_chunk << CHUNK_SHIFT),
  320. beg + (zhdr->start_middle << CHUNK_SHIFT),
  321. zhdr->middle_chunks << CHUNK_SHIFT);
  322. }
  323. #define BIG_CHUNK_GAP 3
  324. /* Has to be called with lock held */
  325. static int z3fold_compact_page(struct z3fold_header *zhdr)
  326. {
  327. struct page *page = virt_to_page(zhdr);
  328. if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
  329. return 0; /* can't move middle chunk, it's used */
  330. if (zhdr->middle_chunks == 0)
  331. return 0; /* nothing to compact */
  332. if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
  333. /* move to the beginning */
  334. mchunk_memmove(zhdr, ZHDR_CHUNKS);
  335. zhdr->first_chunks = zhdr->middle_chunks;
  336. zhdr->middle_chunks = 0;
  337. zhdr->start_middle = 0;
  338. zhdr->first_num++;
  339. return 1;
  340. }
  341. /*
  342. * moving data is expensive, so let's only do that if
  343. * there's substantial gain (at least BIG_CHUNK_GAP chunks)
  344. */
  345. if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
  346. zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
  347. BIG_CHUNK_GAP) {
  348. mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
  349. zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
  350. return 1;
  351. } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
  352. TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
  353. + zhdr->middle_chunks) >=
  354. BIG_CHUNK_GAP) {
  355. unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
  356. zhdr->middle_chunks;
  357. mchunk_memmove(zhdr, new_start);
  358. zhdr->start_middle = new_start;
  359. return 1;
  360. }
  361. return 0;
  362. }
  363. static void do_compact_page(struct z3fold_header *zhdr, bool locked)
  364. {
  365. struct z3fold_pool *pool = zhdr->pool;
  366. struct page *page;
  367. struct list_head *unbuddied;
  368. int fchunks;
  369. page = virt_to_page(zhdr);
  370. if (locked)
  371. WARN_ON(z3fold_page_trylock(zhdr));
  372. else
  373. z3fold_page_lock(zhdr);
  374. if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
  375. z3fold_page_unlock(zhdr);
  376. return;
  377. }
  378. spin_lock(&pool->lock);
  379. list_del_init(&zhdr->buddy);
  380. spin_unlock(&pool->lock);
  381. if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
  382. atomic64_dec(&pool->pages_nr);
  383. return;
  384. }
  385. z3fold_compact_page(zhdr);
  386. unbuddied = get_cpu_ptr(pool->unbuddied);
  387. fchunks = num_free_chunks(zhdr);
  388. if (fchunks < NCHUNKS &&
  389. (!zhdr->first_chunks || !zhdr->middle_chunks ||
  390. !zhdr->last_chunks)) {
  391. /* the page's not completely free and it's unbuddied */
  392. spin_lock(&pool->lock);
  393. list_add(&zhdr->buddy, &unbuddied[fchunks]);
  394. spin_unlock(&pool->lock);
  395. zhdr->cpu = smp_processor_id();
  396. }
  397. put_cpu_ptr(pool->unbuddied);
  398. z3fold_page_unlock(zhdr);
  399. }
  400. static void compact_page_work(struct work_struct *w)
  401. {
  402. struct z3fold_header *zhdr = container_of(w, struct z3fold_header,
  403. work);
  404. do_compact_page(zhdr, false);
  405. }
  406. /*
  407. * API Functions
  408. */
  409. /**
  410. * z3fold_create_pool() - create a new z3fold pool
  411. * @name: pool name
  412. * @gfp: gfp flags when allocating the z3fold pool structure
  413. * @ops: user-defined operations for the z3fold pool
  414. *
  415. * Return: pointer to the new z3fold pool or NULL if the metadata allocation
  416. * failed.
  417. */
  418. static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp,
  419. const struct z3fold_ops *ops)
  420. {
  421. struct z3fold_pool *pool = NULL;
  422. int i, cpu;
  423. pool = kzalloc(sizeof(struct z3fold_pool), gfp);
  424. if (!pool)
  425. goto out;
  426. spin_lock_init(&pool->lock);
  427. spin_lock_init(&pool->stale_lock);
  428. pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2);
  429. if (!pool->unbuddied)
  430. goto out_pool;
  431. for_each_possible_cpu(cpu) {
  432. struct list_head *unbuddied =
  433. per_cpu_ptr(pool->unbuddied, cpu);
  434. for_each_unbuddied_list(i, 0)
  435. INIT_LIST_HEAD(&unbuddied[i]);
  436. }
  437. INIT_LIST_HEAD(&pool->lru);
  438. INIT_LIST_HEAD(&pool->stale);
  439. atomic64_set(&pool->pages_nr, 0);
  440. pool->name = name;
  441. pool->compact_wq = create_singlethread_workqueue(pool->name);
  442. if (!pool->compact_wq)
  443. goto out_unbuddied;
  444. pool->release_wq = create_singlethread_workqueue(pool->name);
  445. if (!pool->release_wq)
  446. goto out_wq;
  447. INIT_WORK(&pool->work, free_pages_work);
  448. pool->ops = ops;
  449. return pool;
  450. out_wq:
  451. destroy_workqueue(pool->compact_wq);
  452. out_unbuddied:
  453. free_percpu(pool->unbuddied);
  454. out_pool:
  455. kfree(pool);
  456. out:
  457. return NULL;
  458. }
  459. /**
  460. * z3fold_destroy_pool() - destroys an existing z3fold pool
  461. * @pool: the z3fold pool to be destroyed
  462. *
  463. * The pool should be emptied before this function is called.
  464. */
  465. static void z3fold_destroy_pool(struct z3fold_pool *pool)
  466. {
  467. destroy_workqueue(pool->release_wq);
  468. destroy_workqueue(pool->compact_wq);
  469. kfree(pool);
  470. }
  471. /**
  472. * z3fold_alloc() - allocates a region of a given size
  473. * @pool: z3fold pool from which to allocate
  474. * @size: size in bytes of the desired allocation
  475. * @gfp: gfp flags used if the pool needs to grow
  476. * @handle: handle of the new allocation
  477. *
  478. * This function will attempt to find a free region in the pool large enough to
  479. * satisfy the allocation request. A search of the unbuddied lists is
  480. * performed first. If no suitable free region is found, then a new page is
  481. * allocated and added to the pool to satisfy the request.
  482. *
  483. * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
  484. * as z3fold pool pages.
  485. *
  486. * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
  487. * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
  488. * a new page.
  489. */
  490. static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
  491. unsigned long *handle)
  492. {
  493. int chunks = 0, i, freechunks;
  494. struct z3fold_header *zhdr = NULL;
  495. struct page *page = NULL;
  496. enum buddy bud;
  497. bool can_sleep = gfpflags_allow_blocking(gfp);
  498. if (!size || (gfp & __GFP_HIGHMEM))
  499. return -EINVAL;
  500. if (size > PAGE_SIZE)
  501. return -ENOSPC;
  502. if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
  503. bud = HEADLESS;
  504. else {
  505. struct list_head *unbuddied;
  506. chunks = size_to_chunks(size);
  507. lookup:
  508. /* First, try to find an unbuddied z3fold page. */
  509. unbuddied = get_cpu_ptr(pool->unbuddied);
  510. for_each_unbuddied_list(i, chunks) {
  511. struct list_head *l = &unbuddied[i];
  512. zhdr = list_first_entry_or_null(READ_ONCE(l),
  513. struct z3fold_header, buddy);
  514. if (!zhdr)
  515. continue;
  516. /* Re-check under lock. */
  517. spin_lock(&pool->lock);
  518. l = &unbuddied[i];
  519. if (unlikely(zhdr != list_first_entry(READ_ONCE(l),
  520. struct z3fold_header, buddy)) ||
  521. !z3fold_page_trylock(zhdr)) {
  522. spin_unlock(&pool->lock);
  523. put_cpu_ptr(pool->unbuddied);
  524. goto lookup;
  525. }
  526. list_del_init(&zhdr->buddy);
  527. zhdr->cpu = -1;
  528. spin_unlock(&pool->lock);
  529. page = virt_to_page(zhdr);
  530. if (test_bit(NEEDS_COMPACTING, &page->private)) {
  531. z3fold_page_unlock(zhdr);
  532. zhdr = NULL;
  533. put_cpu_ptr(pool->unbuddied);
  534. if (can_sleep)
  535. cond_resched();
  536. goto lookup;
  537. }
  538. /*
  539. * this page could not be removed from its unbuddied
  540. * list while pool lock was held, and then we've taken
  541. * page lock so kref_put could not be called before
  542. * we got here, so it's safe to just call kref_get()
  543. */
  544. kref_get(&zhdr->refcount);
  545. break;
  546. }
  547. put_cpu_ptr(pool->unbuddied);
  548. if (zhdr) {
  549. if (zhdr->first_chunks == 0) {
  550. if (zhdr->middle_chunks != 0 &&
  551. chunks >= zhdr->start_middle)
  552. bud = LAST;
  553. else
  554. bud = FIRST;
  555. } else if (zhdr->last_chunks == 0)
  556. bud = LAST;
  557. else if (zhdr->middle_chunks == 0)
  558. bud = MIDDLE;
  559. else {
  560. if (kref_put(&zhdr->refcount,
  561. release_z3fold_page_locked))
  562. atomic64_dec(&pool->pages_nr);
  563. else
  564. z3fold_page_unlock(zhdr);
  565. pr_err("No free chunks in unbuddied\n");
  566. WARN_ON(1);
  567. goto lookup;
  568. }
  569. goto found;
  570. }
  571. bud = FIRST;
  572. }
  573. page = NULL;
  574. if (can_sleep) {
  575. spin_lock(&pool->stale_lock);
  576. zhdr = list_first_entry_or_null(&pool->stale,
  577. struct z3fold_header, buddy);
  578. /*
  579. * Before allocating a page, let's see if we can take one from
  580. * the stale pages list. cancel_work_sync() can sleep so we
  581. * limit this case to the contexts where we can sleep
  582. */
  583. if (zhdr) {
  584. list_del(&zhdr->buddy);
  585. spin_unlock(&pool->stale_lock);
  586. cancel_work_sync(&zhdr->work);
  587. page = virt_to_page(zhdr);
  588. } else {
  589. spin_unlock(&pool->stale_lock);
  590. }
  591. }
  592. if (!page)
  593. page = alloc_page(gfp);
  594. if (!page)
  595. return -ENOMEM;
  596. atomic64_inc(&pool->pages_nr);
  597. zhdr = init_z3fold_page(page, pool);
  598. if (bud == HEADLESS) {
  599. set_bit(PAGE_HEADLESS, &page->private);
  600. goto headless;
  601. }
  602. z3fold_page_lock(zhdr);
  603. found:
  604. if (bud == FIRST)
  605. zhdr->first_chunks = chunks;
  606. else if (bud == LAST)
  607. zhdr->last_chunks = chunks;
  608. else {
  609. zhdr->middle_chunks = chunks;
  610. zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
  611. }
  612. if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
  613. zhdr->middle_chunks == 0) {
  614. struct list_head *unbuddied = get_cpu_ptr(pool->unbuddied);
  615. /* Add to unbuddied list */
  616. freechunks = num_free_chunks(zhdr);
  617. spin_lock(&pool->lock);
  618. list_add(&zhdr->buddy, &unbuddied[freechunks]);
  619. spin_unlock(&pool->lock);
  620. zhdr->cpu = smp_processor_id();
  621. put_cpu_ptr(pool->unbuddied);
  622. }
  623. headless:
  624. spin_lock(&pool->lock);
  625. /* Add/move z3fold page to beginning of LRU */
  626. if (!list_empty(&page->lru))
  627. list_del(&page->lru);
  628. list_add(&page->lru, &pool->lru);
  629. *handle = encode_handle(zhdr, bud);
  630. spin_unlock(&pool->lock);
  631. if (bud != HEADLESS)
  632. z3fold_page_unlock(zhdr);
  633. return 0;
  634. }
  635. /**
  636. * z3fold_free() - frees the allocation associated with the given handle
  637. * @pool: pool in which the allocation resided
  638. * @handle: handle associated with the allocation returned by z3fold_alloc()
  639. *
  640. * In the case that the z3fold page in which the allocation resides is under
  641. * reclaim, as indicated by the PG_reclaim flag being set, this function
  642. * only sets the first|last_chunks to 0. The page is actually freed
  643. * once both buddies are evicted (see z3fold_reclaim_page() below).
  644. */
  645. static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
  646. {
  647. struct z3fold_header *zhdr;
  648. struct page *page;
  649. enum buddy bud;
  650. zhdr = handle_to_z3fold_header(handle);
  651. page = virt_to_page(zhdr);
  652. if (test_bit(PAGE_HEADLESS, &page->private)) {
  653. /* if a headless page is under reclaim, just leave.
  654. * NB: we use test_and_set_bit for a reason: if the bit
  655. * has not been set before, we release this page
  656. * immediately so we don't care about its value any more.
  657. */
  658. if (!test_and_set_bit(PAGE_CLAIMED, &page->private)) {
  659. spin_lock(&pool->lock);
  660. list_del(&page->lru);
  661. spin_unlock(&pool->lock);
  662. free_z3fold_page(page);
  663. atomic64_dec(&pool->pages_nr);
  664. }
  665. return;
  666. }
  667. /* Non-headless case */
  668. z3fold_page_lock(zhdr);
  669. bud = handle_to_buddy(handle);
  670. switch (bud) {
  671. case FIRST:
  672. zhdr->first_chunks = 0;
  673. break;
  674. case MIDDLE:
  675. zhdr->middle_chunks = 0;
  676. break;
  677. case LAST:
  678. zhdr->last_chunks = 0;
  679. break;
  680. default:
  681. pr_err("%s: unknown bud %d\n", __func__, bud);
  682. WARN_ON(1);
  683. z3fold_page_unlock(zhdr);
  684. return;
  685. }
  686. if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) {
  687. atomic64_dec(&pool->pages_nr);
  688. return;
  689. }
  690. if (test_bit(PAGE_CLAIMED, &page->private)) {
  691. z3fold_page_unlock(zhdr);
  692. return;
  693. }
  694. if (test_and_set_bit(NEEDS_COMPACTING, &page->private)) {
  695. z3fold_page_unlock(zhdr);
  696. return;
  697. }
  698. if (zhdr->cpu < 0 || !cpu_online(zhdr->cpu)) {
  699. spin_lock(&pool->lock);
  700. list_del_init(&zhdr->buddy);
  701. spin_unlock(&pool->lock);
  702. zhdr->cpu = -1;
  703. kref_get(&zhdr->refcount);
  704. do_compact_page(zhdr, true);
  705. return;
  706. }
  707. kref_get(&zhdr->refcount);
  708. queue_work_on(zhdr->cpu, pool->compact_wq, &zhdr->work);
  709. z3fold_page_unlock(zhdr);
  710. }
  711. /**
  712. * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
  713. * @pool: pool from which a page will attempt to be evicted
  714. * @retries: number of pages on the LRU list for which eviction will
  715. * be attempted before failing
  716. *
  717. * z3fold reclaim is different from normal system reclaim in that it is done
  718. * from the bottom, up. This is because only the bottom layer, z3fold, has
  719. * information on how the allocations are organized within each z3fold page.
  720. * This has the potential to create interesting locking situations between
  721. * z3fold and the user, however.
  722. *
  723. * To avoid these, this is how z3fold_reclaim_page() should be called:
  724. *
  725. * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
  726. * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
  727. * call the user-defined eviction handler with the pool and handle as
  728. * arguments.
  729. *
  730. * If the handle can not be evicted, the eviction handler should return
  731. * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
  732. * appropriate list and try the next z3fold page on the LRU up to
  733. * a user defined number of retries.
  734. *
  735. * If the handle is successfully evicted, the eviction handler should
  736. * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
  737. * contains logic to delay freeing the page if the page is under reclaim,
  738. * as indicated by the setting of the PG_reclaim flag on the underlying page.
  739. *
  740. * If all buddies in the z3fold page are successfully evicted, then the
  741. * z3fold page can be freed.
  742. *
  743. * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
  744. * no pages to evict or an eviction handler is not registered, -EAGAIN if
  745. * the retry limit was hit.
  746. */
  747. static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
  748. {
  749. int i, ret = 0;
  750. struct z3fold_header *zhdr = NULL;
  751. struct page *page = NULL;
  752. struct list_head *pos;
  753. unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
  754. spin_lock(&pool->lock);
  755. if (!pool->ops || !pool->ops->evict || retries == 0) {
  756. spin_unlock(&pool->lock);
  757. return -EINVAL;
  758. }
  759. for (i = 0; i < retries; i++) {
  760. if (list_empty(&pool->lru)) {
  761. spin_unlock(&pool->lock);
  762. return -EINVAL;
  763. }
  764. list_for_each_prev(pos, &pool->lru) {
  765. page = list_entry(pos, struct page, lru);
  766. /* this bit could have been set by free, in which case
  767. * we pass over to the next page in the pool.
  768. */
  769. if (test_and_set_bit(PAGE_CLAIMED, &page->private))
  770. continue;
  771. zhdr = page_address(page);
  772. if (test_bit(PAGE_HEADLESS, &page->private))
  773. break;
  774. if (!z3fold_page_trylock(zhdr)) {
  775. zhdr = NULL;
  776. continue; /* can't evict at this point */
  777. }
  778. kref_get(&zhdr->refcount);
  779. list_del_init(&zhdr->buddy);
  780. zhdr->cpu = -1;
  781. break;
  782. }
  783. if (!zhdr)
  784. break;
  785. list_del_init(&page->lru);
  786. spin_unlock(&pool->lock);
  787. if (!test_bit(PAGE_HEADLESS, &page->private)) {
  788. /*
  789. * We need encode the handles before unlocking, since
  790. * we can race with free that will set
  791. * (first|last)_chunks to 0
  792. */
  793. first_handle = 0;
  794. last_handle = 0;
  795. middle_handle = 0;
  796. if (zhdr->first_chunks)
  797. first_handle = encode_handle(zhdr, FIRST);
  798. if (zhdr->middle_chunks)
  799. middle_handle = encode_handle(zhdr, MIDDLE);
  800. if (zhdr->last_chunks)
  801. last_handle = encode_handle(zhdr, LAST);
  802. /*
  803. * it's safe to unlock here because we hold a
  804. * reference to this page
  805. */
  806. z3fold_page_unlock(zhdr);
  807. } else {
  808. first_handle = encode_handle(zhdr, HEADLESS);
  809. last_handle = middle_handle = 0;
  810. }
  811. /* Issue the eviction callback(s) */
  812. if (middle_handle) {
  813. ret = pool->ops->evict(pool, middle_handle);
  814. if (ret)
  815. goto next;
  816. }
  817. if (first_handle) {
  818. ret = pool->ops->evict(pool, first_handle);
  819. if (ret)
  820. goto next;
  821. }
  822. if (last_handle) {
  823. ret = pool->ops->evict(pool, last_handle);
  824. if (ret)
  825. goto next;
  826. }
  827. next:
  828. if (test_bit(PAGE_HEADLESS, &page->private)) {
  829. if (ret == 0) {
  830. free_z3fold_page(page);
  831. atomic64_dec(&pool->pages_nr);
  832. return 0;
  833. }
  834. spin_lock(&pool->lock);
  835. list_add(&page->lru, &pool->lru);
  836. spin_unlock(&pool->lock);
  837. } else {
  838. z3fold_page_lock(zhdr);
  839. clear_bit(PAGE_CLAIMED, &page->private);
  840. if (kref_put(&zhdr->refcount,
  841. release_z3fold_page_locked)) {
  842. atomic64_dec(&pool->pages_nr);
  843. return 0;
  844. }
  845. /*
  846. * if we are here, the page is still not completely
  847. * free. Take the global pool lock then to be able
  848. * to add it back to the lru list
  849. */
  850. spin_lock(&pool->lock);
  851. list_add(&page->lru, &pool->lru);
  852. spin_unlock(&pool->lock);
  853. z3fold_page_unlock(zhdr);
  854. }
  855. /* We started off locked to we need to lock the pool back */
  856. spin_lock(&pool->lock);
  857. }
  858. spin_unlock(&pool->lock);
  859. return -EAGAIN;
  860. }
  861. /**
  862. * z3fold_map() - maps the allocation associated with the given handle
  863. * @pool: pool in which the allocation resides
  864. * @handle: handle associated with the allocation to be mapped
  865. *
  866. * Extracts the buddy number from handle and constructs the pointer to the
  867. * correct starting chunk within the page.
  868. *
  869. * Returns: a pointer to the mapped allocation
  870. */
  871. static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
  872. {
  873. struct z3fold_header *zhdr;
  874. struct page *page;
  875. void *addr;
  876. enum buddy buddy;
  877. zhdr = handle_to_z3fold_header(handle);
  878. addr = zhdr;
  879. page = virt_to_page(zhdr);
  880. if (test_bit(PAGE_HEADLESS, &page->private))
  881. goto out;
  882. z3fold_page_lock(zhdr);
  883. buddy = handle_to_buddy(handle);
  884. switch (buddy) {
  885. case FIRST:
  886. addr += ZHDR_SIZE_ALIGNED;
  887. break;
  888. case MIDDLE:
  889. addr += zhdr->start_middle << CHUNK_SHIFT;
  890. set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  891. break;
  892. case LAST:
  893. addr += PAGE_SIZE - (handle_to_chunks(handle) << CHUNK_SHIFT);
  894. break;
  895. default:
  896. pr_err("unknown buddy id %d\n", buddy);
  897. WARN_ON(1);
  898. addr = NULL;
  899. break;
  900. }
  901. z3fold_page_unlock(zhdr);
  902. out:
  903. return addr;
  904. }
  905. /**
  906. * z3fold_unmap() - unmaps the allocation associated with the given handle
  907. * @pool: pool in which the allocation resides
  908. * @handle: handle associated with the allocation to be unmapped
  909. */
  910. static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
  911. {
  912. struct z3fold_header *zhdr;
  913. struct page *page;
  914. enum buddy buddy;
  915. zhdr = handle_to_z3fold_header(handle);
  916. page = virt_to_page(zhdr);
  917. if (test_bit(PAGE_HEADLESS, &page->private))
  918. return;
  919. z3fold_page_lock(zhdr);
  920. buddy = handle_to_buddy(handle);
  921. if (buddy == MIDDLE)
  922. clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  923. z3fold_page_unlock(zhdr);
  924. }
  925. /**
  926. * z3fold_get_pool_size() - gets the z3fold pool size in pages
  927. * @pool: pool whose size is being queried
  928. *
  929. * Returns: size in pages of the given pool.
  930. */
  931. static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
  932. {
  933. return atomic64_read(&pool->pages_nr);
  934. }
  935. /*****************
  936. * zpool
  937. ****************/
  938. static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
  939. {
  940. if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
  941. return pool->zpool_ops->evict(pool->zpool, handle);
  942. else
  943. return -ENOENT;
  944. }
  945. static const struct z3fold_ops z3fold_zpool_ops = {
  946. .evict = z3fold_zpool_evict
  947. };
  948. static void *z3fold_zpool_create(const char *name, gfp_t gfp,
  949. const struct zpool_ops *zpool_ops,
  950. struct zpool *zpool)
  951. {
  952. struct z3fold_pool *pool;
  953. pool = z3fold_create_pool(name, gfp,
  954. zpool_ops ? &z3fold_zpool_ops : NULL);
  955. if (pool) {
  956. pool->zpool = zpool;
  957. pool->zpool_ops = zpool_ops;
  958. }
  959. return pool;
  960. }
  961. static void z3fold_zpool_destroy(void *pool)
  962. {
  963. z3fold_destroy_pool(pool);
  964. }
  965. static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  966. unsigned long *handle)
  967. {
  968. return z3fold_alloc(pool, size, gfp, handle);
  969. }
  970. static void z3fold_zpool_free(void *pool, unsigned long handle)
  971. {
  972. z3fold_free(pool, handle);
  973. }
  974. static int z3fold_zpool_shrink(void *pool, unsigned int pages,
  975. unsigned int *reclaimed)
  976. {
  977. unsigned int total = 0;
  978. int ret = -EINVAL;
  979. while (total < pages) {
  980. ret = z3fold_reclaim_page(pool, 8);
  981. if (ret < 0)
  982. break;
  983. total++;
  984. }
  985. if (reclaimed)
  986. *reclaimed = total;
  987. return ret;
  988. }
  989. static void *z3fold_zpool_map(void *pool, unsigned long handle,
  990. enum zpool_mapmode mm)
  991. {
  992. return z3fold_map(pool, handle);
  993. }
  994. static void z3fold_zpool_unmap(void *pool, unsigned long handle)
  995. {
  996. z3fold_unmap(pool, handle);
  997. }
  998. static u64 z3fold_zpool_total_size(void *pool)
  999. {
  1000. return z3fold_get_pool_size(pool) * PAGE_SIZE;
  1001. }
  1002. static struct zpool_driver z3fold_zpool_driver = {
  1003. .type = "z3fold",
  1004. .owner = THIS_MODULE,
  1005. .create = z3fold_zpool_create,
  1006. .destroy = z3fold_zpool_destroy,
  1007. .malloc = z3fold_zpool_malloc,
  1008. .free = z3fold_zpool_free,
  1009. .shrink = z3fold_zpool_shrink,
  1010. .map = z3fold_zpool_map,
  1011. .unmap = z3fold_zpool_unmap,
  1012. .total_size = z3fold_zpool_total_size,
  1013. };
  1014. MODULE_ALIAS("zpool-z3fold");
  1015. static int __init init_z3fold(void)
  1016. {
  1017. /* Make sure the z3fold header is not larger than the page size */
  1018. BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
  1019. zpool_register_driver(&z3fold_zpool_driver);
  1020. return 0;
  1021. }
  1022. static void __exit exit_z3fold(void)
  1023. {
  1024. zpool_unregister_driver(&z3fold_zpool_driver);
  1025. }
  1026. module_init(init_z3fold);
  1027. module_exit(exit_z3fold);
  1028. MODULE_LICENSE("GPL");
  1029. MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
  1030. MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");