xref: /linux/drivers/gpu/drm/drm_lease.c (revision 4bb2d367a5a2807185a04949ae922d247f650576)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22ed077e4SKeith Packard /*
32ed077e4SKeith Packard  * Copyright © 2017 Keith Packard <keithp@keithp.com>
42ed077e4SKeith Packard  */
50500c04eSSam Ravnborg #include <linux/file.h>
60500c04eSSam Ravnborg #include <linux/uaccess.h>
72ed077e4SKeith Packard 
82ed077e4SKeith Packard #include <drm/drm_auth.h>
92ed077e4SKeith Packard #include <drm/drm_crtc_helper.h>
100500c04eSSam Ravnborg #include <drm/drm_drv.h>
110500c04eSSam Ravnborg #include <drm/drm_file.h>
120500c04eSSam Ravnborg #include <drm/drm_lease.h>
130500c04eSSam Ravnborg #include <drm/drm_print.h>
140500c04eSSam Ravnborg 
150500c04eSSam Ravnborg #include "drm_crtc_internal.h"
160500c04eSSam Ravnborg #include "drm_internal.h"
172ed077e4SKeith Packard 
18d793b8f7SDesmond Cheong Zhi Xi /**
19d793b8f7SDesmond Cheong Zhi Xi  * DOC: drm leasing
20d793b8f7SDesmond Cheong Zhi Xi  *
21d793b8f7SDesmond Cheong Zhi Xi  * DRM leases provide information about whether a DRM master may control a DRM
22d793b8f7SDesmond Cheong Zhi Xi  * mode setting object. This enables the creation of multiple DRM masters that
23d793b8f7SDesmond Cheong Zhi Xi  * manage subsets of display resources.
24d793b8f7SDesmond Cheong Zhi Xi  *
25d793b8f7SDesmond Cheong Zhi Xi  * The original DRM master of a device 'owns' the available drm resources. It
26d793b8f7SDesmond Cheong Zhi Xi  * may create additional DRM masters and 'lease' resources which it controls
27d793b8f7SDesmond Cheong Zhi Xi  * to the new DRM master. This gives the new DRM master control over the
28d793b8f7SDesmond Cheong Zhi Xi  * leased resources until the owner revokes the lease, or the new DRM master
29d793b8f7SDesmond Cheong Zhi Xi  * is closed. Some helpful terminology:
30d793b8f7SDesmond Cheong Zhi Xi  *
31d793b8f7SDesmond Cheong Zhi Xi  * - An 'owner' is a &struct drm_master that is not leasing objects from
32d793b8f7SDesmond Cheong Zhi Xi  *   another &struct drm_master, and hence 'owns' the objects. The owner can be
33d793b8f7SDesmond Cheong Zhi Xi  *   identified as the &struct drm_master for which &drm_master.lessor is NULL.
34d793b8f7SDesmond Cheong Zhi Xi  *
35d793b8f7SDesmond Cheong Zhi Xi  * - A 'lessor' is a &struct drm_master which is leasing objects to one or more
36d793b8f7SDesmond Cheong Zhi Xi  *   other &struct drm_master. Currently, lessees are not allowed to
37d793b8f7SDesmond Cheong Zhi Xi  *   create sub-leases, hence the lessor is the same as the owner.
38d793b8f7SDesmond Cheong Zhi Xi  *
39d793b8f7SDesmond Cheong Zhi Xi  * - A 'lessee' is a &struct drm_master which is leasing objects from some
40d793b8f7SDesmond Cheong Zhi Xi  *   other &struct drm_master. Each lessee only leases resources from a single
41d793b8f7SDesmond Cheong Zhi Xi  *   lessor recorded in &drm_master.lessor, and holds the set of objects that
42d793b8f7SDesmond Cheong Zhi Xi  *   it is leasing in &drm_master.leases.
43d793b8f7SDesmond Cheong Zhi Xi  *
44d793b8f7SDesmond Cheong Zhi Xi  * - A 'lease' is a contract between the lessor and lessee that identifies
45d793b8f7SDesmond Cheong Zhi Xi  *   which resources may be controlled by the lessee. All of the resources
46d793b8f7SDesmond Cheong Zhi Xi  *   that are leased must be owned by or leased to the lessor, and lessors are
47d793b8f7SDesmond Cheong Zhi Xi  *   not permitted to lease the same object to multiple lessees.
48d793b8f7SDesmond Cheong Zhi Xi  *
49d793b8f7SDesmond Cheong Zhi Xi  * The set of objects any &struct drm_master 'controls' is limited to the set
50d793b8f7SDesmond Cheong Zhi Xi  * of objects it leases (for lessees) or all objects (for owners).
51d793b8f7SDesmond Cheong Zhi Xi  *
52d793b8f7SDesmond Cheong Zhi Xi  * Objects not controlled by a &struct drm_master cannot be modified through
53d793b8f7SDesmond Cheong Zhi Xi  * the various state manipulating ioctls, and any state reported back to user
54d793b8f7SDesmond Cheong Zhi Xi  * space will be edited to make them appear idle and/or unusable. For
55d793b8f7SDesmond Cheong Zhi Xi  * instance, connectors always report 'disconnected', while encoders
56d793b8f7SDesmond Cheong Zhi Xi  * report no possible crtcs or clones.
57d793b8f7SDesmond Cheong Zhi Xi  *
58d793b8f7SDesmond Cheong Zhi Xi  * Since each lessee may lease objects from a single lessor, display resource
59d793b8f7SDesmond Cheong Zhi Xi  * leases form a tree of &struct drm_master. As lessees are currently not
60d793b8f7SDesmond Cheong Zhi Xi  * allowed to create sub-leases, the tree depth is limited to 1. All of
61d793b8f7SDesmond Cheong Zhi Xi  * these get activated simultaneously when the top level device owner changes
62d793b8f7SDesmond Cheong Zhi Xi  * through the SETMASTER or DROPMASTER IOCTL, so &drm_device.master points to
63d793b8f7SDesmond Cheong Zhi Xi  * the owner at the top of the lease tree (i.e. the &struct drm_master for which
64d793b8f7SDesmond Cheong Zhi Xi  * &drm_master.lessor is NULL). The full list of lessees that are leasing
65d793b8f7SDesmond Cheong Zhi Xi  * objects from the owner can be searched via the owner's
66d793b8f7SDesmond Cheong Zhi Xi  * &drm_master.lessee_idr.
67d793b8f7SDesmond Cheong Zhi Xi  */
68d793b8f7SDesmond Cheong Zhi Xi 
692ed077e4SKeith Packard #define drm_for_each_lessee(lessee, lessor) \
702ed077e4SKeith Packard 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
712ed077e4SKeith Packard 
7262884cd3SKeith Packard static uint64_t drm_lease_idr_object;
7362884cd3SKeith Packard 
742ed077e4SKeith Packard struct drm_master *drm_lease_owner(struct drm_master *master)
752ed077e4SKeith Packard {
762ed077e4SKeith Packard 	while (master->lessor != NULL)
772ed077e4SKeith Packard 		master = master->lessor;
782ed077e4SKeith Packard 	return master;
792ed077e4SKeith Packard }
802ed077e4SKeith Packard 
812ed077e4SKeith Packard static struct drm_master*
822ed077e4SKeith Packard _drm_find_lessee(struct drm_master *master, int lessee_id)
832ed077e4SKeith Packard {
842ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
852ed077e4SKeith Packard 	return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
862ed077e4SKeith Packard }
872ed077e4SKeith Packard 
882ed077e4SKeith Packard static int _drm_lease_held_master(struct drm_master *master, int id)
892ed077e4SKeith Packard {
902ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
912ed077e4SKeith Packard 	if (master->lessor)
922ed077e4SKeith Packard 		return idr_find(&master->leases, id) != NULL;
932ed077e4SKeith Packard 	return true;
942ed077e4SKeith Packard }
952ed077e4SKeith Packard 
96676f11b5SDesmond Cheong Zhi Xi /* Checks if the given object has been leased to some lessee of drm_master */
972ed077e4SKeith Packard static bool _drm_has_leased(struct drm_master *master, int id)
982ed077e4SKeith Packard {
992ed077e4SKeith Packard 	struct drm_master *lessee;
1002ed077e4SKeith Packard 
1012ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
1022ed077e4SKeith Packard 	drm_for_each_lessee(lessee, master)
1032ed077e4SKeith Packard 		if (_drm_lease_held_master(lessee, id))
1042ed077e4SKeith Packard 			return true;
1052ed077e4SKeith Packard 	return false;
1062ed077e4SKeith Packard }
1072ed077e4SKeith Packard 
108676f11b5SDesmond Cheong Zhi Xi /* Called with idr_mutex held */
1092ed077e4SKeith Packard bool _drm_lease_held(struct drm_file *file_priv, int id)
1102ed077e4SKeith Packard {
11156f0729aSDesmond Cheong Zhi Xi 	bool ret;
11256f0729aSDesmond Cheong Zhi Xi 	struct drm_master *master;
11356f0729aSDesmond Cheong Zhi Xi 
11456f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1152ed077e4SKeith Packard 		return true;
1162ed077e4SKeith Packard 
11756f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
11856f0729aSDesmond Cheong Zhi Xi 	if (!master)
11956f0729aSDesmond Cheong Zhi Xi 		return true;
12056f0729aSDesmond Cheong Zhi Xi 	ret = _drm_lease_held_master(master, id);
12156f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
12256f0729aSDesmond Cheong Zhi Xi 
12356f0729aSDesmond Cheong Zhi Xi 	return ret;
1242ed077e4SKeith Packard }
1252ed077e4SKeith Packard 
1262ed077e4SKeith Packard bool drm_lease_held(struct drm_file *file_priv, int id)
1272ed077e4SKeith Packard {
1282ed077e4SKeith Packard 	struct drm_master *master;
1292ed077e4SKeith Packard 	bool ret;
1302ed077e4SKeith Packard 
13156f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1322ed077e4SKeith Packard 		return true;
1332ed077e4SKeith Packard 
13456f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
13556f0729aSDesmond Cheong Zhi Xi 	if (!master)
13656f0729aSDesmond Cheong Zhi Xi 		return true;
13756f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
13856f0729aSDesmond Cheong Zhi Xi 		ret = true;
13956f0729aSDesmond Cheong Zhi Xi 		goto out;
14056f0729aSDesmond Cheong Zhi Xi 	}
1412ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1422ed077e4SKeith Packard 	ret = _drm_lease_held_master(master, id);
1432ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
14456f0729aSDesmond Cheong Zhi Xi 
14556f0729aSDesmond Cheong Zhi Xi out:
14656f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
1472ed077e4SKeith Packard 	return ret;
1482ed077e4SKeith Packard }
1492ed077e4SKeith Packard 
150676f11b5SDesmond Cheong Zhi Xi /*
151676f11b5SDesmond Cheong Zhi Xi  * Given a bitmask of crtcs to check, reconstructs a crtc mask based on the
152676f11b5SDesmond Cheong Zhi Xi  * crtcs which are visible through the specified file.
1532ed077e4SKeith Packard  */
1542ed077e4SKeith Packard uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
1552ed077e4SKeith Packard {
1562ed077e4SKeith Packard 	struct drm_master *master;
1572ed077e4SKeith Packard 	struct drm_device *dev;
1582ed077e4SKeith Packard 	struct drm_crtc *crtc;
1592ed077e4SKeith Packard 	int count_in, count_out;
1602ed077e4SKeith Packard 	uint32_t crtcs_out = 0;
1612ed077e4SKeith Packard 
16256f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1632ed077e4SKeith Packard 		return crtcs_in;
1642ed077e4SKeith Packard 
16556f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
16656f0729aSDesmond Cheong Zhi Xi 	if (!master)
16756f0729aSDesmond Cheong Zhi Xi 		return crtcs_in;
16856f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
16956f0729aSDesmond Cheong Zhi Xi 		crtcs_out = crtcs_in;
17056f0729aSDesmond Cheong Zhi Xi 		goto out;
17156f0729aSDesmond Cheong Zhi Xi 	}
1722ed077e4SKeith Packard 	dev = master->dev;
1732ed077e4SKeith Packard 
1742ed077e4SKeith Packard 	count_in = count_out = 0;
1752ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1762ed077e4SKeith Packard 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1772ed077e4SKeith Packard 		if (_drm_lease_held_master(master, crtc->base.id)) {
1782ed077e4SKeith Packard 			uint32_t mask_in = 1ul << count_in;
179948de842SSuraj Upadhyay 
1802ed077e4SKeith Packard 			if ((crtcs_in & mask_in) != 0) {
1812ed077e4SKeith Packard 				uint32_t mask_out = 1ul << count_out;
182948de842SSuraj Upadhyay 
1832ed077e4SKeith Packard 				crtcs_out |= mask_out;
1842ed077e4SKeith Packard 			}
1852ed077e4SKeith Packard 			count_out++;
1862ed077e4SKeith Packard 		}
1872ed077e4SKeith Packard 		count_in++;
1882ed077e4SKeith Packard 	}
1892ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
19056f0729aSDesmond Cheong Zhi Xi 
19156f0729aSDesmond Cheong Zhi Xi out:
19256f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
1932ed077e4SKeith Packard 	return crtcs_out;
1942ed077e4SKeith Packard }
1952ed077e4SKeith Packard 
1962ed077e4SKeith Packard /*
1972ed077e4SKeith Packard  * Uses drm_master_create to allocate a new drm_master, then checks to
1982ed077e4SKeith Packard  * make sure all of the desired objects can be leased, atomically
1992ed077e4SKeith Packard  * leasing them to the new drmmaster.
2002ed077e4SKeith Packard  *
201918d89bbSColin Ian King  * 	ERR_PTR(-EACCES)	some other master holds the title to any object
2022ed077e4SKeith Packard  * 	ERR_PTR(-ENOENT)	some object is not a valid DRM object for this device
2032ed077e4SKeith Packard  * 	ERR_PTR(-EBUSY)		some other lessee holds title to this object
2042ed077e4SKeith Packard  *	ERR_PTR(-EEXIST)	same object specified more than once in the provided list
2052ed077e4SKeith Packard  *	ERR_PTR(-ENOMEM)	allocation failed
2062ed077e4SKeith Packard  */
2072ed077e4SKeith Packard static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
2082ed077e4SKeith Packard {
2092ed077e4SKeith Packard 	struct drm_device *dev = lessor->dev;
2102ed077e4SKeith Packard 	int error;
2112ed077e4SKeith Packard 	struct drm_master *lessee;
2122ed077e4SKeith Packard 	int object;
2132ed077e4SKeith Packard 	int id;
2142ed077e4SKeith Packard 	void *entry;
2152ed077e4SKeith Packard 
2162ed077e4SKeith Packard 	DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
2172ed077e4SKeith Packard 
2182ed077e4SKeith Packard 	lessee = drm_master_create(lessor->dev);
2192ed077e4SKeith Packard 	if (!lessee) {
2202ed077e4SKeith Packard 		DRM_DEBUG_LEASE("drm_master_create failed\n");
2212ed077e4SKeith Packard 		return ERR_PTR(-ENOMEM);
2222ed077e4SKeith Packard 	}
2232ed077e4SKeith Packard 
2242ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2252ed077e4SKeith Packard 
2262ed077e4SKeith Packard 	idr_for_each_entry(leases, entry, object) {
2272ed077e4SKeith Packard 		error = 0;
228b5f06893SShayenne da Luz Moura 		if (!idr_find(&dev->mode_config.object_idr, object))
2292ed077e4SKeith Packard 			error = -ENOENT;
2302ed077e4SKeith Packard 		else if (_drm_has_leased(lessor, object))
2312ed077e4SKeith Packard 			error = -EBUSY;
2322ed077e4SKeith Packard 
2332ed077e4SKeith Packard 		if (error != 0) {
2342ed077e4SKeith Packard 			DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
2352ed077e4SKeith Packard 			goto out_lessee;
2362ed077e4SKeith Packard 		}
2372ed077e4SKeith Packard 	}
2382ed077e4SKeith Packard 
239d2a48e52SKeith Packard 	/* Insert the new lessee into the tree */
240d2a48e52SKeith Packard 	id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
241d2a48e52SKeith Packard 	if (id < 0) {
242d2a48e52SKeith Packard 		error = id;
243d2a48e52SKeith Packard 		goto out_lessee;
244d2a48e52SKeith Packard 	}
245d2a48e52SKeith Packard 
246d2a48e52SKeith Packard 	lessee->lessee_id = id;
247d2a48e52SKeith Packard 	lessee->lessor = drm_master_get(lessor);
248d2a48e52SKeith Packard 	list_add_tail(&lessee->lessee_list, &lessor->lessees);
249d2a48e52SKeith Packard 
2502ed077e4SKeith Packard 	/* Move the leases over */
2512ed077e4SKeith Packard 	lessee->leases = *leases;
2522ed077e4SKeith Packard 	DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
2532ed077e4SKeith Packard 
2542ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2552ed077e4SKeith Packard 	return lessee;
2562ed077e4SKeith Packard 
2572ed077e4SKeith Packard out_lessee:
2582ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2592ed077e4SKeith Packard 
260bd36d3baSMarius Vlad 	drm_master_put(&lessee);
261bd36d3baSMarius Vlad 
2622ed077e4SKeith Packard 	return ERR_PTR(error);
2632ed077e4SKeith Packard }
2642ed077e4SKeith Packard 
2652ed077e4SKeith Packard void drm_lease_destroy(struct drm_master *master)
2662ed077e4SKeith Packard {
2672ed077e4SKeith Packard 	struct drm_device *dev = master->dev;
2682ed077e4SKeith Packard 
2692ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2702ed077e4SKeith Packard 
2712ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
2722ed077e4SKeith Packard 
2732ed077e4SKeith Packard 	/* This master is referenced by all lessees, hence it cannot be destroyed
2742ed077e4SKeith Packard 	 * until all of them have been
2752ed077e4SKeith Packard 	 */
2762ed077e4SKeith Packard 	WARN_ON(!list_empty(&master->lessees));
2772ed077e4SKeith Packard 
2782ed077e4SKeith Packard 	/* Remove this master from the lessee idr in the owner */
2792ed077e4SKeith Packard 	if (master->lessee_id != 0) {
2802ed077e4SKeith Packard 		DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
2812ed077e4SKeith Packard 		idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
2822ed077e4SKeith Packard 	}
2832ed077e4SKeith Packard 
2842ed077e4SKeith Packard 	/* Remove this master from any lessee list it may be on */
2852ed077e4SKeith Packard 	list_del(&master->lessee_list);
2862ed077e4SKeith Packard 
2872ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2882ed077e4SKeith Packard 
2892ed077e4SKeith Packard 	if (master->lessor) {
2902ed077e4SKeith Packard 		/* Tell the master to check the lessee list */
291ce858828SDaniel Vetter 		drm_sysfs_lease_event(dev);
2922ed077e4SKeith Packard 		drm_master_put(&master->lessor);
2932ed077e4SKeith Packard 	}
2942ed077e4SKeith Packard 
2952ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
2962ed077e4SKeith Packard }
2972ed077e4SKeith Packard 
2982ed077e4SKeith Packard static void _drm_lease_revoke(struct drm_master *top)
2992ed077e4SKeith Packard {
3002ed077e4SKeith Packard 	int object;
3012ed077e4SKeith Packard 	void *entry;
3022ed077e4SKeith Packard 	struct drm_master *master = top;
3032ed077e4SKeith Packard 
3042ed077e4SKeith Packard 	lockdep_assert_held(&top->dev->mode_config.idr_mutex);
3052ed077e4SKeith Packard 
3062ed077e4SKeith Packard 	/*
3072ed077e4SKeith Packard 	 * Walk the tree starting at 'top' emptying all leases. Because
3082ed077e4SKeith Packard 	 * the tree is fully connected, we can do this without recursing
3092ed077e4SKeith Packard 	 */
3102ed077e4SKeith Packard 	for (;;) {
3112ed077e4SKeith Packard 		DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
3122ed077e4SKeith Packard 
3132ed077e4SKeith Packard 		/* Evacuate the lease */
3142ed077e4SKeith Packard 		idr_for_each_entry(&master->leases, entry, object)
3152ed077e4SKeith Packard 			idr_remove(&master->leases, object);
3162ed077e4SKeith Packard 
3172ed077e4SKeith Packard 		/* Depth-first list walk */
3182ed077e4SKeith Packard 
3192ed077e4SKeith Packard 		/* Down */
3202ed077e4SKeith Packard 		if (!list_empty(&master->lessees)) {
3212ed077e4SKeith Packard 			master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
3222ed077e4SKeith Packard 		} else {
3232ed077e4SKeith Packard 			/* Up */
3242ed077e4SKeith Packard 			while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
3252ed077e4SKeith Packard 				master = master->lessor;
3262ed077e4SKeith Packard 
3272ed077e4SKeith Packard 			if (master == top)
3282ed077e4SKeith Packard 				break;
3292ed077e4SKeith Packard 
3302ed077e4SKeith Packard 			/* Over */
331aec06c76SArushi Singhal 			master = list_next_entry(master, lessee_list);
3322ed077e4SKeith Packard 		}
3332ed077e4SKeith Packard 	}
3342ed077e4SKeith Packard }
3352ed077e4SKeith Packard 
3362ed077e4SKeith Packard void drm_lease_revoke(struct drm_master *top)
3372ed077e4SKeith Packard {
3382ed077e4SKeith Packard 	mutex_lock(&top->dev->mode_config.idr_mutex);
3392ed077e4SKeith Packard 	_drm_lease_revoke(top);
3402ed077e4SKeith Packard 	mutex_unlock(&top->dev->mode_config.idr_mutex);
3412ed077e4SKeith Packard }
34262884cd3SKeith Packard 
34362884cd3SKeith Packard static int validate_lease(struct drm_device *dev,
34462884cd3SKeith Packard 			  int object_count,
34596802905SDaniel Vetter 			  struct drm_mode_object **objects,
34696802905SDaniel Vetter 			  bool universal_planes)
34762884cd3SKeith Packard {
34862884cd3SKeith Packard 	int o;
34962884cd3SKeith Packard 	int has_crtc = -1;
35062884cd3SKeith Packard 	int has_connector = -1;
35162884cd3SKeith Packard 	int has_plane = -1;
35262884cd3SKeith Packard 
35362884cd3SKeith Packard 	/* we want to confirm that there is at least one crtc, plane
35462884cd3SKeith Packard 	   connector object. */
35562884cd3SKeith Packard 
35662884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
35762884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
35862884cd3SKeith Packard 			has_crtc = o;
35962884cd3SKeith Packard 		}
36062884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
36162884cd3SKeith Packard 			has_connector = o;
36262884cd3SKeith Packard 
36396802905SDaniel Vetter 		if (universal_planes) {
36462884cd3SKeith Packard 			if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
36562884cd3SKeith Packard 				has_plane = o;
36662884cd3SKeith Packard 		}
36762884cd3SKeith Packard 	}
36862884cd3SKeith Packard 	if (has_crtc == -1 || has_connector == -1)
36962884cd3SKeith Packard 		return -EINVAL;
37096802905SDaniel Vetter 	if (universal_planes && has_plane == -1)
37162884cd3SKeith Packard 		return -EINVAL;
37262884cd3SKeith Packard 	return 0;
37362884cd3SKeith Packard }
37462884cd3SKeith Packard 
37562884cd3SKeith Packard static int fill_object_idr(struct drm_device *dev,
37662884cd3SKeith Packard 			   struct drm_file *lessor_priv,
37762884cd3SKeith Packard 			   struct idr *leases,
37862884cd3SKeith Packard 			   int object_count,
37962884cd3SKeith Packard 			   u32 *object_ids)
38062884cd3SKeith Packard {
38162884cd3SKeith Packard 	struct drm_mode_object **objects;
38262884cd3SKeith Packard 	u32 o;
38362884cd3SKeith Packard 	int ret;
38496802905SDaniel Vetter 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
38596802905SDaniel Vetter 
38662884cd3SKeith Packard 	objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
38762884cd3SKeith Packard 			  GFP_KERNEL);
38862884cd3SKeith Packard 	if (!objects)
38962884cd3SKeith Packard 		return -ENOMEM;
39062884cd3SKeith Packard 
39162884cd3SKeith Packard 	/* step one - get references to all the mode objects
39262884cd3SKeith Packard 	   and check for validity. */
39362884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
39462884cd3SKeith Packard 		objects[o] = drm_mode_object_find(dev, lessor_priv,
39562884cd3SKeith Packard 						  object_ids[o],
39662884cd3SKeith Packard 						  DRM_MODE_OBJECT_ANY);
39762884cd3SKeith Packard 		if (!objects[o]) {
39862884cd3SKeith Packard 			ret = -ENOENT;
39962884cd3SKeith Packard 			goto out_free_objects;
40062884cd3SKeith Packard 		}
40162884cd3SKeith Packard 
40262884cd3SKeith Packard 		if (!drm_mode_object_lease_required(objects[o]->type)) {
403a0c1af46SDaniel Vetter 			DRM_DEBUG_KMS("invalid object for lease\n");
40462884cd3SKeith Packard 			ret = -EINVAL;
40562884cd3SKeith Packard 			goto out_free_objects;
40662884cd3SKeith Packard 		}
40762884cd3SKeith Packard 	}
40862884cd3SKeith Packard 
40996802905SDaniel Vetter 	ret = validate_lease(dev, object_count, objects, universal_planes);
410a0c1af46SDaniel Vetter 	if (ret) {
411a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("lease validation failed\n");
41262884cd3SKeith Packard 		goto out_free_objects;
413a0c1af46SDaniel Vetter 	}
41462884cd3SKeith Packard 
41562884cd3SKeith Packard 	/* add their IDs to the lease request - taking into account
41662884cd3SKeith Packard 	   universal planes */
41762884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
41862884cd3SKeith Packard 		struct drm_mode_object *obj = objects[o];
41962884cd3SKeith Packard 		u32 object_id = objects[o]->id;
420948de842SSuraj Upadhyay 
42162884cd3SKeith Packard 		DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
42262884cd3SKeith Packard 
42362884cd3SKeith Packard 		/*
42462884cd3SKeith Packard 		 * We're using an IDR to hold the set of leased
42562884cd3SKeith Packard 		 * objects, but we don't need to point at the object's
426b5f06893SShayenne da Luz Moura 		 * data structure from the lease as the main object_idr
42762884cd3SKeith Packard 		 * will be used to actually find that. Instead, all we
42862884cd3SKeith Packard 		 * really want is a 'leased/not-leased' result, for
42962884cd3SKeith Packard 		 * which any non-NULL pointer will work fine.
43062884cd3SKeith Packard 		 */
43162884cd3SKeith Packard 		ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
43262884cd3SKeith Packard 		if (ret < 0) {
43362884cd3SKeith Packard 			DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
43462884cd3SKeith Packard 					object_id, ret);
43562884cd3SKeith Packard 			goto out_free_objects;
43662884cd3SKeith Packard 		}
43796802905SDaniel Vetter 		if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
43862884cd3SKeith Packard 			struct drm_crtc *crtc = obj_to_crtc(obj);
439948de842SSuraj Upadhyay 
44062884cd3SKeith Packard 			ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
44162884cd3SKeith Packard 			if (ret < 0) {
44262884cd3SKeith Packard 				DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
44362884cd3SKeith Packard 						object_id, ret);
44462884cd3SKeith Packard 				goto out_free_objects;
44562884cd3SKeith Packard 			}
44662884cd3SKeith Packard 			if (crtc->cursor) {
44762884cd3SKeith Packard 				ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
44862884cd3SKeith Packard 				if (ret < 0) {
44962884cd3SKeith Packard 					DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
45062884cd3SKeith Packard 							object_id, ret);
45162884cd3SKeith Packard 					goto out_free_objects;
45262884cd3SKeith Packard 				}
45362884cd3SKeith Packard 			}
45462884cd3SKeith Packard 		}
45562884cd3SKeith Packard 	}
45662884cd3SKeith Packard 
45762884cd3SKeith Packard 	ret = 0;
45862884cd3SKeith Packard out_free_objects:
45962884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
46062884cd3SKeith Packard 		if (objects[o])
46162884cd3SKeith Packard 			drm_mode_object_put(objects[o]);
46262884cd3SKeith Packard 	}
46362884cd3SKeith Packard 	kfree(objects);
46462884cd3SKeith Packard 	return ret;
46562884cd3SKeith Packard }
46662884cd3SKeith Packard 
467676f11b5SDesmond Cheong Zhi Xi /*
46862884cd3SKeith Packard  * The master associated with the specified file will have a lease
46962884cd3SKeith Packard  * created containing the objects specified in the ioctl structure.
47062884cd3SKeith Packard  * A file descriptor will be allocated for that and returned to the
47162884cd3SKeith Packard  * application.
47262884cd3SKeith Packard  */
47362884cd3SKeith Packard int drm_mode_create_lease_ioctl(struct drm_device *dev,
47462884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
47562884cd3SKeith Packard {
47662884cd3SKeith Packard 	struct drm_mode_create_lease *cl = data;
47762884cd3SKeith Packard 	size_t object_count;
47862884cd3SKeith Packard 	int ret = 0;
47962884cd3SKeith Packard 	struct idr leases;
48056f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
48162884cd3SKeith Packard 	struct drm_master *lessee = NULL;
48262884cd3SKeith Packard 	struct file *lessee_file = NULL;
48362884cd3SKeith Packard 	struct file *lessor_file = lessor_priv->filp;
48462884cd3SKeith Packard 	struct drm_file *lessee_priv;
48562884cd3SKeith Packard 	int fd = -1;
48662884cd3SKeith Packard 	uint32_t *object_ids;
48762884cd3SKeith Packard 
48862884cd3SKeith Packard 	/* Can't lease without MODESET */
48962884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
49069fdf420SChris Wilson 		return -EOPNOTSUPP;
49162884cd3SKeith Packard 
492a0c1af46SDaniel Vetter 	if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
493a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("invalid flags\n");
49462884cd3SKeith Packard 		return -EINVAL;
495a0c1af46SDaniel Vetter 	}
49662884cd3SKeith Packard 
49756f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
49856f0729aSDesmond Cheong Zhi Xi 	/* Do not allow sub-leases */
49956f0729aSDesmond Cheong Zhi Xi 	if (lessor->lessor) {
50056f0729aSDesmond Cheong Zhi Xi 		DRM_DEBUG_LEASE("recursive leasing not allowed\n");
50156f0729aSDesmond Cheong Zhi Xi 		ret = -EINVAL;
50256f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
50356f0729aSDesmond Cheong Zhi Xi 	}
50456f0729aSDesmond Cheong Zhi Xi 
50562884cd3SKeith Packard 	object_count = cl->object_count;
50662884cd3SKeith Packard 
507*4bb2d367SSimon Ser 	/* Handle leased objects, if any */
508*4bb2d367SSimon Ser 	idr_init(&leases);
509*4bb2d367SSimon Ser 	if (object_count != 0) {
51069ef943dSMatthew Wilcox 		object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
51169ef943dSMatthew Wilcox 					 array_size(object_count, sizeof(__u32)));
51256f0729aSDesmond Cheong Zhi Xi 		if (IS_ERR(object_ids)) {
51356f0729aSDesmond Cheong Zhi Xi 			ret = PTR_ERR(object_ids);
514*4bb2d367SSimon Ser 			idr_destroy(&leases);
51556f0729aSDesmond Cheong Zhi Xi 			goto out_lessor;
51656f0729aSDesmond Cheong Zhi Xi 		}
51762884cd3SKeith Packard 
51862884cd3SKeith Packard 		/* fill and validate the object idr */
51962884cd3SKeith Packard 		ret = fill_object_idr(dev, lessor_priv, &leases,
52062884cd3SKeith Packard 				      object_count, object_ids);
52162884cd3SKeith Packard 		kfree(object_ids);
52262884cd3SKeith Packard 		if (ret) {
523a0c1af46SDaniel Vetter 			DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
52462884cd3SKeith Packard 			idr_destroy(&leases);
52556f0729aSDesmond Cheong Zhi Xi 			goto out_lessor;
52662884cd3SKeith Packard 		}
527*4bb2d367SSimon Ser 	}
52862884cd3SKeith Packard 
52962884cd3SKeith Packard 	/* Allocate a file descriptor for the lease */
53062884cd3SKeith Packard 	fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
53162884cd3SKeith Packard 	if (fd < 0) {
53262884cd3SKeith Packard 		idr_destroy(&leases);
53356f0729aSDesmond Cheong Zhi Xi 		ret = fd;
53456f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
53562884cd3SKeith Packard 	}
53662884cd3SKeith Packard 
53762884cd3SKeith Packard 	DRM_DEBUG_LEASE("Creating lease\n");
538b216a8e7SQiujun Huang 	/* lessee will take the ownership of leases */
53962884cd3SKeith Packard 	lessee = drm_lease_create(lessor, &leases);
54062884cd3SKeith Packard 
54162884cd3SKeith Packard 	if (IS_ERR(lessee)) {
54262884cd3SKeith Packard 		ret = PTR_ERR(lessee);
543b216a8e7SQiujun Huang 		idr_destroy(&leases);
54462884cd3SKeith Packard 		goto out_leases;
54562884cd3SKeith Packard 	}
54662884cd3SKeith Packard 
54762884cd3SKeith Packard 	/* Clone the lessor file to create a new file for us */
54862884cd3SKeith Packard 	DRM_DEBUG_LEASE("Allocating lease file\n");
54919f391ebSAl Viro 	lessee_file = file_clone_open(lessor_file);
55062884cd3SKeith Packard 	if (IS_ERR(lessee_file)) {
55162884cd3SKeith Packard 		ret = PTR_ERR(lessee_file);
55262884cd3SKeith Packard 		goto out_lessee;
55362884cd3SKeith Packard 	}
55462884cd3SKeith Packard 
55562884cd3SKeith Packard 	lessee_priv = lessee_file->private_data;
55662884cd3SKeith Packard 	/* Change the file to a master one */
55762884cd3SKeith Packard 	drm_master_put(&lessee_priv->master);
55862884cd3SKeith Packard 	lessee_priv->master = lessee;
55962884cd3SKeith Packard 	lessee_priv->is_master = 1;
56062884cd3SKeith Packard 	lessee_priv->authenticated = 1;
56162884cd3SKeith Packard 
56262884cd3SKeith Packard 	/* Pass fd back to userspace */
56362884cd3SKeith Packard 	DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
56462884cd3SKeith Packard 	cl->fd = fd;
56562884cd3SKeith Packard 	cl->lessee_id = lessee->lessee_id;
56662884cd3SKeith Packard 
56712d43debSJann Horn 	/* Hook up the fd */
56812d43debSJann Horn 	fd_install(fd, lessee_file);
56912d43debSJann Horn 
57056f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
57162884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
57262884cd3SKeith Packard 	return 0;
57362884cd3SKeith Packard 
57462884cd3SKeith Packard out_lessee:
57562884cd3SKeith Packard 	drm_master_put(&lessee);
57662884cd3SKeith Packard 
57762884cd3SKeith Packard out_leases:
57862884cd3SKeith Packard 	put_unused_fd(fd);
57962884cd3SKeith Packard 
58056f0729aSDesmond Cheong Zhi Xi out_lessor:
58156f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
58262884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
58362884cd3SKeith Packard 	return ret;
58462884cd3SKeith Packard }
58562884cd3SKeith Packard 
58662884cd3SKeith Packard int drm_mode_list_lessees_ioctl(struct drm_device *dev,
58762884cd3SKeith Packard 			       void *data, struct drm_file *lessor_priv)
58862884cd3SKeith Packard {
58962884cd3SKeith Packard 	struct drm_mode_list_lessees *arg = data;
59062884cd3SKeith Packard 	__u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
59162884cd3SKeith Packard 	__u32 count_lessees = arg->count_lessees;
59256f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor, *lessee;
59362884cd3SKeith Packard 	int count;
59462884cd3SKeith Packard 	int ret = 0;
59562884cd3SKeith Packard 
59662884cd3SKeith Packard 	if (arg->pad)
59762884cd3SKeith Packard 		return -EINVAL;
59862884cd3SKeith Packard 
59962884cd3SKeith Packard 	/* Can't lease without MODESET */
60062884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
60169fdf420SChris Wilson 		return -EOPNOTSUPP;
60262884cd3SKeith Packard 
60356f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
60462884cd3SKeith Packard 	DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
60562884cd3SKeith Packard 
60662884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
60762884cd3SKeith Packard 
60862884cd3SKeith Packard 	count = 0;
60962884cd3SKeith Packard 	drm_for_each_lessee(lessee, lessor) {
61062884cd3SKeith Packard 		/* Only list un-revoked leases */
61162884cd3SKeith Packard 		if (!idr_is_empty(&lessee->leases)) {
61262884cd3SKeith Packard 			if (count_lessees > count) {
61362884cd3SKeith Packard 				DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
61462884cd3SKeith Packard 				ret = put_user(lessee->lessee_id, lessee_ids + count);
61562884cd3SKeith Packard 				if (ret)
61662884cd3SKeith Packard 					break;
61762884cd3SKeith Packard 			}
61862884cd3SKeith Packard 			count++;
61962884cd3SKeith Packard 		}
62062884cd3SKeith Packard 	}
62162884cd3SKeith Packard 
62262884cd3SKeith Packard 	DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
62362884cd3SKeith Packard 	if (ret == 0)
62462884cd3SKeith Packard 		arg->count_lessees = count;
62562884cd3SKeith Packard 
62662884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
62756f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
62862884cd3SKeith Packard 
62962884cd3SKeith Packard 	return ret;
63062884cd3SKeith Packard }
63162884cd3SKeith Packard 
632676f11b5SDesmond Cheong Zhi Xi /* Return the list of leased objects for the specified lessee */
63362884cd3SKeith Packard int drm_mode_get_lease_ioctl(struct drm_device *dev,
63462884cd3SKeith Packard 			     void *data, struct drm_file *lessee_priv)
63562884cd3SKeith Packard {
63662884cd3SKeith Packard 	struct drm_mode_get_lease *arg = data;
63762884cd3SKeith Packard 	__u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
63862884cd3SKeith Packard 	__u32 count_objects = arg->count_objects;
63956f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessee;
64062884cd3SKeith Packard 	struct idr *object_idr;
64162884cd3SKeith Packard 	int count;
64262884cd3SKeith Packard 	void *entry;
64362884cd3SKeith Packard 	int object;
64462884cd3SKeith Packard 	int ret = 0;
64562884cd3SKeith Packard 
64662884cd3SKeith Packard 	if (arg->pad)
64762884cd3SKeith Packard 		return -EINVAL;
64862884cd3SKeith Packard 
64962884cd3SKeith Packard 	/* Can't lease without MODESET */
65062884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
65169fdf420SChris Wilson 		return -EOPNOTSUPP;
65262884cd3SKeith Packard 
65356f0729aSDesmond Cheong Zhi Xi 	lessee = drm_file_get_master(lessee_priv);
65462884cd3SKeith Packard 	DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
65562884cd3SKeith Packard 
65662884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
65762884cd3SKeith Packard 
65862884cd3SKeith Packard 	if (lessee->lessor == NULL)
65962884cd3SKeith Packard 		/* owner can use all objects */
660b5f06893SShayenne da Luz Moura 		object_idr = &lessee->dev->mode_config.object_idr;
66162884cd3SKeith Packard 	else
66262884cd3SKeith Packard 		/* lessee can only use allowed object */
66362884cd3SKeith Packard 		object_idr = &lessee->leases;
66462884cd3SKeith Packard 
66562884cd3SKeith Packard 	count = 0;
66662884cd3SKeith Packard 	idr_for_each_entry(object_idr, entry, object) {
66762884cd3SKeith Packard 		if (count_objects > count) {
66862884cd3SKeith Packard 			DRM_DEBUG_LEASE("adding object %d\n", object);
66962884cd3SKeith Packard 			ret = put_user(object, object_ids + count);
67062884cd3SKeith Packard 			if (ret)
67162884cd3SKeith Packard 				break;
67262884cd3SKeith Packard 		}
67362884cd3SKeith Packard 		count++;
67462884cd3SKeith Packard 	}
67562884cd3SKeith Packard 
67662884cd3SKeith Packard 	DRM_DEBUG("lease holds %d objects\n", count);
67762884cd3SKeith Packard 	if (ret == 0)
67862884cd3SKeith Packard 		arg->count_objects = count;
67962884cd3SKeith Packard 
68062884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
68156f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessee);
68262884cd3SKeith Packard 
68362884cd3SKeith Packard 	return ret;
68462884cd3SKeith Packard }
68562884cd3SKeith Packard 
686676f11b5SDesmond Cheong Zhi Xi /*
68762884cd3SKeith Packard  * This removes all of the objects from the lease without
68862884cd3SKeith Packard  * actually getting rid of the lease itself; that way all
68962884cd3SKeith Packard  * references to it still work correctly
69062884cd3SKeith Packard  */
69162884cd3SKeith Packard int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
69262884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
69362884cd3SKeith Packard {
69462884cd3SKeith Packard 	struct drm_mode_revoke_lease *arg = data;
69556f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
69662884cd3SKeith Packard 	struct drm_master *lessee;
69762884cd3SKeith Packard 	int ret = 0;
69862884cd3SKeith Packard 
69962884cd3SKeith Packard 	DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
70062884cd3SKeith Packard 
70162884cd3SKeith Packard 	/* Can't lease without MODESET */
70262884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
70369fdf420SChris Wilson 		return -EOPNOTSUPP;
70462884cd3SKeith Packard 
70556f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
70662884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
70762884cd3SKeith Packard 
70862884cd3SKeith Packard 	lessee = _drm_find_lessee(lessor, arg->lessee_id);
70962884cd3SKeith Packard 
71062884cd3SKeith Packard 	/* No such lessee */
71162884cd3SKeith Packard 	if (!lessee) {
71262884cd3SKeith Packard 		ret = -ENOENT;
71362884cd3SKeith Packard 		goto fail;
71462884cd3SKeith Packard 	}
71562884cd3SKeith Packard 
71662884cd3SKeith Packard 	/* Lease is not held by lessor */
71762884cd3SKeith Packard 	if (lessee->lessor != lessor) {
71862884cd3SKeith Packard 		ret = -EACCES;
71962884cd3SKeith Packard 		goto fail;
72062884cd3SKeith Packard 	}
72162884cd3SKeith Packard 
72262884cd3SKeith Packard 	_drm_lease_revoke(lessee);
72362884cd3SKeith Packard 
72462884cd3SKeith Packard fail:
72562884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
72656f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
72762884cd3SKeith Packard 
72862884cd3SKeith Packard 	return ret;
72962884cd3SKeith Packard }
730