xref: /linux/mm/page_io.c (revision 7d8d6ad659c02ed5d2387777194c22e8e81dbb2b)
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 
14 #include <linux/mm.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/gfp.h>
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>
22 #include <linux/blkdev.h>
23 #include <linux/psi.h>
24 #include <linux/uio.h>
25 #include <linux/sched/task.h>
26 #include <linux/delayacct.h>
27 #include <linux/zswap.h>
28 #include "swap.h"
29 #include "swap_table.h"
30 
31 static void __end_swap_bio_write(struct bio *bio)
32 {
33 	struct folio *folio = bio_first_folio_all(bio);
34 
35 	if (bio->bi_status) {
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 		 *
42 		 * Also clear PG_reclaim to avoid folio_rotate_reclaimable()
43 		 */
44 		folio_mark_dirty(folio);
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);
48 		folio_clear_reclaim(folio);
49 	}
50 	folio_end_writeback(folio);
51 }
52 
53 static void end_swap_bio_write(struct bio *bio)
54 {
55 	__end_swap_bio_write(bio);
56 	bio_put(bio);
57 }
58 
59 static void __end_swap_bio_read(struct bio *bio)
60 {
61 	struct folio *folio = bio_first_folio_all(bio);
62 
63 	if (bio->bi_status) {
64 		pr_alert_ratelimited("Read-error on swap-device (%u:%u:%llu)\n",
65 				     MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
66 				     (unsigned long long)bio->bi_iter.bi_sector);
67 	} else {
68 		folio_mark_uptodate(folio);
69 	}
70 	folio_unlock(folio);
71 }
72 
73 static void end_swap_bio_read(struct bio *bio)
74 {
75 	__end_swap_bio_read(bio);
76 	bio_put(bio);
77 }
78 
79 int generic_swapfile_activate(struct swap_info_struct *sis,
80 				struct file *swap_file,
81 				sector_t *span)
82 {
83 	struct address_space *mapping = swap_file->f_mapping;
84 	struct inode *inode = mapping->host;
85 	unsigned blocks_per_page;
86 	unsigned long page_no;
87 	unsigned blkbits;
88 	sector_t probe_block;
89 	sector_t last_block;
90 	sector_t lowest_block = -1;
91 	sector_t highest_block = 0;
92 	int nr_extents = 0;
93 	int ret;
94 
95 	blkbits = inode->i_blkbits;
96 	blocks_per_page = PAGE_SIZE >> blkbits;
97 
98 	/*
99 	 * Map all the blocks into the extent tree.  This code doesn't try
100 	 * to be very smart.
101 	 */
102 	probe_block = 0;
103 	page_no = 0;
104 	last_block = i_size_read(inode) >> blkbits;
105 	while ((probe_block + blocks_per_page) <= last_block &&
106 			page_no < sis->max) {
107 		unsigned block_in_page;
108 		sector_t first_block;
109 
110 		cond_resched();
111 
112 		first_block = probe_block;
113 		ret = bmap(inode, &first_block);
114 		if (ret || !first_block)
115 			goto bad_bmap;
116 
117 		/*
118 		 * It must be PAGE_SIZE aligned on-disk
119 		 */
120 		if (first_block & (blocks_per_page - 1)) {
121 			probe_block++;
122 			goto reprobe;
123 		}
124 
125 		for (block_in_page = 1; block_in_page < blocks_per_page;
126 					block_in_page++) {
127 			sector_t block;
128 
129 			block = probe_block + block_in_page;
130 			ret = bmap(inode, &block);
131 			if (ret || !block)
132 				goto bad_bmap;
133 
134 			if (block != first_block + block_in_page) {
135 				/* Discontiguity */
136 				probe_block++;
137 				goto reprobe;
138 			}
139 		}
140 
141 		first_block >>= (PAGE_SHIFT - blkbits);
142 		if (page_no) {	/* exclude the header page */
143 			if (first_block < lowest_block)
144 				lowest_block = first_block;
145 			if (first_block > highest_block)
146 				highest_block = first_block;
147 		}
148 
149 		/*
150 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
151 		 */
152 		ret = add_swap_extent(sis, page_no, 1, first_block);
153 		if (ret < 0)
154 			goto out;
155 		nr_extents += ret;
156 		page_no++;
157 		probe_block += blocks_per_page;
158 reprobe:
159 		continue;
160 	}
161 	ret = nr_extents;
162 	*span = 1 + highest_block - lowest_block;
163 	if (page_no == 0)
164 		page_no = 1;	/* force Empty message */
165 	sis->max = page_no;
166 	sis->pages = page_no - 1;
167 out:
168 	return ret;
169 bad_bmap:
170 	pr_err("swapon: swapfile has holes\n");
171 	ret = -EINVAL;
172 	goto out;
173 }
174 
175 static bool is_folio_zero_filled(struct folio *folio)
176 {
177 	unsigned int pos, last_pos;
178 	unsigned long *data;
179 	unsigned int i;
180 
181 	last_pos = PAGE_SIZE / sizeof(*data) - 1;
182 	for (i = 0; i < folio_nr_pages(folio); i++) {
183 		data = kmap_local_folio(folio, i * PAGE_SIZE);
184 		/*
185 		 * Check last word first, incase the page is zero-filled at
186 		 * the start and has non-zero data at the end, which is common
187 		 * in real-world workloads.
188 		 */
189 		if (data[last_pos]) {
190 			kunmap_local(data);
191 			return false;
192 		}
193 		for (pos = 0; pos < last_pos; pos++) {
194 			if (data[pos]) {
195 				kunmap_local(data);
196 				return false;
197 			}
198 		}
199 		kunmap_local(data);
200 	}
201 
202 	return true;
203 }
204 
205 static void swap_zeromap_folio_set(struct folio *folio)
206 {
207 	struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio);
208 	int nr_pages = folio_nr_pages(folio);
209 	struct swap_cluster_info *ci;
210 	swp_entry_t entry;
211 	unsigned int i;
212 
213 	VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
214 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
215 
216 	ci = swap_cluster_get_and_lock(folio);
217 	for (i = 0; i < folio_nr_pages(folio); i++) {
218 		entry = page_swap_entry(folio_page(folio, i));
219 		__swap_table_set_zero(ci, swp_cluster_offset(entry));
220 	}
221 	swap_cluster_unlock(ci);
222 
223 	count_vm_events(SWPOUT_ZERO, nr_pages);
224 	if (objcg) {
225 		count_objcg_events(objcg, SWPOUT_ZERO, nr_pages);
226 		obj_cgroup_put(objcg);
227 	}
228 }
229 
230 static void swap_zeromap_folio_clear(struct folio *folio)
231 {
232 	struct swap_cluster_info *ci;
233 	swp_entry_t entry;
234 	unsigned int i;
235 
236 	VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
237 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
238 
239 	ci = swap_cluster_get_and_lock(folio);
240 	for (i = 0; i < folio_nr_pages(folio); i++) {
241 		entry = page_swap_entry(folio_page(folio, i));
242 		__swap_table_clear_zero(ci, swp_cluster_offset(entry));
243 	}
244 	swap_cluster_unlock(ci);
245 }
246 
247 /*
248  * We may have stale swap cache pages in memory: notice
249  * them here and get rid of the unnecessary final write.
250  */
251 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
252 {
253 	int ret = 0;
254 
255 	if (folio_free_swap(folio))
256 		goto out_unlock;
257 
258 	/*
259 	 * Arch code may have to preserve more data than just the page
260 	 * contents, e.g. memory tags.
261 	 */
262 	ret = arch_prepare_to_swap(folio);
263 	if (ret) {
264 		folio_mark_dirty(folio);
265 		goto out_unlock;
266 	}
267 
268 	/*
269 	 * Use the swap table zero mark to avoid doing IO for zero-filled
270 	 * pages. The zero mark is protected by the cluster lock, which is
271 	 * acquired internally by swap_zeromap_folio_set/clear.
272 	 */
273 	if (is_folio_zero_filled(folio)) {
274 		swap_zeromap_folio_set(folio);
275 		goto out_unlock;
276 	}
277 
278 	/*
279 	 * Clear bits this folio occupies in the zeromap to prevent zero data
280 	 * being read in from any previous zero writes that occupied the same
281 	 * swap entries.
282 	 */
283 	swap_zeromap_folio_clear(folio);
284 
285 	if (zswap_store(folio)) {
286 		count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
287 		goto out_unlock;
288 	}
289 
290 	rcu_read_lock();
291 	if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
292 		rcu_read_unlock();
293 		folio_mark_dirty(folio);
294 		return AOP_WRITEPAGE_ACTIVATE;
295 	}
296 	rcu_read_unlock();
297 
298 	__swap_writepage(folio, swap_plug);
299 	return 0;
300 out_unlock:
301 	folio_unlock(folio);
302 	return ret;
303 }
304 
305 static inline void count_swpout_vm_event(struct folio *folio)
306 {
307 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
308 	if (unlikely(folio_test_pmd_mappable(folio))) {
309 		count_memcg_folio_events(folio, THP_SWPOUT, 1);
310 		count_vm_event(THP_SWPOUT);
311 	}
312 #endif
313 	count_mthp_stat(folio_order(folio), MTHP_STAT_SWPOUT);
314 	count_memcg_folio_events(folio, PSWPOUT, folio_nr_pages(folio));
315 	count_vm_events(PSWPOUT, folio_nr_pages(folio));
316 }
317 
318 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
319 static void bio_associate_blkg_from_page(struct bio *bio, struct folio *folio)
320 {
321 	struct cgroup_subsys_state *css;
322 	struct mem_cgroup *memcg;
323 
324 	if (!folio_memcg_charged(folio))
325 		return;
326 
327 	rcu_read_lock();
328 	memcg = folio_memcg(folio);
329 	css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
330 	if (!css || !css_tryget(css))
331 		css = NULL;
332 	rcu_read_unlock();
333 
334 	bio_associate_blkg_from_css(bio, css);
335 	if (css)
336 		css_put(css);
337 }
338 #else
339 #define bio_associate_blkg_from_page(bio, folio)		do { } while (0)
340 #endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
341 
342 struct swap_iocb {
343 	struct kiocb		iocb;
344 	struct bio_vec		bvecs[SWAP_CLUSTER_MAX];
345 	int			nr_bvecs;
346 	int			len;
347 };
348 static mempool_t *sio_pool;
349 
350 int sio_pool_init(void)
351 {
352 	if (!sio_pool) {
353 		mempool_t *pool = mempool_create_kmalloc_pool(
354 			SWAP_CLUSTER_MAX, sizeof(struct swap_iocb));
355 		if (cmpxchg(&sio_pool, NULL, pool))
356 			mempool_destroy(pool);
357 	}
358 	if (!sio_pool)
359 		return -ENOMEM;
360 	return 0;
361 }
362 
363 static void sio_write_complete(struct kiocb *iocb, long ret)
364 {
365 	struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
366 	struct page *page = sio->bvecs[0].bv_page;
367 	int p;
368 
369 	if (ret != sio->len) {
370 		/*
371 		 * In the case of swap-over-nfs, this can be a
372 		 * temporary failure if the system has limited
373 		 * memory for allocating transmit buffers.
374 		 * Mark the page dirty and avoid
375 		 * folio_rotate_reclaimable but rate-limit the
376 		 * messages.
377 		 */
378 		pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
379 				   ret, swap_dev_pos(page_swap_entry(page)));
380 		for (p = 0; p < sio->nr_bvecs; p++) {
381 			page = sio->bvecs[p].bv_page;
382 			set_page_dirty(page);
383 			ClearPageReclaim(page);
384 		}
385 	}
386 
387 	for (p = 0; p < sio->nr_bvecs; p++)
388 		end_page_writeback(sio->bvecs[p].bv_page);
389 
390 	mempool_free(sio, sio_pool);
391 }
392 
393 static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
394 {
395 	struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
396 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
397 	struct file *swap_file = sis->swap_file;
398 	loff_t pos = swap_dev_pos(folio->swap);
399 
400 	count_swpout_vm_event(folio);
401 	folio_start_writeback(folio);
402 	folio_unlock(folio);
403 	if (sio) {
404 		if (sio->iocb.ki_filp != swap_file ||
405 		    sio->iocb.ki_pos + sio->len != pos) {
406 			swap_write_unplug(sio);
407 			sio = NULL;
408 		}
409 	}
410 	if (!sio) {
411 		sio = mempool_alloc(sio_pool, GFP_NOIO);
412 		init_sync_kiocb(&sio->iocb, swap_file);
413 		sio->iocb.ki_complete = sio_write_complete;
414 		sio->iocb.ki_pos = pos;
415 		sio->nr_bvecs = 0;
416 		sio->len = 0;
417 	}
418 	bvec_set_folio(&sio->bvecs[sio->nr_bvecs], folio, folio_size(folio), 0);
419 	sio->len += folio_size(folio);
420 	sio->nr_bvecs += 1;
421 	if (sio->nr_bvecs == ARRAY_SIZE(sio->bvecs) || !swap_plug) {
422 		swap_write_unplug(sio);
423 		sio = NULL;
424 	}
425 	if (swap_plug)
426 		*swap_plug = sio;
427 }
428 
429 static void swap_writepage_bdev_sync(struct folio *folio,
430 		struct swap_info_struct *sis)
431 {
432 	struct bio_vec bv;
433 	struct bio bio;
434 
435 	bio_init(&bio, sis->bdev, &bv, 1, REQ_OP_WRITE | REQ_SWAP);
436 	bio.bi_iter.bi_sector = swap_folio_sector(folio);
437 	bio_add_folio_nofail(&bio, folio, folio_size(folio), 0);
438 
439 	bio_associate_blkg_from_page(&bio, folio);
440 	count_swpout_vm_event(folio);
441 
442 	folio_start_writeback(folio);
443 	folio_unlock(folio);
444 
445 	submit_bio_wait(&bio);
446 	__end_swap_bio_write(&bio);
447 }
448 
449 static void swap_writepage_bdev_async(struct folio *folio,
450 		struct swap_info_struct *sis)
451 {
452 	struct bio *bio;
453 
454 	bio = bio_alloc(sis->bdev, 1, REQ_OP_WRITE | REQ_SWAP, GFP_NOIO);
455 	bio->bi_iter.bi_sector = swap_folio_sector(folio);
456 	bio->bi_end_io = end_swap_bio_write;
457 	bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
458 
459 	bio_associate_blkg_from_page(bio, folio);
460 	count_swpout_vm_event(folio);
461 	folio_start_writeback(folio);
462 	folio_unlock(folio);
463 	submit_bio(bio);
464 }
465 
466 void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
467 {
468 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
469 
470 	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
471 	/*
472 	 * ->flags can be updated non-atomically,
473 	 * but that will never affect SWP_FS_OPS, so the data_race
474 	 * is safe.
475 	 */
476 	if (data_race(sis->flags & SWP_FS_OPS))
477 		swap_writepage_fs(folio, swap_plug);
478 	/*
479 	 * ->flags can be updated non-atomically,
480 	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
481 	 * is safe.
482 	 */
483 	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
484 		swap_writepage_bdev_sync(folio, sis);
485 	else
486 		swap_writepage_bdev_async(folio, sis);
487 }
488 
489 void swap_write_unplug(struct swap_iocb *sio)
490 {
491 	struct iov_iter from;
492 	struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
493 	int ret;
494 
495 	iov_iter_bvec(&from, ITER_SOURCE, sio->bvecs, sio->nr_bvecs, sio->len);
496 	ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
497 	if (ret != -EIOCBQUEUED)
498 		sio_write_complete(&sio->iocb, ret);
499 }
500 
501 static void sio_read_complete(struct kiocb *iocb, long ret)
502 {
503 	struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
504 	int p;
505 
506 	if (ret == sio->len) {
507 		for (p = 0; p < sio->nr_bvecs; p++) {
508 			struct folio *folio = bvec_folio(&sio->bvecs[p]);
509 
510 			count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
511 			count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
512 			folio_mark_uptodate(folio);
513 			folio_unlock(folio);
514 		}
515 		count_vm_events(PSWPIN, sio->len >> PAGE_SHIFT);
516 	} else {
517 		for (p = 0; p < sio->nr_bvecs; p++) {
518 			struct folio *folio = bvec_folio(&sio->bvecs[p]);
519 
520 			folio_unlock(folio);
521 		}
522 		pr_alert_ratelimited("Read-error on swap-device\n");
523 	}
524 	mempool_free(sio, sio_pool);
525 }
526 
527 /*
528  * Return the count of contiguous swap entries that share the same
529  * zeromap status as the starting entry. If is_zerop is not NULL,
530  * it will return the zeromap status of the starting entry.
531  *
532  * Context: Caller must ensure the cluster containing the entries
533  * that will be checked won't be freed.
534  */
535 static int swap_zeromap_batch(swp_entry_t entry, int max_nr,
536 			      bool *is_zerop)
537 {
538 	int i;
539 	bool is_zero;
540 	unsigned int ci_start = swp_cluster_offset(entry);
541 	struct swap_cluster_info *ci = __swap_entry_to_cluster(entry);
542 
543 	VM_WARN_ON_ONCE(ci_start + max_nr > SWAPFILE_CLUSTER);
544 
545 	rcu_read_lock();
546 	is_zero = __swap_table_test_zero(ci, ci_start);
547 	for (i = 1; i < max_nr; i++)
548 		if (is_zero != __swap_table_test_zero(ci, ci_start + i))
549 			break;
550 	rcu_read_unlock();
551 	if (is_zerop)
552 		*is_zerop = is_zero;
553 
554 	return i;
555 }
556 
557 static bool swap_read_folio_zeromap(struct folio *folio)
558 {
559 	int nr_pages = folio_nr_pages(folio);
560 	struct obj_cgroup *objcg;
561 	bool is_zeromap;
562 
563 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
564 
565 	/*
566 	 * Swapping in a large folio that is partially in the zeromap is not
567 	 * currently handled. Return true without marking the folio uptodate so
568 	 * that an IO error is emitted (e.g. do_swap_page() will sigbus).
569 	 * Folio lock stabilizes the cluster and map, so the check is safe.
570 	 */
571 	if (WARN_ON_ONCE(swap_zeromap_batch(folio->swap, nr_pages,
572 			 &is_zeromap) != nr_pages))
573 		return true;
574 
575 	if (!is_zeromap)
576 		return false;
577 
578 	objcg = get_obj_cgroup_from_folio(folio);
579 	count_vm_events(SWPIN_ZERO, nr_pages);
580 	if (objcg) {
581 		count_objcg_events(objcg, SWPIN_ZERO, nr_pages);
582 		obj_cgroup_put(objcg);
583 	}
584 
585 	folio_zero_range(folio, 0, folio_size(folio));
586 	folio_mark_uptodate(folio);
587 	return true;
588 }
589 
590 static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
591 {
592 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
593 	struct swap_iocb *sio = NULL;
594 	loff_t pos = swap_dev_pos(folio->swap);
595 
596 	if (plug)
597 		sio = *plug;
598 	if (sio) {
599 		if (sio->iocb.ki_filp != sis->swap_file ||
600 		    sio->iocb.ki_pos + sio->len != pos) {
601 			swap_read_unplug(sio);
602 			sio = NULL;
603 		}
604 	}
605 	if (!sio) {
606 		sio = mempool_alloc(sio_pool, GFP_KERNEL);
607 		init_sync_kiocb(&sio->iocb, sis->swap_file);
608 		sio->iocb.ki_pos = pos;
609 		sio->iocb.ki_complete = sio_read_complete;
610 		sio->nr_bvecs = 0;
611 		sio->len = 0;
612 	}
613 	bvec_set_folio(&sio->bvecs[sio->nr_bvecs], folio, folio_size(folio), 0);
614 	sio->len += folio_size(folio);
615 	sio->nr_bvecs += 1;
616 	if (sio->nr_bvecs == ARRAY_SIZE(sio->bvecs) || !plug) {
617 		swap_read_unplug(sio);
618 		sio = NULL;
619 	}
620 	if (plug)
621 		*plug = sio;
622 }
623 
624 static void swap_read_folio_bdev_sync(struct folio *folio,
625 		struct swap_info_struct *sis)
626 {
627 	struct bio_vec bv;
628 	struct bio bio;
629 
630 	bio_init(&bio, sis->bdev, &bv, 1, REQ_OP_READ);
631 	bio.bi_iter.bi_sector = swap_folio_sector(folio);
632 	bio_add_folio_nofail(&bio, folio, folio_size(folio), 0);
633 	/*
634 	 * Keep this task valid during swap readpage because the oom killer may
635 	 * attempt to access it in the page fault retry time check.
636 	 */
637 	get_task_struct(current);
638 	count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
639 	count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
640 	count_vm_events(PSWPIN, folio_nr_pages(folio));
641 	submit_bio_wait(&bio);
642 	__end_swap_bio_read(&bio);
643 	put_task_struct(current);
644 }
645 
646 static void swap_read_folio_bdev_async(struct folio *folio,
647 		struct swap_info_struct *sis)
648 {
649 	struct bio *bio;
650 
651 	bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
652 	bio->bi_iter.bi_sector = swap_folio_sector(folio);
653 	bio->bi_end_io = end_swap_bio_read;
654 	bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
655 	count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
656 	count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
657 	count_vm_events(PSWPIN, folio_nr_pages(folio));
658 	submit_bio(bio);
659 }
660 
661 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
662 {
663 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
664 	bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
665 	bool workingset = folio_test_workingset(folio);
666 	unsigned long pflags;
667 	bool in_thrashing;
668 
669 	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
670 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
671 	VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
672 
673 	/*
674 	 * Count submission time as memory stall and delay. When the device
675 	 * is congested, or the submitting cgroup IO-throttled, submission
676 	 * can be a significant part of overall IO time.
677 	 */
678 	if (workingset) {
679 		delayacct_thrashing_start(&in_thrashing);
680 		psi_memstall_enter(&pflags);
681 	}
682 	delayacct_swapin_start();
683 
684 	if (swap_read_folio_zeromap(folio)) {
685 		folio_unlock(folio);
686 		goto finish;
687 	}
688 
689 	if (zswap_load(folio) != -ENOENT)
690 		goto finish;
691 
692 	/* We have to read from slower devices. Increase zswap protection. */
693 	zswap_folio_swapin(folio);
694 
695 	if (data_race(sis->flags & SWP_FS_OPS)) {
696 		swap_read_folio_fs(folio, plug);
697 	} else if (synchronous) {
698 		swap_read_folio_bdev_sync(folio, sis);
699 	} else {
700 		swap_read_folio_bdev_async(folio, sis);
701 	}
702 
703 finish:
704 	if (workingset) {
705 		delayacct_thrashing_end(&in_thrashing);
706 		psi_memstall_leave(&pflags);
707 	}
708 	delayacct_swapin_end();
709 }
710 
711 void __swap_read_unplug(struct swap_iocb *sio)
712 {
713 	struct iov_iter from;
714 	struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
715 	int ret;
716 
717 	iov_iter_bvec(&from, ITER_DEST, sio->bvecs, sio->nr_bvecs, sio->len);
718 	ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
719 	if (ret != -EIOCBQUEUED)
720 		sio_read_complete(&sio->iocb, ret);
721 }
722