Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Logan Gunthorpe" <logang@deltatee.com>
Cc: dmaengine@vger.kernel.org, Frank.Li@kernel.org,
	linux-pci@vger.kernel.org, vkoul@kernel.org
Subject: Re: [PATCH v1 3/6] dmaengine: switchtec-dma: fix channel leak on registration failure
Date: Fri, 17 Jul 2026 22:34:30 +0000	[thread overview]
Message-ID: <20260717223431.625EE1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260717221001.361421-4-logang@deltatee.com>

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

New issues:
- [High] The patch introduces a race condition leading to a use-after-free because it frees channel structures without removing them from `dma_dev->channels` while a global interrupt is still active.

Pre-existing issues:
- [High] In `switchtec_dma_remove()`, `swdma_dev` is used to unmap resources after it has already been freed.
- [High] Broken cleanup of `chan_status_irq` resulting in a memory leak, wrong IRQ being freed, and a dangling interrupt handler leading to a use-after-free.
--

--- Patch [3]: [PATCH v1 3/6] dmaengine: switchtec-dma: fix channel leak on registration failure ---
commit 26b72b89d5c52be6bb3a650d3492040c2465768c
Author: Logan Gunthorpe <logang@deltatee.com>

dmaengine: switchtec-dma: fix channel leak on registration failure

If dma_async_device_register() fails during probe,
switchtec_dma_chans_release() only stops hardware and frees IRQs and
tasklets, but never frees the per-channel swdma_chan structures. This patch
frees the channels and the array directly in the registration-failure path.

Link: https://lore.kernel.org/dmaengine/20260707165555.350951F000E9@smtp.kernel.org/T/#u

> diff --git a/drivers/dma/switchtec_dma.c b/drivers/dma/switchtec_dma.c
> index f77da31aeb65e..5768948ef4662 100644
> --- a/drivers/dma/switchtec_dma.c
> +++ b/drivers/dma/switchtec_dma.c

[ ... ]

> @@ -1317,6 +1317,11 @@ static int switchtec_dma_create(struct pci_dev *pdev)
>  err_chans_release_exit:
>  	switchtec_dma_chans_release(pdev, swdma_dev);
>  
> +	for (i = 0; i < swdma_dev->chan_cnt; i++)
> +		kfree(swdma_dev->swdma_chans[i]);

[Severity: High]
Does this free the channel structures while they are still linked in
dma_dev->channels?

During initialization, the embedded dma_chan nodes are added to
dma->channels. When freeing them here, the nodes are not removed from the
list, and the global chan_status_irq is still registered and active until
the subsequent err_exit block.

If the hardware asserts the interrupt, or if the interrupt is already being
processed concurrently, could switchtec_dma_chan_status_isr() iterate over
dma_dev->channels through this freed memory?

> +
> +	kfree(swdma_dev->swdma_chans);
> +
>  err_exit:
>  	if (swdma_dev->chan_status_irq)
>  		free_irq(swdma_dev->chan_status_irq, swdma_dev);

[Severity: High]
This is a pre-existing issue, but does this cleanup block use a vector
index rather than the global Linux IRQ number?

During probe, swdma_dev->chan_status_irq stores the MSI-X vector index
(e.g., 0 or 1). If it is 0, the condition evaluates to false, skipping
cleanup. If it is > 0, it calls free_irq() with a small integer, which
attempts to free an unrelated system IRQ.

This leaves the actual MSI-X interrupt handler registered. If the Linux IRQ
number is later reused by another device, could the kernel invoke
switchtec_dma_chan_status_isr() with a dev_id pointing to the freed
swdma_dev structure?


[Severity: High]
This is also a pre-existing issue, but is swdma_dev used to unmap resources
after it has already been freed in switchtec_dma_remove()?

switchtec_dma_remove() calls dma_async_device_unregister(&swdma_dev->dma_dev).
This synchronously drops a reference to the DMA device, which can trigger the
driver's switchtec_dma_release() callback and execute kfree(swdma_dev).

Once dma_async_device_unregister() returns, the swdma_dev pointer can be
invalid, but switchtec_dma_remove() immediately proceeds to call
iounmap(swdma_dev->bar). Could this result in a use-after-free on device
teardown?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260717221001.361421-1-logang@deltatee.com?part=3

  reply	other threads:[~2026-07-17 22:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17 22:09 [PATCH v1 0/6] dmaengine: miscellaneous fixes for ioat, switchtec_dma and plx_dma Logan Gunthorpe
2026-07-17 22:09 ` [PATCH v1 1/6] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc() Logan Gunthorpe
2026-07-17 22:30   ` sashiko-bot
2026-07-17 22:09 ` [PATCH v1 2/6] dmaengine: switchtec-dma: fix resource leak in alloc_chan_resources Logan Gunthorpe
2026-07-17 22:36   ` sashiko-bot
2026-07-17 22:09 ` [PATCH v1 3/6] dmaengine: switchtec-dma: fix channel leak on registration failure Logan Gunthorpe
2026-07-17 22:34   ` sashiko-bot [this message]
2026-07-17 22:09 ` [PATCH v1 4/6] dmaengine: ioat: disable relaxed ordering before registering the device Logan Gunthorpe
2026-07-17 22:34   ` sashiko-bot
2026-07-17 22:10 ` [PATCH v1 5/6] dmaengine: ioat: use sysfs_emit() in per-channel sysfs show() Logan Gunthorpe
2026-07-17 22:35   ` sashiko-bot
2026-07-17 22:10 ` [PATCH v1 6/6] dmaengine: plx_dma: fix NULL pointer deref in plx_dma_isr() Logan Gunthorpe
2026-07-17 22:32   ` 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=20260717223431.625EE1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=logang@deltatee.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vkoul@kernel.org \
    /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