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