From 00dfc9d2a03bcee927fa85f1ebca6741cd8a6c56 Mon Sep 17 00:00:00 2001 From: Tvrtko Ursulin Date: Tue, 14 Apr 2026 09:32:00 +0100 Subject: [PATCH] plugins/amdgpu: Fix shared memory allocation and teardown Allocate shared memory for the whole struct shared_handle_ids and not just for the pointer to it, and by doing so stop relying on mmap rounding up to page size. While at it add error checking that the allocations actually succeeded. Signed-off-by: Tvrtko Ursulin --- plugins/amdgpu/amdgpu_plugin.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index e5cbff42e..60893516d 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -1198,20 +1198,34 @@ int amdgpu_restore_init(void) } if (num_handles > 0) { - shared_memory = mmap(NULL, sizeof(shared_memory), protection, visibility, -1, 0); + shared_memory = mmap(NULL, sizeof(*shared_memory), protection, visibility, -1, 0); + if (shared_memory == MAP_FAILED) { + pr_perror("Failed to allocate shared memory!"); + shared_memory = NULL; + return -1; + } shared_memory->num_handles = num_handles; shared_memory->handles = mmap(NULL, sizeof(struct handle_id) * num_handles, protection, visibility, -1, 0); - - for (int i = 0; i < num_handles; i++) { - shared_memory->handles[i].handle = -1; - shared_memory->handles[i].fdstore_id = -1; + if (shared_memory->handles == MAP_FAILED) { + pr_perror("Failed to allocate shared handles memory!"); + munmap(shared_memory, sizeof(*shared_memory)); + shared_memory = NULL; + return -1; } shared_memory_mutex = shmalloc(sizeof(*shared_memory_mutex)); if (!shared_memory_mutex) { pr_err("Can't create amdgpu mutex\n"); + munmap(shared_memory->handles, sizeof(struct handle_id) * num_handles); + munmap(shared_memory, sizeof(*shared_memory)); + shared_memory = NULL; return -1; } + + for (int i = 0; i < num_handles; i++) { + shared_memory->handles[i].handle = -1; + shared_memory->handles[i].fdstore_id = -1; + } mutex_init(shared_memory_mutex); } }