Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/page_io.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 5 | * |
| 6 | * Swap reorganised 29.12.95, |
| 7 | * Asynchronous swapping added 30.12.95. Stephen Tweedie |
| 8 | * Removed race in async swapping. 14.4.1996. Bruno Haible |
| 9 | * Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie |
| 10 | * Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman |
| 11 | */ |
| 12 | |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/kernel_stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/swap.h> |
| 18 | #include <linux/bio.h> |
| 19 | #include <linux/swapops.h> |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 22 | #include <linux/frontswap.h> |
Minchan Kim | b430e9d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 23 | #include <linux/blkdev.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 24 | #include <linux/uio.h> |
Tetsuo Handa | b0ba2d0 | 2017-08-02 13:32:09 -0700 | [diff] [blame] | 25 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/pgtable.h> |
| 27 | |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 28 | static struct bio *get_swap_bio(gfp_t gfp_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct page *page, bio_end_io_t end_io) |
| 30 | { |
| 31 | struct bio *bio; |
| 32 | |
| 33 | bio = bio_alloc(gfp_flags, 1); |
| 34 | if (bio) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 35 | bio->bi_iter.bi_sector = map_swap_page(page, &bio->bi_bdev); |
| 36 | bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | bio->bi_end_io = end_io; |
Kent Overstreet | 6cf66b4 | 2014-12-22 12:48:42 +0100 | [diff] [blame] | 38 | |
| 39 | bio_add_page(bio, page, PAGE_SIZE, 0); |
| 40 | BUG_ON(bio->bi_iter.bi_size != PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | return bio; |
| 43 | } |
| 44 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 45 | void end_swap_bio_write(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 48 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 49 | if (bio->bi_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | SetPageError(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 51 | /* |
| 52 | * We failed to write the page out to swap-space. |
| 53 | * Re-dirty the page in order to avoid it being reclaimed. |
| 54 | * Also print a dire warning that things will go BAD (tm) |
| 55 | * very quickly. |
| 56 | * |
| 57 | * Also clear PG_reclaim to avoid rotate_reclaimable_page() |
| 58 | */ |
| 59 | set_page_dirty(page); |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 60 | pr_alert("Write-error on swap-device (%u:%u:%llu)\n", |
| 61 | imajor(bio->bi_bdev->bd_inode), |
| 62 | iminor(bio->bi_bdev->bd_inode), |
| 63 | (unsigned long long)bio->bi_iter.bi_sector); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 64 | ClearPageReclaim(page); |
| 65 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | end_page_writeback(page); |
| 67 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Minchan Kim | 3f2b1a0 | 2016-03-22 14:24:36 -0700 | [diff] [blame] | 70 | static void swap_slot_free_notify(struct page *page) |
| 71 | { |
| 72 | struct swap_info_struct *sis; |
| 73 | struct gendisk *disk; |
| 74 | |
| 75 | /* |
| 76 | * There is no guarantee that the page is in swap cache - the software |
| 77 | * suspend code (at least) uses end_swap_bio_read() against a non- |
| 78 | * swapcache page. So we must check PG_swapcache before proceeding with |
| 79 | * this optimization. |
| 80 | */ |
| 81 | if (unlikely(!PageSwapCache(page))) |
| 82 | return; |
| 83 | |
| 84 | sis = page_swap_info(page); |
| 85 | if (!(sis->flags & SWP_BLKDEV)) |
| 86 | return; |
| 87 | |
| 88 | /* |
| 89 | * The swap subsystem performs lazy swap slot freeing, |
| 90 | * expecting that the page will be swapped out again. |
| 91 | * So we can avoid an unnecessary write if the page |
| 92 | * isn't redirtied. |
| 93 | * This is good for real swap storage because we can |
| 94 | * reduce unnecessary I/O and enhance wear-leveling |
| 95 | * if an SSD is used as the as swap device. |
| 96 | * But if in-memory swap device (eg zram) is used, |
| 97 | * this causes a duplicated copy between uncompressed |
| 98 | * data in VM-owned memory and compressed data in |
| 99 | * zram-owned memory. So let's free zram-owned memory |
| 100 | * and make the VM-owned decompressed page *dirty*, |
| 101 | * so the page should be swapped out somewhere again if |
| 102 | * we again wish to reclaim it. |
| 103 | */ |
| 104 | disk = sis->bdev->bd_disk; |
| 105 | if (disk->fops->swap_slot_free_notify) { |
| 106 | swp_entry_t entry; |
| 107 | unsigned long offset; |
| 108 | |
| 109 | entry.val = page_private(page); |
| 110 | offset = swp_offset(entry); |
| 111 | |
| 112 | SetPageDirty(page); |
| 113 | disk->fops->swap_slot_free_notify(sis->bdev, |
| 114 | offset); |
| 115 | } |
| 116 | } |
| 117 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 118 | static void end_swap_bio_read(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct page *page = bio->bi_io_vec[0].bv_page; |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 121 | struct task_struct *waiter = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 123 | if (bio->bi_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | SetPageError(page); |
| 125 | ClearPageUptodate(page); |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 126 | pr_alert("Read-error on swap-device (%u:%u:%llu)\n", |
| 127 | imajor(bio->bi_bdev->bd_inode), |
| 128 | iminor(bio->bi_bdev->bd_inode), |
| 129 | (unsigned long long)bio->bi_iter.bi_sector); |
Minchan Kim | b430e9d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 130 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Minchan Kim | b430e9d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 132 | |
| 133 | SetPageUptodate(page); |
Minchan Kim | 3f2b1a0 | 2016-03-22 14:24:36 -0700 | [diff] [blame] | 134 | swap_slot_free_notify(page); |
Minchan Kim | b430e9d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 135 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | unlock_page(page); |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 137 | WRITE_ONCE(bio->bi_private, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | bio_put(bio); |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 139 | wake_up_process(waiter); |
Tetsuo Handa | b0ba2d0 | 2017-08-02 13:32:09 -0700 | [diff] [blame] | 140 | put_task_struct(waiter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Mel Gorman | a509bc1 | 2012-07-31 16:44:57 -0700 | [diff] [blame] | 143 | int generic_swapfile_activate(struct swap_info_struct *sis, |
| 144 | struct file *swap_file, |
| 145 | sector_t *span) |
| 146 | { |
| 147 | struct address_space *mapping = swap_file->f_mapping; |
| 148 | struct inode *inode = mapping->host; |
| 149 | unsigned blocks_per_page; |
| 150 | unsigned long page_no; |
| 151 | unsigned blkbits; |
| 152 | sector_t probe_block; |
| 153 | sector_t last_block; |
| 154 | sector_t lowest_block = -1; |
| 155 | sector_t highest_block = 0; |
| 156 | int nr_extents = 0; |
| 157 | int ret; |
| 158 | |
| 159 | blkbits = inode->i_blkbits; |
| 160 | blocks_per_page = PAGE_SIZE >> blkbits; |
| 161 | |
| 162 | /* |
| 163 | * Map all the blocks into the extent list. This code doesn't try |
| 164 | * to be very smart. |
| 165 | */ |
| 166 | probe_block = 0; |
| 167 | page_no = 0; |
| 168 | last_block = i_size_read(inode) >> blkbits; |
| 169 | while ((probe_block + blocks_per_page) <= last_block && |
| 170 | page_no < sis->max) { |
| 171 | unsigned block_in_page; |
| 172 | sector_t first_block; |
| 173 | |
Mikulas Patocka | 7e4411b | 2016-07-28 15:48:47 -0700 | [diff] [blame] | 174 | cond_resched(); |
| 175 | |
Mel Gorman | a509bc1 | 2012-07-31 16:44:57 -0700 | [diff] [blame] | 176 | first_block = bmap(inode, probe_block); |
| 177 | if (first_block == 0) |
| 178 | goto bad_bmap; |
| 179 | |
| 180 | /* |
| 181 | * It must be PAGE_SIZE aligned on-disk |
| 182 | */ |
| 183 | if (first_block & (blocks_per_page - 1)) { |
| 184 | probe_block++; |
| 185 | goto reprobe; |
| 186 | } |
| 187 | |
| 188 | for (block_in_page = 1; block_in_page < blocks_per_page; |
| 189 | block_in_page++) { |
| 190 | sector_t block; |
| 191 | |
| 192 | block = bmap(inode, probe_block + block_in_page); |
| 193 | if (block == 0) |
| 194 | goto bad_bmap; |
| 195 | if (block != first_block + block_in_page) { |
| 196 | /* Discontiguity */ |
| 197 | probe_block++; |
| 198 | goto reprobe; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | first_block >>= (PAGE_SHIFT - blkbits); |
| 203 | if (page_no) { /* exclude the header page */ |
| 204 | if (first_block < lowest_block) |
| 205 | lowest_block = first_block; |
| 206 | if (first_block > highest_block) |
| 207 | highest_block = first_block; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks |
| 212 | */ |
| 213 | ret = add_swap_extent(sis, page_no, 1, first_block); |
| 214 | if (ret < 0) |
| 215 | goto out; |
| 216 | nr_extents += ret; |
| 217 | page_no++; |
| 218 | probe_block += blocks_per_page; |
| 219 | reprobe: |
| 220 | continue; |
| 221 | } |
| 222 | ret = nr_extents; |
| 223 | *span = 1 + highest_block - lowest_block; |
| 224 | if (page_no == 0) |
| 225 | page_no = 1; /* force Empty message */ |
| 226 | sis->max = page_no; |
| 227 | sis->pages = page_no - 1; |
| 228 | sis->highest_bit = page_no - 1; |
| 229 | out: |
| 230 | return ret; |
| 231 | bad_bmap: |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 232 | pr_err("swapon: swapfile has holes\n"); |
Mel Gorman | a509bc1 | 2012-07-31 16:44:57 -0700 | [diff] [blame] | 233 | ret = -EINVAL; |
| 234 | goto out; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /* |
| 238 | * We may have stale swap cache pages in memory: notice |
| 239 | * them here and get rid of the unnecessary final write. |
| 240 | */ |
| 241 | int swap_writepage(struct page *page, struct writeback_control *wbc) |
| 242 | { |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 243 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 245 | if (try_to_free_swap(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | unlock_page(page); |
| 247 | goto out; |
| 248 | } |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 249 | if (frontswap_store(page) == 0) { |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 250 | set_page_writeback(page); |
| 251 | unlock_page(page); |
| 252 | end_page_writeback(page); |
| 253 | goto out; |
| 254 | } |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 255 | ret = __swap_writepage(page, wbc, end_swap_bio_write); |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 256 | out: |
| 257 | return ret; |
| 258 | } |
| 259 | |
Matthew Wilcox | dd6bd0d | 2014-06-04 16:07:48 -0700 | [diff] [blame] | 260 | static sector_t swap_page_sector(struct page *page) |
| 261 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 262 | return (sector_t)__page_file_index(page) << (PAGE_SHIFT - 9); |
Matthew Wilcox | dd6bd0d | 2014-06-04 16:07:48 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 265 | int __swap_writepage(struct page *page, struct writeback_control *wbc, |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 266 | bio_end_io_t end_write_func) |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 267 | { |
| 268 | struct bio *bio; |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 269 | int ret; |
Seth Jennings | 2f772e6 | 2013-04-29 15:08:34 -0700 | [diff] [blame] | 270 | struct swap_info_struct *sis = page_swap_info(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 271 | |
Andrew Morton | cc30c5d | 2016-10-07 17:00:52 -0700 | [diff] [blame] | 272 | VM_BUG_ON_PAGE(!PageSwapCache(page), page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 273 | if (sis->flags & SWP_FILE) { |
| 274 | struct kiocb kiocb; |
| 275 | struct file *swap_file = sis->swap_file; |
| 276 | struct address_space *mapping = swap_file->f_mapping; |
Al Viro | 62a8067 | 2014-04-04 23:12:29 -0400 | [diff] [blame] | 277 | struct bio_vec bv = { |
| 278 | .bv_page = page, |
| 279 | .bv_len = PAGE_SIZE, |
| 280 | .bv_offset = 0 |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 281 | }; |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 282 | struct iov_iter from; |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 283 | |
Al Viro | 05afcb7 | 2015-01-23 01:08:07 -0500 | [diff] [blame] | 284 | iov_iter_bvec(&from, ITER_BVEC | WRITE, &bv, 1, PAGE_SIZE); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 285 | init_sync_kiocb(&kiocb, swap_file); |
| 286 | kiocb.ki_pos = page_file_offset(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 287 | |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 288 | set_page_writeback(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 289 | unlock_page(page); |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 290 | ret = mapping->a_ops->direct_IO(&kiocb, &from); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 291 | if (ret == PAGE_SIZE) { |
| 292 | count_vm_event(PSWPOUT); |
| 293 | ret = 0; |
Jerome Marchand | 2d30d31 | 2013-04-29 15:08:47 -0700 | [diff] [blame] | 294 | } else { |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 295 | /* |
| 296 | * In the case of swap-over-nfs, this can be a |
| 297 | * temporary failure if the system has limited |
| 298 | * memory for allocating transmit buffers. |
| 299 | * Mark the page dirty and avoid |
| 300 | * rotate_reclaimable_page but rate-limit the |
| 301 | * messages but do not flag PageError like |
| 302 | * the normal direct-to-bio case as it could |
| 303 | * be temporary. |
| 304 | */ |
Jerome Marchand | 2d30d31 | 2013-04-29 15:08:47 -0700 | [diff] [blame] | 305 | set_page_dirty(page); |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 306 | ClearPageReclaim(page); |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 307 | pr_err_ratelimited("Write error on dio swapfile (%llu)\n", |
| 308 | page_file_offset(page)); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 309 | } |
Mel Gorman | 0cdc444 | 2013-04-29 15:08:48 -0700 | [diff] [blame] | 310 | end_page_writeback(page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
Matthew Wilcox | dd6bd0d | 2014-06-04 16:07:48 -0700 | [diff] [blame] | 314 | ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc); |
| 315 | if (!ret) { |
| 316 | count_vm_event(PSWPOUT); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | ret = 0; |
Seth Jennings | 1eec670 | 2013-04-29 15:08:35 -0700 | [diff] [blame] | 321 | bio = get_swap_bio(GFP_NOIO, page, end_write_func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (bio == NULL) { |
| 323 | set_page_dirty(page); |
| 324 | unlock_page(page); |
| 325 | ret = -ENOMEM; |
| 326 | goto out; |
| 327 | } |
Jens Axboe | 7637241 | 2016-11-01 10:00:38 -0600 | [diff] [blame] | 328 | bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc); |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 329 | count_vm_event(PSWPOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | set_page_writeback(page); |
| 331 | unlock_page(page); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 332 | submit_bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | out: |
| 334 | return ret; |
| 335 | } |
| 336 | |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 337 | int swap_readpage(struct page *page, bool do_poll) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | struct bio *bio; |
| 340 | int ret = 0; |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 341 | struct swap_info_struct *sis = page_swap_info(page); |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 342 | blk_qc_t qc; |
| 343 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Andrew Morton | cc30c5d | 2016-10-07 17:00:52 -0700 | [diff] [blame] | 345 | VM_BUG_ON_PAGE(!PageSwapCache(page), page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 346 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 347 | VM_BUG_ON_PAGE(PageUptodate(page), page); |
Konrad Rzeszutek Wilk | 165c8ae | 2012-05-15 11:32:15 -0400 | [diff] [blame] | 348 | if (frontswap_load(page) == 0) { |
Dan Magenheimer | 38b5faf | 2012-04-09 17:08:06 -0600 | [diff] [blame] | 349 | SetPageUptodate(page); |
| 350 | unlock_page(page); |
| 351 | goto out; |
| 352 | } |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 353 | |
| 354 | if (sis->flags & SWP_FILE) { |
| 355 | struct file *swap_file = sis->swap_file; |
| 356 | struct address_space *mapping = swap_file->f_mapping; |
| 357 | |
| 358 | ret = mapping->a_ops->readpage(swap_file, page); |
| 359 | if (!ret) |
| 360 | count_vm_event(PSWPIN); |
| 361 | return ret; |
| 362 | } |
| 363 | |
Matthew Wilcox | dd6bd0d | 2014-06-04 16:07:48 -0700 | [diff] [blame] | 364 | ret = bdev_read_page(sis->bdev, swap_page_sector(page), page); |
| 365 | if (!ret) { |
Minchan Kim | b06bad1 | 2016-04-28 16:18:41 -0700 | [diff] [blame] | 366 | if (trylock_page(page)) { |
| 367 | swap_slot_free_notify(page); |
| 368 | unlock_page(page); |
| 369 | } |
| 370 | |
Matthew Wilcox | dd6bd0d | 2014-06-04 16:07:48 -0700 | [diff] [blame] | 371 | count_vm_event(PSWPIN); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | ret = 0; |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 376 | bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (bio == NULL) { |
| 378 | unlock_page(page); |
| 379 | ret = -ENOMEM; |
| 380 | goto out; |
| 381 | } |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 382 | bdev = bio->bi_bdev; |
Tetsuo Handa | b0ba2d0 | 2017-08-02 13:32:09 -0700 | [diff] [blame] | 383 | /* |
| 384 | * Keep this task valid during swap readpage because the oom killer may |
| 385 | * attempt to access it in the page fault retry time check. |
| 386 | */ |
| 387 | get_task_struct(current); |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 388 | bio->bi_private = current; |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 389 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 390 | count_vm_event(PSWPIN); |
Shaohua Li | 2395562 | 2017-07-10 15:47:11 -0700 | [diff] [blame] | 391 | bio_get(bio); |
| 392 | qc = submit_bio(bio); |
| 393 | while (do_poll) { |
| 394 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 395 | if (!READ_ONCE(bio->bi_private)) |
| 396 | break; |
| 397 | |
| 398 | if (!blk_mq_poll(bdev_get_queue(bdev), qc)) |
| 399 | break; |
| 400 | } |
| 401 | __set_current_state(TASK_RUNNING); |
| 402 | bio_put(bio); |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | out: |
| 405 | return ret; |
| 406 | } |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 407 | |
| 408 | int swap_set_page_dirty(struct page *page) |
| 409 | { |
| 410 | struct swap_info_struct *sis = page_swap_info(page); |
| 411 | |
| 412 | if (sis->flags & SWP_FILE) { |
| 413 | struct address_space *mapping = sis->swap_file->f_mapping; |
Andrew Morton | cc30c5d | 2016-10-07 17:00:52 -0700 | [diff] [blame] | 414 | |
| 415 | VM_BUG_ON_PAGE(!PageSwapCache(page), page); |
Mel Gorman | 62c230b | 2012-07-31 16:44:55 -0700 | [diff] [blame] | 416 | return mapping->a_ops->set_page_dirty(page); |
| 417 | } else { |
| 418 | return __set_page_dirty_no_writeback(page); |
| 419 | } |
| 420 | } |