From nobody Tue Nov 24 12:36:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795351369; b=VNuQ1udMnLbCUiXwIjNRq157a8cbKUJGwCx2T3tzpOba+QV/+8A5qwCL4KvyoAcmpGas iq0pGtFV2tNOIW5bd1Vxja7sjl9AbWreC2QgUVhZ6tpYrOGf1Qj1mZUaOT4LJOe/EFKHV DTRpZlrShGotlztoqufHicjpR6je01+gm+svzsG3ndvpUEM8s6bP0xRuKeXkSrd4UML0p mqgzc8ywnxhsgC1xO/jtC2ofcTE1jEQiqfM0IxR0qnLjpHBOPd4n5KEFFv/KpzSKAePsk Z4LFF2w7hMOVBMQITAgLIT8IWOOvPpZfPSwUhyKIJRmz4wCrXCbMh8TtHCbzqbWxCrw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795351369; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=Ppxr18V13ksnrm9EzUd2RjXo/8ry52+v8MzRoDOJVzs=; b=CPfCbZ90N4CsU+Oip/hY5ei4UvVseXygEd9u0wfECUUW22oeldZoH5xrOQ++bN1nkU9q otohkGaK0CXLc20CAAfIodsLCn8yYKxXHEi7gGGWLBf2lwlnHuzRuTqPhx+kINsUHCQqP 1c7CMSlQVxjhT6gA2oJYvhnohwoonKLKFTfq2WzbuJnWzLbkUENp0/VMbo5novCXPvyWQ YaqUC36x8iYvR7vYGHGbf1Pti76k3ubUdjps5uH8pNgPmJyotdzbKrmRis8RJwtaDxICS MOBsQH6UHLrWGn5jRNh6tC7oPCxOmWTp6wwE67vXG96Rw5CXrXeSm4OgjxbRsNnIlMw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OLE2cbyM; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38C89606A320; Sun, 22 Nov 2026 12:42:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OLE2cbyM Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7437C1F000C6; Sun, 22 Nov 2026 12:42:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795351368; bh=Ppxr18V13ksnrm9EzUd2RjXo/8ry52+v8MzRoDOJVzs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=OLE2cbyMVO4gaFeDzqvX1rjr+CwWSlQzdzRcoUhJ9UbdgSPvZax08+Xvn3jz78zV7 R9sOAJqcGH8oycehCMl8I687bz5EppKCoHeKGT1Wblio5u5HW1uBmvYPlMoeZ7sOm9 ca0vwXQgPqdPoiTyvxPvQvAiURYhgYROc1SfgjNiacb8s/AGE9yVL+D5OvMdABKyna 7ykiswr7+k5AJiwAbIkpbIrA3CssPlAJtGcGaUpGiWQUf9ilvOEX81DwO2TJWjkVTD oxV1cKAdhZWUaVAVv+ivw0bQ/Ff3NX+6gO0zA6OlQ8pKne8l42rk9bR+B03JfIxFhk WaEWXzJpElNmw== From: "David Hildenbrand (Arm)" Date: Sun, 22 Nov 2026 13:42:17 +0100 Subject: [PATCH 1/2] mm/huge_memory: use NOFS when allocating while splitting folios 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: quoted-printable Message-Id: <20261122-migrate_reclaim_deadlock-v1-1-83c56185b0fb@kernel.org> References: <20261122-migrate_reclaim_deadlock-v1-0-83c56185b0fb@kernel.org> In-Reply-To: <20261122-migrate_reclaim_deadlock-v1-0-83c56185b0fb@kernel.org> To: 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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3379; i=david@kernel.org; h=from:subject:message-id; bh=dIRn4KbtPfU2Ckte5ZbEP8iOqeN2wNX4jIBv2nXxLTI=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKZHpvOMGjcYrCkWGTW1eSItc4TPISVq6SOPDkr8po59 cGCn3F9HaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEwkvZORoX+BeADTCftzi9me luy8rnjkz/3Q+LsVD/0fN3P0ClfFSzL8DzFj4Pp+NrW/9TL39u/KfU+vndFWT+2f5D/j0pQVh1J vcgEA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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. 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 Signed-off-by: David Hildenbrand (Arm) Acked-by: Lorenzo Stoakes (ARM) Reviewed-by: Baolin Wang Reviewed-by: Barry Song Reviewed-by: Chris Down Reviewed-by: Gregory Price (Meta) Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Zi Yan Tested-by: Gregory Price (Meta) --- 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, unsig= ned int new_order, const bool is_swapcache =3D folio_test_swapcache(folio); int old_order =3D folio_order(folio); struct folio *new_folio, *next; + unsigned int nofs_flags; int ret; =20 VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio); =20 + /* + * 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 =3D memalloc_nofs_save(); + if (folio !=3D page_folio(split_at) || folio !=3D page_folio(lock_at)) { ret =3D -EINVAL; goto out; @@ -4478,6 +4487,7 @@ static int __folio_split(struct folio *folio, unsigne= d 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_FAILE= D); + memalloc_nofs_restore(nofs_flags); return ret; } =20 --=20 2.56.1 From nobody Tue Nov 24 12:36:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795351378; b=uHIYPCWZDtAJV0cFyibv4xkwFs06rg8XCuSivLuJz5n8kovUPuNZR973IgHnS3xa5lT2 ZO+9V+TMKycDrhkURQXbcCtVLGZqVDawwze+TkdV0dlOG9apuRDC/MqILDvwzw511r3b1 OmU0S9cy82rmpLlDuKSqI2kbC4Mv2VPBSJ7EPopwLkn9L5gDUpv+Pz1U6Qy4MBSi1pY37 YNfBFCY8wX7Lh5jCV4T0DQvvb+tzoISMZp3RbB1qJFvuLhVQvGo8ioj+TPtE6JZOGBXVU 0W63m/k1eF/7spYF//YiNUTxZVgfarbo8hOtLSst5pqQbvmZEeN2wMxXb5LPS7YjZOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795351378; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=prSkEV3qUSQsXfBvO3BgrBMDcq6TDmX7yh8NVtmfjX8=; b=Cml/WsdIWFzm/GCBC+rezqh9eBTpWC/TgJwpV0PwUeUL5fMVuf4iinDD2V797ZQ5Ljst vBQ0unvQ6r/5Ez2yxPRDhgptz6LagEzTIwrKW3JLGk9jfm38Wf9mk2FutPiy29LM2Z8oy Cq2MF0VCyy2SGa+Hkse4L5cJB4Me1kWChyAVjkc3dFZqXNSiQP5IeQ462eN6m6t0S86yx zLK9eLswHzFTlF1j+RGuaKLpGLmYGME/0DUQLiVwN7jU4Djx9sODggvARMRIh3F+TvgYx oghrdWt/KSBS367Q5Rlgwq5O50jzZyiOsXRHPXohoh3wNZeC20vOzDcVKhLem25Gulg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=F2DN3zDp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 915D4606A32C; Sun, 22 Nov 2026 12:42:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=F2DN3zDp Received: by smtp.kernel.org (Postfix) with ESMTPSA id 891F91F000C6; Sun, 22 Nov 2026 12:42:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795351377; bh=prSkEV3qUSQsXfBvO3BgrBMDcq6TDmX7yh8NVtmfjX8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=F2DN3zDpfzlAFEs1RV733+O8P9L4dFs4bfg6vNKrU1S+dcKF9x++tDnmL79ryBeOC M5gGX1HzxcyFEVTr/tB8Yz6YU6nowJqc4WFP3Rp15bZF9paeEWdJoX2q3dKUAI/9Qb vTHaQCdmlsKlZ1n5csPyoJBwNBXOi+DCVbU7dDReQ4ejBm+qTqwJlDU903RnhI2lP5 lBGjWqD18oKf2tlRFBqmG4VdrplNer3pgnTMEPoykZyIIdjHxC1KY1fFkn8OzIrRm7 2T+UVXDTIMedjV8pFLRCUAeWX5j2aXwdCwUvt8MVmJoEYXdnRxGjSQA8kIf8Xcy3jn 0tmnZQ3cLAyVA== From: "David Hildenbrand (Arm)" Date: Sun, 22 Nov 2026 13:42:18 +0100 Subject: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch 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: quoted-printable Message-Id: <20261122-migrate_reclaim_deadlock-v1-2-83c56185b0fb@kernel.org> References: <20261122-migrate_reclaim_deadlock-v1-0-83c56185b0fb@kernel.org> In-Reply-To: <20261122-migrate_reclaim_deadlock-v1-0-83c56185b0fb@kernel.org> To: 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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10238; i=david@kernel.org; h=from:subject:message-id; bh=iRR0SjdNoHcH4WpV13Z+oejWwFpcHvtvx5X9gVhezrg=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKZHpsezVUJmah4c1X0j8f/DBz23q1iPSYgVn6k6dOMj M+z5im/7ChlYRDjYJAVU2QJCr5v8PLlutpqNz5mmDmsTCBDGLg4BWAi5gUM/92zrSoVZgo1K58O TTc7IteeLv++fInhIhmfCRH+7dHc4Qx/+KZeSS2r9b2RsrrpoWCy4Yre20Kh7962SZebTmi0VKx nAAA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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) Reviewed-by: Chris Down Reviewed-by: Gregory Price (Meta) Reviewed-by: Zi Yan Tested-by: Gregory Price (Meta) --- mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 97 insertions(+), 37 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index e6cd83cf32aa1..3bfa4f9dc5078 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1220,25 +1220,15 @@ static void migrate_folio_done(struct folio *src, } =20 /* 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 =3D -EAGAIN; int old_folio_state =3D 0; struct anon_vma *anon_vma =3D NULL; bool locked =3D false; bool dst_locked =3D false; =20 - dst =3D get_new_folio(src, private); - if (!dst) - return -ENOMEM; - *dstp =3D dst; - - dst->migrate_info =3D 0; - if (!folio_trylock(src)) { if (mode =3D=3D MIGRATE_ASYNC) goto out; @@ -1364,7 +1354,8 @@ static int migrate_folio_unmap(new_folio_t get_new_fo= lio, =20 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); =20 return rc; } @@ -1809,6 +1800,25 @@ static void migrate_folios_undo(struct list_head *sr= c_folios, } } =20 +static void migrate_folio_free_dst(struct folio *dst, + free_folio_t put_new_folio, unsigned long private) +{ + list_del(&dst->lru); + dst->private =3D 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 *fro= m, int pass =3D 0; bool is_thp =3D false; bool is_large =3D false; - struct folio *folio, *folio2, *dst =3D NULL; + struct folio *folio, *dst, *tmp; int rc, rc_saved =3D 0, nr_pages; + LIST_HEAD(pending_folios); + LIST_HEAD(new_folios); LIST_HEAD(unmap_folios); LIST_HEAD(dst_folios); bool nosplit =3D (reason =3D=3D MR_NUMA_MISPLACED); @@ -1845,7 +1857,14 @@ static int migrate_pages_batch(struct list_head *fro= m, thp_retry =3D 0; nr_retry_pages =3D 0; =20 - 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 =3D folio_test_large(folio); is_thp =3D folio_test_pmd_mappable(folio); nr_pages =3D folio_nr_pages(folio); @@ -1925,21 +1944,24 @@ static int migrate_pages_batch(struct list_head *fr= om, continue; } =20 - rc =3D 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 +=3D is_thp; + nr_retry_pages +=3D nr_pages; + continue; + } + + dst =3D 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 +=3D is_thp; @@ -1950,7 +1972,7 @@ static int migrate_pages_batch(struct list_head *from, if (!ret) { stats->nr_thp_split +=3D is_thp; stats->nr_split++; - break; + continue; } else if (reason =3D=3D MR_LONGTERM_PIN && ret =3D=3D -EAGAIN) { /* @@ -1963,18 +1985,42 @@ static int migrate_pages_batch(struct list_head *fr= om, /* Undo duplicated failure counting. */ nr_failed--; stats->nr_thp_failed -=3D is_thp; - break; + continue; } } =20 - stats->nr_failed_pages +=3D nr_pages + nr_retry_pages; - /* nr_failed isn't updated for not used */ - stats->nr_thp_failed +=3D thp_retry; - rc_saved =3D rc; - if (list_empty(&unmap_folios)) - goto out; - else - goto move; + stats->nr_failed_pages +=3D nr_pages; + rc_saved =3D -ENOMEM; + break; + } + /* Remember the source folio until we unmap it. */ + dst->private =3D 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 =3D dst->private; + is_thp =3D folio_test_pmd_mappable(folio); + nr_pages =3D folio_nr_pages(folio); + + cond_resched_tasks_rcu_qs(); + + rc =3D 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 +=3D 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 *fr= om, * 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 +=3D is_thp; stats->nr_failed_pages +=3D nr_pages; break; } } + + if (rc_saved) { + stats->nr_failed_pages +=3D nr_retry_pages; + /* We return rc_saved, so don't update nr_failed. */ + stats->nr_thp_failed +=3D thp_retry; + rc =3D rc_saved; + if (list_empty(&unmap_folios)) + goto out; + goto move; + } } nr_failed +=3D retry; stats->nr_thp_failed +=3D 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); =20 return rc; } --=20 2.56.1