OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
To: alexander.ivanov@virtuozzo.com, vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 1/2] dm-qcow2: fix image I/O completions running on a freed target
Date: Thu, 19 Nov 2026 12:49:13 +0100	[thread overview]
Message-ID: <20261119114914.767536-2-den@openvz.org> (raw)
In-Reply-To: <20261119114914.767536-1-den@openvz.org>

From: Denis V. Lunev <den@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@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

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  reply	other threads:[~2026-11-19 11:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 11:49 [Devel] [PATCH hci-8.2 0/2] dm-qcow2: fix image I/O completions using " Denis V. Lunev
2026-11-19 11:49 ` Denis V. Lunev [this message]
2026-11-19 11:49 ` [Devel] [PATCH hci-8.2 2/2] dm-qcow2: queue the worker before dropping deferred_lock in completions Denis V. Lunev
2026-11-19 12:53 ` [Devel] [PATCH hci-8.2 0/2] dm-qcow2: fix image I/O completions using a freed target Alexander Ivanov
2026-11-19 13:12 ` Andrey Zhadchenko
2026-11-19 14:50   ` Denis V. Lunev
2026-11-20  2:22     ` Andrey Zhadchenko

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=20261119114914.767536-2-den@openvz.org \
    --to=den@openvz.org \
    --cc=alexander.ivanov@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