From d2c81f797c7fd21c98fba6f288766de8339ccf4a Mon Sep 17 00:00:00 2001 From: Sugar Zhang Date: Tue, 29 Apr 2025 18:15:49 +0800 Subject: [PATCH] dmaengine: rockchip-dma: Fix overflow in get_bytes_xfered calculation The transferred bytes are calculated by subtracting first_lli.base from current position (cur_pos). However, cur_pos remains 0 until the first burst transfer completes, which could result in a negative value. This leads to incorrect byte count reporting. Fix the overflow by clamping the calculated bytes to 0 when negative, ensuring the reported transfer position matches hardware state before the first burst completion. Signed-off-by: Sugar Zhang Change-Id: Ie9d14f514d2ebfe068b535b2f9892b319cb8a41b --- drivers/dma/rockchip-dma.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/dma/rockchip-dma.c b/drivers/dma/rockchip-dma.c index 43e6d4a8309c..59fd30e7bac2 100644 --- a/drivers/dma/rockchip-dma.c +++ b/drivers/dma/rockchip-dma.c @@ -682,6 +682,19 @@ static int rk_dma_lch_get_bytes_xfered(struct rk_dma_lch *l) else bytes = ds->desc_hw[0].dar - ds->desc_hw[1].dar; + /* + * The transferred bytes are calculated by subtracting first_lli.base from + * current position (cur_pos). However, cur_pos remains 0 until the first + * burst transfer completes, which could result in a negative value. + * This leads to incorrect byte count reporting. + * + * Fix the overflow by clamping the calculated bytes to 0 when negative, + * ensuring the reported transfer position matches hardware state before + * the first burst completion. + */ + if (bytes < 0) + bytes = 0; + return bytes; }