[PATCH v5.4] dm: interlock pending dm_io and dm_wait_for_bios_completion This is backport of the patch 9f6dc6337610 ("dm: interlock pending dm_io and dm_wait_for_bios_completion") for the kernel 5.4. The bugs fixed by this patch can cause random crashing when reloading dm table, so it is eligible for stable backport. Note that the percpu variable md->pending_io is not needed in the stable kernels, because the "in_flight" counter in struct disk_stats counts the same value, so it is not backported. In order to fix this bug, we swap the calls to generic_end_io_acct and dm_stats_account_io. Signed-off-by: Mikulas Patocka Signed-off-by: Mike Snitzer --- drivers/md/dm.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) Index: linux-stable/drivers/md/dm.c =================================================================== --- linux-stable.orig/drivers/md/dm.c 2022-04-30 19:57:10.000000000 +0200 +++ linux-stable/drivers/md/dm.c 2022-04-30 19:58:57.000000000 +0200 @@ -681,14 +681,16 @@ static void end_io_acct(struct mapped_de { unsigned long duration = jiffies - start_time; - generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0, - start_time); - if (unlikely(dm_stats_used(&md->stats))) dm_stats_account_io(&md->stats, bio_data_dir(bio), bio->bi_iter.bi_sector, bio_sectors(bio), true, duration, stats_aux); + smp_wmb(); + + generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0, + start_time); + /* nudge anyone waiting on suspend queue */ if (unlikely(wq_has_sleeper(&md->wait))) wake_up(&md->wait); @@ -2494,6 +2496,8 @@ static int dm_wait_for_completion(struct } finish_wait(&md->wait, &wait); + smp_rmb(); + return r; }