[PATCH 2/5] HID: intel-thc-hid: intel-thc: free the failed channel

From: Emfox Zhou

Date: Mon Nov 16 2026 - 08:32:18 EST


thc_dma_allocate() unwinds with "while (chan--)". The condition is read
before the decrement, so the first channel released is the one before the
failure, and the failed channel keeps the PRD tables and the
scatter-gather lists it did manage to allocate.

Unwind through thc_dma_release() instead, which walks every channel and
skips the ones that were never set up, and free a scatter-gather list that
was allocated but never mapped: sgl_alloc() leaves one behind when its
entry count is too large.

That leaves release_dma_buffers() with a configuration whose prd_tbls may
still be NULL, which its unconditional memset() does not survive. Clear the
tables only when there are any.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@xxxxxxxxxx>
---
.../hid/intel-thc-hid/intel-thc/intel-thc-dma.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index c0dc44e26..704c02822 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -297,20 +297,20 @@ static void release_dma_buffers(struct thc_device *dev,
return;

for (i = 0; i < config->prd_tbl_num; i++) {
- if (!config->sgls[i] || !config->sgls_nent[i])
+ if (!config->sgls[i])
continue;

- dma_unmap_sg(dev->dev, config->sgls[i],
- config->sgls_nent_pages[i],
- config->dir);
+ if (config->sgls_nent[i])
+ dma_unmap_sg(dev->dev, config->sgls[i],
+ config->sgls_nent_pages[i],
+ config->dir);

sgl_free(config->sgls[i]);
config->sgls[i] = NULL;
}

- memset(config->prd_tbls, 0, prd_tbls_size);
-
if (config->prd_tbls) {
+ memset(config->prd_tbls, 0, prd_tbls_size);
dma_free_coherent(dev->dev, prd_tbls_size, config->prd_tbls,
config->prd_tbls_dma_handle);
config->prd_tbls = NULL;
@@ -425,8 +425,7 @@ int thc_dma_allocate(struct thc_device *dev)
return 0;

release_bufs:
- while (chan--)
- release_dma_buffers(dev, &dev->dma_ctx->dma_config[chan]);
+ thc_dma_release(dev);

return ret;
}