All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Koichiro Den" <den@valinux.co.jp>
Cc: ntb@lists.linux.dev
Subject: Re: [PATCH v2 10/14] NTB: ntb_transport: Wait for RX completions before resetting a QP
Date: Thu, 10 Sep 2026 04:24:12 +0000	[thread overview]
Message-ID: <20260910042413.5026B1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260910040836.3792333-11-den@valinux.co.jp>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Data corruption during fallback RX memory copy.
- [High] Use-After-Free of `ntb_queue_entry` elements during transport queue teardown.
--

commit a2b0eba80182f98f6886cb72c908b76469b3615d
Author: Koichiro Den <den@valinux.co.jp>

NTB: ntb_transport: Wait for RX completions before resetting a QP

This commit fixes a race condition where transport link cleanup can free a
memory window still in use by RX DMA. It adds a wait loop for the RX post
queue to become idle before resetting the QP.

Link: https://lore.kernel.org/r/20260907144257.767281F00A3A@smtp.kernel.org/

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index 36797ea3ff45c..7ccba2c04f548 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c

[ ... ]

> @@ -959,6 +966,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);

[Severity: High]
This is a pre-existing issue, but does ntb_transport_free_queue() also need
this same wait loop to prevent a use-after-free?

When tearing down a queue, ntb_transport_free_queue() calls
dmaengine_terminate_all() and dma_release_channel(). However, as noted in the
patch comment here, some DMA engines lack terminate/synchronize ops. The
cleanup routine then immediately frees the rx_post_q entries:

drivers/ntb/ntb_transport.c:ntb_transport_free_queue() {
    ...
	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
		dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
		kfree(entry);
	}
    ...
}

If a DMA callback is pending or executing asynchronously while the queue is
being torn down, won't it dereference the freed entry pointer after
kfree(entry) is called?

[Severity: High]
This is another pre-existing issue, but can the fallback copy corrupt data
during a DMA error?

If a DMA transfer fails and ntb_rx_copy_callback() falls back to a software
copy, it calculates the offset using the globally incremented qp->rx_index:

drivers/ntb/ntb_transport.c:ntb_rx_copy_callback() {
    ...
		case DMA_TRANS_ABORTED:
		{
			struct ntb_transport_qp *qp = entry->qp;
			void *offset = qp->rx_buff + qp->rx_max_frame *
					qp->rx_index;

			ntb_memcpy_rx(entry, offset);
			qp->rx_memcpy++;
			return;
		}
    ...
}

Because qp->rx_index was already advanced to the next available ring buffer
frame when the transaction was originally submitted in ntb_process_rxc(),
won't this offset calculation point to the wrong frame and copy the wrong
payload? Should this calculation use the entry-specific index
(entry->rx_index) instead?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260910040836.3792333-1-den@valinux.co.jp?part=10

  reply	other threads:[~2026-09-10  4:24 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10  4:08 [PATCH v2 00/14] NTB: ntb_transport: Miscellaneous fixes Koichiro Den
2026-09-10  4:08 ` [PATCH v2 01/14] NTB: ntb_transport: Remove the device debugfs directory Koichiro Den
2026-09-10  4:20   ` sashiko-bot
2026-09-10 18:41   ` Frank Li
2026-09-15 17:52   ` Logan Gunthorpe
2026-09-24 15:37   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 02/14] NTB: ntb_transport: Start TX offload thread after queue setup Koichiro Den
2026-09-11 16:13   ` Frank Li
2026-09-15 18:08   ` Logan Gunthorpe
2026-09-24 15:38   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 03/14] NTB: ntb_transport: Avoid deadlock when cancelling link work Koichiro Den
2026-09-10  4:23   ` sashiko-bot
2026-09-11 16:21   ` Frank Li
2026-09-11 17:41     ` Koichiro Den
2026-09-15 18:19   ` Logan Gunthorpe
2026-09-10  4:08 ` [PATCH v2 04/14] NTB: ntb_transport: Publish link state after QP setup Koichiro Den
2026-09-11 16:39   ` Frank Li
2026-09-15 18:32   ` Logan Gunthorpe
2026-09-24 15:40   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 05/14] NTB: ntb_transport: Avoid losing QP link-up requests Koichiro Den
2026-09-10  4:26   ` sashiko-bot
2026-09-11 16:53   ` Frank Li
2026-09-11 18:04     ` Koichiro Den
2026-09-11 18:21       ` Koichiro Den
2026-09-12  3:20         ` Frank Li
2026-09-12 14:52           ` Koichiro Den
2026-09-18 14:43             ` Dave Jiang
2026-09-19 13:08               ` Koichiro Den
2026-09-10  4:08 ` [PATCH v2 06/14] NTB: ntb_transport: Clear link state before QP cleanup Koichiro Den
2026-09-10  4:27   ` sashiko-bot
2026-09-15 18:55   ` Logan Gunthorpe
2026-09-24 15:45   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 07/14] NTB: ntb_transport: Stop QP work before freeing a queue Koichiro Den
2026-09-10  4:23   ` sashiko-bot
2026-09-15 19:59   ` Logan Gunthorpe
2026-09-10  4:08 ` [PATCH v2 08/14] NTB: ntb_transport: Stop RX tasklet scheduling " Koichiro Den
2026-09-18 15:28   ` Logan Gunthorpe
2026-09-24 15:49   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 09/14] NTB: ntb_transport: Drain RX tasklets during link cleanup Koichiro Den
2026-09-10  4:23   ` sashiko-bot
2026-09-18 15:41   ` Logan Gunthorpe
2026-09-24 15:51   ` Dave Jiang
2026-09-24 15:53     ` Dave Jiang
2026-09-28  5:34       ` Koichiro Den
2026-09-10  4:08 ` [PATCH v2 10/14] NTB: ntb_transport: Wait for RX completions before resetting a QP Koichiro Den
2026-09-10  4:24   ` sashiko-bot [this message]
2026-09-18 17:21   ` Logan Gunthorpe
2026-09-24 15:54   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 11/14] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown Koichiro Den
2026-09-10  4:31   ` sashiko-bot
2026-09-18 18:04   ` Logan Gunthorpe
2026-09-24 15:56   ` Dave Jiang
2026-09-24 15:57     ` Dave Jiang
2026-09-28  8:51       ` Koichiro Den
2026-09-28 15:22         ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 12/14] NTB: ntb_transport: Clear QP pointers when freeing an MW Koichiro Den
2026-09-10  4:32   ` sashiko-bot
2026-09-18 18:16   ` Logan Gunthorpe
2026-09-24 15:59   ` Dave Jiang
2026-09-28  5:35     ` Koichiro Den
2026-09-10  4:08 ` [PATCH v2 13/14] NTB: ntb_transport: Abort link setup on QP MW allocation failure Koichiro Den
2026-09-10  4:40   ` sashiko-bot
2026-09-18 18:17   ` Logan Gunthorpe
2026-09-24 16:00   ` Dave Jiang
2026-09-10  4:08 ` [PATCH v2 14/14] NTB: ntb_transport: Remove clients before freeing transport resources Koichiro Den
2026-09-10  4:36   ` sashiko-bot
2026-09-10  8:48     ` Koichiro Den
2026-09-11 15:49       ` Dave Jiang
2026-09-14  4:27         ` Koichiro Den
2026-09-14 21:02           ` Dave Jiang
2026-09-15  4:01             ` Koichiro Den
2026-09-15 15:50               ` Dave Jiang
2026-09-18 18:19   ` Logan Gunthorpe
2026-09-24 16:02   ` Dave Jiang

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20260910042413.5026B1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=den@valinux.co.jp \
    --cc=ntb@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

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

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