* [Devel] [PATCH hci-8.2 v2 1/8] dm-qcow2: drop the qio argument of endio callbacks
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:40 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 2/8] dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool Denis V. Lunev
` (8 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
The endio callbacks get the qio that completes, but only qrq_endio()
uses it, and that qio is embedded in the qrq it already receives. The
other callbacks ignore it, and their direct callers pass NULL.
Drop the argument, so that the next patch can free a qio before its
callback runs. qrq_endio() finds the embedded qio behind its qrq.
https://virtuozzo.atlassian.net/browse/VSTOR-156036
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-qcow2-cmd.c | 2 +-
drivers/md/dm-qcow2-map.c | 34 +++++++++++++++++-----------------
drivers/md/dm-qcow2.h | 4 +---
3 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/drivers/md/dm-qcow2-cmd.c b/drivers/md/dm-qcow2-cmd.c
index 00b33ccaab10..47a002320e6b 100644
--- a/drivers/md/dm-qcow2-cmd.c
+++ b/drivers/md/dm-qcow2-cmd.c
@@ -42,7 +42,7 @@ int qcow2_inflight_ref_switch(struct qcow2_target *tgt)
return 0;
}
-static void service_qio_endio(struct qcow2_target *tgt, struct qio *qio,
+static void service_qio_endio(struct qcow2_target *tgt,
void *data, blk_status_t status)
{
blk_status_t *status_ptr = data;
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 853e7a1230ee..90e699050131 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -418,7 +418,7 @@ static void do_qio_endio(struct qio *qio)
qio->ref_index = REF_INDEX_INVALID;
/* Note, that this may free qio or its container memory */
if (endio_cb)
- endio_cb(tgt, qio, endio_cb_data, qio->bi_status);
+ endio_cb(tgt, endio_cb_data, qio->bi_status);
if (ref_index < REF_INDEX_INVALID)
qcow2_ref_dec(tgt, ref_index);
@@ -474,7 +474,7 @@ static void end_qios(struct list_head *qio_list, blk_status_t status)
}
}
-static void qio_chain_endio(struct qcow2_target *tgt, struct qio *qio,
+static void qio_chain_endio(struct qcow2_target *tgt,
void *parent_ptr, blk_status_t bi_status)
{
struct qio *parent = parent_ptr;
@@ -2861,7 +2861,7 @@ static int prepare_l1l2_replace(struct qcow2 *qcow2, struct qio *qio,
map->cow_clu_end, L2_LEVEL);
}
-static void backward_merge_write_complete(struct qcow2_target *tgt, struct qio *unused,
+static void backward_merge_write_complete(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
struct qio *qio = qio_ptr;
@@ -2886,7 +2886,7 @@ static void backward_merge_write_complete(struct qcow2_target *tgt, struct qio *
qcow2_dispatch_qios(qcow2, qio, NULL);
}
-static void backward_merge_read_complete(struct qcow2_target *tgt, struct qio *unused,
+static void backward_merge_read_complete(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
struct qio *qio = qio_ptr;
@@ -2902,7 +2902,7 @@ static void backward_merge_read_complete(struct qcow2_target *tgt, struct qio *u
qcow2_dispatch_qios(qcow2, qio, NULL);
}
-static void requeue_if_ok(struct qcow2_target *tgt, struct qio *unused,
+static void requeue_if_ok(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
struct qio *qio = qio_ptr;
@@ -3108,7 +3108,7 @@ static void perform_rw_mapped(struct qcow2_map *map, struct qio *qio)
map_and_submit_rw(qcow2, map->data_clu_pos, qio);
}
-static void cow_read_endio(struct qcow2_target *tgt, struct qio *unused,
+static void cow_read_endio(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
@@ -3333,7 +3333,7 @@ static int prepare_zcow_slices(void *buf, struct qio *qio)
return 0;
}
-static void compressed_read_endio(struct qcow2_target *tgt, struct qio *unused,
+static void compressed_read_endio(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
@@ -3425,9 +3425,9 @@ static void submit_read_compressed(struct qcow2_map *map, struct qio *qio,
*/
if (merge) {
dec_wpc_readers(qcow2, l2_md);
- backward_merge_read_complete(tgt, NULL, qio, BLK_STS_OK);
+ backward_merge_read_complete(tgt, qio, BLK_STS_OK);
} else {
- cow_read_endio(tgt, NULL, qio, BLK_STS_OK);
+ cow_read_endio(tgt, qio, BLK_STS_OK);
}
qcow2_free_qio(read_qio, tgt->qio_pool);
return;
@@ -3445,7 +3445,7 @@ static void submit_read_compressed(struct qcow2_map *map, struct qio *qio,
__submit_rw_mapped(qcow2, read_qio, nr_pages);
}
-static void sliced_cow_read_complete(struct qcow2_target *tgt, struct qio *read_qio,
+static void sliced_cow_read_complete(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
struct qio *qio = qio_ptr;
@@ -3522,7 +3522,7 @@ static void submit_read_sliced_clu(struct qcow2_map *map, struct qio *qio,
if (!nr_segs) { /* Full overwrite */
qio->data = NULL; /* qvec */
- endio_cb(qcow2->tgt, NULL, qio, BLK_STS_OK);
+ endio_cb(qcow2->tgt, qio, BLK_STS_OK);
goto out;
}
@@ -4189,8 +4189,7 @@ static void process_compressed_read(struct list_head *read_list,
}
if (backward_merge_qio(qcow2, qio)) {
- backward_merge_read_complete(qcow2->tgt, NULL, qio,
- BLK_STS_OK);
+ backward_merge_read_complete(qcow2->tgt, qio, BLK_STS_OK);
continue;
}
@@ -4281,7 +4280,7 @@ static void process_backward_merge_write(struct qcow2 *qcow2, struct list_head *
}
}
-static void cow_data_write_endio(struct qcow2_target *tgt, struct qio *unused,
+static void cow_data_write_endio(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
@@ -4329,7 +4328,7 @@ static void submit_cow_data_write(struct qcow2 *qcow2, struct qio *qio, loff_t p
__submit_rw_mapped(qcow2, write_qio, clu_size >> PAGE_SHIFT);
}
-static void sliced_cow_data_write_complete(struct qcow2_target *tgt, struct qio *unused,
+static void sliced_cow_data_write_complete(struct qcow2_target *tgt,
void *qio_ptr, blk_status_t bi_status)
{
@@ -4651,10 +4650,11 @@ static bool qcow2_try_delay_enospc(struct qcow2_target *tgt, struct qcow2_rq *qr
return delayed;
}
-static void qrq_endio(struct qcow2_target *tgt, struct qio *qio,
+static void qrq_endio(struct qcow2_target *tgt,
void *qrq_ptr, blk_status_t bi_status)
{
struct qcow2_rq *qrq = qrq_ptr;
+ struct qio *qio = (void *)qrq + sizeof(*qrq);
struct request *rq = qrq->rq;
if (qrq->bvec)
@@ -4838,7 +4838,7 @@ static void seek_qio_set_sector(struct qio *qio, sector_t bi_sector)
}
/* A failed md page read ends the qios waiting for it */
-static void seek_qio_endio(struct qcow2_target *tgt, struct qio *qio,
+static void seek_qio_endio(struct qcow2_target *tgt,
void *data, blk_status_t bi_status)
{
complete(&((struct qio_data_llseek_hole *)data)->compl);
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 4acddbe7c95f..9c41e1a48e09 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -344,9 +344,7 @@ struct qcow2_rq {
struct bio_vec *bvec;
};
-struct qio;
-typedef void (*qcow2_endio_t)(struct qcow2_target *, struct qio *,
- void *, blk_status_t);
+typedef void (*qcow2_endio_t)(struct qcow2_target *, void *, blk_status_t);
struct qio {
struct bvec_iter bi_iter;
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* [Devel] [PATCH hci-8.2 v2 2/8] dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 1/8] dm-qcow2: drop the qio argument of endio callbacks Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:41 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed Denis V. Lunev
` (7 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
do_qio_endio() calls the endio callback, drops the qio's inflight
reference and only then frees a QIO_FREE_ON_ENDIO_FL qio into
tgt->qio_pool. Either of the first two can end the last user of the
target: a split qio's callback completes its parent request, whose
reference goes, and a metadata page read drops its own reference.
Suspend then finishes, and a dmsetup remove destroys the pool before
the free runs from the backing file system's completion context, which
oopses in mempool_free().
Free the qio first, then call the callback, then drop the reference,
so that nothing touches the target after the last of them. Read the
qio's fields once its last completion has arrived, as children write
the status of their parent up to then.
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@openvz.org>
---
drivers/md/dm-qcow2-map.c | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 90e699050131..2227e06591e9 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -405,25 +405,30 @@ static void qcow2_free_qio(struct qio *qio, mempool_t *pool)
static void do_qio_endio(struct qio *qio)
{
- struct qcow2 *qcow2 = qio->qcow2;
- struct qcow2_target *tgt = qcow2->tgt;
- qcow2_endio_t endio_cb = qio->endio_cb;
- void *endio_cb_data = qio->endio_cb_data;
- unsigned int flags = qio->flags;
- u8 ref_index = qio->ref_index;
+ struct qcow2_target *tgt;
+ qcow2_endio_t endio_cb;
+ blk_status_t bi_status;
+ void *endio_cb_data;
+ u8 ref_index;
if (!atomic_dec_and_test(&qio->remaining))
return;
+ tgt = qio->qcow2->tgt;
+ endio_cb = qio->endio_cb;
+ endio_cb_data = qio->endio_cb_data;
+ bi_status = qio->bi_status;
+ ref_index = qio->ref_index;
qio->ref_index = REF_INDEX_INVALID;
- /* Note, that this may free qio or its container memory */
- if (endio_cb)
- endio_cb(tgt, endio_cb_data, qio->bi_status);
+ /* The callback and the ref drop may end the last user of tgt */
+ if (qio->flags & QIO_FREE_ON_ENDIO_FL)
+ qcow2_free_qio(qio, tgt->qio_pool);
+ /* Note, that this may free qio's container memory */
+ if (endio_cb)
+ endio_cb(tgt, endio_cb_data, bi_status);
if (ref_index < REF_INDEX_INVALID)
qcow2_ref_dec(tgt, ref_index);
- if (flags & QIO_FREE_ON_ENDIO_FL)
- qcow2_free_qio(qio, tgt->qio_pool);
}
static void qio_endio(struct qio *qio)
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 2/8] dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 2/8] dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool Denis V. Lunev
@ 2026-11-23 15:41 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:41 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> do_qio_endio() calls the endio callback, drops the qio's inflight
> reference and only then frees a QIO_FREE_ON_ENDIO_FL qio into
> tgt->qio_pool. Either of the first two can end the last user of the
> target: a split qio's callback completes its parent request, whose
> reference goes, and a metadata page read drops its own reference.
> Suspend then finishes, and a dmsetup remove destroys the pool before
> the free runs from the backing file system's completion context, which
> oopses in mempool_free().
>
> Free the qio first, then call the callback, then drop the reference,
> so that nothing touches the target after the last of them. Read the
> qio's fields once its last completion has arrived, as children write
> the status of their parent up to then.
>
> 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@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=2
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 1/8] dm-qcow2: drop the qio argument of endio callbacks Denis V. Lunev
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 2/8] dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:42 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target Denis V. Lunev
` (6 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
qcow2_dispatch_qios(), qcow2_queue_resubmit() and
md_page_write_complete() add a qio to a worker list under
deferred_lock, drop the lock and only then queue the worker. Called
from an image I/O completion, they let the worker take the qio in that
gap and end it, and suspend and a dmsetup remove can then destroy
tgt->wq before queue_work() runs on it. A backward merge has the same
gap without a remove: qcow2_merge_backward_complete() frees the merged
image, and with it the qcow2->worker that the late queue_work() uses.
Queue the worker before dropping the lock, through a helper that
asserts deferred_lock is held. The worker takes its lists under the
same lock, so the qio, and the reference that keeps the target, outlive
the queue_work() call. Do the same in qcow2_submit_embedded_qio(), where
only the pattern changes: its qios belong to DM requests, and its timer
and merge callers are stopped before the target is destroyed.
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@openvz.org>
---
drivers/md/dm-qcow2-map.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 2227e06591e9..397738351b07 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -447,6 +447,14 @@ static void qcow2_dispatch_qio(struct qcow2 *qcow2, struct qio *qio)
list_add_tail(&qio->link, &qcow2->qios[qio->queue_list_id]);
}
+/* Queued before deferred_lock drops, so the published qio pins tgt */
+static void qcow2_queue_work_locked(struct qcow2 *qcow2,
+ struct work_struct *work)
+{
+ lockdep_assert_held(&qcow2->deferred_lock);
+ queue_work(qcow2->tgt->wq, work);
+}
+
void qcow2_dispatch_qios(struct qcow2 *qcow2, struct qio *qio,
struct list_head *qio_list)
{
@@ -463,9 +471,8 @@ void qcow2_dispatch_qios(struct qcow2 *qcow2, struct qio *qio,
qcow2_dispatch_qio(qcow2, qio);
}
+ qcow2_queue_work_locked(qcow2, &qcow2->worker);
spin_unlock_irqrestore(&qcow2->deferred_lock, flags);
-
- queue_work(qcow2->tgt->wq, &qcow2->worker);
}
static void end_qios(struct list_head *qio_list, blk_status_t status)
@@ -1584,8 +1591,8 @@ static void md_page_write_complete(struct qio *qio)
qio->queue_list_id = QLIST_COMPLETED_WB;
spin_lock_irqsave(&qcow2->deferred_lock, flags);
list_add_tail(&qio->link, &qcow2->qios[QLIST_COMPLETED_WB]);
+ qcow2_queue_work_locked(qcow2, &qcow2->fsync_worker);
spin_unlock_irqrestore(&qcow2->deferred_lock, flags);
- queue_work(qcow2->tgt->wq, &qcow2->fsync_worker);
}
static void submit_rw_md_page(unsigned int rw, struct qcow2 *qcow2,
@@ -3013,8 +3020,8 @@ static void qcow2_queue_resubmit(struct qio *qio)
spin_lock_irqsave(&qcow2->deferred_lock, flags);
list_add_tail(&qio->link, &qcow2->resubmit_qios);
+ qcow2_queue_work_locked(qcow2, &qcow2->worker);
spin_unlock_irqrestore(&qcow2->deferred_lock, flags);
- queue_work(qcow2->tgt->wq, &qcow2->worker);
}
static void data_rw_complete(struct qio *qio)
@@ -4706,7 +4713,6 @@ static void qcow2_submit_embedded_qio(struct qcow2_target *tgt, struct qio *qio)
struct work_struct *worker;
struct qcow2 *qcow2;
unsigned long flags;
- bool queue = true;
qcow2 = qcow2_ref_inc(tgt, &ref_index);
@@ -4723,17 +4729,14 @@ static void qcow2_submit_embedded_qio(struct qcow2_target *tgt, struct qio *qio)
if (unlikely(qcow2->pause_submitting_qios)) {
qcow2_ref_dec(tgt, ref_index);
list_add_tail(&qio->link, &qcow2->paused_qios);
- queue = false;
} else {
qio->qcow2 = qcow2;
qio->queue_list_id = queue_list_id;
qio->ref_index = ref_index;
list_add_tail(&qio->link, &qcow2->qios[qio->queue_list_id]);
+ qcow2_queue_work_locked(qcow2, worker);
}
spin_unlock_irqrestore(&qcow2->deferred_lock, flags);
-
- if (queue)
- queue_work(tgt->wq, worker);
}
void qcow2_submit_embedded_qios(struct qcow2_target *tgt, struct list_head *list)
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed Denis V. Lunev
@ 2026-11-23 15:42 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:42 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> qcow2_dispatch_qios(), qcow2_queue_resubmit() and
> md_page_write_complete() add a qio to a worker list under
> deferred_lock, drop the lock and only then queue the worker. Called
> from an image I/O completion, they let the worker take the qio in that
> gap and end it, and suspend and a dmsetup remove can then destroy
> tgt->wq before queue_work() runs on it. A backward merge has the same
> gap without a remove: qcow2_merge_backward_complete() frees the merged
> image, and with it the qcow2->worker that the late queue_work() uses.
>
> Queue the worker before dropping the lock, through a helper that
> asserts deferred_lock is held. The worker takes its lists under the
> same lock, so the qio, and the reference that keeps the target, outlive
> the queue_work() call. Do the same in qcow2_submit_embedded_qio(), where
> only the pattern changes: its qios belong to DM requests, and its timer
> and merge callers are stopped before the target is destroyed.
>
> 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@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=3
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (2 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:43 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 5/8] dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer Denis V. Lunev
` (5 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
service_qio_endio() decrements tgt->service_qios and only then calls
wake_up() on tgt->service_wq. Service qios hold no inflight reference,
so the waiter in qcow2_service_iter() can see the counter reach zero
in that gap and return: the backward merge ends, and a dmsetup remove
frees the target. The wake-up from the image I/O completion then takes
the freed service_wq lock.
Protect the counter with a dedicated spinlock and hold it over the
wake-up; the waiters check the counter under the same lock with
wait_event_lock_irq(), so they cannot return before the completion
drops it. service_wq.lock itself cannot serve, as the wait takes it
inside. Count a service qio before dispatching it, so that its
completion never runs ahead of the increment, and read the status
under the lock as well.
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@openvz.org>
---
drivers/md/dm-qcow2-cmd.c | 32 ++++++++++++++++++--------------
drivers/md/dm-qcow2-target.c | 1 +
drivers/md/dm-qcow2.h | 4 +++-
3 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/drivers/md/dm-qcow2-cmd.c b/drivers/md/dm-qcow2-cmd.c
index 47a002320e6b..fb3d1e014046 100644
--- a/drivers/md/dm-qcow2-cmd.c
+++ b/drivers/md/dm-qcow2-cmd.c
@@ -46,14 +46,14 @@ static void service_qio_endio(struct qcow2_target *tgt,
void *data, blk_status_t status)
{
blk_status_t *status_ptr = data;
+ unsigned long flags;
- if (unlikely(status)) {
+ spin_lock_irqsave(&tgt->service_lock, flags);
+ if (unlikely(status))
WRITE_ONCE(*status_ptr, status);
- smp_wmb(); /* Pairs with smp_rmb() in qcow2_service_iter() */
- }
-
- atomic_dec(&tgt->service_qios);
+ tgt->service_qios--;
wake_up(&tgt->service_wq);
+ spin_unlock_irqrestore(&tgt->service_lock, flags);
}
static void backward_merge_update_progress(struct qcow2_target *tgt,
@@ -93,21 +93,25 @@ static int qcow2_service_iter(struct qcow2_target *tgt, struct qcow2 *qcow2,
qio->endio_cb = service_qio_endio;
qio->endio_cb_data = &service_status;
+ spin_lock_irq(&tgt->service_lock);
+ wait_event_lock_irq(tgt->service_wq,
+ tgt->service_qios < SERVICE_QIOS_MAX,
+ tgt->service_lock);
+ tgt->service_qios++;
+ spin_unlock_irq(&tgt->service_lock);
+
qcow2_dispatch_qios(qcow2, qio, NULL);
- if (atomic_inc_return(&tgt->service_qios) == SERVICE_QIOS_MAX) {
- wait_event(tgt->service_wq,
- atomic_read(&tgt->service_qios) < SERVICE_QIOS_MAX);
- }
if (unlikely(READ_ONCE(service_status)))
break;
}
- wait_event(tgt->service_wq, !atomic_read(&tgt->service_qios));
- if (!ret) {
- smp_rmb(); /* Pairs with smp_wmb() in service_qio_endio() */
- ret = blk_status_to_errno(READ_ONCE(service_status));
- }
+ spin_lock_irq(&tgt->service_lock);
+ wait_event_lock_irq(tgt->service_wq, !tgt->service_qios,
+ tgt->service_lock);
+ if (!ret)
+ ret = blk_status_to_errno(service_status);
+ spin_unlock_irq(&tgt->service_lock);
backward_merge_update_progress(tgt, (pos > end) ? end : pos);
return ret;
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index d445bfe1232a..f839b444b328 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -541,6 +541,7 @@ static struct qcow2_target *alloc_qcow2_target(struct dm_target *ti)
spin_lock_init(&tgt->event_lock);
mutex_init(&tgt->ctl_mutex);
init_rwsem(&tgt->seek_rwsem);
+ spin_lock_init(&tgt->service_lock);
init_waitqueue_head(&tgt->service_wq);
INIT_WORK(&tgt->event_work, qcow2_event_work);
INIT_LIST_HEAD(&tgt->enospc_qios);
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 9c41e1a48e09..e6a2b9cc4199 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -210,7 +210,9 @@ struct qcow2_target {
bool truncate_error;
bool event_enospc;
- atomic_t service_qios;
+ /* Protects service_qios; held over the wake-up in service_qio_endio() */
+ spinlock_t service_lock;
+ unsigned int service_qios;
struct wait_queue_head service_wq;
struct list_head enospc_qios; /* Delayed after ENOSPC */
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target Denis V. Lunev
@ 2026-11-23 15:43 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:43 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> service_qio_endio() decrements tgt->service_qios and only then calls
> wake_up() on tgt->service_wq. Service qios hold no inflight reference,
> so the waiter in qcow2_service_iter() can see the counter reach zero
> in that gap and return: the backward merge ends, and a dmsetup remove
> frees the target. The wake-up from the image I/O completion then takes
> the freed service_wq lock.
>
> Protect the counter with a dedicated spinlock and hold it over the
> wake-up; the waiters check the counter under the same lock with
> wait_event_lock_irq(), so they cannot return before the completion
> drops it. service_wq.lock itself cannot serve, as the wait takes it
> inside. Count a service qio before dispatching it, so that its
> completion never runs ahead of the increment, and read the status
> under the lock as well.
>
> 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@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=4
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Devel] [PATCH hci-8.2 v2 5/8] dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (3 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:45 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 6/8] dm-qcow2: stop slow_wb_timer and flush the workers before freeing an image Denis V. Lunev
` (4 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
qcow2_flush_deferred_activity() runs the slow writeback timer callback
itself in each of its two passes, so that dirty R1/R2 pages are
written, but skips that when timer_delete_sync_try() finds the callback
running on another CPU. A callback skipped in the first pass is over by
the second, as the inflight reference switch in between waits for an
RCU grace period. The timer can however be rearmed by an R1/R2 page
that the first pass failed to write with ENOSPC, and expire just as the
second pass starts. That callback queues the worker after the last
flush_workqueue(), so the pages are written after the flush returns:
after suspend has cleared the dirty bit of the image, or, at backward
merge complete, on an image that is already freed.
Wait for the callback with timer_delete_sync() and then run it
unconditionally. Every caller can sleep and none holds md_pages_lock.
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@openvz.org>
---
drivers/md/dm-qcow2-target.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index f839b444b328..2a51beaebcab 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -177,8 +177,8 @@ void qcow2_flush_deferred_activity(struct qcow2_target *tgt, struct qcow2 *qcow2
/* Do not redirty R1/R2 failed with ENOSPC on the last pass */
for (i = 0; i < 2; i++) {
WRITE_ONCE(qcow2->md_wb_no_retry, i == 1);
- if (timer_delete_sync_try(&qcow2->slow_wb_timer) != -1)
- qcow2_slow_wb_timer_fn(&qcow2->slow_wb_timer);
+ timer_delete_sync(&qcow2->slow_wb_timer);
+ qcow2_slow_wb_timer_fn(&qcow2->slow_wb_timer);
/* Start md writeback */
flush_workqueue(tgt->wq);
/* Wait AIO of md wb */
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 5/8] dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 5/8] dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer Denis V. Lunev
@ 2026-11-23 15:45 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:45 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] dm-qcow2: Fixes tag names the driver import, not the
ENOSPC-retry commit
--
commit 70d283a3940a99a583c3b5dc1637d001646c9370
Author: Denis V. Lunev <den@openvz.org>
> dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer
>
> qcow2_flush_deferred_activity() runs the slow writeback timer callback
> itself in each of its two passes, so that dirty R1/R2 pages are
> written, but skips that when timer_delete_sync_try() finds the callback
> running on another CPU. A callback skipped in the first pass is over by
> the second, as the inflight reference switch in between waits for an
> RCU grace period. The timer can however be rearmed by an R1/R2 page
> that the first pass failed to write with ENOSPC, and expire just as the
> second pass starts. That callback queues the worker after the last
> flush_workqueue(), so the pages are written after the flush returns:
> after suspend has cleared the dirty bit of the image, or, at backward
> merge complete, on an image that is already freed.
>
> Wait for the callback with timer_delete_sync() and then run it
> unconditionally. Every caller can sleep and none holds md_pages_lock.
>
> 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@openvz.org>
[Severity: Low]
The race described in the message needs a timer rearm by an R1/R2 page that failed with ENOSPC, and that rearm comes from 8805ee1133a5 ("dm-qcow2: retry R1/R2 writeback that failed with ENOSPC"). That commit added the retry in clear_writeback_status() and the two-pass md_wb_no_retry logic in qcow2_flush_deferred_activity(). Without it, nothing rearms the timer between the two passes. Should the tag point there instead?
Fixes: 8805ee1133a5 ("dm-qcow2: retry R1/R2 writeback that failed with ENOSPC")
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=5
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Devel] [PATCH hci-8.2 v2 6/8] dm-qcow2: stop slow_wb_timer and flush the workers before freeing an image
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (4 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 5/8] dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:45 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 7/8] dm-qcow2: cancel the backward merge before destroying the workqueue Denis V. Lunev
` (3 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
qcow2_flush_deferred_activity() ends with an inflight reference switch
that waits for the metadata writeback it started. Those qios are ended
by the image's fsync_worker, and each completion queues that worker
before the qio is taken, so one run can end the last qio while another
run stays queued. qcow2_merge_backward_complete() then frees the merged
image with that work still pending, and it runs on freed memory.
Shut slow_wb_timer down and flush both workers of the merged image
before truncating and freeing it. The timer is not expected to be armed
after the final flush pass, which writes without retrying on ENOSPC,
but qcow2_destroy() only warns about a pending one. In
qcow2_tgt_destroy(), shut down the timer of every image before
destroy_workqueue() drains the remaining work, for the same reason.
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@openvz.org>
---
drivers/md/dm-qcow2-cmd.c | 3 +++
drivers/md/dm-qcow2-target.c | 3 +++
2 files changed, 6 insertions(+)
diff --git a/drivers/md/dm-qcow2-cmd.c b/drivers/md/dm-qcow2-cmd.c
index fb3d1e014046..783159b0161c 100644
--- a/drivers/md/dm-qcow2-cmd.c
+++ b/drivers/md/dm-qcow2-cmd.c
@@ -392,6 +392,9 @@ static int qcow2_merge_backward_complete(struct qcow2_target *tgt)
smp_wmb(); /* Pairs with qcow2_ref_inc() */
qcow2_inflight_ref_switch(tgt); /* Pending qios */
qcow2_flush_deferred_activity(tgt, qcow2); /* Delayed md pages */
+ timer_shutdown_sync(&qcow2->slow_wb_timer);
+ flush_work(&qcow2->worker);
+ flush_work(&qcow2->fsync_worker);
qcow2->lower = NULL;
if (qcow2_file_is_writable(qcow2)) {
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index 2a51beaebcab..d5e94753ddbb 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -252,6 +252,8 @@ static void qcow2_tgt_destroy(struct qcow2_target *tgt)
* since DM waits them. Complete our deferred:
*/
qcow2_flush_deferred_activity_all(tgt);
+ for (qcow2 = tgt->top; qcow2; qcow2 = qcow2->lower)
+ timer_shutdown_sync(&qcow2->slow_wb_timer);
/* Now kill the queue */
destroy_workqueue(tgt->wq);
}
@@ -264,6 +266,7 @@ static void qcow2_tgt_destroy(struct qcow2_target *tgt)
for (i = 0; i < 2; i++)
percpu_ref_exit(&tgt->inflight_ref[i]);
+ qcow2 = tgt->top;
while (qcow2) {
lower = qcow2->lower;
qcow2_destroy(qcow2);
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 6/8] dm-qcow2: stop slow_wb_timer and flush the workers before freeing an image
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 6/8] dm-qcow2: stop slow_wb_timer and flush the workers before freeing an image Denis V. Lunev
@ 2026-11-23 15:45 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:45 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> qcow2_flush_deferred_activity() ends with an inflight reference switch
> that waits for the metadata writeback it started. Those qios are ended
> by the image's fsync_worker, and each completion queues that worker
> before the qio is taken, so one run can end the last qio while another
> run stays queued. qcow2_merge_backward_complete() then frees the merged
> image with that work still pending, and it runs on freed memory.
>
> Shut slow_wb_timer down and flush both workers of the merged image
> before truncating and freeing it. The timer is not expected to be armed
> after the final flush pass, which writes without retrying on ENOSPC,
> but qcow2_destroy() only warns about a pending one. In
> qcow2_tgt_destroy(), shut down the timer of every image before
> destroy_workqueue() drains the remaining work, for the same reason.
>
> 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@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=6
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Devel] [PATCH hci-8.2 v2 7/8] dm-qcow2: cancel the backward merge before destroying the workqueue
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (5 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 6/8] dm-qcow2: stop slow_wb_timer and flush the workers before freeing an image Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:47 ` no-reply
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 8/8] dm-qcow2: flush the backward merge work before freeing the target Denis V. Lunev
` (2 subsequent siblings)
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
qcow2_tgt_destroy() cancels the backward merge only after it has
flushed the images and destroyed tgt->wq. A merge still running there
would dispatch its service qios to the destroyed workqueue and to
images whose deferred activity was already flushed. DM suspends a
target before destroying it, and presuspend already cancels the merge,
so this does not happen today; cancel it first anyway, so that the
teardown does not depend on that.
tgt->wq is never NULL here, since alloc_qcow2_target() fails without
it, so drop the check.
https://virtuozzo.atlassian.net/browse/VSTOR-156036
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-qcow2-target.c | 24 +++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index d5e94753ddbb..9c173e1dce87 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -241,24 +241,22 @@ void qcow2_destroy(struct qcow2 *qcow2)
static void qcow2_tgt_destroy(struct qcow2_target *tgt)
{
- struct qcow2 *lower, *qcow2 = tgt->top;
+ struct qcow2 *lower, *qcow2;
unsigned int i;
+ qcow2_merge_backward_cancel(tgt);
timer_shutdown_sync(&tgt->enospc_timer);
- if (tgt->wq) {
- /*
- * All activity from DM bios are already done,
- * since DM waits them. Complete our deferred:
- */
- qcow2_flush_deferred_activity_all(tgt);
- for (qcow2 = tgt->top; qcow2; qcow2 = qcow2->lower)
- timer_shutdown_sync(&qcow2->slow_wb_timer);
- /* Now kill the queue */
- destroy_workqueue(tgt->wq);
- }
+ /*
+ * All activity from DM bios are already done,
+ * since DM waits them. Complete our deferred:
+ */
+ qcow2_flush_deferred_activity_all(tgt);
+ for (qcow2 = tgt->top; qcow2; qcow2 = qcow2->lower)
+ timer_shutdown_sync(&qcow2->slow_wb_timer);
+ /* Now kill the queue */
+ destroy_workqueue(tgt->wq);
cancel_work_sync(&tgt->event_work);
- qcow2_merge_backward_cancel(tgt);
mempool_destroy(tgt->qio_pool);
mempool_destroy(tgt->qrq_pool);
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* [Devel] [PATCH hci-8.2 v2 8/8] dm-qcow2: flush the backward merge work before freeing the target
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (6 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 7/8] dm-qcow2: cancel the backward merge before destroying the workqueue Denis V. Lunev
@ 2026-11-23 15:36 ` Denis V. Lunev
2026-11-23 15:48 ` no-reply
2026-11-24 10:57 ` [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Alexander Ivanov
2026-11-30 16:14 ` Konstantin Khorenko
9 siblings, 1 reply; 19+ messages in thread
From: Denis V. Lunev @ 2026-11-23 15:36 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
qcow2_merge_backward_work() runs on the system workqueue and ends by
setting the merge state under ctl_mutex and unlocking it. A
qcow2_merge_backward_cancel() that sees BACKWARD_MERGE_STOPPED, or
turns BACKWARD_MERGE_WAIT_COMPLETION into it, returns without
flush_work(), so it can take ctl_mutex while the work is still inside
mutex_unlock(). qcow2_tgt_destroy() then frees the target, and the
mutex in it, under that unlock.
Flush the merge work in qcow2_tgt_destroy() after the cancel,
whatever state the merge is in.
https://virtuozzo.atlassian.net/browse/VSTOR-156036
Fixes: 7d2cf9a7c178 ("dm-qcow2: make merge_backward command asyncronous")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-qcow2-target.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index 9c173e1dce87..5a18738d5b89 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -245,6 +245,7 @@ static void qcow2_tgt_destroy(struct qcow2_target *tgt)
unsigned int i;
qcow2_merge_backward_cancel(tgt);
+ flush_work(&tgt->backward_merge.work);
timer_shutdown_sync(&tgt->enospc_timer);
/*
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 8/8] dm-qcow2: flush the backward merge work before freeing the target
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 8/8] dm-qcow2: flush the backward merge work before freeing the target Denis V. Lunev
@ 2026-11-23 15:48 ` no-reply
0 siblings, 0 replies; 19+ messages in thread
From: no-reply @ 2026-11-23 15:48 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> qcow2_merge_backward_work() runs on the system workqueue and ends by
> setting the merge state under ctl_mutex and unlocking it. A
> qcow2_merge_backward_cancel() that sees BACKWARD_MERGE_STOPPED, or
> turns BACKWARD_MERGE_WAIT_COMPLETION into it, returns without
> flush_work(), so it can take ctl_mutex while the work is still inside
> mutex_unlock(). qcow2_tgt_destroy() then frees the target, and the
> mutex in it, under that unlock.
>
> Flush the merge work in qcow2_tgt_destroy() after the cancel,
> whatever state the merge is in.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-156036
>
> Fixes: 7d2cf9a7c178 ("dm-qcow2: make merge_backward command asyncronous")
> Feature: dm-qcow2: block device over QCOW2 files driver
> Signed-off-by: Denis V. Lunev <den@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123153640.128227-1-den@openvz.org?part=8
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (7 preceding siblings ...)
2026-11-23 15:36 ` [Devel] [PATCH hci-8.2 v2 8/8] dm-qcow2: flush the backward merge work before freeing the target Denis V. Lunev
@ 2026-11-24 10:57 ` Alexander Ivanov
2026-11-30 16:14 ` Konstantin Khorenko
9 siblings, 0 replies; 19+ messages in thread
From: Alexander Ivanov @ 2026-11-24 10:57 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, vasileios.almpanis; +Cc: devel
LGTM
Reviewed-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target
2026-11-23 15:36 [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Denis V. Lunev
` (8 preceding siblings ...)
2026-11-24 10:57 ` [Devel] [PATCH hci-8.2 v2 0/8] dm-qcow2: fix image I/O completions using a freed target Alexander Ivanov
@ 2026-11-30 16:14 ` Konstantin Khorenko
9 siblings, 0 replies; 19+ messages in thread
From: Konstantin Khorenko @ 2026-11-30 16:14 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
Andrey, waiting for a review from you.
Thank you in advance.
--
Best regards,
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
On 11/23/26 16:36, Denis V. Lunev wrote:
> Suspend waits only for the inflight references, so a completion in the
> backing file system's context can still free a helper qio into the
> target's pool, queue work on its workqueue or wake its service waiters
> after a remove has destroyed them. The same holds at backward merge
> complete for the merged image, its timer and its workers. Make every
> completion tail end with the release of whatever keeps the target or
> image alive, and make the destructors wait for the timers and work
> items that are not covered by that.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-156036
>
> Tested in an HCI 8.2 VM with the races held open by hooks: stock oopses
> or corrupts slab in each, the series waits them out. The ploop test
> suite passes in qcow2 and ploop formats, and on the debug kernel with
> lockdep.
>
> qcow2_presuspend() still skips a re-armed enospc_timer, which delays a
> suspend by the ENOSPC timeout; that is left for a separate patch.
>
> v2: no new counter, as Andrey suggested: free a qio before its endio
> callback and reference drop, and keep queue_work() under
> deferred_lock. Also fix the service qio wake-up, the slow_wb_timer
> and worker teardown and the merge work in the destructor.
> v1: http://lore.virtuozzo.com/kernel/20261119114914.767536-1-den@openvz.org/
>
> Denis V. Lunev (8):
> dm-qcow2: drop the qio argument of endio callbacks
> dm-qcow2: fix do_qio_endio() freeing a qio into a destroyed pool
> dm-qcow2: fix queue_work() after the target or image is freed
> dm-qcow2: fix service_qio_endio() waking a freed target
> dm-qcow2: fix qcow2_flush_deferred_activity() skipping a running timer
> dm-qcow2: stop slow_wb_timer and flush the workers before freeing an
> image
> dm-qcow2: cancel the backward merge before destroying the workqueue
> dm-qcow2: flush the backward merge work before freeing the target
>
> drivers/md/dm-qcow2-cmd.c | 37 ++++++++++-------
> drivers/md/dm-qcow2-map.c | 80 ++++++++++++++++++++----------------
> drivers/md/dm-qcow2-target.c | 29 +++++++------
> drivers/md/dm-qcow2.h | 8 ++--
> 4 files changed, 86 insertions(+), 68 deletions(-)
>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 19+ messages in thread