Skip to contentSkip to conversation

md: ensure md devices are freed before module is unloaded.

PATCH Applied NeilBrown 2 participants 2 messages Feb 6, 2017 MD RAID

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

PATCH 1/1 md: ensure md devices are freed before module is unloaded.

Commit: cbd199837750 ("md: Fix unfortunate interaction with evms") change mddev_put() so that it would not destroy an md device while ->ctime was non-zero.

Unfortunately, we didn't make sure to clear ->ctime when unloading the module, so it is possible for an md device to remain after module unload. An attempt to open such a device will trigger an invalid memory reference in:
get_gendisk -> kobj_lookup -> exact_lock -> get_disk

when tring to access disk->fops, which was in the module that has been removed.

So ensure we clear ->ctime in md_exit(), and explain how that is useful, as it isn't immediately obvious when looking at the code.

Fixes cbd199837750 ("md: Fix unfortunate interaction with evms")
---
 drivers/md/md.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/md/md.c +7 -0
index 01175dac0db6..8926fb781cdc 100644
@@ -8980,7 +8980,14 @@ static __exit void md_exit(void)
 
 	for_each_mddev(mddev, tmp) {
 		export_array(mddev);
+		mddev->ctime = 0;
 		mddev->hold_active = 0;
+		/* for_each_mddev() will call mddev_put() at the
+		 * end of each iteration.  As the mddev is now
+		 * fully clear, this will schedule the mddev for destruction
+		 * by a workqueue, and the destroy_workqueue() below
+		 * will wait for that to complete.
+		 */
 	}
 	destroy_workqueue(md_misc_wq);
 	destroy_workqueue(md_wq);
Signature · 1 line
2.11.0
Applied
#1 lore raw
Quoted 15 lines · Neil Brown

Commit: cbd199837750 ("md: Fix unfortunate interaction with evms") change mddev_put() so that it would not destroy an md device while ->ctime was non-zero.

Unfortunately, we didn't make sure to clear ->ctime when unloading the module, so it is possible for an md device to remain after module unload. An attempt to open such a device will trigger an invalid memory reference in:
get_gendisk -> kobj_lookup -> exact_lock -> get_disk

when tring to access disk->fops, which was in the module that has been removed.

Quoted 6 lines

So ensure we clear ->ctime in md_exit(), and explain how that is useful, as it isn't immediately obvious when looking at the code.

Fixes cbd199837750 ("md: Fix unfortunate interaction with evms")

Applied, thanks!

Quoted 26 lines
---
 drivers/md/md.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/md/md.c +7 -0
index 01175dac0db6..8926fb781cdc 100644
@@ -8980,7 +8980,14 @@ static __exit void md_exit(void)
 
 	for_each_mddev(mddev, tmp) {
 		export_array(mddev);
+		mddev->ctime = 0;
 		mddev->hold_active = 0;
+		/* for_each_mddev() will call mddev_put() at the
+		 * end of each iteration.  As the mddev is now
+		 * fully clear, this will schedule the mddev for destruction
+		 * by a workqueue, and the destroy_workqueue() below
+		 * will wait for that to complete.
+		 */
 	}
 	destroy_workqueue(md_misc_wq);
 	destroy_workqueue(md_wq);
Signature · 1 line
2.11.0