Skip to contentSkip to conversation

target/riscv: Exit current TB after an sfence.vma

PATCH [email protected] 5 participants 13 messages Mar 16, 2022 – Mar 31, 2022 RISC-V

lore
Share & tools
lore
#0 lore raw

Here is a test case for this patch. I used to submit this bug on https://bugs.launchpad.net/qemu/+bug/1906516

sfence.vma will flush the tlb, so after this instruction, the translation block should be end. The following code will only work in single step mode:

```
relocate:
 li a0, OFFSET

 la t0, 1f
 add t0, t0, a0
 csrw stvec, t0

 la t0, early_pgtbl
 srl t0, t0, PAGE_SHIFT
 li t1, SATP_SV39
 or t0, t1, t0

 csrw satp, t0
1:
 sfence.vma
 la t0, trap_s
 csrw stvec, t0
 ret
```

In this code, I want to relocate pc to virtual address with the OFFSET prefix. Before writing to satp, pc run at physic address, stvec has been set to label 1 with a virtual prefix and virtual address has been mapping in early_pgtbl, after writing satp, qemu will throw a page fault, and pc will set to virtual address of label 1.

The problem is that, in this situation, the translation block will not end after sfence.vma, and stvec will be set to trap_s,

```
----------------
IN:
Priv: 1; Virt: 0
0x00000000800000dc: 00a080b3 add ra,ra,a0
0x00000000800000e0: 00007297 auipc t0,28672 # 0x800070e0
0x00000000800000e4: f2028293 addi t0,t0,-224
0x00000000800000e8: 00c2d293 srli t0,t0,12
0x00000000800000ec: fff0031b addiw t1,zero,-1
0x00000000800000f0: 03f31313 slli t1,t1,63
0x00000000800000f4: 005362b3 or t0,t1,t0
0x00000000800000f8: 18029073 csrrw zero,satp,t0

----------------
IN:
Priv: 1; Virt: 0
0x00000000800000fc: 12000073 sfence.vma zero,zero
0x0000000080000100: 00000297 auipc t0,0 # 0x80000100
0x0000000080000104: 1c828293 addi t0,t0,456
0x0000000080000108: 10529073 csrrw zero,stvec,t0

riscv_raise_exception: 12
riscv_raise_exception: 12
riscv_raise_exception: 12
riscv_raise_exception: 12
...
```

So, the program will crash. And the program will only work in single step mode:

```
----------------
IN:
Priv: 1; Virt: 0
0x00000000800000f8: 18029073 csrrw zero,satp,t0

----------------
IN:
Priv: 1; Virt: 0
0x00000000800000fc: 12000073 sfence.vma zero,zero

riscv_raise_exception: 12
----------------
IN:
Priv: 1; Virt: 0
0xffffffff800000fc: 12000073 sfence.vma zero,zero

----------------
IN:
Priv: 1; Virt: 0
0xffffffff80000100: 00000297 auipc t0,0 # 0xffffffff80000100

```

The pc will set to label 1, instead of trap_s.

#1 lore raw
Quoted 87 lines · On Wed, Mar 16, 2022 at 10:23 AM <[email protected]> wrote:

Here is a test case for this patch. I used to submit this bug on https://bugs.launchpad.net/qemu/+bug/1906516

sfence.vma will flush the tlb, so after this instruction, the translation block should be end. The following code will only work in single step mode:

```
relocate:
 li a0, OFFSET

 la t0, 1f
 add t0, t0, a0
 csrw stvec, t0

 la t0, early_pgtbl
 srl t0, t0, PAGE_SHIFT
 li t1, SATP_SV39
 or t0, t1, t0

 csrw satp, t0
1:
 sfence.vma
 la t0, trap_s
 csrw stvec, t0
 ret
```

In this code, I want to relocate pc to virtual address with the OFFSET prefix. Before writing to satp, pc run at physic address, stvec has been set to label 1 with a virtual prefix and virtual address has been mapping in early_pgtbl, after writing satp, qemu will throw a page fault, and pc will set to virtual address of label 1.

The problem is that, in this situation, the translation block will not end after sfence.vma, and stvec will be set to trap_s,

```
----------------
IN:
Priv: 1; Virt: 0
0x00000000800000dc: 00a080b3 add ra,ra,a0
0x00000000800000e0: 00007297 auipc t0,28672 # 0x800070e0
0x00000000800000e4: f2028293 addi t0,t0,-224
0x00000000800000e8: 00c2d293 srli t0,t0,12
0x00000000800000ec: fff0031b addiw t1,zero,-1
0x00000000800000f0: 03f31313 slli t1,t1,63
0x00000000800000f4: 005362b3 or t0,t1,t0
0x00000000800000f8: 18029073 csrrw zero,satp,t0

----------------
IN:
Priv: 1; Virt: 0
0x00000000800000fc: 12000073 sfence.vma zero,zero
0x0000000080000100: 00000297 auipc t0,0 # 0x80000100
0x0000000080000104: 1c828293 addi t0,t0,456
0x0000000080000108: 10529073 csrrw zero,stvec,t0

riscv_raise_exception: 12
riscv_raise_exception: 12
riscv_raise_exception: 12
riscv_raise_exception: 12
...
```

So, the program will crash. And the program will only work in single step mode:

```
----------------
IN:
Priv: 1; Virt: 0
0x00000000800000f8: 18029073 csrrw zero,satp,t0

----------------
IN:
Priv: 1; Virt: 0
0x00000000800000fc: 12000073 sfence.vma zero,zero

riscv_raise_exception: 12
----------------
IN:
Priv: 1; Virt: 0
0xffffffff800000fc: 12000073 sfence.vma zero,zero

----------------
IN:
Priv: 1; Virt: 0
0xffffffff80000100: 00000297 auipc t0,0 # 0xffffffff80000100

```
Quoted 1 line

The pc will set to label 1, instead of trap_s.

+qemu-dev and Alistair

This is in for-next on Alistair's tree and fails to boot the kernel with the following error (found -d in_asm mode). Reverting the patch solves the issue.

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201040: 18051073 csrrw zero,satp,a0

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201044: Address 0x80201044 is out of bounds.

0x0000000080201049: Address 0x80201049 is out of bounds.

0x000000008020104e: Address 0x8020104e is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201050: Address 0x80201050 is out of bounds.

0x0000000080201055: Address 0x80201055 is out of bounds.

0x000000008020105a: Address 0x8020105a is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x000000008020105c: Address 0x8020105c is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

Signature · 2 lines
Regards,
Atish
#2 lore raw
Quoted 39 lines · Atish Patra

This is in for-next on Alistair's tree and fails to boot the kernel with the following error (found -d in_asm mode). Reverting the patch solves the issue.

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201040: 18051073 csrrw zero,satp,a0

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201044: Address 0x80201044 is out of bounds.

0x0000000080201049: Address 0x80201049 is out of bounds.

0x000000008020104e: Address 0x8020104e is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201050: Address 0x80201050 is out of bounds.

0x0000000080201055: Address 0x80201055 is out of bounds.

0x000000008020105a: Address 0x8020105a is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x000000008020105c: Address 0x8020105c is out of bounds.

Quoted 6 lines

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

--
Regards,
Atish

Do you have more specific information about which kernel image doesn't boot? The errors you're seeing simply mean that these addresses are not translated by the new address translation context set by the write to the satp.
To be honest I don't immediately see how this could be caused by the patch, as it modifies the behaviour of the sfence.vma instruction, and there are none in your trace.

Idan Horowitz

#3 lore raw
Quoted 41 lines · Idan Horowitz
Quoted 39 lines · Atish Patra

This is in for-next on Alistair's tree and fails to boot the kernel with the following error (found -d in_asm mode). Reverting the patch solves the issue.

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201040: 18051073 csrrw zero,satp,a0

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201044: Address 0x80201044 is out of bounds.

0x0000000080201049: Address 0x80201049 is out of bounds.

0x000000008020104e: Address 0x8020104e is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201050: Address 0x80201050 is out of bounds.

0x0000000080201055: Address 0x80201055 is out of bounds.

0x000000008020105a: Address 0x8020105a is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x000000008020105c: Address 0x8020105c is out of bounds.

Quoted 8 lines
Quoted 6 lines

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

--
Regards,
Atish

Do you have more specific information about which kernel image doesn't boot?

I tested on v5.17 built from defconfig for rv64.

Quoted 7 lines

The errors you're seeing simply mean that these addresses are not translated by the new address translation context set by the write to the satp.
To be honest I don't immediately see how this could be caused by the patch, as it modifies the behaviour of the sfence.vma instruction, and there are none in your trace.

There was a sfence.vma. I just did not share the detailed trace before.

Here is the kernel code executing sfence.vma
https://elixir.bootlin.com/linux/v5.17/source/arch/riscv/kernel/head.S#L122

Here is the detailed trace that should provide more information.
------------------------------------------------------------------------------------------------------------------------------
----------------
IN:
Priv: 1; Virt: 0

0x0000000080a04664:  70e2              ld              ra,56(sp)
0x0000000080a04666:  7442              ld              s0,48(sp)
0x0000000080a04668:  74a2              ld              s1,40(sp)
0x0000000080a0466a:  7902              ld              s2,32(sp)
0x0000000080a0466c:  69e2              ld              s3,24(sp)
0x0000000080a0466e:  6a42              ld              s4,16(sp)
0x0000000080a04670:  6aa2              ld              s5,8(sp)
0x0000000080a04672:  6121              addi            sp,sp,64
0x0000000080a04674:  8082              ret

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201132: 00a05517 auipc a0,10506240
# 0x80c06132

0x0000000080201136:  ece50513          addi            a0,a0,-306
0x000000008020113a:  ec7ff0ef          jal             ra,-314

# 0x80201000

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201000: 00d95597 auipc a1,14241792
# 0x80f96000

0x0000000080201004:  38858593          addi            a1,a1,904
0x0000000080201008:  658c              ld              a1,8(a1)
0x000000008020100a:  fffff617          auipc           a2,-4096
# 0x8020000a
0x000000008020100e:  ff660613          addi            a2,a2,-10
0x0000000080201012:  8d91              sub             a1,a1,a2
0x0000000080201014:  90ae              add             ra,ra,a1
0x0000000080201016:  00000617          auipc           a2,0
# 0x80201016
0x000000008020101a:  02e60613          addi            a2,a2,46
0x000000008020101e:  962e              add             a2,a2,a1
0x0000000080201020:  10561073          csrrw           zero,stvec,a2

----------------
IN:
Priv: 1; Virt: 0

0x0000000080201024:  00c55613          srli            a2,a0,12
0x0000000080201028:  83018593          addi            a1,gp,-2000
0x000000008020102c:  618c              ld              a1,0(a1)
0x000000008020102e:  8e4d              or              a2,a2,a1
0x0000000080201030:  010f7517          auipc           a0,17788928
# 0x812f8030
0x0000000080201034:  fd050513          addi            a0,a0,-48
0x0000000080201038:  8131              srli            a0,a0,12
0x000000008020103a:  8d4d              or              a0,a0,a1
0x000000008020103c:  12000073          sfence.vma      zero,zero

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201040: 18051073 csrrw zero,satp,a0

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201044: Address 0x80201044 is out of bounds.

0x0000000080201049: Address 0x80201049 is out of bounds.

0x000000008020104e: Address 0x8020104e is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x0000000080201050: Address 0x80201050 is out of bounds.

0x0000000080201055: Address 0x80201055 is out of bounds.

0x000000008020105a: Address 0x8020105a is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]

----------------
IN:
Priv: 1; Virt: 0
0x000000008020105c: Address 0x8020105c is out of bounds.

Disassembler disagrees with translator over instruction decoding Please report this to [email protected]
------------------------------------------------------------------------------------------------------------------------------

Quoted 1 line

Idan Horowitz

Signature · 2 lines
Regards,
Atish
#4 lore raw
Quoted 6 lines · Atish Patra

I tested on v5.17 built from defconfig for rv64.

Here is the kernel code executing sfence.vma
https://elixir.bootlin.com/linux/v5.17/source/arch/riscv/kernel/head.S#L122

I believe this is a kernel bug and not a QEMU one. They perform a write to the SATP with the same ASID as the one used before (0) and then expect it to be used, without performing an sfence.vma following it.
This was exposed by my change, as previously the write to the satp was performed in the same TB block as the sfence.vma *before it*, which meant the TLB was not filled in between the previous sfence and the write to SATP following it.
I was able to reproduce the issue with the Fedora Rawhide image in the wiki, and I was able to resolve it by artificially forcing a TLB flush following all writes to SATP.
I think the correct course of action here is to:
1. Report the issue to the linux kernel mailing list and/or contribute a patch that adds said missing sfence.vma following the SATP write. (Atish: Are you able to test if adding an sfence.vma in your kernel build fixes the issue?)
2. Restore the patch

Quoted 4 lines

--
Regards,
Atish

Idan Horowitz

#5 lore raw

I agree with you partly, my test case is actually from linux kernel, I notice the strange sfence.vma before write satp during write our teaching kernel. I think, the strange code is used to bypass the qemu bug that Idan patched. Because in hardware, if the stap is empty, sfence.vma will do nothing. And that's why nobody report it.
Before patch, qemu won't end a BB after sfence (but jump and CSR write do). So, the kernel author reodered write stap and sfence.vma to make sfence.vma place in the same BB with write satp, instead of the following write stvec. (If don't reorder, sfence.vma will place in the same BB with write stvec, that will crash kernel, see my origin analysis).

However, in hardware, since tlb is empty, put the first sfence.vma before or after write satp is not really matters.
In qemu, as Atish's log shown, we should do a imply invisible sfence.vma after write stap to invalid qemu's translation cache.

Jinyan

Quoted 39 lines

-----Original Messages-----
From: "Idan Horowitz" <[email protected]>
Sent Time: 2022-03-30 15:35:19 (Wednesday)
To: "Atish Patra" <[email protected]>
Cc: [email protected], "open list:RISC-V" <[email protected]>, "Alistair Francis" <[email protected]>, "[email protected] Developers" <[email protected]>
Subject: Re: [PATCH] target/riscv: Exit current TB after an sfence.vma

Quoted 6 lines · Atish Patra

I tested on v5.17 built from defconfig for rv64.

Here is the kernel code executing sfence.vma
https://elixir.bootlin.com/linux/v5.17/source/arch/riscv/kernel/head.S#L122

I believe this is a kernel bug and not a QEMU one. They perform a write to the SATP with the same ASID as the one used before (0) and then expect it to be used, without performing an sfence.vma following it.
This was exposed by my change, as previously the write to the satp was performed in the same TB block as the sfence.vma *before it*, which meant the TLB was not filled in between the previous sfence and the write to SATP following it.
I was able to reproduce the issue with the Fedora Rawhide image in the wiki, and I was able to resolve it by artificially forcing a TLB flush following all writes to SATP.
I think the correct course of action here is to:
1. Report the issue to the linux kernel mailing list and/or contribute a patch that adds said missing sfence.vma following the SATP write. (Atish: Are you able to test if adding an sfence.vma in your kernel build fixes the issue?)
2. Restore the patch

Quoted 4 lines

--
Regards,
Atish

Idan Horowitz

maintainer
#6 lore raw

[re-ordering the top post]

+linux-riscv, as this may very well be a kernel bug

Quoted 9 lines · On Wed, 30 Mar 2022 05:38:30 PDT (-0700), [email protected] wrote:
Quoted 9 lines

-----Original Messages-----
From: "Idan Horowitz" <[email protected]>
Sent Time: 2022-03-30 15:35:19 (Wednesday)
To: "Atish Patra" <[email protected]>
Cc: [email protected], "open list:RISC-V" <[email protected]>, "Alistair Francis" <[email protected]>, "[email protected] Developers" <[email protected]>
Subject: Re: [PATCH] target/riscv: Exit current TB after an sfence.vma

Quoted 1 line · Atish Patra
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I tested on v5.17 built from defconfig for rv64.

Here is the kernel code executing sfence.vma
https://elixir.bootlin.com/linux/v5.17/source/arch/riscv/kernel/head.S#L122

I believe this is a kernel bug and not a QEMU one. They perform a write to the SATP with the same ASID as the one used before (0) and

I seem to remember ASID 0 being a special one, with some global-ness, but I couldn't find that in a quick poke into the spec. As below, I'm going to read through this a bit more...

(Also, I haven't had any coffee yet)

Quoted 6 lines
Quoted 6 lines

then expect it to be used, without performing an sfence.vma following it.
This was exposed by my change, as previously the write to the satp was performed in the same TB block as the sfence.vma *before it*, which meant the TLB was not filled in between the previous sfence and the write to SATP following it.

Quoted 8 lines
Quoted 8 lines

I was able to reproduce the issue with the Fedora Rawhide image in the wiki, and I was able to resolve it by artificially forcing a TLB flush following all writes to SATP.
I think the correct course of action here is to:
1. Report the issue to the linux kernel mailing list and/or contribute a patch that adds said missing sfence.vma following the SATP write. (Atish: Are you able to test if adding an sfence.vma in your kernel build fixes the issue?)

If it's a kernel bug we should fix it, but I'm not entirely convinced that's the case. I can confirm that the following makes Linux boot again

arch/riscv/kernel/head.S +1 -0
index ec07f991866a..83373c2bd7d0 100644
@@ -121,6 +121,7 @@ relocate:
       or a0, a0, a1
       sfence.vma
       csrw CSR_SATP, a0
+       sfence.vma

.align 2
1:

       /* Set trap vector to spin forever to help debug */

It's been a while since I read through the rules here so I'm going to go read them again, but IIRC that shouldn't be necessary: that first sfence.vma should be sufficiently global to ensure all prior writes to the page tables are visible, regardless of what's in SATP. That said, I remember there being a lot of subtly here in the spec wording so I'm going to go read the spec again.

Quoted 1 line
Quoted 1 line

2. Restore the patch

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 4 lines

I agree with you partly, my test case is actually from linux kernel, I notice the strange sfence.vma before write satp during write our teaching kernel. I think, the strange code is used to bypass the qemu bug that Idan patched.
Because in hardware, if the stap is empty, sfence.vma will do nothing.
And that's why nobody report it.

IIUC the sfence.vma before the SATP write is very explicitly necessary: without that fence old mappings could be utilized directly after the SATP write, so we might not even be able to fetch the next instruction.

Quoted 8 lines

Before patch, qemu won't end a BB after sfence (but jump and CSR write do).
So, the kernel author reodered write stap and sfence.vma to make sfence.vma place in the same BB with write satp, instead of the following write stvec. (If don't reorder, sfence.vma will place in the same BB with write stvec,
that will crash kernel, see my origin analysis).

However, in hardware, since tlb is empty, put the first sfence.vma before or after write satp is not really matters. In qemu, as Atish's log shown, we should do a imply invisible sfence.vma after
write stap to invalid qemu's translation cache.

The ISA manual is quite explicit about SATP not enforcing these orderings. If what I remember about ASID 0 is true then I do think we'd need one, though, to avoid the aliasing -- hopefully I'll make a bit more sense soon, though...

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#7 lore raw
Quoted 98 lines · Palmer Dabbelt

[re-ordering the top post]

+linux-riscv, as this may very well be a kernel bug

Quoted 9 lines · On Wed, 30 Mar 2022 05:38:30 PDT (-0700), [email protected] wrote:
Quoted 9 lines

-----Original Messages-----
From: "Idan Horowitz" <[email protected]>
Sent Time: 2022-03-30 15:35:19 (Wednesday)
To: "Atish Patra" <[email protected]>
Cc: [email protected], "open list:RISC-V" <[email protected]>, "Alistair Francis" <[email protected]>, "[email protected] Developers" <[email protected]>
Subject: Re: [PATCH] target/riscv: Exit current TB after an sfence.vma

Quoted 1 line · Atish Patra
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I tested on v5.17 built from defconfig for rv64.

Here is the kernel code executing sfence.vma
https://elixir.bootlin.com/linux/v5.17/source/arch/riscv/kernel/head.S#L122

I believe this is a kernel bug and not a QEMU one. They perform a write to the SATP with the same ASID as the one used before (0) and

I seem to remember ASID 0 being a special one, with some global-ness, but I couldn't find that in a quick poke into the spec. As below, I'm going to read through this a bit more...

(Also, I haven't had any coffee yet)

Quoted 6 lines
Quoted 6 lines

then expect it to be used, without performing an sfence.vma following it.
This was exposed by my change, as previously the write to the satp was performed in the same TB block as the sfence.vma *before it*, which meant the TLB was not filled in between the previous sfence and the write to SATP following it.

Quoted 8 lines
Quoted 8 lines

I was able to reproduce the issue with the Fedora Rawhide image in the wiki, and I was able to resolve it by artificially forcing a TLB flush following all writes to SATP.
I think the correct course of action here is to:
1. Report the issue to the linux kernel mailing list and/or contribute a patch that adds said missing sfence.vma following the SATP write. (Atish: Are you able to test if adding an sfence.vma in your kernel build fixes the issue?)

If it's a kernel bug we should fix it, but I'm not entirely convinced that's the case. I can confirm that the following makes Linux boot again

arch/riscv/kernel/head.S +1 -0
index ec07f991866a..83373c2bd7d0 100644
@@ -121,6 +121,7 @@ relocate:
        or a0, a0, a1
        sfence.vma
        csrw CSR_SATP, a0
+       sfence.vma
 .align 2
 1:
        /* Set trap vector to spin forever to help debug */

It's been a while since I read through the rules here so I'm going to go read them again, but IIRC that shouldn't be necessary: that first sfence.vma should be sufficiently global to ensure all prior writes to the page tables are visible, regardless of what's in SATP. That said, I remember there being a lot of subtly here in the spec wording so I'm going to go read the spec again.

Quoted 1 line
Quoted 1 line

2. Restore the patch

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 5 lines

I agree with you partly, my test case is actually from linux kernel, I notice the strange sfence.vma before write satp during write our teaching kernel. I think, the strange code is used to bypass the qemu bug that Idan patched. Because in hardware, if the stap is empty, sfence.vma will do nothing. And that's why nobody report it.

IIUC the sfence.vma before the SATP write is very explicitly necessary:
without that fence old mappings could be utilized directly after the SATP write, so we might not even be able to fetch the next instruction.

Quoted 10 lines

Before patch, qemu won't end a BB after sfence (but jump and CSR write do). So, the kernel author reodered write stap and sfence.vma to make sfence.vma place in the same BB with write satp, instead of the following write stvec. (If don't reorder, sfence.vma will place in the same BB with write stvec, that will crash kernel, see my origin analysis).

However, in hardware, since tlb is empty, put the first sfence.vma before or after write satp is not really matters.
In qemu, as Atish's log shown, we should do a imply invisible sfence.vma after write stap to invalid qemu's translation cache.

Quoted 4 lines

The ISA manual is quite explicit about SATP not enforcing these orderings. If what I remember about ASID 0 is true then I do think we'd need one, though, to avoid the aliasing -- hopefully I'll make a bit more sense soon, though...

OK, I must have just been crazy -- there's nothing special about ASID=0. Looks to me like flushing the TLB on SATP writes is necessary, I just sent a patch with a more concrete description of why.

Thanks!

#8 lore raw
In reply to Palmer Dabbelt
Quoted 9 lines · Palmer Dabbelt

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

The opposite in fact, I did not suggest to revert it, but rather undo the revert (as Alistair already removed it from the apply-next tree), since my original patch fixes buggy behaviour that is blocking the testing of some embedded software on QEMU.

Idan Horowitz

#9 lore raw
Quoted 12 lines · Idan Horowitz
Quoted 9 lines · Palmer Dabbelt

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 4 lines

The opposite in fact, I did not suggest to revert it, but rather undo the revert (as Alistair already removed it from the apply-next tree), since my original patch fixes buggy behaviour that is blocking the testing of some embedded software on QEMU.

So, this is a little tricky.

We want to apply the fix, but that will break current users.

Once the fix is merged into Linux we can apply it here. That should hopefully be right at the start of the 7.1 QEMU development window, which should give time for the fix to propagate into stable kernels and not break too many people by the time QEMU is released.

Alistair

Quoted 3 lines

Idan Horowitz

maintainer
#10 lore raw
Quoted 13 lines · On Wed, 30 Mar 2022 20:23:21 PDT (-0700), [email protected] wrote:
Quoted 12 lines · Idan Horowitz
Quoted 9 lines · Palmer Dabbelt

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 4 lines
Quoted 4 lines

The opposite in fact, I did not suggest to revert it, but rather undo the revert (as Alistair already removed it from the apply-next tree), since my original patch fixes buggy behaviour that is blocking the testing of some embedded software on QEMU.

Ah, sorry -- the QEMU tree I was looking at still had the patch in there, must have just been an old one.

Quoted 8 lines

So, this is a little tricky.

We want to apply the fix, but that will break current users.

Once the fix is merged into Linux we can apply it here. That should hopefully be right at the start of the 7.1 QEMU development window, which should give time for the fix to propagate into stable kernels and not break too many people by the time QEMU is released.

If you think this is a Linux bug then that makes sense, but I think this is a QEMU bug -- I sent a patch, not sure if it went through as it didn't make it to lore.

I also think the bug will manifest without the TB exit patch, maybe in single-step mode and definately if we happen to exit the TB at that point for other reasons. Assuming my reasoning is correct in that patch, we may also be hitting this as arbitrary corruption anywhere. I'd started to write up a "QEMU errata" Linux patch for this, but then convinced myself that just adding the sfence.vma was insufficient.

#11 lore raw
Quoted 27 lines · Palmer Dabbelt
Quoted 13 lines · On Wed, 30 Mar 2022 20:23:21 PDT (-0700), [email protected] wrote:
Quoted 12 lines · Idan Horowitz
Quoted 9 lines · Palmer Dabbelt

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 4 lines
Quoted 4 lines

The opposite in fact, I did not suggest to revert it, but rather undo the revert (as Alistair already removed it from the apply-next tree), since my original patch fixes buggy behaviour that is blocking the testing of some embedded software on QEMU.

Ah, sorry -- the QEMU tree I was looking at still had the patch in there, must have just been an old one.

Quoted 4 lines

So, this is a little tricky.

We want to apply the fix, but that will break current users.

Quoted 8 lines
Quoted 4 lines

Once the fix is merged into Linux we can apply it here. That should hopefully be right at the start of the 7.1 QEMU development window, which should give time for the fix to propagate into stable kernels and not break too many people by the time QEMU is released.

If you think this is a Linux bug then that makes sense, but I think this is a QEMU bug -- I sent a patch, not sure if it went through as it didn't make it to lore.

Ah whoops. I saw the patch but didn't read it, then I assumed it was a Linux bug from your diff earlier.

Quoted 7 lines

I also think the bug will manifest without the TB exit patch, maybe in single-step mode and definately if we happen to exit the TB at that point for other reasons. Assuming my reasoning is correct in that patch, we may also be hitting this as arbitrary corruption anywhere. I'd started to write up a "QEMU errata" Linux patch for this, but then convinced myself that just adding the sfence.vma was insufficient.

Yeah, looking at it now I agree, I'll send a PR for 7.0.

Alistair

maintainer
#12 lore raw
Quoted 33 lines · On Wed, 30 Mar 2022 22:13:39 PDT (-0700), [email protected] wrote:
Quoted 32 lines · Palmer Dabbelt
Quoted 13 lines · On Wed, 30 Mar 2022 20:23:21 PDT (-0700), [email protected] wrote:
Quoted 12 lines · Idan Horowitz
Quoted 9 lines · Palmer Dabbelt

Presumably you mean "revert" here? That might be the right way to go, just to avoid breaking users (even if we fix the kernel bug, it'll take a while to get everyone to update). That said, this smells like the sort of thing that's going to crop up at arbitrary times in dynamic systems so while a revert looks like it'd work around the boot issue we might be making more headaches for folks down the road.

Quoted 4 lines
Quoted 4 lines

The opposite in fact, I did not suggest to revert it, but rather undo the revert (as Alistair already removed it from the apply-next tree), since my original patch fixes buggy behaviour that is blocking the testing of some embedded software on QEMU.

Ah, sorry -- the QEMU tree I was looking at still had the patch in there, must have just been an old one.

Quoted 8 lines

So, this is a little tricky.

We want to apply the fix, but that will break current users.

Once the fix is merged into Linux we can apply it here. That should hopefully be right at the start of the 7.1 QEMU development window, which should give time for the fix to propagate into stable kernels and not break too many people by the time QEMU is released.

Quoted 6 lines
Quoted 3 lines

If you think this is a Linux bug then that makes sense, but I think this is a QEMU bug -- I sent a patch, not sure if it went through as it didn't make it to lore.

Ah whoops. I saw the patch but didn't read it, then I assumed it was a Linux bug from your diff earlier.

No problem, that was the first thing I sent in the morning so I doubt it made any sense.

Quoted 10 lines
Quoted 8 lines

I also think the bug will manifest without the TB exit patch, maybe in single-step mode and definately if we happen to exit the TB at that point for other reasons. Assuming my reasoning is correct in that patch, we may also be hitting this as arbitrary corruption anywhere. I'd started to write up a "QEMU errata" Linux patch for this, but then convinced myself that just adding the sfence.vma was insufficient.

Yeah, looking at it now I agree, I'll send a PR for 7.0.

Thanks!