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

Baolin Wang <[email protected]> Thu, 30 Jul 2026 10:52:59 +0800
Newsgroups org.kernel.vger.cgroups,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>

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 <[email protected]>
> Signed-off-by: Xueyuan Chen <[email protected]>
> ---

LGTM. Thanks.
Reviewed-by: Baolin Wang <[email protected]>

>   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);