Repository navigation
borealis: fix host image copies after unmapped or placed maps, and refuse fd imports smaller than allocationSize - #582
Conversation
3b18094 to
c5ac4f4
Compare
RyanTheTide
left a comment
There was a problem hiding this comment.
No blockers. Both fixes are correct, and the evidence is good: a host test that fails before and passes after for each fix, plus the on-device mapping-state test. I checked the fd-import error path. The BO is unreferenced and mem->bo cleared before fail_alloc. The fd is closed only on the success path, so it stays with the caller as the spec requires. The check uses the raw allocationSize, as anv does. The placed-map interplay also holds: agx_bo_map() returns an existing placed mapping as is, a later placed vkMapMemory2 relocates a mapping that a copy created, and memoryMapRangePlaced is false, so the copy's map + bo_offset_B arithmetic stays valid.
- hk_image.c:2351, hk_image_plane_map(): the unserialized first map is now reached from vkCopyMemoryToImage/vkCopyImageToMemory. Those calls don't require external synchronization on the VkDeviceMemory, and texture-streaming threads call them in parallel by design. Two threads copying into different images in the same never-mapped allocation can both see
_map == NULL, so both mmap, one mapping leaks, and_map_sizecan be torn. Before this change the race was confined to vkBindImageMemory, which applications rarely run in parallel on one allocation. Fix: take the first map under a lock, either a per-BO lock or the device's BO lock, or install it with a compare-and-swap onbo->_mapand munmap the loser. - Testing: please name the dEQP-VK cases that exercise this, if there are any. In the tables the host_image_copy and map_placed results are identical before and after, so CTS apparently doesn't cover either bug. Saying so in the PR would make clear that the new host tests are the only regression guard.
- Naming: both commit subjects use the "hk:" prefix. Please use "borealis:" in the commit messages.
Assisted-by: LLM (Claude:Opus 5.5)
|
Both fixes look correct to me. The import check releases only the reference agx_bo_import took and leaves the fd with the caller. I walked the host-copy lookup through unmap, unmap-with-reserve, placed map and the shared/owned-mapping cases against hk_MapMemory2KHR/hk_UnmapMemory2KHR, and each one ends up on the right mapping. One follow-up worth considering. Host image copies are mostly issued from worker threads, so the unserialized
Neither is a regression, since both sequences were already broken with the pointer taken at bind. But giving host copies a mapping of their own would decouple them from the application's map state entirely. That could be agx_bo_map_owned, kept on the memory object, with the shared mapping as a fallback where the backend can't map twice. Holding bo_map_lock around the lookup-or-map would at least fix the first case. Either could be a separate change. Assisted-by: LLM (Claude:Opus 5.5) |
c5ac4f4 to
cfae811
Compare
|
Thanks for both reviews. Taking the points in order, with your follow-up folded into the first. 1. The first map at copy time, and a placed map under a running copy. I took your structural suggestion, since it closes both cases and a lock around lookup-or-map would close only the first.
The same first map from other entry points. I used a lock there rather than a compare-and-swap for one reason. The backend interface allows a backend that can't map a BO twice, which is the case the copies fall back for, and a CAS needs the losing thread to make a second mapping. The cost is one uncontended lock per first map of a BO. Mapping reuse, the BO cache's included, is unchanged. 2. dEQP-VK coverage. None of it catches either bug. I checked the CTS source:
The host tests are the regression guard, and the PR body now says so. The table below includes a 3. Naming. Merged Erik's Reported-by and Suggested-by stay on both of the first two commits. The first commit adds a Suggested-by for you, since its design is now yours. TestsHost (
On the Mac Studio, base against head, same boot:
All runs on the Mac Studio (M5 Ultra, J775D) in one boot, the pack's kernel on the 27.2 stub, with #362 alone (the base) and #362 with #363, #582 and #583 on top (the head) installed side by side,
The four The PR body is updated to match (the changes, the third commit, the coverage note and these rows). |
There was a problem hiding this comment.
No blockers. Re-checked at cfae811 against c5ac4f4. Both points from our review are resolved. The design is sound.
- Copy mapping:
hk_device_memory_copy_map()gives the memory object its ownagx_bo_map_owned()mapping, and the compare-and-swap installs the first one; the losing thread unmaps its own.vkMapMemory2/vkUnmapMemory2never touch that mapping, so a placed map or an unmap on another thread can no longer pull memory out from under a running copy.hk_FreeMemory()unmappingbo->sizematches what both backends map when there is no fixed address. - Commit 3: the double-checked lock in
agx_bo_map_placed()closes the first-map leak for every caller, Gallium included._map_sizeis written before the release store of_map, and the unlocked fast path reads with acquire, so a reader never sees a torn size. - CTS coverage: the note that no dEQP-VK case catches either bug, and that the host tests are the regression guard, is what we were after.
One small thing, not blocking. src/asahi/lib/agx_device.h:420-425: agx_bo_map_placed() now maps through a copy of the BO on every backend. The ops comment (agx_device.h:117-120) promises that only for backends with bo_mmap_independent, and a backend without the flag is the reason you gave for a lock instead of a CAS. Both backends set the flag today, so nothing breaks. Either call bo_mmap(bo->dev, bo, ...) directly when the flag is clear, or widen the ops comment so every backend must leave the source BO untouched.
On the hk: prefix: see our note on #363 (please use borealis:).
Assisted-by: LLM (Claude:Opus 5.5)
…d placed, on any thread hk_image_plane_bind() kept agx_bo_map(mem->bo) plus the plane's offset for the lifetime of the image, and vkCopyMemoryToImageEXT, vkCopyImageToMemoryEXT and vkCopyImageToImageEXT copied through that pointer. For memory that is neither exported nor imported, the BO's mapping is also the one vkMapMemory2 gives the application: vkUnmapMemory2 removes it, and a VK_EXT_map_memory_placed map moves it to the address the application chose. A host image copy after either went through a range that no longer maps the memory, and faulted or accessed whatever had been mapped there since. Host image copies need no external synchronization on the memory, so a copy on one thread could also lose its mapping to a vkMapMemory2 or vkUnmapMemory2 on another. Give the copies a mapping of their own instead. The plane keeps its memory object and its offset in the BO, and the first host image copy of the memory maps the BO with agx_bo_map_owned(), a mapping that lives until vkFreeMemory. vkMapMemory2 and vkUnmapMemory2 map, move and unmap only the BO's shared mapping or the memory object's own, never the one the copies use, so a range unmapped with VK_MEMORY_UNMAP_RESERVE_BIT_EXT stays reserved and a copy is never left without its mapping. When two threads copy at once into memory that no copy has mapped yet, both map it: a compare-and-swap keeps the mapping installed first, and the other is unmapped. Host memory, which stays where the application put it, and a backend that cannot map a BO twice use the BO's shared mapping. A BO that cannot be mapped fails the copy with VK_ERROR_MEMORY_MAP_FAILED. Binding an image no longer maps its memory on the CPU. The code is common to every GPU the driver supports, and the change is the same on each. Memory that host image copies use gets one more CPU mapping of its BO, at the first copy. hk_host_image_copy covers copies after an unmap, through a placed mapping, after an unmap that keeps the range reserved, a placed map and an unmap while the copies' mapping exists, and two threads copying into memory that no copy has mapped yet. Reported-by: Erik Bourget <erik@ebourget.net> Suggested-by: Erik Bourget <erik@ebourget.net> Suggested-by: Ryan Murray <ryan@aurorasilicon.org> Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
Importing an opaque fd or a dma-buf took the BO's size from the imported object and kept the caller's allocationSize as the size of the memory object, without comparing the two. An image bound to such memory is checked against the BO (hk_image_plane_bind()), but a buffer is bounded only by allocationSize, so a buffer bound to memory imported with a size larger than the object addressed GPU memory past the end of the BO. Fail such an import with VK_ERROR_INVALID_EXTERNAL_HANDLE, as anv does, releasing the BO and leaving the fd with the caller. Imports whose object is at least allocationSize bytes are unchanged. The code is common to every GPU the driver supports, and the change is the same on each. hk_memory_oom now also imports objects smaller than, equal to and larger than allocationSize. Reported-by: Erik Bourget <erik@ebourget.net> Suggested-by: Erik Bourget <erik@ebourget.net> Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…so no mapping leaks agx_bo_map() mapped a BO on its first use without serialization. Two threads mapping one BO for the first time at once, such as two contexts mapping a shared resource, each mapped it and stored their mapping in the BO, so one mapping leaked and _map_size could be the other thread's. The move of a mapping to a placed address (agx_bo_map_placed()) wrote the same two fields the same way. Map a BO, or move its mapping, under a device lock, and check again under it; a BO already mapped where it is wanted is returned without the lock, as before. On a backend with bo_mmap_independent the mapping is made into a copy of the BO and stored after its size, so a thread that finds it without the lock finds a whole mapping, never a failed one. A backend without it maps the BO in place under the lock. This takes a lock rather than installing the mapping with a compare-and-swap because the backend interface allows one that cannot map a BO twice (bo_mmap_independent), and a compare-and-swap needs the losing thread to make a second mapping. This runs for every GPU, in the Gallium and Vulkan drivers: an uncontended lock on each first map of a BO. Mapping reuse, the BO cache's included, is unchanged. hk_host_image_copy now also maps a BO from two threads at once and checks that it is mapped once, also on a backend without bo_mmap_independent, where the BO itself must be the one mapped. Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
cfae811 to
f246170
Compare
|
Thanks. Fixed in the code rather than the comment, amended into the third commit: when |
|
Re-review of f246170: No blockers.
Assisted-by: LLM (Claude:Opus 5.5) |
|
M4 Pro (J616s, G17P) no-regression row. A = aurora-wip
|
|
J413 (Apple M2, G14G B0), one unchanged 13.5-path boot on Aurora Linux 7.3.0-aurora-fleet85. Vulkan CTS 1.4.7.1 (
The image run included all 9,456 names absent from both prior J413 archives, with 2,256 Pass and 7,200 NotSupported on both builds. No GPU faults, crashes, timeouts or SELinux denials in the four windows. The host regressions in this PR cover copy after unmap and placed-map behavior. The separate 45-case focus reproduced subgroup termination Fail → Pass with the merged fix from #245. Conformance-version metadata remained the only Fail on the newer build and is tracked in #345. |
Three fixes for CPU access to memory that the driver reached through a stale pointer or past the end of a BO, or that leaked a mapping when two threads mapped a BO at once.
Changes
hk_image_plane_bind()keptagx_bo_map(mem->bo)plus the plane's offset for the image's lifetime, andvkCopyMemoryToImageEXT,vkCopyImageToMemoryEXTandvkCopyImageToImageEXTcopied through that pointer. For memory that is neither exported nor imported, the BO's mapping is also the onevkMapMemory2returns. SovkUnmapMemory2removes it, and aVK_EXT_map_memory_placedmap moves it to the application's address. A host image copy after either went through a range that no longer maps the memory. Host image copies need no external synchronization on the memory, so a copy on one thread could also lose its mapping to a map or unmap on another.agx_bo_map_owned(). That mapping lives untilvkFreeMemory.vkMapMemory2andvkUnmapMemory2touch only the BO's shared mapping or the memory object's own, never the copies' one. A range unmapped withVK_MEMORY_UNMAP_RESERVE_BIT_EXTstays reserved.VK_ERROR_MEMORY_MAP_FAILED. Binding an image no longer maps its memory on the CPU.allocationSizeas the size of the memory, without comparing the two. Images bound to that memory are checked against the BO inhk_image_plane_bind(). A buffer is bounded only byallocationSize, so it could address GPU memory past the end of the BO.VK_ERROR_INVALID_EXTERNAL_HANDLE, as in anv. The BO is released and the fd stays with the caller. Imports whose object is at leastallocationSizebytes are unchanged.agx_bo_map()mapped a BO on first use without serialization. Two threads mapping one BO for the first time at once, such as two contexts mapping a shared resource, each mapped it and stored their mapping, so one leaked and_map_sizecould be the other thread's. The move of a mapping to a placed address wrote the same fields the same way.agx_bo_map_placed()maps a BO, or moves its mapping, under a newagx_device::bo_mmap_lock, checking again under it. A BO already mapped where it is wanted is returned without the lock, as before. On a backend withbo_mmap_independentthe mapping is made into a copy of the BO and stored after its size, so a reader without the lock never sees a torn size or a failed mapping. A backend without it maps the BO in place under the lock.The code is common to every GPU Borealis supports, and the changes are the same on each. Behaviour changes only for:
allocationSize;Memory used for host image copies gets one more CPU mapping of its BO.
Testing
hk_host_image_copycovers copies after an unmap, through a placed mapping, after an unmap that keeps the range reserved, and after a placed map and an unmap while the copies' mapping is in use.hk_memory_oomalso imports objects smaller than, equal to and larger thanallocationSize.hk_host_image_copyfails at the two-thread check; with commit 3's lock removed, at the shared-mapping check.VK_ERROR_INVALID_EXTERNAL_HANDLEafter, for both dma-buf and opaque fd; imports at the object's size succeed.dEQP-VK coverage
No dEQP-VK case catches these bugs, so the host tests are the regression guard:
image.host_image_copynever maps or unmaps the memory and copies from one thread.memory.map_placednever does a host image copy.image.concurrent_copycopies from many threads into one image whose memory the application never maps. So it reaches the concurrent first map, but its results cannot show a leaked mapping.dEQP-VK on the Mac Studio
All runs on the Mac Studio (M5 Ultra, J775D) in one boot, the pack's kernel on the 27.2 stub, with #362 alone (the base) and #362 with #363, #582 and #583 on top (the head) installed side by side,
ASAHI_M5_HOST_ALIAS_STATIC=1as before. Every case gave the same result on both builds, with no GPU faults:image.host_image_copy, 1 in 8memory.map_placed,api.external.memory,push_descriptoroutsidebinding_modelbinding_modelpush descriptor cases, 1 in 4ycbcr, 1 in 8image.concurrent_copy, 1 in 40The four
pipeline.monolithic.push_descriptor.compute.incremental_updates*cases that failed with and without the series last time now pass on both. The 628-case render subset gives 571 Pass and 57 NotSupported on both.