From 1087f13ee11e381296adff99b9481e4125936a62 Mon Sep 17 00:00:00 2001 From: Satyanvesh Dittakavi Date: Wed, 23 Nov 2022 14:40:12 +0000 Subject: [PATCH] SWDEV-369552 - SWDEV-369553 - SWDEV-369555 - Fix hipMemAdvise - API should return error if the size passed is more than the bytes allocated - Passing invalid device should return error except for the advise hipMemAdviseSetReadMostly Change-Id: I1c4c15136b825215bf1e1f5ee7a9a08e5895d870 [ROCm/clr commit: 873bc030343272c25f3d4fd1baddda252644c1eb] --- projects/clr/hipamd/src/hip_hmm.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/projects/clr/hipamd/src/hip_hmm.cpp b/projects/clr/hipamd/src/hip_hmm.cpp index 5b9dc5ed43..00687f84e4 100644 --- a/projects/clr/hipamd/src/hip_hmm.cpp +++ b/projects/clr/hipamd/src/hip_hmm.cpp @@ -133,11 +133,29 @@ hipError_t hipMemPrefetchAsync(const void* dev_ptr, size_t count, int device, hipError_t hipMemAdvise(const void* dev_ptr, size_t count, hipMemoryAdvise advice, int device) { HIP_INIT_API(hipMemAdvise, dev_ptr, count, advice, device); - if ((dev_ptr == nullptr) || (count == 0) || - ((device != hipCpuDeviceId) && (static_cast(device) >= g_devices.size()))) { + bool isAdviseReadMostly = (advice == hipMemAdviseSetReadMostly) || + (advice == hipMemAdviseUnsetReadMostly); + + if (!isAdviseReadMostly && ((device != hipCpuDeviceId) && + (static_cast(device) >= g_devices.size()))) { + HIP_RETURN(hipErrorInvalidDevice); + } + + if ((dev_ptr == nullptr) || (count == 0)) { HIP_RETURN(hipErrorInvalidValue); } - amd::Device* dev = (device == hipCpuDeviceId) ? + + size_t offset = 0; + amd::Memory* memObj = getMemoryObject(dev_ptr, offset); + if (memObj == nullptr) { + HIP_RETURN(hipErrorMemoryAllocation); + } + + if (count > (memObj->getSize() - offset)) { + HIP_RETURN(hipErrorInvalidValue); + } + + amd::Device* dev = (device == hipCpuDeviceId || isAdviseReadMostly) ? g_devices[0]->devices()[0] : g_devices[device]->devices()[0]; bool use_cpu = (device == hipCpuDeviceId) ? true : false;