Skip to contentSkip to conversation

BUG/PATCH race between upgrade_mode and dm_table_any_congested

PATCH Neil Brown 2 participants 2 messages Mar 23, 2009 – Mar 27, 2009 Device mapper

lore
Share & tools
lore

Neil Brown

author
#0 lore raw

Hi,
A customer recently reported an Oops in dm_table_any_congested (in a 2.6.16 based kernel) that was due to dd->bdev being NULL. so bdev_get_queue dereferenced that NULL and caused the oops.

The only credible explanation for this that we can find is that upgrade_mode sets bdev to NULL temporarily, and does not have any locking to exclude anything from seeing that NULL.

The code in current mainline is exactly the same so if we are correct in our assessment, then the bug is still present.

The Oops has only occurred once and cannot be reproduced so we cannot be certain that this is the cause. However if it really is a bug - and there is not something else which causes mutual exclusion of these two routines, then it should probably be fixed.

Our current patch is below. It is a big ugly, and a better fix might be a more thorough rewrite of the code. However I offer it incase it is useful.

Thanks,
NeilBrown

---
 drivers/md/dm-table.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/md/dm-table.c +7 -7
Index: linux-2.6.16-SLES10_SP2_BRANCH/drivers/md/dm-table.c
===================================================================
@@ -414,14 +414,14 @@ static int upgrade_mode(struct dm_dev *d
 
 	dd_copy = *dd;
 
-	dd->mode |= new_mode;
-	dd->bdev = NULL;
-	r = open_dev(dd, dev);
-	if (!r)
-		close_dev(&dd_copy);
-	else
+	dd_copy.mode |= new_mode;
+	dd_copy.bdev = NULL;
+	r = open_dev(&dd_copy, dev);
+	if (!r) {
+		struct dm_dev dd_copy2 = *dd;
 		*dd = dd_copy;
-
+		close_dev(&dd_copy2);
+	}
 	return r;
 }
 
#1 lore raw
Quoted 6 lines · Neil Brown

The code in current mainline is exactly the same so if we are correct in our assessment, then the bug is still present.

Our current patch is below. It is a big ugly, and a better fix might be a more thorough rewrite of the code. However I offer it incase it is useful.

Indeed this area of code is in need of a clean up.

Here's an alternative version trying additionally to avoid the theoretical risk from overwriting the field with the same value.

Alasdair

From: Alasdair G Kergon <[email protected]>

upgrade_mode() sets bdev to NULL temporarily, and does not have any locking to exclude anything from seeing that NULL.

In dm_table_any_congested() bdev_get_queue() can dereference that NULL and cause a reported oops.

Fix this by not changing that field during the mode upgrade.

---
 drivers/md/dm-table.c |   26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/md/dm-table.c +14 -12
Index: linux-2.6.29/drivers/md/dm-table.c
===================================================================
@@ -399,28 +399,30 @@ static int check_device_area(struct dm_d
 }
 
 /*
- * This upgrades the mode on an already open dm_dev.  Being
+ * This upgrades the mode on an already open dm_dev, being
  * careful to leave things as they were if we fail to reopen the
- * device.
+ * device and not to touch the existing bdev field in case
+ * it is accessed concurrently inside dm_table_any_congested().
  */
 static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
 			struct mapped_device *md)
 {
 	int r;
-	struct dm_dev_internal dd_copy;
-	dev_t dev = dd->dm_dev.bdev->bd_dev;
+	struct dm_dev_internal dd_new, dd_old;
 
-	dd_copy = *dd;
+	dd_new = dd_old = *dd;
+
+	dd_new.dm_dev.mode |= new_mode;
+	dd_new.dm_dev.bdev = NULL;
+
+	r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
+	if (r)
+		return r;
 
 	dd->dm_dev.mode |= new_mode;
-	dd->dm_dev.bdev = NULL;
-	r = open_dev(dd, dev, md);
-	if (!r)
-		close_dev(&dd_copy, md);
-	else
-		*dd = dd_copy;
+	close_dev(&dd_old, md);
 
-	return r;
+	return 0;
 }
 
 /*