Message ID | 20230306121636.3183761-1-zhongjinghua@huaweicloud.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [PATCH-next,v2,Resend] scsi: fix use-after-free problem in scsi_remove_target | expand |
On 3/6/23 04:16, Zhong Jinghua wrote: > + /* > + * If the reference count is already zero, skip this > + * target is safe because scsi_target_destroy() > + * will wait until the host lock has been released > + * before freeing starget. > + */ The above comment has grammatical issues and is confusing. I think the comment that I suggested was much better than the above. Bart.
Thanks for you adevice. Jinghua 在 2023/3/8 6:35, Bart Van Assche 写道: > On 3/6/23 04:16, Zhong Jinghua wrote: >> + /* >> + * If the reference count is already zero, skip this >> + * target is safe because scsi_target_destroy() >> + * will wait until the host lock has been released >> + * before freeing starget. >> + */ > > The above comment has grammatical issues and is confusing. I think the > comment that I suggested was much better than the above. > > Bart. >
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index e7893835b99a..12e8ed6d55cb 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -1561,7 +1561,16 @@ void scsi_remove_target(struct device *dev) starget->state == STARGET_CREATED_REMOVE) continue; if (starget->dev.parent == dev || &starget->dev == dev) { - kref_get(&starget->reap_ref); + + /* + * If the reference count is already zero, skip this + * target is safe because scsi_target_destroy() + * will wait until the host lock has been released + * before freeing starget. + */ + if (!kref_get_unless_zero(&starget->reap_ref)) + continue; + if (starget->state == STARGET_CREATED) starget->state = STARGET_CREATED_REMOVE; else