Move wait queue declaration and unplug to dm_wait_for_completion. The purpose is to minimize duplicate code in the further patches. The patch reorders functions a little bit, but it doesn't matter, for proper non-deadlock operation, it is required that add_wait_queue happens before set_current_state(interruptible) and before the test for !atomic_read(&md->pending) Signed-off-by: Mikulas Patocka --- drivers/md/dm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) Index: linux-2.6.28-clean/drivers/md/dm.c =================================================================== --- linux-2.6.28-clean.orig/drivers/md/dm.c 2009-01-12 15:17:06.000000000 +0100 +++ linux-2.6.28-clean/drivers/md/dm.c 2009-01-12 15:17:11.000000000 +0100 @@ -1334,6 +1334,11 @@ EXPORT_SYMBOL_GPL(dm_put); static int dm_wait_for_completion(struct mapped_device *md) { int r = 0; + DECLARE_WAITQUEUE(wait, current); + + dm_unplug_all(md->queue); + + add_wait_queue(&md->wait, &wait); while (1) { set_current_state(TASK_INTERRUPTIBLE); @@ -1351,6 +1356,8 @@ static int dm_wait_for_completion(struct } set_current_state(TASK_RUNNING); + remove_wait_queue(&md->wait, &wait); + return r; } @@ -1460,7 +1467,6 @@ static void unlock_fs(struct mapped_devi int dm_suspend(struct mapped_device *md, unsigned suspend_flags) { struct dm_table *map = NULL; - DECLARE_WAITQUEUE(wait, current); int r = 0; int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; @@ -1510,20 +1516,14 @@ int dm_suspend(struct mapped_device *md, down_write(&md->io_lock); set_bit(DMF_BLOCK_IO, &md->flags); - add_wait_queue(&md->wait, &wait); up_write(&md->io_lock); - /* unplug */ - if (map) - dm_table_unplug_all(map); - /* * Wait for the already-mapped ios to complete. */ r = dm_wait_for_completion(md); down_write(&md->io_lock); - remove_wait_queue(&md->wait, &wait); if (noflush) __merge_pushback_list(md);