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=1796381063; b=Xy9KzMLz96tdE0Ai5+MJpWngGaHZT1AoFijSlfL98boy0K1sE3H48kARBlbxUyw+rz6b igL4gUu07I1NhiOlmZwQqd0PQuJAbW+kml7S6uPXPAexKfJZ/hzVfxOwekGcmT/a1uBjZ 8D6l5mrj1F9PwTZJOK1gvxNCzATi3DikHvKOr3bN9QNMOtFhExXo0d4yMaghaWdviqtq5 OYeJ3EMtY6kHxdvUdEbxuTInPrufN8vdD4b2OPh/90H5ejMTx1vSnF0eDwByBQfV18q6x BcHxBJqS/C0w4YWh3XOgvFlgl9iFsEDOqgsWjYpC+cK8/fYfpGmH0anc5usAT/ycvJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381063; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=5+4WJzVtwGnxuwynHkaziJwQ5AcMq3WsAjl5ZANHYU8=; b=DaAg+31gO2KRrGXjePEPFjkZ7+EvGHXt501HFFtsLPd6rfuGvCvOMwtl3T9dp4p7C6RE o1B886KPfYrlsH9PnIlwOla8wrVEkHkBe0dqOyv9SWMGpKZ56WJqm4CErW4BRICbsIQ+4 v2yBF2QMUqmJErd5KFLq5ML/bwL+xetDzy9ZNjGzv5p+K9voqcjHfybuVFOGOva9C79DI hoxUMoycvdICa8EWw1/1fVgEJ56yDV45uEZKsTsSMfst6lfCq2fpM0DOIi2c288Nmri4T GK//6PfUqJGsIzrc63Xh9YTSi18zfu3zLra0LiLMh/Mr/4xfQC0FIK2dRORd8GGBnqw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=xp3PQlQt; 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 001CF606A32D; Fri, 4 Dec 2026 10:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=xp3PQlQt Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3447C1F00897; Fri, 4 Dec 2026 10:44:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381062; bh=5+4WJzVtwGnxuwynHkaziJwQ5AcMq3WsAjl5ZANHYU8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=xp3PQlQtKNndsH9pnqMfkBLFxGz1Qj+ZMw45Zm2OZ+53ji3q600zxhXwDi48iGFDy dt3Wwl4v3JeB6aq87YylEeOL8qWFd31AJxLOemmVT4A0x74afhS/UUXSPjg7A61jCx Q2UVC8KSr1zEyugVQuo6EZ2/q4og84WJgk5H7u5E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "David Hildenbrand (Arm)" , Andrew Morton , Li Youhong , Usama Arif , "Lorenzo Stoakes (ARM)" , Zi Yan , Baolin Wang , Miaohe Lin , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Naoya Horiguchi , Wei Yang , Nico Pache , Luis Chamberlain , Tal Zussman Subject: [PATCH 7.3 543/742] mm/memory-failure: fix folio refcount leak and min_order_for_split() locking Date: Fri, 4 Dec 2026 19:06:47 +0900 Message-ID: <20261204100649.589362083@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) commit cd5f5bf781b4ead6a6571818d5613ea2ed0145f3 upstream. hwpoison code can end up calling min_order_for_split() without holding the folio lock. There isn't really something that would prevent concurrent truncation. Consequently folio->mapping can get set to NULL after checking for "!folio->mapping", and if the compiler reloads folio->mapping, mapping_min_folio_order() would try to dereference NULL. While very unlikely to happen in practice, let's just enforce that min_order_for_split() is called with the folio lock held. We can significantly cleanup the calling hwpoison code, and just get rid of try_to_split_thp_page() to hold the folio lock for a bit longer. Just work on folios now, which further cleans up the code. We just have to be careful about doing the page_folio() after splitting, which we have to do already either way. Do not change the way we split for now, this needs more thought and should be done separately. Cleaning this up we fix another issue: in soft_offline_in_use_page(), we would currently have leaked a folio reference. In min_order_for_split(), document and assert that we need the folio lock. Drop the questionable VM_BUG_ON_PAGE(!page_count(p), p) check entirely. While updating the comment in soft_offline_in_use_page(), just drop the "NOTE" part that does not add any value to current code. The folio->mapping problem was identified by Sashiko, and Li Youhong reported it by sending a proposal fix. This likely does not really warrant CCing stable, but I expect little conflicts when doing the backport, so let's just CC stable because of the refcount leak. Link: https://lore.kernel.org/20261030-try_to_split_thp_page-v2-1-c957c9b42eab@kernel.org Fixes: 689b8986776c ("mm/memory-failure: improve large block size folio handling") Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Reported-by: Li Youhong Closes: https://lore.kernel.org/r/20260804035828.2684059-1-dayou5941@163.com Acked-by: Usama Arif Reviewed-by: Lorenzo Stoakes (ARM) Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Acked-by: Miaohe Lin Cc: Liam R. Howlett Cc: Ryan Roberts Cc: Dev Jain Cc: Barry Song Cc: Lance Yang Cc: Naoya Horiguchi Cc: Wei Yang Cc: Nico Pache Cc: Luis Chamberlain Cc: Tal Zussman Cc: Signed-off-by: Greg Kroah-Hartman --- mm/huge_memory.c | 4 +++ mm/memory-failure.c | 63 +++++++++++++++++----------------------------------- 2 files changed, 25 insertions(+), 42 deletions(-) --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4564,10 +4564,14 @@ int folio_split(struct folio *folio, uns * If a file-backed folio is truncated, 0 will be returned. Any subsequent * split attempt should get -EBUSY from split checking code. * + * Context: @folio must be locked. + * * Return: @folio's minimum order for split */ unsigned int min_order_for_split(struct folio *folio) { + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); + if (folio_test_anon(folio)) return 0; --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1705,26 +1705,6 @@ static int identify_page_state(unsigned return page_action(ps, p, pfn); } -/* - * When 'release' is 'false', it means that if thp split has failed, - * there is still more to do, hence the page refcount we took earlier - * is still needed. - */ -static int try_to_split_thp_page(struct page *page, unsigned int new_order, - bool release) -{ - int ret; - - lock_page(page); - ret = split_huge_page_to_order(page, new_order); - unlock_page(page); - - if (ret && release) - put_page(page); - - return ret; -} - static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn, struct address_space *mapping, pgoff_t index, int flags) { @@ -2509,9 +2489,6 @@ try_again: folio_unlock(folio); if (folio_test_large(folio)) { - const int new_order = min_order_for_split(folio); - int err; - /* * The flag must be set after the refcount is bumped * otherwise it may race with THP split. @@ -2526,24 +2503,25 @@ try_again: * page is a valid handlable page. */ folio_set_has_hwpoisoned(folio); - err = try_to_split_thp_page(p, new_order, /* release= */ false); + + folio_lock(folio); + split_huge_page_to_order(p, min_order_for_split(folio)); + folio = page_folio(p); + folio_unlock(folio); + /* * If splitting a folio to order-0 fails, kill the process. * Split the folio regardless to minimize unusable pages. * Because the memory failure code cannot handle large * folios, this split is always treated as if it failed. */ - if (err || new_order) { - /* get folio again in case the original one is split */ - folio = page_folio(p); + if (folio_test_large(folio)) { res = -EHWPOISON; kill_procs_now(p, pfn, flags, folio); - put_page(p); + folio_put(folio); action_result(pfn, MF_MSG_UNSPLIT_THP, MF_FAILED); goto unlock_mutex; } - VM_BUG_ON_PAGE(!page_count(p), p); - folio = page_folio(p); } /* @@ -2861,25 +2839,26 @@ static int soft_offline_in_use_page(stru .reason = MR_MEMORY_FAILURE, }; + folio_lock(folio); if (!huge && folio_test_large(folio)) { - const int new_order = min_order_for_split(folio); - /* - * If new_order (target split order) is not 0, do not split the - * folio at all to retain the still accessible large folio. - * NOTE: if minimizing the number of soft offline pages is - * preferred, split it to non-zero new_order like it is done in - * memory_failure(). + * If we cannot split to order-0, do not split the folio at all + * to retain the still accessible large folio. */ - if (new_order || try_to_split_thp_page(page, /* new_order= */ 0, - /* release= */ true)) { + if (!min_order_for_split(folio)) + ret = split_huge_page_to_order(page, 0) ? -EBUSY : 0; + else + ret = -EBUSY; + folio = page_folio(page); + + if (ret) { + folio_unlock(folio); + folio_put(folio); pr_info("%#lx: thp split failed\n", pfn); - return -EBUSY; + return ret; } - folio = page_folio(page); } - folio_lock(folio); if (!huge) folio_wait_writeback(folio); if (PageHWPoison(page)) {