Fix unmap unalignment part at end of mmap allocation

Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>
This commit is contained in:
Maciej Plewka
2021-01-28 20:58:23 +00:00
committed by Compute-Runtime-Automation
parent 7e9461ffa0
commit 2eaf830aff
5 changed files with 34 additions and 20 deletions

View File

@@ -83,8 +83,7 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
cpuPointer = alignUp(cpuPointer, alignment);
auto pointerDiff = ptrDiff(cpuPointer, cpuBasePointer);
auto allocSize = totalSizeToAlloc - pointerDiff;
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), allocSize, 0u, maxOsContextCount));
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), alignedSize, 0u, maxOsContextCount));
if (!bo) {
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
@@ -101,20 +100,22 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
return nullptr;
}
[[maybe_unused]] auto retPtr = this->mmapFunction(cpuPointer, allocSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset));
[[maybe_unused]] auto retPtr = this->mmapFunction(cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset));
DEBUG_BREAK_IF(retPtr != cpuPointer);
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
emitPinningRequest(bo.get(), allocationData);
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->gpuAddress, allocSize, MemoryPool::System4KBPages);
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->gpuAddress, alignedSize, MemoryPool::System4KBPages);
allocation->setMmapPtr(cpuPointer);
allocation->setMmapSize(allocSize);
allocation->setMmapSize(alignedSize);
if (pointerDiff != 0) {
[[maybe_unused]] auto retCode = this->munmapFunction(cpuBasePointer, pointerDiff);
DEBUG_BREAK_IF(retCode != 0);
}
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), allocSize);
[[maybe_unused]] auto retCode = this->munmapFunction(ptrOffset(cpuPointer, alignedSize), alignment - pointerDiff);
DEBUG_BREAK_IF(retCode != 0);
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSize);
bo.release();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Intel Corporation
* Copyright (C) 2018-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -21,6 +21,7 @@ off_t lseekReturn = 4096u;
std::atomic<int> lseekCalledCount(0);
int closeInputFd = 0;
std::atomic<int> closeCalledCount(0);
StackVec<void *, 10> mmapVector;
TestedDrmMemoryManager::TestedDrmMemoryManager(ExecutionEnvironment &executionEnvironment) : MemoryManagerCreate(gemCloseWorkerMode::gemCloseWorkerInactive,
false,

View File

@@ -17,6 +17,7 @@ extern off_t lseekReturn;
extern std::atomic<int> lseekCalledCount;
extern int closeInputFd;
extern std::atomic<int> closeCalledCount;
extern StackVec<void *, 10> mmapVector;
inline void *mmapMock(void *addr, size_t length, int prot, int flags, int fd, off_t offset) noexcept {
if (addr) {
@@ -25,13 +26,17 @@ inline void *mmapMock(void *addr, size_t length, int prot, int flags, int fd, of
void *ptr = nullptr;
if (length > 0) {
ptr = alignedMalloc(length, MemoryConstants::pageSize64k);
mmapVector.push_back(ptr);
}
return ptr;
}
inline int munmapMock(void *addr, size_t length) noexcept {
if (length > 0) {
alignedFree(addr);
auto ptrIt = std::find(mmapVector.begin(), mmapVector.end(), addr);
if (ptrIt != mmapVector.end()) {
alignedFree(addr);
}
}
return 0;
}