[PATCH] accel/habanalabs: kzalloc + kcalloc to kzalloc_flex

From: Rosen Penev

Date: Tue Mar 10 2026 - 20:46:18 EST


Simplifies allocation by using a flexible array member.

No need to free separately.

Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
.../habanalabs/common/command_submission.c | 20 ++-----------------
drivers/accel/habanalabs/common/habanalabs.h | 2 +-
2 files changed, 3 insertions(+), 19 deletions(-)

diff --git a/drivers/accel/habanalabs/common/command_submission.c b/drivers/accel/habanalabs/common/command_submission.c
index ba4257bda77b..ba99329eacbd 100644
--- a/drivers/accel/habanalabs/common/command_submission.c
+++ b/drivers/accel/habanalabs/common/command_submission.c
@@ -809,7 +809,6 @@ static void cs_do_release(struct kref *ref)

hl_fence_put(cs->fence);

- kfree(cs->jobs_in_queue_cnt);
kfree(cs);
}

@@ -907,9 +906,9 @@ static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,

cntr = &hdev->aggregated_cs_counters;

- cs = kzalloc_obj(*cs, GFP_ATOMIC);
+ cs = kzalloc_flex(*cs, jobs_in_queue_cnt, hdev->asic_prop.max_queues, GFP_ATOMIC);
if (!cs)
- cs = kzalloc_obj(*cs);
+ cs = kzalloc_flex(*cs, jobs_in_queue_cnt, hdev->asic_prop.max_queues);

if (!cs) {
atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
@@ -947,19 +946,6 @@ static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,
goto free_cs;
}

- cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
- sizeof(*cs->jobs_in_queue_cnt), GFP_ATOMIC);
- if (!cs->jobs_in_queue_cnt)
- cs->jobs_in_queue_cnt = kcalloc(hdev->asic_prop.max_queues,
- sizeof(*cs->jobs_in_queue_cnt), GFP_KERNEL);
-
- if (!cs->jobs_in_queue_cnt) {
- atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
- atomic64_inc(&cntr->out_of_mem_drop_cnt);
- rc = -ENOMEM;
- goto free_cs_cmpl;
- }
-
cs_cmpl->hdev = hdev;
cs_cmpl->type = cs->type;
spin_lock_init(&cs_cmpl->lock);
@@ -1014,8 +1000,6 @@ static int allocate_cs(struct hl_device *hdev, struct hl_ctx *ctx,

free_fence:
spin_unlock(&ctx->cs_lock);
- kfree(cs->jobs_in_queue_cnt);
-free_cs_cmpl:
kfree(cs_cmpl);
free_cs:
kfree(cs);
diff --git a/drivers/accel/habanalabs/common/habanalabs.h b/drivers/accel/habanalabs/common/habanalabs.h
index d94c2ba22a6a..dc13c8a6f242 100644
--- a/drivers/accel/habanalabs/common/habanalabs.h
+++ b/drivers/accel/habanalabs/common/habanalabs.h
@@ -2046,7 +2046,6 @@ struct hl_userptr {
* @encaps_signals: true if this CS has encaps reserved signals.
*/
struct hl_cs {
- u16 *jobs_in_queue_cnt;
struct hl_ctx *ctx;
struct list_head job_list;
spinlock_t job_lock;
@@ -2080,6 +2079,7 @@ struct hl_cs {
u8 staged_cs;
u8 skip_reset_on_timeout;
u8 encaps_signals;
+ u16 jobs_in_queue_cnt[];
};

/**
--
2.53.0