diff --git a/projects/clr/rocclr/device/devhostcall.cpp b/projects/clr/rocclr/device/devhostcall.cpp index 51b2d259b0..800e029c1a 100644 --- a/projects/clr/rocclr/device/devhostcall.cpp +++ b/projects/clr/rocclr/device/devhostcall.cpp @@ -94,6 +94,7 @@ static void handlePayload(MessageHandler& messages, uint32_t service, uint64_t* if (payload[0]) { amd::Memory* mem = amd::MemObjMap::FindMemObj(reinterpret_cast(payload[0])); if (mem) { + const_cast(&dev)->RemoveHostcallMemory(mem); amd::MemObjMap::RemoveMemObj(reinterpret_cast(payload[0])); mem->release(); } else { @@ -110,6 +111,7 @@ static void handlePayload(MessageHandler& messages, uint32_t service, uint64_t* device::Memory* dm = buf->getDeviceMemory(dev); va = dm->virtualAddress(); amd::MemObjMap::AddMemObj(reinterpret_cast(va), buf); + const_cast(&dev)->TrackHostcallMemory(buf); } else { buf->release(); } diff --git a/projects/clr/rocclr/device/device.cpp b/projects/clr/rocclr/device/device.cpp index 99334e53f6..41d51bffec 100644 --- a/projects/clr/rocclr/device/device.cpp +++ b/projects/clr/rocclr/device/device.cpp @@ -807,6 +807,16 @@ Device::~Device() { delete vaCacheMap_; } + for (auto memory : hostcall_allocated_memories_) { + if (memory != nullptr) { + amd::MemObjMap::RemoveMemObj( + reinterpret_cast(memory->getDeviceMemory(*this, false)->virtualAddress())); + memory->release(); + } + } + + hostcall_allocated_memories_.clear(); + delete vaCacheAccess_; delete settings_; delete[] info_.extensions_; @@ -1205,6 +1215,20 @@ bool Device::GetHandleForAddressRange(void* dev_ptr, size_t size, void* handle) return dev_mem->GetFDHandleForMem(dev_ptr, size, VmmPtr, handle); } +// ================================================================================================ +void Device::TrackHostcallMemory(amd::Memory* memory) { + hostcall_allocated_memories_.push_back(memory); +} + +// ================================================================================================ +void Device::RemoveHostcallMemory(amd::Memory* memory) { + auto it = + std::find(hostcall_allocated_memories_.begin(), hostcall_allocated_memories_.end(), memory); + if (it != hostcall_allocated_memories_.end()) { + hostcall_allocated_memories_.erase(it); + } +} + } // namespace amd namespace amd::device { diff --git a/projects/clr/rocclr/device/device.hpp b/projects/clr/rocclr/device/device.hpp index 534c3a602f..6ad0dc7c9f 100644 --- a/projects/clr/rocclr/device/device.hpp +++ b/projects/clr/rocclr/device/device.hpp @@ -2226,7 +2226,12 @@ class Device : public RuntimeObject { bool GetHandleForAddressRange(void* dev_ptr, size_t size, void* handle); - protected: + // Registers a memory object allocated via hostcall for later cleanup. + void TrackHostcallMemory(amd::Memory* memory); + + // Removes a memory object from hostcall tracking. + void RemoveHostcallMemory(amd::Memory* memory); + //! Enable the specified extension char* getExtensionString(); @@ -2274,6 +2279,9 @@ class Device : public RuntimeObject { Monitor* vaCacheAccess_; //!< Lock to serialize VA caching access std::map* vaCacheMap_; //!< VA cache map uint32_t index_; //!< Unique device index + + // Tracks all amd::Memory objects allocated via hostcall for this device. + std::vector hostcall_allocated_memories_; }; /*! @}