2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* linux/mm/page_io.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Swap reorganised 29.12.95,
|
|
|
|
* Asynchronous swapping added 30.12.95. Stephen Tweedie
|
|
|
|
* Removed race in async swapping. 14.4.1996. Bruno Haible
|
|
|
|
* Add swap of shared pages through the page cache. 20.2.1998. Stephen Tweedie
|
|
|
|
* Always use brw_page, life becomes simpler. 12 May 1998 Eric Biederman
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/kernel_stat.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 09:04:11 +01:00
|
|
|
#include <linux/gfp.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/swapops.h>
|
2012-08-01 01:44:55 +02:00
|
|
|
#include <linux/buffer_head.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/writeback.h>
|
2012-04-10 01:08:06 +02:00
|
|
|
#include <linux/frontswap.h>
|
2013-05-08 01:19:08 +02:00
|
|
|
#include <linux/aio.h>
|
2013-07-04 00:01:24 +02:00
|
|
|
#include <linux/blkdev.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <asm/pgtable.h>
|
|
|
|
|
2009-12-15 02:58:40 +01:00
|
|
|
static struct bio *get_swap_bio(gfp_t gfp_flags,
|
2005-04-17 00:20:36 +02:00
|
|
|
struct page *page, bio_end_io_t end_io)
|
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
bio = bio_alloc(gfp_flags, 1);
|
|
|
|
if (bio) {
|
2009-12-15 02:58:49 +01:00
|
|
|
bio->bi_sector = map_swap_page(page, &bio->bi_bdev);
|
2009-12-15 02:58:40 +01:00
|
|
|
bio->bi_sector <<= PAGE_SHIFT - 9;
|
2005-04-17 00:20:36 +02:00
|
|
|
bio->bi_io_vec[0].bv_page = page;
|
|
|
|
bio->bi_io_vec[0].bv_len = PAGE_SIZE;
|
|
|
|
bio->bi_io_vec[0].bv_offset = 0;
|
|
|
|
bio->bi_vcnt = 1;
|
|
|
|
bio->bi_size = PAGE_SIZE;
|
|
|
|
bio->bi_end_io = end_io;
|
|
|
|
}
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
2013-04-30 00:08:35 +02:00
|
|
|
void end_swap_bio_write(struct bio *bio, int err)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
|
|
|
|
struct page *page = bio->bi_io_vec[0].bv_page;
|
|
|
|
|
2006-09-26 08:31:26 +02:00
|
|
|
if (!uptodate) {
|
2005-04-17 00:20:36 +02:00
|
|
|
SetPageError(page);
|
2006-09-26 08:31:26 +02:00
|
|
|
/*
|
|
|
|
* We failed to write the page out to swap-space.
|
|
|
|
* Re-dirty the page in order to avoid it being reclaimed.
|
|
|
|
* Also print a dire warning that things will go BAD (tm)
|
|
|
|
* very quickly.
|
|
|
|
*
|
|
|
|
* Also clear PG_reclaim to avoid rotate_reclaimable_page()
|
|
|
|
*/
|
|
|
|
set_page_dirty(page);
|
|
|
|
printk(KERN_ALERT "Write-error on swap-device (%u:%u:%Lu)\n",
|
|
|
|
imajor(bio->bi_bdev->bd_inode),
|
|
|
|
iminor(bio->bi_bdev->bd_inode),
|
|
|
|
(unsigned long long)bio->bi_sector);
|
|
|
|
ClearPageReclaim(page);
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
end_page_writeback(page);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
2007-09-27 12:47:43 +02:00
|
|
|
void end_swap_bio_read(struct bio *bio, int err)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
|
|
|
|
struct page *page = bio->bi_io_vec[0].bv_page;
|
|
|
|
|
|
|
|
if (!uptodate) {
|
|
|
|
SetPageError(page);
|
|
|
|
ClearPageUptodate(page);
|
2006-09-26 08:31:26 +02:00
|
|
|
printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
|
|
|
|
imajor(bio->bi_bdev->bd_inode),
|
|
|
|
iminor(bio->bi_bdev->bd_inode),
|
|
|
|
(unsigned long long)bio->bi_sector);
|
2013-07-04 00:01:24 +02:00
|
|
|
goto out;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2013-07-04 00:01:24 +02:00
|
|
|
|
|
|
|
SetPageUptodate(page);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no guarantee that the page is in swap cache - the software
|
|
|
|
* suspend code (at least) uses end_swap_bio_read() against a non-
|
|
|
|
* swapcache page. So we must check PG_swapcache before proceeding with
|
|
|
|
* this optimization.
|
|
|
|
*/
|
|
|
|
if (likely(PageSwapCache(page))) {
|
|
|
|
struct swap_info_struct *sis;
|
|
|
|
|
|
|
|
sis = page_swap_info(page);
|
|
|
|
if (sis->flags & SWP_BLKDEV) {
|
|
|
|
/*
|
|
|
|
* The swap subsystem performs lazy swap slot freeing,
|
|
|
|
* expecting that the page will be swapped out again.
|
|
|
|
* So we can avoid an unnecessary write if the page
|
|
|
|
* isn't redirtied.
|
|
|
|
* This is good for real swap storage because we can
|
|
|
|
* reduce unnecessary I/O and enhance wear-leveling
|
|
|
|
* if an SSD is used as the as swap device.
|
|
|
|
* But if in-memory swap device (eg zram) is used,
|
|
|
|
* this causes a duplicated copy between uncompressed
|
|
|
|
* data in VM-owned memory and compressed data in
|
|
|
|
* zram-owned memory. So let's free zram-owned memory
|
|
|
|
* and make the VM-owned decompressed page *dirty*,
|
|
|
|
* so the page should be swapped out somewhere again if
|
|
|
|
* we again wish to reclaim it.
|
|
|
|
*/
|
|
|
|
struct gendisk *disk = sis->bdev->bd_disk;
|
|
|
|
if (disk->fops->swap_slot_free_notify) {
|
|
|
|
swp_entry_t entry;
|
|
|
|
unsigned long offset;
|
|
|
|
|
|
|
|
entry.val = page_private(page);
|
|
|
|
offset = swp_offset(entry);
|
|
|
|
|
|
|
|
SetPageDirty(page);
|
|
|
|
disk->fops->swap_slot_free_notify(sis->bdev,
|
|
|
|
offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2005-04-17 00:20:36 +02:00
|
|
|
unlock_page(page);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
2012-08-01 01:44:57 +02:00
|
|
|
int generic_swapfile_activate(struct swap_info_struct *sis,
|
|
|
|
struct file *swap_file,
|
|
|
|
sector_t *span)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = swap_file->f_mapping;
|
|
|
|
struct inode *inode = mapping->host;
|
|
|
|
unsigned blocks_per_page;
|
|
|
|
unsigned long page_no;
|
|
|
|
unsigned blkbits;
|
|
|
|
sector_t probe_block;
|
|
|
|
sector_t last_block;
|
|
|
|
sector_t lowest_block = -1;
|
|
|
|
sector_t highest_block = 0;
|
|
|
|
int nr_extents = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blkbits = inode->i_blkbits;
|
|
|
|
blocks_per_page = PAGE_SIZE >> blkbits;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map all the blocks into the extent list. This code doesn't try
|
|
|
|
* to be very smart.
|
|
|
|
*/
|
|
|
|
probe_block = 0;
|
|
|
|
page_no = 0;
|
|
|
|
last_block = i_size_read(inode) >> blkbits;
|
|
|
|
while ((probe_block + blocks_per_page) <= last_block &&
|
|
|
|
page_no < sis->max) {
|
|
|
|
unsigned block_in_page;
|
|
|
|
sector_t first_block;
|
|
|
|
|
|
|
|
first_block = bmap(inode, probe_block);
|
|
|
|
if (first_block == 0)
|
|
|
|
goto bad_bmap;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It must be PAGE_SIZE aligned on-disk
|
|
|
|
*/
|
|
|
|
if (first_block & (blocks_per_page - 1)) {
|
|
|
|
probe_block++;
|
|
|
|
goto reprobe;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (block_in_page = 1; block_in_page < blocks_per_page;
|
|
|
|
block_in_page++) {
|
|
|
|
sector_t block;
|
|
|
|
|
|
|
|
block = bmap(inode, probe_block + block_in_page);
|
|
|
|
if (block == 0)
|
|
|
|
goto bad_bmap;
|
|
|
|
if (block != first_block + block_in_page) {
|
|
|
|
/* Discontiguity */
|
|
|
|
probe_block++;
|
|
|
|
goto reprobe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first_block >>= (PAGE_SHIFT - blkbits);
|
|
|
|
if (page_no) { /* exclude the header page */
|
|
|
|
if (first_block < lowest_block)
|
|
|
|
lowest_block = first_block;
|
|
|
|
if (first_block > highest_block)
|
|
|
|
highest_block = first_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
|
|
|
|
*/
|
|
|
|
ret = add_swap_extent(sis, page_no, 1, first_block);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
nr_extents += ret;
|
|
|
|
page_no++;
|
|
|
|
probe_block += blocks_per_page;
|
|
|
|
reprobe:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = nr_extents;
|
|
|
|
*span = 1 + highest_block - lowest_block;
|
|
|
|
if (page_no == 0)
|
|
|
|
page_no = 1; /* force Empty message */
|
|
|
|
sis->max = page_no;
|
|
|
|
sis->pages = page_no - 1;
|
|
|
|
sis->highest_bit = page_no - 1;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
bad_bmap:
|
|
|
|
printk(KERN_ERR "swapon: swapfile has holes\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* We may have stale swap cache pages in memory: notice
|
|
|
|
* them here and get rid of the unnecessary final write.
|
|
|
|
*/
|
|
|
|
int swap_writepage(struct page *page, struct writeback_control *wbc)
|
|
|
|
{
|
2013-04-30 00:08:34 +02:00
|
|
|
int ret = 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2009-01-06 23:39:36 +01:00
|
|
|
if (try_to_free_swap(page)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
unlock_page(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-15 17:32:15 +02:00
|
|
|
if (frontswap_store(page) == 0) {
|
2012-04-10 01:08:06 +02:00
|
|
|
set_page_writeback(page);
|
|
|
|
unlock_page(page);
|
|
|
|
end_page_writeback(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2013-04-30 00:08:35 +02:00
|
|
|
ret = __swap_writepage(page, wbc, end_swap_bio_write);
|
2013-04-30 00:08:34 +02:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-30 00:08:35 +02:00
|
|
|
int __swap_writepage(struct page *page, struct writeback_control *wbc,
|
|
|
|
void (*end_write_func)(struct bio *, int))
|
2013-04-30 00:08:34 +02:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
int ret = 0, rw = WRITE;
|
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
2012-08-01 01:44:55 +02:00
|
|
|
|
|
|
|
if (sis->flags & SWP_FILE) {
|
|
|
|
struct kiocb kiocb;
|
|
|
|
struct file *swap_file = sis->swap_file;
|
|
|
|
struct address_space *mapping = swap_file->f_mapping;
|
|
|
|
struct iovec iov = {
|
2012-08-01 01:45:02 +02:00
|
|
|
.iov_base = kmap(page),
|
2012-08-01 01:44:55 +02:00
|
|
|
.iov_len = PAGE_SIZE,
|
|
|
|
};
|
|
|
|
|
|
|
|
init_sync_kiocb(&kiocb, swap_file);
|
|
|
|
kiocb.ki_pos = page_file_offset(page);
|
|
|
|
kiocb.ki_left = PAGE_SIZE;
|
|
|
|
kiocb.ki_nbytes = PAGE_SIZE;
|
|
|
|
|
2013-04-30 00:08:48 +02:00
|
|
|
set_page_writeback(page);
|
2012-08-01 01:44:55 +02:00
|
|
|
unlock_page(page);
|
|
|
|
ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
|
|
|
|
&kiocb, &iov,
|
|
|
|
kiocb.ki_pos, 1);
|
2012-08-01 01:45:02 +02:00
|
|
|
kunmap(page);
|
2012-08-01 01:44:55 +02:00
|
|
|
if (ret == PAGE_SIZE) {
|
|
|
|
count_vm_event(PSWPOUT);
|
|
|
|
ret = 0;
|
2013-04-30 00:08:47 +02:00
|
|
|
} else {
|
2013-04-30 00:08:48 +02:00
|
|
|
/*
|
|
|
|
* In the case of swap-over-nfs, this can be a
|
|
|
|
* temporary failure if the system has limited
|
|
|
|
* memory for allocating transmit buffers.
|
|
|
|
* Mark the page dirty and avoid
|
|
|
|
* rotate_reclaimable_page but rate-limit the
|
|
|
|
* messages but do not flag PageError like
|
|
|
|
* the normal direct-to-bio case as it could
|
|
|
|
* be temporary.
|
|
|
|
*/
|
2013-04-30 00:08:47 +02:00
|
|
|
set_page_dirty(page);
|
2013-04-30 00:08:48 +02:00
|
|
|
ClearPageReclaim(page);
|
|
|
|
pr_err_ratelimited("Write error on dio swapfile (%Lu)\n",
|
|
|
|
page_file_offset(page));
|
2012-08-01 01:44:55 +02:00
|
|
|
}
|
2013-04-30 00:08:48 +02:00
|
|
|
end_page_writeback(page);
|
2012-08-01 01:44:55 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-30 00:08:35 +02:00
|
|
|
bio = get_swap_bio(GFP_NOIO, page, end_write_func);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (bio == NULL) {
|
|
|
|
set_page_dirty(page);
|
|
|
|
unlock_page(page);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (wbc->sync_mode == WB_SYNC_ALL)
|
2011-03-09 11:56:30 +01:00
|
|
|
rw |= REQ_SYNC;
|
2006-06-30 10:55:45 +02:00
|
|
|
count_vm_event(PSWPOUT);
|
2005-04-17 00:20:36 +02:00
|
|
|
set_page_writeback(page);
|
|
|
|
unlock_page(page);
|
|
|
|
submit_bio(rw, bio);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-17 00:33:02 +02:00
|
|
|
int swap_readpage(struct page *page)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct bio *bio;
|
|
|
|
int ret = 0;
|
2012-08-01 01:44:55 +02:00
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2009-01-06 23:39:25 +01:00
|
|
|
VM_BUG_ON(!PageLocked(page));
|
|
|
|
VM_BUG_ON(PageUptodate(page));
|
2012-05-15 17:32:15 +02:00
|
|
|
if (frontswap_load(page) == 0) {
|
2012-04-10 01:08:06 +02:00
|
|
|
SetPageUptodate(page);
|
|
|
|
unlock_page(page);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-08-01 01:44:55 +02:00
|
|
|
|
|
|
|
if (sis->flags & SWP_FILE) {
|
|
|
|
struct file *swap_file = sis->swap_file;
|
|
|
|
struct address_space *mapping = swap_file->f_mapping;
|
|
|
|
|
|
|
|
ret = mapping->a_ops->readpage(swap_file, page);
|
|
|
|
if (!ret)
|
|
|
|
count_vm_event(PSWPIN);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-15 02:58:40 +01:00
|
|
|
bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (bio == NULL) {
|
|
|
|
unlock_page(page);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-06-30 10:55:45 +02:00
|
|
|
count_vm_event(PSWPIN);
|
2005-04-17 00:20:36 +02:00
|
|
|
submit_bio(READ, bio);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-01 01:44:55 +02:00
|
|
|
|
|
|
|
int swap_set_page_dirty(struct page *page)
|
|
|
|
{
|
|
|
|
struct swap_info_struct *sis = page_swap_info(page);
|
|
|
|
|
|
|
|
if (sis->flags & SWP_FILE) {
|
|
|
|
struct address_space *mapping = sis->swap_file->f_mapping;
|
|
|
|
return mapping->a_ops->set_page_dirty(page);
|
|
|
|
} else {
|
|
|
|
return __set_page_dirty_no_writeback(page);
|
|
|
|
}
|
|
|
|
}
|