From af32d407a23fd4c3fcbc4e59a347b7aa5a441474 Mon Sep 17 00:00:00 2001 From: Radostin Stoyanov Date: Sat, 14 Mar 2026 14:01:10 +0000 Subject: [PATCH] plugin/amdgpu: check posix_memalign() return value posix_memalign() returns an error code on failure and does not guarantee setting the pointer to NULL. Check the return value instead of the pointer. Since posix_memalign() does not set errno, assign it explicitly so pr_perror() prints the correct message. Negate the return code instead of hardcoding -ENOMEM to preserve the actual error (e.g. EINVAL for bad alignment). In amdgpu_plugin_drm_dump_file(), replace break with cleanup and goto exit so per-BO resources are freed on failure. Signed-off-by: Radostin Stoyanov --- plugins/amdgpu/amdgpu_plugin.c | 21 ++++++++++++--------- plugins/amdgpu/amdgpu_plugin_drm.c | 14 ++++++++++---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index 183953674..e3ba0de64 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -841,10 +841,11 @@ void *dump_bo_contents(void *_thread_data) buffer_size = kfd_max_buffer_size > 0 ? min(kfd_max_buffer_size, max_bo_size) : max_bo_size; - posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); - if (!buffer) { + ret = posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); + if (ret) { + errno = ret; pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE."); - ret = -ENOMEM; + ret = -ret; goto exit; } @@ -949,10 +950,11 @@ void *restore_bo_contents(void *_thread_data) buffer_size = kfd_max_buffer_size > 0 ? min(kfd_max_buffer_size, max_bo_size) : max_bo_size; - posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); - if (!buffer) { + ret = posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); + if (ret) { + errno = ret; pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE."); - ret = -ENOMEM; + ret = -ret; goto exit; } @@ -2287,10 +2289,11 @@ void *parallel_restore_bo_contents(void *_thread_data) } offset = ftell(bo_contents_fp); - posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); - if (!buffer) { + ret = posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); + if (ret) { + errno = ret; pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE."); - ret = -ENOMEM; + ret = -ret; goto err_sdma; } diff --git a/plugins/amdgpu/amdgpu_plugin_drm.c b/plugins/amdgpu/amdgpu_plugin_drm.c index cbaa43862..c9aa9e4cd 100644 --- a/plugins/amdgpu/amdgpu_plugin_drm.c +++ b/plugins/amdgpu/amdgpu_plugin_drm.c @@ -193,10 +193,11 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int buffer_size = max_bo_size; - posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); - if (!buffer) { + ret = posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size); + if (ret) { + errno = ret; pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE."); - ret = -ENOMEM; + ret = -ret; goto exit; } @@ -394,9 +395,14 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm) ret = posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size); if (ret) { + errno = ret; pr_perror("Failed to allocate buffer"); + ret = -ret; fclose(bo_contents_fp); - break; + close(dmabuf_fd); + amdgpu_device_deinitialize(h_dev); + xfree(vm_info_entries); + goto exit; } ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fp, buffer, handle_entry.size, h_dev, 0x1000,