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=1796389189; b=LMfUII0wpqe3TQh6mloZWk7Z+VfVnoxQ5RlhxQqIyiSe6zCockPxpjrSP3xaGxXbKn3z fgF/K21hPmIUJrbV2EmZCW2/4wQJUPyDxrc+01mI+Rs+oDwFN9gyXo/nyGO2CCxhUnCAJ lJka3nClFKXvFUoEYw4G8xNGUFizQdR5dNaTk8AKohVSwvng0The38KxxT6l7pQhkA5TR W0otTqYIZjXLjMJzrTnAPLzj6yM8ARJjMWRlPzCGv50lUMg1N+YGqiEpc40HFT5uwApIv E7yU1nz2yy4cOFshbVxsc4H5uIznA4yu5YzS+c16HguwkKpr2YzspVm8fv3cflT1jyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389189; 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=tLpEN+dI15k48jyFCnWGjVJRFe8tets2BEbockSI/c4=; b=QCuYqLwinjrrPWya08A0TTcXfRahrp3CngGmCKGjaGrBhaXYnBgbgqb7GLRegi8JnYVD UY2nIKVt30yEN1fAjLXdbMwJfWjkMGJHhQhli7UiogJIGe2Jg4qrtipsKxv5xKZhvS+ch 0XTqO63fh6ybdb50szg+PgCP0h/1BdI/Lb/nhOyg39ye2g1l6Hg7OjbSzzHg3pygOzhHe Myb50wB2IuL8IPJ1+jUhsrOkiODlSVy6BJIGTe3E6jBVDBOAO0rl8b0677UEaDjBtOVJP TLEdNskRxyq5RSl9ls3GYcjFOF3t1TtFSSHt6XuMM3qeg5gg5mtKsc5Vrt6dmmuX9yQ== 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=DTfnuMdz; 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 D9B48606A33C; Fri, 4 Dec 2026 12:59:49 +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=DTfnuMdz Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACA1F1F00933; Fri, 4 Dec 2026 12:59:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389189; bh=tLpEN+dI15k48jyFCnWGjVJRFe8tets2BEbockSI/c4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=DTfnuMdze6qxg6Jfs4MpU6n3DWiKBkJXsOeDDeOnFJR8AxJHhQQk1b2a0QnWxrJLL lU0jDcY8Rl6GKTnIq0U4iOCY5ZgvNG7UgaNIdSKTGZ97fj6zpHr1mtDJns8RVEoOdS q6pUmorBGFbOPwdkp0OLd1u97xsZRZvyXwCcws4fw2k1IWgOWVSFKhYwy9PG2SS6Ou I87dTiMj+jmzLpwV64mWXWsznCM7Rm2pUFDGROoa4expM2aebI7Knsxe/xoGkDTPpg Gbx+OsN5zmhnLEf2IHSlxyG+BH0z2kliVbbRJ8Jo8X0B6bwhCPrQWb6Hn7zGsqKlyi c+KOqmzBYWwGA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:22 +0100 Subject: [PATCH v2 31/31] mm/migrate: use consistent folio locking logic X-Mailing-List: linux-cxl@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 Message-Id: <20261204-fixes_core_3-v2-31-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4829; i=david@kernel.org; h=from:subject:message-id; bh=KkOQ1ALQw0JxLHGeT0IdIQyYuVFA+U2FC6/HN6WkOrw=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjOGvTq19YxsNN/KzCl5ciZ/hGdUpITwB/kXiPGKV Uz7Zi/cUcrCIMbBICumyBIUfN/g5ct1tdVufMwwc1iZQIYwcHEKwEScmBj++wl/fCovF6Gr/O+I YIPOowPqblM+F11NanD/Erd4/vqTAowMW9MV8javWOj1ofXhwrUL4me3L62W+sKwoP7X8m/fPu/ NZAEA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A migrate_folio_unmap() never blocks on the folio lock during direct compaction to prevent deadlocks, as the allocating task might hold that folio lock itself. Further, in MIGRATE_SYNC_LIGHT mode, it does not wait for the folio lock of folios that are not uptodate, as that would effectively wait for I/O. However, try_split_folio() blocks on the folio lock in any mode except MIGRATE_ASYNC. Consequently, a task that holds the folio lock of a large folio while allocating memory could deadlock when direct compaction tries splitting that very folio: folio_lock(A) high-order allocation direct compaction migrate_pages_batch() try_split_folio(A) folio_lock(A) For example, shmem_swapin_folio() can call shmem_replace_folio() with the folio lock of a large swapcache folio held, where it will try allocating a folio of the same order. Some conditions make this deadlock currently impossible: direct compaction skips folios that are not smaller than the requested order. There are no known cases where we could currently deadlock. But it should be the right thing to do to make this locking logic consistent in migration code. Let's make it consistent by factoring the locking logic of migrate_folio_unmap() out into migrate_try_lock_folio() and using it in try_split_folio() as well. Signed-off-by: David Hildenbrand (Arm) --- mm/migrate.c | 71 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 4743bd76e93f4..4bd10c6170d42 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1255,6 +1255,37 @@ static void migrate_folio_done(struct folio *src, folio_put(src); } +static bool migrate_try_lock_folio(struct folio *folio, enum migrate_mode mode) +{ + if (folio_trylock(folio)) + return true; + if (mode == MIGRATE_ASYNC) + return false; + + /* + * It's not safe for direct compaction to call lock_page. For example, + * during page readahead pages are added locked to the LRU. Later, when + * the IO completes the pages are marked uptodate and unlocked. However, + * the queueing could be merging multiple pages for one bio (e.g. + * mpage_readahead). If an allocation happens for the second or third + * page, the process can end up locking the same page twice and + * deadlocking. Rather than trying to be clever about what pages can be + * locked, avoid the use of lock_page for direct compaction altogether. + */ + if (current->flags & PF_MEMALLOC) + return false; + + /* + * In "light" mode, we can wait for transient locks (eg inserting a page + * into the page table), but it's not worth waiting for I/O. + */ + if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(folio)) + return false; + + folio_lock(folio); + return true; +} + /* Obtain the lock on page, remove all ptes. */ static int migrate_folio_unmap(struct folio *src, struct folio *dst, enum migrate_mode mode, struct list_head *ret) @@ -1265,36 +1296,8 @@ static int migrate_folio_unmap(struct folio *src, struct folio *dst, bool locked = false; bool dst_locked = false; - if (!folio_trylock(src)) { - if (mode == MIGRATE_ASYNC) - goto out; - - /* - * It's not safe for direct compaction to call lock_page. - * For example, during page readahead pages are added locked - * to the LRU. Later, when the IO completes the pages are - * marked uptodate and unlocked. However, the queueing - * could be merging multiple pages for one bio (e.g. - * mpage_readahead). If an allocation happens for the - * second or third page, the process can end up locking - * the same page twice and deadlocking. Rather than - * trying to be clever about what pages can be locked, - * avoid the use of lock_page for direct compaction - * altogether. - */ - if (current->flags & PF_MEMALLOC) - goto out; - - /* - * In "light" mode, we can wait for transient locks (eg - * inserting a page into the page table), but it's not - * worth waiting for I/O. - */ - if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(src)) - goto out; - - folio_lock(src); - } + if (!migrate_try_lock_folio(src, mode)) + goto out; locked = true; if (folio_test_mlocked(src)) old_folio_state |= FOLIO_WAS_MLOCKED; @@ -1630,12 +1633,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_f { int rc; - if (mode == MIGRATE_ASYNC) { - if (!folio_trylock(folio)) - return -EAGAIN; - } else { - folio_lock(folio); - } + if (!migrate_try_lock_folio(folio, mode)) + return -EAGAIN; rc = split_folio_to_list(folio, split_folios); folio_unlock(folio); if (!rc) -- 2.56.1