xref: /openbmc/linux/drivers/gpu/drm/drm_lease.c (revision aa0dc6a7)
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 
18 #define drm_for_each_lessee(lessee, lessor) \
19 	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:
28  *
29  * drm_master at the top of the tree (i.e, with lessor NULL
30  */
31 struct drm_master *drm_lease_owner(struct drm_master *master)
32 {
33 	while (master->lessor != NULL)
34 		master = master->lessor;
35 	return master;
36 }
37 
38 /**
39  * _drm_find_lessee - find lessee by id (idr_mutex held)
40  * @master: drm_master of lessor
41  * @lessee_id: id
42  *
43  * RETURN:
44  *
45  * drm_master of the lessee if valid, NULL otherwise
46  */
47 
48 static struct drm_master*
49 _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:
62  *
63  *	true		'master' holds a lease on (or owns) the object
64  *	false		'master' does not hold a lease.
65  */
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 	return true;
72 }
73 
74 /**
75  * _drm_has_leased - check to see if an object has been leased (idr_mutex held)
76  * @master: the master to check the lease status of
77  * @id: the id to check
78  *
79  * Checks if any lessee of 'master' holds a lease on 'id'. Return
80  * value:
81  *
82  *	true		Some lessee holds a lease on the object.
83  *	false		No lessee has a lease on the object.
84  */
85 static bool _drm_has_leased(struct drm_master *master, int id)
86 {
87 	struct drm_master *lessee;
88 
89 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
90 	drm_for_each_lessee(lessee, master)
91 		if (_drm_lease_held_master(lessee, id))
92 			return true;
93 	return false;
94 }
95 
96 /**
97  * _drm_lease_held - check drm_mode_object lease status (idr_mutex held)
98  * @file_priv: the master drm_file
99  * @id: the object id
100  *
101  * Checks if the specified master holds a lease on the object. Return
102  * value:
103  *
104  *	true		'master' holds a lease on (or owns) the object
105  *	false		'master' does not hold a lease.
106  */
107 bool _drm_lease_held(struct drm_file *file_priv, int id)
108 {
109 	if (!file_priv || !file_priv->master)
110 		return true;
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:
122  *
123  *	true		'master' holds a lease on (or owns) the object
124  *	false		'master' does not hold a lease.
125  */
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 	master = file_priv->master;
135 	mutex_lock(&master->dev->mode_config.idr_mutex);
136 	ret = _drm_lease_held_master(master, id);
137 	mutex_unlock(&master->dev->mode_config.idr_mutex);
138 	return ret;
139 }
140 
141 /**
142  * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held)
143  * @file_priv: requestor file
144  * @crtcs_in: bitmask of crtcs to check
145  *
146  * Reconstructs a crtc mask based on the crtcs which are visible
147  * through the specified file.
148  */
149 uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
150 {
151 	struct drm_master *master;
152 	struct drm_device *dev;
153 	struct drm_crtc *crtc;
154 	int count_in, count_out;
155 	uint32_t crtcs_out = 0;
156 
157 	if (!file_priv || !file_priv->master || !file_priv->master->lessor)
158 		return crtcs_in;
159 
160 	master = file_priv->master;
161 	dev = master->dev;
162 
163 	count_in = count_out = 0;
164 	mutex_lock(&master->dev->mode_config.idr_mutex);
165 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
166 		if (_drm_lease_held_master(master, crtc->base.id)) {
167 			uint32_t mask_in = 1ul << count_in;
168 
169 			if ((crtcs_in & mask_in) != 0) {
170 				uint32_t mask_out = 1ul << count_out;
171 
172 				crtcs_out |= mask_out;
173 			}
174 			count_out++;
175 		}
176 		count_in++;
177 	}
178 	mutex_unlock(&master->dev->mode_config.idr_mutex);
179 	return crtcs_out;
180 }
181 
182 /*
183  * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held)
184  * @lessor: lease holder (or owner) of objects
185  * @leases: objects to lease to the new drm_master
186  *
187  * Uses drm_master_create to allocate a new drm_master, then checks to
188  * make sure all of the desired objects can be leased, atomically
189  * leasing them to the new drmmaster.
190  *
191  * 	ERR_PTR(-EACCES)	some other master holds the title to any object
192  * 	ERR_PTR(-ENOENT)	some object is not a valid DRM object for this device
193  * 	ERR_PTR(-EBUSY)		some other lessee holds title to this object
194  *	ERR_PTR(-EEXIST)	same object specified more than once in the provided list
195  *	ERR_PTR(-ENOMEM)	allocation failed
196  */
197 static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
198 {
199 	struct drm_device *dev = lessor->dev;
200 	int error;
201 	struct drm_master *lessee;
202 	int object;
203 	int id;
204 	void *entry;
205 
206 	DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
207 
208 	lessee = drm_master_create(lessor->dev);
209 	if (!lessee) {
210 		DRM_DEBUG_LEASE("drm_master_create failed\n");
211 		return ERR_PTR(-ENOMEM);
212 	}
213 
214 	mutex_lock(&dev->mode_config.idr_mutex);
215 
216 	idr_for_each_entry(leases, entry, object) {
217 		error = 0;
218 		if (!idr_find(&dev->mode_config.object_idr, object))
219 			error = -ENOENT;
220 		else if (_drm_has_leased(lessor, object))
221 			error = -EBUSY;
222 
223 		if (error != 0) {
224 			DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
225 			goto out_lessee;
226 		}
227 	}
228 
229 	/* Insert the new lessee into the tree */
230 	id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
231 	if (id < 0) {
232 		error = id;
233 		goto out_lessee;
234 	}
235 
236 	lessee->lessee_id = id;
237 	lessee->lessor = drm_master_get(lessor);
238 	list_add_tail(&lessee->lessee_list, &lessor->lessees);
239 
240 	/* Move the leases over */
241 	lessee->leases = *leases;
242 	DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
243 
244 	mutex_unlock(&dev->mode_config.idr_mutex);
245 	return lessee;
246 
247 out_lessee:
248 	mutex_unlock(&dev->mode_config.idr_mutex);
249 
250 	drm_master_put(&lessee);
251 
252 	return ERR_PTR(error);
253 }
254 
255 /**
256  * drm_lease_destroy - a master is going away (idr_mutex not held)
257  * @master: the drm_master being destroyed
258  *
259  * All lessees will have been destroyed as they
260  * hold a reference on their lessor. Notify any
261  * lessor for this master so that it can check
262  * the list of lessees.
263  */
264 void drm_lease_destroy(struct drm_master *master)
265 {
266 	struct drm_device *dev = master->dev;
267 
268 	mutex_lock(&dev->mode_config.idr_mutex);
269 
270 	DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
271 
272 	/* This master is referenced by all lessees, hence it cannot be destroyed
273 	 * until all of them have been
274 	 */
275 	WARN_ON(!list_empty(&master->lessees));
276 
277 	/* Remove this master from the lessee idr in the owner */
278 	if (master->lessee_id != 0) {
279 		DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
280 		idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
281 	}
282 
283 	/* Remove this master from any lessee list it may be on */
284 	list_del(&master->lessee_list);
285 
286 	mutex_unlock(&dev->mode_config.idr_mutex);
287 
288 	if (master->lessor) {
289 		/* Tell the master to check the lessee list */
290 		drm_sysfs_lease_event(dev);
291 		drm_master_put(&master->lessor);
292 	}
293 
294 	DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
295 }
296 
297 /**
298  * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held)
299  * @top: the master losing its lease
300  */
301 static void _drm_lease_revoke(struct drm_master *top)
302 {
303 	int object;
304 	void *entry;
305 	struct drm_master *master = top;
306 
307 	lockdep_assert_held(&top->dev->mode_config.idr_mutex);
308 
309 	/*
310 	 * Walk the tree starting at 'top' emptying all leases. Because
311 	 * the tree is fully connected, we can do this without recursing
312 	 */
313 	for (;;) {
314 		DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
315 
316 		/* Evacuate the lease */
317 		idr_for_each_entry(&master->leases, entry, object)
318 			idr_remove(&master->leases, object);
319 
320 		/* Depth-first list walk */
321 
322 		/* Down */
323 		if (!list_empty(&master->lessees)) {
324 			master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
325 		} else {
326 			/* Up */
327 			while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
328 				master = master->lessor;
329 
330 			if (master == top)
331 				break;
332 
333 			/* Over */
334 			master = list_next_entry(master, lessee_list);
335 		}
336 	}
337 }
338 
339 /**
340  * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held)
341  * @top: the master losing its lease
342  */
343 void drm_lease_revoke(struct drm_master *top)
344 {
345 	mutex_lock(&top->dev->mode_config.idr_mutex);
346 	_drm_lease_revoke(top);
347 	mutex_unlock(&top->dev->mode_config.idr_mutex);
348 }
349 
350 static int validate_lease(struct drm_device *dev,
351 			  int object_count,
352 			  struct drm_mode_object **objects,
353 			  bool universal_planes)
354 {
355 	int o;
356 	int has_crtc = -1;
357 	int has_connector = -1;
358 	int has_plane = -1;
359 
360 	/* we want to confirm that there is at least one crtc, plane
361 	   connector object. */
362 
363 	for (o = 0; o < object_count; o++) {
364 		if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
365 			has_crtc = o;
366 		}
367 		if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
368 			has_connector = o;
369 
370 		if (universal_planes) {
371 			if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
372 				has_plane = o;
373 		}
374 	}
375 	if (has_crtc == -1 || has_connector == -1)
376 		return -EINVAL;
377 	if (universal_planes && has_plane == -1)
378 		return -EINVAL;
379 	return 0;
380 }
381 
382 static int fill_object_idr(struct drm_device *dev,
383 			   struct drm_file *lessor_priv,
384 			   struct idr *leases,
385 			   int object_count,
386 			   u32 *object_ids)
387 {
388 	struct drm_mode_object **objects;
389 	u32 o;
390 	int ret;
391 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
392 
393 	objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
394 			  GFP_KERNEL);
395 	if (!objects)
396 		return -ENOMEM;
397 
398 	/* step one - get references to all the mode objects
399 	   and check for validity. */
400 	for (o = 0; o < object_count; o++) {
401 		objects[o] = drm_mode_object_find(dev, lessor_priv,
402 						  object_ids[o],
403 						  DRM_MODE_OBJECT_ANY);
404 		if (!objects[o]) {
405 			ret = -ENOENT;
406 			goto out_free_objects;
407 		}
408 
409 		if (!drm_mode_object_lease_required(objects[o]->type)) {
410 			DRM_DEBUG_KMS("invalid object for lease\n");
411 			ret = -EINVAL;
412 			goto out_free_objects;
413 		}
414 	}
415 
416 	ret = validate_lease(dev, object_count, objects, universal_planes);
417 	if (ret) {
418 		DRM_DEBUG_LEASE("lease validation failed\n");
419 		goto out_free_objects;
420 	}
421 
422 	/* add their IDs to the lease request - taking into account
423 	   universal planes */
424 	for (o = 0; o < object_count; o++) {
425 		struct drm_mode_object *obj = objects[o];
426 		u32 object_id = objects[o]->id;
427 
428 		DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
429 
430 		/*
431 		 * We're using an IDR to hold the set of leased
432 		 * objects, but we don't need to point at the object's
433 		 * data structure from the lease as the main object_idr
434 		 * will be used to actually find that. Instead, all we
435 		 * really want is a 'leased/not-leased' result, for
436 		 * which any non-NULL pointer will work fine.
437 		 */
438 		ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
439 		if (ret < 0) {
440 			DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
441 					object_id, ret);
442 			goto out_free_objects;
443 		}
444 		if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
445 			struct drm_crtc *crtc = obj_to_crtc(obj);
446 
447 			ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
448 			if (ret < 0) {
449 				DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
450 						object_id, ret);
451 				goto out_free_objects;
452 			}
453 			if (crtc->cursor) {
454 				ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
455 				if (ret < 0) {
456 					DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
457 							object_id, ret);
458 					goto out_free_objects;
459 				}
460 			}
461 		}
462 	}
463 
464 	ret = 0;
465 out_free_objects:
466 	for (o = 0; o < object_count; o++) {
467 		if (objects[o])
468 			drm_mode_object_put(objects[o]);
469 	}
470 	kfree(objects);
471 	return ret;
472 }
473 
474 /**
475  * drm_mode_create_lease_ioctl - create a new lease
476  * @dev: the drm device
477  * @data: pointer to struct drm_mode_create_lease
478  * @lessor_priv: the file being manipulated
479  *
480  * The master associated with the specified file will have a lease
481  * created containing the objects specified in the ioctl structure.
482  * A file descriptor will be allocated for that and returned to the
483  * application.
484  */
485 int drm_mode_create_lease_ioctl(struct drm_device *dev,
486 				void *data, struct drm_file *lessor_priv)
487 {
488 	struct drm_mode_create_lease *cl = data;
489 	size_t object_count;
490 	int ret = 0;
491 	struct idr leases;
492 	struct drm_master *lessor = lessor_priv->master;
493 	struct drm_master *lessee = NULL;
494 	struct file *lessee_file = NULL;
495 	struct file *lessor_file = lessor_priv->filp;
496 	struct drm_file *lessee_priv;
497 	int fd = -1;
498 	uint32_t *object_ids;
499 
500 	/* Can't lease without MODESET */
501 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
502 		return -EOPNOTSUPP;
503 
504 	/* Do not allow sub-leases */
505 	if (lessor->lessor) {
506 		DRM_DEBUG_LEASE("recursive leasing not allowed\n");
507 		return -EINVAL;
508 	}
509 
510 	/* need some objects */
511 	if (cl->object_count == 0) {
512 		DRM_DEBUG_LEASE("no objects in lease\n");
513 		return -EINVAL;
514 	}
515 
516 	if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
517 		DRM_DEBUG_LEASE("invalid flags\n");
518 		return -EINVAL;
519 	}
520 
521 	object_count = cl->object_count;
522 
523 	object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
524 			array_size(object_count, sizeof(__u32)));
525 	if (IS_ERR(object_ids))
526 		return PTR_ERR(object_ids);
527 
528 	idr_init(&leases);
529 
530 	/* fill and validate the object idr */
531 	ret = fill_object_idr(dev, lessor_priv, &leases,
532 			      object_count, object_ids);
533 	kfree(object_ids);
534 	if (ret) {
535 		DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
536 		idr_destroy(&leases);
537 		return ret;
538 	}
539 
540 	/* Allocate a file descriptor for the lease */
541 	fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
542 	if (fd < 0) {
543 		idr_destroy(&leases);
544 		return fd;
545 	}
546 
547 	DRM_DEBUG_LEASE("Creating lease\n");
548 	/* lessee will take the ownership of leases */
549 	lessee = drm_lease_create(lessor, &leases);
550 
551 	if (IS_ERR(lessee)) {
552 		ret = PTR_ERR(lessee);
553 		idr_destroy(&leases);
554 		goto out_leases;
555 	}
556 
557 	/* Clone the lessor file to create a new file for us */
558 	DRM_DEBUG_LEASE("Allocating lease file\n");
559 	lessee_file = file_clone_open(lessor_file);
560 	if (IS_ERR(lessee_file)) {
561 		ret = PTR_ERR(lessee_file);
562 		goto out_lessee;
563 	}
564 
565 	lessee_priv = lessee_file->private_data;
566 	/* Change the file to a master one */
567 	drm_master_put(&lessee_priv->master);
568 	lessee_priv->master = lessee;
569 	lessee_priv->is_master = 1;
570 	lessee_priv->authenticated = 1;
571 
572 	/* Pass fd back to userspace */
573 	DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
574 	cl->fd = fd;
575 	cl->lessee_id = lessee->lessee_id;
576 
577 	/* Hook up the fd */
578 	fd_install(fd, lessee_file);
579 
580 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
581 	return 0;
582 
583 out_lessee:
584 	drm_master_put(&lessee);
585 
586 out_leases:
587 	put_unused_fd(fd);
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