vhost-user: delay vhost_user_stop

Since commit b0a335e351, a socket write
may trigger a disconnect events, calling vhost_user_stop() and clearing
all the vhost_dev strutures holding data that vhost.c functions expect
to remain valid. Delay the cleanup to keep the vhost_dev structure
valid during the vhost.c functions.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-id: 20170227104956.24729-1-marcandre.lureau@redhat.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Marc-André Lureau 2017-02-27 14:49:56 +04:00 committed by Peter Maydell
parent 9514f2648c
commit e7c83a885f

View File

@ -190,7 +190,35 @@ static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
qemu_chr_fe_disconnect(&s->chr); qemu_chr_fe_disconnect(&s->chr);
return FALSE; return TRUE;
}
static void net_vhost_user_event(void *opaque, int event);
static void chr_closed_bh(void *opaque)
{
const char *name = opaque;
NetClientState *ncs[MAX_QUEUE_NUM];
VhostUserState *s;
Error *err = NULL;
int queues;
queues = qemu_find_net_clients_except(name, ncs,
NET_CLIENT_DRIVER_NIC,
MAX_QUEUE_NUM);
assert(queues < MAX_QUEUE_NUM);
s = DO_UPCAST(VhostUserState, nc, ncs[0]);
qmp_set_link(name, false, &err);
vhost_user_stop(queues, ncs);
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
opaque, NULL, true);
if (err) {
error_report_err(err);
}
} }
static void net_vhost_user_event(void *opaque, int event) static void net_vhost_user_event(void *opaque, int event)
@ -212,20 +240,31 @@ static void net_vhost_user_event(void *opaque, int event)
trace_vhost_user_event(chr->label, event); trace_vhost_user_event(chr->label, event);
switch (event) { switch (event) {
case CHR_EVENT_OPENED: case CHR_EVENT_OPENED:
s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
net_vhost_user_watch, s);
if (vhost_user_start(queues, ncs, &s->chr) < 0) { if (vhost_user_start(queues, ncs, &s->chr) < 0) {
qemu_chr_fe_disconnect(&s->chr); qemu_chr_fe_disconnect(&s->chr);
return; return;
} }
s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
net_vhost_user_watch, s);
qmp_set_link(name, true, &err); qmp_set_link(name, true, &err);
s->started = true; s->started = true;
break; break;
case CHR_EVENT_CLOSED: case CHR_EVENT_CLOSED:
qmp_set_link(name, false, &err); /* a close event may happen during a read/write, but vhost
vhost_user_stop(queues, ncs); * code assumes the vhost_dev remains setup, so delay the
g_source_remove(s->watch); * stop & clear to idle.
s->watch = 0; * FIXME: better handle failure in vhost code, remove bh
*/
if (s->watch) {
AioContext *ctx = qemu_get_current_aio_context();
g_source_remove(s->watch);
s->watch = 0;
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL,
NULL, NULL, false);
aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
}
break; break;
} }