i915_vm_bind.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ==========================================
  2. I915 VM_BIND feature design and use cases
  3. ==========================================
  4. VM_BIND feature
  5. ================
  6. DRM_I915_GEM_VM_BIND/UNBIND ioctls allows UMD to bind/unbind GEM buffer
  7. objects (BOs) or sections of a BOs at specified GPU virtual addresses on a
  8. specified address space (VM). These mappings (also referred to as persistent
  9. mappings) will be persistent across multiple GPU submissions (execbuf calls)
  10. issued by the UMD, without user having to provide a list of all required
  11. mappings during each submission (as required by older execbuf mode).
  12. The VM_BIND/UNBIND calls allow UMDs to request a timeline out fence for
  13. signaling the completion of bind/unbind operation.
  14. VM_BIND feature is advertised to user via I915_PARAM_VM_BIND_VERSION.
  15. User has to opt-in for VM_BIND mode of binding for an address space (VM)
  16. during VM creation time via I915_VM_CREATE_FLAGS_USE_VM_BIND extension.
  17. VM_BIND/UNBIND ioctl calls executed on different CPU threads concurrently are
  18. not ordered. Furthermore, parts of the VM_BIND/UNBIND operations can be done
  19. asynchronously, when valid out fence is specified.
  20. VM_BIND features include:
  21. * Multiple Virtual Address (VA) mappings can map to the same physical pages
  22. of an object (aliasing).
  23. * VA mapping can map to a partial section of the BO (partial binding).
  24. * Support capture of persistent mappings in the dump upon GPU error.
  25. * Support for userptr gem objects (no special uapi is required for this).
  26. TLB flush consideration
  27. ------------------------
  28. The i915 driver flushes the TLB for each submission and when an object's
  29. pages are released. The VM_BIND/UNBIND operation will not do any additional
  30. TLB flush. Any VM_BIND mapping added will be in the working set for subsequent
  31. submissions on that VM and will not be in the working set for currently running
  32. batches (which would require additional TLB flushes, which is not supported).
  33. Execbuf ioctl in VM_BIND mode
  34. -------------------------------
  35. A VM in VM_BIND mode will not support older execbuf mode of binding.
  36. The execbuf ioctl handling in VM_BIND mode differs significantly from the
  37. older execbuf2 ioctl (See struct drm_i915_gem_execbuffer2).
  38. Hence, a new execbuf3 ioctl has been added to support VM_BIND mode. (See
  39. struct drm_i915_gem_execbuffer3). The execbuf3 ioctl will not accept any
  40. execlist. Hence, no support for implicit sync. It is expected that the below
  41. work will be able to support requirements of object dependency setting in all
  42. use cases:
  43. "dma-buf: Add an API for exporting sync files"
  44. (https://lwn.net/Articles/859290/)
  45. The new execbuf3 ioctl only works in VM_BIND mode and the VM_BIND mode only
  46. works with execbuf3 ioctl for submission. All BOs mapped on that VM (through
  47. VM_BIND call) at the time of execbuf3 call are deemed required for that
  48. submission.
  49. The execbuf3 ioctl directly specifies the batch addresses instead of as
  50. object handles as in execbuf2 ioctl. The execbuf3 ioctl will also not
  51. support many of the older features like in/out/submit fences, fence array,
  52. default gem context and many more (See struct drm_i915_gem_execbuffer3).
  53. In VM_BIND mode, VA allocation is completely managed by the user instead of
  54. the i915 driver. Hence all VA assignment, eviction are not applicable in
  55. VM_BIND mode. Also, for determining object activeness, VM_BIND mode will not
  56. be using the i915_vma active reference tracking. It will instead use dma-resv
  57. object for that (See `VM_BIND dma_resv usage`_).
  58. So, a lot of existing code supporting execbuf2 ioctl, like relocations, VA
  59. evictions, vma lookup table, implicit sync, vma active reference tracking etc.,
  60. are not applicable for execbuf3 ioctl. Hence, all execbuf3 specific handling
  61. should be in a separate file and only functionalities common to these ioctls
  62. can be the shared code where possible.
  63. VM_PRIVATE objects
  64. -------------------
  65. By default, BOs can be mapped on multiple VMs and can also be dma-buf
  66. exported. Hence these BOs are referred to as Shared BOs.
  67. During each execbuf submission, the request fence must be added to the
  68. dma-resv fence list of all shared BOs mapped on the VM.
  69. VM_BIND feature introduces an optimization where user can create BO which
  70. is private to a specified VM via I915_GEM_CREATE_EXT_VM_PRIVATE flag during
  71. BO creation. Unlike Shared BOs, these VM private BOs can only be mapped on
  72. the VM they are private to and can't be dma-buf exported.
  73. All private BOs of a VM share the dma-resv object. Hence during each execbuf
  74. submission, they need only one dma-resv fence list updated. Thus, the fast
  75. path (where required mappings are already bound) submission latency is O(1)
  76. w.r.t the number of VM private BOs.
  77. VM_BIND locking hirarchy
  78. -------------------------
  79. The locking design here supports the older (execlist based) execbuf mode, the
  80. newer VM_BIND mode, the VM_BIND mode with GPU page faults and possible future
  81. system allocator support (See `Shared Virtual Memory (SVM) support`_).
  82. The older execbuf mode and the newer VM_BIND mode without page faults manages
  83. residency of backing storage using dma_fence. The VM_BIND mode with page faults
  84. and the system allocator support do not use any dma_fence at all.
  85. VM_BIND locking order is as below.
  86. 1) Lock-A: A vm_bind mutex will protect vm_bind lists. This lock is taken in
  87. vm_bind/vm_unbind ioctl calls, in the execbuf path and while releasing the
  88. mapping.
  89. In future, when GPU page faults are supported, we can potentially use a
  90. rwsem instead, so that multiple page fault handlers can take the read side
  91. lock to lookup the mapping and hence can run in parallel.
  92. The older execbuf mode of binding do not need this lock.
  93. 2) Lock-B: The object's dma-resv lock will protect i915_vma state and needs to
  94. be held while binding/unbinding a vma in the async worker and while updating
  95. dma-resv fence list of an object. Note that private BOs of a VM will all
  96. share a dma-resv object.
  97. The future system allocator support will use the HMM prescribed locking
  98. instead.
  99. 3) Lock-C: Spinlock/s to protect some of the VM's lists like the list of
  100. invalidated vmas (due to eviction and userptr invalidation) etc.
  101. When GPU page faults are supported, the execbuf path do not take any of these
  102. locks. There we will simply smash the new batch buffer address into the ring and
  103. then tell the scheduler run that. The lock taking only happens from the page
  104. fault handler, where we take lock-A in read mode, whichever lock-B we need to
  105. find the backing storage (dma_resv lock for gem objects, and hmm/core mm for
  106. system allocator) and some additional locks (lock-D) for taking care of page
  107. table races. Page fault mode should not need to ever manipulate the vm lists,
  108. so won't ever need lock-C.
  109. VM_BIND LRU handling
  110. ---------------------
  111. We need to ensure VM_BIND mapped objects are properly LRU tagged to avoid
  112. performance degradation. We will also need support for bulk LRU movement of
  113. VM_BIND objects to avoid additional latencies in execbuf path.
  114. The page table pages are similar to VM_BIND mapped objects (See
  115. `Evictable page table allocations`_) and are maintained per VM and needs to
  116. be pinned in memory when VM is made active (ie., upon an execbuf call with
  117. that VM). So, bulk LRU movement of page table pages is also needed.
  118. VM_BIND dma_resv usage
  119. -----------------------
  120. Fences needs to be added to all VM_BIND mapped objects. During each execbuf
  121. submission, they are added with DMA_RESV_USAGE_BOOKKEEP usage to prevent
  122. over sync (See enum dma_resv_usage). One can override it with either
  123. DMA_RESV_USAGE_READ or DMA_RESV_USAGE_WRITE usage during explicit object
  124. dependency setting.
  125. Note that DRM_I915_GEM_WAIT and DRM_I915_GEM_BUSY ioctls do not check for
  126. DMA_RESV_USAGE_BOOKKEEP usage and hence should not be used for end of batch
  127. check. Instead, the execbuf3 out fence should be used for end of batch check
  128. (See struct drm_i915_gem_execbuffer3).
  129. Also, in VM_BIND mode, use dma-resv apis for determining object activeness
  130. (See dma_resv_test_signaled() and dma_resv_wait_timeout()) and do not use the
  131. older i915_vma active reference tracking which is deprecated. This should be
  132. easier to get it working with the current TTM backend.
  133. Mesa use case
  134. --------------
  135. VM_BIND can potentially reduce the CPU overhead in Mesa (both Vulkan and Iris),
  136. hence improving performance of CPU-bound applications. It also allows us to
  137. implement Vulkan's Sparse Resources. With increasing GPU hardware performance,
  138. reducing CPU overhead becomes more impactful.
  139. Other VM_BIND use cases
  140. ========================
  141. Long running Compute contexts
  142. ------------------------------
  143. Usage of dma-fence expects that they complete in reasonable amount of time.
  144. Compute on the other hand can be long running. Hence it is appropriate for
  145. compute to use user/memory fence (See `User/Memory Fence`_) and dma-fence usage
  146. must be limited to in-kernel consumption only.
  147. Where GPU page faults are not available, kernel driver upon buffer invalidation
  148. will initiate a suspend (preemption) of long running context, finish the
  149. invalidation, revalidate the BO and then resume the compute context. This is
  150. done by having a per-context preempt fence which is enabled when someone tries
  151. to wait on it and triggers the context preemption.
  152. User/Memory Fence
  153. ~~~~~~~~~~~~~~~~~~
  154. User/Memory fence is a <address, value> pair. To signal the user fence, the
  155. specified value will be written at the specified virtual address and wakeup the
  156. waiting process. User fence can be signaled either by the GPU or kernel async
  157. worker (like upon bind completion). User can wait on a user fence with a new
  158. user fence wait ioctl.
  159. Here is some prior work on this:
  160. https://patchwork.freedesktop.org/patch/349417/
  161. Low Latency Submission
  162. ~~~~~~~~~~~~~~~~~~~~~~~
  163. Allows compute UMD to directly submit GPU jobs instead of through execbuf
  164. ioctl. This is made possible by VM_BIND is not being synchronized against
  165. execbuf. VM_BIND allows bind/unbind of mappings required for the directly
  166. submitted jobs.
  167. Debugger
  168. ---------
  169. With debug event interface user space process (debugger) is able to keep track
  170. of and act upon resources created by another process (debugged) and attached
  171. to GPU via vm_bind interface.
  172. GPU page faults
  173. ----------------
  174. GPU page faults when supported (in future), will only be supported in the
  175. VM_BIND mode. While both the older execbuf mode and the newer VM_BIND mode of
  176. binding will require using dma-fence to ensure residency, the GPU page faults
  177. mode when supported, will not use any dma-fence as residency is purely managed
  178. by installing and removing/invalidating page table entries.
  179. Page level hints settings
  180. --------------------------
  181. VM_BIND allows any hints setting per mapping instead of per BO. Possible hints
  182. include placement and atomicity. Sub-BO level placement hint will be even more
  183. relevant with upcoming GPU on-demand page fault support.
  184. Page level Cache/CLOS settings
  185. -------------------------------
  186. VM_BIND allows cache/CLOS settings per mapping instead of per BO.
  187. Evictable page table allocations
  188. ---------------------------------
  189. Make pagetable allocations evictable and manage them similar to VM_BIND
  190. mapped objects. Page table pages are similar to persistent mappings of a
  191. VM (difference here are that the page table pages will not have an i915_vma
  192. structure and after swapping pages back in, parent page link needs to be
  193. updated).
  194. Shared Virtual Memory (SVM) support
  195. ------------------------------------
  196. VM_BIND interface can be used to map system memory directly (without gem BO
  197. abstraction) using the HMM interface. SVM is only supported with GPU page
  198. faults enabled.
  199. VM_BIND UAPI
  200. =============
  201. .. kernel-doc:: Documentation/gpu/rfc/i915_vm_bind.h