From mboxrd@z Thu Jan 1 00:00:00 1970 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=1795947417; b=DDyIfmJfESkjNQS3w0D6UuknmyQixUf7VaXQiZ7h764ASjPWtndnymL5HDB9stL0Pax2 77Ahwhda6xcwddQqbGMUL9hF05HHWB8hml0Qv0dBveRwM97iL4bT6PWes20LdGMhQzbYG ROWay1NfYQe/6TcfgnoctlolWcOcmm/geq4ABBBQTBxdVDnVyDVkUn+rWIjXnOkoMEc7J FsQm9dZzvd7VRdBDFX2yzO2bMcpKIAuajp4Wf9zkH4Iq4Tz94mA6ydyGhYmtzx88K8M8Q Xv6EwE8U2n3A4vg4XpeidgThXvOVPBNowHe2n0/+F36tUVBG+0b+I600nQI+SwsEvZg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947417; 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=IncpRs0LYIzx25bW+NyjEufClOP69as3iYrx0AHOlB4=; b=F/w0o6y8OS0l7rBkVK3pyOCB4kbMfwS0cT1r1w0xS8lY8FpMMd+gZqBPVRQ2QYVgTI0o +6s44BbMr9jUAqn7NKXFkfvfj/883EfD0//KR+ZK8j95/0nHrciLClaAcwvryYScyR8Db knzgyaXaLCAS2dJGIxpJcW/shxAfG8JgH2oYnWdzYpiuAfX1u1P+ZtbMT3NedOoExTstJ NnJCj93k1W4IsXyDHEtrb3JxJv8SwFWt7u2vul+W8bp+WDb5xeJBbMtoqlthTBsFV5SVg sBekydAOmgglzW1PRexw1lyXjNftNuaofdNI9UuBcf1C6bc0gZNG+gmN3OLCwbWKw0w== 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=M8WpFJZ7; 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 0BB7F606A31C; Sun, 29 Nov 2026 10:16:56 +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=M8WpFJZ7 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78C8D1F0089C; Sun, 29 Nov 2026 10:16:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947416; bh=IncpRs0LYIzx25bW+NyjEufClOP69as3iYrx0AHOlB4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=M8WpFJZ7xKiQliZz6WPFkWR72uv9rEorC/2Dl443sJ/xa+hnui24WtjXnKERJAAmR WIvRoxrr8R8Eu7IrcEADuzt7+gZUzjlf/+wn/F0K7mTuUstjyb5grF2r9+qwUQxqIF saekbraPlH6N6ku2xDncVg1yIjM1x7iwJRArV/1r6z2f3C6AW0bLi/1YLPJ4mqs/ib uS3L8MfGynNFJ4fpMRvWFWQyOk+LLPGr779j83fO5cddbhoxSM5RUhpadRiLrwGPiM OcUi6xEwtUb6T9chFEwuzLxvlXOLDljwOewsF/xwMN4mORElz8SyNkjWTO6A2KJOU6 L5s4eBjeX16aw== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:07:03 +0100 Subject: [PATCH 32/32] mm/migrate: use consistent folio locking logic 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 Message-Id: <20261129-fixes_core_3-v1-32-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@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=8aucmLDxwb9vqGp+/EF6tlOTwYCIyLMEfVoY6By5Lbw=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+qppjdUiDVwSl+Ve7zMbcZs71nZVzfvdHknyMg/+ W6BUo9LRykLgxgHg6yYIktQ8H2Dly/X1Va78THDzGFlAhnCwMUpABPJEGT4H3qJPb1by3/rQdbD HwP3eH5ULg3P0eFLLedVrZ3PuFeNk5Hhd6ILwz5zO9FZG0MvzDJwrl1Z7OVx5PNq0bMbhLlrUs9 zAgA= 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 fbe12c4cd8abd..6ef46662bf53f 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1252,6 +1252,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) @@ -1262,36 +1293,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; @@ -1627,12 +1630,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