Re: [RFC PATCH v4 4/4] mm/shmem: split large folios only on -E2BIG

From: Baolin Wang

Date: Wed Jul 29 2026 - 22:58:01 EST




On 7/30/26 10:16 AM, Xueyuan Chen wrote:
shmem_writeout() currently splits a large folio on every
folio_alloc_swap() failure. With the refined return-value contract, only
-E2BIG indicates that splitting might allow smaller folios to be swapped
out.

Enter the split fallback only for -E2BIG. For -ENOSPC and -ENOMEM,
redirty and reactivate the folio as before.

Suggested-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Signed-off-by: Xueyuan Chen <xueyuan.chen21@xxxxxxxxx>
---

LGTM. Thanks.
Reviewed-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>

mm/shmem.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index 6641823bed16..6f959191ef70 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1609,6 +1609,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
pgoff_t index;
int nr_pages;
+ int ret;

Nit: can be in one line:

int nr_pages, ret;

bool split = false;
if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap)
@@ -1689,7 +1690,8 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
folio_mark_uptodate(folio);
}
- if (!folio_alloc_swap(folio)) {
+ ret = folio_alloc_swap(folio);
+ if (!ret) {
bool first_swapped = shmem_recalc_inode(inode, 0, nr_pages);
int error;
@@ -1742,7 +1744,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
swap_cache_del_folio(folio);
goto redirty;
}
- if (nr_pages > 1)
+ if (nr_pages > 1 && ret == -E2BIG)
goto try_split;
redirty:
folio_mark_dirty(folio);