Skip to content

Commit df33ce9

Browse files
kevmwMichael Tokarev
authored and
Michael Tokarev
committed
virtio: Drop out of coroutine context in virtio_load()
virtio_load() as a whole should run in coroutine context because it reads from the migration stream and we don't want this to block. However, it calls virtio_set_features_nocheck() and devices don't expect their .set_features callback to run in a coroutine and therefore call functions that may not be called in coroutine context. To fix this, drop out of coroutine context for calling virtio_set_features_nocheck(). Without this fix, the following crash was reported: #0 __pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44 #1 0x00007efc738c05d3 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at pthread_kill.c:78 #2 0x00007efc73873d26 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #3 0x00007efc738477f3 in __GI_abort () at abort.c:79 #4 0x00007efc7384771b in __assert_fail_base (fmt=0x7efc739dbcb8 "", assertion=assertion@entry=0x560aebfbf5cf "!qemu_in_coroutine()", file=file@entry=0x560aebfcd2d4 "../block/graph-lock.c", line=line@entry=275, function=function@entry=0x560aebfcd34d "void bdrv_graph_rdlock_main_loop(void)") at assert.c:92 #5 0x00007efc7386ccc6 in __assert_fail (assertion=0x560aebfbf5cf "!qemu_in_coroutine()", file=0x560aebfcd2d4 "../block/graph-lock.c", line=275, function=0x560aebfcd34d "void bdrv_graph_rdlock_main_loop(void)") at assert.c:101 #6 0x0000560aebcd8dd6 in bdrv_register_buf () #7 0x0000560aeb97ed97 in ram_block_added.llvm () #8 0x0000560aebb8303f in ram_block_add.llvm () #9 0x0000560aebb834fa in qemu_ram_alloc_internal.llvm () #10 0x0000560aebb2ac98 in vfio_region_mmap () #11 0x0000560aebb3ea0f in vfio_bars_register () #12 0x0000560aebb3c628 in vfio_realize () #13 0x0000560aeb90f0c2 in pci_qdev_realize () #14 0x0000560aebc40305 in device_set_realized () #15 0x0000560aebc48e07 in property_set_bool.llvm () #16 0x0000560aebc46582 in object_property_set () #17 0x0000560aebc4cd58 in object_property_set_qobject () #18 0x0000560aebc46ba7 in object_property_set_bool () #19 0x0000560aeb98b3ca in qdev_device_add_from_qdict () #20 0x0000560aebb1fbaf in virtio_net_set_features () #21 0x0000560aebb46b51 in virtio_set_features_nocheck () #22 0x0000560aebb47107 in virtio_load () #23 0x0000560aeb9ae7ce in vmstate_load_state () #24 0x0000560aeb9d2ee9 in qemu_loadvm_state_main () #25 0x0000560aeb9d45e1 in qemu_loadvm_state () #26 0x0000560aeb9bc32c in process_incoming_migration_co.llvm () #27 0x0000560aebeace56 in coroutine_trampoline.llvm () Cc: [email protected] Buglink: https://issues.redhat.com/browse/RHEL-832 Signed-off-by: Kevin Wolf <[email protected]> Message-ID: <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> (cherry picked from commit 92e2e6a) Signed-off-by: Michael Tokarev <[email protected]>
1 parent eeee989 commit df33ce9

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

hw/virtio/virtio.c

+40-5
Original file line numberDiff line numberDiff line change
@@ -2825,8 +2825,9 @@ static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
28252825
}
28262826

28272827
/* A wrapper for use as a VMState .get function */
2828-
static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
2829-
const VMStateField *field)
2828+
static int coroutine_mixed_fn
2829+
virtio_device_get(QEMUFile *f, void *opaque, size_t size,
2830+
const VMStateField *field)
28302831
{
28312832
VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
28322833
DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
@@ -2853,6 +2854,39 @@ static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
28532854
return bad ? -1 : 0;
28542855
}
28552856

2857+
typedef struct VirtioSetFeaturesNocheckData {
2858+
Coroutine *co;
2859+
VirtIODevice *vdev;
2860+
uint64_t val;
2861+
int ret;
2862+
} VirtioSetFeaturesNocheckData;
2863+
2864+
static void virtio_set_features_nocheck_bh(void *opaque)
2865+
{
2866+
VirtioSetFeaturesNocheckData *data = opaque;
2867+
2868+
data->ret = virtio_set_features_nocheck(data->vdev, data->val);
2869+
aio_co_wake(data->co);
2870+
}
2871+
2872+
static int coroutine_mixed_fn
2873+
virtio_set_features_nocheck_maybe_co(VirtIODevice *vdev, uint64_t val)
2874+
{
2875+
if (qemu_in_coroutine()) {
2876+
VirtioSetFeaturesNocheckData data = {
2877+
.co = qemu_coroutine_self(),
2878+
.vdev = vdev,
2879+
.val = val,
2880+
};
2881+
aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
2882+
virtio_set_features_nocheck_bh, &data);
2883+
qemu_coroutine_yield();
2884+
return data.ret;
2885+
} else {
2886+
return virtio_set_features_nocheck(vdev, val);
2887+
}
2888+
}
2889+
28562890
int virtio_set_features(VirtIODevice *vdev, uint64_t val)
28572891
{
28582892
int ret;
@@ -2906,7 +2940,8 @@ size_t virtio_get_config_size(const VirtIOConfigSizeParams *params,
29062940
return config_size;
29072941
}
29082942

2909-
int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
2943+
int coroutine_mixed_fn
2944+
virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
29102945
{
29112946
int i, ret;
29122947
int32_t config_len;
@@ -3023,14 +3058,14 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
30233058
* host_features.
30243059
*/
30253060
uint64_t features64 = vdev->guest_features;
3026-
if (virtio_set_features_nocheck(vdev, features64) < 0) {
3061+
if (virtio_set_features_nocheck_maybe_co(vdev, features64) < 0) {
30273062
error_report("Features 0x%" PRIx64 " unsupported. "
30283063
"Allowed features: 0x%" PRIx64,
30293064
features64, vdev->host_features);
30303065
return -1;
30313066
}
30323067
} else {
3033-
if (virtio_set_features_nocheck(vdev, features) < 0) {
3068+
if (virtio_set_features_nocheck_maybe_co(vdev, features) < 0) {
30343069
error_report("Features 0x%x unsupported. "
30353070
"Allowed features: 0x%" PRIx64,
30363071
features, vdev->host_features);

0 commit comments

Comments
 (0)