page_io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/page_io.c
  4. *
  5. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  6. *
  7. * Swap reorganised 29.12.95,
  8. * Asynchronous swapping added 30.12.95. Stephen Tweedie
  9. * Removed race in async swapping. 14.4.1996. Bruno Haible
  10. * Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
  11. * Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/gfp.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/swap.h>
  18. #include <linux/bio.h>
  19. #include <linux/swapops.h>
  20. #include <linux/writeback.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/psi.h>
  23. #include <linux/uio.h>
  24. #include <linux/sched/task.h>
  25. #include <linux/delayacct.h>
  26. #include <linux/zswap.h>
  27. #include "swap.h"
  28. static void __end_swap_bio_write(struct bio *bio)
  29. {
  30. struct folio *folio = bio_first_folio_all(bio);
  31. if (bio->bi_status) {
  32. /*
  33. * We failed to write the page out to swap-space.
  34. * Re-dirty the page in order to avoid it being reclaimed.
  35. * Also print a dire warning that things will go BAD (tm)
  36. * very quickly.
  37. *
  38. * Also clear PG_reclaim to avoid folio_rotate_reclaimable()
  39. */
  40. folio_mark_dirty(folio);
  41. pr_alert_ratelimited("Write-error on swap-device (%u:%u:%llu)\n",
  42. MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
  43. (unsigned long long)bio->bi_iter.bi_sector);
  44. folio_clear_reclaim(folio);
  45. }
  46. folio_end_writeback(folio);
  47. }
  48. static void end_swap_bio_write(struct bio *bio)
  49. {
  50. __end_swap_bio_write(bio);
  51. bio_put(bio);
  52. }
  53. static void __end_swap_bio_read(struct bio *bio)
  54. {
  55. struct folio *folio = bio_first_folio_all(bio);
  56. if (bio->bi_status) {
  57. pr_alert_ratelimited("Read-error on swap-device (%u:%u:%llu)\n",
  58. MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
  59. (unsigned long long)bio->bi_iter.bi_sector);
  60. } else {
  61. folio_mark_uptodate(folio);
  62. }
  63. folio_unlock(folio);
  64. }
  65. static void end_swap_bio_read(struct bio *bio)
  66. {
  67. __end_swap_bio_read(bio);
  68. bio_put(bio);
  69. }
  70. int generic_swapfile_activate(struct swap_info_struct *sis,
  71. struct file *swap_file,
  72. sector_t *span)
  73. {
  74. struct address_space *mapping = swap_file->f_mapping;
  75. struct inode *inode = mapping->host;
  76. unsigned blocks_per_page;
  77. unsigned long page_no;
  78. unsigned blkbits;
  79. sector_t probe_block;
  80. sector_t last_block;
  81. sector_t lowest_block = -1;
  82. sector_t highest_block = 0;
  83. int nr_extents = 0;
  84. int ret;
  85. blkbits = inode->i_blkbits;
  86. blocks_per_page = PAGE_SIZE >> blkbits;
  87. /*
  88. * Map all the blocks into the extent tree. This code doesn't try
  89. * to be very smart.
  90. */
  91. probe_block = 0;
  92. page_no = 0;
  93. last_block = i_size_read(inode) >> blkbits;
  94. while ((probe_block + blocks_per_page) <= last_block &&
  95. page_no < sis->max) {
  96. unsigned block_in_page;
  97. sector_t first_block;
  98. cond_resched();
  99. first_block = probe_block;
  100. ret = bmap(inode, &first_block);
  101. if (ret || !first_block)
  102. goto bad_bmap;
  103. /*
  104. * It must be PAGE_SIZE aligned on-disk
  105. */
  106. if (first_block & (blocks_per_page - 1)) {
  107. probe_block++;
  108. goto reprobe;
  109. }
  110. for (block_in_page = 1; block_in_page < blocks_per_page;
  111. block_in_page++) {
  112. sector_t block;
  113. block = probe_block + block_in_page;
  114. ret = bmap(inode, &block);
  115. if (ret || !block)
  116. goto bad_bmap;
  117. if (block != first_block + block_in_page) {
  118. /* Discontiguity */
  119. probe_block++;
  120. goto reprobe;
  121. }
  122. }
  123. first_block >>= (PAGE_SHIFT - blkbits);
  124. if (page_no) { /* exclude the header page */
  125. if (first_block < lowest_block)
  126. lowest_block = first_block;
  127. if (first_block > highest_block)
  128. highest_block = first_block;
  129. }
  130. /*
  131. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  132. */
  133. ret = add_swap_extent(sis, page_no, 1, first_block);
  134. if (ret < 0)
  135. goto out;
  136. nr_extents += ret;
  137. page_no++;
  138. probe_block += blocks_per_page;
  139. reprobe:
  140. continue;
  141. }
  142. ret = nr_extents;
  143. *span = 1 + highest_block - lowest_block;
  144. if (page_no == 0)
  145. page_no = 1; /* force Empty message */
  146. sis->max = page_no;
  147. sis->pages = page_no - 1;
  148. out:
  149. return ret;
  150. bad_bmap:
  151. pr_err("swapon: swapfile has holes\n");
  152. ret = -EINVAL;
  153. goto out;
  154. }
  155. static bool is_folio_zero_filled(struct folio *folio)
  156. {
  157. unsigned int pos, last_pos;
  158. unsigned long *data;
  159. unsigned int i;
  160. last_pos = PAGE_SIZE / sizeof(*data) - 1;
  161. for (i = 0; i < folio_nr_pages(folio); i++) {
  162. data = kmap_local_folio(folio, i * PAGE_SIZE);
  163. /*
  164. * Check last word first, incase the page is zero-filled at
  165. * the start and has non-zero data at the end, which is common
  166. * in real-world workloads.
  167. */
  168. if (data[last_pos]) {
  169. kunmap_local(data);
  170. return false;
  171. }
  172. for (pos = 0; pos < last_pos; pos++) {
  173. if (data[pos]) {
  174. kunmap_local(data);
  175. return false;
  176. }
  177. }
  178. kunmap_local(data);
  179. }
  180. return true;
  181. }
  182. static void swap_zeromap_folio_set(struct folio *folio)
  183. {
  184. struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio);
  185. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  186. int nr_pages = folio_nr_pages(folio);
  187. swp_entry_t entry;
  188. unsigned int i;
  189. for (i = 0; i < folio_nr_pages(folio); i++) {
  190. entry = page_swap_entry(folio_page(folio, i));
  191. set_bit(swp_offset(entry), sis->zeromap);
  192. }
  193. count_vm_events(SWPOUT_ZERO, nr_pages);
  194. if (objcg) {
  195. count_objcg_events(objcg, SWPOUT_ZERO, nr_pages);
  196. obj_cgroup_put(objcg);
  197. }
  198. }
  199. static void swap_zeromap_folio_clear(struct folio *folio)
  200. {
  201. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  202. swp_entry_t entry;
  203. unsigned int i;
  204. for (i = 0; i < folio_nr_pages(folio); i++) {
  205. entry = page_swap_entry(folio_page(folio, i));
  206. clear_bit(swp_offset(entry), sis->zeromap);
  207. }
  208. }
  209. /*
  210. * We may have stale swap cache pages in memory: notice
  211. * them here and get rid of the unnecessary final write.
  212. */
  213. int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
  214. {
  215. int ret = 0;
  216. if (folio_free_swap(folio))
  217. goto out_unlock;
  218. /*
  219. * Arch code may have to preserve more data than just the page
  220. * contents, e.g. memory tags.
  221. */
  222. ret = arch_prepare_to_swap(folio);
  223. if (ret) {
  224. folio_mark_dirty(folio);
  225. goto out_unlock;
  226. }
  227. /*
  228. * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages.
  229. * The bits in zeromap are protected by the locked swapcache folio
  230. * and atomic updates are used to protect against read-modify-write
  231. * corruption due to other zero swap entries seeing concurrent updates.
  232. */
  233. if (is_folio_zero_filled(folio)) {
  234. swap_zeromap_folio_set(folio);
  235. goto out_unlock;
  236. }
  237. /*
  238. * Clear bits this folio occupies in the zeromap to prevent zero data
  239. * being read in from any previous zero writes that occupied the same
  240. * swap entries.
  241. */
  242. swap_zeromap_folio_clear(folio);
  243. if (zswap_store(folio)) {
  244. count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
  245. goto out_unlock;
  246. }
  247. if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
  248. folio_mark_dirty(folio);
  249. return AOP_WRITEPAGE_ACTIVATE;
  250. }
  251. __swap_writepage(folio, swap_plug);
  252. return 0;
  253. out_unlock:
  254. folio_unlock(folio);
  255. return ret;
  256. }
  257. static inline void count_swpout_vm_event(struct folio *folio)
  258. {
  259. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  260. if (unlikely(folio_test_pmd_mappable(folio))) {
  261. count_memcg_folio_events(folio, THP_SWPOUT, 1);
  262. count_vm_event(THP_SWPOUT);
  263. }
  264. #endif
  265. count_mthp_stat(folio_order(folio), MTHP_STAT_SWPOUT);
  266. count_memcg_folio_events(folio, PSWPOUT, folio_nr_pages(folio));
  267. count_vm_events(PSWPOUT, folio_nr_pages(folio));
  268. }
  269. #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
  270. static void bio_associate_blkg_from_page(struct bio *bio, struct folio *folio)
  271. {
  272. struct cgroup_subsys_state *css;
  273. struct mem_cgroup *memcg;
  274. memcg = folio_memcg(folio);
  275. if (!memcg)
  276. return;
  277. rcu_read_lock();
  278. css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
  279. bio_associate_blkg_from_css(bio, css);
  280. rcu_read_unlock();
  281. }
  282. #else
  283. #define bio_associate_blkg_from_page(bio, folio) do { } while (0)
  284. #endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
  285. struct swap_iocb {
  286. struct kiocb iocb;
  287. struct bio_vec bvec[SWAP_CLUSTER_MAX];
  288. int pages;
  289. int len;
  290. };
  291. static mempool_t *sio_pool;
  292. int sio_pool_init(void)
  293. {
  294. if (!sio_pool) {
  295. mempool_t *pool = mempool_create_kmalloc_pool(
  296. SWAP_CLUSTER_MAX, sizeof(struct swap_iocb));
  297. if (cmpxchg(&sio_pool, NULL, pool))
  298. mempool_destroy(pool);
  299. }
  300. if (!sio_pool)
  301. return -ENOMEM;
  302. return 0;
  303. }
  304. static void sio_write_complete(struct kiocb *iocb, long ret)
  305. {
  306. struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
  307. struct page *page = sio->bvec[0].bv_page;
  308. int p;
  309. if (ret != sio->len) {
  310. /*
  311. * In the case of swap-over-nfs, this can be a
  312. * temporary failure if the system has limited
  313. * memory for allocating transmit buffers.
  314. * Mark the page dirty and avoid
  315. * folio_rotate_reclaimable but rate-limit the
  316. * messages.
  317. */
  318. pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
  319. ret, swap_dev_pos(page_swap_entry(page)));
  320. for (p = 0; p < sio->pages; p++) {
  321. page = sio->bvec[p].bv_page;
  322. set_page_dirty(page);
  323. ClearPageReclaim(page);
  324. }
  325. }
  326. for (p = 0; p < sio->pages; p++)
  327. end_page_writeback(sio->bvec[p].bv_page);
  328. mempool_free(sio, sio_pool);
  329. }
  330. static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
  331. {
  332. struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
  333. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  334. struct file *swap_file = sis->swap_file;
  335. loff_t pos = swap_dev_pos(folio->swap);
  336. count_swpout_vm_event(folio);
  337. folio_start_writeback(folio);
  338. folio_unlock(folio);
  339. if (sio) {
  340. if (sio->iocb.ki_filp != swap_file ||
  341. sio->iocb.ki_pos + sio->len != pos) {
  342. swap_write_unplug(sio);
  343. sio = NULL;
  344. }
  345. }
  346. if (!sio) {
  347. sio = mempool_alloc(sio_pool, GFP_NOIO);
  348. init_sync_kiocb(&sio->iocb, swap_file);
  349. sio->iocb.ki_complete = sio_write_complete;
  350. sio->iocb.ki_pos = pos;
  351. sio->pages = 0;
  352. sio->len = 0;
  353. }
  354. bvec_set_folio(&sio->bvec[sio->pages], folio, folio_size(folio), 0);
  355. sio->len += folio_size(folio);
  356. sio->pages += 1;
  357. if (sio->pages == ARRAY_SIZE(sio->bvec) || !swap_plug) {
  358. swap_write_unplug(sio);
  359. sio = NULL;
  360. }
  361. if (swap_plug)
  362. *swap_plug = sio;
  363. }
  364. static void swap_writepage_bdev_sync(struct folio *folio,
  365. struct swap_info_struct *sis)
  366. {
  367. struct bio_vec bv;
  368. struct bio bio;
  369. bio_init(&bio, sis->bdev, &bv, 1, REQ_OP_WRITE | REQ_SWAP);
  370. bio.bi_iter.bi_sector = swap_folio_sector(folio);
  371. bio_add_folio_nofail(&bio, folio, folio_size(folio), 0);
  372. bio_associate_blkg_from_page(&bio, folio);
  373. count_swpout_vm_event(folio);
  374. folio_start_writeback(folio);
  375. folio_unlock(folio);
  376. submit_bio_wait(&bio);
  377. __end_swap_bio_write(&bio);
  378. }
  379. static void swap_writepage_bdev_async(struct folio *folio,
  380. struct swap_info_struct *sis)
  381. {
  382. struct bio *bio;
  383. bio = bio_alloc(sis->bdev, 1, REQ_OP_WRITE | REQ_SWAP, GFP_NOIO);
  384. bio->bi_iter.bi_sector = swap_folio_sector(folio);
  385. bio->bi_end_io = end_swap_bio_write;
  386. bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
  387. bio_associate_blkg_from_page(bio, folio);
  388. count_swpout_vm_event(folio);
  389. folio_start_writeback(folio);
  390. folio_unlock(folio);
  391. submit_bio(bio);
  392. }
  393. void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
  394. {
  395. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  396. VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
  397. /*
  398. * ->flags can be updated non-atomically (scan_swap_map_slots),
  399. * but that will never affect SWP_FS_OPS, so the data_race
  400. * is safe.
  401. */
  402. if (data_race(sis->flags & SWP_FS_OPS))
  403. swap_writepage_fs(folio, swap_plug);
  404. /*
  405. * ->flags can be updated non-atomically (scan_swap_map_slots),
  406. * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
  407. * is safe.
  408. */
  409. else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
  410. swap_writepage_bdev_sync(folio, sis);
  411. else
  412. swap_writepage_bdev_async(folio, sis);
  413. }
  414. void swap_write_unplug(struct swap_iocb *sio)
  415. {
  416. struct iov_iter from;
  417. struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
  418. int ret;
  419. iov_iter_bvec(&from, ITER_SOURCE, sio->bvec, sio->pages, sio->len);
  420. ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
  421. if (ret != -EIOCBQUEUED)
  422. sio_write_complete(&sio->iocb, ret);
  423. }
  424. static void sio_read_complete(struct kiocb *iocb, long ret)
  425. {
  426. struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
  427. int p;
  428. if (ret == sio->len) {
  429. for (p = 0; p < sio->pages; p++) {
  430. struct folio *folio = page_folio(sio->bvec[p].bv_page);
  431. count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
  432. count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
  433. folio_mark_uptodate(folio);
  434. folio_unlock(folio);
  435. }
  436. count_vm_events(PSWPIN, sio->pages);
  437. } else {
  438. for (p = 0; p < sio->pages; p++) {
  439. struct folio *folio = page_folio(sio->bvec[p].bv_page);
  440. folio_unlock(folio);
  441. }
  442. pr_alert_ratelimited("Read-error on swap-device\n");
  443. }
  444. mempool_free(sio, sio_pool);
  445. }
  446. static bool swap_read_folio_zeromap(struct folio *folio)
  447. {
  448. int nr_pages = folio_nr_pages(folio);
  449. struct obj_cgroup *objcg;
  450. bool is_zeromap;
  451. /*
  452. * Swapping in a large folio that is partially in the zeromap is not
  453. * currently handled. Return true without marking the folio uptodate so
  454. * that an IO error is emitted (e.g. do_swap_page() will sigbus).
  455. */
  456. if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
  457. &is_zeromap) != nr_pages))
  458. return true;
  459. if (!is_zeromap)
  460. return false;
  461. objcg = get_obj_cgroup_from_folio(folio);
  462. count_vm_events(SWPIN_ZERO, nr_pages);
  463. if (objcg) {
  464. count_objcg_events(objcg, SWPIN_ZERO, nr_pages);
  465. obj_cgroup_put(objcg);
  466. }
  467. folio_zero_range(folio, 0, folio_size(folio));
  468. folio_mark_uptodate(folio);
  469. return true;
  470. }
  471. static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
  472. {
  473. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  474. struct swap_iocb *sio = NULL;
  475. loff_t pos = swap_dev_pos(folio->swap);
  476. if (plug)
  477. sio = *plug;
  478. if (sio) {
  479. if (sio->iocb.ki_filp != sis->swap_file ||
  480. sio->iocb.ki_pos + sio->len != pos) {
  481. swap_read_unplug(sio);
  482. sio = NULL;
  483. }
  484. }
  485. if (!sio) {
  486. sio = mempool_alloc(sio_pool, GFP_KERNEL);
  487. init_sync_kiocb(&sio->iocb, sis->swap_file);
  488. sio->iocb.ki_pos = pos;
  489. sio->iocb.ki_complete = sio_read_complete;
  490. sio->pages = 0;
  491. sio->len = 0;
  492. }
  493. bvec_set_folio(&sio->bvec[sio->pages], folio, folio_size(folio), 0);
  494. sio->len += folio_size(folio);
  495. sio->pages += 1;
  496. if (sio->pages == ARRAY_SIZE(sio->bvec) || !plug) {
  497. swap_read_unplug(sio);
  498. sio = NULL;
  499. }
  500. if (plug)
  501. *plug = sio;
  502. }
  503. static void swap_read_folio_bdev_sync(struct folio *folio,
  504. struct swap_info_struct *sis)
  505. {
  506. struct bio_vec bv;
  507. struct bio bio;
  508. bio_init(&bio, sis->bdev, &bv, 1, REQ_OP_READ);
  509. bio.bi_iter.bi_sector = swap_folio_sector(folio);
  510. bio_add_folio_nofail(&bio, folio, folio_size(folio), 0);
  511. /*
  512. * Keep this task valid during swap readpage because the oom killer may
  513. * attempt to access it in the page fault retry time check.
  514. */
  515. get_task_struct(current);
  516. count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
  517. count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
  518. count_vm_events(PSWPIN, folio_nr_pages(folio));
  519. submit_bio_wait(&bio);
  520. __end_swap_bio_read(&bio);
  521. put_task_struct(current);
  522. }
  523. static void swap_read_folio_bdev_async(struct folio *folio,
  524. struct swap_info_struct *sis)
  525. {
  526. struct bio *bio;
  527. bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
  528. bio->bi_iter.bi_sector = swap_folio_sector(folio);
  529. bio->bi_end_io = end_swap_bio_read;
  530. bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
  531. count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
  532. count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
  533. count_vm_events(PSWPIN, folio_nr_pages(folio));
  534. submit_bio(bio);
  535. }
  536. void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
  537. {
  538. struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
  539. bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
  540. bool workingset = folio_test_workingset(folio);
  541. unsigned long pflags;
  542. bool in_thrashing;
  543. VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
  544. VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
  545. VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
  546. /*
  547. * Count submission time as memory stall and delay. When the device
  548. * is congested, or the submitting cgroup IO-throttled, submission
  549. * can be a significant part of overall IO time.
  550. */
  551. if (workingset) {
  552. delayacct_thrashing_start(&in_thrashing);
  553. psi_memstall_enter(&pflags);
  554. }
  555. delayacct_swapin_start();
  556. if (swap_read_folio_zeromap(folio)) {
  557. folio_unlock(folio);
  558. goto finish;
  559. }
  560. if (zswap_load(folio) != -ENOENT)
  561. goto finish;
  562. /* We have to read from slower devices. Increase zswap protection. */
  563. zswap_folio_swapin(folio);
  564. if (data_race(sis->flags & SWP_FS_OPS)) {
  565. swap_read_folio_fs(folio, plug);
  566. } else if (synchronous) {
  567. swap_read_folio_bdev_sync(folio, sis);
  568. } else {
  569. swap_read_folio_bdev_async(folio, sis);
  570. }
  571. finish:
  572. if (workingset) {
  573. delayacct_thrashing_end(&in_thrashing);
  574. psi_memstall_leave(&pflags);
  575. }
  576. delayacct_swapin_end();
  577. }
  578. void __swap_read_unplug(struct swap_iocb *sio)
  579. {
  580. struct iov_iter from;
  581. struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
  582. int ret;
  583. iov_iter_bvec(&from, ITER_DEST, sio->bvec, sio->pages, sio->len);
  584. ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
  585. if (ret != -EIOCBQUEUED)
  586. sio_read_complete(&sio->iocb, ret);
  587. }