chore: some random fixes (#5484)

* chore: some random fixes

* clang-format but 19

* texture cache hashing func is not thread safe
This commit is contained in:
Lander Gallastegi authored and GitHub committed 2026-10-22 08:29:57 +03:00
1 parent 617fe9e33f
commit 8ad3f49258
4 files changed
+27 -31

No files matched your search

+10 -10
View File
@@ -147,9 +147,9 @@ void MemoryManager::SetPrtArea(u32 id, VAddr address, u64 size) {
void MemoryManager::CopySparseMemory(VAddr virtual_addr, u8* dest, u64 size) {
const auto& backing_pages = impl.BackingPages();
while (size) {
const u64 page = virtual_addr >> 14;
const u64 offset_in_page = virtual_addr % 16_KB;
const u64 copy_size = std::min<u64>(16_KB - offset_in_page, size);
const u64 page = virtual_addr >> GUEST_PAGE_BITS;
const u64 offset_in_page = virtual_addr % GUEST_PAGE_SIZE;
const u64 copy_size = std::min<u64>(GUEST_PAGE_SIZE - offset_in_page, size);
if (auto* entry = backing_pages.find(page); entry && *entry) {
std::memcpy(dest, *entry + offset_in_page, copy_size);
} else {
@@ -245,7 +245,7 @@ PAddr MemoryManager::PoolExpand(PAddr search_start, PAddr search_end, u64 size,
PAddr MemoryManager::Allocate(PAddr search_start, PAddr search_end, u64 size, u64 alignment,
s32 memory_type) {
std::scoped_lock lk{mutex, unmap_mutex};
alignment = alignment > 0 ? alignment : 16_KB;
alignment = alignment > 0 ? alignment : GUEST_PAGE_SIZE;
auto dmem_area = FindDmemArea(search_start);
auto mapping_start =
@@ -573,7 +573,7 @@ s32 MemoryManager::MapMemory(void** out_addr, VAddr virtual_addr, u64 size, Memo
}
} else if (False(flags & MemoryMapFlags::Fixed)) {
// Find a free virtual addr to map
alignment = alignment > 0 ? alignment : 16_KB;
alignment = alignment > 0 ? alignment : GUEST_PAGE_SIZE;
virtual_addr = virtual_addr == 0 ? DEFAULT_MAPPING_BASE : virtual_addr;
virtual_addr = SearchFree(virtual_addr, size, alignment);
if (virtual_addr == -1) {
@@ -764,7 +764,7 @@ s32 MemoryManager::MapFile(void** out_addr, VAddr virtual_addr, u64 size, Memory
}
} else if (False(flags & MemoryMapFlags::Fixed)) {
virtual_addr = virtual_addr == 0 ? DEFAULT_MAPPING_BASE : virtual_addr;
virtual_addr = SearchFree(virtual_addr, size, 16_KB);
virtual_addr = SearchFree(virtual_addr, size, GUEST_PAGE_SIZE);
if (virtual_addr == -1) {
// No suitable memory areas to map to
return ORBIS_KERNEL_ERROR_ENOMEM;
@@ -917,8 +917,8 @@ s32 MemoryManager::UnmapMemory(VAddr virtual_addr, u64 size) {
std::scoped_lock lk{unmap_mutex};
// Align address and size appropriately
virtual_addr = Common::AlignDown(virtual_addr, 16_KB);
size = Common::AlignUp(size, 16_KB);
virtual_addr = Common::AlignDown(virtual_addr, GUEST_PAGE_SIZE);
size = Common::AlignUp(size, GUEST_PAGE_SIZE);
if (!IsValidMapping(virtual_addr, size)) {
LOG_ERROR(Kernel_Vmm, "addr = {:#x} size = {:#x} is outside the memory map", virtual_addr,
size);
@@ -1360,9 +1360,9 @@ void MemoryManager::NameVirtualRange(VAddr virtual_addr, u64 size, std::string_v
std::scoped_lock lk{mutex, unmap_mutex};
// Sizes are aligned up to the nearest 16_KB
u64 aligned_size = Common::AlignUp(size, 16_KB);
u64 aligned_size = Common::AlignUp(size, GUEST_PAGE_SIZE);
// Addresses are aligned down to the nearest 16_KB
VAddr aligned_addr = Common::AlignDown(virtual_addr, 16_KB);
VAddr aligned_addr = Common::AlignDown(virtual_addr, GUEST_PAGE_SIZE);
ASSERT_MSG(IsValidMapping(aligned_addr, aligned_size),
"Attempted to access invalid address {:#x}", aligned_addr);
@@ -713,12 +713,13 @@ void Instance::CollectPhysicalMemoryInfo() {
// We reserve some memory for the system.
const u64 system_memory = std::min<u64>(total_memory_budget / 8, 1_GB);
total_memory_budget -= system_memory;
return;
} else {
// Leave at least 8 GB for the system on integrated GPUs.
const s64 available_memory = static_cast<s64>(total_memory_budget - device_initial_usage);
total_memory_budget =
static_cast<u64>(std::max<s64>(available_memory - 8_GB, static_cast<s64>(2_GB)));
}
// Leave at least 8 GB for the system on integrated GPUs.
const s64 available_memory = static_cast<s64>(total_memory_budget - device_initial_usage);
total_memory_budget =
static_cast<u64>(std::max<s64>(available_memory - 8_GB, static_cast<s64>(2_GB)));
LOG_INFO(Render_Vulkan, "GPU memory budget is {} MiB", total_memory_budget / 1_MB);
}
void Instance::CollectImageFormatInfo() {
+2 -3
View File
@@ -33,9 +33,8 @@ enum ImageFlagBits : u32 {
GpuDirty = 1 << 2, ///< Image contents have been modified from the GPU (valid data in buffer
///< cache)
Dirty = MaybeCpuDirty | CpuDirty | GpuDirty,
GpuModified = 1 << 3, ///< Contents have been modified from the GPU
MaybeGpuDirty = 1 << 4, ///< Image contents may have been modified in the buffer cache
Registered = 1 << 6, ///< True when the image is registered
GpuModified = 1 << 3, ///< Contents have been modified from the GPU
Registered = 1 << 6, ///< True when the image is registered
};
DECLARE_ENUM_FLAG_OPERATORS(ImageFlagBits)
+9 -13
View File
@@ -29,23 +29,24 @@ static constexpr u32 MAX_SAMPLERS = std::numeric_limits<u16>::max();
static u64 HashBackingMemory(VAddr virtual_addr, u64 size) {
static constexpr std::array<u8, Core::GUEST_PAGE_SIZE> zeros{};
static const std::unique_ptr<XXH3_state_t, decltype(&XXH3_freeState)> state{XXH3_createState(),
XXH3_freeState};
auto* state = XXH3_createState();
const auto& backing_pages = Core::Memory::Instance()->GetAddressSpace().BackingPages();
XXH3_64bits_reset(state.get());
XXH3_64bits_reset(state);
while (size) {
const u64 page = virtual_addr >> Core::GUEST_PAGE_BITS;
const u64 offset_in_page = virtual_addr & (Core::GUEST_PAGE_SIZE - 1);
const u64 hash_size = std::min<u64>(Core::GUEST_PAGE_SIZE - offset_in_page, size);
if (auto* entry = backing_pages.find(page); entry && *entry) {
XXH3_64bits_update(state.get(), *entry + offset_in_page, hash_size);
XXH3_64bits_update(state, *entry + offset_in_page, hash_size);
} else {
XXH3_64bits_update(state.get(), zeros.data(), hash_size);
XXH3_64bits_update(state, zeros.data(), hash_size);
}
size -= hash_size;
virtual_addr += hash_size;
}
return XXH3_64bits_digest(state.get());
const u64 hash = XXH3_64bits_digest(state);
XXH3_freeState(state);
return hash;
}
TextureCache::TextureCache(const Vulkan::Instance& instance_, Vulkan::Scheduler& scheduler_,
@@ -235,7 +236,6 @@ void TextureCache::MarkAsMaybeGpuDirty(VAddr addr, size_t size) {
// Read backing pages directly, without faulting through the guest readback path.
image.gpu_hash = HashBackingMemory(image.info.guest_address, image.info.guest_size);
}
image.flags |= ImageFlagBits::MaybeGpuDirty;
});
}
@@ -574,9 +574,6 @@ ImageId TextureCache::ExpandImage(const ImageInfo& info, ImageId image_id) {
const bool src_has_gpu_data = src_image.HasGpuValidData();
RefreshImage(new_image);
if (src_has_gpu_data) {
runtime.CopyImage(&src_image, &new_image);
}
if (src_image.binding.is_bound || src_image.binding.is_target) {
src_image.binding.needs_rebind = 1u;
@@ -585,7 +582,7 @@ ImageId TextureCache::ExpandImage(const ImageInfo& info, ImageId image_id) {
TrackImage(new_image_id);
new_image.flags &= ~ImageFlagBits::Dirty;
if (src_has_gpu_data) {
new_image.flags |= ImageFlagBits::GpuModified;
runtime.CopyImage(&src_image, &new_image);
}
FreeImage(image_id);
@@ -1087,7 +1084,7 @@ void TextureCache::GarbageCollectImages() {
if (download) {
UntrackImage(image_id);
UnregisterImage(image_id);
if (True(image.flags & ImageFlagBits::MaybeGpuDirty)) {
if (image.gpu_hash != 0) {
buffer_cache.ReadMemory(image.info.guest_address, image.info.guest_size, false,
true, true);
maybe_download.push_back(image_id);
@@ -1207,7 +1204,6 @@ void TextureCache::TouchImage(Image& image) {
image_lru_cache.Touch(image, gc_tick);
// Image is still valid
image.flags &= ~ImageFlagBits::MaybeGpuDirty;
image.gpu_hash = 0;
}