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>
28 #include <linux/delayacct.h>
30 void end_swap_bio_write(struct bio *bio)
32 struct page *page = bio_first_page_all(bio);
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)
42 * Also clear PG_reclaim to avoid folio_rotate_reclaimable()
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);
50 end_page_writeback(page);
54 static void end_swap_bio_read(struct bio *bio)
56 struct page *page = bio_first_page_all(bio);
57 struct task_struct *waiter = bio->bi_private;
61 ClearPageUptodate(page);
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);
68 SetPageUptodate(page);
71 WRITE_ONCE(bio->bi_private, NULL);
74 blk_wake_io_task(waiter);
75 put_task_struct(waiter);
79 int generic_swapfile_activate(struct swap_info_struct *sis,
80 struct file *swap_file,
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;
90 sector_t lowest_block = -1;
91 sector_t highest_block = 0;
95 blkbits = inode->i_blkbits;
96 blocks_per_page = PAGE_SIZE >> blkbits;
99 * Map all the blocks into the extent tree. This code doesn't try
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;
112 first_block = probe_block;
113 ret = bmap(inode, &first_block);
114 if (ret || !first_block)
118 * It must be PAGE_SIZE aligned on-disk
120 if (first_block & (blocks_per_page - 1)) {
125 for (block_in_page = 1; block_in_page < blocks_per_page;
129 block = probe_block + block_in_page;
130 ret = bmap(inode, &block);
134 if (block != first_block + block_in_page) {
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;
150 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
152 ret = add_swap_extent(sis, page_no, 1, first_block);
157 probe_block += blocks_per_page;
162 *span = 1 + highest_block - lowest_block;
164 page_no = 1; /* force Empty message */
166 sis->pages = page_no - 1;
167 sis->highest_bit = page_no - 1;
171 pr_err("swapon: swapfile has holes\n");
177 * We may have stale swap cache pages in memory: notice
178 * them here and get rid of the unnecessary final write.
180 int swap_writepage(struct page *page, struct writeback_control *wbc)
184 if (try_to_free_swap(page)) {
189 * Arch code may have to preserve more data than just the page
190 * contents, e.g. memory tags.
192 ret = arch_prepare_to_swap(page);
194 set_page_dirty(page);
198 if (frontswap_store(page) == 0) {
199 set_page_writeback(page);
201 end_page_writeback(page);
204 ret = __swap_writepage(page, wbc, end_swap_bio_write);
209 static inline void count_swpout_vm_event(struct page *page)
211 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
212 if (unlikely(PageTransHuge(page)))
213 count_vm_event(THP_SWPOUT);
215 count_vm_events(PSWPOUT, thp_nr_pages(page));
218 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
219 static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
221 struct cgroup_subsys_state *css;
222 struct mem_cgroup *memcg;
224 memcg = page_memcg(page);
229 css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
230 bio_associate_blkg_from_css(bio, css);
234 #define bio_associate_blkg_from_page(bio, page) do { } while (0)
235 #endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
237 int __swap_writepage(struct page *page, struct writeback_control *wbc,
238 bio_end_io_t end_write_func)
242 struct swap_info_struct *sis = page_swap_info(page);
244 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
245 if (data_race(sis->flags & SWP_FS_OPS)) {
247 struct file *swap_file = sis->swap_file;
248 struct address_space *mapping = swap_file->f_mapping;
249 struct bio_vec bv = {
254 struct iov_iter from;
256 iov_iter_bvec(&from, WRITE, &bv, 1, PAGE_SIZE);
257 init_sync_kiocb(&kiocb, swap_file);
258 kiocb.ki_pos = page_file_offset(page);
260 set_page_writeback(page);
262 ret = mapping->a_ops->direct_IO(&kiocb, &from);
263 if (ret == PAGE_SIZE) {
264 count_vm_event(PSWPOUT);
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
272 * folio_rotate_reclaimable but rate-limit the
273 * messages but do not flag PageError like
274 * the normal direct-to-bio case as it could
277 set_page_dirty(page);
278 ClearPageReclaim(page);
279 pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
280 page_file_offset(page));
282 end_page_writeback(page);
286 ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
288 count_swpout_vm_event(page);
292 bio = bio_alloc(sis->bdev, 1,
293 REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc),
295 bio->bi_iter.bi_sector = swap_page_sector(page);
296 bio->bi_end_io = end_write_func;
297 bio_add_page(bio, page, thp_size(page), 0);
299 bio_associate_blkg_from_page(bio, page);
300 count_swpout_vm_event(page);
301 set_page_writeback(page);
308 int swap_readpage(struct page *page, bool synchronous)
312 struct swap_info_struct *sis = page_swap_info(page);
313 bool workingset = PageWorkingset(page);
314 unsigned long pflags;
316 VM_BUG_ON_PAGE(!PageSwapCache(page) && !synchronous, page);
317 VM_BUG_ON_PAGE(!PageLocked(page), page);
318 VM_BUG_ON_PAGE(PageUptodate(page), page);
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.
326 psi_memstall_enter(&pflags);
327 delayacct_swapin_start();
329 if (frontswap_load(page) == 0) {
330 SetPageUptodate(page);
335 if (data_race(sis->flags & SWP_FS_OPS)) {
336 struct file *swap_file = sis->swap_file;
337 struct address_space *mapping = swap_file->f_mapping;
339 ret = mapping->a_ops->readpage(swap_file, page);
341 count_vm_event(PSWPIN);
345 if (sis->flags & SWP_SYNCHRONOUS_IO) {
346 ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
348 count_vm_event(PSWPIN);
354 bio = bio_alloc(sis->bdev, 1, REQ_OP_READ, GFP_KERNEL);
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);
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.
363 bio->bi_opf |= REQ_POLLED;
364 get_task_struct(current);
365 bio->bi_private = current;
367 count_vm_event(PSWPIN);
370 while (synchronous) {
371 set_current_state(TASK_UNINTERRUPTIBLE);
372 if (!READ_ONCE(bio->bi_private))
375 if (!bio_poll(bio, NULL, 0))
378 __set_current_state(TASK_RUNNING);
383 psi_memstall_leave(&pflags);
384 delayacct_swapin_end();
388 bool swap_dirty_folio(struct address_space *mapping, struct folio *folio)
390 struct swap_info_struct *sis = swp_swap_info(folio_swap_entry(folio));
392 if (data_race(sis->flags & SWP_FS_OPS)) {
393 const struct address_space_operations *aops;
395 mapping = sis->swap_file->f_mapping;
396 aops = mapping->a_ops;
398 VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
399 return aops->dirty_folio(mapping, folio);
401 return noop_dirty_folio(mapping, folio);