Skip to contentSkip to conversation

dmaengine: qcom_hidma: fix memory leak issues

PATCH In review Qasim Ijaz 3 participants 8 messages Jun 1, 2025 – Jun 16, 2025

Subsystems (4)
lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

cover dmaengine: qcom_hidma: fix memory leak issues

fix a few memory leak issues.

Qasim Ijaz (2):
fix memory leak on probe failure
fix handoff FIFO memory leak on driver removal

 drivers/dma/qcom/hidma_ll.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH 1/2 dmaengine: qcom_hidma: fix memory leak on probe failure

hidma_ll_init() is invoked to create and initialise a struct hidma_lldev object during hidma probe. During this a FIFO buffer is allocated, but if some failure occurs after (like hidma_ll_setup failure) we should clean up the FIFO.

Fixes d1615ca2e085 ("dmaengine: qcom_hidma: implement lower level hardware interface")
---
 drivers/dma/qcom/hidma_ll.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/dma/qcom/hidma_ll.c +3 -1
index 53244e0e34a3..fee448499777 100644
@@ -788,8 +788,10 @@ struct hidma_lldev *hidma_ll_init(struct device *dev, u32 nr_tres,
 		return NULL;
 
 	rc = hidma_ll_setup(lldev);
-	if (rc)
+	if (rc) {
+		kfifo_free(&lldev->handoff_fifo);
 		return NULL;
+	}
 
 	spin_lock_init(&lldev->lock);
 	tasklet_setup(&lldev->task, hidma_ll_tre_complete);
Signature · 1 line
2.39.5
author
#3 lore raw

PATCH 2/2 dmaengine: qcom_hidma: fix handoff FIFO memory leak on driver removal

In reply to Qasim Ijaz

hidma_ll_init() allocates a handoff FIFO, but the matching hidma_ll_uninit() function (which is invoked in remove()) never releases it, leaking memory.

To fix this call kfifo_free in hidma_ll_uninit().

Fixes d1615ca2e085 ("dmaengine: qcom_hidma: implement lower level hardware interface")
---
 drivers/dma/qcom/hidma_ll.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma/qcom/hidma_ll.c +1 -0
index fee448499777..0c2bae46746c 100644
@@ -816,6 +816,7 @@ int hidma_ll_uninit(struct hidma_lldev *lldev)
 
 	required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
 	tasklet_kill(&lldev->task);
+	kfifo_free(&lldev->handoff_fifo);
 	memset(lldev->trepool, 0, required_bytes);
 	lldev->trepool = NULL;
 	atomic_set(&lldev->pending_tre_count, 0);
Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH 2/2] dmaengine: qcom_hidma: fix handoff FIFO memory leak on driver removal

Quoted 19 lines · Qasim Ijaz

hidma_ll_init() allocates a handoff FIFO, but the matching hidma_ll_uninit() function (which is invoked in remove()) never releases it, leaking memory.

To fix this call kfifo_free in hidma_ll_uninit().

Fixes d1615ca2e085 ("dmaengine: qcom_hidma: implement lower level hardware interface")
---
 drivers/dma/qcom/hidma_ll.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma/qcom/hidma_ll.c +0 -0
index fee448499777..0c2bae46746c 100644

drivers/dma/qcom/hidma_ll.c int hidma_ll_uninit(struct hidma_lldev *lldev) · line 822

Quoted 7 lines
drivers/dma/qcom/hidma_ll.c +1 -0
@@ -816,6 +816,7 @@ int hidma_ll_uninit(struct hidma_lldev *lldev)
 
 	required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
 	tasklet_kill(&lldev->task);
+	kfifo_free(&lldev->handoff_fifo);
 	memset(lldev->trepool, 0, required_bytes);
 	lldev->trepool = NULL;
 	atomic_set(&lldev->pending_tre_count, 0);

Is it possible that the handoff_fifo is freed, then we could observe reset complete interrupts before they are being cleared in hidma_ll_uninit later on, which would lead to the following call chain

hidma_ll_inthandler - hidma_ll_int_handler_internal -
hidma_handle_tre_completion - hidma_post_completed - tasklet_schedule(&lldev->task); - hidma_ll_tre_complete - kfifo_out

?

Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] dmaengine: qcom_hidma: fix memory leak on probe failure

In reply to Qasim Ijaz
Quoted 8 lines · Qasim Ijaz

hidma_ll_init() is invoked to create and initialise a struct hidma_lldev object during hidma probe. During this a FIFO buffer is allocated, but if some failure occurs after (like hidma_ll_setup failure) we should clean up the FIFO.

Fixes d1615ca2e085 ("dmaengine: qcom_hidma: implement lower level hardware interface")
maintainer
#5 lore raw

Re: [PATCH 2/2] dmaengine: qcom_hidma: fix handoff FIFO memory leak on driver removal

In reply to Eugen Hristev
Quoted 16 lines · Eugen Hristev

drivers/dma/qcom/hidma_ll.c int hidma_ll_uninit(struct hidma_lldev *lldev) · line 822

Quoted 12 lines
drivers/dma/qcom/hidma_ll.c +1 -0
index fee448499777..0c2bae46746c 100644
@@ -816,6 +816,7 @@ int hidma_ll_uninit(struct hidma_lldev *lldev)
 
  	required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
  	tasklet_kill(&lldev->task);
+	kfifo_free(&lldev->handoff_fifo);
  	memset(lldev->trepool, 0, required_bytes);
  	lldev->trepool = NULL;
  	atomic_set(&lldev->pending_tre_count, 0);

Is it possible that the handoff_fifo is freed, then we could observe reset complete interrupts before they are being cleared in hidma_ll_uninit later on, which would lead to the following call chain

Quoted 3 lines

hidma_ll_inthandler - hidma_ll_int_handler_internal -
hidma_handle_tre_completion - hidma_post_completed - tasklet_schedule(&lldev->task); - hidma_ll_tre_complete - kfifo_out

According to the documentation, the way to guarantee this from not happening

is to call tasklet_disable() to ensure that tasklet completes execution. Only after that

data structures used by the tasklet can be freed.

I think proper order is:

1. tasklet_disable

2. tasklet_kill

3. kfifo_free

author
#6 lore raw

Re: [PATCH 2/2] dmaengine: qcom_hidma: fix handoff FIFO memory leak on driver removal

Quoted 28 lines · Sinan Kaya
Quoted 15 lines · Eugen Hristev

drivers/dma/qcom/hidma_ll.c int hidma_ll_uninit(struct hidma_lldev *lldev) · line 821

Quoted 11 lines
drivers/dma/qcom/hidma_ll.c +1 -0
index fee448499777..0c2bae46746c 100644
@@ -816,6 +816,7 @@ int hidma_ll_uninit(struct hidma_lldev *lldev)
  	required_bytes = sizeof(struct hidma_tre) * lldev->nr_tres;
  	tasklet_kill(&lldev->task);
+	kfifo_free(&lldev->handoff_fifo);
  	memset(lldev->trepool, 0, required_bytes);
  	lldev->trepool = NULL;
  	atomic_set(&lldev->pending_tre_count, 0);

Is it possible that the handoff_fifo is freed, then we could observe reset complete interrupts before they are being cleared in hidma_ll_uninit later on, which would lead to the following call chain

Quoted 3 lines

hidma_ll_inthandler - hidma_ll_int_handler_internal -
hidma_handle_tre_completion - hidma_post_completed - tasklet_schedule(&lldev->task); - hidma_ll_tre_complete - kfifo_out

According to the documentation, the way to guarantee this from not happening

is to call tasklet_disable() to ensure that tasklet completes execution. Only after that

data structures used by the tasklet can be freed.

Quoted 7 lines

I think proper order is:

1. tasklet_disable

2. tasklet_kill

3. kfifo_free

Hi Sinan, hi Eugen,

Thanks for reviewing the patch and for pointing out the correct shutdown ordering.

If you’re both happy with it, I’ll send a v2 that calls tasklet_disable() before tasklet_kill(), then frees the handoff_fifo.

Just let me know and I’ll resend.

Thanks
Qasim

Quoted 3 lines
maintainer
#7 lore raw

Re: [PATCH 2/2] dmaengine: qcom_hidma: fix handoff FIFO memory leak on driver removal

Quoted 7 lines · Qasim Ijaz

Thanks for reviewing the patch and for pointing out the correct shutdown ordering.

If you’re both happy with it, I’ll send a v2 that calls tasklet_disable() before tasklet_kill(), then frees the handoff_fifo.

Just let me know and I’ll resend.

Sure, please test it on a test kernel module before posting with a tasklet.

This should be straightforward to verify.