1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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
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>
29 static struct bio *get_swap_bio(gfp_t gfp_flags,
30 struct page *page, bio_end_io_t end_io)
34 bio = bio_alloc(gfp_flags, 1);
36 struct block_device *bdev;
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;
43 bio_add_page(bio, page, thp_size(page), 0);
48 void end_swap_bio_write(struct bio *bio)
50 struct page *page = bio_first_page_all(bio);
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)
60 * Also clear PG_reclaim to avoid rotate_reclaimable_page()
63 pr_alert("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);
68 end_page_writeback(page);
72 static void end_swap_bio_read(struct bio *bio)
74 struct page *page = bio_first_page_all(bio);
75 struct task_struct *waiter = bio->bi_private;
79 ClearPageUptodate(page);
80 pr_alert("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);
86 SetPageUptodate(page);
89 WRITE_ONCE(bio->bi_private, NULL);
92 blk_wake_io_task(waiter);
93 put_task_struct(waiter);
97 int generic_swapfile_activate(struct swap_info_struct *sis,
98 struct file *swap_file,
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;
106 sector_t probe_block;
108 sector_t lowest_block = -1;
109 sector_t highest_block = 0;
113 blkbits = inode->i_blkbits;
114 blocks_per_page = PAGE_SIZE >> blkbits;
117 * Map all the blocks into the extent tree. This code doesn't try
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;
130 first_block = probe_block;
131 ret = bmap(inode, &first_block);
132 if (ret || !first_block)
136 * It must be PAGE_SIZE aligned on-disk
138 if (first_block & (blocks_per_page - 1)) {
143 for (block_in_page = 1; block_in_page < blocks_per_page;
147 block = probe_block + block_in_page;
148 ret = bmap(inode, &block);
152 if (block != first_block + block_in_page) {
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;
168 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
170 ret = add_swap_extent(sis, page_no, 1, first_block);
175 probe_block += blocks_per_page;
180 *span = 1 + highest_block - lowest_block;
182 page_no = 1; /* force Empty message */
184 sis->pages = page_no - 1;
185 sis->highest_bit = page_no - 1;
189 pr_err("swapon: swapfile has holes\n");
195 * We may have stale swap cache pages in memory: notice
196 * them here and get rid of the unnecessary final write.
198 int swap_writepage(struct page *page, struct writeback_control *wbc)
202 if (try_to_free_swap(page)) {
207 * Arch code may have to preserve more data than just the page
208 * contents, e.g. memory tags.
210 ret = arch_prepare_to_swap(page);
212 set_page_dirty(page);
216 if (frontswap_store(page) == 0) {
217 set_page_writeback(page);
219 end_page_writeback(page);
222 ret = __swap_writepage(page, wbc, end_swap_bio_write);
227 static inline void count_swpout_vm_event(struct page *page)
229 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
230 if (unlikely(PageTransHuge(page)))
231 count_vm_event(THP_SWPOUT);
233 count_vm_events(PSWPOUT, thp_nr_pages(page));
236 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
237 static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
239 struct cgroup_subsys_state *css;
241 if (!page->mem_cgroup)
245 css = cgroup_e_css(page->mem_cgroup->css.cgroup, &io_cgrp_subsys);
246 bio_associate_blkg_from_css(bio, css);
250 #define bio_associate_blkg_from_page(bio, page) do { } while (0)
251 #endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
253 int __swap_writepage(struct page *page, struct writeback_control *wbc,
254 bio_end_io_t end_write_func)
258 struct swap_info_struct *sis = page_swap_info(page);
260 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
261 if (data_race(sis->flags & SWP_FS_OPS)) {
263 struct file *swap_file = sis->swap_file;
264 struct address_space *mapping = swap_file->f_mapping;
265 struct bio_vec bv = {
270 struct iov_iter from;
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);
276 set_page_writeback(page);
278 ret = mapping->a_ops->direct_IO(&kiocb, &from);
279 if (ret == PAGE_SIZE) {
280 count_vm_event(PSWPOUT);
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
293 set_page_dirty(page);
294 ClearPageReclaim(page);
295 pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
296 page_file_offset(page));
298 end_page_writeback(page);
302 ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
304 count_swpout_vm_event(page);
308 bio = get_swap_bio(GFP_NOIO, page, end_write_func);
310 set_page_dirty(page);
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);
324 int swap_readpage(struct page *page, bool synchronous)
328 struct swap_info_struct *sis = page_swap_info(page);
330 struct gendisk *disk;
331 unsigned long pflags;
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);
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.
342 psi_memstall_enter(&pflags);
344 if (frontswap_load(page) == 0) {
345 SetPageUptodate(page);
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;
354 ret = mapping->a_ops->readpage(swap_file, page);
356 count_vm_event(PSWPIN);
360 if (sis->flags & SWP_SYNCHRONOUS_IO) {
361 ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
363 count_vm_event(PSWPIN);
369 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
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.
380 bio_set_op_attrs(bio, REQ_OP_READ, 0);
382 bio->bi_opf |= REQ_HIPRI;
383 get_task_struct(current);
384 bio->bi_private = current;
386 count_vm_event(PSWPIN);
388 qc = submit_bio(bio);
389 while (synchronous) {
390 set_current_state(TASK_UNINTERRUPTIBLE);
391 if (!READ_ONCE(bio->bi_private))
394 if (!blk_poll(disk->queue, qc, true))
397 __set_current_state(TASK_RUNNING);
401 psi_memstall_leave(&pflags);
405 int swap_set_page_dirty(struct page *page)
407 struct swap_info_struct *sis = page_swap_info(page);
409 if (data_race(sis->flags & SWP_FS_OPS)) {
410 struct address_space *mapping = sis->swap_file->f_mapping;
412 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
413 return mapping->a_ops->set_page_dirty(page);
415 return __set_page_dirty_no_writeback(page);