Repository navigation
Conversation
…oss fork and exit GEM_USERPTR counts the pages it pins in the calling mm's pinned_vm and compares that count with RLIMIT_MEMLOCK. The object belongs to the DRM file, not to the mm. A forked child starts with nothing counted, its pins land in the file it shares with its parent, and they stay pinned after it exits, counted against an mm that no longer runs anything. The limit therefore does not bound what one user can pin. Charge the pins to the user instead, in user_struct::locked_vm, as io_uring does for its registered buffers (__io_account_mem()) and iommufd for its pinned pages: every process of the user shares the count, and a pin stays charged until its object is freed. As in both, a CAP_IPC_LOCK caller is not charged. The mm's pinned_vm still counts the pages, as it did. The UAPI comment says so, and the driver joins the users of user_struct::locked_vm in <linux/sched/user.h>, as iommufd did. Only the G17 backend takes user-memory objects (File::supports_user_memory()), so other GPUs do not run this code. A process with a smaller page size is charged the same native pages. A KUnit case checks that a CAP_IPC_LOCK caller is not charged; that, without it, pages charged to the test's user as another process of that user would charge them make the next pin fail although the test's own mm has none; and that dropped pins are given back. Fixes: ecc94a2 ("drm/asahi: GEM_USERPTR, buffer objects over the caller's memory") Reported-by: Erik Bourget <erik@ebourget.net> Suggested-by: Erik Bourget <erik@ebourget.net> Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
Contributor
|
The accounting looks correct to me.
The only hardware row is the J775D. Rows from the other G17 Macs (A18 Pro, M4 Pro) would be good: Assisted-by: LLM (Claude:Opus 5.5) |
DjDeveloperr
requested changes
Nov 22, 2026
DjDeveloperr
left a comment
Contributor
There was a problem hiding this comment.
Pre-PR checklist review on 464023d790812a1522cdf7b3d8eb8aa1c790c309 (CI: host-tests green; note SEP source checks / rust-build / kunit workflows did not trigger for drivers/gpu/drm/asahi/ paths).
Blockers (REQUEST_CHANGES)
-
Build failure with
CONFIG_KUNIT=m(-Dwarnings/dead_code):#[kernel::macros::kunit_tests(asahi_gem_user_memory)]expands to#[cfg(CONFIG_KUNIT = "y")](rust/macros/kunit.rs:44), somod user_memory_testsis compiled out whenCONFIG_KUNIT=m(standard on distro kernels).- However,
drivers/gpu/drm/asahi/gem.rs:1075gates the four privateextern "C"KUnit helpers with#[cfg(CONFIG_KUNIT)], which Kbuild defines for both=yand=m(scripts/kconfig/confdata.c:799). UnderCONFIG_KUNIT=m, all fourextern "C"declarations have no callers and fail the build witherror: function ... is never usedunder-Dwarnings(dead_code). - Every other KUnit helper in
drivers/gpu/drm/asahi/(g17/kick.rs,pairs.rs,preparation.rs,queue/admission.rs,runtime.rs) uses#[cfg(CONFIG_KUNIT = "y")]. - Matching that on the C side,
drivers/gpu/drm/asahi/user_pages.c:67anduser_pages.h:21should use#ifdef CONFIG_KUNIT(i.e.IS_BUILTIN(CONFIG_KUNIT)) instead of#if IS_ENABLED(CONFIG_KUNIT), so the test-onlyasahi_test_drop_ipc_lock()/asahi_test_user_locked_vm_add()/asahi_test_set_memlock()helpers are not compiled into productionaurora.kowhenCONFIG_KUNIT=m.
-
KUnit test cleanup underflows global
root_user->locked_vmon early error return (drivers/gpu/drm/asahi/gem.rs:887-914):- Inside the cleanup closure in
pins_count_against_the_user(),UserPages::pin(addr, 2 * PAGE_SIZE, true)?(line 889) runs beforeasahi_test_user_locked_vm_add(held)(line 897). - If that
?returnsErr(beforeheldis added), line 911 still unconditionally executesasahi_test_user_locked_vm_add(-held), subtracting1024from the globalroot_user->locked_vmthat was never added and leaving it negative for the rest of the kernel run. - Track whether
heldwas added (e.g.,let mut held_added = false;) and only subtract-heldif it was actually added.
- Inside the cleanup closure in
Code Review of Core Accounting Path (Looks Good)
asahi_user_account()/asahi_user_unaccount()inuser_pages.c: Matchesio_uring(ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK)+ atomic cmpxchg loop onuser->locked_vm, pluscheck_add_overflowwhich is strictly safer) andiommufd's underflowWARN_ONwhile still callingfree_uid(user).UserPages::pin()/Drop:userandmmaccounting unwind cleanly ifpages.reserve(),asahi_pin_user_fragments(), orpin_user_pages_fast()fail afterthisis constructed.include/linux/sched/user.h: AddingIS_ENABLED(CONFIG_DRM_ASAHI)touser_struct::locked_vmmatchesIS_ENABLED(CONFIG_IOMMUFD).asahi_test_drop_ipc_lock()/asahi_test_restore_creds():prepare_creds()+override_creds()+put_cred(revert_creds())refcounting is correct for this kernel'srcu_replace_pointer-basedoverride_creds/revert_creds.
Pre-PR Checklist (§1–§6)
- §1 No model gating: Pass — applies to all GPUs supporting user-memory objects (
File::supports_user_memory()). - §2 Production-ready: Pass — default command line, no hacks.
- §3 Safety: Pass — uses standard
RLIMIT_MEMLOCK+user_struct::locked_vm. - §4 Clean-room & attribution: Pass — clean human commit identity and
Signed-off-by;Assisted-by:in PR body. - §5 Evidence: Strong J775D A/B row (including Borealis
dEQP-VK.memory.external_memory_host.*and cross-process fork/exitENOMEMverification), though as noted in## Still to runtheCONFIG_MM_SUBPAGEfragment-pin path (user4k/gem-userptr) and other G17 Macs remain to be run; also please re-verify a build withCONFIG_KUNIT=mafter fixing the cfg gate above. - §6 Naming: Pass (correctly uses Borealis).
Assisted-by: LLM (Gemini 4 Neon)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GEM_USERPTR counts the pages it pins in the calling mm's
pinned_vmand compares that count with RLIMIT_MEMLOCK.The object belongs to the DRM file, not to the mm. A forked child starts with nothing counted, its pins land in the
file it shares with its parent, and they stay pinned after it exits, counted against an mm that no longer runs
anything. So the limit doesn't bound what one user can pin.
Why this shape
The limit was checked in one place,
UserPages::pin(), against a count that belongs to an mm, while the pinnedobject belongs to the DRM file and can outlive the process that made it. The per-mm count (
mm->pinned_vm, as RDMAumem and amdxdna use it) has that property wherever the pinned object outlives the process; counting per DRM file
instead would still give each file of the user its own full limit. The change uses the count the kernel already keeps
for pins owned by a file rather than a process:
user_struct::locked_vm, which io_uring (__io_account_mem()) andiommufd charge the same way. Every process of the user then draws on one limit, a pin is charged until its object is
freed, and a CAP_IPC_LOCK caller is not charged at all, as in both. The mm's
pinned_vmstill counts the pages for/proc. Only the G17 backend creates user-memory objects, so this is the only place they are charged.Change
asahi_user_account()andasahi_user_unaccount()inuser_pages.c, beside the driver's other pin helpers: thesame arithmetic as
__io_account_mem(), with iommufd's underflow warning on release.UserPagesholds the chargeduser with a reference until it is dropped.
<linux/sched/user.h>: the driver joins the users ofuser_struct::locked_vm, as iommufd did.DRM_IOCTL_ASAHI_GEM_USERPTRsays what is counted.CONFIG_MM_SUBPAGE, drm/asahi: GEM_USERPTR in processes with a smaller page size #2226) is charged the same nativepages.
VK_EXT_external_memory_hostimports are the userspace that creates these objects.They now share one RLIMIT_MEMLOCK across all of the user's processes, where each process had its own before. Under
systemd's default (8 MiB for a user session) the sum of a user's host imports is what is limited; an import past
it fails with
VK_ERROR_OUT_OF_DEVICE_MEMORY, as one that took a single process past its own limit did before.The Gallium driver doesn't create these objects, and under virtio (
agx_has_userptr()) they aren't used.Testing
pins_count_against_the_userinasahi_gem_user_memory. A caller with CAP_IPC_LOCK is notcharged, while its mm still counts the pages. With CAP_IPC_LOCK dropped and RLIMIT_MEMLOCK set just above what the
test's user holds, pins within the limit are charged and given back. Once pages are charged to the user as another
of its processes would charge them, the next pin is refused, although the test's own mm has none. On
integration/aurora cacf355 plus this change only, 361 of 363 cases pass and 2 skip, as on integration alone,
where the removed
memlock_limit_policycase runs instead.drm/asahi: t6050: identify the K114/K116 27.2 (26B8125) G17S firmware (on #1173) #1650 (the 27.2 firmware's M5 InitData), arm64: dts: apple: t6050-j775d: Give the Mac Studio (M5 Ultra) a working GPU, and its M and P core capacities (754/1024) to the scheduler #1973 (the J775D GPU node), and two commits of ours not yet posted,
"drm/asahi: t6050: Run the Mac Studio's (M5 Ultra) GPU under the 27.2 boot stub: admit its 26B123 G17D firmware"
and "drm/asahi: t6050: Give the M5 Ultra's 27.2 firmware the power-controller status config of its own 26B123
layout". One boot with this change and one without, through the 27.2 firmware, default kernel command line,
read-only root.
user's processes, children that have exited included, and closing the file gives the charge back.
DefaultLimitMEMLOCK, which every process of the desktopsession has), with a Borealis build that advertises
VK_EXT_external_memory_host:dEQP-VK.memory.external_memory_host.*passes 4 and reports 8 not supported (image formats) of 12, 0 failed,with and without this change. Among the installed applications, FFmpeg's Vulkan upload doesn't import host
memory (no pinned pages while it runs), and the desktop session holds no pinned pages on either kernel. No
installed workload was found that imports host memory, so none is affected.
two) and no new kernel error at err level and above. ALARM has no SELinux, so there is no AVC count.
warning in the files this change touches.
run-host-tests.pyin the build VM: 451 of 451 pass with this change. The base's one failure in the same run is atimeout of an unrelated test's mutation run.
--strict: no errors.Still to run
CONFIG_MM_SUBPAGE.The
user4k/gem-userptrselftest on a subpage kit covers it, and itsVmPinchecks still hold.Assisted-by: LLM (Claude:Opus 5.5)