aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorMel Gorman <mgorman@suse.de>2012-07-31 19:44:55 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-07-31 21:42:47 -0400
commit62c230bc1790923a1b35da03596a68a6c9b5b100 (patch)
tree46b300a00929087627e029b0f4d53e486a97ba7f /mm
parent18022c5d8627a7a9ba8097a0f238b513fae6f5b8 (diff)
mm: add support for a filesystem to activate swap files and use direct_IO for writing swap pages
Currently swapfiles are managed entirely by the core VM by using ->bmap to allocate space and write to the blocks directly. This effectively ensures that the underlying blocks are allocated and avoids the need for the swap subsystem to locate what physical blocks store offsets within a file. If the swap subsystem is to use the filesystem information to locate the blocks, it is critical that information such as block groups, block bitmaps and the block descriptor table that map the swap file were resident in memory. This patch adds address_space_operations that the VM can call when activating or deactivating swap backed by a file. int swap_activate(struct file *); int swap_deactivate(struct file *); The ->swap_activate() method is used to communicate to the file that the VM relies on it, and the address_space should take adequate measures such as reserving space in the underlying device, reserving memory for mempools and pinning information such as the block descriptor table in memory. The ->swap_deactivate() method is called on sys_swapoff() if ->swap_activate() returned success. After a successful swapfile ->swap_activate, the swapfile is marked SWP_FILE and swapper_space.a_ops will proxy to sis->swap_file->f_mappings->a_ops using ->direct_io to write swapcache pages and ->readpage to read. It is perfectly possible that direct_IO be used to read the swap pages but it is an unnecessary complication. Similarly, it is possible that ->writepage be used instead of direct_io to write the pages but filesystem developers have stated that calling writepage from the VM is undesirable for a variety of reasons and using direct_IO opens up the possibility of writing back batches of swap pages in the future. [a.p.zijlstra@chello.nl: Original patch] Signed-off-by: Mel Gorman <mgorman@suse.de> Acked-by: Rik van Riel <riel@redhat.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: David S. Miller <davem@davemloft.net> Cc: Eric B Munson <emunson@mgebm.net> Cc: Eric Paris <eparis@redhat.com> Cc: James Morris <jmorris@namei.org> Cc: Mel Gorman <mgorman@suse.de> Cc: Mike Christie <michaelc@cs.wisc.edu> Cc: Neil Brown <neilb@suse.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Sebastian Andrzej Siewior <sebastian@breakpoint.cc> Cc: Trond Myklebust <Trond.Myklebust@netapp.com> Cc: Xiaotian Feng <dfeng@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/page_io.c52
-rw-r--r--mm/swap_state.c2
-rw-r--r--mm/swapfile.c23
3 files changed, 74 insertions, 3 deletions
diff --git a/mm/page_io.c b/mm/page_io.c
index 34f02923744..307a3e79529 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -17,6 +17,7 @@
17#include <linux/swap.h> 17#include <linux/swap.h>
18#include <linux/bio.h> 18#include <linux/bio.h>
19#include <linux/swapops.h> 19#include <linux/swapops.h>
20#include <linux/buffer_head.h>
20#include <linux/writeback.h> 21#include <linux/writeback.h>
21#include <linux/frontswap.h> 22#include <linux/frontswap.h>
22#include <asm/pgtable.h> 23#include <asm/pgtable.h>
@@ -94,6 +95,7 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
94{ 95{
95 struct bio *bio; 96 struct bio *bio;
96 int ret = 0, rw = WRITE; 97 int ret = 0, rw = WRITE;
98 struct swap_info_struct *sis = page_swap_info(page);
97 99
98 if (try_to_free_swap(page)) { 100 if (try_to_free_swap(page)) {
99 unlock_page(page); 101 unlock_page(page);
@@ -105,6 +107,32 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
105 end_page_writeback(page); 107 end_page_writeback(page);
106 goto out; 108 goto out;
107 } 109 }
110
111 if (sis->flags & SWP_FILE) {
112 struct kiocb kiocb;
113 struct file *swap_file = sis->swap_file;
114 struct address_space *mapping = swap_file->f_mapping;
115 struct iovec iov = {
116 .iov_base = page_address(page),
117 .iov_len = PAGE_SIZE,
118 };
119
120 init_sync_kiocb(&kiocb, swap_file);
121 kiocb.ki_pos = page_file_offset(page);
122 kiocb.ki_left = PAGE_SIZE;
123 kiocb.ki_nbytes = PAGE_SIZE;
124
125 unlock_page(page);
126 ret = mapping->a_ops->direct_IO(KERNEL_WRITE,
127 &kiocb, &iov,
128 kiocb.ki_pos, 1);
129 if (ret == PAGE_SIZE) {
130 count_vm_event(PSWPOUT);
131 ret = 0;
132 }
133 return ret;
134 }
135
108 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write); 136 bio = get_swap_bio(GFP_NOIO, page, end_swap_bio_write);
109 if (bio == NULL) { 137 if (bio == NULL) {
110 set_page_dirty(page); 138 set_page_dirty(page);
@@ -126,6 +154,7 @@ int swap_readpage(struct page *page)
126{ 154{
127 struct bio *bio; 155 struct bio *bio;
128 int ret = 0; 156 int ret = 0;
157 struct swap_info_struct *sis = page_swap_info(page);
129 158
130 VM_BUG_ON(!PageLocked(page)); 159 VM_BUG_ON(!PageLocked(page));
131 VM_BUG_ON(PageUptodate(page)); 160 VM_BUG_ON(PageUptodate(page));
@@ -134,6 +163,17 @@ int swap_readpage(struct page *page)
134 unlock_page(page); 163 unlock_page(page);
135 goto out; 164 goto out;
136 } 165 }
166
167 if (sis->flags & SWP_FILE) {
168 struct file *swap_file = sis->swap_file;
169 struct address_space *mapping = swap_file->f_mapping;
170
171 ret = mapping->a_ops->readpage(swap_file, page);
172 if (!ret)
173 count_vm_event(PSWPIN);
174 return ret;
175 }
176
137 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read); 177 bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
138 if (bio == NULL) { 178 if (bio == NULL) {
139 unlock_page(page); 179 unlock_page(page);
@@ -145,3 +185,15 @@ int swap_readpage(struct page *page)
145out: 185out:
146 return ret; 186 return ret;
147} 187}
188
189int swap_set_page_dirty(struct page *page)
190{
191 struct swap_info_struct *sis = page_swap_info(page);
192
193 if (sis->flags & SWP_FILE) {
194 struct address_space *mapping = sis->swap_file->f_mapping;
195 return mapping->a_ops->set_page_dirty(page);
196 } else {
197 return __set_page_dirty_no_writeback(page);
198 }
199}
diff --git a/mm/swap_state.c b/mm/swap_state.c
index c85b5590ccc..0cb36fb1f61 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -27,7 +27,7 @@
27 */ 27 */
28static const struct address_space_operations swap_aops = { 28static const struct address_space_operations swap_aops = {
29 .writepage = swap_writepage, 29 .writepage = swap_writepage,
30 .set_page_dirty = __set_page_dirty_no_writeback, 30 .set_page_dirty = swap_set_page_dirty,
31 .migratepage = migrate_page, 31 .migratepage = migrate_page,
32}; 32};
33 33
diff --git a/mm/swapfile.c b/mm/swapfile.c
index f89af5ba2eb..6ffc87602f4 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1329,6 +1329,14 @@ static void destroy_swap_extents(struct swap_info_struct *sis)
1329 list_del(&se->list); 1329 list_del(&se->list);
1330 kfree(se); 1330 kfree(se);
1331 } 1331 }
1332
1333 if (sis->flags & SWP_FILE) {
1334 struct file *swap_file = sis->swap_file;
1335 struct address_space *mapping = swap_file->f_mapping;
1336
1337 sis->flags &= ~SWP_FILE;
1338 mapping->a_ops->swap_deactivate(swap_file);
1339 }
1332} 1340}
1333 1341
1334/* 1342/*
@@ -1410,7 +1418,9 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1410 */ 1418 */
1411static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 1419static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
1412{ 1420{
1413 struct inode *inode; 1421 struct file *swap_file = sis->swap_file;
1422 struct address_space *mapping = swap_file->f_mapping;
1423 struct inode *inode = mapping->host;
1414 unsigned blocks_per_page; 1424 unsigned blocks_per_page;
1415 unsigned long page_no; 1425 unsigned long page_no;
1416 unsigned blkbits; 1426 unsigned blkbits;
@@ -1421,13 +1431,22 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
1421 int nr_extents = 0; 1431 int nr_extents = 0;
1422 int ret; 1432 int ret;
1423 1433
1424 inode = sis->swap_file->f_mapping->host;
1425 if (S_ISBLK(inode->i_mode)) { 1434 if (S_ISBLK(inode->i_mode)) {
1426 ret = add_swap_extent(sis, 0, sis->max, 0); 1435 ret = add_swap_extent(sis, 0, sis->max, 0);
1427 *span = sis->pages; 1436 *span = sis->pages;
1428 goto out; 1437 goto out;
1429 } 1438 }
1430 1439
1440 if (mapping->a_ops->swap_activate) {
1441 ret = mapping->a_ops->swap_activate(swap_file);
1442 if (!ret) {
1443 sis->flags |= SWP_FILE;
1444 ret = add_swap_extent(sis, 0, sis->max, 0);
1445 *span = sis->pages;
1446 }
1447 goto out;
1448 }
1449
1431 blkbits = inode->i_blkbits; 1450 blkbits = inode->i_blkbits;
1432 blocks_per_page = PAGE_SIZE >> blkbits; 1451 blocks_per_page = PAGE_SIZE >> blkbits;
1433 1452