SCSI : scsi_device_lookup/scsi_device_lookup_by_target return NULL for an existent scsi_device
James,
I am facing issues with device removal being done when there
are commands outstanding in the LLD. As explained in my original post, its resulting in effects ranging from the duplicate kobject warnings to the inability of the scsi subsystem to find a valid device (all symptoms are related to the fact that two scsi devices with the same HBTL but in diff states, SDEV_DEL and SDEV_RUNNING, are in the lists).
After extensive audit of the code I'm quite certain that the aforementioned
patch is the right thing to do. I'll greatly appreciate if you can highlight reason(s) why that might not be a good idea. Pls apply o/w.
Thanx,
Tomar
Quoted 190 lines · --- Nagendra Tomar
Hello James,
My understanding is that thescsi_deviceinSDEV_DELstate
is there in the scsi_host->devices/scsi_target->devices queue, just because there is some outstanding command holding a reference to it.It will need the device when it completes. Apart from this, for all practical purposes the
scsi_deviceis gone from the system. It could as well be removed from scsi_host->devices/scsi_target->devices lists and be put in some other list, just to hold thescsi_devicetill commands refering to it are completed.The scanning code should not consider these devices to be present in the system. This is correctly handled today, as scsi_device_lookup/scsi_device_lookup_by_target return NULL if there is an
SDEV_DELdevice in the list.Since a
scsi_deviceinSDEV_DELstate is "gone" from the system we should not hold any fresh references on to this device. The current scsi_device_lookup/scsi_device_lookup_by_target implementation rightly ensures that. And since all the sysfs linkages are also removed (in__scsi_remove_device->device_del), user space can also not reference this device. All is good till now.The problem happens when we try to add a new
scsi_devicewith the same HBTL. Since we consider the device "gone" (as described above) we should allow a newscsi_devicewith the same HBTL to be added (to the scsi_host->devices/scsi_target->devices list). This part is also correctly implemented. scsi_add_device->...->scsi_probe_and_add_lun->scsi_device_lookup_by_target will _not_ return the device present in theSDEV_DELstate and hence the scanning will go ahead and try to add a newscsi_device(this one inSDEV_RUNNINGstate) to the devices lists.All is good even till now.
The PROBLEM is, now any
scsi_device_lookupcall trying to lookup this newly addedscsi_device, fails. This is because,scsi_device_lookup->__scsi_device_lookupreturns the first device that it finds in the list, which in this case is the one in theSDEV_DELstate. Now thescsi_device_getcall thatscsi_device_lookupmakes to get a reference on that device returns ENXIO as the device is inSDEV_DELstate, resulting inscsi_device_lookupto return NULL.What
scsi_device_getdoes, is right, as we do not want to hold fresh references onscsi_devicesinSDEV_DELstate. The problem is because of this we fail to lookup the perfectly legitimate device (inSDEV_RUNNINGstate) with the same HBTL sitting in the list.One of the side effects of this is that the
scsi_probe_and_add_lun()goes ahead with the scanning and tries to add this existent device. This is the real problem.My patch avoids this problem by not breaking from the
__scsi_device_lookuploop, if the device is inSDEV_DELstate. After all we should not consider these devices to be part of the system. This will allow us to find the rightscsi_deviceand this "trying to add an existent device" problem will be avoided.And also why should
scsi_device_lookupand__scsi_device_lookupbe different in behaviour. One returns devices inSDEV_DELstate, the other doesn't. The comments suggest that they can be used interchangibly, but for the locking and the extra reference that thescsi_device_lookupholds.This is fixed as a side effect of the patch.
Comments welcome.
Thanx,
TomarQuoted 102 lines · --- James Smart
This sounds like a return to the old behavior, where sdevs in
SDEV_DELwere ignored. However, it too had lots of bad effects. We'd have to go back to the threads over the last 2 years that justified resurrecting the sdev. Start looking at threads like :
http://marc.info/?l=linux-scsi&m=115555788730468&w=2
http://marc.info/?l=linux-scsi&m=116837744314913&w=2
http://marc.info/?l=linux-scsi&m=117139230702785&w=2
http://marc.info/?l=linux-scsi&m=117991046126294&w=2
Also, there's multiple parts to this - the sdev struct, and the sysfs objects and thus namespace associated with the struct, etc.So, in my mind, if this reverts to ignoring sdevs in
SDEV_DEL, and creates a duplicate sdev inSDEV_RUNNING, then it's the wrong patch. What should be considered is where did the resurrection of the sdev go wrong. I remember that Hannes did some updates
http://marc.info/?l=linux-scsi&m=118215727101887&w=2
but I don't believe these ever got merged upstream. Perhaps that's a good place to start.-- james s
Quoted 78 lines · Nagendra Tomar
__scsi_device_lookupand__scsi_device_lookup_by_targetdo not check for thesdev_stateand hence returnscsi_deviceswithsdev_stateset toSDEV_DELalso. It has the following side effects.We can have two
scsi_deviceswith the same HBTL queued in the scsi_host->__devices/scsi_target->devices list, one
in theSDEV_DELstate and the other in, saySDEV_RUNNINGstate.
If the one in theSDEV_DELstate is before the one inSDEV_RUNNING
state, (which will almost always be the case) thescsi_device_lookupandscsi_device_lookup_by_targetwill never find the totally legitimatescsi_device(the one in theSDEV_RUNNINGstate).This is because __scsi_device_lookup/__scsi_device_lookup_by_target always returns the first one in the list (which in our case is the one with the
SDEV_DELstate) and thescsi_device_get()which is called by scsi_device_lookup/scsi_device_lookup_by_target will return-ENXIOfor thisscsi_device, resulting inscsi_device_lookupandscsi_device_lookup_by_targetto return NULL.So we _cannot_ lookup a perfectly valid device present in the
list ofscsi_devices.The right thing to do is to not have
__scsi_device_lookup
and__scsi_device_lookup_by_targetmatch a device if thescsi_devicestate isSDEV_DEL. This will also make these functions similar in behaviour to their scsi_device_lookup/scsi_device_lookup_by_target counterparts, as the comments in the code suggest.One way by which we can have two
scsi_devicesin the list is
as follows.
Suppose ascsi_devicehas some outstanding command(s) whenscsi_remove_deviceis called for it. Due to the extra ref being held by the command in flight, the__scsi_remove_device->put_devicecall will not actually free thescsi_deviceand it will remain in thescsi_devicelist albeit in theSDEV_DELstate. Now if we do ascsi_add_devicefor the same HBTL, a new device with the same HBTL (this one inSDEV_RUNNINGstate) gets added to thescsi_devicelist.Infact if we call
scsi_add_deviceone more time, it happily
goes ahead and tries to add it once more, asscsi_probe_and_add_lun->scsi_device_lookup_by_targetdoes not return the already existing device. This will though result in the kobject EEXIST warning dump.The patch below solves the problem described here by not
returningscsi_devicesinSDEV_DELstate, thus allowingscsi_deviceinSDEV_RUNNINGstate (if any) to be correctly returned, instead.Thanx,
TomarSigned-off-by Nagendra Singh Tomar <[email protected]>---
drivers/scsi/scsi.c renamed from drivers/scsi/scsi.c.orig +2
-2@@ -951,7 +951,7 @@ struct scsi_device *__scsi_device_lookupstruct scsi_device *sdev;list_for_each_entry (sdev, &starget->devices, same_target_siblings) { -if (sdev->lun ==lun) +if (sdev->lun == lun && sdev->sdev_state !=SDEV_DEL )return sdev; }@@ -1008,7 +1008,7 @@ struct scsi_device *__scsi_device_lookuplist_for_each_entry (sdev, &shost->__devices, siblings) {if (sdev->channel == channel && sdev->id == id && - sdev->lun ==lun) + sdev->lun == lun && sdev->sdev_state !=SDEV_DEL )return sdev; }Quoted 9 lines
Quoted 2 lines
___________________________________________________________Support the World Aids Awareness campaign this month with Yahoo! For Good
http://uk.promotions.yahoo.com/forgood/
Quoted 5 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 4 lines
__________________________________________________________Sent from Yahoo! Mail - a smarter inbox http://uk.mail.yahoo.com
__________________________________________________________
Sent from Yahoo! Mail - a smarter inbox http://uk.mail.yahoo.com