aboutsummaryrefslogtreecommitdiff
path: root/hw/misc
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2016-03-15 19:34:40 +0100
committerMarkus Armbruster <armbru@redhat.com>2016-03-21 21:29:01 +0100
commit3a55fc0f243104998bee5106b121cff257df5d33 (patch)
tree95f3004c62b3ce1b7f1be122c02dfe005ec6b734 /hw/misc
parent9db51b4d64ded01536b3851a5a50e484ac2f7899 (diff)
ivshmem: Receive shared memory synchronously in realize()
When configured for interrupts (property "chardev" given), we receive the shared memory from an ivshmem server. We do so asynchronously after realize() completes, by setting up callbacks with qemu_chr_add_handlers(). Keeping server I/O out of realize() that way avoids delays due to a slow server. This is probably relevant only for hot plug. However, this funny "no shared memory, yet" state of the device also causes a raft of issues that are hard or impossible to work around: * The guest is exposed to this state: when we enter and leave it its shared memory contents is apruptly replaced, and device register IVPosition changes. This is a known issue. We document that guests should not access the shared memory after device initialization until the IVPosition register becomes non-negative. For cold plug, the funny state is unlikely to be visible in practice, because we normally receive the shared memory long before the guest gets around to mess with the device. For hot plug, the timing is tighter, but the relative slowness of PCI device configuration has a good chance to hide the funny state. In either case, guests complying with the documented procedure are safe. * Migration becomes racy. If migration completes before the shared memory setup completes on the source, shared memory contents is silently lost. Fortunately, migration is rather unlikely to win this race. If the shared memory's ramblock arrives at the destination before shared memory setup completes, migration fails. There is no known way for a management application to wait for shared memory setup to complete. All you can do is retry failed migration. You can improve your chances by leaving more time between running the destination QEMU and the migrate command. To mitigate silent memory loss, you need to ensure the server initializes shared memory exactly the same on source and destination. These issues are entirely undocumented so far. I'd expect the server to be almost always fast enough to hide these issues. But then rare catastrophic races are in a way the worst kind. This is way more trouble than I'm willing to take from any device. Kill the funny state by receiving shared memory synchronously in realize(). If your hot plug hangs, go kill your ivshmem server. For easier review, this commit only makes the receive synchronous, it doesn't add the necessary error propagation. Without that, the funny state persists. The next commit will do that, and kill it off for real. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <1458066895-20632-26-git-send-email-armbru@redhat.com>
Diffstat (limited to 'hw/misc')
-rw-r--r--hw/misc/ivshmem.c68
1 files changed, 48 insertions, 20 deletions
diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
index c6d5dd5f04..ad168288ea 100644
--- a/hw/misc/ivshmem.c
+++ b/hw/misc/ivshmem.c
@@ -675,27 +675,45 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
process_msg(s, msg, fd);
}
-static void ivshmem_check_version(void *opaque, const uint8_t * buf, int size)
+static int64_t ivshmem_recv_msg(IVShmemState *s, int *pfd)
{
- IVShmemState *s = opaque;
- int tmp;
- int64_t version;
+ int64_t msg;
+ int n, ret;
+
+ n = 0;
+ do {
+ ret = qemu_chr_fe_read_all(s->server_chr, (uint8_t *)&msg + n,
+ sizeof(msg) - n);
+ if (ret < 0 && ret != -EINTR) {
+ /* TODO error handling */
+ return INT64_MIN;
+ }
+ n += ret;
+ } while (n < sizeof(msg));
- if (!fifo_update_and_get_i64(s, buf, size, &version)) {
- return;
- }
+ *pfd = qemu_chr_fe_get_msgfd(s->server_chr);
+ return msg;
+}
- tmp = qemu_chr_fe_get_msgfd(s->server_chr);
- if (tmp != -1 || version != IVSHMEM_PROTOCOL_VERSION) {
+static void ivshmem_recv_setup(IVShmemState *s)
+{
+ int64_t msg;
+ int fd;
+
+ msg = ivshmem_recv_msg(s, &fd);
+ if (fd != -1 || msg != IVSHMEM_PROTOCOL_VERSION) {
fprintf(stderr, "incompatible version, you are connecting to a ivshmem-"
"server using a different protocol please check your setup\n");
- qemu_chr_add_handlers(s->server_chr, NULL, NULL, NULL, s);
return;
}
- IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
- qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
- NULL, s);
+ /*
+ * Receive more messages until we got shared memory.
+ */
+ do {
+ msg = ivshmem_recv_msg(s, &fd);
+ process_msg(s, msg, fd);
+ } while (msg != -1);
}
/* Select the MSI-X vectors used by device.
@@ -900,19 +918,29 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
s->server_chr->filename);
- if (ivshmem_setup_interrupts(s) < 0) {
- error_setg(errp, "failed to initialize interrupts");
- return;
- }
-
/* we allocate enough space for 16 peers and grow as needed */
resize_peers(s, 16);
s->vm_id = -1;
pci_register_bar(dev, 2, attr, &s->bar);
- qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
- ivshmem_check_version, NULL, s);
+ /*
+ * Receive setup messages from server synchronously.
+ * Older versions did it asynchronously, but that creates a
+ * number of entertaining race conditions.
+ * TODO Propagate errors! Without that, we still have races
+ * on errors.
+ */
+ ivshmem_recv_setup(s);
+ if (memory_region_is_mapped(&s->ivshmem)) {
+ qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
+ ivshmem_read, NULL, s);
+ }
+
+ if (ivshmem_setup_interrupts(s) < 0) {
+ error_setg(errp, "failed to initialize interrupts");
+ return;
+ }
} else {
/* just map the file immediately, we're not using a server */
int fd;