drm_vma_manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <[email protected]>
  5. * Copyright (c) 2013 David Herrmann <[email protected]>
  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/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/rbtree.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/types.h>
  31. #include <drm/drm_mm.h>
  32. #include <drm/drm_vma_manager.h>
  33. /**
  34. * DOC: vma offset manager
  35. *
  36. * The vma-manager is responsible to map arbitrary driver-dependent memory
  37. * regions into the linear user address-space. It provides offsets to the
  38. * caller which can then be used on the address_space of the drm-device. It
  39. * takes care to not overlap regions, size them appropriately and to not
  40. * confuse mm-core by inconsistent fake vm_pgoff fields.
  41. * Drivers shouldn't use this for object placement in VMEM. This manager should
  42. * only be used to manage mappings into linear user-space VMs.
  43. *
  44. * We use drm_mm as backend to manage object allocations. But it is highly
  45. * optimized for alloc/free calls, not lookups. Hence, we use an rb-tree to
  46. * speed up offset lookups.
  47. *
  48. * You must not use multiple offset managers on a single address_space.
  49. * Otherwise, mm-core will be unable to tear down memory mappings as the VM will
  50. * no longer be linear.
  51. *
  52. * This offset manager works on page-based addresses. That is, every argument
  53. * and return code (with the exception of drm_vma_node_offset_addr()) is given
  54. * in number of pages, not number of bytes. That means, object sizes and offsets
  55. * must always be page-aligned (as usual).
  56. * If you want to get a valid byte-based user-space address for a given offset,
  57. * please see drm_vma_node_offset_addr().
  58. *
  59. * Additionally to offset management, the vma offset manager also handles access
  60. * management. For every open-file context that is allowed to access a given
  61. * node, you must call drm_vma_node_allow(). Otherwise, an mmap() call on this
  62. * open-file with the offset of the node will fail with -EACCES. To revoke
  63. * access again, use drm_vma_node_revoke(). However, the caller is responsible
  64. * for destroying already existing mappings, if required.
  65. */
  66. /**
  67. * drm_vma_offset_manager_init - Initialize new offset-manager
  68. * @mgr: Manager object
  69. * @page_offset: Offset of available memory area (page-based)
  70. * @size: Size of available address space range (page-based)
  71. *
  72. * Initialize a new offset-manager. The offset and area size available for the
  73. * manager are given as @page_offset and @size. Both are interpreted as
  74. * page-numbers, not bytes.
  75. *
  76. * Adding/removing nodes from the manager is locked internally and protected
  77. * against concurrent access. However, node allocation and destruction is left
  78. * for the caller. While calling into the vma-manager, a given node must
  79. * always be guaranteed to be referenced.
  80. */
  81. void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
  82. unsigned long page_offset, unsigned long size)
  83. {
  84. rwlock_init(&mgr->vm_lock);
  85. drm_mm_init(&mgr->vm_addr_space_mm, page_offset, size);
  86. }
  87. EXPORT_SYMBOL(drm_vma_offset_manager_init);
  88. /**
  89. * drm_vma_offset_manager_destroy() - Destroy offset manager
  90. * @mgr: Manager object
  91. *
  92. * Destroy an object manager which was previously created via
  93. * drm_vma_offset_manager_init(). The caller must remove all allocated nodes
  94. * before destroying the manager. Otherwise, drm_mm will refuse to free the
  95. * requested resources.
  96. *
  97. * The manager must not be accessed after this function is called.
  98. */
  99. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
  100. {
  101. drm_mm_takedown(&mgr->vm_addr_space_mm);
  102. }
  103. EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
  104. /**
  105. * drm_vma_offset_lookup_locked() - Find node in offset space
  106. * @mgr: Manager object
  107. * @start: Start address for object (page-based)
  108. * @pages: Size of object (page-based)
  109. *
  110. * Find a node given a start address and object size. This returns the _best_
  111. * match for the given node. That is, @start may point somewhere into a valid
  112. * region and the given node will be returned, as long as the node spans the
  113. * whole requested area (given the size in number of pages as @pages).
  114. *
  115. * Note that before lookup the vma offset manager lookup lock must be acquired
  116. * with drm_vma_offset_lock_lookup(). See there for an example. This can then be
  117. * used to implement weakly referenced lookups using kref_get_unless_zero().
  118. *
  119. * Example:
  120. *
  121. * ::
  122. *
  123. * drm_vma_offset_lock_lookup(mgr);
  124. * node = drm_vma_offset_lookup_locked(mgr);
  125. * if (node)
  126. * kref_get_unless_zero(container_of(node, sth, entr));
  127. * drm_vma_offset_unlock_lookup(mgr);
  128. *
  129. * RETURNS:
  130. * Returns NULL if no suitable node can be found. Otherwise, the best match
  131. * is returned. It's the caller's responsibility to make sure the node doesn't
  132. * get destroyed before the caller can access it.
  133. */
  134. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  135. unsigned long start,
  136. unsigned long pages)
  137. {
  138. struct drm_mm_node *node, *best;
  139. struct rb_node *iter;
  140. unsigned long offset;
  141. iter = mgr->vm_addr_space_mm.interval_tree.rb_root.rb_node;
  142. best = NULL;
  143. while (likely(iter)) {
  144. node = rb_entry(iter, struct drm_mm_node, rb);
  145. offset = node->start;
  146. if (start >= offset) {
  147. iter = iter->rb_right;
  148. best = node;
  149. if (start == offset)
  150. break;
  151. } else {
  152. iter = iter->rb_left;
  153. }
  154. }
  155. /* verify that the node spans the requested area */
  156. if (best) {
  157. offset = best->start + best->size;
  158. if (offset < start + pages)
  159. best = NULL;
  160. }
  161. if (!best)
  162. return NULL;
  163. return container_of(best, struct drm_vma_offset_node, vm_node);
  164. }
  165. EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
  166. /**
  167. * drm_vma_offset_add() - Add offset node to manager
  168. * @mgr: Manager object
  169. * @node: Node to be added
  170. * @pages: Allocation size visible to user-space (in number of pages)
  171. *
  172. * Add a node to the offset-manager. If the node was already added, this does
  173. * nothing and return 0. @pages is the size of the object given in number of
  174. * pages.
  175. * After this call succeeds, you can access the offset of the node until it
  176. * is removed again.
  177. *
  178. * If this call fails, it is safe to retry the operation or call
  179. * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
  180. * case.
  181. *
  182. * @pages is not required to be the same size as the underlying memory object
  183. * that you want to map. It only limits the size that user-space can map into
  184. * their address space.
  185. *
  186. * RETURNS:
  187. * 0 on success, negative error code on failure.
  188. */
  189. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  190. struct drm_vma_offset_node *node, unsigned long pages)
  191. {
  192. int ret = 0;
  193. write_lock(&mgr->vm_lock);
  194. if (!drm_mm_node_allocated(&node->vm_node))
  195. ret = drm_mm_insert_node(&mgr->vm_addr_space_mm,
  196. &node->vm_node, pages);
  197. write_unlock(&mgr->vm_lock);
  198. return ret;
  199. }
  200. EXPORT_SYMBOL(drm_vma_offset_add);
  201. /**
  202. * drm_vma_offset_remove() - Remove offset node from manager
  203. * @mgr: Manager object
  204. * @node: Node to be removed
  205. *
  206. * Remove a node from the offset manager. If the node wasn't added before, this
  207. * does nothing. After this call returns, the offset and size will be 0 until a
  208. * new offset is allocated via drm_vma_offset_add() again. Helper functions like
  209. * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
  210. * offset is allocated.
  211. */
  212. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  213. struct drm_vma_offset_node *node)
  214. {
  215. write_lock(&mgr->vm_lock);
  216. if (drm_mm_node_allocated(&node->vm_node)) {
  217. drm_mm_remove_node(&node->vm_node);
  218. memset(&node->vm_node, 0, sizeof(node->vm_node));
  219. }
  220. write_unlock(&mgr->vm_lock);
  221. }
  222. EXPORT_SYMBOL(drm_vma_offset_remove);
  223. static int vma_node_allow(struct drm_vma_offset_node *node,
  224. struct drm_file *tag, bool ref_counted)
  225. {
  226. struct rb_node **iter;
  227. struct rb_node *parent = NULL;
  228. struct drm_vma_offset_file *new, *entry;
  229. int ret = 0;
  230. /* Preallocate entry to avoid atomic allocations below. It is quite
  231. * unlikely that an open-file is added twice to a single node so we
  232. * don't optimize for this case. OOM is checked below only if the entry
  233. * is actually used. */
  234. new = kmalloc(sizeof(*entry), GFP_KERNEL);
  235. write_lock(&node->vm_lock);
  236. iter = &node->vm_files.rb_node;
  237. while (likely(*iter)) {
  238. parent = *iter;
  239. entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
  240. if (tag == entry->vm_tag) {
  241. if (ref_counted)
  242. entry->vm_count++;
  243. goto unlock;
  244. } else if (tag > entry->vm_tag) {
  245. iter = &(*iter)->rb_right;
  246. } else {
  247. iter = &(*iter)->rb_left;
  248. }
  249. }
  250. if (!new) {
  251. ret = -ENOMEM;
  252. goto unlock;
  253. }
  254. new->vm_tag = tag;
  255. new->vm_count = 1;
  256. rb_link_node(&new->vm_rb, parent, iter);
  257. rb_insert_color(&new->vm_rb, &node->vm_files);
  258. new = NULL;
  259. unlock:
  260. write_unlock(&node->vm_lock);
  261. kfree(new);
  262. return ret;
  263. }
  264. /**
  265. * drm_vma_node_allow - Add open-file to list of allowed users
  266. * @node: Node to modify
  267. * @tag: Tag of file to remove
  268. *
  269. * Add @tag to the list of allowed open-files for this node. If @tag 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 drm_file *tag)
  285. {
  286. return vma_node_allow(node, tag, true);
  287. }
  288. EXPORT_SYMBOL(drm_vma_node_allow);
  289. /**
  290. * drm_vma_node_allow_once - Add open-file to list of allowed users
  291. * @node: Node to modify
  292. * @tag: Tag of file to remove
  293. *
  294. * Add @tag to the list of allowed open-files for this node.
  295. *
  296. * The list of allowed-users is preserved across drm_vma_offset_add() and
  297. * drm_vma_offset_remove() calls. You may even call it if the node is currently
  298. * not added to any offset-manager.
  299. *
  300. * This is not ref-counted unlike drm_vma_node_allow() hence drm_vma_node_revoke()
  301. * should only be called once after this.
  302. *
  303. * This is locked against concurrent access internally.
  304. *
  305. * RETURNS:
  306. * 0 on success, negative error code on internal failure (out-of-mem)
  307. */
  308. int drm_vma_node_allow_once(struct drm_vma_offset_node *node, struct drm_file *tag)
  309. {
  310. return vma_node_allow(node, tag, false);
  311. }
  312. EXPORT_SYMBOL(drm_vma_node_allow_once);
  313. /**
  314. * drm_vma_node_revoke - Remove open-file from list of allowed users
  315. * @node: Node to modify
  316. * @tag: Tag of file to remove
  317. *
  318. * Decrement the ref-count of @tag in the list of allowed open-files on @node.
  319. * If the ref-count drops to zero, remove @tag from the list. You must call
  320. * this once for every drm_vma_node_allow() on @tag.
  321. *
  322. * This is locked against concurrent access internally.
  323. *
  324. * If @tag is not on the list, nothing is done.
  325. */
  326. void drm_vma_node_revoke(struct drm_vma_offset_node *node,
  327. struct drm_file *tag)
  328. {
  329. struct drm_vma_offset_file *entry;
  330. struct rb_node *iter;
  331. write_lock(&node->vm_lock);
  332. iter = node->vm_files.rb_node;
  333. while (likely(iter)) {
  334. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  335. if (tag == entry->vm_tag) {
  336. if (!--entry->vm_count) {
  337. rb_erase(&entry->vm_rb, &node->vm_files);
  338. kfree(entry);
  339. }
  340. break;
  341. } else if (tag > entry->vm_tag) {
  342. iter = iter->rb_right;
  343. } else {
  344. iter = iter->rb_left;
  345. }
  346. }
  347. write_unlock(&node->vm_lock);
  348. }
  349. EXPORT_SYMBOL(drm_vma_node_revoke);
  350. /**
  351. * drm_vma_node_is_allowed - Check whether an open-file is granted access
  352. * @node: Node to check
  353. * @tag: Tag of file to remove
  354. *
  355. * Search the list in @node whether @tag is currently on the list of allowed
  356. * open-files (see drm_vma_node_allow()).
  357. *
  358. * This is locked against concurrent access internally.
  359. *
  360. * RETURNS:
  361. * true if @filp is on the list
  362. */
  363. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  364. struct drm_file *tag)
  365. {
  366. struct drm_vma_offset_file *entry;
  367. struct rb_node *iter;
  368. read_lock(&node->vm_lock);
  369. iter = node->vm_files.rb_node;
  370. while (likely(iter)) {
  371. entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
  372. if (tag == entry->vm_tag)
  373. break;
  374. else if (tag > entry->vm_tag)
  375. iter = iter->rb_right;
  376. else
  377. iter = iter->rb_left;
  378. }
  379. read_unlock(&node->vm_lock);
  380. return iter;
  381. }
  382. EXPORT_SYMBOL(drm_vma_node_is_allowed);