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