From nobody Fri Jun 12 12:43:35 2026 Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 443E92DEA7B for ; Fri, 15 May 2026 01:57:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810277; cv=none; b=LXzxH+sm4JJboHfMayqT1An+bkTJ/KQIuBulTrCIEXzKTDgl9fCn/gPOFqhiaj2cYFkTPcSIO3XTshf/cIENGAHziVBOmLn2XlINMYu5bdfsyjiL0cpV1l33wzp1DKFJOjgD42yU+dunL5/ksTRDJBl0lfhXtGP2Yusk+TS/Ws4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810277; c=relaxed/simple; bh=7AvDrNyupV4/wMIJNhNUfuHKlNSILUUPu2lPvIsLAl4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=bwCFIU5Fe/sMUfyydmQyc5Cq4D/F6LVh3OW+1na8td0V+uLCmPRPi3hqbs3ump5wiVyhOUPJRe7wAj+N0x//GHAw8K5TiDBkAn7/LwBas8vFrR78BX+FLJXYvYT4PZBzA9w8k98NhJxjHPyijnOP+9CCEmv9Ut6wXUhU00Os1U8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=xw9e8+1v; arc=none smtp.client-ip=91.218.175.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="xw9e8+1v" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1778810273; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wEsHsxQav2TvnbIZZsTRBwS8toLmw21bJKCaqNlP5NQ=; b=xw9e8+1vwCgkcaae38RYCgz4aaH0mPkRcN82TT0yXf7CDFlxh69Of5vRpac4PmZ6qCQUZF L4ioAws5aTUT9J81QAwJvFpyuIammi0RsbYVBHFxARl9yWbBVEW7Db8YUV3nzpg99KndVg UDFUy7uw0x3uNmHLFVIZy9jLiGwFXaE= From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, chrisl@kernel.org, usama.arif@linux.dev, baohua@kernel.org, kasong@tencent.com, nphamcs@gmail.com, shikemeng@huaweicloud.com, youngjun.park@lge.com, hch@infradead.org, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v7 1/3] mm/swap: use swap_ops to register swap device's methods Date: Fri, 15 May 2026 09:57:35 +0800 Message-ID: <20260515015737.890994-2-baoquan.he@linux.dev> In-Reply-To: <20260515015737.890994-1-baoquan.he@linux.dev> References: <20260515015737.890994-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle. Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate operations adapted into swap_ops. Suggested-by: Chris Li Signed-off-by: Baoquan He --- include/linux/swap.h | 3 ++ mm/page_io.c | 104 +++++++++++++++++++++++++------------------ mm/swap.h | 10 ++++- mm/swapfile.c | 9 ++++ mm/zswap.c | 2 +- 5 files changed, 83 insertions(+), 45 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 7a09df6977a5..0d045fc8ec35 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -20,6 +20,8 @@ struct notifier_block; =20 struct bio; =20 +struct swap_ops; + #define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */ #define SWAP_FLAG_PRIO_MASK 0x7fff #define SWAP_FLAG_DISCARD 0x10000 /* enable discard for swap */ @@ -282,6 +284,7 @@ struct swap_info_struct { struct work_struct reclaim_work; /* reclaim worker */ struct list_head discard_clusters; /* discard clusters list */ struct plist_node avail_list; /* entry in swap_avail_head */ + const struct swap_ops *ops; }; =20 static inline swp_entry_t page_swap_entry(struct page *page) diff --git a/mm/page_io.c b/mm/page_io.c index 70cea9e24d2f..2b520af88376 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -240,6 +240,9 @@ static void swap_zeromap_folio_clear(struct folio *foli= o) int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) { int ret =3D 0; + struct swap_info_struct *sis =3D __swap_entry_to_info(folio->swap); + + VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio); =20 if (folio_free_swap(folio)) goto out_unlock; @@ -285,7 +288,7 @@ int swap_writeout(struct folio *folio, struct swap_iocb= **swap_plug) } rcu_read_unlock(); =20 - __swap_writepage(folio, swap_plug); + sis->ops->write_folio(sis, folio, swap_plug); return 0; out_unlock: folio_unlock(folio); @@ -375,10 +378,11 @@ static void sio_write_complete(struct kiocb *iocb, lo= ng ret) mempool_free(sio, sio_pool); } =20 -static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap= _plug) +static void swap_writepage_fs(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **swap_plug) { struct swap_iocb *sio =3D swap_plug ? *swap_plug : NULL; - struct swap_info_struct *sis =3D __swap_entry_to_info(folio->swap); struct file *swap_file =3D sis->swap_file; loff_t pos =3D swap_dev_pos(folio->swap); =20 @@ -411,8 +415,9 @@ static void swap_writepage_fs(struct folio *folio, stru= ct swap_iocb **swap_plug) *swap_plug =3D sio; } =20 -static void swap_writepage_bdev_sync(struct folio *folio, - struct swap_info_struct *sis) +static void swap_writepage_bdev_sync(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug) { struct bio_vec bv; struct bio bio; @@ -431,8 +436,9 @@ static void swap_writepage_bdev_sync(struct folio *foli= o, __end_swap_bio_write(&bio); } =20 -static void swap_writepage_bdev_async(struct folio *folio, - struct swap_info_struct *sis) +static void swap_writepage_bdev_async(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug) { struct bio *bio; =20 @@ -448,29 +454,6 @@ static void swap_writepage_bdev_async(struct folio *fo= lio, submit_bio(bio); } =20 -void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug) -{ - struct swap_info_struct *sis =3D __swap_entry_to_info(folio->swap); - - VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio); - /* - * ->flags can be updated non-atomically, - * but that will never affect SWP_FS_OPS, so the data_race - * is safe. - */ - if (data_race(sis->flags & SWP_FS_OPS)) - swap_writepage_fs(folio, swap_plug); - /* - * ->flags can be updated non-atomically, - * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race - * is safe. - */ - else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO)) - swap_writepage_bdev_sync(folio, sis); - else - swap_writepage_bdev_async(folio, sis); -} - void swap_write_unplug(struct swap_iocb *sio) { struct iov_iter from; @@ -539,9 +522,10 @@ static bool swap_read_folio_zeromap(struct folio *foli= o) return true; } =20 -static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plu= g) +static void swap_read_folio_fs(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug) { - struct swap_info_struct *sis =3D __swap_entry_to_info(folio->swap); struct swap_iocb *sio =3D NULL; loff_t pos =3D swap_dev_pos(folio->swap); =20 @@ -573,8 +557,9 @@ static void swap_read_folio_fs(struct folio *folio, str= uct swap_iocb **plug) *plug =3D sio; } =20 -static void swap_read_folio_bdev_sync(struct folio *folio, - struct swap_info_struct *sis) +static void swap_read_folio_bdev_sync(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug) { struct bio_vec bv; struct bio bio; @@ -595,8 +580,9 @@ static void swap_read_folio_bdev_sync(struct folio *fol= io, put_task_struct(current); } =20 -static void swap_read_folio_bdev_async(struct folio *folio, - struct swap_info_struct *sis) +static void swap_read_folio_bdev_async(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug) { struct bio *bio; =20 @@ -610,6 +596,44 @@ static void swap_read_folio_bdev_async(struct folio *f= olio, submit_bio(bio); } =20 +static const struct swap_ops bdev_fs_swap_ops =3D { + .read_folio =3D swap_read_folio_fs, + .write_folio =3D swap_writepage_fs, +}; + +static const struct swap_ops bdev_sync_swap_ops =3D { + .read_folio =3D swap_read_folio_bdev_sync, + .write_folio =3D swap_writepage_bdev_sync, +}; + +static const struct swap_ops bdev_async_swap_ops =3D { + .read_folio =3D swap_read_folio_bdev_async, + .write_folio =3D swap_writepage_bdev_async, +}; + +int init_swap_ops(struct swap_info_struct *sis) +{ + /* + * ->flags can be updated non-atomically, but that will + * never affect SWP_FS_OPS, so the data_race is safe. + */ + if (data_race(sis->flags & SWP_FS_OPS)) + sis->ops =3D &bdev_fs_swap_ops; + /* + * ->flags can be updated non-atomically, but that will + * never affect SWP_SYNCHRONOUS_IO, so the data_race is safe. + */ + else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO)) + sis->ops =3D &bdev_sync_swap_ops; + else + sis->ops =3D &bdev_async_swap_ops; + + if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio) + return -EINVAL; + + return 0; +} + void swap_read_folio(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis =3D __swap_entry_to_info(folio->swap); @@ -644,13 +668,7 @@ void swap_read_folio(struct folio *folio, struct swap_= iocb **plug) /* We have to read from slower devices. Increase zswap protection. */ zswap_folio_swapin(folio); =20 - if (data_race(sis->flags & SWP_FS_OPS)) { - swap_read_folio_fs(folio, plug); - } else if (synchronous) { - swap_read_folio_bdev_sync(folio, sis); - } else { - swap_read_folio_bdev_async(folio, sis); - } + sis->ops->read_folio(sis, folio, plug); =20 finish: if (workingset) { diff --git a/mm/swap.h b/mm/swap.h index a77016f2423b..8d4375f91632 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -217,6 +217,15 @@ extern void __swap_cluster_free_entries(struct swap_in= fo_struct *si, /* linux/mm/page_io.c */ int sio_pool_init(void); struct swap_iocb; +struct swap_ops { + void (*read_folio)(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug); + void (*write_folio)(struct swap_info_struct *sis, + struct folio *folio, + struct swap_iocb **plug); +}; +int init_swap_ops(struct swap_info_struct *sis); void swap_read_folio(struct folio *folio, struct swap_iocb **plug); void __swap_read_unplug(struct swap_iocb *plug); static inline void swap_read_unplug(struct swap_iocb *plug) @@ -226,7 +235,6 @@ static inline void swap_read_unplug(struct swap_iocb *p= lug) } void swap_write_unplug(struct swap_iocb *sio); int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug); -void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug); =20 /* linux/mm/swap_state.c */ extern struct address_space swap_space __read_mostly; diff --git a/mm/swapfile.c b/mm/swapfile.c index 9174f1eeffb0..29ae79d0fa2e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3597,6 +3597,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) } } =20 + /* + * init_swap_ops() sets si->ops based on flags. It does not need + * swapon_mutex, and must complete before enable_swap_info() + * exposes the device. + */ + error =3D init_swap_ops(si); + if (error) + goto bad_swap_unlock_inode; + error =3D zswap_swapon(si->type, maxpages); if (error) goto bad_swap_unlock_inode; diff --git a/mm/zswap.c b/mm/zswap.c index 4b5149173b0e..192401f46de4 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1054,7 +1054,7 @@ static int zswap_writeback_entry(struct zswap_entry *= entry, folio_set_reclaim(folio); =20 /* start writeback */ - __swap_writepage(folio, NULL); + si->ops->write_folio(si, folio, NULL); =20 out: if (ret && ret !=3D -EEXIST) { --=20 2.52.0 From nobody Fri Jun 12 12:43:35 2026 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 204B830C37A for ; Fri, 15 May 2026 01:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810286; cv=none; b=u9sIs3ngzhDc39F9KmfwMhfmKP1JpA0eY0yZ3p1XXVFSwowgh9A9jVXYmUCb6qVGyGIXfcIlwLH8Hcdvp+vd8jwfZkTnCHFQTpbpGtrEzsQfQxrRv9mQkF4S6fnWeUPFsx3eBabCkzUG7t+q18PTpFAUYrjVfkEbvAMV2Fm6M2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810286; c=relaxed/simple; bh=GHVHK47vcjftNsYHtIsLimJpXemvPgvET3XCkltt6fc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=PDrOyU49PA6Iz82Fyp9DKaGVMj6WoyQVc7fb/utyv9b+OEgkRF1Nt/hxVIk23cfcFKFM44PymuKFaWE7aWqQwS6jjL8cbf8BpiPvx2AIsIpTRSIGtQESfIafHPeFoIwSlrFIf7hzVIZyg7N077QhzwrCOtI525teGzIIKI3bMUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=XmcUqg9r; arc=none smtp.client-ip=95.215.58.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="XmcUqg9r" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1778810283; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ma9rC7Uc9wawJfY5OVvtdjq6Z8aOUOjvo79XUTZ+mxA=; b=XmcUqg9roSeW6RY6JXgXoMBP6xs0eeXvWpov4g/feuF2HLPZdr1oWpdJeTKJxHu7JGQ237 gL0CPCRKABbmkCMSY81i/N/j1/IMCe5iVJsN40dOGECxgqrhcoKQGk4FRCYkM6vqU2oOYl qoJFWt8o8eABbtVI4WIkZNFIzZKtxUg= From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, chrisl@kernel.org, usama.arif@linux.dev, baohua@kernel.org, kasong@tencent.com, nphamcs@gmail.com, shikemeng@huaweicloud.com, youngjun.park@lge.com, hch@infradead.org, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v7 2/3] mm/page_io.c: rename page_io functions to consistent naming Date: Fri, 15 May 2026 09:57:36 +0800 Message-ID: <20260515015737.890994-3-baoquan.he@linux.dev> In-Reply-To: <20260515015737.890994-1-baoquan.he@linux.dev> References: <20260515015737.890994-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Rename the swap I/O functions to use a consistent swap___ naming scheme, so the backend type immediately follows the swap_ prefix. The new names align with swap_ops callbacks: .write_folio and .read_folio. swap_writepage_fs -> swap_fs_write_folio swap_writepage_bdev_sync -> swap_bdev_sync_write_folio swap_writepage_bdev_async -> swap_bdev_async_write_folio swap_read_folio_fs -> swap_fs_read_folio swap_read_folio_bdev_sync -> swap_bdev_sync_read_folio swap_read_folio_bdev_async -> swap_bdev_async_read_folio Signed-off-by: Baoquan He --- mm/page_io.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index 2b520af88376..38b94c560c37 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -378,7 +378,7 @@ static void sio_write_complete(struct kiocb *iocb, long= ret) mempool_free(sio, sio_pool); } =20 -static void swap_writepage_fs(struct swap_info_struct *sis, +static void swap_fs_write_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **swap_plug) { @@ -415,7 +415,7 @@ static void swap_writepage_fs(struct swap_info_struct *= sis, *swap_plug =3D sio; } =20 -static void swap_writepage_bdev_sync(struct swap_info_struct *sis, +static void swap_bdev_sync_write_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug) { @@ -436,7 +436,7 @@ static void swap_writepage_bdev_sync(struct swap_info_s= truct *sis, __end_swap_bio_write(&bio); } =20 -static void swap_writepage_bdev_async(struct swap_info_struct *sis, +static void swap_bdev_async_write_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug) { @@ -522,7 +522,7 @@ static bool swap_read_folio_zeromap(struct folio *folio) return true; } =20 -static void swap_read_folio_fs(struct swap_info_struct *sis, +static void swap_fs_read_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug) { @@ -557,7 +557,7 @@ static void swap_read_folio_fs(struct swap_info_struct = *sis, *plug =3D sio; } =20 -static void swap_read_folio_bdev_sync(struct swap_info_struct *sis, +static void swap_bdev_sync_read_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug) { @@ -580,7 +580,7 @@ static void swap_read_folio_bdev_sync(struct swap_info_= struct *sis, put_task_struct(current); } =20 -static void swap_read_folio_bdev_async(struct swap_info_struct *sis, +static void swap_bdev_async_read_folio(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug) { @@ -597,18 +597,18 @@ static void swap_read_folio_bdev_async(struct swap_in= fo_struct *sis, } =20 static const struct swap_ops bdev_fs_swap_ops =3D { - .read_folio =3D swap_read_folio_fs, - .write_folio =3D swap_writepage_fs, + .read_folio =3D swap_fs_read_folio, + .write_folio =3D swap_fs_write_folio, }; =20 static const struct swap_ops bdev_sync_swap_ops =3D { - .read_folio =3D swap_read_folio_bdev_sync, - .write_folio =3D swap_writepage_bdev_sync, + .read_folio =3D swap_bdev_sync_read_folio, + .write_folio =3D swap_bdev_sync_write_folio, }; =20 static const struct swap_ops bdev_async_swap_ops =3D { - .read_folio =3D swap_read_folio_bdev_async, - .write_folio =3D swap_writepage_bdev_async, + .read_folio =3D swap_bdev_async_read_folio, + .write_folio =3D swap_bdev_async_write_folio, }; =20 int init_swap_ops(struct swap_info_struct *sis) --=20 2.52.0 From nobody Fri Jun 12 12:43:35 2026 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBEFB30CD85 for ; Fri, 15 May 2026 01:58:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810297; cv=none; b=ZamzT9oLe3qhc6DYbfxMU+wnDRmoY5AJJQUjGw2rHTtbBAk+E8xn0xQ/9FBjkeXxTjNI74ry6RNAqUzxwIoo2FrSJeTf0ShGbNXbixncEZmTeiOCGECDxxcn2giu/QS5B4n3nURkVTdfvC3KswUasc82/MewalsLSChm3uLFQhI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778810297; c=relaxed/simple; bh=bMPjdzt1HQb1JHVWTPu4arI9X39DTCXQlIN//QLm8Nc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=lM6xeWAcCoUjAQj5rJn3Rzh3h7PhTx4f9Qviuj7L3jDWGwiSRuoIr1cx/kshBdozY0igeua8bvtXyEVdaOdBBis0/G6uk3pgeHRWSQrRrqezz+zfWg+x2KyEyIrZEnSNV6zaMW836YYhEF/K18JHus2VSVq4S1m95e402XIX66E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=LntZuY8Q; arc=none smtp.client-ip=95.215.58.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="LntZuY8Q" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1778810294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oIPP5FhKMUzrX3A0EHFrIdCr5xrbmrEmKzfzM6TrS4c=; b=LntZuY8QFuRihoIjhUQTi0DBnkEvRf1icssDkm1b4W/Vp3zQZia9jEe+VYIBH1vMUs91bC l5FCfgeJ3t/4Tc2CyZPVJjzJIM5Esi3xhDadY9ApwsQGpcWEgeCbv1hD/IvsdaAFeFqYUs ySG9l1eDWBYUbDcgch5zYFOkXEk/UeU= From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, chrisl@kernel.org, usama.arif@linux.dev, baohua@kernel.org, kasong@tencent.com, nphamcs@gmail.com, shikemeng@huaweicloud.com, youngjun.park@lge.com, hch@infradead.org, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v7 3/3] mm/swap: add unplug callback to swap_ops to fix leaky abstraction Date: Fri, 15 May 2026 09:57:37 +0800 Message-ID: <20260515015737.890994-4-baoquan.he@linux.dev> In-Reply-To: <20260515015737.890994-1-baoquan.he@linux.dev> References: <20260515015737.890994-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When swap_ops was introduced, the FS-swap batch submission remained as a standalone swap_write_unplug() that directly called mapping->a_ops->swap_rw(). This meant callers still had implicit knowledge of filesystem internals rather than going through the swap_ops abstraction. Fix this by adding an unplug callback to struct swap_ops. Each ops table provides its own implementation: - bdev_fs_swap_ops uses the existing FS batch-submission logic - bdev_sync/bdev_async_swap_ops leave it NULL since block-layer plugging handles their I/O The swap_iocb now carries a pointer to its ops table so that swap_write_unplug() can dispatch through the callback without the caller needing to know the swap device type. Signed-off-by: Baoquan He --- mm/page_io.c | 11 ++++++++++- mm/swap.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mm/page_io.c b/mm/page_io.c index 38b94c560c37..2c36d261ad98 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -329,6 +329,7 @@ static void bio_associate_blkg_from_page(struct bio *bi= o, struct folio *folio) =20 struct swap_iocb { struct kiocb iocb; + const struct swap_ops *ops; struct bio_vec bvec[SWAP_CLUSTER_MAX]; int pages; int len; @@ -401,6 +402,7 @@ static void swap_fs_write_folio(struct swap_info_struct= *sis, init_sync_kiocb(&sio->iocb, swap_file); sio->iocb.ki_complete =3D sio_write_complete; sio->iocb.ki_pos =3D pos; + sio->ops =3D sis->ops; sio->pages =3D 0; sio->len =3D 0; } @@ -454,7 +456,7 @@ static void swap_bdev_async_write_folio(struct swap_inf= o_struct *sis, submit_bio(bio); } =20 -void swap_write_unplug(struct swap_iocb *sio) +static void swap_fs_write_folio_unplug(struct swap_iocb *sio) { struct iov_iter from; struct address_space *mapping =3D sio->iocb.ki_filp->f_mapping; @@ -466,6 +468,12 @@ void swap_write_unplug(struct swap_iocb *sio) sio_write_complete(&sio->iocb, ret); } =20 +void swap_write_unplug(struct swap_iocb *sio) +{ + if (sio->ops && sio->ops->unplug) + sio->ops->unplug(sio); +} + static void sio_read_complete(struct kiocb *iocb, long ret) { struct swap_iocb *sio =3D container_of(iocb, struct swap_iocb, iocb); @@ -599,6 +607,7 @@ static void swap_bdev_async_read_folio(struct swap_info= _struct *sis, static const struct swap_ops bdev_fs_swap_ops =3D { .read_folio =3D swap_fs_read_folio, .write_folio =3D swap_fs_write_folio, + .unplug =3D swap_fs_write_folio_unplug, }; =20 static const struct swap_ops bdev_sync_swap_ops =3D { diff --git a/mm/swap.h b/mm/swap.h index 8d4375f91632..67e3b1617146 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -224,6 +224,7 @@ struct swap_ops { void (*write_folio)(struct swap_info_struct *sis, struct folio *folio, struct swap_iocb **plug); + void (*unplug)(struct swap_iocb *sio); }; int init_swap_ops(struct swap_info_struct *sis); void swap_read_folio(struct folio *folio, struct swap_iocb **plug); --=20 2.52.0