Skip to contentSkip to conversation

dmaengine: atcdmac300: Add Andes ATCDMAC300 DMA driver

PATCH Superseded CL Wang 3 participants 5 messages Sep 11

Subsystems (2)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

CL Wang

author
#0 lore raw

v6 cover dmaengine: atcdmac300: Add Andes ATCDMAC300 DMA driver

This patch series adds support for the Andes ATCDMAC300 DMA controller, a memory-to-memory and peripheral DMA controller that provides scatter-gather, cyclic, and slave transfer capabilities.

The ATCDMAC300 IP is embedded in AndesCore-based platforms or SoCs such as AE350 and Qilai.

Changes in v6:
- Rebase onto dmaengine/next; no functional changes from v5
- Fold the MAINTAINERS entry into the driver patch instead of carrying it as a separate patch (series is now 2 patches)

Changes in v5:
- Update copyright year to 2026
- Remove redundant headers (init.h, iopoll.h, mod_devicetable.h)
- Move atcdmac_init_iocp() before of_dma_controller_register() in probe
- Change builtin_platform_driver() to module_platform_driver()
- Implement .remove callback to support safe module unloading
- Update Kconfig entry from bool to tristate
- Add MODULE_AUTHOR, MODULE_DESCRIPTION, MODULE_LICENSE macros

Changes in v4:
- Use items list format with descriptions for reg property in DT binding as suggested by Conor Dooley
- Re-add Acked-by from Conor Dooley for DT binding patch

Changes in v3:
- Rename DT binding file from andestech,qilai-dma.yaml to andestech,ae350-dma.yaml
- Deprecate IP-core-based compatible usage and align with SoC/platform-based strings
- Dropped Acked-by from Conor Dooley due to the above binding change
- Remove "andestech,atcdmac300" from of_device_id table
- Replace deprecated tasklet with threaded IRQ using devm_request_threaded_irq() and IRQF_ONESHOT
- Update locking from spin_lock_bh() to spin_lock_irqsave()
- Use builtin_platform_driver() instead of module_platform_driver()
- Remove "select DMATEST" from Kconfig
- Add separate MAINTAINERS patch (patch 3/3)

Please kindly review.

CL Wang (2):
dt-bindings: dmaengine: Add support for ATCDMAC300 DMA engine
dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

 .../bindings/dma/andestech,ae350-dma.yaml     |   67 +
 MAINTAINERS                                   |    6 +
 drivers/dma/Kconfig                           |   11 +
 drivers/dma/Makefile                          |    1 +
 drivers/dma/atcdmac300.c                      | 1518 +++++++++++++++++
 drivers/dma/atcdmac300.h                      |  296 ++++
 6 files changed, 1899 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/andestech,ae350-dma.yaml
 create mode 100644 drivers/dma/atcdmac300.c
 create mode 100644 drivers/dma/atcdmac300.h
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v6 1/2 dt-bindings: dmaengine: Add support for ATCDMAC300 DMA engine

Document devicetree bindings for Andes ATCDMAC300 DMA engine

ATCDMAC300 is the IP name, which is embedded in AndesCore-based platforms or SoCs such as AE350 and Qilai.

---
Changes for v6:
- Rebase onto dmaengine/next; no changes from v5

Changes for v5:
- No changes from v4

Changes for v4:
- Use items list format with descriptions for reg property as suggested by Conor Dooley

Changes for v3:
- Rename DT binding file from andestech,qilai-dma.yaml to andestech,ae350-dma.yaml.
- Deprecate IP-core-based compatible usage and align with SoC/platform-based strings.
- Dropped Acked-by tag from Conor Dooley due to the above change.

---
 .../bindings/dma/andestech,ae350-dma.yaml     | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/andestech,ae350-dma.yaml
Documentation/devicetree/bindings/dma/andestech,ae350-dma.yaml new file +67 -0
new file mode 100644
index 000000000000..f040a2bf7d4b
@@ -0,0 +1,67 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/dma/andestech,ae350-dma.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Andes ATCDMAC300 DMA Controller
+
+maintainers:
+  - CL Wang <[email protected]>
+
+allOf:
+  - $ref: dma-controller.yaml#
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+          - enum:
+              - andestech,qilai-dma
+          - const: andestech,ae350-dma
+      - const: andestech,ae350-dma
+
+  reg:
+    minItems: 1
+    items:
+      - description: DMA controller register range
+      - description: cache control in IOCP controller
+
+  reg-names:
+    minItems: 1
+    items:
+      - const: dma
+      - const: iocp
+
+  interrupts:
+    maxItems: 1
+
+  "#dma-cells":
+    const: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - "#dma-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        dma-controller@f0c00000 {
+            compatible = "andestech,ae350-dma";
+            reg = <0x0 0xf0c00000 0x0 0x1000>,
+                  <0x0 0xe8000000 0x0 0x10>;
+            reg-names = "dma", "iocp";
+            interrupts = <10 IRQ_TYPE_LEVEL_HIGH>;
+            #dma-cells = <1>;
+        };
+    };
+...
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v6 2/2 dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

In reply to CL Wang

This patch adds support for the Andes ATCDMAC300 DMA controller.

The ATCDMAC300 is a memory-to-memory and peripheral DMA controller that provides scatter-gather, cyclic, and slave transfer capabilities.

---
Changes for v6:
- Rebase onto dmaengine/next; no functional changes from v5
- Fold the MAINTAINERS entry into this patch instead of carrying it as a separate patch

Changes for v5:
- Update copyright year to 2026
- Remove redundant headers (init.h, iopoll.h, mod_devicetable.h)
- Move atcdmac_init_iocp() before of_dma_controller_register() in probe
- Change builtin_platform_driver() to module_platform_driver()
- Implement .remove callback to support safe module unloading
- Update Kconfig entry from bool to tristate
- Add MODULE_AUTHOR, MODULE_DESCRIPTION, MODULE_LICENSE macros

Changes for v4:
- No changes from v3

Changes for v3:
- Remove "andestech,atcdmac300" from of_device_id
- Replace deprecated tasklet with threaded IRQ using devm_request_threaded_irq() and IRQF_ONESHOT to handle bottom-half processing.
- Update locking mechanism from spin_lock_bh() to spin_lock_irqsave()
- Minor cleanups and correctness fixes
- Initialize descriptor pointers (first = NULL) explicitly
- Add missing headers (err.h, iopoll.h, log2.h, sprintf.h)
- Remove unused code paths related to tasklets
- Use builtin_platform_driver() instead of module_platform_driver()
- Remove "select DMATEST" from Kconfig

---
 MAINTAINERS              |    6 +
 drivers/dma/Kconfig      |   11 +
 drivers/dma/Makefile     |    1 +
 drivers/dma/atcdmac300.c | 1518 ++++++++++++++++++++++++++++++++++++++
 drivers/dma/atcdmac300.h |  296 ++++++++
 5 files changed, 1832 insertions(+)
 create mode 100644 drivers/dma/atcdmac300.c
 create mode 100644 drivers/dma/atcdmac300.h
MAINTAINERS +6 -0
index 3a19da74d00c..b99893f795be 100644
@@ -1974,6 +1974,12 @@ S:	Supported
 F:	drivers/clk/analogbits/*
 F:	include/linux/clk/analogbits*
 
+ANDES ATCDMAC300 DMA DRIVER
+M:	CL Wang <[email protected]>
+S:	Supported
+F:	Documentation/devicetree/bindings/dma/andestech,ae350-dma.yaml
+F:	drivers/dma/atcdmac300*
+
 ANDES ATCSPI200 SPI DRIVER
 M:	CL Wang <[email protected]>
 S:	Supported
drivers/dma/Kconfig +11 -0
index ae6a682c9f76..dabd2c8cc94f 100644
@@ -100,6 +100,17 @@ config ARM_DMA350
 	help
 	  Enable support for the Arm DMA-350 controller.
 
+config ATCDMAC300
+	tristate "Andes ATCDMAC300 DMA support"
+	depends on ARCH_ANDES
+	depends on OF
+	select DMA_ENGINE
+	help
+	  Enable support for the Andes ATCDMAC300 DMA controller.
+	  Select Y or M if your platform includes an ATCDMAC300 device that
+	  requires DMA engine support. This driver supports DMA_SLAVE,
+	  DMA_MEMCPY, and DMA_CYCLIC transfer modes.
+
 config AT_HDMAC
 	tristate "Atmel AHB DMA support"
 	depends on ARCH_AT91 || COMPILE_TEST
drivers/dma/Makefile +1 -0
index 14aa086629d5..c8fffb31efc4 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_AMBA_PL08X) += amba-pl08x.o
 obj-$(CONFIG_AMCC_PPC440SPE_ADMA) += ppc4xx/
 obj-$(CONFIG_APPLE_ADMAC) += apple-admac.o
 obj-$(CONFIG_ARM_DMA350) += arm-dma350.o
+obj-$(CONFIG_ATCDMAC300) += atcdmac300.o
 obj-$(CONFIG_AT_HDMAC) += at_hdmac.o
 obj-$(CONFIG_AT_XDMAC) += at_xdmac.o
 obj-$(CONFIG_AXI_DMAC) += dma-axi-dmac.o
drivers/dma/atcdmac300.c new file +1518 -0

1,518 lines not on this page · raw message

drivers/dma/atcdmac300.h new file +296 -0

296 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v6 2/2] dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider: - [Critical] Double-callback and Use-After-Free race in cyclic transfer completion during channel…

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [Critical] Double-callback and Use-After-Free race in cyclic transfer completion during channel termination.
- [High] Masking pending hardware interrupts with software state (used_chan) causes an unacknowledged IRQ storm.
- [High] The DMA device is exposed to clients before its IRQ handler and cache coherency (IOCP) are fully initialized.
- [High] Polling for up to 100ms with interrupts disabled in atcdmac_terminate_all causes soft lockups.
- [High] The channel-wide cyclic flag is sticky, and unconditional resume restarts cause arbitrary DMA execution or kernel panics.
- [High] Suspend/resume race with the threaded IRQ causes transfer desynchronization and hardware corruption.
- [High] dmaengine API violation in atcdmac_terminate_all: invoking completion callbacks for aborted transfers.
- [High] System suspend times out and fails if a slave DMA channel is actively waiting for peripheral data.
- [High] Sleep-in-atomic bug on PREEMPT_RT due to using spinlock_t in hardirq context.
- [High] Kernel panic due to list_del_init() on an uninitialized list_head in error paths.
- [Medium] Writing a blanket clear mask to REG_INT_STA clears unhandled interrupts, permanently losing them. --

commit e06d7bd262ebaa03646e2e3ac2876918f25083d4
Author: CL Wang <[email protected]>

dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

This patch adds support for the Andes ATCDMAC300 DMA controller. The ATCDMAC300 is a memory-to-memory and peripheral DMA controller that provides scatter-gather, cyclic, and slave transfer capabilities.

Quoted 4 lines
drivers/dma/atcdmac300.c new file +0 -0
new file mode 100644

[ ... ]

drivers/dma/atcdmac300.c line 84

Quoted 11 lines
drivers/dma/atcdmac300.c new file +11 -0
+static struct atcdmac_desc *atcdmac_alloc_desc(struct dma_chan *chan,
+					       gfp_t gfp_flags)
+{
+	struct atcdmac_dmac *dmac = atcdmac_dev_to_dmac(chan->device);
+	struct atcdmac_desc *desc;
+	dma_addr_t phys;
+
+	desc = dma_pool_zalloc(dmac->dma_desc_pool, gfp_flags, &phys);
+	if (desc) {
+		INIT_LIST_HEAD(&desc->tx_list);
+		dma_async_tx_descriptor_init(&desc->txd, chan);

[Severity: High]
Does this code leave desc_node uninitialized?

dma_pool_zalloc zeroes out the descriptor, leaving the pointers for desc_node as NULL since INIT_LIST_HEAD is not called for it. If a subsequent descriptor allocation fails (for instance, in atcdmac_prep_device_sg), the error path calls atcdmac_put_desc on the first descriptor.

This descends into atcdmac_put_desc_nolock, which performs list_del_init on desc_node, potentially causing a NULL pointer dereference on the uninitialized list head.

[ ... ]

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+static void atcdmac_put_desc_nolock(struct atcdmac_chan *dmac_chan,
+				    struct atcdmac_desc *desc)
+{

[ ... ]

drivers/dma/atcdmac300.c

Quoted 1 line
drivers/dma/atcdmac300.c new file +1 -0
+		list_del_init(&desc->desc_node);

[Severity: High]
Can this trigger a NULL pointer dereference as described above?

[ ... ]

drivers/dma/atcdmac300.c line 295

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+static void atcdmac_start_next_trans(struct atcdmac_chan *dmac_chan)
+{
+	struct atcdmac_desc *next_tx = NULL;
+	struct atcdmac_desc *dma_desc;
+
+	if (dmac_chan->cyclic) {
+		/* Get the next DMA descriptor from tx_list. */
+		dma_desc = atcdmac_get_active_head(dmac_chan);
+		dma_desc->at = dma_desc->at->next;

[Severity: High]
Could this dereference an invalid pointer if the active_list is empty?

The channel-wide cyclic flag is sticky and is never cleared when a transfer is terminated. If atcdmac_start_next_trans is called (for example, during system resume) on a previously cyclic channel that is now idle, active_list will be empty.

Using list_first_entry via atcdmac_get_active_head on an empty list returns a bogus pointer, leading to a fault when dma_desc->at is accessed.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static void atcdmac_advance_work(struct atcdmac_chan *dmac_chan)
+{

[ ... ]

drivers/dma/atcdmac300.c line 371

Quoted 7 lines
drivers/dma/atcdmac300.c new file +7 -0
+	if (dmac_chan->cyclic) {
+		if (!stop)
+			atcdmac_start_next_trans(dmac_chan);
+
+		spin_unlock_irqrestore(&dmac_chan->lock, flags);
+		res.result = DMA_TRANS_NOERROR;
+		dmaengine_desc_get_callback_invoke(&dma_desc->txd, &res);

[Severity: Critical]
Is there a use-after-free race here if atcdmac_terminate_all runs concurrently?

dma_desc remains in the unlocked active_list while the channel lock is dropped to invoke the client callback. If another thread calls atcdmac_terminate_all, it will splice active_list, abort the channel, manually run the callback on dma_desc again, and return it to the free_list.

Another CPU can then immediately reallocate the descriptor and overwrite the callback pointers while the first CPU is still reading them to execute, which could lead to arbitrary code execution.

[Severity: High]
Can this delayed threaded IRQ cause a double-start race with system resume?

If a transfer finishes right before suspend, the hardware interrupt fires but the threaded IRQ handler may not execute before the system freezes. On resume, stop_mask is cleared and the transfer is restarted. When normal operations resume, this delayed threaded IRQ runs, finds stop cleared, and erroneously double-starts the transfer while it is already actively running.

[ ... ]

drivers/dma/atcdmac300.c line 507

Quoted 11 lines
drivers/dma/atcdmac300.c new file +11 -0
+static irqreturn_t atcdmac_interrupt(int irq, void *dev_id)
+{
+	struct atcdmac_dmac *dmac = dev_id;
+	struct atcdmac_chan *dmac_chan;
+	unsigned int status;
+	unsigned int int_ch;
+	int ret = IRQ_NONE;
+	int i;
+
+	regmap_read(dmac->regmap, REG_INT_STA, &status);
+	int_ch = READ_ONCE(dmac->used_chan) & DMA_INT_ALL(status);

[Severity: High]
Does masking hardware interrupts with the software used_chan state risk an unacknowledged IRQ storm?

If an interrupt fires for a channel where the software used_chan bit is 0 (such as an abort interrupt triggered by atcdmac_terminate_all on an idle channel), int_ch evaluates to 0.

The handler bypasses the clear routine and returns without acknowledging the hardware interrupt, potentially causing an infinite loop of interrupts.

drivers/dma/atcdmac300.c line 510

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+
+	while (int_ch) {
+		spin_lock(&dmac->lock);

[Severity: High]
Is it safe to use a standard spinlock_t in hardirq context here?

On PREEMPT_RT kernels, spinlock_t is implemented as an RT mutex which can sleep. Locks acquired in genuine hardware interrupt context typically need to be typed as raw_spinlock_t to avoid scheduling while atomic.

drivers/dma/atcdmac300.c line 513

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+		dmac->used_chan = READ_ONCE(dmac->used_chan) & ~int_ch;
+		spin_unlock(&dmac->lock);
+		regmap_write(dmac->regmap, REG_INT_STA, DMA_INT_CLR(int_ch));

[Severity: Medium]
Does writing a blanket clear mask erase unhandled interrupts?

The DMA_INT_CLR macro writes 1s to the TC, ABT, and ERR bits for the given channel, regardless of which interrupts were actually set in the status read. If a new interrupt (like ERR) asserts after the status read but before this clear write, it gets cleared but never handled.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_terminate_all(struct dma_chan *chan)
+{

[ ... ]

drivers/dma/atcdmac300.c line 958

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+	spin_lock_irqsave(&dmac_chan->lock, flags);
+	atcdmac_abort_chan(dmac_chan);
+	atcdmac_enable_chan(dmac_chan, 0);
+	ret = regmap_read_poll_timeout_atomic(dmac_chan->dma_dev->regmap,
+					      REG_CH_EN,
+					      val,
+					      !(val & BIT(dmac_chan->chan_id)),
+					      10,
+					      ATCDMAC_CHAN_TIMEOUT_US);

[Severity: High]
Can this cause a soft lockup by polling for up to 100ms with interrupts disabled?

Calling regmap_read_poll_timeout_atomic with a 100,000us timeout while holding a spinlock in hardirq-disabled context can trigger the NMI watchdog and create extreme system latency spikes.

[ ... ]

drivers/dma/atcdmac300.c line 969

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+	list_for_each_entry_safe(desc_cur, desc_next, &list, desc_node) {
+		atcdmac_run_tx_complete_actions(desc_cur, DMA_TRANS_ABORTED);

[Severity: High]
Is it permissible to invoke completion callbacks for aborted transfers?

The dmaengine API explicitly prohibits executing callbacks for aborted descriptors because clients (such as ALSA) typically call terminate_all with their own stream locks held. Invoking the callback here can cause a deadlock when the client attempts to acquire the exact same lock.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/dma/atcdmac300.c line 1415

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+	ret = dma_async_device_register(&dmac->dma_device);
+	if (ret)
+		return ret;
+
+	ret = atcdmac_init_irq(pdev, dmac);
+	if (ret)
+		goto err_dma_async_register;
+
+	ret = atcdmac_init_iocp(pdev, dmac);

[Severity: High]
Are the DMA channels exposed to clients before the IRQ handler and cache coherency (IOCP) are fully initialized?

dma_async_device_register makes the DMA channels immediately available to kernel clients. If a client immediately requests a channel and starts a transfer before atcdmac_init_irq and atcdmac_init_iocp execute, the transfer will run without cache snooping configured, which could lead to memory corruption.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_resume(struct device *dev)
+{

[ ... ]

drivers/dma/atcdmac300.c line 1459

Quoted 7 lines
drivers/dma/atcdmac300.c new file +7 -0
+	list_for_each_entry_safe(chan,
+				 chan_next,
+				 &dmac->dma_device.channels,
+				 device_node) {
+		dmac_chan = atcdmac_chan_to_dmac_chan(chan);
+		spin_lock_irqsave(&dmac_chan->lock, flags);
+		atcdmac_start_next_trans(dmac_chan);

[Severity: High]
Could this improperly restart idle cyclic channels?

This unconditionally calls atcdmac_start_next_trans for every channel regardless of its active software state. If a channel was previously cyclic but is currently idle, it will cause a fault as described earlier when dereferencing the empty active_list.

[ ... ]

drivers/dma/atcdmac300.c line 1477

Quoted 12 lines
drivers/dma/atcdmac300.c new file +12 -0
+static int atcdmac_suspend(struct device *dev)
+{
+	struct atcdmac_dmac *dmac = dev_get_drvdata(dev);
+	int ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dmac->lock, flags);
+	dmac->stop_mask = BIT(dmac->num_ch) - 1;
+	spin_unlock_irqrestore(&dmac->lock, flags);
+	ret = atcdmac_wait_chan_idle(dmac,
+				     dmac->stop_mask,
+				     ATCDMAC_CHAN_TIMEOUT_US * dmac->num_ch);

[Severity: High]
Will system suspend time out and fail if a slave DMA channel is actively waiting for peripheral data?

The suspend sequence sets a stop_mask that prevents software from chaining the next descriptor, but does not pause actively running transfers. It then blocks waiting for all channels to report idle.

If a DEV_TO_MEM slave channel is waiting for hardware flow control or incoming data, it will never complete. The polling loop will eventually time out, aborting the entire system sleep transition.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH v6 2/2] dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

Quoted 5 lines · On Fri, Sep 11, 2026 at 07:57:12AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [Critical] Double-callback and Use-After-Free race in cyclic transfer completion during channel termination.
- [High] Masking pending hardware interrupts with software state (used_chan) causes an unacknowledged IRQ storm.
- [High] The DMA device is exposed to clients before its IRQ handler and cache coherency (IOCP) are fully initialized.
- [High] Polling for up to 100ms with interrupts disabled in atcdmac_terminate_all causes soft lockups.

Quoted 8 lines

- [High] The channel-wide cyclic flag is sticky, and unconditional resume restarts cause arbitrary DMA execution or kernel panics.
- [High] Suspend/resume race with the threaded IRQ causes transfer desynchronization and hardware corruption.
- [High] dmaengine API violation in atcdmac_terminate_all: invoking completion callbacks for aborted transfers.
- [High] System suspend times out and fails if a slave DMA channel is actively waiting for peripheral data.
- [High] Sleep-in-atomic bug on PREEMPT_RT due to using spinlock_t in hardirq context.
- [High] Kernel panic due to list_del_init() on an uninitialized list_head in error paths.
- [Medium] Writing a blanket clear mask to REG_INT_STA clears unhandled interrupts, permanently losing them. --

Please fix these problems. I will start review after fix sashiko report issues.

Frank

Quoted 260 lines

commit e06d7bd262ebaa03646e2e3ac2876918f25083d4
Author: CL Wang <[email protected]>

dmaengine: atcdmac300: Add driver for Andes ATCDMAC300 DMA controller

This patch adds support for the Andes ATCDMAC300 DMA controller. The ATCDMAC300 is a memory-to-memory and peripheral DMA controller that provides scatter-gather, cyclic, and slave transfer capabilities.

Quoted 4 lines
drivers/dma/atcdmac300.c new file +0 -0
new file mode 100644

[ ... ]

drivers/dma/atcdmac300.c line 84

Quoted 11 lines
drivers/dma/atcdmac300.c new file +11 -0
+static struct atcdmac_desc *atcdmac_alloc_desc(struct dma_chan *chan,
+					       gfp_t gfp_flags)
+{
+	struct atcdmac_dmac *dmac = atcdmac_dev_to_dmac(chan->device);
+	struct atcdmac_desc *desc;
+	dma_addr_t phys;
+
+	desc = dma_pool_zalloc(dmac->dma_desc_pool, gfp_flags, &phys);
+	if (desc) {
+		INIT_LIST_HEAD(&desc->tx_list);
+		dma_async_tx_descriptor_init(&desc->txd, chan);

[Severity: High]
Does this code leave desc_node uninitialized?

dma_pool_zalloc zeroes out the descriptor, leaving the pointers for desc_node as NULL since INIT_LIST_HEAD is not called for it. If a subsequent descriptor allocation fails (for instance, in atcdmac_prep_device_sg), the error path calls atcdmac_put_desc on the first descriptor.

This descends into atcdmac_put_desc_nolock, which performs list_del_init on desc_node, potentially causing a NULL pointer dereference on the uninitialized list head.

[ ... ]

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+static void atcdmac_put_desc_nolock(struct atcdmac_chan *dmac_chan,
+				    struct atcdmac_desc *desc)
+{

[ ... ]

drivers/dma/atcdmac300.c

Quoted 1 line
drivers/dma/atcdmac300.c new file +1 -0
+		list_del_init(&desc->desc_node);

[Severity: High]
Can this trigger a NULL pointer dereference as described above?

[ ... ]

drivers/dma/atcdmac300.c line 295

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+static void atcdmac_start_next_trans(struct atcdmac_chan *dmac_chan)
+{
+	struct atcdmac_desc *next_tx = NULL;
+	struct atcdmac_desc *dma_desc;
+
+	if (dmac_chan->cyclic) {
+		/* Get the next DMA descriptor from tx_list. */
+		dma_desc = atcdmac_get_active_head(dmac_chan);
+		dma_desc->at = dma_desc->at->next;

[Severity: High]
Could this dereference an invalid pointer if the active_list is empty?

The channel-wide cyclic flag is sticky and is never cleared when a transfer is terminated. If atcdmac_start_next_trans is called (for example, during system resume) on a previously cyclic channel that is now idle, active_list will be empty.

Using list_first_entry via atcdmac_get_active_head on an empty list returns a bogus pointer, leading to a fault when dma_desc->at is accessed.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static void atcdmac_advance_work(struct atcdmac_chan *dmac_chan)
+{

[ ... ]

drivers/dma/atcdmac300.c line 371

Quoted 7 lines
drivers/dma/atcdmac300.c new file +7 -0
+	if (dmac_chan->cyclic) {
+		if (!stop)
+			atcdmac_start_next_trans(dmac_chan);
+
+		spin_unlock_irqrestore(&dmac_chan->lock, flags);
+		res.result = DMA_TRANS_NOERROR;
+		dmaengine_desc_get_callback_invoke(&dma_desc->txd, &res);

[Severity: Critical]
Is there a use-after-free race here if atcdmac_terminate_all runs concurrently?

dma_desc remains in the unlocked active_list while the channel lock is dropped to invoke the client callback. If another thread calls atcdmac_terminate_all, it will splice active_list, abort the channel, manually run the callback on dma_desc again, and return it to the free_list.

Another CPU can then immediately reallocate the descriptor and overwrite the callback pointers while the first CPU is still reading them to execute, which could lead to arbitrary code execution.

[Severity: High]
Can this delayed threaded IRQ cause a double-start race with system resume?

If a transfer finishes right before suspend, the hardware interrupt fires but the threaded IRQ handler may not execute before the system freezes. On resume, stop_mask is cleared and the transfer is restarted. When normal operations resume, this delayed threaded IRQ runs, finds stop cleared, and erroneously double-starts the transfer while it is already actively running.

[ ... ]

drivers/dma/atcdmac300.c line 507

Quoted 11 lines
drivers/dma/atcdmac300.c new file +11 -0
+static irqreturn_t atcdmac_interrupt(int irq, void *dev_id)
+{
+	struct atcdmac_dmac *dmac = dev_id;
+	struct atcdmac_chan *dmac_chan;
+	unsigned int status;
+	unsigned int int_ch;
+	int ret = IRQ_NONE;
+	int i;
+
+	regmap_read(dmac->regmap, REG_INT_STA, &status);
+	int_ch = READ_ONCE(dmac->used_chan) & DMA_INT_ALL(status);

[Severity: High]
Does masking hardware interrupts with the software used_chan state risk an unacknowledged IRQ storm?

If an interrupt fires for a channel where the software used_chan bit is 0 (such as an abort interrupt triggered by atcdmac_terminate_all on an idle channel), int_ch evaluates to 0.

The handler bypasses the clear routine and returns without acknowledging the hardware interrupt, potentially causing an infinite loop of interrupts.

drivers/dma/atcdmac300.c line 510

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+
+	while (int_ch) {
+		spin_lock(&dmac->lock);

[Severity: High]
Is it safe to use a standard spinlock_t in hardirq context here?

On PREEMPT_RT kernels, spinlock_t is implemented as an RT mutex which can sleep. Locks acquired in genuine hardware interrupt context typically need to be typed as raw_spinlock_t to avoid scheduling while atomic.

drivers/dma/atcdmac300.c line 513

Quoted 3 lines
drivers/dma/atcdmac300.c new file +3 -0
+		dmac->used_chan = READ_ONCE(dmac->used_chan) & ~int_ch;
+		spin_unlock(&dmac->lock);
+		regmap_write(dmac->regmap, REG_INT_STA, DMA_INT_CLR(int_ch));

[Severity: Medium]
Does writing a blanket clear mask erase unhandled interrupts?

The DMA_INT_CLR macro writes 1s to the TC, ABT, and ERR bits for the given channel, regardless of which interrupts were actually set in the status read. If a new interrupt (like ERR) asserts after the status read but before this clear write, it gets cleared but never handled.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_terminate_all(struct dma_chan *chan)
+{

[ ... ]

drivers/dma/atcdmac300.c line 958

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+	spin_lock_irqsave(&dmac_chan->lock, flags);
+	atcdmac_abort_chan(dmac_chan);
+	atcdmac_enable_chan(dmac_chan, 0);
+	ret = regmap_read_poll_timeout_atomic(dmac_chan->dma_dev->regmap,
+					      REG_CH_EN,
+					      val,
+					      !(val & BIT(dmac_chan->chan_id)),
+					      10,
+					      ATCDMAC_CHAN_TIMEOUT_US);

[Severity: High]
Can this cause a soft lockup by polling for up to 100ms with interrupts disabled?

Calling regmap_read_poll_timeout_atomic with a 100,000us timeout while holding a spinlock in hardirq-disabled context can trigger the NMI watchdog and create extreme system latency spikes.

[ ... ]

drivers/dma/atcdmac300.c line 969

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+	list_for_each_entry_safe(desc_cur, desc_next, &list, desc_node) {
+		atcdmac_run_tx_complete_actions(desc_cur, DMA_TRANS_ABORTED);

[Severity: High]
Is it permissible to invoke completion callbacks for aborted transfers?

The dmaengine API explicitly prohibits executing callbacks for aborted descriptors because clients (such as ALSA) typically call terminate_all with their own stream locks held. Invoking the callback here can cause a deadlock when the client attempts to acquire the exact same lock.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/dma/atcdmac300.c line 1415

Quoted 9 lines
drivers/dma/atcdmac300.c new file +9 -0
+	ret = dma_async_device_register(&dmac->dma_device);
+	if (ret)
+		return ret;
+
+	ret = atcdmac_init_irq(pdev, dmac);
+	if (ret)
+		goto err_dma_async_register;
+
+	ret = atcdmac_init_iocp(pdev, dmac);

[Severity: High]
Are the DMA channels exposed to clients before the IRQ handler and cache coherency (IOCP) are fully initialized?

dma_async_device_register makes the DMA channels immediately available to kernel clients. If a client immediately requests a channel and starts a transfer before atcdmac_init_irq and atcdmac_init_iocp execute, the transfer will run without cache snooping configured, which could lead to memory corruption.

[ ... ]

Quoted 2 lines
drivers/dma/atcdmac300.c new file +2 -0
+static int atcdmac_resume(struct device *dev)
+{

[ ... ]

drivers/dma/atcdmac300.c line 1459

Quoted 7 lines
drivers/dma/atcdmac300.c new file +7 -0
+	list_for_each_entry_safe(chan,
+				 chan_next,
+				 &dmac->dma_device.channels,
+				 device_node) {
+		dmac_chan = atcdmac_chan_to_dmac_chan(chan);
+		spin_lock_irqsave(&dmac_chan->lock, flags);
+		atcdmac_start_next_trans(dmac_chan);

[Severity: High]
Could this improperly restart idle cyclic channels?

This unconditionally calls atcdmac_start_next_trans for every channel regardless of its active software state. If a channel was previously cyclic but is currently idle, it will cause a fault as described earlier when dereferencing the empty active_list.

[ ... ]

drivers/dma/atcdmac300.c line 1477

Quoted 12 lines
drivers/dma/atcdmac300.c new file +12 -0
+static int atcdmac_suspend(struct device *dev)
+{
+	struct atcdmac_dmac *dmac = dev_get_drvdata(dev);
+	int ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dmac->lock, flags);
+	dmac->stop_mask = BIT(dmac->num_ch) - 1;
+	spin_unlock_irqrestore(&dmac->lock, flags);
+	ret = atcdmac_wait_chan_idle(dmac,
+				     dmac->stop_mask,
+				     ATCDMAC_CHAN_TIMEOUT_US * dmac->num_ch);

[Severity: High]
Will system suspend time out and fail if a slave DMA channel is actively waiting for peripheral data?

The suspend sequence sets a stop_mask that prevents software from chaining the next descriptor, but does not pause actively running transfers. It then blocks waiting for all channels to report idle.

If a DEV_TO_MEM slave channel is waiting for hardware flow control or incoming data, it will never complete. The polling loop will eventually time out, aborting the entire system sleep transition.

[ ... ]

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2