All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions.
@ 2026-11-20  9:34 WX Chen
  2026-11-24 18:42 ` Daniel Henrique Barboza
  2026-11-25  1:52 ` Chao Liu
  0 siblings, 2 replies; 5+ messages in thread
From: WX Chen @ 2026-11-20  9:34 UTC (permalink / raw)
  To: qemu-devel
  Cc: Palmer Dabbelt, Alistair Francis, Weiwei Li,
	Daniel Henrique Barboza, Liu Zhiwei, Chao Liu, qemu-riscv,
	Tao Tang, WX Chen

Currently, they are implemented as full memory barriers, which may introduce stronger host barriers than the guest actually requires.

Signed-off-by: WX Chen <wxchen0913@gmail.com>
---
 target/riscv/tcg/insn_trans/trans_rvi.c.inc | 36 +++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/target/riscv/tcg/insn_trans/trans_rvi.c.inc b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
index 4784ea1607e74eb5b0076425a732b5d3817b3482..6e094e5886360c2f9f7c187e82e6f60da36b84e4 100644
--- a/target/riscv/tcg/insn_trans/trans_rvi.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
@@ -968,8 +968,40 @@ static bool trans_pause(DisasContext *ctx, arg_pause *a)
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {
-    /* FENCE is a full memory barrier. */
-    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
+    /*
+     * FENCE.TSO orders all R/W pairs except W -> R.
+     * For TSO, the pred and succ should all be RW,
+     * otherwise it should be treated as normal FENCE.
+     */
+    if ((ctx->opcode & 0xf0000000) == 0x80000000 &&
+        a->pred == 3 && a->succ == 3) {
+        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_SC);
+        return true;
+    }
+
+    /* Reserved case, choose the strongest ordering.  */
+    if (ctx->opcode & 0xf0000000) {
+        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
+        return true;
+    }
+
+    /* Normal FENCE. I/R for BIT 3/BIT 1, O/W for BIT 2/BIT 0 */
+    TCGBar ordering = 0;
+    if ((a->pred & 0xa) && (a->succ & 0xa)) {
+        ordering |= TCG_MO_LD_LD;
+    }
+    if ((a->pred & 0xa) && (a->succ & 0x5)) {
+        ordering |= TCG_MO_LD_ST;
+    }
+    if ((a->pred & 0x5) && (a->succ & 0xa)) {
+        ordering |= TCG_MO_ST_LD;
+    }
+    if ((a->pred & 0x5) && (a->succ & 0x5)) {
+        ordering |= TCG_MO_ST_ST;
+    }
+    if (ordering) {
+        tcg_gen_mb(ordering | TCG_BAR_SC);
+    }
     return true;
 }
 

---
base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
change-id: 20261120-fix-riscv-fence-20261119-10a425b78673

Best regards,
-- 
WX Chen <wxchen0913@gmail.com>



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions.
  2026-11-20  9:34 [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions WX Chen
@ 2026-11-24 18:42 ` Daniel Henrique Barboza
  2026-11-25  1:45   ` WX Chen
  2026-11-25  1:52 ` Chao Liu
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Henrique Barboza @ 2026-11-24 18:42 UTC (permalink / raw)
  To: WX Chen, qemu-devel
  Cc: Palmer Dabbelt, Alistair Francis, Weiwei Li, Liu Zhiwei, Chao Liu,
	qemu-riscv, Tao Tang

Hi,

Looks good overall.  A few comments: the commit title

"Provide weaker memory ordering for RISC-V guest FENCE instructions."

We add the submodule we're changing in it.  For this patch it could be:

"target/riscv: weaker memory ordering for guest FENCE instructions"

One more thing:

On 11/20/2026 6:34 AM, WX Chen wrote:
> Currently, they are implemented as full memory barriers, which may introduce stronger host barriers than the guest actually requires.
> 
> Signed-off-by: WX Chen <wxchen0913@gmail.com>
> ---
>   target/riscv/tcg/insn_trans/trans_rvi.c.inc | 36 +++++++++++++++++++++++++++--
>   1 file changed, 34 insertions(+), 2 deletions(-)
> 
> diff --git a/target/riscv/tcg/insn_trans/trans_rvi.c.inc b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> index 4784ea1607e74eb5b0076425a732b5d3817b3482..6e094e5886360c2f9f7c187e82e6f60da36b84e4 100644
> --- a/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> +++ b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> @@ -968,8 +968,40 @@ static bool trans_pause(DisasContext *ctx, arg_pause *a)
>   
>   static bool trans_fence(DisasContext *ctx, arg_fence *a)
>   {
> -    /* FENCE is a full memory barrier. */
> -    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
> +    /*
> +     * FENCE.TSO orders all R/W pairs except W -> R.
> +     * For TSO, the pred and succ should all be RW,
> +     * otherwise it should be treated as normal FENCE.
> +     */
> +    if ((ctx->opcode & 0xf0000000) == 0x80000000 &&
> +        a->pred == 3 && a->succ == 3) {
> +        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_SC);
> +        return true;
> +    }
> +
> +    /* Reserved case, choose the strongest ordering.  */
> +    if (ctx->opcode & 0xf0000000) {
> +        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
> +        return true;
> +    }
> +
> +    /* Normal FENCE. I/R for BIT 3/BIT 1, O/W for BIT 2/BIT 0 */
> +    TCGBar ordering = 0;

For our current code style, unless we're in a ifcond branch or switch, var declarations
are always in the beginning of functions.


Thanks,
Daniel

> +    if ((a->pred & 0xa) && (a->succ & 0xa)) {
> +        ordering |= TCG_MO_LD_LD;
> +    }
> +    if ((a->pred & 0xa) && (a->succ & 0x5)) {
> +        ordering |= TCG_MO_LD_ST;
> +    }
> +    if ((a->pred & 0x5) && (a->succ & 0xa)) {
> +        ordering |= TCG_MO_ST_LD;
> +    }
> +    if ((a->pred & 0x5) && (a->succ & 0x5)) {
> +        ordering |= TCG_MO_ST_ST;
> +    }
> +    if (ordering) {
> +        tcg_gen_mb(ordering | TCG_BAR_SC);
> +    }
>       return true;
>   }
>   
> 
> ---
> base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
> change-id: 20261120-fix-riscv-fence-20261119-10a425b78673
> 
> Best regards,



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions.
  2026-11-24 18:42 ` Daniel Henrique Barboza
@ 2026-11-25  1:45   ` WX Chen
  0 siblings, 0 replies; 5+ messages in thread
From: WX Chen @ 2026-11-25  1:45 UTC (permalink / raw)
  To: Daniel Henrique Barboza, qemu-devel
  Cc: Palmer Dabbelt, Alistair Francis, Weiwei Li, Liu Zhiwei, Chao Liu,
	qemu-riscv, Tao Tang

Hi Daniel,

     Thanks for the comment!

> We add the submodule we're changing in it.  For this patch it could be:
>
> "target/riscv: weaker memory ordering for guest FENCE instructions" 
Yes I missed it, will do it on v2.

> For our current code style, unless we're in a ifcond branch or switch, 
> var declarations
> are always in the beginning of functions.

Got it. Will fix it on v2.

Thanks,
Chen

>
> Thanks,
> Daniel
>
>> +    if ((a->pred & 0xa) && (a->succ & 0xa)) {
>> +        ordering |= TCG_MO_LD_LD;
>> +    }
>> +    if ((a->pred & 0xa) && (a->succ & 0x5)) {
>> +        ordering |= TCG_MO_LD_ST;
>> +    }
>> +    if ((a->pred & 0x5) && (a->succ & 0xa)) {
>> +        ordering |= TCG_MO_ST_LD;
>> +    }
>> +    if ((a->pred & 0x5) && (a->succ & 0x5)) {
>> +        ordering |= TCG_MO_ST_ST;
>> +    }
>> +    if (ordering) {
>> +        tcg_gen_mb(ordering | TCG_BAR_SC);
>> +    }
>>       return true;
>>   }
>>
>> ---
>> base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
>> change-id: 20261120-fix-riscv-fence-20261119-10a425b78673
>>
>> Best regards,
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions.
  2026-11-20  9:34 [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions WX Chen
  2026-11-24 18:42 ` Daniel Henrique Barboza
@ 2026-11-25  1:52 ` Chao Liu
  2026-11-25  2:26   ` WX Chen
  1 sibling, 1 reply; 5+ messages in thread
From: Chao Liu @ 2026-11-25  1:52 UTC (permalink / raw)
  To: WX Chen
  Cc: Chao Liu, qemu-devel, Palmer Dabbelt, Alistair Francis, Weiwei Li,
	Daniel Henrique Barboza, Liu Zhiwei, qemu-riscv, Tao Tang

On Fri, 20 Nov 2026 17:34:54 +0800 WX Chen <wxchen0913@gmail.com> wrote:

> Currently, they are implemented as full memory barriers, which may introduce stronger host barriers than the guest actually requires.

Please wrap the commit body at 76 columns.

https://www.qemu.org/docs/master/devel/submitting-a-patch.html

Thanks
Chao
> 
> Signed-off-by: WX Chen <wxchen0913@gmail.com>
> ---
>  target/riscv/tcg/insn_trans/trans_rvi.c.inc | 36 +++++++++++++++++++++++++++--
>  1 file changed, 34 insertions(+), 2 deletions(-)
> 
> diff --git a/target/riscv/tcg/insn_trans/trans_rvi.c.inc b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> index 4784ea1607e74eb5b0076425a732b5d3817b3482..6e094e5886360c2f9f7c187e82e6f60da36b84e4 100644
> --- a/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> +++ b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
> @@ -968,8 +968,40 @@ static bool trans_pause(DisasContext *ctx, arg_pause *a)
>  
>  static bool trans_fence(DisasContext *ctx, arg_fence *a)
>  {
> -    /* FENCE is a full memory barrier. */
> -    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
> +    /*
> +     * FENCE.TSO orders all R/W pairs except W -> R.
> +     * For TSO, the pred and succ should all be RW,
> +     * otherwise it should be treated as normal FENCE.
> +     */
> +    if ((ctx->opcode & 0xf0000000) == 0x80000000 &&
> +        a->pred == 3 && a->succ == 3) {
> +        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_SC);
> +        return true;
> +    }
> +
> +    /* Reserved case, choose the strongest ordering.  */
> +    if (ctx->opcode & 0xf0000000) {
> +        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
> +        return true;
> +    }
> +
> +    /* Normal FENCE. I/R for BIT 3/BIT 1, O/W for BIT 2/BIT 0 */
> +    TCGBar ordering = 0;
> +    if ((a->pred & 0xa) && (a->succ & 0xa)) {
> +        ordering |= TCG_MO_LD_LD;
> +    }
> +    if ((a->pred & 0xa) && (a->succ & 0x5)) {
> +        ordering |= TCG_MO_LD_ST;
> +    }
> +    if ((a->pred & 0x5) && (a->succ & 0xa)) {
> +        ordering |= TCG_MO_ST_LD;
> +    }
> +    if ((a->pred & 0x5) && (a->succ & 0x5)) {
> +        ordering |= TCG_MO_ST_ST;
> +    }
> +    if (ordering) {
> +        tcg_gen_mb(ordering | TCG_BAR_SC);
> +    }
>      return true;
>  }
>  
> 
> ---
> base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
> change-id: 20261120-fix-riscv-fence-20261119-10a425b78673
> 
> Best regards,
> -- 
> WX Chen <wxchen0913@gmail.com>
> 
> 
> 

Sent using hkml (https://github.com/sjp38/hackermail)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions.
  2026-11-25  1:52 ` Chao Liu
@ 2026-11-25  2:26   ` WX Chen
  0 siblings, 0 replies; 5+ messages in thread
From: WX Chen @ 2026-11-25  2:26 UTC (permalink / raw)
  To: Chao Liu
  Cc: qemu-devel, Palmer Dabbelt, Alistair Francis, Weiwei Li,
	Daniel Henrique Barboza, Liu Zhiwei, qemu-riscv, Tao Tang

[-- Attachment #1: Type: text/plain, Size: 2728 bytes --]

Hi,
     Thanks for the comment!

On 11/25/26 09:52, Chao Liu wrote:
> On Fri, 20 Nov 2026 17:34:54 +0800 WX Chen<wxchen0913@gmail.com> wrote:
>
>> Currently, they are implemented as full memory barriers, which may introduce stronger host barriers than the guest actually requires.
> Please wrap the commit body at 76 columns.
>
> https://www.qemu.org/docs/master/devel/submitting-a-patch.html
>
> Thanks
> Chao

|Thanks for pointing this out. I'll wrap the commit body at 76 columns 
in v2. Thanks, Chen |

>> Signed-off-by: WX Chen<wxchen0913@gmail.com>
>> ---
>>   target/riscv/tcg/insn_trans/trans_rvi.c.inc | 36 +++++++++++++++++++++++++++--
>>   1 file changed, 34 insertions(+), 2 deletions(-)
>>
>> diff --git a/target/riscv/tcg/insn_trans/trans_rvi.c.inc b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
>> index 4784ea1607e74eb5b0076425a732b5d3817b3482..6e094e5886360c2f9f7c187e82e6f60da36b84e4 100644
>> --- a/target/riscv/tcg/insn_trans/trans_rvi.c.inc
>> +++ b/target/riscv/tcg/insn_trans/trans_rvi.c.inc
>> @@ -968,8 +968,40 @@ static bool trans_pause(DisasContext *ctx, arg_pause *a)
>>   
>>   static bool trans_fence(DisasContext *ctx, arg_fence *a)
>>   {
>> -    /* FENCE is a full memory barrier. */
>> -    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
>> +    /*
>> +     * FENCE.TSO orders all R/W pairs except W -> R.
>> +     * For TSO, the pred and succ should all be RW,
>> +     * otherwise it should be treated as normal FENCE.
>> +     */
>> +    if ((ctx->opcode & 0xf0000000) == 0x80000000 &&
>> +        a->pred == 3 && a->succ == 3) {
>> +        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_SC);
>> +        return true;
>> +    }
>> +
>> +    /* Reserved case, choose the strongest ordering.  */
>> +    if (ctx->opcode & 0xf0000000) {
>> +        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
>> +        return true;
>> +    }
>> +
>> +    /* Normal FENCE. I/R for BIT 3/BIT 1, O/W for BIT 2/BIT 0 */
>> +    TCGBar ordering = 0;
>> +    if ((a->pred & 0xa) && (a->succ & 0xa)) {
>> +        ordering |= TCG_MO_LD_LD;
>> +    }
>> +    if ((a->pred & 0xa) && (a->succ & 0x5)) {
>> +        ordering |= TCG_MO_LD_ST;
>> +    }
>> +    if ((a->pred & 0x5) && (a->succ & 0xa)) {
>> +        ordering |= TCG_MO_ST_LD;
>> +    }
>> +    if ((a->pred & 0x5) && (a->succ & 0x5)) {
>> +        ordering |= TCG_MO_ST_ST;
>> +    }
>> +    if (ordering) {
>> +        tcg_gen_mb(ordering | TCG_BAR_SC);
>> +    }
>>       return true;
>>   }
>>   
>>
>> ---
>> base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
>> change-id: 20261120-fix-riscv-fence-20261119-10a425b78673
>>
>> Best regards,
>> -- 
>> WX Chen<wxchen0913@gmail.com>
>>
>>
>>
> Sent using hkml (https://github.com/sjp38/hackermail)

[-- Attachment #2: Type: text/html, Size: 3941 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-25  2:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  9:34 [PATCH] Provide weaker memory ordering for RISC-V guest FENCE instructions WX Chen
2026-11-24 18:42 ` Daniel Henrique Barboza
2026-11-25  1:45   ` WX Chen
2026-11-25  1:52 ` Chao Liu
2026-11-25  2:26   ` WX Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.