From a5de07d1b82c8ba61a95f4453c3996a8fe0db416 Mon Sep 17 00:00:00 2001 From: Shweta Khatri Date: Wed, 23 Jul 2025 16:58:53 -0400 Subject: [PATCH 1/5] rocr: Remove ISA check to disable stochastic support for GFX12.0 in ROCR Feature support should be determined by KFD via the query-capabilities IOCTL, not in ROCR. --- runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp b/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp index 6f9fe60e70..1b6ecb8b86 100644 --- a/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp +++ b/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp @@ -2563,10 +2563,6 @@ hsa_status_t GpuAgent::PcSamplingIterateConfig(hsa_ven_amd_pcs_iterate_configura if (ret != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR; for (uint32_t i = 0; i < size; i++) { - if ((isa_->GetMajorVersion() == 12 && (isa_->GetMinorVersion() == 0)) && - sampleInfoList[i].method == HSA_PC_SAMPLING_METHOD_KIND_STOCHASTIC_V1) { - continue; - } hsa_ven_amd_pcs_configuration_t hsaPcSampling; if (ConvertHsaKmtPcSamplingInfoToHsa(&sampleInfoList[i], &hsaPcSampling) == HSA_STATUS_SUCCESS && cb(&hsaPcSampling, cb_data) == HSA_STATUS_INFO_BREAK) @@ -2613,10 +2609,6 @@ hsa_status_t GpuAgent::PcSamplingCreateFromId(HsaPcSamplingTraceId ioctlId, if (sampling_method == HSA_VEN_AMD_PCS_METHOD_HOSTTRAP_V1) { pcs_data = &pcs_hosttrap_data_; } else if (sampling_method == HSA_VEN_AMD_PCS_METHOD_STOCHASTIC_V1) { - if (isa_->GetMajorVersion() == 12 && (isa_->GetMinorVersion() == 0)) { - return HSA_STATUS_ERROR_INVALID_ARGUMENT; - } - pcs_data = &pcs_stochastic_data_; } else { // Unsupported sampling method From 9312468655f5a5d3ce9169c469809c1f0143c1c7 Mon Sep 17 00:00:00 2001 From: Honglei Huang Date: Tue, 29 Jul 2025 10:37:32 +0800 Subject: [PATCH 2/5] rocr: add memory sharing call to Driver interface This change improves the abstraction of memory sharing operations by moving them to the driver layer and adds safety checks for cross-driver operations. - Add ShareMemory and RegisterSharedHandle methods to support memory sharing between processes - Add IsDifferentDriver utility methods to check driver compatibility across agents/nodes - Refactor IPC memory handling to use driver-based memory sharing instead of direct HSAKMT calls - Improve error handling for memory sharing operations across different drivers Signed-off-by: Honglei Huang --- .../core/driver/kfd/amd_kfd_driver.cpp | 24 +++++++++++++++++ runtime/hsa-runtime/core/inc/amd_kfd_driver.h | 3 +++ runtime/hsa-runtime/core/inc/driver.h | 19 ++++++++++++++ runtime/hsa-runtime/core/inc/runtime.h | 16 ++++++++++++ runtime/hsa-runtime/core/runtime/runtime.cpp | 26 ++++++++++++------- 5 files changed, 78 insertions(+), 10 deletions(-) diff --git a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp index 935007b6a3..723925f1bc 100644 --- a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp +++ b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp @@ -692,5 +692,29 @@ hsa_status_t KfdDriver::GetWallclockFrequency(uint32_t node_id, uint64_t* freque return HSA_STATUS_SUCCESS; } +hsa_status_t KfdDriver::ShareMemory(void* mem, size_t size, + HsaSharedMemoryHandle* share_mem) const { + assert(share_mem); + + if (HSAKMT_CALL(hsaKmtShareMemory(mem, size, share_mem)) != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::RegisterSharedHandle(const HsaSharedMemoryHandle* share_mem, void** mem, + uint64_t* size) const { + assert(share_mem); + assert(mem); + assert(size); + + if (HSAKMT_CALL(hsaKmtRegisterSharedHandle(share_mem, mem, size)) != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + + return HSA_STATUS_SUCCESS; +} + } // namespace AMD } // namespace rocr diff --git a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h index ca8ee8a593..da1a6501e4 100644 --- a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h +++ b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h @@ -136,6 +136,9 @@ public: const HsaMemMapFlags* mem_flags, uint32_t num_nodes, const uint32_t* nodes) const override; hsa_status_t MakeMemoryUnresident(const void* mem) const override; + hsa_status_t ShareMemory(void* mem, size_t size, HsaSharedMemoryHandle* share_mem) const override; + hsa_status_t RegisterSharedHandle(const HsaSharedMemoryHandle* share_mem, void** mem, + uint64_t* size) const override; hsa_status_t OpenSMI(uint32_t node_id, int* fd) const override; diff --git a/runtime/hsa-runtime/core/inc/driver.h b/runtime/hsa-runtime/core/inc/driver.h index 3a4082e24f..f97e4b479a 100644 --- a/runtime/hsa-runtime/core/inc/driver.h +++ b/runtime/hsa-runtime/core/inc/driver.h @@ -351,6 +351,25 @@ public: /// @return HSA_STATUS_SUCCESS if the driver successfully makes the memory virtual hsa_status_t MakeMemoryUnresident(const void* mem) const = 0; + /// @brief Shares memory with another process. + /// @param[in] mem Pointer to the memory to be shared. + /// @param[in] size Size of the memory to be shared. + /// @param[out] share_mem Pointer to the shared memory handle. + /// @return HSA_STATUS_SUCCESS if the memory was successfully shared, or an error code. + virtual hsa_status_t ShareMemory(void* mem, size_t size, HsaSharedMemoryHandle* share_mem) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Registers a shared memory handle. + /// @param[in] share_mem Pointer to the shared memory handle. + /// @param[out] mem Pointer to the memory. + /// @param[out] size Size of the memory. + /// @return HSA_STATUS_SUCCESS if the memory was successfully registered, or an error code. + virtual hsa_status_t RegisterSharedHandle(const HsaSharedMemoryHandle* share_mem, void** mem, + uint64_t* size) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + /// Unique identifier for supported kernel-mode drivers. const DriverType kernel_driver_type_; diff --git a/runtime/hsa-runtime/core/inc/runtime.h b/runtime/hsa-runtime/core/inc/runtime.h index 4b7e489d94..b9e45a8350 100644 --- a/runtime/hsa-runtime/core/inc/runtime.h +++ b/runtime/hsa-runtime/core/inc/runtime.h @@ -508,6 +508,22 @@ class Runtime { return **driver; } + /// @brief Check if the drivers of the agents are different. + /// @param [in] agents Array of agents to check. + /// @param [in] num_agents Number of agents in the array. + /// @return True if the drivers of the agents are different, false otherwise. + static bool IsDifferentDriver(Agent* agents, uint32_t num_agents) { + if (num_agents == 0 || agents == nullptr) return true; + + auto first_driver_type = agents[0].driver().kernel_driver_type_; + for (uint32_t i = 1; i < num_agents; ++i) { + if (agents[i].driver().kernel_driver_type_ != first_driver_type) { + return true; + } + } + return false; + } + std::vector>& AgentDrivers() { return agent_drivers_; } static bool IsGPUDriver(DriverType driver_type) { diff --git a/runtime/hsa-runtime/core/runtime/runtime.cpp b/runtime/hsa-runtime/core/runtime/runtime.cpp index 14b57254bb..b2c8be1f4f 100644 --- a/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -1216,6 +1216,7 @@ hsa_status_t Runtime::IPCCreate(void* ptr, size_t len, hsa_amd_ipc_memory_t* han if (info.agentBaseAddress != ptr || info.sizeInBytes != len) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + Agent* agent = Agent::Convert(info.agentOwner); bool useFrag = (block.base != ptr || block.length != len); // Assume all pointers and blocks are 4Kb aligned. uint32_t fragOffset = (reinterpret_cast(ptr) - @@ -1229,7 +1230,7 @@ hsa_status_t Runtime::IPCCreate(void* ptr, size_t len, hsa_amd_ipc_memory_t* han if (!ipc_dmabuf_supported_) { HsaSharedMemoryHandle *sHandle = reinterpret_cast(handle); - if (HSAKMT_CALL(hsaKmtShareMemory(block.base, block.length, sHandle)) != HSAKMT_STATUS_SUCCESS) + if (agent->driver().ShareMemory(block.base, block.length, sHandle) != HSA_STATUS_SUCCESS) return HSA_STATUS_ERROR_INVALID_ARGUMENT; hsa_status_t err = HSA_STATUS_SUCCESS; @@ -1250,7 +1251,6 @@ hsa_status_t Runtime::IPCCreate(void* ptr, size_t len, hsa_amd_ipc_memory_t* han handle->handle[1] = dmaBufFdHandleHi; handle->handle[2] = getpid(); // socket server name handle - Agent *agent = Agent::Convert(info.agentOwner); handle->handle[3] = agent->device_type() == Agent::kAmdCpuDevice; // System sub allocations are not supported for now. if (handle->handle[3] && useFrag) return HSA_STATUS_ERROR_INVALID_ARGUMENT; @@ -1391,6 +1391,9 @@ hsa_status_t Runtime::IPCAttach(const hsa_amd_ipc_memory_t* handle, size_t len, bool isFragment = false; uint32_t fragOffset = 0; + if (Runtime::IsDifferentDriver(*agents, num_agents)) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + core::Driver* driver = &agents[0]->driver(); + auto fixFragment = [&](amdgpu_bo_handle ldrm_bo) { if (isFragment) { importAddress = reinterpret_cast(importAddress) + fragOffset; @@ -1402,14 +1405,17 @@ hsa_status_t Runtime::IPCAttach(const hsa_amd_ipc_memory_t* handle, size_t len, allocation_map_[importAddress].ldrm_bo = ldrm_bo; }; - auto importMemory = [&](unsigned int numNodes, HSAuint32 *nodes, - amdgpu_bo_import_result *res) { - int ret = ipc_dmabuf_supported_ ? - IPCClientImport(importHandle.handle[2], dmaBufFDHandle, res, - numNodes, nodes, &importAddress, &importSize) : - HSAKMT_CALL(hsaKmtRegisterSharedHandle(reinterpret_cast(&importHandle), - &importAddress, &importSize)); - if (ret != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + auto importMemory = [&](unsigned int numNodes, HSAuint32* nodes, amdgpu_bo_import_result* res) { + if (ipc_dmabuf_supported_) { + int ret = IPCClientImport(importHandle.handle[2], dmaBufFDHandle, res, numNodes, nodes, + &importAddress, &importSize); + if (ret != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } else { + hsa_status_t ret = driver->RegisterSharedHandle( + reinterpret_cast(&importHandle), &importAddress, + &importSize); + if (ret != HSA_STATUS_SUCCESS) return ret; + } return HSA_STATUS_SUCCESS; }; From a47c060d6a69bb40934c31ae9b87a952269ee45f Mon Sep 17 00:00:00 2001 From: Honglei Huang Date: Tue, 29 Jul 2025 10:41:29 +0800 Subject: [PATCH 3/5] rocr/driver: add ASAN header page management to Driver class Add ASAN header page management to Driver - Add ReplaceAsanHeaderPage and ReturnAsanHeaderPage to Driver interface - Implement ASAN functions in KfdDriver using hsaKmt calls Signed-off-by: Honglei Huang --- .../core/driver/kfd/amd_kfd_driver.cpp | 14 ++++++++++++++ runtime/hsa-runtime/core/inc/amd_kfd_driver.h | 2 ++ runtime/hsa-runtime/core/inc/driver.h | 16 ++++++++++++++++ runtime/hsa-runtime/core/runtime/runtime.cpp | 2 +- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp index 723925f1bc..2e2546bf10 100644 --- a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp +++ b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp @@ -716,5 +716,19 @@ hsa_status_t KfdDriver::RegisterSharedHandle(const HsaSharedMemoryHandle* share_ return HSA_STATUS_SUCCESS; } +hsa_status_t KfdDriver::ReplaceAsanHeaderPage(void* mem) const { + if (HSAKMT_CALL(hsaKmtReplaceAsanHeaderPage(mem)) != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::ReturnAsanHeaderPage(void* mem) const { + if (HSAKMT_CALL(hsaKmtReturnAsanHeaderPage(mem)) != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + } // namespace AMD } // namespace rocr diff --git a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h index da1a6501e4..7f3f59f8d4 100644 --- a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h +++ b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h @@ -139,6 +139,8 @@ public: hsa_status_t ShareMemory(void* mem, size_t size, HsaSharedMemoryHandle* share_mem) const override; hsa_status_t RegisterSharedHandle(const HsaSharedMemoryHandle* share_mem, void** mem, uint64_t* size) const override; + hsa_status_t ReplaceAsanHeaderPage(void* mem) const override; + hsa_status_t ReturnAsanHeaderPage(void* mem) const override; hsa_status_t OpenSMI(uint32_t node_id, int* fd) const override; diff --git a/runtime/hsa-runtime/core/inc/driver.h b/runtime/hsa-runtime/core/inc/driver.h index f97e4b479a..151edf6a98 100644 --- a/runtime/hsa-runtime/core/inc/driver.h +++ b/runtime/hsa-runtime/core/inc/driver.h @@ -370,6 +370,22 @@ public: return HSA_STATUS_ERROR_INVALID_AGENT; } + /// @brief Replaces the ASAN header page with a valid one. + /// @param[in] mem Pointer to the memory to be replaced. + /// @return HSA_STATUS_SUCCESS if the ASAN header page was successfully replaced, or an error + /// code. + virtual hsa_status_t ReplaceAsanHeaderPage(void* mem) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Returns the ASAN header page to its original state. + /// @param[in] mem Pointer to the memory to be returned. + /// @return HSA_STATUS_SUCCESS if the ASAN header page was successfully returned, or an error + /// code. + virtual hsa_status_t ReturnAsanHeaderPage(void* mem) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + /// Unique identifier for supported kernel-mode drivers. const DriverType kernel_driver_type_; diff --git a/runtime/hsa-runtime/core/runtime/runtime.cpp b/runtime/hsa-runtime/core/runtime/runtime.cpp index b2c8be1f4f..e756ec7902 100644 --- a/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -368,7 +368,7 @@ hsa_status_t Runtime::FreeMemory(void* ptr) { } if (alloc_flags & core::MemoryRegion::AllocateAsan) - assert(HSAKMT_CALL(hsaKmtReturnAsanHeaderPage(ptr)) == HSAKMT_STATUS_SUCCESS); + assert(region->owner()->driver().ReturnAsanHeaderPage(ptr) == HSA_STATUS_SUCCESS); const hsa_status_t err = region->Free(ptr, size); if (err != HSA_STATUS_SUCCESS) { From 56cb9390ff627eb0a68b20bc8cf761938aa0afb3 Mon Sep 17 00:00:00 2001 From: Honglei Huang Date: Fri, 27 Jun 2025 11:09:17 +0800 Subject: [PATCH 4/5] rocr/driver: add PC sampling support to driver interface Add PC sampling functionality to the driver interface: 1. Add new PC sampling methods to Driver base class: - PcSamplingQueryCapabilities - PcSamplingCreate - PcSamplingDestroy - PcSamplingStart - PcSamplingStop 2. Implement PC sampling methods in KfdDriver using HSAKMT APIs: - Map HSAKMT status codes to HSA status codes - Handle resource busy conditions - Proper error handling for all operations Signed-off-by: Honglei Huang --- .../core/driver/kfd/amd_kfd_driver.cpp | 59 +++++++++++++++++++ runtime/hsa-runtime/core/inc/amd_kfd_driver.h | 8 +++ runtime/hsa-runtime/core/inc/driver.h | 51 ++++++++++++++++ .../core/runtime/amd_gpu_agent.cpp | 28 ++++----- 4 files changed, 132 insertions(+), 14 deletions(-) diff --git a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp index 2e2546bf10..ba4700340d 100644 --- a/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp +++ b/runtime/hsa-runtime/core/driver/kfd/amd_kfd_driver.cpp @@ -730,5 +730,64 @@ hsa_status_t KfdDriver::ReturnAsanHeaderPage(void* mem) const { return HSA_STATUS_SUCCESS; } +hsa_status_t KfdDriver::PcSamplingQueryCapabilities(uint32_t node_id, void* sample_info, + uint32_t sample_info_sz, + uint32_t* sz_needed) const { + HSAKMT_STATUS status = HSAKMT_CALL( + hsaKmtPcSamplingQueryCapabilities(node_id, sample_info, sample_info_sz, sz_needed)); + if (status == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) { + return static_cast(HSA_STATUS_ERROR_RESOURCE_BUSY); + } + if (status != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::PcSamplingCreate(uint32_t node_id, HsaPcSamplingInfo* sample_info, + uint32_t* trace_id) const { + HSAKMT_STATUS status = HSAKMT_CALL(hsaKmtPcSamplingCreate(node_id, sample_info, trace_id)); + if (status == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) { + return static_cast(HSA_STATUS_ERROR_RESOURCE_BUSY); + } + if (status != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::PcSamplingDestroy(uint32_t node_id, uint32_t trace_id) const { + HSAKMT_STATUS status = HSAKMT_CALL(hsaKmtPcSamplingDestroy(node_id, trace_id)); + if (status == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) { + return static_cast(HSA_STATUS_ERROR_RESOURCE_BUSY); + } + if (status != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::PcSamplingStart(uint32_t node_id, uint32_t trace_id) const { + HSAKMT_STATUS status = HSAKMT_CALL(hsaKmtPcSamplingStart(node_id, trace_id)); + if (status == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) { + return static_cast(HSA_STATUS_ERROR_RESOURCE_BUSY); + } + if (status != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + +hsa_status_t KfdDriver::PcSamplingStop(uint32_t node_id, uint32_t trace_id) const { + HSAKMT_STATUS status = HSAKMT_CALL(hsaKmtPcSamplingStop(node_id, trace_id)); + if (status == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) { + return static_cast(HSA_STATUS_ERROR_RESOURCE_BUSY); + } + if (status != HSAKMT_STATUS_SUCCESS) { + return HSA_STATUS_ERROR; + } + return HSA_STATUS_SUCCESS; +} + } // namespace AMD } // namespace rocr diff --git a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h index 7f3f59f8d4..6a78aac6de 100644 --- a/runtime/hsa-runtime/core/inc/amd_kfd_driver.h +++ b/runtime/hsa-runtime/core/inc/amd_kfd_driver.h @@ -141,6 +141,14 @@ public: uint64_t* size) const override; hsa_status_t ReplaceAsanHeaderPage(void* mem) const override; hsa_status_t ReturnAsanHeaderPage(void* mem) const override; + hsa_status_t PcSamplingQueryCapabilities(uint32_t node_id, void* sample_info, + uint32_t sample_info_sz, + uint32_t* sz_needed) const override; + hsa_status_t PcSamplingCreate(uint32_t node_id, HsaPcSamplingInfo* sample_info, + uint32_t* trace_id) const override; + hsa_status_t PcSamplingDestroy(uint32_t node_id, uint32_t trace_id) const override; + hsa_status_t PcSamplingStart(uint32_t node_id, uint32_t trace_id) const override; + hsa_status_t PcSamplingStop(uint32_t node_id, uint32_t trace_id) const override; hsa_status_t OpenSMI(uint32_t node_id, int* fd) const override; diff --git a/runtime/hsa-runtime/core/inc/driver.h b/runtime/hsa-runtime/core/inc/driver.h index 151edf6a98..e2265126bc 100644 --- a/runtime/hsa-runtime/core/inc/driver.h +++ b/runtime/hsa-runtime/core/inc/driver.h @@ -386,6 +386,57 @@ public: return HSA_STATUS_ERROR_INVALID_AGENT; } + /// @brief Queries the PC sampling capabilities. + /// @param[in] node_id Node ID of the agent + /// @param[in] sample_info Pointer to the sample information + /// @param[in] sample_info_sz Size of the sample information + /// @param[out] sz_needed Size of the sample information needed + /// @return HSA_STATUS_SUCCESS if the PC sampling capabilities were successfully queried, or an + /// error code. + virtual hsa_status_t PcSamplingQueryCapabilities(uint32_t node_id, void* sample_info, + uint32_t sample_info_sz, + uint32_t* sz_needed) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Creates a PC sampling session. + /// @param[in] node_id Node ID of the agent + /// @param[in] sample_info Pointer to the sample information + /// @param[out] trace_id Pointer to the trace ID + /// @return HSA_STATUS_SUCCESS if the PC sampling session was successfully created, or an error + /// code. + virtual hsa_status_t PcSamplingCreate(uint32_t node_id, HsaPcSamplingInfo* sample_info, + uint32_t* trace_id) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Destroys a PC sampling session. + /// @param[in] node_id Node ID of the agent + /// @param[in] trace_id Trace ID of the PC sampling session + /// @return HSA_STATUS_SUCCESS if the PC sampling session was successfully destroyed, or an error + /// code. + virtual hsa_status_t PcSamplingDestroy(uint32_t node_id, uint32_t trace_id) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Starts a PC sampling session. + /// @param[in] node_id Node ID of the agent + /// @param[in] trace_id Trace ID of the PC sampling session + /// @return HSA_STATUS_SUCCESS if the PC sampling session was successfully started, or an error + /// code. + virtual hsa_status_t PcSamplingStart(uint32_t node_id, uint32_t trace_id) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + + /// @brief Stops a PC sampling session. + /// @param[in] node_id Node ID of the agent + /// @param[in] trace_id Trace ID of the PC sampling session + /// @return HSA_STATUS_SUCCESS if the PC sampling session was successfully stopped, or an error + /// code. + virtual hsa_status_t PcSamplingStop(uint32_t node_id, uint32_t trace_id) const { + return HSA_STATUS_ERROR_INVALID_AGENT; + } + /// Unique identifier for supported kernel-mode drivers. const DriverType kernel_driver_type_; diff --git a/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp b/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp index 1b6ecb8b86..31b2f11c81 100644 --- a/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp +++ b/runtime/hsa-runtime/core/runtime/amd_gpu_agent.cpp @@ -2553,14 +2553,14 @@ hsa_status_t GpuAgent::PcSamplingIterateConfig(hsa_ven_amd_pcs_iterate_configura return HSA_STATUS_ERROR; // First query to get size of list needed - HSAKMT_STATUS ret = HSAKMT_CALL(hsaKmtPcSamplingQueryCapabilities(node_id(), NULL, 0, &size)); - if (ret != HSAKMT_STATUS_SUCCESS || size == 0) return HSA_STATUS_ERROR; + hsa_status_t ret = driver().PcSamplingQueryCapabilities(node_id(), NULL, 0, &size); + if (ret != HSA_STATUS_SUCCESS || size == 0) return ret; std::vector sampleInfoList(size); - ret = HSAKMT_CALL(hsaKmtPcSamplingQueryCapabilities(node_id(), sampleInfoList.data(), sampleInfoList.size(), - &size)); + ret = driver().PcSamplingQueryCapabilities(node_id(), sampleInfoList.data(), + sampleInfoList.size(), &size); - if (ret != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR; + if (ret != HSA_STATUS_SUCCESS) return ret; for (uint32_t i = 0; i < size; i++) { hsa_ven_amd_pcs_configuration_t hsaPcSampling; @@ -2586,10 +2586,9 @@ hsa_status_t GpuAgent::PcSamplingCreate(pcs::PcsRuntime::PcSamplingSession& sess // Pass the sampling information to the kernel driver to create PC // sampling session. - HSAKMT_STATUS retkmt = HSAKMT_CALL(hsaKmtPcSamplingCreate(node_id(), &sampleInfo, &thunkId)); - if (retkmt != HSAKMT_STATUS_SUCCESS) { - return (retkmt == HSAKMT_STATUS_KERNEL_ALREADY_OPENED) ? (hsa_status_t)HSA_STATUS_ERROR_RESOURCE_BUSY - : HSA_STATUS_ERROR; + ret = driver().PcSamplingCreate(node_id(), &sampleInfo, &thunkId); + if (ret != HSA_STATUS_SUCCESS) { + return ret; } debug_print("Created PC sampling session with thunkId:%d\n", thunkId); @@ -2795,7 +2794,7 @@ hsa_status_t GpuAgent::PcSamplingCreateFromId(HsaPcSamplingTraceId ioctlId, hsa_status_t GpuAgent::PcSamplingDestroy(pcs::PcsRuntime::PcSamplingSession& session) { if (PcSamplingStop(session) != HSA_STATUS_SUCCESS) return HSA_STATUS_ERROR; - HSAKMT_STATUS retKmt = HSAKMT_CALL(hsaKmtPcSamplingDestroy(node_id(), session.ThunkId())); + hsa_status_t ret = driver().PcSamplingDestroy(node_id(), session.ThunkId()); hsa_ven_amd_pcs_method_kind_t sampling_method = session.method(); pcs_data_t* pcs_data = nullptr; @@ -2827,7 +2826,7 @@ hsa_status_t GpuAgent::PcSamplingDestroy(pcs::PcsRuntime::PcSamplingSession& ses // Update the trap handler to clear any associated device data UpdateTrapHandlerWithPCS(nullptr, nullptr); - return (retKmt == HSAKMT_STATUS_SUCCESS) ? HSA_STATUS_SUCCESS : HSA_STATUS_ERROR; + return ret; } hsa_status_t GpuAgent::PcSamplingStart(pcs::PcsRuntime::PcSamplingSession& session) { @@ -2894,8 +2893,9 @@ hsa_status_t GpuAgent::PcSamplingStart(pcs::PcsRuntime::PcSamplingSession& sessi } // Start the sampling session in the kernel driver - if (HSAKMT_CALL(hsaKmtPcSamplingStart(node_id(), session.ThunkId())) == HSAKMT_STATUS_SUCCESS) + if (driver().PcSamplingStart(node_id(), session.ThunkId()) == HSA_STATUS_SUCCESS) { return HSA_STATUS_SUCCESS; + } debug_print("Failed to start PC sampling session with thunkId:%d\n", session.ThunkId()); // Clean up if starting the session failed @@ -2915,8 +2915,8 @@ hsa_status_t GpuAgent::PcSamplingStop(pcs::PcsRuntime::PcSamplingSession& sessio session.stop(); // Stop PC sampling in the kernel driver - HSAKMT_STATUS retKmt = HSAKMT_CALL(hsaKmtPcSamplingStop(node_id(), session.ThunkId())); - if (retKmt != HSAKMT_STATUS_SUCCESS) + hsa_status_t ret = driver().PcSamplingStop(node_id(), session.ThunkId()); + if (ret != HSA_STATUS_SUCCESS) throw AMD::hsa_exception(HSA_STATUS_ERROR, "Failed to stop PC Sampling session."); // Determine the sampling method and corresponding data From ea0a3e8da4d0b7e18654f85a92b2428197683459 Mon Sep 17 00:00:00 2001 From: Yiannis Papadopoulos Date: Thu, 31 Jul 2025 15:00:47 -0400 Subject: [PATCH 5/5] libhsakmt: Use numa_node_size64 with long long --- libhsakmt/src/fmm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libhsakmt/src/fmm.c b/libhsakmt/src/fmm.c index 84e18cd42a..97b7864ab3 100644 --- a/libhsakmt/src/fmm.c +++ b/libhsakmt/src/fmm.c @@ -1675,7 +1675,7 @@ static void* udmabuf_allocation(uint32_t gpu_id, uint32_t node_id, uint64_t size if (bind_mem_to_numa(numa_node_id, mem, size, mflags)) goto error_release_aperture; - node_size = numa_node_size(numa_node_id, &free_size); + node_size = numa_node_size64(numa_node_id, &free_size); pr_debug("udmabuf_allocation: numa_node_id %d, node_size %lld, free_size %lld\n", numa_node_id, node_size, free_size); /* compare free size at numa_node_id with size */