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 06/11] NTB: ntb_transport: Publish link state after QP setup
Date: Mon, 07 Sep 2026 14:47:00 +0000	[thread overview]
Message-ID: <20260907144701.702E41F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260907142429.951930-7-den@valinux.co.jp>

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

New issues:
- [High] Store-Load memory ordering race between `nt->link_is_up` and `qp->client_ready` allows both threads to miss link scheduling, potentially deadlocking link establishment.

Pre-existing issues:
- [High] Workqueue teardown race leading to Use-After-Free during `ntb_transport_free()`.
- [High] Missing minimum size validation for memory windows causes an integer underflow when calculating tx_size and rx_size, leading to massive out-of-bounds MMIO writes and kernel heap memory corruption.
--

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

NTB: ntb_transport: Publish link state after QP setup

This commit delays publishing the transport link state until all QPs' MW and
peer MSI states are set up. It uses a release store for link state publication
and acquire loads before queuing QP link work to avoid premature execution.

Link: https://lore.kernel.org/r/anyKbq3mpLG4y7rb@SMW015318

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index c8e7fc774b4fd..411017873a83b 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c

[ ... ]

> @@ -1102,16 +1109,19 @@ static void ntb_transport_link_work(struct work_struct *work)
>  			goto out1;
>  	}
>  
>  	for (i = 0; i < nt->qp_count; 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. */
> +	smp_store_release(&nt->link_is_up, true);
> +
> +	for (i = 0; i < nt->qp_count; i++) {
> +		struct ntb_transport_qp *qp = &nt->qp_vec[i];
>  
>  		if (qp->client_ready)
> -			schedule_delayed_work(&qp->link_work, 0);
> +			ntb_transport_schedule_qp_link(qp, 0);

[Severity: High]
Does this code have a Store-Load memory ordering race that could lead to a
deadlock?

In this thread, ntb_transport_link_work() performs a release store to
nt->link_is_up, followed by a plain load of qp->client_ready.

Meanwhile, ntb_transport_link_up() performs a plain store to qp->client_ready,
followed by an acquire load of nt->link_is_up via
ntb_transport_schedule_qp_link():

drivers/ntb/ntb_transport.c:ntb_transport_link_up() {
    ...
    qp->client_ready = true;
    ntb_transport_schedule_qp_link(qp, 0);
    ...
}

Since neither smp_store_release() nor smp_load_acquire() provides a full
memory barrier (Store-Load ordering), is it possible for both threads to delay
their stores in the CPU store buffer? If this happens, both threads could read
false and miss the wakeup, leaving the link permanently inactive.

[ ... ]

[Severity: High]
This is a pre-existing issue, but is there a use-after-free race during
workqueue teardown?

Looking at ntb_transport_free(), the delayed work is canceled before the
hardware events are neutralized:

drivers/ntb/ntb_transport.c:ntb_transport_free() {
    ...
    ntb_transport_link_cleanup(nt);
    cancel_work_sync(&nt->link_cleanup);
    cancel_delayed_work_sync(&nt->link_work);

    qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;

    /* verify that all the qp's are freed */
    for (i = 0; i < nt->qp_count; i++) {
        qp = &nt->qp_vec[i];
        if (qp_bitmap_alloc & BIT_ULL(i))
            ntb_transport_free_queue(qp);
    }

    ntb_link_disable(ndev);
    ntb_clear_ctx(ndev);
    ...
}

If a hardware link event fires between cancel_delayed_work_sync() and
ntb_clear_ctx(), the interrupt handler could requeue the work item. After
ntb_transport_free() finishes freeing the context, couldn't the requeued work
execute and dereference the freed memory?

[Severity: High]
This is a pre-existing issue, but does this code lack minimum size validation
for memory windows, leading to a potential integer underflow?

In ntb_transport_init_queue(), the tx_size calculation unconditionally subtracts
the size of the struct:

drivers/ntb/ntb_transport.c:ntb_transport_init_queue() {
    ...
    tx_size -= sizeof(struct ntb_rx_info);
    ...
}

Similarly, in ntb_transport_setup_qp_mw():

drivers/ntb/ntb_transport.c:ntb_transport_setup_qp_mw() {
    ...
    rx_size -= sizeof(struct ntb_rx_info);
    ...
}

If tx_size or rx_size is less than 4 bytes, this will underflow. For tx_size,
setting the max_mw_size module parameter to a small value could trigger an
underflow. For rx_size, if a remote peer advertises a window size that
truncates to 0, could it underflow and lead to out-of-bounds MMIO writes
or memory corruption?

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

  reply	other threads:[~2026-09-07 14:47 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07 14:24 [PATCH 00/11] NTB: ntb_transport: Miscellaneous fixes Koichiro Den
2026-09-07 14:24 ` [PATCH 01/11] NTB: ntb_transport: Remove the device debugfs directory Koichiro Den
2026-09-07 14:36   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 02/11] NTB: ntb_transport: Start TX offload thread after queue setup Koichiro Den
2026-09-07 14:37   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 03/11] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown Koichiro Den
2026-09-07 14:38   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 04/11] NTB: ntb_transport: Clear QP pointers when freeing an MW Koichiro Den
2026-09-07 14:42   ` sashiko-bot
2026-09-09  4:57     ` Koichiro Den
2026-09-07 14:24 ` [PATCH 05/11] NTB: ntb_transport: Avoid deadlock when cancelling link work Koichiro Den
2026-09-07 14:45   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 06/11] NTB: ntb_transport: Publish link state after QP setup Koichiro Den
2026-09-07 14:47   ` sashiko-bot [this message]
2026-09-09  4:42     ` Koichiro Den
2026-09-07 14:24 ` [PATCH 07/11] NTB: ntb_transport: Clear link state before QP cleanup Koichiro Den
2026-09-07 14:43   ` sashiko-bot
2026-09-09  4:47     ` Koichiro Den
2026-09-07 14:24 ` [PATCH 08/11] NTB: ntb_transport: Abort link setup on QP MW allocation failure Koichiro Den
2026-09-07 14:45   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 09/11] NTB: ntb_transport: Stop QP work before freeing a queue Koichiro Den
2026-09-07 14:50   ` sashiko-bot
2026-09-07 14:24 ` [PATCH 10/11] NTB: ntb_transport: Stop RX tasklet scheduling " Koichiro Den
2026-09-07 14:24 ` [PATCH 11/11] NTB: ntb_transport: Drain RX tasklets during link cleanup Koichiro Den
2026-09-07 14:50   ` sashiko-bot

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=20260907144701.702E41F00A3A@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