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

no-reply at sashiko.virtuozzo.com no-reply at sashiko.virtuozzo.com
Wed Nov 18 16:24:00 MSK 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm-ploop: inflight_md drain in ploop_destroy() runs after
  kthread_stop()
--

commit 8139d701139f09d811617b4ccc06b25fb85ca925
Author: Alexander Ivanov <alexander.ivanov at virtuozzo.com>

dm-ploop: fix use-after-free on the last inflight_md decrement

The patch frees the piwb first, then decrements inflight_md and wakes waiters under inflight_md_wq.lock as the last access to the ploop. ploop_destroy() now waits for inflight_md to reach zero and takes the same lock before freeing anything.

> 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);
> +

[Severity: Medium]
Is this wait too late? It runs after kthread_stop() has already stopped and released the runners, kt_worker and kt_allocator. A BAT write that is still in flight can complete during this wait_event(). ploop_put_piwb() then calls ploop_bat_write_finish(), which reaches ploop_dispatch_pios() or ploop_complete_cow() and ends up in ploop_schedule_work(). That calls wake_up_process(ploop->kt_worker->task) on a task that has already exited, so it can be a use-after-free of the task_struct. Any pios queued this way are never processed, because the threads are gone.

This is reachable when ploop_postsuspend() skipped its own inflight_md wait because the device is in standby (the !ploop_is_standby() check), and dtr then runs with BAT writes still in flight. The patch protects the lifetime of the ploop, the pio_pool and the waitqueue, but not that of the stopped kthreads.

Would it be better to drain inflight_md before the kthread_stop() calls? The worker would still have to be able to process the pios dispatched by those completions.

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118131032.3372996-1-alexander.ivanov@virtuozzo.com?part=6



More information about the Devel mailing list