Skip to content

Commit 922f0e0

Browse files
srishanmalexdeucher
authored andcommitted
drm/amdkfd: Use dynamic allocation for CU occupancy array in 'kfd_get_cu_occupancy()'
The `kfd_get_cu_occupancy` function previously declared a large `cu_occupancy` array as a local variable, which could lead to stack overflows due to excessive stack usage. This commit replaces the static array allocation with dynamic memory allocation using `kcalloc`, thereby reducing the stack size. This change avoids the risk of stack overflows in kernel space, in scenarios where `AMDGPU_MAX_QUEUES` is large. The allocated memory is freed using `kfree` before the function returns to prevent memory leaks. Fixes the below with gcc W=1: drivers/gpu/drm/amd/amdgpu/../amdkfd/kfd_process.c: In function ‘kfd_get_cu_occupancy’: drivers/gpu/drm/amd/amdgpu/../amdkfd/kfd_process.c:322:1: warning: the frame size of 1056 bytes is larger than 1024 bytes [-Wframe-larger-than=] 322 | } | ^ Fixes: 6ae9e1a ("drm/amdkfd: Update logic for CU occupancy calculations") Cc: Harish Kasiviswanathan <[email protected]> Cc: Felix Kuehling <[email protected]> Cc: Christian König <[email protected]> Cc: Alex Deucher <[email protected]> Signed-off-by: Srinivasan Shanmugam <[email protected]> Suggested-by: Mukul Joshi <[email protected]> Reviewed-by: Mukul Joshi <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent 136ce12 commit 922f0e0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

drivers/gpu/drm/amd/amdkfd/kfd_process.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,9 @@ static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
271271
struct kfd_process *proc = NULL;
272272
struct kfd_process_device *pdd = NULL;
273273
int i;
274-
struct kfd_cu_occupancy cu_occupancy[AMDGPU_MAX_QUEUES];
274+
struct kfd_cu_occupancy *cu_occupancy;
275275
u32 queue_format;
276276

277-
memset(cu_occupancy, 0x0, sizeof(cu_occupancy));
278-
279277
pdd = container_of(attr, struct kfd_process_device, attr_cu_occupancy);
280278
dev = pdd->dev;
281279
if (dev->kfd2kgd->get_cu_occupancy == NULL)
@@ -293,6 +291,10 @@ static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
293291
wave_cnt = 0;
294292
max_waves_per_cu = 0;
295293

294+
cu_occupancy = kcalloc(AMDGPU_MAX_QUEUES, sizeof(*cu_occupancy), GFP_KERNEL);
295+
if (!cu_occupancy)
296+
return -ENOMEM;
297+
296298
/*
297299
* For GFX 9.4.3, fetch the CU occupancy from the first XCC in the partition.
298300
* For AQL queues, because of cooperative dispatch we multiply the wave count
@@ -318,6 +320,7 @@ static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
318320

319321
/* Translate wave count to number of compute units */
320322
cu_cnt = (wave_cnt + (max_waves_per_cu - 1)) / max_waves_per_cu;
323+
kfree(cu_occupancy);
321324
return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
322325
}
323326

0 commit comments

Comments
 (0)