drm_vma_manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * Copyright (c) 2012 David Airlie <airlied@linux.ie>
  5. * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/export.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/rbtree.h>
  29. #include <linux/slab.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/types.h>
  32. #include <drm/drm_mm.h>
  33. #include <drm/drm_vma_manager.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. drm_mm_init(&mgr->vm_addr_space_mm, page_offset, size);
  87. }
  88. EXPORT_SYMBOL(drm_vma_offset_manager_init);
  89. /**
  90. * drm_vma_offset_manager_destroy() - Destroy offset manager
  91. * @mgr: Manager object
  92. *
  93. * Destroy an object manager which was previously created via
  94. * drm_vma_offset_manager_init(). The caller must remove all allocated nodes
  95. * before destroying the manager. Otherwise, drm_mm will refuse to free the
  96. * requested resources.
  97. *
  98. * The manager must not be accessed after this function is called.
  99. */
  100. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
  101. {
  102. drm_mm_takedown(&mgr->vm_addr_space_mm);
  103. }
  104. EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
  105. /**
  106. * drm_vma_offset_lookup_locked() - Find node in offset space
  107. * @mgr: Manager object
  108. * @start: Start address for object (page-based)
  109. * @pages: Size of object (page-based)
  110. *
  111. * Find a node given a start address and object size. This returns the _best_
  112. * match for the given node. That is, @start may point somewhere into a valid
  113. * region and the given node will be returned, as long as the node spans the
  114. * whole requested area (given the size in number of pages as @pages).
  115. *
  116. * Note that before lookup the vma offset manager lookup lock must be acquired
  117. * with drm_vma_offset_lock_lookup(). See there for an example. This can then be
  118. * used to implement weakly referenced lookups using kref_get_unless_zero().
  119. *
  120. * Example:
  121. *
  122. * ::
  123. *
  124. * drm_vma_offset_lock_lookup(mgr);
  125. * node = drm_vma_offset_lookup_locked(mgr);
  126. * if (node)
  127. * kref_get_unless_zero(container_of(node, sth, entr));
  128. * drm_vma_offset_unlock_lookup(mgr);
  129. *
  130. * RETURNS:
  131. * Returns NULL if no suitable node can be found. Otherwise, the best match
  132. * is returned. It's the caller's responsibility to make sure the node doesn't
  133. * get destroyed before the caller can access it.
  134. */
  135. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  136. unsigned long start,
  137. unsigned long pages)
  138. {
  139. struct drm_mm_node *node, *best;
  140. struct rb_node *iter;
  141. unsigned long offset;
  142. iter = mgr->vm_addr_space_mm.interval_tree.rb_root.rb_node;
  143. best = NULL;
  144. while (likely(iter)) {
  145. node = rb_entry(iter, struct drm_mm_node, rb);
  146. offset = node->start;
  147. if (start >= offset) {
  148. iter = iter->rb_right;
  149. best = node;
  150. if (start == offset)
  151. break;
  152. } else {
  153. iter = iter->rb_left;
  154. }
  155. }
  156. /* verify that the node spans the requested area */
  157. if (best) {
  158. offset = best->start + best->size;
  159. if (offset < start + pages)
  160. best = NULL;
  161. }
  162. if (!best)
  163. return NULL;
  164. return container_of(best, struct drm_vma_offset_node, vm_node);
  165. }
  166. EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
  167. /**
  168. * drm_vma_offset_add() - Add offset node to manager
  169. * @mgr: Manager object
  170. * @node: Node to be added
  171. * @pages: Allocation size visible to user-space (in number of pages)
  172. *
  173. * Add a node to the offset-manager. If the node was already added, this does
  174. * nothing and return 0. @pages is the size of the object given in number of
  175. * pages.
  176. * After this call succeeds, you can access the offset of the node until it
  177. * is removed again.
  178. *
  179. * If this call fails, it is safe to retry the operation or call
  180. * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
  181. * case.
  182. *
  183. * @pages is not required to be the same size as the underlying memory object
  184. * that you want to map. It only limits the size that user-space can map into
  185. * their address space.
  186. *
  187. * RETURNS:
  188. * 0 on success, negative error code on failure.
  189. */
  190. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  191. struct drm_vma_offset_node *node, unsigned long pages)
  192. {
  193. int ret = 0;
  194. write_lock(&mgr->vm_lock);
  195. if (!drm_mm_node_allocated(&node->vm_node))
  196. ret = drm_mm_insert_node(&mgr->vm_addr_space_mm,
  197. &node->vm_node, pages);
  198. write_unlock(&mgr->vm_lock);
  199. return ret;
  200. }
  201. EXPORT_SYMBOL(drm_vma_offset_add);
  202. /**
  203. * drm_vma_offset_remove() - Remove offset node from manager
  204. * @mgr: Manager object
  205. * @node: Node to be removed
  206. *
  207. * Remove a node from the offset manager. If the node wasn't added before, this
  208. * does nothing. After this call returns, the offset and size will be 0 until a
  209. * new offset is allocated via drm_vma_offset_add() again. Helper functions like
  210. * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
  211. * offset is allocated.
  212. */
  213. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  214. struct drm_vma_offset_node *node)
  215. {
  216. write_lock(&mgr->vm_lock);
  217. if (drm_mm_node_allocated(&node->vm_node)) {
  218. drm_mm_remove_node(&node->vm_node);
  219. memset(&node->vm_node, 0, sizeof(node->vm_node));
  220. }
  221. write_unlock(&mgr->vm_lock);
  222. }
  223. EXPORT_SYMBOL(drm_vma_offset_remove);
  224. static int vma_node_allow(struct drm_vma_offset_node *node,
  225. struct drm_file *tag, bool ref_counted)
  226. {
  227. struct rb_node **iter;
  228. struct rb_node *parent = NULL;
  229. struct drm_vma_offset_file *new, *entry;
  230. int ret = 0;
  231. /* Preallocate entry to avoid atomic allocations below. It is quite
  232. * unlikely that an open-file is added twice to a single node so we
  233. * don't optimize for this case. OOM is checked below only if the entry
  234. * is actually used. */
  235. new = kmalloc_obj(*entry);
  236. write_lock(&node->vm_lock);
  237. iter = &node->vm_files.rb_node;
  238. while (likely(*iter)) {
  239. parent = *iter;
  240. entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
  241. if (tag == entry->vm_tag) {
  242. if (ref_counted)
  243. entry->vm_count++;
  244. goto unlock;
  245. } else if (tag > entry->vm_tag) {
  246. iter = &(*iter)->rb_right;
  247. } else {
  248. iter = &(*iter)->rb_left;
  249. }
  250. }
  251. if (!new) {
  252. ret = -ENOMEM;
  253. goto unlock;
  254. }
  255. new->vm_tag = tag;
  256. new->vm_count = 1;
  257. rb_link_node(&new->vm_rb, parent, iter);
  258. rb_insert_color(&new->vm_rb, &node->vm_files);
  259. new = NULL;
  260. unlock:
  261. write_unlock(&node->vm_lock);
  262. kfree(new);
  263. return ret;
  264. }
  265. /**
  266. * drm_vma_node_allow - Add open-file to list of allowed users
  267. * @node: Node to modify
  268. * @tag: Tag of file to remove
  269. *
  270. * Add @tag to the list of allowed open-files for this node. If @tag 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 drm_file *tag)
  286. {
  287. return vma_node_allow(node, tag, true);
  288. }
  289. EXPORT_SYMBOL(drm_vma_node_allow);
  290. /**
  291. * drm_vma_node_allow_once - Add open-file to list of allowed users
  292. * @node: Node to modify
  293. * @tag: Tag of file to remove
  294. *
  295. * Add @tag to the list of allowed open-files for this node.
  296. *
  297. * The list of allowed-users is preserved across drm_vma_offset_add() and
  298. * drm_vma_offset_remove() calls. You may even call it if the node is currently
  299. * not added to any offset-manager.
  300. *
  301. * This is not ref-counted unlike drm_vma_node_allow() hence drm_vma_node_revoke()
  302. * should only be called once after this.
  303. *
  304. * This is locked against concurrent access internally.
  305. *
  306. * RETURNS:
  307. * 0 on success, negative error code on internal failure (out-of-mem)
  308. */
  309. int drm_vma_node_allow_once(struct drm_vma_offset_node *node, struct drm_file *tag)
  310. {
  311. return vma_node_allow(node, tag, false);
  312. }
  313. EXPORT_SYMBOL(drm_vma_node_allow_once);
  314. /**
  315. * drm_vma_node_revoke - Remove open-file from list of allowed users
  316. * @node: Node to modify
  317. * @tag: Tag of file to remove
  318. *
  319. * Decrement the ref-count of @tag in the list of allowed open-files on @node.
  320. * If the ref-count drops to zero, remove @tag from the list. You must call
  321. * this once for every drm_vma_node_allow() on @tag.
  322. *
  323. * This is locked against concurrent access internally.
  324. *
  325. * If @tag is not on the list, nothing is done.
  326. */
  327. void drm_vma_node_revoke(struct drm_vma_offset_node *node,
  328. struct drm_file *tag)
  329. {
  330. struct drm_vma_offset_file *entry;
  331. struct rb_node *iter;
  332. write_lock(&node->vm_lock);
  333. iter = node->vm_files.rb_node;
  334. while (likely(iter)) {
  335. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  336. if (tag == entry->vm_tag) {
  337. if (!--entry->vm_count) {
  338. rb_erase(&entry->vm_rb, &node->vm_files);
  339. kfree(entry);
  340. }
  341. break;
  342. } else if (tag > entry->vm_tag) {
  343. iter = iter->rb_right;
  344. } else {
  345. iter = iter->rb_left;
  346. }
  347. }
  348. write_unlock(&node->vm_lock);
  349. }
  350. EXPORT_SYMBOL(drm_vma_node_revoke);
  351. /**
  352. * drm_vma_node_is_allowed - Check whether an open-file is granted access
  353. * @node: Node to check
  354. * @tag: Tag of file to remove
  355. *
  356. * Search the list in @node whether @tag is currently on the list of allowed
  357. * open-files (see drm_vma_node_allow()).
  358. *
  359. * This is locked against concurrent access internally.
  360. *
  361. * RETURNS:
  362. * true if @filp is on the list
  363. */
  364. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  365. struct drm_file *tag)
  366. {
  367. struct drm_vma_offset_file *entry;
  368. struct rb_node *iter;
  369. read_lock(&node->vm_lock);
  370. iter = node->vm_files.rb_node;
  371. while (likely(iter)) {
  372. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  373. if (tag == entry->vm_tag)
  374. break;
  375. else if (tag > entry->vm_tag)
  376. iter = iter->rb_right;
  377. else
  378. iter = iter->rb_left;
  379. }
  380. read_unlock(&node->vm_lock);
  381. return iter;
  382. }
  383. EXPORT_SYMBOL(drm_vma_node_is_allowed);