[Devel] [PATCH hci-8.2 1/2] dm-qcow2: fix image I/O completions running on a freed target
Denis V. Lunev
den at openvz.org
Thu Nov 19 14:49:13 MSK 2026
From: Denis V. Lunev <den at openvz.org>
Nothing keeps the qcow2 target alive while an image file I/O completion
runs. Suspend waits only for the inflight references, and a completion
can end the qio that holds the last one and then still use the target:
- a qio freed on endio that holds no reference of its own goes into
tgt->qio_pool after its callback has ended its owner, e.g. a split
qio after its parent, or a COW, compressed read or merge helper qio;
- a metadata page read drops its own reference and then frees its qio
into tgt->qio_pool.
These run in the completion context of the backing file system, which
dm-qcow2 does not own, and a dmsetup remove in that window destroys the
pool under them.
Take a percpu reference in qcow2_call_rw_iter() for every image file
I/O and drop it after the completion handler returns.
qcow2_tgt_destroy() kills it once nothing can submit and waits for the
last completion before it frees the pools. The wait comes after
destroy_workqueue(), because the drained worker may still submit image
I/O.
https://virtuozzo.atlassian.net/browse/VSTOR-156036
Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den at openvz.org>
---
drivers/md/dm-qcow2-target.c | 25 +++++++++++++++++++++++++
drivers/md/dm-qcow2.h | 2 ++
2 files changed, 27 insertions(+)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index 2f792d823e7b..2394e9d8ad3b 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -108,9 +108,12 @@ static bool should_fail_rw(struct qcow2 *qcow2)
static void qcow2_aio_do_completion(struct qio *qio)
{
+ struct qcow2_target *tgt = qio->qcow2->tgt;
+
if (!atomic_dec_and_test(&qio->aio_ref))
return;
qio->complete(qio);
+ percpu_ref_put(&tgt->file_io_ref);
}
static void qcow2_aio_complete(struct kiocb *iocb, long ret)
@@ -133,6 +136,7 @@ void qcow2_call_rw_iter(struct qcow2 *qcow2, loff_t pos, unsigned int rw,
iocb->ki_pos = pos;
iocb->ki_complete = qcow2_aio_complete;
+ percpu_ref_get(&qcow2->tgt->file_io_ref);
atomic_set(&qio->aio_ref, 2);
if (unlikely(should_fail_rw(qcow2)))
@@ -258,9 +262,13 @@ static void qcow2_tgt_destroy(struct qcow2_target *tgt)
cancel_work_sync(&tgt->event_work);
qcow2_merge_backward_cancel(tgt);
+ percpu_ref_kill(&tgt->file_io_ref);
+ wait_for_completion(&tgt->file_io_comp);
+
mempool_destroy(tgt->qio_pool);
mempool_destroy(tgt->qrq_pool);
+ percpu_ref_exit(&tgt->file_io_ref);
for (i = 0; i < 2; i++)
percpu_ref_exit(&tgt->inflight_ref[i]);
@@ -501,6 +509,14 @@ static void qcow2_event_work(struct work_struct *ws)
dm_table_event(tgt->ti->table);
}
+static void file_io_ref_release(struct percpu_ref *ref)
+{
+ struct qcow2_target *tgt = container_of(ref, struct qcow2_target,
+ file_io_ref);
+
+ complete(&tgt->file_io_comp);
+}
+
static struct qcow2_target *alloc_qcow2_target(struct dm_target *ti)
{
percpu_ref_func_t *release;
@@ -537,6 +553,15 @@ static struct qcow2_target *alloc_qcow2_target(struct dm_target *ti)
}
}
+ init_completion(&tgt->file_io_comp);
+ if (percpu_ref_init(&tgt->file_io_ref, file_io_ref_release, 0,
+ GFP_KERNEL)) {
+ for (i = 0; i < 2; i++)
+ percpu_ref_exit(&tgt->inflight_ref[i]);
+ ti->error = "could not alloc percpu_ref";
+ goto out_wq;
+ }
+
init_completion(&tgt->inflight_ref_comp);
spin_lock_init(&tgt->event_lock);
mutex_init(&tgt->ctl_mutex);
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 3f1d5b5eef26..411d9a6eb04d 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -202,6 +202,8 @@ struct qcow2_target {
struct completion inflight_ref_comp;
struct percpu_ref inflight_ref[2];
+ struct completion file_io_comp;
+ struct percpu_ref file_io_ref;
unsigned int inflight_ref_index:1;
bool service_operations_allowed;
--
2.55.0
More information about the Devel
mailing list