Skip to content

Commit f332bf3

Browse files
Set allocation's lock state only in lockResource and unlockResource methods
Change-Id: I60f35801287166f5bdb0dfcd31ff0118c56ec22a Signed-off-by: Mateusz Jablonski <[email protected]>
1 parent 09c62ca commit f332bf3

20 files changed

+161
-149
lines changed

runtime/command_stream/aub_command_stream_receiver_hw.inl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,12 @@ bool AUBCommandStreamReceiverHw<GfxFamily>::writeMemory(GraphicsAllocation &gfxA
635635
if (cpuAddress == nullptr) {
636636
DEBUG_BREAK_IF(gfxAllocation.isLocked());
637637
cpuAddress = this->getMemoryManager()->lockResource(&gfxAllocation);
638-
gfxAllocation.setLocked(true);
639638
}
640639

641640
writeMemory(gpuAddress, cpuAddress, size, this->getMemoryBank(&gfxAllocation), this->getPPGTTAdditionalBits(&gfxAllocation), gfxAllocation.devicesBitfield);
642641

643642
if (gfxAllocation.isLocked()) {
644643
this->getMemoryManager()->unlockResource(&gfxAllocation);
645-
gfxAllocation.setLocked(false);
646644
}
647645

648646
if (AubHelper::isOneTimeAubWritableAllocationType(gfxAllocation.getAllocationType())) {

runtime/memory_manager/memory_manager.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ void MemoryManager::applyCommonCleanup() {
132132
}
133133

134134
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
135+
if (!gfxAllocation) {
136+
return;
137+
}
135138
freeGraphicsMemoryImpl(gfxAllocation);
136139
}
137140
//if not in use destroy in place
@@ -328,4 +331,22 @@ CommandStreamReceiver *MemoryManager::getDefaultCommandStreamReceiver(uint32_t d
328331
return executionEnvironment.commandStreamReceivers[deviceId][defaultEngineIndex].get();
329332
}
330333

334+
void *MemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
335+
if (!graphicsAllocation) {
336+
return nullptr;
337+
}
338+
DEBUG_BREAK_IF(graphicsAllocation->isLocked());
339+
auto retVal = lockResourceImpl(*graphicsAllocation);
340+
graphicsAllocation->setLocked(true);
341+
return retVal;
342+
}
343+
344+
void MemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
345+
if (!graphicsAllocation) {
346+
return;
347+
}
348+
DEBUG_BREAK_IF(!graphicsAllocation->isLocked());
349+
unlockResourceImpl(*graphicsAllocation);
350+
graphicsAllocation->setLocked(false);
351+
}
331352
} // namespace OCLRT

runtime/memory_manager/memory_manager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class MemoryManager {
129129

130130
virtual bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) { return false; };
131131

132-
virtual void *lockResource(GraphicsAllocation *graphicsAllocation) = 0;
133-
virtual void unlockResource(GraphicsAllocation *graphicsAllocation) = 0;
132+
void *lockResource(GraphicsAllocation *graphicsAllocation);
133+
void unlockResource(GraphicsAllocation *graphicsAllocation);
134134

135135
void cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *);
136136
GraphicsAllocation *createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding);
@@ -245,6 +245,9 @@ class MemoryManager {
245245
}
246246
GraphicsAllocation *allocateGraphicsMemoryForImageFromHostPtr(ImageInfo &imgInfo, const void *hostPtr);
247247

248+
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
249+
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
250+
248251
bool force32bitAllocations = false;
249252
bool virtualPaddingAvailable = false;
250253
GraphicsAllocation *paddingAllocation = nullptr;

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ void OsAgnosticMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocat
148148
}
149149

150150
void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
151-
if (gfxAllocation == nullptr)
152-
return;
153-
154151
delete gfxAllocation->gmm;
155152

156153
if ((uintptr_t)gfxAllocation->getUnderlyingBuffer() == dummyAddress) {

runtime/memory_manager/os_agnostic_memory_manager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class OsAgnosticMemoryManager : public MemoryManager {
5454
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
5555
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
5656
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
57-
void *lockResource(GraphicsAllocation *graphicsAllocation) override { return ptrOffset(graphicsAllocation->getUnderlyingBuffer(), static_cast<size_t>(graphicsAllocation->allocationOffset)); };
58-
void unlockResource(GraphicsAllocation *graphicsAllocation) override{};
5957

6058
AllocationStatus populateOsHandles(OsHandleStorage &handleStorage) override;
6159
void cleanOsHandles(OsHandleStorage &handleStorage) override;
@@ -74,6 +72,9 @@ class OsAgnosticMemoryManager : public MemoryManager {
7472
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
7573
GraphicsAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override;
7674

75+
void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) override { return ptrOffset(graphicsAllocation.getUnderlyingBuffer(), static_cast<size_t>(graphicsAllocation.allocationOffset)); };
76+
void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) override{};
77+
7778
private:
7879
unsigned long long counter = 0;
7980
bool fakeBigAllocations = false;

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,6 @@ void DrmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gf
546546
void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
547547
DrmAllocation *input;
548548
input = static_cast<DrmAllocation *>(gfxAllocation);
549-
if (input == nullptr)
550-
return;
551549
if (input->gmm)
552550
delete input->gmm;
553551

@@ -675,19 +673,16 @@ bool DrmMemoryManager::setDomainCpu(GraphicsAllocation &graphicsAllocation, bool
675673
return drm->ioctl(DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0;
676674
}
677675

678-
void *DrmMemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
679-
if (graphicsAllocation == nullptr)
680-
return nullptr;
681-
682-
auto cpuPtr = graphicsAllocation->getUnderlyingBuffer();
676+
void *DrmMemoryManager::lockResourceImpl(GraphicsAllocation &graphicsAllocation) {
677+
auto cpuPtr = graphicsAllocation.getUnderlyingBuffer();
683678
if (cpuPtr != nullptr) {
684-
auto success = setDomainCpu(*graphicsAllocation, false);
679+
auto success = setDomainCpu(graphicsAllocation, false);
685680
DEBUG_BREAK_IF(!success);
686681
(void)success;
687682
return cpuPtr;
688683
}
689684

690-
auto bo = static_cast<DrmAllocation *>(graphicsAllocation)->getBO();
685+
auto bo = static_cast<DrmAllocation &>(graphicsAllocation).getBO();
691686
if (bo == nullptr)
692687
return nullptr;
693688

@@ -700,23 +695,20 @@ void *DrmMemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
700695

701696
bo->setLockedAddress(reinterpret_cast<void *>(mmap_arg.addr_ptr));
702697

703-
auto success = setDomainCpu(*graphicsAllocation, false);
698+
auto success = setDomainCpu(graphicsAllocation, false);
704699
DEBUG_BREAK_IF(!success);
705700
(void)success;
706701

707702
return bo->peekLockedAddress();
708703
}
709704

710-
void DrmMemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
711-
if (graphicsAllocation == nullptr)
712-
return;
713-
714-
auto cpuPtr = graphicsAllocation->getUnderlyingBuffer();
705+
void DrmMemoryManager::unlockResourceImpl(GraphicsAllocation &graphicsAllocation) {
706+
auto cpuPtr = graphicsAllocation.getUnderlyingBuffer();
715707
if (cpuPtr != nullptr) {
716708
return;
717709
}
718710

719-
auto bo = static_cast<DrmAllocation *>(graphicsAllocation)->getBO();
711+
auto bo = static_cast<DrmAllocation &>(graphicsAllocation).getBO();
720712
if (bo == nullptr)
721713
return;
722714

runtime/os_interface/linux/drm_memory_manager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2018 Intel Corporation
2+
* Copyright (C) 2017-2019 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -37,8 +37,6 @@ class DrmMemoryManager : public MemoryManager {
3737
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override;
3838
GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) override;
3939
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) override { return nullptr; }
40-
void *lockResource(GraphicsAllocation *graphicsAllocation) override;
41-
void unlockResource(GraphicsAllocation *graphicsAllocation) override;
4240

4341
uint64_t getSystemSharedMemory() override;
4442
uint64_t getMaxApplicationAddress() override;
@@ -72,6 +70,9 @@ class DrmMemoryManager : public MemoryManager {
7270
DrmAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) override;
7371
DrmAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override;
7472

73+
void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) override;
74+
void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) override;
75+
7576
Drm *drm;
7677
BufferObject *pinBB;
7778
size_t pinThreshold = 8 * 1024 * 1024;

runtime/os_interface/windows/wddm/wddm.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -630,37 +630,37 @@ bool Wddm::openNTHandle(HANDLE handle, WddmAllocation *alloc) {
630630
return true;
631631
}
632632

633-
void *Wddm::lockResource(WddmAllocation *wddmAllocation) {
633+
void *Wddm::lockResource(WddmAllocation &wddmAllocation) {
634634

635-
if (wddmAllocation->needsMakeResidentBeforeLock) {
635+
if (wddmAllocation.needsMakeResidentBeforeLock) {
636636
applyBlockingMakeResident(wddmAllocation);
637637
}
638638

639639
NTSTATUS status = STATUS_UNSUCCESSFUL;
640640
D3DKMT_LOCK2 lock2 = {};
641641

642-
lock2.hAllocation = wddmAllocation->handle;
642+
lock2.hAllocation = wddmAllocation.handle;
643643
lock2.hDevice = this->device;
644644

645645
status = gdi->lock2(&lock2);
646646
DEBUG_BREAK_IF(status != STATUS_SUCCESS);
647647

648-
kmDafListener->notifyLock(featureTable->ftrKmdDaf, adapter, device, wddmAllocation->handle, 0, gdi->escape);
648+
kmDafListener->notifyLock(featureTable->ftrKmdDaf, adapter, device, wddmAllocation.handle, 0, gdi->escape);
649649

650650
return lock2.pData;
651651
}
652652

653-
void Wddm::unlockResource(WddmAllocation *wddmAllocation) {
653+
void Wddm::unlockResource(WddmAllocation &wddmAllocation) {
654654
NTSTATUS status = STATUS_UNSUCCESSFUL;
655655
D3DKMT_UNLOCK2 unlock2 = {};
656656

657-
unlock2.hAllocation = wddmAllocation->handle;
657+
unlock2.hAllocation = wddmAllocation.handle;
658658
unlock2.hDevice = this->device;
659659

660660
status = gdi->unlock2(&unlock2);
661661
DEBUG_BREAK_IF(status != STATUS_SUCCESS);
662662

663-
kmDafListener->notifyUnlock(featureTable->ftrKmdDaf, adapter, device, &wddmAllocation->handle, 1, gdi->escape);
663+
kmDafListener->notifyUnlock(featureTable->ftrKmdDaf, adapter, device, &wddmAllocation.handle, 1, gdi->escape);
664664
}
665665

666666
void Wddm::kmDafLock(WddmAllocation *wddmAllocation) {
@@ -964,35 +964,35 @@ EvictionStatus Wddm::evictAllTemporaryResources() {
964964
return error ? EvictionStatus::FAILED : EvictionStatus::SUCCESS;
965965
}
966966

967-
EvictionStatus Wddm::evictTemporaryResource(WddmAllocation *allocation) {
967+
EvictionStatus Wddm::evictTemporaryResource(WddmAllocation &allocation) {
968968
auto &lock = acquireLock(temporaryResourcesLock);
969-
auto position = std::find(temporaryResources.begin(), temporaryResources.end(), allocation->handle);
969+
auto position = std::find(temporaryResources.begin(), temporaryResources.end(), allocation.handle);
970970
if (position == temporaryResources.end()) {
971971
return EvictionStatus::NOT_APPLIED;
972972
}
973973
*position = temporaryResources.back();
974974
temporaryResources.pop_back();
975975
uint64_t sizeToTrim = 0;
976-
if (!evict(&allocation->handle, 1, sizeToTrim)) {
976+
if (!evict(&allocation.handle, 1, sizeToTrim)) {
977977
return EvictionStatus::FAILED;
978978
}
979979
return EvictionStatus::SUCCESS;
980980
}
981-
void Wddm::applyBlockingMakeResident(WddmAllocation *allocation) {
981+
void Wddm::applyBlockingMakeResident(WddmAllocation &allocation) {
982982
bool madeResident = false;
983-
while (!(madeResident = makeResident(&allocation->handle, 1, false, nullptr))) {
983+
while (!(madeResident = makeResident(&allocation.handle, 1, false, nullptr))) {
984984
if (evictAllTemporaryResources() == EvictionStatus::SUCCESS) {
985985
continue;
986986
}
987-
if (!makeResident(&allocation->handle, 1, false, nullptr)) {
987+
if (!makeResident(&allocation.handle, 1, false, nullptr)) {
988988
DEBUG_BREAK_IF(true);
989989
return;
990990
};
991991
break;
992992
}
993993
DEBUG_BREAK_IF(!madeResident);
994994
auto &lock = acquireLock(temporaryResourcesLock);
995-
temporaryResources.push_back(allocation->handle);
995+
temporaryResources.push_back(allocation.handle);
996996
lock.unlock();
997997
while (currentPagingFenceValue > *getPagingFenceAddress())
998998
;

runtime/os_interface/windows/wddm/wddm.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class Wddm {
7474
MOCKABLE_VIRTUAL bool destroyAllocations(D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle);
7575
MOCKABLE_VIRTUAL bool openSharedHandle(D3DKMT_HANDLE handle, WddmAllocation *alloc);
7676
bool openNTHandle(HANDLE handle, WddmAllocation *alloc);
77-
MOCKABLE_VIRTUAL void *lockResource(WddmAllocation *wddmAllocation);
78-
MOCKABLE_VIRTUAL void unlockResource(WddmAllocation *wddmAllocation);
77+
MOCKABLE_VIRTUAL void *lockResource(WddmAllocation &wddmAllocation);
78+
MOCKABLE_VIRTUAL void unlockResource(WddmAllocation &wddmAllocation);
7979
MOCKABLE_VIRTUAL void kmDafLock(WddmAllocation *wddmAllocation);
8080
MOCKABLE_VIRTUAL bool isKmDafEnabled() { return featureTable->ftrKmdDaf; };
8181

@@ -152,8 +152,8 @@ class Wddm {
152152
return pagingFenceAddress;
153153
}
154154
MOCKABLE_VIRTUAL EvictionStatus evictAllTemporaryResources();
155-
MOCKABLE_VIRTUAL EvictionStatus evictTemporaryResource(WddmAllocation *allocation);
156-
MOCKABLE_VIRTUAL void applyBlockingMakeResident(WddmAllocation *allocation);
155+
MOCKABLE_VIRTUAL EvictionStatus evictTemporaryResource(WddmAllocation &allocation);
156+
MOCKABLE_VIRTUAL void applyBlockingMakeResident(WddmAllocation &allocation);
157157
MOCKABLE_VIRTUAL std::unique_lock<SpinLock> acquireLock(SpinLock &lock);
158158

159159
protected:

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(AllocationData
8181
}
8282

8383
auto cpuPtr = lockResource(wddmAllocation.get());
84-
wddmAllocation->setLocked(true);
8584

8685
// 64kb map is not needed
8786
auto status = wddm->mapGpuVirtualAddress(wddmAllocation.get(), cpuPtr, false, false, false);
@@ -287,24 +286,21 @@ void WddmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *g
287286
}
288287
}
289288

290-
void *WddmMemoryManager::lockResource(GraphicsAllocation *graphicsAllocation) {
291-
return wddm->lockResource(static_cast<WddmAllocation *>(graphicsAllocation));
289+
void *WddmMemoryManager::lockResourceImpl(GraphicsAllocation &graphicsAllocation) {
290+
return wddm->lockResource(static_cast<WddmAllocation &>(graphicsAllocation));
292291
};
293-
void WddmMemoryManager::unlockResource(GraphicsAllocation *graphicsAllocation) {
294-
wddm->unlockResource(static_cast<WddmAllocation *>(graphicsAllocation));
292+
void WddmMemoryManager::unlockResourceImpl(GraphicsAllocation &graphicsAllocation) {
293+
auto &wddmAllocation = static_cast<WddmAllocation &>(graphicsAllocation);
294+
wddm->unlockResource(wddmAllocation);
295+
if (wddmAllocation.needsMakeResidentBeforeLock) {
296+
auto evictionStatus = wddm->evictTemporaryResource(wddmAllocation);
297+
DEBUG_BREAK_IF(evictionStatus == EvictionStatus::FAILED);
298+
}
295299
};
296300

297301
void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) {
298302
WddmAllocation *input = static_cast<WddmAllocation *>(gfxAllocation);
299303
DEBUG_BREAK_IF(!validateAllocation(input));
300-
if (gfxAllocation == nullptr) {
301-
return;
302-
}
303-
304-
if (input->isLocked() && input->needsMakeResidentBeforeLock) {
305-
auto evictionStatus = wddm->evictTemporaryResource(input);
306-
DEBUG_BREAK_IF(evictionStatus == EvictionStatus::FAILED);
307-
}
308304

309305
for (auto &osContext : this->registeredOsContexts) {
310306
if (osContext) {
@@ -343,7 +339,6 @@ void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation
343339
}
344340
if (input->isLocked()) {
345341
unlockResource(input);
346-
input->setLocked(false);
347342
}
348343
auto status = tryDeferDeletions(allocationHandles, allocationCount, resourceHandle);
349344
DEBUG_BREAK_IF(!status);

0 commit comments

Comments
 (0)