Minchan Kim | 9cbf01d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 1 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/bio.h> |
| 20 | #include <linux/swapops.h> |
| 21 | #include <linux/writeback.h> |
Minchan Kim | 9cbf01d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/pgtable.h> |
| 24 | |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 25 | static struct bio *get_swap_bio(gfp_t gfp_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | struct page *page, bio_end_io_t end_io) |
| 27 | { |
| 28 | struct bio *bio; |
| 29 | |
| 30 | bio = bio_alloc(gfp_flags, 1); |
| 31 | if (bio) { |
Lee Schermerhorn | d4906e1 | 2009-12-14 17:58:49 -0800 | [diff] [blame] | 32 | bio->bi_sector = map_swap_page(page, &bio->bi_bdev); |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 33 | bio->bi_sector <<= PAGE_SHIFT - 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | bio->bi_io_vec[0].bv_page = page; |
| 35 | bio->bi_io_vec[0].bv_len = PAGE_SIZE; |
| 36 | bio->bi_io_vec[0].bv_offset = 0; |
| 37 | bio->bi_vcnt = 1; |
| 38 | bio->bi_idx = 0; |
| 39 | bio->bi_size = PAGE_SIZE; |
| 40 | bio->bi_end_io = end_io; |
| 41 | } |
| 42 | return bio; |
| 43 | } |
| 44 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 45 | static void end_swap_bio_write(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 48 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 49 | |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 50 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | SetPageError(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 52 | /* |
| 53 | * We failed to write the page out to swap-space. |
| 54 | * Re-dirty the page in order to avoid it being reclaimed. |
| 55 | * Also print a dire warning that things will go BAD (tm) |
| 56 | * very quickly. |
| 57 | * |
| 58 | * Also clear PG_reclaim to avoid rotate_reclaimable_page() |
| 59 | */ |
| 60 | set_page_dirty(page); |
| 61 | printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n", |
| 62 | imajor(bio->bi_bdev->bd_inode), |
| 63 | iminor(bio->bi_bdev->bd_inode), |
| 64 | (unsigned long long)bio->bi_sector); |
| 65 | ClearPageReclaim(page); |
| 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | end_page_writeback(page); |
| 68 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 71 | void end_swap_bio_read(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 74 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (!uptodate) { |
| 77 | SetPageError(page); |
| 78 | ClearPageUptodate(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 79 | printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n", |
| 80 | imajor(bio->bi_bdev->bd_inode), |
| 81 | iminor(bio->bi_bdev->bd_inode), |
| 82 | (unsigned long long)bio->bi_sector); |
Minchan Kim | 9cbf01d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 83 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
Minchan Kim | 9cbf01d | 2013-07-03 15:01:24 -0700 | [diff] [blame] | 85 | |
| 86 | SetPageUptodate(page); |
| 87 | |
| 88 | /* |
| 89 | * There is no guarantee that the page is in swap cache - the software |
| 90 | * suspend code (at least) uses end_swap_bio_read() against a non- |
| 91 | * swapcache page. So we must check PG_swapcache before proceeding with |
| 92 | * this optimization. |
| 93 | */ |
| 94 | if (likely(PageSwapCache(page))) { |
| 95 | struct swap_info_struct *sis; |
| 96 | |
| 97 | sis = page_swap_info(page); |
| 98 | if (sis->flags & SWP_BLKDEV) { |
| 99 | /* |
| 100 | * The swap subsystem performs lazy swap slot freeing, |
| 101 | * expecting that the page will be swapped out again. |
| 102 | * So we can avoid an unnecessary write if the page |
| 103 | * isn't redirtied. |
| 104 | * This is good for real swap storage because we can |
| 105 | * reduce unnecessary I/O and enhance wear-leveling |
| 106 | * if an SSD is used as the as swap device. |
| 107 | * But if in-memory swap device (eg zram) is used, |
| 108 | * this causes a duplicated copy between uncompressed |
| 109 | * data in VM-owned memory and compressed data in |
| 110 | * zram-owned memory. So let's free zram-owned memory |
| 111 | * and make the VM-owned decompressed page *dirty*, |
| 112 | * so the page should be swapped out somewhere again if |
| 113 | * we again wish to reclaim it. |
| 114 | */ |
| 115 | struct gendisk *disk = sis->bdev->bd_disk; |
| 116 | if (disk->fops->swap_slot_free_notify) { |
| 117 | swp_entry_t entry; |
| 118 | unsigned long offset; |
| 119 | |
| 120 | entry.val = page_private(page); |
| 121 | offset = swp_offset(entry); |
| 122 | |
| 123 | SetPageDirty(page); |
| 124 | disk->fops->swap_slot_free_notify(sis->bdev, |
| 125 | offset); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unlock_page(page); |
| 132 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * We may have stale swap cache pages in memory: notice |
| 137 | * them here and get rid of the unnecessary final write. |
| 138 | */ |
| 139 | int swap_writepage(struct page *page, struct writeback_control *wbc) |
| 140 | { |
| 141 | struct bio *bio; |
| 142 | int ret = 0, rw = WRITE; |
| 143 | |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 144 | if (try_to_free_swap(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | unlock_page(page); |
| 146 | goto out; |
| 147 | } |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 148 | bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (bio == NULL) { |
| 150 | set_page_dirty(page); |
| 151 | unlock_page(page); |
| 152 | ret = -ENOMEM; |
| 153 | goto out; |
| 154 | } |
| 155 | if (wbc->sync_mode == WB_SYNC_ALL) |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 156 | rw |= REQ_SYNC; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 157 | count_vm_event(PSWPOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | set_page_writeback(page); |
| 159 | unlock_page(page); |
| 160 | submit_bio(rw, bio); |
| 161 | out: |
| 162 | return ret; |
| 163 | } |
| 164 | |
Minchan Kim | aca8bf3 | 2009-06-16 15:33:02 -0700 | [diff] [blame] | 165 | int swap_readpage(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | struct bio *bio; |
| 168 | int ret = 0; |
| 169 | |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 170 | VM_BUG_ON(!PageLocked(page)); |
| 171 | VM_BUG_ON(PageUptodate(page)); |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 172 | bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (bio == NULL) { |
| 174 | unlock_page(page); |
| 175 | ret = -ENOMEM; |
| 176 | goto out; |
| 177 | } |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 178 | count_vm_event(PSWPIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | submit_bio(READ, bio); |
| 180 | out: |
| 181 | return ret; |
| 182 | } |