From 570b9d968bf9b16974252ef7cbce73fa6dac34f3 Mon Sep 17 00:00:00 2001 From: Alasdair G Kergon Date: Thu, 2 Apr 2009 19:55:28 +0100 Subject: [RHEL5.7 PATCH] dm table: fix upgrade mode race Upstream commit 570b9d968bf9b16974252ef7cbce73fa6dac34f3 Author: Alasdair G Kergon Date: Thu Apr 2 19:55:28 2009 +0100 dm table: fix upgrade mode race 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. Cc: stable@kernel.org Cc: Neil Brown Signed-off-by: Alasdair G Kergon --- drivers/md/dm-table.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) Index: linux-rhel5/drivers/md/dm-table.c =================================================================== --- linux-rhel5.orig/drivers/md/dm-table.c +++ linux-rhel5/drivers/md/dm-table.c @@ -440,27 +440,29 @@ 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 *dd, int new_mode, struct mapped_device *md) { int r; - struct dm_dev dd_copy; - dev_t dev = dd->bdev->bd_dev; + struct dm_dev dd_new, dd_old; - dd_copy = *dd; + dd_new = dd_old = *dd; + + dd_new.mode |= new_mode; + dd_new.bdev = NULL; + + r = open_dev(&dd_new, dd->bdev->bd_dev, md); + if (r) + return r; dd->mode |= new_mode; - dd->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; } /*