Skip to contentSkip to conversation

dm: fix possible race in dm_start_io_acct

PATCH In review Benjamin Marzinski 2 participants 2 messages Jun 14, 2022 – Jun 15, 2022 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH 1/1 dm: fix possible race in dm_start_io_acct

After commit 82f6cdcc3676c ("dm: switch dm_io booleans over to proper flags") dm_start_io_acct stopped atomically checking and setting was_accounted, which turned into the DM_IO_ACCOUNTED flag. This opened the possibility for a race where IO accounting is started twice for duplicate bios. To remove the race, check the flag while holding the io->lock.

Fixes 82f6cdcc3676c ("dm: switch dm_io booleans over to proper flags")
---
 drivers/md/dm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/md/dm.c +7 -4
index d8f16183bf27..c7d2dbf03ccb 100644
@@ -544,17 +544,20 @@ static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
 {
 	/*
 	 * Ensure IO accounting is only ever started once.
+	 * Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO.
 	 */
-	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
-		return;
-
-	/* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */
 	if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) {
+		if (WARN_ON_ONCE(dm_io_flagged(io, DM_IO_ACCOUNTED)))
+			return;
 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
 	} else {
 		unsigned long flags;
 		/* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */
 		spin_lock_irqsave(&io->lock, flags);
+		if (dm_io_flagged(io, DM_IO_ACCOUNTED)) {
+			spin_unlock_irqrestore(&io->lock, flags);
+			return;
+		}
 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
 		spin_unlock_irqrestore(&io->lock, flags);
 	}
Signature · 6 lines
2.30.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
maintainer
#1 lore raw
Quoted 18 lines · Benjamin Marzinski

After commit 82f6cdcc3676c ("dm: switch dm_io booleans over to proper flags") dm_start_io_acct stopped atomically checking and setting was_accounted, which turned into the DM_IO_ACCOUNTED flag. This opened the possibility for a race where IO accounting is started twice for duplicate bios. To remove the race, check the flag while holding the io->lock.

Fixes 82f6cdcc3676c ("dm: switch dm_io booleans over to proper flags")
---
 drivers/md/dm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/md/dm.c +0 -0
index d8f16183bf27..c7d2dbf03ccb 100644

drivers/md/dm.c static void dm_start_io_acct(struct dm_io *io, struct bio *clone) · line 550

Quoted 8 lines
drivers/md/dm.c +1 -3
@@ -544,17 +544,20 @@ static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
 {
 	/*
 	 * Ensure IO accounting is only ever started once.
+	 * Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO.
 	 */
-	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
-		return;
-

This check ^ is best left at the top since it avoids needless locking in the DM_TIO_IS_DUPLICATE_BIO case.

drivers/md/dm.c static void dm_start_io_acct(struct dm_io *io, struct bio *clone) · line 551

Quoted 4 lines
drivers/md/dm.c +2 -1
-	/* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */
 	if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) {
+		if (WARN_ON_ONCE(dm_io_flagged(io, DM_IO_ACCOUNTED)))
+			return;

So this can be dropped ^

drivers/md/dm.c static void dm_start_io_acct(struct dm_io *io, struct bio *clone) · line 560

Quoted 9 lines
drivers/md/dm.c +4 -0
 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
 	} else {
 		unsigned long flags;
 		/* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */
 		spin_lock_irqsave(&io->lock, flags);
+		if (dm_io_flagged(io, DM_IO_ACCOUNTED)) {
+			spin_unlock_irqrestore(&io->lock, flags);
+			return;
+		}

Leaving only this part ^ as the necessary change to fix the race.

drivers/md/dm.c static void dm_start_io_acct(struct dm_io *io, struct bio *clone) · line 563

Quoted 3 lines
drivers/md/dm.c +0 -0
 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
 		spin_unlock_irqrestore(&io->lock, flags);
 	}

Now applied and staged in linux-next for v5.19-rc3 (or later). I won't rebase so this commit will remain valid:
https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git/commit/?h=dm-5.19&id=10eb3a0d517fcc83eeea4242c149461205675eb4

Mike

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel