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