Skip to contentSkip to conversation

accel/habanalabs: kzalloc + kcaalloc to kzalloc_flex

PATCH New Rosen Penev 1 participant 1 message Jun 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/habanalabs: kzalloc + kcaalloc to kzalloc_flex

Simplifies allocation by using a flexible array member.

No need to free separately.

---
 .../habanalabs/common/command_submission.c    | 20 ++-----------------
 drivers/accel/habanalabs/common/habanalabs.h  |  2 +-
 2 files changed, 3 insertions(+), 19 deletions(-)
drivers/accel/habanalabs/common/command_submission.c +2 -18
index ba4257bda77b..ba99329eacbd 100644
@@ -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);
drivers/accel/habanalabs/common/habanalabs.h +1 -1
index d94c2ba22a6a..dc13c8a6f242 100644
@@ -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[];
 };
 
 /**
Signature · 1 line
2.54.0