drm_auth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Author Rickard E. (Rik) Faith <faith@valinux.com>
  9. * Author Gareth Hughes <gareth@valinux.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the next
  19. * paragraph) shall be included in all copies or substantial portions of the
  20. * Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  26. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  27. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. * OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <drm/drmP.h>
  31. #include "drm_internal.h"
  32. #include "drm_legacy.h"
  33. #include <drm/drm_lease.h>
  34. /**
  35. * DOC: master and authentication
  36. *
  37. * &struct drm_master is used to track groups of clients with open
  38. * primary/legacy device nodes. For every &struct drm_file which has had at
  39. * least once successfully became the device master (either through the
  40. * SET_MASTER IOCTL, or implicitly through opening the primary device node when
  41. * no one else is the current master that time) there exists one &drm_master.
  42. * This is noted in &drm_file.is_master. All other clients have just a pointer
  43. * to the &drm_master they are associated with.
  44. *
  45. * In addition only one &drm_master can be the current master for a &drm_device.
  46. * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
  47. * implicitly through closing/openeing the primary device node. See also
  48. * drm_is_current_master().
  49. *
  50. * Clients can authenticate against the current master (if it matches their own)
  51. * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
  52. * this allows controlled access to the device for an entire group of mutually
  53. * trusted clients.
  54. */
  55. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  56. {
  57. struct drm_auth *auth = data;
  58. int ret = 0;
  59. mutex_lock(&dev->master_mutex);
  60. if (!file_priv->magic) {
  61. ret = idr_alloc(&file_priv->master->magic_map, file_priv,
  62. 1, 0, GFP_KERNEL);
  63. if (ret >= 0)
  64. file_priv->magic = ret;
  65. }
  66. auth->magic = file_priv->magic;
  67. mutex_unlock(&dev->master_mutex);
  68. DRM_DEBUG("%u\n", auth->magic);
  69. return ret < 0 ? ret : 0;
  70. }
  71. int drm_authmagic(struct drm_device *dev, void *data,
  72. struct drm_file *file_priv)
  73. {
  74. struct drm_auth *auth = data;
  75. struct drm_file *file;
  76. DRM_DEBUG("%u\n", auth->magic);
  77. mutex_lock(&dev->master_mutex);
  78. file = idr_find(&file_priv->master->magic_map, auth->magic);
  79. if (file) {
  80. file->authenticated = 1;
  81. idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
  82. }
  83. mutex_unlock(&dev->master_mutex);
  84. return file ? 0 : -EINVAL;
  85. }
  86. struct drm_master *drm_master_create(struct drm_device *dev)
  87. {
  88. struct drm_master *master;
  89. master = kzalloc(sizeof(*master), GFP_KERNEL);
  90. if (!master)
  91. return NULL;
  92. kref_init(&master->refcount);
  93. spin_lock_init(&master->lock.spinlock);
  94. init_waitqueue_head(&master->lock.lock_queue);
  95. idr_init(&master->magic_map);
  96. master->dev = dev;
  97. /* initialize the tree of output resource lessees */
  98. master->lessor = NULL;
  99. master->lessee_id = 0;
  100. INIT_LIST_HEAD(&master->lessees);
  101. INIT_LIST_HEAD(&master->lessee_list);
  102. idr_init(&master->leases);
  103. idr_init(&master->lessee_idr);
  104. return master;
  105. }
  106. static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
  107. bool new_master)
  108. {
  109. int ret = 0;
  110. dev->master = drm_master_get(fpriv->master);
  111. if (dev->driver->master_set) {
  112. ret = dev->driver->master_set(dev, fpriv, new_master);
  113. if (unlikely(ret != 0)) {
  114. drm_master_put(&dev->master);
  115. }
  116. }
  117. return ret;
  118. }
  119. static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  120. {
  121. struct drm_master *old_master;
  122. int ret;
  123. lockdep_assert_held_once(&dev->master_mutex);
  124. old_master = fpriv->master;
  125. fpriv->master = drm_master_create(dev);
  126. if (!fpriv->master) {
  127. fpriv->master = old_master;
  128. return -ENOMEM;
  129. }
  130. if (dev->driver->master_create) {
  131. ret = dev->driver->master_create(dev, fpriv->master);
  132. if (ret)
  133. goto out_err;
  134. }
  135. fpriv->is_master = 1;
  136. fpriv->authenticated = 1;
  137. ret = drm_set_master(dev, fpriv, true);
  138. if (ret)
  139. goto out_err;
  140. if (old_master)
  141. drm_master_put(&old_master);
  142. return 0;
  143. out_err:
  144. /* drop references and restore old master on failure */
  145. drm_master_put(&fpriv->master);
  146. fpriv->master = old_master;
  147. return ret;
  148. }
  149. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  150. struct drm_file *file_priv)
  151. {
  152. int ret = 0;
  153. mutex_lock(&dev->master_mutex);
  154. if (drm_is_current_master(file_priv))
  155. goto out_unlock;
  156. if (dev->master) {
  157. ret = -EINVAL;
  158. goto out_unlock;
  159. }
  160. if (!file_priv->master) {
  161. ret = -EINVAL;
  162. goto out_unlock;
  163. }
  164. if (!file_priv->is_master) {
  165. ret = drm_new_set_master(dev, file_priv);
  166. goto out_unlock;
  167. }
  168. if (file_priv->master->lessor != NULL) {
  169. DRM_DEBUG_LEASE("Attempt to set lessee %d as master\n", file_priv->master->lessee_id);
  170. ret = -EINVAL;
  171. goto out_unlock;
  172. }
  173. ret = drm_set_master(dev, file_priv, false);
  174. out_unlock:
  175. mutex_unlock(&dev->master_mutex);
  176. return ret;
  177. }
  178. static void drm_drop_master(struct drm_device *dev,
  179. struct drm_file *fpriv)
  180. {
  181. if (dev->driver->master_drop)
  182. dev->driver->master_drop(dev, fpriv);
  183. drm_master_put(&dev->master);
  184. }
  185. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  186. struct drm_file *file_priv)
  187. {
  188. int ret = -EINVAL;
  189. mutex_lock(&dev->master_mutex);
  190. if (!drm_is_current_master(file_priv))
  191. goto out_unlock;
  192. if (!dev->master)
  193. goto out_unlock;
  194. if (file_priv->master->lessor != NULL) {
  195. DRM_DEBUG_LEASE("Attempt to drop lessee %d as master\n", file_priv->master->lessee_id);
  196. ret = -EINVAL;
  197. goto out_unlock;
  198. }
  199. ret = 0;
  200. drm_drop_master(dev, file_priv);
  201. out_unlock:
  202. mutex_unlock(&dev->master_mutex);
  203. return ret;
  204. }
  205. int drm_master_open(struct drm_file *file_priv)
  206. {
  207. struct drm_device *dev = file_priv->minor->dev;
  208. int ret = 0;
  209. /* if there is no current master make this fd it, but do not create
  210. * any master object for render clients */
  211. mutex_lock(&dev->master_mutex);
  212. if (!dev->master)
  213. ret = drm_new_set_master(dev, file_priv);
  214. else
  215. file_priv->master = drm_master_get(dev->master);
  216. mutex_unlock(&dev->master_mutex);
  217. return ret;
  218. }
  219. void drm_master_release(struct drm_file *file_priv)
  220. {
  221. struct drm_device *dev = file_priv->minor->dev;
  222. struct drm_master *master = file_priv->master;
  223. mutex_lock(&dev->master_mutex);
  224. if (file_priv->magic)
  225. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  226. if (!drm_is_current_master(file_priv))
  227. goto out;
  228. if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
  229. /*
  230. * Since the master is disappearing, so is the
  231. * possibility to lock.
  232. */
  233. mutex_lock(&dev->struct_mutex);
  234. if (master->lock.hw_lock) {
  235. if (dev->sigdata.lock == master->lock.hw_lock)
  236. dev->sigdata.lock = NULL;
  237. master->lock.hw_lock = NULL;
  238. master->lock.file_priv = NULL;
  239. wake_up_interruptible_all(&master->lock.lock_queue);
  240. }
  241. mutex_unlock(&dev->struct_mutex);
  242. }
  243. if (dev->master == file_priv->master)
  244. drm_drop_master(dev, file_priv);
  245. out:
  246. if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
  247. /* Revoke any leases held by this or lessees, but only if
  248. * this is the "real" master
  249. */
  250. drm_lease_revoke(master);
  251. }
  252. /* drop the master reference held by the file priv */
  253. if (file_priv->master)
  254. drm_master_put(&file_priv->master);
  255. mutex_unlock(&dev->master_mutex);
  256. }
  257. /**
  258. * drm_is_current_master - checks whether @priv is the current master
  259. * @fpriv: DRM file private
  260. *
  261. * Checks whether @fpriv is current master on its device. This decides whether a
  262. * client is allowed to run DRM_MASTER IOCTLs.
  263. *
  264. * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
  265. * - the current master is assumed to own the non-shareable display hardware.
  266. */
  267. bool drm_is_current_master(struct drm_file *fpriv)
  268. {
  269. return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
  270. }
  271. EXPORT_SYMBOL(drm_is_current_master);
  272. /**
  273. * drm_master_get - reference a master pointer
  274. * @master: &struct drm_master
  275. *
  276. * Increments the reference count of @master and returns a pointer to @master.
  277. */
  278. struct drm_master *drm_master_get(struct drm_master *master)
  279. {
  280. kref_get(&master->refcount);
  281. return master;
  282. }
  283. EXPORT_SYMBOL(drm_master_get);
  284. static void drm_master_destroy(struct kref *kref)
  285. {
  286. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  287. struct drm_device *dev = master->dev;
  288. if (drm_core_check_feature(dev, DRIVER_MODESET))
  289. drm_lease_destroy(master);
  290. if (dev->driver->master_destroy)
  291. dev->driver->master_destroy(dev, master);
  292. drm_legacy_master_rmmaps(dev, master);
  293. idr_destroy(&master->magic_map);
  294. idr_destroy(&master->leases);
  295. idr_destroy(&master->lessee_idr);
  296. kfree(master->unique);
  297. kfree(master);
  298. }
  299. /**
  300. * drm_master_put - unreference and clear a master pointer
  301. * @master: pointer to a pointer of &struct drm_master
  302. *
  303. * This decrements the &drm_master behind @master and sets it to NULL.
  304. */
  305. void drm_master_put(struct drm_master **master)
  306. {
  307. kref_put(&(*master)->refcount, drm_master_destroy);
  308. *master = NULL;
  309. }
  310. EXPORT_SYMBOL(drm_master_put);