mm/swap: move swap_ops into file systems for file system-based swap

Currently swap to and from file systems goes through two indirect calls
between the swap ops and the swap_rw method.  Reduce this by directly
providing the swap_ops from the file system.

For this refactor swap_fs_submit into a swap_fs_prepare_rw helper that
initializes the iov_iter on the callers stack so that file systems can
call it directly, and use that to initialize file system specific ops in
the NFS and SMB clients, which then get passed to swap_fs_activate.

Link: https://lore.kernel.org/20260723054622.3460249-4-hch@lst.de
Signed-off-by: Christoph Hellwig <hch@lst.de>
Acked-by: Chris Li <chrisl@kernel.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Kairui Song <kasong@tencent.com>
Cc: Kairui Song <ryncsn@gmail.com>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Steve French <sfrench@samba.org>
Cc: Usama Arif <usama.arif@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Christoph Hellwig
2026-08-24 18:43:20 -07:00
committed by Andrew Morton
parent 52d85ca90c
commit 22779ae817
10 changed files with 93 additions and 96 deletions
+7 -27
View File
@@ -650,11 +650,9 @@ const struct swap_ops swap_bdev_ops = {
.can_merge = swap_bdev_can_merge,
};
static void swap_fs_submit(struct swap_io_ctx *ctx, int rw)
void swap_fs_prepare_rw(struct swap_io_ctx *ctx, int rw, struct iov_iter *iter)
{
struct swap_iocb *sio = ctx->sio;
struct iov_iter iter;
int ret;
init_sync_kiocb(&sio->iocb, ctx->sis->swap_file);
sio->iocb.ki_pos = swap_dev_pos(bvec_folio(&sio->bvecs[0])->swap);
@@ -663,40 +661,22 @@ static void swap_fs_submit(struct swap_io_ctx *ctx, int rw)
else
sio->iocb.ki_complete = swap_fs_read_complete;
iov_iter_bvec(&iter, rw == WRITE ? ITER_SOURCE : ITER_DEST,
iov_iter_bvec(iter, rw == WRITE ? ITER_SOURCE : ITER_DEST,
sio->bvecs, sio->nr_bvecs, sio->len);
ret = sio->iocb.ki_filp->f_mapping->a_ops->swap_rw(&sio->iocb, &iter);
if (ret != -EIOCBQUEUED)
sio->iocb.ki_complete(&sio->iocb, ret);
}
EXPORT_SYMBOL_GPL(swap_fs_prepare_rw);
static void swap_fs_submit_write(struct swap_io_ctx *ctx)
{
swap_fs_submit(ctx, WRITE);
}
static void swap_fs_submit_read(struct swap_io_ctx *ctx)
{
swap_fs_submit(ctx, READ);
}
static bool swap_fs_can_merge(struct folio *folio, struct folio *prev_folio,
bool swap_fs_can_merge(struct folio *folio, struct folio *prev_folio,
size_t prev_folio_size, int rw)
{
return swap_dev_pos(folio->swap) ==
swap_dev_pos(prev_folio->swap) + prev_folio_size;
}
EXPORT_SYMBOL_GPL(swap_fs_can_merge);
static const struct swap_ops swap_fs_ops = {
.flags = SWAP_OPS_F_REQUIRE_NOFS,
.submit_write = swap_fs_submit_write,
.submit_read = swap_fs_submit_read,
.can_merge = swap_fs_can_merge,
};
int swap_fs_activate(struct swap_info_struct *sis)
int swap_fs_activate(struct swap_info_struct *sis, const struct swap_ops *ops)
{
sis->ops = &swap_fs_ops;
sis->ops = ops;
return add_swap_extent(sis, 0, sis->max, 0);
}
EXPORT_SYMBOL_GPL(swap_fs_activate);