From faaceaaff8a4a3d67872f40fa448af0f83982b62 Mon Sep 17 00:00:00 2001 From: "Spruit, Neil R" Date: Thu, 19 Oct 2023 17:47:30 +0000 Subject: [PATCH] fix: Check Unaligned before rounding size expected from reserve Related-To: NEO-9127 - Fix the calc to check unaligned before rounding the expected size from reserve memory. Signed-off-by: Spruit, Neil R --- .../core/source/context/context_imp.cpp | 6 +- .../sources/context/test_context.cpp | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 740f04bc28..1146551e6f 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -1051,7 +1051,11 @@ size_t ContextImp::getPageAlignedSizeRequired(size_t size, NEO::HeapIndex *heapR } // Given a size larger than the pageSize, then round the size up to the next pageSize alignment if unaligned if (size > pageSize) { - return ((size + pageSize) & (~pageSize)); + if (size % pageSize > 0) { + return ((size + pageSize) & (~pageSize)); + } else { + return size; + } } return pageSize; } diff --git a/level_zero/core/test/unit_tests/sources/context/test_context.cpp b/level_zero/core/test/unit_tests/sources/context/test_context.cpp index 1dd0126ce2..6b97bedbd8 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context.cpp @@ -1656,6 +1656,65 @@ TEST_F(ContextTest, whenCallingVirtualMemoryReservationWithInvalidArgumentsThenU EXPECT_EQ(ZE_RESULT_SUCCESS, res); } +TEST_F(ContextTest, whenCallingVirtualMemoryReservationWithInvalidMultiPageSizeInArgumentsThenUnsupportedSizeReturned) { + ze_context_handle_t hContext; + ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; + + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + + ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); + + void *pStart = 0x0; + size_t size = 64u; + void *ptr = nullptr; + size_t pagesize = 0u; + + res = contextImp->queryVirtualMemPageSize(device, size, &pagesize); + + size = pagesize * 3 + 10; + + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + NEO::MemoryManager *failingReserveMemoryManager = new ReserveMemoryManagerMock(*neoDevice->executionEnvironment); + auto memoryManager = driverHandle->getMemoryManager(); + driverHandle->setMemoryManager(failingReserveMemoryManager); + res = contextImp->reserveVirtualMem(pStart, size, &ptr); + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, res); + driverHandle->setMemoryManager(memoryManager); + delete failingReserveMemoryManager; + + res = contextImp->destroy(); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); +} + +TEST_F(ContextTest, whenCallingVirtualMemoryReservationWithValidMultiPageSizeInArgumentsThenSuccessReturned) { + ze_context_handle_t hContext; + ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0}; + + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + + ContextImp *contextImp = static_cast(L0::Context::fromHandle(hContext)); + + void *pStart = 0x0; + size_t size = 64u; + void *ptr = nullptr; + size_t pagesize = 0u; + + res = contextImp->queryVirtualMemPageSize(device, size, &pagesize); + + size = pagesize * 3; + + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + res = contextImp->reserveVirtualMem(pStart, size, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + res = contextImp->freeVirtualMem(ptr, size); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + + res = contextImp->destroy(); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); +} + TEST_F(ContextTest, whenCallingPhysicalMemoryAllocateWhenOutOfMemoryThenOutofMemoryReturned) { ze_context_handle_t hContext; ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};