Skip to content

Commit f1c24dd

Browse files
jchodorCompute-Runtime-Automation
authored andcommitted
WDDM - guard against duplicated host ptr allocs
Signed-off-by: Jaroslaw Chodor <[email protected]>
1 parent 071bb13 commit f1c24dd

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

opencl/test/unit_test/command_stream/command_stream_receiver_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,22 @@ TEST_F(CreateAllocationForHostSurfaceTest, givenTemporaryAllocationWhenCreateAll
11131113
EXPECT_EQ(allocationPtr, hostSurfaceAllocationPtr);
11141114
}
11151115

1116+
TEST_F(CreateAllocationForHostSurfaceTest, whenCreatingAllocationFromHostPtrSurfaceThenLockMutex) {
1117+
const char memory[8] = {1, 2, 3, 4, 5, 6, 7, 8};
1118+
size_t size = sizeof(memory);
1119+
HostPtrSurface surface(const_cast<char *>(memory), size, true);
1120+
1121+
MockExecutionEnvironment executionEnvironment;
1122+
executionEnvironment.initializeMemoryManager();
1123+
DeviceBitfield deviceBitfield(1);
1124+
MockCommandStreamReceiver commandStreamReceiver(executionEnvironment, 0u, deviceBitfield);
1125+
auto osContext = executionEnvironment.memoryManager->createAndRegisterOsContext(&commandStreamReceiver, EngineDescriptorHelper::getDefaultDescriptor(deviceBitfield));
1126+
commandStreamReceiver.osContext = osContext;
1127+
EXPECT_EQ(0, commandStreamReceiver.hostPtrSurfaceCreationMutexLockCount);
1128+
commandStreamReceiver.createAllocationForHostSurface(surface, true);
1129+
EXPECT_EQ(1, commandStreamReceiver.hostPtrSurfaceCreationMutexLockCount);
1130+
}
1131+
11161132
TEST_F(CreateAllocationForHostSurfaceTest, givenReadOnlyHostPointerWhenAllocationForHostSurfaceWithPtrCopyAllowedIsCreatedThenCopyAllocationIsCreatedAndMemoryCopied) {
11171133
const char memory[8] = {1, 2, 3, 4, 5, 6, 7, 8};
11181134
size_t size = sizeof(memory);

shared/source/command_stream/command_stream_receiver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,14 @@ bool CommandStreamReceiver::createPreemptionAllocation() {
601601
std::unique_lock<CommandStreamReceiver::MutexType> CommandStreamReceiver::obtainUniqueOwnership() {
602602
return std::unique_lock<CommandStreamReceiver::MutexType>(this->ownershipMutex);
603603
}
604+
std::unique_lock<CommandStreamReceiver::MutexType> CommandStreamReceiver::obtainHostPtrSurfaceCreationLock() {
605+
return std::unique_lock<CommandStreamReceiver::MutexType>(this->hostPtrSurfaceCreationMutex);
606+
}
604607
AllocationsList &CommandStreamReceiver::getTemporaryAllocations() { return internalAllocationStorage->getTemporaryAllocations(); }
605608
AllocationsList &CommandStreamReceiver::getAllocationsForReuse() { return internalAllocationStorage->getAllocationsForReuse(); }
606609

607610
bool CommandStreamReceiver::createAllocationForHostSurface(HostPtrSurface &surface, bool requiresL3Flush) {
611+
std::unique_lock<decltype(hostPtrSurfaceCreationMutex)> lock = this->obtainHostPtrSurfaceCreationLock();
608612
auto allocation = internalAllocationStorage->obtainTemporaryAllocationWithPtr(surface.getSurfaceSize(), surface.getMemoryPointer(), GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR);
609613

610614
if (allocation == nullptr) {

shared/source/command_stream/command_stream_receiver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class CommandStreamReceiver {
281281
void printDeviceIndex();
282282
void checkForNewResources(uint32_t submittedTaskCount, uint32_t allocationTaskCount, GraphicsAllocation &gfxAllocation);
283283
bool checkImplicitFlushForGpuIdle();
284+
MOCKABLE_VIRTUAL std::unique_lock<MutexType> obtainHostPtrSurfaceCreationLock();
284285

285286
std::unique_ptr<FlushStampTracker> flushStamp;
286287
std::unique_ptr<SubmissionAggregator> submissionAggregator;
@@ -297,6 +298,7 @@ class CommandStreamReceiver {
297298
ResidencyContainer residencyAllocations;
298299
ResidencyContainer evictionAllocations;
299300
MutexType ownershipMutex;
301+
MutexType hostPtrSurfaceCreationMutex;
300302
ExecutionEnvironment &executionEnvironment;
301303

302304
LinearStream commandStream;

shared/source/memory_manager/allocations_list.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ AllocationsList::AllocationsList()
2828
std::unique_ptr<GraphicsAllocation> AllocationsList::detachAllocation(size_t requiredMinimalSize, const void *requiredPtr, CommandStreamReceiver *commandStreamReceiver, GraphicsAllocation::AllocationType allocationType) {
2929
ReusableAllocationRequirements req;
3030
req.requiredMinimalSize = requiredMinimalSize;
31-
commandStreamReceiver == nullptr ? req.csrTagAddress = nullptr : req.csrTagAddress = commandStreamReceiver->getTagAddress();
31+
req.csrTagAddress = (commandStreamReceiver == nullptr) ? nullptr : commandStreamReceiver->getTagAddress();
3232
req.allocationType = allocationType;
33-
commandStreamReceiver == nullptr ? req.contextId = UINT32_MAX : req.contextId = commandStreamReceiver->getOsContext().getContextId();
33+
req.contextId = (commandStreamReceiver == nullptr) ? UINT32_MAX : commandStreamReceiver->getOsContext().getContextId();
3434
req.requiredPtr = requiredPtr;
3535
GraphicsAllocation *a = nullptr;
3636
GraphicsAllocation *retAlloc = processLocked<AllocationsList, &AllocationsList::detachAllocationImpl>(a, static_cast<void *>(&req));

shared/test/common/mocks/mock_command_stream_receiver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class MockCommandStreamReceiver : public CommandStreamReceiver {
117117
makeResidentCalledTimes++;
118118
}
119119

120+
std::unique_lock<CommandStreamReceiver::MutexType> obtainHostPtrSurfaceCreationLock() override {
121+
++hostPtrSurfaceCreationMutexLockCount;
122+
return CommandStreamReceiver::obtainHostPtrSurfaceCreationLock();
123+
}
124+
120125
void postInitFlagsSetup() override {}
121126

122127
std::vector<char> instructionHeapReserveredData;
@@ -131,6 +136,7 @@ class MockCommandStreamReceiver : public CommandStreamReceiver {
131136
bool callParentGetTagAddress = true;
132137
bool createPreemptionAllocationReturn = true;
133138
bool createPreemptionAllocationParentCall = false;
139+
int hostPtrSurfaceCreationMutexLockCount = 0;
134140
};
135141

136142
template <typename GfxFamily>

0 commit comments

Comments
 (0)