virtio: Make memory barriers be memory barriers
PATCH 1/1 virtio: Make memory barriers be memory barriers
The virtio code already has memory barrier wmb() macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.
Specifically, the QEMU puts new element into the "used" ring and then updates the ring free-running counter. Without a barrier between these under the right circumstances, the guest linux driver can receive an interrupt, read the counter change but find the ring element to be handled still has an old value, leading to an "id %u is not a head!\n" error message.
The problem is easy to reproduce on POWER using virtio-net with heavy traffic.
The patch defines wmb() as __sync_synchronize(), a cross platform memory barrier primitive available in sufficiently recent gcc versions (gcc 4.2 and after?). If we care about older gccs then this patch will need to be updated with some sort of fallback.
--- hw/virtio.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)
hw/virtio.c +2 -8
@@ -21,14 +21,8 @@* x86 pagesize again. */ #define VIRTIO_PCI_VRING_ALIGN 4096 -/* QEMU doesn't strictly need write barriers since everything runs in -* lock-step. We'll leave the calls to wmb() in though to make it obvious for -* KVM or if kqemu gets SMP support. -* In any case, we must prevent the compiler from reordering the code. -* TODO: we likely need some rmb()/mb() as well. -*/ - -#define wmb () __asm__ __volatile__("": : :"memory") +/* TODO: we may also need rmb()s. It hasn't bitten us yet, but.. */ +#define wmb () __sync_synchronize() typedef struct VRingDesc {
Signature · 1 line
1.7.5.4
Quoted 4 lines · David Gibson
The patch defines
wmb()as__sync_synchronize(), a cross platform memory barrier primitive available in sufficiently recent gcc versions (gcc 4.2 and after?). If we care about older gccs then this patch will need to be updated with some sort of fallback.
Yes, though RHEL5's 4.1 has it backported.
Quoted 2 lines
Signed-off-by Alexey Kardashevskiy<[email protected]>Signed-off-by David Gibson<[email protected]>
It will most definitely break OpenBSD, but anyway:
Paolo
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · David Gibson
The patch defines
wmb()as__sync_synchronize(), a cross platform memory barrier primitive available in sufficiently recent gcc versions (gcc 4.2 and after?). If we care about older gccs then this patch will need to be updated with some sort of fallback.
Quoted 6 lines
Yes, though RHEL5's 4.1 has it backported.
Quoted 2 lines
Signed-off-by Alexey Kardashevskiy<[email protected]>Signed-off-by David Gibson<[email protected]>It will most definitely break OpenBSD, but anyway:
Uh, why?
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 6 lines · David Gibson
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Signed-off-by Alexey Kardashevskiy<[email protected]>Signed-off-by David Gibson<[email protected]>It will most definitely break OpenBSD, but anyway:
Uh, why?
They use an ancient compiler because they do not want to use GPLv3. I thought it was 4.1.something but actually it is 4.2.1, so it should work.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · David Gibson
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
>Signed-off-by: Alexey Kardashevskiy<[email protected]> >Signed-off-by: David Gibson<[email protected]>
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It will most definitely break OpenBSD, but anyway:
Uh, why?
They use an ancient compiler because they do not want to use GPLv3. I thought it was 4.1.something but actually it is 4.2.1, so it should work.
It works:
$ cat >sync_synch.c
void f(void)
{
__sync_synchronize();
}
$ gcc -c sync_synch.c -Wall
$ objdump -d sync_synch.osync_synch.o: file format elf64-sparc
Disassembly of section .text:
0000000000000000 <f>:
0: 9d e3 bf 40 save %sp, -192, %sp 4: 81 43 e0 0f membar #StoreStore|#LoadStore|#StoreLoad|#LoadLoad 8: 81 cf e0 08 rett %i7 + 8 c: 01 00 00 00 nop $ gcc -v Reading specs from /usr/bin/../lib/gcc-lib/sparc64-unknown-openbsd4.9/4.2.1/specs Target: sparc64-unknown-openbsd4.9 Configured with: OpenBSD/sparc64 system compiler Thread model: posix gcc version 4.2.1 20070719
Quoted 38 lines · David Gibson
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.Specifically, the QEMU puts new element into the "used" ring and then updates the ring free-running counter. Without a barrier between these under the right circumstances, the guest linux driver can receive an interrupt, read the counter change but find the ring element to be handled still has an old value, leading to an "id %u is not a head!\n" error message.
The problem is easy to reproduce on POWER using virtio-net with heavy traffic.
The patch defines
wmb()as__sync_synchronize(), a cross platform memory barrier primitive available in sufficiently recent gcc versions (gcc 4.2 and after?). If we care about older gccs then this patch will need to be updated with some sort of fallback.Signed-off-by Alexey Kardashevskiy <[email protected]>Signed-off-by David Gibson <[email protected]>--- hw/virtio.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)hw/virtio.c +0
-0@@ -21,14 +21,8 @@* x86 pagesize again. */ #define VIRTIO_PCI_VRING_ALIGN 4096
hw/virtio.c line 25
Quoted 10 lines
hw/virtio.c +2
-8-/* QEMU doesn't strictly need write barriers since everything runs in -* lock-step. We'll leave the calls to wmb() in though to make it obvious for -* KVM or if kqemu gets SMP support. -* In any case, we must prevent the compiler from reordering the code. -* TODO: we likely need some rmb()/mb() as well. -*/ - -#define wmb () __asm__ __volatile__("": : :"memory") +/* TODO: we may also need rmb()s. It hasn't bitten us yet, but.. */ +#define wmb () __sync_synchronize()
That asm directive also implicitly provided a compiler barrier, I could find whether __sync_synchronize() provides one as well.
Any idea if it does?
Quoted 3 lines
hw/virtio.c +0
-0typedef struct VRingDesc {
Signature · 1 line
Sasha.
Quoted 39 lines · Sasha Levin
Quoted 38 lines · David Gibson
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.Specifically, the QEMU puts new element into the "used" ring and then updates the ring free-running counter. Without a barrier between these under the right circumstances, the guest linux driver can receive an interrupt, read the counter change but find the ring element to be handled still has an old value, leading to an "id %u is not a head!\n" error message.
The problem is easy to reproduce on POWER using virtio-net with heavy traffic.
The patch defines
wmb()as__sync_synchronize(), a cross platform memory barrier primitive available in sufficiently recent gcc versions (gcc 4.2 and after?). If we care about older gccs then this patch will need to be updated with some sort of fallback.Signed-off-by Alexey Kardashevskiy <[email protected]>Signed-off-by David Gibson <[email protected]>--- hw/virtio.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)hw/virtio.c +0
-0@@ -21,14 +21,8 @@* x86 pagesize again. */ #define VIRTIO_PCI_VRING_ALIGN 4096
Quoted 12 lines
hw/virtio.c line 25
Quoted 10 lines
hw/virtio.c +2
-8-/* QEMU doesn't strictly need write barriers since everything runs in -* lock-step. We'll leave the calls to wmb() in though to make it obvious for -* KVM or if kqemu gets SMP support. -* In any case, we must prevent the compiler from reordering the code. -* TODO: we likely need some rmb()/mb() as well. -*/ - -#define wmb () __asm__ __volatile__("": : :"memory") +/* TODO: we may also need rmb()s. It hasn't bitten us yet, but.. */ +#define wmb () __sync_synchronize()That asm directive also implicitly provided a compiler barrier, I could
couldn't.
Quoted 8 lines
find whether
__sync_synchronize()provides one as well.Any idea if it does?
Quoted 3 lines
hw/virtio.c +0
-0typedef struct VRingDesc {
Signature · 1 line
Sasha.
Quoted 5 lines · Sasha Levin
Quoted 3 lines
Quoted 3 lines
-#define
wmb()__asm____volatile__("": : :"memory")
+ /* TODO: we may also need rmb()s. It hasn't bitten us yet, but.. */
+ #definewmb()__sync_synchronize()That asm directive also implicitly provided a compiler barrier, I could
^^^^
only
Quoted 3 lines
find whether
__sync_synchronize()provides one as well.Any idea if it does?
Of course, __sync_synchronize() is a stronger barrier. The asm is simply not a synchronization primitive.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Sasha Levin
Quoted 3 lines
Quoted 3 lines
-#define
wmb()__asm____volatile__("": : :"memory")
+ /* TODO: we may also need rmb()s. It hasn't bitten us yet, but.. */
+ #definewmb()__sync_synchronize()That asm directive also implicitly provided a compiler barrier, I could
^^^^
Quoted 8 lines
only
Quoted 3 lines
find whether
__sync_synchronize()provides one as well.Any idea if it does?
Of course,
__sync_synchronize()is a stronger barrier. The asm is simply not a synchronization primitive.
Good to hear that confirmed, I hadn't checked myself. But then a cpu memory barrier primitive without a compiler barrier would be an insane thing to have.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 8 lines · David Gibson
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.
Why not limit the change to ppc then?
Signature · 1 line
MST
Quoted 10 lines · Michael S. Tsirkin
Quoted 8 lines
Quoted 8 lines
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Michael S. Tsirkin
Quoted 8 lines
Quoted 8 lines
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.Why not limit the change to ppc then?
Quoted 7 lines
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence. But there might be reasons why that is not an issue either if we look closely enough.
Signature · 1 line
MST
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.
No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become __sync_synchronize() too, or you should use inline assembly.
Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Paolo
Quoted 21 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Quoted 6 lines
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest uses smp_ variants for barriers.
Signature · 1 line
MST
Quoted 3 lines · Michael S. Tsirkin
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.
/* Make sure buffer is written before we update index. */
wmb();Without it, a guest could see a partially updated buffer, because the buffer and index writes are unlocked stores to different locations.
Even if the guest uses barriers, with ioeventfd it will only order the CPU that is running the guest, not the one that is running the iothread.
In fact I'm surprised that it works at all under x86 with ioeventfd.
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 3 lines · Michael S. Tsirkin
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers./* Make sure buffer is written before we update index. */ wmb();Without it, a guest could see a partially updated buffer, because the buffer and index writes are unlocked stores to different locations.
Sorry, I still don't understand. Yes, they are unlocked stores to different locations. How does it follow that a guest could see a partially updated buffer? Just to make sure - we are talking about x86 here, not ppc, right?
Quoted 6 lines
Even if the guest uses barriers, with ioeventfd it will only order the CPU that is running the guest, not the one that is running the iothread. In fact I'm surprised that it works at all under x86 with ioeventfd.
Paolo
I can try to explain if I understand the problem you see.
Signature · 1 line
MST
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Quoted 5 lines
Quoted 1 line
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.
Ok, so, I'm having a bit of trouble with the fact that I'm having to argue the case that things the protocol requiress to be memory barriers actually *be* memory barriers on all platforms.
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of sync_synchronize, if you will. But just leaving them out on x86!? Seriously, wtf? Do you enjoy having software that works chiefly by accident?
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 34 lines · David Gibson
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Quoted 5 lines
Quoted 1 line
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.
Quoted 7 lines
Ok, so, I'm having a bit of trouble with the fact that I'm having to argue the case that things the protocol requiress to be memory barriers actually *be* memory barriers on all platforms.
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.
That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm__ __volatile__("": : :"memory")
ppc needs something like sync_synchronize there.
Quoted 3 lines
But just leaving them out on x86!?
Seriously, wtf? Do you enjoy having software that works chiefly by accident?
I'm surprised at the controversy too. People seem to argue that x86 cpu does not reorder stores and that we need an sfence between stores to prevent the guest from seeing them out of order, at the same time.
Quoted 5 lines
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 43 lines · Michael S. Tsirkin
Quoted 34 lines · David Gibson
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Quoted 5 lines
Quoted 1 line
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.Quoted 7 lines
Ok, so, I'm having a bit of trouble with the fact that I'm having to argue the case that things the protocol requiress to be memory barriers actually *be* memory barriers on all platforms.
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.
Quoted 5 lines
That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm____volatile__("": : :"memory")
ppc needs something likesync_synchronizethere.
But you're approaching this the wrong way around - correctness should come first. That is, we should first ensure that there is a sufficient memory barrier to satisfy the protocol. Then, *if* there is a measurable performance improvement and *if* we can show that a weaker barrier is sufficient on a given platform, then we can whittle it down to a lighter barrier.
Quoted 8 lines
Quoted 3 lines
But just leaving them out on x86!?
Seriously, wtf? Do you enjoy having software that works chiefly by accident?I'm surprised at the controversy too. People seem to argue that x86 cpu does not reorder stores and that we need an sfence between stores to prevent the guest from seeing them out of order, at the same time.
I don't know the x86 storage model well enough to definitively say that the barrier is not necessary there - nor to say that it is necessary. All I know is that the x86 model is quite strongly ordered, and I assume that is why the lack of barrier has not caused an observed problem on x86.
Again, correctness first. sync_synchronize should be a sufficient barrier for wmb() on all platforms. If you really don't want it, the onus is on you to show that (a) it's safe to do so and (b) it's actually worth it.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 50 lines · David Gibson
Quoted 43 lines · Michael S. Tsirkin
Quoted 34 lines · David Gibson
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Quoted 5 lines
Quoted 1 line
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.Quoted 7 lines
Ok, so, I'm having a bit of trouble with the fact that I'm having to argue the case that things the protocol requiress to be memory barriers actually *be* memory barriers on all platforms.
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.Quoted 5 lines
That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm____volatile__("": : :"memory")
ppc needs something likesync_synchronizethere.
Quoted 6 lines
But you're approaching this the wrong way around - correctness should come first. That is, we should first ensure that there is a sufficient memory barrier to satisfy the protocol. Then, *if* there is a measurable performance improvement and *if* we can show that a weaker barrier is sufficient on a given platform, then we can whittle it down to a lighter barrier.
You are only looking at ppc. But on x86 this code ships in production. So changes should be made in a way to reduce
a potential for regressions, balancing risk versus potential benefit. I'm trying to point out a way to do this.
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
But just leaving them out on x86!?
Seriously, wtf? Do you enjoy having software that works chiefly by accident?I'm surprised at the controversy too. People seem to argue that x86 cpu does not reorder stores and that we need an sfence between stores to prevent the guest from seeing them out of order, at the same time.
Quoted 5 lines
I don't know the x86 storage model well enough to definitively say that the barrier is not necessary there - nor to say that it is necessary. All I know is that the x86 model is quite strongly ordered, and I assume that is why the lack of barrier has not caused an observed problem on x86.
Please review Documentation/memory-barriers.txt as one reference. then look at how SMP barriers are implemented at various systems. In particular, note how it says 'Mandatory barriers should not be used to control SMP effects'.
Quoted 3 lines
Again, correctness first.
sync_synchronizeshould be a sufficient barrier forwmb()on all platforms. If you really don't want it, the onus is on you
Just for fun, I did a quick hack replacing all barriers with mb() in the userspace virtio test. This is on x386.
Before:
[mst@tuck virtio]$ sudo time ./virtio_test
spurious wakeus: 0x1da
24.53user 14.63system 0:41.91elapsed 93%CPU (0avgtext+0avgdata 464maxresident)k
0inputs+0outputs (0major+154minor)pagefaults 0swaps
After:
[mst@tuck virtio]$ sudo time ./virtio_test
spurious wakeus: 0x218
33.97user 6.22system 0:42.10elapsed 95%CPU (0avgtext+0avgdata 464maxresident)k
0inputs+0outputs (0major+154minor)pagefaults 0swaps
So user time went up significantly, as expected. Surprisingly the kernel side started working more efficiently - surprising since kernel was not changed - with net effect close to evening out.
So a risk of performance regressions from unnecessary fencing seems to be non-zero, assuming that time doesn't lie. This might be worth investigating, but I'm out of time right now.
Quoted 2 lines
to show that (a) it's safe to do so and
(b) it's actually worth it.
Worth what? I'm asking you to minimuse disruption to other platforms while you fix ppc.
Signature · 1 line
MST
Quoted 58 lines · Michael S. Tsirkin
Quoted 50 lines · David Gibson
Quoted 43 lines · Michael S. Tsirkin
Quoted 34 lines · David Gibson
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
Quoted 7 lines
Quoted 3 lines
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Paolo
Right.
To summarize, on x86 we probably want wmb and rmb to be compiler barrier only. Only mb might in theory need to be an mfence.No, wmb needs to be sfence and rmb needs to be lfence. GCC does not provide those, so they should become
__sync_synchronize()too, or you should use inline assembly.Quoted 2 lines
But there might be reasons why that is not an issue either if we look closely enough.
Since the ring buffers are not using locked instructions (no xchg or cmpxchg) the barriers simply must be there, even on x86. Whether it works in practice is not interesting, only the formal model is interesting.
Quoted 5 lines
Quoted 1 line
Paolo
Well, can you describe an issue in virtio that lfence/sfence help solve in terms of a memory model please?
Pls note that guest usessmp_variants for barriers.Quoted 7 lines
Ok, so, I'm having a bit of trouble with the fact that I'm having to argue the case that things the protocol requiress to be memory barriers actually *be* memory barriers on all platforms.
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.Quoted 5 lines
That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm____volatile__("": : :"memory")
ppc needs something likesync_synchronizethere.Quoted 6 lines
But you're approaching this the wrong way around - correctness should come first. That is, we should first ensure that there is a sufficient memory barrier to satisfy the protocol. Then, *if* there is a measurable performance improvement and *if* we can show that a weaker barrier is sufficient on a given platform, then we can whittle it down to a lighter barrier.
Quoted 4 lines
You are only looking at ppc. But on x86 this code ships in production. So changes should be made in a way to reduce
a potential for regressions, balancing risk versus potential benefit. I'm trying to point out a way to do this.
Oh, please. Adding a stronger barrier has a miniscule chance of breaking things. And this in a project that has build-breaking regressions with tedious frequency.
Quoted 15 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
But just leaving them out on x86!?
Seriously, wtf? Do you enjoy having software that works chiefly by accident?I'm surprised at the controversy too. People seem to argue that x86 cpu does not reorder stores and that we need an sfence between stores to prevent the guest from seeing them out of order, at the same time.
Quoted 5 lines
I don't know the x86 storage model well enough to definitively say that the barrier is not necessary there - nor to say that it is necessary. All I know is that the x86 model is quite strongly ordered, and I assume that is why the lack of barrier has not caused an observed problem on x86.
Quoted 4 lines
Please review
Documentation/memory-barriers.txtas one reference. then look at how SMP barriers are implemented at various systems. In particular, note how it says 'Mandatory barriers should not be used to control SMP effects'.
No, again, correctness first; the onus of showing it's safe is on those who want weaker barriers.
Quoted 21 lines
Quoted 3 lines
Again, correctness first.
sync_synchronizeshould be a sufficient barrier forwmb()on all platforms. If you really don't want it, the onus is on youJust for fun, I did a quick hack replacing all barriers with
mb()in the userspace virtio test. This is on x386.Before:
[mst@tuck virtio]$ sudo time ./virtio_test
spurious wakeus:0x1da
24.53user 14.63system 0:41.91elapsed 93%CPU (0avgtext+0avgdata 464maxresident)k
0inputs+0outputs (0major+154minor)pagefaults 0swapsAfter:
[mst@tuck virtio]$ sudo time ./virtio_test
spurious wakeus:0x218
33.97user 6.22system 0:42.10elapsed 95%CPU (0avgtext+0avgdata 464maxresident)k
0inputs+0outputs (0major+154minor)pagefaults 0swaps
Quoted 3 lines
So user time went up significantly, as expected. Surprisingly the kernel side started working more efficiently - surprising since kernel was not changed - with net effect close to evening out.
Right. So small overall performance impact, and that's on a dedicated testcase which does nothing but the virtio protocol. I *strongly* suspect the extra cost of the memory barriers will be well and truly lost in the rest of the overhead of the qemu networking code.
Quoted 10 lines
So a risk of performance regressions from unnecessary fencing seems to be non-zero, assuming that time doesn't lie. This might be worth investigating, but I'm out of time right now.
Quoted 2 lines
to show that (a) it's safe to do so and
(b) it's actually worth it.Worth what? I'm asking you to minimuse disruption to other platforms while you fix ppc.
I'm not "fixing ppc". I'm fixing a fundamental flaw in the protocol implementation. _So far_ I've only observed the effects on ppc, but that doesn't mean they don't exist.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 3 lines · David Gibson
I'm not "fixing ppc". I'm fixing a fundamental flaw in the protocol implementation._So
far_I've only observed the effects on ppc, but that doesn't mean they don't exist.
Actually Michael is right. The implementation is correct on x86, though wrong anywhere else (perhaps s390?). On those architectures you do not need rmb() and wmb().
So doing the __sync_synchronize() on ppc only as he proposed is wrong; but not doing it (and leaving only a compiler barrier) on x86 is correct. See http://g.oswego.edu/dl/jmm/cookbook.html under "Multiprocessors".
Paolo
Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · David Gibson
I'm not "fixing ppc". I'm fixing a fundamental flaw in the protocol implementation._So
far_I've only observed the effects on ppc, but that doesn't mean they don't exist.
Quoted 8 lines
Actually Michael is right. The implementation is correct on x86, though wrong anywhere else (perhaps s390?). On those architectures you do not need
rmb()andwmb().So doing the
__sync_synchronize()on ppc only as he proposed is wrong; but not doing it (and leaving only a compiler barrier) on x86 is correct. See http://g.oswego.edu/dl/jmm/cookbook.html under "Multiprocessors".
Well, in any case, I've realised that we ought to merge the barriers in virtio with those in qemu-barrier.h. Which are also unsafe on non-x86.
I have a draft two patch series to do that, which I intend to post soon. Unfortunately, I haven't been able to test it on ppc kvm yet, because there are other bugs which are making qemu not even boot the pseries machine on a ppc64 host. Still trying to work out what the hell's going on there, smells like bad extension or masking of CTR (SLOF seems to get stuck in a bdnz loop with something close to 2^57 in CTR).
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 6 lines · Paolo Bonzini
Quoted 3 lines · David Gibson
I'm not "fixing ppc". I'm fixing a fundamental flaw in the protocol implementation._So
far_I've only observed the effects on ppc, but that doesn't mean they don't exist.Actually Michael is right. The implementation is correct on x86, though wrong anywhere else (perhaps s390?). On those architectures you do not need
rmb()andwmb().
Are we sure? Nothing prevents the guest from using weakly-ordered writes, is there? For example, using MOVNTDQ.
Although in that case the guest is probably required to issue an SFENCE.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · David Gibson
I'm not "fixing ppc". I'm fixing a fundamental flaw in the protocol implementation._So
far_I've only observed the effects on ppc, but that doesn't mean they don't exist.
Quoted 6 lines
Quoted 3 lines
Actually Michael is right. The implementation is correct on x86, though wrong anywhere else (perhaps s390?). On those architectures you do not need
rmb()andwmb().Are we sure? Nothing prevents the guest from using weakly-ordered writes, is there? For example, using MOVNTDQ.
Yes but the macros in question are here to order qemu accesses, not guest accesses.
Quoted 4 lines
Although in that case the guest is probably required to issue an SFENCE.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 9 lines · Avi Kivity
Quoted 4 lines
Actually Michael is right. The implementation is correct on x86, though wrong anywhere else (perhaps s390?). On those architectures you do not need
rmb()andwmb().Are we sure? Nothing prevents the guest from using weakly-ordered writes, is there? For example, using MOVNTDQ.
Although in that case the guest is probably required to issue an SFENCE.
Yes, that's the guest problem. You cannot do an SFENCE on the guest's behalf anyway.
Paolo
Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines
Quoted 3 lines
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm____volatile__("": : :"memory")
ppc needs something likesync_synchronizethere.
No, rmb and wmb need to generate code. You are right that in some places there will be some extra barriers.
If you want a richer set of barriers, that must be something like {rr,rw,wr,ww}_mb{_acq,_rel,} (again not counting the Alpha). On x86, then, all the rr/rw/ww barriers will be compiler barriers because the hardware already enforces ordering. The other three map to lfence/sfence/mfence:
barrier assembly why?
---------------------------------------------------------------------
wr_mb_acq lfence prevents the read from moving up -> acquire wr_mb_rel sfence prevents the write from moving down -> release wr_mb mfence (full barrier)
But if you stick to rmb/wmb/mb, then the correct definition of rmb is "the least strict barrier that provides all three of rr_mb(), rw_mb_rel() and wr_mb_acq()". This is, as expected, an lfence. Similarly, wmb must provide all three of ww_mb(), wr_mb_rel() and rw_mb_acq(), and this is an sfence.
So the right place to put an #ifdef is not "wmb()", but the _uses_ of wmb() where you know you need a barrier that is less strict. That's why I say David patch is correct; on top of that you may change the particular uses of wmb() in virtio.c to compiler barriers, for example when you only care about ordering writes after writes.
Likewise, there may even be places in which you could #ifdef out a full memory barrier. For example, if you only care about ordering writes with respect to reads, x86 hardware is already providing that and you could omit the mb().
I think in general it is premature optimization, though.
Regarding specific examples in virtio where lfence and sfence could be used, there may be one when using event signaling. In the backend you write first the index of your response, then you check whether to generate an event. (I think) the following requirements hold:
* if you read the event-index too early, you might skip an event and deadlock. So you need at least a read barrier.
* you can write the response-index after reading the event-index, as long as you write it before waking up the guest.
So, in that case an x86 lfence should be enough, though again without more consideration I would use a full barrier just to be sure.
Paolo
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines
Quoted 3 lines
I mean argue for a richer set of barriers, with per-arch minimal implementations instead of the large but portable hammer of
sync_synchronize, if you will.
Quoted 7 lines
Quoted 5 lines
That's what I'm saying really. On x86 the richer set of barriers need not insert code at all for both wmb and rmb macros. All we might need is an 'optimization barrier'- e.g. linux does
__asm____volatile__("": : :"memory")
ppc needs something likesync_synchronizethere.No, rmb and wmb need to generate code.
If they do we'll have to surround each their use with ifndef x86 as you suggest later. Which is just messy. Maybe we should rename the barriers to smp_rmb/smp_wmb/smp_mb - this is what linux guests do.
Quoted 44 lines
You are right that in some
places there will be some extra barriers.If you want a richer set of barriers, that must be something like {rr,rw,wr,ww}_mb{_acq,_rel,} (again not counting the Alpha). On x86, then, all the rr/rw/ww barriers will be compiler barriers because the hardware already enforces ordering. The other three map to lfence/sfence/mfence:
barrier assembly why?
---------------------------------------------------------------------wr_mb_acq lfence prevents the read from moving up -> acquire wr_mb_rel sfence prevents the write from moving down -> release wr_mb mfence (full barrier)But if you stick to rmb/wmb/mb, then the correct definition of rmb is "the least strict barrier that provides all three of
rr_mb(),rw_mb_rel()andwr_mb_acq()". This is, as expected, an lfence. Similarly, wmb must provide all three ofww_mb(),wr_mb_rel()andrw_mb_acq(), and this is an sfence.So the right place to put an #ifdef is not "
wmb()", but the _uses_ ofwmb()where you know you need a barrier that is less strict. That's why I say David patch is correct; on top of that you may change the particular uses ofwmb()invirtio.cto compiler barriers, for example when you only care about ordering writes after writes.
Likewise, there may even be places in which you could #ifdef out a full memory barrier. For example, if you only care about ordering writes with respect to reads, x86 hardware is already providing that and you could omit themb().
I think in general it is premature optimization, though.Regarding specific examples in virtio where lfence and sfence could be used, there may be one when using event signaling. In the backend you write first the index of your response, then you check whether to generate an event. (I think) the following requirements hold:
* if you read the event-index too early, you might skip an event and deadlock. So you need at least a read barrier.
* you can write the response-index after reading the event-index, as long as you write it before waking up the guest.
Quoted 5 lines
So, in that case an x86 lfence should be enough, though again without more consideration I would use a full barrier just to be sure.
Paolo
lfence in this place will not affect the relative order of read versus write.
Signature · 1 line
MST
Quoted 4 lines
Quoted 1 line
No, rmb and wmb need to generate code.
If they do we'll have to surround each their use with ifndef x86 as you suggest later. Which is just messy.
[1 hour later]
I see what you mean now. You assume there are no accesses to write-combining memory (of course) or non-temporal load/stores (because they are accessed only with assembly), so you can make rmb/wmb no-ops on x86. I was confused by the kernel (and liburcu's) choice to use lfence/sfence for rmb/wmb.
Then it's indeed better to move the wmb() defines to qemu-barrier.h, where they can be made processor-dependent. S390, it seems, also does not need rmb/wmb.
Paolo
Quoted 10 lines · Paolo Bonzini
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines
Quoted 8 lines
The virtio code already has memory barrier
wmb()macros in the code. However they are was defined as no-ops. The comment claims that real barriers are not necessary because the code does not run concurrent. However, with kvm and io-thread enabled, this is not true and this qemu code can indeed run concurrently with the guest kernel. This does not cause problems on x86 due to it's strongly ordered storage model, but it causes a race leading to virtio errors on POWER which has a relaxed storage ordering model.
Quoted 7 lines
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Not to mention that pcc is not the only non-x86 architecture. I don't know all their storage models off hand, but the point is that there is a required order to these writes, so there should be a memory barrier.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 10 lines · David Gibson
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Not to mention that pcc is not the only non-x86 architecture. I don't know all their storage models off hand, but the point is that there is a required order to these writes, so there should be a memory barrier.
Indeed, I interpreted Michael's question more as "why not limit the change to non-x86". I think we should cater to all memory models except perhaps the Alpha's.
Paolo
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · David Gibson
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Why not limit the change to ppc then?
Because the bug is masked by the x86 memory model, but it is still there even there conceptually. It is not really true that x86 does not need memory barriers, though it doesn't in this case:
http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/
Not to mention that pcc is not the only non-x86 architecture. I don't know all their storage models off hand, but the point is that there is a required order to these writes, so there should be a memory barrier.
Quoted 5 lines
Indeed, I interpreted Michael's question more as "why not limit the change to non-x86". I think we should cater to all memory models except perhaps the Alpha's.
Paolo
Yes, x86 is pretty popular.