[PATCH v2 2/2] perf/core: Don't loop forever on a bad pmd in perf_get_pgtable_size()
From: JunYoung Lee
Date: Wed Dec 02 2026 - 06:59:26 EST
perf_get_pgtable_size() hands pte_offset_map() a copy of the pmd. For a
bad pmd, such as an x86-64 espfix entry, pte_offset_map() fails and only
clears the copy, so the walk reads the same entry again and retries
forever with interrupts disabled. A PERF_SAMPLE_CODE_PAGE_SIZE sample
with an IP in the espfix area hangs the CPU this way.
Return 0 for a bad pmd instead.
Fixes: a92cbb82c8d3 ("perf/core: allow pte_offset_map() to fail")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: JunYoung Lee <ljyoung04@xxxxxxxxx>
---
Changes in v2:
- Split out of v1 into its own patch (Peter).
Tested on 693c2e04e3f1 (kernelCTF LTS config, KVM guest, AMD host) with
a root exclude_kernel=0 breakpoint event probing the 128 espfix slots:
- With 1/2 only: the probe of the populated slot never returns, the
console fills with "bad pmd" messages and the guest stops answering.
- With 1/2 and 2/2: all probes return with code_page_size=0 and no
"bad pmd" message appears.
kernel/events/core.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4c7f173d7..dbede0eab 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8710,6 +8710,14 @@ static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr)
if (pmd_leaf(pmd))
return pmd_leaf_size(pmd);
+ /*
+ * pte_offset_map() fails on a bad pmd and only clears our local
+ * copy, so retrying would read the same entry again and loop
+ * forever with interrupts disabled.
+ */
+ if (pmd_bad(pmd))
+ return 0;
+
ptep = pte_offset_map(&pmd, addr);
if (!ptep)
goto again;
--
2.56.1