From nobody Mon Sep 28 08:54:53 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 0D8743F1AC0 for ; Mon, 24 Aug 2026 09:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563809; cv=none; b=tBh2AKG3tF4h9y5wIGliow3E+y4Uiw12km65h0ltNcfz3gNQ3xvIs7tT6bxehSbGBrUH3vOv9lJt9uatBM2UuOQiH5M1MCLpzhWxO+a4yoFplvj+yBo285nZdv+4+FmQ2PyFZsQCyOy9NPbOAif/CdWw/k6mRMwge45V2KqAXT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563809; c=relaxed/simple; bh=mp1cYCqbvfJALgCzLnvRN0dxdui+lVigVvJITXo9b+Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ndxs6OF/fHgnhD32iIihymKQMksUp6pQbkY69mYwKc2yXfe31vCfnaI0NFWdpBiRr6JWoY8uolmUrR+EhIm6w84EV8f1iw1nogxSm71VneVOYtKkcQaBnGVjBeSqeX6+7/x7wxSniLnPKfhiRWRGsIJMRy3//6YfgjQ/QeoEWNk= 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=PU8Q6IMg; arc=none smtp.client-ip=209.85.210.174 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="PU8Q6IMg" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-848743155bcso1006989b3a.0 for ; Mon, 24 Aug 2026 02:30:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787563807; x=1788168607; 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=S4Ip1jEqtN/coAyik2SQv7pRhr7t2Z+l9dqjS0wG+sk=; b=PU8Q6IMgIkY3+wz/Mq/nUkOO/RbNJdh7bjNnbHHTbhuX7VeDCzpgSL2Re8g5AX5Fk1 SY/mDQebhSuoyOp9N8GL5xEXXuOCNuVNi+U+hCt6ye+iWUdQRvfJHcs1c0baioM47TTs xE6UUg5yR5NNpTz7c/TVaaKQ1nvXXFBLJ9dO+TatcKmTyqDB10vZbDR1flhp7vNJU2K1 KBM+mYZ4Kei2YkYw3BY82j9Ajv9DW7Epu5mAFvNsLtqEv6UyVOO6t7WJKGmQU5O5I84x sXxg7JHzHUrgzEB0VX3ic+oLJNnha41nm9lgj5rBT61m6rmObpxNcnhLrhe77MZZgjtN oC8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787563807; x=1788168607; 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=S4Ip1jEqtN/coAyik2SQv7pRhr7t2Z+l9dqjS0wG+sk=; b=IHkuYi72Ytm6QW4tfqZ+vBNhGQwjmPwc1HxwhUwX8jWxZLzMuLGIu95fQTe582guA/ w7B2kW/MXnPB74dHjKvbqfPaBVTJJ8kYp+3fbyvjO4tXeFIqQSrNl6kOmbqdz0pJ58Wi pG8VCMbynqrsF7V4+OSl4rooUlzkTPlkQUASAiCxai8GiwymwNBJqxebj6xrRC6PIhGW 3VXnMa0Bj5QUq3qdcPcfe0NSOUSMsELl6ao5oSUNRqz/eNPm9dj7ozkLuakFOjqvSb0O zjnPnqaDziCoWHFcumIzqhlPrxLTeWmuv0tCd/jo00EW7V2cmmjB7ud7p/sUpg4tj3JX MrUA== X-Forwarded-Encrypted: i=1; AHgh+RpRvo7HfAz7Rtqmx7Uzz4NvC6CtPUvrk8q3QQbYsg553ZjaMWfd2yJXZSrahk+I3N0UvAJliKjynWoeuf8=@vger.kernel.org X-Gm-Message-State: AFuF++k9UbcXNRlNvpsIPPc16IAQcod/9wMlvCAP9HEp0O73RNtx54Wt c+sKzBJO35l2HlTYc/RJ8PLPSvCW+krYg/USFkrMfa7SN5LHT2wgLSWs X-Gm-Gg: AR+sD13Cw5WQ9qRhxYHIEP3rmcnmJ93AhdCrSB5X7iww4Ye3A38sOhEoQuYCcEqM7dl 6vjCci8nLpOmUxsU28AGXEyU0sOt90CzC91xIP3XNHAty6ReQ3aG/Ml89VnFrzsT4R/vRvrGCSk WQmVqPEaUIDpozSx0ku8Vedl26AT9pEUrDfhNmPY6An4gLktbKzyTogKDdPyJiUJTxaolRFvHkh OG206ebAkcG7hiU5KfDYCStyc5Y1SKtmqU+sY2i+LGdD7tyawmnXHiPi+LPFX5ocjRemb36z5iA J1ZEnt/McsT1Mg9+6xSOkVN4xZB7X/XTYrorweflegfumlTqCu8vMN8C/SUtnls9RKjWf8ASc4m K8qHm/IRLZaBmHHcvn/HNDrtnGFIRtyJcZ2BsFdLzQwe0iuX6rn/vbnPheIQofHnwb3WQ61s9QJ YS0Me3VwU+TMrzzXnXIL4jZT6ceeAQaG9k9+1p7HqiJ/rM0eIJX7VKSF5zSPOBqxPurRwcCoRMN i0= X-Received: by 2002:a05:6300:14d:b0:3c4:3767:adb2 with SMTP id adf61e73a8af0-3cd2fd4d873mr48989218637.1.1787563807262; Mon, 24 Aug 2026 02:30:07 -0700 (PDT) Received: from localhost.localdomain ([240e:b8f:1df9:a600:c693:b19f:ada0:748]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc199e60035sm845226a12.22.2026.08.24.02.30.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2026 02:30:06 -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, stable@vger.kernel.org, Vernon Yang Subject: [PATCH v3 1/3] mm: khugepaged: fix swap entry value to folio_pfn() Date: Mon, 24 Aug 2026 17:29:33 +0800 Message-ID: <20260824092935.73892-2-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260824092935.73892-1-vernon2gm@gmail.com> References: <20260824092935.73892-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 --- 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 5a48c5406cce..7b526528f85b 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 79effd3f3da4..00337405c0e0 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2689,6 +2689,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; @@ -2719,6 +2720,7 @@ static enum scan_result collapse_scan_file(struct mm_= struct *mm, continue; } =20 + pfn =3D folio_pfn(folio); if (is_pmd_order(folio_order(folio))) { result =3D SCAN_PTE_MAPPED_HUGEPAGE; /* @@ -2779,7 +2781,8 @@ 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, (!folio || xa_is_value(folio)) ? -1 : p= fn, + file, present, swap, result); return result; } =20 --=20 2.53.0 From nobody Mon Sep 28 08:54:53 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 64CAB282F12 for ; Mon, 24 Aug 2026 09:30:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563813; cv=none; b=ovv6G606Ar3sWewDB6upDQheyeTRlYhnlcIKkCEdgmpiemNKggo+v3ITTyPFYG7z1R+GR717Z3f3EGIEQ9bWH42+4f2mmvpusfygf6mf7E2XZxYgVsno9F4Q0EEjLE/Ya1hcbf8elePY4+Qh48PEb5rHDCUqTtDZ3OmkNbvc8wY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563813; c=relaxed/simple; bh=FWng7qCEjeGMPDD7slzOVT3h3y67g/hV8NYocXvUfeY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iooVBi0daB8TG7z0woVFOIiwYd2hZRWhLNcAyv/lAFUc3HjKNjN4Y5dlkNSzETrxG5dXIR4tJXXJ/G7uW3ZXriYoH6oW62du6PQ1HGfJS/qtTqZyV40rLtVgamIo0x5ZLDM4+aonpen4VnW+1SiR9nVSFPWm6sQXT4OhNHI54Ws= 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=jdKebhmj; arc=none smtp.client-ip=209.85.215.177 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="jdKebhmj" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-cc1469c1b83so4279779a12.1 for ; Mon, 24 Aug 2026 02:30:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787563812; x=1788168612; 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=nNTnRdSFN4Auzxi1H+B0QKsOJrKfL6HrsAGQ1BLbnH4=; b=jdKebhmjF+EZbpbcxUG+XsYQtmPolZMxqHvZwKQuynNLP7OHkziyeHKJxiZN/6SFCH DC0F9JejSL+iepeLZuPclmG6Oqrgsxp/jXWR8XFNav/V0UBxctPbSbRplkms6mDsJ+fL 1z9n87y5bzt4QZ2mbVA/FKwGO4XrHs6O2FsC1uBemZ+bkTMn41P481KacAjr0ApnGTZ5 YEDH9uk3ecFppr164jQ8Xk0aGgnmda84BD/bz+WzwkO1MPquIuqXkliMOpRod+ZZHcjt AZTLVFfGTjBzBYjQb71sqhA7RHkVwm7y64JPSqNAOpc1w8fk+MG/cN8+BMrnUf44B5ed LFVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787563812; x=1788168612; 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=nNTnRdSFN4Auzxi1H+B0QKsOJrKfL6HrsAGQ1BLbnH4=; b=IQx/g1KM/R01DL7ar5TI9x4naTi501a83UbNg44yW+IidvU+lw9vlpSt68QxKyL1zy IIEMchkjHCHvt8YOuqypay7sQKqjCFyL1XhQ9W7CQvrril/wBDzZwFVZS0TDca2cl2VU 4oeFvep5d8lA/b48Z89ao9ZOf9OB6598zIWTphDy9mhlhO2qjCiXjM2WMC6Dc8ZmNK5Z v6fFP5iX/tylzETyAkWWFeFj9tq6tFWX+ODSet8Dr6D9pd3A+H/aDgdNsDc+4+yqxe8o fJS4fYnFuiHi0k3o2N/hmYmjKMqCnmwi1B4jBbtj0glD4Go+jd7V1Atd3xVlJhF5N/3C iIKQ== X-Forwarded-Encrypted: i=1; AHgh+Rrc1E8L2m35mIHrgFhrwkuOSDQzXICV57hEABndvJVni8MttIIaNZQ8VufmqmR6H8IYOh+cDhXRxxS6k5M=@vger.kernel.org X-Gm-Message-State: AFuF++lMdg0AfMGuJqqeJblojHxZuDy5PW6oEkefAesTgddAQaLKpQ+q XEDA5NrnZ+2IerXgJ/4c2AdOG7ChCEKIr0m0ZW28tv0sqTPDXN+66HPe X-Gm-Gg: AR+sD11lW5TZR3jkamwQSAq64x31YV+A9CvZo7n5JtAF1S/Kg6tlTce1PQqUSPyYY3E fzEbtbOo8jAbONv+XHUJ55kqX/u9neIGg+dDEDDD9Ku5oChbWLwuOka27TbhKor7m2YLV6gS3v5 3zPBdg3OEFY5e4mO19Tlmc8CzpQ1FxZKh57qXaiXZICOQv0AUJeJjWty9lEVXmjHRDTqQCe/MrJ L25mtHJ/XBMFeR6I7sey/N14uMlmbmLJhq7Hj7f57u4NaKE3Xl1qf2RZM3vMOXeuwXV5KoGW8ad VRzA2XA0KhDaMwavjFJ59Enx9c1hZdf590b9k3mjRfbvgFZ4bDWgwqJeQ/bJ5cmESeEH1j6bRMj QVIQGLUuHCeXYAFYtx0UZfTbT6QeZAOacc4vJcfyREPALICMhWlhY43wnZtTCYocyj27co9qKS8 Y8oOxNqRKrHeZqy0El6M74IPavg3GQnaqKd36302tmPVW9lp09ZJdeeS7I8XXAhiZllR2jA8/fJ uA= X-Received: by 2002:a05:6a20:94c9:b0:3cc:8344:1213 with SMTP id adf61e73a8af0-3cd2fe7a37cmr43381415637.8.1787563811608; Mon, 24 Aug 2026 02:30:11 -0700 (PDT) Received: from localhost.localdomain ([240e:b8f:1df9:a600:c693:b19f:ada0:748]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc199e60035sm845226a12.22.2026.08.24.02.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2026 02:30:11 -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, stable@vger.kernel.org, Vernon Yang Subject: [PATCH v3 2/3] mm: khugepaged: fix folio is used after pte_unmap_unlock() Date: Mon, 24 Aug 2026 17:29:34 +0800 Message-ID: <20260824092935.73892-3-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260824092935.73892-1-vernon2gm@gmail.com> References: <20260824092935.73892-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 Acked-by: Lorenzo Stoakes (ARM) --- 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 7b526528f85b..fa828967e1fb 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 00337405c0e0..4e0fca5942dd 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1618,6 +1618,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; @@ -1780,6 +1781,8 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, result =3D SCAN_SUCCEED; } out_unmap: + if (folio) + pfn =3D folio_pfn(folio); pte_unmap_unlock(pte, ptl); if (result =3D=3D SCAN_SUCCEED) { /* collapse_huge_page() expects the lock to be dropped before calling */ @@ -1790,7 +1793,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 08:54:53 2026 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 918813F3292 for ; Mon, 24 Aug 2026 09:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563818; cv=none; b=A3vOogocF5DNdsA+jyMPdNTykizEH9YG1orhlDFtTUpl6EtSCqWeTa1yYOQ8620TevP/JIkfm+Fv375e/sqdw0EdX7to8XfxsNPEPguTcADY88eFX2UuI/Ht7iJioIC8K2+hymE3qMrodefhupIt6YTzMO7CWaoDnMhm25MOre4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787563818; c=relaxed/simple; bh=F20+2pN27w1wV106mm78Uqa6vlea/vNu7ZZQw84xAlU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J8gCGCfYgNZRLauGuOC0KPncPqyF3p6/lUXm3haXVDCgwpG2MT8B7HWY813AAZJ9lCuISUoPetQisoE7AhAmxgcFcgCoXnHv2Ei6j23z47emY77TOCwuGOFqALBrKnJ2nD9zmTqeeZeZ9p0pSpTOB7tDDv8eTlo/WSlvJlbTAb8= 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=DqRyKLCh; arc=none smtp.client-ip=209.85.210.177 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="DqRyKLCh" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-84faf0fa17eso3253516b3a.2 for ; Mon, 24 Aug 2026 02:30:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787563816; x=1788168616; 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=2yr3XBcdQ3LShy8RFIN2zpBH4EW/xdxEP6silOD6LRY=; b=DqRyKLChYlUPPg64B613vYeFBRbdIE6dwLXT2GYtxdNkks5tSmLRq1lrifcBDsU5ot 3odVw32vII3qBuL+OnvQ5q7h1PywTFup26L/iYNPjKXrzJEGxCwErX7GYF0C530aMoV7 Wsw4ZJQXr9PKcq1dGkoboSj/ghie4YzmulifREMDKWubLCxO4aabAKT67IdCvTa0jQOL KP/gy1iDbyTdBzDg3GGHnpFIfYW2XwVEOgUV1MD+LZQItJse1TIjQNik6ACjpETfLS87 zyM7wH1Rz0wAgKUE0/xtgEIrOLWY7y+C7S9DzgDb80eOSJphPN+6fE9pg62YN3cUzIWJ 1K3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787563816; x=1788168616; 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=2yr3XBcdQ3LShy8RFIN2zpBH4EW/xdxEP6silOD6LRY=; b=N/SH6NaW6Ih2RJn2si5wWdEW1EE5b+8+9Ex1ldkhY1F68nmZ77qEugfmUHmsJP3fVS AikSlpUSBHst66ladqUovYewZGubVucFyxD/i4KeNCbE5UyPKk/f1FUTbQYDngRl+dNu x4aZBxvhCBSVp8sJvKZ/qTRxJr8TBQimOJXo5YARyc/6Uxh+ZMczC/kCWrLv5sEyjHWs C2pmBGNrZt+gd+DzY0KV0BJHUtRGvMntMvnVjgNRKH06h9MbjW6ewDwpFQGETAEiA7Nz h6hkcvx1FB5OBVxyvfMuFnaLP1AHAmGQfAWbmGDInVGUQ7X55RTswfVCiVO3REDDBCQ8 mLfA== X-Forwarded-Encrypted: i=1; AHgh+Rr07c692gFxL56TyLG9akXvdVnSyFvmS/jDXvRVwKSPxK4+U2PhuKWSfRQMDR8G3fC+IdRIJy3NIRY3060=@vger.kernel.org X-Gm-Message-State: AFuF++nH0+juYEUhzvUYmaqL2fIBYy0N/gQ4S7sXi2chQgDCJtACooBj vA/PdQFRqjWBBrvzfoBjL3dK9MctvPIKmXy9pAhGMD1vx9mutf+36TE6 X-Gm-Gg: AR+sD13z3WSFxJIQCuRfI7mTmTbql6d0+4P/ukXYEaxsLiXUWilku3AC/RkpfJygs2l AqJHCqFFORk+RTCijcRTcdR227rM3ih9VgwUSmqR53wscqhwekrbWQ5h5WcAQhZ1SJXNSS1AUSe I20whB8s8LnRYrL2mVIpk+p5c6PwlC7INJXNtCrdeb4GDRcCnIsq0Nq+DbEnkAgZZGZip883wpp D7I8kmc18/1L98R0d3ha0SYHB7pQ7wcP4DVlHLiejTJUpN7ohMZLF8++D/doFxm/r7JDPWqN7Ru msJshWJUoPTvk58cGL2xdKrIRnkJ4TuMibfxwwgzBtxWKjSOhknR7c3EoK4gPecWxWc5zaEzWQj IAGDOfLj5KyIuHFSy0P+IKDwXCKowxwjC0MoVdtZB4ZABwFNbS4Miw27NFWw4Spcp9qsJKvSbGN 3Bp7LmDnFEL/P+kxkOIAl7wE/QdaM58O4P37Hbh5Es1/tn4bWCqiFccvfFx5kSMAPZmvDU0IlSJ Y1MPAfA4SAEFE0ev9ffBRmtFw== X-Received: by 2002:a05:6a21:a93:b0:3c3:a140:9392 with SMTP id adf61e73a8af0-3cd4b6850aemr34704553637.0.1787563815848; Mon, 24 Aug 2026 02:30:15 -0700 (PDT) Received: from localhost.localdomain ([240e:b8f:1df9:a600:c693:b19f:ada0:748]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc199e60035sm845226a12.22.2026.08.24.02.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2026 02:30:15 -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, stable@vger.kernel.org, Vernon Yang Subject: [PATCH v3 3/3] mm: khugepaged: fix folio is used after folio_put/unlock() Date: Mon, 24 Aug 2026 17:29:35 +0800 Message-ID: <20260824092935.73892-4-vernon2gm@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260824092935.73892-1-vernon2gm@gmail.com> References: <20260824092935.73892-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 Acked-by: Lorenzo Stoakes (ARM) --- 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 fa828967e1fb..5fb4d92cfd84 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 4e0fca5942dd..24347f1a94ae 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2254,6 +2254,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); @@ -2633,6 +2634,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 /* @@ -2671,12 +2673,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