From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: claudio.fontana@huawei.com, cam@cs.ualberta.ca,
mlureau@redhat.com, david.marchand@6wind.com,
pbonzini@redhat.com
Subject: [Qemu-devel] [PATCH 23/38] ivshmem: Receive shared memory synchronously in realize()
Date: Mon, 29 Feb 2016 19:40:39 +0100 [thread overview]
Message-ID: <1456771254-17511-24-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1456771254-17511-1-git-send-email-armbru@redhat.com>
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>
---
hw/misc/ivshmem.c | 70 +++++++++++++++++++++++++++++++++++++---------------
tests/ivshmem-test.c | 26 ++++++-------------
2 files changed, 57 insertions(+), 39 deletions(-)
diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
index c366087..352937f 100644
--- a/hw/misc/ivshmem.c
+++ b/hw/misc/ivshmem.c
@@ -676,27 +676,47 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
process_msg(s, incoming_posn, incoming_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;
- if (!fifo_update_and_get_i64(s, buf, size, &version)) {
- return;
- }
+ 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));
- tmp = qemu_chr_fe_get_msgfd(s->server_chr);
- if (tmp != -1 || version != IVSHMEM_PROTOCOL_VERSION) {
+ *pfd = qemu_chr_fe_get_msgfd(s->server_chr);
+ return msg;
+}
+
+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);
+
+ assert(memory_region_is_mapped(&s->ivshmem));
}
/* Select the MSI-X vectors used by device.
@@ -903,19 +923,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;
diff --git a/tests/ivshmem-test.c b/tests/ivshmem-test.c
index c1dd7bb..68d6840 100644
--- a/tests/ivshmem-test.c
+++ b/tests/ivshmem-test.c
@@ -309,35 +309,23 @@ static void test_ivshmem_server(bool msi)
ret = ivshmem_server_start(&server);
g_assert_cmpint(ret, ==, 0);
- setup_vm_with_server(&state1, nvectors, msi);
- s1 = &state1;
- setup_vm_with_server(&state2, nvectors, msi);
- s2 = &state2;
-
- /* check state before server sends stuff */
- g_assert_cmpuint(in_reg(s1, IVPOSITION), ==, 0xffffffff);
- g_assert_cmpuint(in_reg(s2, IVPOSITION), ==, 0xffffffff);
- g_assert_cmpuint(qtest_readb(s1->qtest, (uintptr_t)s1->mem_base), ==, 0x00);
-
thread.server = &server;
ret = pipe(thread.pipe);
g_assert_cmpint(ret, ==, 0);
thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
g_assert(thread.thread != NULL);
- /* waiting for devices to become operational */
- while (g_get_monotonic_time() < end_time) {
- g_usleep(1000);
- if ((int)in_reg(s1, IVPOSITION) >= 0 &&
- (int)in_reg(s2, IVPOSITION) >= 0) {
- break;
- }
- }
+ setup_vm_with_server(&state1, nvectors, msi);
+ s1 = &state1;
+ setup_vm_with_server(&state2, nvectors, msi);
+ s2 = &state2;
/* check got different VM ids */
vm1 = in_reg(s1, IVPOSITION);
vm2 = in_reg(s2, IVPOSITION);
- g_assert_cmpuint(vm1, !=, vm2);
+ g_assert_cmpint(vm1, >=, 0);
+ g_assert_cmpint(vm2, >=, 0);
+ g_assert_cmpint(vm1, !=, vm2);
/* check number of MSI-X vectors */
global_qtest = s1->qtest;
--
2.4.3
next prev parent reply other threads:[~2016-02-29 18:41 UTC|newest]
Thread overview: 118+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-29 18:40 [Qemu-devel] [PATCH 00/38] ivshmem: Fixes, cleanups, device model split Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 01/38] exec: Fix memory allocation when memory path names new file Markus Armbruster
2016-03-01 11:35 ` Paolo Bonzini
2016-03-01 11:58 ` Markus Armbruster
2016-03-04 18:50 ` Markus Armbruster
2016-03-07 13:12 ` Paolo Bonzini
2016-02-29 18:40 ` [Qemu-devel] [PATCH 02/38] qemu-doc: Fix ivshmem huge page example Markus Armbruster
2016-03-01 10:51 ` Marc-André Lureau
2016-03-01 11:35 ` Paolo Bonzini
2016-02-29 18:40 ` [Qemu-devel] [PATCH 03/38] event_notifier: Make event_notifier_init_fd() #ifdef CONFIG_EVENTFD Markus Armbruster
2016-03-01 10:57 ` Marc-André Lureau
2016-03-01 12:00 ` Markus Armbruster
2016-03-01 12:05 ` Paolo Bonzini
2016-03-01 11:35 ` Paolo Bonzini
2016-02-29 18:40 ` [Qemu-devel] [PATCH 04/38] tests/libqos/pci-pc: Fix qpci_pc_iomap() to map BARs aligned Markus Armbruster
2016-03-01 11:05 ` Marc-André Lureau
2016-03-01 12:05 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 05/38] ivshmem-test: Improve test case /ivshmem/single Markus Armbruster
2016-03-01 11:06 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 06/38] ivshmem-test: Clean up wait for devices to become operational Markus Armbruster
2016-03-01 11:10 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 07/38] ivshmem-test: Improve test cases /ivshmem/server-* Markus Armbruster
2016-03-01 11:13 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 08/38] ivshmem: Rewrite specification document Markus Armbruster
2016-03-01 11:25 ` Marc-André Lureau
2016-03-01 15:46 ` Eric Blake
2016-03-02 9:50 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 09/38] ivshmem: Add missing newlines to debug printfs Markus Armbruster
2016-03-01 12:20 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 10/38] ivshmem: Compile debug prints unconditionally to prevent bit-rot Markus Armbruster
2016-03-01 12:22 ` Marc-André Lureau
2016-03-01 15:49 ` Eric Blake
2016-03-02 9:51 ` Markus Armbruster
2016-03-02 15:52 ` Eric Blake
2016-02-29 18:40 ` [Qemu-devel] [PATCH 11/38] ivshmem: Clean up after commit 9940c32 Markus Armbruster
2016-03-01 12:47 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 12/38] ivshmem: Drop ivshmem_event() stub Markus Armbruster
2016-03-01 12:48 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 13/38] ivshmem: Don't destroy the chardev on version mismatch Markus Armbruster
2016-03-01 15:39 ` Marc-André Lureau
2016-03-02 9:52 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 14/38] ivshmem: Fix harmless misuse of Error Markus Armbruster
2016-03-01 15:47 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 15/38] ivshmem: Failed realize() can leave migration blocker behind Markus Armbruster
2016-03-01 15:59 ` Marc-André Lureau
2016-03-02 9:54 ` Markus Armbruster
2016-03-02 10:50 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 16/38] ivshmem: Clean up register callbacks Markus Armbruster
2016-03-01 16:04 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 17/38] ivshmem: Clean up MSI-X conditions Markus Armbruster
2016-03-01 16:57 ` Marc-André Lureau
2016-03-02 10:25 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 18/38] ivshmem: Leave INTx alone when using MSI-X Markus Armbruster
2016-03-01 17:14 ` Marc-André Lureau
2016-03-01 17:30 ` Paolo Bonzini
2016-03-02 11:04 ` Markus Armbruster
2016-03-02 14:15 ` Paolo Bonzini
2016-03-02 15:50 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 19/38] ivshmem: Assert interrupts are set up once Markus Armbruster
2016-03-02 12:02 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 20/38] ivshmem: Simplify rejection of invalid peer ID from server Markus Armbruster
2016-03-02 15:08 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 21/38] ivshmem: Disentangle ivshmem_read() Markus Armbruster
2016-03-02 15:28 ` Marc-André Lureau
2016-03-02 15:53 ` Markus Armbruster
2016-03-02 17:33 ` Marc-André Lureau
2016-03-02 19:15 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 22/38] ivshmem: Plug leaks on unplug, fix peer disconnect Markus Armbruster
2016-03-02 17:47 ` Marc-André Lureau
2016-03-02 19:19 ` Markus Armbruster
2016-03-02 23:52 ` Marc-André Lureau
2016-02-29 18:40 ` Markus Armbruster [this message]
2016-03-02 18:11 ` [Qemu-devel] [PATCH 23/38] ivshmem: Receive shared memory synchronously in realize() Marc-André Lureau
2016-03-02 19:28 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 24/38] ivshmem: Propagate errors through ivshmem_recv_setup() Markus Armbruster
2016-03-02 18:27 ` Marc-André Lureau
2016-03-02 19:35 ` Markus Armbruster
2016-03-03 0:03 ` Marc-André Lureau
2016-03-03 7:16 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 25/38] ivshmem: Rely on server sending the ID right after the version Markus Armbruster
2016-03-02 18:36 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 26/38] ivshmem: Drop the hackish test for UNIX domain chardev Markus Armbruster
2016-03-02 18:38 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 27/38] ivshmem: Simplify how we cope with short reads from server Markus Armbruster
2016-03-02 18:41 ` Marc-André Lureau
2016-03-02 19:38 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 28/38] ivshmem: Tighten check of property "size" Markus Armbruster
2016-03-02 18:44 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 29/38] ivshmem: Implement shm=... with a memory backend Markus Armbruster
2016-03-01 11:37 ` Paolo Bonzini
2016-03-01 12:08 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 30/38] ivshmem: Simplify memory regions for BAR 2 (shared memory) Markus Armbruster
2016-03-01 11:42 ` Paolo Bonzini
2016-03-01 12:14 ` Markus Armbruster
2016-03-01 12:17 ` Paolo Bonzini
2016-03-01 11:46 ` Paolo Bonzini
2016-03-01 14:06 ` Markus Armbruster
2016-03-01 15:15 ` Paolo Bonzini
2016-03-02 11:06 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 31/38] ivshmem: Inline check_shm_size() into its only caller Markus Armbruster
2016-03-02 18:49 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 32/38] qdev: New DEFINE_PROP_ON_OFF_AUTO Markus Armbruster
2016-03-02 18:54 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 33/38] ivshmem: Replace int role_val by OnOffAuto master Markus Armbruster
2016-03-02 18:56 ` Marc-André Lureau
2016-03-02 19:39 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 34/38] ivshmem: Split ivshmem-plain, ivshmem-doorbell off ivshmem Markus Armbruster
2016-03-03 13:53 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 35/38] ivshmem: Clean up after the previous commit Markus Armbruster
2016-03-03 13:56 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 36/38] ivshmem: Drop ivshmem property x-memdev Markus Armbruster
2016-03-03 14:03 ` Marc-André Lureau
2016-03-03 14:17 ` Markus Armbruster
2016-02-29 18:40 ` [Qemu-devel] [PATCH 37/38] ivshmem: Require master to have ID zero Markus Armbruster
2016-03-03 14:11 ` Marc-André Lureau
2016-02-29 18:40 ` [Qemu-devel] [PATCH 38/38] contrib/ivshmem-server: Print "not for production" warning Markus Armbruster
2016-03-03 14:15 ` Marc-André Lureau
2016-03-07 18:42 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1456771254-17511-24-git-send-email-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=cam@cs.ualberta.ca \
--cc=claudio.fontana@huawei.com \
--cc=david.marchand@6wind.com \
--cc=mlureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).