Tracking the internal allocation when creating a buffer

This fix prevents the creation of a buffer from the pointer
obtained from clEnqueueMapBuffer

Change-Id: I203f2d5263eeb02fe0d73bc9db159438af3cf1fc
This commit is contained in:
Koska, Andrzej
2018-05-08 10:00:23 +02:00
committed by sys_ocldev
parent c46f716d6c
commit fe56c57318
16 changed files with 233 additions and 11 deletions

View File

@@ -124,6 +124,9 @@ Buffer *Buffer::create(Context *context,
}
if (allocateMemory) {
memory = memoryManager->createGraphicsAllocationWithRequiredBitness(size, nullptr, true);
if (memory) {
memoryManager->addAllocationToHostPtrManager(memory);
}
if (context->isProvidingPerformanceHints()) {
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, CL_BUFFER_NEEDS_ALLOCATE_MEMORY);
}
@@ -166,6 +169,7 @@ Buffer *Buffer::create(Context *context,
isHostPtrSVM,
false);
if (!pBuffer && allocateMemory) {
memoryManager->removeAllocationFromHostPtrManager(memory);
memoryManager->freeGraphicsMemory(memory);
memory = nullptr;
}
@@ -208,6 +212,11 @@ void Buffer::checkMemory(cl_mem_flags flags,
if (flags & CL_MEM_USE_HOST_PTR) {
if (hostPtr) {
auto fragment = memMngr->hostPtrManager.getFragment(hostPtr);
if (fragment && fragment->driverAllocation) {
errcodeRet = CL_INVALID_HOST_PTR;
return;
}
if (alignUp(hostPtr, MemoryConstants::cacheLineSize) != hostPtr ||
alignUp(size, MemoryConstants::cacheLineSize) != size ||
minAddress > reinterpret_cast<uintptr_t>(hostPtr) ||

View File

@@ -72,6 +72,7 @@ MemObj::~MemObj() {
peekSharingHandler()->releaseReusedGraphicsAllocation();
}
if (graphicsAllocation && !associatedMemObject && !isObjectRedescribed && !isHostPtrSVM && graphicsAllocation->peekReuseCount() == 0) {
memoryManager->removeAllocationFromHostPtrManager(graphicsAllocation);
bool doAsyncDestrucions = DebugManager.flags.EnableAsyncDestroyAllocations.get();
if (!doAsyncDestrucions) {
needWait = true;

View File

@@ -76,6 +76,7 @@ struct FragmentStorage {
int refCount = 0;
OsHandle *osInternalStorage = nullptr;
ResidencyData *residency = nullptr;
bool driverAllocation = false;
};
struct AllocationStorageData {

View File

@@ -90,6 +90,9 @@ class MemoryManager {
virtual ~MemoryManager();
MOCKABLE_VIRTUAL void *allocateSystemMemory(size_t size, size_t alignment);
virtual void addAllocationToHostPtrManager(GraphicsAllocation *memory) = 0;
virtual void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) = 0;
virtual GraphicsAllocation *allocateGraphicsMemory(size_t size) {
return allocateGraphicsMemory(size, static_cast<size_t>(0u));
}

View File

@@ -110,6 +110,26 @@ GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocationFromSharedH
return graphicsAllocation;
}
void OsAgnosticMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) {
FragmentStorage fragment = {};
fragment.driverAllocation = true;
fragment.fragmentCpuPointer = gfxAllocation->getUnderlyingBuffer();
fragment.fragmentSize = alignUp(gfxAllocation->getUnderlyingBufferSize(), MemoryConstants::pageSize);
fragment.osInternalStorage = new OsHandle();
hostPtrManager.storeFragment(fragment);
}
void OsAgnosticMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) {
auto buffer = gfxAllocation->getUnderlyingBuffer();
auto fragment = hostPtrManager.getFragment(buffer);
if (fragment && fragment->driverAllocation) {
OsHandle *osStorageToRelease = fragment->osInternalStorage;
if (hostPtrManager.releaseHostPtr(buffer)) {
delete osStorageToRelease;
}
}
}
void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
if (gfxAllocation == nullptr)
return;
@@ -166,7 +186,7 @@ MemoryManager::AllocationStatus OsAgnosticMemoryManager::populateOsHandles(OsHan
handleStorage.fragmentStorageData[i].osHandleStorage = new OsHandle();
handleStorage.fragmentStorageData[i].residency = new ResidencyData();
FragmentStorage newFragment;
FragmentStorage newFragment = {};
newFragment.fragmentCpuPointer = const_cast<void *>(handleStorage.fragmentStorageData[i].cpuPtr);
newFragment.fragmentSize = handleStorage.fragmentStorageData[i].fragmentSize;
newFragment.osInternalStorage = handleStorage.fragmentStorageData[i].osHandleStorage;

View File

@@ -65,6 +65,8 @@ class OsAgnosticMemoryManager : public MemoryManager {
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness, bool reuseBO) override;
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) override { return nullptr; }
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) override;
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
void *lockResource(GraphicsAllocation *graphicsAllocation) override { return nullptr; };
void unlockResource(GraphicsAllocation *graphicsAllocation) override{};

View File

@@ -412,6 +412,28 @@ GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation
return new DrmAllocation(bo, (void *)srcPtr, (uint64_t)ptrOffset(gpuRange, offset), sizeWithPadding);
}
void DrmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) {
DrmAllocation *drmMemory = static_cast<DrmAllocation *>(gfxAllocation);
FragmentStorage fragment = {};
fragment.driverAllocation = true;
fragment.fragmentCpuPointer = gfxAllocation->getUnderlyingBuffer();
fragment.fragmentSize = alignUp(gfxAllocation->getUnderlyingBufferSize(), MemoryConstants::pageSize);
fragment.osInternalStorage = new OsHandle();
fragment.osInternalStorage->bo = drmMemory->getBO();
hostPtrManager.storeFragment(fragment);
}
void DrmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) {
auto buffer = gfxAllocation->getUnderlyingBuffer();
auto fragment = hostPtrManager.getFragment(buffer);
if (fragment && fragment->driverAllocation) {
OsHandle *osStorageToRelease = fragment->osInternalStorage;
if (hostPtrManager.releaseHostPtr(buffer)) {
delete osStorageToRelease;
}
}
}
void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
DrmAllocation *input;
input = static_cast<DrmAllocation *>(gfxAllocation);

View File

@@ -40,7 +40,8 @@ class DrmMemoryManager : public MemoryManager {
~DrmMemoryManager() override;
BufferObject *getPinBB() const;
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
DrmAllocation *allocateGraphicsMemory(size_t size, size_t alignment) override {
return allocateGraphicsMemory(size, alignment, false, false);

View File

@@ -248,6 +248,32 @@ GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromNTHandle(void
return createAllocationFromHandle((osHandle)((UINT_PTR)handle), false, true);
}
void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) {
WddmAllocation *wddmMemory = static_cast<WddmAllocation *>(gfxAllocation);
FragmentStorage fragment = {};
fragment.driverAllocation = true;
fragment.fragmentCpuPointer = gfxAllocation->getUnderlyingBuffer();
fragment.fragmentSize = alignUp(gfxAllocation->getUnderlyingBufferSize(), MemoryConstants::pageSize);
fragment.osInternalStorage = new OsHandle();
fragment.osInternalStorage->gpuPtr = gfxAllocation->getGpuAddress();
fragment.osInternalStorage->handle = gfxAllocation->peekSharedHandle();
fragment.osInternalStorage->gmm = gfxAllocation->gmm;
fragment.residency = &wddmMemory->getResidencyData();
hostPtrManager.storeFragment(fragment);
}
void WddmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) {
auto buffer = gfxAllocation->getUnderlyingBuffer();
auto fragment = hostPtrManager.getFragment(buffer);
if (fragment && fragment->driverAllocation) {
OsHandle *osStorageToRelease = fragment->osInternalStorage;
if (hostPtrManager.releaseHostPtr(buffer)) {
delete osStorageToRelease;
}
}
}
void *WddmMemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
return wddm->lockResource(static_cast<WddmAllocation *>(graphicsAllocation));
};
@@ -277,6 +303,7 @@ void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation
}
delete input->gmm;
}
if (input->peekSharedHandle() == false &&
input->cpuPtrAllocated == false &&
input->fragmentsStorage.fragmentCount > 0) {

View File

@@ -53,6 +53,8 @@ class WddmMemoryManager : public MemoryManager {
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness, bool reuseBO) override;
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) override;
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) override;
void addAllocationToHostPtrManager(GraphicsAllocation *memory) override;
void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) override;
void *lockResource(GraphicsAllocation *graphicsAllocation) override;
void unlockResource(GraphicsAllocation *graphicsAllocation) override;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -104,6 +104,28 @@ TEST_F(clEnqueueMapBufferTests, validInputs_returnsSuccess) {
clReleaseEvent(eventReturned);
}
TEST_F(clEnqueueMapBufferTests, GivenMappedPointerWhenCreateBufferFormThisPointerThenReturnInvalidHostPointer) {
unsigned int bufferSize = 16;
cl_mem buffer = clCreateBuffer(pContext, CL_MEM_READ_WRITE, bufferSize, nullptr, &retVal);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_NE(nullptr, buffer);
void *hostPointer = clEnqueueMapBuffer(pCommandQueue, buffer, CL_TRUE, CL_MAP_READ, 0, bufferSize, 0, nullptr, nullptr, &retVal);
EXPECT_NE(nullptr, hostPointer);
EXPECT_EQ(CL_SUCCESS, retVal);
retVal = clEnqueueUnmapMemObject(pCommandQueue, buffer, hostPointer, 0, NULL, NULL);
EXPECT_EQ(CL_SUCCESS, retVal);
auto bufferFromHostPtr = clCreateBuffer(pContext, CL_MEM_USE_HOST_PTR, bufferSize, hostPointer, &retVal);
EXPECT_EQ(CL_INVALID_HOST_PTR, retVal);
EXPECT_EQ(nullptr, bufferFromHostPtr);
retVal = clReleaseMemObject(buffer);
EXPECT_EQ(CL_SUCCESS, retVal);
}
class EnqueueMapBufferFlagsTest : public api_fixture,
public testing::TestWithParam<uint64_t /*cl_mem_flags*/> {
public:

View File

@@ -512,9 +512,9 @@ HWTEST_F(EnqueueReadBufferRectTest, givenInOrderQueueAndMemObjWithOffsetPointThe
HWTEST_F(EnqueueReadBufferRectTest, givenInOrderQueueAndMemObjWithOffsetPointDiffrentStorageWithHostWhenReadBufferIsExecutedThenTaskLevelShouldBeIncreased) {
cl_int retVal = CL_SUCCESS;
void *ptr = buffer->getCpuAddressForMemoryTransfer();
size_t bufferOrigin[] = {50, 50, 0};
size_t hostOrigin[] = {10, 10, 0};
size_t region[] = {50, 50, 1};
size_t bufferOrigin[] = {0, 0, 0};
size_t hostOrigin[] = {1, 1, 0};
size_t region[] = {1, 1, 1};
retVal = pCmdQ->enqueueReadBufferRect(
buffer.get(),
CL_FALSE,
@@ -539,7 +539,7 @@ HWTEST_F(EnqueueReadBufferRectTest, givenInOrderQueueAndDstPtrEqualSrcPtrAndNonZ
void *ptr = nonZeroCopyBuffer->getCpuAddressForMemoryTransfer();
size_t bufferOrigin[] = {0, 0, 0};
size_t hostOrigin[] = {0, 0, 0};
size_t region[] = {50, 50, 1};
size_t region[] = {1, 1, 1};
retVal = pCmdQ->enqueueReadBufferRect(
nonZeroCopyBuffer.get(),
CL_FALSE,

View File

@@ -509,9 +509,9 @@ HWTEST_F(EnqueueWriteBufferRectTest, givenInOrderQueueAndMemObjWithOffsetPointTh
HWTEST_F(EnqueueWriteBufferRectTest, givenInOrderQueueAndMemObjWithOffsetPointDiffrentStorageWithHostWhenWriteBufferIsExecutedThenTaskLevelShouldBeIncreased) {
cl_int retVal = CL_SUCCESS;
void *ptr = buffer->getCpuAddressForMemoryTransfer();
size_t bufferOrigin[] = {50, 50, 0};
size_t hostOrigin[] = {10, 10, 0};
size_t region[] = {50, 50, 1};
size_t bufferOrigin[] = {0, 0, 0};
size_t hostOrigin[] = {1, 1, 0};
size_t region[] = {1, 1, 1};
retVal = pCmdQ->enqueueWriteBufferRect(
buffer.get(),
CL_FALSE,
@@ -536,7 +536,7 @@ HWTEST_F(EnqueueWriteBufferRectTest, givenInOrderQueueAndDstPtrEqualSrcPtrAndNon
void *ptr = nonZeroCopyBuffer->getCpuAddressForMemoryTransfer();
size_t bufferOrigin[] = {0, 0, 0};
size_t hostOrigin[] = {0, 0, 0};
size_t region[] = {50, 50, 1};
size_t region[] = {1, 1, 1};
retVal = pCmdQ->enqueueWriteBufferRect(
nonZeroCopyBuffer.get(),
CL_FALSE,

View File

@@ -149,6 +149,41 @@ TEST_F(MemoryAllocatorTest, allocateSystem) {
memoryManager->freeSystemMemory(ptr);
}
TEST_F(MemoryAllocatorTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToHostPtrManagerThenfragmentHasCorrectValues) {
void *cpuPtr = (void *)0x30000;
size_t size = 0x1000;
GraphicsAllocation gfxAllocation(cpuPtr, size);
memoryManager->addAllocationToHostPtrManager(&gfxAllocation);
auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_NE(fragment, nullptr);
EXPECT_TRUE(fragment->driverAllocation);
EXPECT_EQ(fragment->refCount, 1);
EXPECT_EQ(fragment->fragmentCpuPointer, cpuPtr);
EXPECT_EQ(fragment->fragmentSize, size);
EXPECT_NE(fragment->osInternalStorage, nullptr);
FragmentStorage fragmentStorage = {};
fragmentStorage.fragmentCpuPointer = cpuPtr;
memoryManager->hostPtrManager.storeFragment(fragmentStorage);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = false;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = true;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 1);
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment, nullptr);
}
TEST_F(MemoryAllocatorTest, allocateSystemAligned) {
unsigned int alignment = 0x100;

View File

@@ -183,6 +183,43 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDefaultDrmMemoryManger
EXPECT_NE(nullptr, memoryManager->getDrmInternal32BitAllocator());
}
TEST_F(DrmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToHostPtrManagerThenfragmentHasCorrectValues) {
void *cpuPtr = (void *)0x30000;
size_t size = 0x1000;
DrmAllocation gfxAllocation(nullptr, cpuPtr, size);
memoryManager->addAllocationToHostPtrManager(&gfxAllocation);
auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_NE(fragment, nullptr);
EXPECT_TRUE(fragment->driverAllocation);
EXPECT_EQ(fragment->refCount, 1);
EXPECT_EQ(fragment->refCount, 1);
EXPECT_EQ(fragment->fragmentCpuPointer, cpuPtr);
EXPECT_EQ(fragment->fragmentSize, size);
EXPECT_NE(fragment->osInternalStorage, nullptr);
EXPECT_EQ(fragment->osInternalStorage->bo, gfxAllocation.getBO());
FragmentStorage fragmentStorage = {};
fragmentStorage.fragmentCpuPointer = cpuPtr;
memoryManager->hostPtrManager.storeFragment(fragmentStorage);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = false;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = true;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 1);
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment, nullptr);
}
TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenforcePinAllowedWhenMemoryManagerIsCreatedThenPinBbIsCreated) {
auto memoryManager = new (std::nothrow) TestedDrmMemoryManager(this->mock, true, false);
EXPECT_NE(nullptr, memoryManager->getPinBB());

View File

@@ -80,6 +80,46 @@ HWTEST_F(WddmMemoryManagerTest, givenDefaultWddmMemoryManagerWhenAskedForVirtual
EXPECT_FALSE(memoryManager->peekVirtualPaddingSupport());
}
TEST_F(WddmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToHostPtrManagerThenfragmentHasCorrectValues) {
memoryManager.reset(new (std::nothrow) MockWddmMemoryManager(wddm));
void *cpuPtr = (void *)0x30000;
size_t size = 0x1000;
WddmAllocation gfxAllocation(cpuPtr, size, nullptr);
memoryManager->addAllocationToHostPtrManager(&gfxAllocation);
auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_NE(fragment, nullptr);
EXPECT_TRUE(fragment->driverAllocation);
EXPECT_EQ(fragment->refCount, 1);
EXPECT_EQ(fragment->fragmentCpuPointer, cpuPtr);
EXPECT_EQ(fragment->fragmentSize, size);
EXPECT_NE(fragment->osInternalStorage, nullptr);
EXPECT_EQ(fragment->osInternalStorage->gmm, gfxAllocation.gmm);
EXPECT_NE(fragment->osInternalStorage->gpuPtr, 0ULL);
EXPECT_EQ(fragment->osInternalStorage->handle, gfxAllocation.peekSharedHandle());
EXPECT_NE(fragment->residency, nullptr);
FragmentStorage fragmentStorage = {};
fragmentStorage.fragmentCpuPointer = cpuPtr;
memoryManager->hostPtrManager.storeFragment(fragmentStorage);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = false;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 2);
fragment->driverAllocation = true;
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment->refCount, 1);
memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation);
fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer());
EXPECT_EQ(fragment, nullptr);
}
HWTEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtr) {
SetUpMm<FamilyType>();
// three pages