1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright © 2017 Keith Packard <keithp@keithp.com>
4 */
5 #include <linux/file.h>
6 #include <linux/uaccess.h>
7
8 #include <drm/drm_auth.h>
9 #include <drm/drm_crtc_helper.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>
14
15 #include "drm_crtc_internal.h"
16 #include "drm_internal.h"
17 #include "drm_legacy.h"
18
19 #define drm_for_each_lessee(lessee, lessor) list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
20
21 static uint64_t drm_lease_idr_object;
22
23 /**
24 * drm_lease_owner - return ancestor owner drm_master
25 * @master: drm_master somewhere within tree of lessees and lessors
26 *
27 * RETURN:struct drm_master *
28 *
29 * drm_master at the top of the tree (i.e, with lessor NULL
30 */
drm_lease_owner(struct drm_master * master)31 struct drm_master *drm_lease_owner(struct drm_master *master)
32 {
33 while (master->lessor != NULL) {
34 master = master->lessor;
35 }
36 return master;
37 }
38
39 /**
40 * _drm_find_lessee - find lessee by id (idr_mutex held)
41 * @master: drm_master of lessor
42 * @lessee_id: id
43 *
44 * RETURN:static struct drm_master *
45 *
46 * drm_master of the lessee if valid, NULL otherwise
47 */
48
_drm_find_lessee(struct drm_master * master,int lessee_id)49 static struct drm_master *_drm_find_lessee(struct drm_master *master, int lessee_id)
50 {
51 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
52 return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
53 }
54
55 /**
56 * _drm_lease_held_master - check to see if an object is leased (or owned) by master (idr_mutex held)
57 * @master: the master to check the lease status of
58 * @id: the id to check
59 *
60 * Checks if the specified master holds a lease on the object. Return
61 * value:see below
62 *
63 * true 'master' holds a lease on (or owns) the object
64 * false 'master' does not hold a lease.
65 */
_drm_lease_held_master(struct drm_master * master,int id)66 static int _drm_lease_held_master(struct drm_master *master, int id)
67 {
68 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
69 if (master->lessor) {
70 return idr_find(&master->leases, id) != NULL;
71 }
72 return true;
73 }
74
75 /**
76 * _drm_has_leased - check to see if an object has been leased (idr_mutex held)
77 * @master: the master to check the lease status of
78 * @id: the id to check
79 *
80 * Checks if any lessee of 'master' holds a lease on 'id'. Return
81 * value:see below
82 *
83 * true Some lessee holds a lease on the object.
84 * false No lessee has a lease on the object.
85 */
_drm_has_leased(struct drm_master * master,int id)86 static bool _drm_has_leased(struct drm_master *master, int id)
87 {
88 struct drm_master *lessee;
89
90 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
91 drm_for_each_lessee(lessee, master) if (_drm_lease_held_master(lessee, id)) return true;
92 return false;
93 }
94
95 /**
96 * _drm_lease_held - check drm_mode_object lease status (idr_mutex held)
97 * @file_priv: the master drm_file
98 * @id: the object id
99 *
100 * Checks if the specified master holds a lease on the object. Return
101 * value:see below
102 *
103 * true 'master' holds a lease on (or owns) the object
104 * false 'master' does not hold a lease.
105 */
_drm_lease_held(struct drm_file * file_priv,int id)106 bool _drm_lease_held(struct drm_file *file_priv, int id)
107 {
108 if (!file_priv || !file_priv->master) {
109 return true;
110 }
111
112 return _drm_lease_held_master(file_priv->master, id);
113 }
114
115 /**
116 * drm_lease_held - check drm_mode_object lease status (idr_mutex not held)
117 * @file_priv: the master drm_file
118 * @id: the object id
119 *
120 * Checks if the specified master holds a lease on the object. Return
121 * value:see below
122 *
123 * true 'master' holds a lease on (or owns) the object
124 * false 'master' does not hold a lease.
125 */
drm_lease_held(struct drm_file * file_priv,int id)126 bool drm_lease_held(struct drm_file *file_priv, int id)
127 {
128 struct drm_master *master;
129 bool ret;
130
131 if (!file_priv || !file_priv->master || !file_priv->master->lessor) {
132 return true;
133 }
134
135 master = file_priv->master;
136 mutex_lock(&master->dev->mode_config.idr_mutex);
137 ret = _drm_lease_held_master(master, id);
138 mutex_unlock(&master->dev->mode_config.idr_mutex);
139 return ret;
140 }
141
142 /**
143 * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held)
144 * @file_priv: requestor file
145 * @crtcs_in: bitmask of crtcs to check
146 *
147 * Reconstructs a crtc mask based on the crtcs which are visible
148 * through the specified file.
149 */
drm_lease_filter_crtcs(struct drm_file * file_priv,uint32_t crtcs_in)150 uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
151 {
152 struct drm_master *master;
153 struct drm_device *dev;
154 struct drm_crtc *crtc;
155 int count_in, count_out;
156 uint32_t crtcs_out = 0;
157
158 if (!file_priv || !file_priv->master || !file_priv->master->lessor) {
159 return crtcs_in;
160 }
161
162 master = file_priv->master;
163 dev = master->dev;
164
165 count_in = count_out = 0;
166 mutex_lock(&master->dev->mode_config.idr_mutex);
167 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
168 {
169 if (_drm_lease_held_master(master, crtc->base.id)) {
170 uint32_t mask_in = 1ul << count_in;
171
172 if ((crtcs_in & mask_in) != 0) {
173 uint32_t mask_out = 1ul << count_out;
174
175 crtcs_out |= mask_out;
176 }
177 count_out++;
178 }
179 count_in++;
180 }
181 mutex_unlock(&master->dev->mode_config.idr_mutex);
182 return crtcs_out;
183 }
184
185 /*
186 * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held)
187 * @lessor: lease holder (or owner) of objects
188 * @leases: objects to lease to the new drm_master
189 *
190 * Uses drm_master_create to allocate a new drm_master, then checks to
191 * make sure all of the desired objects can be leased, atomically
192 * leasing them to the new drmmaster.
193 *
194 * ERR_PTR(-EACCES) some other master holds the title to any object
195 * ERR_PTR(-ENOENT) some object is not a valid DRM object for this device
196 * ERR_PTR(-EBUSY) some other lessee holds title to this object
197 * ERR_PTR(-EEXIST) same object specified more than once in the provided list
198 * ERR_PTR(-ENOMEM) allocation failed
199 */
drm_lease_create(struct drm_master * lessor,struct idr * leases)200 static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
201 {
202 struct drm_device *dev = lessor->dev;
203 int error;
204 struct drm_master *lessee;
205 int object;
206 int id;
207 void *entry;
208
209 DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
210
211 lessee = drm_master_create(lessor->dev);
212 if (!lessee) {
213 DRM_DEBUG_LEASE("drm_master_create failed\n");
214 return ERR_PTR(-ENOMEM);
215 }
216
217 mutex_lock(&dev->mode_config.idr_mutex);
218
219 idr_for_each_entry(leases, entry, object)
220 {
221 error = 0;
222 if (!idr_find(&dev->mode_config.object_idr, object)) {
223 error = -ENOENT;
224 } else if (_drm_has_leased(lessor, object)) {
225 error = -EBUSY;
226 }
227
228 if (error != 0) {
229 DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
230 goto out_lessee;
231 }
232 }
233
234 /* Insert the new lessee into the tree */
235 id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
236 if (id < 0) {
237 error = id;
238 goto out_lessee;
239 }
240
241 lessee->lessee_id = id;
242 lessee->lessor = drm_master_get(lessor);
243 list_add_tail(&lessee->lessee_list, &lessor->lessees);
244
245 /* Move the leases over */
246 lessee->leases = *leases;
247 DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
248
249 mutex_unlock(&dev->mode_config.idr_mutex);
250 return lessee;
251
252 out_lessee:
253 mutex_unlock(&dev->mode_config.idr_mutex);
254
255 drm_master_put(&lessee);
256
257 return ERR_PTR(error);
258 }
259
260 /**
261 * drm_lease_destroy - a master is going away (idr_mutex not held)
262 * @master: the drm_master being destroyed
263 *
264 * All lessees will have been destroyed as they
265 * hold a reference on their lessor. Notify any
266 * lessor for this master so that it can check
267 * the list of lessees.
268 */
drm_lease_destroy(struct drm_master * master)269 void drm_lease_destroy(struct drm_master *master)
270 {
271 struct drm_device *dev = master->dev;
272
273 mutex_lock(&dev->mode_config.idr_mutex);
274
275 DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
276
277 /* This master is referenced by all lessees, hence it cannot be destroyed
278 * until all of them have been
279 */
280 WARN_ON(!list_empty(&master->lessees));
281
282 /* Remove this master from the lessee idr in the owner */
283 if (master->lessee_id != 0) {
284 DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
285 idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
286 }
287
288 /* Remove this master from any lessee list it may be on */
289 list_del(&master->lessee_list);
290
291 mutex_unlock(&dev->mode_config.idr_mutex);
292
293 if (master->lessor) {
294 /* Tell the master to check the lessee list */
295 drm_sysfs_lease_event(dev);
296 drm_master_put(&master->lessor);
297 }
298
299 DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
300 }
301
302 /**
303 * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held)
304 * @top: the master losing its lease
305 */
_drm_lease_revoke(struct drm_master * top)306 static void _drm_lease_revoke(struct drm_master *top)
307 {
308 int object;
309 void *entry;
310 struct drm_master *master = top;
311
312 lockdep_assert_held(&top->dev->mode_config.idr_mutex);
313
314 /*
315 * Walk the tree starting at 'top' emptying all leases. Because
316 * the tree is fully connected, we can do this without recursing
317 */
318 for (;;) {
319 DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
320
321 /* Evacuate the lease */
322 idr_for_each_entry(&master->leases, entry, object) idr_remove(&master->leases, object);
323
324 /* Depth-first list walk */
325
326 /* Down */
327 if (!list_empty(&master->lessees)) {
328 master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
329 } else {
330 /* Up */
331 while (master != top &&
332 master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list)) {
333 master = master->lessor;
334 }
335
336 if (master == top) {
337 break;
338 }
339
340 /* Over */
341 master = list_next_entry(master, lessee_list);
342 }
343 }
344 }
345
346 /**
347 * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held)
348 * @top: the master losing its lease
349 */
drm_lease_revoke(struct drm_master * top)350 void drm_lease_revoke(struct drm_master *top)
351 {
352 mutex_lock(&top->dev->mode_config.idr_mutex);
353 _drm_lease_revoke(top);
354 mutex_unlock(&top->dev->mode_config.idr_mutex);
355 }
356
validate_lease(struct drm_device * dev,int object_count,struct drm_mode_object ** objects,bool universal_planes)357 static int validate_lease(struct drm_device *dev, int object_count, struct drm_mode_object **objects,
358 bool universal_planes)
359 {
360 int o;
361 int has_crtc = -1;
362 int has_connector = -1;
363 int has_plane = -1;
364
365 /* we want to confirm that there is at least one crtc, plane
366 connector object. */
367
368 for (o = 0; o < object_count; o++) {
369 if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
370 has_crtc = o;
371 }
372 if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1) {
373 has_connector = o;
374 }
375
376 if (universal_planes) {
377 if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1) {
378 has_plane = o;
379 }
380 }
381 }
382 if (has_crtc == -1 || has_connector == -1) {
383 return -EINVAL;
384 }
385 if (universal_planes && has_plane == -1) {
386 return -EINVAL;
387 }
388 return 0;
389 }
390
fill_object_idr(struct drm_device * dev,struct drm_file * lessor_priv,struct idr * leases,int object_count,u32 * object_ids)391 static int fill_object_idr(struct drm_device *dev, struct drm_file *lessor_priv, struct idr *leases, int object_count,
392 u32 *object_ids)
393 {
394 struct drm_mode_object **objects;
395 u32 o;
396 int ret;
397 bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
398
399 objects = kcalloc(object_count, sizeof(struct drm_mode_object *), GFP_KERNEL);
400 if (!objects) {
401 return -ENOMEM;
402 }
403
404 /* step one - get references to all the mode objects
405 and check for validity. */
406 for (o = 0; o < object_count; o++) {
407 objects[o] = drm_mode_object_find(dev, lessor_priv, object_ids[o], DRM_MODE_OBJECT_ANY);
408 if (!objects[o]) {
409 ret = -ENOENT;
410 goto out_free_objects;
411 }
412
413 if (!drm_mode_object_lease_required(objects[o]->type)) {
414 DRM_DEBUG_KMS("invalid object for lease\n");
415 ret = -EINVAL;
416 goto out_free_objects;
417 }
418 }
419
420 ret = validate_lease(dev, object_count, objects, universal_planes);
421 if (ret) {
422 DRM_DEBUG_LEASE("lease validation failed\n");
423 goto out_free_objects;
424 }
425
426 /* add their IDs to the lease request - taking into account
427 universal planes */
428 for (o = 0; o < object_count; o++) {
429 struct drm_mode_object *obj = objects[o];
430 u32 object_id = objects[o]->id;
431
432 DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
433
434 /*
435 * We're using an IDR to hold the set of leased
436 * objects, but we don't need to point at the object's
437 * data structure from the lease as the main object_idr
438 * will be used to actually find that. Instead, all we
439 * really want is a 'leased/not-leased' result, for
440 * which any non-NULL pointer will work fine.
441 */
442 ret = idr_alloc(leases, &drm_lease_idr_object, object_id, object_id + 1, GFP_KERNEL);
443 if (ret < 0) {
444 DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n", object_id, ret);
445 goto out_free_objects;
446 }
447 if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
448 struct drm_crtc *crtc = obj_to_crtc(obj);
449
450 ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1,
451 GFP_KERNEL);
452 if (ret < 0) {
453 DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n", object_id, ret);
454 goto out_free_objects;
455 }
456 if (crtc->cursor) {
457 ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1,
458 GFP_KERNEL);
459 if (ret < 0) {
460 DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n", object_id, ret);
461 goto out_free_objects;
462 }
463 }
464 }
465 }
466
467 ret = 0;
468 out_free_objects:
469 for (o = 0; o < object_count; o++) {
470 if (objects[o]) {
471 drm_mode_object_put(objects[o]);
472 }
473 }
474 kfree(objects);
475 return ret;
476 }
477
478 /**
479 * drm_mode_create_lease_ioctl - create a new lease
480 * @dev: the drm device
481 * @data: pointer to struct drm_mode_create_lease
482 * @lessor_priv: the file being manipulated
483 *
484 * The master associated with the specified file will have a lease
485 * created containing the objects specified in the ioctl structure.
486 * A file descriptor will be allocated for that and returned to the
487 * application.
488 */
drm_mode_create_lease_ioctl(struct drm_device * dev,void * data,struct drm_file * lessor_priv)489 int drm_mode_create_lease_ioctl(struct drm_device *dev, void *data, struct drm_file *lessor_priv)
490 {
491 struct drm_mode_create_lease *cl = data;
492 size_t object_count;
493 int ret = 0;
494 struct idr leases;
495 struct drm_master *lessor = lessor_priv->master;
496 struct drm_master *lessee = NULL;
497 struct file *lessee_file = NULL;
498 struct file *lessor_file = lessor_priv->filp;
499 struct drm_file *lessee_priv;
500 int fd = -1;
501 uint32_t *object_ids;
502
503 /* Can't lease without MODESET */
504 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
505 return -EOPNOTSUPP;
506 }
507
508 /* Do not allow sub-leases */
509 if (lessor->lessor) {
510 DRM_DEBUG_LEASE("recursive leasing not allowed\n");
511 return -EINVAL;
512 }
513
514 /* need some objects */
515 if (cl->object_count == 0) {
516 DRM_DEBUG_LEASE("no objects in lease\n");
517 return -EINVAL;
518 }
519
520 if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
521 DRM_DEBUG_LEASE("invalid flags\n");
522 return -EINVAL;
523 }
524
525 object_count = cl->object_count;
526
527 object_ids = memdup_user(u64_to_user_ptr(cl->object_ids), array_size(object_count, sizeof(__u32)));
528 if (IS_ERR(object_ids)) {
529 return PTR_ERR(object_ids);
530 }
531
532 idr_init(&leases);
533
534 /* fill and validate the object idr */
535 ret = fill_object_idr(dev, lessor_priv, &leases, object_count, object_ids);
536 kfree(object_ids);
537 if (ret) {
538 DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
539 idr_destroy(&leases);
540 return ret;
541 }
542
543 /* Allocate a file descriptor for the lease */
544 fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
545 if (fd < 0) {
546 idr_destroy(&leases);
547 return fd;
548 }
549
550 DRM_DEBUG_LEASE("Creating lease\n");
551 /* lessee will take the ownership of leases */
552 lessee = drm_lease_create(lessor, &leases);
553 if (IS_ERR(lessee)) {
554 ret = PTR_ERR(lessee);
555 idr_destroy(&leases);
556 goto out_leases;
557 }
558
559 /* Clone the lessor file to create a new file for us */
560 DRM_DEBUG_LEASE("Allocating lease file\n");
561 lessee_file = file_clone_open(lessor_file);
562 if (IS_ERR(lessee_file)) {
563 ret = PTR_ERR(lessee_file);
564 goto out_lessee;
565 }
566
567 lessee_priv = lessee_file->private_data;
568 /* Change the file to a master one */
569 drm_master_put(&lessee_priv->master);
570 lessee_priv->master = lessee;
571 lessee_priv->is_master = 1;
572 lessee_priv->authenticated = 1;
573
574 /* Pass fd back to userspace */
575 DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
576 cl->fd = fd;
577 cl->lessee_id = lessee->lessee_id;
578
579 /* Hook up the fd */
580 fd_install(fd, lessee_file);
581
582 DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
583 return 0;
584
585 out_lessee:
586 drm_master_put(&lessee);
587
588 out_leases:
589 put_unused_fd(fd);
590
591 DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
592 return ret;
593 }
594
595 /**
596 * drm_mode_list_lessees_ioctl - list lessee ids
597 * @dev: the drm device
598 * @data: pointer to struct drm_mode_list_lessees
599 * @lessor_priv: the file being manipulated
600 *
601 * Starting from the master associated with the specified file,
602 * the master with the provided lessee_id is found, and then
603 * an array of lessee ids associated with leases from that master
604 * are returned.
605 */
606
drm_mode_list_lessees_ioctl(struct drm_device * dev,void * data,struct drm_file * lessor_priv)607 int drm_mode_list_lessees_ioctl(struct drm_device *dev, void *data, struct drm_file *lessor_priv)
608 {
609 struct drm_mode_list_lessees *arg = data;
610 __u32 __user *lessee_ids = (__u32 __user *)(uintptr_t)(arg->lessees_ptr);
611 __u32 count_lessees = arg->count_lessees;
612 struct drm_master *lessor = lessor_priv->master, *lessee;
613 int count;
614 int ret = 0;
615
616 if (arg->pad) {
617 return -EINVAL;
618 }
619
620 /* Can't lease without MODESET */
621 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
622 return -EOPNOTSUPP;
623 }
624
625 DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
626
627 mutex_lock(&dev->mode_config.idr_mutex);
628
629 count = 0;
630 drm_for_each_lessee(lessee, lessor)
631 {
632 /* Only list un-revoked leases */
633 if (!idr_is_empty(&lessee->leases)) {
634 if (count_lessees > count) {
635 DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
636 ret = put_user(lessee->lessee_id, lessee_ids + count);
637 if (ret) {
638 break;
639 }
640 }
641 count++;
642 }
643 }
644
645 DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
646 if (ret == 0) {
647 arg->count_lessees = count;
648 }
649
650 mutex_unlock(&dev->mode_config.idr_mutex);
651
652 return ret;
653 }
654
655 /**
656 * drm_mode_get_lease_ioctl - list leased objects
657 * @dev: the drm device
658 * @data: pointer to struct drm_mode_get_lease
659 * @lessee_priv: the file being manipulated
660 *
661 * Return the list of leased objects for the specified lessee
662 */
663
drm_mode_get_lease_ioctl(struct drm_device * dev,void * data,struct drm_file * lessee_priv)664 int drm_mode_get_lease_ioctl(struct drm_device *dev, void *data, struct drm_file *lessee_priv)
665 {
666 struct drm_mode_get_lease *arg = data;
667 __u32 __user *object_ids = (__u32 __user *)(uintptr_t)(arg->objects_ptr);
668 __u32 count_objects = arg->count_objects;
669 struct drm_master *lessee = lessee_priv->master;
670 struct idr *object_idr;
671 int count;
672 void *entry;
673 int object;
674 int ret = 0;
675
676 if (arg->pad) {
677 return -EINVAL;
678 }
679
680 /* Can't lease without MODESET */
681 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
682 return -EOPNOTSUPP;
683 }
684
685 DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
686
687 mutex_lock(&dev->mode_config.idr_mutex);
688
689 if (lessee->lessor == NULL) {
690 /* owner can use all objects */
691 object_idr = &lessee->dev->mode_config.object_idr;
692 } else {
693 /* lessee can only use allowed object */
694 object_idr = &lessee->leases;
695 }
696
697 count = 0;
698 idr_for_each_entry(object_idr, entry, object)
699 {
700 if (count_objects > count) {
701 DRM_DEBUG_LEASE("adding object %d\n", object);
702 ret = put_user(object, object_ids + count);
703 if (ret) {
704 break;
705 }
706 }
707 count++;
708 }
709
710 DRM_DEBUG("lease holds %d objects\n", count);
711 if (ret == 0) {
712 arg->count_objects = count;
713 }
714
715 mutex_unlock(&dev->mode_config.idr_mutex);
716
717 return ret;
718 }
719
720 /**
721 * drm_mode_revoke_lease_ioctl - revoke lease
722 * @dev: the drm device
723 * @data: pointer to struct drm_mode_revoke_lease
724 * @lessor_priv: the file being manipulated
725 *
726 * This removes all of the objects from the lease without
727 * actually getting rid of the lease itself; that way all
728 * references to it still work correctly
729 */
drm_mode_revoke_lease_ioctl(struct drm_device * dev,void * data,struct drm_file * lessor_priv)730 int drm_mode_revoke_lease_ioctl(struct drm_device *dev, void *data, struct drm_file *lessor_priv)
731 {
732 struct drm_mode_revoke_lease *arg = data;
733 struct drm_master *lessor = lessor_priv->master;
734 struct drm_master *lessee;
735 int ret = 0;
736
737 DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
738
739 /* Can't lease without MODESET */
740 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
741 return -EOPNOTSUPP;
742 }
743
744 mutex_lock(&dev->mode_config.idr_mutex);
745
746 lessee = _drm_find_lessee(lessor, arg->lessee_id);
747 /* No such lessee */
748 if (!lessee) {
749 ret = -ENOENT;
750 goto fail;
751 }
752
753 /* Lease is not held by lessor */
754 if (lessee->lessor != lessor) {
755 ret = -EACCES;
756 goto fail;
757 }
758
759 _drm_lease_revoke(lessee);
760
761 fail:
762 mutex_unlock(&dev->mode_config.idr_mutex);
763
764 return ret;
765 }
766