From bf415671892a03136d9bfe9f1c558c859fe97eee Mon Sep 17 00:00:00 2001 From: David Yat Sin Date: Mon, 19 Dec 2022 00:09:08 +0000 Subject: [PATCH] Add retain handle and get allocation properties Support function to retain allocation handle for memory mappings. The get allocation properties function will return the current allocation properties for existing memory mappings. This is part of patch series for Virtual Memory API. Change-Id: I0a53a11b6efc2b5bf9d463512a489a2abd812551 [ROCm/ROCR-Runtime commit: 687eb043d4941caeb55ef77e0c50781a1f5497f6] --- .../core/common/hsa_table_interface.cpp | 11 ++++++ .../hsa-runtime/core/inc/hsa_ext_amd_impl.h | 9 +++++ .../runtime/hsa-runtime/core/inc/runtime.h | 6 ++++ .../core/runtime/hsa_api_trace.cpp | 3 ++ .../hsa-runtime/core/runtime/hsa_ext_amd.cpp | 30 ++++++++++++++++ .../hsa-runtime/core/runtime/runtime.cpp | 26 ++++++++++++++ .../runtime/hsa-runtime/hsacore.so.def | 2 ++ .../runtime/hsa-runtime/inc/hsa_api_trace.h | 3 ++ .../runtime/hsa-runtime/inc/hsa_ext_amd.h | 34 +++++++++++++++++++ 9 files changed, 124 insertions(+) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp index d812e6fbb7..c3ecc94532 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp @@ -1281,6 +1281,17 @@ hsa_status_t HSA_API hsa_amd_vmem_import_shareable_handle(int dmabuf_fd, return amdExtTable->hsa_amd_vmem_import_shareable_handle_fn(dmabuf_fd, handle); } +hsa_status_t HSA_API hsa_amd_vmem_retain_alloc_handle(hsa_amd_vmem_alloc_handle_t* handle, + void* addr) { + return amdExtTable->hsa_amd_vmem_retain_alloc_handle_fn(handle, addr); +} + +hsa_status_t HSA_API hsa_amd_vmem_get_alloc_properties_from_handle( + hsa_amd_vmem_alloc_handle_t alloc_handle, hsa_amd_memory_pool_t* pool, + hsa_amd_memory_type_t* type) { + return amdExtTable->hsa_amd_vmem_get_alloc_properties_from_handle_fn(alloc_handle, pool, type); +} + // Tools only table interfaces. namespace rocr { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h index 328cc66684..5461c998d6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h @@ -337,6 +337,15 @@ hsa_status_t hsa_amd_vmem_export_shareable_handle(int* dmabuf_fd, // Mirrors Amd Extension Apis hsa_status_t hsa_amd_vmem_import_shareable_handle(int dmabuf_fd, hsa_amd_vmem_alloc_handle_t* handle); + +// Mirrors Amd Extension Apis +hsa_status_t hsa_amd_vmem_retain_alloc_handle(hsa_amd_vmem_alloc_handle_t* allocHandle, void* addr); + +// Mirrors Amd Extension Apis +hsa_status_t hsa_amd_vmem_get_alloc_properties_from_handle(hsa_amd_vmem_alloc_handle_t allocHandle, + hsa_amd_memory_pool_t* pool, + hsa_amd_memory_type_t* type); + } // namespace amd } // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h index 5193281e70..15f45371cc 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h @@ -382,6 +382,12 @@ class Runtime { hsa_status_t VMemoryImportShareableHandle(const int dmabuf_fd, hsa_amd_vmem_alloc_handle_t* handle); + hsa_status_t VMemoryRetainAllocHandle(hsa_amd_vmem_alloc_handle_t* memoryHandle, void* addr); + + hsa_status_t VMemoryGetAllocPropertiesFromHandle(const hsa_amd_vmem_alloc_handle_t memoryHandle, + const core::MemoryRegion** mem_region, + hsa_amd_memory_type_t* type); + const std::vector& cpu_agents() { return cpu_agents_; } const std::vector& gpu_agents() { return gpu_agents_; } diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp index 91a68913df..2ee8ffe90d 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_api_trace.cpp @@ -412,6 +412,9 @@ void HsaApiTable::UpdateAmdExts() { amd_ext_api.hsa_amd_vmem_get_access_fn = AMD::hsa_amd_vmem_get_access; amd_ext_api.hsa_amd_vmem_export_shareable_handle_fn = AMD::hsa_amd_vmem_export_shareable_handle; amd_ext_api.hsa_amd_vmem_import_shareable_handle_fn = AMD::hsa_amd_vmem_import_shareable_handle; + amd_ext_api.hsa_amd_vmem_retain_alloc_handle_fn = AMD::hsa_amd_vmem_retain_alloc_handle; + amd_ext_api.hsa_amd_vmem_get_alloc_properties_from_handle_fn = + AMD::hsa_amd_vmem_get_alloc_properties_from_handle; } void LoadInitialHsaApiTable() { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp index cb00d2c031..df47fcc262 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp @@ -1326,5 +1326,35 @@ hsa_status_t hsa_amd_vmem_import_shareable_handle(int dmabuf_fd, CATCH; } +hsa_status_t hsa_amd_vmem_retain_alloc_handle(hsa_amd_vmem_alloc_handle_t* allocHandle, + void* addr) { + TRY; + IS_OPEN(); + IS_BAD_PTR(addr); + + return core::Runtime::runtime_singleton_->VMemoryRetainAllocHandle(allocHandle, addr); + CATCH; +} + +hsa_status_t hsa_amd_vmem_get_alloc_properties_from_handle(hsa_amd_vmem_alloc_handle_t allocHandle, + hsa_amd_memory_pool_t* pool, + hsa_amd_memory_type_t* type) { + TRY; + IS_OPEN(); + IS_BAD_PTR(pool); + IS_BAD_PTR(type); + + const core::MemoryRegion* mem_region = NULL; + hsa_status_t ret = core::Runtime::runtime_singleton_->VMemoryGetAllocPropertiesFromHandle( + allocHandle, &mem_region, type); + if (ret == HSA_STATUS_SUCCESS) { + hsa_region_t region = core::MemoryRegion::Convert(mem_region); + pool->handle = region.handle; + } + + return ret; + CATCH; +} + } // namespace amd } // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 8d3abd0d72..07c94a104f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -2869,5 +2869,31 @@ hsa_status_t Runtime::VMemoryImportShareableHandle(int dmabuf_fd, return HSA_STATUS_SUCCESS; } +hsa_status_t Runtime::VMemoryRetainAllocHandle(hsa_amd_vmem_alloc_handle_t* mapped_handle, + void* va) { + auto mappedHandleIt = mapped_handle_map_.find(va); + if (mappedHandleIt == mapped_handle_map_.end()) return HSA_STATUS_ERROR_INVALID_ALLOCATION; + + MemoryHandle* memoryHandle = mappedHandleIt->second.mem_handle; + memoryHandle->ref_count++; + *mapped_handle = MemoryHandle::Convert(memoryHandle->thunk_handle); + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t Runtime::VMemoryGetAllocPropertiesFromHandle(hsa_amd_vmem_alloc_handle_t allocHandle, + const core::MemoryRegion** mem_region, + hsa_amd_memory_type_t* type) { + auto memoryHandleIt = memory_handle_map_.find(reinterpret_cast(allocHandle.handle)); + if (memoryHandleIt == memory_handle_map_.end()) return HSA_STATUS_ERROR_INVALID_ALLOCATION; + + *mem_region = memoryHandleIt->second.region; + *type = (memoryHandleIt->second.alloc_flag & core::MemoryRegion::AllocatePinned) + ? MEMORY_TYPE_PINNED + : MEMORY_TYPE_NONE; + + return HSA_STATUS_SUCCESS; +} + } // namespace core } // namespace rocr diff --git a/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def b/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def index 74f721f566..23473fc40f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def +++ b/projects/rocr-runtime/runtime/hsa-runtime/hsacore.so.def @@ -243,6 +243,8 @@ global: hsa_amd_vmem_get_access; hsa_amd_vmem_export_shareable_handle; hsa_amd_vmem_import_shareable_handle; + hsa_amd_vmem_retain_alloc_handle; + hsa_amd_vmem_get_alloc_properties_from_handle; local: *; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h index 048c8bf52d..6efaf87726 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h @@ -204,6 +204,9 @@ struct AmdExtTable { decltype(hsa_amd_vmem_get_access)* hsa_amd_vmem_get_access_fn; decltype(hsa_amd_vmem_export_shareable_handle)* hsa_amd_vmem_export_shareable_handle_fn; decltype(hsa_amd_vmem_import_shareable_handle)* hsa_amd_vmem_import_shareable_handle_fn; + decltype(hsa_amd_vmem_retain_alloc_handle)* hsa_amd_vmem_retain_alloc_handle_fn; + decltype(hsa_amd_vmem_get_alloc_properties_from_handle)* + hsa_amd_vmem_get_alloc_properties_from_handle_fn; }; // Table to export HSA Core Runtime Apis diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h index 7da9bf20f6..e493ac7ff8 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h @@ -2941,6 +2941,40 @@ hsa_status_t hsa_amd_vmem_export_shareable_handle(int* dmabuf_fd, */ hsa_status_t hsa_amd_vmem_import_shareable_handle(int dmabuf_fd, hsa_amd_vmem_alloc_handle_t* handle); + +/* + * @brief Returns memory handle for mapped memory + * + * Return a memory handle for previously mapped memory. The handle will be the same value of handle + * used to map the memory. The returned handle must be released with corresponding number of calls + * to hsa_amd_vmem_handle_release. + * + * @param[out] memory_handle memory handle for this mapped address + * @param[in] mapped address + * + * @retval ::HSA_STATUS_SUCCESS + * + * @retval ::HSA_STATUS_ERROR_INVALID_ALLOCATION Invalid address + */ +hsa_status_t hsa_amd_vmem_retain_alloc_handle(hsa_amd_vmem_alloc_handle_t* memory_handle, + void* addr); + +/* +* @brief Returns the current allocation properties of a handle +* +* Returns the allocation properties of an existing handle +* +* @param[in] memory_handle memory handle to be queried +* @param[out] pool memory pool that owns this handle +* @param[out] memory type + +* @retval ::HSA_STATUS_SUCCESS +* +* @retval ::HSA_STATUS_ERROR_INVALID_ALLOCATION Invalid memory_handle +*/ +hsa_status_t hsa_amd_vmem_get_alloc_properties_from_handle( + hsa_amd_vmem_alloc_handle_t memory_handle, hsa_amd_memory_pool_t* pool, + hsa_amd_memory_type_t* type); #ifdef __cplusplus } // end extern "C" block #endif