linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] driver core: Make it safe to use get_device() if the reference count is zero
@ 2017-12-14  3:39 Jason Yan
  2017-12-14  7:42 ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Yan @ 2017-12-14  3:39 UTC (permalink / raw)
  To: gregkh, martin.petersen, jejb
  Cc: linux-scsi, linux-kernel, Jason Yan, Bart Van Assche,
	Ewan D . Milne, Christoph Hellwig

Some driviers may have the chance to increase a reference count that
has dropped to zero when using get_device() because of their design.

We have met such a issue with scsi:
https://www.spinics.net/lists/linux-scsi/msg115295.html

The scsi core will keep the scsi device object in the host list after
it has been deleted and the iterator can still find it. All of the
places where need iterating have to check the state of the scsi device
and this makes a lot of code redundancy and complexity.

Provide a safe mechanism in get_device() by using
kobject_get_unless_zero().

Suggested-by: Bart Van Assche <bart.vanassche@wdc.com>
Signed-off-by: Jason Yan <yanaijie@huawei.com>
CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
CC: Bart Van Assche <bart.vanassche@wdc.com>
CC: Ewan D. Milne <emilne@redhat.com>
CC: James E.J. Bottomley <jejb@linux.vnet.ibm.com>
CC: Christoph Hellwig <hch@lst.de>
---
 drivers/base/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 12ebd05..cc74810 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1916,7 +1916,7 @@ EXPORT_SYMBOL_GPL(device_register);
  */
 struct device *get_device(struct device *dev)
 {
-	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
+	return dev && kobject_get_unless_zero(&dev->kobj) ? dev : NULL;
 }
 EXPORT_SYMBOL_GPL(get_device);
 
-- 
2.9.5

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-12-14  9:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-14  3:39 [PATCH] driver core: Make it safe to use get_device() if the reference count is zero Jason Yan
2017-12-14  7:42 ` Greg KH
2017-12-14  7:56   ` Jason Yan
2017-12-14  8:10     ` Greg KH
2017-12-14  8:27       ` Jason Yan
2017-12-14  8:56         ` Greg KH
2017-12-14  9:24           ` Jason Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).