Splitting a file folio might allocate memory for the xarray and call filemap_release_folio(). If either ends up in direct reclaim or compaction with __GFP_FS, we can deadlock on the folio lock. For example, when reclaim evicts the inode through the dcache shrinker it can 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 example 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. We really must not allocate with __GFP_FS while holding a folio lock. As Willy explains [1], direct compaction can also trigger deadlocks. 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. 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@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Barry Song Reviewed-by: Baolin Wang Reviewed-by: Zi Yan Acked-by: Lorenzo Stoakes (ARM) Reviewed-by: Chris Down Link: https://lore.kernel.org/r/awSgk1VTUsiVQOAT@casper.infradead.org [1] Signed-off-by: David Hildenbrand (Arm) --- mm/huge_memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e22daf846dbe3..44424af08cdd0 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 allocate memory while holding the folio lock. Direct + * reclaim or compaction could try evicting the inode, isolating and + * locking our folio, or wait for writeback on cgroup v1, to then + * 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