mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jason Yan <yanaijie@huawei.com>
To: <gregkh@linuxfoundation.org>, <martin.petersen@oracle.com>,
	<jejb@linux.vnet.ibm.com>
Cc: <linux-scsi@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	Jason Yan <yanaijie@huawei.com>,
	Bart Van Assche <bart.vanassche@wdc.com>,
	"Ewan D . Milne" <emilne@redhat.com>,
	Christoph Hellwig <hch@lst.de>
Subject: [PATCH] driver core: Make it safe to use get_device() if the reference count is zero
Date: Thu, 14 Dec 2017 11:39:36 +0800	[thread overview]
Message-ID: <20171214033936.6534-1-yanaijie@huawei.com> (raw)

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

             reply	other threads:[~2017-12-14  3:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-14  3:39 Jason Yan [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171214033936.6534-1-yanaijie@huawei.com \
    --to=yanaijie@huawei.com \
    --cc=bart.vanassche@wdc.com \
    --cc=emilne@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®