mm/swap: add a new swap_ops.h header to allow for pluggable swap ops

Add a new header to declare the swap_iocb, swap_ops and swap_ctx to allow
for swap_ops implementations outside of mm/page_io.c.  This will be used
to remove the double indirection for file system-based swap.  There is no
functional change, just a move of the declarations.

Note that there already is a swapops.h header, which is totally unrelated
to struct swap_ops.  The close naming is a bit unfortunate, but I could
not think of a better name for this header.

Link: https://lore.kernel.org/20260723054622.3460249-3-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-07-23 07:46:05 +02:00
committed by Andrew Morton
parent 3c5194eb2e
commit 52d85ca90c
9 changed files with 47 additions and 32 deletions

View File

@@ -17242,6 +17242,7 @@ S: Maintained
F: Documentation/ABI/testing/sysfs-kernel-mm-swap
F: Documentation/mm/swap-table.rst
F: include/linux/swap.h
F: include/linux/swap_ops.h
F: include/linux/swapfile.h
F: include/linux/swapops.h
F: mm/page_io.c

39
include/linux/swap_ops.h Normal file
View File

@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _MM_SWAP_OPS_H
#define _MM_SWAP_OPS_H
#include <linux/swap.h> /* for SWAP_CLUSTER_MAX */
struct swap_iocb {
union {
struct kiocb iocb;
struct bio bio;
};
struct bio_vec bvecs[SWAP_CLUSTER_MAX];
int nr_bvecs;
int len;
};
struct swap_io_ctx {
struct swap_iocb *sio;
struct swap_info_struct *sis;
};
/*
* SWAP_OPS_F_REQUIRE_NOFS:
* When set, all reclaim operations must operated as GFS_NOFS and not
* just GFP_NOIO, as GFP_NOIO allocations could recourse into the
* file system backing this swap file.
*/
#define SWAP_OPS_F_REQUIRE_NOFS (1U << 0)
struct swap_ops {
unsigned int flags;
bool (*can_merge)(struct folio *folio, struct folio *prev_folio,
size_t prev_folio_size, int rw);
void (*submit_write)(struct swap_io_ctx *ctx);
void (*submit_read)(struct swap_io_ctx *ctx);
};
#endif /* _MM_SWAP_OPS_H */

View File

@@ -32,6 +32,7 @@
#include <linux/leafops.h>
#include <linux/shmem_fs.h>
#include <linux/mmu_notifier.h>
#include <linux/swap_ops.h>
#include <asm/tlb.h>

View File

@@ -25,6 +25,7 @@
#include <linux/sched/task.h>
#include <linux/delayacct.h>
#include <linux/zswap.h>
#include <linux/swap_ops.h>
#include "swap.h"
#include "swap_table.h"
@@ -300,15 +301,6 @@ static bool folio_blkg_can_merge(struct folio *folio, struct folio *prev_folio)
#define bio_associate_blkg_from_page(bio, folio) do { } while (0)
#endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */
struct swap_iocb {
union {
struct kiocb iocb;
struct bio bio;
};
struct bio_vec bvecs[SWAP_CLUSTER_MAX];
int nr_bvecs;
int len;
};
static mempool_t *sio_pool;
int sio_pool_init(void)

View File

@@ -41,6 +41,7 @@
#include <linux/swapfile.h>
#include <linux/iversion.h>
#include <linux/unicode.h>
#include <linux/swap_ops.h>
#include "swap.h"
static struct vfsmount *shm_mnt __ro_after_init;

View File

@@ -10,6 +10,7 @@
struct mempolicy;
struct swap_iocb;
struct swap_memcg_table;
struct swap_io_ctx;
#if defined(MAX_POSSIBLE_PHYSMEM_BITS)
#define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
@@ -91,28 +92,6 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
return READ_ONCE(vm_swappiness);
}
struct swap_io_ctx {
struct swap_iocb *sio;
struct swap_info_struct *sis;
};
/*
* SWAP_OPS_F_REQUIRE_NOFS:
* When set, all reclaim operations must operated as GFS_NOFS and not
* just GFP_NOIO, as GFP_NOIO allocations could recourse into the
* file system backing this swap file.
*/
#define SWAP_OPS_F_REQUIRE_NOFS (1U << 0)
struct swap_ops {
unsigned int flags;
bool (*can_merge)(struct folio *folio, struct folio *prev_folio,
size_t prev_folio_size, int rw);
void (*submit_write)(struct swap_io_ctx *ctx);
void (*submit_read)(struct swap_io_ctx *ctx);
};
#ifdef CONFIG_SWAP
#include <linux/swapops.h> /* for swp_offset */
#include <linux/blk_types.h> /* for bio_end_io_t */

View File

@@ -23,6 +23,7 @@
#include <linux/huge_mm.h>
#include <linux/shmem_fs.h>
#include <linux/sysctl.h>
#include <linux/swap_ops.h>
#include "internal.h"
#include "swap_table.h"
#include "swap.h"

View File

@@ -58,6 +58,7 @@
#include <linux/random.h>
#include <linux/mmu_notifier.h>
#include <linux/parser.h>
#include <linux/swap_ops.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>

View File

@@ -20,7 +20,7 @@
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/swap.h>
#include <linux/swap_ops.h>
#include <linux/crypto.h>
#include <linux/scatterlist.h>
#include <linux/mempolicy.h>