diff --git a/src/asahi/vulkan/hk_device_memory.c b/src/asahi/vulkan/hk_device_memory.c index 3143a860cc04..1b40bfff6511 100644 --- a/src/asahi/vulkan/hk_device_memory.c +++ b/src/asahi/vulkan/hk_device_memory.c @@ -482,12 +482,12 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, * BO: a request whose page rounding below wraps to 0 would otherwise * reach agx_bo_create() as a 0-byte BO and fail in the kernel. */ if (!(fd_info && fd_info->handleType) && - pAllocateInfo->allocationSize > heap->size) { + pAllocateInfo->allocationSize > heap->allocation_limit) { return vk_errorf(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY, - "allocationSize %" PRIu64 " exceeds heap %u " + "allocationSize %" PRIu64 " exceeds heap %u allocation limit " "(%" PRIu64 " bytes)", (uint64_t)pAllocateInfo->allocationSize, - type->heapIndex, (uint64_t)heap->size); + type->heapIndex, (uint64_t)heap->allocation_limit); } /* The spec requires allocationSize > 0 when nothing is imported @@ -499,7 +499,7 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, const uint64_t aligned_size = align64(MAX2(pAllocateInfo->allocationSize, 1), alignment); uint64_t used = p_atomic_read(heap->used); - if (used > heap->size || aligned_size > heap->size - used) + if (used > heap->allocation_limit || aligned_size > heap->allocation_limit - used) return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); mem = vk_device_memory_create(&dev->vk, pAllocateInfo, pAllocator, @@ -707,7 +707,7 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, hk_set_memory_priority(dev, mem, priority_info->priority); uint64_t heap_used = p_atomic_add_return(heap->used, mem->bo->size); - if (heap_used > heap->size) { + if (heap_used > heap->allocation_limit) { hk_FreeMemory(device, hk_device_memory_to_handle(mem), pAllocator); return vk_errorf(device, VK_ERROR_OUT_OF_DEVICE_MEMORY, "Out of heap memory"); diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index 88434d37fa22..9ae1f047c7f6 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -2129,7 +2129,7 @@ hk_physical_device_free_disk_cache(struct hk_physical_device *pdev) } uint64_t -hk_get_sysmem_heap_size(struct hk_physical_device *pdev) +hk_get_sysmem_heap_size(struct hk_physical_device *pdev, uint64_t *allocation_limit) { uint64_t total_ram_B = 0; const bool has_mem = os_get_total_physical_memory(&total_ram_B); @@ -2141,6 +2141,7 @@ hk_get_sysmem_heap_size(struct hk_physical_device *pdev) } else { pdev->sysmem_heap_percent = 1.0f; } + *allocation_limit = pdev->sysmem; return pdev->sysmem; } @@ -2148,10 +2149,20 @@ hk_get_sysmem_heap_size(struct hk_physical_device *pdev) return 0; struct hk_instance *instance = hk_physical_device_instance(pdev); - return hk_resolve_uma_sysmem_heap( + const uint64_t heap = hk_resolve_uma_sysmem_heap( total_ram_B, instance->drirc.misc.heap_memory_percent, pdev->dev.has_memory_limits, pdev->dev.params.memory_limit, pdev->dev.params.client_memory_limit, &pdev->sysmem_heap_percent); + /* Without an advertised cap, preserve the pre-all-RAM admission limit. + * The heap property still describes the entire addressable UMA heap. */ + bool uncapped = !pdev->dev.has_memory_limits || + (!pdev->dev.params.memory_limit && + !pdev->dev.params.client_memory_limit); + bool heuristic = instance->drirc.misc.heap_memory_percent <= OS_GPU_HEAP_SIZE_HEURISTIC; + *allocation_limit = uncapped && heuristic + ? ROUND_DOWN_TO(total_ram_B / 2, 1ull << 20) + : heap; + return heap; } static simple_mtx_t hk_heap_usage_lock = SIMPLE_MTX_INITIALIZER; @@ -2431,7 +2442,8 @@ hk_create_drm_physical_device(struct vk_instance *_instance, } } - uint64_t sysmem_size_B = hk_get_sysmem_heap_size(pdev); + uint64_t allocation_limit; + uint64_t sysmem_size_B = hk_get_sysmem_heap_size(pdev, &allocation_limit); if (sysmem_size_B == 0) { result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED, "Failed to query total system memory"); @@ -2447,6 +2459,7 @@ hk_create_drm_physical_device(struct vk_instance *_instance, uint32_t sysmem_heap_idx = pdev->mem_heap_count++; pdev->mem_heaps[sysmem_heap_idx] = (struct hk_memory_heap){ .size = sysmem_size_B, + .allocation_limit = allocation_limit, .used = &pdev->heap_usage->used[sysmem_heap_idx], .cached = &pdev->heap_usage->cached[sysmem_heap_idx], .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT, @@ -2696,7 +2709,12 @@ hk_GetPhysicalDeviceMemoryProperties2( &pdev->vk, percent, heap->size, used); } - p->heapBudget[i] = budget; + /* On a failed query or a sub-MiB estimate, report only what the + * application already holds. Every advertised heap needs a + * positive budget, even when it is empty. */ + if (budget == 0) + budget = allocated; + p->heapBudget[i] = MAX2(1, MIN2(budget, heap->allocation_limit)); } /* From the Vulkan 1.3.278 spec: diff --git a/src/asahi/vulkan/hk_physical_device.h b/src/asahi/vulkan/hk_physical_device.h index cc13212f7482..2ad0ed54fc43 100644 --- a/src/asahi/vulkan/hk_physical_device.h +++ b/src/asahi/vulkan/hk_physical_device.h @@ -47,7 +47,7 @@ struct hk_heap_usage { }; struct hk_memory_heap { - uint64_t size; + uint64_t size, allocation_limit; /* Point into the physical device's hk_heap_usage. */ uint64_t *used; p_atomic_uint64_t *cached; @@ -143,7 +143,7 @@ void hk_border_emulation_remember(struct hk_physical_device *pdev); void hk_min_lod_emulation_recall(struct hk_physical_device *pdev); void hk_min_lod_emulation_remember(struct hk_physical_device *pdev); -uint64_t hk_get_sysmem_heap_size(struct hk_physical_device *pdev); +uint64_t hk_get_sysmem_heap_size(struct hk_physical_device *pdev, uint64_t *allocation_limit); /* Size Borealis's UMA system-memory heap from the kernel's exported memacct * admission capacities (drm_asahi_params_global::memory_limit and @@ -158,8 +158,11 @@ uint64_t hk_get_sysmem_heap_size(struct hk_physical_device *pdev); * heap never exceeds kernel admission (and able to raise the heap above 50% * when driver limits are disabled). * - With OS_GPU_HEAP_SIZE_HEURISTIC (0.0f), use kernel_limit_B when non-zero, - * or cap at 50% of total_ram_B when kernel_limit_B is 0 (both driver limits - * disabled, legacy 600-byte kernels, and virtio) to avoid swapping. + * else all of total_ram_B (both driver limits disabled, legacy 600-byte + * kernels, and virtio). The heap is the memory a device can address, not + * what it can get: VK_EXT_memory_budget's heapBudget follows free memory, + * and the kernel refuses what the system or the client's memory cgroup + * cannot back. */ static inline uint64_t hk_resolve_uma_sysmem_heap(uint64_t total_ram_B, float driconf_pct, @@ -192,7 +195,7 @@ hk_resolve_uma_sysmem_heap(uint64_t total_ram_B, float driconf_pct, } else if (kernel_limit_B > 0) { heap_B = ROUND_DOWN_TO(MIN2(kernel_limit_B, total_ram_B), 1ull << 20); } else { - heap_B = ROUND_DOWN_TO(total_ram_B / 2, 1ull << 20); + heap_B = total_aligned_B; } if (percent_out) { diff --git a/src/asahi/vulkan/tests/test-hk-memory-oom.c b/src/asahi/vulkan/tests/test-hk-memory-oom.c index 1cbe0effedcb..e53fedbeaa02 100644 --- a/src/asahi/vulkan/tests/test-hk-memory-oom.c +++ b/src/asahi/vulkan/tests/test-hk-memory-oom.c @@ -447,6 +447,7 @@ main(void) pdev->heap_usage = &heap_usage; pdev->mem_types[0].heapIndex = 0; pdev->mem_heaps[0].size = 8 * 1024 * MIB; + pdev->mem_heaps[0].allocation_limit = pdev->mem_heaps[0].size; pdev->mem_heaps[0].used = &heap_usage.used[0]; pdev->mem_heaps[0].cached = &heap_usage.cached[0]; simple_mtx_init(&dev->dev.bo_map_lock, mtx_plain); @@ -569,13 +570,17 @@ main(void) hk_FreeMemory(hk_device_to_handle(dev), memory, &allocator); assert(!pinned && !host_live && !dev->dev.bo_cache.size); - /* Physical heap and alignment failures must also precede BO allocation. */ - pdev->mem_heaps[0].size = 512 * MIB; + /* The advertised heap can exceed the capacity for fresh allocations. */ + pdev->mem_heaps[0].allocation_limit = 512 * MIB; before = bo_calls; assert(alloc(dev, 1024 * MIB, &memory) == VK_ERROR_OUT_OF_DEVICE_MEMORY); assert(alloc(dev, UINT64_MAX, &memory) == VK_ERROR_OUT_OF_DEVICE_MEMORY); + heap_usage.used[0] = 511 * MIB; + assert(alloc(dev, 2 * MIB, &memory) == VK_ERROR_OUT_OF_DEVICE_MEMORY); + assert(heap_usage.used[0] == 511 * MIB); + heap_usage.used[0] = 0; assert(bo_calls == before); - pdev->mem_heaps[0].size = 8 * 1024 * MIB; + pdev->mem_heaps[0].allocation_limit = 8 * 1024 * MIB; /* Missing/unlimited cgroups keep normal allocation behavior. */ limited = false; diff --git a/src/asahi/vulkan/tests/test-hk-uma-heap-sizing.c b/src/asahi/vulkan/tests/test-hk-uma-heap-sizing.c index 6884e7006cc7..8d35f2c47f0c 100644 --- a/src/asahi/vulkan/tests/test-hk-uma-heap-sizing.c +++ b/src/asahi/vulkan/tests/test-hk-uma-heap-sizing.c @@ -136,8 +136,8 @@ test_resolve_uma_sysmem_heap(void) assert(hk_resolve_uma_sysmem_heap(0, 0.0f, true, GIB(4), GIB(4), &pct) == 0); assert(pct == -1.0f); - /* Legacy kernel / virtio (has_memory_limits = false) preserves the 50% - * fallback across UMA tiers even if struct fields are non-zero. + /* Legacy kernel / virtio (has_memory_limits = false) reports all of RAM + * across UMA tiers even if struct fields are non-zero. */ const uint64_t tiers[] = { MIB(7400), GIB(8), MIB(15800), MIB(31800), MIB(63742), GIB(128), @@ -146,12 +146,12 @@ test_resolve_uma_sysmem_heap(void) uint64_t ram = tiers[i]; pct = 0.0f; uint64_t heap = hk_resolve_uma_sysmem_heap(ram, 0.0f, false, 0, 0, &pct); - assert(heap == ROUND_DOWN_TO(ram / 2, MIB(1))); - assert(near_f(pct, 0.5f, 0.001f)); + assert(heap == ROUND_DOWN_TO(ram, MIB(1))); + assert(near_f(pct, 1.0f, 0.001f)); } assert(hk_resolve_uma_sysmem_heap(GIB(64), 0.0f, false, GIB(48), GIB(48), - &pct) == GIB(32)); - assert(pct == 0.5f); + &pct) == GIB(64)); + assert(pct == 1.0f); /* Default Aurora kernel (mem_limit_pct = 67, client_mem_limit_pct = 50) * sizes the heap to the 50% per-client admission limit. @@ -209,30 +209,22 @@ test_resolve_uma_sysmem_heap(void) } /* Disabled driver limits on a 616-byte Aurora kernel (has_memory_limits = - * true, limits = 0, 0) keep Borealis's 50% default cap to avoid swapping - * unless explicitly raised via driconf. + * true, limits = 0, 0) report all of RAM; heapBudget is the live signal. */ for (unsigned i = 0; i < ARRAY_SIZE(tiers); i++) { uint64_t ram = tiers[i]; pct = 0.0f; uint64_t heap = hk_resolve_uma_sysmem_heap(ram, 0.0f, true, 0, 0, &pct); - assert(heap == ROUND_DOWN_TO(ram / 2, MIB(1))); - assert(near_f(pct, 0.5f, 0.001f)); - } - { - const uint64_t ram = MIB(63742); - pct = 0.0f; - uint64_t heap = hk_resolve_uma_sysmem_heap(ram, 0.0f, true, 0, 0, &pct); - assert(heap == ROUND_DOWN_TO(ram / 2, MIB(1))); - assert(pct == 0.5f); + assert(heap == ROUND_DOWN_TO(ram, MIB(1))); + assert(near_f(pct, 1.0f, 0.001f)); } /* Explicit driconf heap_memory_percent overrides: * - Lower than kernel limit (0.25 with 50% kernel limit) -> 25%. * - Higher than kernel limit (0.80 / 0.90 with 67% / 50% kernel limits) -> * clamped to 50% so VkMemoryHeap::size never exceeds kernel admission. - * - Higher overrides with disabled driver limits (0, 0) -> raises the heap - * above the 50% default cap (80%, 90%, 100%). + * - Overrides with disabled driver limits (0, 0) set the heap to that + * share of RAM (80%, 90%, 100%). */ { const uint64_t ram = GIB(64); @@ -295,6 +287,9 @@ test_memory_budget_multi_pdev(void) { struct hk_instance instance = {0}; vk_object_base_init(NULL, &instance.vk.base, VK_OBJECT_TYPE_INSTANCE); + vk_instance_to_handle(&instance.vk); + list_inithead(&instance.vk.debug_utils.callbacks); + list_inithead(&instance.vk.debug_report.callbacks); instance.drirc.misc.heap_memory_percent = OS_GPU_HEAP_SIZE_HEURISTIC; fake_total_ram_B = GIB(64); @@ -302,7 +297,7 @@ test_memory_budget_multi_pdev(void) fake_meminfo_ok = true; /* Two physical devices under one VkInstance: pdev_a has 82% kernel limits, - * pdev_b is a legacy/virtio device without memory limits (50% fallback). + * pdev_b is a legacy/virtio device without memory limits (all of RAM). * Resolving pdev_a must not mutate instance.drirc.misc.heap_memory_percent * or corrupt pdev_b, and VK_EXT_memory_budget must report non-zero scaled * budgets <= heap.size on both devices. @@ -318,18 +313,26 @@ test_memory_budget_multi_pdev(void) pdev_b.dev.has_memory_limits = false; - pdev_a.mem_heaps[0].size = hk_get_sysmem_heap_size(&pdev_a); + pdev_a.mem_heaps[0].size = hk_get_sysmem_heap_size( + &pdev_a, &pdev_a.mem_heaps[0].allocation_limit); assert(instance.drirc.misc.heap_memory_percent == OS_GPU_HEAP_SIZE_HEURISTIC); - pdev_b.mem_heaps[0].size = hk_get_sysmem_heap_size(&pdev_b); + pdev_b.mem_heaps[0].size = hk_get_sysmem_heap_size( + &pdev_b, &pdev_b.mem_heaps[0].allocation_limit); assert(instance.drirc.misc.heap_memory_percent == OS_GPU_HEAP_SIZE_HEURISTIC); assert(pdev_a.mem_heaps[0].size == ROUND_DOWN_TO(pdev_a.dev.params.client_memory_limit, MIB(1))); - assert(pdev_b.mem_heaps[0].size == GIB(32)); + assert(pdev_b.mem_heaps[0].size == GIB(64)); + assert(pdev_a.mem_heaps[0].allocation_limit == pdev_a.mem_heaps[0].size); + assert(pdev_b.mem_heaps[0].allocation_limit == GIB(32)); + pdev_b.dev.has_memory_limits = true; + pdev_b.dev.params.memory_limit = pdev_b.dev.params.client_memory_limit = 0; + assert(hk_get_sysmem_heap_size(&pdev_b, &pdev_b.mem_heaps[0].allocation_limit) == GIB(64)); + assert(pdev_b.mem_heaps[0].allocation_limit == GIB(32)); assert(near_f(pdev_a.sysmem_heap_percent, 0.82f, 0.002f)); - assert(pdev_b.sysmem_heap_percent == 0.5f); + assert(pdev_b.sysmem_heap_percent == 1.0f); usage_a.used[0] = GIB(2); usage_b.used[0] = GIB(1); @@ -367,17 +370,17 @@ test_memory_budget_multi_pdev(void) assert(budget_b.heapBudget[0] > 0); assert(budget_b.heapBudget[0] <= props_b.memoryProperties.memoryHeaps[0].size); uint64_t expected_b = vk_physical_device_heap_budget( - fake_avail_ram_B, 0.9f * 0.5f, pdev_b.mem_heaps[0].size, + fake_avail_ram_B, 0.9f * 1.0f, pdev_b.mem_heaps[0].size, usage_b.used[0]); - assert(budget_b.heapBudget[0] == expected_b); - assert(budget_a.heapBudget[0] > budget_b.heapBudget[0]); + assert(budget_b.heapBudget[0] == MIN2(expected_b, GIB(32))); } /* HK_SYSMEM override also populates sysmem_heap_percent so the fallback * branch when used >= sysmem still reports a non-zero budget. */ pdev_b.sysmem = GIB(8); - pdev_b.mem_heaps[0].size = hk_get_sysmem_heap_size(&pdev_b); + pdev_b.mem_heaps[0].size = hk_get_sysmem_heap_size( + &pdev_b, &pdev_b.mem_heaps[0].allocation_limit); assert(pdev_b.mem_heaps[0].size == GIB(8)); assert(pdev_b.sysmem_heap_percent == 0.125f); usage_b.used[0] = GIB(8); @@ -392,6 +395,22 @@ test_memory_budget_multi_pdev(void) hk_physical_device_to_handle(&pdev_b), &props_sysmem); assert(budget_sysmem.heapBudget[0] == GIB(8)); + pdev_b.sysmem = 0; + fake_meminfo_ok = false; + usage_b.used[0] = MIB(2); + hk_GetPhysicalDeviceMemoryProperties2( + hk_physical_device_to_handle(&pdev_b), &props_sysmem); + assert(budget_sysmem.heapBudget[0] == MIB(2)); + + fake_meminfo_ok = true; + fake_avail_ram_B = 1; + usage_b.used[0] = 0; + hk_GetPhysicalDeviceMemoryProperties2( + hk_physical_device_to_handle(&pdev_b), &props_sysmem); + assert(budget_sysmem.heapBudget[0] == 1); + for (unsigned i = 1; i < VK_MAX_MEMORY_HEAPS; ++i) + assert(budget_sysmem.heapBudget[i] == 0); + vk_object_base_finish(&pdev_b.vk.base); vk_object_base_finish(&pdev_a.vk.base); vk_object_base_finish(&instance.vk.base);