OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2] dm-ploop: fix image I/O completions running on a freed ploop
@ 2026-11-18 22:27 Denis V. Lunev
  2026-11-18 22:32 ` no-reply
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-18 22:27 UTC (permalink / raw)
  To: alexander.ivanov, vasileios.almpanis; +Cc: devel

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-19 16:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 22:27 [Devel] [PATCH hci-8.2] dm-ploop: fix image I/O completions running on a freed ploop Denis V. Lunev
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox