Article View: linux.kernel.netdev
Article #18726[PATCH v3 28/31] driver core: Allow driver attachment with the device lock held
From: Leon Romanovsky
Date: Wed, 18 Nov 2026 19:26
Date: Wed, 18 Nov 2026 19:26
101 lines
11510 bytes
11510 bytes
From: Leon Romanovsky <leonro@nvidia.com>
USB authorization needs to keep driver attachment and its configuration
result under the same device lock. Dropping the lock to call
device_attach() would let another request change that result before the
authorizing caller can inspect it.
Provide synchronous attachment for callers that already hold the device
lock. Keep asynchronous scheduling outside the lock in the existing path.
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
---
drivers/base/dd.c | 34 ++++++++++++++++++++++++++++++----
include/linux/device.h | 1 +
2 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 4c4723384dc66..2f45b53e5ba91 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -1222,12 +1222,11 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
put_device(dev);
}
-static int __device_attach(struct device *dev, bool allow_async)
+static int __device_attach_locked(struct device *dev, bool allow_async,
+ bool *async)
{
int ret = 0;
- bool async = false;
- device_lock(dev);
mutex_lock(&dev->p->driver_lock);
if (dev->p->dead) {
goto out_unlock;
@@ -1265,7 +1264,7 @@ static int __device_attach(struct device *dev, bool allow_async)
*/
dev_dbg(dev, "scheduling asynchronous probe\n");
get_device(dev);
- async = true;
+ *async = true;
} else {
pm_request_idle(dev);
}
@@ -1275,6 +1274,16 @@ static int __device_attach(struct device *dev, bool allow_async)
}
out_unlock:
mutex_unlock(&dev->p->driver_lock);
+ return ret;
+}
+
+static int __device_attach(struct device *dev, bool allow_async)
+{
+ bool async = false;
+ int ret;
+
+ device_lock(dev);
+ ret = __device_attach_locked(dev, allow_async, &async);
device_unlock(dev);
if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
put_device(dev);
@@ -1284,6 +1293,23 @@ static int __device_attach(struct device *dev, bool allow_async)
return ret;
}
+/**
+ * device_attach_locked - try to attach a driver with the device locked
+ * @dev: device to attach
+ *
+ * Synchronous device_attach() with the device lock held by the caller.
+ * When called for a USB interface, the parent lock must also be held.
+ * The device lock remains held on return.
+ *
+ * Return: 1 if bound, 0 if no driver bound, or a negative error.
+ */
+int device_attach_locked(struct device *dev)
+{
+ device_lock_assert(dev);
+ return __device_attach_locked(dev, false, NULL);
+}
+EXPORT_SYMBOL_GPL(device_attach_locked);
+
/**
* device_attach - try to attach device to a driver.
* @dev: device.
diff --git a/include/linux/device.h b/include/linux/device.h
index 3db6657dffe41..05b8f4af36873 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1337,6 +1337,7 @@ int __must_check device_driver_attach_unrestricted(
int __must_check device_bind_driver(struct device *dev);
void device_release_driver(struct device *dev);
int __must_check device_attach(struct device *dev);
+int __must_check device_attach_locked(struct device *dev);
int __must_check driver_attach(const struct device_driver *drv);
void device_initial_probe(struct device *dev);
int __must_check device_reprobe(struct device *dev);
--
2.56.1
Technical Details
Message-ID:
<20261118-device-trust-core-v3-28-d655875c4bfb@nvidia.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366400-linux-kernel-netdev=dewy.ruReferences:
<20261118-device-trust-core-v3-0-d655875c4bfb@nvidia.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366400-linux-kernel-netdev=dewy.ru From: leon@kernel.org (Leon Romanovsky) Newsgroups: linux.kernel.netdev Subject: [PATCH v3 28/31] driver core: Allow driver attachment with the device lock held Date: Wed, 18 Nov 2026 21:26:58 +0200 Organization: Usenet Network Message-ID: <20261118-device-trust-core-v3-28-d655875c4bfb@nvidia.com> References: <20261118-device-trust-core-v3-0-d655875c4bfb@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2683725"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:4fSpshQP9EzHCnhkBlQYpIy51Fw= sha256:gODqvqv7aOwqUaBOyTUshH2hzYyND44CYiWCXh5CgW8= sha1:VOWkGf3P5GqRIh2OT6fCyToTjT4= sha256:kCVdBmA4jSCumf0DgJKTuXedODvb+8wc2VCIZ5ISsxw= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 38DAB1153AF; Wed, 18 Nov 2026 22:40:44 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.301 X-Spam-Status: No, score=-2.301 tagged_above=-9999 required=6.31 tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FYEGA2XlKoht; Wed, 18 Nov 2026 22:40:41 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-366400-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id A3EC51153A1; Wed, 18 Nov 2026 22:40:32 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id D4943145E67; Wed, 18 Nov 2026 19:34:06 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795030146; b=vwhK3PbmR4uzFzV8gVDaUjd8VbJ1Zt25nfWa0qS7Oo/OFE9JDI42P81OxFdg7SG1MQGB VcB/H1RGDUcFgwnTtwpQmqJr8GBXBK63PQynwjLGwuM6wmrMTll2nUy9ustWp2D/Hrhr+ KKoscmlQS2DWXonu+clyiJBerAErRBn9Pil2hpgglKmNHM8IqusfFIoCHmDwcNC1MZhvk etBp2cOC0EDO7WZvVLAEt6tx8JaOrTbIqJwCWOAu+S0SPpyZ4f2HA162zkAqOtvrXpCZw oO86bW0/zRNJLnSBnVB7JQ3hcNEjpwgXbCPOkp0terHB5DGUMdy0KtpbkQOpbepE8bA== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795030146; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Type:X-Mailer: Content-Transfer-Encoding; bh=1V5OySgH+ZSszge5UvHlymL+jckhu9tpr2xqgdevGuY=; b=oUb2eq+j+x9kTi9wm65Eim8PVBVDcpjfh7o7aVtl27pFbceggJOoddhIBKeVOP1jifJ8 Y3Tn2csNirz15HJOmccPOncu3tSm8C6uKmTUxWzlLpOiyS7nzdGHJWRTcgHYliu0jpTfE ZVsMpdBF0TAdcYLnum+W69K4iR+WWH5dI+hkqGEdNmxglUQYY+N698whx4OLSDnBMJFwI bcFFl7uQnuSZ2De05uUyvsy/Ag8XDWH6jhiJphp2HTS4aep+HOtZOfHz3AF8VcUHUfbws 0xPJIPP+1xE69YzXC3QPNuUcFIpB/SxjoYUV/5hGz8SOFRNbV8tJ5yuYsHGOjoDiIWw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jVYon4HH; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3880C6077DCB; Wed, 18 Nov 2026 19:29:06 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jVYon4HH X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795030145; b=exDsZ2nM8Q9DfTqvPMcrsdxDEsYfIvra8lmElynlq5NqkbmFrNn2IpbppP9TiPxFJtIf ox4/j/L2RfU5pM2Ty0C46urO11zQW+D2vV0zmezHz43OXG8isrT9lcQCtbKOeKbLYzLXT QXdOTqjfgPtxWWKiYkNWLmuOi/hoaDjSBq3AoSeeq/oaXc0m9x2Rydl3zo96NwHJTVosI TNpxZJiX6H8zvHl6abZQhiFKuMbJo/IfCDCcVvUlIlRfu292pCEZtfj6Yyep2QAhHOedQ aaT7pi0HbiEh7Te0n9cxeBkcAAj8Fi1EKEF9Lj9hEXWvFUEaG4VADRa8xdtWk6NxmUg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795030145; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type:X-Mailer: Content-Transfer-Encoding; bh=1V5OySgH+ZSszge5UvHlymL+jckhu9tpr2xqgdevGuY=; b=C+4ATZwl7GkABMD52c9Xh1rbKLI+SIQ4Lm30+NhS/HMKFfyYJCD7PFtqLhTecsVzAS1a Zn2h8C3xfALll5IckLAAgJe2tqCvU6TwEr4coKHK3ChjiR9qlzpf+2pBL1uJeyQkHs8Ho 8wMk0XC4FdS+O1ImNa2SAIXcdggZX+jvUD1EqHHscGMSIG0KzmGijW5Q4haW4pzK+erMX O9xG3ektIrSE05Pn8/Cnv48dADVooBnDYS0xUCvPgx7zw015AYo+TVZaMml1LTzJ+87W7 FLEDi6DdIMEEpAo82zhSzLavXIJ+NCuYnFyXiSdRJNld1bNxZgHJGWMF++XVAl5bzcg== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jVYon4HH; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5414E606BB5D; Wed, 18 Nov 2026 19:29:04 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3D601F000C7; Wed, 18 Nov 2026 19:29:03 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795030144; bh=1V5OySgH+ZSszge5UvHlymL+jckhu9tpr2xqgdevGuY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jVYon4HHntgw2K3s3j0o1jJjWdeJxstyXtRmoG8gdDVdsNKee6TP08bVAQQko85sY xYcCCuP1mnI7Pn6KQhsZVptc7Yfy14sPZxx56wIBJUe8P3gOIOOaN54ENf43Rs3tDl YthiSHBr8ioKEq2skaQhcz6SbKMdhVFi1Y5lIE4sAhagJ+4VRZY+1uPaWuEvTQiCOO LDB8wfceU38Wp3XpuOpuDbPbxvMlEqAWghC3I+KG0o3pUov+qLxcakw1xqOvFCuuxB GFRk69toorsTOfSyuugDgS0ciwp2k7hTKuxH6qko3hW1Ah5MF68b2duGEGpWkRpTL+ Q/374NS5keUGQ== X-Cc: linux-kernel@vger.kernel.org, driver-core@lists.linux.dev, linux-input@vger.kernel.org, netdev@vger.kernel.org, linux-wireless@vger.kernel.org, linux-usb@vger.kernel.org, linux-bluetooth@vger.kernel.org, linux-acpi@vger.kernel.org, linux-doc@vger.kernel.org, linux-pci@vger.kernel.org, iommu@lists.linux.dev, linux-coco@lists.linux.dev, Alexey Kardashevskiy <aik@amd.com>, Luiz Augusto von Dentz <luiz.von.dentz@intel.com>, Greg Kroah-Hartman <gregkh@suse.de>, linux-sound@vger.kernel.org, Jason Gunthorpe <jgg@ziepe.ca> X-Mailer: git-send-email 2.56.1 X-In-Reply-To: <20261118-device-trust-core-v3-0-d655875c4bfb@nvidia.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-Mailer: b4 0.15-dev-18f8f