Skip to contentSkip to conversation

pseries: Enable in-kernel H_LOGICAL_CI_{LOAD, STORE} implementations

RFC In review David Gibson 5 participants 15 messages Feb 3, 2015 – Feb 9, 2015

lore
Share & tools
lore
#0 lore raw

RFC 1/1 pseries: Enable in-kernel H_LOGICAL_CI_{LOAD, STORE} implementations

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

---
 hw/ppc/spapr.c       |  5 +++++
 target-ppc/kvm.c     | 27 +++++++++++++++++++++++++++
 target-ppc/kvm_ppc.h |  5 +++++
 3 files changed, 37 insertions(+)

The kernel support this qemu patch enables has been posted but not merged as of this post. See https://lkml.org/lkml/2015/2/3/17

hw/ppc/spapr.c +5 -0
index b560459..40fe1dd 100644
@@ -1459,6 +1459,11 @@ static void ppc_spapr_init(MachineState *machine)
         qemu_register_reset(spapr_cpu_reset, cpu);
     }
 
+    if (kvm_enabled()) {
+        /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
+        kvmppc_enable_logical_ci_hcalls();
+    }
+
     /* allocate RAM */
     spapr->ram_limit = ram_size;
     memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
target-ppc/kvm.c +27 -0
index 1edf2b5..c9d04e8 100644
@@ -1882,6 +1882,33 @@ int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
     return 0;
 }
 
+static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
+{
+    return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
+}
+
+void kvmppc_enable_logical_ci_hcalls(void)
+{
+    int ret1, ret2;
+
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }
+}
+
 void kvmppc_set_papr(PowerPCCPU *cpu)
 {
     CPUState *cs = CPU(cpu);
target-ppc/kvm_ppc.h +5 -0
index 2e0224c..4d30e27 100644
@@ -24,6 +24,7 @@ bool kvmppc_get_host_serial(char **buf);
 int kvmppc_get_hasidle(CPUPPCState *env);
 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len);
 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level);
+void kvmppc_enable_logical_ci_hcalls(void);
 void kvmppc_set_papr(PowerPCCPU *cpu);
 int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t cpu_version);
 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy);
@@ -107,6 +108,10 @@ static inline int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
     return -1;
 }
 
+static inline void kvmppc_enable_logical_ci_hcalls(void)
+{
+}
+
 static inline void kvmppc_set_papr(PowerPCCPU *cpu)
 {
 }
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

Quoted 5 lines

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

#2 lore raw
In reply to David Gibson
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 6 lines
target-ppc/kvm.c +6 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 10 lines
target-ppc/kvm.c +10 -0
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying. Is there any way to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

Paul.

#3 lore raw
Quoted 30 lines · Paul Mackerras
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
target-ppc/kvm.c +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 5 lines
target-ppc/kvm.c +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

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
#4 lore raw
Quoted 42 lines · David Gibson
Quoted 30 lines · Paul Mackerras
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
target-ppc/kvm.c +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 5 lines
target-ppc/kvm.c +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Alex

#5 lore raw
Quoted 49 lines · Alexander Graf
Quoted 46 lines · David Gibson
Quoted 30 lines · Paul Mackerras
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
target-ppc/kvm.c +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 5 lines
target-ppc/kvm.c +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

Quoted 7 lines
Quoted 3 lines

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

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
#6 lore raw
Quoted 54 lines · David Gibson
Quoted 53 lines · Alexander Graf
Quoted 49 lines · David Gibson
Quoted 30 lines · Paul Mackerras
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
target-ppc/kvm.c +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 5 lines
target-ppc/kvm.c +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

Quoted 7 lines
Quoted 3 lines

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Alex

#7 lore raw
Quoted 61 lines · Alexander Graf
Quoted 58 lines · David Gibson
Quoted 56 lines · Alexander Graf
Quoted 42 lines · David Gibson
Quoted 30 lines · Paul Mackerras
Quoted 15 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
target-ppc/kvm.c +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

target-ppc/kvm.c int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) · line 1909

Quoted 5 lines
target-ppc/kvm.c +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

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
#8 lore raw
Quoted 79 lines

Am 05.02.2015 um 03:55 schrieb David Gibson <[email protected]>:

Quoted 62 lines · Alexander Graf
Quoted 61 lines · David Gibson
Quoted 57 lines · Alexander Graf
Quoted 45 lines · David Gibson
Quoted 31 lines · Paul Mackerras
Quoted 16 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Alex

Quoted 6 lines
Signature · 5 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
#9 lore raw
Quoted 85 lines · Alexander Graf
Quoted 79 lines

Am 05.02.2015 um 03:55 schrieb David Gibson <[email protected]>:

Quoted 62 lines · Alexander Graf
Quoted 61 lines · David Gibson
Quoted 57 lines · Alexander Graf
Quoted 45 lines · David Gibson
Quoted 31 lines · Paul Mackerras
Quoted 16 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

Quoted 3 lines

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

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
#10 lore raw
Quoted 86 lines · David Gibson
Quoted 85 lines · Alexander Graf
Quoted 84 lines

Am 05.02.2015 um 03:55 schrieb David Gibson <[email protected]>:

Quoted 62 lines · Alexander Graf
Quoted 61 lines · David Gibson
Quoted 57 lines · Alexander Graf
Quoted 45 lines · David Gibson
Quoted 31 lines · Paul Mackerras
Quoted 16 lines · David Gibson

qemu currently implements the hypercalls H_LOGICAL_CI_LOAD and H_LOGICAL_CI_STORE as PAPR extensions. These are used by the SLOF firmware for IO, because performing cache inhibited MMIO accesses with the MMU off (real mode) is very awkward on POWER.

This approach breaks when SLOF needs to access IO devices implemented within KVM instead of in qemu. The simplest example would be virtio-blk using an iothread, because the iothread / dataplane mechanism relies on an in-kernel implementation of the virtio queue notification MMIO.

To fix this, an in-kernel implementation of these hypercalls has been made, however, the hypercalls still need to be enabled from qemu. This performs the necessary calls to do so.

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

Quoted 7 lines
Quoted 3 lines

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

So at least with vhost-net the in-kernel trapping is optional. If we happen to get MMIO into QEMU, we'll just handle it there.

Enlighten me why the iothread stuff can't handle it that way too.

Alex

#11 lore raw
Quoted 4 lines · Alexander Graf
Quoted 1 line · David Gibson

On Thu, Feb 05, 2015 at 11:22:13AM +0100, Alexander Graf wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 48 lines
Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
Quoted 24 lines
Quoted 13 lines

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Quoted 8 lines
Quoted 3 lines

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

So at least with vhost-net the in-kernel trapping is optional. If we happen to get MMIO into QEMU, we'll just handle it there.

Enlighten me why the iothread stuff can't handle it that way too.

So, as I understand it, it could, but it doesn't. Working out how to fix it properly requires better understanding of the dataplane code than I currently possess,

So, using virtio-blk as the example case. Normally the queue notify mmio will get routed by the general virtio code to virtio_blk_handle_output().

In the case of dataplane, that just calls virtio_blk_data_plane_start(). So the first time we get a vq notify, the dataplane is started. That sets up the host notifier (VirtioBusClass::set_host_notifier -> virtio_pci_set_host_notifier -> virtio_pci_set_host_notifier_internal -> memory_region_add_eventfd() -> memory_region_transaction_commit() -> address_space_update_ioeventfds - >address_space_add_del_ioeventfds -> kvm_mem_ioeventfd_add -> kvm_set_ioeventfd_mmio -> KVM_IOEVENTFD ioctl)

From this point on further calls to virtio_blk_handle_output() are IIUC a "can't happen", because vq notifies should go to the eventfd instead, where they will kick the iothread.

So, with SLOF, the first request is ok - it hits virtio_blk_handle_output() which starts the iothread which goes on to process the request.

On the second request, however, we get back into virtio_blk_data_plane_start() which sees the iothread is already running and aborts. I think it is assuming that this must be the result of a race with another vcpu starting the dataplane, and so assumes the racing thread will have woken the dataplane which will then handle this vcpu's request as well.

In our case, however, the IO hcalls go through to virtio_blk_handle_output() when the dataplane already going, and become no-ops without waking it up again to handle the new request.

Enlightened enough yet?

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
#12 lore raw
Quoted 106 lines · David Gibson
Quoted 4 lines · Alexander Graf
Quoted 1 line · David Gibson

On Thu, Feb 05, 2015 at 11:22:13AM +0100, Alexander Graf wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 48 lines
Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
Quoted 24 lines
Quoted 13 lines

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Quoted 8 lines
Quoted 3 lines

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

So at least with vhost-net the in-kernel trapping is optional. If we happen to get MMIO into QEMU, we'll just handle it there.

Enlighten me why the iothread stuff can't handle it that way too.

So, as I understand it, it could, but it doesn't. Working out how to fix it properly requires better understanding of the dataplane code than I currently possess,

So, using virtio-blk as the example case. Normally the queue notify mmio will get routed by the general virtio code to virtio_blk_handle_output().

In the case of dataplane, that just calls virtio_blk_data_plane_start(). So the first time we get a vq notify, the dataplane is started. That sets up the host notifier (VirtioBusClass::set_host_notifier -> virtio_pci_set_host_notifier -> virtio_pci_set_host_notifier_internal -> memory_region_add_eventfd() -> memory_region_transaction_commit() -> address_space_update_ioeventfds - >address_space_add_del_ioeventfds -> kvm_mem_ioeventfd_add -> kvm_set_ioeventfd_mmio -> KVM_IOEVENTFD ioctl)

From this point on further calls to virtio_blk_handle_output() are IIUC a "can't happen", because vq notifies should go to the eventfd instead, where they will kick the iothread.

So, with SLOF, the first request is ok - it hits virtio_blk_handle_output() which starts the iothread which goes on to process the request.

On the second request, however, we get back into virtio_blk_data_plane_start() which sees the iothread is already running and aborts. I think it is assuming that this must be the result of a race with another vcpu starting the dataplane, and so assumes the racing thread will have woken the dataplane which will then handle this vcpu's request as well.

Quoted 5 lines

In our case, however, the IO hcalls go through to virtio_blk_handle_output() when the dataplane already going, and become no-ops without waking it up again to handle the new request.

Enlightened enough yet?

So reading this, it sounds like we could just add logic in the virtio dataplane code that allows for a graceful fallback to QEMU based MMIO by triggering the eventfd itself in the MMIO handler. When going via this slow path, we should of course emit a warning (once) to the user ;).

Stefan, what do you think?

Alex

#13 lore raw
Quoted 113 lines · On Fri, 06 Feb 2015 08:56:32 +0100 Alexander Graf <[email protected]> wrote:
Quoted 110 lines · David Gibson
Quoted 4 lines · Alexander Graf
Quoted 1 line · David Gibson

On Thu, Feb 05, 2015 at 11:22:13AM +0100, Alexander Graf wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 48 lines
Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
Quoted 24 lines
Quoted 13 lines

[snip]

Quoted 11 lines
(quoted diff) +11 -0
+    ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+    if (ret1 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+                " %s\n", strerror(errno));
+    }
+
+    ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+    if (ret2 != 0) {
+        fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+                " %s\n", strerror(errno));
+     }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    if ((ret1 != 0) || (ret2 != 0)) {
+        fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+                " may be unable to operate devices with in-kernel emulation\n");
+    }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Quoted 8 lines
Quoted 3 lines

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

So at least with vhost-net the in-kernel trapping is optional. If we happen to get MMIO into QEMU, we'll just handle it there.

Enlighten me why the iothread stuff can't handle it that way too.

So, as I understand it, it could, but it doesn't. Working out how to fix it properly requires better understanding of the dataplane code than I currently possess,

So, using virtio-blk as the example case. Normally the queue notify mmio will get routed by the general virtio code to virtio_blk_handle_output().

In the case of dataplane, that just calls virtio_blk_data_plane_start(). So the first time we get a vq notify, the dataplane is started. That sets up the host notifier (VirtioBusClass::set_host_notifier -> virtio_pci_set_host_notifier -> virtio_pci_set_host_notifier_internal -> memory_region_add_eventfd() -> memory_region_transaction_commit() -> address_space_update_ioeventfds - >address_space_add_del_ioeventfds -> kvm_mem_ioeventfd_add -> kvm_set_ioeventfd_mmio -> KVM_IOEVENTFD ioctl)

From this point on further calls to virtio_blk_handle_output() are IIUC a "can't happen", because vq notifies should go to the eventfd instead, where they will kick the iothread.

So, with SLOF, the first request is ok - it hits virtio_blk_handle_output() which starts the iothread which goes on to process the request.

On the second request, however, we get back into virtio_blk_data_plane_start() which sees the iothread is already running and aborts. I think it is assuming that this must be the result of a race with another vcpu starting the dataplane, and so assumes the racing thread will have woken the dataplane which will then handle this vcpu's request as well.

In our case, however, the IO hcalls go through to virtio_blk_handle_output() when the dataplane already going, and become no-ops without waking it up again to handle the new request.

Quoted 8 lines
Quoted 1 line

Enlightened enough yet?

So reading this, it sounds like we could just add logic in the virtio dataplane code that allows for a graceful fallback to QEMU based MMIO by triggering the eventfd itself in the MMIO handler. When going via this slow path, we should of course emit a warning (once) to the user ;).

Stefan, what do you think?

So, as I understand it this should be possible. I did even have a draft which did this. However, I don't know the dataplane well enough to know what gotchas there might be in terms of races, and therefore how to do this quite right.

Note that this doesn't remove the need for the in-kernel H_LOGICAL_CI_* hcalls, because those will still be necessary if we get real in-kernel emulated devices in future.

Signature · 2 lines
David Gibson <[email protected]>
Senior Software Engineer, Virtualization, Red Hat
#14 lore raw
Quoted 123 lines · David Gibson
Quoted 120 lines · On Fri, 06 Feb 2015 08:56:32 +0100 Alexander Graf <[email protected]> wrote:
Quoted 106 lines · David Gibson
Quoted 4 lines · Alexander Graf
Quoted 1 line · David Gibson

On Thu, Feb 05, 2015 at 11:22:13AM +0100, Alexander Graf wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 48 lines
Quoted 36 lines
Quoted 36 lines
Quoted 35 lines
Quoted 24 lines
Quoted 13 lines

[snip]

Quoted 11 lines

+ ret1 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
+ if (ret1 != 0) {
+ fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_LOAD in KVM:"
+ " %s\n", strerror(errno));
+ }
+
+ ret2 = kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
+ if (ret2 != 0) {
+ fprintf(stderr, "Warning: error enabling H_LOGICAL_CI_STORE in KVM:"
+ " %s\n", strerror(errno));
+ }

Quoted 8 lines
Quoted 5 lines

+
+ if ((ret1 != 0) || (ret2 != 0)) {
+ fprintf(stderr, "Warning: Couldn't enable H_LOGICAL_CI_* in KVM, SLOF"
+ " may be unable to operate devices with in-kernel emulation\n");
+ }

You'll always get these warnings if you're running on an old (meaning current upstream) kernel, which could be annoying.

True.

Quoted 7 lines
Quoted 3 lines

Is there any way
to tell whether you have configured any devices which need the in-kernel MMIO emulation and only warn if you have?

In theory, I guess so. In practice I can't see how you'd enumerate all devices that might require kernel intervention without something horribly invasive.

We could WARN_ONCE in QEMU if we emulate such a hypercall, but its handler is io_mem_unassigned (or we add another minimum priority huge memory region on all 64bits of address space that reports the breakage).

Quoted 7 lines
Quoted 3 lines

Would that work for the virtio+iothread case? I had the impression the kernel handled notification region was layered over the qemu emulated region in that case.

IIRC we don't have a way to call back into kvm saying "please write to this in-kernel device". But we could at least defer the warning to a point where we know that we actually hit it.

Right, but I'm saying we might miss the warning in cases where we want it, because the KVM device is shadowed by a qemu device, so qemu won't see the IO as unassigned or unhandled.

Quoted 5 lines

In particular, I think that will happen in the case of virtio-blk with iothread, which is the simplest case in which to observe the problem. The virtio-blk device exists in qemu and is functional, but we rely on KVM catching the queue notification MMIO before it reaches the qemu implementation of the rest of the device's IO space.

But in that case the VM stays functional and will merely see a performance hit when using virtio in SLOF, no? I don't think that's a problem worth worrying users about.

Quoted 8 lines
Quoted 3 lines

Alas, no. The iothread stuff *relies* on the in-kernel notification, so it will not work if the IO gets punted to qemu. This is the whole reason for the in-kernel hcall implementation.

So at least with vhost-net the in-kernel trapping is optional. If we happen to get MMIO into QEMU, we'll just handle it there.

Enlighten me why the iothread stuff can't handle it that way too.

So, as I understand it, it could, but it doesn't. Working out how to fix it properly requires better understanding of the dataplane code than I currently possess,

So, using virtio-blk as the example case. Normally the queue notify mmio will get routed by the general virtio code to virtio_blk_handle_output().

In the case of dataplane, that just calls virtio_blk_data_plane_start(). So the first time we get a vq notify, the dataplane is started. That sets up the host notifier (VirtioBusClass::set_host_notifier -> virtio_pci_set_host_notifier -> virtio_pci_set_host_notifier_internal -> memory_region_add_eventfd() -> memory_region_transaction_commit() -> address_space_update_ioeventfds - >address_space_add_del_ioeventfds -> kvm_mem_ioeventfd_add -> kvm_set_ioeventfd_mmio -> KVM_IOEVENTFD ioctl)

From this point on further calls to virtio_blk_handle_output() are IIUC a "can't happen", because vq notifies should go to the eventfd instead, where they will kick the iothread.

So, with SLOF, the first request is ok - it hits virtio_blk_handle_output() which starts the iothread which goes on to process the request.

On the second request, however, we get back into virtio_blk_data_plane_start() which sees the iothread is already running and aborts. I think it is assuming that this must be the result of a race with another vcpu starting the dataplane, and so assumes the racing thread will have woken the dataplane which will then handle this vcpu's request as well.

Quoted 5 lines

In our case, however, the IO hcalls go through to virtio_blk_handle_output() when the dataplane already going, and become no-ops without waking it up again to handle the new request.

Enlightened enough yet?

So reading this, it sounds like we could just add logic in the virtio dataplane code that allows for a graceful fallback to QEMU based MMIO by triggering the eventfd itself in the MMIO handler. When going via this slow path, we should of course emit a warning (once) to the user ;).

Quoted 6 lines
Quoted 1 line

Stefan, what do you think?

So, as I understand it this should be possible. I did even have a draft which did this. However, I don't know the dataplane well enough to know what gotchas there might be in terms of races, and therefore how to do this quite right.

I'm sure Stefan knows :).

Quoted 3 lines

Note that this doesn't remove the need for the in-kernel H_LOGICAL_CI_* hcalls, because those will still be necessary if we get real in-kernel emulated devices in future.

Oh, I definitely agree on that part. I just want to make sure we gracefully run on older host kernels with newer QEMU versions.

Alex