Skip to contentSkip to conversation

x86/mm: More KASAN per-CPU CEA mapping bug fixes

PATCH Superseded Sean Christopherson 1 participant 4 messages Nov 4, 2022 – Nov 9, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/mm: More KASAN per-CPU CEA mapping bug fixes

Two more bug fixes from the gift that keeps on giving. Both of these were found by inspection, and both are lightly tested. I'm pretty confident that patch 01 fixes a real bug, somewhat less so about patch 02, though given the other issues I debugged I don't see anything that prevents the DS buffers from exploding.

Sean Christopherson (2):
x86/mm: Recompute physical address for every page of per-CPU CEA
mapping
x86/mm: Populate KASAN shadow for per-CPU DS buffers in CPU entry area

 arch/x86/mm/cpu_entry_area.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

base-commit: 3301badde43dee7c2a013fbd6479c258366519da

Signature · 1 line
2.38.1.431.g37b22c650d-goog
#1 lore raw

PATCH 1/2 x86/mm: Recompute physical address for every page of per-CPU CEA mapping

Recompute the physical address for each per-CPU page in the CPU entry area, a recent commit inadvertantly modified cea_map_percpu_pages() such that every PTE is mapped to the physical address of the first page.

Fixes 9fd429c28073 ("x86/kasan: Map shadow for percpu pages on demand")
Cc Andrey Ryabinin <[email protected]>
---
 arch/x86/mm/cpu_entry_area.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/cpu_entry_area.c +1 -1
index dff9001e5e12..d831aae94b41 100644
@@ -97,7 +97,7 @@ cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)
 					early_pfn_to_nid(PFN_DOWN(pa)));
 
 	for ( ; pages; pages--, cea_vaddr+= PAGE_SIZE, ptr += PAGE_SIZE)
-		cea_set_pte(cea_vaddr, pa, prot);
+		cea_set_pte(cea_vaddr, per_cpu_ptr_to_phys(ptr), prot);
 }
 
 static void __init percpu_setup_debug_store(unsigned int cpu)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#2 lore raw

PATCH 2/2 x86/mm: Populate KASAN shadow for per-CPU DS buffers in CPU entry area

In reply to Sean Christopherson

Bounce through cea_map_percpu_pages() when setting the initial protections for per-CPU DS buffers so that KASAN populates a shadow for said mapping. Failure to populate the shadow will result in a not-present #PF during KASAN validation if DS buffers are activated later on.

Fixes 9fd429c28073 ("x86/kasan: Map shadow for percpu pages on demand")
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
---
 arch/x86/mm/cpu_entry_area.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
arch/x86/mm/cpu_entry_area.c +4 -6
index d831aae94b41..64ae557ceb22 100644
@@ -91,13 +91,12 @@ void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags)
 static void __init
 cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)
 {
-	phys_addr_t pa = per_cpu_ptr_to_phys(ptr);
+	int nid = ptr ? early_pfn_to_nid(PFN_DOWN(per_cpu_ptr_to_phys(ptr))) : 0;
 
-	kasan_populate_shadow_for_vaddr(cea_vaddr, pages * PAGE_SIZE,
-					early_pfn_to_nid(PFN_DOWN(pa)));
+	kasan_populate_shadow_for_vaddr(cea_vaddr, pages * PAGE_SIZE, nid);
 
 	for ( ; pages; pages--, cea_vaddr+= PAGE_SIZE, ptr += PAGE_SIZE)
-		cea_set_pte(cea_vaddr, per_cpu_ptr_to_phys(ptr), prot);
+		cea_set_pte(cea_vaddr, ptr ? per_cpu_ptr_to_phys(ptr) : 0, prot);
 }
 
 static void __init percpu_setup_debug_store(unsigned int cpu)
@@ -121,8 +120,7 @@ static void __init percpu_setup_debug_store(unsigned int cpu)
 	 * memory like debug store buffers.
 	 */
 	npages = sizeof(struct debug_store_buffers) / PAGE_SIZE;
-	for (; npages; npages--, cea += PAGE_SIZE)
-		cea_set_pte(cea, 0, PAGE_NONE);
+	cea_map_percpu_pages(cea, NULL, npages, PAGE_NONE);
 #endif
 }
 
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#3 lore raw

Re: [PATCH 2/2] x86/mm: Populate KASAN shadow for per-CPU DS buffers in CPU entry area

Quoted 22 lines · Sean Christopherson

Bounce through cea_map_percpu_pages() when setting the initial protections for per-CPU DS buffers so that KASAN populates a shadow for said mapping. Failure to populate the shadow will result in a not-present #PF during KASAN validation if DS buffers are activated later on.

Fixes 9fd429c28073 ("x86/kasan: Map shadow for percpu pages on demand")
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
---
 arch/x86/mm/cpu_entry_area.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
arch/x86/mm/cpu_entry_area.c +0 -0
index d831aae94b41..64ae557ceb22 100644
@@ -91,13 +91,12 @@ void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags)
 static void __init
 cea_map_percpu_pages(void *cea_vaddr, void *ptr, int pages, pgprot_t prot)
 {

arch/x86/mm/cpu_entry_area.c void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags) · line 99

Quoted 10 lines
arch/x86/mm/cpu_entry_area.c +3 -4
-	phys_addr_t pa = per_cpu_ptr_to_phys(ptr);
+	int nid = ptr ? early_pfn_to_nid(PFN_DOWN(per_cpu_ptr_to_phys(ptr))) : 0;
 
-	kasan_populate_shadow_for_vaddr(cea_vaddr, pages * PAGE_SIZE,
-					early_pfn_to_nid(PFN_DOWN(pa)));
+	kasan_populate_shadow_for_vaddr(cea_vaddr, pages * PAGE_SIZE, nid);
 
 	for ( ; pages; pages--, cea_vaddr+= PAGE_SIZE, ptr += PAGE_SIZE)
-		cea_set_pte(cea_vaddr, per_cpu_ptr_to_phys(ptr), prot);
+		cea_set_pte(cea_vaddr, ptr ? per_cpu_ptr_to_phys(ptr) : 0, prot);

Gah, this is broken. If pages > 1, subsequent iterations will generate a non-NULL ptr. This is likely what the kernel test bot is complaining about[1]. Andrey's suggestion to map the entire per-CPU area in one go[2] should obviate the need for special casing the DS buffer. I'll give that a whirl and smush all three of these mini-series together.

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