zswap.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * zswap.c - zswap driver file
  3. *
  4. * zswap is a backend for frontswap that takes pages that are in the process
  5. * of being swapped out and attempts to compress and store them in a
  6. * RAM-based memory pool. This can result in a significant I/O reduction on
  7. * the swap device and, in the case where decompressing from RAM is faster
  8. * than reading from the swap 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/zpool.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. /* Total bytes used by the compressed storage */
  45. static u64 zswap_pool_total_size;
  46. /* The number of compressed pages currently stored in zswap */
  47. static atomic_t zswap_stored_pages = ATOMIC_INIT(0);
  48. /*
  49. * The statistics below are not protected from concurrent access for
  50. * performance reasons so they may not be a 100% accurate. However,
  51. * they do provide useful information on roughly how many times a
  52. * certain event is occurring.
  53. */
  54. /* Pool limit was hit (see zswap_max_pool_percent) */
  55. static u64 zswap_pool_limit_hit;
  56. /* Pages written back when pool limit was reached */
  57. static u64 zswap_written_back_pages;
  58. /* Store failed due to a reclaim failure after pool limit was reached */
  59. static u64 zswap_reject_reclaim_fail;
  60. /* Compressed page was too big for the allocator to (optimally) store */
  61. static u64 zswap_reject_compress_poor;
  62. /* Store failed because underlying allocator could not get memory */
  63. static u64 zswap_reject_alloc_fail;
  64. /* Store failed because the entry metadata could not be allocated (rare) */
  65. static u64 zswap_reject_kmemcache_fail;
  66. /* Duplicate store was encountered (rare) */
  67. static u64 zswap_duplicate_entry;
  68. /*********************************
  69. * tunables
  70. **********************************/
  71. #define ZSWAP_PARAM_UNSET ""
  72. /* Enable/disable zswap (disabled by default) */
  73. static bool zswap_enabled;
  74. static int zswap_enabled_param_set(const char *,
  75. const struct kernel_param *);
  76. static struct kernel_param_ops zswap_enabled_param_ops = {
  77. .set = zswap_enabled_param_set,
  78. .get = param_get_bool,
  79. };
  80. module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
  81. /* Crypto compressor to use */
  82. #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
  83. static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  84. static int zswap_compressor_param_set(const char *,
  85. const struct kernel_param *);
  86. static struct kernel_param_ops zswap_compressor_param_ops = {
  87. .set = zswap_compressor_param_set,
  88. .get = param_get_charp,
  89. .free = param_free_charp,
  90. };
  91. module_param_cb(compressor, &zswap_compressor_param_ops,
  92. &zswap_compressor, 0644);
  93. /* Compressed storage zpool to use */
  94. #define ZSWAP_ZPOOL_DEFAULT "zbud"
  95. static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
  96. static int zswap_zpool_param_set(const char *, const struct kernel_param *);
  97. static struct kernel_param_ops zswap_zpool_param_ops = {
  98. .set = zswap_zpool_param_set,
  99. .get = param_get_charp,
  100. .free = param_free_charp,
  101. };
  102. module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
  103. /* The maximum percentage of memory that the compressed pool can occupy */
  104. static unsigned int zswap_max_pool_percent = 20;
  105. module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644);
  106. /*********************************
  107. * data structures
  108. **********************************/
  109. struct zswap_pool {
  110. struct zpool *zpool;
  111. struct crypto_comp * __percpu *tfm;
  112. struct kref kref;
  113. struct list_head list;
  114. struct work_struct work;
  115. struct hlist_node node;
  116. char tfm_name[CRYPTO_MAX_ALG_NAME];
  117. };
  118. /*
  119. * struct zswap_entry
  120. *
  121. * This structure contains the metadata for tracking a single compressed
  122. * page within zswap.
  123. *
  124. * rbnode - links the entry into red-black tree for the appropriate swap type
  125. * offset - the swap offset for the entry. Index into the red-black tree.
  126. * refcount - the number of outstanding reference to the entry. This is needed
  127. * to protect against premature freeing of the entry by code
  128. * concurrent calls to load, invalidate, and writeback. The lock
  129. * for the zswap_tree structure that contains the entry must
  130. * be held while changing the refcount. Since the lock must
  131. * be held, there is no reason to also make refcount atomic.
  132. * length - the length in bytes of the compressed page data. Needed during
  133. * decompression
  134. * pool - the zswap_pool the entry's data is in
  135. * handle - zpool allocation handle that stores the compressed page data
  136. */
  137. struct zswap_entry {
  138. struct rb_node rbnode;
  139. pgoff_t offset;
  140. int refcount;
  141. unsigned int length;
  142. struct zswap_pool *pool;
  143. unsigned long handle;
  144. };
  145. struct zswap_header {
  146. swp_entry_t swpentry;
  147. };
  148. /*
  149. * The tree lock in the zswap_tree struct protects a few things:
  150. * - the rbtree
  151. * - the refcount field of each entry in the tree
  152. */
  153. struct zswap_tree {
  154. struct rb_root rbroot;
  155. spinlock_t lock;
  156. };
  157. static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
  158. /* RCU-protected iteration */
  159. static LIST_HEAD(zswap_pools);
  160. /* protects zswap_pools list modification */
  161. static DEFINE_SPINLOCK(zswap_pools_lock);
  162. /* pool counter to provide unique names to zpool */
  163. static atomic_t zswap_pools_count = ATOMIC_INIT(0);
  164. /* used by param callback function */
  165. static bool zswap_init_started;
  166. /* fatal error during init */
  167. static bool zswap_init_failed;
  168. /* init completed, but couldn't create the initial pool */
  169. static bool zswap_has_pool;
  170. /*********************************
  171. * helpers and fwd declarations
  172. **********************************/
  173. #define zswap_pool_debug(msg, p) \
  174. pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name, \
  175. zpool_get_type((p)->zpool))
  176. static int zswap_writeback_entry(struct zpool *pool, unsigned long handle);
  177. static int zswap_pool_get(struct zswap_pool *pool);
  178. static void zswap_pool_put(struct zswap_pool *pool);
  179. static const struct zpool_ops zswap_zpool_ops = {
  180. .evict = zswap_writeback_entry
  181. };
  182. static bool zswap_is_full(void)
  183. {
  184. return totalram_pages * zswap_max_pool_percent / 100 <
  185. DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
  186. }
  187. static void zswap_update_total_size(void)
  188. {
  189. struct zswap_pool *pool;
  190. u64 total = 0;
  191. rcu_read_lock();
  192. list_for_each_entry_rcu(pool, &zswap_pools, list)
  193. total += zpool_get_total_size(pool->zpool);
  194. rcu_read_unlock();
  195. zswap_pool_total_size = total;
  196. }
  197. /*********************************
  198. * zswap entry functions
  199. **********************************/
  200. static struct kmem_cache *zswap_entry_cache;
  201. static int __init zswap_entry_cache_create(void)
  202. {
  203. zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
  204. return zswap_entry_cache == NULL;
  205. }
  206. static void __init zswap_entry_cache_destroy(void)
  207. {
  208. kmem_cache_destroy(zswap_entry_cache);
  209. }
  210. static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp)
  211. {
  212. struct zswap_entry *entry;
  213. entry = kmem_cache_alloc(zswap_entry_cache, gfp);
  214. if (!entry)
  215. return NULL;
  216. entry->refcount = 1;
  217. RB_CLEAR_NODE(&entry->rbnode);
  218. return entry;
  219. }
  220. static void zswap_entry_cache_free(struct zswap_entry *entry)
  221. {
  222. kmem_cache_free(zswap_entry_cache, entry);
  223. }
  224. /*********************************
  225. * rbtree functions
  226. **********************************/
  227. static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset)
  228. {
  229. struct rb_node *node = root->rb_node;
  230. struct zswap_entry *entry;
  231. while (node) {
  232. entry = rb_entry(node, struct zswap_entry, rbnode);
  233. if (entry->offset > offset)
  234. node = node->rb_left;
  235. else if (entry->offset < offset)
  236. node = node->rb_right;
  237. else
  238. return entry;
  239. }
  240. return NULL;
  241. }
  242. /*
  243. * In the case that a entry with the same offset is found, a pointer to
  244. * the existing entry is stored in dupentry and the function returns -EEXIST
  245. */
  246. static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry,
  247. struct zswap_entry **dupentry)
  248. {
  249. struct rb_node **link = &root->rb_node, *parent = NULL;
  250. struct zswap_entry *myentry;
  251. while (*link) {
  252. parent = *link;
  253. myentry = rb_entry(parent, struct zswap_entry, rbnode);
  254. if (myentry->offset > entry->offset)
  255. link = &(*link)->rb_left;
  256. else if (myentry->offset < entry->offset)
  257. link = &(*link)->rb_right;
  258. else {
  259. *dupentry = myentry;
  260. return -EEXIST;
  261. }
  262. }
  263. rb_link_node(&entry->rbnode, parent, link);
  264. rb_insert_color(&entry->rbnode, root);
  265. return 0;
  266. }
  267. static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry)
  268. {
  269. if (!RB_EMPTY_NODE(&entry->rbnode)) {
  270. rb_erase(&entry->rbnode, root);
  271. RB_CLEAR_NODE(&entry->rbnode);
  272. }
  273. }
  274. /*
  275. * Carries out the common pattern of freeing and entry's zpool allocation,
  276. * freeing the entry itself, and decrementing the number of stored pages.
  277. */
  278. static void zswap_free_entry(struct zswap_entry *entry)
  279. {
  280. zpool_free(entry->pool->zpool, entry->handle);
  281. zswap_pool_put(entry->pool);
  282. zswap_entry_cache_free(entry);
  283. atomic_dec(&zswap_stored_pages);
  284. zswap_update_total_size();
  285. }
  286. /* caller must hold the tree lock */
  287. static void zswap_entry_get(struct zswap_entry *entry)
  288. {
  289. entry->refcount++;
  290. }
  291. /* caller must hold the tree lock
  292. * remove from the tree and free it, if nobody reference the entry
  293. */
  294. static void zswap_entry_put(struct zswap_tree *tree,
  295. struct zswap_entry *entry)
  296. {
  297. int refcount = --entry->refcount;
  298. BUG_ON(refcount < 0);
  299. if (refcount == 0) {
  300. zswap_rb_erase(&tree->rbroot, entry);
  301. zswap_free_entry(entry);
  302. }
  303. }
  304. /* caller must hold the tree lock */
  305. static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
  306. pgoff_t offset)
  307. {
  308. struct zswap_entry *entry;
  309. entry = zswap_rb_search(root, offset);
  310. if (entry)
  311. zswap_entry_get(entry);
  312. return entry;
  313. }
  314. /*********************************
  315. * per-cpu code
  316. **********************************/
  317. static DEFINE_PER_CPU(u8 *, zswap_dstmem);
  318. static int zswap_dstmem_prepare(unsigned int cpu)
  319. {
  320. u8 *dst;
  321. dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
  322. if (!dst)
  323. return -ENOMEM;
  324. per_cpu(zswap_dstmem, cpu) = dst;
  325. return 0;
  326. }
  327. static int zswap_dstmem_dead(unsigned int cpu)
  328. {
  329. u8 *dst;
  330. dst = per_cpu(zswap_dstmem, cpu);
  331. kfree(dst);
  332. per_cpu(zswap_dstmem, cpu) = NULL;
  333. return 0;
  334. }
  335. static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
  336. {
  337. struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
  338. struct crypto_comp *tfm;
  339. if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu)))
  340. return 0;
  341. tfm = crypto_alloc_comp(pool->tfm_name, 0, 0);
  342. if (IS_ERR_OR_NULL(tfm)) {
  343. pr_err("could not alloc crypto comp %s : %ld\n",
  344. pool->tfm_name, PTR_ERR(tfm));
  345. return -ENOMEM;
  346. }
  347. *per_cpu_ptr(pool->tfm, cpu) = tfm;
  348. return 0;
  349. }
  350. static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
  351. {
  352. struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
  353. struct crypto_comp *tfm;
  354. tfm = *per_cpu_ptr(pool->tfm, cpu);
  355. if (!IS_ERR_OR_NULL(tfm))
  356. crypto_free_comp(tfm);
  357. *per_cpu_ptr(pool->tfm, cpu) = NULL;
  358. return 0;
  359. }
  360. /*********************************
  361. * pool functions
  362. **********************************/
  363. static struct zswap_pool *__zswap_pool_current(void)
  364. {
  365. struct zswap_pool *pool;
  366. pool = list_first_or_null_rcu(&zswap_pools, typeof(*pool), list);
  367. WARN_ONCE(!pool && zswap_has_pool,
  368. "%s: no page storage pool!\n", __func__);
  369. return pool;
  370. }
  371. static struct zswap_pool *zswap_pool_current(void)
  372. {
  373. assert_spin_locked(&zswap_pools_lock);
  374. return __zswap_pool_current();
  375. }
  376. static struct zswap_pool *zswap_pool_current_get(void)
  377. {
  378. struct zswap_pool *pool;
  379. rcu_read_lock();
  380. pool = __zswap_pool_current();
  381. if (!zswap_pool_get(pool))
  382. pool = NULL;
  383. rcu_read_unlock();
  384. return pool;
  385. }
  386. static struct zswap_pool *zswap_pool_last_get(void)
  387. {
  388. struct zswap_pool *pool, *last = NULL;
  389. rcu_read_lock();
  390. list_for_each_entry_rcu(pool, &zswap_pools, list)
  391. last = pool;
  392. WARN_ONCE(!last && zswap_has_pool,
  393. "%s: no page storage pool!\n", __func__);
  394. if (!zswap_pool_get(last))
  395. last = NULL;
  396. rcu_read_unlock();
  397. return last;
  398. }
  399. /* type and compressor must be null-terminated */
  400. static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
  401. {
  402. struct zswap_pool *pool;
  403. assert_spin_locked(&zswap_pools_lock);
  404. list_for_each_entry_rcu(pool, &zswap_pools, list) {
  405. if (strcmp(pool->tfm_name, compressor))
  406. continue;
  407. if (strcmp(zpool_get_type(pool->zpool), type))
  408. continue;
  409. /* if we can't get it, it's about to be destroyed */
  410. if (!zswap_pool_get(pool))
  411. continue;
  412. return pool;
  413. }
  414. return NULL;
  415. }
  416. static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
  417. {
  418. struct zswap_pool *pool;
  419. char name[38]; /* 'zswap' + 32 char (max) num + \0 */
  420. gfp_t gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM;
  421. int ret;
  422. if (!zswap_has_pool) {
  423. /* if either are unset, pool initialization failed, and we
  424. * need both params to be set correctly before trying to
  425. * create a pool.
  426. */
  427. if (!strcmp(type, ZSWAP_PARAM_UNSET))
  428. return NULL;
  429. if (!strcmp(compressor, ZSWAP_PARAM_UNSET))
  430. return NULL;
  431. }
  432. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  433. if (!pool)
  434. return NULL;
  435. /* unique name for each pool specifically required by zsmalloc */
  436. snprintf(name, 38, "zswap%x", atomic_inc_return(&zswap_pools_count));
  437. pool->zpool = zpool_create_pool(type, name, gfp, &zswap_zpool_ops);
  438. if (!pool->zpool) {
  439. pr_err("%s zpool not available\n", type);
  440. goto error;
  441. }
  442. pr_debug("using %s zpool\n", zpool_get_type(pool->zpool));
  443. strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name));
  444. pool->tfm = alloc_percpu(struct crypto_comp *);
  445. if (!pool->tfm) {
  446. pr_err("percpu alloc failed\n");
  447. goto error;
  448. }
  449. ret = cpuhp_state_add_instance(CPUHP_MM_ZSWP_POOL_PREPARE,
  450. &pool->node);
  451. if (ret)
  452. goto error;
  453. pr_debug("using %s compressor\n", pool->tfm_name);
  454. /* being the current pool takes 1 ref; this func expects the
  455. * caller to always add the new pool as the current pool
  456. */
  457. kref_init(&pool->kref);
  458. INIT_LIST_HEAD(&pool->list);
  459. zswap_pool_debug("created", pool);
  460. return pool;
  461. error:
  462. free_percpu(pool->tfm);
  463. if (pool->zpool)
  464. zpool_destroy_pool(pool->zpool);
  465. kfree(pool);
  466. return NULL;
  467. }
  468. static __init struct zswap_pool *__zswap_pool_create_fallback(void)
  469. {
  470. bool has_comp, has_zpool;
  471. has_comp = crypto_has_comp(zswap_compressor, 0, 0);
  472. if (!has_comp && strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
  473. pr_err("compressor %s not available, using default %s\n",
  474. zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
  475. param_free_charp(&zswap_compressor);
  476. zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
  477. has_comp = crypto_has_comp(zswap_compressor, 0, 0);
  478. }
  479. if (!has_comp) {
  480. pr_err("default compressor %s not available\n",
  481. zswap_compressor);
  482. param_free_charp(&zswap_compressor);
  483. zswap_compressor = ZSWAP_PARAM_UNSET;
  484. }
  485. has_zpool = zpool_has_pool(zswap_zpool_type);
  486. if (!has_zpool && strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
  487. pr_err("zpool %s not available, using default %s\n",
  488. zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
  489. param_free_charp(&zswap_zpool_type);
  490. zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
  491. has_zpool = zpool_has_pool(zswap_zpool_type);
  492. }
  493. if (!has_zpool) {
  494. pr_err("default zpool %s not available\n",
  495. zswap_zpool_type);
  496. param_free_charp(&zswap_zpool_type);
  497. zswap_zpool_type = ZSWAP_PARAM_UNSET;
  498. }
  499. if (!has_comp || !has_zpool)
  500. return NULL;
  501. return zswap_pool_create(zswap_zpool_type, zswap_compressor);
  502. }
  503. static void zswap_pool_destroy(struct zswap_pool *pool)
  504. {
  505. zswap_pool_debug("destroying", pool);
  506. cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node);
  507. free_percpu(pool->tfm);
  508. zpool_destroy_pool(pool->zpool);
  509. kfree(pool);
  510. }
  511. static int __must_check zswap_pool_get(struct zswap_pool *pool)
  512. {
  513. if (!pool)
  514. return 0;
  515. return kref_get_unless_zero(&pool->kref);
  516. }
  517. static void __zswap_pool_release(struct work_struct *work)
  518. {
  519. struct zswap_pool *pool = container_of(work, typeof(*pool), work);
  520. synchronize_rcu();
  521. /* nobody should have been able to get a kref... */
  522. WARN_ON(kref_get_unless_zero(&pool->kref));
  523. /* pool is now off zswap_pools list and has no references. */
  524. zswap_pool_destroy(pool);
  525. }
  526. static void __zswap_pool_empty(struct kref *kref)
  527. {
  528. struct zswap_pool *pool;
  529. pool = container_of(kref, typeof(*pool), kref);
  530. spin_lock(&zswap_pools_lock);
  531. WARN_ON(pool == zswap_pool_current());
  532. list_del_rcu(&pool->list);
  533. INIT_WORK(&pool->work, __zswap_pool_release);
  534. schedule_work(&pool->work);
  535. spin_unlock(&zswap_pools_lock);
  536. }
  537. static void zswap_pool_put(struct zswap_pool *pool)
  538. {
  539. kref_put(&pool->kref, __zswap_pool_empty);
  540. }
  541. /*********************************
  542. * param callbacks
  543. **********************************/
  544. /* val must be a null-terminated string */
  545. static int __zswap_param_set(const char *val, const struct kernel_param *kp,
  546. char *type, char *compressor)
  547. {
  548. struct zswap_pool *pool, *put_pool = NULL;
  549. char *s = strstrip((char *)val);
  550. int ret;
  551. if (zswap_init_failed) {
  552. pr_err("can't set param, initialization failed\n");
  553. return -ENODEV;
  554. }
  555. /* no change required */
  556. if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
  557. return 0;
  558. /* if this is load-time (pre-init) param setting,
  559. * don't create a pool; that's done during init.
  560. */
  561. if (!zswap_init_started)
  562. return param_set_charp(s, kp);
  563. if (!type) {
  564. if (!zpool_has_pool(s)) {
  565. pr_err("zpool %s not available\n", s);
  566. return -ENOENT;
  567. }
  568. type = s;
  569. } else if (!compressor) {
  570. if (!crypto_has_comp(s, 0, 0)) {
  571. pr_err("compressor %s not available\n", s);
  572. return -ENOENT;
  573. }
  574. compressor = s;
  575. } else {
  576. WARN_ON(1);
  577. return -EINVAL;
  578. }
  579. spin_lock(&zswap_pools_lock);
  580. pool = zswap_pool_find_get(type, compressor);
  581. if (pool) {
  582. zswap_pool_debug("using existing", pool);
  583. WARN_ON(pool == zswap_pool_current());
  584. list_del_rcu(&pool->list);
  585. }
  586. spin_unlock(&zswap_pools_lock);
  587. if (!pool)
  588. pool = zswap_pool_create(type, compressor);
  589. if (pool)
  590. ret = param_set_charp(s, kp);
  591. else
  592. ret = -EINVAL;
  593. spin_lock(&zswap_pools_lock);
  594. if (!ret) {
  595. put_pool = zswap_pool_current();
  596. list_add_rcu(&pool->list, &zswap_pools);
  597. zswap_has_pool = true;
  598. } else if (pool) {
  599. /* add the possibly pre-existing pool to the end of the pools
  600. * list; if it's new (and empty) then it'll be removed and
  601. * destroyed by the put after we drop the lock
  602. */
  603. list_add_tail_rcu(&pool->list, &zswap_pools);
  604. put_pool = pool;
  605. }
  606. spin_unlock(&zswap_pools_lock);
  607. if (!zswap_has_pool && !pool) {
  608. /* if initial pool creation failed, and this pool creation also
  609. * failed, maybe both compressor and zpool params were bad.
  610. * Allow changing this param, so pool creation will succeed
  611. * when the other param is changed. We already verified this
  612. * param is ok in the zpool_has_pool() or crypto_has_comp()
  613. * checks above.
  614. */
  615. ret = param_set_charp(s, kp);
  616. }
  617. /* drop the ref from either the old current pool,
  618. * or the new pool we failed to add
  619. */
  620. if (put_pool)
  621. zswap_pool_put(put_pool);
  622. return ret;
  623. }
  624. static int zswap_compressor_param_set(const char *val,
  625. const struct kernel_param *kp)
  626. {
  627. return __zswap_param_set(val, kp, zswap_zpool_type, NULL);
  628. }
  629. static int zswap_zpool_param_set(const char *val,
  630. const struct kernel_param *kp)
  631. {
  632. return __zswap_param_set(val, kp, NULL, zswap_compressor);
  633. }
  634. static int zswap_enabled_param_set(const char *val,
  635. const struct kernel_param *kp)
  636. {
  637. if (zswap_init_failed) {
  638. pr_err("can't enable, initialization failed\n");
  639. return -ENODEV;
  640. }
  641. if (!zswap_has_pool && zswap_init_started) {
  642. pr_err("can't enable, no pool configured\n");
  643. return -ENODEV;
  644. }
  645. return param_set_bool(val, kp);
  646. }
  647. /*********************************
  648. * writeback code
  649. **********************************/
  650. /* return enum for zswap_get_swap_cache_page */
  651. enum zswap_get_swap_ret {
  652. ZSWAP_SWAPCACHE_NEW,
  653. ZSWAP_SWAPCACHE_EXIST,
  654. ZSWAP_SWAPCACHE_FAIL,
  655. };
  656. /*
  657. * zswap_get_swap_cache_page
  658. *
  659. * This is an adaption of read_swap_cache_async()
  660. *
  661. * This function tries to find a page with the given swap entry
  662. * in the swapper_space address space (the swap cache). If the page
  663. * is found, it is returned in retpage. Otherwise, a page is allocated,
  664. * added to the swap cache, and returned in retpage.
  665. *
  666. * If success, the swap cache page is returned in retpage
  667. * Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache
  668. * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated,
  669. * the new page is added to swapcache and locked
  670. * Returns ZSWAP_SWAPCACHE_FAIL on error
  671. */
  672. static int zswap_get_swap_cache_page(swp_entry_t entry,
  673. struct page **retpage)
  674. {
  675. bool page_was_allocated;
  676. *retpage = __read_swap_cache_async(entry, GFP_KERNEL,
  677. NULL, 0, &page_was_allocated);
  678. if (page_was_allocated)
  679. return ZSWAP_SWAPCACHE_NEW;
  680. if (!*retpage)
  681. return ZSWAP_SWAPCACHE_FAIL;
  682. return ZSWAP_SWAPCACHE_EXIST;
  683. }
  684. /*
  685. * Attempts to free an entry by adding a page to the swap cache,
  686. * decompressing the entry data into the page, and issuing a
  687. * bio write to write the page back to the swap device.
  688. *
  689. * This can be thought of as a "resumed writeback" of the page
  690. * to the swap device. We are basically resuming the same swap
  691. * writeback path that was intercepted with the frontswap_store()
  692. * in the first place. After the page has been decompressed into
  693. * the swap cache, the compressed version stored by zswap can be
  694. * freed.
  695. */
  696. static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
  697. {
  698. struct zswap_header *zhdr;
  699. swp_entry_t swpentry;
  700. struct zswap_tree *tree;
  701. pgoff_t offset;
  702. struct zswap_entry *entry;
  703. struct page *page;
  704. struct crypto_comp *tfm;
  705. u8 *src, *dst;
  706. unsigned int dlen;
  707. int ret;
  708. struct writeback_control wbc = {
  709. .sync_mode = WB_SYNC_NONE,
  710. };
  711. /* extract swpentry from data */
  712. zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
  713. swpentry = zhdr->swpentry; /* here */
  714. zpool_unmap_handle(pool, handle);
  715. tree = zswap_trees[swp_type(swpentry)];
  716. offset = swp_offset(swpentry);
  717. /* find and ref zswap entry */
  718. spin_lock(&tree->lock);
  719. entry = zswap_entry_find_get(&tree->rbroot, offset);
  720. if (!entry) {
  721. /* entry was invalidated */
  722. spin_unlock(&tree->lock);
  723. return 0;
  724. }
  725. spin_unlock(&tree->lock);
  726. BUG_ON(offset != entry->offset);
  727. /* try to allocate swap cache page */
  728. switch (zswap_get_swap_cache_page(swpentry, &page)) {
  729. case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
  730. ret = -ENOMEM;
  731. goto fail;
  732. case ZSWAP_SWAPCACHE_EXIST:
  733. /* page is already in the swap cache, ignore for now */
  734. put_page(page);
  735. ret = -EEXIST;
  736. goto fail;
  737. case ZSWAP_SWAPCACHE_NEW: /* page is locked */
  738. /* decompress */
  739. dlen = PAGE_SIZE;
  740. src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
  741. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  742. dst = kmap_atomic(page);
  743. tfm = *get_cpu_ptr(entry->pool->tfm);
  744. ret = crypto_comp_decompress(tfm, src, entry->length,
  745. dst, &dlen);
  746. put_cpu_ptr(entry->pool->tfm);
  747. kunmap_atomic(dst);
  748. zpool_unmap_handle(entry->pool->zpool, entry->handle);
  749. BUG_ON(ret);
  750. BUG_ON(dlen != PAGE_SIZE);
  751. /* page is up to date */
  752. SetPageUptodate(page);
  753. }
  754. /* move it to the tail of the inactive list after end_writeback */
  755. SetPageReclaim(page);
  756. /* start writeback */
  757. __swap_writepage(page, &wbc, end_swap_bio_write);
  758. put_page(page);
  759. zswap_written_back_pages++;
  760. spin_lock(&tree->lock);
  761. /* drop local reference */
  762. zswap_entry_put(tree, entry);
  763. /*
  764. * There are two possible situations for entry here:
  765. * (1) refcount is 1(normal case), entry is valid and on the tree
  766. * (2) refcount is 0, entry is freed and not on the tree
  767. * because invalidate happened during writeback
  768. * search the tree and free the entry if find entry
  769. */
  770. if (entry == zswap_rb_search(&tree->rbroot, offset))
  771. zswap_entry_put(tree, entry);
  772. spin_unlock(&tree->lock);
  773. goto end;
  774. /*
  775. * if we get here due to ZSWAP_SWAPCACHE_EXIST
  776. * a load may happening concurrently
  777. * it is safe and okay to not free the entry
  778. * if we free the entry in the following put
  779. * it it either okay to return !0
  780. */
  781. fail:
  782. spin_lock(&tree->lock);
  783. zswap_entry_put(tree, entry);
  784. spin_unlock(&tree->lock);
  785. end:
  786. return ret;
  787. }
  788. static int zswap_shrink(void)
  789. {
  790. struct zswap_pool *pool;
  791. int ret;
  792. pool = zswap_pool_last_get();
  793. if (!pool)
  794. return -ENOENT;
  795. ret = zpool_shrink(pool->zpool, 1, NULL);
  796. zswap_pool_put(pool);
  797. return ret;
  798. }
  799. /*********************************
  800. * frontswap hooks
  801. **********************************/
  802. /* attempts to compress and store an single page */
  803. static int zswap_frontswap_store(unsigned type, pgoff_t offset,
  804. struct page *page)
  805. {
  806. struct zswap_tree *tree = zswap_trees[type];
  807. struct zswap_entry *entry, *dupentry;
  808. struct crypto_comp *tfm;
  809. int ret;
  810. unsigned int dlen = PAGE_SIZE, len;
  811. unsigned long handle;
  812. char *buf;
  813. u8 *src, *dst;
  814. struct zswap_header *zhdr;
  815. if (!zswap_enabled || !tree) {
  816. ret = -ENODEV;
  817. goto reject;
  818. }
  819. /* reclaim space if needed */
  820. if (zswap_is_full()) {
  821. zswap_pool_limit_hit++;
  822. if (zswap_shrink()) {
  823. zswap_reject_reclaim_fail++;
  824. ret = -ENOMEM;
  825. goto reject;
  826. }
  827. }
  828. /* allocate entry */
  829. entry = zswap_entry_cache_alloc(GFP_KERNEL);
  830. if (!entry) {
  831. zswap_reject_kmemcache_fail++;
  832. ret = -ENOMEM;
  833. goto reject;
  834. }
  835. /* if entry is successfully added, it keeps the reference */
  836. entry->pool = zswap_pool_current_get();
  837. if (!entry->pool) {
  838. ret = -EINVAL;
  839. goto freepage;
  840. }
  841. /* compress */
  842. dst = get_cpu_var(zswap_dstmem);
  843. tfm = *get_cpu_ptr(entry->pool->tfm);
  844. src = kmap_atomic(page);
  845. ret = crypto_comp_compress(tfm, src, PAGE_SIZE, dst, &dlen);
  846. kunmap_atomic(src);
  847. put_cpu_ptr(entry->pool->tfm);
  848. if (ret) {
  849. ret = -EINVAL;
  850. goto put_dstmem;
  851. }
  852. /* store */
  853. len = dlen + sizeof(struct zswap_header);
  854. ret = zpool_malloc(entry->pool->zpool, len,
  855. __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
  856. &handle);
  857. if (ret == -ENOSPC) {
  858. zswap_reject_compress_poor++;
  859. goto put_dstmem;
  860. }
  861. if (ret) {
  862. zswap_reject_alloc_fail++;
  863. goto put_dstmem;
  864. }
  865. zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
  866. zhdr->swpentry = swp_entry(type, offset);
  867. buf = (u8 *)(zhdr + 1);
  868. memcpy(buf, dst, dlen);
  869. zpool_unmap_handle(entry->pool->zpool, handle);
  870. put_cpu_var(zswap_dstmem);
  871. /* populate entry */
  872. entry->offset = offset;
  873. entry->handle = handle;
  874. entry->length = dlen;
  875. /* map */
  876. spin_lock(&tree->lock);
  877. do {
  878. ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry);
  879. if (ret == -EEXIST) {
  880. zswap_duplicate_entry++;
  881. /* remove from rbtree */
  882. zswap_rb_erase(&tree->rbroot, dupentry);
  883. zswap_entry_put(tree, dupentry);
  884. }
  885. } while (ret == -EEXIST);
  886. spin_unlock(&tree->lock);
  887. /* update stats */
  888. atomic_inc(&zswap_stored_pages);
  889. zswap_update_total_size();
  890. return 0;
  891. put_dstmem:
  892. put_cpu_var(zswap_dstmem);
  893. zswap_pool_put(entry->pool);
  894. freepage:
  895. zswap_entry_cache_free(entry);
  896. reject:
  897. return ret;
  898. }
  899. /*
  900. * returns 0 if the page was successfully decompressed
  901. * return -1 on entry not found or error
  902. */
  903. static int zswap_frontswap_load(unsigned type, pgoff_t offset,
  904. struct page *page)
  905. {
  906. struct zswap_tree *tree = zswap_trees[type];
  907. struct zswap_entry *entry;
  908. struct crypto_comp *tfm;
  909. u8 *src, *dst;
  910. unsigned int dlen;
  911. int ret;
  912. /* find */
  913. spin_lock(&tree->lock);
  914. entry = zswap_entry_find_get(&tree->rbroot, offset);
  915. if (!entry) {
  916. /* entry was written back */
  917. spin_unlock(&tree->lock);
  918. return -1;
  919. }
  920. spin_unlock(&tree->lock);
  921. /* decompress */
  922. dlen = PAGE_SIZE;
  923. src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
  924. ZPOOL_MM_RO) + sizeof(struct zswap_header);
  925. dst = kmap_atomic(page);
  926. tfm = *get_cpu_ptr(entry->pool->tfm);
  927. ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);
  928. put_cpu_ptr(entry->pool->tfm);
  929. kunmap_atomic(dst);
  930. zpool_unmap_handle(entry->pool->zpool, entry->handle);
  931. BUG_ON(ret);
  932. spin_lock(&tree->lock);
  933. zswap_entry_put(tree, entry);
  934. spin_unlock(&tree->lock);
  935. return 0;
  936. }
  937. /* frees an entry in zswap */
  938. static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset)
  939. {
  940. struct zswap_tree *tree = zswap_trees[type];
  941. struct zswap_entry *entry;
  942. /* find */
  943. spin_lock(&tree->lock);
  944. entry = zswap_rb_search(&tree->rbroot, offset);
  945. if (!entry) {
  946. /* entry was written back */
  947. spin_unlock(&tree->lock);
  948. return;
  949. }
  950. /* remove from rbtree */
  951. zswap_rb_erase(&tree->rbroot, entry);
  952. /* drop the initial reference from entry creation */
  953. zswap_entry_put(tree, entry);
  954. spin_unlock(&tree->lock);
  955. }
  956. /* frees all zswap entries for the given swap type */
  957. static void zswap_frontswap_invalidate_area(unsigned type)
  958. {
  959. struct zswap_tree *tree = zswap_trees[type];
  960. struct zswap_entry *entry, *n;
  961. if (!tree)
  962. return;
  963. /* walk the tree and free everything */
  964. spin_lock(&tree->lock);
  965. rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
  966. zswap_free_entry(entry);
  967. tree->rbroot = RB_ROOT;
  968. spin_unlock(&tree->lock);
  969. kfree(tree);
  970. zswap_trees[type] = NULL;
  971. }
  972. static void zswap_frontswap_init(unsigned type)
  973. {
  974. struct zswap_tree *tree;
  975. tree = kzalloc(sizeof(*tree), GFP_KERNEL);
  976. if (!tree) {
  977. pr_err("alloc failed, zswap disabled for swap type %d\n", type);
  978. return;
  979. }
  980. tree->rbroot = RB_ROOT;
  981. spin_lock_init(&tree->lock);
  982. zswap_trees[type] = tree;
  983. }
  984. static struct frontswap_ops zswap_frontswap_ops = {
  985. .store = zswap_frontswap_store,
  986. .load = zswap_frontswap_load,
  987. .invalidate_page = zswap_frontswap_invalidate_page,
  988. .invalidate_area = zswap_frontswap_invalidate_area,
  989. .init = zswap_frontswap_init
  990. };
  991. /*********************************
  992. * debugfs functions
  993. **********************************/
  994. #ifdef CONFIG_DEBUG_FS
  995. #include <linux/debugfs.h>
  996. static struct dentry *zswap_debugfs_root;
  997. static int __init zswap_debugfs_init(void)
  998. {
  999. if (!debugfs_initialized())
  1000. return -ENODEV;
  1001. zswap_debugfs_root = debugfs_create_dir("zswap", NULL);
  1002. if (!zswap_debugfs_root)
  1003. return -ENOMEM;
  1004. debugfs_create_u64("pool_limit_hit", S_IRUGO,
  1005. zswap_debugfs_root, &zswap_pool_limit_hit);
  1006. debugfs_create_u64("reject_reclaim_fail", S_IRUGO,
  1007. zswap_debugfs_root, &zswap_reject_reclaim_fail);
  1008. debugfs_create_u64("reject_alloc_fail", S_IRUGO,
  1009. zswap_debugfs_root, &zswap_reject_alloc_fail);
  1010. debugfs_create_u64("reject_kmemcache_fail", S_IRUGO,
  1011. zswap_debugfs_root, &zswap_reject_kmemcache_fail);
  1012. debugfs_create_u64("reject_compress_poor", S_IRUGO,
  1013. zswap_debugfs_root, &zswap_reject_compress_poor);
  1014. debugfs_create_u64("written_back_pages", S_IRUGO,
  1015. zswap_debugfs_root, &zswap_written_back_pages);
  1016. debugfs_create_u64("duplicate_entry", S_IRUGO,
  1017. zswap_debugfs_root, &zswap_duplicate_entry);
  1018. debugfs_create_u64("pool_total_size", S_IRUGO,
  1019. zswap_debugfs_root, &zswap_pool_total_size);
  1020. debugfs_create_atomic_t("stored_pages", S_IRUGO,
  1021. zswap_debugfs_root, &zswap_stored_pages);
  1022. return 0;
  1023. }
  1024. static void __exit zswap_debugfs_exit(void)
  1025. {
  1026. debugfs_remove_recursive(zswap_debugfs_root);
  1027. }
  1028. #else
  1029. static int __init zswap_debugfs_init(void)
  1030. {
  1031. return 0;
  1032. }
  1033. static void __exit zswap_debugfs_exit(void) { }
  1034. #endif
  1035. /*********************************
  1036. * module init and exit
  1037. **********************************/
  1038. static int __init init_zswap(void)
  1039. {
  1040. struct zswap_pool *pool;
  1041. int ret;
  1042. zswap_init_started = true;
  1043. if (zswap_entry_cache_create()) {
  1044. pr_err("entry cache creation failed\n");
  1045. goto cache_fail;
  1046. }
  1047. ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
  1048. zswap_dstmem_prepare, zswap_dstmem_dead);
  1049. if (ret) {
  1050. pr_err("dstmem alloc failed\n");
  1051. goto dstmem_fail;
  1052. }
  1053. ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
  1054. "mm/zswap_pool:prepare",
  1055. zswap_cpu_comp_prepare,
  1056. zswap_cpu_comp_dead);
  1057. if (ret)
  1058. goto hp_fail;
  1059. pool = __zswap_pool_create_fallback();
  1060. if (pool) {
  1061. pr_info("loaded using pool %s/%s\n", pool->tfm_name,
  1062. zpool_get_type(pool->zpool));
  1063. list_add(&pool->list, &zswap_pools);
  1064. zswap_has_pool = true;
  1065. } else {
  1066. pr_err("pool creation failed\n");
  1067. zswap_enabled = false;
  1068. }
  1069. frontswap_register_ops(&zswap_frontswap_ops);
  1070. if (zswap_debugfs_init())
  1071. pr_warn("debugfs initialization failed\n");
  1072. return 0;
  1073. hp_fail:
  1074. cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
  1075. dstmem_fail:
  1076. zswap_entry_cache_destroy();
  1077. cache_fail:
  1078. /* if built-in, we aren't unloaded on failure; don't allow use */
  1079. zswap_init_failed = true;
  1080. zswap_enabled = false;
  1081. return -ENOMEM;
  1082. }
  1083. /* must be late so crypto has time to come up */
  1084. late_initcall(init_zswap);
  1085. MODULE_LICENSE("GPL");
  1086. MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
  1087. MODULE_DESCRIPTION("Compressed cache for swap pages");