[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250804163947.630568-1-abinashsinghlalotra@gmail.com>
Date: Mon, 4 Aug 2025 22:09:46 +0530
From: Abinash Singh <abinashsinghlalotra@...il.com>
To: min.ma@....com,
lizhi.hou@....com
Cc: ogabbay@...nel.org,
dri-devel@...ts.freedesktop.org,
linux-kernel@...r.kernel.org,
Abinash Singh <abinashsinghlalotra@...il.com>
Subject: [RFC PATCH] drivers/accel/amdxdna : refactor resource cleanup in aie2_{ctx, error} to use scope-based helpers
This refactors the `aie2_error_async_events_alloc` function in `aie2_ctx.c`
And `aie2_hwctx_init` function in `aie2_error.c` to replace traditional
goto-based error handling with scope-based cleanup helpers.
No functional changes intended.
Signed-off-by: Abinash Singh <abinashsinghlalotra@...il.com>
---
Hi ,
This patch will improve code quaility and will support
using cleanup helpers in future.
Reference : https://docs.kernel.org/core-api/cleanup.html
......
The “goto error” pattern is notorious for introducing subtle resource leaks.
It is tedious and error prone to add new resource acquisition constraints into
code paths that already have several unwind conditions. The “cleanup” helpers
enable the compiler to help with this tedium and can aid in maintaining
LIFO (last in first out) unwind ordering to avoid unintentional leaks.
...
I will look into other places where we can use this auto cleanup feature.
If you have any suggestion/feedback I will be happy hearing that.
Thank You!
Have a great day..!!
---
drivers/accel/amdxdna/aie2_ctx.c | 9 +++------
drivers/accel/amdxdna/aie2_error.c | 9 +++------
2 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/drivers/accel/amdxdna/aie2_ctx.c b/drivers/accel/amdxdna/aie2_ctx.c
index e04549f64d69..b860859c643d 100644
--- a/drivers/accel/amdxdna/aie2_ctx.c
+++ b/drivers/accel/amdxdna/aie2_ctx.c
@@ -13,7 +13,7 @@
#include <linux/types.h>
#include <linux/xarray.h>
#include <trace/events/amdxdna.h>
-
+#include <linux/cleanup.h>
#include "aie2_msg_priv.h"
#include "aie2_pci.h"
#include "aie2_solver.h"
@@ -528,7 +528,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)
.dev = xdna->ddev.dev,
};
struct drm_gpu_scheduler *sched;
- struct amdxdna_hwctx_priv *priv;
+ struct amdxdna_hwctx_priv *priv __free(kfree) = NULL;
struct amdxdna_gem_obj *heap;
struct amdxdna_dev_hdl *ndev;
int i, ret;
@@ -543,8 +543,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)
if (!heap) {
XDNA_ERR(xdna, "The client dev heap object not exist");
mutex_unlock(&client->mm_lock);
- ret = -ENOENT;
- goto free_priv;
+ return -ENOENT;
}
drm_gem_object_get(to_gobj(heap));
mutex_unlock(&client->mm_lock);
@@ -648,8 +647,6 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)
amdxdna_gem_unpin(heap);
put_heap:
drm_gem_object_put(to_gobj(heap));
-free_priv:
- kfree(priv);
return ret;
}
diff --git a/drivers/accel/amdxdna/aie2_error.c b/drivers/accel/amdxdna/aie2_error.c
index 5ee905632a39..bea36e7fe14f 100644
--- a/drivers/accel/amdxdna/aie2_error.c
+++ b/drivers/accel/amdxdna/aie2_error.c
@@ -10,7 +10,7 @@
#include <linux/dma-mapping.h>
#include <linux/kthread.h>
#include <linux/kernel.h>
-
+#include <linux/cleanup.h>
#include "aie2_msg_priv.h"
#include "aie2_pci.h"
#include "amdxdna_mailbox.h"
@@ -308,7 +308,7 @@ int aie2_error_async_events_alloc(struct amdxdna_dev_hdl *ndev)
struct amdxdna_dev *xdna = ndev->xdna;
u32 total_col = ndev->total_col;
u32 total_size = ASYNC_BUF_SIZE * total_col;
- struct async_events *events;
+ struct async_events *events __free(kfree) = NULL;
int i, ret;
events = kzalloc(struct_size(events, event, total_col), GFP_KERNEL);
@@ -318,8 +318,7 @@ int aie2_error_async_events_alloc(struct amdxdna_dev_hdl *ndev)
events->buf = dma_alloc_noncoherent(xdna->ddev.dev, total_size, &events->addr,
DMA_FROM_DEVICE, GFP_KERNEL);
if (!events->buf) {
- ret = -ENOMEM;
- goto free_events;
+ return -ENOMEM;
}
events->size = total_size;
events->event_cnt = total_col;
@@ -352,7 +351,5 @@ int aie2_error_async_events_alloc(struct amdxdna_dev_hdl *ndev)
free_buf:
dma_free_noncoherent(xdna->ddev.dev, events->size, events->buf,
events->addr, DMA_FROM_DEVICE);
-free_events:
- kfree(events);
return ret;
}
--
2.50.1
Powered by blists - more mailing lists