From: Yongpeng Yang <[email protected]>

__dm_suspend() ends with set_bit(DMF_SUSPENDED) followed by
synchronize_srcu() before its caller runs the ->postsuspend methods,
so a target message that passed dm_suspended() has finished by then.
__dm_destroy() lacks that final synchronize_srcu(): a target that
implements ->message and relies on dm_suspended() for serialization
has its handler race its own ->postsuspend when an active device is
removed.  With the verity messages added later in this series:

CPU0                                  CPU1
target_message()                      dev_remove()
  dm_get_live_table()                   __dm_destroy()
  verity_message()                        dm_table_presuspend_targets()
    dm_suspended() -> false                set_bit(DMF_SUSPENDED)
    dm_bufio_prefetch()                   dm_table_postsuspend_targets()
      __bufio_new()                         verity_postsuspend()
        /* b->hold_count == 1 */              dm_bufio_client_reset()
      dm_bufio_unlock(c)                       drop_buffers()
                                                 /* b is held: WARN_ON */
      submit_io(b, ...)
      dm_bufio_release(b)

No in-tree target is exposed today: every one with messages
serializes them against its own ->postsuspend with a private lock
(multipath's work_mutex).  But the hole is in the core, and any new
message-bearing target that trusts dm_suspended() falls into it.

Mirror the suspend path: set the flag, drop the SRCU read lock (the
table stays pinned by md->map, which cannot change under the suspend
lock on a device being destroyed), wait for the grace period, then run
postsuspend.

Fixes: 3f77316de0ec0 ("dm: separate device deletion from dm_put")
Signed-off-by: Yongpeng Yang <[email protected]>
Assisted-by: LLM
---
 drivers/md/dm.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index 38eaae8ac9b4..921b8c85f10d 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -2736,6 +2736,7 @@ static void __dm_destroy(struct mapped_device *md, bool 
wait)
 {
        struct dm_table *map;
        int srcu_idx;
+       bool suspend_targets;
 
        might_sleep();
 
@@ -2752,14 +2753,21 @@ static void __dm_destroy(struct mapped_device *md, bool 
wait)
         */
        mutex_lock(&md->suspend_lock);
        map = dm_get_live_table(md, &srcu_idx);
-       if (!dm_suspended_md(md)) {
+       suspend_targets = !dm_suspended_md(md);
+       if (suspend_targets) {
                dm_table_presuspend_targets(map);
                set_bit(DMF_SUSPENDED, &md->flags);
                set_bit(DMF_POST_SUSPENDING, &md->flags);
-               dm_table_postsuspend_targets(map);
        }
        /* dm_put_live_table must be before fsleep, otherwise deadlock is 
possible */
        dm_put_live_table(md, srcu_idx);
+
+       /* Wait for in-flight target messages, like __dm_suspend() does. */
+       if (suspend_targets)
+               synchronize_srcu(&md->io_barrier);
+
+       if (suspend_targets)
+               dm_table_postsuspend_targets(map);
        mutex_unlock(&md->suspend_lock);
 
        /*
-- 
2.43.0


Reply via email to