drm_vma_manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  3. * Copyright (c) 2012 David Airlie <airlied@linux.ie>
  4. * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <drm/drmP.h>
  25. #include <drm/drm_mm.h>
  26. #include <drm/drm_vma_manager.h>
  27. #include <linux/fs.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/rbtree.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/types.h>
  34. /**
  35. * DOC: vma offset manager
  36. *
  37. * The vma-manager is responsible to map arbitrary driver-dependent memory
  38. * regions into the linear user address-space. It provides offsets to the
  39. * caller which can then be used on the address_space of the drm-device. It
  40. * takes care to not overlap regions, size them appropriately and to not
  41. * confuse mm-core by inconsistent fake vm_pgoff fields.
  42. * Drivers shouldn't use this for object placement in VMEM. This manager should
  43. * only be used to manage mappings into linear user-space VMs.
  44. *
  45. * We use drm_mm as backend to manage object allocations. But it is highly
  46. * optimized for alloc/free calls, not lookups. Hence, we use an rb-tree to
  47. * speed up offset lookups.
  48. *
  49. * You must not use multiple offset managers on a single address_space.
  50. * Otherwise, mm-core will be unable to tear down memory mappings as the VM will
  51. * no longer be linear.
  52. *
  53. * This offset manager works on page-based addresses. That is, every argument
  54. * and return code (with the exception of drm_vma_node_offset_addr()) is given
  55. * in number of pages, not number of bytes. That means, object sizes and offsets
  56. * must always be page-aligned (as usual).
  57. * If you want to get a valid byte-based user-space address for a given offset,
  58. * please see drm_vma_node_offset_addr().
  59. *
  60. * Additionally to offset management, the vma offset manager also handles access
  61. * management. For every open-file context that is allowed to access a given
  62. * node, you must call drm_vma_node_allow(). Otherwise, an mmap() call on this
  63. * open-file with the offset of the node will fail with -EACCES. To revoke
  64. * access again, use drm_vma_node_revoke(). However, the caller is responsible
  65. * for destroying already existing mappings, if required.
  66. */
  67. /**
  68. * drm_vma_offset_manager_init - Initialize new offset-manager
  69. * @mgr: Manager object
  70. * @page_offset: Offset of available memory area (page-based)
  71. * @size: Size of available address space range (page-based)
  72. *
  73. * Initialize a new offset-manager. The offset and area size available for the
  74. * manager are given as @page_offset and @size. Both are interpreted as
  75. * page-numbers, not bytes.
  76. *
  77. * Adding/removing nodes from the manager is locked internally and protected
  78. * against concurrent access. However, node allocation and destruction is left
  79. * for the caller. While calling into the vma-manager, a given node must
  80. * always be guaranteed to be referenced.
  81. */
  82. void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
  83. unsigned long page_offset, unsigned long size)
  84. {
  85. rwlock_init(&mgr->vm_lock);
  86. mgr->vm_addr_space_rb = RB_ROOT;
  87. drm_mm_init(&mgr->vm_addr_space_mm, page_offset, size);
  88. }
  89. EXPORT_SYMBOL(drm_vma_offset_manager_init);
  90. /**
  91. * drm_vma_offset_manager_destroy() - Destroy offset manager
  92. * @mgr: Manager object
  93. *
  94. * Destroy an object manager which was previously created via
  95. * drm_vma_offset_manager_init(). The caller must remove all allocated nodes
  96. * before destroying the manager. Otherwise, drm_mm will refuse to free the
  97. * requested resources.
  98. *
  99. * The manager must not be accessed after this function is called.
  100. */
  101. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
  102. {
  103. /* take the lock to protect against buggy drivers */
  104. write_lock(&mgr->vm_lock);
  105. drm_mm_takedown(&mgr->vm_addr_space_mm);
  106. write_unlock(&mgr->vm_lock);
  107. }
  108. EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
  109. /**
  110. * drm_vma_offset_lookup() - Find node in offset space
  111. * @mgr: Manager object
  112. * @start: Start address for object (page-based)
  113. * @pages: Size of object (page-based)
  114. *
  115. * Find a node given a start address and object size. This returns the _best_
  116. * match for the given node. That is, @start may point somewhere into a valid
  117. * region and the given node will be returned, as long as the node spans the
  118. * whole requested area (given the size in number of pages as @pages).
  119. *
  120. * RETURNS:
  121. * Returns NULL if no suitable node can be found. Otherwise, the best match
  122. * is returned. It's the caller's responsibility to make sure the node doesn't
  123. * get destroyed before the caller can access it.
  124. */
  125. struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
  126. unsigned long start,
  127. unsigned long pages)
  128. {
  129. struct drm_vma_offset_node *node;
  130. read_lock(&mgr->vm_lock);
  131. node = drm_vma_offset_lookup_locked(mgr, start, pages);
  132. read_unlock(&mgr->vm_lock);
  133. return node;
  134. }
  135. EXPORT_SYMBOL(drm_vma_offset_lookup);
  136. /**
  137. * drm_vma_offset_lookup_locked() - Find node in offset space
  138. * @mgr: Manager object
  139. * @start: Start address for object (page-based)
  140. * @pages: Size of object (page-based)
  141. *
  142. * Same as drm_vma_offset_lookup() but requires the caller to lock offset lookup
  143. * manually. See drm_vma_offset_lock_lookup() for an example.
  144. *
  145. * RETURNS:
  146. * Returns NULL if no suitable node can be found. Otherwise, the best match
  147. * is returned.
  148. */
  149. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  150. unsigned long start,
  151. unsigned long pages)
  152. {
  153. struct drm_vma_offset_node *node, *best;
  154. struct rb_node *iter;
  155. unsigned long offset;
  156. iter = mgr->vm_addr_space_rb.rb_node;
  157. best = NULL;
  158. while (likely(iter)) {
  159. node = rb_entry(iter, struct drm_vma_offset_node, vm_rb);
  160. offset = node->vm_node.start;
  161. if (start >= offset) {
  162. iter = iter->rb_right;
  163. best = node;
  164. if (start == offset)
  165. break;
  166. } else {
  167. iter = iter->rb_left;
  168. }
  169. }
  170. /* verify that the node spans the requested area */
  171. if (best) {
  172. offset = best->vm_node.start + best->vm_node.size;
  173. if (offset < start + pages)
  174. best = NULL;
  175. }
  176. return best;
  177. }
  178. EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
  179. /* internal helper to link @node into the rb-tree */
  180. static void _drm_vma_offset_add_rb(struct drm_vma_offset_manager *mgr,
  181. struct drm_vma_offset_node *node)
  182. {
  183. struct rb_node **iter = &mgr->vm_addr_space_rb.rb_node;
  184. struct rb_node *parent = NULL;
  185. struct drm_vma_offset_node *iter_node;
  186. while (likely(*iter)) {
  187. parent = *iter;
  188. iter_node = rb_entry(*iter, struct drm_vma_offset_node, vm_rb);
  189. if (node->vm_node.start < iter_node->vm_node.start)
  190. iter = &(*iter)->rb_left;
  191. else if (node->vm_node.start > iter_node->vm_node.start)
  192. iter = &(*iter)->rb_right;
  193. else
  194. BUG();
  195. }
  196. rb_link_node(&node->vm_rb, parent, iter);
  197. rb_insert_color(&node->vm_rb, &mgr->vm_addr_space_rb);
  198. }
  199. /**
  200. * drm_vma_offset_add() - Add offset node to manager
  201. * @mgr: Manager object
  202. * @node: Node to be added
  203. * @pages: Allocation size visible to user-space (in number of pages)
  204. *
  205. * Add a node to the offset-manager. If the node was already added, this does
  206. * nothing and return 0. @pages is the size of the object given in number of
  207. * pages.
  208. * After this call succeeds, you can access the offset of the node until it
  209. * is removed again.
  210. *
  211. * If this call fails, it is safe to retry the operation or call
  212. * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
  213. * case.
  214. *
  215. * @pages is not required to be the same size as the underlying memory object
  216. * that you want to map. It only limits the size that user-space can map into
  217. * their address space.
  218. *
  219. * RETURNS:
  220. * 0 on success, negative error code on failure.
  221. */
  222. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  223. struct drm_vma_offset_node *node, unsigned long pages)
  224. {
  225. int ret;
  226. write_lock(&mgr->vm_lock);
  227. if (drm_mm_node_allocated(&node->vm_node)) {
  228. ret = 0;
  229. goto out_unlock;
  230. }
  231. ret = drm_mm_insert_node(&mgr->vm_addr_space_mm, &node->vm_node,
  232. pages, 0, DRM_MM_SEARCH_DEFAULT);
  233. if (ret)
  234. goto out_unlock;
  235. _drm_vma_offset_add_rb(mgr, node);
  236. out_unlock:
  237. write_unlock(&mgr->vm_lock);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(drm_vma_offset_add);
  241. /**
  242. * drm_vma_offset_remove() - Remove offset node from manager
  243. * @mgr: Manager object
  244. * @node: Node to be removed
  245. *
  246. * Remove a node from the offset manager. If the node wasn't added before, this
  247. * does nothing. After this call returns, the offset and size will be 0 until a
  248. * new offset is allocated via drm_vma_offset_add() again. Helper functions like
  249. * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
  250. * offset is allocated.
  251. */
  252. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  253. struct drm_vma_offset_node *node)
  254. {
  255. write_lock(&mgr->vm_lock);
  256. if (drm_mm_node_allocated(&node->vm_node)) {
  257. rb_erase(&node->vm_rb, &mgr->vm_addr_space_rb);
  258. drm_mm_remove_node(&node->vm_node);
  259. memset(&node->vm_node, 0, sizeof(node->vm_node));
  260. }
  261. write_unlock(&mgr->vm_lock);
  262. }
  263. EXPORT_SYMBOL(drm_vma_offset_remove);
  264. /**
  265. * drm_vma_node_allow - Add open-file to list of allowed users
  266. * @node: Node to modify
  267. * @filp: Open file to add
  268. *
  269. * Add @filp to the list of allowed open-files for this node. If @filp is
  270. * already on this list, the ref-count is incremented.
  271. *
  272. * The list of allowed-users is preserved across drm_vma_offset_add() and
  273. * drm_vma_offset_remove() calls. You may even call it if the node is currently
  274. * not added to any offset-manager.
  275. *
  276. * You must remove all open-files the same number of times as you added them
  277. * before destroying the node. Otherwise, you will leak memory.
  278. *
  279. * This is locked against concurrent access internally.
  280. *
  281. * RETURNS:
  282. * 0 on success, negative error code on internal failure (out-of-mem)
  283. */
  284. int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp)
  285. {
  286. struct rb_node **iter;
  287. struct rb_node *parent = NULL;
  288. struct drm_vma_offset_file *new, *entry;
  289. int ret = 0;
  290. /* Preallocate entry to avoid atomic allocations below. It is quite
  291. * unlikely that an open-file is added twice to a single node so we
  292. * don't optimize for this case. OOM is checked below only if the entry
  293. * is actually used. */
  294. new = kmalloc(sizeof(*entry), GFP_KERNEL);
  295. write_lock(&node->vm_lock);
  296. iter = &node->vm_files.rb_node;
  297. while (likely(*iter)) {
  298. parent = *iter;
  299. entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
  300. if (filp == entry->vm_filp) {
  301. entry->vm_count++;
  302. goto unlock;
  303. } else if (filp > entry->vm_filp) {
  304. iter = &(*iter)->rb_right;
  305. } else {
  306. iter = &(*iter)->rb_left;
  307. }
  308. }
  309. if (!new) {
  310. ret = -ENOMEM;
  311. goto unlock;
  312. }
  313. new->vm_filp = filp;
  314. new->vm_count = 1;
  315. rb_link_node(&new->vm_rb, parent, iter);
  316. rb_insert_color(&new->vm_rb, &node->vm_files);
  317. new = NULL;
  318. unlock:
  319. write_unlock(&node->vm_lock);
  320. kfree(new);
  321. return ret;
  322. }
  323. EXPORT_SYMBOL(drm_vma_node_allow);
  324. /**
  325. * drm_vma_node_revoke - Remove open-file from list of allowed users
  326. * @node: Node to modify
  327. * @filp: Open file to remove
  328. *
  329. * Decrement the ref-count of @filp in the list of allowed open-files on @node.
  330. * If the ref-count drops to zero, remove @filp from the list. You must call
  331. * this once for every drm_vma_node_allow() on @filp.
  332. *
  333. * This is locked against concurrent access internally.
  334. *
  335. * If @filp is not on the list, nothing is done.
  336. */
  337. void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp)
  338. {
  339. struct drm_vma_offset_file *entry;
  340. struct rb_node *iter;
  341. write_lock(&node->vm_lock);
  342. iter = node->vm_files.rb_node;
  343. while (likely(iter)) {
  344. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  345. if (filp == entry->vm_filp) {
  346. if (!--entry->vm_count) {
  347. rb_erase(&entry->vm_rb, &node->vm_files);
  348. kfree(entry);
  349. }
  350. break;
  351. } else if (filp > entry->vm_filp) {
  352. iter = iter->rb_right;
  353. } else {
  354. iter = iter->rb_left;
  355. }
  356. }
  357. write_unlock(&node->vm_lock);
  358. }
  359. EXPORT_SYMBOL(drm_vma_node_revoke);
  360. /**
  361. * drm_vma_node_is_allowed - Check whether an open-file is granted access
  362. * @node: Node to check
  363. * @filp: Open-file to check for
  364. *
  365. * Search the list in @node whether @filp is currently on the list of allowed
  366. * open-files (see drm_vma_node_allow()).
  367. *
  368. * This is locked against concurrent access internally.
  369. *
  370. * RETURNS:
  371. * true iff @filp is on the list
  372. */
  373. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  374. struct file *filp)
  375. {
  376. struct drm_vma_offset_file *entry;
  377. struct rb_node *iter;
  378. read_lock(&node->vm_lock);
  379. iter = node->vm_files.rb_node;
  380. while (likely(iter)) {
  381. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  382. if (filp == entry->vm_filp)
  383. break;
  384. else if (filp > entry->vm_filp)
  385. iter = iter->rb_right;
  386. else
  387. iter = iter->rb_left;
  388. }
  389. read_unlock(&node->vm_lock);
  390. return iter;
  391. }
  392. EXPORT_SYMBOL(drm_vma_node_is_allowed);