drm/lease: optionally expose leases as device nodes
Subsystems (4)
RFC PATCH v2 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 sibling of the primary node with DEVTYPE=drm_lease.
The exposed node deliberately reuses the anonymous lease file's state, including its private data and file operations.
This keeps DRM and driver ioctl handling on the original lessee without introducing a proxy driver.
The anonymous lease file remains alive while the exposed node is open. The device is unregistered when the lease is destroyed.
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.
---
Changes in v2:
* Restrict exposed-lease minor allocation and hotplug iteration to the 256 minors covered by register_chrdev().
* Replace get_file_rcu() with get_file_active() when pinning the anonymous lease file.
* Pin the copied file operations under the XArray lock and copy f_mapping from the anonymous lease file.
* Reorder device setup and error unwinding so the XArray entry is removed before the final device reference is dropped.
* Remove the stale <linux/idr.h> include and reformat the commit message.
v1: https://lore.kernel.org/dri-devel/[email protected]/
RFC questions:
* Is using O_CREAT to request device-node exposure acceptable UAPI, or should this use a DRM-specific flag?
* Are there lifetime or locking constraints missed by sharing the anonymous lease file's private_data and copied file operations?
The kernel only exposes the lease device; a userspace broker is still needed. For a static multiseat configuration the broker must run before logind. It opens the physical DRM node, acquires DRM master, creates all leases from the same lessor, and keeps the lessor and returned lease file descriptors open. The QEMU test setup used this logind.service drop-in:
[Unit]
Requires=drm-lease-broker.service
After=drm-lease-broker.serviceThe QEMU virtio-gpu resources used for the two leases were:
lease 1 / seat0:
connector 40 (Virtual-1)
CRTC 39
primary plane 35 and cursor plane 36 (implicitly included)
lease 2 / seat1:
connector 47 (Virtual-2)
CRTC 46
primary plane 42 and cursor plane 43 (implicitly included)
The minimal broker used for this test is available at:
https://gist.github.com/languagelawyer/cfe02f4a0d9ba9726a86f74ecb73f990
Its invocation and output were:
[root@archlinux ~]# /root/release /dev/dri/card0 [ 40 39 ] [ 47 46 ] Created lease 1 for group 1: 40 39
Created lease 2 for group 2: 47 46
These IDs are specific to this QEMU configuration; a real broker must enumerate the resources dynamically. DRM_CLIENT_CAP_UNIVERSAL_PLANES was not enabled, so the compatible planes were added implicitly by DRM. With that client capability enabled, the broker must include the plane IDs explicitly.
With the lease devices and separate input devices assigned through udev, logind reported:
[root@archlinux ~]# loginctl seat-status seat0
seat0
Devices: n/a
├─/sys/devices/pci0000:00/0000:00:02.0/drm/card0-lessee-1 │ [MASTER] drm:card0-lessee-1 ├─/sys/devices/pci0000:00/0000:00:07.0/virtio6/input/input3 │ input:input3 "QEMU Virtio Tablet" ├─/sys/devices/platform/LNXPWRBN:00/input/input0 │ input:input0 "Power Button" ├─/sys/devices/platform/i8042/serio0/input/input4 │ input:input4 "AT Translated Set 2 keyboard" └─/sys/devices/platform/i8042/serio1/input/input6
input:input6 "ImExPS/2 Generic Explorer Mouse"
[root@archlinux ~]# loginctl seat-status seat1
seat1
Devices: n/a
├─/sys/devices/pci0000:00/0000:00:02.0/drm/card0-lessee-2 │ [MASTER] drm:card0-lessee-2 ├─/sys/devices/pci0000:00/0000:00:03.0/virtio2/input/input1 │ input:input1 "QEMU Virtio Keyboard" └─/sys/devices/pci0000:00/0000:00:04.0/virtio3/input/input2
input:input2 "QEMU Virtio Tablet"
Two Xorg servers ran concurrently with different modes:
Virtual-1 connected primary 1024x768+0+0
Virtual-2 connected primary 800x600+0+0The corresponding systemd/udev change is available at
https://github.com/languagelawyer/systemd/commit/9781cf0be05f6bcec9bd428db86f37e7d3030570
It tags drm_lease devices as seat masters and prevents them from replacing the physical card's by-path symlink.
It is not part of this kernel patch.
Documentation/gpu/drm-uapi.rst | 41 +++++++ drivers/gpu/drm/drm_drv.c | 5 + drivers/gpu/drm/drm_internal.h | 5 + drivers/gpu/drm/drm_lease.c | 205 ++++++++++++++++++++++++++++++++- drivers/gpu/drm/drm_sysfs.c | 2 + include/uapi/drm/drm_mode.h | 7 +- 6 files changed, 263 insertions(+), 2 deletions(-)
Documentation/gpu/drm-uapi.rst +41 -0
@@ -48,6 +48,47 @@ DRM Display Resource Leasing .. kernel-doc:: drivers/gpu/drm/drm_lease.c :doc: drm leasing +Exposing a lease as a device +---------------------------- + +By default, :c:macro:`DRM_IOCTL_MODE_CREATE_LEASE` only 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 sibling of the primary DRM device and is named after the +primary node and the lessee ID. For example, lessee 1 of ``card0`` is exposed +as ``/dev/dri/card0-lessee-1``. Its uevent contains ``DEVTYPE=drm_lease`` so +that device managers can distinguish it from a DRM primary node. + +Opening the device accesses the same lease as the anonymous file descriptor. +In particular, all opens share the lessee's DRM file private data, including +its DRM master state, client capabilities and GEM handle namespace. Each open +device file keeps the lease alive. The device is unregistered after the last +reference to the lease file is closed. Revoking the lease removes its objects +but does not unregister the device while references remain open. + +Connector hotplug changes generate ``HOTPLUG=1`` change uevents for the +exposed lease as well as for the primary node. A device manager can make an +exposed lease available 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. Rules which +create ``dri/by-path/*-card`` links should therefore restrict those links to +``DEVTYPE=drm_minor`` so that a lease does not replace its primary node's +link. + Open-Source Userspace Requirements ==================================
drivers/gpu/drm/drm_drv.c +5 -0
@@ -1250,6 +1250,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 +1284,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_internal.h +5 -0
@@ -101,6 +101,10 @@ 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_lease.c */ +int drm_lease_init (void ); +void drm_lease_cleanup (void ); +/* drm_managed.c */ void drm_managed_release (struct drm_device *dev);void drmm_add_final_kfree (struct drm_device *dev,void *container);
@@ -171,6 +175,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 +204 -1
244 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/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. */
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Exposed lease devices are not unregistered when the primary DRM device is unregistered, causing a…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Exposed lease devices are not unregistered when the primary DRM device is unregistered, causing a resource leak of the hardware device.
- [High] A race condition in drm_expose_lease exposes the lease device to concurrent lookups before initialization completes, leading to a Use-After-Free on error paths. --
commit 384e9d119c9161556cff8b6b1ef37502f82fc82f
Author: Andrey Erokhin <[email protected]>
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 sibling of the primary node with DEVTYPE=drm_lease.
Quoted 4 lines
drivers/gpu/drm/drm_lease.c +0
-0
[ ... ]
Quoted 10 lines
drivers/gpu/drm/drm_lease.c +6
-0@@ -263,10 +271,144 @@ static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idrreturn ERR_PTR (error); } +struct drm_exposed_lease { +struct file *lessee_file; +struct drm_master *lessee; +struct file_operations fops; +struct device kdev; +};
[ ... ]
Quoted 22 lines
drivers/gpu/drm/drm_lease.c +22
-0+static int drm_expose_lease (struct file *lessee_file, +struct drm_master *lessee) +{ +int ret =0 ; +u32 minor; +struct drm_exposed_lease *exposed; +struct device *drm_kdev; +struct device *kdev; + + exposed =kzalloc_obj (*exposed); +if (!exposed) +return -ENOMEM ; + + exposed->lessee_file = lessee_file; + exposed->lessee = lessee; + exposed->fops = *lessee_file->f_op; + exposed->fops.release = drm_exposed_lease_release; + + drm_kdev = lessee->dev->primary->kdev; + kdev = &exposed->kdev; + +device_initialize (kdev);
drivers/gpu/drm/drm_lease.c static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr · line 367
Quoted 10 lines
drivers/gpu/drm/drm_lease.c +10
-0+ kdev->class = drm_kdev->class; + kdev->type = &drm_exposed_lease_device_type; + kdev->parent = drm_kdev->parent; + kdev->release = drm_exposed_device_release; + ret =dev_set_name (kdev,"%s-lessee-%d" ,dev_name (drm_kdev), lessee->lessee_id); +if (ret <0 ) + goto device_put; + + ret =xa_alloc (&drm_exposed_lease_minors_xa, &minor, exposed, +XA_LIMIT (0 ,DRM_EXPOSED_LEASE_MINORS -1 ),GFP_KERNEL );
[Severity: High]
This makes the exposed struct globally visible in the xarray before device_add() has completed.
Could a concurrent thread guess the minor number and call open() on the newly created lease device node while it is still being initialized?
drivers/gpu/drm/drm_lease.c static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr · line 372
Quoted 5 lines
drivers/gpu/drm/drm_lease.c +5
-0+if (ret <0 ) + goto device_put; + + kdev->devt =MKDEV (drm_exposed_lease_major, minor); + ret =device_add (kdev);
[Severity: High]
When the primary DRM device is unregistered (e.g., via drm_dev_unregister() on a hot-unplug), how are these exposed lease devices cleaned up?
Since this lease device takes a kobject reference to its parent (kdev->parent = drm_kdev->parent), if the lease devices are not explicitly unregistered during primary device teardown, could this pin the parent kobject indefinitely and leak the physical hardware device structures?
drivers/gpu/drm/drm_lease.c static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr · line 382
Quoted 10 lines
drivers/gpu/drm/drm_lease.c +10
-0+if (ret <0 ) + goto minor_free; + +return 0 ; + +minor_free: +xa_erase (&drm_exposed_lease_minors_xa, minor); + +device_put: +put_device (kdev);
[Severity: High]
If device_add() fails and we enter this error path, put_device(kdev) will free the exposed struct.
If a concurrent thread had already looked up the minor and passed it to drm_lease_open(), wouldn't this free cause a dangling pointer in the concurrent open()?
Quoted 3 lines
drivers/gpu/drm/drm_lease.c +3
-0+ +return ret; +}
[ ... ]
Quoted 13 lines
drivers/gpu/drm/drm_lease.c +13
-0+static int drm_lease_open (struct inode *inode,struct file *filp) +{ +struct drm_exposed_lease *exposed; +struct file *lessee_file =NULL ; +const struct file_operations *fops =NULL ; + +xa_lock (&drm_exposed_lease_minors_xa); + exposed =xa_load (&drm_exposed_lease_minors_xa,iminor (inode)); +if (exposed && exposed->lessee_file) { + lessee_file =get_file_active (&exposed->lessee_file); +if (lessee_file) + fops =fops_get (&exposed->fops); + }
drivers/gpu/drm/drm_lease.c int drm_mode_revoke_lease_ioctl(struct drm_device *dev, · line 906
Quoted 10 lines
drivers/gpu/drm/drm_lease.c +10
-0+xa_unlock (&drm_exposed_lease_minors_xa); + +if (!lessee_file) +return -ENODEV ; +if (!fops) { +fput (lessee_file); +return -ENODEV ; + } + +replace_fops (filp, fops);
[Severity: High]
If drm_expose_lease() fails at device_add() and frees the exposed struct after this concurrent drm_lease_open() has called replace_fops(filp, fops), won't filp->f_op point to freed memory?
Could this lead to a Use-After-Free on subsequent syscalls (like ioctl or close)?
Quoted 5 lines
drivers/gpu/drm/drm_lease.c +5
-0+ filp->f_mapping = lessee_file->f_mapping; + filp->private_data = lessee_file->private_data; + +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1