From 96545e914b7996d9dd059382655456afcd63bf46 Mon Sep 17 00:00:00 2001 From: Lancelot Six Date: Fri, 16 Aug 2024 15:23:55 +0000 Subject: [PATCH] coredump: Improve error handling when reading VRAM It is possible for the runtime to receive an interrupt while trying to access VRAM data using /proc/self/mem. In such case, pread(2) would return -1 and set errno to -EINTR. This is not an error case, the pread(2) call just need to be restarted, however current implementation would tread it as an error. This patch changes the the implementation to correctly retry on EINTR. While at it, this patch also handles cases where pread(2) reads less data than originally requested. Change-Id: I6a72fc5eda4afd90319f0d24b35c9eac6d1ff41c Signed-off-by: David Yat Sin [ROCm/ROCR-Runtime commit: 3e0d3d6d61f8b07320905527621e2c487db49de9] --- .../libamdhsacode/amd_core_dump.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_core_dump.cpp b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_core_dump.cpp index 5cc878df8c..d284223332 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_core_dump.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_core_dump.cpp @@ -249,10 +249,25 @@ struct LoadSegmentBuilder : public SegmentBuilder { hsa_status_t Read(void* buf, size_t buf_size, off_t offset) override { if (fd_ == -1) return HSA_STATUS_ERROR; - if (pread(fd_, buf, buf_size, offset) == -1) { - perror("Failed to read GPU memory"); + + size_t done = 0; + ssize_t read; + do { + read = pread(fd_, buf + done, buf_size - done, offset + done); + + if (read == -1 && errno != EINTR) { + perror("Failed to read GPU memory"); + return HSA_STATUS_ERROR; + } + else if (read > 0) + done += read; + } while (read != 0 && done < buf_size); + + if (read == 0 && done < buf_size) { + fprintf(stderr, "Reached unexpected EOF while reading VRAM.\n"); return HSA_STATUS_ERROR; } + return HSA_STATUS_SUCCESS; }