fix: add rootDeviceIndex for BufferObjectHandleWrapper

Modified sharedBoHandles to use the (boHandle, rootDeviceIndex) pair
as a key instead of boHandle, and added rootDeviceIndex variable in
the BufferObjectHandleWrapper class of drm_buffer_object.h.

Previously sharedBoHandles uses boHandle as a key. However, this will
not work with the system using multiple devices, because each devices
can return the same handle to refer different memory region.

Related-To: GSD-9024
Signed-off-by: Young Jin Yoon <young.jin.yoon@intel.com>
This commit is contained in:
Young Jin Yoon
2024-09-10 00:41:25 +00:00
committed by Compute-Runtime-Automation
parent ea5b586c37
commit 675ec13439
6 changed files with 156 additions and 49 deletions

View File

@@ -35,7 +35,7 @@ BufferObjectHandleWrapper BufferObjectHandleWrapper::acquireSharedOwnership() {
std::lock_guard lock{controlBlock->blockMutex};
controlBlock->refCount++;
return BufferObjectHandleWrapper{boHandle, Ownership::strong, controlBlock};
return BufferObjectHandleWrapper{boHandle, rootDeviceIndex, Ownership::strong, controlBlock};
}
BufferObjectHandleWrapper BufferObjectHandleWrapper::acquireWeakOwnership() {
@@ -46,7 +46,7 @@ BufferObjectHandleWrapper BufferObjectHandleWrapper::acquireWeakOwnership() {
std::lock_guard lock{controlBlock->blockMutex};
controlBlock->weakRefCount++;
return BufferObjectHandleWrapper{boHandle, Ownership::weak, controlBlock};
return BufferObjectHandleWrapper{boHandle, rootDeviceIndex, Ownership::weak, controlBlock};
}
BufferObjectHandleWrapper::~BufferObjectHandleWrapper() {
@@ -79,7 +79,7 @@ bool BufferObjectHandleWrapper::canCloseBoHandle() {
}
BufferObject::BufferObject(uint32_t rootDeviceIndex, Drm *drm, uint64_t patIndex, int handle, size_t size, size_t maxOsContextCount)
: BufferObject(rootDeviceIndex, drm, patIndex, BufferObjectHandleWrapper{handle}, size, maxOsContextCount) {}
: BufferObject(rootDeviceIndex, drm, patIndex, BufferObjectHandleWrapper{handle, rootDeviceIndex}, size, maxOsContextCount) {}
BufferObject::BufferObject(uint32_t rootDeviceIndex, Drm *drm, uint64_t patIndex, BufferObjectHandleWrapper &&handle, size_t size, size_t maxOsContextCount)
: drm(drm), handle(std::move(handle)), size(size), refCount(1), rootDeviceIndex(rootDeviceIndex) {

View File

@@ -45,11 +45,11 @@ class BufferObjectHandleWrapper {
};
public:
explicit BufferObjectHandleWrapper(int boHandle) noexcept
: boHandle{boHandle} {}
explicit BufferObjectHandleWrapper(int boHandle, uint32_t rootDeviceIndex) noexcept
: boHandle{boHandle}, rootDeviceIndex(rootDeviceIndex) {}
BufferObjectHandleWrapper(BufferObjectHandleWrapper &&other) noexcept
: boHandle(std::exchange(other.boHandle, -1)), ownership(other.ownership), controlBlock(std::exchange(other.controlBlock, nullptr)) {}
: boHandle(std::exchange(other.boHandle, -1)), rootDeviceIndex(std::exchange(other.rootDeviceIndex, UINT32_MAX)), ownership(other.ownership), controlBlock(std::exchange(other.controlBlock, nullptr)) {}
~BufferObjectHandleWrapper();
@@ -65,16 +65,23 @@ class BufferObjectHandleWrapper {
int getBoHandle() const {
return boHandle;
}
uint32_t getRootDeviceIndex() const {
return rootDeviceIndex;
}
void setBoHandle(int handle) {
boHandle = handle;
}
void setRootDeviceIndex(uint32_t index) {
rootDeviceIndex = index;
}
protected:
BufferObjectHandleWrapper(int boHandle, Ownership ownership, ControlBlock *controlBlock)
: boHandle{boHandle}, ownership{ownership}, controlBlock{controlBlock} {}
BufferObjectHandleWrapper(int boHandle, uint32_t rootDeviceIndex, Ownership ownership, ControlBlock *controlBlock)
: boHandle{boHandle}, rootDeviceIndex{rootDeviceIndex}, ownership{ownership}, controlBlock{controlBlock} {}
int boHandle{};
uint32_t rootDeviceIndex{UINT32_MAX};
Ownership ownership{Ownership::strong};
ControlBlock *controlBlock{nullptr};
};

View File

@@ -239,13 +239,13 @@ uint32_t DrmMemoryManager::unreference(NEO::BufferObject *bo, bool synchronousDe
if (bo->peekIsReusableAllocation()) {
eraseSharedBufferObject(bo);
}
auto rootDeviceIndex = bo->getRootDeviceIndex();
int boHandle = bo->getHandle();
bo->close();
if (bo->isBoHandleShared() && bo->getHandle() != boHandle) {
// Shared BO was closed - handle was invalidated. Remove weak reference from container.
eraseSharedBoHandleWrapper(boHandle);
eraseSharedBoHandleWrapper(boHandle, rootDeviceIndex);
}
if (lock) {
@@ -926,7 +926,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromMultipleShared
totalSize += size;
auto patIndex = drm.getPatIndex(nullptr, properties.allocationType, CacheRegion::defaultRegion, CachePolicy::writeBack, false, MemoryPoolHelper::isSystemMemoryPool(memoryPool));
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle);
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle, properties.rootDeviceIndex} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle, properties.rootDeviceIndex);
bo = new (std::nothrow) BufferObject(properties.rootDeviceIndex, &drm, patIndex, std::move(boHandleWrapper), size, maxOsContextCount);
i++;
@@ -1007,32 +1007,32 @@ void DrmMemoryManager::registerSharedBoHandleAllocation(DrmAllocation *drmAlloca
}
auto &bos = drmAllocation->getBOs();
auto rootDeviceIndex = drmAllocation->getRootDeviceIndex();
for (auto *bo : bos) {
if (bo == nullptr) {
continue;
}
auto foundHandleWrapperIt = sharedBoHandles.find(bo->getHandle());
auto foundHandleWrapperIt = sharedBoHandles.find(std::pair<int, uint32_t>(bo->getHandle(), rootDeviceIndex));
if (foundHandleWrapperIt == std::end(sharedBoHandles)) {
sharedBoHandles.emplace(bo->getHandle(), bo->acquireWeakOwnershipOfBoHandle());
sharedBoHandles.emplace(std::make_pair(bo->getHandle(), rootDeviceIndex), bo->acquireWeakOwnershipOfBoHandle());
} else {
bo->markAsSharedBoHandle();
}
}
}
BufferObjectHandleWrapper DrmMemoryManager::tryToGetBoHandleWrapperWithSharedOwnership(int boHandle) {
auto foundHandleWrapperIt = sharedBoHandles.find(boHandle);
BufferObjectHandleWrapper DrmMemoryManager::tryToGetBoHandleWrapperWithSharedOwnership(int boHandle, uint32_t rootDeviceIndex) {
auto foundHandleWrapperIt = sharedBoHandles.find(std::make_pair(boHandle, rootDeviceIndex));
if (foundHandleWrapperIt == std::end(sharedBoHandles)) {
return BufferObjectHandleWrapper{boHandle};
return BufferObjectHandleWrapper{boHandle, rootDeviceIndex};
}
return foundHandleWrapperIt->second.acquireSharedOwnership();
}
void DrmMemoryManager::eraseSharedBoHandleWrapper(int boHandle) {
auto foundHandleWrapperIt = sharedBoHandles.find(boHandle);
void DrmMemoryManager::eraseSharedBoHandleWrapper(int boHandle, uint32_t rootDeviceIndex) {
auto foundHandleWrapperIt = sharedBoHandles.find(std::make_pair(boHandle, rootDeviceIndex));
if (foundHandleWrapperIt != std::end(sharedBoHandles)) {
sharedBoHandles.erase(foundHandleWrapperIt);
}
@@ -1079,7 +1079,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(c
UNRECOVERABLE_IF(size == std::numeric_limits<size_t>::max());
auto patIndex = drm.getPatIndex(nullptr, properties.allocationType, CacheRegion::defaultRegion, CachePolicy::writeBack, false, MemoryPoolHelper::isSystemMemoryPool(memoryPool));
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle);
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle, properties.rootDeviceIndex} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle, properties.rootDeviceIndex);
bo = new (std::nothrow) BufferObject(properties.rootDeviceIndex, &drm, patIndex, std::move(boHandleWrapper), size, maxOsContextCount);
@@ -2633,7 +2633,7 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl
}
auto boHandle = static_cast<int>(openFd.handle);
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle);
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle, properties.rootDeviceIndex} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle, properties.rootDeviceIndex);
const bool useBooMmap = drm.getMemoryInfo() && properties.useMmapObject;
if (!useBooMmap) {

View File

@@ -24,6 +24,12 @@ enum class AtomicAccessMode : uint32_t;
enum class GemCloseWorkerMode;
struct BoHandleDeviceIndexPairComparer {
bool operator()(std::pair<int, uint32_t> const &lhs, std::pair<int, uint32_t> const &rhs) const {
return (lhs.first < rhs.first) || (lhs.second < rhs.second);
}
};
class DrmMemoryManager : public MemoryManager {
public:
DrmMemoryManager(GemCloseWorkerMode mode,
@@ -114,8 +120,8 @@ class DrmMemoryManager : public MemoryManager {
protected:
void registerSharedBoHandleAllocation(DrmAllocation *drmAllocation);
BufferObjectHandleWrapper tryToGetBoHandleWrapperWithSharedOwnership(int boHandle);
void eraseSharedBoHandleWrapper(int boHandle);
BufferObjectHandleWrapper tryToGetBoHandleWrapperWithSharedOwnership(int boHandle, uint32_t rootDeviceIndex);
void eraseSharedBoHandleWrapper(int boHandle, uint32_t rootDeviceIndex);
MOCKABLE_VIRTUAL BufferObject *findAndReferenceSharedBufferObject(int boHandle, uint32_t rootDeviceIndex);
void eraseSharedBufferObject(BufferObject *bo);
@@ -187,7 +193,7 @@ class DrmMemoryManager : public MemoryManager {
std::vector<BufferObject *> sharingBufferObjects;
std::mutex mtx;
std::map<int, BufferObjectHandleWrapper> sharedBoHandles;
std::map<std::pair<int, uint32_t>, BufferObjectHandleWrapper, BoHandleDeviceIndexPairComparer> sharedBoHandles;
std::vector<std::vector<GraphicsAllocation *>> localMemAllocs;
std::vector<size_t> localMemBanksCount;
std::vector<GraphicsAllocation *> sysMemAllocs;