From patchwork Thu Dec 14 03:39:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Yan X-Patchwork-Id: 10111417 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A03AD60327 for ; Thu, 14 Dec 2017 03:34:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9138628C0F for ; Thu, 14 Dec 2017 03:34:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FE3F29A75; Thu, 14 Dec 2017 03:34:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE8B229AD3 for ; Thu, 14 Dec 2017 03:34:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752119AbdLNDdy (ORCPT ); Wed, 13 Dec 2017 22:33:54 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2678 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751917AbdLNDdx (ORCPT ); Wed, 13 Dec 2017 22:33:53 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 9EB293BE0BBC6; Thu, 14 Dec 2017 11:33:39 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.361.1; Thu, 14 Dec 2017 11:33:34 +0800 From: Jason Yan To: , , CC: , , Jason Yan , Bart Van Assche , "Ewan D . Milne" , Christoph Hellwig 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 Message-ID: <20171214033936.6534-1-yanaijie@huawei.com> X-Mailer: git-send-email 2.9.5 MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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; } EXPORT_SYMBOL_GPL(get_device);