Re: [PATCH-next v2 2/2] scsi: fix iscsi rescan fails to create block device

From: Greg KH
Date: Sun Jan 29 2023 - 01:46:40 EST


On Sun, Jan 29, 2023 at 09:13:55AM +0800, Yu Kuai wrote:
> Hi, Greg
>
> 在 2023/01/28 18:45, Greg KH 写道:
> > > diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
> > > index cac7c902cf70..a22109cdb8ef 100644
> > > --- a/drivers/scsi/scsi_sysfs.c
> > > +++ b/drivers/scsi/scsi_sysfs.c
> > > @@ -1535,9 +1535,7 @@ static void __scsi_remove_target(struct scsi_target *starget)
> > > if (sdev->channel != starget->channel ||
> > > sdev->id != starget->id)
> > > continue;
> > > - if (sdev->sdev_state == SDEV_DEL ||
> > > - sdev->sdev_state == SDEV_CANCEL ||
> > > - !get_device(&sdev->sdev_gendev))
> > > + if (!get_device_unless_zero(&sdev->sdev_gendev))
> >
> > If sdev_gendev is 0 here, the object is gone and you are working with
> > memory that is already freed so something is _VERY_ wrong.
>
> In fact, this patch will work:
>
> In __scsi_remove_target(), 'host_lock' is held to protect iterating
> siblings, and object will wait for this lock in
> scsi_device_dev_release() to remove siblings. Hence sdev will not be
> freed untill the lock is released.

Then you got lucky, as that is not how a reference counted object should
be working (i.e. the reference dropped to 0 and it still be kept alive.)

Please fix up the scsi logic here, don't abuse the reference count code.

thanks,

greg k-h