[PATCH 3/4] crypto: hisilicon/qm - fix memory leak in hisi_qm_sort_devices

From: Chenghai Huang

Date: Fri Sep 11 2026 - 06:38:15 EST


From: Wenkai Lin <linwenkai6@xxxxxxxxxxxxx>

hisi_qm_sort_devices() allocates a struct hisi_qm_resource for each
QM device and inserts it into one of two local lists (non_full_list
or full_list). If kzalloc() fails mid-loop, the function returns
-ENOMEM immediately without freeing the resources already inserted
into the local lists.

Because the splice into the caller's @head list happens only after
the loop completes, the caller's free_list(&head) cannot reclaim
them, so the already-allocated res entries are lost.

Fix by freeing both local lists before returning -ENOMEM.

Fixes: 2a75decec119 ("crypto: hisilicon/qm - optimize device selection priority based on queue ref count and NUMA distance")
Signed-off-by: Wenkai Lin <linwenkai6@xxxxxxxxxxxxx>
Signed-off-by: Chenghai Huang <huangchenghai2@xxxxxxxxxx>
---
drivers/crypto/hisilicon/qm.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c
index e915cacef5c0..0448c68dbde4 100644
--- a/drivers/crypto/hisilicon/qm.c
+++ b/drivers/crypto/hisilicon/qm.c
@@ -3846,8 +3846,11 @@ static int hisi_qm_sort_devices(int node, struct list_head *head,
dev_node = 0;

res = kzalloc_obj(*res);
- if (!res)
+ if (!res) {
+ free_list(&non_full_list);
+ free_list(&full_list);
return -ENOMEM;
+ }

res->qm = qm;
res->distance = node_distance(dev_node, node);
--
2.43.0