drm/lease: optionally expose leases as device nodes
Subsystems (4)
RFC PATCH v3 1/1 drm/lease: optionally expose leases as device nodes
DRM leases are only returned as anonymous file descriptors. Device managers such as udev and logind cannot discover these leases, assign them to seats, or hand them to sessions via their device APIs.
Accept O_CREAT in DRM_IOCTL_MODE_CREATE_LEASE to expose the lease as a DRM class device named after its primary node and lessee ID. Make the device a child of the primary node with DEVTYPE=drm_lease.
Give each open of the exposed node an independent DRM file context, with its own client capabilities, GEM and syncobj handle namespaces, event queues, and driver-private state.
Reuse the physical driver's file operations and the common DRM open and release paths without introducing a proxy driver.
Use the physical device's shared address space for mappings.
Share the lessee's DRM master and leased resources among these clients. Each client holds a reference to the original anonymous lease file, keeping the lease alive without sharing its per-file state. Closing an independently opened client does not revoke the lease; the node remains while references to the original file exist. Revocation clears the resource set but leaves the node registered. Unregistering the physical DRM device removes its exposed lease nodes and prevents new opens, even if existing clients still hold references.
Arbitrate modesetting access between independent opens of each lease. An open acquires a free local master role if the lessor is current master. SET_MASTER acquires a free role explicitly; DROP_MASTER releases it. Dropping master preserves both the lease and the client's file context. Closing the active client also releases the role.
Retain the usual master permission checks.
Serialize local ownership changes with the existing master_mutex. This lets logind hand modesetting access between sessions on one lease without changing the physical master or other leases' active clients.
The original anonymous lease file remains outside this arbitration and retains its existing authority.
Keep this file with the trusted lease broker.
All clients still depend on the lessor being current master.
Mirror DRM hotplug uevents to exposed leases so that a display server monitoring its assigned node observes connector changes.
Userspace can identify these nodes through DEVTYPE=drm_lease. Udev rules should tag lease nodes as seat masters.
Since they inherit the physical device's ID_PATH,
lease nodes should be excluded from ordinary DRM card by-path symlinks.
---
AI disclosure:
Most of the coding for this revision was done by an AI coding assistant (ChatGPT/Codex), including the per-open contexts and lease-local master arbitration. I have read through the resulting code myself. The assistant also helped with test programs, QEMU testing and debugging.
Changes since v2:
* Give each exposed-node open its own drm_file instead of sharing private_data with the original anonymous lease file. Client capabilities, handles, events and driver-private state are now per open, with ordinary DRM/driver cleanup on release.
* Add lease-local SET_MASTER/DROP_MASTER arbitration for these opens. Keep the broker's original anonymous lease file outside this scheme.
* Reuse the physical driver's fops directly, without a copied fops table or custom release wrapper, and use the common DRM open path with the physical device's shared address space.
* Serialize publication, lookup and teardown with a dedicated mutex, retain get_file_active() for pinning the original lease file, and use the locking XArray operations for allocation and removal. New opens cannot access a node before publication completes.
* Unpublish exposed nodes when the physical DRM device unregisters. Retain their minor reservations until lease destruction.
* Register and allocate the full MINORBITS minor range for the exposed-lease character device, rather than the 256-minor range.
* Rename the nodes to cardN-lease-ID and make them children of cardN, rather than siblings named cardN-lessee-ID.
* Update the UAPI documentation, lifetime comments and helper names.
v2: https://lore.kernel.org/dri-devel/[email protected]/
v1: https://lore.kernel.org/dri-devel/[email protected]/
Userspace setup:
A broker must own the physical DRM master, create all leases from that lessor, and retain the physical and original anonymous lease descriptors. Session clients open the exposed nodes, normally through logind. They must not receive the broker's original anonymous lease descriptors, which retain their existing authority outside local master arbitration.
The stand starts the broker on the card0 udev event
and orders the display manager after drm-lease-broker.service. The physical card is reserved for the broker;
lease nodes and separate input devices are assigned to seat0 and seat1.
The minimal example broker is available at:
https://gist.github.com/languagelawyer/cfe02f4a0d9ba9726a86f74ecb73f990
The two-head virtio-gpu configuration used:
/root/release /dev/dri/card0 [ 40 39 ] [ 47 46 ]
lease 1 / seat0: connector 40 (Virtual-1), CRTC 39
lease 2 / seat1: connector 47 (Virtual-2), CRTC 46The corresponding primary/cursor plane IDs are 35/36 and 42/43. These IDs are specific to the stand; a real broker must enumerate them. The example does not enable DRM_CLIENT_CAP_UNIVERSAL_PLANES, so DRM adds the compatible planes implicitly.
With that capability enabled, plane IDs must be included explicitly.
The systemd/udev rule changes are available at:
https://github.com/languagelawyer/systemd/commit/93c15122fad4d7017d4c914eb0c1b925e4696c87
This userspace change is not part of the kernel patch.
It tags lease nodes as seat masters and excludes them from card by-path symlinks.
With LightDM running, logind reports a graphical session on each seat:
# loginctl show-seat seat0 -p Id -p ActiveSession -p Sessions -p CanGraphical
Id=seat0
ActiveSession=c64
CanGraphical=yes
Sessions=c64 # loginctl show-seat seat1 -p Id -p ActiveSession -p Sessions -p CanGraphical
Id=seat1
ActiveSession=c63
CanGraphical=yes
Sessions=c63Testing and limitations:
Initial tests used an Arch guest with two virtio-gpu heads, separate input devices, logind, lockdep and CONFIG_VT disabled. Per-open capability, GEM/syncobj handle and event isolation passed, as did close/reopen, lease lifetime, revoke and local-master handover tests. Concurrent SET_MASTER stress found no overlapping local owners; this is not a proof that every ownership or teardown race is covered.
Two Xorg servers work concurrently, one on each lease.
In that non-VT setup, switching users on one lease failed end to end. The LightDM setup initially launched Xorg outside a logind session. With Xorg launched in proper PAM/logind sessions, master handover worked, but Xorg did not restore its scanout mode automatically on resume and also reported input re-enable failures.
An explicit xrandr mode change restored the intended output mode.
A follow-up with CONFIG_VT enabled passed 20/20 switches between two Xorg 21.1.21 sessions on seat0, using separate VTs and proper PAM sessions. Each resumed server automatically restored its 1024x768 or 800x600 mode and received tablet input, without corrective xrandr mode changes. The Xorg on the other lease retained its active 800x600 mode and input. Checks used kernel CRTC state and X input valuators; all servers survived. No kernel source or Xorg source changes were needed for this follow-up. LightDM's own switching workflow was not validated by that test.
Repeating the test on seat1, where Xorg does not use a VT, reproduced the scanout/input recovery failure despite CONFIG_VT=y. Across six activations, logind switched sessions and local master moved, but A did not restore 1024x768 over B's 800x600 scanout. Both resumed servers failed injected tablet and keyboard checks; the control Xorg on seat0 retained its mode and input.
A separate explicit mode change succeeded after the switching test.
Weston was used for end-to-end session-switching tests without VT. Unmodified Weston 15.0.1 and main at 9669073fe8f4 (16.0.90) could stall DRM recovery after a hotplug event while the session was inactive:
recovery deferred future repaints even though no recovery repaint had been scheduled. The session regained master but stopped rendering. A controlled event for the lease's own connector reproduced the problem.
The following existing Weston MR resolved the tested failure:
https://gitlab.freedesktop.org/wayland/weston/-/merge_requests/2095
With its full diff from f8f49eac61d9 applied to main at 9669073fe8f4:
* 10/10 pause -> own-connector hotplug -> resume cycles passed.
* 20/20 switches between two users on one lease passed,
alternating 1024x768 and 800x600, while the other lease kept rendering.
* Tests checked actual CRTC modes and continuing page flips, not merely that the compositor process survived or held master.
The switching test also observed genuine kernel HOTPLUG=1 events, without CONNECTOR, on the physical card and both exposed leases.
Pixman and software GL/llvmpipe rendering were exercised on the leases. This does not cover hardware-accelerated rendering or other GPU drivers.
RFC questions:
* Is O_CREAT acceptable for requesting exposure, or should this use a DRM-specific flag?
* Does this per-open context and lease-local master model fit DRM's ownership rules, particularly driver callbacks and paths that inspect is_master directly?
Documentation/gpu/drm-uapi.rst | 65 +++++++++ drivers/gpu/drm/drm_auth.c | 61 ++++++++- drivers/gpu/drm/drm_drv.c | 6 + drivers/gpu/drm/drm_file.c | 79 +++++++---- drivers/gpu/drm/drm_internal.h | 9 ++ drivers/gpu/drm/drm_lease.c | 237 ++++++++++++++++++++++++++++++++- drivers/gpu/drm/drm_sysfs.c | 2 + include/drm/drm_auth.h | 11 ++ include/drm/drm_file.h | 15 ++- include/uapi/drm/drm_mode.h | 7 +- 10 files changed, 458 insertions(+), 34 deletions(-)
Documentation/gpu/drm-uapi.rst +65 -0
@@ -48,6 +48,71 @@ DRM Display Resource Leasing .. kernel-doc:: drivers/gpu/drm/drm_lease.c :doc: drm leasing +Exposing a lease as a device +---------------------------- + +By default, lease creation returns an anonymous file descriptor. +Passing ``O_CREAT`` in ``drm_mode_create_lease.flags`` +also registers the lease as a DRM class device. +The libdrm wrapper can be used as follows:: + + uint32_t lessee_id; + int lease_fd; + + lease_fd = drmModeCreateLease(lessor_fd, object_ids, object_count, + O_CLOEXEC | O_CREAT, &lessee_id); + if (lease_fd < 0) + /* Handle the error. */ + +The device is a child of the primary DRM device. +Its name contains the primary node and the lessee ID. +For example, lessee 1 of ``card0`` is exposed as ``/dev/dri/card0-lease-1``. +Its uevent contains ``DEVTYPE=drm_lease``, +allowing device managers to distinguish it from a DRM primary node. + +Opening the device creates an independent DRM file context for the same lease. +Client capabilities, GEM and sync object handles, and event queues +are private to each open and are cleaned up when that file is released. +The DRM master and leased display resources are shared. +Each open device file keeps the original lease file alive, +and closing an independently opened client does not revoke the lease. +The node remains until the last reference to the lease file is closed. +Revoking the lease removes its objects, +but leaves the device registered while references remain open. +If the underlying DRM device is unregistered, +the lease device node is removed immediately. +Existing opens retain their usual DRM unplug semantics. + +Only one independently opened client can be current master of a lease. +The first open becomes master if the lessor is current master. +``DROP_MASTER`` relinquishes this role without revoking the lease, +and ``SET_MASTER`` acquires it if no other client holds it. +Closing the active client also relinquishes the role; +existing clients must use ``SET_MASTER`` to acquire it. +The usual master permission checks apply. +This arbitration does not change the physical device's master +or the active clients of other leases. + +The original anonymous lease file retains its existing master semantics +and does not participate in this arbitration. +It must remain with the trusted lease broker rather than being handed +to a session that needs suspendable modesetting access. +All clients still depend on the lessor being current master. + +Connector hotplug changes generate ``HOTPLUG=1`` change uevents +for both the exposed lease and primary node. +A device manager can expose it for seat assignment with a rule such as:: + + SUBSYSTEM=="drm", ENV{DEVTYPE}=="drm_lease", \ + ENV{ID_FOR_SEAT}="drm-lease-$kernel", TAG+="seat", \ + TAG+="master-of-seat" + +An exposed lease inherits the physical device's path information. +Its name also matches ``card*``. +Rules which create ``dri/by-path/*-card`` links +should therefore require ``DEVTYPE=drm_minor``, +so that a lease does not replace its primary node's link. + Open-Source Userspace Requirements ==================================
drivers/gpu/drm/drm_auth.c +58 -3
109 lines not on this page · raw message
drivers/gpu/drm/drm_drv.c +6 -0
@@ -1168,6 +1168,7 @@ EXPORT_SYMBOL(drm_dev_register);void drm_dev_unregister (struct drm_device *dev) { dev->registered = false; +drm_lease_unpublish_exposed (dev);drm_client_sysrq_unregister (dev);drm_panic_unregister (dev);
@@ -1250,6 +1251,7 @@ static void drm_core_exit(void)drm_privacy_screen_lookup_exit ();drm_panic_exit ();accel_core_exit (); +drm_lease_cleanup ();unregister_chrdev (DRM_MAJOR ,"drm" );drm_debugfs_remove_root ();drm_sysfs_destroy ();
@@ -1283,6 +1285,10 @@ static int __init drm_core_init(void)drm_panic_init (); + ret =drm_lease_init (); +if (ret <0 ) + goto error; +drm_privacy_screen_lookup_init (); ret =drm_ras_genl_family_register ();
drivers/gpu/drm/drm_file.c +52 -27
120 lines not on this page · raw message
drivers/gpu/drm/drm_internal.h +9 -0
@@ -101,6 +101,14 @@ int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,void drm_prime_remove_buf_handle (struct drm_prime_file_private *prime_fpriv,uint32_t handle); +/* drm_file.c */ +int drm_open_lease (struct file *filp,struct file *lease_file); + +/* drm_lease.c */ +int drm_lease_init (void ); +void drm_lease_cleanup (void ); +void drm_lease_unpublish_exposed (struct drm_device *dev); +/* drm_managed.c */ void drm_managed_release (struct drm_device *dev);void drmm_add_final_kfree (struct drm_device *dev,void *container);
@@ -171,6 +179,7 @@ void drm_sysfs_connector_remove_early(struct drm_connector *connector);void drm_sysfs_connector_remove (struct drm_connector *connector);void drm_sysfs_lease_event (struct drm_device *dev); +void drm_lease_uevent (struct drm_device *dev,char *envp[]);/* drm_gem.c */ int drm_gem_init (struct drm_device *dev);
drivers/gpu/drm/drm_lease.c +236 -1
276 lines not on this page · raw message
drivers/gpu/drm/drm_sysfs.c +2 -0
@@ -449,6 +449,7 @@ void drm_sysfs_hotplug_event(struct drm_device *dev)drm_dbg_kms (dev,"generating hotplug event\n" );kobject_uevent_env (&dev->primary->kdev->kobj,KOBJ_CHANGE , envp); +drm_lease_uevent (dev, envp); }EXPORT_SYMBOL (drm_sysfs_hotplug_event);
@@ -474,6 +475,7 @@ void drm_sysfs_connector_hotplug_event(struct drm_connector *connector) connector->base.id, connector->name);kobject_uevent_env (&dev->primary->kdev->kobj,KOBJ_CHANGE , envp); +drm_lease_uevent (dev, envp); }EXPORT_SYMBOL (drm_sysfs_connector_hotplug_event);
include/drm/drm_auth.h +11 -0
@@ -80,6 +80,17 @@ struct drm_master {*/ struct drm_master *lessor; +/** +* @lease_master: +* +* Active independently opened client of an exposed lease, or NULL. +* The original anonymous lease file does not participate in this +* arbitration. Protected by &drm_device.master_mutex, with READ_ONCE() +* for current-master checks. This pointer is only compared, never +* dereferenced, and is cleared before the client is freed. +*/ +struct drm_file *lease_master; +/** * @lessee_id: *
include/drm/drm_file.h +13 -2
@@ -228,8 +228,9 @@ struct drm_file {/** * @is_master: * -* This client is the creator of @master. Protected by struct -* &drm_device.master_mutex. +* This client is the creator of @master, +* or an independently opened client of an exposed lease. +* Protected by struct &drm_device.master_mutex. * * See also the :ref:`section on primary nodes and authentication * <drm_primary_node>`.
@@ -323,6 +324,16 @@ struct drm_file {/** @filp: Pointer to the core file structure. */ struct file *filp; +/** +* @lease_file: +* +* Reference to the original lease file for an independently opened +* exposed lease client. Keeps the lease alive without sharing its +* per-file resources. This client must not revoke the lease on close. +* NULL for ordinary DRM files, including the original lease file. +*/ +struct file *lease_file; +/** * @driver_priv: *
include/uapi/drm/drm_mode.h +6 -1
@@ -1436,7 +1436,12 @@ struct drm_mode_create_lease {__u64 object_ids;/** @object_count: Number of object ids */ __u32 object_count; -/** @flags: flags for new FD (O_CLOEXEC, etc) */ +/** +* @flags: Flags for the new file descriptor. +* +* O_CLOEXEC and O_NONBLOCK control the returned file descriptor. +* O_CREAT additionally exposes the lease as a DRM class device. +*/ __u32 flags;/** @lessee_id: Return: unique identifier for lessee. */
Signature · 1 line
2.55.0