[PATCH] accel/habanalabs: Use common error handling code in hl_cb_alloc()

From: Markus Elfring

Date: Wed Jun 10 2026 - 14:36:27 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 10 Jun 2026 20:30:23 +0200

Use an additional label so that a bit of exception handling can be better
reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/accel/habanalabs/common/command_buffer.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/accel/habanalabs/common/command_buffer.c b/drivers/accel/habanalabs/common/command_buffer.c
index e929db8bc023..980c9e4f96c6 100644
--- a/drivers/accel/habanalabs/common/command_buffer.c
+++ b/drivers/accel/habanalabs/common/command_buffer.c
@@ -126,10 +126,8 @@ static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,

if (internal_cb) {
p = (void *) gen_pool_alloc(hdev->internal_cb_pool, cb_size);
- if (!p) {
- kfree(cb);
- return NULL;
- }
+ if (!p)
+ goto free_cb;

cb_offset = p - hdev->internal_cb_pool_virt_addr;
cb->is_internal = true;
@@ -147,14 +145,17 @@ static struct hl_cb *hl_cb_alloc(struct hl_device *hdev, u32 cb_size,
dev_err(hdev->dev,
"failed to allocate %d of dma memory for CB\n",
cb_size);
- kfree(cb);
- return NULL;
+ goto free_cb;
}

cb->kernel_address = p;
cb->size = cb_size;

return cb;
+
+free_cb:
+ kfree(cb);
+ return NULL;
}

struct hl_cb_mmap_mem_alloc_args {
--
2.54.0