page_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/buffer_head.h>
  21. #include <linux/writeback.h>
  22. #include <linux/frontswap.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/psi.h>
  25. #include <linux/uio.h>
  26. #include <linux/sched/task.h>
  27. #include <linux/delayacct.h>
  28. #include "swap.h"
  29. static void end_swap_bio_write(struct bio *bio)
  30. {
  31. struct page *page = bio_first_page_all(bio);
  32. if (bio->bi_status) {
  33. SetPageError(page);
  34. /*
  35. * We failed to write the page out to swap-space.
  36. * Re-dirty the page in order to avoid it being reclaimed.
  37. * Also print a dire warning that things will go BAD (tm)
  38. * very quickly.
  39. *
  40. * Also clear PG_reclaim to avoid folio_rotate_reclaimable()
  41. */
  42. set_page_dirty(page);
  43. pr_alert_ratelimited("Write-error on swap-device (%u:%u:%llu)\n",
  44. MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
  45. (unsigned long long)bio->bi_iter.bi_sector);
  46. ClearPageReclaim(page);
  47. }
  48. end_page_writeback(page);
  49. bio_put(bio);
  50. }
  51. static void end_swap_bio_read(struct bio *bio)
  52. {
  53. struct page *page = bio_first_page_all(bio);
  54. struct task_struct *waiter = bio->bi_private;
  55. if (bio->bi_status) {
  56. SetPageError(page);
  57. ClearPageUptodate(page);
  58. pr_alert_ratelimited("Read-error on swap-device (%u:%u:%llu)\n",
  59. MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
  60. (unsigned long long)bio->bi_iter.bi_sector);
  61. goto out;
  62. }
  63. SetPageUptodate(page);
  64. out:
  65. unlock_page(page);
  66. WRITE_ONCE(bio->bi_private, NULL);
  67. bio_put(bio);
  68. if (waiter) {
  69. blk_wake_io_task(waiter);
  70. put_task_struct(waiter);
  71. }
  72. }
  73. int generic_swapfile_activate(struct swap_info_struct *sis,
  74. struct file *swap_file,
  75. sector_t *span)
  76. {
  77. struct address_space *mapping = swap_file->f_mapping;
  78. struct inode *inode = mapping->host;
  79. unsigned blocks_per_page;
  80. unsigned long page_no;
  81. unsigned blkbits;
  82. sector_t probe_block;
  83. sector_t last_block;
  84. sector_t lowest_block = -1;
  85. sector_t highest_block = 0;
  86. int nr_extents = 0;
  87. int ret;
  88. blkbits = inode->i_blkbits;
  89. blocks_per_page = PAGE_SIZE >> blkbits;
  90. /*
  91. * Map all the blocks into the extent tree. This code doesn't try
  92. * to be very smart.
  93. */
  94. probe_block = 0;
  95. page_no = 0;
  96. last_block = i_size_read(inode) >> blkbits;
  97. while ((probe_block + blocks_per_page) <= last_block &&
  98. page_no < sis->max) {
  99. unsigned block_in_page;
  100. sector_t first_block;
  101. cond_resched();
  102. first_block = probe_block;
  103. ret = bmap(inode, &first_block);
  104. if (ret || !first_block)
  105. goto bad_bmap;
  106. /*
  107. * It must be PAGE_SIZE aligned on-disk
  108. */
  109. if (first_block & (blocks_per_page - 1)) {
  110. probe_block++;
  111. goto reprobe;
  112. }
  113. for (block_in_page = 1; block_in_page < blocks_per_page;
  114. block_in_page++) {
  115. sector_t block;
  116. block = probe_block + block_in_page;
  117. ret = bmap(inode, &block);
  118. if (ret || !block)
  119. goto bad_bmap;
  120. if (block != first_block + block_in_page) {
  121. /* Discontiguity */
  122. probe_block++;
  123. goto reprobe;
  124. }
  125. }
  126. first_block >>= (PAGE_SHIFT - blkbits);
  127. if (page_no) { /* exclude the header page */
  128. if (first_block < lowest_block)
  129. lowest_block = first_block;
  130. if (first_block > highest_block)
  131. highest_block = first_block;
  132. }
  133. /*
  134. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  135. */
  136. ret = add_swap_extent(sis, page_no, 1, first_block);
  137. if (ret < 0)
  138. goto out;
  139. nr_extents += ret;
  140. page_no++;
  141. probe_block += blocks_per_page;
  142. reprobe:
  143. continue;
  144. }
  145. ret = nr_extents;
  146. *span = 1 + highest_block - lowest_block;
  147. if (page_no == 0)
  148. page_no = 1; /* force Empty message */
  149. sis->max = page_no;
  150. sis->pages = page_no - 1;
  151. sis->highest_bit = page_no - 1;
  152. out:
  153. return ret;
  154. bad_bmap:
  155. pr_err("swapon: swapfile has holes\n");
  156. ret = -EINVAL;
  157. goto out;
  158. }
  159. /*
  160. * We may have stale swap cache pages in memory: notice
  161. * them here and get rid of the unnecessary final write.
  162. */
  163. int swap_writepage(struct page *page, struct writeback_control *wbc)
  164. {
  165. struct folio *folio = page_folio(page);
  166. int ret = 0;
  167. if (folio_free_swap(folio)) {
  168. folio_unlock(folio);
  169. goto out;
  170. }
  171. /*
  172. * Arch code may have to preserve more data than just the page
  173. * contents, e.g. memory tags.
  174. */
  175. ret = arch_prepare_to_swap(&folio->page);
  176. if (ret) {
  177. folio_mark_dirty(folio);
  178. folio_unlock(folio);
  179. goto out;
  180. }
  181. if (frontswap_store(&folio->page) == 0) {
  182. folio_start_writeback(folio);
  183. folio_unlock(folio);
  184. folio_end_writeback(folio);
  185. goto out;
  186. }
  187. ret = __swap_writepage(&folio->page, wbc);
  188. out:
  189. return ret;
  190. }
  191. static inline void count_swpout_vm_event(struct page *page)
  192. {
  193. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  194. if (unlikely(PageTransHuge(page)))
  195. count_vm_event(THP_SWPOUT);
  196. #endif
  197. count_vm_events(PSWPOUT, thp_nr_pages(page));
  198. }
  199. #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
  200. static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
  201. {
  202. struct cgroup_subsys_state *css;
  203. struct mem_cgroup *memcg;
  204. memcg = page_memcg(page);
  205. if (!memcg)
  206. return;
  207. rcu_read_lock();
  208. css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
  209. bio_associate_blkg_from_css(bio, css);
  210. rcu_read_unlock();
  211. }
  212. #else
  213. #define bio_associate_blkg_from_page(bio, page) do { } while (0)
  214. #endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
  215. struct swap_iocb {
  216. struct kiocb iocb;
  217. struct bio_vec bvec[SWAP_CLUSTER_MAX];
  218. int pages;
  219. int len;
  220. };
  221. static mempool_t *sio_pool;
  222. int sio_pool_init(void)
  223. {
  224. if (!sio_pool) {
  225. mempool_t *pool = mempool_create_kmalloc_pool(
  226. SWAP_CLUSTER_MAX, sizeof(struct swap_iocb));
  227. if (cmpxchg(&sio_pool, NULL, pool))
  228. mempool_destroy(pool);
  229. }
  230. if (!sio_pool)
  231. return -ENOMEM;
  232. return 0;
  233. }
  234. static void sio_write_complete(struct kiocb *iocb, long ret)
  235. {
  236. struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
  237. struct page *page = sio->bvec[0].bv_page;
  238. int p;
  239. if (ret != sio->len) {
  240. /*
  241. * In the case of swap-over-nfs, this can be a
  242. * temporary failure if the system has limited
  243. * memory for allocating transmit buffers.
  244. * Mark the page dirty and avoid
  245. * folio_rotate_reclaimable but rate-limit the
  246. * messages but do not flag PageError like
  247. * the normal direct-to-bio case as it could
  248. * be temporary.
  249. */
  250. pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
  251. ret, page_file_offset(page));
  252. for (p = 0; p < sio->pages; p++) {
  253. page = sio->bvec[p].bv_page;
  254. set_page_dirty(page);
  255. ClearPageReclaim(page);
  256. }
  257. } else {
  258. for (p = 0; p < sio->pages; p++)
  259. count_swpout_vm_event(sio->bvec[p].bv_page);
  260. }
  261. for (p = 0; p < sio->pages; p++)
  262. end_page_writeback(sio->bvec[p].bv_page);
  263. mempool_free(sio, sio_pool);
  264. }
  265. static int swap_writepage_fs(struct page *page, struct writeback_control *wbc)
  266. {
  267. struct swap_iocb *sio = NULL;
  268. struct swap_info_struct *sis = page_swap_info(page);
  269. struct file *swap_file = sis->swap_file;
  270. loff_t pos = page_file_offset(page);
  271. set_page_writeback(page);
  272. unlock_page(page);
  273. if (wbc->swap_plug)
  274. sio = *wbc->swap_plug;
  275. if (sio) {
  276. if (sio->iocb.ki_filp != swap_file ||
  277. sio->iocb.ki_pos + sio->len != pos) {
  278. swap_write_unplug(sio);
  279. sio = NULL;
  280. }
  281. }
  282. if (!sio) {
  283. sio = mempool_alloc(sio_pool, GFP_NOIO);
  284. init_sync_kiocb(&sio->iocb, swap_file);
  285. sio->iocb.ki_complete = sio_write_complete;
  286. sio->iocb.ki_pos = pos;
  287. sio->pages = 0;
  288. sio->len = 0;
  289. }
  290. sio->bvec[sio->pages].bv_page = page;
  291. sio->bvec[sio->pages].bv_len = thp_size(page);
  292. sio->bvec[sio->pages].bv_offset = 0;
  293. sio->len += thp_size(page);
  294. sio->pages += 1;
  295. if (sio->pages == ARRAY_SIZE(sio->bvec) || !wbc->swap_plug) {
  296. swap_write_unplug(sio);
  297. sio = NULL;
  298. }
  299. if (wbc->swap_plug)
  300. *wbc->swap_plug = sio;
  301. return 0;
  302. }
  303. int __swap_writepage(struct page *page, struct writeback_control *wbc)
  304. {
  305. struct bio *bio;
  306. int ret;
  307. struct swap_info_struct *sis = page_swap_info(page);
  308. VM_BUG_ON_PAGE(!PageSwapCache(page), page);
  309. /*
  310. * ->flags can be updated non-atomicially (scan_swap_map_slots),
  311. * but that will never affect SWP_FS_OPS, so the data_race
  312. * is safe.
  313. */
  314. if (data_race(sis->flags & SWP_FS_OPS))
  315. return swap_writepage_fs(page, wbc);
  316. ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
  317. if (!ret) {
  318. count_swpout_vm_event(page);
  319. return 0;
  320. }
  321. bio = bio_alloc(sis->bdev, 1,
  322. REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc),
  323. GFP_NOIO);
  324. bio->bi_iter.bi_sector = swap_page_sector(page);
  325. bio->bi_end_io = end_swap_bio_write;
  326. bio_add_page(bio, page, thp_size(page), 0);
  327. bio_associate_blkg_from_page(bio, page);
  328. count_swpout_vm_event(page);
  329. set_page_writeback(page);
  330. unlock_page(page);
  331. submit_bio(bio);
  332. return 0;
  333. }
  334. void swap_write_unplug(struct swap_iocb *sio)
  335. {
  336. struct iov_iter from;
  337. struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
  338. int ret;
  339. iov_iter_bvec(&from, ITER_SOURCE, sio->bvec, sio->pages, sio->len);
  340. ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
  341. if (ret != -EIOCBQUEUED)
  342. sio_write_complete(&sio->iocb, ret);
  343. }
  344. static void sio_read_complete(struct kiocb *iocb, long ret)
  345. {
  346. struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
  347. int p;
  348. if (ret == sio->len) {
  349. for (p = 0; p < sio->pages; p++) {
  350. struct page *page = sio->bvec[p].bv_page;
  351. SetPageUptodate(page);
  352. unlock_page(page);
  353. }
  354. count_vm_events(PSWPIN, sio->pages);
  355. } else {
  356. for (p = 0; p < sio->pages; p++) {
  357. struct page *page = sio->bvec[p].bv_page;
  358. SetPageError(page);
  359. ClearPageUptodate(page);
  360. unlock_page(page);
  361. }
  362. pr_alert_ratelimited("Read-error on swap-device\n");
  363. }
  364. mempool_free(sio, sio_pool);
  365. }
  366. static void swap_readpage_fs(struct page *page,
  367. struct swap_iocb **plug)
  368. {
  369. struct swap_info_struct *sis = page_swap_info(page);
  370. struct swap_iocb *sio = NULL;
  371. loff_t pos = page_file_offset(page);
  372. if (plug)
  373. sio = *plug;
  374. if (sio) {
  375. if (sio->iocb.ki_filp != sis->swap_file ||
  376. sio->iocb.ki_pos + sio->len != pos) {
  377. swap_read_unplug(sio);
  378. sio = NULL;
  379. }
  380. }
  381. if (!sio) {
  382. sio = mempool_alloc(sio_pool, GFP_KERNEL);
  383. init_sync_kiocb(&sio->iocb, sis->swap_file);
  384. sio->iocb.ki_pos = pos;
  385. sio->iocb.ki_complete = sio_read_complete;
  386. sio->pages = 0;
  387. sio->len = 0;
  388. }
  389. sio->bvec[sio->pages].bv_page = page;
  390. sio->bvec[sio->pages].bv_len = thp_size(page);
  391. sio->bvec[sio->pages].bv_offset = 0;
  392. sio->len += thp_size(page);
  393. sio->pages += 1;
  394. if (sio->pages == ARRAY_SIZE(sio->bvec) || !plug) {
  395. swap_read_unplug(sio);
  396. sio = NULL;
  397. }
  398. if (plug)
  399. *plug = sio;
  400. }
  401. int swap_readpage(struct page *page, bool synchronous,
  402. struct swap_iocb **plug)
  403. {
  404. struct bio *bio;
  405. int ret = 0;
  406. struct swap_info_struct *sis = page_swap_info(page);
  407. bool workingset = PageWorkingset(page);
  408. unsigned long pflags;
  409. bool in_thrashing;
  410. VM_BUG_ON_PAGE(!PageSwapCache(page) && !synchronous, page);
  411. VM_BUG_ON_PAGE(!PageLocked(page), page);
  412. VM_BUG_ON_PAGE(PageUptodate(page), page);
  413. /*
  414. * Count submission time as memory stall and delay. When the device
  415. * is congested, or the submitting cgroup IO-throttled, submission
  416. * can be a significant part of overall IO time.
  417. */
  418. if (workingset) {
  419. delayacct_thrashing_start(&in_thrashing);
  420. psi_memstall_enter(&pflags);
  421. }
  422. delayacct_swapin_start();
  423. if (frontswap_load(page) == 0) {
  424. SetPageUptodate(page);
  425. unlock_page(page);
  426. goto out;
  427. }
  428. if (data_race(sis->flags & SWP_FS_OPS)) {
  429. swap_readpage_fs(page, plug);
  430. goto out;
  431. }
  432. if (sis->flags & SWP_SYNCHRONOUS_IO) {
  433. ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
  434. if (!ret) {
  435. count_vm_event(PSWPIN);
  436. goto out;
  437. }
  438. }
  439. ret = 0;
  440. bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
  441. bio->bi_iter.bi_sector = swap_page_sector(page);
  442. bio->bi_end_io = end_swap_bio_read;
  443. bio_add_page(bio, page, thp_size(page), 0);
  444. /*
  445. * Keep this task valid during swap readpage because the oom killer may
  446. * attempt to access it in the page fault retry time check.
  447. */
  448. if (synchronous) {
  449. get_task_struct(current);
  450. bio->bi_private = current;
  451. }
  452. count_vm_event(PSWPIN);
  453. bio_get(bio);
  454. submit_bio(bio);
  455. while (synchronous) {
  456. set_current_state(TASK_UNINTERRUPTIBLE);
  457. if (!READ_ONCE(bio->bi_private))
  458. break;
  459. blk_io_schedule();
  460. }
  461. __set_current_state(TASK_RUNNING);
  462. bio_put(bio);
  463. out:
  464. if (workingset) {
  465. delayacct_thrashing_end(&in_thrashing);
  466. psi_memstall_leave(&pflags);
  467. }
  468. delayacct_swapin_end();
  469. return ret;
  470. }
  471. void __swap_read_unplug(struct swap_iocb *sio)
  472. {
  473. struct iov_iter from;
  474. struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
  475. int ret;
  476. iov_iter_bvec(&from, ITER_DEST, sio->bvec, sio->pages, sio->len);
  477. ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
  478. if (ret != -EIOCBQUEUED)
  479. sio_read_complete(&sio->iocb, ret);
  480. }