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