xref: /openbmc/linux/mm/page_io.c (revision 9b4e30bd)
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/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 
30 static void end_swap_bio_write(struct bio *bio)
31 {
32 	struct page *page = bio_first_page_all(bio);
33 
34 	if (bio->bi_status) {
35 		SetPageError(page);
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 		set_page_dirty(page);
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 		ClearPageReclaim(page);
49 	}
50 	end_page_writeback(page);
51 	bio_put(bio);
52 }
53 
54 static void __end_swap_bio_read(struct bio *bio)
55 {
56 	struct page *page = bio_first_page_all(bio);
57 
58 	if (bio->bi_status) {
59 		SetPageError(page);
60 		ClearPageUptodate(page);
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);
64 	} else {
65 		SetPageUptodate(page);
66 	}
67 	unlock_page(page);
68 }
69 
70 static void end_swap_bio_read(struct bio *bio)
71 {
72 	__end_swap_bio_read(bio);
73 	bio_put(bio);
74 }
75 
76 int 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 	/*
96 	 * Map all the blocks into the extent tree.  This code doesn't try
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 
107 		cond_resched();
108 
109 		first_block = probe_block;
110 		ret = bmap(inode, &first_block);
111 		if (ret || !first_block)
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 
126 			block = probe_block + block_in_page;
127 			ret = bmap(inode, &block);
128 			if (ret || !block)
129 				goto bad_bmap;
130 
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;
155 reprobe:
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;
165 out:
166 	return ret;
167 bad_bmap:
168 	pr_err("swapon: swapfile has holes\n");
169 	ret = -EINVAL;
170 	goto out;
171 }
172 
173 /*
174  * We may have stale swap cache pages in memory: notice
175  * them here and get rid of the unnecessary final write.
176  */
177 int swap_writepage(struct page *page, struct writeback_control *wbc)
178 {
179 	struct folio *folio = page_folio(page);
180 	int ret = 0;
181 
182 	if (folio_free_swap(folio)) {
183 		folio_unlock(folio);
184 		goto out;
185 	}
186 	/*
187 	 * Arch code may have to preserve more data than just the page
188 	 * contents, e.g. memory tags.
189 	 */
190 	ret = arch_prepare_to_swap(&folio->page);
191 	if (ret) {
192 		folio_mark_dirty(folio);
193 		folio_unlock(folio);
194 		goto out;
195 	}
196 	if (frontswap_store(&folio->page) == 0) {
197 		folio_start_writeback(folio);
198 		folio_unlock(folio);
199 		folio_end_writeback(folio);
200 		goto out;
201 	}
202 	ret = __swap_writepage(&folio->page, wbc);
203 out:
204 	return ret;
205 }
206 
207 static 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
213 	count_vm_events(PSWPOUT, thp_nr_pages(page));
214 }
215 
216 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
217 static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
218 {
219 	struct cgroup_subsys_state *css;
220 	struct mem_cgroup *memcg;
221 
222 	memcg = page_memcg(page);
223 	if (!memcg)
224 		return;
225 
226 	rcu_read_lock();
227 	css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
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 
235 struct swap_iocb {
236 	struct kiocb		iocb;
237 	struct bio_vec		bvec[SWAP_CLUSTER_MAX];
238 	int			pages;
239 	int			len;
240 };
241 static mempool_t *sio_pool;
242 
243 int sio_pool_init(void)
244 {
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 }
255 
256 static void sio_write_complete(struct kiocb *iocb, long ret)
257 {
258 	struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
259 	struct page *page = sio->bvec[0].bv_page;
260 	int p;
261 
262 	if (ret != sio->len) {
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 		 */
273 		pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
274 				   ret, page_file_offset(page));
275 		for (p = 0; p < sio->pages; p++) {
276 			page = sio->bvec[p].bv_page;
277 			set_page_dirty(page);
278 			ClearPageReclaim(page);
279 		}
280 	} else {
281 		for (p = 0; p < sio->pages; p++)
282 			count_swpout_vm_event(sio->bvec[p].bv_page);
283 	}
284 
285 	for (p = 0; p < sio->pages; p++)
286 		end_page_writeback(sio->bvec[p].bv_page);
287 
288 	mempool_free(sio, sio_pool);
289 }
290 
291 static int swap_writepage_fs(struct page *page, struct writeback_control *wbc)
292 {
293 	struct swap_iocb *sio = NULL;
294 	struct swap_info_struct *sis = page_swap_info(page);
295 	struct file *swap_file = sis->swap_file;
296 	loff_t pos = page_file_offset(page);
297 
298 	set_page_writeback(page);
299 	unlock_page(page);
300 	if (wbc->swap_plug)
301 		sio = *wbc->swap_plug;
302 	if (sio) {
303 		if (sio->iocb.ki_filp != swap_file ||
304 		    sio->iocb.ki_pos + sio->len != pos) {
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;
315 		sio->len = 0;
316 	}
317 	sio->bvec[sio->pages].bv_page = page;
318 	sio->bvec[sio->pages].bv_len = thp_size(page);
319 	sio->bvec[sio->pages].bv_offset = 0;
320 	sio->len += thp_size(page);
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;
330 }
331 
332 int __swap_writepage(struct page *page, struct writeback_control *wbc)
333 {
334 	struct bio *bio;
335 	int ret;
336 	struct swap_info_struct *sis = page_swap_info(page);
337 
338 	VM_BUG_ON_PAGE(!PageSwapCache(page), page);
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);
346 
347 	ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
348 	if (!ret) {
349 		count_swpout_vm_event(page);
350 		return 0;
351 	}
352 
353 	bio = bio_alloc(sis->bdev, 1,
354 			REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc),
355 			GFP_NOIO);
356 	bio->bi_iter.bi_sector = swap_page_sector(page);
357 	bio->bi_end_io = end_swap_bio_write;
358 	bio_add_page(bio, page, thp_size(page), 0);
359 
360 	bio_associate_blkg_from_page(bio, page);
361 	count_swpout_vm_event(page);
362 	set_page_writeback(page);
363 	unlock_page(page);
364 	submit_bio(bio);
365 
366 	return 0;
367 }
368 
369 void 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 
375 	iov_iter_bvec(&from, ITER_SOURCE, sio->bvec, sio->pages, sio->len);
376 	ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
377 	if (ret != -EIOCBQUEUED)
378 		sio_write_complete(&sio->iocb, ret);
379 }
380 
381 static void sio_read_complete(struct kiocb *iocb, long ret)
382 {
383 	struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
384 	int p;
385 
386 	if (ret == sio->len) {
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);
394 	} else {
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");
403 	}
404 	mempool_free(sio, sio_pool);
405 }
406 
407 static void swap_readpage_fs(struct page *page,
408 			     struct swap_iocb **plug)
409 {
410 	struct swap_info_struct *sis = page_swap_info(page);
411 	struct swap_iocb *sio = NULL;
412 	loff_t pos = page_file_offset(page);
413 
414 	if (plug)
415 		sio = *plug;
416 	if (sio) {
417 		if (sio->iocb.ki_filp != sis->swap_file ||
418 		    sio->iocb.ki_pos + sio->len != pos) {
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;
429 		sio->len = 0;
430 	}
431 	sio->bvec[sio->pages].bv_page = page;
432 	sio->bvec[sio->pages].bv_len = thp_size(page);
433 	sio->bvec[sio->pages].bv_offset = 0;
434 	sio->len += thp_size(page);
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;
442 }
443 
444 static void swap_readpage_bdev_sync(struct page *page,
445 		struct swap_info_struct *sis)
446 {
447 	struct bio_vec bv;
448 	struct bio bio;
449 
450 	if ((sis->flags & SWP_SYNCHRONOUS_IO) &&
451 	    !bdev_read_page(sis->bdev, swap_page_sector(page), page)) {
452 		count_vm_event(PSWPIN);
453 		return;
454 	}
455 
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);
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 	 */
463 	get_task_struct(current);
464 	count_vm_event(PSWPIN);
465 	submit_bio_wait(&bio);
466 	__end_swap_bio_read(&bio);
467 	put_task_struct(current);
468 }
469 
470 static void swap_readpage_bdev_async(struct page *page,
471 		struct swap_info_struct *sis)
472 {
473 	struct bio *bio;
474 
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;
479 	}
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);
487 }
488 
489 void 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);
516 	} else if (synchronous) {
517 		swap_readpage_bdev_sync(page, sis);
518 	} else {
519 		swap_readpage_bdev_async(page, sis);
520 	}
521 
522 	if (workingset) {
523 		delayacct_thrashing_end(&in_thrashing);
524 		psi_memstall_leave(&pflags);
525 	}
526 	delayacct_swapin_end();
527 }
528 
529 void __swap_read_unplug(struct swap_iocb *sio)
530 {
531 	struct iov_iter from;
532 	struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
533 	int ret;
534 
535 	iov_iter_bvec(&from, ITER_DEST, sio->bvec, sio->pages, sio->len);
536 	ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
537 	if (ret != -EIOCBQUEUED)
538 		sio_read_complete(&sio->iocb, ret);
539 }
540