From e7a7feb273de12668ab0149f02926bccd20cf4c8 Mon Sep 17 00:00:00 2001 From: kjayapra-amd Date: Wed, 29 May 2024 19:09:32 -0400 Subject: [PATCH] SWDEV-464828 - Initial implementation of VMM IPC on PAL/Windows. Change-Id: I3d5e148fad9105704db6724b00df06bef4fc9d2f --- hipamd/src/hip_vm.cpp | 11 ++--------- rocclr/device/device.hpp | 13 +++++++------ rocclr/device/pal/paldevice.cpp | 29 +++++++++++++++++++++++++++++ rocclr/device/pal/paldevice.hpp | 13 +++++++++++++ rocclr/device/rocm/rocdevice.cpp | 28 +++++++++++++++++++++++----- rocclr/device/rocm/rocdevice.hpp | 6 ++++-- rocclr/device/rocm/rocmemory.cpp | 2 +- 7 files changed, 79 insertions(+), 23 deletions(-) diff --git a/hipamd/src/hip_vm.cpp b/hipamd/src/hip_vm.cpp index 5dae1d2721..295d1520a2 100644 --- a/hipamd/src/hip_vm.cpp +++ b/hipamd/src/hip_vm.cpp @@ -156,7 +156,7 @@ hipError_t hipMemExportToShareableHandle(void* shareableHandle, } if (!ga->asAmdMemory().getContext().devices()[0]->ExportShareableVMMHandle( - ga->asAmdMemory().getUserData().hsa_handle, flags, shareableHandle)) { + ga->asAmdMemory(), flags, shareableHandle)) { LogPrintfError("Exporting Handle failed with flags: %d", flags); HIP_RETURN(hipErrorInvalidValue); } @@ -224,20 +224,13 @@ hipError_t hipMemImportFromShareableHandle(hipMemGenericAllocationHandle_t* hand } amd::Device* device = hip::getCurrentDevice()->devices()[0]; - amd::Memory* phys_mem_obj = new (device->context()) amd::Buffer(device->context(), - ROCCLR_MEM_PHYMEM | ROCCLR_MEM_INTERPROCESS, 0, osHandle); + amd::Memory* phys_mem_obj = device->ImportShareableVMMHandle(osHandle); if (phys_mem_obj == nullptr) { LogError("failed to new a va range curr_mem_obj object!"); HIP_RETURN(hipErrorInvalidValue); } - if (!phys_mem_obj->create(nullptr, false)) { - LogError("failed to create a va range mem object"); - phys_mem_obj->release(); - HIP_RETURN(hipErrorInvalidValue); - } - hipMemAllocationProp prop {}; prop.type = hipMemAllocationTypePinned; prop.location.type = hipMemLocationTypeDevice; diff --git a/rocclr/device/device.hpp b/rocclr/device/device.hpp index 7e7de98dae..a86e4c05e0 100644 --- a/rocclr/device/device.hpp +++ b/rocclr/device/device.hpp @@ -1877,11 +1877,12 @@ class Device : public RuntimeObject { /** * Export Shareable VMM Handle to FD * - * @param hsa_handle hsa_handle which has the phys_mem info. + * @param amd_mem_obj amd::Memory obj which holds the hsa_handle. * @param flags any flags to be passed * @param shareableHandle exported handle, points to fdesc. */ - virtual bool ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle) { + virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, + void* shareableHandle) { ShouldNotCallThis(); return false; } @@ -1889,12 +1890,12 @@ class Device : public RuntimeObject { /** * Import FD from Shareable VMM Handle * - * @param osHandle os handle/fdesc - * @param hsa_handle_ptr hsa_handle which has the phys_mem info. + * @param osHandle os handle/fdesc/void* + * @param amd_mem_obj amd_mem_obj with hsa_handle/memory_obj. */ - virtual bool ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const { + virtual amd::Memory* ImportShareableVMMHandle(void* osHandle) { ShouldNotCallThis(); - return false; + return nullptr; } /** diff --git a/rocclr/device/pal/paldevice.cpp b/rocclr/device/pal/paldevice.cpp index aa9ef1dfce..fc5916d389 100644 --- a/rocclr/device/pal/paldevice.cpp +++ b/rocclr/device/pal/paldevice.cpp @@ -2780,4 +2780,33 @@ void Device::DestroyExtSemaphore(void* extSemaphore) { amd::Os::alignedFree(extSemaphore); } +// ================================================================================================ +bool Device::ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle) { + device::Memory* dev_mem = static_cast(amd_mem_obj.getDeviceMemory(*this)); + return dev_mem->ExportHandle(shareableHandle); +} + +// ================================================================================================ +amd::Memory* Device::ImportShareableVMMHandle(void* osHandle) { + + int flags = 0; + size_t mem_offset = 0; + size_t mem_size = 0; + + amd::Memory* amd_mem_obj = new (context()) amd::IpcBuffer(context(), flags, mem_offset, + mem_size, osHandle); + + if (amd_mem_obj == nullptr) { + LogError("failed to create a mem object!"); + return nullptr; + } + + if (!amd_mem_obj->create(nullptr)) { + LogError("failed to create a svm hidden buffer!"); + amd_mem_obj->release(); + return nullptr; + } + return amd_mem_obj; +} + } // namespace amd::pal diff --git a/rocclr/device/pal/paldevice.hpp b/rocclr/device/pal/paldevice.hpp index 0e23f14e3a..0c207fab79 100644 --- a/rocclr/device/pal/paldevice.hpp +++ b/rocclr/device/pal/paldevice.hpp @@ -155,6 +155,15 @@ class NullDevice : public amd::Device { return true; } + virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, + void* shareableHandle) { + return false; + } + + virtual amd::Memory* ImportShareableVMMHandle(void* osHandle) { + return nullptr; + } + virtual bool importExtSemaphore(void** extSemaphore,const amd::Os::FileDesc& handle, amd::ExternalSemaphoreHandleType sem_handle_type) override { return false; @@ -557,6 +566,10 @@ class Device : public NullDevice { return true; } + virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle); + + virtual amd::Memory* ImportShareableVMMHandle(void* osHandle); + //! Returns SRD manger object SrdManager& srds() const { return *srdManager_; } diff --git a/rocclr/device/rocm/rocdevice.cpp b/rocclr/device/rocm/rocdevice.cpp index 2ca56d354d..bfd46a2491 100644 --- a/rocclr/device/rocm/rocdevice.cpp +++ b/rocclr/device/rocm/rocdevice.cpp @@ -2534,17 +2534,17 @@ bool Device::GetMemAccess(void* va_addr, VmmAccess* access_flags_ptr) { } // ================================================================================================ -bool Device::ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle) { +bool Device::ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle) { hsa_status_t hsa_status = HSA_STATUS_SUCCESS; hsa_amd_vmem_alloc_handle_t hsa_vmem_handle {}; + hsa_vmem_handle.handle = amd_mem_obj.getUserData().hsa_handle; + int dmabuf_fd = 0; - if (hsa_handle == 0) { + if (hsa_vmem_handle.handle == 0) { LogError("HSA Handle is not valid"); return false; } - int dmabuf_fd = 0; - hsa_vmem_handle.handle = hsa_handle; if ((hsa_status = hsa_amd_vmem_export_shareable_handle(&dmabuf_fd, hsa_vmem_handle, flags)) != HSA_STATUS_SUCCESS) { LogPrintfError("Failed hsa_vmem_export_shareable_handle with status: %d \n", hsa_status); @@ -2557,7 +2557,7 @@ bool Device::ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shar } // ================================================================================================ -bool Device::ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const { +bool Device::ImportShareableHSAHandle(void* osHandle, uint64_t* hsa_handle_ptr) const { hsa_status_t hsa_status = HSA_STATUS_SUCCESS; hsa_amd_vmem_alloc_handle_t hsa_vmem_handle {}; @@ -2577,6 +2577,24 @@ bool Device::ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) return true; } +// ================================================================================================ +amd::Memory* Device::ImportShareableVMMHandle(void* osHandle) { + amd::Memory* amd_mem_obj = new (context()) amd::Buffer(context(), + ROCCLR_MEM_PHYMEM | ROCCLR_MEM_INTERPROCESS, 0, osHandle); + if (amd_mem_obj == nullptr) { + LogError("Cannot create memory object"); + return nullptr; + } + + if (!amd_mem_obj->create(nullptr, false)) { + LogError("Failed to create mem_obj from imported fd"); + amd_mem_obj->release(); + return nullptr; + } + + return amd_mem_obj; +} + // ================================================================================================ bool Device::SetSvmAttributesInt(const void* dev_ptr, size_t count, amd::MemoryAdvice advice, bool first_alloc, bool use_cpu) const { diff --git a/rocclr/device/rocm/rocdevice.hpp b/rocclr/device/rocm/rocdevice.hpp index 6d1b03c30d..2f91df4a16 100644 --- a/rocclr/device/rocm/rocdevice.hpp +++ b/rocclr/device/rocm/rocdevice.hpp @@ -480,9 +480,11 @@ class Device : public NullDevice { virtual bool SetMemAccess(void* va_addr, size_t va_size, VmmAccess access_flags); virtual bool GetMemAccess(void* va_addr, VmmAccess* access_flags_ptr); - virtual bool ExportShareableVMMHandle(uint64_t hsa_handle, int flags, void* shareableHandle); + virtual bool ExportShareableVMMHandle(amd::Memory& amd_mem_obj, int flags, void* shareableHandle); - virtual bool ImportShareableVMMHandle(void* osHandle, uint64_t* hsa_handle_ptr) const; + bool ImportShareableHSAHandle(void* osHandle, uint64_t* hsa_handle_ptr) const; + + virtual amd::Memory* ImportShareableVMMHandle(void* osHandle); virtual bool SetClockMode(const cl_set_device_clock_mode_input_amd setClockModeInput, cl_set_device_clock_mode_output_amd* pSetClockModeOutput); diff --git a/rocclr/device/rocm/rocmemory.cpp b/rocclr/device/rocm/rocmemory.cpp index 963e1afc77..d2229e025c 100644 --- a/rocclr/device/rocm/rocmemory.cpp +++ b/rocclr/device/rocm/rocmemory.cpp @@ -772,7 +772,7 @@ bool Buffer::create(bool alloc_local) { if (memFlags & ROCCLR_MEM_INTERPROCESS) { int dmabuf_fd = *(reinterpret_cast(owner()->getSvmPtr())); // if interprocess flag is set, then the memory is importable. - if (!dev().ImportShareableVMMHandle(owner()->getSvmPtr(), + if (!dev().ImportShareableHSAHandle(owner()->getSvmPtr(), &owner()->getUserData().hsa_handle)) { LogPrintfError("Importing Shareable Memory failed with os_handle: 0x%x \n", owner()->getSvmPtr());