Skip to content

Commit ef3678e

Browse files
Add checks for default cache policy
Signed-off-by: Slawomir Milczarek <[email protected]>
1 parent a23f211 commit ef3678e

File tree

9 files changed

+111
-16
lines changed

9 files changed

+111
-16
lines changed

opencl/test/unit_test/os_interface/linux/device_command_stream_fixture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class DrmMockTime : public DrmMockSuccess {
7373
class DrmMockCustom : public Drm {
7474
public:
7575
using Drm::bindAvailable;
76+
using Drm::cacheInfo;
7677
using Drm::memoryInfo;
7778

7879
struct IoctlResExt {

opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,6 +4194,17 @@ TEST(DrmAllocationTest, givenResourceRegistrationEnabledWhenIsaIsRegisteredThenC
41944194
EXPECT_EQ(2u, drm.unregisterCalledCount);
41954195
}
41964196

4197+
TEST(DrmAllocationTest, givenDrmAllocationWhenSetCacheRegionIsCalledForDefaultRegionThenReturnTrue) {
4198+
auto executionEnvironment = std::make_unique<ExecutionEnvironment>();
4199+
executionEnvironment->prepareRootDeviceEnvironments(1);
4200+
4201+
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
4202+
4203+
MockDrmAllocation allocation(GraphicsAllocation::AllocationType::BUFFER, MemoryPool::LocalMemory);
4204+
4205+
EXPECT_TRUE(allocation.setCacheRegion(&drm, CacheRegion::Default));
4206+
}
4207+
41974208
TEST(DrmAllocationTest, givenDrmAllocationWhenCacheInfoIsNotAvailableThenCacheRegionIsNotSet) {
41984209
auto executionEnvironment = std::make_unique<ExecutionEnvironment>();
41994210
executionEnvironment->prepareRootDeviceEnvironments(1);
@@ -4202,10 +4213,10 @@ TEST(DrmAllocationTest, givenDrmAllocationWhenCacheInfoIsNotAvailableThenCacheRe
42024213

42034214
MockDrmAllocation allocation(GraphicsAllocation::AllocationType::BUFFER, MemoryPool::LocalMemory);
42044215

4205-
EXPECT_FALSE(allocation.setCacheRegion(&drm, CacheRegion::None));
4216+
EXPECT_FALSE(allocation.setCacheRegion(&drm, CacheRegion::Region1));
42064217
}
42074218

4208-
TEST(DrmAllocationTest, givenDrmAllocationWhenCacheInfoIsAvailableThenCacheRegionIsNotSetForTheDefault) {
4219+
TEST(DrmAllocationTest, givenDrmAllocationWhenDefaultCacheInfoIsAvailableThenCacheRegionIsNotSet) {
42094220
auto executionEnvironment = std::make_unique<ExecutionEnvironment>();
42104221
executionEnvironment->prepareRootDeviceEnvironments(1);
42114222

@@ -4214,7 +4225,7 @@ TEST(DrmAllocationTest, givenDrmAllocationWhenCacheInfoIsAvailableThenCacheRegio
42144225

42154226
MockDrmAllocation allocation(GraphicsAllocation::AllocationType::BUFFER, MemoryPool::LocalMemory);
42164227

4217-
EXPECT_FALSE(allocation.setCacheRegion(&drm, CacheRegion::Default));
4228+
EXPECT_FALSE(allocation.setCacheRegion(&drm, CacheRegion::Region1));
42184229
}
42194230

42204231
TEST(DrmAllocationTest, givenDrmAllocationWhenCacheRegionIsNotSetThenReturnFalse) {
@@ -4260,4 +4271,63 @@ TEST(DrmAllocationTest, givenDrmAllocationWhenCacheRegionIsSetSuccessfullyThenSe
42604271
}
42614272
}
42624273
}
4274+
4275+
TEST_F(DrmMemoryManagerTest, givenDrmAllocationWithHostPtrWhenItIsCreatedWithCacheRegionThenSetRegionInBufferObject) {
4276+
mock->ioctl_expected.total = -1;
4277+
auto drm = static_cast<DrmMockCustom *>(executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->get()->getDrm());
4278+
drm->cacheInfo.reset(new MockCacheInfo());
4279+
4280+
auto ptr = reinterpret_cast<void *>(0x1000);
4281+
auto size = MemoryConstants::pageSize;
4282+
4283+
OsHandleStorage storage;
4284+
storage.fragmentStorageData[0].cpuPtr = ptr;
4285+
storage.fragmentStorageData[0].fragmentSize = 1;
4286+
storage.fragmentCount = 1;
4287+
4288+
memoryManager->populateOsHandles(storage, rootDeviceIndex);
4289+
4290+
auto allocation = std::make_unique<DrmAllocation>(rootDeviceIndex, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY,
4291+
nullptr, ptr, castToUint64(ptr), size, MemoryPool::System4KBPages);
4292+
allocation->fragmentsStorage = storage;
4293+
4294+
allocation->setCacheAdvice(drm, 1024, CacheRegion::Region1);
4295+
4296+
for (uint32_t i = 0; i < storage.fragmentCount; i++) {
4297+
auto bo = allocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo;
4298+
EXPECT_EQ(CacheRegion::Region1, bo->peekCacheRegion());
4299+
}
4300+
4301+
storage.fragmentStorageData[0].freeTheFragment = true;
4302+
memoryManager->cleanOsHandles(storage, rootDeviceIndex);
4303+
}
4304+
4305+
TEST_F(DrmMemoryManagerTest, givenDrmAllocationWithHostPtrWhenItIsCreatedWithIncorrectCacheRegionThenReturnNull) {
4306+
mock->ioctl_expected.total = -1;
4307+
auto drm = static_cast<DrmMockCustom *>(executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->get()->getDrm());
4308+
drm->setupCacheInfo(*defaultHwInfo.get());
4309+
4310+
auto ptr = reinterpret_cast<void *>(0x1000);
4311+
auto size = MemoryConstants::pageSize;
4312+
4313+
allocationData.size = size;
4314+
allocationData.hostPtr = ptr;
4315+
allocationData.cacheRegion = 0xFFFF;
4316+
4317+
auto allocation = std::unique_ptr<GraphicsAllocation>(memoryManager->allocateGraphicsMemoryWithHostPtr(allocationData));
4318+
EXPECT_EQ(allocation, nullptr);
4319+
}
4320+
4321+
TEST_F(DrmMemoryManagerTest, givenDrmAllocationWithWithAlignmentFromUserptrWhenItIsCreatedWithIncorrectCacheRegionThenReturnNull) {
4322+
mock->ioctl_expected.total = -1;
4323+
auto drm = static_cast<DrmMockCustom *>(executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->get()->getDrm());
4324+
drm->setupCacheInfo(*defaultHwInfo.get());
4325+
4326+
auto size = MemoryConstants::pageSize;
4327+
allocationData.size = size;
4328+
allocationData.cacheRegion = 0xFFFF;
4329+
4330+
auto allocation = static_cast<DrmAllocation *>(memoryManager->createAllocWithAlignmentFromUserptr(allocationData, size, 0, 0, 0x1000));
4331+
EXPECT_EQ(allocation, nullptr);
4332+
}
42634333
} // namespace NEO

opencl/test/unit_test/os_interface/linux/drm_mock_cache_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct MockCacheInfo : public CacheInfo {
1717
~MockCacheInfo() override = default;
1818

1919
bool getCacheRegion(size_t regionSize, CacheRegion regionIndex) override {
20-
return (regionIndex > CacheRegion::None) ? true : false;
20+
return (regionIndex < CacheRegion::Count) ? true : false;
2121
}
2222
};
2323

shared/source/memory_manager/memory_manager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const Alloc
112112
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, allocationData.size, allocationData.hostPtr, allocationData.rootDeviceIndex);
113113
if (osStorage.fragmentCount > 0) {
114114
graphicsAllocation = createGraphicsAllocation(osStorage, allocationData);
115+
if (graphicsAllocation == nullptr) {
116+
hostPtrManager->releaseHandleStorage(allocationData.rootDeviceIndex, osStorage);
117+
cleanOsHandles(osStorage, allocationData.rootDeviceIndex);
118+
}
115119
}
116120
return graphicsAllocation;
117121
}

shared/source/os_interface/linux/cache_info.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
namespace NEO {
1414

1515
enum class CacheRegion : uint16_t {
16-
None = 0,
16+
Default = 0,
1717
Region1,
1818
Region2,
19-
Default = None
19+
Count,
20+
None = 0xFFFF
2021
};
2122

2223
struct CacheInfo {

shared/source/os_interface/linux/drm_allocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ bool DrmAllocation::setCacheAdvice(Drm *drm, size_t regionSize, CacheRegion regi
3434
return false;
3535
}
3636

37+
if (fragmentsStorage.fragmentCount > 0) {
38+
for (uint32_t i = 0; i < fragmentsStorage.fragmentCount; i++) {
39+
auto bo = fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo;
40+
bo->setCacheRegion(regionIndex);
41+
}
42+
return true;
43+
}
44+
3745
for (auto bo : bufferObjects) {
3846
if (bo != nullptr) {
3947
bo->setCacheRegion(regionIndex);

shared/source/os_interface/linux/drm_allocation_extended.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ void DrmAllocation::bindBOs(OsContext *osContext, uint32_t vmHandleId, std::vect
1919
}
2020

2121
bool DrmAllocation::setCacheRegion(Drm *drm, CacheRegion regionIndex) {
22+
if (regionIndex == CacheRegion::Default) {
23+
return true;
24+
}
25+
2226
auto cacheInfo = static_cast<CacheInfoImpl *>(drm->getCacheInfo());
2327
if (cacheInfo == nullptr) {
2428
return false;

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ void DrmMemoryManager::emitPinningRequest(BufferObject *bo, const AllocationData
223223

224224
DrmAllocation *DrmMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) {
225225
auto hostPtr = const_cast<void *>(allocationData.hostPtr);
226-
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, nullptr, hostPtr, castToUint64(hostPtr), allocationData.size, MemoryPool::System4KBPages);
226+
auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, allocationData.type, nullptr, hostPtr, castToUint64(hostPtr), allocationData.size, MemoryPool::System4KBPages);
227227
allocation->fragmentsStorage = handleStorage;
228-
allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion));
229-
return allocation;
228+
if (!allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion))) {
229+
return nullptr;
230+
}
231+
return allocation.release();
230232
}
231233

232234
DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) {
@@ -265,22 +267,26 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignmentFromUserptr(const Alloc
265267
return nullptr;
266268
}
267269

268-
auto bo = allocUserptr(reinterpret_cast<uintptr_t>(res), size, 0, allocationData.rootDeviceIndex);
269-
270+
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(allocUserptr(reinterpret_cast<uintptr_t>(res), size, 0, allocationData.rootDeviceIndex));
270271
if (!bo) {
271272
alignedFreeWrapper(res);
272273
return nullptr;
273274
}
274275

275-
obtainGpuAddress(allocationData, bo, gpuAddress);
276-
emitPinningRequest(bo, allocationData);
276+
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
277+
emitPinningRequest(bo.get(), allocationData);
277278

278-
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo, res, bo->gpuAddress, size, MemoryPool::System4KBPages);
279+
auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, allocationData.type, bo.get(), res, bo->gpuAddress, size, MemoryPool::System4KBPages);
279280
allocation->setDriverAllocatedCpuPtr(res);
280281
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSVMSize);
281-
allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion));
282+
if (!allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion))) {
283+
alignedFreeWrapper(res);
284+
return nullptr;
285+
}
282286

283-
return allocation;
287+
bo.release();
288+
289+
return allocation.release();
284290
}
285291

286292
void DrmMemoryManager::obtainGpuAddress(const AllocationData &allocationData, BufferObject *bo, uint64_t gpuAddress) {

shared/test/common/mocks/linux/mock_drm_memory_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
7070
using DrmMemoryManager::allocateShareableMemory;
7171
using DrmMemoryManager::allocUserptr;
7272
using DrmMemoryManager::createAllocWithAlignment;
73+
using DrmMemoryManager::createAllocWithAlignmentFromUserptr;
7374
using DrmMemoryManager::createGraphicsAllocation;
7475
using DrmMemoryManager::createSharedBufferObject;
7576
using DrmMemoryManager::eraseSharedBufferObject;

0 commit comments

Comments
 (0)