Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1594820ybi; Wed, 17 Jul 2019 18:22:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqzyrepLl1v3eRVQLqXZAa7KC5x+NoM5Y9u8ef2hNE0vFmSAwuLtUxwX6KBu5KCsK/5MrnwU X-Received: by 2002:a63:3281:: with SMTP id y123mr43797938pgy.72.1563412976273; Wed, 17 Jul 2019 18:22:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563412976; cv=none; d=google.com; s=arc-20160816; b=aHfp4LGU6EZ16B2uIQghi+zexNmzGrWtok0FupQW7uOdWJGtSARadiBUH7DZh8uCJk Sd9pMydCrR2ea1uUP80ptIp1YkX7+Ip/TggdgjExdCNKsdiOkrMdRH+pcMhX4Ce0uqt+ P683cs2LFrwlklj7KXmAckWGqBGMsxRgjvs44zGTG4O9kmgc5Y/yTvp9QBh3j3V+Eowz stxkeSD0ROUueyXNP2bu60qCchV140WSlNXv6Y692Uqm6xqUwhFZFvAhLuF8rZYCqkDu +wtQgcUmVRwuS6mQrYlFSRx0jX2eEz6cUYLINKMkeN7I1mU+0Y7I0nAKR8udeZPbEWbv YOVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject; bh=i6BJGGa1VWRh9kCPfGnnqFM6c4My8uWezUcjspLKJ9k=; b=xZAv4ZcnuPFKJZwUcv/azv7CiAi+tt0NCx3BCqDeU+gUEFaggAt1zhcg/yMfuQMGRn wGfpbcaUmUkytmU15p27Yn2+M47TCT7n0mllHjpH9wX4I9O9l/05UbSoHmZBzz3qGSJL mGNTywuFyZbKis2wuwbcoJYTzLCLJq3SO+YlyXJ9OVeuJImTRDHZVlHSOWb3U2/3iJVb zt+IrcMfxY8bwDUfPb/tTNlNx6I9rty5MkvJMfszuGo58HZEOmG57qUI2waOMaqif+1p 7umPeC1s1yQegATYmv5c5s9SfAQajIuimjuw+fDa+etkuaVGCBtEslvKaHMI7+/gUdpw 1jww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g63si418689plb.362.2019.07.17.18.22.40; Wed, 17 Jul 2019 18:22:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732226AbfGRBWM (ORCPT + 99 others); Wed, 17 Jul 2019 21:22:12 -0400 Received: from mga05.intel.com ([192.55.52.43]:8190 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727658AbfGRBWL (ORCPT ); Wed, 17 Jul 2019 21:22:11 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Jul 2019 18:22:10 -0700 X-IronPort-AV: E=Sophos;i="5.64,276,1559545200"; d="scan'208";a="179087622" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Jul 2019 18:22:10 -0700 Subject: [PATCH v2 1/7] drivers/base: Introduce kill_device() From: Dan Williams To: linux-nvdimm@lists.01.org Cc: Greg Kroah-Hartman , "Rafael J. Wysocki" , stable@vger.kernel.org, Jane Chu , peterz@infradead.org, vishal.l.verma@intel.com, linux-kernel@vger.kernel.org Date: Wed, 17 Jul 2019 18:07:53 -0700 Message-ID: <156341207332.292348.14959761496009347574.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <156341206785.292348.1660822720191643298.stgit@dwillia2-desk3.amr.corp.intel.com> References: <156341206785.292348.1660822720191643298.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The libnvdimm subsystem arranges for devices to be destroyed as a result of a sysfs operation. Since device_unregister() cannot be called from an actively running sysfs attribute of the same device libnvdimm arranges for device_unregister() to be performed in an out-of-line async context. The driver core maintains a 'dead' state for coordinating its own racing async registration / de-registration requests. Rather than add local 'dead' state tracking infrastructure to libnvdimm device objects, export the existing state tracking via a new kill_device() helper. The kill_device() helper simply marks the device as dead, i.e. that it is on its way to device_del(), or returns that the device was already dead. This can be used in advance of calling device_unregister() for subsystems like libnvdimm that might need to handle multiple user threads racing to delete a device. This refactoring does not change any behavior, but it is a pre-requisite for follow-on fixes and therefore marked for -stable. Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Fixes: 4d88a97aa9e8 ("libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver...") Cc: Tested-by: Jane Chu Signed-off-by: Dan Williams --- drivers/base/core.c | 27 +++++++++++++++++++-------- include/linux/device.h | 1 + 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index fd7511e04e62..eaf3aa0cb803 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -2211,6 +2211,24 @@ void put_device(struct device *dev) } EXPORT_SYMBOL_GPL(put_device); +bool kill_device(struct device *dev) +{ + /* + * Require the device lock and set the "dead" flag to guarantee that + * the update behavior is consistent with the other bitfields near + * it and that we cannot have an asynchronous probe routine trying + * to run while we are tearing out the bus/class/sysfs from + * underneath the device. + */ + lockdep_assert_held(&dev->mutex); + + if (dev->p->dead) + return false; + dev->p->dead = true; + return true; +} +EXPORT_SYMBOL_GPL(kill_device); + /** * device_del - delete device from system. * @dev: device. @@ -2230,15 +2248,8 @@ void device_del(struct device *dev) struct kobject *glue_dir = NULL; struct class_interface *class_intf; - /* - * Hold the device lock and set the "dead" flag to guarantee that - * the update behavior is consistent with the other bitfields near - * it and that we cannot have an asynchronous probe routine trying - * to run while we are tearing out the bus/class/sysfs from - * underneath the device. - */ device_lock(dev); - dev->p->dead = true; + kill_device(dev); device_unlock(dev); /* Notify clients of device removal. This call must come diff --git a/include/linux/device.h b/include/linux/device.h index e85264fb6616..0da5c67f6be1 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1373,6 +1373,7 @@ extern int (*platform_notify_remove)(struct device *dev); */ extern struct device *get_device(struct device *dev); extern void put_device(struct device *dev); +extern bool kill_device(struct device *dev); #ifdef CONFIG_DEVTMPFS extern int devtmpfs_create_node(struct device *dev);