Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios

From: Lorenzo Stoakes (ARM)

Date: Mon Nov 23 2026 - 12:27:17 EST


On Sun, Nov 22, 2026 at 01:42:17PM +0100, David Hildenbrand (Arm) wrote:
> Various callers (migration, memory-failure, s390_wiggle_split_folio) split
> locked folios without holding a reference on the folio's inode.
>
> Splitting a file folio might allocate memory for the xarray and call
> filemap_release_folio(). If either ends up in direct reclaim with
> __GFP_FS, reclaim can evict the inode through the dcache shrinker, and
> wait in truncate_inode_pages_final() for the folio that we hold locked:
>
> try_split_folio()
> folio_lock(A)
> split_folio_to_list(A)
> xas_split_alloc()
> try_to_free_pages()
> super_cache_scan()
> prune_dcache_sb()
> iput_final()
> evict()
> truncate_inode_pages_final()
> folio_lock(A)
>
> This requires that nothing else holds a reference on the inode, and can
> be triggered most easily with FUSE, which evicts inodes on the last
> reference.
>
> Further, xas_split_alloc() charges the XArray nodes to the memcg, and on
> cgroup v1, memcg reclaim waits for file folios under writeback if
> __GFP_FS is set. That can deadlock if writeback waits for a folio lock
> that we hold, for example with ext4.
>
> Let's fix it by splitting folios in a NOFS scope, so callers don't have
> to care. A NOFS scope also covers allocations that filesystems make with
> their own gfp flags, for example in filemap_release_folio().
>
> On NFS, splitting folios with pending writes or ongoing writes to
> fscache might now fail slightly more often, until these writes have
> completed. On netfs, only folios that are being written to fscache are
> affected. All fairly irrelevant.
>
> This has medium urgency.

But medium-rare tests best!

>
> Fixes: 6b24ca4a1a8d ("mm: Use multi-index entries in the page cache")
> Fixes: eef88fe45ac9 ("s390/uv: Split large folios in gmap_make_secure()")
> Fixes: 789763523fb4 ("xarray: honor XA_FLAGS_ACCOUNT in xas_split_alloc()")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

LGTM so:

Acked-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>

> ---
> mm/huge_memory.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index e22daf846dbe3..c4f6c642c2620 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -4425,11 +4425,20 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
> const bool is_swapcache = folio_test_swapcache(folio);
> int old_order = folio_order(folio);
> struct folio *new_folio, *next;
> + unsigned int nofs_flags;
> int ret;
>
> VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
> VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
>
> + /*
> + * We might not hold a reference on a locked folio's inode, but we might
> + * allocate memory under the folio lock. Direct reclaim could try
> + * evicting the inode, or wait for writeback on cgroup v1, to then

Ahhh. Cgroup v1. Of course.

> + * deadlock on folio locks.
> + */
> + nofs_flags = memalloc_nofs_save();
> +
> if (folio != page_folio(split_at) || folio != page_folio(lock_at)) {
> ret = -EINVAL;
> goto out;
> @@ -4478,6 +4487,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
> if (is_pmd_order(old_order))
> count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
> count_mthp_stat(old_order, !ret ? MTHP_STAT_SPLIT : MTHP_STAT_SPLIT_FAILED);
> + memalloc_nofs_restore(nofs_flags);
> return ret;
> }
>
>
> --
> 2.56.1
>

--
Cheers, Lorenzo