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> |
| 20 | #include <linux/writeback.h> |
Olav Haugan | 81a2825 | 2013-11-14 09:14:11 -0800 | [diff] [blame^] | 21 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/pgtable.h> |
| 23 | |
Olav Haugan | 81a2825 | 2013-11-14 09:14:11 -0800 | [diff] [blame^] | 24 | /* |
| 25 | * We don't need to see swap errors more than once every 1 second to know |
| 26 | * that a problem is occurring. |
| 27 | */ |
| 28 | #define SWAP_ERROR_LOG_RATE_MS 1000 |
| 29 | |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 30 | static struct bio *get_swap_bio(gfp_t gfp_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct page *page, bio_end_io_t end_io) |
| 32 | { |
| 33 | struct bio *bio; |
| 34 | |
| 35 | bio = bio_alloc(gfp_flags, 1); |
| 36 | if (bio) { |
Lee Schermerhorn | d4906e1 | 2009-12-14 17:58:49 -0800 | [diff] [blame] | 37 | bio->bi_sector = map_swap_page(page, &bio->bi_bdev); |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 38 | bio->bi_sector <<= PAGE_SHIFT - 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | bio->bi_io_vec[0].bv_page = page; |
| 40 | bio->bi_io_vec[0].bv_len = PAGE_SIZE; |
| 41 | bio->bi_io_vec[0].bv_offset = 0; |
| 42 | bio->bi_vcnt = 1; |
| 43 | bio->bi_idx = 0; |
| 44 | bio->bi_size = PAGE_SIZE; |
| 45 | bio->bi_end_io = end_io; |
| 46 | } |
| 47 | return bio; |
| 48 | } |
| 49 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 50 | static void end_swap_bio_write(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 53 | struct page *page = bio->bi_io_vec[0].bv_page; |
Olav Haugan | 81a2825 | 2013-11-14 09:14:11 -0800 | [diff] [blame^] | 54 | static unsigned long swap_error_rs_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 56 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | SetPageError(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 58 | /* |
| 59 | * We failed to write the page out to swap-space. |
| 60 | * Re-dirty the page in order to avoid it being reclaimed. |
| 61 | * Also print a dire warning that things will go BAD (tm) |
| 62 | * very quickly. |
| 63 | * |
| 64 | * Also clear PG_reclaim to avoid rotate_reclaimable_page() |
| 65 | */ |
| 66 | set_page_dirty(page); |
Olav Haugan | 81a2825 | 2013-11-14 09:14:11 -0800 | [diff] [blame^] | 67 | if (printk_timed_ratelimit(&swap_error_rs_time, |
| 68 | SWAP_ERROR_LOG_RATE_MS)) |
| 69 | printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n", |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 70 | imajor(bio->bi_bdev->bd_inode), |
| 71 | iminor(bio->bi_bdev->bd_inode), |
| 72 | (unsigned long long)bio->bi_sector); |
| 73 | ClearPageReclaim(page); |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | end_page_writeback(page); |
| 76 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 79 | void end_swap_bio_read(struct bio *bio, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 82 | struct page *page = bio->bi_io_vec[0].bv_page; |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (!uptodate) { |
| 85 | SetPageError(page); |
| 86 | ClearPageUptodate(page); |
Peter Zijlstra | 6ddab3b | 2006-09-25 23:31:26 -0700 | [diff] [blame] | 87 | printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n", |
| 88 | imajor(bio->bi_bdev->bd_inode), |
| 89 | iminor(bio->bi_bdev->bd_inode), |
| 90 | (unsigned long long)bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } else { |
| 92 | SetPageUptodate(page); |
| 93 | } |
| 94 | unlock_page(page); |
| 95 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
| 99 | * We may have stale swap cache pages in memory: notice |
| 100 | * them here and get rid of the unnecessary final write. |
| 101 | */ |
| 102 | int swap_writepage(struct page *page, struct writeback_control *wbc) |
| 103 | { |
| 104 | struct bio *bio; |
| 105 | int ret = 0, rw = WRITE; |
| 106 | |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 107 | if (try_to_free_swap(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | unlock_page(page); |
| 109 | goto out; |
| 110 | } |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 111 | bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (bio == NULL) { |
| 113 | set_page_dirty(page); |
| 114 | unlock_page(page); |
| 115 | ret = -ENOMEM; |
| 116 | goto out; |
| 117 | } |
| 118 | if (wbc->sync_mode == WB_SYNC_ALL) |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 119 | rw |= REQ_SYNC; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 120 | count_vm_event(PSWPOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | set_page_writeback(page); |
| 122 | unlock_page(page); |
| 123 | submit_bio(rw, bio); |
| 124 | out: |
| 125 | return ret; |
| 126 | } |
| 127 | |
Minchan Kim | aca8bf3 | 2009-06-16 15:33:02 -0700 | [diff] [blame] | 128 | int swap_readpage(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct bio *bio; |
| 131 | int ret = 0; |
| 132 | |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 133 | VM_BUG_ON(!PageLocked(page)); |
| 134 | VM_BUG_ON(PageUptodate(page)); |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 135 | bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (bio == NULL) { |
| 137 | unlock_page(page); |
| 138 | ret = -ENOMEM; |
| 139 | goto out; |
| 140 | } |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 141 | count_vm_event(PSWPIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | submit_bio(READ, bio); |
| 143 | out: |
| 144 | return ret; |
| 145 | } |