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 4/8] dm-qcow2: fix service_qio_endio() waking a freed target
Date: Mon, 23 Nov 2026 16:36:36 +0100 [thread overview]
Message-ID: <20261123153640.128227-5-den@openvz.org> (raw)
In-Reply-To: <20261123153640.128227-1-den@openvz.org>
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
next prev 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 " 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 ` [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
2026-11-23 15:36 ` Denis V. Lunev [this message]
2026-11-23 15:43 ` [Devel] [PATCH hci-8.2 v2 4/8] dm-qcow2: fix service_qio_endio() waking a freed target 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-5-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