From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795374157; b=c715FgtlzMwlAXQIPw0uyUtVc4n5RUELdze56VSDWQ1dQo46E1Zy+c3tESfDOHv4QaSL CREJtHLUQ4f5KOy+4Z2xRSXML9cRoXGaJFusSzDghw7wQ//zzoo78p8rlXIGXY5AfYUWz BGgY75jGQbOF6DMUY9jakRkPIpkZevgkOpHC1aYtYrFS9aKUNyi1ZNetmoWxq/SsIi4DC fP6Pr165Uaf+bx3KI4eiyJY7J5OqCXVQMcm8O2xbSrPMtviL08aHvL/k+YqBgnOX7qmhH GAzc28bL0b9ZLXCJ97zbWAZ1NziZMta2kt1q3BmOpIETCWnCGqGNPFU+605VgmLeuig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795374157; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: MIME-Version:Content-Type:Content-Transfer-Encoding:Subject:From:To: Cc:In-Reply-To:References:Date:Message-Id:X-Mailer; bh=Lp0osgtiZVKWMvtaLvxNrKUfcz8WxeL6pbk5OTyZtio=; b=XAloYtoaGfDz82z5xqcmB3gar+m0ildRhirD3X6LUU8JZIad9j+EUwXQWzuhYLwWZbO/ oCPrINkY2Nr67RElgbBJDjJfTUDULH95YpDl+22zslLv2aCeG8KQoAsYwwptN3kGMrIdg /b66KloPvynDTEBbeF+cO0n07rlyA3q6XXmKvhOLivyMZ9O4PtcyOySw9W8hTBTDhjIys +Ya2YiSzDL61YQuoJF+ujAGh+ORcpGhRHT+Ty+esWnLFjI9leV4sepEIGMD7PbSAdDSTE 3STqXRiUOYUCNRzAKfR6EZWA8xvbM6CynipDAAiVtphBcGh399oXpIUSaO1C68RQrCQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=uQFeWFLZ; arc=none smtp.remote-ip=209.85.222.179 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8958C606A331 for ; Sun, 22 Nov 2026 19:02:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8958C606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=uQFeWFLZ Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-949b0d32315so236445285a.1 for ; Sun, 22 Nov 2026 11:02:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1795374156; x=1795978956; darn=vger.kernel.org; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=Lp0osgtiZVKWMvtaLvxNrKUfcz8WxeL6pbk5OTyZtio=; b=uQFeWFLZsxcxcM3Y5PRguhI2N3sC44abIIYqvAJ5480AJO17aLrxmv/Mrc1sQy/1Ab e/GKhbYXMeYyweITHXj6HCJ5gXaNgtkOfrWlaaL2ktZHbIG6vePBtVvSQorfGijae/dp HG4fydroZks/uH9axcRNOtC70y4ETiD0cGLFyv69QD5/lMjRMg2fKsg7QTLJK8jM/qTJ xnKeI6VcXjHi+oOkz55ieTVlqga4f6mnk5x7VYtdcWZMCVgnDd3ZsKJTb5g5Hl/CZIzv kA+8BnsxL/pxPlzjn+2FoD/PXx7Y5Ub/demE8XrMa8TEZOOXCxxI7CUMEutpIaJPR//1 M7Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795374156; x=1795978956; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Lp0osgtiZVKWMvtaLvxNrKUfcz8WxeL6pbk5OTyZtio=; b=RzKXfB/RkIn7yTharpCXIpzn2ZXHvYH+/cq1K1mc/fwBJaoczn+F2DMjOo02+Ljs63 7OQcv3TKfEEzQi6wQ8S2t3Us3l6Jn4IKZ5a9rRO244E9N4WfOXP0aNSx5wHJhG4rUU/F dMe/AMUGsqP2OgRtAWmdv7FpECTlS+h7hNsnnaWVunchJ1Cts5jIg/i3eMy268LyM+ky h26BOAv8I82svPsM3Fq5wP21nb24nSK6H4KS6W/uHCUfECWiUtwh8tpjMKLQsNLR3xt5 vI98wbmU3WwehsGLnOQU49DNKxlUgsABz/XFMDDRFoHqzcCU8lt96k1VOx8k/xLhhoNW bAzA== X-Forwarded-Encrypted: i=1; AL+hErGfOV/PlYKbvGXAdsXFTRiiLf2VrTE0uPSsfNoyg1LdB4C/Wie9gUtDpKMm7dNf7ot5uu1VlUJHWkQv96M=@vger.kernel.org X-Gm-Message-State: APuU4Z9004dNMGMVjvoC1oYYKjhJUoyCuo67hb8i5hft0cRGmZXReUAG 3rac0GSiQ4C2lXHxuKVs/mK9El3qPe2GsS5WfT6DmipNsvVAFuPtm+ELsigf5Au4ubY= X-Gm-Gg: AZFdTZeppZqDvgLNfAuz39uLHZCVup0Ipk7Ow+aNq7i18bshDm71tCpEVnOgKZOHt98 jDpBWFslYi9FPGQCFbEhC26KswJpmJoMLznGXWkoMp+t4vWPKNUzhChCYDFNz7D5bpb3rKDsJB5 u+ZGdXuXazL9mYPTcT6mKIyOfLJEnFGDw+Nfi8PwRQPhIHMe+OZQ0E0ND79F98A3Qr38PJXlcSJ 4dUcsrByDokeZ18bFyHn1M2BSfDabcTDDDzsWH2ZKigDnbnY1aAwDny8yPqfJOM3i+GkC6/+G21 68ioDxavW1mxtL4p8vjIbHc+Jgy/aAO+4aKnzMeRp95KcJQlxZBsOpVFbiAulgjSRZ6C2szaaG8 TMbwopxt8z+jHwdWHfljyz1J7jeoH6Lv/UdYwgX2YXjawyi2nsW+Ce+6ttwQidlOwUNOzcdobBy hsezTQTgw8+kEWUdqTpAFnf0wgc3jOzJrNztonhycjpgvxYDKrvWXij6hmk4Fuj9UBswxEsUzS3 j4AWsCmQsEcUsfLpVGjUSGuaxBJ1QhcKKdHIadL7Ud0nykPb/Vyz6JM1yjN03sDzR1+cR7XTA2p TCKUlBtqpv3QByxzBjQ4twpKti+Bq0Y1XYJN72wqTEkh5Tqy9bn5/wPcloEQHKUKUhK6/gC2Uwi LVPxSebl89K3WbSseY4zLe0yqcC/Z X-Received: by 2002:a05:620a:a403:20b0:949:c967:eb68 with SMTP id af79cd13be357-949c967ee7fmr803688985a.1.1795374156268; Sun, 22 Nov 2026 11:02:36 -0800 (PST) Received: from [192.168.50.231] (pool-71-191-145-202.washdc.fios.verizon.net. [71.191.145.202]) by smtp.gmail.com with ESMTPSA id af79cd13be357-949bb4219c4sm651293585a.41.2026.11.22.11.02.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 11:02:35 -0800 (PST) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: Re: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch From: "Gregory Price (Meta)" To: "David Hildenbrand (Arm)" Cc: Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Andrew Morton , "Matthew Wilcox (Oracle)" , William Kucharski , Heiko Carstens , Alexander Gordeev , Claudio Imbrenda , Johannes Weiner , "Lorenzo Stoakes (ARM)" , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Christian Brauner , Jan Kara , Miklos Szeredi , Joanne Koong , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Chris Mason In-Reply-To: <20261122-migrate_reclaim_deadlock-v1-2-83c56185b0fb@kernel.org> References: <20261122-migrate_reclaim_deadlock-v1-0-83c56185b0fb@kernel.org> <20261122-migrate_reclaim_deadlock-v1-2-83c56185b0fb@kernel.org> Date: Sun, 22 Nov 2026 14:02:34 -0500 Message-Id: <179537415429.412063.2442586940573913371.b4-reply@b4> X-Mailer: b4 0.17-dev On 2026-11-22 13:42 +0100, David Hildenbrand (Arm) wrote: > 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. > > Fixes: 5dfab109d519 ("migrate_pages: batch _unmap and _move") > Cc: stable@vger.kernel.org > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) 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. Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) -- ~Gregory