OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
To: andrey.zhadchenko@virtuozzo.com, alexander.ivanov@virtuozzo.com,
	vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed
Date: Mon, 23 Nov 2026 16:36:35 +0100	[thread overview]
Message-ID: <20261123153640.128227-4-den@openvz.org> (raw)
In-Reply-To: <20261123153640.128227-1-den@openvz.org>

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

  parent reply	other threads:[~2026-11-23 15:38 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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: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
2026-11-23 15:41   ` no-reply
2026-11-23 15:36 ` Denis V. Lunev [this message]
2026-11-23 15:42   ` [Devel] [PATCH hci-8.2 v2 3/8] dm-qcow2: fix queue_work() after the target or image is freed 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
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
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
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
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
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123153640.128227-4-den@openvz.org \
    --to=den@openvz.org \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=vasileios.almpanis@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox