xref: /linux/drivers/gpu/drm/drm_lease.c (revision 56f0729a510f92151682ff6c89f69724d5595d6e)
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 
182ed077e4SKeith Packard #define drm_for_each_lessee(lessee, lessor) \
192ed077e4SKeith Packard 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
202ed077e4SKeith Packard 
2162884cd3SKeith Packard static uint64_t drm_lease_idr_object;
2262884cd3SKeith Packard 
232ed077e4SKeith Packard /**
242ed077e4SKeith Packard  * drm_lease_owner - return ancestor owner drm_master
252ed077e4SKeith Packard  * @master: drm_master somewhere within tree of lessees and lessors
262ed077e4SKeith Packard  *
272ed077e4SKeith Packard  * RETURN:
282ed077e4SKeith Packard  *
292ed077e4SKeith Packard  * drm_master at the top of the tree (i.e, with lessor NULL
302ed077e4SKeith Packard  */
312ed077e4SKeith Packard struct drm_master *drm_lease_owner(struct drm_master *master)
322ed077e4SKeith Packard {
332ed077e4SKeith Packard 	while (master->lessor != NULL)
342ed077e4SKeith Packard 		master = master->lessor;
352ed077e4SKeith Packard 	return master;
362ed077e4SKeith Packard }
372ed077e4SKeith Packard 
382ed077e4SKeith Packard /**
392ed077e4SKeith Packard  * _drm_find_lessee - find lessee by id (idr_mutex held)
402ed077e4SKeith Packard  * @master: drm_master of lessor
417591844cSChris Wilson  * @lessee_id: id
422ed077e4SKeith Packard  *
432ed077e4SKeith Packard  * RETURN:
442ed077e4SKeith Packard  *
452ed077e4SKeith Packard  * drm_master of the lessee if valid, NULL otherwise
462ed077e4SKeith Packard  */
472ed077e4SKeith Packard 
482ed077e4SKeith Packard static struct drm_master*
492ed077e4SKeith Packard _drm_find_lessee(struct drm_master *master, int lessee_id)
502ed077e4SKeith Packard {
512ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
522ed077e4SKeith Packard 	return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
532ed077e4SKeith Packard }
542ed077e4SKeith Packard 
552ed077e4SKeith Packard /**
562ed077e4SKeith Packard  * _drm_lease_held_master - check to see if an object is leased (or owned) by master (idr_mutex held)
572ed077e4SKeith Packard  * @master: the master to check the lease status of
582ed077e4SKeith Packard  * @id: the id to check
592ed077e4SKeith Packard  *
602ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
612ed077e4SKeith Packard  * value:
622ed077e4SKeith Packard  *
632ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
642ed077e4SKeith Packard  *	false		'master' does not hold a lease.
652ed077e4SKeith Packard  */
662ed077e4SKeith Packard static int _drm_lease_held_master(struct drm_master *master, int id)
672ed077e4SKeith Packard {
682ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
692ed077e4SKeith Packard 	if (master->lessor)
702ed077e4SKeith Packard 		return idr_find(&master->leases, id) != NULL;
712ed077e4SKeith Packard 	return true;
722ed077e4SKeith Packard }
732ed077e4SKeith Packard 
742ed077e4SKeith Packard /**
752ed077e4SKeith Packard  * _drm_has_leased - check to see if an object has been leased (idr_mutex held)
762ed077e4SKeith Packard  * @master: the master to check the lease status of
772ed077e4SKeith Packard  * @id: the id to check
782ed077e4SKeith Packard  *
792ed077e4SKeith Packard  * Checks if any lessee of 'master' holds a lease on 'id'. Return
802ed077e4SKeith Packard  * value:
812ed077e4SKeith Packard  *
822ed077e4SKeith Packard  *	true		Some lessee holds a lease on the object.
832ed077e4SKeith Packard  *	false		No lessee has a lease on the object.
842ed077e4SKeith Packard  */
852ed077e4SKeith Packard static bool _drm_has_leased(struct drm_master *master, int id)
862ed077e4SKeith Packard {
872ed077e4SKeith Packard 	struct drm_master *lessee;
882ed077e4SKeith Packard 
892ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
902ed077e4SKeith Packard 	drm_for_each_lessee(lessee, master)
912ed077e4SKeith Packard 		if (_drm_lease_held_master(lessee, id))
922ed077e4SKeith Packard 			return true;
932ed077e4SKeith Packard 	return false;
942ed077e4SKeith Packard }
952ed077e4SKeith Packard 
962ed077e4SKeith Packard /**
972ed077e4SKeith Packard  * _drm_lease_held - check drm_mode_object lease status (idr_mutex held)
987591844cSChris Wilson  * @file_priv: the master drm_file
992ed077e4SKeith Packard  * @id: the object id
1002ed077e4SKeith Packard  *
1012ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1022ed077e4SKeith Packard  * value:
1032ed077e4SKeith Packard  *
1042ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1052ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1062ed077e4SKeith Packard  */
1072ed077e4SKeith Packard bool _drm_lease_held(struct drm_file *file_priv, int id)
1082ed077e4SKeith Packard {
109*56f0729aSDesmond Cheong Zhi Xi 	bool ret;
110*56f0729aSDesmond Cheong Zhi Xi 	struct drm_master *master;
111*56f0729aSDesmond Cheong Zhi Xi 
112*56f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1132ed077e4SKeith Packard 		return true;
1142ed077e4SKeith Packard 
115*56f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
116*56f0729aSDesmond Cheong Zhi Xi 	if (!master)
117*56f0729aSDesmond Cheong Zhi Xi 		return true;
118*56f0729aSDesmond Cheong Zhi Xi 	ret = _drm_lease_held_master(master, id);
119*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
120*56f0729aSDesmond Cheong Zhi Xi 
121*56f0729aSDesmond Cheong Zhi Xi 	return ret;
1222ed077e4SKeith Packard }
1232ed077e4SKeith Packard 
1242ed077e4SKeith Packard /**
1252ed077e4SKeith Packard  * drm_lease_held - check drm_mode_object lease status (idr_mutex not held)
1267591844cSChris Wilson  * @file_priv: the master drm_file
1272ed077e4SKeith Packard  * @id: the object id
1282ed077e4SKeith Packard  *
1292ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1302ed077e4SKeith Packard  * value:
1312ed077e4SKeith Packard  *
1322ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1332ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1342ed077e4SKeith Packard  */
1352ed077e4SKeith Packard bool drm_lease_held(struct drm_file *file_priv, int id)
1362ed077e4SKeith Packard {
1372ed077e4SKeith Packard 	struct drm_master *master;
1382ed077e4SKeith Packard 	bool ret;
1392ed077e4SKeith Packard 
140*56f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1412ed077e4SKeith Packard 		return true;
1422ed077e4SKeith Packard 
143*56f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
144*56f0729aSDesmond Cheong Zhi Xi 	if (!master)
145*56f0729aSDesmond Cheong Zhi Xi 		return true;
146*56f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
147*56f0729aSDesmond Cheong Zhi Xi 		ret = true;
148*56f0729aSDesmond Cheong Zhi Xi 		goto out;
149*56f0729aSDesmond Cheong Zhi Xi 	}
1502ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1512ed077e4SKeith Packard 	ret = _drm_lease_held_master(master, id);
1522ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
153*56f0729aSDesmond Cheong Zhi Xi 
154*56f0729aSDesmond Cheong Zhi Xi out:
155*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
1562ed077e4SKeith Packard 	return ret;
1572ed077e4SKeith Packard }
1582ed077e4SKeith Packard 
1592ed077e4SKeith Packard /**
1602ed077e4SKeith Packard  * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held)
1612ed077e4SKeith Packard  * @file_priv: requestor file
1627591844cSChris Wilson  * @crtcs_in: bitmask of crtcs to check
1632ed077e4SKeith Packard  *
1642ed077e4SKeith Packard  * Reconstructs a crtc mask based on the crtcs which are visible
1652ed077e4SKeith Packard  * through the specified file.
1662ed077e4SKeith Packard  */
1672ed077e4SKeith Packard uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
1682ed077e4SKeith Packard {
1692ed077e4SKeith Packard 	struct drm_master *master;
1702ed077e4SKeith Packard 	struct drm_device *dev;
1712ed077e4SKeith Packard 	struct drm_crtc *crtc;
1722ed077e4SKeith Packard 	int count_in, count_out;
1732ed077e4SKeith Packard 	uint32_t crtcs_out = 0;
1742ed077e4SKeith Packard 
175*56f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1762ed077e4SKeith Packard 		return crtcs_in;
1772ed077e4SKeith Packard 
178*56f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
179*56f0729aSDesmond Cheong Zhi Xi 	if (!master)
180*56f0729aSDesmond Cheong Zhi Xi 		return crtcs_in;
181*56f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
182*56f0729aSDesmond Cheong Zhi Xi 		crtcs_out = crtcs_in;
183*56f0729aSDesmond Cheong Zhi Xi 		goto out;
184*56f0729aSDesmond Cheong Zhi Xi 	}
1852ed077e4SKeith Packard 	dev = master->dev;
1862ed077e4SKeith Packard 
1872ed077e4SKeith Packard 	count_in = count_out = 0;
1882ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1892ed077e4SKeith Packard 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1902ed077e4SKeith Packard 		if (_drm_lease_held_master(master, crtc->base.id)) {
1912ed077e4SKeith Packard 			uint32_t mask_in = 1ul << count_in;
192948de842SSuraj Upadhyay 
1932ed077e4SKeith Packard 			if ((crtcs_in & mask_in) != 0) {
1942ed077e4SKeith Packard 				uint32_t mask_out = 1ul << count_out;
195948de842SSuraj Upadhyay 
1962ed077e4SKeith Packard 				crtcs_out |= mask_out;
1972ed077e4SKeith Packard 			}
1982ed077e4SKeith Packard 			count_out++;
1992ed077e4SKeith Packard 		}
2002ed077e4SKeith Packard 		count_in++;
2012ed077e4SKeith Packard 	}
2022ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
203*56f0729aSDesmond Cheong Zhi Xi 
204*56f0729aSDesmond Cheong Zhi Xi out:
205*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
2062ed077e4SKeith Packard 	return crtcs_out;
2072ed077e4SKeith Packard }
2082ed077e4SKeith Packard 
2092ed077e4SKeith Packard /*
2102ed077e4SKeith Packard  * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held)
2112ed077e4SKeith Packard  * @lessor: lease holder (or owner) of objects
2122ed077e4SKeith Packard  * @leases: objects to lease to the new drm_master
2132ed077e4SKeith Packard  *
2142ed077e4SKeith Packard  * Uses drm_master_create to allocate a new drm_master, then checks to
2152ed077e4SKeith Packard  * make sure all of the desired objects can be leased, atomically
2162ed077e4SKeith Packard  * leasing them to the new drmmaster.
2172ed077e4SKeith Packard  *
218918d89bbSColin Ian King  * 	ERR_PTR(-EACCES)	some other master holds the title to any object
2192ed077e4SKeith Packard  * 	ERR_PTR(-ENOENT)	some object is not a valid DRM object for this device
2202ed077e4SKeith Packard  * 	ERR_PTR(-EBUSY)		some other lessee holds title to this object
2212ed077e4SKeith Packard  *	ERR_PTR(-EEXIST)	same object specified more than once in the provided list
2222ed077e4SKeith Packard  *	ERR_PTR(-ENOMEM)	allocation failed
2232ed077e4SKeith Packard  */
2242ed077e4SKeith Packard static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
2252ed077e4SKeith Packard {
2262ed077e4SKeith Packard 	struct drm_device *dev = lessor->dev;
2272ed077e4SKeith Packard 	int error;
2282ed077e4SKeith Packard 	struct drm_master *lessee;
2292ed077e4SKeith Packard 	int object;
2302ed077e4SKeith Packard 	int id;
2312ed077e4SKeith Packard 	void *entry;
2322ed077e4SKeith Packard 
2332ed077e4SKeith Packard 	DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
2342ed077e4SKeith Packard 
2352ed077e4SKeith Packard 	lessee = drm_master_create(lessor->dev);
2362ed077e4SKeith Packard 	if (!lessee) {
2372ed077e4SKeith Packard 		DRM_DEBUG_LEASE("drm_master_create failed\n");
2382ed077e4SKeith Packard 		return ERR_PTR(-ENOMEM);
2392ed077e4SKeith Packard 	}
2402ed077e4SKeith Packard 
2412ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2422ed077e4SKeith Packard 
2432ed077e4SKeith Packard 	idr_for_each_entry(leases, entry, object) {
2442ed077e4SKeith Packard 		error = 0;
245b5f06893SShayenne da Luz Moura 		if (!idr_find(&dev->mode_config.object_idr, object))
2462ed077e4SKeith Packard 			error = -ENOENT;
2472ed077e4SKeith Packard 		else if (_drm_has_leased(lessor, object))
2482ed077e4SKeith Packard 			error = -EBUSY;
2492ed077e4SKeith Packard 
2502ed077e4SKeith Packard 		if (error != 0) {
2512ed077e4SKeith Packard 			DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
2522ed077e4SKeith Packard 			goto out_lessee;
2532ed077e4SKeith Packard 		}
2542ed077e4SKeith Packard 	}
2552ed077e4SKeith Packard 
256d2a48e52SKeith Packard 	/* Insert the new lessee into the tree */
257d2a48e52SKeith Packard 	id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
258d2a48e52SKeith Packard 	if (id < 0) {
259d2a48e52SKeith Packard 		error = id;
260d2a48e52SKeith Packard 		goto out_lessee;
261d2a48e52SKeith Packard 	}
262d2a48e52SKeith Packard 
263d2a48e52SKeith Packard 	lessee->lessee_id = id;
264d2a48e52SKeith Packard 	lessee->lessor = drm_master_get(lessor);
265d2a48e52SKeith Packard 	list_add_tail(&lessee->lessee_list, &lessor->lessees);
266d2a48e52SKeith Packard 
2672ed077e4SKeith Packard 	/* Move the leases over */
2682ed077e4SKeith Packard 	lessee->leases = *leases;
2692ed077e4SKeith Packard 	DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
2702ed077e4SKeith Packard 
2712ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2722ed077e4SKeith Packard 	return lessee;
2732ed077e4SKeith Packard 
2742ed077e4SKeith Packard out_lessee:
2752ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2762ed077e4SKeith Packard 
277bd36d3baSMarius Vlad 	drm_master_put(&lessee);
278bd36d3baSMarius Vlad 
2792ed077e4SKeith Packard 	return ERR_PTR(error);
2802ed077e4SKeith Packard }
2812ed077e4SKeith Packard 
2822ed077e4SKeith Packard /**
2832ed077e4SKeith Packard  * drm_lease_destroy - a master is going away (idr_mutex not held)
2842ed077e4SKeith Packard  * @master: the drm_master being destroyed
2852ed077e4SKeith Packard  *
2862ed077e4SKeith Packard  * All lessees will have been destroyed as they
2872ed077e4SKeith Packard  * hold a reference on their lessor. Notify any
2882ed077e4SKeith Packard  * lessor for this master so that it can check
2892ed077e4SKeith Packard  * the list of lessees.
2902ed077e4SKeith Packard  */
2912ed077e4SKeith Packard void drm_lease_destroy(struct drm_master *master)
2922ed077e4SKeith Packard {
2932ed077e4SKeith Packard 	struct drm_device *dev = master->dev;
2942ed077e4SKeith Packard 
2952ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2962ed077e4SKeith Packard 
2972ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
2982ed077e4SKeith Packard 
2992ed077e4SKeith Packard 	/* This master is referenced by all lessees, hence it cannot be destroyed
3002ed077e4SKeith Packard 	 * until all of them have been
3012ed077e4SKeith Packard 	 */
3022ed077e4SKeith Packard 	WARN_ON(!list_empty(&master->lessees));
3032ed077e4SKeith Packard 
3042ed077e4SKeith Packard 	/* Remove this master from the lessee idr in the owner */
3052ed077e4SKeith Packard 	if (master->lessee_id != 0) {
3062ed077e4SKeith Packard 		DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
3072ed077e4SKeith Packard 		idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
3082ed077e4SKeith Packard 	}
3092ed077e4SKeith Packard 
3102ed077e4SKeith Packard 	/* Remove this master from any lessee list it may be on */
3112ed077e4SKeith Packard 	list_del(&master->lessee_list);
3122ed077e4SKeith Packard 
3132ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
3142ed077e4SKeith Packard 
3152ed077e4SKeith Packard 	if (master->lessor) {
3162ed077e4SKeith Packard 		/* Tell the master to check the lessee list */
317ce858828SDaniel Vetter 		drm_sysfs_lease_event(dev);
3182ed077e4SKeith Packard 		drm_master_put(&master->lessor);
3192ed077e4SKeith Packard 	}
3202ed077e4SKeith Packard 
3212ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
3222ed077e4SKeith Packard }
3232ed077e4SKeith Packard 
3242ed077e4SKeith Packard /**
3252ed077e4SKeith Packard  * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held)
3267591844cSChris Wilson  * @top: the master losing its lease
3272ed077e4SKeith Packard  */
3282ed077e4SKeith Packard static void _drm_lease_revoke(struct drm_master *top)
3292ed077e4SKeith Packard {
3302ed077e4SKeith Packard 	int object;
3312ed077e4SKeith Packard 	void *entry;
3322ed077e4SKeith Packard 	struct drm_master *master = top;
3332ed077e4SKeith Packard 
3342ed077e4SKeith Packard 	lockdep_assert_held(&top->dev->mode_config.idr_mutex);
3352ed077e4SKeith Packard 
3362ed077e4SKeith Packard 	/*
3372ed077e4SKeith Packard 	 * Walk the tree starting at 'top' emptying all leases. Because
3382ed077e4SKeith Packard 	 * the tree is fully connected, we can do this without recursing
3392ed077e4SKeith Packard 	 */
3402ed077e4SKeith Packard 	for (;;) {
3412ed077e4SKeith Packard 		DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
3422ed077e4SKeith Packard 
3432ed077e4SKeith Packard 		/* Evacuate the lease */
3442ed077e4SKeith Packard 		idr_for_each_entry(&master->leases, entry, object)
3452ed077e4SKeith Packard 			idr_remove(&master->leases, object);
3462ed077e4SKeith Packard 
3472ed077e4SKeith Packard 		/* Depth-first list walk */
3482ed077e4SKeith Packard 
3492ed077e4SKeith Packard 		/* Down */
3502ed077e4SKeith Packard 		if (!list_empty(&master->lessees)) {
3512ed077e4SKeith Packard 			master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
3522ed077e4SKeith Packard 		} else {
3532ed077e4SKeith Packard 			/* Up */
3542ed077e4SKeith Packard 			while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
3552ed077e4SKeith Packard 				master = master->lessor;
3562ed077e4SKeith Packard 
3572ed077e4SKeith Packard 			if (master == top)
3582ed077e4SKeith Packard 				break;
3592ed077e4SKeith Packard 
3602ed077e4SKeith Packard 			/* Over */
361aec06c76SArushi Singhal 			master = list_next_entry(master, lessee_list);
3622ed077e4SKeith Packard 		}
3632ed077e4SKeith Packard 	}
3642ed077e4SKeith Packard }
3652ed077e4SKeith Packard 
3662ed077e4SKeith Packard /**
3672ed077e4SKeith Packard  * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held)
3682ed077e4SKeith Packard  * @top: the master losing its lease
3692ed077e4SKeith Packard  */
3702ed077e4SKeith Packard void drm_lease_revoke(struct drm_master *top)
3712ed077e4SKeith Packard {
3722ed077e4SKeith Packard 	mutex_lock(&top->dev->mode_config.idr_mutex);
3732ed077e4SKeith Packard 	_drm_lease_revoke(top);
3742ed077e4SKeith Packard 	mutex_unlock(&top->dev->mode_config.idr_mutex);
3752ed077e4SKeith Packard }
37662884cd3SKeith Packard 
37762884cd3SKeith Packard static int validate_lease(struct drm_device *dev,
37862884cd3SKeith Packard 			  int object_count,
37996802905SDaniel Vetter 			  struct drm_mode_object **objects,
38096802905SDaniel Vetter 			  bool universal_planes)
38162884cd3SKeith Packard {
38262884cd3SKeith Packard 	int o;
38362884cd3SKeith Packard 	int has_crtc = -1;
38462884cd3SKeith Packard 	int has_connector = -1;
38562884cd3SKeith Packard 	int has_plane = -1;
38662884cd3SKeith Packard 
38762884cd3SKeith Packard 	/* we want to confirm that there is at least one crtc, plane
38862884cd3SKeith Packard 	   connector object. */
38962884cd3SKeith Packard 
39062884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
39162884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
39262884cd3SKeith Packard 			has_crtc = o;
39362884cd3SKeith Packard 		}
39462884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
39562884cd3SKeith Packard 			has_connector = o;
39662884cd3SKeith Packard 
39796802905SDaniel Vetter 		if (universal_planes) {
39862884cd3SKeith Packard 			if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
39962884cd3SKeith Packard 				has_plane = o;
40062884cd3SKeith Packard 		}
40162884cd3SKeith Packard 	}
40262884cd3SKeith Packard 	if (has_crtc == -1 || has_connector == -1)
40362884cd3SKeith Packard 		return -EINVAL;
40496802905SDaniel Vetter 	if (universal_planes && has_plane == -1)
40562884cd3SKeith Packard 		return -EINVAL;
40662884cd3SKeith Packard 	return 0;
40762884cd3SKeith Packard }
40862884cd3SKeith Packard 
40962884cd3SKeith Packard static int fill_object_idr(struct drm_device *dev,
41062884cd3SKeith Packard 			   struct drm_file *lessor_priv,
41162884cd3SKeith Packard 			   struct idr *leases,
41262884cd3SKeith Packard 			   int object_count,
41362884cd3SKeith Packard 			   u32 *object_ids)
41462884cd3SKeith Packard {
41562884cd3SKeith Packard 	struct drm_mode_object **objects;
41662884cd3SKeith Packard 	u32 o;
41762884cd3SKeith Packard 	int ret;
41896802905SDaniel Vetter 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
41996802905SDaniel Vetter 
42062884cd3SKeith Packard 	objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
42162884cd3SKeith Packard 			  GFP_KERNEL);
42262884cd3SKeith Packard 	if (!objects)
42362884cd3SKeith Packard 		return -ENOMEM;
42462884cd3SKeith Packard 
42562884cd3SKeith Packard 	/* step one - get references to all the mode objects
42662884cd3SKeith Packard 	   and check for validity. */
42762884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
42862884cd3SKeith Packard 		objects[o] = drm_mode_object_find(dev, lessor_priv,
42962884cd3SKeith Packard 						  object_ids[o],
43062884cd3SKeith Packard 						  DRM_MODE_OBJECT_ANY);
43162884cd3SKeith Packard 		if (!objects[o]) {
43262884cd3SKeith Packard 			ret = -ENOENT;
43362884cd3SKeith Packard 			goto out_free_objects;
43462884cd3SKeith Packard 		}
43562884cd3SKeith Packard 
43662884cd3SKeith Packard 		if (!drm_mode_object_lease_required(objects[o]->type)) {
437a0c1af46SDaniel Vetter 			DRM_DEBUG_KMS("invalid object for lease\n");
43862884cd3SKeith Packard 			ret = -EINVAL;
43962884cd3SKeith Packard 			goto out_free_objects;
44062884cd3SKeith Packard 		}
44162884cd3SKeith Packard 	}
44262884cd3SKeith Packard 
44396802905SDaniel Vetter 	ret = validate_lease(dev, object_count, objects, universal_planes);
444a0c1af46SDaniel Vetter 	if (ret) {
445a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("lease validation failed\n");
44662884cd3SKeith Packard 		goto out_free_objects;
447a0c1af46SDaniel Vetter 	}
44862884cd3SKeith Packard 
44962884cd3SKeith Packard 	/* add their IDs to the lease request - taking into account
45062884cd3SKeith Packard 	   universal planes */
45162884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
45262884cd3SKeith Packard 		struct drm_mode_object *obj = objects[o];
45362884cd3SKeith Packard 		u32 object_id = objects[o]->id;
454948de842SSuraj Upadhyay 
45562884cd3SKeith Packard 		DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
45662884cd3SKeith Packard 
45762884cd3SKeith Packard 		/*
45862884cd3SKeith Packard 		 * We're using an IDR to hold the set of leased
45962884cd3SKeith Packard 		 * objects, but we don't need to point at the object's
460b5f06893SShayenne da Luz Moura 		 * data structure from the lease as the main object_idr
46162884cd3SKeith Packard 		 * will be used to actually find that. Instead, all we
46262884cd3SKeith Packard 		 * really want is a 'leased/not-leased' result, for
46362884cd3SKeith Packard 		 * which any non-NULL pointer will work fine.
46462884cd3SKeith Packard 		 */
46562884cd3SKeith Packard 		ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
46662884cd3SKeith Packard 		if (ret < 0) {
46762884cd3SKeith Packard 			DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
46862884cd3SKeith Packard 					object_id, ret);
46962884cd3SKeith Packard 			goto out_free_objects;
47062884cd3SKeith Packard 		}
47196802905SDaniel Vetter 		if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
47262884cd3SKeith Packard 			struct drm_crtc *crtc = obj_to_crtc(obj);
473948de842SSuraj Upadhyay 
47462884cd3SKeith Packard 			ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
47562884cd3SKeith Packard 			if (ret < 0) {
47662884cd3SKeith Packard 				DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
47762884cd3SKeith Packard 						object_id, ret);
47862884cd3SKeith Packard 				goto out_free_objects;
47962884cd3SKeith Packard 			}
48062884cd3SKeith Packard 			if (crtc->cursor) {
48162884cd3SKeith Packard 				ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
48262884cd3SKeith Packard 				if (ret < 0) {
48362884cd3SKeith Packard 					DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
48462884cd3SKeith Packard 							object_id, ret);
48562884cd3SKeith Packard 					goto out_free_objects;
48662884cd3SKeith Packard 				}
48762884cd3SKeith Packard 			}
48862884cd3SKeith Packard 		}
48962884cd3SKeith Packard 	}
49062884cd3SKeith Packard 
49162884cd3SKeith Packard 	ret = 0;
49262884cd3SKeith Packard out_free_objects:
49362884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
49462884cd3SKeith Packard 		if (objects[o])
49562884cd3SKeith Packard 			drm_mode_object_put(objects[o]);
49662884cd3SKeith Packard 	}
49762884cd3SKeith Packard 	kfree(objects);
49862884cd3SKeith Packard 	return ret;
49962884cd3SKeith Packard }
50062884cd3SKeith Packard 
50162884cd3SKeith Packard /**
50262884cd3SKeith Packard  * drm_mode_create_lease_ioctl - create a new lease
50362884cd3SKeith Packard  * @dev: the drm device
50462884cd3SKeith Packard  * @data: pointer to struct drm_mode_create_lease
5057591844cSChris Wilson  * @lessor_priv: the file being manipulated
50662884cd3SKeith Packard  *
50762884cd3SKeith Packard  * The master associated with the specified file will have a lease
50862884cd3SKeith Packard  * created containing the objects specified in the ioctl structure.
50962884cd3SKeith Packard  * A file descriptor will be allocated for that and returned to the
51062884cd3SKeith Packard  * application.
51162884cd3SKeith Packard  */
51262884cd3SKeith Packard int drm_mode_create_lease_ioctl(struct drm_device *dev,
51362884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
51462884cd3SKeith Packard {
51562884cd3SKeith Packard 	struct drm_mode_create_lease *cl = data;
51662884cd3SKeith Packard 	size_t object_count;
51762884cd3SKeith Packard 	int ret = 0;
51862884cd3SKeith Packard 	struct idr leases;
519*56f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
52062884cd3SKeith Packard 	struct drm_master *lessee = NULL;
52162884cd3SKeith Packard 	struct file *lessee_file = NULL;
52262884cd3SKeith Packard 	struct file *lessor_file = lessor_priv->filp;
52362884cd3SKeith Packard 	struct drm_file *lessee_priv;
52462884cd3SKeith Packard 	int fd = -1;
52562884cd3SKeith Packard 	uint32_t *object_ids;
52662884cd3SKeith Packard 
52762884cd3SKeith Packard 	/* Can't lease without MODESET */
52862884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
52969fdf420SChris Wilson 		return -EOPNOTSUPP;
53062884cd3SKeith Packard 
53162884cd3SKeith Packard 	/* need some objects */
532a0c1af46SDaniel Vetter 	if (cl->object_count == 0) {
533a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("no objects in lease\n");
53462884cd3SKeith Packard 		return -EINVAL;
535a0c1af46SDaniel Vetter 	}
53662884cd3SKeith Packard 
537a0c1af46SDaniel Vetter 	if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
538a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("invalid flags\n");
53962884cd3SKeith Packard 		return -EINVAL;
540a0c1af46SDaniel Vetter 	}
54162884cd3SKeith Packard 
542*56f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
543*56f0729aSDesmond Cheong Zhi Xi 	/* Do not allow sub-leases */
544*56f0729aSDesmond Cheong Zhi Xi 	if (lessor->lessor) {
545*56f0729aSDesmond Cheong Zhi Xi 		DRM_DEBUG_LEASE("recursive leasing not allowed\n");
546*56f0729aSDesmond Cheong Zhi Xi 		ret = -EINVAL;
547*56f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
548*56f0729aSDesmond Cheong Zhi Xi 	}
549*56f0729aSDesmond Cheong Zhi Xi 
55062884cd3SKeith Packard 	object_count = cl->object_count;
55162884cd3SKeith Packard 
55269ef943dSMatthew Wilcox 	object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
55369ef943dSMatthew Wilcox 			array_size(object_count, sizeof(__u32)));
554*56f0729aSDesmond Cheong Zhi Xi 	if (IS_ERR(object_ids)) {
555*56f0729aSDesmond Cheong Zhi Xi 		ret = PTR_ERR(object_ids);
556*56f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
557*56f0729aSDesmond Cheong Zhi Xi 	}
55862884cd3SKeith Packard 
55962884cd3SKeith Packard 	idr_init(&leases);
56062884cd3SKeith Packard 
56162884cd3SKeith Packard 	/* fill and validate the object idr */
56262884cd3SKeith Packard 	ret = fill_object_idr(dev, lessor_priv, &leases,
56362884cd3SKeith Packard 			      object_count, object_ids);
56462884cd3SKeith Packard 	kfree(object_ids);
56562884cd3SKeith Packard 	if (ret) {
566a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
56762884cd3SKeith Packard 		idr_destroy(&leases);
568*56f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
56962884cd3SKeith Packard 	}
57062884cd3SKeith Packard 
57162884cd3SKeith Packard 	/* Allocate a file descriptor for the lease */
57262884cd3SKeith Packard 	fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
57362884cd3SKeith Packard 	if (fd < 0) {
57462884cd3SKeith Packard 		idr_destroy(&leases);
575*56f0729aSDesmond Cheong Zhi Xi 		ret = fd;
576*56f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
57762884cd3SKeith Packard 	}
57862884cd3SKeith Packard 
57962884cd3SKeith Packard 	DRM_DEBUG_LEASE("Creating lease\n");
580b216a8e7SQiujun Huang 	/* lessee will take the ownership of leases */
58162884cd3SKeith Packard 	lessee = drm_lease_create(lessor, &leases);
58262884cd3SKeith Packard 
58362884cd3SKeith Packard 	if (IS_ERR(lessee)) {
58462884cd3SKeith Packard 		ret = PTR_ERR(lessee);
585b216a8e7SQiujun Huang 		idr_destroy(&leases);
58662884cd3SKeith Packard 		goto out_leases;
58762884cd3SKeith Packard 	}
58862884cd3SKeith Packard 
58962884cd3SKeith Packard 	/* Clone the lessor file to create a new file for us */
59062884cd3SKeith Packard 	DRM_DEBUG_LEASE("Allocating lease file\n");
59119f391ebSAl Viro 	lessee_file = file_clone_open(lessor_file);
59262884cd3SKeith Packard 	if (IS_ERR(lessee_file)) {
59362884cd3SKeith Packard 		ret = PTR_ERR(lessee_file);
59462884cd3SKeith Packard 		goto out_lessee;
59562884cd3SKeith Packard 	}
59662884cd3SKeith Packard 
59762884cd3SKeith Packard 	lessee_priv = lessee_file->private_data;
59862884cd3SKeith Packard 	/* Change the file to a master one */
59962884cd3SKeith Packard 	drm_master_put(&lessee_priv->master);
60062884cd3SKeith Packard 	lessee_priv->master = lessee;
60162884cd3SKeith Packard 	lessee_priv->is_master = 1;
60262884cd3SKeith Packard 	lessee_priv->authenticated = 1;
60362884cd3SKeith Packard 
60462884cd3SKeith Packard 	/* Pass fd back to userspace */
60562884cd3SKeith Packard 	DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
60662884cd3SKeith Packard 	cl->fd = fd;
60762884cd3SKeith Packard 	cl->lessee_id = lessee->lessee_id;
60862884cd3SKeith Packard 
60912d43debSJann Horn 	/* Hook up the fd */
61012d43debSJann Horn 	fd_install(fd, lessee_file);
61112d43debSJann Horn 
612*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
61362884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
61462884cd3SKeith Packard 	return 0;
61562884cd3SKeith Packard 
61662884cd3SKeith Packard out_lessee:
61762884cd3SKeith Packard 	drm_master_put(&lessee);
61862884cd3SKeith Packard 
61962884cd3SKeith Packard out_leases:
62062884cd3SKeith Packard 	put_unused_fd(fd);
62162884cd3SKeith Packard 
622*56f0729aSDesmond Cheong Zhi Xi out_lessor:
623*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
62462884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
62562884cd3SKeith Packard 	return ret;
62662884cd3SKeith Packard }
62762884cd3SKeith Packard 
62862884cd3SKeith Packard /**
62962884cd3SKeith Packard  * drm_mode_list_lessees_ioctl - list lessee ids
63062884cd3SKeith Packard  * @dev: the drm device
63162884cd3SKeith Packard  * @data: pointer to struct drm_mode_list_lessees
63262884cd3SKeith Packard  * @lessor_priv: the file being manipulated
63362884cd3SKeith Packard  *
63462884cd3SKeith Packard  * Starting from the master associated with the specified file,
63562884cd3SKeith Packard  * the master with the provided lessee_id is found, and then
63662884cd3SKeith Packard  * an array of lessee ids associated with leases from that master
63762884cd3SKeith Packard  * are returned.
63862884cd3SKeith Packard  */
63962884cd3SKeith Packard 
64062884cd3SKeith Packard int drm_mode_list_lessees_ioctl(struct drm_device *dev,
64162884cd3SKeith Packard 			       void *data, struct drm_file *lessor_priv)
64262884cd3SKeith Packard {
64362884cd3SKeith Packard 	struct drm_mode_list_lessees *arg = data;
64462884cd3SKeith Packard 	__u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
64562884cd3SKeith Packard 	__u32 count_lessees = arg->count_lessees;
646*56f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor, *lessee;
64762884cd3SKeith Packard 	int count;
64862884cd3SKeith Packard 	int ret = 0;
64962884cd3SKeith Packard 
65062884cd3SKeith Packard 	if (arg->pad)
65162884cd3SKeith Packard 		return -EINVAL;
65262884cd3SKeith Packard 
65362884cd3SKeith Packard 	/* Can't lease without MODESET */
65462884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
65569fdf420SChris Wilson 		return -EOPNOTSUPP;
65662884cd3SKeith Packard 
657*56f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
65862884cd3SKeith Packard 	DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
65962884cd3SKeith Packard 
66062884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
66162884cd3SKeith Packard 
66262884cd3SKeith Packard 	count = 0;
66362884cd3SKeith Packard 	drm_for_each_lessee(lessee, lessor) {
66462884cd3SKeith Packard 		/* Only list un-revoked leases */
66562884cd3SKeith Packard 		if (!idr_is_empty(&lessee->leases)) {
66662884cd3SKeith Packard 			if (count_lessees > count) {
66762884cd3SKeith Packard 				DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
66862884cd3SKeith Packard 				ret = put_user(lessee->lessee_id, lessee_ids + count);
66962884cd3SKeith Packard 				if (ret)
67062884cd3SKeith Packard 					break;
67162884cd3SKeith Packard 			}
67262884cd3SKeith Packard 			count++;
67362884cd3SKeith Packard 		}
67462884cd3SKeith Packard 	}
67562884cd3SKeith Packard 
67662884cd3SKeith Packard 	DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
67762884cd3SKeith Packard 	if (ret == 0)
67862884cd3SKeith Packard 		arg->count_lessees = count;
67962884cd3SKeith Packard 
68062884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
681*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
68262884cd3SKeith Packard 
68362884cd3SKeith Packard 	return ret;
68462884cd3SKeith Packard }
68562884cd3SKeith Packard 
68662884cd3SKeith Packard /**
68762884cd3SKeith Packard  * drm_mode_get_lease_ioctl - list leased objects
68862884cd3SKeith Packard  * @dev: the drm device
68962884cd3SKeith Packard  * @data: pointer to struct drm_mode_get_lease
6907591844cSChris Wilson  * @lessee_priv: the file being manipulated
69162884cd3SKeith Packard  *
69262884cd3SKeith Packard  * Return the list of leased objects for the specified lessee
69362884cd3SKeith Packard  */
69462884cd3SKeith Packard 
69562884cd3SKeith Packard int drm_mode_get_lease_ioctl(struct drm_device *dev,
69662884cd3SKeith Packard 			     void *data, struct drm_file *lessee_priv)
69762884cd3SKeith Packard {
69862884cd3SKeith Packard 	struct drm_mode_get_lease *arg = data;
69962884cd3SKeith Packard 	__u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
70062884cd3SKeith Packard 	__u32 count_objects = arg->count_objects;
701*56f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessee;
70262884cd3SKeith Packard 	struct idr *object_idr;
70362884cd3SKeith Packard 	int count;
70462884cd3SKeith Packard 	void *entry;
70562884cd3SKeith Packard 	int object;
70662884cd3SKeith Packard 	int ret = 0;
70762884cd3SKeith Packard 
70862884cd3SKeith Packard 	if (arg->pad)
70962884cd3SKeith Packard 		return -EINVAL;
71062884cd3SKeith Packard 
71162884cd3SKeith Packard 	/* Can't lease without MODESET */
71262884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
71369fdf420SChris Wilson 		return -EOPNOTSUPP;
71462884cd3SKeith Packard 
715*56f0729aSDesmond Cheong Zhi Xi 	lessee = drm_file_get_master(lessee_priv);
71662884cd3SKeith Packard 	DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
71762884cd3SKeith Packard 
71862884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
71962884cd3SKeith Packard 
72062884cd3SKeith Packard 	if (lessee->lessor == NULL)
72162884cd3SKeith Packard 		/* owner can use all objects */
722b5f06893SShayenne da Luz Moura 		object_idr = &lessee->dev->mode_config.object_idr;
72362884cd3SKeith Packard 	else
72462884cd3SKeith Packard 		/* lessee can only use allowed object */
72562884cd3SKeith Packard 		object_idr = &lessee->leases;
72662884cd3SKeith Packard 
72762884cd3SKeith Packard 	count = 0;
72862884cd3SKeith Packard 	idr_for_each_entry(object_idr, entry, object) {
72962884cd3SKeith Packard 		if (count_objects > count) {
73062884cd3SKeith Packard 			DRM_DEBUG_LEASE("adding object %d\n", object);
73162884cd3SKeith Packard 			ret = put_user(object, object_ids + count);
73262884cd3SKeith Packard 			if (ret)
73362884cd3SKeith Packard 				break;
73462884cd3SKeith Packard 		}
73562884cd3SKeith Packard 		count++;
73662884cd3SKeith Packard 	}
73762884cd3SKeith Packard 
73862884cd3SKeith Packard 	DRM_DEBUG("lease holds %d objects\n", count);
73962884cd3SKeith Packard 	if (ret == 0)
74062884cd3SKeith Packard 		arg->count_objects = count;
74162884cd3SKeith Packard 
74262884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
743*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessee);
74462884cd3SKeith Packard 
74562884cd3SKeith Packard 	return ret;
74662884cd3SKeith Packard }
74762884cd3SKeith Packard 
74862884cd3SKeith Packard /**
74962884cd3SKeith Packard  * drm_mode_revoke_lease_ioctl - revoke lease
75062884cd3SKeith Packard  * @dev: the drm device
75162884cd3SKeith Packard  * @data: pointer to struct drm_mode_revoke_lease
7527591844cSChris Wilson  * @lessor_priv: the file being manipulated
75362884cd3SKeith Packard  *
75462884cd3SKeith Packard  * This removes all of the objects from the lease without
75562884cd3SKeith Packard  * actually getting rid of the lease itself; that way all
75662884cd3SKeith Packard  * references to it still work correctly
75762884cd3SKeith Packard  */
75862884cd3SKeith Packard int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
75962884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
76062884cd3SKeith Packard {
76162884cd3SKeith Packard 	struct drm_mode_revoke_lease *arg = data;
762*56f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
76362884cd3SKeith Packard 	struct drm_master *lessee;
76462884cd3SKeith Packard 	int ret = 0;
76562884cd3SKeith Packard 
76662884cd3SKeith Packard 	DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
76762884cd3SKeith Packard 
76862884cd3SKeith Packard 	/* Can't lease without MODESET */
76962884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
77069fdf420SChris Wilson 		return -EOPNOTSUPP;
77162884cd3SKeith Packard 
772*56f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
77362884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
77462884cd3SKeith Packard 
77562884cd3SKeith Packard 	lessee = _drm_find_lessee(lessor, arg->lessee_id);
77662884cd3SKeith Packard 
77762884cd3SKeith Packard 	/* No such lessee */
77862884cd3SKeith Packard 	if (!lessee) {
77962884cd3SKeith Packard 		ret = -ENOENT;
78062884cd3SKeith Packard 		goto fail;
78162884cd3SKeith Packard 	}
78262884cd3SKeith Packard 
78362884cd3SKeith Packard 	/* Lease is not held by lessor */
78462884cd3SKeith Packard 	if (lessee->lessor != lessor) {
78562884cd3SKeith Packard 		ret = -EACCES;
78662884cd3SKeith Packard 		goto fail;
78762884cd3SKeith Packard 	}
78862884cd3SKeith Packard 
78962884cd3SKeith Packard 	_drm_lease_revoke(lessee);
79062884cd3SKeith Packard 
79162884cd3SKeith Packard fail:
79262884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
793*56f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
79462884cd3SKeith Packard 
79562884cd3SKeith Packard 	return ret;
79662884cd3SKeith Packard }
797