zswap.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * zswap.c - zswap driver file
  3. *
  4. * zswap is a backend for frontswap that takes pages that are in the
  5. * process of being swapped out and attempts to compress them and store
  6. * them in a RAM-based memory pool. This results in a significant I/O
  7. * reduction on the real swap device and, in the case of a slow swap
  8. * device, can also improve workload performance.
  9. *
  10. * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/cpu.h>
  25. #include <linux/highmem.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/types.h>
  29. #include <linux/atomic.h>
  30. #include <linux/frontswap.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/swap.h>
  33. #include <linux/crypto.h>
  34. #include <linux/mempool.h>
  35. #include <linux/zsmalloc.h>
  36. #include <linux/mm_types.h>
  37. #include <linux/page-flags.h>
  38. #include <linux/swapops.h>
  39. #include <linux/writeback.h>
  40. #include <linux/pagemap.h>
  41. /*********************************
  42. * statistics
  43. **********************************/
  44. /* Number of memory pages used by the compressed pool */
  45. atomic_t zswap_pool_pages = ATOMIC_INIT(0);
  46. /* The number of compressed pages currently stored in zswap */
  47. atomic_t zswap_stored_pages = ATOMIC_INIT(0);
  48. #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
  49. /* The number of outstanding pages awaiting writeback */
  50. static atomic_t zswap_outstanding_writebacks = ATOMIC_INIT(0);
  51. #endif
  52. /*
  53. * The statistics below are not protected from concurrent access for
  54. * performance reasons so they may not be a 100% accurate. However,
  55. * they do provide useful information on roughly how many times a
  56. * certain event is occurring.
  57. */
  58. static u64 zswap_pool_limit_hit;
  59. static u64 zswap_written_back_pages;
  60. static u64 zswap_reject_compress_poor;
  61. static u64 zswap_writeback_attempted;
  62. static u64 zswap_reject_tmppage_fail;
  63. static u64 zswap_reject_zsmalloc_fail;
  64. static u64 zswap_reject_kmemcache_fail;
  65. static u64 zswap_saved_by_writeback;
  66. static u64 zswap_duplicate_entry;
  67. /*********************************
  68. * tunables
  69. **********************************/
  70. /* Enable/disable zswap (enabled by default, fixed at boot for now) */
  71. static bool zswap_enabled = 1;
  72. module_param_named(enabled, zswap_enabled, bool, 0);
  73. /* Compressor to be used by zswap (fixed at boot for now) */
  74. #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
  75. static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  76. module_param_named(compressor, zswap_compressor, charp, 0);
  77. /* The maximum percentage of memory that the compressed pool can occupy */
  78. static unsigned int zswap_max_pool_percent = 20;
  79. module_param_named(max_pool_percent,
  80. zswap_max_pool_percent, uint, 0644);
  81. /*
  82. * Maximum compression ratio, as as percentage, for an acceptable
  83. * compressed page. Any pages that do not compress by at least
  84. * this ratio will be rejected.
  85. */
  86. static unsigned int zswap_max_compression_ratio = 80;
  87. module_param_named(max_compression_ratio,
  88. zswap_max_compression_ratio, uint, 0644);
  89. /*
  90. * Maximum number of outstanding writebacks allowed at any given time.
  91. * This is to prevent decompressing an unbounded number of compressed
  92. * pages into the swap cache all at once, and to help with writeback
  93. * congestion.
  94. */
  95. #define ZSWAP_MAX_OUTSTANDING_FLUSHES 64
  96. /*********************************
  97. * compression functions
  98. **********************************/
  99. /* per-cpu compression transforms */
  100. static struct crypto_comp * __percpu *zswap_comp_pcpu_tfms;
  101. enum comp_op {
  102. ZSWAP_COMPOP_COMPRESS,
  103. ZSWAP_COMPOP_DECOMPRESS
  104. };
  105. static int zswap_comp_op(enum comp_op op, const u8 *src, unsigned int slen,
  106. u8 *dst, unsigned int *dlen)
  107. {
  108. struct crypto_comp *tfm;
  109. int ret;
  110. tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, get_cpu());
  111. switch (op) {
  112. case ZSWAP_COMPOP_COMPRESS:
  113. ret = crypto_comp_compress(tfm, src, slen, dst, dlen);
  114. break;
  115. case ZSWAP_COMPOP_DECOMPRESS:
  116. ret = crypto_comp_decompress(tfm, src, slen, dst, dlen);
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. }
  121. put_cpu();
  122. return ret;
  123. }
  124. static int __init zswap_comp_init(void)
  125. {
  126. if (!crypto_has_comp(zswap_compressor, 0, 0)) {
  127. pr_info("%s compressor not available\n", zswap_compressor);
  128. /* fall back to default compressor */
  129. zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  130. if (!crypto_has_comp(zswap_compressor, 0, 0))
  131. /* can't even load the default compressor */
  132. return -ENODEV;
  133. }
  134. pr_info("using %s compressor\n", zswap_compressor);
  135. /* alloc percpu transforms */
  136. zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *);
  137. if (!zswap_comp_pcpu_tfms)
  138. return -ENOMEM;
  139. return 0;
  140. }
  141. static void zswap_comp_exit(void)
  142. {
  143. /* free percpu transforms */
  144. if (zswap_comp_pcpu_tfms)
  145. free_percpu(zswap_comp_pcpu_tfms);
  146. }
  147. /*********************************
  148. * data structures
  149. **********************************/
  150. /*
  151. * struct zswap_entry
  152. *
  153. * This structure contains the metadata for tracking a single compressed
  154. * page within zswap.
  155. *
  156. * rbnode - links the entry into red-black tree for the appropriate swap type
  157. * lru - links the entry into the lru list for the appropriate swap type
  158. * refcount - the number of outstanding reference to the entry. This is needed
  159. * to protect against premature freeing of the entry by code
  160. * concurent calls to load, invalidate, and writeback. The lock
  161. * for the zswap_tree structure that contains the entry must
  162. * be held while changing the refcount. Since the lock must
  163. * be held, there is no reason to also make refcount atomic.
  164. * type - the swap type for the entry. Used to map back to the zswap_tree
  165. * structure that contains the entry.
  166. * offset - the swap offset for the entry. Index into the red-black tree.
  167. * handle - zsmalloc allocation handle that stores the compressed page data
  168. * length - the length in bytes of the compressed page data. Needed during
  169. * decompression
  170. */
  171. struct zswap_entry {
  172. struct rb_node rbnode;
  173. struct list_head lru;
  174. int refcount;
  175. pgoff_t offset;
  176. unsigned long handle;
  177. unsigned int length;
  178. };
  179. /*
  180. * The tree lock in the zswap_tree struct protects a few things:
  181. * - the rbtree
  182. * - the lru list
  183. * - the refcount field of each entry in the tree
  184. */
  185. struct zswap_tree {
  186. struct rb_root rbroot;
  187. struct list_head lru;
  188. spinlock_t lock;
  189. struct zs_pool *pool;
  190. unsigned type;
  191. };
  192. static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
  193. /*********************************
  194. * zswap entry functions
  195. **********************************/
  196. #define ZSWAP_KMEM_CACHE_NAME "zswap_entry_cache"
  197. static struct kmem_cache *zswap_entry_cache;
  198. static inline int zswap_entry_cache_create(void)
  199. {
  200. zswap_entry_cache =
  201. kmem_cache_create(ZSWAP_KMEM_CACHE_NAME,
  202. sizeof(struct zswap_entry), 0, 0, NULL);
  203. return (zswap_entry_cache == NULL);
  204. }
  205. static inline void zswap_entry_cache_destory(void)
  206. {
  207. kmem_cache_destroy(zswap_entry_cache);
  208. }
  209. static inline struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
  210. {
  211. struct zswap_entry *entry;
  212. entry = kmem_cache_alloc(zswap_entry_cache, gfp);
  213. if (!entry)
  214. return NULL;
  215. INIT_LIST_HEAD(&entry->lru);
  216. entry->refcount = 1;
  217. return entry;
  218. }
  219. static inline void zswap_entry_cache_free(struct zswap_entry *entry)
  220. {
  221. kmem_cache_free(zswap_entry_cache, entry);
  222. }
  223. static inline void zswap_entry_get(struct zswap_entry *entry)
  224. {
  225. entry->refcount++;
  226. }
  227. static inline int zswap_entry_put(struct zswap_entry *entry)
  228. {
  229. entry->refcount--;
  230. return entry->refcount;
  231. }
  232. /*********************************
  233. * rbtree functions
  234. **********************************/
  235. static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
  236. {
  237. struct rb_node *node = root->rb_node;
  238. struct zswap_entry *entry;
  239. while (node) {
  240. entry = rb_entry(node, struct zswap_entry, rbnode);
  241. if (entry->offset > offset)
  242. node = node->rb_left;
  243. else if (entry->offset < offset)
  244. node = node->rb_right;
  245. else
  246. return entry;
  247. }
  248. return NULL;
  249. }
  250. /*
  251. * In the case that a entry with the same offset is found, it a pointer to
  252. * the existing entry is stored in dupentry and the function returns -EEXIST
  253. */
  254. static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
  255. struct zswap_entry **dupentry)
  256. {
  257. struct rb_node **link = &root->rb_node, *parent = NULL;
  258. struct zswap_entry *myentry;
  259. while (*link) {
  260. parent = *link;
  261. myentry = rb_entry(parent, struct zswap_entry, rbnode);
  262. if (myentry->offset > entry->offset)
  263. link = &(*link)->rb_left;
  264. else if (myentry->offset < entry->offset)
  265. link = &(*link)->rb_right;
  266. else {
  267. *dupentry = myentry;
  268. return -EEXIST;
  269. }
  270. }
  271. rb_link_node(&entry->rbnode, parent, link);
  272. rb_insert_color(&entry->rbnode, root);
  273. return 0;
  274. }
  275. /*********************************
  276. * per-cpu code
  277. **********************************/
  278. static DEFINE_PER_CPU(u8 *, zswap_dstmem);
  279. static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu)
  280. {
  281. struct crypto_comp *tfm;
  282. u8 *dst;
  283. switch (action) {
  284. case CPU_UP_PREPARE:
  285. tfm = crypto_alloc_comp(zswap_compressor, 0, 0);
  286. if (IS_ERR(tfm)) {
  287. pr_err("can't allocate compressor transform\n");
  288. return NOTIFY_BAD;
  289. }
  290. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm;
  291. dst = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
  292. if (!dst) {
  293. pr_err("can't allocate compressor buffer\n");
  294. crypto_free_comp(tfm);
  295. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  296. return NOTIFY_BAD;
  297. }
  298. per_cpu(zswap_dstmem, cpu) = dst;
  299. break;
  300. case CPU_DEAD:
  301. case CPU_UP_CANCELED:
  302. tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu);
  303. if (tfm) {
  304. crypto_free_comp(tfm);
  305. *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL;
  306. }
  307. dst = per_cpu(zswap_dstmem, cpu);
  308. if (dst) {
  309. kfree(dst);
  310. per_cpu(zswap_dstmem, cpu) = NULL;
  311. }
  312. break;
  313. default:
  314. break;
  315. }
  316. return NOTIFY_OK;
  317. }
  318. static int zswap_cpu_notifier(struct notifier_block *nb,
  319. unsigned long action, void *pcpu)
  320. {
  321. unsigned long cpu = (unsigned long)pcpu;
  322. return __zswap_cpu_notifier(action, cpu);
  323. }
  324. static struct notifier_block zswap_cpu_notifier_block = {
  325. .notifier_call = zswap_cpu_notifier
  326. };
  327. static int zswap_cpu_init(void)
  328. {
  329. unsigned long cpu;
  330. get_online_cpus();
  331. for_each_online_cpu(cpu)
  332. if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK)
  333. goto cleanup;
  334. register_cpu_notifier(&zswap_cpu_notifier_block);
  335. put_online_cpus();
  336. return 0;
  337. cleanup:
  338. for_each_online_cpu(cpu)
  339. __zswap_cpu_notifier(CPU_UP_CANCELED, cpu);
  340. put_online_cpus();
  341. return -ENOMEM;
  342. }
  343. /*********************************
  344. * zsmalloc callbacks
  345. **********************************/
  346. static mempool_t *zswap_page_pool;
  347. static inline unsigned int zswap_max_pool_pages(void)
  348. {
  349. return zswap_max_pool_percent * totalram_pages / 100;
  350. }
  351. static inline int zswap_page_pool_create(void)
  352. {
  353. /* TODO: dynamically size mempool */
  354. zswap_page_pool = mempool_create_page_pool(256, 0);
  355. if (!zswap_page_pool)
  356. return -ENOMEM;
  357. return 0;
  358. }
  359. static inline void zswap_page_pool_destroy(void)
  360. {
  361. mempool_destroy(zswap_page_pool);
  362. }
  363. static struct page *zswap_alloc_page(gfp_t flags)
  364. {
  365. struct page *page;
  366. if (atomic_read(&zswap_pool_pages) >= zswap_max_pool_pages()) {
  367. zswap_pool_limit_hit++;
  368. return NULL;
  369. }
  370. page = mempool_alloc(zswap_page_pool, flags);
  371. if (page)
  372. atomic_inc(&zswap_pool_pages);
  373. return page;
  374. }
  375. static void zswap_free_page(struct page *page)
  376. {
  377. if (!page)
  378. return;
  379. mempool_free(page, zswap_page_pool);
  380. atomic_dec(&zswap_pool_pages);
  381. }
  382. static struct zs_ops zswap_zs_ops = {
  383. .alloc = zswap_alloc_page,
  384. .free = zswap_free_page
  385. };
  386. /*********************************
  387. * helpers
  388. **********************************/
  389. /*
  390. * Carries out the common pattern of freeing and entry's zsmalloc allocation,
  391. * freeing the entry itself, and decrementing the number of stored pages.
  392. */
  393. static void zswap_free_entry(struct zswap_tree *tree, struct zswap_entry *entry)
  394. {
  395. zs_free(tree->pool, entry->handle);
  396. zswap_entry_cache_free(entry);
  397. atomic_dec(&zswap_stored_pages);
  398. }
  399. #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
  400. /*********************************
  401. * writeback code
  402. **********************************/
  403. static void zswap_end_swap_write(struct bio *bio, int err)
  404. {
  405. end_swap_bio_write(bio, err);
  406. atomic_dec(&zswap_outstanding_writebacks);
  407. zswap_written_back_pages++;
  408. }
  409. /* return enum for zswap_get_swap_cache_page */
  410. enum zswap_get_swap_ret {
  411. ZSWAP_SWAPCACHE_NEW,
  412. ZSWAP_SWAPCACHE_EXIST,
  413. ZSWAP_SWAPCACHE_NOMEM
  414. };
  415. /*
  416. * zswap_get_swap_cache_page
  417. *
  418. * This is an adaption of read_swap_cache_async()
  419. *
  420. * This function tries to find a page with the given swap entry
  421. * in the swapper_space address space (the swap cache). If the page
  422. * is found, it is returned in retpage. Otherwise, a page is allocated,
  423. * added to the swap cache, and returned in retpage.
  424. *
  425. * If success, the swap cache page is returned in retpage
  426. * Returns 0 if page was already in the swap cache, page is not locked
  427. * Returns 1 if the new page needs to be populated, page is locked
  428. * Returns <0 on error
  429. */
  430. static int zswap_get_swap_cache_page(swp_entry_t entry,
  431. struct page **retpage)
  432. {
  433. struct page *found_page, *new_page = NULL;
  434. struct address_space *swapper_space = &swapper_spaces[swp_type(entry)];
  435. int err;
  436. *retpage = NULL;
  437. do {
  438. /*
  439. * First check the swap cache. Since this is normally
  440. * called after lookup_swap_cache() failed, re-calling
  441. * that would confuse statistics.
  442. */
  443. found_page = find_get_page(swapper_space, entry.val);
  444. if (found_page)
  445. break;
  446. /*
  447. * Get a new page to read into from swap.
  448. */
  449. if (!new_page) {
  450. new_page = alloc_page(GFP_KERNEL);
  451. if (!new_page)
  452. break; /* Out of memory */
  453. }
  454. /*
  455. * call radix_tree_preload() while we can wait.
  456. */
  457. err = radix_tree_preload(GFP_KERNEL);
  458. if (err)
  459. break;
  460. /*
  461. * Swap entry may have been freed since our caller observed it.
  462. */
  463. err = swapcache_prepare(entry);
  464. if (err == -EEXIST) { /* seems racy */
  465. radix_tree_preload_end();
  466. continue;
  467. }
  468. if (err) { /* swp entry is obsolete ? */
  469. radix_tree_preload_end();
  470. break;
  471. }
  472. /* May fail (-ENOMEM) if radix-tree node allocation failed. */
  473. __set_page_locked(new_page);
  474. SetPageSwapBacked(new_page);
  475. err = __add_to_swap_cache(new_page, entry);
  476. if (likely(!err)) {
  477. radix_tree_preload_end();
  478. lru_cache_add_anon(new_page);
  479. *retpage = new_page;
  480. return ZSWAP_SWAPCACHE_NEW;
  481. }
  482. radix_tree_preload_end();
  483. ClearPageSwapBacked(new_page);
  484. __clear_page_locked(new_page);
  485. /*
  486. * add_to_swap_cache() doesn't return -EEXIST, so we can safely
  487. * clear SWAP_HAS_CACHE flag.
  488. */
  489. swapcache_free(entry, NULL);
  490. } while (err != -ENOMEM);
  491. if (new_page)
  492. page_cache_release(new_page);
  493. if (!found_page)
  494. return ZSWAP_SWAPCACHE_NOMEM;
  495. *retpage = found_page;
  496. return ZSWAP_SWAPCACHE_EXIST;
  497. }
  498. /*
  499. * Attempts to free and entry by adding a page to the swap cache,
  500. * decompressing the entry data into the page, and issuing a
  501. * bio write to write the page back to the swap device.
  502. *
  503. * This can be thought of as a "resumed writeback" of the page
  504. * to the swap device. We are basically resuming the same swap
  505. * writeback path that was intercepted with the frontswap_store()
  506. * in the first place. After the page has been decompressed into
  507. * the swap cache, the compressed version stored by zswap can be
  508. * freed.
  509. */
  510. static int zswap_writeback_entry(struct zswap_tree *tree,
  511. struct zswap_entry *entry)
  512. {
  513. unsigned long type = tree->type;
  514. struct page *page;
  515. swp_entry_t swpentry;
  516. u8 *src, *dst;
  517. unsigned int dlen;
  518. int ret;
  519. struct writeback_control wbc = {
  520. .sync_mode = WB_SYNC_NONE,
  521. };
  522. /* get/allocate page in the swap cache */
  523. swpentry = swp_entry(type, entry->offset);
  524. /* try to allocate swap cache page */
  525. switch (zswap_get_swap_cache_page(swpentry, &page)) {
  526. case ZSWAP_SWAPCACHE_NOMEM: /* no memory */
  527. return -ENOMEM;
  528. break; /* not reached */
  529. case ZSWAP_SWAPCACHE_EXIST: /* page is unlocked */
  530. /* page is already in the swap cache, ignore for now */
  531. return -EEXIST;
  532. break; /* not reached */
  533. case ZSWAP_SWAPCACHE_NEW: /* page is locked */
  534. /* decompress */
  535. dlen = PAGE_SIZE;
  536. src = zs_map_object(tree->pool, entry->handle, ZS_MM_RO);
  537. dst = kmap_atomic(page);
  538. ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
  539. dst, &dlen);
  540. kunmap_atomic(dst);
  541. zs_unmap_object(tree->pool, entry->handle);
  542. BUG_ON(ret);
  543. BUG_ON(dlen != PAGE_SIZE);
  544. /* page is up to date */
  545. SetPageUptodate(page);
  546. }
  547. /* start writeback */
  548. SetPageReclaim(page);
  549. if (!__swap_writepage(page, &wbc, zswap_end_swap_write))
  550. atomic_inc(&zswap_outstanding_writebacks);
  551. page_cache_release(page);
  552. return 0;
  553. }
  554. /*
  555. * Attempts to free nr of entries via writeback to the swap device.
  556. * The number of entries that were actually freed is returned.
  557. */
  558. static int zswap_writeback_entries(struct zswap_tree *tree, int nr)
  559. {
  560. struct zswap_entry *entry;
  561. int i, ret, refcount, freed_nr = 0;
  562. for (i = 0; i < nr; i++) {
  563. /*
  564. * This limits is arbitrary for now until a better
  565. * policy can be implemented. This is so we don't
  566. * eat all of RAM decompressing pages for writeback.
  567. */
  568. if (atomic_read(&zswap_outstanding_writebacks) >
  569. ZSWAP_MAX_OUTSTANDING_FLUSHES)
  570. break;
  571. spin_lock(&tree->lock);
  572. /* dequeue from lru */
  573. if (list_empty(&tree->lru)) {
  574. spin_unlock(&tree->lock);
  575. break;
  576. }
  577. entry = list_first_entry(&tree->lru,
  578. struct zswap_entry, lru);
  579. list_del_init(&entry->lru);
  580. /* so invalidate doesn't free the entry from under us */
  581. zswap_entry_get(entry);
  582. spin_unlock(&tree->lock);
  583. /* attempt writeback */
  584. ret = zswap_writeback_entry(tree, entry);
  585. spin_lock(&tree->lock);
  586. /* drop reference from above */
  587. refcount = zswap_entry_put(entry);
  588. if (!ret)
  589. /* drop the initial reference from entry creation */
  590. refcount = zswap_entry_put(entry);
  591. /*
  592. * There are four possible values for refcount here:
  593. * (1) refcount is 2, writeback failed and load is in progress;
  594. * do nothing, load will add us back to the LRU
  595. * (2) refcount is 1, writeback failed; do not free entry,
  596. * add back to LRU
  597. * (3) refcount is 0, (normal case) not invalidate yet;
  598. * remove from rbtree and free entry
  599. * (4) refcount is -1, invalidate happened during writeback;
  600. * free entry
  601. */
  602. if (refcount == 1)
  603. list_add(&entry->lru, &tree->lru);
  604. if (refcount == 0) {
  605. /* no invalidate yet, remove from rbtree */
  606. rb_erase(&entry->rbnode, &tree->rbroot);
  607. }
  608. spin_unlock(&tree->lock);
  609. if (refcount <= 0) {
  610. /* free the entry */
  611. zswap_free_entry(tree, entry);
  612. freed_nr++;
  613. }
  614. }
  615. return freed_nr;
  616. }
  617. #endif /* CONFIG_ZSWAP_ENABLE_WRITEBACK */
  618. /*******************************************
  619. * page pool for temporary compression result
  620. ********************************************/
  621. #define ZSWAP_TMPPAGE_POOL_PAGES 16
  622. static LIST_HEAD(zswap_tmppage_list);
  623. static DEFINE_SPINLOCK(zswap_tmppage_lock);
  624. static void zswap_tmppage_pool_destroy(void)
  625. {
  626. struct page *page, *tmppage;
  627. spin_lock(&zswap_tmppage_lock);
  628. list_for_each_entry_safe(page, tmppage, &zswap_tmppage_list, lru) {
  629. list_del(&page->lru);
  630. __free_pages(page, 1);
  631. }
  632. spin_unlock(&zswap_tmppage_lock);
  633. }
  634. static int zswap_tmppage_pool_create(void)
  635. {
  636. int i;
  637. struct page *page;
  638. for (i = 0; i < ZSWAP_TMPPAGE_POOL_PAGES; i++) {
  639. page = alloc_pages(GFP_KERNEL, 1);
  640. if (!page) {
  641. zswap_tmppage_pool_destroy();
  642. return -ENOMEM;
  643. }
  644. spin_lock(&zswap_tmppage_lock);
  645. list_add(&page->lru, &zswap_tmppage_list);
  646. spin_unlock(&zswap_tmppage_lock);
  647. }
  648. return 0;
  649. }
  650. static inline struct page *zswap_tmppage_alloc(void)
  651. {
  652. struct page *page;
  653. spin_lock(&zswap_tmppage_lock);
  654. if (list_empty(&zswap_tmppage_list)) {
  655. spin_unlock(&zswap_tmppage_lock);
  656. return NULL;
  657. }
  658. page = list_first_entry(&zswap_tmppage_list, struct page, lru);
  659. list_del(&page->lru);
  660. spin_unlock(&zswap_tmppage_lock);
  661. return page;
  662. }
  663. static inline void zswap_tmppage_free(struct page *page)
  664. {
  665. spin_lock(&zswap_tmppage_lock);
  666. list_add(&page->lru, &zswap_tmppage_list);
  667. spin_unlock(&zswap_tmppage_lock);
  668. }
  669. /*********************************
  670. * frontswap hooks
  671. **********************************/
  672. /* attempts to compress and store an single page */
  673. static int zswap_frontswap_store(unsigned type, pgoff_t offset,
  674. struct page *page)
  675. {
  676. struct zswap_tree *tree = zswap_trees[type];
  677. struct zswap_entry *entry, *dupentry;
  678. int ret;
  679. unsigned int dlen = PAGE_SIZE;
  680. unsigned long handle;
  681. char *buf;
  682. u8 *src, *dst;
  683. struct page *tmppage;
  684. bool writeback_attempted = 0;
  685. #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
  686. u8 *tmpdst;
  687. #endif
  688. if (!tree) {
  689. ret = -ENODEV;
  690. goto reject;
  691. }
  692. /* if this page got EIO on pageout before, give up immediately */
  693. if (PageError(page)) {
  694. ret = -ENOMEM;
  695. goto reject;
  696. }
  697. /* allocate entry */
  698. entry = zswap_entry_cache_alloc(GFP_KERNEL);
  699. if (!entry) {
  700. zswap_reject_kmemcache_fail++;
  701. ret = -ENOMEM;
  702. goto reject;
  703. }
  704. /* compress */
  705. dst = get_cpu_var(zswap_dstmem);
  706. src = kmap_atomic(page);
  707. ret = zswap_comp_op(ZSWAP_COMPOP_COMPRESS, src, PAGE_SIZE, dst, &dlen);
  708. kunmap_atomic(src);
  709. if (ret) {
  710. ret = -EINVAL;
  711. goto freepage;
  712. }
  713. if ((dlen * 100 / PAGE_SIZE) > zswap_max_compression_ratio) {
  714. zswap_reject_compress_poor++;
  715. ret = -E2BIG;
  716. goto freepage;
  717. }
  718. /* store */
  719. handle = zs_malloc(tree->pool, dlen,
  720. __GFP_NORETRY | __GFP_HIGHMEM | __GFP_NOMEMALLOC |
  721. __GFP_NOWARN);
  722. if (!handle) {
  723. #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
  724. zswap_writeback_attempted++;
  725. /*
  726. * Copy compressed buffer out of per-cpu storage so
  727. * we can re-enable preemption.
  728. */
  729. tmppage = zswap_tmppage_alloc();
  730. if (!tmppage) {
  731. zswap_reject_tmppage_fail++;
  732. ret = -ENOMEM;
  733. goto freepage;
  734. }
  735. writeback_attempted = 1;
  736. tmpdst = page_address(tmppage);
  737. memcpy(tmpdst, dst, dlen);
  738. dst = tmpdst;
  739. put_cpu_var(zswap_dstmem);
  740. /* try to free up some space */
  741. /* TODO: replace with more targeted policy */
  742. zswap_writeback_entries(tree, 16);
  743. /* try again, allowing wait */
  744. handle = zs_malloc(tree->pool, dlen,
  745. __GFP_NORETRY | __GFP_HIGHMEM | __GFP_NOMEMALLOC |
  746. __GFP_NOWARN);
  747. if (!handle) {
  748. /* still no space, fail */
  749. zswap_reject_zsmalloc_fail++;
  750. ret = -ENOMEM;
  751. goto freepage;
  752. }
  753. zswap_saved_by_writeback++;
  754. #else
  755. ret = -ENOMEM;
  756. goto freepage;
  757. #endif
  758. }
  759. buf = zs_map_object(tree->pool, handle, ZS_MM_WO);
  760. memcpy(buf, dst, dlen);
  761. zs_unmap_object(tree->pool, handle);
  762. if (writeback_attempted)
  763. zswap_tmppage_free(tmppage);
  764. else
  765. put_cpu_var(zswap_dstmem);
  766. /* populate entry */
  767. entry->offset = offset;
  768. entry->handle = handle;
  769. entry->length = dlen;
  770. /* map */
  771. spin_lock(&tree->lock);
  772. do {
  773. ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
  774. if (ret == -EEXIST) {
  775. zswap_duplicate_entry++;
  776. /* remove from rbtree and lru */
  777. rb_erase(&dupentry->rbnode, &tree->rbroot);
  778. if (!list_empty(&dupentry->lru))
  779. list_del_init(&dupentry->lru);
  780. if (!zswap_entry_put(dupentry)) {
  781. /* free */
  782. zswap_free_entry(tree, dupentry);
  783. }
  784. }
  785. } while (ret == -EEXIST);
  786. list_add_tail(&entry->lru, &tree->lru);
  787. spin_unlock(&tree->lock);
  788. /* update stats */
  789. atomic_inc(&zswap_stored_pages);
  790. return 0;
  791. freepage:
  792. if (writeback_attempted)
  793. zswap_tmppage_free(tmppage);
  794. else
  795. put_cpu_var(zswap_dstmem);
  796. zswap_entry_cache_free(entry);
  797. reject:
  798. return ret;
  799. }
  800. /*
  801. * returns 0 if the page was successfully decompressed
  802. * return -1 on entry not found or error
  803. */
  804. static int zswap_frontswap_load(unsigned type, pgoff_t offset,
  805. struct page *page)
  806. {
  807. struct zswap_tree *tree = zswap_trees[type];
  808. struct zswap_entry *entry;
  809. u8 *src, *dst;
  810. unsigned int dlen;
  811. int refcount;
  812. /* find */
  813. spin_lock(&tree->lock);
  814. entry = zswap_rb_search(&tree->rbroot, offset);
  815. if (!entry) {
  816. /* entry was written back */
  817. spin_unlock(&tree->lock);
  818. return -1;
  819. }
  820. zswap_entry_get(entry);
  821. /* remove from lru */
  822. if (!list_empty(&entry->lru))
  823. list_del_init(&entry->lru);
  824. spin_unlock(&tree->lock);
  825. /* decompress */
  826. dlen = PAGE_SIZE;
  827. src = zs_map_object(tree->pool, entry->handle, ZS_MM_RO);
  828. dst = kmap_atomic(page);
  829. zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length,
  830. dst, &dlen);
  831. kunmap_atomic(dst);
  832. zs_unmap_object(tree->pool, entry->handle);
  833. spin_lock(&tree->lock);
  834. refcount = zswap_entry_put(entry);
  835. if (likely(refcount)) {
  836. list_add_tail(&entry->lru, &tree->lru);
  837. spin_unlock(&tree->lock);
  838. return 0;
  839. }
  840. spin_unlock(&tree->lock);
  841. /*
  842. * We don't have to unlink from the rbtree because
  843. * zswap_writeback_entry() or zswap_frontswap_invalidate page()
  844. * has already done this for us if we are the last reference.
  845. */
  846. /* free */
  847. zswap_free_entry(tree, entry);
  848. return 0;
  849. }
  850. /* invalidates a single page */
  851. static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
  852. {
  853. struct zswap_tree *tree = zswap_trees[type];
  854. struct zswap_entry *entry;
  855. int refcount;
  856. /* find */
  857. spin_lock(&tree->lock);
  858. entry = zswap_rb_search(&tree->rbroot, offset);
  859. if (!entry) {
  860. /* entry was written back */
  861. spin_unlock(&tree->lock);
  862. return;
  863. }
  864. /* remove from rbtree and lru */
  865. rb_erase(&entry->rbnode, &tree->rbroot);
  866. if (!list_empty(&entry->lru))
  867. list_del_init(&entry->lru);
  868. /* drop the initial reference from entry creation */
  869. refcount = zswap_entry_put(entry);
  870. spin_unlock(&tree->lock);
  871. if (refcount) {
  872. /* writeback in progress, writeback will free */
  873. return;
  874. }
  875. /* free */
  876. zswap_free_entry(tree, entry);
  877. }
  878. /* invalidates all pages for the given swap type */
  879. static void zswap_frontswap_invalidate_area(unsigned type)
  880. {
  881. struct zswap_tree *tree = zswap_trees[type];
  882. struct rb_node *node;
  883. struct zswap_entry *entry;
  884. if (!tree)
  885. return;
  886. /* walk the tree and free everything */
  887. spin_lock(&tree->lock);
  888. /*
  889. * TODO: Even though this code should not be executed because
  890. * the try_to_unuse() in swapoff should have emptied the tree,
  891. * it is very wasteful to rebalance the tree after every
  892. * removal when we are freeing the whole tree.
  893. *
  894. * If post-order traversal code is ever added to the rbtree
  895. * implementation, it should be used here.
  896. */
  897. while ((node = rb_first(&tree->rbroot))) {
  898. entry = rb_entry(node, struct zswap_entry, rbnode);
  899. rb_erase(&entry->rbnode, &tree->rbroot);
  900. zs_free(tree->pool, entry->handle);
  901. zswap_entry_cache_free(entry);
  902. atomic_dec(&zswap_stored_pages);
  903. }
  904. tree->rbroot = RB_ROOT;
  905. INIT_LIST_HEAD(&tree->lru);
  906. spin_unlock(&tree->lock);
  907. }
  908. /* NOTE: this is called in atomic context from swapon and must not sleep */
  909. static void zswap_frontswap_init(unsigned type)
  910. {
  911. struct zswap_tree *tree;
  912. tree = kzalloc(sizeof(struct zswap_tree), GFP_ATOMIC);
  913. if (!tree)
  914. goto err;
  915. tree->pool = zs_create_pool(GFP_NOWAIT, &zswap_zs_ops);
  916. if (!tree->pool)
  917. goto freetree;
  918. tree->rbroot = RB_ROOT;
  919. INIT_LIST_HEAD(&tree->lru);
  920. spin_lock_init(&tree->lock);
  921. tree->type = type;
  922. zswap_trees[type] = tree;
  923. return;
  924. freetree:
  925. kfree(tree);
  926. err:
  927. pr_err("alloc failed, zswap disabled for swap type %d\n", type);
  928. }
  929. static struct frontswap_ops zswap_frontswap_ops = {
  930. .store = zswap_frontswap_store,
  931. .load = zswap_frontswap_load,
  932. .invalidate_page = zswap_frontswap_invalidate_page,
  933. .invalidate_area = zswap_frontswap_invalidate_area,
  934. .init = zswap_frontswap_init
  935. };
  936. /*********************************
  937. * debugfs functions
  938. **********************************/
  939. #ifdef CONFIG_DEBUG_FS
  940. #include <linux/debugfs.h>
  941. static struct dentry *zswap_debugfs_root;
  942. static int __init zswap_debugfs_init(void)
  943. {
  944. if (!debugfs_initialized())
  945. return -ENODEV;
  946. zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
  947. if (!zswap_debugfs_root)
  948. return -ENOMEM;
  949. debugfs_create_u64("saved_by_writeback", S_IRUGO,
  950. zswap_debugfs_root, &zswap_saved_by_writeback);
  951. debugfs_create_u64("pool_limit_hit", S_IRUGO,
  952. zswap_debugfs_root, &zswap_pool_limit_hit);
  953. debugfs_create_u64("reject_writeback_attempted", S_IRUGO,
  954. zswap_debugfs_root, &zswap_writeback_attempted);
  955. debugfs_create_u64("reject_tmppage_fail", S_IRUGO,
  956. zswap_debugfs_root, &zswap_reject_tmppage_fail);
  957. debugfs_create_u64("reject_zsmalloc_fail", S_IRUGO,
  958. zswap_debugfs_root, &zswap_reject_zsmalloc_fail);
  959. debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
  960. zswap_debugfs_root, &zswap_reject_kmemcache_fail);
  961. debugfs_create_u64("reject_compress_poor", S_IRUGO,
  962. zswap_debugfs_root, &zswap_reject_compress_poor);
  963. debugfs_create_u64("written_back_pages", S_IRUGO,
  964. zswap_debugfs_root, &zswap_written_back_pages);
  965. debugfs_create_u64("duplicate_entry", S_IRUGO,
  966. zswap_debugfs_root, &zswap_duplicate_entry);
  967. debugfs_create_atomic_t("pool_pages", S_IRUGO,
  968. zswap_debugfs_root, &zswap_pool_pages);
  969. debugfs_create_atomic_t("stored_pages", S_IRUGO,
  970. zswap_debugfs_root, &zswap_stored_pages);
  971. #ifdef CONFIG_ZSWAP_ENABLE_WRITEBACK
  972. debugfs_create_atomic_t("outstanding_writebacks", S_IRUGO,
  973. zswap_debugfs_root, &zswap_outstanding_writebacks);
  974. #endif
  975. return 0;
  976. }
  977. static void __exit zswap_debugfs_exit(void)
  978. {
  979. debugfs_remove_recursive(zswap_debugfs_root);
  980. }
  981. #else
  982. static inline int __init zswap_debugfs_init(void)
  983. {
  984. return 0;
  985. }
  986. static inline void __exit zswap_debugfs_exit(void) { }
  987. #endif
  988. /*********************************
  989. * module init and exit
  990. **********************************/
  991. static int __init init_zswap(void)
  992. {
  993. if (!zswap_enabled)
  994. return 0;
  995. pr_info("loading zswap\n");
  996. if (zswap_entry_cache_create()) {
  997. pr_err("entry cache creation failed\n");
  998. goto error;
  999. }
  1000. if (zswap_page_pool_create()) {
  1001. pr_err("page pool initialization failed\n");
  1002. goto pagepoolfail;
  1003. }
  1004. if (zswap_tmppage_pool_create()) {
  1005. pr_err("workmem pool initialization failed\n");
  1006. goto tmppoolfail;
  1007. }
  1008. if (zswap_comp_init()) {
  1009. pr_err("compressor initialization failed\n");
  1010. goto compfail;
  1011. }
  1012. if (zswap_cpu_init()) {
  1013. pr_err("per-cpu initialization failed\n");
  1014. goto pcpufail;
  1015. }
  1016. frontswap_register_ops(&zswap_frontswap_ops);
  1017. if (zswap_debugfs_init())
  1018. pr_warn("debugfs initialization failed\n");
  1019. return 0;
  1020. pcpufail:
  1021. zswap_comp_exit();
  1022. compfail:
  1023. zswap_tmppage_pool_destroy();
  1024. tmppoolfail:
  1025. zswap_page_pool_destroy();
  1026. pagepoolfail:
  1027. zswap_entry_cache_destory();
  1028. error:
  1029. return -ENOMEM;
  1030. }
  1031. /* must be late so crypto has time to come up */
  1032. late_initcall(init_zswap);
  1033. MODULE_LICENSE("GPL");
  1034. MODULE_AUTHOR("Seth Jennings <sjenning@linux.vnet.ibm.com>");
  1035. MODULE_DESCRIPTION("Compressed cache for swap pages");