Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
SWDEV-380035 - Check for agent and ptr match for hsa LOCKED ptr
Browse files Browse the repository at this point in the history
Also do not create Arena Memobj for pinned memory

Change-Id: Ibecfe90c62cfa252e3da45408041f3d1cb3acbbb
  • Loading branch information
mangupta authored and zhang2amd committed Mar 20, 2023
1 parent dd5f3d2 commit e047204
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions device/rocm/rocdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3221,7 +3221,9 @@ device::Signal* Device::createSignal() const {
amd::Memory* Device::GetArenaMemObj(const void* ptr, size_t& offset, size_t size) {
// Only create arena_mem_object if CPU memory is accessible from HMM
// or if runtime received an interop from another ROCr's client
if (!info_.hmmCpuMemoryAccessible_ && !IsValidAllocation(ptr, size)) {
hsa_amd_pointer_info_t ptr_info = {};
ptr_info.size = sizeof(hsa_amd_pointer_info_t);
if (!info_.hmmCpuMemoryAccessible_ && !IsValidAllocation(ptr, size, &ptr_info)) {
return nullptr;
}

Expand All @@ -3238,8 +3240,9 @@ amd::Memory* Device::GetArenaMemObj(const void* ptr, size_t& offset, size_t size
}

// Calculate the offset of the pointer.
const void* dev_ptr = reinterpret_cast<void*>(arena_mem_obj_->getDeviceMemory(
*arena_mem_obj_->getContext().devices()[0])->virtualAddress());
const void* dev_ptr = reinterpret_cast<void*>(
arena_mem_obj_->getDeviceMemory(*arena_mem_obj_->getContext().devices()[0])
->virtualAddress());
offset = reinterpret_cast<size_t>(ptr) - reinterpret_cast<size_t>(dev_ptr);

return arena_mem_obj_;
Expand All @@ -3253,20 +3256,25 @@ void Device::ReleaseGlobalSignal(void* signal) const {
}

// ================================================================================================
bool Device::IsValidAllocation(const void* dev_ptr, size_t size) const {
hsa_amd_pointer_info_t ptr_info = {};
ptr_info.size = sizeof(hsa_amd_pointer_info_t);
bool Device::IsValidAllocation(const void* dev_ptr, size_t size, hsa_amd_pointer_info_t* ptr_info) {
// Query ptr type to see if it's a HMM allocation
hsa_status_t status = hsa_amd_pointer_info(
const_cast<void*>(dev_ptr), &ptr_info, nullptr, nullptr, nullptr);
hsa_status_t status =
hsa_amd_pointer_info(const_cast<void*>(dev_ptr), ptr_info, nullptr, nullptr, nullptr);
// The call should never fail in ROCR, but just check for an error and continue
if (status != HSA_STATUS_SUCCESS) {
LogError("hsa_amd_pointer_info() failed");
}
// Check if it's a legacy non-HMM allocation in ROCr
if (ptr_info.type != HSA_EXT_POINTER_TYPE_UNKNOWN) {
if ((size != 0) && ((reinterpret_cast<const_address>(dev_ptr) -
reinterpret_cast<const_address>(ptr_info.agentBaseAddress)) > size)) {

// Return false for pinned memory. A true return may result in a race because
// ROCclr may attempt to do a pin/copy/unpin underneath in a multithreaded environment
if (ptr_info->type == HSA_EXT_POINTER_TYPE_LOCKED) {
return false;
}

if (ptr_info->type != HSA_EXT_POINTER_TYPE_UNKNOWN) {
if ((size != 0) &&
((reinterpret_cast<const_address>(dev_ptr) -
reinterpret_cast<const_address>(ptr_info->agentBaseAddress)) > size)) {
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion device/rocm/rocdevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ class Device : public NullDevice {
const bool isFineGrainSupported() const;

//! Returns True if memory pointer is known to ROCr (excludes HMM allocations)
bool IsValidAllocation(const void* dev_ptr, size_t size) const;
bool IsValidAllocation(const void* dev_ptr, size_t size, hsa_amd_pointer_info_t* ptr_info);

//! Allocates hidden heap for device memory allocations
void HiddenHeapAlloc();
Expand Down

0 comments on commit e047204

Please sign in to comment.