drm_vma_manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. Please use VM_NONLINEAR in that case and implement your
  52. * own offset managers.
  53. *
  54. * This offset manager works on page-based addresses. That is, every argument
  55. * and return code (with the exception of drm_vma_node_offset_addr()) is given
  56. * in number of pages, not number of bytes. That means, object sizes and offsets
  57. * must always be page-aligned (as usual).
  58. * If you want to get a valid byte-based user-space address for a given offset,
  59. * please see drm_vma_node_offset_addr().
  60. *
  61. * Additionally to offset management, the vma offset manager also handles access
  62. * management. For every open-file context that is allowed to access a given
  63. * node, you must call drm_vma_node_allow(). Otherwise, an mmap() call on this
  64. * open-file with the offset of the node will fail with -EACCES. To revoke
  65. * access again, use drm_vma_node_revoke(). However, the caller is responsible
  66. * for destroying already existing mappings, if required.
  67. */
  68. /**
  69. * drm_vma_offset_manager_init - Initialize new offset-manager
  70. * @mgr: Manager object
  71. * @page_offset: Offset of available memory area (page-based)
  72. * @size: Size of available address space range (page-based)
  73. *
  74. * Initialize a new offset-manager. The offset and area size available for the
  75. * manager are given as @page_offset and @size. Both are interpreted as
  76. * page-numbers, not bytes.
  77. *
  78. * Adding/removing nodes from the manager is locked internally and protected
  79. * against concurrent access. However, node allocation and destruction is left
  80. * for the caller. While calling into the vma-manager, a given node must
  81. * always be guaranteed to be referenced.
  82. */
  83. void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
  84. unsigned long page_offset, unsigned long size)
  85. {
  86. rwlock_init(&mgr->vm_lock);
  87. mgr->vm_addr_space_rb = RB_ROOT;
  88. drm_mm_init(&mgr->vm_addr_space_mm, page_offset, size);
  89. }
  90. EXPORT_SYMBOL(drm_vma_offset_manager_init);
  91. /**
  92. * drm_vma_offset_manager_destroy() - Destroy offset manager
  93. * @mgr: Manager object
  94. *
  95. * Destroy an object manager which was previously created via
  96. * drm_vma_offset_manager_init(). The caller must remove all allocated nodes
  97. * before destroying the manager. Otherwise, drm_mm will refuse to free the
  98. * requested resources.
  99. *
  100. * The manager must not be accessed after this function is called.
  101. */
  102. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
  103. {
  104. /* take the lock to protect against buggy drivers */
  105. write_lock(&mgr->vm_lock);
  106. drm_mm_takedown(&mgr->vm_addr_space_mm);
  107. write_unlock(&mgr->vm_lock);
  108. }
  109. EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
  110. /**
  111. * drm_vma_offset_lookup() - Find node in offset space
  112. * @mgr: Manager object
  113. * @start: Start address for object (page-based)
  114. * @pages: Size of object (page-based)
  115. *
  116. * Find a node given a start address and object size. This returns the _best_
  117. * match for the given node. That is, @start may point somewhere into a valid
  118. * region and the given node will be returned, as long as the node spans the
  119. * whole requested area (given the size in number of pages as @pages).
  120. *
  121. * RETURNS:
  122. * Returns NULL if no suitable node can be found. Otherwise, the best match
  123. * is returned. It's the caller's responsibility to make sure the node doesn't
  124. * get destroyed before the caller can access it.
  125. */
  126. struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
  127. unsigned long start,
  128. unsigned long pages)
  129. {
  130. struct drm_vma_offset_node *node;
  131. read_lock(&mgr->vm_lock);
  132. node = drm_vma_offset_lookup_locked(mgr, start, pages);
  133. read_unlock(&mgr->vm_lock);
  134. return node;
  135. }
  136. EXPORT_SYMBOL(drm_vma_offset_lookup);
  137. /**
  138. * drm_vma_offset_lookup_locked() - Find node in offset space
  139. * @mgr: Manager object
  140. * @start: Start address for object (page-based)
  141. * @pages: Size of object (page-based)
  142. *
  143. * Same as drm_vma_offset_lookup() but requires the caller to lock offset lookup
  144. * manually. See drm_vma_offset_lock_lookup() for an example.
  145. *
  146. * RETURNS:
  147. * Returns NULL if no suitable node can be found. Otherwise, the best match
  148. * is returned.
  149. */
  150. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  151. unsigned long start,
  152. unsigned long pages)
  153. {
  154. struct drm_vma_offset_node *node, *best;
  155. struct rb_node *iter;
  156. unsigned long offset;
  157. iter = mgr->vm_addr_space_rb.rb_node;
  158. best = NULL;
  159. while (likely(iter)) {
  160. node = rb_entry(iter, struct drm_vma_offset_node, vm_rb);
  161. offset = node->vm_node.start;
  162. if (start >= offset) {
  163. iter = iter->rb_right;
  164. best = node;
  165. if (start == offset)
  166. break;
  167. } else {
  168. iter = iter->rb_left;
  169. }
  170. }
  171. /* verify that the node spans the requested area */
  172. if (best) {
  173. offset = best->vm_node.start + best->vm_node.size;
  174. if (offset < start + pages)
  175. best = NULL;
  176. }
  177. return best;
  178. }
  179. EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
  180. /* internal helper to link @node into the rb-tree */
  181. static void _drm_vma_offset_add_rb(struct drm_vma_offset_manager *mgr,
  182. struct drm_vma_offset_node *node)
  183. {
  184. struct rb_node **iter = &mgr->vm_addr_space_rb.rb_node;
  185. struct rb_node *parent = NULL;
  186. struct drm_vma_offset_node *iter_node;
  187. while (likely(*iter)) {
  188. parent = *iter;
  189. iter_node = rb_entry(*iter, struct drm_vma_offset_node, vm_rb);
  190. if (node->vm_node.start < iter_node->vm_node.start)
  191. iter = &(*iter)->rb_left;
  192. else if (node->vm_node.start > iter_node->vm_node.start)
  193. iter = &(*iter)->rb_right;
  194. else
  195. BUG();
  196. }
  197. rb_link_node(&node->vm_rb, parent, iter);
  198. rb_insert_color(&node->vm_rb, &mgr->vm_addr_space_rb);
  199. }
  200. /**
  201. * drm_vma_offset_add() - Add offset node to manager
  202. * @mgr: Manager object
  203. * @node: Node to be added
  204. * @pages: Allocation size visible to user-space (in number of pages)
  205. *
  206. * Add a node to the offset-manager. If the node was already added, this does
  207. * nothing and return 0. @pages is the size of the object given in number of
  208. * pages.
  209. * After this call succeeds, you can access the offset of the node until it
  210. * is removed again.
  211. *
  212. * If this call fails, it is safe to retry the operation or call
  213. * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
  214. * case.
  215. *
  216. * @pages is not required to be the same size as the underlying memory object
  217. * that you want to map. It only limits the size that user-space can map into
  218. * their address space.
  219. *
  220. * RETURNS:
  221. * 0 on success, negative error code on failure.
  222. */
  223. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  224. struct drm_vma_offset_node *node, unsigned long pages)
  225. {
  226. int ret;
  227. write_lock(&mgr->vm_lock);
  228. if (drm_mm_node_allocated(&node->vm_node)) {
  229. ret = 0;
  230. goto out_unlock;
  231. }
  232. ret = drm_mm_insert_node(&mgr->vm_addr_space_mm, &node->vm_node,
  233. pages, 0, DRM_MM_SEARCH_DEFAULT);
  234. if (ret)
  235. goto out_unlock;
  236. _drm_vma_offset_add_rb(mgr, node);
  237. out_unlock:
  238. write_unlock(&mgr->vm_lock);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(drm_vma_offset_add);
  242. /**
  243. * drm_vma_offset_remove() - Remove offset node from manager
  244. * @mgr: Manager object
  245. * @node: Node to be removed
  246. *
  247. * Remove a node from the offset manager. If the node wasn't added before, this
  248. * does nothing. After this call returns, the offset and size will be 0 until a
  249. * new offset is allocated via drm_vma_offset_add() again. Helper functions like
  250. * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
  251. * offset is allocated.
  252. */
  253. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  254. struct drm_vma_offset_node *node)
  255. {
  256. write_lock(&mgr->vm_lock);
  257. if (drm_mm_node_allocated(&node->vm_node)) {
  258. rb_erase(&node->vm_rb, &mgr->vm_addr_space_rb);
  259. drm_mm_remove_node(&node->vm_node);
  260. memset(&node->vm_node, 0, sizeof(node->vm_node));
  261. }
  262. write_unlock(&mgr->vm_lock);
  263. }
  264. EXPORT_SYMBOL(drm_vma_offset_remove);
  265. /**
  266. * drm_vma_node_allow - Add open-file to list of allowed users
  267. * @node: Node to modify
  268. * @filp: Open file to add
  269. *
  270. * Add @filp to the list of allowed open-files for this node. If @filp is
  271. * already on this list, the ref-count is incremented.
  272. *
  273. * The list of allowed-users is preserved across drm_vma_offset_add() and
  274. * drm_vma_offset_remove() calls. You may even call it if the node is currently
  275. * not added to any offset-manager.
  276. *
  277. * You must remove all open-files the same number of times as you added them
  278. * before destroying the node. Otherwise, you will leak memory.
  279. *
  280. * This is locked against concurrent access internally.
  281. *
  282. * RETURNS:
  283. * 0 on success, negative error code on internal failure (out-of-mem)
  284. */
  285. int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp)
  286. {
  287. struct rb_node **iter;
  288. struct rb_node *parent = NULL;
  289. struct drm_vma_offset_file *new, *entry;
  290. int ret = 0;
  291. /* Preallocate entry to avoid atomic allocations below. It is quite
  292. * unlikely that an open-file is added twice to a single node so we
  293. * don't optimize for this case. OOM is checked below only if the entry
  294. * is actually used. */
  295. new = kmalloc(sizeof(*entry), GFP_KERNEL);
  296. write_lock(&node->vm_lock);
  297. iter = &node->vm_files.rb_node;
  298. while (likely(*iter)) {
  299. parent = *iter;
  300. entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
  301. if (filp == entry->vm_filp) {
  302. entry->vm_count++;
  303. goto unlock;
  304. } else if (filp > entry->vm_filp) {
  305. iter = &(*iter)->rb_right;
  306. } else {
  307. iter = &(*iter)->rb_left;
  308. }
  309. }
  310. if (!new) {
  311. ret = -ENOMEM;
  312. goto unlock;
  313. }
  314. new->vm_filp = filp;
  315. new->vm_count = 1;
  316. rb_link_node(&new->vm_rb, parent, iter);
  317. rb_insert_color(&new->vm_rb, &node->vm_files);
  318. new = NULL;
  319. unlock:
  320. write_unlock(&node->vm_lock);
  321. kfree(new);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(drm_vma_node_allow);
  325. /**
  326. * drm_vma_node_revoke - Remove open-file from list of allowed users
  327. * @node: Node to modify
  328. * @filp: Open file to remove
  329. *
  330. * Decrement the ref-count of @filp in the list of allowed open-files on @node.
  331. * If the ref-count drops to zero, remove @filp from the list. You must call
  332. * this once for every drm_vma_node_allow() on @filp.
  333. *
  334. * This is locked against concurrent access internally.
  335. *
  336. * If @filp is not on the list, nothing is done.
  337. */
  338. void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp)
  339. {
  340. struct drm_vma_offset_file *entry;
  341. struct rb_node *iter;
  342. write_lock(&node->vm_lock);
  343. iter = node->vm_files.rb_node;
  344. while (likely(iter)) {
  345. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  346. if (filp == entry->vm_filp) {
  347. if (!--entry->vm_count) {
  348. rb_erase(&entry->vm_rb, &node->vm_files);
  349. kfree(entry);
  350. }
  351. break;
  352. } else if (filp > entry->vm_filp) {
  353. iter = iter->rb_right;
  354. } else {
  355. iter = iter->rb_left;
  356. }
  357. }
  358. write_unlock(&node->vm_lock);
  359. }
  360. EXPORT_SYMBOL(drm_vma_node_revoke);
  361. /**
  362. * drm_vma_node_is_allowed - Check whether an open-file is granted access
  363. * @node: Node to check
  364. * @filp: Open-file to check for
  365. *
  366. * Search the list in @node whether @filp is currently on the list of allowed
  367. * open-files (see drm_vma_node_allow()).
  368. *
  369. * This is locked against concurrent access internally.
  370. *
  371. * RETURNS:
  372. * true iff @filp is on the list
  373. */
  374. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  375. struct file *filp)
  376. {
  377. struct drm_vma_offset_file *entry;
  378. struct rb_node *iter;
  379. read_lock(&node->vm_lock);
  380. iter = node->vm_files.rb_node;
  381. while (likely(iter)) {
  382. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  383. if (filp == entry->vm_filp)
  384. break;
  385. else if (filp > entry->vm_filp)
  386. iter = iter->rb_right;
  387. else
  388. iter = iter->rb_left;
  389. }
  390. read_unlock(&node->vm_lock);
  391. return iter;
  392. }
  393. EXPORT_SYMBOL(drm_vma_node_is_allowed);