Skip to content

Commit 7eadabc

Browse files
neilbrownakpm00
authored andcommitted
mm: perform async writes to SWP_FS_OPS swap-space using ->swap_rw
This patch switches swap-out to SWP_FS_OPS swap-spaces to use ->swap_rw and makes the writes asynchronous, like they are for other swap spaces. To make it async we need to allocate the kiocb struct from a mempool. This may block, but won't block as long as waiting for the write to complete. At most it will wait for some previous swap IO to complete. Link: https://lkml.kernel.org/r/164859778126.29473.12399585304843922231.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 eb79f3a commit 7eadabc

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

mm/page_io.c

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,57 @@ int sio_pool_init(void)
254254
return 0;
255255
}
256256

257+
static void sio_write_complete(struct kiocb *iocb, long ret)
258+
{
259+
struct swap_iocb *sio = container_of(iocb, struct swap_iocb, iocb);
260+
struct page *page = sio->bvec.bv_page;
261+
262+
if (ret != PAGE_SIZE) {
263+
/*
264+
* In the case of swap-over-nfs, this can be a
265+
* temporary failure if the system has limited
266+
* memory for allocating transmit buffers.
267+
* Mark the page dirty and avoid
268+
* folio_rotate_reclaimable but rate-limit the
269+
* messages but do not flag PageError like
270+
* the normal direct-to-bio case as it could
271+
* be temporary.
272+
*/
273+
set_page_dirty(page);
274+
ClearPageReclaim(page);
275+
pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
276+
ret, page_file_offset(page));
277+
} else
278+
count_vm_event(PSWPOUT);
279+
end_page_writeback(page);
280+
mempool_free(sio, sio_pool);
281+
}
282+
283+
static int swap_writepage_fs(struct page *page, struct writeback_control *wbc)
284+
{
285+
struct swap_iocb *sio;
286+
struct swap_info_struct *sis = page_swap_info(page);
287+
struct file *swap_file = sis->swap_file;
288+
struct address_space *mapping = swap_file->f_mapping;
289+
struct iov_iter from;
290+
int ret;
291+
292+
set_page_writeback(page);
293+
unlock_page(page);
294+
sio = mempool_alloc(sio_pool, GFP_NOIO);
295+
init_sync_kiocb(&sio->iocb, swap_file);
296+
sio->iocb.ki_complete = sio_write_complete;
297+
sio->iocb.ki_pos = page_file_offset(page);
298+
sio->bvec.bv_page = page;
299+
sio->bvec.bv_len = PAGE_SIZE;
300+
sio->bvec.bv_offset = 0;
301+
iov_iter_bvec(&from, WRITE, &sio->bvec, 1, PAGE_SIZE);
302+
ret = mapping->a_ops->swap_rw(&sio->iocb, &from);
303+
if (ret != -EIOCBQUEUED)
304+
sio_write_complete(&sio->iocb, ret);
305+
return ret;
306+
}
307+
257308
int __swap_writepage(struct page *page, struct writeback_control *wbc,
258309
bio_end_io_t end_write_func)
259310
{
@@ -262,46 +313,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
262313
struct swap_info_struct *sis = page_swap_info(page);
263314

264315
VM_BUG_ON_PAGE(!PageSwapCache(page), page);
265-
if (data_race(sis->flags & SWP_FS_OPS)) {
266-
struct kiocb kiocb;
267-
struct file *swap_file = sis->swap_file;
268-
struct address_space *mapping = swap_file->f_mapping;
269-
struct bio_vec bv = {
270-
.bv_page = page,
271-
.bv_len = PAGE_SIZE,
272-
.bv_offset = 0
273-
};
274-
struct iov_iter from;
275-
276-
iov_iter_bvec(&from, WRITE, &bv, 1, PAGE_SIZE);
277-
init_sync_kiocb(&kiocb, swap_file);
278-
kiocb.ki_pos = page_file_offset(page);
279-
280-
set_page_writeback(page);
281-
unlock_page(page);
282-
ret = mapping->a_ops->direct_IO(&kiocb, &from);
283-
if (ret == PAGE_SIZE) {
284-
count_vm_event(PSWPOUT);
285-
ret = 0;
286-
} else {
287-
/*
288-
* In the case of swap-over-nfs, this can be a
289-
* temporary failure if the system has limited
290-
* memory for allocating transmit buffers.
291-
* Mark the page dirty and avoid
292-
* folio_rotate_reclaimable but rate-limit the
293-
* messages but do not flag PageError like
294-
* the normal direct-to-bio case as it could
295-
* be temporary.
296-
*/
297-
set_page_dirty(page);
298-
ClearPageReclaim(page);
299-
pr_err_ratelimited("Write error on dio swapfile (%llu)\n",
300-
page_file_offset(page));
301-
}
302-
end_page_writeback(page);
303-
return ret;
304-
}
316+
/*
317+
* ->flags can be updated non-atomicially (scan_swap_map_slots),
318+
* but that will never affect SWP_FS_OPS, so the data_race
319+
* is safe.
320+
*/
321+
if (data_race(sis->flags & SWP_FS_OPS))
322+
return swap_writepage_fs(page, wbc);
305323

306324
ret = bdev_write_page(sis->bdev, swap_page_sector(page), page, wbc);
307325
if (!ret) {

0 commit comments

Comments
 (0)