Skip to content

Commit e721ff1

Browse files
Add ULT for DrmMemoryManager
- ensure DrmMemoryManager::releaseGpuRange() calls GmmHelper::decanonize() before pass gpuAddress to GfxPartition::freeGpuAddressRange() Related-To: NEO-2877, NEO-3530 Change-Id: I6f6b745a8f9262c6980b9ddd32c70d376fa49726 Signed-off-by: Venevtsev, Igor <igor.venevtsev@intel.com>
1 parent 5f4cf2a commit e721ff1

File tree

10 files changed

+89
-66
lines changed

10 files changed

+89
-66
lines changed

runtime/memory_manager/gfx_partition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ constexpr auto internalHeapIndex = is32bit ? HeapIndex::HEAP_INTERNAL : HeapInde
3131
class GfxPartition {
3232
public:
3333
GfxPartition() {}
34-
~GfxPartition();
34+
MOCKABLE_VIRTUAL ~GfxPartition();
3535

3636
void init(uint64_t gpuAddressSpace, size_t cpuAddressRangeSizeToReserve);
3737

@@ -47,7 +47,7 @@ class GfxPartition {
4747
getHeap(heapIndex).free(ptr, size);
4848
}
4949

50-
void freeGpuAddressRange(uint64_t ptr, size_t size);
50+
MOCKABLE_VIRTUAL void freeGpuAddressRange(uint64_t ptr, size_t size);
5151

5252
uint64_t getHeapBase(HeapIndex heapIndex) {
5353
return getHeap(heapIndex).getBase();

runtime/memory_manager/memory_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ MemoryManager::MemoryManager(ExecutionEnvironment &executionEnvironment) : execu
4242
this->enable64kbpages = DebugManager.flags.Enable64kbpages.get() != 0;
4343
}
4444
localMemoryUsageBankSelector.reset(new LocalMemoryUsageBankSelector(getBanksCount()));
45+
gfxPartition = std::make_unique<GfxPartition>();
4546
}
4647

4748
MemoryManager::~MemoryManager() {

runtime/memory_manager/memory_manager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class MemoryManager {
106106
virtual uint64_t getLocalMemorySize() = 0;
107107

108108
uint64_t getMaxApplicationAddress() { return is64bit ? MemoryConstants::max64BitAppAddress : MemoryConstants::max32BitAppAddress; };
109-
uint64_t getInternalHeapBaseAddress() { return gfxPartition.getHeapBase(internalHeapIndex); }
110-
uint64_t getExternalHeapBaseAddress() { return gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL); }
109+
uint64_t getInternalHeapBaseAddress() { return gfxPartition->getHeapBase(internalHeapIndex); }
110+
uint64_t getExternalHeapBaseAddress() { return gfxPartition->getHeapBase(HeapIndex::HEAP_EXTERNAL); }
111111

112-
bool isLimitedRange() { return gfxPartition.isLimitedRange(); }
112+
bool isLimitedRange() { return gfxPartition->isLimitedRange(); }
113113

114114
bool peek64kbPagesEnabled() const { return enable64kbpages; }
115115
bool peekForce32BitAllocations() const { return force32bitAllocations; }
@@ -222,7 +222,7 @@ class MemoryManager {
222222
uint32_t latestContextId = std::numeric_limits<uint32_t>::max();
223223
uint32_t defaultEngineIndex = 0;
224224
std::unique_ptr<DeferredDeleter> multiContextResourceDestructor;
225-
GfxPartition gfxPartition;
225+
std::unique_ptr<GfxPartition> gfxPartition;
226226
std::unique_ptr<LocalMemoryUsageBankSelector> localMemoryUsageBankSelector;
227227
};
228228

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ OsAgnosticMemoryManager::OsAgnosticMemoryManager(bool aubUsage, ExecutionEnviron
2929

3030
// 4 x sizeof(Heap32) + 2 x sizeof(Standard/Standard64k)
3131
size_t reservedCpuAddressRangeSize = is64bit ? (4 * 4 + 2 * (aubUsage ? 32 : 4)) * GB : 0;
32-
gfxPartition.init(gpuAddressSpace, reservedCpuAddressRangeSize);
32+
gfxPartition->init(gpuAddressSpace, reservedCpuAddressRangeSize);
3333
}
3434

3535
OsAgnosticMemoryManager::~OsAgnosticMemoryManager() {
@@ -104,7 +104,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con
104104
auto heap = useInternal32BitAllocator(allocationData.type) ? internalHeapIndex : HeapIndex::HEAP_EXTERNAL;
105105
if (allocationData.hostPtr) {
106106
auto allocationSize = alignSizeWholePage(allocationData.hostPtr, allocationData.size);
107-
auto gpuVirtualAddress = gfxPartition.heapAllocate(heap, allocationSize);
107+
auto gpuVirtualAddress = gfxPartition->heapAllocate(heap, allocationSize);
108108
if (!gpuVirtualAddress) {
109109
return nullptr;
110110
}
@@ -114,7 +114,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con
114114
allocationData.size, counter, MemoryPool::System4KBPagesWith32BitGpuAddressing, false, false, false);
115115

116116
memAlloc->set32BitAllocation(true);
117-
memAlloc->setGpuBaseAddress(GmmHelper::canonize(gfxPartition.getHeapBase(heap)));
117+
memAlloc->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(heap)));
118118
memAlloc->sizeToFree = allocationSize;
119119

120120
counter++;
@@ -123,7 +123,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con
123123

124124
auto allocationSize = alignUp(allocationData.size, MemoryConstants::pageSize);
125125
void *ptrAlloc = nullptr;
126-
auto gpuAddress = gfxPartition.heapAllocate(heap, allocationSize);
126+
auto gpuAddress = gfxPartition->heapAllocate(heap, allocationSize);
127127

128128
if (allocationData.size < 0xfffff000) {
129129
if (fakeBigAllocations) {
@@ -140,7 +140,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con
140140
false, false);
141141

142142
memoryAllocation->set32BitAllocation(true);
143-
memoryAllocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition.getHeapBase(heap)));
143+
memoryAllocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(heap)));
144144
memoryAllocation->sizeToFree = allocationSize;
145145
}
146146
counter++;
@@ -206,7 +206,7 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo
206206

207207
if (sizeToFree) {
208208
auto gpuAddressToFree = GmmHelper::decanonize(memoryAllocation->getGpuAddress()) & ~MemoryConstants::pageMask;
209-
gfxPartition.freeGpuAddressRange(gpuAddressToFree, sizeToFree);
209+
gfxPartition->freeGpuAddressRange(gpuAddressToFree, sizeToFree);
210210
}
211211

212212
alignedFreeWrapper(gfxAllocation->getDriverAllocatedCpuPtr());
@@ -312,12 +312,12 @@ MemoryAllocation *OsAgnosticMemoryManager::createMemoryAllocation(GraphicsAlloca
312312

313313
auto heap = (force32bitAllocations || requireSpecificBitness) ? HeapIndex::HEAP_EXTERNAL : HeapIndex::HEAP_STANDARD;
314314

315-
uint64_t limitedGpuAddress = gfxPartition.heapAllocate(heap, alignedSize);
315+
uint64_t limitedGpuAddress = gfxPartition->heapAllocate(heap, alignedSize);
316316

317317
auto memoryAllocation = new MemoryAllocation(allocationType, driverAllocatedCpuPointer, pMem, limitedGpuAddress, memSize,
318318
count, pool, multiOsContextCapable, uncacheable, flushL3Required);
319319

320-
memoryAllocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition.getHeapBase(heap)));
320+
memoryAllocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(heap)));
321321
memoryAllocation->sizeToFree = alignedSize;
322322

323323
return memoryAllocation;

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ DrmMemoryManager::DrmMemoryManager(gemCloseWorkerMode mode,
3939
forcePinEnabled(forcePinAllowed),
4040
validateHostPtrMemory(validateHostPtrMemory) {
4141
supportsMultiStorageResources = false;
42-
gfxPartition.init(platformDevices[0]->capabilityTable.gpuAddressSpace, getSizeToReserve());
42+
gfxPartition->init(platformDevices[0]->capabilityTable.gpuAddressSpace, getSizeToReserve());
4343
MemoryManager::virtualPaddingAvailable = true;
4444
if (mode != gemCloseWorkerMode::gemCloseWorkerInactive) {
4545
gemCloseWorker.reset(new DrmGemCloseWorker(*this));
@@ -130,12 +130,12 @@ uint32_t DrmMemoryManager::unreference(NEO::BufferObject *bo, bool synchronousDe
130130
uint64_t DrmMemoryManager::acquireGpuRange(size_t &size, StorageAllocatorType &storageType, bool specificBitness) {
131131
if (specificBitness && this->force32bitAllocations) {
132132
storageType = BIT32_ALLOCATOR_EXTERNAL;
133-
return GmmHelper::canonize(gfxPartition.heapAllocate(HeapIndex::HEAP_EXTERNAL, size));
133+
return GmmHelper::canonize(gfxPartition->heapAllocate(HeapIndex::HEAP_EXTERNAL, size));
134134
}
135135

136136
if (isLimitedRange()) {
137137
storageType = INTERNAL_ALLOCATOR_WITH_DYNAMIC_BITRANGE;
138-
return GmmHelper::canonize(gfxPartition.heapAllocate(HeapIndex::HEAP_STANDARD, size));
138+
return GmmHelper::canonize(gfxPartition->heapAllocate(HeapIndex::HEAP_STANDARD, size));
139139
}
140140

141141
storageType = MMAP_ALLOCATOR;
@@ -150,7 +150,7 @@ void DrmMemoryManager::releaseGpuRange(void *address, size_t unmapSize, StorageA
150150

151151
uint64_t graphicsAddress = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(address));
152152
graphicsAddress = GmmHelper::decanonize(graphicsAddress);
153-
gfxPartition.freeGpuAddressRange(graphicsAddress, unmapSize);
153+
gfxPartition->freeGpuAddressRange(graphicsAddress, unmapSize);
154154
}
155155

156156
NEO::BufferObject *DrmMemoryManager::allocUserptr(uintptr_t address, size_t size, uint64_t flags) {
@@ -228,7 +228,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithAlignment(const Alloc
228228
if (allocationData.type == GraphicsAllocation::AllocationType::SVM_CPU) {
229229
//add 2MB padding in case reserved addr is not 2MB aligned
230230
reserveSizeAligned = alignUp(cSize, cAlignment) + cAlignment;
231-
reservedGpuAddress = GmmHelper::canonize(gfxPartition.heapAllocate(HeapIndex::HEAP_STANDARD, reserveSizeAligned));
231+
reservedGpuAddress = GmmHelper::canonize(gfxPartition->heapAllocate(HeapIndex::HEAP_STANDARD, reserveSizeAligned));
232232
if (!reservedGpuAddress) {
233233
bo->close();
234234
delete bo;
@@ -346,7 +346,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
346346
uintptr_t inputPtr = reinterpret_cast<uintptr_t>(allocationData.hostPtr);
347347
auto allocationSize = alignSizeWholePage(allocationData.hostPtr, allocationData.size);
348348
auto realAllocationSize = allocationSize;
349-
auto gpuVirtualAddress = gfxPartition.heapAllocate(allocatorToUse, realAllocationSize);
349+
auto gpuVirtualAddress = gfxPartition->heapAllocate(allocatorToUse, realAllocationSize);
350350
if (!gpuVirtualAddress) {
351351
return nullptr;
352352
}
@@ -355,7 +355,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
355355

356356
BufferObject *bo = allocUserptr(alignedUserPointer, allocationSize, 0);
357357
if (!bo) {
358-
gfxPartition.heapFree(allocatorToUse, gpuVirtualAddress, realAllocationSize);
358+
gfxPartition->heapFree(allocatorToUse, gpuVirtualAddress, realAllocationSize);
359359
return nullptr;
360360
}
361361

@@ -366,13 +366,13 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
366366
auto allocation = new DrmAllocation(allocationData.type, bo, const_cast<void *>(allocationData.hostPtr), GmmHelper::canonize(ptrOffset(gpuVirtualAddress, inputPointerOffset)),
367367
allocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing, false);
368368
allocation->set32BitAllocation(true);
369-
allocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition.getHeapBase(allocatorToUse)));
369+
allocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(allocatorToUse)));
370370
return allocation;
371371
}
372372

373373
size_t alignedAllocationSize = alignUp(allocationData.size, MemoryConstants::pageSize);
374374
auto allocationSize = alignedAllocationSize;
375-
auto res = gfxPartition.heapAllocate(allocatorToUse, allocationSize);
375+
auto res = gfxPartition->heapAllocate(allocatorToUse, allocationSize);
376376

377377
if (!res) {
378378
return nullptr;
@@ -381,15 +381,15 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
381381
auto ptrAlloc = alignedMallocWrapper(alignedAllocationSize, MemoryConstants::allocationAlignment);
382382

383383
if (!ptrAlloc) {
384-
gfxPartition.heapFree(allocatorToUse, res, allocationSize);
384+
gfxPartition->heapFree(allocatorToUse, res, allocationSize);
385385
return nullptr;
386386
}
387387

388388
BufferObject *bo = allocUserptr(reinterpret_cast<uintptr_t>(ptrAlloc), alignedAllocationSize, 0);
389389

390390
if (!bo) {
391391
alignedFreeWrapper(ptrAlloc);
392-
gfxPartition.heapFree(allocatorToUse, res, allocationSize);
392+
gfxPartition->heapFree(allocatorToUse, res, allocationSize);
393393
return nullptr;
394394
}
395395

@@ -404,7 +404,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
404404
MemoryPool::System4KBPagesWith32BitGpuAddressing, false);
405405

406406
allocation->set32BitAllocation(true);
407-
allocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition.getHeapBase(allocatorToUse)));
407+
allocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(allocatorToUse)));
408408
allocation->setDriverAllocatedCpuPtr(ptrAlloc);
409409
return allocation;
410410
}
@@ -480,7 +480,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
480480
drmAllocation->set32BitAllocation(true);
481481
drmAllocation->setGpuBaseAddress(getExternalHeapBaseAddress());
482482
} else if (isLimitedRange()) {
483-
drmAllocation->setGpuBaseAddress(gfxPartition.getHeapBase(HeapIndex::HEAP_STANDARD));
483+
drmAllocation->setGpuBaseAddress(gfxPartition->getHeapBase(HeapIndex::HEAP_STANDARD));
484484
}
485485

486486
if (properties.imgInfo) {
@@ -570,7 +570,7 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
570570
void *reserveAddress = gfxAllocation->getReservedAddressPtr();
571571
if (reserveAddress) {
572572
auto gpuAddressToFree = GmmHelper::decanonize(reinterpret_cast<uint64_t>(reserveAddress));
573-
gfxPartition.freeGpuAddressRange(gpuAddressToFree, gfxAllocation->getReservedAddressSize());
573+
gfxPartition->freeGpuAddressRange(gpuAddressToFree, gfxAllocation->getReservedAddressSize());
574574
}
575575
delete gfxAllocation;
576576

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ WddmMemoryManager::WddmMemoryManager(ExecutionEnvironment &executionEnvironment)
4545
if (asyncDeleterEnabled)
4646
deferredDeleter = createDeferredDeleter();
4747
mallocRestrictions.minAddress = wddm->getWddmMinAddress();
48-
wddm->initGfxPartition(gfxPartition);
48+
wddm->initGfxPartition(*gfxPartition);
4949
}
5050

5151
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) {
@@ -504,13 +504,13 @@ bool WddmMemoryManager::mapGpuVaForOneHandleAllocation(WddmAllocation *allocatio
504504
addressToMap = allocation->reservedGpuVirtualAddress;
505505
}
506506
auto status = wddm->mapGpuVirtualAddress(allocation->getDefaultGmm(), allocation->getDefaultHandle(),
507-
gfxPartition.getHeapMinimalAddress(heapIndex), gfxPartition.getHeapLimit(heapIndex),
507+
gfxPartition->getHeapMinimalAddress(heapIndex), gfxPartition->getHeapLimit(heapIndex),
508508
addressToMap, allocation->getGpuAddressToModify());
509509

510510
if (!status && deferredDeleter) {
511511
deferredDeleter->drain(true);
512512
status = wddm->mapGpuVirtualAddress(allocation->getDefaultGmm(), allocation->getDefaultHandle(),
513-
gfxPartition.getHeapMinimalAddress(heapIndex), gfxPartition.getHeapLimit(heapIndex),
513+
gfxPartition->getHeapMinimalAddress(heapIndex), gfxPartition->getHeapLimit(heapIndex),
514514
addressToMap, allocation->getGpuAddressToModify());
515515
}
516516
if (!status) {
@@ -542,8 +542,8 @@ void WddmMemoryManager::obtainGpuAddressIfNeeded(WddmAllocation *allocation) {
542542
if (allocation->getNumHandles() > 1u) {
543543
auto heapIndex = selectHeap(allocation, false, executionEnvironment.isFullRangeSvm());
544544
allocation->reservedSizeForGpuVirtualAddress = allocation->getAlignedSize();
545-
allocation->reservedGpuVirtualAddress = wddm->reserveGpuVirtualAddress(gfxPartition.getHeapMinimalAddress(heapIndex),
546-
gfxPartition.getHeapLimit(heapIndex),
545+
allocation->reservedGpuVirtualAddress = wddm->reserveGpuVirtualAddress(gfxPartition->getHeapMinimalAddress(heapIndex),
546+
gfxPartition->getHeapLimit(heapIndex),
547547
allocation->reservedSizeForGpuVirtualAddress);
548548
}
549549
}

unit_tests/mocks/linux/mock_drm_memory_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
5353
using DrmMemoryManager::getDefaultDrmContextId;
5454
using DrmMemoryManager::gfxPartition;
5555
using DrmMemoryManager::pinThreshold;
56+
using DrmMemoryManager::releaseGpuRange;
5657
using DrmMemoryManager::setDomainCpu;
5758
using DrmMemoryManager::sharingBufferObjects;
5859
using DrmMemoryManager::supportsMultiStorageResources;
@@ -103,7 +104,8 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
103104
}
104105

105106
DrmGemCloseWorker *getgemCloseWorker() { return this->gemCloseWorker.get(); }
106-
void forceLimitedRangeAllocator(uint64_t range) { gfxPartition.init(range, getSizeToReserve()); }
107+
void forceLimitedRangeAllocator(uint64_t range) { gfxPartition->init(range, getSizeToReserve()); }
108+
void overrideGfxPartition(GfxPartition *newGfxPartition) { gfxPartition.reset(newGfxPartition); }
107109

108110
DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, GraphicsAllocation::AllocationType allocationType) {
109111
bool allocateMemory = ptr == nullptr;

unit_tests/mocks/mock_gfx_partition.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "runtime/memory_manager/gfx_partition.h"
99

10+
#include "gmock/gmock.h"
11+
1012
using namespace NEO;
1113

1214
class MockGfxPartition : public GfxPartition {
@@ -27,5 +29,7 @@ class MockGfxPartition : public GfxPartition {
2729
return reservedCpuAddressRangeSize;
2830
}
2931

32+
MOCK_METHOD2(freeGpuAddressRange, void(uint64_t gpuAddress, size_t size));
33+
3034
static std::array<HeapIndex, static_cast<uint32_t>(HeapIndex::TOTAL_HEAPS)> allHeapNames;
3135
};

0 commit comments

Comments
 (0)