Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,12 @@ bool wouldRetireImport(const HostImports& state, const ImportRun& run) {
return previous.base + previous.bytes > run.base;
}

bool sameRange(const ImportFailure& entry, const GuestAllocations::Lease& lease) {
const auto current = entry.ranges.size() == 1 ? leasedRangeOwner(lease, entry.base, entry.base + entry.bytes) : nullptr;
if (current != nullptr && entry.bytes < current->bytes) return !entry.ranges.front().owner_before(current) && !current.owner_before(entry.ranges.front());
return sameRun(entry, importRun(lease, entry.base));
}

bool sameRange(const HostImport& entry, const GuestAllocations::Lease& lease) {
if (entry.ranges.size() == 1 && !entry.joined && entry.bytes < entry.ranges.front()->bytes) {
const auto current = leasedRangeOwner(lease, entry.base, entry.base + entry.bytes);
Expand Down Expand Up @@ -1265,7 +1271,7 @@ void refreshImports(const Context& context, HostImports& state, const GuestAlloc
retireImport(context.device, state, it, lease);
it = next;
}
for (auto it = state.failed.begin(); it != state.failed.end();) it = sameRun(it->second, importRun(lease, it->first)) ? std::next(it) : state.failed.erase(it);
for (auto it = state.failed.begin(); it != state.failed.end();) it = sameRange(it->second, lease) ? std::next(it) : state.failed.erase(it);
for (auto it = state.failedJoins.begin(); it != state.failedJoins.end();) it = sameRun(it->second, joinedRun(lease, it->second.base, it->second.base + it->second.bytes)) ? std::next(it) : state.failedJoins.erase(it);
}

Expand Down
33 changes: 33 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/HostImportSparseDmaBuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct MockDevice {
std::map<VkDeviceMemory, VkDeviceSize> memories;
std::vector<Bind> binds;
VkResult bindResult = VK_SUCCESS;
VkResult allocationResult = VK_SUCCESS;
std::uint64_t allocationAttempts = 0;
std::uint64_t nextAddress = 0x500000000ull;
};

Expand Down Expand Up @@ -100,6 +102,8 @@ VKAPI_ATTR void VKAPI_CALL mockGetBufferMemoryRequirements(VkDevice, VkBuffer bu
}

VKAPI_ATTR VkResult VKAPI_CALL mockAllocateMemory(VkDevice, const VkMemoryAllocateInfo* info, const VkAllocationCallbacks*, VkDeviceMemory* memory) {
++mock.allocationAttempts;
if (mock.allocationResult != VK_SUCCESS) return mock.allocationResult;
*memory = reinterpret_cast<VkDeviceMemory>(mock.next++);
mock.memories[*memory] = info->allocationSize;
return VK_SUCCESS;
Expand Down Expand Up @@ -263,6 +267,34 @@ void UnalignedOffsetIsDecidedBeforeImporting() {
Expect(mock.memories.size() == memories && mock.binds.empty(), "a chunk was imported or bound for a range that does not take the sparse path");
}

void RetainsFailedWindowAcrossUnrelatedMutation() {
constexpr std::uint64_t window = 4ull << 20u;
SharedRange range(window * 2, 0, true);
const auto context = mockContext();
SetHostImportRangeWindow(window);
mock.allocationResult = VK_ERROR_INVALID_EXTERNAL_HANDLE;
const auto address = range.Address() + window;
std::lock_guard gpu(AgcDriver::GuestMemory::GpuMutex());
const auto attempts = mock.allocationAttempts;
Expect(HostImportFor(context, address, Alignment) == nullptr, "the failed import window was accepted");
const auto failedAttempts = mock.allocationAttempts;
Expect(failedAttempts > attempts, "the import window did not reach allocation");
Expect(HostImportFor(context, address, Alignment) == nullptr && mock.allocationAttempts == failedAttempts, "an unchanged failed window was retried");
SharedRange unrelated(Alignment, 0, true);
Expect(HostImportFor(context, address, Alignment) == nullptr && mock.allocationAttempts == failedAttempts, "an unrelated registry change retried a failed import window");
Expect(HostImportFor(context, range.Address(), Alignment) == nullptr && mock.allocationAttempts > failedAttempts, "a different import window inherited the first window's failure");
const auto beforeRemap = mock.allocationAttempts;
{
GuestAllocations::Mutation mutation;
mutation.Remove(range.backing.base);
mutation.Add(range.backing.base, static_cast<std::size_t>(range.backing.bytes), true, true, true);
}
mock.allocationResult = VK_SUCCESS;
const auto* imported = HostImportFor(context, address, Alignment);
Expect(imported != nullptr && imported->base == address && imported->bytes == window && mock.allocationAttempts > beforeRemap, "a remapped failed import window was not retried successfully");
SetHostImportRangeWindow(64ull << 20u);
}

void FailureAfterTheDecisionThrows(bool sealed, VkResult bind, const char* step) {
const auto bytes = RangeBytes();
SharedRange range(bytes, 0, sealed);
Expand Down Expand Up @@ -297,6 +329,7 @@ int main() {
UnalignedOffsetIsDecidedBeforeImporting();
FailureAfterTheDecisionThrows(false, VK_SUCCESS, "UDMABUF_CREATE");
FailureAfterTheDecisionThrows(true, VK_ERROR_DEVICE_LOST, "vkQueueBindSparse");
RetainsFailedWindowAcrossUnrelatedMutation();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
Expand Down
Loading