From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Guodong Xu <guodong@riscstar.com>,
Juan Li <lijuan@linux.spacemit.com>,
Vinod Koul <vkoul@kernel.org>, Sasha Levin <sashal@kernel.org>,
dlan@gentoo.org, dmaengine@vger.kernel.org,
linux-riscv@lists.infradead.org, spacemit@lists.linux.dev
Subject: [PATCH AUTOSEL 6.18] dmaengine: mmp_pdma: Fix race condition in mmp_pdma_residue()
Date: Tue, 20 Jan 2026 14:34:48 -0500 [thread overview]
Message-ID: <20260120193456.865383-5-sashal@kernel.org> (raw)
In-Reply-To: <20260120193456.865383-1-sashal@kernel.org>
From: Guodong Xu <guodong@riscstar.com>
[ Upstream commit a143545855bc2c6e1330f6f57ae375ac44af00a7 ]
Add proper locking in mmp_pdma_residue() to prevent use-after-free when
accessing descriptor list and descriptor contents.
The race occurs when multiple threads call tx_status() while the tasklet
on another CPU is freeing completed descriptors:
CPU 0 CPU 1
----- -----
mmp_pdma_tx_status()
mmp_pdma_residue()
-> NO LOCK held
list_for_each_entry(sw, ..)
DMA interrupt
dma_do_tasklet()
-> spin_lock(&desc_lock)
list_move(sw->node, ...)
spin_unlock(&desc_lock)
| dma_pool_free(sw) <- FREED!
-> access sw->desc <- UAF!
This issue can be reproduced when running dmatest on the same channel with
multiple threads (threads_per_chan > 1).
Fix by protecting the chain_running list iteration and descriptor access
with the chan->desc_lock spinlock.
Signed-off-by: Juan Li <lijuan@linux.spacemit.com>
Signed-off-by: Guodong Xu <guodong@riscstar.com>
Link: https://patch.msgid.link/20251216-mmp-pdma-race-v1-1-976a224bb622@riscstar.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
LLM Generated explanations, may be completely bogus:
This confirms the `mmp_pdma_residue()` function was added in 2014. The
race condition has existed since then.
## Analysis Summary
### 1. COMMIT MESSAGE ANALYSIS
The commit message clearly describes:
- A **use-after-free (UAF) race condition** in `mmp_pdma_residue()`
- The race occurs between `tx_status()` (calling `mmp_pdma_residue()`)
and the DMA tasklet (`dma_do_tasklet()`)
- **Clear reproduction steps**: running dmatest with `threads_per_chan >
1`
- The CPU timeline diagram explicitly shows the race window between
unlocked list iteration and descriptor free
Keywords: "Fix race condition", "use-after-free", "UAF"
### 2. CODE CHANGE ANALYSIS
**The bug:** `mmp_pdma_residue()` iterates over `chan->chain_running`
list and accesses descriptor contents (`sw->desc`) without holding the
`desc_lock` spinlock. Meanwhile, `dma_do_tasklet()` can:
1. Hold `desc_lock`
2. Move descriptors from `chain_running` to `chain_cleanup` via
`list_move()`
3. Release `desc_lock`
4. Free the descriptor via `dma_pool_free()`
If `mmp_pdma_residue()` is iterating over the list when this happens, it
can access freed memory (UAF).
**The fix:** Adds `spin_lock_irqsave(&chan->desc_lock, flags)` before
the `list_for_each_entry()` loop and `spin_unlock_irqrestore()` at all
exit points from the function. This is:
- Small (+6 lines, -0 lines of actual logic)
- Surgical - uses the existing `desc_lock` that already protects this
list elsewhere
- Follows existing driver patterns (other list operations use this lock)
### 3. CLASSIFICATION
This is a **bug fix** for a **use-after-free vulnerability**. This is a
serious memory safety issue:
- Can cause kernel crashes/panics
- Can lead to data corruption
- Potential security implications (UAF bugs are commonly exploitable)
### 4. SCOPE AND RISK ASSESSMENT
- **Lines changed**: ~10 lines (just adding lock/unlock around existing
code)
- **Files touched**: 1 file
- **Complexity**: Very low - straightforward spinlock acquisition
- **Risk of regression**: Minimal - uses existing lock that is designed
for this purpose
- **Potential issues**: Slightly increased lock contention when calling
`tx_status()`, but this is necessary for correctness
### 5. USER IMPACT
- **Who is affected**: Users of MMP PDMA DMA controller (Marvell PXA/MMP
platforms, SpacemiT K1)
- **Trigger conditions**: Multi-threaded DMA operations on same channel
(common in dmatest, may occur in real workloads)
- **Severity**: High - UAF can cause kernel crashes or undefined
behavior
- **Reproducibility**: Can be reproduced with dmatest (`threads_per_chan
> 1`)
### 6. STABILITY INDICATORS
- Has proper sign-offs: `Signed-off-by` from author, forwarder, and DMA
maintainer (Vinod Koul)
- Link to mailing list discussion included
- The existing lock (`desc_lock`) has been in the driver since the
beginning
- The locking pattern matches other functions in the driver
### 7. DEPENDENCY CHECK
- No dependencies on other commits
- The affected code (`mmp_pdma_residue()`) has existed since 2014
- The `desc_lock` spinlock exists in all stable trees
- Should apply cleanly to any kernel from 3.15+ (when residue reporting
was added)
**Note**: Recent mainline commits added SpacemiT K1 support with 64-bit
operations (`pdev->ops->get_desc_dst_addr`, etc.), but the core logic
and the race condition existed with the original 32-bit implementation
too. For older stable trees (pre-6.x), minor context adjustments might
be needed due to the `pdev->ops->` refactoring, but the fix concept
remains the same.
### Conclusion
This commit fixes a clear **use-after-free race condition** in the MMP
PDMA driver. The bug:
- Can cause kernel crashes/panics
- Has existed since 2014 (when residue reporting was added)
- Is reproducible with standard kernel testing tools (dmatest)
The fix:
- Is small and surgical (~10 lines of spinlock additions)
- Uses existing infrastructure (the `desc_lock` already exists for this
purpose)
- Follows existing driver patterns
- Has minimal regression risk
This is exactly the type of fix stable trees need: a clearly correct fix
for a real bug that users can hit, with minimal risk of introducing new
problems.
**YES**
drivers/dma/mmp_pdma.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/dma/mmp_pdma.c b/drivers/dma/mmp_pdma.c
index d07229a748868..481b58c414e47 100644
--- a/drivers/dma/mmp_pdma.c
+++ b/drivers/dma/mmp_pdma.c
@@ -928,6 +928,7 @@ static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
{
struct mmp_pdma_desc_sw *sw;
struct mmp_pdma_device *pdev = to_mmp_pdma_dev(chan->chan.device);
+ unsigned long flags;
u64 curr;
u32 residue = 0;
bool passed = false;
@@ -945,6 +946,8 @@ static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
else
curr = pdev->ops->read_src_addr(chan->phy);
+ spin_lock_irqsave(&chan->desc_lock, flags);
+
list_for_each_entry(sw, &chan->chain_running, node) {
u64 start, end;
u32 len;
@@ -989,6 +992,7 @@ static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
continue;
if (sw->async_tx.cookie == cookie) {
+ spin_unlock_irqrestore(&chan->desc_lock, flags);
return residue;
} else {
residue = 0;
@@ -996,6 +1000,8 @@ static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
}
}
+ spin_unlock_irqrestore(&chan->desc_lock, flags);
+
/* We should only get here in case of cyclic transactions */
return residue;
}
--
2.51.0
next prev parent reply other threads:[~2026-01-20 19:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 19:34 [PATCH AUTOSEL 6.18] ALSA: usb-audio: Prevent excessive number of frames Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-6.6] ASoC: amd: yc: Fix microphone on ASUS M6500RE Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-5.10] ASoC: tlv320adcx140: Propagate error codes during probe Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-6.1] nvme-fc: release admin tagset if init fails Sasha Levin
2026-01-20 19:34 ` Sasha Levin [this message]
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-5.10] ASoC: davinci-evm: Fix reference leak in davinci_evm_probe Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-6.6] nvmet-tcp: fixup hang in nvmet_tcp_listen_data_ready() Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18-6.12] ASoC: simple-card-utils: Check device node before overwrite direction Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18] ASoC: Intel: sof_sdw: Add new quirks for PTL on Dell with CS42L43 Sasha Levin
2026-01-20 19:34 ` [PATCH AUTOSEL 6.18] ALSA: hda/tas2781: Add newly-released HP laptop Sasha Levin
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=20260120193456.865383-5-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=dlan@gentoo.org \
--cc=dmaengine@vger.kernel.org \
--cc=guodong@riscstar.com \
--cc=lijuan@linux.spacemit.com \
--cc=linux-riscv@lists.infradead.org \
--cc=patches@lists.linux.dev \
--cc=spacemit@lists.linux.dev \
--cc=stable@vger.kernel.org \
--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