Linux PCI Non-Transparent Bridge framework and drivers
 help / color / mirror / Atom feed
From: Koichiro Den <den@valinux.co.jp>
To: sashiko-reviews@lists.linux.dev
Cc: ntb@lists.linux.dev
Subject: Re: [PATCH v4 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW
Date: Wed, 2 Dec 2026 22:38:56 +0900	[thread overview]
Message-ID: <aw_abm23-HVvt6AP@z790sl> (raw)
In-Reply-To: <sashiko-outbox-236450@kernel.org>

On Fri, Nov 27, 2026 at 02:24:57AM +0000, sashiko-bot@kernel.org wrote:
> 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
> --

This is the same pre-existing lifetime race already flagged by Sashiko during
the v2 review:
https://lore.kernel.org/r/20260910043233.57D091F000FF@smtp.kernel.org/
I'd prefer to handle the broader fix separately, as discussed (with Dave) in v2.

Best regards,
Koichiro Den

> 
> commit 6c932f2e3a5a220eb89ffa33367cf59f97e380ad
> Author: Koichiro Den <den@valinux.co.jp>
> 
> 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.
> 
> > diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> > index fa53fcb87bad5..be1ae3789da1d 100644
> > --- a/drivers/ntb/ntb_transport.c
> > +++ b/drivers/ntb/ntb_transport.c
> > @@ -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);
> 
> [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                                    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 write
> 
> The 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().
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261127020533.2481725-1-den@valinux.co.jp?part=13

  reply	other threads:[~2026-12-02 13:39 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  2:05 [PATCH v4 00/15] NTB: ntb_transport: Miscellaneous fixes Koichiro Den
2026-11-27  2:05 ` [PATCH v4 01/15] NTB: ntb_transport: Remove the device debugfs directory Koichiro Den
2026-11-27  2:05 ` [PATCH v4 02/15] NTB: ntb_transport: Start TX offload thread after queue setup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 03/15] NTB: ntb_transport: Make link setup flags atomic Koichiro Den
2026-11-27  2:05 ` [PATCH v4 04/15] NTB: ntb_transport: Avoid deadlock when cancelling link work Koichiro Den
2026-11-27  2:05 ` [PATCH v4 05/15] NTB: ntb_transport: Publish link state after QP setup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 06/15] NTB: ntb_transport: Avoid losing QP link-up requests Koichiro Den
2026-11-27  2:05 ` [PATCH v4 07/15] NTB: ntb_transport: Clear link state before QP cleanup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 08/15] NTB: ntb_transport: Stop QP work before freeing a queue Koichiro Den
2026-11-27  2:05 ` [PATCH v4 09/15] NTB: ntb_transport: Stop RX tasklet scheduling " Koichiro Den
2026-11-27  2:05 ` [PATCH v4 10/15] NTB: ntb_transport: Drain RX tasklets during link cleanup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 11/15] NTB: ntb_transport: Wait for RX completions before resetting a QP Koichiro Den
2026-11-27  2:05 ` [PATCH v4 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown Koichiro Den
2026-11-27  2:21   ` sashiko-bot
2026-12-02 13:35     ` Koichiro Den
2026-11-27  2:05 ` [PATCH v4 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW Koichiro Den
2026-11-27  2:24   ` sashiko-bot
2026-12-02 13:38     ` Koichiro Den [this message]
2026-11-27  2:05 ` [PATCH v4 14/15] NTB: ntb_transport: Abort link setup on QP MW allocation failure Koichiro Den
2026-11-27  2:05 ` [PATCH v4 15/15] NTB: ntb_transport: Remove clients before freeing transport resources 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=aw_abm23-HVvt6AP@z790sl \
    --to=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