Skip to contentSkip to conversation

pc-bios/s390-ccw: Use memory barriers in virtio code

PATCH In review Thomas Huth 5 participants 18 messages Feb 16, 2021 – Feb 17, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pc-bios/s390-ccw: Use memory barriers in virtio code

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

---
 pc-bios/s390-ccw/virtio-net.c | 1 +
 pc-bios/s390-ccw/virtio.c     | 1 +
 pc-bios/s390-ccw/virtio.h     | 2 ++
 3 files changed, 4 insertions(+)
pc-bios/s390-ccw/virtio-net.c +1 -0
index 2fcb0a58c5..25598a7a97 100644
@@ -127,6 +127,7 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
     /* Mark buffer as available to the host again */
     rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id;
+    virtio_mb();
     rxvq->avail->idx = rxvq->avail->idx + 1;
     vring_notify(rxvq);
 
pc-bios/s390-ccw/virtio.c +1 -0
index ab49840db8..fb9687f9b3 100644
@@ -154,6 +154,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
 
     /* Chains only have a single ID */
     if (!(flags & VRING_DESC_F_NEXT)) {
+        virtio_mb();
         vr->avail->idx++;
     }
 }
pc-bios/s390-ccw/virtio.h +2 -0
index 19fceb6495..6ac65482a9 100644
@@ -271,6 +271,8 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+#define virtio_mb()  asm volatile("bcr 14,0" : : : "memory")
+
 bool vring_notify(VRing *vr);
 int drain_irqs(SubChannelId schid);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 11 lines · Thomas Huth

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 6 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

This noticeably increases the reliability of the test for me:
it goes from failing one time in 2 or 3 to failing about one time in 5 or 6. However it does still hit the virtio_scsi_parse_req() check on "out_size && in_size" eventually. So my guess is that this is not a sufficient supply of barriers. I'm firmly not a virtio expert but my reading of the spec suggested that some of the way the s390-ccw code is doing things might be in the wrong order and need restructuring beyond merely "add barriers to existing code".

thanks
-- PMM

#2 lore raw
Quoted 19 lines · Peter Maydell
Quoted 11 lines · Thomas Huth

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 6 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

Quoted 8 lines

This noticeably increases the reliability of the test for me:
it goes from failing one time in 2 or 3 to failing about one time in 5 or 6. However it does still hit the virtio_scsi_parse_req() check on "out_size && in_size" eventually. So my guess is that this is not a sufficient supply of barriers. I'm firmly not a virtio expert but my reading of the spec suggested that some of the way the s390-ccw code is doing things might be in the wrong order and need restructuring beyond merely "add barriers to existing code".

I've had a look at the virtio 1.0 spec and looked for "barrier" there, and these two spots were the only ones that I identified that were definitely missing. The other spots in the spec that talk about memory barriers rather seem to be related to notification suppression, which we are not doing here, if I've got that right. So no clue what else could be going wrong here on your aarch64 host...

Thomas

#3 lore raw
In reply to Thomas Huth
Quoted 40 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

---
 pc-bios/s390-ccw/virtio-net.c | 1 +
 pc-bios/s390-ccw/virtio.c     | 1 +
 pc-bios/s390-ccw/virtio.h     | 2 ++
 3 files changed, 4 insertions(+)
pc-bios/s390-ccw/virtio-net.c +1 -0
index 2fcb0a58c5..25598a7a97 100644
@@ -127,6 +127,7 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
     /* Mark buffer as available to the host again */
     rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id;
+    virtio_mb();
     rxvq->avail->idx = rxvq->avail->idx + 1;
     vring_notify(rxvq);
 
pc-bios/s390-ccw/virtio.c +0 -0
index ab49840db8..fb9687f9b3 100644

pc-bios/s390-ccw/virtio.c void vring_send_buf(VRing *vr, void *p, int len, int flags) · line 157

Quoted 4 lines
pc-bios/s390-ccw/virtio.c +1 -0
@@ -154,6 +154,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
 
     /* Chains only have a single ID */
     if (!(flags & VRING_DESC_F_NEXT)) {
+        virtio_mb();

I think you need to also need barriers for changes to the buffers, as the spec talks about "manipulating the descriptor table".

pc-bios/s390-ccw/virtio.h struct VirtioCmd { · line 274

Quoted 12 lines
pc-bios/s390-ccw/virtio.c +0 -0
         vr->avail->idx++;
     }
 }
pc-bios/s390-ccw/virtio.h +1 -0
index 19fceb6495..6ac65482a9 100644
@@ -271,6 +271,8 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+#define virtio_mb()  asm volatile("bcr 14,0" : : : "memory")

The bios is built for z900, so you probably need a bcr15 here?

Quoted 4 lines
pc-bios/s390-ccw/virtio.h +1 -0
+
 bool vring_notify(VRing *vr);
 int drain_irqs(SubChannelId schid);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
#4 lore raw
Quoted 44 lines · Cornelia Huck
Quoted 41 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

---
  pc-bios/s390-ccw/virtio-net.c | 1 +
  pc-bios/s390-ccw/virtio.c     | 1 +
  pc-bios/s390-ccw/virtio.h     | 2 ++
  3 files changed, 4 insertions(+)
pc-bios/s390-ccw/virtio-net.c +1 -0
index 2fcb0a58c5..25598a7a97 100644
@@ -127,6 +127,7 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
      /* Mark buffer as available to the host again */
      rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id;
+    virtio_mb();
      rxvq->avail->idx = rxvq->avail->idx + 1;
      vring_notify(rxvq);
 
pc-bios/s390-ccw/virtio.c +0 -0
index ab49840db8..fb9687f9b3 100644
@@ -154,6 +154,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
 
Quoted 6 lines

pc-bios/s390-ccw/virtio.c void vring_send_buf(VRing *vr, void *p, int len, int flags) · line 157

Quoted 3 lines
pc-bios/s390-ccw/virtio.c +1 -0
      /* Chains only have a single ID */
      if (!(flags & VRING_DESC_F_NEXT)) {
+        virtio_mb();

I think you need to also need barriers for changes to the buffers, as the spec talks about "manipulating the descriptor table".

Which paragraph in the virtio spec are you refering to here? I can't find that part right now...

Quoted 11 lines
Quoted 11 lines
pc-bios/s390-ccw/virtio.c +0 -0
          vr->avail->idx++;
      }
  }
pc-bios/s390-ccw/virtio.h +0 -0
index 19fceb6495..6ac65482a9 100644
@@ -271,6 +271,8 @@ struct VirtioCmd {
  };
  typedef struct VirtioCmd VirtioCmd;
 
Quoted 3 lines

pc-bios/s390-ccw/virtio.h struct VirtioCmd { · line 274

Quoted 1 line
pc-bios/s390-ccw/virtio.h +1 -0
+#define virtio_mb()  asm volatile("bcr 14,0" : : : "memory")

The bios is built for z900, so you probably need a bcr15 here?

I thought about that, too, but for TCG, it currently should not matter since both, 14 and 15, end up with the same code in op_bc() in target/s390x/translate.c. And on a real host, we've never seen this problem to occur, so it should not matter there, too. But if you prefer (e.g. in case somebody tweaks the TCG implementation one day), I can also switch to bcr15 instead.

Thomas

#5 lore raw
Quoted 52 lines · On Tue, 16 Feb 2021 15:21:45 +0100 Thomas Huth <[email protected]> wrote:
Quoted 44 lines · Cornelia Huck
Quoted 41 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

---
  pc-bios/s390-ccw/virtio-net.c | 1 +
  pc-bios/s390-ccw/virtio.c     | 1 +
  pc-bios/s390-ccw/virtio.h     | 2 ++
  3 files changed, 4 insertions(+)
pc-bios/s390-ccw/virtio-net.c +1 -0
index 2fcb0a58c5..25598a7a97 100644
@@ -127,6 +127,7 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
      /* Mark buffer as available to the host again */
      rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id;
+    virtio_mb();
      rxvq->avail->idx = rxvq->avail->idx + 1;
      vring_notify(rxvq);
 
pc-bios/s390-ccw/virtio.c +0 -0
index ab49840db8..fb9687f9b3 100644
@@ -154,6 +154,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags)
 
Quoted 6 lines

pc-bios/s390-ccw/virtio.c void vring_send_buf(VRing *vr, void *p, int len, int flags) · line 157

Quoted 3 lines
pc-bios/s390-ccw/virtio.c +1 -0
      /* Chains only have a single ID */
      if (!(flags & VRING_DESC_F_NEXT)) {
+        virtio_mb();  

I think you need to also need barriers for changes to the buffers, as the spec talks about "manipulating the descriptor table".

Quoted 2 lines

Which paragraph in the virtio spec are you refering to here? I can't find that part right now...

Step 4 in "2.7.13 Supplying Buffers to The Device":

"The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step."

Quoted 16 lines
Quoted 12 lines
Quoted 12 lines
pc-bios/s390-ccw/virtio.c +0 -0
          vr->avail->idx++;
      }
  }
pc-bios/s390-ccw/virtio.h +0 -0
index 19fceb6495..6ac65482a9 100644
@@ -271,6 +271,8 @@ struct VirtioCmd {
  };
  typedef struct VirtioCmd VirtioCmd;
 
Quoted 3 lines

pc-bios/s390-ccw/virtio.h struct VirtioCmd { · line 274

Quoted 1 line
pc-bios/s390-ccw/virtio.h +1 -0
+#define virtio_mb()  asm volatile("bcr 14,0" : : : "memory")  

The bios is built for z900, so you probably need a bcr15 here?

Quoted 6 lines

I thought about that, too, but for TCG, it currently should not matter since both, 14 and 15, end up with the same code in op_bc() in target/s390x/translate.c. And on a real host, we've never seen this problem to occur, so it should not matter there, too. But if you prefer (e.g. in case somebody tweaks the TCG implementation one day), I can also switch to bcr15 instead.

OK, if they are both implemented with the same code, it should not really matter. We don't run on any hardware that doesn't support bcr14 anyway.

#6 lore raw
Quoted 5 lines · Cornelia Huck

Step 4 in "2.7.13 Supplying Buffers to The Device":

"The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step."

I thought that my first time through the spec as well, but I think the whole of section 2.7 is dealing with "packed virtqueues", which have to be explicitly negotiated and which I don't think the s390-ccw code is doing.

thnaks
-- PMM

#7 lore raw
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Cornelia Huck

Step 4 in "2.7.13 Supplying Buffers to The Device":

"The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step."

I thought that my first time through the spec as well, but I think the whole of section 2.7 is dealing with "packed virtqueues", which have to be explicitly negotiated and which I don't think the s390-ccw code is doing.

Right. I think the s390-ccw code is still based on virtio v1.0, that's why I also only looked at that version of the spec.

Thomas

#8 lore raw
Quoted 9 lines · Thomas Huth
Quoted 7 lines · Peter Maydell
Quoted 5 lines · Cornelia Huck

Step 4 in "2.7.13 Supplying Buffers to The Device":

"The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step."

Quoted 7 lines
Quoted 4 lines

I thought that my first time through the spec as well, but I think the whole of section 2.7 is dealing with "packed virtqueues", which have to be explicitly negotiated and which I don't think the s390-ccw code is doing.

Right. I think the s390-ccw code is still based on virtio v1.0, that's why I also only looked at that version of the spec.

I think the ideal would be to find somebody who's really well acquainted with the virtio spec (MST?) and ask them to have a quick look-over the s390-ccw code to say where it needs changes... The fact that this patch doesn't completely fix the bug leaves me suspicious that we're missing something in our readings of the spec.

-- PMM

#10 lore raw
In reply to Thomas Huth
Quoted 11 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 4 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

I suggest to the barrier after incrementing the idx field for two reasons. First: If the device were to see the notification, but not see the incremented idx field, it would effectively loose initiative. That is pretty straight forward, because the notification just says 'check out that queue', and if we don't see the incremented index, miss the buffer that was made available by incrementing idx.

Second: We are in the bios, and I hope even an unnecessary barrier would not hurt us significantly.

Conny, what do you think?

Regards,
Halil

#9 lore raw
In reply to Peter Maydell
Quoted 6 lines · On Tue, 16 Feb 2021 14:37:22 +0000 Peter Maydell <[email protected]>…
Quoted 5 lines · Thomas Huth
Quoted 3 lines · Peter Maydell
Quoted 2 lines · Cornelia Huck

Step 4 in "2.7.13 Supplying Buffers to The Device":

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

"The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step."

I thought that my first time through the spec as well, but I think the whole of section 2.7 is dealing with "packed virtqueues", which have to be explicitly negotiated and which I don't think the s390-ccw code is doing.

2.7 is split, 2.8 packed (at least in my built-from-git version; maybe I should have mentioned that :)

Quoted 3 lines
Quoted 3 lines

Right. I think the s390-ccw code is still based on virtio v1.0, that's why I also only looked at that version of the spec.

Yes, the bios code is using split.

Quoted 6 lines

I think the ideal would be to find somebody who's really well acquainted with the virtio spec (MST?) and ask them to have a quick look-over the s390-ccw code to say where it needs changes... The fact that this patch doesn't completely fix the bug leaves me suspicious that we're missing something in our readings of the spec.

Unfortunately, the bios virtio code is not that easy to follow :/

#11 lore raw
In reply to Halil Pasic
Quoted 27 lines · On Tue, 16 Feb 2021 15:40:10 +0100 Halil Pasic <[email protected]> wrote:
Quoted 11 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 4 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

I suggest to the barrier after incrementing the idx field for two reasons. First: If the device were to see the notification, but not see the incremented idx field, it would effectively loose initiative. That is pretty straight forward, because the notification just says 'check out that queue', and if we don't see the incremented index, miss the buffer that was made available by incrementing idx.

Quoted 2 lines

Second: We are in the bios, and I hope even an unnecessary barrier would not hurt us significantly.

Yes to both, I think that is worth a try.

Quoted 6 lines

Conny, what do you think?

Regards,
Halil

#12 lore raw
In reply to Halil Pasic
Quoted 19 lines · Halil Pasic
Quoted 11 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 4 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

Quoted 7 lines

I suggest to the barrier after incrementing the idx field for two reasons. First: If the device were to see the notification, but not see the incremented idx field, it would effectively loose initiative. That is pretty straight forward, because the notification just says 'check out that queue', and if we don't see the incremented index, miss the buffer that was made available by incrementing idx.

I was just about to reply that this is certainly not necessary, since the DIAGNOSE instruction that we use for the notification hypercall should be serializing anyway ... but after looking at the PoP, it actually is not marked as a serializing instruction! (while e.g. SVC - supervisor call - is explicitly marked as serializing)

So maybe that's worth a try: Peter, could you please apply this patch on top an see whether it makes a difference?

pc-bios/s390-ccw/virtio.c +1 -0
@@ -54,6 +54,7 @@ static long kvm_hypercall(unsigned long nr, unsigned long param1,
     register ulong r_param3 asm("4") = param3;
     register long retval asm("2");
 
+    virtio_mb();
     asm volatile ("diag 2,4,0x500"
                   : "=d" (retval)
                   : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)

Thanks,
Thomas

#13 lore raw
Quoted 13 lines · Thomas Huth

I was just about to reply that this is certainly not necessary, since the DIAGNOSE instruction that we use for the notification hypercall should be serializing anyway ... but after looking at the PoP, it actually is not marked as a serializing instruction! (while e.g. SVC - supervisor call - is explicitly marked as serializing)

So maybe that's worth a try: Peter, could you please apply this patch on top an see whether it makes a difference?

pc-bios/s390-ccw/virtio.c static long kvm_hypercall(unsigned long nr, unsigned long param1, · line 60

Quoted 7 lines
pc-bios/s390-ccw/virtio.c +1 -0
@@ -54,6 +54,7 @@ static long kvm_hypercall(unsigned long nr, unsigned long param1,
      register ulong r_param3 asm("4") = param3;
      register long retval asm("2");
 
+    virtio_mb();
      asm volatile ("diag 2,4,0x500"
                    : "=d" (retval)
                    : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)

Doesn't really help (maybe brings the occurrence rate down a bit more, towards about 1 in 9?)

-- PMM

#14 lore raw
In reply to Thomas Huth
Quoted 13 lines · On Tue, 16 Feb 2021 17:15:19 +0100 Thomas Huth <[email protected]> wrote:
Quoted 5 lines
Quoted 4 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

Quoted 7 lines

I suggest to the barrier after incrementing the idx field for two reasons. First: If the device were to see the notification, but not see the incremented idx field, it would effectively loose initiative. That is pretty straight forward, because the notification just says 'check out that queue', and if we don't see the incremented index, miss the buffer that was made available by incrementing idx.

Quoted 5 lines

I was just about to reply that this is certainly not necessary, since the DIAGNOSE instruction that we use for the notification hypercall should be serializing anyway ... but after looking at the PoP, it actually is not marked as a serializing instruction! (while e.g. SVC - supervisor call - is explicitly marked as serializing)

I was talking about whether a suitable barrier is needed, from virtio perspective. How the suitable barrier is put in place is a different issue.

Regards,
Halil

#15 lore raw
In reply to Thomas Huth
Quoted 44 lines · Thomas Huth
Quoted 19 lines · Halil Pasic
Quoted 11 lines · On Tue, 16 Feb 2021 12:00:56 +0100 Thomas Huth <[email protected]> wrote:

According to the virtio specification, a memory barrier should be used before incrementing the idx field in the "available" ring. So far, we did not do this in the s390-ccw bios yet, but recently Peter Maydell saw problems with the s390-ccw bios when running the qtests on an aarch64 host (the bios panic'ed with the message:
"SCSI cannot report LUNs: response VS RESP=09"), which could maybe be related to the missing memory barriers. Thus let's add those barriers now. Since we've only seen the problem on TCG so far, a "bcr 14,0" should be sufficient here to trigger the tcg_gen_mb() in the TCG translate code.

Quoted 4 lines

(Note: The virtio spec also talks about using a memory barrier *after* incrementing the idx field, but if I understood correctly this is only required when using notification suppression - which we don't use in the s390-ccw bios here)

Quoted 7 lines

I suggest to the barrier after incrementing the idx field for two reasons. First: If the device were to see the notification, but not see the incremented idx field, it would effectively loose initiative. That is pretty straight forward, because the notification just says 'check out that queue', and if we don't see the incremented index, miss the buffer that was made available by incrementing idx.

I was just about to reply that this is certainly not necessary, since the DIAGNOSE instruction that we use for the notification hypercall should be serializing anyway ... but after looking at the PoP, it actually is not marked as a serializing instruction! (while e.g. SVC - supervisor call - is explicitly marked as serializing)

So maybe that's worth a try: Peter, could you please apply this patch on top an see whether it makes a difference?

pc-bios/s390-ccw/virtio.c +0 -0
@@ -54,6 +54,7 @@ static long kvm_hypercall(unsigned long nr, unsigned long
 param1,
      register ulong r_param3 asm("4") = param3;
      register long retval asm("2");
Quoted 5 lines


+    virtio_mb();
     asm volatile ("diag 2,4,0x500"
                   : "=d" (retval)
                   : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)

This is patching the wrong thing, IMO. You're adding barriers to the guest that I think ought not be architecturally required -- memory accesses on s390x are strongly ordered, with or without diag/svc/whatever.

With

tcg/aarch64/tcg-target.c.inc +2 -0
index 1376cdc404..3c5f38be62 100644
@@ -1622,6 +1622,8 @@ static void tcg_out_tlb_read
     TCGType mask_type;
     uint64_t compare_mask;
 
+    tcg_out_mb(s, TCG_MO_ALL);
+
     mask_type = (TARGET_PAGE_BITS + CPU_TLB_DYN_MAX_BITS > 32
                  ? TCG_TYPE_I64 : TCG_TYPE_I32);
 

which is a gigantic hammer, adding a host barrier before every qemu guest access, I can no longer provoke a failure (previously visible 1 in 4, now no failures in 100).

With that as a data point for success, I'm going to try to use host load-acquire / store-release instructions, and then apply TCG_GUEST_DEFAULT_MO and see if I can find something that works reasonably.

r~

#16 lore raw
Quoted 20 lines · Richard Henderson

On 2/16/21 8:15 AM, Thomas Huth wrote:
With

tcg/aarch64/tcg-target.c.inc +2 -0
index 1376cdc404..3c5f38be62 100644
@@ -1622,6 +1622,8 @@ static void tcg_out_tlb_read
     TCGType mask_type;
     uint64_t compare_mask;
 
+    tcg_out_mb(s, TCG_MO_ALL);
+
     mask_type = (TARGET_PAGE_BITS + CPU_TLB_DYN_MAX_BITS > 32
                  ? TCG_TYPE_I64 : TCG_TYPE_I32);
 

which is a gigantic hammer, adding a host barrier before every qemu guest access, I can no longer provoke a failure (previously visible 1 in 4, now no failures in 100).

Quoted 3 lines

With that as a data point for success, I'm going to try to use host load-acquire / store-release instructions, and then apply TCG_GUEST_DEFAULT_MO and see if I can find something that works reasonably.

This isn't aarch64-host-specific, though, is it? It's going to be the situation for any host with a relaxed memory model. Do we really want to make all loads and stores lower-performance by adding in the ldacq/strel (or worse, barriers everywhere on host archs without ldacq/strel)? I feel like there ought to be an alternate approach involving using some kind of exclusion to ensure that we don't run the iothreads in parallel with the vCPU thread if we're using the non-MTTCG setup where all the vCPUs are on a single thread, and that that's probably less of a perf hit.

thanks
-- PMM

#17 lore raw
Quoted 2 lines · Peter Maydell

This isn't aarch64-host-specific, though, is it? It's going to be the situation for any host with a relaxed memory model.

Yes. I intend to make the code-generation changes generic.

Quoted 4 lines

Do we really
want to make all loads and stores lower-performance by adding in the ldacq/strel (or worse, barriers everywhere on host archs without ldacq/strel)?

Well, yes. But then we get to enable mttcg too.

Quoted 5 lines

I feel like there ought to be an alternate approach involving using some kind of exclusion to ensure that we don't run the iothreads in parallel with the vCPU thread if we're using the non-MTTCG setup where all the vCPUs are on a single thread, and that that's probably less of a perf hit.

I don't know where to put such a block, do you?

The memory barriers are a perf hit with -smp 1, but I would think that all that and more are recoverable by not having to run -smp 2 serially.

r~