Linux USB
 help / color / mirror / Atom feed
From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: linux-usb@vger.kernel.org
Cc: Yehezkel Bernat <YehezkelShB@gmail.com>,
	Lukas Wunner <lukas@wunner.de>,
	Andreas Noever <andreas.noever@gmail.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	Mika Westerberg <westeri@kernel.org>
Subject: [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed
Date: Wed, 25 Nov 2026 16:26:15 +0100	[thread overview]
Message-ID: <20261125152623.456054-3-mika.westerberg@linux.intel.com> (raw)
In-Reply-To: <20261125152623.456054-1-mika.westerberg@linux.intel.com>

From: "Mika Westerberg (Intel)" <westeri@kernel.org>

tb_xdomain_remove() only stops the handshake and leaves the XDomain
registered to the bus. Unregistering happens separately without tb->lock
by tb_domain_unregister_unplugged_xdomains() but this is never called
when the domain itself is removed. This may cause adding XDomains later
on fail like:

  sysfs: cannot create duplicate filename '/bus/thunderbolt/devices/0-1'
  thunderbolt 0-1: failed to add XDomain device

Fix this by first unbinding the service drivers in tb_domain_remove(),
and then unregistering the XDomains once tb->lock has been released and
the workqueue has been flushed (the work items unregister the unplugged
XDomains too, so this avoids doing it twice).

Fixes: a8937f35cf39 ("thunderbolt: Remove XDomain from the bus without holding tb->lock")
Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/domain.c  | 34 ++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/xdomain.c |  2 +-
 include/linux/thunderbolt.h   |  3 +++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index f9951d4311cb..61666e1a9b39 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -78,6 +78,9 @@ static int tb_service_probe(struct device *dev)
 	struct tb_service *svc = tb_to_service(dev);
 	struct tb_service_driver *driver;
 
+	if (READ_ONCE(tb_service_parent(svc)->tb->removing))
+		return -ENODEV;
+
 	driver = container_of(dev->driver, struct tb_service_driver, driver);
 
 	return driver->probe(svc);
@@ -491,6 +494,29 @@ int tb_domain_add(struct tb *tb, bool reset)
 	return ret;
 }
 
+static int unbind_service(struct device *dev, void *data)
+{
+	if (tb_is_service(dev))
+		device_release_driver(dev);
+	return 0;
+}
+
+static int unbind_services(struct device *dev, void *data)
+{
+	struct tb_xdomain *xd;
+	const struct tb *tb = data;
+
+	xd = tb_to_xdomain(dev);
+	if (xd && xd->tb == tb) {
+		scoped_guard(mutex, &xd->lock)
+			xd->removing = true;
+		cancel_work_sync(&xd->services_work);
+		device_for_each_child_reverse(&xd->dev, NULL, unbind_service);
+	}
+
+	return 0;
+}
+
 /**
  * tb_domain_remove() - Removes and releases a domain
  * @tb: Domain to remove
@@ -500,6 +526,13 @@ int tb_domain_add(struct tb *tb, bool reset)
  */
 void tb_domain_remove(struct tb *tb)
 {
+	WRITE_ONCE(tb->removing, true);
+	/*
+	 * Unbind service drivers now so they can do their cleanup with
+	 * the working control channel.
+	 */
+	bus_for_each_dev(&tb_bus_type, NULL, tb, unbind_services);
+
 	mutex_lock(&tb->lock);
 	if (tb->cm_ops->stop)
 		tb->cm_ops->stop(tb);
@@ -508,6 +541,7 @@ void tb_domain_remove(struct tb *tb)
 	mutex_unlock(&tb->lock);
 
 	flush_workqueue(tb->wq);
+	tb_domain_unregister_unplugged_xdomains(tb);
 
 	if (tb->cm_ops->deinit)
 		tb->cm_ops->deinit(tb);
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index 55e17f4e35dc..fceaa177b3c1 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -1848,7 +1848,7 @@ static void tb_xdomain_services_work(struct work_struct *work)
 
 	/* The state work may replace the remote properties meanwhile */
 	scoped_guard(mutex, &xd->lock) {
-		if (xd->removing)
+		if (READ_ONCE(xd->tb->removing) || xd->removing)
 			return;
 		dir = tb_property_copy_dir(xd->remote_properties);
 		if (!dir)
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 48ae996a779c..03f315934e44 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -78,6 +78,8 @@ enum tb_security_level {
  * @index: Linux assigned domain number
  * @security_level: Current security level
  * @nboot_acl: Number of boot ACLs the domain supports
+ * @removing: Domain is about to be removed, no new XDomain services are
+ *	      allowed to be registered
  * @privdata: Private connection manager specific data
  */
 struct tb {
@@ -91,6 +93,7 @@ struct tb {
 	int index;
 	enum tb_security_level security_level;
 	size_t nboot_acl;
+	bool removing;
 	unsigned long privdata[];
 };
 
-- 
2.50.1


  parent reply	other threads:[~2026-11-25 15:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:26 [PATCH 0/9] thunderbolt: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 1/9] thunderbolt: Add and remove XDomain services outside of the state work Mika Westerberg
2026-11-25 15:26 ` Mika Westerberg [this message]
2026-11-25 15:26 ` [PATCH 3/9] thunderbolt: stream: Keep better track of the HopIDs Mika Westerberg
2026-11-25 15:26 ` [PATCH 4/9] thunderbolt: stream: Validate the ConfigFS group name more strictly Mika Westerberg
2026-11-25 15:26 ` [PATCH 5/9] thunderbolt: Allow DMA tunnel also between two lane adapters Mika Westerberg
2026-11-25 15:26 ` [PATCH 6/9] thunderbolt: Add KUnit tests for lane to lane DMA tunnels Mika Westerberg
2026-11-25 15:26 ` [PATCH 7/9] thunderbolt: Add tb_xdomains_enable_paths() and tb_xdomains_disable_paths() Mika Westerberg
2026-11-25 15:26 ` [PATCH 8/9] thunderbolt: stream: Introduce USB4STREAM coupling Mika Westerberg
2026-11-25 15:26 ` [PATCH 9/9] Documentation: thunderbolt: Document " Mika Westerberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125152623.456054-3-mika.westerberg@linux.intel.com \
    --to=mika.westerberg@linux.intel.com \
    --cc=YehezkelShB@gmail.com \
    --cc=andreas.noever@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=westeri@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox