mirror of
https://github.com/intel/compute-runtime.git
synced 2025-09-15 13:01:45 +08:00
Align mmaped bo address properly
Change-Id: I010f6619821ad715bb6f0e9640be19943a45abd8 Signed-off-by: Lukasz Jobczyk <lukasz.jobczyk@intel.com>
This commit is contained in:

committed by
sys_ocldev

parent
eb8f5fa301
commit
8892ee3f1f
@ -260,6 +260,9 @@ TEST_F(DrmMemoryManagerLocalMemoryTest, givenMemoryInfoWhenAllocateWithAlignment
|
||||
auto allocation = memoryManager->allocateGraphicsMemoryWithAlignment(allocationData);
|
||||
|
||||
EXPECT_NE(allocation, nullptr);
|
||||
EXPECT_NE(allocation->getMmapPtr(), nullptr);
|
||||
EXPECT_NE(allocation->getMmapSize(), 0u);
|
||||
EXPECT_EQ(allocation->getAllocationOffset(), 0u);
|
||||
EXPECT_EQ(1u, mock->createExt.handle);
|
||||
|
||||
memoryManager->freeGraphicsMemory(allocation);
|
||||
|
@ -2805,14 +2805,16 @@ TEST_F(DrmMemoryManagerUSMHostAllocationTests, givenMmapPtrWhenFreeGraphicsMemor
|
||||
mock->ioctl_expected.gemUserptr = 1;
|
||||
mock->ioctl_expected.gemClose = 1;
|
||||
|
||||
const size_t size = 16384;
|
||||
AllocationData allocationData;
|
||||
allocationData.size = 16384;
|
||||
allocationData.size = size;
|
||||
allocationData.rootDeviceIndex = rootDeviceIndex;
|
||||
auto alloc = memoryManager->allocateGraphicsMemoryWithAlignment(allocationData);
|
||||
EXPECT_NE(nullptr, alloc);
|
||||
|
||||
auto ptr = memoryManager->mmapFunction(0, alloc->getUnderlyingBufferSize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0);
|
||||
auto ptr = memoryManager->mmapFunction(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0);
|
||||
static_cast<DrmAllocation *>(alloc)->setMmapPtr(ptr);
|
||||
static_cast<DrmAllocation *>(alloc)->setMmapSize(size);
|
||||
|
||||
memoryManager->freeGraphicsMemoryImpl(alloc);
|
||||
}
|
||||
|
@ -65,6 +65,8 @@ class DrmAllocation : public GraphicsAllocation {
|
||||
|
||||
void *getMmapPtr() { return this->mmapPtr; }
|
||||
void setMmapPtr(void *ptr) { this->mmapPtr = ptr; }
|
||||
size_t getMmapSize() { return this->mmapSize; }
|
||||
void setMmapSize(size_t size) { this->mmapSize = size; }
|
||||
|
||||
void makeBOsResident(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
|
||||
void bindBO(BufferObject *bo, OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
|
||||
@ -77,5 +79,6 @@ class DrmAllocation : public GraphicsAllocation {
|
||||
StackVec<uint32_t, 1> registeredBoBindHandles;
|
||||
|
||||
void *mmapPtr = nullptr;
|
||||
size_t mmapSize = 0u;
|
||||
};
|
||||
} // namespace NEO
|
||||
|
@ -669,7 +669,7 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
|
||||
this->unregisterAllocation(gfxAllocation);
|
||||
|
||||
if (drmAlloc->getMmapPtr()) {
|
||||
this->munmapFunction(drmAlloc->getMmapPtr(), gfxAllocation->getUnderlyingBufferSize());
|
||||
this->munmapFunction(drmAlloc->getMmapPtr(), drmAlloc->getMmapSize());
|
||||
}
|
||||
|
||||
for (auto &engine : this->registeredEngines) {
|
||||
|
@ -73,9 +73,16 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
|
||||
}
|
||||
|
||||
if (useBooMmap) {
|
||||
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), 0u, alignedSVMSize, 0u, maxOsContextCount));
|
||||
auto totalSizeToAlloc = alignedSVMSize + alignment;
|
||||
auto cpuPointer = this->mmapFunction(0, totalSizeToAlloc, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
|
||||
auto cpuBasePointer = cpuPointer;
|
||||
cpuPointer = alignUp(cpuPointer, alignment);
|
||||
|
||||
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), alignedSVMSize, 0u, maxOsContextCount));
|
||||
|
||||
if (!bo) {
|
||||
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -85,23 +92,18 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
|
||||
|
||||
auto ret = this->getDrm(allocationData.rootDeviceIndex).ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &gemMmap);
|
||||
if (ret != 0) {
|
||||
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void *cpuPointer = reinterpret_cast<void *>(this->mmapFunction(0, alignedSVMSize, PROT_READ | PROT_WRITE, MAP_SHARED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset)));
|
||||
|
||||
auto cpuBasePointer = cpuPointer;
|
||||
cpuPointer = alignUp(cpuPointer, alignment);
|
||||
auto offset = ptrDiff(cpuPointer, cpuBasePointer);
|
||||
|
||||
bo->gpuAddress = reinterpret_cast<uintptr_t>(cpuPointer);
|
||||
this->mmapFunction(cpuPointer, alignedSVMSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset));
|
||||
|
||||
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
|
||||
emitPinningRequest(bo.get(), allocationData);
|
||||
|
||||
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->gpuAddress, alignedSVMSize, MemoryPool::System4KBPages);
|
||||
allocation->setMmapPtr(cpuBasePointer);
|
||||
allocation->setAllocationOffset(offset);
|
||||
allocation->setMmapSize(totalSizeToAlloc);
|
||||
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSVMSize);
|
||||
|
||||
bo.release();
|
||||
|
@ -17,6 +17,9 @@ extern off_t lseekReturn;
|
||||
extern std::atomic<int> lseekCalledCount;
|
||||
|
||||
inline void *mmapMock(void *addr, size_t length, int prot, int flags, int fd, off_t offset) noexcept {
|
||||
if (addr) {
|
||||
return addr;
|
||||
}
|
||||
void *ptr = nullptr;
|
||||
if (length > 0) {
|
||||
ptr = alignedMalloc(length, MemoryConstants::pageSize64k);
|
||||
|
Reference in New Issue
Block a user