thunderbolt: Reset affected AMD host interfaces before DMA HopID reuse
Subsystems (6)
PATCH 1/3 thunderbolt: Allow tb_ring_start() to fail
In reply to Basavaraj Natikartb_ring_start() returns void, so its callers cannot tell when a ring fails to start and keep building an unusable tunnel. On some host interfaces a DMA HopID also cannot be reprogrammed until the host interface has been reset.
Hence, let tb_ring_start() return an error and unwind the callers on
failure: stop an already started TX ring when its RX peer fails to start, and disable the DMA paths enabled before the rings were started.
A stream can also stay open after a failed resume. Therefore, free the partial allocations, clear the ring pointers, and let the subsequent I/O and close return without touching the freed rings. Check readiness under the device mutex and use a wake token so a wakeup is not lost across the unlocked sleep.
---
drivers/net/thunderbolt/main.c | 14 ++- drivers/thunderbolt/ctl.c | 23 ++++- drivers/thunderbolt/dma_test.c | 32 +++++- drivers/thunderbolt/nhi.c | 12 ++- drivers/thunderbolt/stream.c | 175 +++++++++++++++++++++------------ include/linux/thunderbolt.h | 2 +- 6 files changed, 181 insertions(+), 77 deletions(-)
drivers/net/thunderbolt/main.c +12 -2
@@ -669,8 +669,16 @@ static void tbnet_connected_work(struct work_struct *work)* the Rx ring before any incoming packets are allowed to * arrive. */ -tb_ring_start (net->tx_ring.ring); -tb_ring_start (net->rx_ring.ring); + ret =tb_ring_start (net->tx_ring.ring); +if (ret) { +netdev_dbg (net->dev,"failed to start Tx ring, ret=%d\n" , ret); + goto err_release_hopid; + } + ret =tb_ring_start (net->rx_ring.ring); +if (ret) { +netdev_dbg (net->dev,"failed to start Rx ring, ret=%d\n" , ret); + goto err_stop_tx; + } ret =tbnet_alloc_rx_buffers (net,TBNET_RING_SIZE );if (ret)
@@ -701,7 +709,9 @@ static void tbnet_connected_work(struct work_struct *work)tbnet_free_buffers (&net->rx_ring); err_stop_rings:tb_ring_stop (net->rx_ring.ring); +err_stop_tx:tb_ring_stop (net->tx_ring.ring); +err_release_hopid:tb_xdomain_release_in_hopid (net->xd, net->remote_transmit_path);tbnet_connect_failed (net); }
drivers/thunderbolt/ctl.c +20 -3
@@ -728,10 +728,27 @@ void tb_ctl_free(struct tb_ctl *ctl)*/ void tb_ctl_start (struct tb_ctl *ctl) { -int i; +int i, ret;tb_ctl_dbg (ctl,"control channel starting...\n" ); -tb_ring_start (ctl->tx);/* is used to ack hotplug packets, start first */ -tb_ring_start (ctl->rx); + +/* +* TX is used to ack hotplug packets so start it first. -ENODEV +* means the host controller itself is already gone (expected on +* an unplug-during-suspend resume), so do not warn about that. +*/ + ret =tb_ring_start (ctl->tx); +if (ret) { +if (ret != -ENODEV ) +tb_ctl_WARN (ctl,"failed to start TX ring\n" ); +return ; + } + ret =tb_ring_start (ctl->rx); +if (ret) { +if (ret != -ENODEV ) +tb_ctl_WARN (ctl,"failed to start RX ring\n" ); +tb_ring_stop (ctl->tx); +return ; + }for (i =0 ; i <TB_CTL_RX_PKG_COUNT ; i++)tb_ctl_rx_submit (ctl->rx_packets[i]);
drivers/thunderbolt/dma_test.c +28 -4
@@ -203,12 +203,36 @@ static int dma_test_start_rings(struct dma_test *dt)return ret; } -if (dt->tx_ring) -tb_ring_start (dt->tx_ring); -if (dt->rx_ring) -tb_ring_start (dt->rx_ring); +if (dt->tx_ring) { + ret =tb_ring_start (dt->tx_ring); +if (ret) + goto err_disable_paths; + } +if (dt->rx_ring) { + ret =tb_ring_start (dt->rx_ring); +if (ret) + goto err_stop_tx; + }return 0 ; + +err_stop_tx: +tb_xdomain_disable_paths (dt->xd, dt->tx_hopid, + dt->tx_ring ? dt->tx_ring->hop :-1 , + dt->rx_hopid, + dt->rx_ring ? dt->rx_ring->hop :-1 ); +if (dt->tx_ring) +tb_ring_stop (dt->tx_ring); + goto err_free; +err_disable_paths: +tb_xdomain_disable_paths (dt->xd, dt->tx_hopid, + dt->tx_ring ? dt->tx_ring->hop :-1 , + dt->rx_hopid, + dt->rx_ring ? dt->rx_ring->hop :-1 ); +err_free: +dma_test_free_rings (dt); + +return ret; }static void dma_test_stop_rings (struct dma_test *dt)
drivers/thunderbolt/nhi.c +10 -2
@@ -741,18 +741,24 @@ EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);* @ring: Ring to start * * Must not be invoked in parallel with tb_ring_stop(). +* +* Returns %0 on success and negative errno in case of failure. */ -void tb_ring_start (struct tb_ring *ring) +int tb_ring_start (struct tb_ring *ring) {u16 frame_size; +int ret =0 ;u32 flags;spin_lock_irq (&ring->nhi->lock);spin_lock (&ring->lock); -if (ring->nhi->going_away) +if (ring->nhi->going_away) { + ret = -ENODEV ; goto err; + }if (ring->running) {dev_WARN (ring->nhi->dev,"ring already started\n" ); + ret = -EBUSY ; goto err; } dev_dbg(ring->nhi->dev, "starting %s %d\n",
@@ -810,6 +816,8 @@ void tb_ring_start(struct tb_ring *ring) err:spin_unlock (&ring->lock);spin_unlock_irq (&ring->nhi->lock); + +return ret; }EXPORT_SYMBOL_GPL (tb_ring_start);
drivers/thunderbolt/stream.c +110 -65
310 lines not on this page · raw message
include/linux/thunderbolt.h +1 -1
@@ -672,7 +672,7 @@ struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, unsigned int flags, int e2e_tx_hop, u16 sof_mask, u16 eof_mask, void (*start_poll)(void *), void *poll_data); -void tb_ring_start (struct tb_ring *ring); +int tb_ring_start (struct tb_ring *ring);bool tb_ring_flush (struct tb_ring *ring,unsigned int timeout_msec);void tb_ring_stop (struct tb_ring *ring);void tb_ring_free (struct tb_ring *ring);
Signature · 1 line
2.34.1
PATCH 2/3 thunderbolt: Reset the host interface before reusing a DMA HopID
In reply to Basavaraj NatikarReusing a DMA HopID without an intervening host interface reset can hang the TX ring on some host routers. Resetting on every DMA tunnel teardown clears the state but, as the reset affects all rings, also disturbs unrelated active tunnels.
Hence, track the DMA HopIDs programmed since the last reset, prefer unused HopIDs when allocating rings, and check for reuse at tb_ring_start() too, since networking retains its rings across reconnect. Return -EAGAIN instead of programming a HopID that still needs a reset.
Run the reset from a work item once all DMA rings are idle: serialize it with the connection manager, stop the control channel around it, and block DMA rings from starting during the reset. Fence the work across domain removal and PM transitions, preserve live DMA rings across freeze/thaw, and restore the interrupt-mask shadow under the NHI lock after the reset.
On -EAGAIN, retry the networking login asynchronously and block the work producers before teardown cancels the workers. Keep peer disconnection separate from administrative shutdown so it cannot reopen the login gate, while stream and DMA-test callers unwind immediately and return the error to userspace.
Enable this only for reset-capable host interfaces marked with QUIRK_RESET_DMA_ON_REUSE. A competing DMA tunnel must stop before its dirty HopID can be reused, and retrying does not interrupt that tunnel.
---
Documentation/admin-guide/thunderbolt.rst | 6 + drivers/net/thunderbolt/main.c | 106 ++++++---- drivers/thunderbolt/ctl.c | 13 ++ drivers/thunderbolt/ctl.h | 1 + drivers/thunderbolt/domain.c | 66 +++++- drivers/thunderbolt/nhi.c | 239 ++++++++++++++++++++-- drivers/thunderbolt/nhi.h | 1 + drivers/thunderbolt/nhi_regs.h | 4 + include/linux/thunderbolt.h | 20 ++ 9 files changed, 399 insertions(+), 57 deletions(-)
Documentation/admin-guide/thunderbolt.rst +6 -0
@@ -410,6 +410,12 @@ transfer data:: host2 # cat /dev/tbstream0 host1 # dmesg > /dev/tbstream0 +On affected host interfaces a DMA HopID cannot be reused until the host +interface has been reset. Opening a stream or starting a DMA test can return +``EAGAIN`` until all other DMA tunnels stop and the reset completes. Retry +the complete operation; it does not interrupt an active networking tunnel. +If stream resume fails, close and reopen the stream to retry setup. + Once you are done with the stream you can remove them:: host2 # cd /sys/kernel/config/thunderbolt/stream
drivers/net/thunderbolt/main.c +70 -36
228 lines not on this page · raw message
drivers/thunderbolt/ctl.c +13 -0
@@ -779,6 +779,19 @@ void tb_ctl_stop(struct tb_ctl *ctl)tb_ctl_dbg (ctl,"control channel stopped\n" ); } +/* Close the enqueue gate only after all existing requests have drained. */ +bool tb_ctl_stop_if_idle (struct tb_ctl *ctl) +{ + scoped_guard(mutex, &ctl->request_queue_lock) { +if (!ctl->running || !list_empty (&ctl->request_queue)) +return false; + ctl->running = false; + } + +tb_ctl_stop (ctl); +return true; +} +/* public interface, commands */ /**
drivers/thunderbolt/ctl.h +1 -0
@@ -25,6 +25,7 @@ struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec, event_cb cb, void *cb_data);void tb_ctl_start (struct tb_ctl *ctl);void tb_ctl_stop (struct tb_ctl *ctl); +bool tb_ctl_stop_if_idle (struct tb_ctl *ctl);void tb_ctl_free (struct tb_ctl *ctl);/* configuration commands */
drivers/thunderbolt/domain.c +64 -2
128 lines not on this page · raw message
drivers/thunderbolt/nhi.c +220 -19
343 lines not on this page · raw message
drivers/thunderbolt/nhi.h +1 -0
@@ -123,6 +123,7 @@ struct tb_nhi_ops {/* Host interface quirks */ #define QUIRK_AUTO_CLEAR_INT BIT(0)#define QUIRK_E2E BIT(1) +#define QUIRK_RESET_DMA_ON_REUSE BIT(2)/* * Minimal number of vectors when we use MSI-X. Two for control channel
drivers/thunderbolt/nhi_regs.h +4 -0
@@ -115,6 +115,10 @@ struct ring_desc {#define REG_CAPS_VERSION_MASK GENMASK(23, 16)#define REG_CAPS_VERSION_2 0x40 +/* Host Interface Reset - resets TX/RX rings and E2E flow control counters */ +#define REG_HOST_INTERFACE_RESET 0x39858 +#define REG_HOST_INTERFACE_RESET_RST BIT(0) +#define REG_DMA_MISC 0x39864#define REG_DMA_MISC_INT_AUTO_CLEAR BIT(2)#define REG_DMA_MISC_DISABLE_AUTO_CLEAR BIT(17)
include/linux/thunderbolt.h +20 -0
@@ -515,6 +515,21 @@ void tb_service_properties_changed(struct tb_service *svc);* MSI-X is used. * @hop_count: Number of rings (end point hops) supported by NHI. * @quirks: NHI specific quirks if any +* @dma_hops_used: Bitmap of the TX and RX DMA HopIDs programmed after the +* last host interface reset. Only with +* %QUIRK_RESET_DMA_ON_REUSE. +* @resetting: The host interface reset is in progress. Only with +* %QUIRK_RESET_DMA_ON_REUSE. +* @removing: The domain this host interface belongs to is being removed. +* Set before releasing the domain lock so +* reset_work does not restart the control channel while the +* domain is tearing down. Only with %QUIRK_RESET_DMA_ON_REUSE. +* @suspended: The domain's control channel is stopped for system or runtime PM. +* Set before the control channel is stopped so reset_work +* does not restart it while the NHI is about to be powered +* down. Only with %QUIRK_RESET_DMA_ON_REUSE. +* @reset_work: Work that runs the host interface reset once all the DMA +* rings are idle. Only with %QUIRK_RESET_DMA_ON_REUSE. * @domain_released: Completed when domain has been fully released * @host_reset: Host router was reset on driver load, or forced on system * shutdown/reboot. When set, tb_stop() asserts DPR on connected
@@ -535,6 +550,11 @@ struct tb_nhi {struct work_struct interrupt_work;u32 hop_count;unsigned long quirks; +unsigned long *dma_hops_used; +bool resetting; +bool removing; +bool suspended; +struct delayed_work reset_work;struct completion domain_released;bool host_reset; };
Signature · 1 line
2.34.1
cover thunderbolt: Reset affected AMD host interfaces before DMA HopID reuse
Some AMD USB4 host routers hang a TX ring when a DMA HopID is reused after a DMA path is torn down without a host interface reset in between. Resetting on every teardown clears the state but also disrupts other active DMA tunnels on the same host interface.
Track the TX and RX DMA HopIDs programmed since the last reset and prefer unused HopIDs when allocating rings. Check reuse at tb_ring_start() as well, since networking retains its rings across reconnect, and return -EAGAIN instead of reprogramming a HopID that still needs a reset. Run the reset from a per-NHI work item once all DMA rings are idle, serialized with the connection manager and with the control channel stopped only over the reset. Enable it only for the affected AMD routers via QUIRK_RESET_DMA_ON_REUSE, and only on v1 host interfaces.
On -EAGAIN networking retries login asynchronously; stream and DMA-test users retry the operation. An active tunnel is left running even if it blocks another tunnel from starting until it stops.
Basavaraj Natikar (3):
thunderbolt: Allow tb_ring_start() to fail
thunderbolt: Reset the host interface before reusing a DMA HopID
thunderbolt: Add quirk to reset host interface for AMD USB4 routers
Documentation/admin-guide/thunderbolt.rst | 6 + drivers/net/thunderbolt/main.c | 120 +++++++---- drivers/thunderbolt/ctl.c | 36 +++- drivers/thunderbolt/ctl.h | 1 + drivers/thunderbolt/dma_test.c | 32 ++- drivers/thunderbolt/domain.c | 66 +++++- drivers/thunderbolt/nhi.c | 249 ++++++++++++++++++++-- drivers/thunderbolt/nhi.h | 13 ++ drivers/thunderbolt/nhi_regs.h | 4 + drivers/thunderbolt/pci.c | 21 ++ drivers/thunderbolt/stream.c | 175 +++++++++------ include/linux/thunderbolt.h | 22 +- 12 files changed, 612 insertions(+), 133 deletions(-)
Signature · 1 line
2.34.1
PATCH 3/3 thunderbolt: Add quirk to reset host interface for AMD USB4 routers
Affected AMD USB4 host routers hang a TX ring when a DMA HopID is programmed again after the DMA paths are torn down without an intervening host interface reset.
Hence, set QUIRK_RESET_DMA_ON_REUSE for these routers so a used DMA HopID is not reprogrammed until the host interface has been reset. The reset waits for all DMA rings to stop, leaving unrelated active tunnels undisturbed.
---
drivers/thunderbolt/nhi.h | 12 ++++++++++++ drivers/thunderbolt/pci.c | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+)
drivers/thunderbolt/nhi.h +12 -0
@@ -118,6 +118,18 @@ struct tb_nhi_ops {#define PCI_DEVICE_ID_INTEL_PTL_P_NHI0 0xe433#define PCI_DEVICE_ID_INTEL_PTL_P_NHI1 0xe434 +#define PCI_DEVICE_ID_AMD_1AH_M60H_NHI0 0x1120 +#define PCI_DEVICE_ID_AMD_1AH_M60H_NHI1 0x1121 +#define PCI_DEVICE_ID_AMD_1AH_M68H_NHI0 0x113b +#define PCI_DEVICE_ID_AMD_1AH_M68H_NHI1 0x113c +#define PCI_DEVICE_ID_AMD_1AH_M80H_NHI0 0x1155 +#define PCI_DEVICE_ID_AMD_1AH_M80H_NHI1 0x1158 +#define PCI_DEVICE_ID_AMD_1AH_M80H_NHI2 0x1159 +#define PCI_DEVICE_ID_AMD_1AH_M24H_NHI0 0x151c +#define PCI_DEVICE_ID_AMD_1AH_M24H_NHI1 0x151d +#define PCI_DEVICE_ID_AMD_1AH_M70H_NHI0 0x158d +#define PCI_DEVICE_ID_AMD_1AH_M70H_NHI1 0x158e +#define PCI_CLASS_SERIAL_USB_USB4 0x0c0340/* Host interface quirks */
drivers/thunderbolt/pci.c +21 -0
@@ -63,6 +63,27 @@ static void nhi_pci_check_quirks(struct tb_nhi_pci *nhi_pci) nhi->quirks |=QUIRK_E2E ;break ; } + }else if (pdev->vendor ==PCI_VENDOR_ID_AMD ) { +switch (pdev->device) { +case PCI_DEVICE_ID_AMD_1AH_M60H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M60H_NHI1 : +case PCI_DEVICE_ID_AMD_1AH_M68H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M68H_NHI1 : +case PCI_DEVICE_ID_AMD_1AH_M80H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M80H_NHI1 : +case PCI_DEVICE_ID_AMD_1AH_M80H_NHI2 : +case PCI_DEVICE_ID_AMD_1AH_M24H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M24H_NHI1 : +case PCI_DEVICE_ID_AMD_1AH_M70H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M70H_NHI1 : +/* +* These hosts may hang the Tx ring if its HopID is +* programmed again without a host interface reset +* in between. +*/ + nhi->quirks |=QUIRK_RESET_DMA_ON_REUSE ; +break ; + } } }
Signature · 1 line
2.34.1
Re: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
In reply to Basavaraj NatikarHi,
drivers/thunderbolt/nhi.c line 20
Quoted 6 lines · Basavaraj Natikar
+++
b/drivers/thunderbolt/nhi.cdrivers/thunderbolt/nhi.c +1
-0@@ -17,6 +17,7 @@#include <linux/iommu.h> #include <linux/lockdep.h> #include <linux/module.h> +#include <linux/pci.h>
I haven't looked at the code at all yet but this one is no-go. We are trying to make the NHI code generic that can work with any interconnect, like the controllers on ARM based SoCs (aka platform bus) and that's why we have pci.c for the PCIe based NHIs.
Re: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
Quoted 14 lines · Mika Westerberg
Hi,
drivers/thunderbolt/nhi.c line 20
Quoted 6 lines · Basavaraj Natikar
+++
b/drivers/thunderbolt/nhi.cdrivers/thunderbolt/nhi.c +1
-0@@ -17,6 +17,7 @@#include <linux/iommu.h> #include <linux/lockdep.h> #include <linux/module.h> +#include <linux/pci.h> I haven't looked at the code at all yet but this one is no-go. We are trying to make the NHI code generic that can work with any interconnect, like the controllers on ARM based SoCs (aka platform bus) and that's why we have
pci.cfor the PCIe based NHIs.
To add to that - what tree did you base on? I tried on thunderbolt/next (a93a8e3200002f0c345fec4c340390e9a60aabc7) and it doesn't apply.
╰─❯ b4 shazam https://lore.kernel.org/linux-usb/[email protected]/T/#m2e70efa2f627df231d9f975424096587b819a354
Looking up https://lore.kernel.org/all/[email protected]/
Grabbing thread from lore.kernel.org/all/[email protected]/t.mbox.gz
Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 5 messages in the thread
Analyzing 0 code-review messages
Checking attestation on all messages, may take a moment...
---
✓ [PATCH 1/3] thunderbolt: Allow tb_ring_start() to fail
✓ [PATCH 2/3] thunderbolt: Reset the host interface before reusing a
DMA HopID
✓ [PATCH 3/3] thunderbolt: Add quirk to reset host interface for AMD
USB4 routers
---
✓ Signed: DKIM/amd.com
---
Total patches: 3
---
Applying: thunderbolt: Allow tb_ring_start() to fail
Applying: thunderbolt: Reset the host interface before reusing a DMA HopID
Patch failed at 0002 thunderbolt: Reset the host interface before reusing a DMA HopID
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
error: patch failed: drivers/thunderbolt/nhi.c:17
error: drivers/thunderbolt/nhi.c: patch does not apply
error: patch failed: drivers/thunderbolt/nhi.h:123
error: drivers/thunderbolt/nhi.h: patch does not apply
error: patch failed: drivers/thunderbolt/nhi_regs.h:115
error: drivers/thunderbolt/nhi_regs.h: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Re: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
In reply to Mika WesterbergHi Mika,
Quoted 13 lines · Mika Westerberg
Hi,
drivers/thunderbolt/nhi.c line 20
Quoted 6 lines · Basavaraj Natikar
+++
b/drivers/thunderbolt/nhi.cdrivers/thunderbolt/nhi.c +1
-0@@ -17,6 +17,7 @@#include <linux/iommu.h> #include <linux/lockdep.h> #include <linux/module.h> +#include <linux/pci.h> I haven't looked at the code at all yet but this one is no-go. We are trying to make the NHI code generic that can work with any interconnect, like the controllers on ARM based SoCs (aka platform bus) and that's why we have
pci.cfor the PCIe based NHIs.
I rebased the series onto thunderbolt/next a93a8e3200002f0c345fec4c340390e9a60aabc7. The reworked v2 removes the PCI-specific dependency from generic nhi.c and uses the existing reset_interface operation instead.
Thanks,
--
Basavaraj
Re: [PATCH 1/3] thunderbolt: Allow tb_ring_start() to fail
In reply to Basavaraj NatikarHi,
Quoted 20 lines · Basavaraj Natikar
tb_ring_start()returns void, so its callers cannot tell when a ring fails to start and keep building an unusable tunnel. On some host interfaces a DMA HopID also cannot be reprogrammed until the host interface has been reset.Hence, let
tb_ring_start()return an error and unwind the callers on
failure: stop an already started TX ring when its RX peer fails to start, and disable the DMA paths enabled before the rings were started.A stream can also stay open after a failed resume. Therefore, free the partial allocations, clear the ring pointers, and let the subsequent I/O and close return without touching the freed rings. Check readiness under the device mutex and use a wake token so a wakeup is not lost across the unlocked sleep.
Co-developed-by Sanath S <[email protected]>Signed-off-by Sanath S <[email protected]>Signed-off-by Basavaraj Natikar <[email protected]>---
Quoted 7 lines
drivers/net/thunderbolt/main.c | 14 ++- drivers/thunderbolt/ctl.c | 23 ++++- drivers/thunderbolt/dma_test.c | 32 +++++- drivers/thunderbolt/nhi.c | 12 ++- drivers/thunderbolt/stream.c | 175 +++++++++++++++++++++------------ include/linux/thunderbolt.h | 2 +- 6 files changed, 181 insertions(+), 77 deletions(-)
This is pretty invasive change. How well did you test this?
I would have been hoping we can avoid touching the service drivers completely.
drivers/net/thunderbolt/main.c static void tbnet_connected_work(struct work_struct *work) · line 674
Quoted 13 lines
drivers/net/thunderbolt/main.c +3
-2@@ -669,8 +669,16 @@ static void tbnet_connected_work(struct work_struct *work)* the Rx ring before any incoming packets are allowed to * arrive. */ -tb_ring_start (net->tx_ring.ring); -tb_ring_start (net->rx_ring.ring); + ret =tb_ring_start (net->tx_ring.ring); +if (ret) { +netdev_dbg (net->dev,"failed to start Tx ring, ret=%d\n" , ret);
the ret= is not consistent wit the rest of the driver and should this be _warn() instead?
Quoted 28 lines
drivers/net/thunderbolt/main.c +9
-0+ goto err_release_hopid; + } + ret =tb_ring_start (net->rx_ring.ring); +if (ret) { +netdev_dbg (net->dev,"failed to start Rx ring, ret=%d\n" , ret); + goto err_stop_tx; + } ret =tbnet_alloc_rx_buffers (net,TBNET_RING_SIZE );if (ret)@@ -701,7 +709,9 @@ static void tbnet_connected_work(struct work_struct *work)tbnet_free_buffers (&net->rx_ring); err_stop_rings:tb_ring_stop (net->rx_ring.ring); +err_stop_tx:tb_ring_stop (net->tx_ring.ring); +err_release_hopid:tb_xdomain_release_in_hopid (net->xd, net->remote_transmit_path);tbnet_connect_failed (net); }drivers/thunderbolt/ctl.c +0
-0@@ -728,10 +728,27 @@ void tb_ctl_free(struct tb_ctl *ctl)*/ void tb_ctl_start (struct tb_ctl *ctl) {
drivers/thunderbolt/ctl.c void tb_ctl_free(struct tb_ctl *ctl) · line 737
Quoted 10 lines
drivers/thunderbolt/ctl.c +6
-3-int i; +int i, ret;tb_ctl_dbg (ctl,"control channel starting...\n" ); -tb_ring_start (ctl->tx);/* is used to ack hotplug packets, start first */ -tb_ring_start (ctl->rx); + +/* +* TX is used to ack hotplug packets so start it first. -ENODEV +* means the host controller itself is already gone (expected on +* an unplug-during-suspend resume), so do not warn about that.
What?
Quoted 40 lines
drivers/thunderbolt/ctl.c +14
-0+*/ + ret =tb_ring_start (ctl->tx); +if (ret) { +if (ret != -ENODEV ) +tb_ctl_WARN (ctl,"failed to start TX ring\n" ); +return ; + } + ret =tb_ring_start (ctl->rx); +if (ret) { +if (ret != -ENODEV ) +tb_ctl_WARN (ctl,"failed to start RX ring\n" ); +tb_ring_stop (ctl->tx); +return ; + }for (i =0 ; i <TB_CTL_RX_PKG_COUNT ; i++)tb_ctl_rx_submit (ctl->rx_packets[i]);drivers/thunderbolt/dma_test.c +10
-4@@ -203,12 +203,36 @@ static int dma_test_start_rings(struct dma_test *dt)return ret; } -if (dt->tx_ring) -tb_ring_start (dt->tx_ring); -if (dt->rx_ring) -tb_ring_start (dt->rx_ring); +if (dt->tx_ring) { + ret =tb_ring_start (dt->tx_ring); +if (ret) + goto err_disable_paths; + } +if (dt->rx_ring) { + ret =tb_ring_start (dt->rx_ring); +if (ret) + goto err_stop_tx; + }
drivers/thunderbolt/dma_test.c static int dma_test_start_rings(struct dma_test *dt) · line 226
Quoted 10 lines
drivers/thunderbolt/dma_test.c +9
-0return 0 ; + +err_stop_tx: +tb_xdomain_disable_paths (dt->xd, dt->tx_hopid, + dt->tx_ring ? dt->tx_ring->hop :-1 , + dt->rx_hopid, + dt->rx_ring ? dt->rx_ring->hop :-1 ); +if (dt->tx_ring) +tb_ring_stop (dt->tx_ring); + goto err_free;
This is way too fragile :-(
Quoted 192 lines
drivers/thunderbolt/dma_test.c +9
-0+err_disable_paths: +tb_xdomain_disable_paths (dt->xd, dt->tx_hopid, + dt->tx_ring ? dt->tx_ring->hop :-1 , + dt->rx_hopid, + dt->rx_ring ? dt->rx_ring->hop :-1 ); +err_free: +dma_test_free_rings (dt); + +return ret; }static void dma_test_stop_rings (struct dma_test *dt)drivers/thunderbolt/nhi.c +10
-2@@ -741,18 +741,24 @@ EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);* @ring: Ring to start * * Must not be invoked in parallel with tb_ring_stop(). +* +* Returns %0 on success and negative errno in case of failure. */ -void tb_ring_start (struct tb_ring *ring) +int tb_ring_start (struct tb_ring *ring) {u16 frame_size; +int ret =0 ;u32 flags;spin_lock_irq (&ring->nhi->lock);spin_lock (&ring->lock); -if (ring->nhi->going_away) +if (ring->nhi->going_away) { + ret = -ENODEV ; goto err; + }if (ring->running) {dev_WARN (ring->nhi->dev,"ring already started\n" ); + ret = -EBUSY ; goto err; } dev_dbg(ring->nhi->dev, "starting %s %d\n",@@ -810,6 +816,8 @@ void tb_ring_start(struct tb_ring *ring) err:spin_unlock (&ring->lock);spin_unlock_irq (&ring->nhi->lock); + +return ret; }EXPORT_SYMBOL_GPL (tb_ring_start);drivers/thunderbolt/stream.c +36
-27@@ -259,6 +259,9 @@ static void tbstream_ring_free(struct tbstream_ring *ring)enum dma_data_direction dir;int i; +if (!ring->frames) +return ; +if (ring->ring->is_tx) dir =DMA_TO_DEVICE ;else @@ -279,6 +282,7 @@ static void tbstream_ring_free(struct tbstream_ring *ring) ring->prod =0 ; ring->cons =0 ;kfree (ring->frames); + ring->frames =NULL ; }static inline bool tbstream_ring_available (const struct tbstream_ring *ring)@@ -575,6 +579,9 @@ static int tbstream_dev_send_close(struct tbstream_dev *sdev)struct tbstream_frame *sf;ktime_t timeout; +if (!sdev->tx_ring.ring) +return -ESHUTDOWN ; +/* * Wait for the ring to have available slots before we send the * CLOSE packet. @@ -645,7 +652,7 @@ static int tbstream_dev_start(struct tbstream_dev *sdev) ret =tbstream_dev_alloc_tx_buffers (sdev);if (ret) - goto err_free_tx; + goto err_free_tx_buffers; e2e_tx_hop = ring->hop; sof_mask =BIT (TBSTREAM_FRAME_START );@@ -672,8 +679,12 @@ static int tbstream_dev_start(struct tbstream_dev *sdev) sdev->rx_pending = false; -tb_ring_start (sdev->tx_ring.ring); -tb_ring_start (sdev->rx_ring.ring); + ret =tb_ring_start (sdev->tx_ring.ring); +if (ret) + goto err_disable_paths; + ret =tb_ring_start (sdev->rx_ring.ring); +if (ret) + goto err_stop_tx; ret =tbstream_dev_alloc_rx_buffers (sdev);if (ret)@@ -682,13 +693,20 @@ static int tbstream_dev_start(struct tbstream_dev *sdev) err_stop:tb_ring_stop (sdev->rx_ring.ring); +tbstream_ring_free (&sdev->rx_ring); +err_stop_tx:tb_ring_stop (sdev->tx_ring.ring); +err_disable_paths: +tb_xdomain_disable_paths (xd, sdev->out_hopid, sdev->tx_ring.ring->hop, + sdev->in_hopid, sdev->rx_ring.ring->hop); err_free_rx:tb_ring_free (sdev->rx_ring.ring); + sdev->rx_ring.ring =NULL ; err_free_tx_buffers:tbstream_ring_free (&sdev->tx_ring); -err_free_tx:tb_ring_free (sdev->tx_ring.ring); + sdev->tx_ring.ring =NULL ; +wake_up_interruptible (&sdev->wait);return ret; }@@ -708,6 +726,10 @@ static void tbstream_dev_stop(struct tbstream_dev *sdev) {struct tb_xdomain *xd; +/* Starting may have failed and freed the rings already */ +if (!sdev->tx_ring.ring) +return ; +if (sdev->busy_poll) {/* * When busy polling we must advance the ring ourselves @@ -742,6 +764,7 @@ static void tbstream_dev_stop(struct tbstream_dev *sdev)tbstream_ring_free (&sdev->tx_ring);tb_ring_free (sdev->tx_ring.ring); sdev->tx_ring.ring =NULL ; +wake_up_interruptible (&sdev->wait); }/* Use only with read_iter/write_iter() to handle nowait */ @@ -757,29 +780,6 @@ static int tbstream_dev_lock(struct tbstream_dev *sdev, bool nowait)return 0 ; } -/* Must not be called with @sdev->lock held */ -static int tbstream_dev_busy_poll_wait (struct tbstream_dev *sdev, -struct tbstream_ring *ring) -{ -for (;;) { -if (signal_pending (current)) -return -ERESTARTSYS ; -if (tb_ring_poll_pending (ring->ring)) -return 0 ; -/* -* For TX ring we need to check the RX side too because -* it might have received CLOSE packet. -*/ -if (ring == &sdev->tx_ring && -tb_ring_poll_pending (sdev->rx_ring.ring)) -return 0 ; -if (tbstream_dev_valid (sdev) !=0 || -tbstream_dev_closed (sdev) ||tbstream_dev_removed (sdev)) -return 0 ; -cond_resched (); - } -} -static bool tbstream_dev_has_event (struct tbstream_dev *sdev,struct tbstream_ring *ring) {@@ -796,6 +796,52 @@ tbstream_dev_has_event(struct tbstream_dev *sdev, struct tbstream_ring *ring)return tb_ring_poll_pending (sdev->rx_ring.ring); } +static bool tbstream_dev_ready (struct tbstream_dev *sdev, +struct tbstream_ring *ring) +{ +lockdep_assert_held (&sdev->lock); + +/* Starting may have failed and freed the rings already */ +if (!sdev->tx_ring.ring) +return true; +
drivers/thunderbolt/stream.c tbstream_dev_has_event(struct tbstream_dev *sdev, struct tbstream_ring *ring) · line 817
Quoted 10 lines
drivers/thunderbolt/stream.c +10
-0+return tbstream_dev_has_event (sdev, ring) || +tbstream_dev_close_received (sdev) || +tb_ring_poll_pending (ring->ring); +} + +/* Must not be called with @sdev->lock held. */ +static int tbstream_dev_wait (struct tbstream_dev *sdev, +struct tbstream_ring *ring) +{ +DEFINE_WAIT_FUNC (wait, woken_wake_function);
woken_wake_function?
Quoted 14 lines
drivers/thunderbolt/stream.c +14
-0+int ret =0 ; + +add_wait_queue (&sdev->wait, &wait); +for (;;) { +bool ready; + + ret =mutex_lock_interruptible (&sdev->lock); +if (ret) +break ; + ready =tbstream_dev_ready (sdev, ring); +mutex_unlock (&sdev->lock); +if (ready) +break ; +if (signal_pending (current)) {
drivers/thunderbolt/stream.c tbstream_dev_has_event(struct tbstream_dev *sdev, struct tbstream_ring *ring) · line 841
Quoted 10 lines
drivers/thunderbolt/stream.c +10
-0+ ret = -ERESTARTSYS ; +break ; + } +if (sdev->busy_poll) +cond_resched (); +else +/* The wake token bridges the unlocked check-to-sleep gap. */ +wait_woken (&wait,TASK_INTERRUPTIBLE ,MAX_SCHEDULE_TIMEOUT ); + } +remove_wait_queue (&sdev->wait, &wait);
This is also really complex to just handle the error in tb_ring_start(). I wonder what scenarios were actually tested?
drivers/thunderbolt/stream.c tbstream_dev_fops_read_iter(struct kiocb *kiocb, struct iov_iter *to) · line 865
Quoted 13 lines
drivers/thunderbolt/stream.c +6
-0+return ret; +} +static ssize_t tbstream_dev_fops_read_iter (struct kiocb *kiocb,struct iov_iter *to) {@@ -814,6 +860,11 @@ tbstream_dev_fops_read_iter(struct kiocb *kiocb, struct iov_iter *to)return ret;for (;;) { +if (!sdev->tx_ring.ring) { +mutex_unlock (&sdev->lock); +return -ESHUTDOWN ;
Now the ring went away behind the reader?
I don't think this is a good approach to be honest.
Quoted 91 lines
drivers/thunderbolt/stream.c +20
-23+ } +/* Advance RX completions */ tbstream_dev_advance_rx (sdev);@@ -838,16 +889,9 @@ tbstream_dev_fops_read_iter(struct kiocb *kiocb, struct iov_iter *to)if (nowait)return -EAGAIN ; -if (sdev->busy_poll) { - ret =tbstream_dev_busy_poll_wait (sdev, &sdev->rx_ring); -if (ret) -return ret; - }else { - ret =wait_event_interruptible (sdev->wait, -tbstream_dev_has_event (sdev, &sdev->rx_ring)); -if (ret) -return ret; - } + ret =tbstream_dev_wait (sdev, &sdev->rx_ring); +if (ret) +return ret; ret =tbstream_dev_lock (sdev, nowait);if (ret)@@ -957,6 +1001,11 @@ tbstream_dev_fops_write_iter(struct kiocb *kiocb, struct iov_iter *from)return ret;for (;;) { +if (!sdev->tx_ring.ring) { +mutex_unlock (&sdev->lock); +return -ESHUTDOWN ; + } +/* Advance TX (and RX) completions */ tbstream_dev_advance_both (sdev);@@ -984,17 +1033,9 @@ tbstream_dev_fops_write_iter(struct kiocb *kiocb, struct iov_iter *from)if (nowait)return -EAGAIN ; -if (sdev->busy_poll) { - ret =tbstream_dev_busy_poll_wait (sdev, &sdev->tx_ring); -if (ret) -return ret; - }else { - ret =wait_event_interruptible (sdev->wait, -tbstream_dev_has_event (sdev, &sdev->tx_ring) || -tbstream_dev_close_received (sdev)); -if (ret) -return ret; - } + ret =tbstream_dev_wait (sdev, &sdev->tx_ring); +if (ret) +return ret; ret =tbstream_dev_lock (sdev, nowait);if (ret)@@ -1041,7 +1082,7 @@ tbstream_dev_fops_poll(struct file *file, struct poll_table_struct *wait)poll_wait (file, &sdev->wait, wait);guard (mutex)(&sdev->lock); -if (tbstream_dev_valid (sdev) !=0 ) +if (tbstream_dev_valid (sdev) !=0 || !sdev->tx_ring.ring)return EPOLLHUP |EPOLLERR ;/* @@ -1105,6 +1146,11 @@ static int tbstream_dev_fops_open(struct inode *inode, struct file *file) } } +if (sdev->users && !sdev->tx_ring.ring) { + ret = -ESHUTDOWN ; + goto err_unlock; + } +/* Only on first open we allocate rings and enable paths */ if (!sdev->users++) { ret =tbstream_dev_start (sdev);@@ -1136,7 +1182,7 @@ static int tbstream_dev_fops_release(struct inode *inode, struct file *file)struct tbstream_dev *sdev =to_tbstream_dev (file->private_data);mutex_lock (&sdev->lock); -if (--sdev->users ==0 ) { +if (--sdev->users ==0 && sdev->tx_ring.ring) {/* * Advance now in case there is CLOSE waiting in the RX * ring. @@ -1884,14 +1930,14 @@ static int __maybe_unused tbstream_suspend(struct device *dev)if (!sg)return 0 ;
drivers/thunderbolt/stream.c static int __maybe_unused tbstream_suspend(struct device *dev) · line 1938
Quoted 10 lines
drivers/thunderbolt/stream.c +3
-3+mutex_lock (&sg->lock);list_for_each_entry_reverse (sdev, &sg->dev_list, list) { -tbstream_dev_get (sdev); -/* Stop the stream (if it was open) */ +mutex_lock (&sdev->lock);if (sdev->users)tbstream_dev_stop (sdev); -tbstream_dev_put (sdev); +mutex_unlock (&sdev->lock);
What's this?
Quoted 60 lines
drivers/thunderbolt/stream.c +11
-12} - +mutex_unlock (&sg->lock);config_group_put (&sg->group);return 0 ; }@@ -1902,28 +1948,27 @@ static int __maybe_unused tbstream_resume(struct device *dev)struct tbstream *stream =tb_service_get_drvdata (svc);struct tbstream_group *sg;struct tbstream_dev *sdev; +int ret =0 ; sg =tbstream_group_find (stream);if (!sg)return 0 ; +mutex_lock (&sg->lock);list_for_each_entry (sdev, &sg->dev_list, list) { -tbstream_dev_get (sdev); +mutex_lock (&sdev->lock);if (sdev->users) { -int ret; +int err =tbstream_dev_start (sdev); - ret =tbstream_dev_start (sdev); -if (ret) { -tbstream_dev_put (sdev); -config_group_put (&sg->group); -return ret; - } +if (err && !ret) + ret = err; } -tbstream_dev_put (sdev); +mutex_unlock (&sdev->lock); +wake_up_interruptible (&sdev->wait); } - +mutex_unlock (&sg->lock);config_group_put (&sg->group); -return 0 ; +return ret; }static const struct dev_pm_ops tbstream_pm_ops = {include/linux/thunderbolt.h +1
-1@@ -672,7 +672,7 @@ struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, unsigned int flags, int e2e_tx_hop, u16 sof_mask, u16 eof_mask, void (*start_poll)(void *), void *poll_data); -void tb_ring_start (struct tb_ring *ring); +int tb_ring_start (struct tb_ring *ring);bool tb_ring_flush (struct tb_ring *ring,unsigned int timeout_msec);void tb_ring_stop (struct tb_ring *ring);void tb_ring_free (struct tb_ring *ring);Signature · 1 line
2.34.1
Re: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
In reply to Basavaraj NatikarHi,
Quoted 22 lines · Basavaraj Natikar
Reusing a DMA HopID without an intervening host interface reset can hang the TX ring on some host routers. Resetting on every DMA tunnel teardown clears the state but, as the reset affects all rings, also disturbs unrelated active tunnels.
Hence, track the DMA HopIDs programmed since the last reset, prefer unused HopIDs when allocating rings, and check for reuse at
tb_ring_start()too, since networking retains its rings across reconnect. Return-EAGAINinstead of programming a HopID that still needs a reset.Run the reset from a work item once all DMA rings are idle: serialize it with the connection manager, stop the control channel around it, and block DMA rings from starting during the reset. Fence the work across domain removal and PM transitions, preserve live DMA rings across freeze/thaw, and restore the interrupt-mask shadow under the NHI lock after the reset.
On
-EAGAIN, retry the networking login asynchronously and block the work producers before teardown cancels the workers. Keep peer disconnection separate from administrative shutdown so it cannot reopen the login gate, while stream and DMA-test callers unwind immediately and return the error to userspace.
Quoted 5 lines
Enable this only for reset-capable host interfaces marked with
QUIRK_RESET_DMA_ON_REUSE. A competing DMA tunnel must stop before its dirty HopID can be reused, and retrying does not interrupt that tunnel.Suggested-by Mika Westerberg <[email protected]>
Yeah, I'm not really sure I suggested this :(
My idea was to done it so that it is nicely contained inside nhi.c without distracting the service drivers. What you are doing is complete opposite of that.
Given the complexity I would then rather just take the previous quirk with the deadlock fixed.
Re: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
Hi Mika,
Quoted 30 lines · Mika Westerberg
Hi,
Quoted 22 lines · Basavaraj Natikar
Reusing a DMA HopID without an intervening host interface reset can hang the TX ring on some host routers. Resetting on every DMA tunnel teardown clears the state but, as the reset affects all rings, also disturbs unrelated active tunnels.
Hence, track the DMA HopIDs programmed since the last reset, prefer unused HopIDs when allocating rings, and check for reuse at
tb_ring_start()too, since networking retains its rings across reconnect. Return-EAGAINinstead of programming a HopID that still needs a reset.Run the reset from a work item once all DMA rings are idle: serialize it with the connection manager, stop the control channel around it, and block DMA rings from starting during the reset. Fence the work across domain removal and PM transitions, preserve live DMA rings across freeze/thaw, and restore the interrupt-mask shadow under the NHI lock after the reset.
On
-EAGAIN, retry the networking login asynchronously and block the work producers before teardown cancels the workers. Keep peer disconnection separate from administrative shutdown so it cannot reopen the login gate, while stream and DMA-test callers unwind immediately and return the error to userspace.Quoted 5 lines
Enable this only for reset-capable host interfaces marked with
QUIRK_RESET_DMA_ON_REUSE. A competing DMA tunnel must stop before its dirty HopID can be reused, and retrying does not interrupt that tunnel.Suggested-by Mika Westerberg <[email protected]>
Quoted 8 lines
Yeah, I'm not really sure I suggested this :(
My idea was to done it so that it is nicely contained inside
nhi.cwithout distracting the service drivers. What you are doing is complete opposite of that.Given the complexity I would then rather just take the previous quirk with the deadlock fixed.
Agreed. The current approach spreads the handling into the service drivers and is more complex than intended.
Thanks,
--
Basavaraj
[email protected]
AI reviewRe: [PATCH 1/3] thunderbolt: Allow tb_ring_start() to fail
In reply to Basavaraj NatikarSashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
tb_ring_start()returns void, so its callers cannot tell when a ring fails to start and keep building an unusable tunnel. On some host interfaces a DMA HopID also cannot be reprogrammed until the host interface has been reset.Hence, let
tb_ring_start()return an error and unwind the callers on
failure: stop an already started TX ring when its RX peer fails to start, and disable the DMA paths enabled before the rings were started.A stream can also stay open after a failed resume. Therefore, free the partial allocations, clear the ring pointers, and let the subsequent I/O and close return without touching the freed rings. Check readiness under the device mutex and use a wake token so a wakeup is not lost across the unlocked sleep.
Quoted 3 lines
Co-developed-by Sanath S <[email protected]>Signed-off-by Sanath S <[email protected]>Signed-off-by Basavaraj Natikar <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 2/3] thunderbolt: Reset the host interface before reusing a DMA HopID
In reply to Basavaraj NatikarThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] thunderbolt: NULL pointer dereference in nhi_reset_work() during probe failure - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] thunderbolt: NULL pointer dereference in nhi_reset_work() during probe failure
- [High] thunderbolt: lost control channel interrupt due to race in nhi_reset_work()
- [Low] net: thunderbolt: mixed goto and guard() cleanup in tbnet_connected_work() --
commit dc6bb5398632a1b975f04bc4d5441e027de1977f
Author: Basavaraj Natikar <[email protected]>
Subject: thunderbolt: Reset the host interface before reusing a DMA HopID
This patch tracks DMA HopIDs and resets the host interface once all DMA rings are idle to allow safe HopID reuse. It also defers ring allocation when HopIDs are dirty and blocks control and DMA traffic during the reset.
Quoted 4 lines
drivers/net/thunderbolt/main.c +0
-0
[ ... ]
Quoted 13 lines
drivers/net/thunderbolt/main.c +3
-3@@ -644,9 +657,9 @@ static void tbnet_connected_work(struct work_struct *work)if (netif_carrier_ok (net->dev))return ; -mutex_lock (&net->connection_lock); - connected = net->login_sent && net->login_received; -mutex_unlock (&net->connection_lock); +scoped_guard (mutex, &net->connection_lock) + connected = !net->stopping && !net->disconnecting && + net->login_sent && net->login_received;if (!connected)return ;
[ ... ]
drivers/net/thunderbolt/main.c static void tbnet_connected_work(struct work_struct *work) · line 741
Quoted 11 lines
drivers/net/thunderbolt/main.c +6
-0@@ -714,24 +732,34 @@ static void tbnet_connected_work(struct work_struct *work) err_release_hopid:tb_xdomain_release_in_hopid (net->xd, net->remote_transmit_path);tbnet_connect_failed (net); +if (ret == -EAGAIN ) { +guard (mutex)(&net->connection_lock); +if (!net->stopping && !net->disconnecting) +queue_delayed_work (system_long_wq, &net->login_work, +msecs_to_jiffies (TBNET_LOGIN_DELAY )); + } }
[Severity: Low]
This isn't a bug, but does this function mix goto-based error handling with scoped-based cleanup? The cleanup subsystem guidelines indicate that we should either convert all resources that need a goto cleanup to scope-based cleanup, or convert none of them. Since this function still uses goto labels like err_release_hopid and err_free_tx_buffers, adding scoped_guard and guard could create confusing ownership semantics.
Quoted 4 lines
drivers/thunderbolt/nhi.c +0
-0
[ ... ]
Quoted 10 lines
drivers/thunderbolt/nhi.c +2
-1@@ -513,7 +515,8 @@ void tb_ring_poll_complete(struct tb_ring *ring)spin_lock_irqsave (&ring->nhi->lock, flags);spin_lock (&ring->lock); -if (ring->start_poll) +if (ring->start_poll && ring->running && !ring->nhi->resetting && + !ring->nhi->going_away)__ring_interrupt_mask (ring, false);spin_unlock (&ring->lock);spin_unlock_irqrestore (&ring->nhi->lock, flags);
[ ... ]
drivers/thunderbolt/nhi.c struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, · line 851
Quoted 8 lines
drivers/thunderbolt/nhi.c +8
-0+static void nhi_reset_work (struct work_struct *work) +{ + struct tb_nhi *nhi = container_of(to_delayed_work(work), struct tb_nhi, + reset_work); +struct tb *tb =dev_get_drvdata (nhi->dev); + +/* The connection manager must be blocked over the reset */ +guard (mutex)(&tb->lock);
[Severity: High]
Could this dereference a NULL tb pointer during probe failure?
In nhi_probe(), tb_domain_add() is called prior to dev_set_drvdata(). If tb_domain_add() discovers routers and synchronously binds drivers that start and stop DMA rings, tb_ring_stop() can queue nhi->reset_work. If tb_domain_add() subsequently fails, the probe error path tears down the domain and nhi_reset_work() may execute from the workqueue concurrently.
At this point, dev_get_drvdata() would return NULL because dev_set_drvdata() was never reached, leading to a kernel panic when tb->lock is accessed.
Quoted 19 lines
drivers/thunderbolt/nhi.c +19
-0+ + scoped_guard(spinlock_irq, &nhi->lock) { +if (nhi->going_away || nhi->removing || nhi->suspended) +return ; +if (bitmap_empty (nhi->dma_hops_used,nhi_dma_hops_bits (nhi))) +return ; +if (nhi_dma_rings_running (nhi)) +return ; + +/* Keep the DMA rings from starting over the reset */ + nhi->resetting = true; + } + +if (!tb_ctl_stop_if_idle (tb->ctl)) { + scoped_guard(spinlock_irq, &nhi->lock) { + nhi->resetting = false; +queue_delayed_work (system_long_wq, &nhi->reset_work, +msecs_to_jiffies (100 )); + }
drivers/thunderbolt/nhi.c struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, · line 880
Quoted 10 lines
drivers/thunderbolt/nhi.c +10
-0+return ; + } +nhi_reset_interface (nhi); +tb_ctl_start (tb->ctl); + + scoped_guard(spinlock_irq, &nhi->lock) { +bitmap_zero (nhi->dma_hops_used,nhi_dma_hops_bits (nhi)); + nhi->resetting = false; + } +}
[Severity: High]
Does this sequence open a race condition that permanently loses the control channel interrupt?
When tb_ctl_start() is called, nhi->resetting is still true. The hardware can immediately receive a packet and trigger an MSI-X interrupt, which masks the interrupt and schedules the polling callback.
When the callback processes the packet and calls tb_ring_poll_complete(), the check for !ring->nhi->resetting will fail because nhi->resetting is only cleared after tb_ctl_start() returns. This skips the unmasking of the interrupt and leaves the control channel permanently hung.
Should nhi->resetting = false be executed before calling tb_ctl_start() to close this window?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 3/3] thunderbolt: Add quirk to reset host interface for AMD USB4 routers
In reply to Basavaraj NatikarThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] thunderbolt: NULL pointer dereference in nhi_reset_work() due to late dev_set_drvdata() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] thunderbolt: NULL pointer dereference in nhi_reset_work() due to late dev_set_drvdata() --
commit 9823327103fd3f6d1af1a55c73b76fed9264dad0
Author: Basavaraj Natikar <[email protected]>
thunderbolt: Add quirk to reset host interface for AMD USB4 routers
This commit adds a quirk for AMD USB4 routers to reset the host interface before reprogramming a reused DMA HopID, preventing TX ring hangs. The reset operation waits for DMA rings to stop while leaving other tunnels intact.
Quoted 4 lines
drivers/thunderbolt/pci.c +0
-0
[ ... ]
Quoted 6 lines
drivers/thunderbolt/pci.c +2
-0@@ -63,6 +63,27 @@ static void nhi_pci_check_quirks(struct tb_nhi_pci *nhi_pci) nhi->quirks |=QUIRK_E2E ;break ; } + }else if (pdev->vendor ==PCI_VENDOR_ID_AMD ) { +switch (pdev->device) {
[ ... ]
drivers/thunderbolt/pci.c static void nhi_pci_check_quirks(struct tb_nhi_pci *nhi_pci) · line 84
Quoted 8 lines
drivers/thunderbolt/pci.c +8
-0+case PCI_DEVICE_ID_AMD_1AH_M70H_NHI0 : +case PCI_DEVICE_ID_AMD_1AH_M70H_NHI1 : +/* +* These hosts may hang the Tx ring if its HopID is +* programmed again without a host interface reset +* in between. +*/ + nhi->quirks |=QUIRK_RESET_DMA_ON_REUSE ;
[Severity: High]
Does adding this quirk expose a race condition during probe that leads to a NULL pointer dereference in nhi_reset_work()?
When QUIRK_RESET_DMA_ON_REUSE is set, it seems nhi_reset_work() can be queued when a DMA ring stops. However, looking at the initialization sequence in nhi_probe(), drvdata is set very late:
drivers/thunderbolt/nhi.c:nhi_probe() {
...
res = tb_domain_add(tb, host_reset);
if (res) {
...
}
dev_set_drvdata(dev, tb);
...
}During tb_domain_add(), event processing is started and the lock is dropped before returning:
drivers/thunderbolt/domain.c:tb_domain_add() {
...
/* This starts event processing */
mutex_unlock(&tb->lock);
device_init_wakeup(&tb->dev, true);
...
}Because tb_domain_add() allows asynchronous events (like DMA ring start/stop operations) to execute before returning to nhi_probe(), nhi_reset_work() can be scheduled while drvdata is still NULL. If this happens, it leads to a crash when trying to acquire the lock:
drivers/thunderbolt/nhi.c:nhi_reset_work() {
struct tb *tb = dev_get_drvdata(nhi->dev);
/* The connection manager must be blocked over the reset */
guard(mutex)(&tb->lock);
...
}Can we ensure drvdata is set before enabling event processing, or otherwise prevent the reset work from running before tb_domain_add() finishes?
Quoted 4 lines
drivers/thunderbolt/pci.c +2
-0+break ; + } } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3