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] dm-ploop: fix image I/O completions running on a freed ploop
Date: Wed, 18 Nov 2026 23:27:02 +0100 [thread overview]
Message-ID: <20261118222702.3878760-1-den@openvz.org> (raw)
From: Denis V. Lunev <den@openvz.org>
Nothing keeps the struct ploop alive while an image file I/O completion
runs. When that completion ends the last request, dm drops its md
reference, and suspend and ploop_destroy() wait for neither:
- a write split at a cluster boundary: the last split pio to complete
ends the parent request, then frees itself into ploop->pio_pool;
- a write that allocated a cluster: ploop_bat_write_finish() ends the
data pios, then ploop_put_piwb() decrements inflight_md and frees the
piwb pio into ploop->pio_pool.
Both run in the completion context of the backing file system, such as
the dio workqueue that converts unwritten extents, which ploop does not
own. A dmsetup remove in that window ends in a general protection fault
in mempool_free() on the destroyed pool.
Take a percpu reference in ploop_call_rw_iter() for every image file
I/O and drop it after the completion handler returns. ploop_destroy()
kills it once its threads are stopped and waits for the last completion
before it frees the pools and the struct ploop.
A completion can also wake the dispatcher after its request ended, e.g.
ploop_complete_cow() calls ploop_schedule_work() after failing the COW
pio, and the dispatcher is already stopped by then. Hold a reference on
each worker task from creation until after that wait, so the wakeup
never reaches a freed task_struct.
https://virtuozzo.atlassian.net/browse/VSTOR-155932
Fixes: 6ddad5a4d226 ("dm-ploop: Add ploop target driver")
Feature: dm-ploop: ploop target driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-ploop-target.c | 40 +++++++++++++++++++++++++++++++++---
drivers/md/dm-ploop.h | 2 ++
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 799b9d61dcdb..aefb69e8a40b 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -37,9 +37,12 @@ extern struct static_key_false ploop_standby_check;
static void ploop_aio_do_completion(struct pio *pio)
{
+ struct ploop *ploop = pio->ploop;
+
if (!atomic_dec_and_test(&pio->aio_ref))
return;
pio->complete(pio);
+ percpu_ref_put(&ploop->file_io_ref);
}
static void ploop_aio_complete(struct kiocb *iocb, long ret)
@@ -64,6 +67,7 @@ void ploop_call_rw_iter(struct file *file, loff_t pos, unsigned rw,
iocb->ki_complete = ploop_aio_complete;
iocb->ki_flags |= IOCB_ALLOC_CACHE;
+ percpu_ref_get(&pio->ploop->file_io_ref);
atomic_set(&pio->aio_ref, 2);
if (rw == WRITE)
@@ -123,6 +127,13 @@ static void ploop_inflight_bios_ref_exit1(struct percpu_ref *ref)
complete(&ploop->inflight_bios_ref_comp);
}
+static void ploop_file_io_ref_release(struct percpu_ref *ref)
+{
+ struct ploop *ploop = container_of(ref, struct ploop, file_io_ref);
+
+ complete(&ploop->file_io_comp);
+}
+
void ploop_free_md_pages_tree(struct rb_root *root)
{
struct rb_node *node;
@@ -159,6 +170,14 @@ static bool ploop_empty_htable(struct hlist_head head[])
return true;
}
+static void ploop_worker_free(struct ploop_worker *worker)
+{
+ if (!worker)
+ return;
+ put_task_struct(worker->task);
+ kfree(worker);
+}
+
static void ploop_destroy(struct ploop *ploop)
{
int i;
@@ -183,7 +202,6 @@ static void ploop_destroy(struct ploop *ploop)
if (ploop->kt_runners[i]) {
wake_up_process(ploop->kt_runners[i]->task);
kthread_stop(ploop->kt_runners[i]->task);
- kfree(ploop->kt_runners[i]);
}
}
}
@@ -194,14 +212,22 @@ static void ploop_destroy(struct ploop *ploop)
if (ploop->kt_allocator)
kthread_stop(ploop->kt_allocator->task);
+ percpu_ref_kill(&ploop->file_io_ref);
+ wait_for_completion(&ploop->file_io_comp);
+
WARN_ON(!llist_empty(&ploop->pios[PLOOP_LIST_PREPARE]));
WARN_ON(!llist_empty(&ploop->llresubmit_pios));
WARN_ON(!llist_empty(&ploop->enospc_pios));
+ if (ploop->kt_runners) {
+ for (i = 0; i < ploop->nkt_runners; i++)
+ ploop_worker_free(ploop->kt_runners[i]);
+ }
kfree(ploop->kt_runners);
- kfree(ploop->kt_worker);
- kfree(ploop->kt_allocator);
+ ploop_worker_free(ploop->kt_worker);
+ ploop_worker_free(ploop->kt_allocator);
}
+ percpu_ref_exit(&ploop->file_io_ref);
for (i = 0; i < 2; i++)
percpu_ref_exit(&ploop->inflight_bios_ref[i]);
/* Nobody uses it after destroy_workqueue() */
@@ -396,6 +422,7 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop,
if (IS_ERR(task))
goto out_err;
+ get_task_struct(task);
worker->task = task;
if (*pref == 'r')
worker->runner_id = id - 1;
@@ -517,6 +544,13 @@ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv)
}
}
+ init_completion(&ploop->file_io_comp);
+ if (percpu_ref_init(&ploop->file_io_ref, ploop_file_io_ref_release,
+ 0, GFP_KERNEL)) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
ti->private = ploop;
ploop->ti = ti;
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 5ae02ef5ffc2..093b7f9397a4 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -220,6 +220,8 @@ struct ploop {
struct percpu_ref inflight_bios_ref[2];
bool inflight_ref_comp_pending;
unsigned int inflight_bios_ref_index:1;
+ struct percpu_ref file_io_ref;
+ struct completion file_io_comp;
struct llist_head llsuspended_pios;
bool stop_submitting_pios;
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next reply other threads:[~2026-11-18 22:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 22:27 Denis V. Lunev [this message]
2026-11-18 22:32 ` no-reply
2026-11-19 8:15 ` Alexander Ivanov
2026-11-19 16:23 ` Konstantin Khorenko
2026-11-19 16:24 ` 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=20261118222702.3878760-1-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.