1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Fabian Ebner <f.ebner@proxmox.com>
3 Date: Wed, 25 May 2022 13:59:38 +0200
4 Subject: [PATCH] PVE-Backup: ensure jobs in di_list are referenced
6 Ensures that qmp_backup_cancel doesn't pick a job that's already been
7 freed. With unlucky timings it seems possible that:
8 1. job_exit -> job_completed -> job_finalize_single starts
9 2. pvebackup_co_complete_stream gets spawned in completion callback
10 3. job finalize_single finishes -> job's refcount hits zero -> job is
12 4. qmp_backup_cancel comes in and locks backup_state.backup_mutex
13 before pvebackup_co_complete_stream can remove the job from the
15 5. qmp_backup_cancel will pick a job that's already been freed
17 Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
18 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
20 pve-backup.c | 13 +++++++++++++
21 1 file changed, 13 insertions(+)
23 Index: pve-qemu-kvm-6.2.0/pve-backup.c
24 ===================================================================
25 --- pve-qemu-kvm-6.2.0.orig/pve-backup.c
26 +++ pve-qemu-kvm-6.2.0/pve-backup.c
27 @@ -316,6 +316,14 @@ static void coroutine_fn pvebackup_co_co
32 + AioContext *ctx = di->job->job.aio_context;
33 + aio_context_acquire(ctx);
34 + job_unref(&di->job->job);
36 + aio_context_release(ctx);
39 // remove self from job list
40 backup_state.di_list = g_list_remove(backup_state.di_list, di);
42 @@ -491,9 +499,12 @@ static void create_backup_jobs_bh(void *
43 bitmap_mode, false, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
44 JOB_DEFAULT, pvebackup_complete_cb, di, backup_state.txn, &local_err);
46 - aio_context_release(aio_context);
53 + aio_context_release(aio_context);
55 if (!job || local_err) {
56 error_setg(errp, "backup_job_create failed: %s",
57 @@ -521,12 +532,16 @@ static void create_backup_jobs_bh(void *
61 - if (!canceled && di->job) {
63 AioContext *ctx = di->job->job.aio_context;
64 aio_context_acquire(ctx);
65 - job_cancel_sync(&di->job->job, true);
67 + job_cancel_sync(&di->job->job, true);
70 + job_unref(&di->job->job);
72 aio_context_release(ctx);