Fix bug in VA sharing multithreaded scenario

- createGraphicsAllocationFromSharedHandle was not threadsafe,
instead of reusing BufferObject for a single handle when
multiple threads were creating memory objects from a single VASurface,
new BO could be created and placed in container with BOs for reuse.
This was leading to errors in ioctl calls.

- add lock for following set of operations:
 1. find BufferObject with a given handle in container
 2. create shared BO when not found
 3. add shared BO to container
 prevents creating multiple BOs for a single handle

- replace recursive mutex with regular mutex as mutex shouldn't
be locked recursively

Change-Id: I0937e2abf3bf1c672c6d77422d46e441f7216a68
This commit is contained in:
Hoppe, Mateusz
2018-08-01 14:47:14 +02:00
committed by sys_ocldev
parent 2aa898be58
commit e1eaf3ded0
7 changed files with 212 additions and 79 deletions

View File

@@ -79,8 +79,6 @@ DrmMemoryManager::~DrmMemoryManager() {
}
void DrmMemoryManager::eraseSharedBufferObject(OCLRT::BufferObject *bo) {
std::lock_guard<decltype(mtx)> lock(mtx);
auto it = std::find(sharingBufferObjects.begin(), sharingBufferObjects.end(), bo);
//If an object isReused = true, it must be in the vector
DEBUG_BREAK_IF(it == sharingBufferObjects.end());
@@ -88,8 +86,6 @@ void DrmMemoryManager::eraseSharedBufferObject(OCLRT::BufferObject *bo) {
}
void DrmMemoryManager::pushSharedBufferObject(OCLRT::BufferObject *bo) {
std::lock_guard<decltype(mtx)> lock(mtx);
bo->isReused = true;
sharingBufferObjects.push_back(bo);
}
@@ -111,6 +107,7 @@ uint32_t DrmMemoryManager::unreference(OCLRT::BufferObject *bo, bool synchronous
auto allocatorType = bo->peekAllocationType();
if (bo->isReused) {
std::lock_guard<decltype(mtx)> lock(mtx);
eraseSharedBufferObject(bo);
}
@@ -314,8 +311,6 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, const
BufferObject *DrmMemoryManager::findAndReferenceSharedBufferObject(int boHandle) {
BufferObject *bo = nullptr;
std::lock_guard<decltype(mtx)> lock(mtx);
for (const auto &i : sharingBufferObjects) {
if (i->handle == static_cast<int>(boHandle)) {
bo = i;
@@ -365,7 +360,10 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
auto boHandle = openFd.handle;
BufferObject *bo = nullptr;
std::unique_lock<std::mutex> lock(mtx, std::defer_lock);
if (reuseBO) {
lock.lock();
bo = findAndReferenceSharedBufferObject(boHandle);
}

View File

@@ -94,7 +94,7 @@ class DrmMemoryManager : public MemoryManager {
decltype(&munmap) munmapFunction = munmap;
decltype(&close) closeFunction = close;
std::vector<BufferObject *> sharingBufferObjects;
std::recursive_mutex mtx;
std::mutex mtx;
std::unique_ptr<Allocator32bit> internal32bitAllocator;
};
} // namespace OCLRT