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