static enum dmastatus mtkcqdmatxstatus(struct dma_chan *c, dmacookiet cookie, struct…
- User
static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c, dma_cookie_t cookie, struct dma_tx_state *txstate) { struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c); struct mtk_cqdma_vdesc *cvd; struct virt_dma_desc *vd; enum dma_status ret; unsigned long flags; size_t bytes = 0;
ret = dma_cookie_status(c, cookie, txstate); if (ret == DMA_COMPLETE || !txstate) return ret; spin_lock_irqsave(&cvc->pc->lock, flags); spin_lock_irqsave(&cvc->vc.lock, flags); vd = mtk_cqdma_find_active_desc(c, cookie); spin_unlock_irqrestore(&cvc->vc.lock, flags); spin_unlock_irqrestore(&cvc->pc->lock, flags); if (vd) { cvd = to_cqdma_vdesc(vd); bytes = cvd->residue; } dma_set_residue(txstate, bytes); return ret;} 这段代码该如何修复,spin_lock_irqsave存了一次,还需要再存吗?
Assistant
这段代码确实存在问题。
spin_lock_irqsave的使用方式不正确。主要问题是:
- 连续两次调用
spin_lock_irqsave使用同一个flags变量 - 解锁顺序错误(应该按照加锁的相反顺序解锁)
修复方案有两种:
方案一:使用两个不同的 flags 变量
static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c, dma_cookie_t cookie, struct dma_tx_state *txstate) { struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c); struct mtk_cqdma_vdesc *cvd; struct virt_dma_desc *vd; enum dma_status ret; unsigned long flags1, flags2; size_t bytes = 0; ret = dma_cookie_status(c, cookie, txstate); if (ret == DMA_COMPLETE || !txstate) return ret; spin_lock_irqsave(&cvc->pc->lock, flags1); spin_lock_irqsave(&cvc->vc.lock, flags2); vd = mtk_cqdma_find_active_desc(c, cookie); spin_unlock_irqrestore(&cvc->vc.lock, flags2); spin_unlock_irqrestore(&cvc->pc->lock, flags1); if (vd) { cvd = to_cqdma_vdesc(vd); bytes = cvd->residue; } dma_set_residue(txstate, bytes); return ret; }方案二:如果只需要一个锁(更常见的情况)
static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c, dma_cookie_t cookie, struct dma_tx_state *txstate) { struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c); struct mtk_cqdma_vdesc *cvd; struct virt_dma_desc *vd; enum dma_status ret; unsigned long flags; size_t bytes = 0; ret = dma_cookie_status(c, cookie, txstate); if (ret == DMA_COMPLETE || !txstate) return ret; spin_lock_irqsave(&cvc->vc.lock, flags); vd = mtk_cqdma_find_active_desc(c, cookie); if (vd) { cvd = to_cqdma_vdesc(vd); bytes = cvd->residue; } spin_unlock_irqrestore(&cvc->vc.lock, flags); dma_set_residue(txstate, bytes); return ret; }关于是否需要两个锁,这取决于:
mtk_cqdma_find_active_desc函数的实现细节- 数据结构的保护需求
- 是否存在潜在的死锁风险
通常在 DMA 驱动中,虚拟通道锁(
vc.lock)就足以保护描述符列表的访问。只有在需要同时访问物理通道和虚拟通道资源时,才需要两个锁。建议查看驱动的其他部分,看看类似操作是如何处理的。- 连续两次调用
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.