gpusvm.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. .. SPDX-License-Identifier: (GPL-2.0+ OR MIT)
  2. ===============
  3. GPU SVM Section
  4. ===============
  5. Agreed upon design principles
  6. =============================
  7. * migrate_to_ram path
  8. * Rely only on core MM concepts (migration PTEs, page references, and
  9. page locking).
  10. * No driver specific locks other than locks for hardware interaction in
  11. this path. These are not required and generally a bad idea to
  12. invent driver defined locks to seal core MM races.
  13. * An example of a driver-specific lock causing issues occurred before
  14. fixing do_swap_page to lock the faulting page. A driver-exclusive lock
  15. in migrate_to_ram produced a stable livelock if enough threads read
  16. the faulting page.
  17. * Partial migration is supported (i.e., a subset of pages attempting to
  18. migrate can actually migrate, with only the faulting page guaranteed
  19. to migrate).
  20. * Driver handles mixed migrations via retry loops rather than locking.
  21. * Eviction
  22. * Eviction is defined as migrating data from the GPU back to the
  23. CPU without a virtual address to free up GPU memory.
  24. * Only looking at physical memory data structures and locks as opposed to
  25. looking at virtual memory data structures and locks.
  26. * No looking at mm/vma structs or relying on those being locked.
  27. * The rationale for the above two points is that CPU virtual addresses
  28. can change at any moment, while the physical pages remain stable.
  29. * GPU page table invalidation, which requires a GPU virtual address, is
  30. handled via the notifier that has access to the GPU virtual address.
  31. * GPU fault side
  32. * mmap_read only used around core MM functions which require this lock
  33. and should strive to take mmap_read lock only in GPU SVM layer.
  34. * Big retry loop to handle all races with the mmu notifier under the gpu
  35. pagetable locks/mmu notifier range lock/whatever we end up calling
  36. those.
  37. * Races (especially against concurrent eviction or migrate_to_ram)
  38. should not be handled on the fault side by trying to hold locks;
  39. rather, they should be handled using retry loops. One possible
  40. exception is holding a BO's dma-resv lock during the initial migration
  41. to VRAM, as this is a well-defined lock that can be taken underneath
  42. the mmap_read lock.
  43. * One possible issue with the above approach is if a driver has a strict
  44. migration policy requiring GPU access to occur in GPU memory.
  45. Concurrent CPU access could cause a livelock due to endless retries.
  46. While no current user (Xe) of GPU SVM has such a policy, it is likely
  47. to be added in the future. Ideally, this should be resolved on the
  48. core-MM side rather than through a driver-side lock.
  49. * Physical memory to virtual backpointer
  50. * This does not work, as no pointers from physical memory to virtual
  51. memory should exist. mremap() is an example of the core MM updating
  52. the virtual address without notifying the driver of address
  53. change rather the driver only receiving the invalidation notifier.
  54. * The physical memory backpointer (page->zone_device_data) should remain
  55. stable from allocation to page free. Safely updating this against a
  56. concurrent user would be very difficult unless the page is free.
  57. * GPU pagetable locking
  58. * Notifier lock only protects range tree, pages valid state for a range
  59. (rather than seqno due to wider notifiers), pagetable entries, and
  60. mmu notifier seqno tracking, it is not a global lock to protect
  61. against races.
  62. * All races handled with big retry as mentioned above.
  63. Overview of baseline design
  64. ===========================
  65. .. kernel-doc:: drivers/gpu/drm/drm_gpusvm.c
  66. :doc: Overview
  67. .. kernel-doc:: drivers/gpu/drm/drm_gpusvm.c
  68. :doc: Locking
  69. .. kernel-doc:: drivers/gpu/drm/drm_gpusvm.c
  70. :doc: Partial Unmapping of Ranges
  71. .. kernel-doc:: drivers/gpu/drm/drm_gpusvm.c
  72. :doc: Examples
  73. Overview of drm_pagemap design
  74. ==============================
  75. .. kernel-doc:: drivers/gpu/drm/drm_pagemap.c
  76. :doc: Overview
  77. .. kernel-doc:: drivers/gpu/drm/drm_pagemap.c
  78. :doc: Migration
  79. Possible future design features
  80. ===============================
  81. * Concurrent GPU faults
  82. * CPU faults are concurrent so makes sense to have concurrent GPU
  83. faults.
  84. * Should be possible with fined grained locking in the driver GPU
  85. fault handler.
  86. * No expected GPU SVM changes required.
  87. * Ranges with mixed system and device pages
  88. * Can be added if required to drm_gpusvm_get_pages fairly easily.
  89. * Multi-GPU support
  90. * Work in progress and patches expected after initially landing on GPU
  91. SVM.
  92. * Ideally can be done with little to no changes to GPU SVM.
  93. * Drop ranges in favor of radix tree
  94. * May be desirable for faster notifiers.
  95. * Compound device pages
  96. * Nvidia, AMD, and Intel all have agreed expensive core MM functions in
  97. migrate device layer are a performance bottleneck, having compound
  98. device pages should help increase performance by reducing the number
  99. of these expensive calls.
  100. * Higher order dma mapping for migration
  101. * 4k dma mapping adversely affects migration performance on Intel
  102. hardware, higher order (2M) dma mapping should help here.
  103. * Build common userptr implementation on top of GPU SVM
  104. * Driver side madvise implementation and migration policies
  105. * Pull in pending dma-mapping API changes from Leon / Nvidia when these land