xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/drm_auth.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5*4882a593Smuzhiyun  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6*4882a593Smuzhiyun  * All Rights Reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Author Rickard E. (Rik) Faith <faith@valinux.com>
9*4882a593Smuzhiyun  * Author Gareth Hughes <gareth@valinux.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
12*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
13*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
14*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
16*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
19*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
20*4882a593Smuzhiyun  * Software.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25*4882a593Smuzhiyun  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26*4882a593Smuzhiyun  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27*4882a593Smuzhiyun  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28*4882a593Smuzhiyun  * OTHER DEALINGS IN THE SOFTWARE.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/slab.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <drm/drm_auth.h>
34*4882a593Smuzhiyun #include <drm/drm_drv.h>
35*4882a593Smuzhiyun #include <drm/drm_file.h>
36*4882a593Smuzhiyun #include <drm/drm_lease.h>
37*4882a593Smuzhiyun #include <drm/drm_print.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include "drm_internal.h"
40*4882a593Smuzhiyun #include "drm_legacy.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun  * DOC: master and authentication
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * &struct drm_master is used to track groups of clients with open
46*4882a593Smuzhiyun  * primary/legacy device nodes. For every &struct drm_file which has had at
47*4882a593Smuzhiyun  * least once successfully became the device master (either through the
48*4882a593Smuzhiyun  * SET_MASTER IOCTL, or implicitly through opening the primary device node when
49*4882a593Smuzhiyun  * no one else is the current master that time) there exists one &drm_master.
50*4882a593Smuzhiyun  * This is noted in &drm_file.is_master. All other clients have just a pointer
51*4882a593Smuzhiyun  * to the &drm_master they are associated with.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * In addition only one &drm_master can be the current master for a &drm_device.
54*4882a593Smuzhiyun  * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
55*4882a593Smuzhiyun  * implicitly through closing/openeing the primary device node. See also
56*4882a593Smuzhiyun  * drm_is_current_master().
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * Clients can authenticate against the current master (if it matches their own)
59*4882a593Smuzhiyun  * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
60*4882a593Smuzhiyun  * this allows controlled access to the device for an entire group of mutually
61*4882a593Smuzhiyun  * trusted clients.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
drm_getmagic(struct drm_device * dev,void * data,struct drm_file * file_priv)64*4882a593Smuzhiyun int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct drm_auth *auth = data;
67*4882a593Smuzhiyun 	int ret = 0;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
70*4882a593Smuzhiyun 	if (!file_priv->magic) {
71*4882a593Smuzhiyun 		ret = idr_alloc(&file_priv->master->magic_map, file_priv,
72*4882a593Smuzhiyun 				1, 0, GFP_KERNEL);
73*4882a593Smuzhiyun 		if (ret >= 0)
74*4882a593Smuzhiyun 			file_priv->magic = ret;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 	auth->magic = file_priv->magic;
77*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	DRM_DEBUG("%u\n", auth->magic);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return ret < 0 ? ret : 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
drm_authmagic(struct drm_device * dev,void * data,struct drm_file * file_priv)84*4882a593Smuzhiyun int drm_authmagic(struct drm_device *dev, void *data,
85*4882a593Smuzhiyun 		  struct drm_file *file_priv)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct drm_auth *auth = data;
88*4882a593Smuzhiyun 	struct drm_file *file;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	DRM_DEBUG("%u\n", auth->magic);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
93*4882a593Smuzhiyun 	file = idr_find(&file_priv->master->magic_map, auth->magic);
94*4882a593Smuzhiyun 	if (file) {
95*4882a593Smuzhiyun 		file->authenticated = 1;
96*4882a593Smuzhiyun 		idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	return file ? 0 : -EINVAL;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
drm_master_create(struct drm_device * dev)103*4882a593Smuzhiyun struct drm_master *drm_master_create(struct drm_device *dev)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	struct drm_master *master;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	master = kzalloc(sizeof(*master), GFP_KERNEL);
108*4882a593Smuzhiyun 	if (!master)
109*4882a593Smuzhiyun 		return NULL;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	kref_init(&master->refcount);
112*4882a593Smuzhiyun 	drm_master_legacy_init(master);
113*4882a593Smuzhiyun 	idr_init(&master->magic_map);
114*4882a593Smuzhiyun 	master->dev = dev;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* initialize the tree of output resource lessees */
117*4882a593Smuzhiyun 	INIT_LIST_HEAD(&master->lessees);
118*4882a593Smuzhiyun 	INIT_LIST_HEAD(&master->lessee_list);
119*4882a593Smuzhiyun 	idr_init(&master->leases);
120*4882a593Smuzhiyun 	idr_init(&master->lessee_idr);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	return master;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
drm_set_master(struct drm_device * dev,struct drm_file * fpriv,bool new_master)125*4882a593Smuzhiyun static void drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
126*4882a593Smuzhiyun 			   bool new_master)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	dev->master = drm_master_get(fpriv->master);
129*4882a593Smuzhiyun 	if (dev->driver->master_set)
130*4882a593Smuzhiyun 		dev->driver->master_set(dev, fpriv, new_master);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	fpriv->was_master = true;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
drm_new_set_master(struct drm_device * dev,struct drm_file * fpriv)135*4882a593Smuzhiyun static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct drm_master *old_master;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	lockdep_assert_held_once(&dev->master_mutex);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	WARN_ON(fpriv->is_master);
142*4882a593Smuzhiyun 	old_master = fpriv->master;
143*4882a593Smuzhiyun 	fpriv->master = drm_master_create(dev);
144*4882a593Smuzhiyun 	if (!fpriv->master) {
145*4882a593Smuzhiyun 		fpriv->master = old_master;
146*4882a593Smuzhiyun 		return -ENOMEM;
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	fpriv->is_master = 1;
150*4882a593Smuzhiyun 	fpriv->authenticated = 1;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	drm_set_master(dev, fpriv, true);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (old_master)
155*4882a593Smuzhiyun 		drm_master_put(&old_master);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * In the olden days the SET/DROP_MASTER ioctls used to return EACCES when
162*4882a593Smuzhiyun  * CAP_SYS_ADMIN was not set. This was used to prevent rogue applications
163*4882a593Smuzhiyun  * from becoming master and/or failing to release it.
164*4882a593Smuzhiyun  *
165*4882a593Smuzhiyun  * At the same time, the first client (for a given VT) is _always_ master.
166*4882a593Smuzhiyun  * Thus in order for the ioctls to succeed, one had to _explicitly_ run the
167*4882a593Smuzhiyun  * application as root or flip the setuid bit.
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * If the CAP_SYS_ADMIN was missing, no other client could become master...
170*4882a593Smuzhiyun  * EVER :-( Leading to a) the graphics session dying badly or b) a completely
171*4882a593Smuzhiyun  * locked session.
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  * As some point systemd-logind was introduced to orchestrate and delegate
175*4882a593Smuzhiyun  * master as applicable. It does so by opening the fd and passing it to users
176*4882a593Smuzhiyun  * while in itself logind a) does the set/drop master per users' request and
177*4882a593Smuzhiyun  * b)  * implicitly drops master on VT switch.
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * Even though logind looks like the future, there are a few issues:
180*4882a593Smuzhiyun  *  - some platforms don't have equivalent (Android, CrOS, some BSDs) so
181*4882a593Smuzhiyun  * root is required _solely_ for SET/DROP MASTER.
182*4882a593Smuzhiyun  *  - applications may not be updated to use it,
183*4882a593Smuzhiyun  *  - any client which fails to drop master* can DoS the application using
184*4882a593Smuzhiyun  * logind, to a varying degree.
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * * Either due missing CAP_SYS_ADMIN or simply not calling DROP_MASTER.
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * Here we implement the next best thing:
190*4882a593Smuzhiyun  *  - ensure the logind style of fd passing works unchanged, and
191*4882a593Smuzhiyun  *  - allow a client to drop/set master, iff it is/was master at a given point
192*4882a593Smuzhiyun  * in time.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * Note: DROP_MASTER cannot be free for all, as an arbitrator user could:
195*4882a593Smuzhiyun  *  - DoS/crash the arbitrator - details would be implementation specific
196*4882a593Smuzhiyun  *  - open the node, become master implicitly and cause issues
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * As a result this fixes the following when using root-less build w/o logind
199*4882a593Smuzhiyun  * - startx
200*4882a593Smuzhiyun  * - weston
201*4882a593Smuzhiyun  * - various compositors based on wlroots
202*4882a593Smuzhiyun  */
203*4882a593Smuzhiyun static int
drm_master_check_perm(struct drm_device * dev,struct drm_file * file_priv)204*4882a593Smuzhiyun drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	if (file_priv->pid == task_pid(current) && file_priv->was_master)
207*4882a593Smuzhiyun 		return 0;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
210*4882a593Smuzhiyun 		return -EACCES;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
drm_setmaster_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)215*4882a593Smuzhiyun int drm_setmaster_ioctl(struct drm_device *dev, void *data,
216*4882a593Smuzhiyun 			struct drm_file *file_priv)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int ret;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ret = drm_master_check_perm(dev, file_priv);
223*4882a593Smuzhiyun 	if (ret)
224*4882a593Smuzhiyun 		goto out_unlock;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (drm_is_current_master(file_priv))
227*4882a593Smuzhiyun 		goto out_unlock;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	if (dev->master) {
230*4882a593Smuzhiyun 		ret = -EBUSY;
231*4882a593Smuzhiyun 		goto out_unlock;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (!file_priv->master) {
235*4882a593Smuzhiyun 		ret = -EINVAL;
236*4882a593Smuzhiyun 		goto out_unlock;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	if (!file_priv->is_master) {
240*4882a593Smuzhiyun 		ret = drm_new_set_master(dev, file_priv);
241*4882a593Smuzhiyun 		goto out_unlock;
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (file_priv->master->lessor != NULL) {
245*4882a593Smuzhiyun 		DRM_DEBUG_LEASE("Attempt to set lessee %d as master\n", file_priv->master->lessee_id);
246*4882a593Smuzhiyun 		ret = -EINVAL;
247*4882a593Smuzhiyun 		goto out_unlock;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	drm_set_master(dev, file_priv, false);
251*4882a593Smuzhiyun out_unlock:
252*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
253*4882a593Smuzhiyun 	return ret;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
drm_drop_master(struct drm_device * dev,struct drm_file * fpriv)256*4882a593Smuzhiyun static void drm_drop_master(struct drm_device *dev,
257*4882a593Smuzhiyun 			    struct drm_file *fpriv)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	if (dev->driver->master_drop)
260*4882a593Smuzhiyun 		dev->driver->master_drop(dev, fpriv);
261*4882a593Smuzhiyun 	drm_master_put(&dev->master);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
drm_dropmaster_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)264*4882a593Smuzhiyun int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
265*4882a593Smuzhiyun 			 struct drm_file *file_priv)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	int ret;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	ret = drm_master_check_perm(dev, file_priv);
272*4882a593Smuzhiyun 	if (ret)
273*4882a593Smuzhiyun 		goto out_unlock;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (!drm_is_current_master(file_priv)) {
276*4882a593Smuzhiyun 		ret = -EINVAL;
277*4882a593Smuzhiyun 		goto out_unlock;
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (!dev->master) {
281*4882a593Smuzhiyun 		ret = -EINVAL;
282*4882a593Smuzhiyun 		goto out_unlock;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	if (file_priv->master->lessor != NULL) {
286*4882a593Smuzhiyun 		DRM_DEBUG_LEASE("Attempt to drop lessee %d as master\n", file_priv->master->lessee_id);
287*4882a593Smuzhiyun 		ret = -EINVAL;
288*4882a593Smuzhiyun 		goto out_unlock;
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	drm_drop_master(dev, file_priv);
292*4882a593Smuzhiyun out_unlock:
293*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
294*4882a593Smuzhiyun 	return ret;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
drm_master_open(struct drm_file * file_priv)297*4882a593Smuzhiyun int drm_master_open(struct drm_file *file_priv)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct drm_device *dev = file_priv->minor->dev;
300*4882a593Smuzhiyun 	int ret = 0;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* if there is no current master make this fd it, but do not create
303*4882a593Smuzhiyun 	 * any master object for render clients */
304*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
305*4882a593Smuzhiyun 	if (!dev->master)
306*4882a593Smuzhiyun 		ret = drm_new_set_master(dev, file_priv);
307*4882a593Smuzhiyun 	else
308*4882a593Smuzhiyun 		file_priv->master = drm_master_get(dev->master);
309*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	return ret;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
drm_master_release(struct drm_file * file_priv)314*4882a593Smuzhiyun void drm_master_release(struct drm_file *file_priv)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct drm_device *dev = file_priv->minor->dev;
317*4882a593Smuzhiyun 	struct drm_master *master;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
320*4882a593Smuzhiyun 	master = file_priv->master;
321*4882a593Smuzhiyun 	if (file_priv->magic)
322*4882a593Smuzhiyun 		idr_remove(&file_priv->master->magic_map, file_priv->magic);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (!drm_is_current_master(file_priv))
325*4882a593Smuzhiyun 		goto out;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	drm_legacy_lock_master_cleanup(dev, master);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (dev->master == file_priv->master)
330*4882a593Smuzhiyun 		drm_drop_master(dev, file_priv);
331*4882a593Smuzhiyun out:
332*4882a593Smuzhiyun 	if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
333*4882a593Smuzhiyun 		/* Revoke any leases held by this or lessees, but only if
334*4882a593Smuzhiyun 		 * this is the "real" master
335*4882a593Smuzhiyun 		 */
336*4882a593Smuzhiyun 		drm_lease_revoke(master);
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* drop the master reference held by the file priv */
340*4882a593Smuzhiyun 	if (file_priv->master)
341*4882a593Smuzhiyun 		drm_master_put(&file_priv->master);
342*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun /**
346*4882a593Smuzhiyun  * drm_is_current_master - checks whether @priv is the current master
347*4882a593Smuzhiyun  * @fpriv: DRM file private
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * Checks whether @fpriv is current master on its device. This decides whether a
350*4882a593Smuzhiyun  * client is allowed to run DRM_MASTER IOCTLs.
351*4882a593Smuzhiyun  *
352*4882a593Smuzhiyun  * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
353*4882a593Smuzhiyun  * - the current master is assumed to own the non-shareable display hardware.
354*4882a593Smuzhiyun  */
drm_is_current_master(struct drm_file * fpriv)355*4882a593Smuzhiyun bool drm_is_current_master(struct drm_file *fpriv)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun EXPORT_SYMBOL(drm_is_current_master);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /**
362*4882a593Smuzhiyun  * drm_master_get - reference a master pointer
363*4882a593Smuzhiyun  * @master: &struct drm_master
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  * Increments the reference count of @master and returns a pointer to @master.
366*4882a593Smuzhiyun  */
drm_master_get(struct drm_master * master)367*4882a593Smuzhiyun struct drm_master *drm_master_get(struct drm_master *master)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	kref_get(&master->refcount);
370*4882a593Smuzhiyun 	return master;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun EXPORT_SYMBOL(drm_master_get);
373*4882a593Smuzhiyun 
drm_master_destroy(struct kref * kref)374*4882a593Smuzhiyun static void drm_master_destroy(struct kref *kref)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	struct drm_master *master = container_of(kref, struct drm_master, refcount);
377*4882a593Smuzhiyun 	struct drm_device *dev = master->dev;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	if (drm_core_check_feature(dev, DRIVER_MODESET))
380*4882a593Smuzhiyun 		drm_lease_destroy(master);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	drm_legacy_master_rmmaps(dev, master);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	idr_destroy(&master->magic_map);
385*4882a593Smuzhiyun 	idr_destroy(&master->leases);
386*4882a593Smuzhiyun 	idr_destroy(&master->lessee_idr);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	kfree(master->unique);
389*4882a593Smuzhiyun 	kfree(master);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun /**
393*4882a593Smuzhiyun  * drm_master_put - unreference and clear a master pointer
394*4882a593Smuzhiyun  * @master: pointer to a pointer of &struct drm_master
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  * This decrements the &drm_master behind @master and sets it to NULL.
397*4882a593Smuzhiyun  */
drm_master_put(struct drm_master ** master)398*4882a593Smuzhiyun void drm_master_put(struct drm_master **master)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun 	kref_put(&(*master)->refcount, drm_master_destroy);
401*4882a593Smuzhiyun 	*master = NULL;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun EXPORT_SYMBOL(drm_master_put);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /* Used by drm_client and drm_fb_helper */
drm_master_internal_acquire(struct drm_device * dev)406*4882a593Smuzhiyun bool drm_master_internal_acquire(struct drm_device *dev)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	mutex_lock(&dev->master_mutex);
409*4882a593Smuzhiyun 	if (dev->master) {
410*4882a593Smuzhiyun 		mutex_unlock(&dev->master_mutex);
411*4882a593Smuzhiyun 		return false;
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return true;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun EXPORT_SYMBOL(drm_master_internal_acquire);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /* Used by drm_client and drm_fb_helper */
drm_master_internal_release(struct drm_device * dev)419*4882a593Smuzhiyun void drm_master_internal_release(struct drm_device *dev)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	mutex_unlock(&dev->master_mutex);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun EXPORT_SYMBOL(drm_master_internal_release);
424