Skip to contentSkip to conversation

riscv: pgtable: Enhance set_pte to prevent OoO risk

PATCH In review [email protected] 3 participants 8 messages Dec 8, 2023 – Dec 12, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: pgtable: Enhance set_pte to prevent OoO risk

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +7 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
 	*ptep = pteval;
+
+	/*
+	 * Only if the new pte is present and kernel, otherwise TLB
+	 * maintenance or update_mmu_cache() have the necessary barriers.
+	 */
+	if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+		RISCV_FENCE(rw,rw);
 }
 
 void flush_icache_pte(pte_t pte);
Signature · 7 lines
2.40.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#1 lore raw
Quoted 6 lines · On Fri, Dec 8, 2023 at 11:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

Sorry, TSO has no Immunity. The above sentence should be rewritten with:

When changing from an invalid pte to a valid one for a kernel page, there is no guarantee tlb_flush in Linux. So, there is an OoO risk for riscv, e.g.:

Quoted 44 lines

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +7 -1
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);
 }
 
 void flush_icache_pte(pte_t pte);
--

2.40.1

Signature · 2 lines
Best Regards
 Guo Ren
#2 lore raw
In reply to [email protected]

Hi Guo,

Quoted 34 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +0 -0
index 294044429e8e..2fae5a5438e0 100644

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 10 lines
arch/riscv/include/asm/pgtable.h +7 -0
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thanks,

Alex

Quoted 6 lines
arch/riscv/include/asm/pgtable.h +0 -1
 }
 
 void flush_icache_pte(pte_t pte);
--

2.40.1

author
#3 lore raw
Quoted 45 lines · Alexandre Ghiti

Hi Guo,

Quoted 41 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +4 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
Quoted 8 lines

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 3 lines
arch/riscv/include/asm/pgtable.h +3 -0
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thx for the reply!

The sfence.vma is too expensive, so the situation is tricky. See the arm64 commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"), which is similar. That is, linux assumes invalid pte won't get into TLB. Think about memory hotplug:

mm/sparse.c: sparse_add_section() {
...

        memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
        if (IS_ERR(memmap))
                return PTR_ERR(memmap);

        /*
         * Poison uninitialized struct pages in order to catch invalid flags
         * combinations.
         */
        page_init_poison(memmap, sizeof(struct page) * nr_pages);

...
}
The section_activate would use set_pte to setup vmemmap, and page_init_poison would access these pages' struct.

That means:
sd t0, (a0) // a0 = struct page's pte address, pteval is changed from invalid to valid
...
lw/sw t1, (a1) // a1 = va of struct page

If the lw/sw instruction is executed speculatively before the set_pte, we need a fence to prevent this.

Quoted 11 lines

Thanks,

Alex

Quoted 6 lines
arch/riscv/include/asm/pgtable.h +0 -1
 }
 
 void flush_icache_pte(pte_t pte);
--

2.40.1

Signature · 2 lines
Best Regards
 Guo Ren
#4 lore raw
Quoted 73 lines · Guo Ren
Quoted 45 lines · Alexandre Ghiti

Hi Guo,

Quoted 41 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +4 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
Quoted 8 lines

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 3 lines
arch/riscv/include/asm/pgtable.h +3 -0
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thx for the reply!

The sfence.vma is too expensive, so the situation is tricky. See the arm64 commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"), which is similar. That is, linux assumes invalid pte won't get into TLB. Think about memory hotplug:

mm/sparse.c: sparse_add_section() {
...

        memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
        if (IS_ERR(memmap))
                return PTR_ERR(memmap);

        /*
         * Poison uninitialized struct pages in order to catch invalid flags
         * combinations.
         */
        page_init_poison(memmap, sizeof(struct page) * nr_pages);
Quoted 4 lines

...
}
The section_activate would use set_pte to setup vmemmap, and page_init_poison would access these pages' struct.

So I think the generic code must be fixed by adding a flush_cache_vmap() in vmemmap_populate_range() or similar: several architectures implement flush_cache_vmap() because they need to do "something" after a new mapping is established, so vmemmap should not be any different.

Quoted 9 lines

That means:
sd t0, (a0) // a0 = struct page's pte address, pteval is changed from invalid to valid
...
lw/sw t1, (a1) // a1 = va of struct page

If the lw/sw instruction is executed speculatively before the set_pte, we need a fence to prevent this.

Yes I agree, but to me we need the fence property of sfence.vma to make sure the PTW sees the new pte, unless I'm mistaken and something in the privileged specification states that a fence is enough?

Quoted 18 lines
Quoted 12 lines

Thanks,

Alex

Quoted 6 lines
arch/riscv/include/asm/pgtable.h +0 -1
 }
 
 void flush_icache_pte(pte_t pte);
--

2.40.1

--
Best Regards
Guo Ren

author
#5 lore raw
Quoted 80 lines · Alexandre Ghiti
Quoted 73 lines · Guo Ren
Quoted 45 lines · Alexandre Ghiti

Hi Guo,

Quoted 41 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +4 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
Quoted 8 lines

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 3 lines
arch/riscv/include/asm/pgtable.h +3 -0
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thx for the reply!

The sfence.vma is too expensive, so the situation is tricky. See the arm64 commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"), which is similar. That is, linux assumes invalid pte won't get into TLB. Think about memory hotplug:

mm/sparse.c: sparse_add_section() {
...

        memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
        if (IS_ERR(memmap))
                return PTR_ERR(memmap);

        /*
         * Poison uninitialized struct pages in order to catch invalid flags
         * combinations.
         */
        page_init_poison(memmap, sizeof(struct page) * nr_pages);
Quoted 4 lines

...
}
The section_activate would use set_pte to setup vmemmap, and page_init_poison would access these pages' struct.

Quoted 5 lines

So I think the generic code must be fixed by adding a flush_cache_vmap() in vmemmap_populate_range() or similar: several architectures implement flush_cache_vmap() because they need to do "something" after a new mapping is established, so vmemmap should not be any different.

Perhaps generic code assumes TLB won't contain invalid entries. When invalid -> valid, Linux won't do any tlb_flush, ref:

* Use set_p*_safe(), and elide TLB flushing, when confident that *no*
* TLB flush will be required as a result of the "set". For example, use
* in scenarios where it is known ahead of time that the routine is
* setting non-present entries, or re-setting an existing entry to the
* same value. Otherwise, use the typical "set" helpers and flush the
* TLB.

Quoted 8 lines
Quoted 8 lines

That means:
sd t0, (a0) // a0 = struct page's pte address, pteval is changed from invalid to valid
...
lw/sw t1, (a1) // a1 = va of struct page

Quoted 6 lines
Quoted 2 lines

If the lw/sw instruction is executed speculatively before the set_pte, we need a fence to prevent this.

Yes I agree, but to me we need the fence property of sfence.vma to make sure the PTW sees the new pte, unless I'm mistaken and something in the privileged specification states that a fence is enough?

All PTW are triggered by IFU & load/store. For the "set" scenarios, we just need to prevent the access va before the set_pte. So:
- Don't worry about IFU, which fetches the code sequentially.
- Use a fence prevent load/store before set_pte.

Sfence.vma is used for invalidate TLB, not for invalid -> valid.

Quoted 19 lines
Quoted 19 lines
Quoted 13 lines

Thanks,

Alex

Quoted 6 lines
arch/riscv/include/asm/pgtable.h +0 -1
 }
 
 void flush_icache_pte(pte_t pte);
--

2.40.1

--
Best Regards
Guo Ren

Signature · 2 lines
Best Regards
 Guo Ren
#6 lore raw
Quoted 110 lines · Guo Ren
Quoted 80 lines · Alexandre Ghiti
Quoted 73 lines · Guo Ren
Quoted 45 lines · Alexandre Ghiti

Hi Guo,

Quoted 41 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +4 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
Quoted 8 lines

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 3 lines
arch/riscv/include/asm/pgtable.h +3 -0
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thx for the reply!

The sfence.vma is too expensive, so the situation is tricky. See the arm64 commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"), which is similar. That is, linux assumes invalid pte won't get into TLB. Think about memory hotplug:

mm/sparse.c: sparse_add_section() {
...

        memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
        if (IS_ERR(memmap))
                return PTR_ERR(memmap);

        /*
         * Poison uninitialized struct pages in order to catch invalid flags
         * combinations.
         */
        page_init_poison(memmap, sizeof(struct page) * nr_pages);
Quoted 4 lines

...
}
The section_activate would use set_pte to setup vmemmap, and page_init_poison would access these pages' struct.

Quoted 5 lines

So I think the generic code must be fixed by adding a flush_cache_vmap() in vmemmap_populate_range() or similar: several architectures implement flush_cache_vmap() because they need to do "something" after a new mapping is established, so vmemmap should not be any different.

Perhaps generic code assumes TLB won't contain invalid entries. When invalid -> valid, Linux won't do any tlb_flush, ref:

* Use set_p*_safe(), and elide TLB flushing, when confident that *no*
* TLB flush will be required as a result of the "set". For example, use
* in scenarios where it is known ahead of time that the routine is
* setting non-present entries, or re-setting an existing entry to the
* same value. Otherwise, use the typical "set" helpers and flush the
* TLB.

Quoted 8 lines
Quoted 8 lines

That means:
sd t0, (a0) // a0 = struct page's pte address, pteval is changed from invalid to valid
...
lw/sw t1, (a1) // a1 = va of struct page

Quoted 6 lines
Quoted 2 lines

If the lw/sw instruction is executed speculatively before the set_pte, we need a fence to prevent this.

Yes I agree, but to me we need the fence property of sfence.vma to make sure the PTW sees the new pte, unless I'm mistaken and something in the privileged specification states that a fence is enough?

Quoted 6 lines

All PTW are triggered by IFU & load/store. For the "set" scenarios, we just need to prevent the access va before the set_pte. So:
- Don't worry about IFU, which fetches the code sequentially.
- Use a fence prevent load/store before set_pte.

Sfence.vma is used for invalidate TLB, not for invalid -> valid.

I think the problem is that, architecturally, you can't prevent a PTW by preventing access to the virtual address. The RISC-V privileged spec allows caching the results of PTWs from speculative execution, and it allows caching invalid PTEs. So effectively, as soon as satp is written, software must be able to handle _any_ virtual address being in the TLB.

To avoid the sfence.vma in the invalid->valid case, you need to handle the possible page fault, like in Alex's series here:

https://lore.kernel.org/linux-riscv/[email protected]/

Regards,
Samuel

author
#7 lore raw
Quoted 125 lines · Samuel Holland
Quoted 110 lines · Guo Ren
Quoted 80 lines · Alexandre Ghiti
Quoted 73 lines · Guo Ren
Quoted 45 lines · Alexandre Ghiti

Hi Guo,

Quoted 41 lines · On Fri, Dec 8, 2023 at 4:10 PM <[email protected]> wrote:

From: Guo Ren <[email protected]>

When changing from an invalid pte to a valid one for a kernel page, there is no need for tlb_flush. It's okay for the TSO memory model, but there is an OoO risk for the Weak one. eg:

sd t0, (a0) // a0 = pte address, pteval is changed from invalid to valid
...
ld t1, (a1) // a1 = va of above pte

If the ld instruction is executed speculatively before the sd instruction. Then it would bring an invalid entry into the TLB, and when the ld instruction retired, a spurious page fault occurred. Because the vmemmap has been ignored by vmalloc_fault, the spurious page fault would cause kernel panic.

This patch was inspired by the commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"). For RISC-V, there is no requirement in the spec to guarantee all tlb entries are valid and no requirement to PTW filter out invalid entries. Of course, micro-arch could give a more robust design, but here, use a software fence to guarantee.

---
 arch/riscv/include/asm/pgtable.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/pgtable.h +4 -0
index 294044429e8e..2fae5a5438e0 100644
@@ -511,6 +511,13 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
 static inline void set_pte(pte_t *ptep, pte_t pteval)
 {
        *ptep = pteval;
+
+       /*
+        * Only if the new pte is present and kernel, otherwise TLB
+        * maintenance or update_mmu_cache() have the necessary barriers.
Quoted 8 lines

arch/riscv/include/asm/pgtable.h static inline int pte_same(pte_t pte_a, pte_t pte_b) · line 520

Quoted 3 lines
arch/riscv/include/asm/pgtable.h +3 -0
+        */
+       if (pte_val(pteval) & (_PAGE_PRESENT | _PAGE_GLOBAL))
+               RISCV_FENCE(rw,rw);

Only a sfence.vma can guarantee that the PTW actually sees a new mapping, a fence is not enough. That being said, new kernel mappings (vmalloc ones) are correctly handled in the kernel by using flush_cache_vmap(). Did you observe something that this patch fixes?

Thx for the reply!

The sfence.vma is too expensive, so the situation is tricky. See the arm64 commit: 7f0b1bf04511 ("arm64: Fix barriers used for page table modifications"), which is similar. That is, linux assumes invalid pte won't get into TLB. Think about memory hotplug:

mm/sparse.c: sparse_add_section() {
...

        memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
        if (IS_ERR(memmap))
                return PTR_ERR(memmap);

        /*
         * Poison uninitialized struct pages in order to catch invalid flags
         * combinations.
         */
        page_init_poison(memmap, sizeof(struct page) * nr_pages);
Quoted 4 lines

...
}
The section_activate would use set_pte to setup vmemmap, and page_init_poison would access these pages' struct.

Quoted 5 lines

So I think the generic code must be fixed by adding a flush_cache_vmap() in vmemmap_populate_range() or similar: several architectures implement flush_cache_vmap() because they need to do "something" after a new mapping is established, so vmemmap should not be any different.

Perhaps generic code assumes TLB won't contain invalid entries. When invalid -> valid, Linux won't do any tlb_flush, ref:

* Use set_p*_safe(), and elide TLB flushing, when confident that *no*
* TLB flush will be required as a result of the "set". For example, use
* in scenarios where it is known ahead of time that the routine is
* setting non-present entries, or re-setting an existing entry to the
* same value. Otherwise, use the typical "set" helpers and flush the
* TLB.

Quoted 8 lines
Quoted 8 lines

That means:
sd t0, (a0) // a0 = struct page's pte address, pteval is changed from invalid to valid
...
lw/sw t1, (a1) // a1 = va of struct page

Quoted 6 lines
Quoted 2 lines

If the lw/sw instruction is executed speculatively before the set_pte, we need a fence to prevent this.

Yes I agree, but to me we need the fence property of sfence.vma to make sure the PTW sees the new pte, unless I'm mistaken and something in the privileged specification states that a fence is enough?

Quoted 6 lines

All PTW are triggered by IFU & load/store. For the "set" scenarios, we just need to prevent the access va before the set_pte. So:
- Don't worry about IFU, which fetches the code sequentially.
- Use a fence prevent load/store before set_pte.

Sfence.vma is used for invalidate TLB, not for invalid -> valid.

I think the problem is that, architecturally, you can't prevent a PTW by preventing access to the virtual address. The RISC-V privileged spec allows caching the results of PTWs from speculative execution, and it allows caching invalid PTEs. So effectively, as soon as satp is written, software must be able to handle _any_ virtual address being in the TLB.

Quoted 4 lines

To avoid the sfence.vma in the invalid->valid case, you need to handle the possible page fault, like in Alex's series here:

https://lore.kernel.org/linux-riscv/[email protected]/

Just as this patch series said:

+ * The RISC-V kernel does not eagerly emit a sfence.vma after each
+ * new vmalloc mapping, which may result in exceptions:
+ * - if the uarch caches invalid entries, the new mapping would not be
+ * observed by the page table walker and an invalidation is needed.
+ * - if the uarch does not cache invalid entries, a reordered access
+ * could "miss" the new mapping and traps: in that case, we only need
+ * to retry the access, no sfence.vma is required.

I'm talking about "uarch does not cache invalid entries, a reordered access could "miss" the new mapping and traps".
Using a fence in set_pte is another solution, and better than retrying. Of course the premise is that the fence is not expensive for micro-arch.

- Arm64 used this way: commit: 7f0b1bf04511 ("arm64: Fix barriers
- X86 is similar, because it's TSO, so any load + store instructions
between set_pte & next access would give a barrier, eg:
set_pte va
load (acquire)
store (release)
load/store va

Another topic is about "retry the access", this is about kernel virtual address space spurious page fault right? And Alex is preventing that in the riscv linux kernel and it would cause a lot of side effects.

Quoted 4 lines

Regards,
Samuel

Signature · 2 lines
Best Regards
 Guo Ren