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