Skip to content

drm/asahi: GEM_USERPTR in 4K (user4k) processes - #2114

Closed
jgoyvaerts wants to merge 2 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:pr/gem-userptr-subpage
Closed

jgoyvaerts wants to merge 2 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:pr/gem-userptr-subpage

Conversation

@jgoyvaerts

Copy link
Copy Markdown
Contributor

DRM_IOCTL_ASAHI_GEM_USERPTR (#1609) pins with pin_user_pages_fast(), which returns EOPNOTSUPP in every process whose page size is smaller than the native one (user4k: 4K processes on a 16K kernel, so everything under FEX). There, VK_EXT_external_memory_host imports always fail. Two commits:

  1. mm: populate a subpage window's uffd-wp markers with the faulting page. Only do_anonymous_subpage() changes, the first-write path of a CONFIG_MM_SUBPAGE mm with a smaller page size. Processes with the native page size and every other uffd-wp path are untouched. The GPU needs it because it can only use a native page that a 4K process maps whole and in order. A userfaultfd write watch (WP_ASYNC | WP_UNPOPULATED on fresh memory, then PAGEMAP_SCAN) leaves a uffd-wp marker in every neighbouring PTE. anon_subpage_missing_mask() counted only none PTEs as missing, so each first write took a fresh native page for one 4K page: 4x the memory, and no window was ever importable. Now a marker in a registered VMA counts as missing. The neighbour gets the uffd-wp bit it stood for, so PAGEMAP_SCAN still reports exactly the written pages. Markers in a VMA that is no longer registered stay as they were. The arm64 user4k uffd-wp-init selftest gains this write-first case.
  2. drm/asahi: GEM_USERPTR in processes with a smaller page size. In such a process, user_pages.c pins the range with pin_user_fragments_remote(). It hands the GPU a native page only where the process maps all of it, its fragments in order at consecutive addresses. It is all or nothing: EFAULT otherwise, so the GPU never sees memory the process does not map. pinned_vm counts native pages as before. Processes with the native page size keep pin_user_pages_fast(). The drm_asahi_gem_userptr doc gains one sentence: in a process with a page size below 16 KiB, each 16 KiB of the range must be one native page the process maps whole and in order (EFAULT otherwise).

On top of #1609 (merged) and the subpage fill from e91d6d3. No uAPI change and no knobs.

How tested (M4 Pro T6040 only; generic MM_SUBPAGE code, no SoC gate, not tried on other SoCs). Run on our daily kernel with both commits and Mesa with #242:

  • Layout probe: 8 MiB of anonymous memory in a 4K process, write-protected through userfaultfd before its first touch, then written once per 4K page. Before: 0/512 16K blocks were one native page in order. After: 512/512, the same as unregistered memory or memory registered after its first touch. Write-watch probe: PAGEMAP_SCAN(PAGE_IS_WRITTEN) lists exactly the 3 pages written out of 64, native and 4K.
  • Host-import probe through VK_EXT_external_memory_host: native 16K and x86 4K under FEX, plain and uffd-protected. The GPU fills one part and copies another, and the CPU checks both plus an untouched part: 0 wrong words of 262144 in each. A range with a 4K quarter of every native page re-faulted elsewhere is refused in a 4K process (VmPin 0 kB before and after).
  • dEQP-VK.memory.external_memory_host.*: 5 Pass, 8 NotSupported (images), 0 Fail. A later kernel rebased on integration/aurora passed a 15-case host-memory and swapchain set, native and x86. No GPU recoveries in any of these runs.
  • On this branch (integration/aurora 677be39): defconfig + asahi.config (MM_SUBPAGE=y, DRM_ASAHI=m) builds Image and modules clean, aurora.ko included. mm/memory.o and user_pages.o are also clean at W=1. checkpatch reports only the house-style prototypes in user_pages.c (as in retain.c and fwid.c).
  • uffd-wp-init (with the new case) as /init of a user4k=on QEMU guest: UFFD CONTRACT PASS, 16K and 4K workers. With mm/memory.c from the base, the 4K worker fails the new case: the neighbours stay non-present markers. Its marker cases also pass natively on the M4 Pro, in a 16K process and a 4K one.
  • KUnit: the asahi/SEP/ISP/DMA set with USERFAULTFD=y and MM_SUBPAGE off (the stub path): 696 tests, 695 pass, 1 skip, asahi_gem_user_memory 5/5. The arm64_user4k config with USERFAULTFD=y and user4k=on: the anon fault suites (arm64-user4k.*anon*) 12 pass, 2 skip, and the shmem uffd tests pass. A full arm64-user4k run stalled after user4k_iommufd_lifetime_test (an IOMMUFD test this series does not touch) and was stopped. The base was not tried.
  • host-tests job asahi-m4 (it reads gem.rs) passes. Not run: the whole host-tests workflow and the asahi-userptr selftest. The probe sources (layout, write-watch scan, host import; small C files) can be posted here if useful.

Known limits, not tested: a buffer still COW-shared with a fork child that has not exec'd yet, and long-term-pin migration out of CMA or movable blocks, may leave a 4K range unimportable (EFAULT).

The M4 Pro runs above used the commits before review. Since then: -EOPNOTSUPP from the fragment pin (hugetlb or VM_MIXEDMAP ranges) is reported as EFAULT, the uAPI doc sentence and the selftest case were added, and the build, KUnit and checkpatch above were rerun.

🤖 Generated with Claude Code

In an mm whose user page size is smaller than the native page, a write
fault on unpopulated anonymous memory fills the whole native window
from one fresh native page, each 4K page at its own offset. That keeps
the window physically in VA order, and costs one native page for four
user pages.

A uffd-wp marker (UFFD_FEATURE_WP_UNPOPULATED) is unpopulated memory
too, but anon_subpage_missing_mask() counted only none PTEs as missing.
In a VMA write-protected before its first touch, every neighbour holds
a marker, so each first write took a native page of its own for one
4K page: four times the memory, and no window was ever one native page
in order. A device that maps native pages (a GPU with 16K pages taking
user memory through GEM_USERPTR) can then use none of it.

Count a uffd-wp marker in a uffd-wp VMA as missing, and give the
neighbour it stood for the uffd-wp bit, as the faulting page already
gets it from its own marker: the populated neighbours stay write
protected and unwritten for PAGEMAP_SCAN. Markers in a VMA that is no
longer registered are left as they were.

Only do_anonymous_subpage() changes, the first-write path of an mm with
a smaller page size (CONFIG_MM_SUBPAGE). Processes with the native page
size and every other uffd-wp path are untouched.

The next patch needs this: it lets GEM_USERPTR pin user memory in a 4K
process only where a native page is mapped whole and in order. A write
watch built on userfaultfd (WP_ASYNC on fresh memory, then PAGEMAP_SCAN,
as an emulator running under FEX uses it) protects memory this way
before touching it, so without this fix none of that memory can be
imported.

Measured with a probe in a 4K process on an M4 Pro (16K kernel): 8 MiB
of anonymous memory, write-protected through userfaultfd with
WP_ASYNC | WP_UNPOPULATED before its first touch, then written once per
4K page. Before, 0 of 512 16K blocks were one native page in order;
memory registered after its first touch, or never registered, gave
512 of 512. With this change all three give 512 of 512. A second probe
writes 3 of 64 pages write-protected the same way: PAGEMAP_SCAN with
PAGE_IS_WRITTEN reports exactly those 3, in a native 16K process and in
a 4K one. Tested on an M4 Pro (T6040) only.

The arm64 user4k uffd-wp-init selftest gains the write-first case: a
native-aligned window of four user pages, write-protected with
WP_ASYNC | WP_UNPOPULATED, written in its second page first. With a
smaller user page size the other three must be present and still
write-protected; with the native size they stay markers. It passes in
the native and 4K workers in a user4k=on QEMU guest, and fails the 4K
worker without this change.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Assisted-by: LLM
GEM_USERPTR pins its range with pin_user_pages_fast(). In a process
whose user page size is smaller than the native one (CONFIG_MM_SUBPAGE,
user4k: 4K processes on a 16K kernel, such as everything running under
FEX), GUP cannot return pages, and every GEM_USERPTR fails with
EOPNOTSUPP, so VK_EXT_external_memory_host imports fail there.

Such a process maps each of its pages to a fragment of a native page,
and pin_user_fragments_remote() pins those. The GPU maps native pages,
so a native page can back the object only where the process maps all
of it, its fragments in order at consecutive addresses: then the GPU
sees exactly what the CPU sees, and nothing the process does not map.
user_pages.c pins the range as fragments and checks that layout, all
or nothing (EFAULT where a native page is not mapped whole and in
order). The fragment records hold the pins and are released with the
object; pinned_vm counts native pages, as for other processes.

Processes with the native page size keep pin_user_pages_fast(). The
GEM_USERPTR uAPI doc now states the layout rule for such processes.

Tested on an M4 Pro (T6040) only, with the previous patch, through
VK_EXT_external_memory_host in Mesa: a probe imports 4 MiB of anonymous
memory, has the GPU fill one part and copy another, and checks both
and an untouched part from the CPU through its own mapping. Native
16K and x86 4K under FEX, plain memory and memory write-protected with
userfaultfd before its first touch: 0 wrong words of 262144 in each
part. Memory with a 4K quarter of every native page re-faulted
elsewhere is refused in a 4K process, and VmPin stays at 0 kB.
dEQP-VK.memory.external_memory_host.*: 5 Pass, 8 NotSupported
(images), 0 Fail. No GPU recoveries during the runs.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Assisted-by: LLM

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does not fit the accelerated additive/UAPI-only category: it changes generic MM_SUBPAGE first-write population and long-term fragment pin ownership as well as GEM_USERPTR. Independent source review found no concrete defect in the reviewed full-granule/same-folio checks, partial/error cleanup, typed pin lifetime, dirtying/memlock accounting or WP-marker preservation. Exact-head host CI and the reported targeted QEMU/KUnit/native/FEX coverage are useful evidence.

I am keeping this outside the current quick-land batch for focused MM-contract review and disposition of the reported full user4k suite stall after the IOMMUFD lifetime test. There is no baseline control in the supplied report, so I am neither attributing the stall to this PR nor treating the suite as passed. Please link any already available base-control/isolation evidence; no new Mac boot or device test is requested here. This is a review/evidence follow-up, not a claim that the candidate is safe solely because CI is green.

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Closing in favour of the split: #2225 carries the mm change (uffd-wp markers in a subpage window) and #2226 the drm/asahi GEM_USERPTR part, each with its own selftest, so the MM contract can be reviewed on its own as suggested above. The base-control evidence for the full arm64-user4k stall after user4k_iommufd_lifetime_test will go on #2225.

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.

2 participants