blob: e60e43f11cfe63a5eaf0599e044c8038877417bb [file] [log] [blame]
Minchan Kim9cbf01d2013-07-03 15:01:24 -07001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Kim9cbf01d2013-07-03 15:01:24 -070022#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/pgtable.h>
24
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080025static struct bio *get_swap_bio(gfp_t gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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 Schermerhornd4906e12009-12-14 17:58:49 -080032 bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -080033 bio->bi_sector <<= PAGE_SHIFT - 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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
NeilBrown6712ecf2007-09-27 12:47:43 +020045static void end_swap_bio_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
48 struct page *page = bio->bi_io_vec[0].bv_page;
49
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070050 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SetPageError(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070052 /*
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 Torvalds1da177e2005-04-16 15:20:36 -070067 end_page_writeback(page);
68 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
NeilBrown6712ecf2007-09-27 12:47:43 +020071void end_swap_bio_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
74 struct page *page = bio->bi_io_vec[0].bv_page;
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!uptodate) {
77 SetPageError(page);
78 ClearPageUptodate(page);
Peter Zijlstra6ddab3b2006-09-25 23:31:26 -070079 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 Kim9cbf01d2013-07-03 15:01:24 -070083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Minchan Kim9cbf01d2013-07-03 15:01:24 -070085
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
130out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unlock_page(page);
132 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
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 */
139int swap_writepage(struct page *page, struct writeback_control *wbc)
140{
141 struct bio *bio;
142 int ret = 0, rw = WRITE;
143
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800144 if (try_to_free_swap(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unlock_page(page);
146 goto out;
147 }
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800148 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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 Axboe721a9602011-03-09 11:56:30 +0100156 rw |= REQ_SYNC;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700157 count_vm_event(PSWPOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 set_page_writeback(page);
159 unlock_page(page);
160 submit_bio(rw, bio);
161out:
162 return ret;
163}
164
Minchan Kimaca8bf32009-06-16 15:33:02 -0700165int swap_readpage(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct bio *bio;
168 int ret = 0;
169
Hugh Dickins51726b12009-01-06 14:39:25 -0800170 VM_BUG_ON(!PageLocked(page));
171 VM_BUG_ON(PageUptodate(page));
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -0800172 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (bio == NULL) {
174 unlock_page(page);
175 ret = -ENOMEM;
176 goto out;
177 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700178 count_vm_event(PSWPIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 submit_bio(READ, bio);
180out:
181 return ret;
182}