Skip to content

Commit f5b22d0

Browse files
Dima Stepanovmstsirkin
Dima Stepanov
authored andcommitted
vhost: recheck dev state in the vhost_migration_log routine
vhost-user devices can get a disconnect in the middle of the VHOST-USER handshake on the migration start. If disconnect event happened right before sending next VHOST-USER command, then the vhost_dev_set_log() call in the vhost_migration_log() function will return error. This error will lead to the assert() and close the QEMU migration source process. For the vhost-user devices the disconnect event should not break the migration process, because: - the device will be in the stopped state, so it will not be changed during migration - if reconnect will be made the migration log will be reinitialized as part of reconnect/init process: #0 vhost_log_global_start (listener=0x563989cf7be0) at hw/virtio/vhost.c:920 #1 0x000056398603d8bc in listener_add_address_space (listener=0x563989cf7be0, as=0x563986ea4340 <address_space_memory>) at softmmu/memory.c:2664 #2 0x000056398603dd30 in memory_listener_register (listener=0x563989cf7be0, as=0x563986ea4340 <address_space_memory>) at softmmu/memory.c:2740 #3 0x0000563985fd6956 in vhost_dev_init (hdev=0x563989cf7bd8, opaque=0x563989cf7e30, backend_type=VHOST_BACKEND_TYPE_USER, busyloop_timeout=0) at hw/virtio/vhost.c:1385 #4 0x0000563985f7d0b8 in vhost_user_blk_connect (dev=0x563989cf7990) at hw/block/vhost-user-blk.c:315 #5 0x0000563985f7d3f6 in vhost_user_blk_event (opaque=0x563989cf7990, event=CHR_EVENT_OPENED) at hw/block/vhost-user-blk.c:379 Update the vhost-user-blk device with the internal started_vu field which will be used for initialization (vhost_user_blk_start) and clean up (vhost_user_blk_stop). This additional flag in the VhostUserBlk structure will be used to track whether the device really needs to be stopped and cleaned up on a vhost-user level. The disconnect event will set the overall VHOST device (not vhost-user) to the stopped state, so it can be used by the general vhost_migration_log routine. Such approach could be propogated to the other vhost-user devices, but better idea is just to make the same connect/disconnect code for all the vhost-user devices. This migration issue was slightly discussed earlier: - https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg01509.html - https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg05241.html Signed-off-by: Dima Stepanov <[email protected]> Reviewed-by: Raphael Norwitz <[email protected]> Message-Id: <9fbfba06791a87813fcee3e2315f0b904cc6789a.1599813294.git.dimastep@yandex-team.ru> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent d110b6b commit f5b22d0

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

hw/block/vhost-user-blk.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ static int vhost_user_blk_start(VirtIODevice *vdev)
150150
error_report("Error starting vhost: %d", -ret);
151151
goto err_guest_notifiers;
152152
}
153+
s->started_vu = true;
153154

154155
/* guest_notifier_mask/pending not used yet, so just unmask
155156
* everything here. virtio-pci will do the right thing by
@@ -175,6 +176,11 @@ static void vhost_user_blk_stop(VirtIODevice *vdev)
175176
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
176177
int ret;
177178

179+
if (!s->started_vu) {
180+
return;
181+
}
182+
s->started_vu = false;
183+
178184
if (!k->set_guest_notifiers) {
179185
return;
180186
}
@@ -341,9 +347,7 @@ static void vhost_user_blk_disconnect(DeviceState *dev)
341347
}
342348
s->connected = false;
343349

344-
if (s->dev.started) {
345-
vhost_user_blk_stop(vdev);
346-
}
350+
vhost_user_blk_stop(vdev);
347351

348352
vhost_dev_cleanup(&s->dev);
349353
}
@@ -399,6 +403,15 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
399403
NULL, NULL, false);
400404
aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque);
401405
}
406+
407+
/*
408+
* Move vhost device to the stopped state. The vhost-user device
409+
* will be clean up and disconnected in BH. This can be useful in
410+
* the vhost migration code. If disconnect was caught there is an
411+
* option for the general vhost code to get the dev state without
412+
* knowing its type (in this case vhost-user).
413+
*/
414+
s->dev.started = false;
402415
break;
403416
case CHR_EVENT_BREAK:
404417
case CHR_EVENT_MUX_IN:

hw/virtio/vhost.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,21 +871,42 @@ static int vhost_migration_log(MemoryListener *listener, bool enable)
871871
dev->log_enabled = enable;
872872
return 0;
873873
}
874+
875+
r = 0;
874876
if (!enable) {
875877
r = vhost_dev_set_log(dev, false);
876878
if (r < 0) {
877-
return r;
879+
goto check_dev_state;
878880
}
879881
vhost_log_put(dev, false);
880882
} else {
881883
vhost_dev_log_resize(dev, vhost_get_log_size(dev));
882884
r = vhost_dev_set_log(dev, true);
883885
if (r < 0) {
884-
return r;
886+
goto check_dev_state;
885887
}
886888
}
889+
890+
check_dev_state:
887891
dev->log_enabled = enable;
888-
return 0;
892+
/*
893+
* vhost-user-* devices could change their state during log
894+
* initialization due to disconnect. So check dev state after
895+
* vhost communication.
896+
*/
897+
if (!dev->started) {
898+
/*
899+
* Since device is in the stopped state, it is okay for
900+
* migration. Return success.
901+
*/
902+
r = 0;
903+
}
904+
if (r) {
905+
/* An error is occured. */
906+
dev->log_enabled = false;
907+
}
908+
909+
return r;
889910
}
890911

891912
static void vhost_log_global_start(MemoryListener *listener)

include/hw/virtio/vhost-user-blk.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ struct VHostUserBlk {
4040
VhostUserState vhost_user;
4141
struct vhost_virtqueue *vhost_vqs;
4242
VirtQueue **virtqs;
43+
44+
/*
45+
* There are at least two steps of initialization of the
46+
* vhost-user device. The first is a "connect" step and
47+
* second is a "start" step. Make a separation between
48+
* those initialization phases by using two fields.
49+
*/
50+
/* vhost_user_blk_connect/vhost_user_blk_disconnect */
4351
bool connected;
52+
/* vhost_user_blk_start/vhost_user_blk_stop */
53+
bool started_vu;
4454
};
4555

4656
#endif

0 commit comments

Comments
 (0)