Re: [PATCH] scsi: sd: call device_del() if device_add_disk() fails

Fabio M. De Francesco
2022-03-31T09:07:52Z

On giovedì? 31 marzo 2022 07:45:12 CEST Christoph Hellwig wrote:
> > The temptation was to call device_unregister() which is a combined
> > device_del(); device_put(); but when the device_initialize() and
> > device_add() are called separately, then I think it is more readable to
> > call del and put separately as well.
>
> I think we should also consolidate the initialization side. Using
> device_register and device_unregister would have prevented this bug
> and I should have switched to that before refactoring the code.
>
If I don't misunderstand what you wrote, I think you mean something like
the following changes:

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a390679cf458..7a000a9a9dbe 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3431,7 +3431,7 @@ static int sd_probe(struct device *dev)
sdkp->disk_dev.class = &sd_disk_class;
dev_set_name(&sdkp->disk_dev, "%s", dev_name(dev));

- error = device_add(&sdkp->disk_dev);
+ error = device_register(&sdkp->disk_dev);
if (error) {
put_device(&sdkp->disk_dev);
goto out;
@@ -3474,7 +3474,7 @@ static int sd_probe(struct device *dev)

error = device_add_disk(dev, gd, NULL);
if (error) {
- put_device(&sdkp->disk_dev);
+ device_unregister(&sdkp->disk_dev);
goto out;
}

@Dan, @Christoph: what do you think of the changes that I've copy-pasted above?

Thanks,

Fabio M. De Francesco