From faa7ee48bafb43caac6e578be83f21e006c4d448 Mon Sep 17 00:00:00 2001 From: Christophe Paquot Date: Mon, 6 Jun 2022 11:08:52 -0700 Subject: [PATCH] SWDEV-322620 - Virtual Memory Management Implements hipMemRetainAllocationHandle HIP layer updates the svmPtr of the amd::memory ROCCLR just manages the MemObjMap This is because HIP knows the orignal (generic) address of the physical memory whereas ROCCLR doesn't keep track of that Change-Id: Ied26d7f814bc468f5cf586c967020737b9aa9e9f --- hipamd/src/hip_vm.cpp | 20 ++++++++++++++++++-- hipamd/src/hip_vm.hpp | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/hipamd/src/hip_vm.cpp b/hipamd/src/hip_vm.cpp index 31eb381a76..2c81a42f1b 100644 --- a/hipamd/src/hip_vm.cpp +++ b/hipamd/src/hip_vm.cpp @@ -202,8 +202,8 @@ hipError_t hipMemMap(void* ptr, size_t size, size_t offset, hipMemGenericAllocat cmd->awaitCompletion(); cmd->release(); - amd::Memory* va = amd::MemObjMap::FindMemObj(ptr); - va->getUserData().deviceId = ga->GetProperties().location.id; + // update the internal svm address to ptr + ga->asAmdMemory().setSvmPtr(ptr); HIP_RETURN(hipSuccess); } @@ -235,6 +235,18 @@ hipError_t hipMemRetainAllocationHandle(hipMemGenericAllocationHandle_t* handle, if (handle == nullptr || addr == nullptr) HIP_RETURN(hipErrorInvalidValue); + amd::Memory* mem = amd::MemObjMap::FindMemObj(addr); + + if (mem == nullptr) { + HIP_RETURN(hipErrorInvalidValue); + } + + *handle = reinterpret_cast(mem->getUserData().data); + + if (*handle == nullptr) { + HIP_RETURN(hipErrorInvalidValue); + } + HIP_RETURN(hipSuccess); } @@ -265,6 +277,10 @@ hipError_t hipMemUnmap(void* ptr, size_t size) { cmd->awaitCompletion(); cmd->release(); + // restore the original va of the generic allocation + hip::GenericAllocation* ga = reinterpret_cast(va->getUserData().data); + va->setSvmPtr(ga->genericAddress()); + HIP_RETURN(hipSuccess); } diff --git a/hipamd/src/hip_vm.hpp b/hipamd/src/hip_vm.hpp index a6b8f7ed7d..d87768ac46 100644 --- a/hipamd/src/hip_vm.hpp +++ b/hipamd/src/hip_vm.hpp @@ -38,7 +38,8 @@ public: const hipMemAllocationProp& GetProperties() const { return properties_; } hipMemGenericAllocationHandle_t asMemGenericAllocationHandle() { return reinterpret_cast(this); } - amd::Memory& asAmdMemory() { return *amd::MemObjMap::FindMemObj(ptr_); } + amd::Memory& asAmdMemory() { return *amd::MemObjMap::FindMemObj(genericAddress()); } + void* genericAddress() const { return ptr_; } }; };