Repository navigation
drm/asahi: GEM_USERPTR in 4K (user4k) processes - #2114
jgoyvaerts wants to merge 2 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
|
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 |
DRM_IOCTL_ASAHI_GEM_USERPTR(#1609) pins withpin_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_hostimports always fail. Two commits:do_anonymous_subpage()changes, the first-write path of aCONFIG_MM_SUBPAGEmm 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_UNPOPULATEDon fresh memory, thenPAGEMAP_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, soPAGEMAP_SCANstill reports exactly the written pages. Markers in a VMA that is no longer registered stay as they were. The arm64 user4kuffd-wp-initselftest gains this write-first case.user_pages.cpins the range withpin_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_vmcounts native pages as before. Processes with the native page size keeppin_user_pages_fast(). Thedrm_asahi_gem_userptrdoc 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_SUBPAGEcode, no SoC gate, not tried on other SoCs). Run on our daily kernel with both commits and Mesa with #242:PAGEMAP_SCAN(PAGE_IS_WRITTEN)lists exactly the 3 pages written out of 64, native and 4K.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.defconfig+asahi.config(MM_SUBPAGE=y,DRM_ASAHI=m) buildsImageand modules clean,aurora.koincluded.mm/memory.oanduser_pages.oare also clean at W=1. checkpatch reports only the house-style prototypes inuser_pages.c(as inretain.candfwid.c).uffd-wp-init(with the new case) as/initof auser4k=onQEMU guest: UFFD CONTRACT PASS, 16K and 4K workers. Withmm/memory.cfrom 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.USERFAULTFD=yandMM_SUBPAGEoff (the stub path): 696 tests, 695 pass, 1 skip,asahi_gem_user_memory5/5. Thearm64_user4kconfig withUSERFAULTFD=yanduser4k=on: the anon fault suites (arm64-user4k.*anon*) 12 pass, 2 skip, and the shmem uffd tests pass. A fullarm64-user4krun stalled afteruser4k_iommufd_lifetime_test(an IOMMUFD test this series does not touch) and was stopped. The base was not tried.host-testsjobasahi-m4(it readsgem.rs) passes. Not run: the wholehost-testsworkflow and theasahi-userptrselftest. 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:
-EOPNOTSUPPfrom 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