From e1027fc721656bffef01bb8586ff8080d94fe17b Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 16 Nov 2026 11:38:20 -0300 Subject: [PATCH 1/2] drm/asahi: GEM_USERPTR in processes with a smaller page size 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. The GEM_USERPTR uAPI documentation states the layout requirement. Processes with the native page size keep pin_user_pages_fast(). Tested on an M4 Pro: see the userptr probe in the cover note (native 16K and userpages 4k). Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM --- drivers/gpu/drm/asahi/Makefile | 2 +- drivers/gpu/drm/asahi/gem.rs | 50 +++++++++++- drivers/gpu/drm/asahi/user_pages.c | 121 +++++++++++++++++++++++++++++ drivers/gpu/drm/asahi/user_pages.h | 17 ++++ include/uapi/drm/asahi_drm.h | 4 + 5 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/drm/asahi/user_pages.c create mode 100644 drivers/gpu/drm/asahi/user_pages.h diff --git a/drivers/gpu/drm/asahi/Makefile b/drivers/gpu/drm/asahi/Makefile index 890d47c40af6b..b17f3d47a3e06 100644 --- a/drivers/gpu/drm/asahi/Makefile +++ b/drivers/gpu/drm/asahi/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DRM_ASAHI) += aurora.o -aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o g17/sleep.o g17/pmpdash.o +aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o user_pages.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o g17/sleep.o g17/pmpdash.o CFLAGS_g17/trace.o := -I$(src)/g17 aurora-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o diff --git a/drivers/gpu/drm/asahi/gem.rs b/drivers/gpu/drm/asahi/gem.rs index 40487a77e97ea..bad222981a6ae 100644 --- a/drivers/gpu/drm/asahi/gem.rs +++ b/drivers/gpu/drm/asahi/gem.rs @@ -459,9 +459,13 @@ pub(crate) struct UserPages { mm: ARef, /// Pages counted in `pinned_vm`: all of the range, including any not pinned yet. counted: i64, + /// The fragment pins of a process with a smaller page size, which hold `pages` (null when + /// `pages` are ordinary pins). + fragments: *mut core::ffi::c_void, } -// SAFETY: The pinned pages may be released from any thread; the mm reference is thread-safe. +// SAFETY: The pinned pages and fragment records may be released from any thread; the mm +// reference is thread-safe. unsafe impl Send for UserPages {} // SAFETY: Shared access only reads the array. unsafe impl Sync for UserPages {} @@ -475,7 +479,9 @@ impl UserPages { /// /// All of the range is pinned, long-term, or none of it. Fails with `EFAULT` when a page cannot /// be pinned: unmapped, a device or PFN mapping, or a writable pin of a mapping that is not - /// writable or whose filesystem tracks writes (`FOLL_LONGTERM` refuses those). Fails with + /// writable or whose filesystem tracks writes (`FOLL_LONGTERM` refuses those). In a process + /// whose page size is smaller than the native one, a native page is accepted only where the + /// process maps all of it, in order at consecutive addresses (`EFAULT` otherwise). Fails with /// `ENOMEM` when the pages would take the process past RLIMIT_MEMLOCK without CAP_IPC_LOCK, /// counting every page pinned this way or by other long-term pinners. /// @@ -510,12 +516,33 @@ impl UserPages { writable, mm, counted: added, + fragments: core::ptr::null_mut(), }; this.pages.reserve(count, GFP_KERNEL)?; let mut flags = kernel::bindings::FOLL_LONGTERM; if writable { flags |= kernel::bindings::FOLL_WRITE; } + // SAFETY: No preconditions; it reads the current mm. + if unsafe { asahi_user_mm_is_subpage() } { + // SAFETY: The array has room for `count` entries. On success the call stored `count` + // pages, which the records it returned in `fragments` hold. + let ret = unsafe { + asahi_pin_user_fragments( + addr as _, + count as _, + flags, + this.pages.as_mut_ptr().cast(), + &mut this.fragments, + ) + }; + if ret < 0 { + return Err(Error::from_errno(ret as i32)); + } + // SAFETY: See above. + unsafe { this.pages.inc_len(count) }; + return Ok(this); + } while this.pages.len() < count { let done = this.pages.len(); let chunk = (count - done).min(PIN_CHUNK); @@ -556,7 +583,11 @@ impl UserPages { impl Drop for UserPages { fn drop(&mut self) { - if !self.pages.is_empty() { + if !self.fragments.is_null() { + // SAFETY: The records hold every page of the array and are released only here. + // Pages the device may have written are marked dirty. + unsafe { asahi_unpin_user_fragments(self.fragments, self.writable) }; + } else if !self.pages.is_empty() { // SAFETY: The array holds pages pinned by `pin_user_pages_fast()` and not yet // unpinned. Pages the device may have written are marked dirty. unsafe { @@ -985,6 +1016,19 @@ pub(crate) fn prepare_new_object_g15(gem: &Object) -> Result { Ok(()) } +// user_pages.c: long-term pins in a process whose page size is smaller than the native one. +extern "C" { + fn asahi_user_mm_is_subpage() -> bool; + fn asahi_pin_user_fragments( + start: core::ffi::c_ulong, + nr_pages: core::ffi::c_ulong, + gup_flags: core::ffi::c_uint, + pages: *mut *mut core::ffi::c_void, + out: *mut *mut core::ffi::c_void, + ) -> core::ffi::c_long; + fn asahi_unpin_user_fragments(frags: *mut core::ffi::c_void, dirty: bool); +} + #[cfg(CONFIG_DRM_ASAHI_G15)] extern "C" { fn asahi_g15_dma_coherent(dev: *mut core::ffi::c_void) -> bool; diff --git a/drivers/gpu/drm/asahi/user_pages.c b/drivers/gpu/drm/asahi/user_pages.c new file mode 100644 index 0000000000000..b0c9c4dbe8879 --- /dev/null +++ b/drivers/gpu/drm/asahi/user_pages.c @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Long-term pins of user memory as whole native pages, for GEM_USERPTR. + * + * The GPU maps memory in native pages. A process whose user page size is + * smaller (CONFIG_MM_SUBPAGE) maps each of its pages to a fragment of a + * native page, and only the fragment API can pin those. A native page can be + * given to the GPU only where the process maps all of it, its fragments in + * order at consecutive addresses: then the GPU sees what the CPU sees, and no + * memory the process does not map. + */ + +#include +#include +#include +#include + +#include "user_pages.h" + +#ifdef CONFIG_MM_SUBPAGE +/* Pinned fragments of a range; released only through asahi_unpin_user_fragments(). */ +struct asahi_user_fragments { + unsigned long count; + struct user_page_fragment records[]; +}; + +bool asahi_user_mm_is_subpage(void) +{ + return current->mm && mm_page_size(current->mm) < PAGE_SIZE; +} + +void asahi_unpin_user_fragments(struct asahi_user_fragments *frags, bool dirty) +{ + if (!frags) + return; + release_user_fragments(frags->records, frags->count, dirty); + kvfree(frags); +} + +/* The native page that @rec and its @n - 1 successors map in order, or NULL. */ +static struct page *whole_native_page(const struct user_page_fragment *rec, + unsigned long n, unsigned long granule) +{ + unsigned long i; + + if (!IS_ALIGNED(rec->offset, PAGE_SIZE)) + return NULL; + for (i = 0; i < n; i++) + if (rec[i].folio != rec->folio || rec[i].length != granule || + rec[i].offset != rec->offset + i * granule) + return NULL; + return folio_page(rec->folio, rec->offset >> PAGE_SHIFT); +} + +/* + * Pins @nr_pages native pages' worth of the current process's memory at + * @start (native-page aligned) and stores the native pages in @pages. All or + * nothing: -EFAULT if a page cannot be pinned or a native page is not mapped + * whole and in order; -ENOMEM if the records cannot be allocated. + */ +long asahi_pin_user_fragments(unsigned long start, unsigned long nr_pages, + unsigned int gup_flags, struct page **pages, + struct asahi_user_fragments **out) +{ + struct mm_struct *mm = current->mm; + unsigned long granule = mm_page_size(mm); + unsigned long per_page = PAGE_SIZE / granule; + unsigned long length = nr_pages << PAGE_SHIFT, done = 0, i; + struct asahi_user_fragments *frags; + long ret = 0; + + if (nr_pages > (ULONG_MAX >> PAGE_SHIFT) / per_page) + return -ENOMEM; + frags = kvzalloc(struct_size(frags, records, nr_pages * per_page), + GFP_KERNEL_ACCOUNT); + if (!frags) + return -ENOMEM; + while (done < length) { + long n = pin_user_fragments_remote(mm, start + done, length - done, + gup_flags, frags->records + frags->count, + nr_pages * per_page - frags->count); + + if (n <= 0) { + ret = n ?: -EFAULT; + goto fail; + } + for (i = frags->count; i < frags->count + n; i++) + done += frags->records[i].length; + frags->count += n; + } + for (i = 0; i < nr_pages; i++) { + pages[i] = whole_native_page(&frags->records[i * per_page], per_page, granule); + if (!pages[i]) { + ret = -EFAULT; + goto fail; + } + } + *out = frags; + return 0; + +fail: + asahi_unpin_user_fragments(frags, false); + return ret; +} +#else +bool asahi_user_mm_is_subpage(void) +{ + return false; +} + +void asahi_unpin_user_fragments(struct asahi_user_fragments *frags, bool dirty) +{ +} + +long asahi_pin_user_fragments(unsigned long start, unsigned long nr_pages, + unsigned int gup_flags, struct page **pages, + struct asahi_user_fragments **out) +{ + return -EOPNOTSUPP; +} +#endif diff --git a/drivers/gpu/drm/asahi/user_pages.h b/drivers/gpu/drm/asahi/user_pages.h new file mode 100644 index 0000000000000..d856192fdedfe --- /dev/null +++ b/drivers/gpu/drm/asahi/user_pages.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef _ASAHI_USER_PAGES_H +#define _ASAHI_USER_PAGES_H + +#include + +struct asahi_user_fragments; +struct page; + +/* Called from gem.rs. */ +bool asahi_user_mm_is_subpage(void); +long asahi_pin_user_fragments(unsigned long start, unsigned long nr_pages, + unsigned int gup_flags, struct page **pages, + struct asahi_user_fragments **out); +void asahi_unpin_user_fragments(struct asahi_user_fragments *frags, bool dirty); + +#endif diff --git a/include/uapi/drm/asahi_drm.h b/include/uapi/drm/asahi_drm.h index fc3b32a63b470..c0f82e8f0d245 100644 --- a/include/uapi/drm/asahi_drm.h +++ b/include/uapi/drm/asahi_drm.h @@ -780,6 +780,10 @@ struct drm_asahi_gem_create { * while the object lives. Device and PFN mappings, including CPU mappings of * other buffer objects, are refused (EFAULT). * + * In a process whose page size is smaller than the native one, a native page + * is accepted only where the process maps all of it, in order at consecutive + * addresses; otherwise the call fails with EFAULT. + * * The object cannot be mapped with DRM_IOCTL_ASAHI_GEM_MMAP_OFFSET, exported, * given advice with DRM_IOCTL_ASAHI_GEM_MADVISE, bound with * DRM_IOCTL_ASAHI_GEM_BIND_OBJECT or registered as a VM status mirror (all From 6d18dfb72dde701d1d85ab24f01f472a7b848ba5 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Thu, 19 Nov 2026 01:45:41 -0300 Subject: [PATCH 2/2] selftests/arm64: cover GEM_USERPTR in a 4K process gem-userptr re-executes itself as a 4K process and imports anonymous 32K ranges with DRM_IOCTL_ASAHI_GEM_USERPTR on the GPU render node, without submitting GPU work. A range whose 16K pages are each mapped whole and in order pins 32K in VmPin until the object is closed. A 16K page with one 4K page moved in from elsewhere, or with two of its own 4K pages swapped, fails with EFAULT and leaves VmPin unchanged. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM --- .../testing/selftests/arm64/user4k/.gitignore | 1 + tools/testing/selftests/arm64/user4k/Makefile | 4 +- .../selftests/arm64/user4k/gem-userptr.c | 138 ++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/user4k/gem-userptr.c diff --git a/tools/testing/selftests/arm64/user4k/.gitignore b/tools/testing/selftests/arm64/user4k/.gitignore index e3e47c9305901..bbcee19ebedc6 100644 --- a/tools/testing/selftests/arm64/user4k/.gitignore +++ b/tools/testing/selftests/arm64/user4k/.gitignore @@ -1,4 +1,5 @@ page-contract +gem-userptr swap-fixture-init same-quarter diff --git a/tools/testing/selftests/arm64/user4k/Makefile b/tools/testing/selftests/arm64/user4k/Makefile index 889386ca79ffc..95826fa0f644d 100644 --- a/tools/testing/selftests/arm64/user4k/Makefile +++ b/tools/testing/selftests/arm64/user4k/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -TEST_GEN_PROGS := page-contract proc-args +TEST_GEN_PROGS := page-contract proc-args gem-userptr TEST_GEN_PROGS_EXTENDED := iommufd-init coarse-shmem-init bpf-uptr-init vhost-log-init uprobes-init user-events-init uffd-move-init swap-fixture-init exec-fixture-init mixed-process memory-interfaces advice reclaim-advice proc-pages proc-memory core-fixture-init context-stress-init mlock-fixture-init lazyfree-fixture-init thread-fixture-init splice-fixture-init dio-fixture-init uring-fixture-init gem-fixture-init compat-fixture-init granule-proc-init granule-swap-init aslr-fixture-init cpu-granule-init user16k-smoke-init cpu-granule16-init granule-threeway-init compat-mlock-init mglru-granule-init memcg-pressure-init memcg-accounting-init runtime-app-init fex-contract-init exec-policy-init uffd-wp-init shmem-uffd-swap-init compat-uffd-init shmem-hole-init shmem-thp-init shmem-collapse-init shmem-pin-init shmem-race-init shmem-pressure-init TEST_GEN_PROGS_EXTENDED += vfio-fixture-init sysv-shm-init perf-ring-init TEST_GEN_PROGS_EXTENDED += aio-ring-init numa-policy-init numa-balancing-init cow-pool @@ -120,6 +120,8 @@ $(OUTPUT)/iommufd-init: LDFLAGS += -static -Wl,-z,max-page-size=65536 $(OUTPUT)/iommufd-init: LDLIBS += -pthread $(OUTPUT)/iommufd-init: CFLAGS += $(KHDR_INCLUDES) +$(OUTPUT)/gem-userptr: CFLAGS += $(KHDR_INCLUDES) + # Privileged recording-domain fixture; run only in a disposable test VM. $(OUTPUT)/vfio-fixture-init: LDFLAGS += -static -Wl,-z,max-page-size=65536 $(OUTPUT)/compat-vfio-probe: compat-vfio-probe.c diff --git a/tools/testing/selftests/arm64/user4k/gem-userptr.c b/tools/testing/selftests/arm64/user4k/gem-userptr.c new file mode 100644 index 0000000000000..1e40809ee3e1c --- /dev/null +++ b/tools/testing/selftests/arm64/user4k/gem-userptr.c @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * DRM_IOCTL_ASAHI_GEM_USERPTR in a 4K process on a 16K kernel: a native page + * backs the object only where the process maps all of it, in order. No GPU + * work is submitted. A 16K process runs the test in a 4K exec of itself. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "../../../../../include/uapi/linux/prctl.h" +#include +#include +#include +#include +#include +#include +#include "../../kselftest.h" + +#define NATIVE 16384UL +#define LEAF 4096UL +#define LEN (2 * NATIVE) + +static long vm_pin_kb(void) +{ + char line[256]; + long kb = -1; + FILE *f = fopen("/proc/self/status", "r"); + + if (!f) + return -1; + while (fgets(line, sizeof(line), f)) + if (sscanf(line, "VmPin: %ld kB", &kb) == 1) + break; + fclose(f); + return kb; +} + +static char *map(size_t len, int prot) +{ + char *p = mmap(NULL, len, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (p == MAP_FAILED) + ksft_exit_fail_msg("mmap: %s\n", strerror(errno)); + return p; +} + +/* A written, native-aligned LEN range: each native page mapped whole, in order. */ +static char *range(void) +{ + char *p = map(LEN + NATIVE, PROT_READ | PROT_WRITE); + + p = (char *)(((uintptr_t)p + NATIVE - 1) & ~(NATIVE - 1)); + memset(p, 0x5a, LEN); + return p; +} + +static void move(char *from, char *to) +{ + if (mremap(from, LEAF, LEAF, MREMAP_MAYMOVE | MREMAP_FIXED, to) != to) + ksft_exit_fail_msg("mremap: %s\n", strerror(errno)); +} + +/* GEM_USERPTR of the range fails with @err, or pins LEN until closed. */ +static void userptr(int fd, char *p, int err, const char *name) +{ + struct drm_asahi_gem_userptr args = { + .userptr = (uintptr_t)p, .size = LEN, + }; + long pin0 = vm_pin_kb(), pinned, after; + int ret = ioctl(fd, DRM_IOCTL_ASAHI_GEM_USERPTR, &args) ? errno : 0; + + pinned = vm_pin_kb() - pin0; + if (!ret) { + struct drm_gem_close close_arg = { .handle = args.handle }; + + ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close_arg); + } + after = vm_pin_kb() - pin0; + ksft_print_msg("%s: errno %d, VmPin +%ld kB, +%ld kB after close\n", + name, ret, pinned, after); + ksft_test_result(ret == err && pinned == (err ? 0 : (long)LEN >> 10) && + !after, "%s\n", name); +} + +int main(int argc, char **argv) +{ + char name[16] = ""; + struct drm_version version = { + .name = name, .name_len = sizeof(name) - 1, + }; + struct drm_asahi_params_global params = {}; + struct drm_asahi_get_params get = { + .pointer = (uintptr_t)¶ms, .size = sizeof(params), + }; + char *p, *q; + int fd; + + if (getauxval(AT_PAGESZ) == NATIVE && argc == 1) { + if (prctl(PR_AURORA_SET_EXEC_PAGE_SIZE, LEAF, 0UL, 0UL, 0UL)) + ksft_exit_skip("no 4K processes: %s\n", strerror(errno)); + execl("/proc/self/exe", argv[0], "--4k", NULL); + ksft_exit_fail_msg("exec: %s\n", strerror(errno)); + } + ksft_print_header(); + if (getauxval(AT_PAGESZ) != LEAF) + ksft_exit_skip("not a 4K process\n"); + fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC); + /* "asahi" is the driver's name before it became "aurora". */ + if (fd < 0 || ioctl(fd, DRM_IOCTL_VERSION, &version) || + (strcmp(name, "aurora") && strcmp(name, "asahi"))) + ksft_exit_skip("renderD128 is not an asahi render node\n"); + if (ioctl(fd, DRM_IOCTL_ASAHI_GET_PARAMS, &get) || + !(params.features & DRM_ASAHI_FEATURE_USERPTR)) + ksft_exit_skip("no DRM_ASAHI_FEATURE_USERPTR\n"); + ksft_set_plan(3); + + userptr(fd, range(), 0, "native pages mapped whole and in order"); + + p = range(); + q = map(LEAF, PROT_READ | PROT_WRITE); + memset(q, 0xa5, LEAF); + move(q, p + NATIVE + LEAF); + userptr(fd, p, EFAULT, "a 4K page from elsewhere in a native page"); + + /* Swap two 4K pages of one native page: all of it, out of order. */ + p = range(); + q = map(LEAF, PROT_NONE); + move(p + NATIVE + LEAF, q); + move(p + NATIVE + 2 * LEAF, p + NATIVE + LEAF); + move(q, p + NATIVE + 2 * LEAF); + userptr(fd, p, EFAULT, "a native page mapped out of order"); + + close(fd); + ksft_finished(); +}