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 <tvrtko.ursulin@igalia.com>
This commit is contained in:
Tvrtko Ursulin 2026-04-14 09:32:00 +01:00 committed by Alexander Mikhalitsyn
parent 0269883c7e
commit 00dfc9d2a0
No known key found for this signature in database
GPG key ID: B1F47F5CB05B4FA3

View file

@ -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);
}
}