[Devel] [PATCH hci-8.2 v10 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement

Alexander Ivanov alexander.ivanov at virtuozzo.com
Thu Nov 19 18:15:44 MSK 2026


The last piwb reference is often dropped from the BAT write completion
in aio context. ploop_bat_write_finish() completes the data pios there,
and once the last original request is done, suspend can finish and a
table reload or remove can free the ploop in ploop_destroy(). The
completing CPU still decrements inflight_md, calls
wake_up_interruptible() on inflight_md_wq and frees the piwb into
ploop->pio_pool, so it can lock a freed waitqueue or free into a
destroyed mempool.

Nothing on the suspend or destroy path waits for inflight_md, and the
one waiter that exists, ploop_suspend_submitting_pios(), checks the
counter without a lock. It can see zero and return before the wakeup and
ploop_free_piwb() are done with the ploop.

Free the piwb first, then decrement inflight_md and wake the waiters
with wake_up_locked() under inflight_md_wq.lock as the last access to
the ploop. Unlike wake_up_interruptible(), it wakes the uninterruptible
wait in ploop_destroy(). In ploop_destroy(), wait for inflight_md to
drop to zero and then take the same lock before freeing anything, so the
waker is done with the ploop.

https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 9dff9fe86d29 ("dm-ploop: rework bat completion logic")
Fixes: d1a1e5108aeb ("dm-ploop: add inflight metadata writeback counter")
Feature: dm-ploop: ploop target driver

Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 drivers/md/dm-ploop-map.c    | 21 +++++++++++++++++----
 drivers/md/dm-ploop-target.c |  7 +++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 1df31bbc5410..2f85383c4682 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -924,9 +924,24 @@ static void ploop_free_piwb(struct ploop_index_wb *piwb)
 
 static void ploop_bat_write_finish(struct pio *pio, void *piwb_ptr,
 				     blk_status_t bi_status);
+/*
+ * ploop_destroy() takes inflight_md_wq.lock before freeing the ploop, so it
+ * stays valid while we hold the lock. This must be the last access to it.
+ */
+static void ploop_dec_inflight_md(struct ploop *ploop)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&ploop->inflight_md_wq.lock, flags);
+	if (atomic_dec_and_test(&ploop->inflight_md))
+		wake_up_locked(&ploop->inflight_md_wq);
+	spin_unlock_irqrestore(&ploop->inflight_md_wq.lock, flags);
+}
+
 static void ploop_put_piwb(struct ploop_index_wb *piwb)
 {
 	if (atomic_dec_and_test(&piwb->count)) {
+		struct ploop *ploop = piwb->ploop;
 
 		ploop_bat_write_finish(piwb->pio, piwb, piwb->bi_status);
 
@@ -940,9 +955,8 @@ static void ploop_put_piwb(struct ploop_index_wb *piwb)
 		 * zero keeps it as is
 		 */
 
-		if (atomic_dec_and_test(&piwb->ploop->inflight_md))
-			wake_up_interruptible(&piwb->ploop->inflight_md_wq);
 		ploop_free_piwb(piwb);
+		ploop_dec_inflight_md(ploop);
 	}
 }
 
@@ -1113,9 +1127,8 @@ void ploop_break_bat_update(struct ploop *ploop, struct md_page *md,
 	spin_unlock(&md->md_lock);
 	spin_unlock_irqrestore(&ploop->bat_lock, flags);
 
-	if (atomic_dec_and_test(&ploop->inflight_md))
-		wake_up_interruptible(&ploop->inflight_md_wq);
 	ploop_free_piwb(piwb);
+	ploop_dec_inflight_md(ploop);
 }
 
 static void ploop_bat_page_zero_cluster(struct ploop *ploop,
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 799b9d61dcdb..3b5f37a58bb1 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -194,6 +194,13 @@ static void ploop_destroy(struct ploop *ploop)
 		if (ploop->kt_allocator)
 			kthread_stop(ploop->kt_allocator->task);
 
+		/* BAT writes may still be completing from aio context */
+		wait_event(ploop->inflight_md_wq,
+			   !atomic_read(&ploop->inflight_md));
+		/* Wait for ploop_dec_inflight_md() to drop the lock */
+		spin_lock_irq(&ploop->inflight_md_wq.lock);
+		spin_unlock_irq(&ploop->inflight_md_wq.lock);
+
 		WARN_ON(!llist_empty(&ploop->pios[PLOOP_LIST_PREPARE]));
 		WARN_ON(!llist_empty(&ploop->llresubmit_pios));
 		WARN_ON(!llist_empty(&ploop->enospc_pios));
-- 
2.53.0



More information about the Devel mailing list