Skip to content

Commit 2282679

Browse files
neilbrownakpm00
authored andcommitted
mm: submit multipage write for SWP_FS_OPS swap-space
swap_writepage() is given one page at a time, but may be called repeatedly in succession. For block-device swapspace, the blk_plug functionality allows the multiple pages to be combined together at lower layers. That cannot be used for SWP_FS_OPS as blk_plug may not exist - it is only active when CONFIG_BLOCK=y. Consequently all swap reads over NFS are single page reads. With this patch we pass a pointer-to-pointer via the wbc. swap_writepage can store state between calls - much like the pointer passed explicitly to swap_readpage. After calling swap_writepage() some number of times, the state will be passed to swap_write_unplug() which can submit the combined request. Link: https://lkml.kernel.org/r/164859778128.29473.5191868522654408537.stgit@noble.brown Signed-off-by: NeilBrown <neilb@suse.de> Reviewed-by: Christoph Hellwig <hch@lst.de> Tested-by: David Howells <dhowells@redhat.com> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Cc: Hugh Dickins <hughd@google.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Trond Myklebust <trond.myklebust@hammerspace.com> Cc: Miaohe Lin <linmiaohe@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 5169b84 commit 2282679

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

include/linux/writeback.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ struct writeback_control {
8080

8181
unsigned punt_to_cgroup:1; /* cgrp punting, see __REQ_CGROUP_PUNT */
8282

83+
/* To enable batching of swap writes to non-block-device backends,
84+
* "plug" can be set point to a 'struct swap_iocb *'. When all swap
85+
* writes have been submitted, if with swap_iocb is not NULL,
86+
* swap_write_unplug() should be called.
87+
*/
88+
struct swap_iocb **swap_plug;
89+
8390
#ifdef CONFIG_CGROUP_WRITEBACK
8491
struct bdi_writeback *wb; /* wb this writeback is issued under */
8592
struct inode *inode; /* inode being written out */

mm/page_io.c

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
259259
{
260260
struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
261261
struct page *page = sio->bvec[0].bv_page;
262+
int p;
262263

263-
if (ret != PAGE_SIZE) {
264+
if (ret != PAGE_SIZE * sio->pages) {
264265
/*
265266
* In the case of swap-over-nfs, this can be a
266267
* temporary failure if the system has limited
@@ -271,43 +272,63 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
271272
* the normal direct-to-bio case as it could
272273
* be temporary.
273274
*/
274-
set_page_dirty(page);
275-
ClearPageReclaim(page);
276275
pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
277276
ret, page_file_offset(page));
277+
for (p = 0; p < sio->pages; p++) {
278+
page = sio->bvec[p].bv_page;
279+
set_page_dirty(page);
280+
ClearPageReclaim(page);
281+
}
278282
} else
279-
count_vm_event(PSWPOUT);
280-
end_page_writeback(page);
283+
count_vm_events(PSWPOUT, sio->pages);
284+
285+
for (p = 0; p < sio->pages; p++)
286+
end_page_writeback(sio->bvec[p].bv_page);
287+
281288
mempool_free(sio, sio_pool);
282289
}
283290

284291
static int swap_writepage_fs(struct page *page, struct writeback_control *wbc)
285292
{
286-
struct swap_iocb *sio;
293+
struct swap_iocb *sio = NULL;
287294
struct swap_info_struct *sis = page_swap_info(page);
288295
struct file *swap_file = sis->swap_file;
289-
struct address_space *mapping = swap_file->f_mapping;
290-
struct iov_iter from;
291-
int ret;
296+
loff_t pos = page_file_offset(page);
292297

293298
set_page_writeback(page);
294299
unlock_page(page);
295-
sio = mempool_alloc(sio_pool, GFP_NOIO);
296-
init_sync_kiocb(&sio->iocb, swap_file);
297-
sio->iocb.ki_complete = sio_write_complete;
298-
sio->iocb.ki_pos = page_file_offset(page);
299-
sio->bvec[0].bv_page = page;
300-
sio->bvec[0].bv_len = PAGE_SIZE;
301-
sio->bvec[0].bv_offset = 0;
302-
iov_iter_bvec(&from, WRITE, &sio->bvec[0], 1, PAGE_SIZE);
303-
ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
304-
if (ret != -EIOCBQUEUED)
305-
sio_write_complete(&sio->iocb, ret);
306-
return ret;
300+
if (wbc->swap_plug)
301+
sio = *wbc->swap_plug;
302+
if (sio) {
303+
if (sio->iocb.ki_filp != swap_file ||
304+
sio->iocb.ki_pos + sio->pages * PAGE_SIZE != pos) {
305+
swap_write_unplug(sio);
306+
sio = NULL;
307+
}
308+
}
309+
if (!sio) {
310+
sio = mempool_alloc(sio_pool, GFP_NOIO);
311+
init_sync_kiocb(&sio->iocb, swap_file);
312+
sio->iocb.ki_complete = sio_write_complete;
313+
sio->iocb.ki_pos = pos;
314+
sio->pages = 0;
315+
}
316+
sio->bvec[sio->pages].bv_page = page;
317+
sio->bvec[sio->pages].bv_len = PAGE_SIZE;
318+
sio->bvec[sio->pages].bv_offset = 0;
319+
sio->pages += 1;
320+
if (sio->pages == ARRAY_SIZE(sio->bvec) || !wbc->swap_plug) {
321+
swap_write_unplug(sio);
322+
sio = NULL;
323+
}
324+
if (wbc->swap_plug)
325+
*wbc->swap_plug = sio;
326+
327+
return 0;
307328
}
308329

309330
int __swap_writepage(struct page *page, struct writeback_control *wbc,
310-
bio_end_io_t end_write_func)
331+
bio_end_io_t end_write_func)
311332
{
312333
struct bio *bio;
313334
int ret;
@@ -344,6 +365,19 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
344365
return 0;
345366
}
346367

368+
void swap_write_unplug(struct swap_iocb *sio)
369+
{
370+
struct iov_iter from;
371+
struct address_space *mapping = sio->iocb.ki_filp->f_mapping;
372+
int ret;
373+
374+
iov_iter_bvec(&from, WRITE, sio->bvec, sio->pages,
375+
PAGE_SIZE * sio->pages);
376+
ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
377+
if (ret != -EIOCBQUEUED)
378+
sio_write_complete(&sio->iocb, ret);
379+
}
380+
347381
static void sio_read_complete(struct kiocb *iocb, long ret)
348382
{
349383
struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);

mm/swap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
1616
if (unlikely(plug))
1717
__swap_read_unplug(plug);
1818
}
19+
void swap_write_unplug(struct swap_iocb *sio);
1920
int swap_writepage(struct page *page, struct writeback_control *wbc);
2021
void end_swap_bio_write(struct bio *bio);
2122
int __swap_writepage(struct page *page, struct writeback_control *wbc,
@@ -71,6 +72,9 @@ static inline int swap_readpage(struct page *page, bool do_poll,
7172
{
7273
return 0;
7374
}
75+
static inline void swap_write_unplug(struct swap_iocb *sio)
76+
{
77+
}
7478

7579
static inline struct address_space *swap_address_space(swp_entry_t entry)
7680
{

mm/vmscan.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,8 @@ typedef enum {
11561156
* pageout is called by shrink_page_list() for each dirty page.
11571157
* Calls ->writepage().
11581158
*/
1159-
static pageout_t pageout(struct folio *folio, struct address_space *mapping)
1159+
static pageout_t pageout(struct folio *folio, struct address_space *mapping,
1160+
struct swap_iocb **plug)
11601161
{
11611162
/*
11621163
* If the folio is dirty, only perform writeback if that write
@@ -1201,6 +1202,7 @@ static pageout_t pageout(struct folio *folio, struct address_space *mapping)
12011202
.range_start = 0,
12021203
.range_end = LLONG_MAX,
12031204
.for_reclaim = 1,
1205+
.swap_plug = plug,
12041206
};
12051207

12061208
folio_set_reclaim(folio);
@@ -1533,6 +1535,7 @@ static unsigned int shrink_page_list(struct list_head *page_list,
15331535
unsigned int nr_reclaimed = 0;
15341536
unsigned int pgactivate = 0;
15351537
bool do_demote_pass;
1538+
struct swap_iocb *plug = NULL;
15361539

15371540
memset(stat, 0, sizeof(*stat));
15381541
cond_resched();
@@ -1814,7 +1817,7 @@ static unsigned int shrink_page_list(struct list_head *page_list,
18141817
* starts and then write it out here.
18151818
*/
18161819
try_to_unmap_flush_dirty();
1817-
switch (pageout(folio, mapping)) {
1820+
switch (pageout(folio, mapping, &plug)) {
18181821
case PAGE_KEEP:
18191822
goto keep_locked;
18201823
case PAGE_ACTIVATE:
@@ -1968,6 +1971,8 @@ static unsigned int shrink_page_list(struct list_head *page_list,
19681971
list_splice(&ret_pages, page_list);
19691972
count_vm_events(PGACTIVATE, pgactivate);
19701973

1974+
if (plug)
1975+
swap_write_unplug(plug);
19711976
return nr_reclaimed;
19721977
}
19731978

0 commit comments

Comments
 (0)