From 89070536c07e4e8558e579b4f6c0e0ab7d4837b2 Mon Sep 17 00:00:00 2001 From: Karthik Jayaprakash <54370791+kjayapra-amd@users.noreply.github.com> Date: Wed, 27 Aug 2025 14:28:53 -0400 Subject: [PATCH] SWDEV-552141 - Fix handle/fd type passed from app to align with spec. (#759) * SWDEV-552141 - Fix handle/fd type passed from app to align with spec. * SWDEV-552141 - Fix handle/fd type passed from app to align with spec. --- projects/clr/rocclr/device/rocm/rocdevice.cpp | 2 +- projects/clr/rocclr/device/rocm/rocmemory.cpp | 1 - .../hipMemGetHandleForAddressRange.cc | 29 ++++++++++--------- .../hipMemImportFromShareableHandle.cc | 20 ++++++++----- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/projects/clr/rocclr/device/rocm/rocdevice.cpp b/projects/clr/rocclr/device/rocm/rocdevice.cpp index 0c36b9b209..16396feea8 100644 --- a/projects/clr/rocclr/device/rocm/rocdevice.cpp +++ b/projects/clr/rocclr/device/rocm/rocdevice.cpp @@ -2434,7 +2434,7 @@ bool Device::ImportShareableHSAHandle(void* osHandle, uint64_t* hsa_handle_ptr) return false; } - int dmabuf_fd = *(reinterpret_cast(osHandle)); + int dmabuf_fd = static_cast(reinterpret_cast(osHandle)); if ((hsa_status = hsa_amd_vmem_import_shareable_handle(dmabuf_fd, &hsa_vmem_handle)) != HSA_STATUS_SUCCESS) { LogPrintfError("Failed hsa_amd_vmem_import_shareable_handle with status: %d \n", hsa_status); diff --git a/projects/clr/rocclr/device/rocm/rocmemory.cpp b/projects/clr/rocclr/device/rocm/rocmemory.cpp index f85a1680d9..64870a7dbd 100644 --- a/projects/clr/rocclr/device/rocm/rocmemory.cpp +++ b/projects/clr/rocclr/device/rocm/rocmemory.cpp @@ -778,7 +778,6 @@ bool Buffer::create(bool alloc_local) { if (memFlags & ROCCLR_MEM_PHYMEM) { if (memFlags & ROCCLR_MEM_INTERPROCESS) { - int dmabuf_fd = *(reinterpret_cast(owner()->getSvmPtr())); // if interprocess flag is set, then the memory is importable. if (!dev().ImportShareableHSAHandle(owner()->getSvmPtr(), &owner()->getUserData().hsa_handle)) { diff --git a/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemGetHandleForAddressRange.cc b/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemGetHandleForAddressRange.cc index 214df38fdc..ea4ccc4a33 100644 --- a/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemGetHandleForAddressRange.cc +++ b/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemGetHandleForAddressRange.cc @@ -222,10 +222,11 @@ hipDeviceptr_t createVirtualMemoryAndFillData(int size, int* reservedAddrSize, i * Helper function to validate the handle from hipMemGetHandleForAddressRange * by extracting the data from the handle */ -bool validateHandle(int* handle, int size, int device = 0) { +bool validateHandle(int handle, int size, int device = 0) { hipMemGenericAllocationHandle_t imported_handle; - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, reinterpret_cast(handle), - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(handle)), + hipMemHandleTypePosixFileDescriptor)); size_t granularity = GetGranularity(device); if (granularity <= 0) { @@ -303,7 +304,7 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_DeviceMemory") { constexpr int kDeviceId = 0; HIP_CHECK(hipDeviceGet(&device, kDeviceId)); checkVMMSupported(device); - REQUIRE(validateHandle(&handle, size) == true); + REQUIRE(validateHandle(handle, size) == true); HIP_CHECK(hipFree(srcDevMem)); } @@ -341,7 +342,7 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_VM") { hipMemGetHandleForAddressRange(&handle, ptrA, sizeBytes, hipMemRangeHandleTypeDmaBufFd, 0)); REQUIRE(handle > 0); - REQUIRE(validateHandle(&handle, size) == true); + REQUIRE(validateHandle(handle, size) == true); HIP_CHECK(hipMemUnmap(ptrA, reservedAddrSize)); HIP_CHECK(hipMemAddressFree(ptrA, reservedAddrSize)); @@ -393,7 +394,7 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_DeviceMemory_InAnotherDevice") { HIP_CHECK(hipDeviceGet(&device, dstDeviceId)); checkVMMSupported(device); - REQUIRE(validateHandle(&handle, size, dstDeviceId) == true); + REQUIRE(validateHandle(handle, size, dstDeviceId) == true); HIP_CHECK(hipFree(srcDevMem)); HIP_CHECK(hipDeviceReset()) @@ -448,7 +449,7 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_VM_InAnotherDevice") { HIP_CHECK(hipDeviceGet(&device, dstDeviceId)); checkVMMSupported(device); - REQUIRE(validateHandle(&handle, size, dstDeviceId) == true); + REQUIRE(validateHandle(handle, size, dstDeviceId) == true); HIP_CHECK(hipMemUnmap(ptrA, reservedAddrSize)); HIP_CHECK(hipMemAddressFree(ptrA, reservedAddrSize)); @@ -498,15 +499,16 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_MulProc_Socket_DeviceMem") { checkSysCallErrors(sockObj.recvShareableHdl(&shHandle)); hipMemGenericAllocationHandle_t imported_handle; // import the sareable handle - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shHandle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shHandle)), + hipMemHandleTypePosixFileDescriptor)); hipDevice_t device; HIP_CHECK(hipDeviceGet(&device, 0)); checkVMMSupported(device); // Validate the handle - REQUIRE(validateHandle(&shHandle, size_mem / sizeof(int))); + REQUIRE(validateHandle(shHandle, size_mem / sizeof(int))); checkSysCallErrors(sockObj.closeThisSock()); REQUIRE(close(fd[0]) == 0); @@ -591,14 +593,15 @@ TEST_CASE("Unit_hipMemGetHandleForAddressRange_MulProc_Socket_VM") { checkSysCallErrors(sockObj.recvShareableHdl(&shHandle)); hipMemGenericAllocationHandle_t imported_handle; // import the sareable handle - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shHandle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shHandle)), + hipMemHandleTypePosixFileDescriptor)); hipDevice_t device; HIP_CHECK(hipDeviceGet(&device, 0)); checkVMMSupported(device); // Validate handle - REQUIRE(validateHandle(&shHandle, size_mem / sizeof(int))); + REQUIRE(validateHandle(shHandle, size_mem / sizeof(int))); checkSysCallErrors(sockObj.closeThisSock()); REQUIRE(close(fd[0]) == 0); diff --git a/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemImportFromShareableHandle.cc b/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemImportFromShareableHandle.cc index b6d6fbd0f9..5e7283c2ab 100644 --- a/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemImportFromShareableHandle.cc +++ b/projects/hip-tests/catch/unit/virtualMemoryManagement/hipMemImportFromShareableHandle.cc @@ -80,8 +80,9 @@ TEST_CASE("Unit_hipMemImportFromShareableHandle_Positive_Basic") { HIP_CHECK(hipMemExportToShareableHandle(&shareable_handle, handle, hipMemHandleTypePosixFileDescriptor, 0)); hipMemGenericAllocationHandle_t imported_handle; - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shareable_handle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shareable_handle)), + hipMemHandleTypePosixFileDescriptor)); HIP_CHECK(hipMemRelease(handle)); CTX_DESTROY(); } @@ -179,8 +180,9 @@ TEST_CASE("Unit_hipMemImportFromShareableHandle_MulProc_ChldUseHdl") { checkSysCallErrors(sockObj.recvShareableHdl(&shHandle)); hipMemGenericAllocationHandle_t imported_handle; // import the sareable handle - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shHandle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shHandle)), + hipMemHandleTypePosixFileDescriptor)); // Allocate virtual address range hipDeviceptr_t ptrA; HIP_CHECK(hipMemAddressReserve(&ptrA, size_mem, 0, 0, 0)); @@ -311,8 +313,9 @@ TEST_CASE("Unit_hipMemImportFromShareableHandle_MulProc_ParntChldUseHdl") { hipMemGenericAllocationHandle_t imported_handle; // import the sareable handle - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shHandle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shHandle)), + hipMemHandleTypePosixFileDescriptor)); // Allocate virtual address range hipDeviceptr_t ptrA; HIP_CHECK(hipMemAddressReserve(&ptrA, size_mem, 0, 0, 0)); @@ -448,8 +451,9 @@ TEST_CASE("Unit_hipMemImportFromShareableHandle_MulProc_GrndChldUseHdl") { hipMemGenericAllocationHandle_t imported_handle; // import the sareable handle - HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, &shHandle, - hipMemHandleTypePosixFileDescriptor)); + HIP_CHECK(hipMemImportFromShareableHandle(&imported_handle, + reinterpret_cast(static_cast(shHandle)), + hipMemHandleTypePosixFileDescriptor)); // Allocate virtual address range hipDeviceptr_t ptrA; HIP_CHECK(hipMemAddressReserve(&ptrA, size_mem, 0, 0, 0));