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=1796387374; b=HPtzg0g3SE7Gg5AVGDtiIWpL708DrSpNECw+hP6jF+AYilOLh/wJTPcJZuSIzoAyWkjL RqNALtnyXR9UG0hOEOUk8bLXTZ+WbQWVffa/twTONR6L152VHk/ySI56E3b0wEIInE7qw j1SxX9BhiXXsfspW1ERTmzJXiZO3R+x+AZZQjrI5xdkQg7omOJsVWgfTrSXxm2GLQEak3 M/3qzV8cStyXeFWHd54WV9T6/T37WJf5Z0Dcu2fDdN/0Wz84h/0S8+1ZVN0lGTxlNZjvV No1gIOa30iAZ3r6iAbuwJpP7TrFxJOBiPL8LCvkbG85cQ0oOGbBIzruu7mW0LU6in1w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387374; 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=kCCLmv/jy2XxwQ4pNBVOP0y5aZZVh6ieUolx25coeqI=; b=nNQ/FbXAL+fEwM1v1U1xsM0dWTF1yjQeqme7AuW2Tpp9Om8pjtu06+l/hlZJB6knOmTV 6iC/XeYR0SWjgN/+iqcf6JMwm5YA9Crcef2l/TuT/rNvPzGny2oPGEdbH95vP/mS+C+/8 ymPppYPEKCP+3VajZ46B2EV8GrKZERTo8OL3gNMQKS+YJ6Yuk5G3fhqhg6qKlLnLRW0Kt 5JlGRiTvUu/Di+oPaaWBJuxCGMmo87uOuMqT+MADjKfzdZ25WTumR+jtmHZGriRGR1OH6 wNT5LUTZ1cWTDw6/H+7FB23uxmc0/hGTUu9lEilFqDcY+tey4KoBZom5WP6hz4yrxSA== 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=FndLjr7l; 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 6718C606A32C; Fri, 4 Dec 2026 12:29:34 +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=FndLjr7l Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1576B1F000C2; Fri, 4 Dec 2026 12:29:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387373; bh=kCCLmv/jy2XxwQ4pNBVOP0y5aZZVh6ieUolx25coeqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FndLjr7lF46tJfIMoa7g8Y/vt46XkjrKC/JeLFxzFm0V2ZU3PMf8h3jcWIKx+DCyo zduQhZD97CPi4BxiNNyRLOWUaM/iqZDXf1uyaWWtnVtYgQjukUkQNs/8TQk73C1ZMH 4z6IaSeHBSrVferOpsJ4aV7YcXZbHiI7WwJ72gi8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Vernon Yang , Andrew Morton , "David Hildenbrand (Arm)" , "Lorenzo Stoakes (ARM)" , Barry Song , Dev Jain , Lance Yang , Ryan Roberts , Zach OKeefe , Sasha Levin Subject: [PATCH 6.1 0217/1059] mm: khugepaged: fix folio is used after pte_unmap_unlock() Date: Fri, 4 Dec 2026 18:55:48 +0900 Message-ID: <20261204100649.893850938@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@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 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Vernon Yang [ Upstream commit ba2f1f92ddfabd340c9248fd2380104f717ee8b1 ] After the page table lock has dropped, the folio can be freed concurrently. The trace_mm_khugepaged_scan_pmd() is left with a dangling folio pointer. So using the folio_pfn() before dropping the page table lock, closing use-after-free window. And other pre-existing bug, When the `for (i = 0; i < HPAGE_PMD_NR; i++)` iteration to terminate and the folio operation preceding is normal, but pfn will be incorrect. so we really only trace the PFN if it really was problematic. Link: https://lore.kernel.org/20260909025804.3233645-3-vernon2gm@gmail.com Fixes: 7d2eba0557c1 ("mm: add tracepoint for scanning pages") Signed-off-by: Vernon Yang Signed-off-by: Andrew Morton Acked-by: David Hildenbrand (Arm) Acked-by: Lorenzo Stoakes (ARM) Cc: Barry Song Cc: Dev Jain Cc: Lance Yang Cc: Ryan Roberts Cc: Zach O'Keefe Cc: [ sashal: Replaced upstream's folio_pfn(folio) with folio_pfn(page_folio(page)) at the shared-PTE exit, since folio isn't assigned yet there, and kept the older function names and the tracepoint's writable argument. ] Signed-off-by: Sasha Levin --- include/trace/events/huge_memory.h | 6 +++--- mm/khugepaged.c | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index 23bd4c1f984e8..54a85638ab99f 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -53,10 +53,10 @@ SCAN_STATUS TRACE_EVENT(mm_khugepaged_scan_pmd, - TP_PROTO(struct mm_struct *mm, struct page *page, bool writable, + TP_PROTO(struct mm_struct *mm, unsigned long pfn, bool writable, int referenced, int none_or_zero, int status, int unmapped), - TP_ARGS(mm, page, writable, referenced, none_or_zero, status, unmapped), + TP_ARGS(mm, pfn, writable, referenced, none_or_zero, status, unmapped), TP_STRUCT__entry( __field(struct mm_struct *, mm) @@ -70,7 +70,7 @@ TRACE_EVENT(mm_khugepaged_scan_pmd, TP_fast_assign( __entry->mm = mm; - __entry->pfn = page ? page_to_pfn(page) : -1; + __entry->pfn = pfn; __entry->writable = writable; __entry->referenced = referenced; __entry->none_or_zero = none_or_zero; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index ba41de673bc15..fabbcf2d735d4 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1142,6 +1142,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, int none_or_zero = 0, shared = 0; struct page *page = NULL; struct folio *folio = NULL; + unsigned long failed_pfn = -1; unsigned long _address; spinlock_t *ptl; int node = NUMA_NO_NODE, unmapped = 0; @@ -1218,6 +1219,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, if (cc->is_khugepaged && shared > khugepaged_max_ptes_shared) { result = SCAN_EXCEED_SHARED_PTE; + failed_pfn = folio_pfn(page_folio(page)); count_vm_event(THP_SCAN_EXCEED_SHARED_PTE); goto out_unmap; } @@ -1233,19 +1235,23 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, node = folio_nid(folio); if (hpage_collapse_scan_abort(node, cc)) { result = SCAN_SCAN_ABORT; + failed_pfn = folio_pfn(folio); goto out_unmap; } cc->node_load[node]++; if (!folio_test_lru(folio)) { result = SCAN_PAGE_LRU; + failed_pfn = folio_pfn(folio); goto out_unmap; } if (folio_test_locked(folio)) { result = SCAN_PAGE_LOCK; + failed_pfn = folio_pfn(folio); goto out_unmap; } if (!folio_test_anon(folio)) { result = SCAN_PAGE_ANON; + failed_pfn = folio_pfn(folio); goto out_unmap; } @@ -1268,6 +1274,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, */ if (!is_refcount_suitable(&folio->page)) { result = SCAN_PAGE_COUNT; + failed_pfn = folio_pfn(folio); goto out_unmap; } @@ -1299,7 +1306,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, *mmap_locked = false; } out: - trace_mm_khugepaged_scan_pmd(mm, &folio->page, writable, referenced, + trace_mm_khugepaged_scan_pmd(mm, failed_pfn, writable, referenced, none_or_zero, result, unmapped); return result; } -- 2.53.0