From patchwork Wed Nov 29 17:39:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 10082995 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 5842960353 for ; Wed, 29 Nov 2017 17:39:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46A9C27D16 for ; Wed, 29 Nov 2017 17:39:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B01929894; Wed, 29 Nov 2017 17:39:17 +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,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham 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 AD0C427D16 for ; Wed, 29 Nov 2017 17:39:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934742AbdK2RjN (ORCPT ); Wed, 29 Nov 2017 12:39:13 -0500 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:11264 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934224AbdK2RjK (ORCPT ); Wed, 29 Nov 2017 12:39:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1511977151; x=1543513151; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=lMA4P6vqChs+u1FSy5r14r12Cpfzr0FIrnjFer/xh4M=; b=D4Vx0kMJ/R61KEobDcmQZVBoK0UtTo/D4PbiKgU6diCG5FzET8Q7WhtD 6mFOlF2GwAbGENCn1Num/4WlwRWyBLbIVwRcQJWOnogbYDOrNOAy+SPwu AWIWRYAsb+bqGYYSEbUxFjAZE2cHG4iJAlBGMeMKdlPM+UNETztUhIW+5 egREUV1FcB+Bupy1cWWcTqSaK1/YA+1HQGL4I1uY9UIoRzIy6Dy8QWnPC h1Ztxaq/aBBGjrQiSM2akXLjX2D7nEj6s9S4doAbVx5DIhqextLG+naRO uf1iifW31axsrxDTHckC+Qh79drlyHkV6WHnNNhdeP1REFeeWu1cOgNxq A==; X-IronPort-AV: E=Sophos;i="5.45,338,1508774400"; d="scan'208";a="63551397" Received: from mail-dm3nam03lp0024.outbound.protection.outlook.com (HELO NAM03-DM3-obe.outbound.protection.outlook.com) ([207.46.163.24]) by ob1.hgst.iphmx.com with ESMTP; 30 Nov 2017 01:39:09 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector1-wdc-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=lMA4P6vqChs+u1FSy5r14r12Cpfzr0FIrnjFer/xh4M=; b=e3871PwOikQ7/UmcvRB9LdJ+iLzdhW5YopIhF3Z8FAUfiHGnK9Zlgorlwm+9e7McmhlsNPvZrBltU7B1xlg8WdH2zbXFbQj+oSEqK2g0TVq6sXnUNkt1Fp4y99a+drMlA9KA+IrOBN5iZIr9c9qBU4Ps+qi4eetfCpyWB++Q9GE= Received: from CY1PR0401MB1536.namprd04.prod.outlook.com (10.163.19.154) by CY1PR0401MB1533.namprd04.prod.outlook.com (10.163.19.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.260.4; Wed, 29 Nov 2017 17:39:07 +0000 Received: from CY1PR0401MB1536.namprd04.prod.outlook.com ([10.163.19.154]) by CY1PR0401MB1536.namprd04.prod.outlook.com ([10.163.19.154]) with mapi id 15.20.0260.007; Wed, 29 Nov 2017 17:39:07 +0000 From: Bart Van Assche To: "hch@lst.de" CC: "zhaohongjiang@huawei.com" , "jthumshirn@suse.de" , "martin.petersen@oracle.com" , "hare@suse.de" , "linux-scsi@vger.kernel.org" , "gregkh@linuxfoundation.org" , "yanaijie@huawei.com" , "jejb@linux.vnet.ibm.com" , "miaoxie@huawei.com" Subject: Re: [PATCH] scsi: fix race condition when removing target Thread-Topic: [PATCH] scsi: fix race condition when removing target Thread-Index: AQHTaL419b/hc2HTKkqT4kE8HNNTZqMriecAgAAApgCAABXdgA== Date: Wed, 29 Nov 2017 17:39:07 +0000 Message-ID: <1511977145.2671.13.camel@wdc.com> References: <20171129030556.47833-1-yanaijie@huawei.com> <1511972310.2671.7.camel@wdc.com> <20171129162050.GA32071@lst.de> In-Reply-To: <20171129162050.GA32071@lst.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Bart.VanAssche@wdc.com; x-originating-ip: [199.255.44.171] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; CY1PR0401MB1533; 20:rHb/emPPjOVm1CDdOUxtYiMVuN+lHpktO8LEHyEJTS8OdCvCYPiJeXs5OPaHyfKvpJ07Kg5bEFUsul7n0JUL5o/0WlTktO3BN8XMxRoJ3aY1W2TvM8jphWIh5b8EYdeDE7vP+6+6i0goWuR1GPwfTzYmy/ZDcXtOoFqXhH+5gno= x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: 6be3f8ab-721d-4231-223a-08d53750177a x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(48565401081)(2017052603199); SRVR:CY1PR0401MB1533; x-ms-traffictypediagnostic: CY1PR0401MB1533: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040450)(2401047)(5005006)(8121501046)(10201501046)(3002001)(3231022)(93006095)(93001095)(6055026)(6041248)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123562025)(20161123558100)(20161123560025)(6072148)(201708071742011); SRVR:CY1PR0401MB1533; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:CY1PR0401MB1533; x-forefront-prvs: 05066DEDBB x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(6009001)(346002)(376002)(366004)(39860400002)(189002)(199003)(24454002)(377424004)(6486002)(77096006)(4001150100001)(25786009)(66066001)(2501003)(305945005)(14454004)(7736002)(99286004)(6116002)(102836003)(81156014)(36756003)(86362001)(33646002)(1730700003)(101416001)(54906003)(8676002)(6436002)(6512007)(3846002)(6506006)(478600001)(106356001)(2351001)(316002)(53936002)(5660300001)(229853002)(4326008)(103116003)(2906002)(105586002)(97736004)(3280700002)(5640700003)(72206003)(81166006)(6246003)(189998001)(2900100001)(68736007)(8936002)(2950100002)(7416002)(6916009)(3660700001); DIR:OUT; SFP:1102; SCL:1; SRVR:CY1PR0401MB1533; H:CY1PR0401MB1536.namprd04.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-ID: <4B7115E51170774EB5DDACC4839C75A9@namprd04.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6be3f8ab-721d-4231-223a-08d53750177a X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Nov 2017 17:39:07.1865 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0401MB1533 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 On Wed, 2017-11-29 at 17:20 +0100, hch@lst.de wrote: > On Wed, Nov 29, 2017 at 04:18:30PM +0000, Bart Van Assche wrote: > > As the above patch description shows it can happen that the SCSI core calls > > get_device() after the device reference count has reached zero and before > > the memory for struct device is freed. Although the above patch looks fine > > to me, would you consider it acceptable to modify get_device() such that it > > uses kobject_get_unless_zero() instead of kobject_get()? I'm asking this > > because that change would help to reduce the complexity of the already too > > complicated SCSI core. > > I don't think we can just modify get_device, but we can add a new > get_device_unless_zero. In fact I have an open coded variant of that > in nvme, and was planning to submit one for the current merge window.. Sorry but I don't see why we can't modify get_device()? Can you explain why you think that something like the patch below is wrong? Thanks, Bart. [PATCH] Make it safe to use get_device() if the reference count is zero --- 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 110230d86527..049a5d9dba8a 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);