From nobody Mon Sep 28 23:53:40 2026 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A39DC1E5B68 for ; Sat, 15 Aug 2026 05:20:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771205; cv=none; b=PKoYCxQet4FCXeUYo4IY6IARKSm1fWyKUEXJ54Ht/EuiYwaJ9p6eTZy+sDBeQGCgg0QSh4/3pbMN31P+jHpEhSJSMvsOg8SPAdi5LJKJidN76wPAPjzmcG7n4kg6zhb9CrIRL3FmrDLuxz7PvNV3GNIiDSDdzzoC6+XGw/8y3N8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771205; c=relaxed/simple; bh=2r1RF881LYsHVlBEr4htWYzA6gwwa9pnKcUsZN5CSdE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uxbe4sjZ9vji12k69GMuH0jADB+8JmE5KhvlFzvvxGmrmYfB14NodTTrEXPpZgffcfKf9fk0+78jrTCgS0NT2gI/WYnfrF83i4YN5UCNRUSZOrhpPODv2awrPWUDNr9mumw+5/XTYomhiNqy2fdeS21eVAiL+n7VZBkSuY96gh4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XpppSY+S; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XpppSY+S" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-3811f512167so2482503a91.3 for ; Fri, 14 Aug 2026 22:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786771204; x=1787376004; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/yDUp3cp/r1Pc+NMXZTiNUemFBBGGof6Vn7/JKGoQAI=; b=XpppSY+SwcweMS6++TdSfy0vRvVfPfk0bqIKbTf/VMyuacUDz4eTB4IavWfpjq85EV 99mHD2Fl1z5AgAOdSPhHygGPoKPvK0c6q73PYKLVkyz3YXRzsVafzxuhp4FA8SWiyYq7 Hzg+nRSPEn/l/I1ndlvUFAMzo+HDiupEeBia6mGI1u+GNbsUUGDDL4uddU/SuxY0geWd MWsmIsQA955yk2Y1Ycy8XQa0n8s0lt812q2GortHv5oS2G6D6E8Xtri9SQ/utIG5tueH zD/HKj98BB625OMle7i6vgix19ZlyBeHVTiUlR08VbuhLywbTirrs7OZbEAsqVr/HC4G 06/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786771204; x=1787376004; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/yDUp3cp/r1Pc+NMXZTiNUemFBBGGof6Vn7/JKGoQAI=; b=O2Ihdz4AYzTngPoazUPhW1+9pqkkgUvDZOPZIaDrGO9CWxsz3/062HtzxAaGXEt8J0 pKqu9AV2FYe3RisErGzXpMpFzZVNX20FDQURujcu5lyULwdaftYE2pZDwIA7YRA2d6Ps rNHIW07zxH1xe2Jae4KkAHRyZZljisYufndbXkP0bd8bV4Xk6gAeT6T8rUni2gYVFmn8 sy1/BB/8D0LXTv6e4ymzBBMVPymS6IgkCXQHKQCQkucjG8Iji3iStVZJN6tMScvFC9Rd jwPBZ/8qox4rXKa00/xmyudpRQyAkBEg4gp7wb5IlT/jYBAa5jWJM0wEkE7rGTXNZOI0 30qA== X-Forwarded-Encrypted: i=1; AHgh+RqwJum/mAhZa5thThTOv5lSL1k2eLz2mktvpnn0DqDkGvDok9qE2/YpMcXdarNgpnMz+K7SAL3KJ+eCiDc=@vger.kernel.org X-Gm-Message-State: AOJu0YwjFECBsTogQxfT0cK4hY+Q6KQQqQzNph4g21UakcNT95OTYc8W feOdmgynljY3hwSqkU4cBIyZZJ7g7fiSdzKY1dPUgpNwS8DQEDkyu2oA X-Gm-Gg: AR+sD10dKEsudGg0zzagH1IjnjY8OdVUL63UzJPqXBdVqu3TnmXUXnZY/m0U8gEqzBU vXQbIyFmIo9slB9+a/ZUNlrN+bEiwsFYeSsocyjQD6TSfZ1J8VP/JpHIc6YGjND44YAZbSipBoJ rHqAVeTf3XmSAxo6e0VWSuYqiHUdGX9bZuUs4W9hXprMSG20AuaKgUEQcoLoKgx8qr+xLikDHEJ gIXFRJ4GuteQrkVqhjBRy4Wqp3sfwzDir93Z5TGEJVXKshGMUtw0AUuSbzjH0uRb7l3anOvkQgf Navwro4n0itkVNLZS0KbRhuokhBbJGmydN5tFGoeMTp5jov+no5yg+CBzPa2QWb7Yza78SDxNa2 VGZ+r8QsftY5p9gDYxUhY5o6jX1P+WCH5RkKdTEtWPg3CTmscurag+kA5FJq4EwjDkdF5sy3/XK 0krCzdYTDX04ROUyZQ7I0bz7Rr9HiZ4RZ6XFKqMv1vZ+y8MBqhIYJn7MhY5wPAx7kHwfLy4nu46 JiDF//JQBY= X-Received: by 2002:a17:90b:1dcc:b0:38e:6d4c:14e6 with SMTP id 98e67ed59e1d1-3933b77c874mr13623090a91.3.1786771203820; Fri, 14 Aug 2026 22:20:03 -0700 (PDT) Received: from localhost.localdomain ([49.79.111.198]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-394ea96c7fcsm5269702a91.3.2026.08.14.22.19.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Aug 2026 22:20:03 -0700 (PDT) From: Vernon Yang To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org Cc: nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, usama.arif@linux.dev, zokeefe@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vernon Yang , stable@vger.kernel.org Subject: [PATCH v2 1/3] mm: khugepaged: fix swap entry value to folio_pfn() Date: Sat, 15 Aug 2026 13:19:22 +0800 Message-ID: <20260815051924.194810-2-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260815051924.194810-1-vernon2gm@gmail.com> References: <20260815051924.194810-1-vernon2gm@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vernon Yang When the swap entries found exceed max_ptes_swap, the loop is left via break with folio still holding the xarray value that encodes the swap entry, not valid folio pointer. That value is passed to trace_mm_khugepaged_scan_file(), which feeds it to folio_pfn(). On FLATMEM and SPARSEMEM_VMEMMAP, the page_to_pfn() is plain pointer arithmetic, so the trace event merely prints bogus scan_pfn. On classic SPARSEMEM, the page_to_pfn() reads page->flags, dereferencing the tiny encoded integer and oopsing khugepaged whenever the trace event is enabled. So when folio is the swap entry value, simply set pfn to -1, just like exhausted scan naturally. And the folio_put() has maybe dropped the last reference of folio. The trace_mm_khugepaged_scan_file() is left with a dangling folio pointer. so using the folio_pfn() before dropping the reference, closing use-after-free window. Fixes: d41fd2016ed0 ("mm/khugepaged: add tracepoint to hpage_collapse_scan_= file()") Cc: stable@vger.kernel.org Signed-off-by: Vernon Yang Tested-by: Lance Yang --- include/trace/events/huge_memory.h | 6 +++--- mm/khugepaged.c | 14 +++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge= _memory.h index 291fae364c62..d3572d4ef453 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -178,10 +178,10 @@ TRACE_EVENT(mm_collapse_huge_page_swapin, =20 TRACE_EVENT(mm_khugepaged_scan_file, =20 - TP_PROTO(struct mm_struct *mm, struct folio *folio, struct file *file, + TP_PROTO(struct mm_struct *mm, unsigned long pfn, struct file *file, int present, int swap, int result), =20 - TP_ARGS(mm, folio, file, present, swap, result), + TP_ARGS(mm, pfn, file, present, swap, result), =20 TP_STRUCT__entry( __field(struct mm_struct *, mm) @@ -194,7 +194,7 @@ TRACE_EVENT(mm_khugepaged_scan_file, =20 TP_fast_assign( __entry->mm =3D mm; - __entry->pfn =3D folio ? folio_pfn(folio) : -1; + __entry->pfn =3D pfn; __assign_str(filename); __entry->present =3D present; __entry->swap =3D swap; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 617bca76db49..e7830761d3a2 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2683,6 +2683,7 @@ static enum scan_result collapse_scan_file(struct mm_= struct *mm, int present, swap; int node =3D NUMA_NO_NODE; enum scan_result result =3D SCAN_SUCCEED; + unsigned long pfn; =20 present =3D 0; swap =3D 0; @@ -2720,27 +2721,23 @@ static enum scan_result collapse_scan_file(struct m= m_struct *mm, * PMD-sized THP implies that we can only try * retracting the PTE table. */ - folio_put(folio); break; } =20 node =3D folio_nid(folio); if (collapse_scan_abort(node, cc)) { result =3D SCAN_SCAN_ABORT; - folio_put(folio); break; } cc->node_load[node]++; =20 if (!folio_test_lru(folio)) { result =3D SCAN_PAGE_LRU; - folio_put(folio); break; } =20 if (folio_expected_ref_count(folio) + 1 !=3D folio_ref_count(folio)) { result =3D SCAN_PAGE_COUNT; - folio_put(folio); break; } =20 @@ -2759,7 +2756,14 @@ static enum scan_result collapse_scan_file(struct mm= _struct *mm, cond_resched_rcu(); } } + if (!folio || xa_is_value(folio)) { + pfn =3D -1; + } else { + pfn =3D folio_pfn(folio); + folio_put(folio); + } rcu_read_unlock(); + if (result =3D=3D SCAN_PTE_MAPPED_HUGEPAGE) cc->progress++; else @@ -2774,7 +2778,7 @@ static enum scan_result collapse_scan_file(struct mm_= struct *mm, } } =20 - trace_mm_khugepaged_scan_file(mm, folio, file, present, swap, result); + trace_mm_khugepaged_scan_file(mm, pfn, file, present, swap, result); return result; } =20 --=20 2.53.0 From nobody Mon Sep 28 23:53:40 2026 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41142230BE9 for ; Sat, 15 Aug 2026 05:20:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771211; cv=none; b=rYSHjIeXetYfnTNx/83vVwHWyE/HQNp24S5YXm0I1mJyDtXVFrR6lfObsqReDZgI+rmndIeRjL05bMwRixps7359IE3wzqRUOF3llrm2Pq1iLOIm2rLi1c6qheP08NlnRS8nH0INOkRXTWUC0koAYVuJWBzXcVvDOkPSHWZUPDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771211; c=relaxed/simple; bh=pyjg4tQzeWbPwGazrdi1gvyH5AHb8VIEL7W7KMpA1Sg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dPdtdMyiyegasvEyZDx472nTBXuxmIT2kMhUPo0YMrKdAqxRVzHjMgfA+6GJluVYiZ/gff3AhVtzLnHs9SOxZfzhhXyECYJ0AR5VySu2zvaLmunX/GknxbCmEKIhvB8uBtDyfXJceK0J3XKusIfb8vTZc/8u++DUWKUPNeWWcbk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fPM9uKk5; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fPM9uKk5" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-381b831d535so2693006a91.0 for ; Fri, 14 Aug 2026 22:20:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786771209; x=1787376009; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2LNNcf+87LrqHmKQ3ILDbepTdxy8o+GDR7o/oD8fAmg=; b=fPM9uKk5bydwBUFPCbKodcuiLb4a+vBi/4JI/8FWMvGifiAaq7b4I0KAPFcNzzzYD+ lqx60DIgXhzAf2/cjnuGJzloo/QgQi+J45LS75M+UnuA5d2YauggNrIg3x5ZaM8QQFUF 0t75/I8afq1Ls8P40Y124a54qTpr7j5EdY+qWTwJ6kGanSL3TvL7U7mAXKPhNidcTZIZ /uXV4Jf/2Xr8uswjnMY19Kc0CzGWbNC1K2rW21M6mWhl502ouggfRoYyikFcR7q7EW00 Qdq4Iofw3cHGxqHitmKKeKnSG7qeHjExRybFrqXV9/yPz5+valtwc5QhJTqcK8Djjkx1 Nlrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786771209; x=1787376009; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=2LNNcf+87LrqHmKQ3ILDbepTdxy8o+GDR7o/oD8fAmg=; b=Ou4f9fTmQzg++g9U8gBXbMVti2Qh1ylkywDqt39Ma3ObKMXOxURk9DrW3PaVIL8Pwf 4Xy8AnaGmPTTl01jPMg2XrgVK1Z0WSJB7t4OZoM811nJ/Tqel6h+stJ8G9mus5/ExeBV XphyZNM1zN+ueRXCL3e/eqjLeJ8HxnDjX9txF+713g5ln+i4rdx96pVeTz1mWdfG98D/ 9YM2qVIIANYgVDP/oaHS8AVYwCc+HEqN+XCule4m7+MExU4+RmQwl8rujWX6ZwwnIYPj j0OplqZpO4gOR6E3YDc4aERfciYOtT+RmU4jFFpYnkoPSPNskSG89Bk+0/lTuakbD+KG rdxQ== X-Forwarded-Encrypted: i=1; AHgh+RrsDPU38pH+FyQbYjpgW0uuzNHFjTuASkiIOIfh5H5VivC85Se9UG8iJxtIh9NdxPWpfMPf0TfL8P9NjgQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7o0ETZqccIBYbN75TZ4QHotr0jdDH9kR5CL/KMpXSsSucWDtv 6f7kqc/3r+3rk5HWnHDoa5MWJ0niRf4BEIVZ3LUKXSqJeF+vpZIKDFDOCIConlXM X-Gm-Gg: AR+sD11s4CVPW5usFd+4teM4NQCaQ0Wzkakoe8+U33oTzDtjhdpjGgS5MKjp7qpv0mI xcTDlr70dTji/HJwr0e6y69qjV1LPYHu3r96kL0/BSMW0TDDLlPjf8qZv7y3qZxvoUwfXoZCyD+ 44+qJ4DBm8IgYgbWlvNEF5Z0rWMNdzElNBY3T0WnFSgZ0HM3yKQQRXdlJ1x1qadou4Wk0AEkaj2 NWp4lSTZtwwkOLIJWLVW/Udev5989mSCUGy0dMWfFMIcdze4vuqR1l+JQblDSx0HygFCfyQLxZH zZr39ljB5W0dWk4JNu/9dyytvO3lHg5I/uJkxMJyisTxvbRv6DI2fr2dLWZNF39Qc4LhH7fh3V2 xDY9buXiKagcIY0KlWYlsbDgxgE3MjTt9fB4nwxiDRuWUuNgeSLZinZgsecwIBcVrZz9o1RL2oQ JNVq4nrm46xOfUMzi0tPEeZ10JdCgVciIDqVAS4vmE3he/aFCiMjMD92S6glQ2E8rvPrkZ9XF0 X-Received: by 2002:a17:90b:48ce:b0:390:b41a:b92b with SMTP id 98e67ed59e1d1-3933b6d05c3mr13639215a91.4.1786771209186; Fri, 14 Aug 2026 22:20:09 -0700 (PDT) Received: from localhost.localdomain ([49.79.111.198]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-394ea96c7fcsm5269702a91.3.2026.08.14.22.20.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Aug 2026 22:20:08 -0700 (PDT) From: Vernon Yang To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org Cc: nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, usama.arif@linux.dev, zokeefe@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vernon Yang , stable@vger.kernel.org Subject: [PATCH v2 2/3] mm: khugepaged: fix folio is used after pte_unmap_unlock() Date: Sat, 15 Aug 2026 13:19:23 +0800 Message-ID: <20260815051924.194810-3-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260815051924.194810-1-vernon2gm@gmail.com> References: <20260815051924.194810-1-vernon2gm@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vernon Yang 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. Fixes: 7d2eba0557c1 ("mm: add tracepoint for scanning pages") Cc: stable@vger.kernel.org Signed-off-by: Vernon Yang Tested-by: Lance Yang --- include/trace/events/huge_memory.h | 6 +++--- mm/khugepaged.c | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge= _memory.h index d3572d4ef453..5dc71d292f47 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -55,10 +55,10 @@ SCAN_STATUS =20 TRACE_EVENT(mm_khugepaged_scan_pmd, =20 - TP_PROTO(struct mm_struct *mm, struct folio *folio, + TP_PROTO(struct mm_struct *mm, unsigned long pfn, int referenced, int none_or_zero, int status, int unmapped), =20 - TP_ARGS(mm, folio, referenced, none_or_zero, status, unmapped), + TP_ARGS(mm, pfn, referenced, none_or_zero, status, unmapped), =20 TP_STRUCT__entry( __field(struct mm_struct *, mm) @@ -71,7 +71,7 @@ TRACE_EVENT(mm_khugepaged_scan_pmd, =20 TP_fast_assign( __entry->mm =3D mm; - __entry->pfn =3D folio ? folio_pfn(folio) : -1; + __entry->pfn =3D pfn; __entry->referenced =3D referenced; __entry->none_or_zero =3D none_or_zero; __entry->status =3D status; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index e7830761d3a2..7c8c48577408 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1603,6 +1603,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, enum scan_result result =3D SCAN_FAIL; struct page *page =3D NULL; struct folio *folio =3D NULL; + unsigned long pfn =3D -1; unsigned long addr; unsigned long enabled_orders; spinlock_t *ptl; @@ -1778,6 +1779,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, result =3D SCAN_SUCCEED; } out_unmap: + pfn =3D folio ? folio_pfn(folio) : -1; pte_unmap_unlock(pte, ptl); if (result =3D=3D SCAN_SUCCEED) { /* collapse_huge_page expects the lock to be dropped before calling */ @@ -1788,7 +1790,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, *lock_dropped =3D true; } out: - trace_mm_khugepaged_scan_pmd(mm, folio, referenced, + trace_mm_khugepaged_scan_pmd(mm, pfn, referenced, none_or_zero, result, unmapped); return result; } --=20 2.53.0 From nobody Mon Sep 28 23:53:40 2026 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACD2E29C327 for ; Sat, 15 Aug 2026 05:20:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771216; cv=none; b=egeIBAEqptXDs3EKTl2/F7B8sjayLjnymY0MMnoNoK5LlrQDEEf1ktAHm2qLcym24i+6of3y2KUAFYXzX7UypXrnR9C/1Pn470RhYZUe+piwueCOsfX/R5dG4AOkoQb1LFHMA485geJOUBsbnYW5aJwca+Sl01OuwDPSu3gWIx8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786771216; c=relaxed/simple; bh=jfucFgPT6pmFcjHVONsvKl7Ym2miaiPtaKIoa58Whlo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dwMea95GOskNOeMAkytKGM05bxLBDFRxVt9sINnbhXVOGgYjteAM2l3Y0fo215BUiOYXAX5Cwp0fuF0Hulkd2Dk1Ohvup14IQkJKU55wr5bsXx84ztUvySHFhsWASX75HFwNvSvj1dO8nBReuDuajtky+Omc4s4rcZmKolVw5f0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AK4kvOyZ; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AK4kvOyZ" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-38e041ea211so1893919a91.0 for ; Fri, 14 Aug 2026 22:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786771215; x=1787376015; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MGLWuLEG0kDSgM2QRoCYSrx2dw3K2Ihdk1BDWCYkYo4=; b=AK4kvOyZXbCLRVel34vgjAj8laW0nH+gBSvjbQhawT2FA80pUV7VevN78HTdLAzjcg Tj2x+xk3XiYUw3P/59PIe7+3HlCYzEcBfqYygJfmwfLaSVLn9y3+4bIVX0s54i+H+9NL 0xXewOu8O6jWBE58rBlq8PbAR8/GTFHaCtpRxB3yfeq1SEYvDcg+d8eHPulzu8oXwor8 2ohm4eRc/N+Cd3gQ3KNYJW6YSTFfst0uSPnWRIxu/qEy2bSlcDlBVfmNquLWXVaBeD7w U3AYnFwQqrv/2x4eA0J7D6jynwagxDZmCQcdTFpCnBEvBwnw/Gt43bkUFO0Aq2YGyt+V Ibhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786771215; x=1787376015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=MGLWuLEG0kDSgM2QRoCYSrx2dw3K2Ihdk1BDWCYkYo4=; b=mv+FoBLdwC7GODLBN0grEP3faQp1VnIOlGMZE/A/Z/UVesLuhG0h0PVUp4CHth4nAQ rHOXsWtKHO7S4e78Q3be6qBvPqLWskuMs+CLCdal2fBY07TIYPGF1faLo9/t2hxJ7nXV GzyqKwyS1lVA3zxtzehkFI3eP96Lecm3fcp4RiOMSnPvmBaehadnpoSdLg004rGORmYp l20ATduwYZXrh+Fb0583jMby9lNEQyfZXhv+6ELldUxIL7HrHd+6q/refpOtDXrUP+wn mHS7UKYllNG7ZzpUhoACSIMiCY3aECnwtopP9QnWmKuY3yU85BeZpettzO6zFUutgbGE uq/A== X-Forwarded-Encrypted: i=1; AHgh+Rp9YyXxBHxo8Z2XJzFA5XY4l6oxdVlNwEJvSt4ePU8wFByEpsF7+b1f+ry7GrejLdqc/4OCl95iXi7y+2I=@vger.kernel.org X-Gm-Message-State: AOJu0YyZzSDJtxXH2hkD4u8KRC/JlfO1pADPGX0qUn2s29tQauCBY/vz WcpXBz5f6ac2DyBcPOQHy+39nnHIJgr5VzPDDWvwL5d+GZ2xefqvRsg1 X-Gm-Gg: AR+sD11wZeMxmvR8qxQIavHhs1iApooxKxVOy8oNWpnYyXl/L+1ZUCLZMzZvD81oDGB FS5/enqChueAlEHJFL5EInvsYmuD0UMwAYmmp46Qj7yNVUsq/4T+H4odKnvtx1zIi93PVY0HhP2 gL2lv7cfxI40WaUR3hwPsUuYwOUBeSSP3LE0y+gzwOK6P1vrotteYBhebp/zvLISOcA16oYUglm nrVS0jN1tKV0t7ROVwr47pRW89lR7wC1CzBATrqXTw84DGeceh2pHAE9jPn0dY1kduj+7WBhvHX j5Z70JKAKlb4+pgMkXlnFKz3prE0RJEmImPZU+AOQto4Vcl3YLM09+iMzG1sSSVlE+KaI/8mIi7 XCT1swgSXzutwK4JpIXdv/nwE3ca5i/xXEQ26REAfEgUnBWI8XGlb6KhIjGIOufB/94oN2k4q9E lpVS8nOs414fyZi+9IUKHUOVLN7jRQOkx9JwT2hWZDHiYiDLCkIxxXdsTpxHKwAZ2Sx/Lw5s3a X-Received: by 2002:a17:90b:4a8c:b0:381:bc4c:da56 with SMTP id 98e67ed59e1d1-3933b95158dmr10890633a91.19.1786771214559; Fri, 14 Aug 2026 22:20:14 -0700 (PDT) Received: from localhost.localdomain ([49.79.111.198]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-394ea96c7fcsm5269702a91.3.2026.08.14.22.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Aug 2026 22:20:14 -0700 (PDT) From: Vernon Yang To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org Cc: nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, usama.arif@linux.dev, zokeefe@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Vernon Yang , stable@vger.kernel.org Subject: [PATCH v2 3/3] mm: khugepaged: fix folio is used after folio_put/unlock() Date: Sat, 15 Aug 2026 13:19:24 +0800 Message-ID: <20260815051924.194810-4-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260815051924.194810-1-vernon2gm@gmail.com> References: <20260815051924.194810-1-vernon2gm@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vernon Yang On the rollback path, folio_put() has already dropped the last reference of new_folio. On the success path, new_folio is already unlocked and can be freed concurrently. The trace_mm_khugepaged_collapse_file() is left with a dangling folio pointer. So using the folio_pfn() before dropping the reference, closing use-after-free window. Fixes: 4c9473e87e75 ("mm/khugepaged: add tracepoint to collapse_file()") Cc: stable@vger.kernel.org Signed-off-by: Vernon Yang Tested-by: Lance Yang --- include/trace/events/huge_memory.h | 6 +++--- mm/khugepaged.c | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge= _memory.h index 5dc71d292f47..1b301d4e2dba 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -211,10 +211,10 @@ TRACE_EVENT(mm_khugepaged_scan_file, ); =20 TRACE_EVENT(mm_khugepaged_collapse_file, - TP_PROTO(struct mm_struct *mm, struct folio *new_folio, pgoff_t index, + TP_PROTO(struct mm_struct *mm, unsigned long new_pfn, pgoff_t index, unsigned long addr, bool is_shmem, struct file *file, int nr, int result), - TP_ARGS(mm, new_folio, index, addr, is_shmem, file, nr, result), + TP_ARGS(mm, new_pfn, index, addr, is_shmem, file, nr, result), TP_STRUCT__entry( __field(struct mm_struct *, mm) __field(unsigned long, hpfn) @@ -228,7 +228,7 @@ TRACE_EVENT(mm_khugepaged_collapse_file, =20 TP_fast_assign( __entry->mm =3D mm; - __entry->hpfn =3D new_folio ? folio_pfn(new_folio) : -1; + __entry->hpfn =3D new_pfn; __entry->index =3D index; __entry->addr =3D addr; __entry->is_shmem =3D is_shmem; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 7c8c48577408..7df0fd2d2fdc 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2247,6 +2247,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, struct address_space *mapping =3D file->f_mapping; struct page *dst; struct folio *folio, *tmp, *new_folio; + unsigned long new_pfn =3D -1; pgoff_t index =3D 0, end =3D start + HPAGE_PMD_NR; LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); @@ -2626,6 +2627,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, retract_page_tables(mapping, start); if (cc && !cc->is_khugepaged) result =3D SCAN_PTE_MAPPED_HUGEPAGE; + new_pfn =3D folio_pfn(new_folio); folio_unlock(new_folio); =20 /* @@ -2664,12 +2666,13 @@ static enum scan_result collapse_file(struct mm_str= uct *mm, unsigned long addr, } =20 new_folio->mapping =3D NULL; + new_pfn =3D folio_pfn(new_folio); =20 folio_unlock(new_folio); folio_put(new_folio); out: VM_BUG_ON(!list_empty(&pagelist)); - trace_mm_khugepaged_collapse_file(mm, new_folio, index, addr, is_shmem, f= ile, HPAGE_PMD_NR, result); + trace_mm_khugepaged_collapse_file(mm, new_pfn, index, addr, is_shmem, fil= e, HPAGE_PMD_NR, result); return result; } =20 --=20 2.53.0