1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright © 2017 Keith Packard <keithp@keithp.com>
5 #include <linux/file.h>
6 #include <linux/uaccess.h>
8 #include <drm/drm_auth.h>
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_lease.h>
13 #include <drm/drm_print.h>
15 #include "drm_crtc_internal.h"
16 #include "drm_internal.h"
21 * DRM leases provide information about whether a DRM master may control a DRM
22 * mode setting object. This enables the creation of multiple DRM masters that
23 * manage subsets of display resources.
25 * The original DRM master of a device 'owns' the available drm resources. It
26 * may create additional DRM masters and 'lease' resources which it controls
27 * to the new DRM master. This gives the new DRM master control over the
28 * leased resources until the owner revokes the lease, or the new DRM master
29 * is closed. Some helpful terminology:
31 * - An 'owner' is a &struct drm_master that is not leasing objects from
32 * another &struct drm_master, and hence 'owns' the objects. The owner can be
33 * identified as the &struct drm_master for which &drm_master.lessor is NULL.
35 * - A 'lessor' is a &struct drm_master which is leasing objects to one or more
36 * other &struct drm_master. Currently, lessees are not allowed to
37 * create sub-leases, hence the lessor is the same as the owner.
39 * - A 'lessee' is a &struct drm_master which is leasing objects from some
40 * other &struct drm_master. Each lessee only leases resources from a single
41 * lessor recorded in &drm_master.lessor, and holds the set of objects that
42 * it is leasing in &drm_master.leases.
44 * - A 'lease' is a contract between the lessor and lessee that identifies
45 * which resources may be controlled by the lessee. All of the resources
46 * that are leased must be owned by or leased to the lessor, and lessors are
47 * not permitted to lease the same object to multiple lessees.
49 * The set of objects any &struct drm_master 'controls' is limited to the set
50 * of objects it leases (for lessees) or all objects (for owners).
52 * Objects not controlled by a &struct drm_master cannot be modified through
53 * the various state manipulating ioctls, and any state reported back to user
54 * space will be edited to make them appear idle and/or unusable. For
55 * instance, connectors always report 'disconnected', while encoders
56 * report no possible crtcs or clones.
58 * Since each lessee may lease objects from a single lessor, display resource
59 * leases form a tree of &struct drm_master. As lessees are currently not
60 * allowed to create sub-leases, the tree depth is limited to 1. All of
61 * these get activated simultaneously when the top level device owner changes
62 * through the SETMASTER or DROPMASTER IOCTL, so &drm_device.master points to
63 * the owner at the top of the lease tree (i.e. the &struct drm_master for which
64 * &drm_master.lessor is NULL). The full list of lessees that are leasing
65 * objects from the owner can be searched via the owner's
66 * &drm_master.lessee_idr.
69 #define drm_for_each_lessee(lessee, lessor) \
70 list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
72 static uint64_t drm_lease_idr_object
;
74 struct drm_master
*drm_lease_owner(struct drm_master
*master
)
76 while (master
->lessor
!= NULL
)
77 master
= master
->lessor
;
81 static struct drm_master
*
82 _drm_find_lessee(struct drm_master
*master
, int lessee_id
)
84 lockdep_assert_held(&master
->dev
->mode_config
.idr_mutex
);
85 return idr_find(&drm_lease_owner(master
)->lessee_idr
, lessee_id
);
88 static int _drm_lease_held_master(struct drm_master
*master
, int id
)
90 lockdep_assert_held(&master
->dev
->mode_config
.idr_mutex
);
92 return idr_find(&master
->leases
, id
) != NULL
;
96 /* Checks if the given object has been leased to some lessee of drm_master */
97 static bool _drm_has_leased(struct drm_master
*master
, int id
)
99 struct drm_master
*lessee
;
101 lockdep_assert_held(&master
->dev
->mode_config
.idr_mutex
);
102 drm_for_each_lessee(lessee
, master
)
103 if (_drm_lease_held_master(lessee
, id
))
108 /* Called with idr_mutex held */
109 bool _drm_lease_held(struct drm_file
*file_priv
, int id
)
112 struct drm_master
*master
;
117 master
= drm_file_get_master(file_priv
);
120 ret
= _drm_lease_held_master(master
, id
);
121 drm_master_put(&master
);
126 bool drm_lease_held(struct drm_file
*file_priv
, int id
)
128 struct drm_master
*master
;
134 master
= drm_file_get_master(file_priv
);
137 if (!master
->lessor
) {
141 mutex_lock(&master
->dev
->mode_config
.idr_mutex
);
142 ret
= _drm_lease_held_master(master
, id
);
143 mutex_unlock(&master
->dev
->mode_config
.idr_mutex
);
146 drm_master_put(&master
);
151 * Given a bitmask of crtcs to check, reconstructs a crtc mask based on the
152 * crtcs which are visible through the specified file.
154 uint32_t drm_lease_filter_crtcs(struct drm_file
*file_priv
, uint32_t crtcs_in
)
156 struct drm_master
*master
;
157 struct drm_device
*dev
;
158 struct drm_crtc
*crtc
;
159 int count_in
, count_out
;
160 uint32_t crtcs_out
= 0;
165 master
= drm_file_get_master(file_priv
);
168 if (!master
->lessor
) {
169 crtcs_out
= crtcs_in
;
174 count_in
= count_out
= 0;
175 mutex_lock(&master
->dev
->mode_config
.idr_mutex
);
176 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
177 if (_drm_lease_held_master(master
, crtc
->base
.id
)) {
178 uint32_t mask_in
= 1ul << count_in
;
180 if ((crtcs_in
& mask_in
) != 0) {
181 uint32_t mask_out
= 1ul << count_out
;
183 crtcs_out
|= mask_out
;
189 mutex_unlock(&master
->dev
->mode_config
.idr_mutex
);
192 drm_master_put(&master
);
197 * Uses drm_master_create to allocate a new drm_master, then checks to
198 * make sure all of the desired objects can be leased, atomically
199 * leasing them to the new drmmaster.
201 * ERR_PTR(-EACCES) some other master holds the title to any object
202 * ERR_PTR(-ENOENT) some object is not a valid DRM object for this device
203 * ERR_PTR(-EBUSY) some other lessee holds title to this object
204 * ERR_PTR(-EEXIST) same object specified more than once in the provided list
205 * ERR_PTR(-ENOMEM) allocation failed
207 static struct drm_master
*drm_lease_create(struct drm_master
*lessor
, struct idr
*leases
)
209 struct drm_device
*dev
= lessor
->dev
;
211 struct drm_master
*lessee
;
216 drm_dbg_lease(dev
, "lessor %d\n", lessor
->lessee_id
);
218 lessee
= drm_master_create(lessor
->dev
);
220 drm_dbg_lease(dev
, "drm_master_create failed\n");
221 return ERR_PTR(-ENOMEM
);
224 mutex_lock(&dev
->mode_config
.idr_mutex
);
226 idr_for_each_entry(leases
, entry
, object
) {
228 if (!idr_find(&dev
->mode_config
.object_idr
, object
))
230 else if (_drm_has_leased(lessor
, object
))
234 drm_dbg_lease(dev
, "object %d failed %d\n", object
, error
);
239 /* Insert the new lessee into the tree */
240 id
= idr_alloc(&(drm_lease_owner(lessor
)->lessee_idr
), lessee
, 1, 0, GFP_KERNEL
);
246 lessee
->lessee_id
= id
;
247 lessee
->lessor
= drm_master_get(lessor
);
248 list_add_tail(&lessee
->lessee_list
, &lessor
->lessees
);
250 /* Move the leases over */
251 lessee
->leases
= *leases
;
252 drm_dbg_lease(dev
, "new lessee %d %p, lessor %d %p\n",
253 lessee
->lessee_id
, lessee
, lessor
->lessee_id
, lessor
);
255 mutex_unlock(&dev
->mode_config
.idr_mutex
);
259 mutex_unlock(&dev
->mode_config
.idr_mutex
);
261 drm_master_put(&lessee
);
263 return ERR_PTR(error
);
266 void drm_lease_destroy(struct drm_master
*master
)
268 struct drm_device
*dev
= master
->dev
;
270 mutex_lock(&dev
->mode_config
.idr_mutex
);
272 drm_dbg_lease(dev
, "drm_lease_destroy %d\n", master
->lessee_id
);
274 /* This master is referenced by all lessees, hence it cannot be destroyed
275 * until all of them have been
277 WARN_ON(!list_empty(&master
->lessees
));
279 /* Remove this master from the lessee idr in the owner */
280 if (master
->lessee_id
!= 0) {
281 drm_dbg_lease(dev
, "remove master %d from device list of lessees\n",
283 idr_remove(&(drm_lease_owner(master
)->lessee_idr
), master
->lessee_id
);
286 /* Remove this master from any lessee list it may be on */
287 list_del(&master
->lessee_list
);
289 mutex_unlock(&dev
->mode_config
.idr_mutex
);
291 if (master
->lessor
) {
292 /* Tell the master to check the lessee list */
293 drm_sysfs_lease_event(dev
);
294 drm_master_put(&master
->lessor
);
297 drm_dbg_lease(dev
, "drm_lease_destroy done %d\n", master
->lessee_id
);
300 static void _drm_lease_revoke(struct drm_master
*top
)
304 struct drm_master
*master
= top
;
306 lockdep_assert_held(&top
->dev
->mode_config
.idr_mutex
);
309 * Walk the tree starting at 'top' emptying all leases. Because
310 * the tree is fully connected, we can do this without recursing
313 drm_dbg_lease(master
->dev
, "revoke leases for %p %d\n",
314 master
, master
->lessee_id
);
316 /* Evacuate the lease */
317 idr_for_each_entry(&master
->leases
, entry
, object
)
318 idr_remove(&master
->leases
, object
);
320 /* Depth-first list walk */
323 if (!list_empty(&master
->lessees
)) {
324 master
= list_first_entry(&master
->lessees
, struct drm_master
, lessee_list
);
327 while (master
!= top
&& master
== list_last_entry(&master
->lessor
->lessees
, struct drm_master
, lessee_list
))
328 master
= master
->lessor
;
334 master
= list_next_entry(master
, lessee_list
);
339 void drm_lease_revoke(struct drm_master
*top
)
341 mutex_lock(&top
->dev
->mode_config
.idr_mutex
);
342 _drm_lease_revoke(top
);
343 mutex_unlock(&top
->dev
->mode_config
.idr_mutex
);
346 static int validate_lease(struct drm_device
*dev
,
348 struct drm_mode_object
**objects
,
349 bool universal_planes
)
353 int has_connector
= -1;
356 /* we want to confirm that there is at least one crtc, plane
359 for (o
= 0; o
< object_count
; o
++) {
360 if (objects
[o
]->type
== DRM_MODE_OBJECT_CRTC
&& has_crtc
== -1) {
363 if (objects
[o
]->type
== DRM_MODE_OBJECT_CONNECTOR
&& has_connector
== -1)
366 if (universal_planes
) {
367 if (objects
[o
]->type
== DRM_MODE_OBJECT_PLANE
&& has_plane
== -1)
371 if (has_crtc
== -1 || has_connector
== -1)
373 if (universal_planes
&& has_plane
== -1)
378 static int fill_object_idr(struct drm_device
*dev
,
379 struct drm_file
*lessor_priv
,
384 struct drm_mode_object
**objects
;
387 bool universal_planes
= READ_ONCE(lessor_priv
->universal_planes
);
389 objects
= kcalloc(object_count
, sizeof(struct drm_mode_object
*),
394 /* step one - get references to all the mode objects
395 and check for validity. */
396 for (o
= 0; o
< object_count
; o
++) {
397 objects
[o
] = drm_mode_object_find(dev
, lessor_priv
,
399 DRM_MODE_OBJECT_ANY
);
402 goto out_free_objects
;
405 if (!drm_mode_object_lease_required(objects
[o
]->type
)) {
406 DRM_DEBUG_KMS("invalid object for lease\n");
408 goto out_free_objects
;
412 ret
= validate_lease(dev
, object_count
, objects
, universal_planes
);
414 drm_dbg_lease(dev
, "lease validation failed\n");
415 goto out_free_objects
;
418 /* add their IDs to the lease request - taking into account
420 for (o
= 0; o
< object_count
; o
++) {
421 struct drm_mode_object
*obj
= objects
[o
];
422 u32 object_id
= objects
[o
]->id
;
424 drm_dbg_lease(dev
, "Adding object %d to lease\n", object_id
);
427 * We're using an IDR to hold the set of leased
428 * objects, but we don't need to point at the object's
429 * data structure from the lease as the main object_idr
430 * will be used to actually find that. Instead, all we
431 * really want is a 'leased/not-leased' result, for
432 * which any non-NULL pointer will work fine.
434 ret
= idr_alloc(leases
, &drm_lease_idr_object
, object_id
, object_id
+ 1, GFP_KERNEL
);
436 drm_dbg_lease(dev
, "Object %d cannot be inserted into leases (%d)\n",
438 goto out_free_objects
;
440 if (obj
->type
== DRM_MODE_OBJECT_CRTC
&& !universal_planes
) {
441 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
443 ret
= idr_alloc(leases
, &drm_lease_idr_object
, crtc
->primary
->base
.id
, crtc
->primary
->base
.id
+ 1, GFP_KERNEL
);
445 drm_dbg_lease(dev
, "Object primary plane %d cannot be inserted into leases (%d)\n",
447 goto out_free_objects
;
450 ret
= idr_alloc(leases
, &drm_lease_idr_object
, crtc
->cursor
->base
.id
, crtc
->cursor
->base
.id
+ 1, GFP_KERNEL
);
452 drm_dbg_lease(dev
, "Object cursor plane %d cannot be inserted into leases (%d)\n",
454 goto out_free_objects
;
462 for (o
= 0; o
< object_count
; o
++) {
464 drm_mode_object_put(objects
[o
]);
471 * The master associated with the specified file will have a lease
472 * created containing the objects specified in the ioctl structure.
473 * A file descriptor will be allocated for that and returned to the
476 int drm_mode_create_lease_ioctl(struct drm_device
*dev
,
477 void *data
, struct drm_file
*lessor_priv
)
479 struct drm_mode_create_lease
*cl
= data
;
483 struct drm_master
*lessor
;
484 struct drm_master
*lessee
= NULL
;
485 struct file
*lessee_file
= NULL
;
486 struct file
*lessor_file
= lessor_priv
->filp
;
487 struct drm_file
*lessee_priv
;
489 uint32_t *object_ids
;
491 /* Can't lease without MODESET */
492 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
495 if (cl
->flags
&& (cl
->flags
& ~(O_CLOEXEC
| O_NONBLOCK
))) {
496 drm_dbg_lease(dev
, "invalid flags\n");
500 lessor
= drm_file_get_master(lessor_priv
);
501 /* Do not allow sub-leases */
502 if (lessor
->lessor
) {
503 drm_dbg_lease(dev
, "recursive leasing not allowed\n");
508 object_count
= cl
->object_count
;
510 /* Handle leased objects, if any */
512 if (object_count
!= 0) {
513 object_ids
= memdup_array_user(u64_to_user_ptr(cl
->object_ids
),
514 object_count
, sizeof(__u32
));
515 if (IS_ERR(object_ids
)) {
516 ret
= PTR_ERR(object_ids
);
517 idr_destroy(&leases
);
521 /* fill and validate the object idr */
522 ret
= fill_object_idr(dev
, lessor_priv
, &leases
,
523 object_count
, object_ids
);
526 drm_dbg_lease(dev
, "lease object lookup failed: %i\n", ret
);
527 idr_destroy(&leases
);
532 /* Allocate a file descriptor for the lease */
533 fd
= get_unused_fd_flags(cl
->flags
& (O_CLOEXEC
| O_NONBLOCK
));
535 idr_destroy(&leases
);
540 drm_dbg_lease(dev
, "Creating lease\n");
541 /* lessee will take the ownership of leases */
542 lessee
= drm_lease_create(lessor
, &leases
);
544 if (IS_ERR(lessee
)) {
545 ret
= PTR_ERR(lessee
);
546 idr_destroy(&leases
);
550 /* Clone the lessor file to create a new file for us */
551 drm_dbg_lease(dev
, "Allocating lease file\n");
552 lessee_file
= file_clone_open(lessor_file
);
553 if (IS_ERR(lessee_file
)) {
554 ret
= PTR_ERR(lessee_file
);
558 lessee_priv
= lessee_file
->private_data
;
559 /* Change the file to a master one */
560 drm_master_put(&lessee_priv
->master
);
561 lessee_priv
->master
= lessee
;
562 lessee_priv
->is_master
= 1;
563 lessee_priv
->authenticated
= 1;
565 /* Pass fd back to userspace */
566 drm_dbg_lease(dev
, "Returning fd %d id %d\n", fd
, lessee
->lessee_id
);
568 cl
->lessee_id
= lessee
->lessee_id
;
571 fd_install(fd
, lessee_file
);
573 drm_master_put(&lessor
);
574 drm_dbg_lease(dev
, "drm_mode_create_lease_ioctl succeeded\n");
578 drm_master_put(&lessee
);
584 drm_master_put(&lessor
);
585 drm_dbg_lease(dev
, "drm_mode_create_lease_ioctl failed: %d\n", ret
);
589 int drm_mode_list_lessees_ioctl(struct drm_device
*dev
,
590 void *data
, struct drm_file
*lessor_priv
)
592 struct drm_mode_list_lessees
*arg
= data
;
593 __u32 __user
*lessee_ids
= (__u32 __user
*) (uintptr_t) (arg
->lessees_ptr
);
594 __u32 count_lessees
= arg
->count_lessees
;
595 struct drm_master
*lessor
, *lessee
;
602 /* Can't lease without MODESET */
603 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
606 lessor
= drm_file_get_master(lessor_priv
);
607 drm_dbg_lease(dev
, "List lessees for %d\n", lessor
->lessee_id
);
609 mutex_lock(&dev
->mode_config
.idr_mutex
);
612 drm_for_each_lessee(lessee
, lessor
) {
613 /* Only list un-revoked leases */
614 if (!idr_is_empty(&lessee
->leases
)) {
615 if (count_lessees
> count
) {
616 drm_dbg_lease(dev
, "Add lessee %d\n",
618 ret
= put_user(lessee
->lessee_id
, lessee_ids
+ count
);
626 drm_dbg_lease(dev
, "Lessor leases to %d\n", count
);
628 arg
->count_lessees
= count
;
630 mutex_unlock(&dev
->mode_config
.idr_mutex
);
631 drm_master_put(&lessor
);
636 /* Return the list of leased objects for the specified lessee */
637 int drm_mode_get_lease_ioctl(struct drm_device
*dev
,
638 void *data
, struct drm_file
*lessee_priv
)
640 struct drm_mode_get_lease
*arg
= data
;
641 __u32 __user
*object_ids
= (__u32 __user
*) (uintptr_t) (arg
->objects_ptr
);
642 __u32 count_objects
= arg
->count_objects
;
643 struct drm_master
*lessee
;
644 struct idr
*object_idr
;
653 /* Can't lease without MODESET */
654 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
657 lessee
= drm_file_get_master(lessee_priv
);
658 drm_dbg_lease(dev
, "get lease for %d\n", lessee
->lessee_id
);
660 mutex_lock(&dev
->mode_config
.idr_mutex
);
662 if (lessee
->lessor
== NULL
)
663 /* owner can use all objects */
664 object_idr
= &lessee
->dev
->mode_config
.object_idr
;
666 /* lessee can only use allowed object */
667 object_idr
= &lessee
->leases
;
670 idr_for_each_entry(object_idr
, entry
, object
) {
671 if (count_objects
> count
) {
672 drm_dbg_lease(dev
, "adding object %d\n", object
);
673 ret
= put_user(object
, object_ids
+ count
);
680 DRM_DEBUG("lease holds %d objects\n", count
);
682 arg
->count_objects
= count
;
684 mutex_unlock(&dev
->mode_config
.idr_mutex
);
685 drm_master_put(&lessee
);
691 * This removes all of the objects from the lease without
692 * actually getting rid of the lease itself; that way all
693 * references to it still work correctly
695 int drm_mode_revoke_lease_ioctl(struct drm_device
*dev
,
696 void *data
, struct drm_file
*lessor_priv
)
698 struct drm_mode_revoke_lease
*arg
= data
;
699 struct drm_master
*lessor
;
700 struct drm_master
*lessee
;
703 drm_dbg_lease(dev
, "revoke lease for %d\n", arg
->lessee_id
);
705 /* Can't lease without MODESET */
706 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
709 lessor
= drm_file_get_master(lessor_priv
);
710 mutex_lock(&dev
->mode_config
.idr_mutex
);
712 lessee
= _drm_find_lessee(lessor
, arg
->lessee_id
);
720 /* Lease is not held by lessor */
721 if (lessee
->lessor
!= lessor
) {
726 _drm_lease_revoke(lessee
);
729 mutex_unlock(&dev
->mode_config
.idr_mutex
);
730 drm_master_put(&lessor
);