zswap.c 35 KB

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