Skip to content

Commit 45942b7

Browse files
lukasstraub2jasowang
authored andcommitted
net/colo-compare.c: Check that colo-compare is active
If the colo-compare object is removed before failover and a checkpoint happens, qemu crashes because it tries to lock the destroyed event_mtx in colo_notify_compares_event. Fix this by checking if everything is initialized by introducing a new variable colo_compare_active which is protected by a new mutex colo_compare_mutex. The new mutex also protects against concurrent access of the net_compares list and makes sure that colo_notify_compares_event isn't active while we destroy event_mtx and event_complete_cond. With this it also is again possible to use colo without colo-compare (periodic mode) and to use multiple colo-compare for multiple network interfaces. Signed-off-by: Lukas Straub <[email protected]> Tested-by: Lukas Straub <[email protected]> Reviewed-by: Zhang Chen <[email protected]> Signed-off-by: Zhang Chen <[email protected]> Signed-off-by: Jason Wang <[email protected]>
1 parent 7665854 commit 45942b7

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

net/colo-compare.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static NotifierList colo_compare_notifiers =
5454
#define REGULAR_PACKET_CHECK_MS 3000
5555
#define DEFAULT_TIME_OUT_MS 3000
5656

57+
static QemuMutex colo_compare_mutex;
58+
static bool colo_compare_active;
5759
static QemuMutex event_mtx;
5860
static QemuCond event_complete_cond;
5961
static int event_unhandled_count;
@@ -906,6 +908,12 @@ static void check_old_packet_regular(void *opaque)
906908
void colo_notify_compares_event(void *opaque, int event, Error **errp)
907909
{
908910
CompareState *s;
911+
qemu_mutex_lock(&colo_compare_mutex);
912+
913+
if (!colo_compare_active) {
914+
qemu_mutex_unlock(&colo_compare_mutex);
915+
return;
916+
}
909917

910918
qemu_mutex_lock(&event_mtx);
911919
QTAILQ_FOREACH(s, &net_compares, next) {
@@ -919,6 +927,7 @@ void colo_notify_compares_event(void *opaque, int event, Error **errp)
919927
}
920928

921929
qemu_mutex_unlock(&event_mtx);
930+
qemu_mutex_unlock(&colo_compare_mutex);
922931
}
923932

924933
static void colo_compare_timer_init(CompareState *s)
@@ -1274,7 +1283,14 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
12741283
s->vnet_hdr);
12751284
}
12761285

1286+
qemu_mutex_lock(&colo_compare_mutex);
1287+
if (!colo_compare_active) {
1288+
qemu_mutex_init(&event_mtx);
1289+
qemu_cond_init(&event_complete_cond);
1290+
colo_compare_active = true;
1291+
}
12771292
QTAILQ_INSERT_TAIL(&net_compares, s, next);
1293+
qemu_mutex_unlock(&colo_compare_mutex);
12781294

12791295
s->out_sendco.s = s;
12801296
s->out_sendco.chr = &s->chr_out;
@@ -1292,9 +1308,6 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
12921308

12931309
g_queue_init(&s->conn_list);
12941310

1295-
qemu_mutex_init(&event_mtx);
1296-
qemu_cond_init(&event_complete_cond);
1297-
12981311
s->connection_track_table = g_hash_table_new_full(connection_key_hash,
12991312
connection_key_equal,
13001313
g_free,
@@ -1382,12 +1395,19 @@ static void colo_compare_finalize(Object *obj)
13821395

13831396
qemu_bh_delete(s->event_bh);
13841397

1398+
qemu_mutex_lock(&colo_compare_mutex);
13851399
QTAILQ_FOREACH(tmp, &net_compares, next) {
13861400
if (tmp == s) {
13871401
QTAILQ_REMOVE(&net_compares, s, next);
13881402
break;
13891403
}
13901404
}
1405+
if (QTAILQ_EMPTY(&net_compares)) {
1406+
colo_compare_active = false;
1407+
qemu_mutex_destroy(&event_mtx);
1408+
qemu_cond_destroy(&event_complete_cond);
1409+
}
1410+
qemu_mutex_unlock(&colo_compare_mutex);
13911411

13921412
AioContext *ctx = iothread_get_aio_context(s->iothread);
13931413
aio_context_acquire(ctx);
@@ -1415,15 +1435,18 @@ static void colo_compare_finalize(Object *obj)
14151435
object_unref(OBJECT(s->iothread));
14161436
}
14171437

1418-
qemu_mutex_destroy(&event_mtx);
1419-
qemu_cond_destroy(&event_complete_cond);
1420-
14211438
g_free(s->pri_indev);
14221439
g_free(s->sec_indev);
14231440
g_free(s->outdev);
14241441
g_free(s->notify_dev);
14251442
}
14261443

1444+
static void __attribute__((__constructor__)) colo_compare_init_globals(void)
1445+
{
1446+
colo_compare_active = false;
1447+
qemu_mutex_init(&colo_compare_mutex);
1448+
}
1449+
14271450
static const TypeInfo colo_compare_info = {
14281451
.name = TYPE_COLO_COMPARE,
14291452
.parent = TYPE_OBJECT,

0 commit comments

Comments
 (0)