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=1793306159; b=wVqM5NvgiGMYa8DqcS3p1BqYpUgnWD/PNA6FmIzQo86kY9jQCr/ARH1OwDCoyArR+PaQ s/7qv7k2VvT/zIWjB+Txrm4FQF7S6i2DAurf2NCiPZsdiW2QvfjO3eJOa92T0cwDB6jhd 0euIp6Iz3HRmN0MyBmeon479znp37/bjbtecdfVIZERjwCs6RzkPwD79JZpCOb1ZQ1hhB 2mWjHeWG7mp+ZUjTUW6EFvW+vWeN1jvYon/CwnQLwSvl7iHxRMxCBDbIrq1nkJURwZEOt kF6LvGaozFsC57uDsy7C8EM2F+z7KdQu+lpJQ9JskGa04Wrub/NCHR6ZsTOENfqQijw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793306159; 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=/fGHxJQ6UBEKb1MyrsQbaeyi9F3Q1SAWrZUXTnnmA+s=; b=lFg+IQTMGKOoaWVLllRTUPzZCocBrV5P9s7fjD36Xe6jxfNsDSyL7006ZKLDlGlQwpJx SCLHyNXJgnw7ulqb13NE+uHuOSgY0SYBvrdRF21EJB/Chdo+I7bv+L2meg65tDiSbI2Sm 50GdXWhPjSB8pnXak90rNlsAT2TdDJu+UZq0NTVdFMoqb+tAbiWX2XLDZ52NngSPw1XVK ICVPnXQUKPXynxUEB3Vp2FUSzZgw4g3ZIYg/8jP3xK/897T3azHbjf4MLeAnlfEhYP5b3 LCvwqiBy+2arcwp5hpBNmaios6lKVfRTCTYyxHohMjp3J+p+tLdqkRZAmcnpVrC3viQ== 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=deQ6JqP8; 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 2241A60A260C; Thu, 29 Oct 2026 20:35:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=deQ6JqP8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 349FC1F01816; Thu, 29 Oct 2026 20:35:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793306158; bh=/fGHxJQ6UBEKb1MyrsQbaeyi9F3Q1SAWrZUXTnnmA+s=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=deQ6JqP8E39elc/a/zDI3+44nUc0V+UdqENmn8aSAT3ekcvuHumd/vR6ilUWW0Que 489/6FXtF1BOAUvwnNIPwaXfBuawdAl8VT9J80QprdmAlBsAJp0OkQ0aYjMRuAhl+7 ZWcI+3x+EWEkxWPsRXduBT/JJHH+tH9AoCoXtpQbiq+gQUi3eroUna1TQZBM6u23OK UsqBgKA8bSEkz2vSeRRFH5PpgSO5IealTYBMlSoTrJvEKvsIZj3PJwJS/p0ba85nXQ PJya3A6K5AyqI9/h427FaLsWkW5tINAXhitEpp8Phx65nPxL5yci6UxOFUXZw00Db4 tEzt/Qnn1kQmQ== From: "David Hildenbrand (Arm)" Date: Thu, 29 Oct 2026 21:33:05 +0100 Subject: [PATCH v2 10/47] mm/huge_memory: fix list corruption when deferred_split_scan() races with deferred_split_folio() X-Mailing-List: linux-arch@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: <20261029-fixes_core-v2-10-a64ce7f83abb@kernel.org> References: <20261029-fixes_core-v2-0-a64ce7f83abb@kernel.org> In-Reply-To: <20261029-fixes_core-v2-0-a64ce7f83abb@kernel.org> To: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Peter Zijlstra , Jann Horn , Pedro Falcato , Hugh Dickins , Pasha Tatashin , Pratyush Yadav , "Matthew Wilcox (Oracle)" , Jan Kara , Zi Yan , Lance Yang , Rik van Riel , Harry Yoo , Alistair Popple , Nico Pache , Ryan Roberts , Dev Jain , Usama Arif , Kiryl Shutsemau , Matthew Brost , John Hubbard , Peter Xu , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Oscar Salvador , Minchan Kim , Muchun Song , Xu Xin , Chengming Zhou , Chris Li , Kemeng Shi , Nhat Pham , Youngjun Park , Gregory Price , Joshua Hahn , Rakie Kim , Ying Huang , Jason Gunthorpe Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org, stable@vger.kernel.org, Josef Bacik , Mani Tofigh , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3649; i=david@kernel.org; h=from:subject:message-id; bh=sU0SgxqnvF4Yi3G1bVV3Axo8VYJH1CcFJAyepsrgHZA=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIer23dYvp22dKO2if7usK4Vsgdm/zsU/hxlk/O0yueT VXbnhJ6qqOUhUGMg0FWTJElKPi+wcuX62qr3fiYYeawMoEMYeDiFICJ7HzNyHCgv/Vn4CU56Qq9 kgtLyzP83L+8v53ePvtsj0zKS4VmbhWG/5F7BeeW1ygZzmPjEugR2RVgx/0oqm1bLsvSJ37dJye XcgIA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A Since the deferred split queue became a list_lru, the isolate callback moves the folios it took a reference on to an on-stack list, linked through folio->_deferred_list, and deferred_split_scan() takes them off that list with list_del_init() without holding the list_lru lock. deferred_split_folio() does not know about the on-stack list. It only holds the list_lru lock when it looks at the same list_head, and it can run for such a folio: a THP is queued when it is mapped, as a candidate for the underused check, and is queued "again" when a part of it gets unmapped: CPU0 deferred_split_scan() CPU1 deferred_split_folio() list_del_init(&folio->_deferred_list) next->prev = prev prev->next = next entry->next = entry list_lru_lock_irqsave() folio_set_partially_mapped() __list_lru_add() list_empty(item) -> true list_add_tail(item, &l->list) item->next = &l->list item->prev = tail entry->prev = entry The folio now sits on the list_lru with ->prev pointing to itself. The next removal leaves the neighbours pointing to it, and they still do so after the folio has been freed. With CONFIG_DEBUG_LIST: list_del corruption. prev->next should be ffd4000000270090, but was ffd4000000278090. (prev=ffd4000000270090) Fix it by grabbing the lock around list_del_init() so deferred_split_folio() cannot race with it. Fixes: fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru") Cc: stable@vger.kernel.org Reported-by: Josef Bacik Reviewed-by: Johannes Weiner Reviewed-by: Mani Tofigh Tested-by: Mani Tofigh Reviewed-by: Lance Yang Signed-off-by: David Hildenbrand (Arm) --- mm/huge_memory.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index bd44d58e8684..4c0194af9016 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4522,8 +4522,7 @@ int split_folio_to_list(struct folio *folio, struct list_head *list) * * It is unsafe to call folio_unqueue_deferred_split() until folio refcount is * zero: because even when the list_lru lock is held, a non-empty - * _deferred_list might be in use on deferred_split_scan()'s unlocked - * on-stack list. + * _deferred_list might be in use on deferred_split_scan()'s on-stack list. * * The list_lru sublist is determined by folio's memcg: it is therefore * important to unqueue deferred split before changing folio memcg. @@ -4665,10 +4664,22 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, deferred_split_isolate, &dispose); list_for_each_entry_safe(folio, next, &dispose, _deferred_list) { + struct mem_cgroup *memcg; + struct list_lru_one *lru; bool did_split = false; bool underused = false; + /* + * Sync with deferred_split_folio(), which can requeue the + * folio once list_empty() is true, and sets PG_partially_mapped. + */ + rcu_read_lock(); + memcg = folio_memcg(folio); + lru = list_lru_lock_irq(&deferred_split_lru, folio_nid(folio), + &memcg); list_del_init(&folio->_deferred_list); + list_lru_unlock_irq(lru); + rcu_read_unlock(); if (!folio_test_partially_mapped(folio)) { /* -- 2.43.0