mm: fix deadlock on the folio lock due to inode eviction
Subsystems (5)
David Hildenbrand (Arm)
maintainercover mm: fix deadlock on the folio lock due to inode eviction
Whenever we allocate memory with folio locks held and trigger direct reclaim, we might end up trying to evict an unused inode. Evicting inodes with a populated page cache takes folio locks, and if we happen to need one of the folio locks we already hold, we'll deadlock.
This can, in general, not happen if we also pin the folio's inode: for example, through a VMA that cannot go away concurrently. However, we have some scenarios where we hold folio locks without pinning inodes. Examples include page migration and some folio split users.
FUSE makes this easier to trigger, because it evicts inodes on the last reference. A similar deadlock with FUSE was recently fixed in FUSE [1].
Let's handle folio splitting by simply running in NOFS context. We only perform small allocations, so it doesn't really make a difference. Interestingly, this also fixes one of a handful of possible memcg v1 deadlocks.
For page migration, change the code to allocate destination folios without holding folio locks, which is in any case the right thing to do to reduce folio lock holding times.
Some of this might also be fixable using Jan's work on deferred inode reclaim [2]. However, that is not stable material, and not allocating folios while holding other folio locks is an improvement on its own.
Unprivileged users can trigger this deadlock with FUSE and mbind()/migrate_pages(), however, it requires multiple NUMA nodes and quite some luck.
I reproduced the original deadlocks with FUSE and made sure they can no longer be triggered with these fixes. Further, I ran the usual migration and folio splitting selftests.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
---
David Hildenbrand (Arm) (2):
mm/huge_memory: use NOFS when allocating while splitting folios mm/migrate: allocate destination folios before locking a batch
mm/huge_memory.c | 10 +++++ mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 107 insertions(+), 37 deletions(-)
---
base-commit: eb01811cc3713af1f78dfe18fa89c5270ebfc767
change-id: 20261121-migrate_reclaim_deadlock-44e28e226b9d
Best regards,
Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
David Hildenbrand (Arm)
maintainerPATCH 1/2 mm/huge_memory: use NOFS when allocating while splitting folios
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.
--- mm/huge_memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/huge_memory.c +10 -0
@@ -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 +* 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; }
Signature · 1 line
2.56.1
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
Quoted 38 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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.
Quoted 8 lines
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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM:
Signature · 2 lines
cheers, jose a. p-a
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
In reply to David Hildenbrand (Arm)Quoted 38 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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.
Quoted 8 lines
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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Reproduced the hang determnistically.
Base deadlocked.
With patch 1: -ENOMEM (safe)
With patch 2: split succeeds
Signature · 1 line
~Gregory
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
Quoted 49 lines · Gregory Price (Meta)
Quoted 38 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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.
Quoted 8 lines
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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 3 lines
Reproduced the hang determnistically.
Base deadlocked.
With patch 1:-ENOMEM(safe)
Hi Gregory,
Just curious how those -ENOMEM failures occur. With GFP_NOFS, we should still be able to reclaim anonymous folios (except in SWAP_OPS_F_REQUIRE_NOFS cases) and clean page cache folios, right?
Quoted 5 lines
With patch 2: split succeeds
Tested-by Gregory Price (Meta) <[email protected]>Reviewed-by Gregory Price (Meta) <[email protected]>
+1 for the patch itself.
Thanks,
Barry
Gregory Price (Meta)
reviewerRe: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
Quoted 11 lines · Barry Song
Quoted 3 lines · Gregory Price (Meta)
Reproduced the hang determnistically.
Base deadlocked.
With patch 1:-ENOMEM(safe)Hi Gregory,
Just curious how those
-ENOMEMfailures occur. WithGFP_NOFS, we should still be able to reclaim anonymous folios (except inSWAP_OPS_F_REQUIRE_NOFScases) and clean page cache folios, right?
Ah this is a little dumb, but i set memory.max=0 to force charge failures. The -ENOMEM is basically expected in this scenario.
Not really something you have to address.
Signature · 1 line
~Gregory
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
In reply to David Hildenbrand (Arm)Quoted 40 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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.
Quoted 7 lines
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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Make sense.
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
In reply to David Hildenbrand (Arm)Quoted 50 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>--- mm/huge_memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
<snip>
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 4490
Quoted 5 lines
mm/huge_memory.c +1
-0@@ -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);
We will need to make sure future code all come to here for the return.
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 4493
Quoted 3 lines
mm/huge_memory.c +0
-0return ret; }
Otherwise, LGMT.
Signature · 2 lines
Best Regards, Yan, Zi
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
In reply to David Hildenbrand (Arm)Quoted 33 lines · David Hildenbrand (Arm)
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 intruncate_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_FSis 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().
Quoted 6 lines
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!
Quoted 7 lines
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()")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM so:
Quoted 12 lines
--- mm/huge_memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/huge_memory.c +0
-0@@ -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;
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 4437
Quoted 10 lines
mm/huge_memory.c +5
-0+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.
Quoted 17 lines
mm/huge_memory.c +5
-1+* 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; } --
Quoted 2 lines
2.56.1
--
Cheers, Lorenzo
Re: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
In reply to David Hildenbrand (Arm)Quoted 8 lines · David Hildenbrand (Arm)
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 intruncate_inode_pages_final()for the folio that we hold locked:
Quoted 11 lines
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)The "holding a reference to the inode" piece is a red herring. We can deadlock even without it (although maybe the AI can't see it). If we don't remove __GFP_FS, we can hit this path:
folio_lock(A)
kmalloc
alloc_pages
try_to_compact_pages
compact_zone_order
compact_zone(MIGRATE_SYNC_LIGHT)
migrate_pages
migrate_pages_sync
migrate_pages_batch
try_split_folio
folio_lock(A)
See this discussion:
https://lore.kernel.org/all/[email protected]/
which led to patch f2c817bed58d in June 2020.
So yes, anywhere we're allocating memory with a file folio locked, we should enter nofs scope. Perhaps we can teach the AI that rule?
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios
Quoted 25 lines · Matthew Wilcox
Quoted 8 lines · David Hildenbrand (Arm)
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 intruncate_inode_pages_final()for the folio that we hold locked:Quoted 11 lines
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)The "holding a reference to the inode" piece is a red herring. We can deadlock even without it (although maybe the AI can't see it). If we don't remove
__GFP_FS, we can hit this path:
Quoted 11 lines
folio_lock(A)
kmalloc
alloc_pages
try_to_compact_pages
compact_zone_order
compact_zone(MIGRATE_SYNC_LIGHT)
migrate_pages
migrate_pages_sync
migrate_pages_batch
try_split_folio
folio_lock(A)
Quite nasty, thanks for these details. I was aware that memcg v1 would run into deadlocks whenever we would perform a charged allocation, but sync compaction triggering it is even worse.
Quoted 4 lines
See this discussion:
https://lore.kernel.org/all/[email protected]/
which led to patchf2c817bed58din June 2020.
Thanks for the pointer.
Quoted 3 lines
So yes, anywhere we're allocating memory with a file folio locked, we should enter nofs scope. Perhaps we can teach the AI that rule?
I have a couple of reports here regarding memcg v1 deadlocks, so it's not completely aware of the issues yet.
We should document the rules of what you can or can't do while holding the folio lock somewhere, yes!
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerPATCH 2/2 mm/migrate: allocate destination folios before locking a batch
In reply to David Hildenbrand (Arm)Migration locks folios without holding a reference on their inode. migrate_pages_batch() keeps a whole batch locked while allocating the destinations of the next folios. If such an allocation ends up in direct reclaim with __GFP_FS, reclaim can evict an inode through the dcache shrinker, and wait in truncate_inode_pages_final() for a folio that we hold locked:
migrate_pages_batch()
migrate_folio_unmap(A)
folio_trylock(A)
migrate_folio_unmap(B)
get_new_folio()
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.
Consequently, allocations under the folio lock are dangerous if we cannot prevent that scenario through an inode reference; and we want to avoid grabbing inode references, because we might then drop the last one, and would have to evict the inode from migration context, ideally without holding any folio locks.
Allocating in a NOFS scope would be one option, but it would have rather bad side effects, such as direct compaction not migrating file folios and the OOM killer not being invoked.
So let's fix it by allocating all destination folios of a batch before locking any source folio. Now, we no longer allocate destination folios while holding folio locks, which also avoids holding these locks during direct reclaim or compaction for these allocations: an improvement on its own.
We keep the destination folio of a folio that we have to retry, instead of freeing it and allocating a new one. If we would have to allocate while already holding folio locks, which can happen when retrying a failed split for longterm pinning, we leave the folio to the synchronous fallback in migrate_pages_sync() instead, which migrates folios one by one.
Note that nfs_migrate_folio() still writes back the folio while holding its lock in MIGRATE_SYNC mode, which allocates memory. As NFS only evicts stale or unlinked inodes on the last reference, this is unlikely to deadlock; this must be sorted out in NFS code independently.
This has medium urgency.
--- mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 37 deletions(-)
mm/migrate.c +97 -37
@@ -1220,25 +1220,15 @@ static void migrate_folio_done(struct folio *src, }/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap (new_folio_t get_new_folio, -free_folio_t put_new_folio,unsigned long private, -struct folio *src,struct folio **dstp,enum migrate_mode mode, -struct list_head *ret) +static int migrate_folio_unmap (struct folio *src,struct folio *dst, +enum migrate_mode mode,struct list_head *ret) { -struct folio *dst;int rc = -EAGAIN ;int old_folio_state =0 ;struct anon_vma *anon_vma =NULL ;bool locked = false;bool dst_locked = false; - dst =get_new_folio (src, private); -if (!dst) -return -ENOMEM ; - *dstp = dst; - - dst->migrate_info =0 ; -if (!folio_trylock (src)) {if (mode ==MIGRATE_ASYNC ) goto out;
@@ -1364,7 +1354,8 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,migrate_folio_undo_src (src, old_folio_state &FOLIO_WAS_MAPPED , anon_vma, locked, ret); -migrate_folio_undo_dst (dst, dst_locked, put_new_folio, private); +if (dst_locked) +folio_unlock (dst);return rc; }
@@ -1809,6 +1800,25 @@ static void migrate_folios_undo(struct list_head *src_folios, } } +static void migrate_folio_free_dst (struct folio *dst, +free_folio_t put_new_folio,unsigned long private) +{ +list_del (&dst->lru); + dst->private =NULL ; +migrate_folio_undo_dst (dst, false, put_new_folio, private); +} + +static void migrate_folios_undo_unused (struct list_head *pending_folios, +struct list_head *new_folios,free_folio_t put_new_folio, +unsigned long private,struct list_head *from) +{ +struct folio *dst, *tmp; + +list_for_each_entry_safe (dst, tmp, new_folios, lru) +migrate_folio_free_dst (dst, put_new_folio, private); +list_splice (pending_folios, from); +} +/* * migrate_pages_batch() first unmaps folios in the from list as many as * possible, then move the unmapped folios.
@@ -1831,8 +1841,10 @@ static int migrate_pages_batch(struct list_head *from,int pass =0 ;bool is_thp = false;bool is_large = false; -struct folio *folio, *folio2, *dst =NULL ; +struct folio *folio, *dst, *tmp;int rc, rc_saved =0 , nr_pages; +LIST_HEAD (pending_folios); +LIST_HEAD (new_folios);LIST_HEAD (unmap_folios);LIST_HEAD (dst_folios);bool nosplit = (reason ==MR_NUMA_MISPLACED );
@@ -1845,7 +1857,14 @@ static int migrate_pages_batch(struct list_head *from, thp_retry =0 ; nr_retry_pages =0 ; -list_for_each_entry_safe (folio, folio2, from, lru) { +/* +* Prepare the source folios, and allocate the destination +* folios before locking any source folio. We lock source folios +* without holding an inode reference, and direct reclaim could +* try evicting the inode to then deadlock on folio locks. +* Further, this reduces folio locking times. +*/ +list_for_each_entry_safe (folio, tmp, from, lru) { is_large =folio_test_large (folio); is_thp =folio_test_pmd_mappable (folio); nr_pages =folio_nr_pages (folio);
@@ -1925,21 +1944,24 @@ static int migrate_pages_batch(struct list_head *from,continue ; } - rc =migrate_folio_unmap (get_new_folio, put_new_folio, - private, folio, &dst, mode, ret_folios);/* -* The rules are: -* 0: folio will be put on unmap_folios list, -* dst folio put on dst_folios list -* -EAGAIN: stay on the from list -* -ENOMEM: stay on the from list -* Other errno: put on ret_folios list +* Don't allocate while holding folio locks, but leave +* the folio to the one-by-one fallback in +* migrate_pages_sync(). */ -switch (rc) { -case -ENOMEM : +if (!list_empty (&unmap_folios)) { + retry++; + thp_retry += is_thp; + nr_retry_pages += nr_pages; +continue ; + } + + dst =get_new_folio (folio, private); +if (!dst) {/* * When memory is low, don't bother to try to migrate -* other folios, move unmapped folios, then exit. +* other folios, migrate the folios that we have +* destination folios for, then exit. */ nr_failed++; stats->nr_thp_failed += is_thp;
@@ -1950,7 +1972,7 @@ static int migrate_pages_batch(struct list_head *from,if (!ret) { stats->nr_thp_split += is_thp; stats->nr_split++; -break ; +continue ; }else if (reason ==MR_LONGTERM_PIN && ret == -EAGAIN ) {/*
@@ -1963,18 +1985,42 @@ static int migrate_pages_batch(struct list_head *from,/* Undo duplicated failure counting. */ nr_failed--; stats->nr_thp_failed -= is_thp; -break ; +continue ; } } - stats->nr_failed_pages += nr_pages + nr_retry_pages; -/* nr_failed isn't updated for not used */ - stats->nr_thp_failed += thp_retry; - rc_saved = rc; -if (list_empty (&unmap_folios)) - goto out; -else - goto move; + stats->nr_failed_pages += nr_pages; + rc_saved = -ENOMEM ; +break ; + } +/* Remember the source folio until we unmap it. */ + dst->private = folio; +list_move_tail (&folio->lru, &pending_folios); +list_add_tail (&dst->lru, &new_folios); + } + +/* +* Lock and unmap the source folios. Each destination folio +* remembers its source folio in ->private, so walk the +* destination folios. +*/ +list_for_each_entry_safe (dst, tmp, &new_folios, lru) { + folio = dst->private; + is_thp =folio_test_pmd_mappable (folio); + nr_pages =folio_nr_pages (folio); + +cond_resched_tasks_rcu_qs (); + + rc =migrate_folio_unmap (folio, dst, mode, ret_folios); +/* +* The rules are: +* 0: folio will be put on unmap_folios list, +* dst folio put on dst_folios list +* -EAGAIN: stay on the pending_folios list, +* keep dst +* Other errno: put on ret_folios list, free dst +*/ +switch (rc) {case -EAGAIN : retry++; thp_retry += is_thp;
@@ -1982,7 +2028,7 @@ static int migrate_pages_batch(struct list_head *from,break ;case 0 :list_move_tail (&folio->lru, &unmap_folios); -list_add_tail (&dst->lru, &dst_folios); +list_move_tail (&dst->lru, &dst_folios);break ;default :/*
@@ -1991,12 +2037,24 @@ static int migrate_pages_batch(struct list_head *from,* removed from migration folio list and not * retried in the next outer loop. */ +migrate_folio_free_dst (dst, put_new_folio, + private); nr_failed++; stats->nr_thp_failed += is_thp; stats->nr_failed_pages += nr_pages;break ; } } + +if (rc_saved) { + stats->nr_failed_pages += nr_retry_pages; +/* We return rc_saved, so don't update nr_failed. */ + stats->nr_thp_failed += thp_retry; + rc = rc_saved; +if (list_empty (&unmap_folios)) + goto out; + goto move; + } } nr_failed += retry; stats->nr_thp_failed += thp_retry;
@@ -2026,6 +2084,8 @@ static int migrate_pages_batch(struct list_head *from,/* Cleanup remaining folios */ migrate_folios_undo (&unmap_folios, &dst_folios, put_new_folio, private, ret_folios); +migrate_folios_undo_unused (&pending_folios, &new_folios, put_new_folio, + private, from);return rc; }
Signature · 1 line
2.56.1
Re: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch
Quoted 52 lines · David Hildenbrand (Arm)
Migration locks folios without holding a reference on their inode.
migrate_pages_batch()keeps a whole batch locked while allocating the destinations of the next folios. If such an allocation ends up in direct reclaim with__GFP_FS, reclaim can evict an inode through the dcache shrinker, and wait intruncate_inode_pages_final()for a folio that we hold locked:migrate_pages_batch() migrate_folio_unmap(A) folio_trylock(A) migrate_folio_unmap(B) get_new_folio() 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.
Consequently, allocations under the folio lock are dangerous if we cannot prevent that scenario through an inode reference; and we want to avoid grabbing inode references, because we might then drop the last one, and would have to evict the inode from migration context, ideally without holding any folio locks.
Allocating in a NOFS scope would be one option, but it would have rather bad side effects, such as direct compaction not migrating file folios and the OOM killer not being invoked.
So let's fix it by allocating all destination folios of a batch before locking any source folio. Now, we no longer allocate destination folios while holding folio locks, which also avoids holding these locks during direct reclaim or compaction for these allocations: an improvement on its own.
We keep the destination folio of a folio that we have to retry, instead of freeing it and allocating a new one. If we would have to allocate while already holding folio locks, which can happen when retrying a failed split for longterm pinning, we leave the folio to the synchronous fallback in
migrate_pages_sync()instead, which migrates folios one by one.Note that
nfs_migrate_folio()still writes back the folio while holding its lock inMIGRATE_SYNCmode, which allocates memory. As NFS only evicts stale or unlinked inodes on the last reference, this is unlikely to deadlock; this must be sorted out in NFS code independently.
Quoted 6 lines
This has medium urgency.
Fixes 5dfab109d519 ("migrate_pages: batch _unmap and _move")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
I was able to reproduce the issue and validate that this change fixes it. I used FUSE to do multiple allocations and migrations while triggering the shrinkers to reclaim dentries / cause FUSE to reclaim the inodes.
In the base we end up deadlocked, with patch 1 and 2, the migrations succeed but the reclaimed folios will just end up freed at the end of it all anyway. Some migration work gets wasted either way, but that's fine.
I agree this is better than the NOFS option.
Signature · 1 line
~Gregory
Re: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch
In reply to David Hildenbrand (Arm)Quoted 62 lines · David Hildenbrand (Arm)
Migration locks folios without holding a reference on their inode.
migrate_pages_batch()keeps a whole batch locked while allocating the destinations of the next folios. If such an allocation ends up in direct reclaim with__GFP_FS, reclaim can evict an inode through the dcache shrinker, and wait intruncate_inode_pages_final()for a folio that we hold locked:migrate_pages_batch() migrate_folio_unmap(A) folio_trylock(A) migrate_folio_unmap(B) get_new_folio() 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.
Consequently, allocations under the folio lock are dangerous if we cannot prevent that scenario through an inode reference; and we want to avoid grabbing inode references, because we might then drop the last one, and would have to evict the inode from migration context, ideally without holding any folio locks.
Allocating in a NOFS scope would be one option, but it would have rather bad side effects, such as direct compaction not migrating file folios and the OOM killer not being invoked.
So let's fix it by allocating all destination folios of a batch before locking any source folio. Now, we no longer allocate destination folios while holding folio locks, which also avoids holding these locks during direct reclaim or compaction for these allocations: an improvement on its own.
We keep the destination folio of a folio that we have to retry, instead of freeing it and allocating a new one. If we would have to allocate while already holding folio locks, which can happen when retrying a failed split for longterm pinning, we leave the folio to the synchronous fallback in
migrate_pages_sync()instead, which migrates folios one by one.Note that
nfs_migrate_folio()still writes back the folio while holding its lock inMIGRATE_SYNCmode, which allocates memory. As NFS only evicts stale or unlinked inodes on the last reference, this is unlikely to deadlock; this must be sorted out in NFS code independently.This has medium urgency.
Fixes 5dfab109d519 ("migrate_pages: batch _unmap and _move")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>--- mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 37 deletions(-)
<snip>
mm/migrate.c static int migrate_pages_batch(struct list_head *from, · line 1997
Quoted 2 lines
mm/migrate.c +2
-0+/* Remember the source folio until we unmap it. */ + dst->private = folio;
Clever to use ->private, but I wonder if we want a new field name, like we added migrate_info, to clarify its use.
In addition, it will be great to keep migration info for each pair of src and dst in an array of structs to avoid the ->private being used for different things at different migration phases, but allocating such an arrary will cause headaches. So this patch is probably the best we can get.
Signature · 2 lines
Best Regards, Yan, Zi
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch
Quoted 72 lines · Zi Yan
Quoted 62 lines · David Hildenbrand (Arm)
Migration locks folios without holding a reference on their inode.
migrate_pages_batch()keeps a whole batch locked while allocating the destinations of the next folios. If such an allocation ends up in direct reclaim with__GFP_FS, reclaim can evict an inode through the dcache shrinker, and wait intruncate_inode_pages_final()for a folio that we hold locked:migrate_pages_batch() migrate_folio_unmap(A) folio_trylock(A) migrate_folio_unmap(B) get_new_folio() 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.
Consequently, allocations under the folio lock are dangerous if we cannot prevent that scenario through an inode reference; and we want to avoid grabbing inode references, because we might then drop the last one, and would have to evict the inode from migration context, ideally without holding any folio locks.
Allocating in a NOFS scope would be one option, but it would have rather bad side effects, such as direct compaction not migrating file folios and the OOM killer not being invoked.
So let's fix it by allocating all destination folios of a batch before locking any source folio. Now, we no longer allocate destination folios while holding folio locks, which also avoids holding these locks during direct reclaim or compaction for these allocations: an improvement on its own.
We keep the destination folio of a folio that we have to retry, instead of freeing it and allocating a new one. If we would have to allocate while already holding folio locks, which can happen when retrying a failed split for longterm pinning, we leave the folio to the synchronous fallback in
migrate_pages_sync()instead, which migrates folios one by one.Note that
nfs_migrate_folio()still writes back the folio while holding its lock inMIGRATE_SYNCmode, which allocates memory. As NFS only evicts stale or unlinked inodes on the last reference, this is unlikely to deadlock; this must be sorted out in NFS code independently.This has medium urgency.
Fixes 5dfab109d519 ("migrate_pages: batch _unmap and _move")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>--- mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 37 deletions(-)<snip>
mm/migrate.c static int migrate_pages_batch(struct list_head *from, · line 1997
Quoted 2 lines
mm/migrate.c +2
-0+/* Remember the source folio until we unmap it. */ + dst->private = folio;Clever to use ->private, but I wonder if we want a new field name, like we added
migrate_info, to clarify its use.
Quoted 5 lines
In addition, it will be great to keep migration info for each pair of src and dst in an array of structs to avoid the ->private being used for different things at different migration phases, but allocating such an arrary will cause headaches. So this patch is probably the best we can get.
Right, this is something for stable that doesn't get to wild, but we should clean it up further on top.
Thanks!
Signature · 3 lines
Cheers, David
Hi David,
Thanks, this looks good. I exercised that in a VM:
- with the series applied, migrate_pages() on a virtiofs file mapping no longer allocates destination folios while it holds folios from the batch locked
- splitting a file THP no longer passes __GFP_FS to the XArray node allocations
- and the migration and split_huge_page selftests still pass
That said, I didn't manage to practically trigger the deadlock with a virtiofs mbind() migration racing an unmap before this patch. It might just be that I am unlucky though, and the code looks correct.
Given that for the whole series:
Chris
David Hildenbrand (Arm)
maintainerQuoted 11 lines · Chris Down
Hi David,
Thanks, this looks good. I exercised that in a VM:
- with the series applied,
migrate_pages()on a virtiofs file mapping no longer allocates destination folios while it holds folios from the batch locked
- splitting a file THP no longer passes__GFP_FSto the XArray node allocations
- and the migration andsplit_huge_pageselftests still pass
Quoted 7 lines
That said, I didn't manage to practically trigger the deadlock with a virtiofs
mbind()migration racing an unmap before this patch. It might just be that I am unlucky though, and the code looks correct.Given that for the whole series:
Reviewed-by Chris Down <[email protected]>
Thanks everyone for the very fast review!
Signature · 3 lines
Cheers, David
Quoted 11 lines · David Hildenbrand (Arm)
Whenever we allocate memory with folio locks held and trigger direct reclaim, we might end up trying to evict an unused inode. Evicting inodes with a populated page cache takes folio locks, and if we happen to need one of the folio locks we already hold, we'll deadlock.
This can, in general, not happen if we also pin the folio's inode: for example, through a VMA that cannot go away concurrently. However, we have some scenarios where we hold folio locks without pinning inodes. Examples include page migration and some folio split users.
[...]
Applied, thanks!
tree: mm/core.git
branch: for-next-fixes
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.
[1/2] mm/huge_memory: use NOFS when allocating while splitting folios
https://git.kernel.org/mm/core/c/acc29031 [2/2] mm/migrate: allocate destination folios before locking a batch https://git.kernel.org/mm/core/c/15f32855
Signature · 3 lines
Cheers, David