All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Logan Gunthorpe" <logang@deltatee.com>
Cc: linux-pci@vger.kernel.org, dmaengine@vger.kernel.org,
	Frank.Li@kernel.org, vkoul@kernel.org
Subject: Re: [PATCH v2 01/11] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc()
Date: Tue, 21 Jul 2026 16:27:10 +0000	[thread overview]
Message-ID: <20260721162711.3D3431F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260721155739.62120-2-logang@deltatee.com>

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

Pre-existing issues:
- [High] Memory leak of DMA channel resources on error path in `switchtec_dma_alloc_chan_resources()`.
- [High] Memory leak of `switchtec_dma_chan` structures and channel arrays on module probe error paths.
- [High] Deadlock due to client callback invocation while holding `complete_lock`.
- [High] `switchtec_dma_prep_desc()` returns with `submit_lock` held, preventing clients from preparing multiple descriptors.
- [High] Unsafe atomic delays of up to 100ms with a spinlock held and a 60ms mdelay in atomic context.
- [Medium] Large contiguous 1MB DMA memory allocations use GFP_NOWAIT in a sleepable context, risking allocation failure.
--

--- Patch [1]: [PATCH v2 01/11] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc() ---
commit 4a5b609a9359892c57ed414c821b1570151f6b0b
Author: Logan Gunthorpe <logang@deltatee.com>

dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc()

This commit adds NULL assignments to the hw_sq, hw_cq, and desc_ring pointers
after they are freed in switchtec_dma_free_desc(). This fixes a double-free
bug that occurs if switchtec_dma_alloc_chan_resources() fails during unwind
and a subsequent retry also fails, calling the free function a second time.

> diff --git a/drivers/dma/switchtec_dma.c b/drivers/dma/switchtec_dma.c
> index 3ef9286406159..a4a7d66d042de 100644
> --- a/drivers/dma/switchtec_dma.c
> +++ b/drivers/dma/switchtec_dma.c
> @@ -886,14 +886,18 @@ static void switchtec_dma_free_desc(struct switchtec_dma_chan *swdma_chan)
>  	if (swdma_chan->hw_sq)
>  		dma_free_coherent(swdma_dev->dma_dev.dev, size,
>  				  swdma_chan->hw_sq, swdma_chan->dma_addr_sq);
> +	swdma_chan->hw_sq = NULL;
>  
>  	size = SWITCHTEC_DMA_CQ_SIZE * sizeof(*swdma_chan->hw_cq);
>  	if (swdma_chan->hw_cq)
>  		dma_free_coherent(swdma_dev->dma_dev.dev, size,
>  				  swdma_chan->hw_cq, swdma_chan->dma_addr_cq);
> +	swdma_chan->hw_cq = NULL;
>  
> -	for (i = 0; i < SWITCHTEC_DMA_RING_SIZE; i++)
> +	for (i = 0; i < SWITCHTEC_DMA_RING_SIZE; i++) {
>  		kfree(swdma_chan->desc_ring[i]);
> +		swdma_chan->desc_ring[i] = NULL;
> +	}
>  }
>  
>  static int switchtec_dma_alloc_desc(struct switchtec_dma_chan *swdma_chan)

[Severity: High]
This isn't a bug introduced by this patch, but does the error path in
switchtec_dma_alloc_chan_resources() leak the descriptors?

If enable_channel() fails after switchtec_dma_alloc_desc() has already allocated
resources, the code returns immediately:

drivers/dma/switchtec_dma.c:switchtec_dma_alloc_chan_resources() {
    ...
	rc = enable_channel(swdma_chan);
	if (rc)
		return rc;
    ...
}

Will this permanently leak the DMA descriptors and coherent memory since
switchtec_dma_free_desc() is not called?

[Severity: High]
This is a pre-existing issue, but does switchtec_dma_chan_free() skip freeing
the channel structure itself?

If probe fails and calls switchtec_dma_chans_release() -> switchtec_dma_chan_free(),
it stops the channel but never calls kfree(swdma_chan):

drivers/dma/switchtec_dma.c:switchtec_dma_chan_free() {
    ...
	switchtec_dma_chan_stop(swdma_chan);

	return 0;
}

Also, on module probe error paths, switchtec_dma_create() calls
switchtec_dma_chans_release() but doesn't free the swdma_dev->swdma_chans array:

drivers/dma/switchtec_dma.c:switchtec_dma_create() {
    ...
err_chans_release_exit:
	switchtec_dma_chans_release(pdev, swdma_dev);

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

	iounmap(swdma_dev->bar);
	kfree(swdma_dev);
	return rc;
}

Could this lead to memory leaks of the switchtec_dma_chan structures and the
swdma_chans array during early probe failures?

[Severity: High]
This isn't a bug introduced by this patch, but can we self-deadlock when invoking
the client callback?

In switchtec_dma_cleanup_completed(), the callback is invoked synchronously while
holding complete_lock:

drivers/dma/switchtec_dma.c:switchtec_dma_cleanup_completed() {
    ...
		spin_lock_bh(&swdma_chan->complete_lock);
    ...
			dmaengine_desc_get_callback_invoke(&desc->txd, &res);
    ...
}

If the client callback checks TX status via dma_async_is_tx_complete(), it routes
to switchtec_dma_tx_status(), which calls switchtec_dma_cleanup_completed() again.

Will this cause a deadlock when trying to reacquire complete_lock?

[Severity: High]
This is a pre-existing issue, but does switchtec_dma_prep_desc() return with a
spinlock held?

drivers/dma/switchtec_dma.c:switchtec_dma_prep_desc() {
    ...
	spin_lock_bh(&swdma_chan->submit_lock);
    ...
	/* return with the lock held, it will be released in tx_submit */

	return &desc->txd;
}

Does this violate the DMA engine API by preventing clients from preparing
multiple descriptors before submitting them?

If a client batches requests and calls prep twice, won't it self-deadlock on
submit_lock?

[Severity: High]
This isn't a bug introduced by this patch, but do we have unsafe delays in
atomic contexts?

In pause_reset_channel(), which is reachable from the device_terminate_all
callback (allowed in atomic context), there is a 60ms busy-wait:

drivers/dma/switchtec_dma.c:pause_reset_channel() {
    ...
	/* wait 60ms to ensure no pending CEs */
	mdelay(60);
    ...
}

Additionally, wait_for_chan_status() spins for up to 100ms while holding
hw_ctrl_lock:

drivers/dma/switchtec_dma.c:wait_for_chan_status() {
    ...
	return readl_poll_timeout_atomic(&chan_hw->status, status,
					 (set && (status & mask)) ||
					 (!set && !(status & mask)),
					 10, 100 * USEC_PER_MSEC);
}

Will these long atomic delays cause severe system latency spikes or soft lockups?

[Severity: Medium]
This is a pre-existing issue, but is it safe to use GFP_NOWAIT for large
allocations in switchtec_dma_alloc_desc()?

drivers/dma/switchtec_dma.c:switchtec_dma_alloc_desc() {
    ...
	size = SWITCHTEC_DMA_SQ_SIZE * sizeof(*swdma_chan->hw_sq);
	swdma_chan->hw_sq = dma_alloc_coherent(swdma_dev->dma_dev.dev, size,
					       &swdma_chan->dma_addr_sq,
					       GFP_NOWAIT);
    ...
}

Since device_alloc_chan_resources() is called under a mutex and is allowed to sleep,
could using GFP_NOWAIT here unnecessarily cause allocation failures under memory
pressure since it prevents direct reclaim and compaction?

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

  reply	other threads:[~2026-07-21 16:27 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 15:57 [PATCH v2 00/11] dmaengine: miscellaneous fixes for ioat, switchtec_dma and plx_dma Logan Gunthorpe
2026-07-21 15:57 ` [PATCH v2 01/11] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc() Logan Gunthorpe
2026-07-21 16:27   ` sashiko-bot [this message]
2026-07-21 15:57 ` [PATCH v2 02/11] dmaengine: switchtec-dma: fix resource leak in alloc_chan_resources Logan Gunthorpe
2026-07-21 16:27   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 03/11] dmaengine: switchtec-dma: halt channel on alloc_chan_resources error Logan Gunthorpe
2026-07-21 16:25   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 04/11] dmaengine: switchtec-dma: fix channel leak on registration failure Logan Gunthorpe
2026-07-21 16:30   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 05/11] dmaengine: switchtec-dma: make switchtec_dma_chans_release() void Logan Gunthorpe
2026-07-21 16:24   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 06/11] dmaengine: switchtec-dma: unlink channels before freeing on registration failure Logan Gunthorpe
2026-07-21 16:28   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 07/11] dmaengine: switchtec-dma: fix use-after-free of swdma_dev in remove() Logan Gunthorpe
2026-07-21 16:36   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 08/11] dmaengine: switchtec-dma: fix chan_status_irq cleanup on create() error Logan Gunthorpe
2026-07-21 16:26   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 09/11] dmaengine: ioat: disable relaxed ordering before registering the device Logan Gunthorpe
2026-07-21 16:31   ` Dave Jiang
2026-07-21 16:35   ` sashiko-bot
2026-07-21 15:57 ` [PATCH v2 10/11] dmaengine: ioat: use sysfs_emit() in per-channel sysfs show() Logan Gunthorpe
2026-07-21 16:28   ` sashiko-bot
2026-07-21 16:31   ` Dave Jiang
2026-07-21 15:57 ` [PATCH v2 11/11] dmaengine: plx_dma: fix NULL pointer deref in plx_dma_isr() Logan Gunthorpe
2026-07-21 16:35   ` 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=20260721162711.3D3431F000E9@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 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.