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 3/9] thunderbolt: stream: Keep better track of the HopIDs
Date: Wed, 25 Nov 2026 16:26:16 +0100	[thread overview]
Message-ID: <20261125152623.456054-4-mika.westerberg@linux.intel.com> (raw)
In-Reply-To: <20261125152623.456054-1-mika.westerberg@linux.intel.com>

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

Currently if a HopID is already taken and we try to allocate that with
previous HopID already set like this (HopID 10 is already allocated)

  # echo 9 > in_hopid
  # echo 10 > in_hopid

In the second echo we release 9 but since allocation of 10 fails we
return -EBUSY and leave 9 as ->in_hopid. If at this point the stream is
removed with rmdir we release 9 again which results WARN() from
ida_free().

Make this work better so that we keep track of what we actually have
allocated and keep the configured value as separate.

Signed-off-by: Mika Westerberg (Intel) <westeri@kernel.org>
---
 drivers/thunderbolt/stream.c | 77 +++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 24 deletions(-)

diff --git a/drivers/thunderbolt/stream.c b/drivers/thunderbolt/stream.c
index c9e1987a6924..2b73f585ccff 100644
--- a/drivers/thunderbolt/stream.c
+++ b/drivers/thunderbolt/stream.c
@@ -128,6 +128,8 @@ struct tbstream_ring {
  * @index: Unique identifier for the character device
  * @in_hopid: In HopID
  * @out_hopid: Out HopID
+ * @in_hopid_alloc: Allocated @in_hopid, %0 if not allocated
+ * @out_hopid_alloc: Allocated @out_hopid, %0 if not allocated
  * @ring_size: Size of the rings
  * @throttling: Interrupt throttling rate in ns
  * @busy_poll: Instead of interrupts, busy poll the rings
@@ -145,6 +147,11 @@ struct tbstream_ring {
  * @close_received is used on write side to notify the writer that the
  * other side sent CLOSE. @closed on the other hand is used on the read
  * side to make read(2) return EOF to the caller.
+ *
+ * @in_hopid and @out_hopid are the values configured by the user and
+ * stay (except %-1 which will be replaced by an automatically allocated
+ * HopID). @in_hopid_alloc and @out_hopid_alloc are the currently
+ * allocated values based on those.
  */
 struct tbstream_dev {
 	struct config_group group;
@@ -154,6 +161,8 @@ struct tbstream_dev {
 	int index;
 	int in_hopid;
 	int out_hopid;
+	int in_hopid_alloc;
+	int out_hopid_alloc;
 	unsigned int ring_size;
 	unsigned int throttling;
 	bool busy_poll;
@@ -308,10 +317,10 @@ static void tbstream_dev_release(struct kref *kref)
 	if (sdev->stream) {
 		struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
 
-		if (sdev->out_hopid > 0)
-			tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
-		if (sdev->in_hopid > 0)
-			tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+		if (sdev->out_hopid_alloc)
+			tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+		if (sdev->in_hopid_alloc)
+			tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
 
 		tbstream_put(sdev->stream);
 	}
@@ -344,7 +353,7 @@ static inline int tbstream_dev_valid(const struct tbstream_dev *sdev)
 
 	if (!tbstream_valid(stream))
 		return -ENXIO;
-	if (sdev->in_hopid <= 0 || sdev->out_hopid <= 0)
+	if (!sdev->in_hopid_alloc || !sdev->out_hopid_alloc)
 		return -EINVAL;
 	return 0;
 }
@@ -660,10 +669,10 @@ static int tbstream_dev_start(struct tbstream_dev *sdev)
 	}
 	sdev->rx_ring.ring = ring;
 
-	ret = tb_xdomain_enable_paths(xd, sdev->out_hopid,
-				     sdev->tx_ring.ring->hop,
-				     sdev->in_hopid,
-				     sdev->rx_ring.ring->hop);
+	ret = tb_xdomain_enable_paths(xd, sdev->out_hopid_alloc,
+				      sdev->tx_ring.ring->hop,
+				      sdev->in_hopid_alloc,
+				      sdev->rx_ring.ring->hop);
 	if (ret)
 		goto err_free_rx;
 
@@ -736,9 +745,9 @@ static void tbstream_dev_stop(struct tbstream_dev *sdev)
 
 	xd = tbstream_dev_xdomain(sdev);
 	if (xd) {
-		tb_xdomain_disable_paths(xd, sdev->out_hopid,
+		tb_xdomain_disable_paths(xd, sdev->out_hopid_alloc,
 					 sdev->tx_ring.ring->hop,
-					 sdev->in_hopid,
+					 sdev->in_hopid_alloc,
 					 sdev->rx_ring.ring->hop);
 	}
 
@@ -1331,7 +1340,8 @@ static int tbstream_dev_update_properties(struct tbstream_dev *sdev)
 
 	ret = service_update_properties(stream->svc,
 					config_item_name(&sdev->group.cg_item),
-					sdev->in_hopid, sdev->out_hopid);
+					sdev->in_hopid_alloc,
+					sdev->out_hopid_alloc);
 	if (!ret)
 		tb_service_properties_changed(stream->svc);
 
@@ -1344,9 +1354,15 @@ static int tbstream_dev_alloc_in_hopid(struct tbstream_dev *sdev, int hopid)
 	struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
 	int ret;
 
-	if (sdev->in_hopid > 0 && sdev->in_hopid != hopid)
-		tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+	/* Already allocated */
+	if (hopid > 0 && hopid == sdev->in_hopid_alloc)
+		return 0;
+	if (hopid < 0 && sdev->in_hopid_alloc)
+		return 0;
 	if (!hopid) {
+		if (sdev->in_hopid_alloc)
+			tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+		sdev->in_hopid_alloc = 0;
 		sdev->in_hopid = hopid;
 		return 0;
 	}
@@ -1361,7 +1377,9 @@ static int tbstream_dev_alloc_in_hopid(struct tbstream_dev *sdev, int hopid)
 		tb_xdomain_release_in_hopid(xd, ret);
 		return -EBUSY;
 	}
-	sdev->in_hopid = ret;
+	if (sdev->in_hopid_alloc)
+		tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+	sdev->in_hopid_alloc = sdev->in_hopid = ret;
 	return 0;
 }
 
@@ -1370,9 +1388,14 @@ static int tbstream_dev_alloc_out_hopid(struct tbstream_dev *sdev, int hopid)
 	struct tb_xdomain *xd = tbstream_dev_xdomain(sdev);
 	int ret;
 
-	if (sdev->out_hopid > 0 && sdev->out_hopid != hopid)
-		tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
+	if (hopid > 0 && hopid == sdev->out_hopid_alloc)
+		return 0;
+	if (hopid < 0 && sdev->out_hopid_alloc)
+		return 0;
 	if (!hopid) {
+		if (sdev->out_hopid_alloc)
+			tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+		sdev->out_hopid_alloc = 0;
 		sdev->out_hopid = hopid;
 		return 0;
 	}
@@ -1383,7 +1406,9 @@ static int tbstream_dev_alloc_out_hopid(struct tbstream_dev *sdev, int hopid)
 		tb_xdomain_release_out_hopid(xd, ret);
 		return -EBUSY;
 	}
-	sdev->out_hopid = ret;
+	if (sdev->out_hopid_alloc)
+		tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+	sdev->out_hopid_alloc = sdev->out_hopid = ret;
 	return 0;
 }
 
@@ -1597,8 +1622,8 @@ tbstream_dev_attach_stream(struct tbstream_dev *sdev, struct tbstream_group *sg)
 			tbstream_dev_alloc_out_hopid(sdev, sdev->out_hopid);
 	}
 
-	service_update_properties(stream->svc, name, sdev->in_hopid,
-				  sdev->out_hopid);
+	service_update_properties(stream->svc, name, sdev->in_hopid_alloc,
+				  sdev->out_hopid_alloc);
 	tb_service_properties_changed(stream->svc);
 
 	/* Notify any openerers that the stream is now attached */
@@ -1617,10 +1642,14 @@ static void tbstream_dev_detach_stream(struct tbstream_dev *sdev)
 			return;
 		sdev->stream = NULL;
 		xd = tb_service_parent(stream->svc);
-		if (sdev->out_hopid > 0)
-			tb_xdomain_release_out_hopid(xd, sdev->out_hopid);
-		if (sdev->in_hopid > 0)
-			tb_xdomain_release_in_hopid(xd, sdev->in_hopid);
+		if (sdev->out_hopid_alloc) {
+			tb_xdomain_release_out_hopid(xd, sdev->out_hopid_alloc);
+			sdev->out_hopid_alloc = 0;
+		}
+		if (sdev->in_hopid_alloc) {
+			tb_xdomain_release_in_hopid(xd, sdev->in_hopid_alloc);
+			sdev->in_hopid_alloc = 0;
+		}
 	}
 
 	service_update_properties(stream->svc, name, 0, 0);
-- 
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 ` [PATCH 2/9] thunderbolt: Unregister XDomains when the domain is removed Mika Westerberg
2026-11-25 15:26 ` Mika Westerberg [this message]
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-4-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