drm_vma_manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_locked() - 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. * Note that before lookup the vma offset manager lookup lock must be acquired
  121. * with drm_vma_offset_lock_lookup(). See there for an example. This can then be
  122. * used to implement weakly referenced lookups using kref_get_unless_zero().
  123. *
  124. * Example:
  125. * drm_vma_offset_lock_lookup(mgr);
  126. * node = drm_vma_offset_lookup_locked(mgr);
  127. * if (node)
  128. * kref_get_unless_zero(container_of(node, sth, entr));
  129. * drm_vma_offset_unlock_lookup(mgr);
  130. *
  131. * RETURNS:
  132. * Returns NULL if no suitable node can be found. Otherwise, the best match
  133. * is returned. It's the caller's responsibility to make sure the node doesn't
  134. * get destroyed before the caller can access it.
  135. */
  136. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  137. unsigned long start,
  138. unsigned long pages)
  139. {
  140. struct drm_vma_offset_node *node, *best;
  141. struct rb_node *iter;
  142. unsigned long offset;
  143. iter = mgr->vm_addr_space_rb.rb_node;
  144. best = NULL;
  145. while (likely(iter)) {
  146. node = rb_entry(iter, struct drm_vma_offset_node, vm_rb);
  147. offset = node->vm_node.start;
  148. if (start >= offset) {
  149. iter = iter->rb_right;
  150. best = node;
  151. if (start == offset)
  152. break;
  153. } else {
  154. iter = iter->rb_left;
  155. }
  156. }
  157. /* verify that the node spans the requested area */
  158. if (best) {
  159. offset = best->vm_node.start + best->vm_node.size;
  160. if (offset < start + pages)
  161. best = NULL;
  162. }
  163. return best;
  164. }
  165. EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
  166. /* internal helper to link @node into the rb-tree */
  167. static void _drm_vma_offset_add_rb(struct drm_vma_offset_manager *mgr,
  168. struct drm_vma_offset_node *node)
  169. {
  170. struct rb_node **iter = &mgr->vm_addr_space_rb.rb_node;
  171. struct rb_node *parent = NULL;
  172. struct drm_vma_offset_node *iter_node;
  173. while (likely(*iter)) {
  174. parent = *iter;
  175. iter_node = rb_entry(*iter, struct drm_vma_offset_node, vm_rb);
  176. if (node->vm_node.start < iter_node->vm_node.start)
  177. iter = &(*iter)->rb_left;
  178. else if (node->vm_node.start > iter_node->vm_node.start)
  179. iter = &(*iter)->rb_right;
  180. else
  181. BUG();
  182. }
  183. rb_link_node(&node->vm_rb, parent, iter);
  184. rb_insert_color(&node->vm_rb, &mgr->vm_addr_space_rb);
  185. }
  186. /**
  187. * drm_vma_offset_add() - Add offset node to manager
  188. * @mgr: Manager object
  189. * @node: Node to be added
  190. * @pages: Allocation size visible to user-space (in number of pages)
  191. *
  192. * Add a node to the offset-manager. If the node was already added, this does
  193. * nothing and return 0. @pages is the size of the object given in number of
  194. * pages.
  195. * After this call succeeds, you can access the offset of the node until it
  196. * is removed again.
  197. *
  198. * If this call fails, it is safe to retry the operation or call
  199. * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
  200. * case.
  201. *
  202. * @pages is not required to be the same size as the underlying memory object
  203. * that you want to map. It only limits the size that user-space can map into
  204. * their address space.
  205. *
  206. * RETURNS:
  207. * 0 on success, negative error code on failure.
  208. */
  209. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  210. struct drm_vma_offset_node *node, unsigned long pages)
  211. {
  212. int ret;
  213. write_lock(&mgr->vm_lock);
  214. if (drm_mm_node_allocated(&node->vm_node)) {
  215. ret = 0;
  216. goto out_unlock;
  217. }
  218. ret = drm_mm_insert_node(&mgr->vm_addr_space_mm, &node->vm_node,
  219. pages, 0, DRM_MM_SEARCH_DEFAULT);
  220. if (ret)
  221. goto out_unlock;
  222. _drm_vma_offset_add_rb(mgr, node);
  223. out_unlock:
  224. write_unlock(&mgr->vm_lock);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL(drm_vma_offset_add);
  228. /**
  229. * drm_vma_offset_remove() - Remove offset node from manager
  230. * @mgr: Manager object
  231. * @node: Node to be removed
  232. *
  233. * Remove a node from the offset manager. If the node wasn't added before, this
  234. * does nothing. After this call returns, the offset and size will be 0 until a
  235. * new offset is allocated via drm_vma_offset_add() again. Helper functions like
  236. * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
  237. * offset is allocated.
  238. */
  239. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  240. struct drm_vma_offset_node *node)
  241. {
  242. write_lock(&mgr->vm_lock);
  243. if (drm_mm_node_allocated(&node->vm_node)) {
  244. rb_erase(&node->vm_rb, &mgr->vm_addr_space_rb);
  245. drm_mm_remove_node(&node->vm_node);
  246. memset(&node->vm_node, 0, sizeof(node->vm_node));
  247. }
  248. write_unlock(&mgr->vm_lock);
  249. }
  250. EXPORT_SYMBOL(drm_vma_offset_remove);
  251. /**
  252. * drm_vma_node_allow - Add open-file to list of allowed users
  253. * @node: Node to modify
  254. * @filp: Open file to add
  255. *
  256. * Add @filp to the list of allowed open-files for this node. If @filp is
  257. * already on this list, the ref-count is incremented.
  258. *
  259. * The list of allowed-users is preserved across drm_vma_offset_add() and
  260. * drm_vma_offset_remove() calls. You may even call it if the node is currently
  261. * not added to any offset-manager.
  262. *
  263. * You must remove all open-files the same number of times as you added them
  264. * before destroying the node. Otherwise, you will leak memory.
  265. *
  266. * This is locked against concurrent access internally.
  267. *
  268. * RETURNS:
  269. * 0 on success, negative error code on internal failure (out-of-mem)
  270. */
  271. int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp)
  272. {
  273. struct rb_node **iter;
  274. struct rb_node *parent = NULL;
  275. struct drm_vma_offset_file *new, *entry;
  276. int ret = 0;
  277. /* Preallocate entry to avoid atomic allocations below. It is quite
  278. * unlikely that an open-file is added twice to a single node so we
  279. * don't optimize for this case. OOM is checked below only if the entry
  280. * is actually used. */
  281. new = kmalloc(sizeof(*entry), GFP_KERNEL);
  282. write_lock(&node->vm_lock);
  283. iter = &node->vm_files.rb_node;
  284. while (likely(*iter)) {
  285. parent = *iter;
  286. entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
  287. if (filp == entry->vm_filp) {
  288. entry->vm_count++;
  289. goto unlock;
  290. } else if (filp > entry->vm_filp) {
  291. iter = &(*iter)->rb_right;
  292. } else {
  293. iter = &(*iter)->rb_left;
  294. }
  295. }
  296. if (!new) {
  297. ret = -ENOMEM;
  298. goto unlock;
  299. }
  300. new->vm_filp = filp;
  301. new->vm_count = 1;
  302. rb_link_node(&new->vm_rb, parent, iter);
  303. rb_insert_color(&new->vm_rb, &node->vm_files);
  304. new = NULL;
  305. unlock:
  306. write_unlock(&node->vm_lock);
  307. kfree(new);
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(drm_vma_node_allow);
  311. /**
  312. * drm_vma_node_revoke - Remove open-file from list of allowed users
  313. * @node: Node to modify
  314. * @filp: Open file to remove
  315. *
  316. * Decrement the ref-count of @filp in the list of allowed open-files on @node.
  317. * If the ref-count drops to zero, remove @filp from the list. You must call
  318. * this once for every drm_vma_node_allow() on @filp.
  319. *
  320. * This is locked against concurrent access internally.
  321. *
  322. * If @filp is not on the list, nothing is done.
  323. */
  324. void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp)
  325. {
  326. struct drm_vma_offset_file *entry;
  327. struct rb_node *iter;
  328. write_lock(&node->vm_lock);
  329. iter = node->vm_files.rb_node;
  330. while (likely(iter)) {
  331. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  332. if (filp == entry->vm_filp) {
  333. if (!--entry->vm_count) {
  334. rb_erase(&entry->vm_rb, &node->vm_files);
  335. kfree(entry);
  336. }
  337. break;
  338. } else if (filp > entry->vm_filp) {
  339. iter = iter->rb_right;
  340. } else {
  341. iter = iter->rb_left;
  342. }
  343. }
  344. write_unlock(&node->vm_lock);
  345. }
  346. EXPORT_SYMBOL(drm_vma_node_revoke);
  347. /**
  348. * drm_vma_node_is_allowed - Check whether an open-file is granted access
  349. * @node: Node to check
  350. * @filp: Open-file to check for
  351. *
  352. * Search the list in @node whether @filp is currently on the list of allowed
  353. * open-files (see drm_vma_node_allow()).
  354. *
  355. * This is locked against concurrent access internally.
  356. *
  357. * RETURNS:
  358. * true iff @filp is on the list
  359. */
  360. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  361. struct file *filp)
  362. {
  363. struct drm_vma_offset_file *entry;
  364. struct rb_node *iter;
  365. read_lock(&node->vm_lock);
  366. iter = node->vm_files.rb_node;
  367. while (likely(iter)) {
  368. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  369. if (filp == entry->vm_filp)
  370. break;
  371. else if (filp > entry->vm_filp)
  372. iter = iter->rb_right;
  373. else
  374. iter = iter->rb_left;
  375. }
  376. read_unlock(&node->vm_lock);
  377. return iter;
  378. }
  379. EXPORT_SYMBOL(drm_vma_node_is_allowed);