amdgpu_amdkfd_fence.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2016-2018 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/dma-fence.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/atomic.h>
  25. #include <linux/stacktrace.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched/mm.h>
  29. #include "amdgpu_amdkfd.h"
  30. static const struct dma_fence_ops amdkfd_fence_ops;
  31. static atomic_t fence_seq = ATOMIC_INIT(0);
  32. /* Eviction Fence
  33. * Fence helper functions to deal with KFD memory eviction.
  34. * Big Idea - Since KFD submissions are done by user queues, a BO cannot be
  35. * evicted unless all the user queues for that process are evicted.
  36. *
  37. * All the BOs in a process share an eviction fence. When process X wants
  38. * to map VRAM memory but TTM can't find enough space, TTM will attempt to
  39. * evict BOs from its LRU list. TTM checks if the BO is valuable to evict
  40. * by calling ttm_bo_driver->eviction_valuable().
  41. *
  42. * ttm_bo_driver->eviction_valuable() - will return false if the BO belongs
  43. * to process X. Otherwise, it will return true to indicate BO can be
  44. * evicted by TTM.
  45. *
  46. * If ttm_bo_driver->eviction_valuable returns true, then TTM will continue
  47. * the evcition process for that BO by calling ttm_bo_evict --> amdgpu_bo_move
  48. * --> amdgpu_copy_buffer(). This sets up job in GPU scheduler.
  49. *
  50. * GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to
  51. * nofity when the BO is free to move. fence_add_callback --> enable_signaling
  52. * --> amdgpu_amdkfd_fence.enable_signaling
  53. *
  54. * amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
  55. * user queues and signal fence. The work item will also start another delayed
  56. * work item to restore BOs
  57. */
  58. struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
  59. struct mm_struct *mm)
  60. {
  61. struct amdgpu_amdkfd_fence *fence;
  62. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  63. if (fence == NULL)
  64. return NULL;
  65. /* This reference gets released in amdkfd_fence_release */
  66. mmgrab(mm);
  67. fence->mm = mm;
  68. get_task_comm(fence->timeline_name, current);
  69. spin_lock_init(&fence->lock);
  70. dma_fence_init(&fence->base, &amdkfd_fence_ops, &fence->lock,
  71. context, atomic_inc_return(&fence_seq));
  72. return fence;
  73. }
  74. struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
  75. {
  76. struct amdgpu_amdkfd_fence *fence;
  77. if (!f)
  78. return NULL;
  79. fence = container_of(f, struct amdgpu_amdkfd_fence, base);
  80. if (fence && f->ops == &amdkfd_fence_ops)
  81. return fence;
  82. return NULL;
  83. }
  84. static const char *amdkfd_fence_get_driver_name(struct dma_fence *f)
  85. {
  86. return "amdgpu_amdkfd_fence";
  87. }
  88. static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
  89. {
  90. struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
  91. return fence->timeline_name;
  92. }
  93. /**
  94. * amdkfd_fence_enable_signaling - This gets called when TTM wants to evict
  95. * a KFD BO and schedules a job to move the BO.
  96. * If fence is already signaled return true.
  97. * If fence is not signaled schedule a evict KFD process work item.
  98. */
  99. static bool amdkfd_fence_enable_signaling(struct dma_fence *f)
  100. {
  101. struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
  102. if (!fence)
  103. return false;
  104. if (dma_fence_is_signaled(f))
  105. return true;
  106. if (!kgd2kfd->schedule_evict_and_restore_process(fence->mm, f))
  107. return true;
  108. return false;
  109. }
  110. /**
  111. * amdkfd_fence_release - callback that fence can be freed
  112. *
  113. * @fence: fence
  114. *
  115. * This function is called when the reference count becomes zero.
  116. * Drops the mm_struct reference and RCU schedules freeing up the fence.
  117. */
  118. static void amdkfd_fence_release(struct dma_fence *f)
  119. {
  120. struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
  121. /* Unconditionally signal the fence. The process is getting
  122. * terminated.
  123. */
  124. if (WARN_ON(!fence))
  125. return; /* Not an amdgpu_amdkfd_fence */
  126. mmdrop(fence->mm);
  127. kfree_rcu(f, rcu);
  128. }
  129. /**
  130. * amdkfd_fence_check_mm - Check if @mm is same as that of the fence @f
  131. * if same return TRUE else return FALSE.
  132. *
  133. * @f: [IN] fence
  134. * @mm: [IN] mm that needs to be verified
  135. */
  136. bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm)
  137. {
  138. struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
  139. if (!fence)
  140. return false;
  141. else if (fence->mm == mm)
  142. return true;
  143. return false;
  144. }
  145. static const struct dma_fence_ops amdkfd_fence_ops = {
  146. .get_driver_name = amdkfd_fence_get_driver_name,
  147. .get_timeline_name = amdkfd_fence_get_timeline_name,
  148. .enable_signaling = amdkfd_fence_enable_signaling,
  149. .signaled = NULL,
  150. .wait = dma_fence_default_wait,
  151. .release = amdkfd_fence_release,
  152. };