Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751479AbdLNHmZ (ORCPT ); Thu, 14 Dec 2017 02:42:25 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:54962 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750751AbdLNHmY (ORCPT ); Thu, 14 Dec 2017 02:42:24 -0500 Date: Thu, 14 Dec 2017 08:42:27 +0100 From: Greg KH To: Jason Yan Cc: martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, Bart Van Assche , "Ewan D . Milne" , Christoph Hellwig Subject: Re: [PATCH] driver core: Make it safe to use get_device() if the reference count is zero Message-ID: <20171214074227.GA30120@kroah.com> References: <20171214033936.6534-1-yanaijie@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171214033936.6534-1-yanaijie@huawei.com> User-Agent: Mutt/1.9.1 (2017-09-22) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1878 Lines: 51 On Thu, Dec 14, 2017 at 11:39:36AM +0800, Jason Yan wrote: > Some driviers may have the chance to increase a reference count that > has dropped to zero when using get_device() because of their design. Then those drivers are broken :) > 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 > Signed-off-by: Jason Yan > CC: Greg Kroah-Hartman > CC: Bart Van Assche > CC: Ewan D. Milne > CC: James E.J. Bottomley > CC: Christoph Hellwig > --- > 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; I really don't want to do this, the bus the device is on should prevent this from happening. Also, once that reference count drops to zero, the memory should be freed, so you really have a stale pointer here, and this code would fail if you had slab debugging enabled anyway. So I don't even think this fixes the issue you think it fixes :) thanks, greg k-h