Skip to content

Commit cdb5240

Browse files
Add new functions reserving address range to Memory Manager
Change-Id: I947203c24495c9e5a206b95bb0c69440824586b6 Signed-off-by: Zbigniew Zdanowicz <zbigniew.zdanowicz@intel.com>
1 parent 669d192 commit cdb5240

16 files changed

+110
-33
lines changed

runtime/memory_manager/graphics_allocation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ void GraphicsAllocation::setAllocationType(AllocationType allocationType) {
1919

2020
GraphicsAllocation::GraphicsAllocation(AllocationType allocationType, void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress,
2121
size_t sizeIn, MemoryPool::Type pool, bool multiOsContextCapable)
22-
: size(sizeIn),
23-
cpuPtr(cpuPtrIn),
24-
gpuBaseAddress(baseAddress),
22+
: gpuBaseAddress(baseAddress),
2523
gpuAddress(gpuAddress),
24+
size(sizeIn),
25+
cpuPtr(cpuPtrIn),
2626
memoryPool(pool),
2727
allocationType(allocationType) {
2828
allocationInfo.flags.multiOsContextCapable = multiOsContextCapable;
2929
}
3030

3131
GraphicsAllocation::GraphicsAllocation(AllocationType allocationType, void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn,
3232
MemoryPool::Type pool, bool multiOsContextCapable)
33-
: size(sizeIn),
33+
: gpuAddress(castToUint64(cpuPtrIn)),
34+
size(sizeIn),
3435
cpuPtr(cpuPtrIn),
35-
gpuAddress(castToUint64(cpuPtrIn)),
3636
memoryPool(pool),
3737
allocationType(allocationType) {
3838
sharingInfo.sharedHandle = sharedHandleIn;

runtime/memory_manager/graphics_allocation.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
165165
allocationType == AllocationType::COMMAND_BUFFER;
166166
}
167167
static StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
168+
void *getReservedAddressPtr() const {
169+
return this->reservedAddressRangeInfo.addressPtr;
170+
}
171+
size_t getReservedAddressSize() const {
172+
return this->reservedAddressRangeInfo.rangeSize;
173+
}
174+
void setReservedAddressRange(void *reserveAddress, size_t size) {
175+
this->reservedAddressRangeInfo.addressPtr = reserveAddress;
176+
this->reservedAddressRangeInfo.rangeSize = size;
177+
}
168178

169179
Gmm *getDefaultGmm() const {
170180
return getGmm(0u);
@@ -222,24 +232,29 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
222232
}
223233
};
224234

225-
uint64_t allocationOffset = 0u;
226-
void *driverAllocatedCpuPointer = nullptr;
235+
struct ReservedAddressRange {
236+
void *addressPtr = nullptr;
237+
size_t rangeSize = 0;
238+
};
227239

228-
//this variable can only be modified from SubmissionAggregator
229240
friend class SubmissionAggregator;
230-
size_t size = 0;
231-
void *cpuPtr = nullptr;
241+
242+
AllocationInfo allocationInfo;
243+
AubInfo aubInfo;
244+
SharingInfo sharingInfo;
245+
ReservedAddressRange reservedAddressRangeInfo;
246+
247+
uint64_t allocationOffset = 0u;
232248
uint64_t gpuBaseAddress = 0;
233249
uint64_t gpuAddress = 0;
250+
void *driverAllocatedCpuPointer = nullptr;
251+
size_t size = 0;
252+
void *cpuPtr = nullptr;
234253
void *lockedPtr = nullptr;
235254

236255
MemoryPool::Type memoryPool = MemoryPool::MemoryNull;
237256
AllocationType allocationType = AllocationType::UNKNOWN;
238257

239-
AllocationInfo allocationInfo;
240-
AubInfo aubInfo;
241-
SharingInfo sharingInfo;
242-
243258
std::array<UsageInfo, maxOsContextCount> usageInfos;
244259
std::atomic<uint32_t> registeredContextsNum{0};
245260
std::array<Gmm *, maxHandleCount> gmms{};

runtime/memory_manager/memory_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ class MemoryManager {
185185
virtual bool copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, const void *memoryToCopy, uint32_t sizeToCopy) const;
186186
static HeapIndex selectHeap(const GraphicsAllocation *allocation, const void *ptr, const HardwareInfo &hwInfo);
187187
static std::unique_ptr<MemoryManager> createMemoryManager(bool enable64KBpages, bool enableLocalMemory, ExecutionEnvironment &exeEnv);
188+
virtual void *reserveCpuAddressRange(size_t size) = 0;
189+
virtual void releaseReservedCpuAddressRange(void *reserved, size_t size) = 0;
188190

189191
protected:
190192
struct AllocationData {

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo
169169
}
170170

171171
alignedFreeWrapper(gfxAllocation->getDriverAllocatedCpuPtr());
172+
if (gfxAllocation->getReservedAddressPtr()) {
173+
releaseReservedCpuAddressRange(gfxAllocation->getReservedAddressPtr(), gfxAllocation->getReservedAddressSize());
174+
}
172175
delete gfxAllocation;
173176
}
174177

@@ -256,4 +259,13 @@ Allocator32bit *OsAgnosticMemoryManager::create32BitAllocator(bool aubUsage) {
256259

257260
return new Allocator32bit(heap32Base, allocatorSize);
258261
}
262+
263+
void *OsAgnosticMemoryManager::reserveCpuAddressRange(size_t size) {
264+
void *reservePtr = allocateSystemMemory(size, MemoryConstants::preferredAlignment);
265+
return reservePtr;
266+
}
267+
268+
void OsAgnosticMemoryManager::releaseReservedCpuAddressRange(void *reserved, size_t size) {
269+
alignedFreeWrapper(reserved);
270+
}
259271
} // namespace OCLRT

runtime/memory_manager/os_agnostic_memory_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class OsAgnosticMemoryManager : public MemoryManager {
6363

6464
Allocator32bit *create32BitAllocator(bool enableLocalMemory);
6565

66+
void *reserveCpuAddressRange(size_t size) override;
67+
void releaseReservedCpuAddressRange(void *reserved, size_t size) override;
68+
6669
protected:
6770
GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) override;
6871
GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) override;

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ uint64_t DrmMemoryManager::acquireGpuRange(size_t &size, StorageAllocatorType &s
170170
}
171171

172172
storageType = MMAP_ALLOCATOR;
173-
return reinterpret_cast<uint64_t>(mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0));
173+
return reinterpret_cast<uint64_t>(reserveCpuAddressRange(size));
174174
}
175175

176176
void DrmMemoryManager::releaseGpuRange(void *address, size_t unmapSize, StorageAllocatorType allocatorType) {
177177
if (allocatorType == MMAP_ALLOCATOR) {
178-
munmapFunction(address, unmapSize);
178+
releaseReservedCpuAddressRange(address, unmapSize);
179179
return;
180180
}
181181

@@ -579,7 +579,10 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
579579
if (gfxAllocation->peekSharedHandle() != Sharing::nonSharedResource) {
580580
closeFunction(gfxAllocation->peekSharedHandle());
581581
}
582-
582+
void *reserveAddress = gfxAllocation->getReservedAddressPtr();
583+
if (reserveAddress) {
584+
releaseReservedCpuAddressRange(reserveAddress, gfxAllocation->getReservedAddressSize());
585+
}
583586
delete gfxAllocation;
584587

585588
unreference(search);
@@ -733,8 +736,16 @@ void DrmMemoryManager::unlockResourceImpl(GraphicsAllocation &graphicsAllocation
733736
if (bo == nullptr)
734737
return;
735738

736-
munmapFunction(bo->peekLockedAddress(), bo->peekSize());
739+
releaseReservedCpuAddressRange(bo->peekLockedAddress(), bo->peekSize());
737740

738741
bo->setLockedAddress(nullptr);
739742
}
743+
void *DrmMemoryManager::reserveCpuAddressRange(size_t size) {
744+
void *reservePtr = mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
745+
return reservePtr;
746+
}
747+
748+
void DrmMemoryManager::releaseReservedCpuAddressRange(void *reserved, size_t size) {
749+
munmapFunction(reserved, size);
750+
}
740751
} // namespace OCLRT

runtime/os_interface/linux/drm_memory_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class DrmMemoryManager : public MemoryManager {
5555
}
5656

5757
DrmGemCloseWorker *peekGemCloseWorker() const { return this->gemCloseWorker.get(); }
58+
void *reserveCpuAddressRange(size_t size) override;
59+
void releaseReservedCpuAddressRange(void *reserved, size_t size) override;
5860

5961
protected:
6062
BufferObject *findAndReferenceSharedBufferObject(int boHandle);

runtime/os_interface/windows/wddm/wddm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ bool Wddm::makeResident(const D3DKMT_HANDLE *handles, uint32_t count, bool cantT
289289
}
290290

291291
bool Wddm::mapGpuVirtualAddress(WddmAllocation *allocation, void *cpuPtr) {
292-
void *mapPtr = allocation->getReservedAddress() != nullptr ? allocation->getReservedAddress() : cpuPtr;
292+
void *mapPtr = allocation->getReservedAddressPtr() != nullptr ? allocation->getReservedAddressPtr() : cpuPtr;
293293
return mapGpuVirtualAddressImpl(allocation->getDefaultGmm(), allocation->getDefaultHandle(), mapPtr, allocation->getGpuAddressToModify(),
294294
MemoryManager::selectHeap(allocation, mapPtr, *hardwareInfoTable[gfxPlatform->eProductFamily]));
295295
}

runtime/os_interface/windows/wddm_allocation.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ constexpr size_t trimListUnusedPosition = std::numeric_limits<size_t>::max();
2626
class WddmAllocation : public GraphicsAllocation {
2727
public:
2828
WddmAllocation(AllocationType allocationType, void *cpuPtrIn, size_t sizeIn, void *reservedAddr, MemoryPool::Type pool, bool multiOsContextCapable)
29-
: GraphicsAllocation(allocationType, cpuPtrIn, castToUint64(cpuPtrIn), 0llu, sizeIn, pool, multiOsContextCapable), reservedAddressSpace(reservedAddr) {
29+
: GraphicsAllocation(allocationType, cpuPtrIn, castToUint64(cpuPtrIn), 0llu, sizeIn, pool, multiOsContextCapable) {
3030
trimCandidateListPositions.fill(trimListUnusedPosition);
31+
reservedAddressRangeInfo.addressPtr = reservedAddr;
32+
reservedAddressRangeInfo.rangeSize = sizeIn;
3133
}
3234

3335
WddmAllocation(AllocationType allocationType, void *cpuPtrIn, size_t sizeIn, osHandle sharedHandle, MemoryPool::Type pool, bool multiOsContextCapable)
@@ -65,13 +67,6 @@ class WddmAllocation : public GraphicsAllocation {
6567
return trimListUnusedPosition;
6668
}
6769

68-
void *getReservedAddress() const {
69-
return this->reservedAddressSpace;
70-
}
71-
72-
void setReservedAddress(void *reserveMem) {
73-
this->reservedAddressSpace = reserveMem;
74-
}
7570
void setGpuAddress(uint64_t graphicsAddress) { this->gpuAddress = graphicsAddress; }
7671
void setCpuAddress(void *cpuPtr) { this->cpuPtr = cpuPtr; }
7772

@@ -93,6 +88,5 @@ class WddmAllocation : public GraphicsAllocation {
9388
std::array<D3DKMT_HANDLE, maxHandleCount> handles{};
9489
ResidencyData residency;
9590
std::array<size_t, maxOsContextCount> trimCandidateListPositions;
96-
void *reservedAddressSpace = nullptr;
9791
};
9892
} // namespace OCLRT

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl
230230
if (!wddm->reserveValidAddressRange(size, ptr)) {
231231
return nullptr;
232232
}
233-
allocation->setReservedAddress(ptr);
233+
allocation->setReservedAddressRange(ptr, size);
234234
} else if (requireSpecificBitness && this->force32bitAllocations) {
235235
allocation->set32BitAllocation(true);
236236
allocation->setGpuBaseAddress(GmmHelper::canonize(allocator32Bit->getBase()));
@@ -339,7 +339,9 @@ void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation
339339
DEBUG_BREAK_IF(!status);
340340
alignedFreeWrapper(input->getDriverAllocatedCpuPtr());
341341
}
342-
wddm->releaseReservedAddress(input->getReservedAddress());
342+
if (input->getReservedAddressPtr()) {
343+
releaseReservedCpuAddressRange(input->getReservedAddressPtr(), input->getReservedAddressSize());
344+
}
343345
delete gfxAllocation;
344346
}
345347

@@ -502,4 +504,14 @@ bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation) {
502504
return (wddmSuccess == STATUS_SUCCESS);
503505
}
504506

507+
void *WddmMemoryManager::reserveCpuAddressRange(size_t size) {
508+
void *reservePtr = nullptr;
509+
wddm->reserveValidAddressRange(size, reservePtr);
510+
return reservePtr;
511+
}
512+
513+
void WddmMemoryManager::releaseReservedCpuAddressRange(void *reserved, size_t size) {
514+
wddm->releaseReservedAddress(reserved);
515+
}
516+
505517
} // namespace OCLRT

0 commit comments

Comments
 (0)