Skip to content

Commit 1c7ce4b

Browse files
hreineckemartinkpetersen
authored andcommitted
scsi: ch: synchronize ch_probe() and ch_open()
The 'ch' device node is created before the configuration is being read in, which leads to a race window when ch_open() is called before that. To avoid any races we should be taking the device mutex during ch_readconfig() and ch_init_elem(), and also during ch_open(). That ensures ch_probe is finished before ch_open() completes. Link: https://lore.kernel.org/r/20200213153207.123357-3-hare@suse.de Reviewed-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 6616728 commit 1c7ce4b

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎drivers/scsi/ch.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,10 @@ ch_open(struct inode *inode, struct file *file)
606606
mutex_unlock(&ch_mutex);
607607
return -ENXIO;
608608
}
609+
/* Synchronize with ch_probe() */
610+
mutex_lock(&ch->lock);
609611
file->private_data = ch;
612+
mutex_unlock(&ch->lock);
610613
mutex_unlock(&ch_mutex);
611614
return 0;
612615
}
@@ -949,6 +952,9 @@ static int ch_probe(struct device *dev)
949952
goto remove_idr;
950953
}
951954

955+
mutex_init(&ch->lock);
956+
kref_init(&ch->ref);
957+
ch->device = sd;
952958
class_dev = device_create(ch_sysfs_class, dev,
953959
MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
954960
"s%s", ch->name);
@@ -959,15 +965,16 @@ static int ch_probe(struct device *dev)
959965
goto put_device;
960966
}
961967

962-
mutex_init(&ch->lock);
963-
kref_init(&ch->ref);
964-
ch->device = sd;
968+
mutex_lock(&ch->lock);
965969
ret = ch_readconfig(ch);
966-
if (ret)
970+
if (ret) {
971+
mutex_unlock(&ch->lock);
967972
goto destroy_dev;
973+
}
968974
if (init)
969975
ch_init_elem(ch);
970976

977+
mutex_unlock(&ch->lock);
971978
dev_set_drvdata(dev, ch);
972979
sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
973980

0 commit comments

Comments
 (0)