Skip to content

host: make system blob memory work on macOS - #182

Open
utzcoz wants to merge 7 commits into
google:mainfrom
utzcoz:macos-host-memory
Open

utzcoz wants to merge 7 commits into
google:mainfrom
utzcoz:macos-host-memory

Conversation

@utzcoz

@utzcoz utzcoz commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Two host fixes for running the guest Vulkan driver through kumquat on macOS.
Both only take effect with SystemBlob enabled.

  • vulkan: add a guest only device local memory type when the host reports a
    single host visible unified type. Metal cannot bind a tiled image to an
    imported host pointer; this gives images ordinary device memory. Unit test
    added.

  • base: put the pid in the shared memory name and unlink it on creation, so a
    killed server does not leave objects that make the next ftruncate fail.

Tested with bazel test //host/vulkan:gfxstream_emulatedphysicalmemory_tests,
and vulkaninfo, vkcube and a dEQP-VK smoke run on kosmickrisp and MoltenVK.

@utzcoz

utzcoz commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

@utzcoz

utzcoz commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping @gurchetansingh @jmacnak for reviewing.

Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp Outdated
@gurchetansingh

Copy link
Copy Markdown
Collaborator

@jmacnak knows this code well, would be the right person to review

Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp Outdated
@utzcoz
utzcoz force-pushed the macos-host-memory branch 3 times, most recently from 293c959 to 7129900 Compare September 10, 2026 16:01

@jmacnak jmacnak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thread about readability but overall logic LGTM

Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp
@utzcoz
utzcoz force-pushed the macos-host-memory branch 6 times, most recently from f0f5808 to 18adce1 Compare September 16, 2026 11:16
@utzcoz

utzcoz commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi @jmacnak , could you review it again? There is an old requested change from you, and it might require your approval to remove this flag.

@jmacnak jmacnak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay!

Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp Outdated
Comment thread host/vulkan/vk_decoder_global_state.cpp
Comment thread common/base/SharedMemory_posix.cpp Outdated
@jmacnak
jmacnak requested a review from kocdemir September 23, 2026 18:04
auto-merge was automatically disabled September 24, 2026 14:39

Head branch was pushed to by a user without write access

@utzcoz

utzcoz commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Hi @jmacnak @gurchetansingh @kocdemir , I have addressed new comments. PTAL.

@utzcoz

utzcoz commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

I decide to keep device buffer query regeneration part of vk_decoder.py only and update it.

@utzcoz

utzcoz commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Hi @jmacnak @gurchetansingh @kocdemir , friendly ping for reviewing.

Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp Outdated
Comment thread host/vulkan/vk_emulated_physical_device_memory.cpp
@utzcoz

utzcoz commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Hi @jmacnak @kocdemir comments addressed, PTAL.

vkGetDeviceImageMemoryRequirements answers with the host's memory type bits
unless the format is one gfxstream decompresses. The query that names a
created image translates them first.

Nothing notices while a guest memory type index is also a host one, which holds
until a type is emulated. With one, a guest is told an image fits memory types
it does not have, and asking the same question the other way answers something
else.

Translate on the way out on every path.
A system blob's shared memory is handed to the driver as a host pointer, and a
driver states the alignment it takes in minImportedHostPointerAlignment. The
size is rounded to 4096 instead, which is the page size the guest is assumed to
have rather than anything the importing driver asked for.

On Apple silicon a page is 16KB and kosmickrisp asks for that, so an allocation
whose size is a multiple of 4096 but not of 16KB is imported against the
driver's stated requirement.

Round up to whichever is larger. Where the two agree, which is everywhere a
page is 4096, nothing changes.
… Apple

System blob memory is named shared-memory-vk-N with a per process counter and
opened without O_EXCL. A server that is killed leaves its objects behind, the
next one reuses the names, and on macOS an object that already has a size
cannot be resized: ftruncate fails with EINVAL and vkAllocateMemory fails.

Put the pid in the name, and on Apple create with O_EXCL, unlinking and making
the object again when the name is taken. What has the name then is either left
over from a dead process or belongs to a live one, which keeps it through its
descriptor either way; sharing it by accident is what O_CREAT alone did.
With system blobs, host visible memory is shared memory imported as a host
pointer, and Metal cannot bind a tiled image to that. A device that reports one
unified memory type, as kosmickrisp does, then has nowhere to put an image, and
kk_image_plane_bind asserts on the first one.

Add a guest only type in that case: device local, allocating from the same host
type without host visible emulation. A tiled image is offered that type alone,
since the host visible one cannot hold it; a linear image may still use either.
MoltenVK reports a device local only type of its own, so nothing changes there.

Guest memory types are now kept as a list of structs, each naming its host
type and what it is reserved for, instead of index maps in both directions.
The new type is inserted where the spec's ordering puts it: a type whose flags
are a strict subset of another's sits at a lower index, which on a unified
memory device is the front. The AHB type is still appended, as before.

Metal is what makes this the common case, so it is behind __APPLE__.
vkGetDeviceBufferMemoryRequirements is the one requirements query the decoder
still hands to the driver directly, so with an emulated memory type its answer
names host types, the way the device image query's did.

Add the global state handler that translates it. The decoder reaches it once
the guest's generator routes the query through global state, which is a mesa
change; until the decoder is regenerated from that, it is not called.
Regenerated with mesa's generate-gfxstream-vulkan.sh at caf81bd201a, keeping
what mesa 4b40fa7ace8 changes: vkGetDeviceBufferMemoryRequirements and its KHR
alias now go through global state, so the handler that gives the guest its own
memory types is reached. The generator's other output since the decoder was
last regenerated is left for a regeneration of its own.
The shared memory code has an Apple path of its own now, and nothing ran its
tests there. Let the reusable Bazel workflow take a test list, and give the
macOS job the base tests, which need no graphics driver.

The WorkerThread case that expects a ready future before start() is not run
on any platform and fails on all of them; it is filtered out until it is fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants