Skip to contentSkip to conversation

x86/hyperv: use pointed-to type size for page table offsets

PATCH Applied [email protected] 3 participants 3 messages Nov 1 – Nov 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hyperv: use pointed-to type size for page table offsets

From: Mohammad Shahid <[email protected]>

The Hyper-V crash trampoline code calculated page table entry offsets using sizeof() on pointer variables.

Use sizeof(*ptr) instead to calculates the size of the pointed-to page table entry.

This makes entry size explicit and avoids relying on the pointer size matching the page table entry size.

This issue was reported by Coccinelle script noderef.cocci.

---
 arch/x86/hyperv/hv_crash.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/hyperv/hv_crash.c +4 -4
index 5ffcc23255de..810eb1a4a0a2 100644
@@ -512,20 +512,20 @@ static void hv_crash_build_tramp_pt(void)
 	pte_t *pte;
 	u64 pa, addr = trampoline_pa;
 
-	p4d = hv_crash_ptpgs[0] + pgd_index(addr) * sizeof(p4d);
+	p4d = hv_crash_ptpgs[0] + pgd_index(addr) * sizeof(*p4d);
 	pa = virt_to_phys(hv_crash_ptpgs[1]);
 	set_p4d(p4d, __p4d(_PAGE_TABLE | pa));
 	p4d->p4d &= ~(_PAGE_NX);	/* enable execute */
 
-	pud = hv_crash_ptpgs[1] + pud_index(addr) * sizeof(pud);
+	pud = hv_crash_ptpgs[1] + pud_index(addr) * sizeof(*pud);
 	pa = virt_to_phys(hv_crash_ptpgs[2]);
 	set_pud(pud, __pud(_PAGE_TABLE | pa));
 
-	pmd = hv_crash_ptpgs[2] + pmd_index(addr) * sizeof(pmd);
+	pmd = hv_crash_ptpgs[2] + pmd_index(addr) * sizeof(*pmd);
 	pa = virt_to_phys(hv_crash_ptpgs[3]);
 	set_pmd(pmd, __pmd(_PAGE_TABLE | pa));
 
-	pte = hv_crash_ptpgs[3] + pte_index(addr) * sizeof(pte);
+	pte = hv_crash_ptpgs[3] + pte_index(addr) * sizeof(*pte);
 	set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

From: [email protected] <[email protected]> Sent: Sunday, November 1, 2026 1:21 AM

Quoted 11 lines

The Hyper-V crash trampoline code calculated page table entry offsets using sizeof() on pointer variables.

Use sizeof(*ptr) instead to calculates the size of the pointed-to page table entry.

This makes entry size explicit and avoids relying on the pointer size matching the page table entry size.

This issue was reported by Coccinelle script noderef.cocci.

FWIW, this issue was also reported by the kernel test robot. See [1] & [2]. So you should probably add "Reported-by:" and "Closes: " tags for those reports.

In any case, the fix looks good to me.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 38 lines
---
 arch/x86/hyperv/hv_crash.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/hyperv/hv_crash.c +4 -5
index 5ffcc23255de..810eb1a4a0a2 100644
@@ -512,20 +512,20 @@ static void hv_crash_build_tramp_pt(void)
 	pte_t *pte;
 	u64 pa, addr = trampoline_pa;
 
-	p4d = hv_crash_ptpgs[0] + pgd_index(addr) * sizeof(p4d);
+	p4d = hv_crash_ptpgs[0] + pgd_index(addr) * sizeof(*p4d);
 	pa = virt_to_phys(hv_crash_ptpgs[1]);
 	set_p4d(p4d, __p4d(_PAGE_TABLE | pa));
 	p4d->p4d &= ~(_PAGE_NX);	/* enable execute */
 
-	pud = hv_crash_ptpgs[1] + pud_index(addr) * sizeof(pud);
+	pud = hv_crash_ptpgs[1] + pud_index(addr) * sizeof(*pud);
 	pa = virt_to_phys(hv_crash_ptpgs[2]);
 	set_pud(pud, __pud(_PAGE_TABLE | pa));
 
-	pmd = hv_crash_ptpgs[2] + pmd_index(addr) * sizeof(pmd);
+	pmd = hv_crash_ptpgs[2] + pmd_index(addr) * sizeof(*pmd);
 	pa = virt_to_phys(hv_crash_ptpgs[3]);
 	set_pmd(pmd, __pmd(_PAGE_TABLE | pa));
 
-	pte = hv_crash_ptpgs[3] + pte_index(addr) * sizeof(pte);
+	pte = hv_crash_ptpgs[3] + pte_index(addr) * sizeof(*pte);
 	set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
 }
 
--

2.43.0

maintainer Applied
#2 lore raw
Quoted 17 lines · Michael Kelley

From: [email protected] <[email protected]> Sent: Sunday, November 1, 2026 1:21 AM

Quoted 11 lines

The Hyper-V crash trampoline code calculated page table entry offsets using sizeof() on pointer variables.

Use sizeof(*ptr) instead to calculates the size of the pointed-to page table entry.

This makes entry size explicit and avoids relying on the pointer size matching the page table entry size.

This issue was reported by Coccinelle script noderef.cocci.

FWIW, this issue was also reported by the kernel test robot. See [1] & [2]. So you should probably add "Reported-by:" and "Closes: " tags for those reports.

Quoted 6 lines

In any case, the fix looks good to me.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Applied. Thanks.

Wei