From c8664b50a4ea3789d330b100d8f6f30cee1b857b Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Tue, 22 Oct 2019 13:29:39 +0200 Subject: [PATCH] MemoryManager: don't access command stream receivers from execution environment Related-To: NEO-3691, NEO-3857 Change-Id: I8d83456f4164d2b25d78bffa368cffe988c498b0 Signed-off-by: Mateusz Jablonski --- runtime/memory_manager/memory_manager.cpp | 4 - runtime/memory_manager/memory_manager.h | 1 - .../os_interface/linux/drm_memory_manager.cpp | 4 +- .../command_stream_receiver_tests.cpp | 5 - .../fixtures/memory_allocator_fixture.h | 6 +- .../mem_obj/mem_obj_destruction_tests.cpp | 16 +- unit_tests/mem_obj/mem_obj_tests.cpp | 11 +- .../mocks/linux/mock_drm_memory_manager.h | 1 + .../linux/drm_memory_manager_tests.cpp | 186 +++++++++++++----- .../windows/wddm_memory_manager_tests.cpp | 1 - 10 files changed, 153 insertions(+), 82 deletions(-) diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index c6886ba4a0..d19981f69e 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -396,10 +396,6 @@ void MemoryManager::unregisterEngineForCsr(CommandStreamReceiver *commandStreamR } } -CommandStreamReceiver *MemoryManager::getDefaultCommandStreamReceiver(uint32_t deviceId) const { - return peekExecutionEnvironment().commandStreamReceivers[deviceId][defaultEngineIndex].get(); -} - void *MemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) { if (!graphicsAllocation) { return nullptr; diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index c7487c9a8d..59e8a84631 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -149,7 +149,6 @@ class MemoryManager { OsContext *createAndRegisterOsContext(CommandStreamReceiver *commandStreamReceiver, aub_stream::EngineType engineType, DeviceBitfield deviceBitfield, PreemptionMode preemptionMode, bool lowPriority); uint32_t getRegisteredEnginesCount() const { return static_cast(registeredEngines.size()); } - CommandStreamReceiver *getDefaultCommandStreamReceiver(uint32_t deviceId) const; EngineControlContainer &getRegisteredEngines(); EngineControl *getRegisteredEngineForCsr(CommandStreamReceiver *commandStreamReceiver); void unregisterEngineForCsr(CommandStreamReceiver *commandStreamReceiver); diff --git a/runtime/os_interface/linux/drm_memory_manager.cpp b/runtime/os_interface/linux/drm_memory_manager.cpp index 2172c96a58..a9c8734e5f 100644 --- a/runtime/os_interface/linux/drm_memory_manager.cpp +++ b/runtime/os_interface/linux/drm_memory_manager.cpp @@ -684,8 +684,8 @@ int DrmMemoryManager::obtainFdFromHandle(int boHandle) { } uint32_t DrmMemoryManager::getDefaultDrmContextId() const { - auto &osContextLinux = static_cast(getDefaultCommandStreamReceiver(0)->getOsContext()); - return osContextLinux.getDrmContextIds()[0]; + auto osContextLinux = static_cast(registeredEngines[defaultEngineIndex].osContext); + return osContextLinux->getDrmContextIds()[0]; } } // namespace NEO diff --git a/unit_tests/command_stream/command_stream_receiver_tests.cpp b/unit_tests/command_stream/command_stream_receiver_tests.cpp index a2d523a610..4bee783885 100644 --- a/unit_tests/command_stream/command_stream_receiver_tests.cpp +++ b/unit_tests/command_stream/command_stream_receiver_tests.cpp @@ -145,11 +145,6 @@ TEST_F(CommandStreamReceiverTest, givenCommandStreamReceiverWhenGetCSIsCalledThe EXPECT_EQ(GraphicsAllocation::AllocationType::COMMAND_BUFFER, commandStreamAllocation->getAllocationType()); } -TEST_F(CommandStreamReceiverTest, memoryManagerHasAccessToCSR) { - auto *memoryManager = commandStreamReceiver->getMemoryManager(); - EXPECT_EQ(commandStreamReceiver, memoryManager->getDefaultCommandStreamReceiver(0)); -} - HWTEST_F(CommandStreamReceiverTest, whenStoreAllocationThenStoredAllocationHasTaskCountFromCsr) { auto &csr = pDevice->getUltCommandStreamReceiver(); auto *memoryManager = csr.getMemoryManager(); diff --git a/unit_tests/fixtures/memory_allocator_fixture.h b/unit_tests/fixtures/memory_allocator_fixture.h index 9ae7140346..0b7f0ffc6f 100644 --- a/unit_tests/fixtures/memory_allocator_fixture.h +++ b/unit_tests/fixtures/memory_allocator_fixture.h @@ -26,7 +26,7 @@ class MemoryAllocatorFixture : public MemoryManagementFixture { executionEnvironment->initializeCommandStreamReceiver(0, 0); memoryManager = new MockMemoryManager(false, false, *executionEnvironment); executionEnvironment->memoryManager.reset(memoryManager); - csr = memoryManager->getDefaultCommandStreamReceiver(0); + csr = executionEnvironment->commandStreamReceivers[0][0].get(); auto engineType = HwHelper::get(platformDevices[0]->platform.eRenderCoreFamily).getGpgpuEngineInstances()[0]; auto osContext = memoryManager->createAndRegisterOsContext(csr, engineType, 1, PreemptionHelper::getDefaultPreemptionMode(*platformDevices[0]), false); csr->setupContext(*osContext); @@ -39,6 +39,6 @@ class MemoryAllocatorFixture : public MemoryManagementFixture { protected: std::unique_ptr executionEnvironment; - MockMemoryManager *memoryManager; - CommandStreamReceiver *csr; + MockMemoryManager *memoryManager = nullptr; + CommandStreamReceiver *csr = nullptr; }; diff --git a/unit_tests/mem_obj/mem_obj_destruction_tests.cpp b/unit_tests/mem_obj/mem_obj_destruction_tests.cpp index 22019f0dcc..13d52b884c 100644 --- a/unit_tests/mem_obj/mem_obj_destruction_tests.cpp +++ b/unit_tests/mem_obj/mem_obj_destruction_tests.cpp @@ -42,7 +42,8 @@ class MemObjDestructionTest : public ::testing::TestWithParam { MemoryPropertiesFlagsParser::createMemoryPropertiesFlags({CL_MEM_READ_WRITE}), CL_MEM_READ_WRITE, 0, size, nullptr, nullptr, allocation, true, false, false); - *device->getDefaultEngine().commandStreamReceiver->getTagAddress() = 0; + csr = device->getDefaultEngine().commandStreamReceiver; + *csr->getTagAddress() = 0; contextId = device->getDefaultEngine().osContext->getContextId(); } @@ -65,13 +66,14 @@ class MemObjDestructionTest : public ::testing::TestWithParam { } constexpr static uint32_t taskCountReady = 3u; - ExecutionEnvironment *executionEnvironment; + ExecutionEnvironment *executionEnvironment = nullptr; std::unique_ptr device; uint32_t contextId = 0; - MockMemoryManager *memoryManager; + MockMemoryManager *memoryManager = nullptr; std::unique_ptr context; - GraphicsAllocation *allocation; - MemObj *memObj; + GraphicsAllocation *allocation = nullptr; + MemObj *memObj = nullptr; + CommandStreamReceiver *csr = nullptr; size_t size = MemoryConstants::pageSize; }; @@ -112,7 +114,7 @@ TEST_P(MemObjAsyncDestructionTest, givenMemObjWithDestructableAllocationWhenAsyn } else { makeMemObjNotReady(); } - auto &allocationList = memoryManager->getDefaultCommandStreamReceiver(0)->getTemporaryAllocations(); + auto &allocationList = csr->getTemporaryAllocations(); EXPECT_TRUE(allocationList.peekIsEmpty()); delete memObj; @@ -299,7 +301,7 @@ HWTEST_P(MemObjSyncDestructionTest, givenMemObjWithDestructableAllocationWhenAsy .WillByDefault(::testing::Invoke(waitForCompletionWithTimeoutMock)); delete memObj; - auto &allocationList = memoryManager->getDefaultCommandStreamReceiver(0)->getTemporaryAllocations(); + auto &allocationList = mockCsr->getTemporaryAllocations(); EXPECT_TRUE(allocationList.peekIsEmpty()); } diff --git a/unit_tests/mem_obj/mem_obj_tests.cpp b/unit_tests/mem_obj/mem_obj_tests.cpp index 9148cf169d..bf2efc021d 100644 --- a/unit_tests/mem_obj/mem_obj_tests.cpp +++ b/unit_tests/mem_obj/mem_obj_tests.cpp @@ -153,12 +153,13 @@ TEST(MemObj, givenNotReadyGraphicsAllocationWhenMemObjDestroysAllocationAsyncThe auto memoryManager = context.getDevice(0)->getExecutionEnvironment()->memoryManager.get(); auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); - allocation->updateTaskCount(2, context.getDevice(0)->getDefaultEngine().osContext->getContextId()); - *(memoryManager->getDefaultCommandStreamReceiver(0)->getTagAddress()) = 1; + auto defaultEngine = context.getDevice(0)->getDefaultEngine(); + allocation->updateTaskCount(2, defaultEngine.osContext->getContextId()); + *(defaultEngine.commandStreamReceiver->getTagAddress()) = 1; MemoryPropertiesFlags memoryProperties = MemoryPropertiesFlagsParser::createMemoryPropertiesFlags({CL_MEM_COPY_HOST_PTR}); MemObj memObj(&context, CL_MEM_OBJECT_BUFFER, memoryProperties, CL_MEM_COPY_HOST_PTR, 0, MemoryConstants::pageSize, nullptr, nullptr, nullptr, true, false, false); - auto &allocationList = memoryManager->getDefaultCommandStreamReceiver(0)->getTemporaryAllocations(); + auto &allocationList = defaultEngine.commandStreamReceiver->getTemporaryAllocations(); EXPECT_TRUE(allocationList.peekIsEmpty()); memObj.destroyGraphicsAllocation(allocation, true); @@ -196,7 +197,7 @@ TEST(MemObj, givenNotUsedGraphicsAllocationWhenMemObjDestroysAllocationAsyncThen MemObj memObj(&context, CL_MEM_OBJECT_BUFFER, memoryProperties, CL_MEM_COPY_HOST_PTR, 0, MemoryConstants::pageSize, nullptr, nullptr, nullptr, true, false, false); - auto &allocationList = memoryManager.getDefaultCommandStreamReceiver(0)->getTemporaryAllocations(); + auto &allocationList = context.getDevice(0)->getDefaultEngine().commandStreamReceiver->getTemporaryAllocations(); EXPECT_TRUE(allocationList.peekIsEmpty()); memObj.destroyGraphicsAllocation(allocation, true); @@ -214,7 +215,7 @@ TEST(MemObj, givenMemoryManagerWithoutDeviceWhenMemObjDestroysAllocationAsyncThe MemObj memObj(&context, CL_MEM_OBJECT_BUFFER, memoryProperties, CL_MEM_COPY_HOST_PTR, 0, MemoryConstants::pageSize, nullptr, nullptr, nullptr, true, false, false); - auto &allocationList = memoryManager.getDefaultCommandStreamReceiver(0)->getTemporaryAllocations(); + auto &allocationList = context.getDevice(0)->getDefaultEngine().commandStreamReceiver->getTemporaryAllocations(); EXPECT_TRUE(allocationList.peekIsEmpty()); memObj.destroyGraphicsAllocation(allocation, true); diff --git a/unit_tests/mocks/linux/mock_drm_memory_manager.h b/unit_tests/mocks/linux/mock_drm_memory_manager.h index 30b06b6f49..64ab807dcf 100644 --- a/unit_tests/mocks/linux/mock_drm_memory_manager.h +++ b/unit_tests/mocks/linux/mock_drm_memory_manager.h @@ -48,6 +48,7 @@ class TestedDrmMemoryManager : public MemoryManagerCreate { using DrmMemoryManager::supportsMultiStorageResources; using DrmMemoryManager::unlockResourceInLocalMemoryImpl; using MemoryManager::allocateGraphicsMemoryInDevicePool; + using MemoryManager::registeredEngines; using MemoryManager::useInternal32BitAllocator; TestedDrmMemoryManager(ExecutionEnvironment &executionEnvironment) : MemoryManagerCreate(gemCloseWorkerMode::gemCloseWorkerInactive, diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index 4303b819a7..cdc63aa45e 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -134,6 +134,10 @@ TEST_F(DrmMemoryManagerTest, pinAfterAllocateWhenAskedAndAllowedAndBigAllocation mock->ioctl_expected.gemClose = 2; auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); auto alloc = static_cast(memoryManager->allocateGraphicsMemoryWithProperties(createAllocationProperties(10 * MemoryConstants::megaByte, true))); @@ -167,6 +171,10 @@ TEST_F(DrmMemoryManagerTest, givenDrmContextIdWhenAllocationIsCreatedThenPinWith mock->ioctl_expected.gemClose = 2; auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } auto drmContextId = memoryManager->getDefaultDrmContextId(); ASSERT_NE(nullptr, memoryManager->getPinBB()); EXPECT_NE(0u, drmContextId); @@ -199,6 +207,10 @@ TEST_F(DrmMemoryManagerTest, doNotPinAfterAllocateWhenNotAskedButAllowed) { mock->ioctl_expected.gemWait = 1; auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); auto alloc = static_cast(memoryManager->allocateGraphicsMemoryWithProperties(createAllocationProperties(MemoryConstants::pageSize, false))); @@ -214,6 +226,10 @@ TEST_F(DrmMemoryManagerTest, doNotPinAfterAllocateWhenAskedButNotAllowed) { mock->ioctl_expected.gemClose = 1; auto memoryManager = std::make_unique(false, false, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } auto alloc = static_cast(memoryManager->allocateGraphicsMemoryWithProperties(createAllocationProperties(MemoryConstants::pageSize, true))); ASSERT_NE(nullptr, alloc); @@ -230,6 +246,10 @@ TEST_F(DrmMemoryManagerTest, pinAfterAllocateWhenAskedAndAllowedAndBigAllocation mock->ioctl_expected.gemWait = 1; auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); allocationData.size = 10 * MB; @@ -249,6 +269,10 @@ TEST_F(DrmMemoryManagerTest, givenSmallAllocationHostPtrAllocationWhenForcePinIs mock->ioctl_expected.gemClose = 2; auto memoryManager = std::make_unique(false, true, false, *executionEnvironment); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); // one page is too small for early pinning @@ -556,11 +580,15 @@ TEST_F(DrmMemoryManagerTest, NullOsHandleStorageAskedForPopulationReturnsFilledP } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValidationWhenReadOnlyPointerCausesPinningFailWithEfaultThenPopulateOsHandlesReturnsInvalidHostPointerError) { - std::unique_ptr testedMemoryManager(new (std::nothrow) TestedDrmMemoryManager(false, - false, - true, - *executionEnvironment)); + std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, + false, + true, + *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } OsHandleStorage storage; storage.fragmentStorageData[0].cpuPtr = reinterpret_cast(0x1000); storage.fragmentStorageData[0].fragmentSize = 1; @@ -573,7 +601,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValid mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.execbuffer2 = 1; - MemoryManager::AllocationStatus result = testedMemoryManager->populateOsHandles(storage); + MemoryManager::AllocationStatus result = memoryManager->populateOsHandles(storage); EXPECT_EQ(MemoryManager::AllocationStatus::InvalidHostPointer, result); mock->testIoctls(); @@ -583,16 +611,19 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValid EXPECT_EQ(nullptr, storage.fragmentStorageData[2].osHandleStorage); storage.fragmentStorageData[0].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(storage); + memoryManager->cleanOsHandles(storage); mock->ioctl_res_ext = &mock->NONE; } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValidationWhenPinningFailWithErrorDifferentThanEfaultThenPopulateOsHandlesReturnsError) { - std::unique_ptr testedMemoryManager(new (std::nothrow) TestedDrmMemoryManager(false, - false, - true, - *executionEnvironment)); - + std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, + false, + true, + *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } OsHandleStorage storage; storage.fragmentStorageData[0].cpuPtr = reinterpret_cast(0x1000); storage.fragmentStorageData[0].fragmentSize = 1; @@ -605,7 +636,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValid mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.execbuffer2 = 1; - MemoryManager::AllocationStatus result = testedMemoryManager->populateOsHandles(storage); + MemoryManager::AllocationStatus result = memoryManager->populateOsHandles(storage); EXPECT_EQ(MemoryManager::AllocationStatus::Error, result); mock->testIoctls(); @@ -615,7 +646,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValid EXPECT_EQ(nullptr, storage.fragmentStorageData[2].osHandleStorage); storage.fragmentStorageData[0].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(storage); + memoryManager->cleanOsHandles(storage); mock->ioctl_res_ext = &mock->NONE; } @@ -2382,7 +2413,10 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenMemoryManagerWhenAlloc false, true, *executionEnvironment)); - + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } void *ptr = reinterpret_cast(0x1001); auto size = 4096u; auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{false, size}, ptr); @@ -2441,11 +2475,11 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna this->mock->ioctl_expected.gemUserptr = 1; EXPECT_THROW( { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, - false, - true, - *executionEnvironment)); - EXPECT_NE(nullptr, testedMemoryManager.get()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, + false, + true, + *executionEnvironment)); + EXPECT_NE(nullptr, memoryManager.get()); }, std::exception); this->mock->ioctl_res = 0; @@ -2455,9 +2489,13 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEnabledValidateHostMemoryWhenPopulateOsHandlesIsCalledThenHostMemoryIsValidated) { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager.get()); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } + ASSERT_NE(nullptr, memoryManager.get()); + ASSERT_NE(nullptr, memoryManager->getPinBB()); mock->reset(); mock->ioctl_expected.gemUserptr = 1; @@ -2466,7 +2504,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna OsHandleStorage handleStorage; handleStorage.fragmentStorageData[0].cpuPtr = reinterpret_cast(0x1000); handleStorage.fragmentStorageData[0].fragmentSize = 4096; - auto result = testedMemoryManager->populateOsHandles(handleStorage); + auto result = memoryManager->populateOsHandles(handleStorage); EXPECT_EQ(MemoryManager::AllocationStatus::Success, result); mock->testIoctls(); @@ -2474,7 +2512,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna EXPECT_NE(nullptr, handleStorage.fragmentStorageData[0].osHandleStorage); handleStorage.fragmentStorageData[0].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEnabledValidateHostMemoryWhenPopulateOsHandlesIsCalledWithFirstFragmentAlreadyAllocatedThenNewBosAreValidated) { @@ -2495,12 +2533,16 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna size_t numberOfBosPinned; }; - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager.get()); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } + ASSERT_NE(nullptr, memoryManager.get()); + ASSERT_NE(nullptr, memoryManager->getPinBB()); PinBufferObject *pinBB = new PinBufferObject(this->mock.get()); - testedMemoryManager->injectPinBB(pinBB); + memoryManager->injectPinBB(pinBB); mock->reset(); mock->ioctl_expected.gemUserptr = 2; @@ -2520,7 +2562,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna handleStorage.fragmentStorageData[2].cpuPtr = reinterpret_cast(0x4000); handleStorage.fragmentStorageData[2].fragmentSize = 4096; - auto result = testedMemoryManager->populateOsHandles(handleStorage); + auto result = memoryManager->populateOsHandles(handleStorage); EXPECT_EQ(MemoryManager::AllocationStatus::Success, result); mock->testIoctls(); @@ -2536,7 +2578,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEna handleStorage.fragmentStorageData[1].freeTheFragment = true; handleStorage.fragmentStorageData[2].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenValidateHostPtrMemoryEnabledWhenHostPtrAllocationIsCreatedWithoutForcingPinThenBufferObjectIsPinned) { @@ -2545,6 +2587,10 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenValidateHostPtrMemoryE mock->ioctl_expected.gemClose = 2; std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, true, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); size_t size = 10 * MB; @@ -2558,20 +2604,24 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenValidateHostPtrMemoryE } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledHostMemoryValidationWhenValidHostPointerIsPassedToPopulateThenSuccessIsReturned) { - std::unique_ptr testedMemoryManager(new (std::nothrow) TestedDrmMemoryManager(false, - false, - true, - *executionEnvironment)); + std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, + false, + true, + *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } OsHandleStorage storage; storage.fragmentStorageData[0].cpuPtr = reinterpret_cast(0x1000); storage.fragmentStorageData[0].fragmentSize = 1; - auto result = testedMemoryManager->populateOsHandles(storage); + auto result = memoryManager->populateOsHandles(storage); EXPECT_EQ(MemoryManager::AllocationStatus::Success, result); EXPECT_NE(nullptr, storage.fragmentStorageData[0].osHandleStorage); storage.fragmentStorageData[0].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(storage); + memoryManager->cleanOsHandles(storage); } TEST_F(DrmMemoryManagerTest, givenForcePinAndHostMemoryValidationEnabledWhenSmallAllocationIsCreatedThenBufferObjectIsPinned) { @@ -2581,6 +2631,10 @@ TEST_F(DrmMemoryManagerTest, givenForcePinAndHostMemoryValidationEnabledWhenSmal mock->ioctl_expected.gemClose = 2; // 1 pinBB, 1 small allocation std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, true, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } ASSERT_NE(nullptr, memoryManager->getPinBB()); // one page is too small for early pinning but pinning is used for host memory validation @@ -2600,6 +2654,10 @@ TEST_F(DrmMemoryManagerTest, givenForcePinAllowedAndNoPinBBInMemoryManagerWhenAl mock->ioctl_expected.gemClose = 1; mock->ioctl_res = -1; std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(false, true, false, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } EXPECT_EQ(nullptr, memoryManager->getPinBB()); mock->ioctl_res = 0; @@ -2693,6 +2751,10 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenAllocateGraphicsMemoryForN TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenForcePinNotAllowedAndHostMemoryValidationEnabledWhenAllocationIsCreatedThenBufferObjectIsPinnedOnlyOnce) { std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } mock->reset(); mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.execbuffer2 = 1; @@ -2715,6 +2777,10 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenForcePinNotAllowedAndH TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenForcePinNotAllowedAndHostMemoryValidationDisabledWhenAllocationIsCreatedThenBufferObjectIsNotPinned) { std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, false, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } mock->reset(); mock->ioctl_expected.gemUserptr = 1; mock->ioctl_expected.gemClose = 1; @@ -2735,9 +2801,13 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenForcePinNotAllowedAndH } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMemoryWhenReadOnlyPointerCausesPinningFailWithEfaultThenPopulateOsHandlesMarksFragmentsToFree) { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager.get()); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } + ASSERT_NE(nullptr, memoryManager.get()); + ASSERT_NE(nullptr, memoryManager->getPinBB()); mock->reset(); @@ -2761,7 +2831,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem handleStorage.fragmentStorageData[2].cpuPtr = reinterpret_cast(0x4000); handleStorage.fragmentStorageData[2].fragmentSize = 4096; - auto result = testedMemoryManager->populateOsHandles(handleStorage); + auto result = memoryManager->populateOsHandles(handleStorage); EXPECT_EQ(MemoryManager::AllocationStatus::InvalidHostPointer, result); mock->testIoctls(); @@ -2777,13 +2847,17 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem handleStorage.fragmentStorageData[1].freeTheFragment = true; handleStorage.fragmentStorageData[2].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); mock->ioctl_res_ext = &mock->NONE; } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMemoryWhenReadOnlyPointerCausesPinningFailWithEfaultThenPopulateOsHandlesDoesNotStoreTheFragments) { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } + ASSERT_NE(nullptr, memoryManager->getPinBB()); mock->reset(); @@ -2807,12 +2881,12 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem handleStorage.fragmentStorageData[2].cpuPtr = reinterpret_cast(0x4000); handleStorage.fragmentStorageData[2].fragmentSize = 4096; - auto result = testedMemoryManager->populateOsHandles(handleStorage); + auto result = memoryManager->populateOsHandles(handleStorage); EXPECT_EQ(MemoryManager::AllocationStatus::InvalidHostPointer, result); mock->testIoctls(); - auto hostPtrManager = static_cast(testedMemoryManager->getHostPtrManager()); + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); EXPECT_EQ(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[1].cpuPtr)); @@ -2822,13 +2896,17 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem handleStorage.fragmentStorageData[1].freeTheFragment = true; handleStorage.fragmentStorageData[2].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); mock->ioctl_res_ext = &mock->NONE; } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMemoryWhenPopulateOsHandlesSucceedsThenFragmentIsStoredInHostPtrManager) { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + memoryManager->registeredEngines = EngineControlContainer{this->device->engines}; + for (auto engine : memoryManager->registeredEngines) { + engine.osContext->incRefInternal(); + } + ASSERT_NE(nullptr, memoryManager->getPinBB()); mock->reset(); mock->ioctl_expected.gemUserptr = 1; @@ -2839,22 +2917,22 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem handleStorage.fragmentStorageData[0].cpuPtr = reinterpret_cast(0x1000); handleStorage.fragmentStorageData[0].fragmentSize = 4096; - auto result = testedMemoryManager->populateOsHandles(handleStorage); + auto result = memoryManager->populateOsHandles(handleStorage); EXPECT_EQ(MemoryManager::AllocationStatus::Success, result); mock->testIoctls(); - auto hostPtrManager = static_cast(testedMemoryManager->getHostPtrManager()); + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); EXPECT_EQ(1u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[0].cpuPtr)); handleStorage.fragmentStorageData[0].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDrmMemoryManagerWhenCleanOsHandlesDeletesHandleDataThenOsHandleStorageAndResidencyIsSetToNullptr) { - std::unique_ptr testedMemoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); - ASSERT_NE(nullptr, testedMemoryManager->getPinBB()); + std::unique_ptr memoryManager(new TestedDrmMemoryManager(false, false, true, *executionEnvironment)); + ASSERT_NE(nullptr, memoryManager->getPinBB()); OsHandleStorage handleStorage; handleStorage.fragmentStorageData[0].osHandleStorage = new OsHandle(); @@ -2870,7 +2948,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDrmMemoryManagerWhenCl handleStorage.fragmentStorageData[0].freeTheFragment = true; handleStorage.fragmentStorageData[1].freeTheFragment = true; - testedMemoryManager->cleanOsHandles(handleStorage); + memoryManager->cleanOsHandles(handleStorage); for (uint32_t i = 0; i < 2; i++) { EXPECT_EQ(nullptr, handleStorage.fragmentStorageData[i].osHandleStorage); diff --git a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp index 354786ffc4..6dc5c5ba9c 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -1627,7 +1627,6 @@ TEST(WddmMemoryManagerCleanupTest, givenUsedTagAllocationInWddmMemoryManagerWhen executionEnvironment.memoryManager = std::make_unique(executionEnvironment); auto osContext = executionEnvironment.memoryManager->createAndRegisterOsContext(csr, aub_stream::ENGINE_RCS, 1, preemptionMode, false); csr->setupContext(*osContext); - EXPECT_EQ(csr, executionEnvironment.memoryManager->getDefaultCommandStreamReceiver(0)); auto tagAllocator = csr->getEventPerfCountAllocator(100); auto allocation = tagAllocator->getTag()->getBaseGraphicsAllocation();