Skip to content

Commit b014089

Browse files
committed
md: Fix race when creating a new md device.
There is a race when creating an md device by opening /dev/mdXX. If two processes do this at much the same time they will follow the call path __blkdev_get -> get_gendisk -> kobj_lookup The first will call -> md_probe -> md_alloc -> add_disk -> blk_register_region and the race happens when the second gets to kobj_lookup after add_disk has called blk_register_region but before it returns to md_alloc. In the case the second will not call md_probe (as the probe is already done) but will get a handle on the gendisk, return to __blkdev_get which will then call md_open (via the ->open) pointer. As mddev->gendisk hasn't been set yet, md_open will think something is wrong an return with ERESTARTSYS. This can loop endlessly while the first thread makes no progress through add_disk. Nothing is blocking it, but due to scheduler behaviour it doesn't get a turn. So this is essentially a live-lock. We fix this by simply moving the assignment to mddev->gendisk before the call the add_disk() so md_open doesn't get confused. Also move blk_queue_flush earlier because add_disk should be as late as possible. To make sure that md_open doesn't complete until md_alloc has done all that is needed, we take mddev->open_mutex during the last part of md_alloc. md_open will wait for this. This can cause a lock-up on boot so Cc:ing for stable. For 2.6.36 and earlier a different patch will be needed as the 'blk_queue_flush' call isn't there. Signed-off-by: NeilBrown <neilb@suse.de> Reported-by: Thomas Jarosch <thomas.jarosch@intra2net.com> Tested-by: Thomas Jarosch <thomas.jarosch@intra2net.com> Cc: stable@kernel.org
1 parent 693d92a commit b014089

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎drivers/md/md.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4347,13 +4347,19 @@ static int md_alloc(dev_t dev, char *name)
43474347
disk->fops = &md_fops;
43484348
disk->private_data = mddev;
43494349
disk->queue = mddev->queue;
4350+
blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
43504351
/* Allow extended partitions. This makes the
43514352
* 'mdp' device redundant, but we can't really
43524353
* remove it now.
43534354
*/
43544355
disk->flags |= GENHD_FL_EXT_DEVT;
4355-
add_disk(disk);
43564356
mddev->gendisk = disk;
4357+
/* As soon as we call add_disk(), another thread could get
4358+
* through to md_open, so make sure it doesn't get too far
4359+
*/
4360+
mutex_lock(&mddev->open_mutex);
4361+
add_disk(disk);
4362+
43574363
error = kobject_init_and_add(&mddev->kobj, &md_ktype,
43584364
&disk_to_dev(disk)->kobj, "%s", "md");
43594365
if (error) {
@@ -4367,8 +4373,7 @@ static int md_alloc(dev_t dev, char *name)
43674373
if (mddev->kobj.sd &&
43684374
sysfs_create_group(&mddev->kobj, &md_bitmap_group))
43694375
printk(KERN_DEBUG "pointless warning\n");
4370-
4371-
blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4376+
mutex_unlock(&mddev->open_mutex);
43724377
abort:
43734378
mutex_unlock(&disks_mutex);
43744379
if (!error && mddev->kobj.sd) {

0 commit comments

Comments
 (0)