NTB: ntb_transport: Miscellaneous fixes
Subsystems (2)
v4 cover NTB: ntb_transport: Miscellaneous fixes
This series attempts to fix miscellaneous issues in ntb_transport.
Jon, ntb-next has moved forward, so I've rebased this series onto its latest tip. The rebase was conflict-free and needed no code changes. I've also updated a comment in patch 6 as Logan suggested.
Please consider picking this up for ntb-next.
Best regards,
Koichiro
---
Changes in v4:
- Rebase onto the current ntb-next.
- Clarify the memory-barrier comment in patch 6. (Logan)
- Collect Reviewed-by tags from v3. Thanks for reviewing!
Changes in v3:
- Convert flags to atomic_t in a new prep patch. (Frank)
- Drop the redundant TX credit assertion. (Sashiko)
- Collect Reviewed-by tags. Thanks to Frank, Logan and Dave.
Changes in v2:
- Address Sashiko feedback, including regressions introduced by v1, with new patches 5 and 10.
Note that to keep the series manageable, this revision does not attempt to fix all the reported pre-existing issues.
- Add a fix for client removal ordering (patch 14).
- Reorder patches, moving v1 patch 4 after the fixes it depends on.
- Add missing Cc: [email protected].
v3: https://lore.kernel.org/r/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/
Koichiro Den (15):
NTB: ntb_transport: Remove the device debugfs directory
NTB: ntb_transport: Start TX offload thread after queue setup
NTB: ntb_transport: Make link setup flags atomic
NTB: ntb_transport: Avoid deadlock when cancelling link work
NTB: ntb_transport: Publish link state after QP setup
NTB: ntb_transport: Avoid losing QP link-up requests
NTB: ntb_transport: Clear link state before QP cleanup
NTB: ntb_transport: Stop QP work before freeing a queue
NTB: ntb_transport: Stop RX tasklet scheduling before freeing a queue
NTB: ntb_transport: Drain RX tasklets during link cleanup
NTB: ntb_transport: Wait for RX completions before resetting a QP
NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
NTB: ntb_transport: Clear QP pointers when freeing an MW
NTB: ntb_transport: Abort link setup on QP MW allocation failure
NTB: ntb_transport: Remove clients before freeing transport resources
drivers/ntb/ntb_transport.c | 245 ++++++++++++++++++++++++------------ 1 file changed, 168 insertions(+), 77 deletions(-)
base-commit: 0aa2e6bc39f17e1199345aaf21278e3ea25e6036
Signature · 1 line
2.54.0
PATCH v4 1/15 NTB: ntb_transport: Remove the device debugfs directory
ntb_transport_free() removes QP debugfs directories but leaves the device directory. On rebind, debugfs_create_dir() fails with -EEXIST and QP statistics files are not recreated. Module unload masks this by removing the entire debugfs tree.
To reproduce:
# ls /sys/kernel/debug/ntb_transport/0001:10:00.0/ qp0 # echo 0001:10:00.0 > /sys/bus/ntb/drivers/ntb_transport/unbind # ls /sys/kernel/debug/ntb_transport/ 0001:10:00.0 <-- should not remain # echo 0001:10:00.0 > /sys/bus/ntb/drivers/ntb_transport/bind
.. and then dmesg shows:
debugfs: '0001:10:00.0' already exists in 'ntb_transport'
# ls /sys/kernel/debug/ntb_transport/0001:10:00.0/ (nothing) <-- should be 'qp0'
Remove the device debugfs tree on teardown and probe failure.
Verified that unbind removes the directory and rebind recreates qp0.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/ntb/ntb_transport.c +3 -1
@@ -1382,6 +1382,7 @@ static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev) err3:ntb_clear_ctx (ndev); err2: +debugfs_remove_recursive (nt->debugfs_node_dir);kfree (nt->qp_vec); err1:while (i--) {
@@ -1401,6 +1402,8 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)u64 qp_bitmap_alloc;int i; +debugfs_remove_recursive (nt->debugfs_node_dir); +ntb_transport_link_cleanup (nt);cancel_work_sync (&nt->link_cleanup);cancel_delayed_work_sync (&nt->link_work);
@@ -1412,7 +1415,6 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev) qp = &nt->qp_vec[i];if (qp_bitmap_alloc &BIT_ULL (i))ntb_transport_free_queue (qp); -debugfs_remove_recursive (qp->debugfs_dir); }ntb_link_disable (ndev);
Signature · 1 line
2.54.0
PATCH v4 2/15 NTB: ntb_transport: Start TX offload thread after queue setup
In reply to Koichiro Denntb_transport_create_queue() starts the per-QP TX offload thread before DMA mappings and queue entries are allocated. If later setup fails, the error path returns the QP to the free bitmap without stopping the thread. A retry can then reinitialize its waitqueue while the old thread is still waiting on it.
Start the thread after queue setup.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/ntb/ntb_transport.c +14 -14
@@ -2057,20 +2057,6 @@ ntb_transport_create_queue(void *data, struct device *client_dev, qp->tx_handler = handlers->tx_handler; qp->event_handler = handlers->event_handler; -init_waitqueue_head (&qp->tx_offload_wq); -if (tx_memcpy_offload) { - qp->tx_offload_thread =kthread_run (ntb_tx_memcpy_kthread, qp, -"ntb-txcpy/%s/%u" , -pci_name (ndev->pdev), qp->qp_num); -if (IS_ERR (qp->tx_offload_thread)) { -dev_warn (&nt->ndev->dev, -"tx memcpy offload thread creation failed: %ld; falling back to inline copy\n" , -PTR_ERR (qp->tx_offload_thread)); - qp->tx_offload_thread =NULL ; - } - }else - qp->tx_offload_thread =NULL ; -dma_cap_zero (dma_mask);dma_cap_set (DMA_MEMCPY , dma_mask);
@@ -2132,6 +2118,20 @@ ntb_transport_create_queue(void *data, struct device *client_dev, &qp->tx_free_q); } +init_waitqueue_head (&qp->tx_offload_wq); + qp->tx_offload_thread =NULL ; +if (tx_memcpy_offload) { + qp->tx_offload_thread =kthread_run (ntb_tx_memcpy_kthread, qp, +"ntb-txcpy/%s/%u" , +pci_name (ndev->pdev), qp->qp_num); +if (IS_ERR (qp->tx_offload_thread)) { +dev_warn (&nt->ndev->dev, +"tx memcpy offload thread creation failed: %ld; falling back to inline copy\n" , +PTR_ERR (qp->tx_offload_thread)); + qp->tx_offload_thread =NULL ; + } + } +ntb_db_clear (qp->ndev, qp_bit);ntb_db_clear_mask (qp->ndev, qp_bit);
Signature · 1 line
2.54.0
PATCH v4 3/15 NTB: ntb_transport: Make link setup flags atomic
In reply to Koichiro DenConvert nt->link_is_up and qp->client_ready to atomic_t and use atomic accessors throughout. This prepares for the unlocked cleanup check and the link-up ordering fixes that follow.
Leave control flow and locking unchanged.
---
Changes in v4:
- None.
Ref. https://lore.kernel.org/r/i3b4kyeuwyjssav2kne5uhxmltwl2bmug2weyfaujxtrwlkuox@ms6ozb55tmz5/
drivers/ntb/ntb_transport.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/ntb/ntb_transport.c +16 -15
@@ -47,6 +47,7 @@* Contact Information: * Jon Mason <[email protected]> */ +#include <linux/atomic.h> #include <linux/debugfs.h> #include <linux/delay.h> #include <linux/dmaengine.h>
@@ -142,7 +143,7 @@ struct ntb_transport_qp {struct dma_chan *tx_dma_chan;struct dma_chan *rx_dma_chan; -bool client_ready; +atomic_t client_ready;bool link_is_up;bool active;
@@ -249,7 +250,7 @@ struct ntb_transport_ctx {unsigned int msi_spad_offset;u64 msi_db_mask; -bool link_is_up; +atomic_t link_is_up;struct delayed_work link_work;struct work_struct link_cleanup;
@@ -945,7 +946,7 @@ static void ntb_qp_link_cleanup_work(struct work_struct *work)ntb_qp_link_cleanup (qp); -if (nt->link_is_up) +if (atomic_read (&nt->link_is_up))schedule_delayed_work (&qp->link_work,msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); }
@@ -972,7 +973,7 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)cancel_delayed_work_sync (&qp->link_work); } -if (!nt->link_is_up) +if (!atomic_read (&nt->link_is_up))cancel_delayed_work_sync (&nt->link_work);for (i =0 ; i < nt->mw_count; i++)
@@ -1084,7 +1085,7 @@ static void ntb_transport_link_work(struct work_struct *work) goto out1; } - nt->link_is_up = true; +atomic_set (&nt->link_is_up, true);for (i =0 ; i < nt->qp_count; i++) {struct ntb_transport_qp *qp = &nt->qp_vec[i];
@@ -1092,7 +1093,7 @@ static void ntb_transport_link_work(struct work_struct *work)ntb_transport_setup_qp_mw (nt, i);ntb_transport_setup_qp_peer_msi (nt, i); -if (qp->client_ready) +if (atomic_read (&qp->client_ready))schedule_delayed_work (&qp->link_work,0 ); }
@@ -1121,7 +1122,7 @@ static void ntb_qp_link_work(struct work_struct *work)struct ntb_transport_ctx *nt = qp->transport;int val; -WARN_ON (!nt->link_is_up); +WARN_ON (!atomic_read (&nt->link_is_up)); val =ntb_spad_read (nt->ndev,QP_LINKS );
@@ -1141,7 +1142,7 @@ static void ntb_qp_link_work(struct work_struct *work)if (qp->active)tasklet_schedule (&qp->rxc_db_work); - }else if (nt->link_is_up) + }else if (atomic_read (&nt->link_is_up))schedule_delayed_work (&qp->link_work,msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); }
@@ -1165,7 +1166,7 @@ static int ntb_transport_init_queue(struct ntb_transport_ctx *nt, qp->qp_num = qp_num; qp->transport = nt; qp->ndev = nt->ndev; - qp->client_ready = false; +atomic_set (&qp->client_ready, false); qp->event_handler =NULL ;ntb_qp_link_context_reset (qp);
@@ -1373,7 +1374,7 @@ static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)if (rc) goto err3; - nt->link_is_up = false; +atomic_set (&nt->link_is_up, false);ntb_link_enable (ndev,NTB_SPEED_AUTO ,NTB_WIDTH_AUTO );ntb_link_event (ndev);
@@ -1457,7 +1458,7 @@ static void ntb_complete_rxc(struct ntb_transport_qp *qp)spin_unlock_irqrestore (&qp->ntb_rx_q_lock, irqflags); -if (qp->rx_handler && qp->client_ready) +if (qp->rx_handler &&atomic_read (&qp->client_ready)) qp->rx_handler (qp, qp->cb_data, cb_data, len);spin_lock_irqsave (&qp->ntb_rx_q_lock, irqflags);
@@ -2271,7 +2272,7 @@ void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)struct ntb_queue_entry *entry;void *buf; -if (!qp || qp->client_ready) +if (!qp ||atomic_read (&qp->client_ready))return NULL ; entry =ntb_list_rm (&qp->ntb_rx_q_lock, &qp->rx_pend_q);
@@ -2388,9 +2389,9 @@ void ntb_transport_link_up(struct ntb_transport_qp *qp)if (!qp)return ; - qp->client_ready = true; +atomic_set (&qp->client_ready, true); -if (qp->transport->link_is_up) +if (atomic_read (&qp->transport->link_is_up))schedule_delayed_work (&qp->link_work,0 ); }EXPORT_SYMBOL_GPL (ntb_transport_link_up);
@@ -2410,7 +2411,7 @@ void ntb_transport_link_down(struct ntb_transport_qp *qp)if (!qp)return ; - qp->client_ready = false; +atomic_set (&qp->client_ready, false); val =ntb_spad_read (qp->ndev,QP_LINKS );
Signature · 1 line
2.54.0
PATCH v4 4/15 NTB: ntb_transport: Avoid deadlock when cancelling link work
In reply to Koichiro DenDuring initial link setup, ntb_transport_link_work() can retry with nt->link_is_up still false. A retry can block on link_event_lock while cleanup holds it and waits in cancel_delayed_work_sync(), leading to deadlock.
Move the conditional cancellation outside link_event_lock, before QP cleanup. Keep QP cleanup and MW release under the lock so link work cannot restart QPs between them. Put the locking in ntb_transport_link_cleanup() to cover both worker and remove paths.
---
Changes in v4:
- None.
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/ntb/ntb_transport.c +5 -4
@@ -962,6 +962,11 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)u64 qp_bitmap_alloc;unsigned int i, count; +if (!atomic_read (&nt->link_is_up)) +cancel_delayed_work_sync (&nt->link_work); + +guard (mutex)(&nt->link_event_lock); + qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;/* Pass along the info to any clients */
@@ -973,9 +978,6 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)cancel_delayed_work_sync (&qp->link_work); } -if (!atomic_read (&nt->link_is_up)) -cancel_delayed_work_sync (&nt->link_work); -for (i =0 ; i < nt->mw_count; i++)ntb_free_mw (nt, i);
@@ -993,7 +995,6 @@ static void ntb_transport_link_cleanup_work(struct work_struct *work)struct ntb_transport_ctx *nt = container_of(work, struct ntb_transport_ctx, link_cleanup); -guard (mutex)(&nt->link_event_lock);ntb_transport_link_cleanup (nt); }
Signature · 1 line
2.54.0
PATCH v4 5/15 NTB: ntb_transport: Publish link state after QP setup
In reply to Koichiro Denntb_transport_link_work() marks the transport link up before setting up the QPs' MW and peer MSI state. A concurrent ntb_transport_link_up() can then queue QP link work, which may enable RX and notify the client before setup finishes.
Publish link_is_up with a release store after setting up all QPs, and use acquire loads before queuing QP link work.
---
Changes in v4:
- None.
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 40 +++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-)
drivers/ntb/ntb_transport.c +25 -15
@@ -923,6 +923,16 @@ static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp) qp->remote_rx_info->entry = qp->rx_max_entry -1 ; } +static void ntb_transport_schedule_qp_link (struct ntb_transport_qp *qp, +unsigned long delay) +{ +struct ntb_transport_ctx *nt = qp->transport; + +/* Pair with the link publication in ntb_transport_link_work(). */ +if (atomic_read_acquire (&nt->link_is_up)) +schedule_delayed_work (&qp->link_work, delay); +} +static void ntb_qp_link_cleanup (struct ntb_transport_qp *qp) {struct ntb_transport_ctx *nt = qp->transport;
@@ -942,13 +952,10 @@ static void ntb_qp_link_cleanup_work(struct work_struct *work)struct ntb_transport_qp *qp =container_of (work, struct ntb_transport_qp, link_cleanup); -struct ntb_transport_ctx *nt = qp->transport;ntb_qp_link_cleanup (qp); - -if (atomic_read (&nt->link_is_up)) -schedule_delayed_work (&qp->link_work, -msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); +ntb_transport_schedule_qp_link (qp, +msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); }static void ntb_qp_link_down (struct ntb_transport_qp *qp)
@@ -1086,16 +1093,19 @@ static void ntb_transport_link_work(struct work_struct *work) goto out1; } -atomic_set (&nt->link_is_up, true); -for (i =0 ; i < nt->qp_count; i++) { -struct ntb_transport_qp *qp = &nt->qp_vec[i]; -ntb_transport_setup_qp_mw (nt, i);ntb_transport_setup_qp_peer_msi (nt, i); + } + +/* Publish the link only after every QP has been set up. */ +atomic_set_release (&nt->link_is_up, true); + +for (i =0 ; i < nt->qp_count; i++) { +struct ntb_transport_qp *qp = &nt->qp_vec[i];if (atomic_read (&qp->client_ready)) -schedule_delayed_work (&qp->link_work,0 ); +ntb_transport_schedule_qp_link (qp,0 ); }return ;
@@ -1143,9 +1153,10 @@ static void ntb_qp_link_work(struct work_struct *work)if (qp->active)tasklet_schedule (&qp->rxc_db_work); - }else if (atomic_read (&nt->link_is_up)) -schedule_delayed_work (&qp->link_work, -msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); + }else { +ntb_transport_schedule_qp_link (qp, +msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT )); + } } static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
@@ -2392,8 +2403,7 @@ void ntb_transport_link_up(struct ntb_transport_qp *qp)atomic_set (&qp->client_ready, true); -if (atomic_read (&qp->transport->link_is_up)) -schedule_delayed_work (&qp->link_work,0 ); +ntb_transport_schedule_qp_link (qp,0 ); }EXPORT_SYMBOL_GPL (ntb_transport_link_up);
Signature · 1 line
2.54.0
PATCH v4 6/15 NTB: ntb_transport: Avoid losing QP link-up requests
In reply to Koichiro Denntb_netdev_open() can call ntb_transport_link_up() while the transport worker is completing setup on another CPU. Concurrent transport setup and a client link-up request can both read the other's flag as false and leave QP link work unqueued. The QP then stays down until another link event or client link-up request.
This is the store-buffering pattern described in tools/memory-model/Documentation/recipes.txt ("Store buffering").
Add a full barrier between the store and load on each side.
---
Changes in v4:
- Clarify the memory-barrier comment. (Logan)
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/ntb/ntb_transport.c +10 -0
@@ -1101,6 +1101,13 @@ static void ntb_transport_link_work(struct work_struct *work)/* Publish the link only after every QP has been set up. */ atomic_set_release (&nt->link_is_up, true); +/* +* Order the link_is_up store before the client_ready loads below, so +* that this path or ntb_transport_link_up() is guaranteed to see the +* other's flag. Pairs with smp_mb() in ntb_transport_link_up(). +*/ +smp_mb (); +for (i =0 ; i < nt->qp_count; i++) {struct ntb_transport_qp *qp = &nt->qp_vec[i];
@@ -2403,6 +2410,9 @@ void ntb_transport_link_up(struct ntb_transport_qp *qp)atomic_set (&qp->client_ready, true); +/* Pairs with the barrier in ntb_transport_link_work(). */ +smp_mb (); +ntb_transport_schedule_qp_link (qp,0 ); }EXPORT_SYMBOL_GPL (ntb_transport_link_up);
Signature · 1 line
2.54.0
PATCH v4 7/15 NTB: ntb_transport: Clear link state before QP cleanup
In reply to Koichiro DenCleanup leaves the transport link marked up after releasing its MWs. A subsequent client link-up request can therefore start QP link work before the transport has been set up again.
Clear link_is_up before cancelling QP work and releasing the MWs. Have QP link work return if the transport went down after it was queued.
---
Changes in v4:
- None.
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/ntb/ntb_transport.c +5 -1
@@ -974,6 +974,8 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)guard (mutex)(&nt->link_event_lock); +atomic_set (&nt->link_is_up, false); + qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;/* Pass along the info to any clients */
@@ -1140,7 +1142,9 @@ static void ntb_qp_link_work(struct work_struct *work)struct ntb_transport_ctx *nt = qp->transport;int val; -WARN_ON (!atomic_read (&nt->link_is_up)); +/* Pair with the link publication in ntb_transport_link_work(). */ +if (!atomic_read_acquire (&nt->link_is_up)) +return ; val =ntb_spad_read (nt->ndev,QP_LINKS );
Signature · 1 line
2.54.0
PATCH v4 8/15 NTB: ntb_transport: Stop QP work before freeing a queue
In reply to Koichiro Denntb_transport_free_queue() cancels qp->link_work but not qp->link_cleanup. A peer link-down message can queue cleanup while ntb_netdev is freeing the QP. Cleanup can then requeue link work after the queue resources have been freed.
Disable and wait for cleanup, then link work, before freeing resources. Unlike cancel, disable also prevents the RX tasklet and transport link setup from queuing more work. Enable the works only after queue creation succeeds.
Clear client_ready first so RX completions and transport link setup see that the client is no longer ready. Clear link_is_up and active after the workers stop, since link work can set both back to true.
---
Changes in v4:
- None.
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/ntb/ntb_transport.c +9 -2
@@ -1240,6 +1240,8 @@ static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,INIT_DELAYED_WORK (&qp->link_work, ntb_qp_link_work);INIT_WORK (&qp->link_cleanup, ntb_qp_link_cleanup_work); +disable_delayed_work (&qp->link_work); +disable_work (&qp->link_cleanup);spin_lock_init (&qp->ntb_rx_q_lock);spin_lock_init (&qp->ntb_tx_free_q_lock);
@@ -2156,6 +2158,9 @@ ntb_transport_create_queue(void *data, struct device *client_dev, } } +enable_work (&qp->link_cleanup); +enable_delayed_work (&qp->link_work); +ntb_db_clear (qp->ndev, qp_bit);ntb_db_clear_mask (qp->ndev, qp_bit);
@@ -2201,6 +2206,10 @@ void ntb_transport_free_queue(struct ntb_transport_qp *qp) pdev = qp->ndev->pdev; +atomic_set (&qp->client_ready, false); +disable_work_sync (&qp->link_cleanup); +disable_delayed_work_sync (&qp->link_work); + qp->link_is_up = false; qp->active = false;if (qp->tx_offload_thread) {
@@ -2248,8 +2257,6 @@ void ntb_transport_free_queue(struct ntb_transport_qp *qp)ntb_db_set_mask (qp->ndev, qp_bit);tasklet_kill (&qp->rxc_db_work); -cancel_delayed_work_sync (&qp->link_work); - qp->cb_data =NULL ; qp->rx_handler =NULL ; qp->tx_handler =NULL ;
Signature · 1 line
2.54.0
PATCH v4 9/15 NTB: ntb_transport: Stop RX tasklet scheduling before freeing a queue
In reply to Koichiro DenA caller can read qp->active before teardown clears it, then schedule the RX tasklet after tasklet_kill() returns. The MSI handler does not check active at all. Teardown also releases DMA channels before draining the tasklet.
Protect active updates and the check-and-schedule sequence with rx_sched_lock, including the MSI path. Clear active under the lock, then drain the tasklet before releasing DMA channels or queue entries. QP link work is already disabled, so it cannot reactivate RX. Use a separate lock to avoid contention with RX list operations.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 50 +++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 19 deletions(-)
drivers/ntb/ntb_transport.c +31 -19
@@ -180,6 +180,8 @@ struct ntb_transport_qp {unsigned int rx_max_frame;unsigned int rx_alloc_entry;dma_cookie_t last_cookie; +/* Protect active and RX tasklet scheduling. */ +spinlock_t rx_sched_lock;struct tasklet_struct rxc_db_work;void (*event_handler)(void *data,int status);
@@ -650,11 +652,26 @@ static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,return 0 ; } +static void ntb_transport_set_qp_active (struct ntb_transport_qp *qp,bool active) +{ +guard (spinlock_irqsave)(&qp->rx_sched_lock); + + qp->active = active; +} + +static void ntb_transport_schedule_rxc (struct ntb_transport_qp *qp) +{ +guard (spinlock_irqsave)(&qp->rx_sched_lock); + +if (qp->active) +tasklet_schedule (&qp->rxc_db_work); +} +static irqreturn_t ntb_transport_isr (int irq,void *dev) {struct ntb_transport_qp *qp = dev; -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp);return IRQ_HANDLED ; }
@@ -896,7 +913,7 @@ static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,static void ntb_qp_link_context_reset (struct ntb_transport_qp *qp) { qp->link_is_up = false; - qp->active = false; +ntb_transport_set_qp_active (qp, false); qp->tx_index =0 ; qp->rx_index =0 ;
@@ -1157,13 +1174,12 @@ static void ntb_qp_link_work(struct work_struct *work)if (val &BIT (qp->qp_num)) {dev_info (&pdev->dev,"qp %d: Link Up\n" , qp->qp_num); qp->link_is_up = true; - qp->active = true; +ntb_transport_set_qp_active (qp, true);if (qp->event_handler) qp->event_handler (qp->cb_data, qp->link_is_up); -if (qp->active) -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp); }else {ntb_transport_schedule_qp_link (qp,msecs_to_jiffies (NTB_LINK_DOWN_TIMEOUT ));
@@ -1191,6 +1207,7 @@ static int ntb_transport_init_queue(struct ntb_transport_ctx *nt, qp->ndev = nt->ndev;atomic_set (&qp->client_ready, false); qp->event_handler =NULL ; +spin_lock_init (&qp->rx_sched_lock);ntb_qp_link_context_reset (qp);if (mw_num < qp_count % mw_count)
@@ -1728,8 +1745,7 @@ static void ntb_transport_rxc_db(unsigned long data)if (i == qp->rx_max_entry) {/* there is more work to do */ -if (qp->active) -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp); }else if (ntb_db_read (qp->ndev) &BIT_ULL (qp->qp_num)) {/* the doorbell bit is set: clear it */ ntb_db_clear (qp->ndev,BIT_ULL (qp->qp_num));
@@ -1740,8 +1756,7 @@ static void ntb_transport_rxc_db(unsigned long data)* ntb_process_rxc and clearing the doorbell bit: * there might be some more work to do. */ -if (qp->active) -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp); } }
@@ -2210,7 +2225,11 @@ void ntb_transport_free_queue(struct ntb_transport_qp *qp)disable_work_sync (&qp->link_cleanup);disable_delayed_work_sync (&qp->link_work); qp->link_is_up = false; - qp->active = false; +ntb_transport_set_qp_active (qp, false); + + qp_bit =BIT_ULL (qp->qp_num); +ntb_db_set_mask (qp->ndev, qp_bit); +tasklet_kill (&qp->rxc_db_work);if (qp->tx_offload_thread) {kthread_stop (qp->tx_offload_thread);
@@ -2252,11 +2271,6 @@ void ntb_transport_free_queue(struct ntb_transport_qp *qp)dma_release_channel (chan); } - qp_bit =BIT_ULL (qp->qp_num); - -ntb_db_set_mask (qp->ndev, qp_bit); -tasklet_kill (&qp->rxc_db_work); - qp->cb_data =NULL ; qp->rx_handler =NULL ; qp->tx_handler =NULL ;
@@ -2351,8 +2365,7 @@ int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,ntb_list_add (&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q); -if (qp->active) -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp);return 0 ; }
@@ -2549,8 +2562,7 @@ static void ntb_transport_doorbell_callback(void *data, int vector) qp_num =__ffs (db_bits); qp = &nt->qp_vec[qp_num]; -if (qp->active) -tasklet_schedule (&qp->rxc_db_work); +ntb_transport_schedule_rxc (qp); db_bits &= ~BIT_ULL (qp_num); }
Signature · 1 line
2.54.0
PATCH v4 10/15 NTB: ntb_transport: Drain RX tasklets during link cleanup
In reply to Koichiro Denntb_qp_link_cleanup() cancels QP link work but does not wait for the RX tasklet. The tasklet can still be processing the ring while cleanup resets the QP, and transport link cleanup can free the MW before the tasklet finishes.
Clear active under rx_sched_lock and drain the tasklet before resetting the QP. Temporarily disable QP link work so a concurrent client link-up request cannot reactivate RX during cleanup, then re-enable it for the existing link setup paths.
This does not drain RX DMA transfers or their completion callbacks. The next patch waits for RX DMA and its completion path to finish accessing the MW.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/ntb/ntb_transport.c +6 -1
@@ -957,11 +957,16 @@ static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)dev_info (&pdev->dev,"qp %d: Link Cleanup\n" , qp->qp_num); -cancel_delayed_work_sync (&qp->link_work); +disable_delayed_work_sync (&qp->link_work); +ntb_transport_set_qp_active (qp, false); +tasklet_kill (&qp->rxc_db_work); +ntb_qp_link_down_reset (qp);if (qp->event_handler) qp->event_handler (qp->cb_data, qp->link_is_up); + +enable_delayed_work (&qp->link_work); }static void ntb_qp_link_cleanup_work (struct work_struct *work)
Signature · 1 line
2.54.0
PATCH v4 11/15 NTB: ntb_transport: Wait for RX completions before resetting a QP
In reply to Koichiro DenTransport link cleanup can free an MW still used by RX DMA or its completion path. QP-only cleanup retains the MW, but can restart RX on ring slots whose old completion callbacks have not yet cleared the headers.
Wait for rx_post_q to empty before resetting the QP. ntb_complete_rxc() finishes its MW accesses before removing each entry under ntb_rx_q_lock, so cleanup can free the MW without racing with these RX accesses. Using dmaengine_terminate_sync() instead would not work with drivers such as IOAT that lack the required ops. Cookie-based waits would not work with DMA_COMPLETION_NO_ORDER either.
DMA teardown in ntb_transport_free_queue() is unchanged.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/ntb/ntb_transport.c +18 -0
@@ -950,6 +950,13 @@ static void ntb_transport_schedule_qp_link(struct ntb_transport_qp *qp,schedule_delayed_work (&qp->link_work, delay); } +static bool ntb_transport_rx_idle (struct ntb_transport_qp *qp) +{ +guard (spinlock_irqsave)(&qp->ntb_rx_q_lock); + +return list_empty (&qp->rx_post_q); +} +static void ntb_qp_link_cleanup (struct ntb_transport_qp *qp) {struct ntb_transport_ctx *nt = qp->transport;
@@ -960,6 +967,17 @@ static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)disable_delayed_work_sync (&qp->link_work);ntb_transport_set_qp_active (qp, false);tasklet_kill (&qp->rxc_db_work); +/* +* Some DMA engines lack terminate/synchronize ops (e.g. IOAT), and +* DMA_COMPLETION_NO_ORDER rules out cookie-based waits. +* +* Waiting for rx_post_q to empty suffices: ntb_complete_rxc() finishes +* its MW accesses before removing each entry under ntb_rx_q_lock. +* qp->active is false and rxc_db_work is stopped, so no new RX DMA +* can be submitted. +*/ +while (!ntb_transport_rx_idle (qp)) +fsleep (1000 );ntb_qp_link_down_reset (qp);
Signature · 1 line
2.54.0
PATCH v4 12/15 NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
In reply to Koichiro DenThe next patch clears remote_rx_info when freeing its MW. ntb_transport_tx_free_entry() and debugfs stats reads can run during link cleanup, so make them handle a NULL pointer.
The pointer is accessed locklessly. Use READ_ONCE() and WRITE_ONCE() to prevent compiler-induced tearing, and retain the read value so the NULL check and dereference use the same pointer.
Also drop the redundant credit assertion in ntb_async_tx(). The caller checks for space, but cleanup can clear remote_rx_info before this second check.
---
Changes in v4:
- None.
v3: https://lore.kernel.org/r/[email protected]/
drivers/ntb/ntb_transport.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
drivers/ntb/ntb_transport.c +17 -6
@@ -490,6 +490,7 @@ EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);static int ntb_qp_debugfs_stats_show (struct seq_file *s,void *v) {struct ntb_transport_qp *qp = s->private; +struct ntb_rx_info *remote_rx_info;if (!qp || !qp->link_is_up)return 0 ;
@@ -517,7 +518,9 @@ static int ntb_qp_debugfs_stats_show(struct seq_file *s, void *v)seq_printf (s,"tx_err_no_buf - %llu\n" , qp->tx_err_no_buf);seq_printf (s,"tx_mw - \t0x%p\n" , qp->tx_mw);seq_printf (s,"tx_index (H) - \t%u\n" , qp->tx_index); -seq_printf (s,"RRI (T) - \t%u\n" , qp->remote_rx_info->entry); + remote_rx_info =READ_ONCE (qp->remote_rx_info); +if (remote_rx_info) +seq_printf (s,"RRI (T) - \t%u\n" , remote_rx_info->entry);seq_printf (s,"tx_max_entry - \t%u\n" , qp->tx_max_entry);seq_printf (s,"free tx - \t%u\n" ,ntb_transport_tx_free_entry (qp));seq_putc (s,'\n' );
@@ -612,7 +615,7 @@ static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count); rx_size -=sizeof (struct ntb_rx_info ); - qp->remote_rx_info = qp->rx_buff + rx_size; +WRITE_ONCE (qp->remote_rx_info, qp->rx_buff + rx_size);/* Due to housekeeping, there must be atleast 2 buffs */ qp->rx_max_frame =min (transport_mtu, rx_size /2 );
@@ -935,9 +938,12 @@ static void ntb_qp_link_context_reset(struct ntb_transport_qp *qp)static void ntb_qp_link_down_reset (struct ntb_transport_qp *qp) { +struct ntb_rx_info *remote_rx_info; +ntb_qp_link_context_reset (qp); -if (qp->remote_rx_info) - qp->remote_rx_info->entry = qp->rx_max_entry -1 ; + remote_rx_info =READ_ONCE (qp->remote_rx_info); +if (remote_rx_info) + remote_rx_info->entry = qp->rx_max_entry -1 ; } static void ntb_transport_schedule_qp_link(struct ntb_transport_qp *qp,
@@ -1990,7 +1996,6 @@ static void ntb_async_tx(struct ntb_transport_qp *qp, hdr = offset + qp->tx_max_frame -sizeof (struct ntb_payload_header ); entry->tx_hdr = hdr; -WARN_ON_ONCE (!ntb_transport_tx_free_entry (qp));WRITE_ONCE (qp->tx_index, (qp->tx_index +1 ) % qp->tx_max_entry);iowrite32 (entry->len, &hdr->len);
@@ -2559,8 +2564,14 @@ EXPORT_SYMBOL_GPL(ntb_transport_max_size);unsigned int ntb_transport_tx_free_entry (struct ntb_transport_qp *qp) { +struct ntb_rx_info *remote_rx_info =READ_ONCE (qp->remote_rx_info);unsigned int head = qp->tx_index; -unsigned int tail = qp->remote_rx_info->entry; +unsigned int tail; + +if (!remote_rx_info) +return 0 ; + + tail = remote_rx_info->entry;return tail >= head ? tail - head : qp->tx_max_entry + tail - head; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] ntb: TOCTOU use-after-free on remote_rx_info during MW teardown --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ntb: TOCTOU use-after-free on remote_rx_info during MW teardown --
commit 19817e05f16664b733ed390594b75eacca761094
Author: Koichiro Den <[email protected]>
NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
This commit updates qp->remote_rx_info accesses to use READ_ONCE() and WRITE_ONCE() and check for NULL in preparation for clearing remote_rx_info during memory window teardown, and removes the redundant free-entry warning in ntb_async_tx().
Quoted 4 lines
drivers/ntb/ntb_transport.c +0
-0
[ ... ]
Quoted 6 lines
drivers/ntb/ntb_transport.c +1
-0@@ -2559,8 +2564,14 @@ EXPORT_SYMBOL_GPL(ntb_transport_max_size);unsigned int ntb_transport_tx_free_entry (struct ntb_transport_qp *qp) { +struct ntb_rx_info *remote_rx_info =READ_ONCE (qp->remote_rx_info);unsigned int head = qp->tx_index;
drivers/ntb/ntb_transport.c EXPORT_SYMBOL_GPL(ntb_transport_max_size); · line 2577
Quoted 10 lines
drivers/ntb/ntb_transport.c +6
-1-unsigned int tail = qp->remote_rx_info->entry; +unsigned int tail; + +if (!remote_rx_info) +return 0 ; + + tail = remote_rx_info->entry;return tail >= head ? tail - head : qp->tx_max_entry + tail - head; }
[Severity: High]
Can ntb_transport_tx_free_entry(), ntb_qp_debugfs_stats_show(), and ntb_qp_link_down_reset() still access freed DMA memory if ntb_free_mw() runs between READ_ONCE(qp->remote_rx_info) and the dereference of remote_rx_info->entry?
qp->remote_rx_info points into the inbound MW DMA buffer allocated in ntb_alloc_mw_buffer() and freed by dma_free_attrs() in ntb_free_mw(). In the next commit in the series, commit "NTB: ntb_transport: Clear QP pointers when freeing an MW", ntb_free_mw() clears qp->remote_rx_info with WRITE_ONCE() and immediately frees the buffer without waiting for concurrent lockless readers or writers to finish:
CPU0 CPU1ntb_netdev_tx_timer()
ntb_transport_tx_free_entry(qp)
remote_rx_info =
READ_ONCE(qp->remote_rx_info);
if (!remote_rx_info)
ntb_transport_link_cleanup_work()
ntb_transport_link_cleanup()
ntb_free_mw()
WRITE_ONCE(qp->remote_rx_info,
NULL);
dma_free_attrs(...);
tail = remote_rx_info->entry; <-- use-after-freeDuring runtime link-down cleanup, ntb_qp_link_cleanup() notifies the client to stop the subqueue, but does not stop ntb_netdev_tx_timer() or serialize against in-flight ntb_qp_debugfs_stats_show() reads or ntb_transport_link_down() -> ntb_send_link_down() -> ntb_qp_link_down_reset() writes. Any caller delayed after loading a non-NULL remote_rx_info will read or write the freed DMA buffer.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v4 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
Quoted 4 lines · On Fri, Nov 27, 2026 at 02:21:12AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ntb: TOCTOU use-after-free onremote_rx_infoduring MW teardown --
Note this is a pre-existing issue, already flagged by Sashiko in v2 thread:
https://lore.kernel.org/r/[email protected]/
IOW, this is not a regression introduced by this series.
We discussed leaving the broader fix for separate work in the thread, and Dave was fine with that:
https://lore.kernel.org/r/[email protected]/
I'd prefer to keep that for this series.
Best regards,
Koichiro Den
Quoted 69 lines
commit
19817e05f16664b733ed390594b75eacca761094
Author: Koichiro Den <[email protected]>NTB:
ntb_transport: Prepare remote RX info accesses for MW teardownThis commit updates
qp->remote_rx_infoaccesses to useREAD_ONCE()andWRITE_ONCE()and check for NULL in preparation for clearingremote_rx_infoduring memory window teardown, and removes the redundant free-entry warning inntb_async_tx().Quoted 4 lines
drivers/ntb/ntb_transport.c +0
-0[ ... ]
Quoted 6 lines
drivers/ntb/ntb_transport.c +1
-0@@ -2559,8 +2564,14 @@ EXPORT_SYMBOL_GPL(ntb_transport_max_size);unsigned int ntb_transport_tx_free_entry (struct ntb_transport_qp *qp) { +struct ntb_rx_info *remote_rx_info =READ_ONCE (qp->remote_rx_info);unsigned int head = qp->tx_index;drivers/ntb/ntb_transport.c EXPORT_SYMBOL_GPL(ntb_transport_max_size); · line 2577
Quoted 10 lines
drivers/ntb/ntb_transport.c +6
-1-unsigned int tail = qp->remote_rx_info->entry; +unsigned int tail; + +if (!remote_rx_info) +return 0 ; + + tail = remote_rx_info->entry;return tail >= head ? tail - head : qp->tx_max_entry + tail - head; }[Severity: High]
Canntb_transport_tx_free_entry(),ntb_qp_debugfs_stats_show(), andntb_qp_link_down_reset()still access freed DMA memory ifntb_free_mw()runs betweenREAD_ONCE(qp->remote_rx_info)and the dereference ofremote_rx_info->entry?
qp->remote_rx_infopoints into the inbound MW DMA buffer allocated inntb_alloc_mw_buffer()and freed bydma_free_attrs()inntb_free_mw(). In the next commit in the series, commit "NTB:ntb_transport: Clear QP pointers when freeing an MW",ntb_free_mw()clearsqp->remote_rx_infowithWRITE_ONCE()and immediately frees the buffer without waiting for concurrent lockless readers or writers to finish:CPU0 CPU1
ntb_netdev_tx_timer()
ntb_transport_tx_free_entry(qp)remote_rx_info = READ_ONCE(qp->remote_rx_info); if (!remote_rx_info) ntb_transport_link_cleanup_work() ntb_transport_link_cleanup() ntb_free_mw() WRITE_ONCE(qp->remote_rx_info, NULL); dma_free_attrs(...); tail = remote_rx_info->entry; <-- use-after-freeDuring runtime link-down cleanup,
ntb_qp_link_cleanup()notifies the client to stop the subqueue, but does not stopntb_netdev_tx_timer()or serialize against in-flightntb_qp_debugfs_stats_show()reads orntb_transport_link_down()->ntb_send_link_down()->ntb_qp_link_down_reset()writes. Any caller delayed after loading a non-NULLremote_rx_infowill read or write the freed DMA buffer.Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v4 13/15 NTB: ntb_transport: Clear QP pointers when freeing an MW
In reply to Koichiro Denntb_transport_link_cleanup() frees MW buffers but leaves rx_buff and remote_rx_info pointing into them. With a QP still allocated, another link-down notification or transport unbind before MW setup runs again can make ntb_qp_link_down_reset() write to freed memory through remote_rx_info.
Clear both pointers in ntb_free_mw() for all QPs using that MW, including those without a client. This also covers link-setup failures.
---
Changes in v4:
- None.
How to reproduce:
1. Load ntb_transport and ntb_netdev on both sides and establish the transport/QP links once. Stop traffic, but leave ntb_netdev loaded on VHOST so its QPs remain allocated throughout the test.
2. On HOST, unload ntb_netdev and ntb_transport, leaving ntb_hw_epf bound:
modprobe -r ntb_netdev ntb_transport
Transport removal sends COMMAND_LINK_DOWN to VHOST. Wait for ntb_transport_link_cleanup_work() to return on VHOST, using a function-graph trace. The "Link Cleanup" message is printed before MW release and is not sufficient to establish completion. Do not bring the link back up before the next step.
3-(A). UAF via repeated link-down notification
Use ntb_tool on HOST to send another link-down request:
HOST# modprobe ntb_tool
HOST# echo N > "/sys/kernel/debug/ntb_tool/$ntb_host_dev/link"
==================================================================
BUG: KASAN: vmalloc-out-of-bounds in ntb_qp_link_down_reset+0x2c0..
...
Call trace:
...
__asan_report_store4_noabort+0x1c/0x28
ntb_qp_link_down_reset+0x2c0/0x2e0 [ntb_transport]
ntb_qp_link_cleanup+0xc4/0x148 [ntb_transport]
ntb_transport_link_cleanup+0x314/0x350 [ntb_transport]
ntb_transport_link_cleanup_work+0x2c/0x50 [ntb_transport]
process_one_work+0x5b8/0x12f0
...3-(B). UAF via transport removal after link-down
VHOST# echo "$ntb_vhost_dev" > \
/sys/bus/ntb/drivers/ntb_transport/unbind
==================================================================
BUG: KASAN: vmalloc-out-of-bounds in ntb_qp_link_down_reset+0x2c0..
...
Call trace:
...
__asan_report_store4_noabort+0x1c/0x28
ntb_qp_link_down_reset+0x2c0/0x2e0 [ntb_transport]
ntb_qp_link_cleanup+0xc4/0x148 [ntb_transport]
ntb_transport_link_cleanup+0x314/0x350 [ntb_transport]
ntb_transport_free+0x68/0x588 [ntb_transport]
ntb_remove+0x5c/0xa0 [ntb]Verified that neither test triggers a KASAN report with this patch.
drivers/ntb/ntb_transport.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/ntb/ntb_transport.c +7 -0
@@ -782,10 +782,17 @@ static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) {struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];struct device *dma_dev =ntb_get_dma_dev (nt->ndev); +unsigned int i;if (!mw->virt_addr)return ; +/* Drop references from every QP using this MW. */ +for (i = num_mw; i < nt->qp_count; i += nt->mw_count) { + nt->qp_vec[i].rx_buff =NULL ; +WRITE_ONCE (nt->qp_vec[i].remote_rx_info,NULL ); + } +ntb_mw_clear_trans (nt->ndev,PIDX , num_mw);dma_free_attrs (dma_dev, mw->alloc_size, mw->alloc_addr, mw->original_dma_addr,DMA_ATTR_FORCE_CONTIGUOUS );
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ntb_transport: ntb_free_mw() frees MW without synchronizing remote_rx_info --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ntb_transport: ntb_free_mw() frees MW without synchronizing remote_rx_info
--
commit 6c932f2e3a5a220eb89ffa33367cf59f97e380ad
Author: Koichiro Den <[email protected]>
NTB: ntb_transport: Clear QP pointers when freeing an MW
This commit clears rx_buff and remote_rx_info in ntb_free_mw() for all QPs using a memory window before freeing its backing buffer.
Quoted 12 lines
drivers/ntb/ntb_transport.c +1
-0@@ -782,10 +782,17 @@ static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) {struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];struct device *dma_dev =ntb_get_dma_dev (nt->ndev); +unsigned int i;if (!mw->virt_addr)return ;
drivers/ntb/ntb_transport.c static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) · line 798
Quoted 10 lines
drivers/ntb/ntb_transport.c +6
-0+/* Drop references from every QP using this MW. */ +for (i = num_mw; i < nt->qp_count; i += nt->mw_count) { + nt->qp_vec[i].rx_buff =NULL ; +WRITE_ONCE (nt->qp_vec[i].remote_rx_info,NULL ); + } +ntb_mw_clear_trans (nt->ndev,PIDX , num_mw);dma_free_attrs (dma_dev, mw->alloc_size, mw->alloc_addr, mw->original_dma_addr,DMA_ATTR_FORCE_CONTIGUOUS );
[Severity: Medium]
Can concurrent lockless accesses to remote_rx_info still race with dma_free_attrs() here and trigger a use-after-free?
ntb_free_mw() clears remote_rx_info with WRITE_ONCE() and immediately frees mw->alloc_addr without waiting for readers or writers that already loaded a non-NULL remote_rx_info pointer via READ_ONCE().
For example, ntb_transport_link_down() runs without nt->link_event_lock when a client closes or changes MTU:
CPU0 CPU1ntb_transport_link_down(qp)
if (qp->link_is_up)
ntb_send_link_down(qp)
msleep(100)
ntb_transport_link_cleanup(nt)
ntb_qp_link_cleanup(qp)
ntb_qp_link_down_reset(qp)
rri = READ_ONCE(qp->remote_rx_info)
ntb_free_mw(nt, num_mw)
WRITE_ONCE(qp->remote_rx_info,
NULL)
dma_free_attrs(...,
mw->alloc_addr,
...)
rri->entry = ... <-- use-after-free writeThe same race applies to lockless readers in ntb_transport_tx_free_entry() (called from ntb_netdev_tx_timer() or TX completion handlers, which ntb_qp_link_cleanup() does not stop) and ntb_qp_debugfs_stats_show().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH v4 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW
Quoted 5 lines · On Fri, Nov 27, 2026 at 02:24:57AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium]ntb_transport:ntb_free_mw()frees MW without synchronizingremote_rx_info
--
This is the same pre-existing lifetime race already flagged by Sashiko during the v2 review:
https://lore.kernel.org/r/[email protected]/
I'd prefer to handle the broader fix separately, as discussed (with Dave) in v2.
Best regards,
Koichiro Den
Quoted 66 lines
commit
6c932f2e3a5a220eb89ffa33367cf59f97e380ad
Author: Koichiro Den <[email protected]>NTB:
ntb_transport: Clear QP pointers when freeing an MWThis commit clears
rx_buffandremote_rx_infoinntb_free_mw()for all QPs using a memory window before freeing its backing buffer.Quoted 12 lines
drivers/ntb/ntb_transport.c +1
-0@@ -782,10 +782,17 @@ static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) {struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];struct device *dma_dev =ntb_get_dma_dev (nt->ndev); +unsigned int i;if (!mw->virt_addr)return ;drivers/ntb/ntb_transport.c static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) · line 798
Quoted 10 lines
drivers/ntb/ntb_transport.c +6
-0+/* Drop references from every QP using this MW. */ +for (i = num_mw; i < nt->qp_count; i += nt->mw_count) { + nt->qp_vec[i].rx_buff =NULL ; +WRITE_ONCE (nt->qp_vec[i].remote_rx_info,NULL ); + } +ntb_mw_clear_trans (nt->ndev,PIDX , num_mw);dma_free_attrs (dma_dev, mw->alloc_size, mw->alloc_addr, mw->original_dma_addr,DMA_ATTR_FORCE_CONTIGUOUS );[Severity: Medium]
Can concurrent lockless accesses toremote_rx_infostill race withdma_free_attrs()here and trigger a use-after-free?
ntb_free_mw()clearsremote_rx_infowithWRITE_ONCE()and immediately freesmw->alloc_addrwithout waiting for readers or writers that already loaded a non-NULLremote_rx_infopointer viaREAD_ONCE().For example,
ntb_transport_link_down()runs withoutnt->link_event_lockwhen a client closes or changes MTU:CPU0 CPU1
ntb_transport_link_down(qp)
if (qp->link_is_up)ntb_send_link_down(qp) msleep(100) ntb_transport_link_cleanup(nt) ntb_qp_link_cleanup(qp) ntb_qp_link_down_reset(qp) rri = READ_ONCE(qp->remote_rx_info) ntb_free_mw(nt, num_mw) WRITE_ONCE(qp->remote_rx_info, NULL) dma_free_attrs(..., mw->alloc_addr, ...) rri->entry = ... <-- use-after-free writeThe same race applies to lockless readers in
ntb_transport_tx_free_entry()(called fromntb_netdev_tx_timer()or TX completion handlers, whichntb_qp_link_cleanup()does not stop) andntb_qp_debugfs_stats_show().Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v4 14/15 NTB: ntb_transport: Abort link setup on QP MW allocation failure
In reply to Koichiro Denntb_transport_setup_qp_mw() can fail while growing a QP's RX entry pool, but the link worker ignores that error. The worker can consequently publish a QP whose memory-window state is only partly initialized.
Abort on the first QP setup error and release the MWs through the existing error path instead of publishing the transport link.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/ntb/ntb_transport.c +3 -1
@@ -1149,7 +1149,9 @@ static void ntb_transport_link_work(struct work_struct *work) }for (i =0 ; i < nt->qp_count; i++) { -ntb_transport_setup_qp_mw (nt, i); + rc =ntb_transport_setup_qp_mw (nt, i); +if (rc) + goto out1;ntb_transport_setup_qp_peer_msi (nt, i); }
Signature · 1 line
2.54.0
PATCH v4 15/15 NTB: ntb_transport: Remove clients before freeing transport resources
In reply to Koichiro DenUnbinding ntb_transport can call ntb_transport_free() while ntb_netdev is still bound. The transport frees MWs and QP resources before unregistering the clients, so the netdev's transmit path and timer can access freed memory. Its remove callback also calls ntb_transport_free_queue() on a QP whose resources have already been released. This teardown order is unsafe and somewhat unintuitive.
The crash can be reproduced with an intensive TX load, during which you unbind the NTB device. The following is a KASAN report from my VHOST/HOST setup using vNTB.
VHOST# sudo iperf3 -ub0 -c $HOST -l 100 -P 100 &
VHOST# echo $VHOST_NTB_DEV > /sys/bus/ntb/drivers/ntb_transport/unbind
==================================================================
BUG: KASAN: vmalloc-out-of-bounds in ntb_transport_tx_free_entry+0xf0 ... Call trace: ... __asan_report_load4_noabort+0x1c/0x30 ntb_transport_tx_free_entry+0xf0/0x130 [ntb_transport] ntb_netdev_tx_timer+0x78/0x260 [ntb_netdev] ...
Disable and drain transport link work first, then unregister the clients so they stop using and release their QPs. After that, free any QPs left over before running transport link cleanup. Disabling the work keeps link events from restarting setup or cleanup during client removal.
---
Changes in v4:
- None.
drivers/ntb/ntb_transport.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/ntb/ntb_transport.c +6 -5
@@ -1485,9 +1485,11 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)debugfs_remove_recursive (nt->debugfs_node_dir); -ntb_transport_link_cleanup (nt); -cancel_work_sync (&nt->link_cleanup); -cancel_delayed_work_sync (&nt->link_work); +/* Stop transport work before clients release their QPs. */ +disable_delayed_work_sync (&nt->link_work); +disable_work_sync (&nt->link_cleanup); + +ntb_bus_remove (nt); qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
@@ -1498,11 +1500,10 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)ntb_transport_free_queue (qp); } +ntb_transport_link_cleanup (nt);ntb_link_disable (ndev);ntb_clear_ctx (ndev); -ntb_bus_remove (nt); -for (i = nt->mw_count; i--; ) {ntb_free_mw (nt, i);iounmap (nt->mw_vec[i].vbase);
Signature · 1 line
2.54.0
Quoted 1 line · Koichiro Den
This series attempts to fix miscellaneous issues in
ntb_transport.
Tested on x86_64/KVM: two guests joined by an emulated NTB device (qemu-ntb [1]), ntb_netdev over ntb_transport, use_dma=0 and 1, ntb-next 0aa2e6bc39f1 with KASAN and lockdep.
Without the series I hit (matched to the patches by their changelogs; the leak and the transport-context use-after-free under patch 15 also checked with patches 1-14 only):
- patch 1: debugfs "already exists" on rebind
- patch 4: the link_work/link_cleanup deadlock (lockdep + hung task)
- patch 10: KASAN in ntb_transport_rxc_db() during unbind
- patch 13, step 3-(B): KASAN in ntb_qp_link_down_reset()
- patch 15: KASAN in ntb_transport_tx_free_entry() (via the DMA completion callback), an RX skb leak (below), and a use-after-free of the transport context in ntb_transport_link_cleanup_work() during rmmod
The leak: unbinding ntb_transport while ntb_netdev is bound logs "Freeing item from non-empty rx_pend_q" 100 times per unbind. ntb_transport_free() frees the queues before ntb_bus_remove() removes ntb_netdev, and ntb_transport_free_queue() frees the entries but not the skbs ntb_netdev posted, so ntb_netdev cannot take them back with ntb_transport_rx_remove(). SUnreclaim on that side went from 39 MB before the first unbind to 589 MB after 41, about 13 MB per unbind. With patches 1-14 it still grows (39 MB to 309 MB after 19 unbinds); with patch 15 it does not. This is not in the patch 15 changelog; maybe worth a line.
With the series:
- patch 1 test and step 3-(B): pass, 11 of 11 runs for 3-(B)
- unbind under load: 100 rounds with use_dma=0 (SUnreclaim stays between 61 and 72 MB), 667 with use_dma=1 (in 10 runs; 3 of them stopped early on the link-up race below, 2 on a stall of the test script that has since been fixed)
- load/unload churn: 150 rounds with use_dma=0, 100 with use_dma=1
- no KASAN, lockdep or hung task report in any of these runs
While testing I also hit an older race, not caused by this series: the link sometimes does not come up after both sides load ntb_transport, when the link cleanup queued at probe time runs after the peer's link-up. I am handling it separately, details and an RFC in [3].
Tests and the failure reports: [2]
[1] https://github.com/oguzhankynk/qemu-ntb
[2] https://github.com/oguzhankynk/qemu-ntb/blob/ntb-transport-teardown/docs/transport-teardown.md
[3] https://lore.kernel.org/r/[email protected]/