zswap.c 33 KB

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