Linux PCI Non-Transparent Bridge framework and drivers
 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 v3 15/15] NTB: ntb_transport: Remove clients before freeing transport resources
Date: Mon, 28 Sep 2026 16:04:43 +0000	[thread overview]
Message-ID: <20260928160444.4F31C1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260928152550.3354675-16-den@valinux.co.jp>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ntb: use-after-free and DMA memory corruption in teardown due to bypassed wait loop
--

commit 51bc5e4115a3fe5b0d9105d1dfb2a458cfc0b016
Author: Koichiro Den <den@valinux.co.jp>
NTB: ntb_transport: Remove clients before freeing transport resources

This commit fixes a tear-down order issue where transport resources are
freed before unregistering clients. It modifies ntb_transport_free to
disable transport work and call ntb_bus_remove earlier. It also reorders
the transport link cleanup after queue freeing to prevent setup during
removal.

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index af7f240479f4e..eef3a214d91a0 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c
[ ... ]
> @@ -1497,11 +1499,10 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
>  			ntb_transport_free_queue(qp);
>  	}
>  
> +	ntb_transport_link_cleanup(nt);

[Severity: High]
Does this reordering inadvertently skip the DMA synchronization wait loop?

When ntb_transport_free_queue is called in the loop above, it frees the
queues and marks them as free by updating the bitmap:

void ntb_transport_free_queue(struct ntb_transport_qp *qp)
{
        ...
        qp->transport->qp_bitmap_free |= qp_bit;
}

Then, when ntb_transport_link_cleanup is called immediately after, it
evaluates the allocated queues using the updated bitmap:

static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
{
        ...
        qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
        
        for (i = 0; i < nt->qp_count; i++)
                if (qp_bitmap_alloc & BIT_ULL(i)) {
                        qp = &nt->qp_vec[i];
                        ntb_qp_link_cleanup(qp);
                        ...
}

Because the queues were just marked as free by ntb_transport_free_queue,
qp_bitmap_alloc evaluates to zero. This completely bypasses the loop that
calls ntb_qp_link_cleanup.

Bypassing ntb_qp_link_cleanup skips the critical DMA wait loop:

while (!ntb_transport_rx_idle(qp))
        fsleep(1000);

Without this synchronization, entries in rx_post_q are freed in
ntb_transport_free_queue while the hardware might still be actively
accessing them via DMA. Could this lead to a use-after-free or memory
corruption when ntb_rx_copy_callback later asynchronously executes and
operates on the freed queue entries?

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

  reply	other threads:[~2026-09-28 16:04 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28 15:25 [PATCH v3 00/15] NTB: ntb_transport: Miscellaneous fixes Koichiro Den
2026-09-28 15:25 ` [PATCH v3 01/15] NTB: ntb_transport: Remove the device debugfs directory Koichiro Den
2026-09-28 15:25 ` [PATCH v3 02/15] NTB: ntb_transport: Start TX offload thread after queue setup Koichiro Den
2026-09-28 15:25 ` [PATCH v3 03/15] NTB: ntb_transport: Make link setup flags atomic Koichiro Den
2026-09-28 15:53   ` Logan Gunthorpe
2026-10-09 23:08   ` Dave Jiang
2026-09-28 15:25 ` [PATCH v3 04/15] NTB: ntb_transport: Avoid deadlock when cancelling link work Koichiro Den
2026-09-28 15:43   ` sashiko-bot
2026-09-29  1:19     ` Koichiro Den
2026-10-09 23:09   ` Dave Jiang
2026-09-28 15:25 ` [PATCH v3 05/15] NTB: ntb_transport: Publish link state after QP setup Koichiro Den
2026-09-28 15:25 ` [PATCH v3 06/15] NTB: ntb_transport: Avoid losing QP link-up requests Koichiro Den
2026-09-28 16:57   ` Logan Gunthorpe
2026-09-29  1:43     ` Koichiro Den
2026-10-09 23:11   ` Dave Jiang
2026-09-28 15:25 ` [PATCH v3 07/15] NTB: ntb_transport: Clear link state before QP cleanup Koichiro Den
2026-09-28 15:25 ` [PATCH v3 08/15] NTB: ntb_transport: Stop QP work before freeing a queue Koichiro Den
2026-10-09 23:11   ` Dave Jiang
2026-09-28 15:25 ` [PATCH v3 09/15] NTB: ntb_transport: Stop RX tasklet scheduling " Koichiro Den
2026-09-28 15:25 ` [PATCH v3 10/15] NTB: ntb_transport: Drain RX tasklets during link cleanup Koichiro Den
2026-09-28 15:25 ` [PATCH v3 11/15] NTB: ntb_transport: Wait for RX completions before resetting a QP Koichiro Den
2026-09-28 15:54   ` sashiko-bot
2026-09-29  1:27     ` Koichiro Den
2026-09-28 15:25 ` [PATCH v3 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown Koichiro Den
2026-09-28 15:25 ` [PATCH v3 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW Koichiro Den
2026-09-28 15:25 ` [PATCH v3 14/15] NTB: ntb_transport: Abort link setup on QP MW allocation failure Koichiro Den
2026-09-28 15:25 ` [PATCH v3 15/15] NTB: ntb_transport: Remove clients before freeing transport resources Koichiro Den
2026-09-28 16:04   ` sashiko-bot [this message]
2026-09-29  1:31     ` Koichiro Den

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=20260928160444.4F31C1F000FF@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox