shmem_unuse() disallows eviction of an inode while swapping in its pages, and shmem_evict_inode() waits until eviction is allowed again. However, swapping in folios will end up allocating memory with __GFP_FS, possibly triggering direct reclaim, where the superblock shrinkers can drop the last reference to that very inode. For example, shmem_unused_huge_shrink() takes and drops an inode reference to split large folios beyond i_size: swapoff another task ======= ============ shmem_unuse() shmem_disallow_eviction() shmem_swapin_folio() direct reclaim shmem_unused_huge_shrink() igrab() unlink(), close() iput() shmem_evict_inode() waits for eviction to be allowed Consequently, swapoff can deadlock against itself. Let's fix it by not pinning inodes whose eviction is disallowed in shmem_unused_huge_shrink(), effectively skipping them in this reclaim pass. Within shmem, shmem_unused_huge_shrink() is the only place where reclaim takes and drops a temporary reference to a shmem inode. We could also avoid the deadlock by swapping in under NOFS scope in shmem_unuse(). However, allocating a lot of folios under NOFS does not sound like the right approach. This has low urgency, as the race is hard to hit and swapoff is privileged. Fixes: af53d3e9e040 ("mm: swapoff: shmem_unuse() stop eviction without igrab()") Cc: stable@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Baolin Wang Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/shmem.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/shmem.c b/mm/shmem.c index 58b337efe7b02..798581db29853 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -884,6 +884,16 @@ static enum lru_status shmem_unused_huge_isolate(struct list_head *item, if (!spin_trylock(&info->lock)) return LRU_SKIP; + /* + * Do not grab temporary inode references if eviction is disallowed, as + * we might later put the last remaining one and deadlock (e.g., + * swapoff). + */ + if (atomic_read(&info->stop_eviction)) { + spin_unlock(&info->lock); + return LRU_SKIP; + } + /* pin the inode */ inode = igrab(&info->vfs_inode); /* inode is about to be evicted */ -- 2.56.1