]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/page_io.c
selftests: clarify common error when running gup_test
[thirdparty/linux.git] / mm / page_io.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
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>
5a0e3ad6 16#include <linux/gfp.h>
1da177e4
LT
17#include <linux/pagemap.h>
18#include <linux/swap.h>
19#include <linux/bio.h>
20#include <linux/swapops.h>
62c230bc 21#include <linux/buffer_head.h>
1da177e4 22#include <linux/writeback.h>
38b5faf4 23#include <linux/frontswap.h>
b430e9d1 24#include <linux/blkdev.h>
93779069 25#include <linux/psi.h>
e2e40f2c 26#include <linux/uio.h>
b0ba2d0f 27#include <linux/sched/task.h>
a3d5dc90 28#include <linux/delayacct.h>
1da177e4 29
4246a0b6 30void end_swap_bio_write(struct bio *bio)
1da177e4 31{
263663cd 32 struct page *page = bio_first_page_all(bio);
1da177e4 33
4e4cbee9 34 if (bio->bi_status) {
1da177e4 35 SetPageError(page);
6ddab3b9
PZ
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 *
575ced1c 42 * Also clear PG_reclaim to avoid folio_rotate_reclaimable()
6ddab3b9
PZ
43 */
44 set_page_dirty(page);
25eaab43
GD
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);
6ddab3b9
PZ
48 ClearPageReclaim(page);
49 }
1da177e4
LT
50 end_page_writeback(page);
51 bio_put(bio);
1da177e4
LT
52}
53
4246a0b6 54static void end_swap_bio_read(struct bio *bio)
1da177e4 55{
263663cd 56 struct page *page = bio_first_page_all(bio);
23955622 57 struct task_struct *waiter = bio->bi_private;
1da177e4 58
4e4cbee9 59 if (bio->bi_status) {
1da177e4
LT
60 SetPageError(page);
61 ClearPageUptodate(page);
25eaab43
GD
62 pr_alert_ratelimited("Read-error on swap-device (%u:%u:%llu)\n",
63 MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
64 (unsigned long long)bio->bi_iter.bi_sector);
b430e9d1 65 goto out;
1da177e4 66 }
b430e9d1
MK
67
68 SetPageUptodate(page);
b430e9d1 69out:
1da177e4 70 unlock_page(page);
23955622 71 WRITE_ONCE(bio->bi_private, NULL);
1da177e4 72 bio_put(bio);
87518530
ON
73 if (waiter) {
74 blk_wake_io_task(waiter);
75 put_task_struct(waiter);
76 }
1da177e4
LT
77}
78
a509bc1a
MG
79int 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 /*
4efaceb1 99 * Map all the blocks into the extent tree. This code doesn't try
a509bc1a
MG
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
7e4411bf
MP
110 cond_resched();
111
30460e1e
CM
112 first_block = probe_block;
113 ret = bmap(inode, &first_block);
114 if (ret || !first_block)
a509bc1a
MG
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
30460e1e
CM
129 block = probe_block + block_in_page;
130 ret = bmap(inode, &block);
131 if (ret || !block)
a509bc1a 132 goto bad_bmap;
30460e1e 133
a509bc1a
MG
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;
158reprobe:
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 sis->highest_bit = page_no - 1;
168out:
169 return ret;
170bad_bmap:
1170532b 171 pr_err("swapon: swapfile has holes\n");
a509bc1a
MG
172 ret = -EINVAL;
173 goto out;
174}
175
1da177e4
LT
176/*
177 * We may have stale swap cache pages in memory: notice
178 * them here and get rid of the unnecessary final write.
179 */
180int swap_writepage(struct page *page, struct writeback_control *wbc)
181{
2f772e6c 182 int ret = 0;
1da177e4 183
a2c43eed 184 if (try_to_free_swap(page)) {
1da177e4
LT
185 unlock_page(page);
186 goto out;
187 }
8a84802e
SP
188 /*
189 * Arch code may have to preserve more data than just the page
190 * contents, e.g. memory tags.
191 */
192 ret = arch_prepare_to_swap(page);
193 if (ret) {
194 set_page_dirty(page);
195 unlock_page(page);
196 goto out;
197 }
165c8aed 198 if (frontswap_store(page) == 0) {
38b5faf4
DM
199 set_page_writeback(page);
200 unlock_page(page);
201 end_page_writeback(page);
202 goto out;
203 }
1eec6702 204 ret = __swap_writepage(page, wbc, end_swap_bio_write);
2f772e6c
SJ
205out:
206 return ret;
207}
208
225311a4
HY
209static inline void count_swpout_vm_event(struct page *page)
210{
211#ifdef CONFIG_TRANSPARENT_HUGEPAGE
212 if (unlikely(PageTransHuge(page)))
213 count_vm_event(THP_SWPOUT);
214#endif
6c357848 215 count_vm_events(PSWPOUT, thp_nr_pages(page));
225311a4
HY
216}
217
a18b9b15
CH
218#if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
219static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
220{
221 struct cgroup_subsys_state *css;
bcfe06bf 222 struct mem_cgroup *memcg;
a18b9b15 223
bcfe06bf
RG
224 memcg = page_memcg(page);
225 if (!memcg)
a18b9b15
CH
226 return;
227
228 rcu_read_lock();
bcfe06bf 229 css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
a18b9b15
CH
230 bio_associate_blkg_from_css(bio, css);
231 rcu_read_unlock();
232}
233#else
234#define bio_associate_blkg_from_page(bio, page) do { } while (0)
235#endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
236
1eec6702 237int __swap_writepage(struct page *page, struct writeback_control *wbc,
4246a0b6 238 bio_end_io_t end_write_func)
2f772e6c
SJ
239{
240 struct bio *bio;
4e49ea4a 241 int ret;
2f772e6c 242 struct swap_info_struct *sis = page_swap_info(page);
62c230bc 243
cc30c5d6 244 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
32646315 245 if (data_race(sis->flags & SWP_FS_OPS)) {
62c230bc
MG
246 struct kiocb kiocb;
247 struct file *swap_file = sis->swap_file;
248 struct address_space *mapping = swap_file->f_mapping;
62a8067a
AV
249 struct bio_vec bv = {
250 .bv_page = page,
251 .bv_len = PAGE_SIZE,
252 .bv_offset = 0
253 };
05afcb77 254 struct iov_iter from;
62c230bc 255
aa563d7b 256 iov_iter_bvec(&from, WRITE, &bv, 1, PAGE_SIZE);
62c230bc
MG
257 init_sync_kiocb(&kiocb, swap_file);
258 kiocb.ki_pos = page_file_offset(page);
62c230bc 259
0cdc444a 260 set_page_writeback(page);
62c230bc 261 unlock_page(page);
c8b8e32d 262 ret = mapping->a_ops->direct_IO(&kiocb, &from);
62c230bc
MG
263 if (ret == PAGE_SIZE) {
264 count_vm_event(PSWPOUT);
265 ret = 0;
2d30d31e 266 } else {
0cdc444a
MG
267 /*
268 * In the case of swap-over-nfs, this can be a
269 * temporary failure if the system has limited
270 * memory for allocating transmit buffers.
271 * Mark the page dirty and avoid
575ced1c 272 * folio_rotate_reclaimable but rate-limit the
0cdc444a
MG
273 * messages but do not flag PageError like
274 * the normal direct-to-bio case as it could
275 * be temporary.
276 */
2d30d31e 277 set_page_dirty(page);
0cdc444a 278 ClearPageReclaim(page);
1170532b
JP
279 pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
280 page_file_offset(page));
62c230bc 281 }
0cdc444a 282 end_page_writeback(page);
62c230bc
MG
283 return ret;
284 }
285
dd6bd0d9
MW
286 ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
287 if (!ret) {
225311a4 288 count_swpout_vm_event(page);
dd6bd0d9
MW
289 return 0;
290 }
291
07888c66
CH
292 bio = bio_alloc(sis->bdev, 1,
293 REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc),
294 GFP_NOIO);
48d15436 295 bio->bi_iter.bi_sector = swap_page_sector(page);
48d15436
CH
296 bio->bi_end_io = end_write_func;
297 bio_add_page(bio, page, thp_size(page), 0);
298
6a7f6d86 299 bio_associate_blkg_from_page(bio, page);
225311a4 300 count_swpout_vm_event(page);
1da177e4
LT
301 set_page_writeback(page);
302 unlock_page(page);
4e49ea4a 303 submit_bio(bio);
548d9782
ML
304
305 return 0;
1da177e4
LT
306}
307
0bcac06f 308int swap_readpage(struct page *page, bool synchronous)
1da177e4
LT
309{
310 struct bio *bio;
311 int ret = 0;
62c230bc 312 struct swap_info_struct *sis = page_swap_info(page);
d8c47cc7 313 bool workingset = PageWorkingset(page);
93779069 314 unsigned long pflags;
1da177e4 315
0bcac06f 316 VM_BUG_ON_PAGE(!PageSwapCache(page) && !synchronous, page);
309381fe
SL
317 VM_BUG_ON_PAGE(!PageLocked(page), page);
318 VM_BUG_ON_PAGE(PageUptodate(page), page);
93779069
MK
319
320 /*
321 * Count submission time as memory stall. When the device is congested,
322 * or the submitting cgroup IO-throttled, submission can be a
323 * significant part of overall IO time.
324 */
d8c47cc7
JW
325 if (workingset)
326 psi_memstall_enter(&pflags);
a3d5dc90 327 delayacct_swapin_start();
93779069 328
165c8aed 329 if (frontswap_load(page) == 0) {
38b5faf4
DM
330 SetPageUptodate(page);
331 unlock_page(page);
332 goto out;
333 }
62c230bc 334
32646315 335 if (data_race(sis->flags & SWP_FS_OPS)) {
62c230bc
MG
336 struct file *swap_file = sis->swap_file;
337 struct address_space *mapping = swap_file->f_mapping;
338
339 ret = mapping->a_ops->readpage(swap_file, page);
340 if (!ret)
341 count_vm_event(PSWPIN);
93779069 342 goto out;
62c230bc
MG
343 }
344
5115db10
CH
345 if (sis->flags & SWP_SYNCHRONOUS_IO) {
346 ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
347 if (!ret) {
5115db10
CH
348 count_vm_event(PSWPIN);
349 goto out;
350 }
dd6bd0d9
MW
351 }
352
353 ret = 0;
07888c66 354 bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
48d15436
CH
355 bio->bi_iter.bi_sector = swap_page_sector(page);
356 bio->bi_end_io = end_swap_bio_read;
357 bio_add_page(bio, page, thp_size(page), 0);
b0ba2d0f
TH
358 /*
359 * Keep this task valid during swap readpage because the oom killer may
360 * attempt to access it in the page fault retry time check.
361 */
87518530 362 if (synchronous) {
6ce913fe 363 bio->bi_opf |= REQ_POLLED;
87518530
ON
364 get_task_struct(current);
365 bio->bi_private = current;
366 }
f8891e5e 367 count_vm_event(PSWPIN);
23955622 368 bio_get(bio);
3e08773c 369 submit_bio(bio);
0bcac06f 370 while (synchronous) {
1ac5cd49 371 set_current_state(TASK_UNINTERRUPTIBLE);
23955622
SL
372 if (!READ_ONCE(bio->bi_private))
373 break;
374
5a72e899 375 if (!bio_poll(bio, NULL, 0))
0f190a7a 376 blk_io_schedule();
23955622
SL
377 }
378 __set_current_state(TASK_RUNNING);
379 bio_put(bio);
380
1da177e4 381out:
d8c47cc7
JW
382 if (workingset)
383 psi_memstall_leave(&pflags);
a3d5dc90 384 delayacct_swapin_end();
1da177e4
LT
385 return ret;
386}
62c230bc 387
7e63df00 388bool swap_dirty_folio(struct address_space *mapping, struct folio *folio)
62c230bc 389{
7e63df00 390 struct swap_info_struct *sis = swp_swap_info(folio_swap_entry(folio));
62c230bc 391
32646315 392 if (data_race(sis->flags & SWP_FS_OPS)) {
7e63df00
MWO
393 const struct address_space_operations *aops;
394
395 mapping = sis->swap_file->f_mapping;
396 aops = mapping->a_ops;
cc30c5d6 397
7e63df00 398 VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
3a3bae50 399 return aops->dirty_folio(mapping, folio);
62c230bc 400 } else {
46de8b97 401 return noop_dirty_folio(mapping, folio);
62c230bc
MG
402 }
403}