Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/log2.h> |
| 25 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 26 | #include <linux/sched/mm.h> |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 27 | #include <linux/sched/task.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 28 | #include <linux/slab.h> |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 29 | #include <linux/amd-iommu.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 30 | #include <linux/notifier.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 31 | #include <linux/compat.h> |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 32 | #include <linux/mman.h> |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 33 | #include <linux/file.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 34 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 35 | struct mm_struct; |
| 36 | |
| 37 | #include "kfd_priv.h" |
Felix Kuehling | 403575c | 2018-02-06 20:32:44 -0500 | [diff] [blame] | 38 | #include "kfd_device_queue_manager.h" |
Ben Goz | c3447e8 | 2015-05-20 18:05:44 +0300 | [diff] [blame] | 39 | #include "kfd_dbgmgr.h" |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 40 | #include "kfd_iommu.h" |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 41 | |
| 42 | /* |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 43 | * List of struct kfd_process (field kfd_process). |
| 44 | * Unique/indexed by mm_struct* |
| 45 | */ |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 46 | DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 47 | static DEFINE_MUTEX(kfd_processes_mutex); |
| 48 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 49 | DEFINE_SRCU(kfd_processes_srcu); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 50 | |
| 51 | static struct workqueue_struct *kfd_process_wq; |
| 52 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 53 | static struct kfd_process *find_process(const struct task_struct *thread); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 54 | static void kfd_process_ref_release(struct kref *ref); |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 55 | static struct kfd_process *create_process(const struct task_struct *thread, |
| 56 | struct file *filep); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 57 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 58 | static void evict_process_worker(struct work_struct *work); |
| 59 | static void restore_process_worker(struct work_struct *work); |
| 60 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 61 | |
| 62 | void kfd_process_create_wq(void) |
| 63 | { |
| 64 | if (!kfd_process_wq) |
Bhaktipriya Shridhar | fd320bf | 2016-05-29 21:14:11 +0530 | [diff] [blame] | 65 | kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void kfd_process_destroy_wq(void) |
| 69 | { |
| 70 | if (kfd_process_wq) { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 71 | destroy_workqueue(kfd_process_wq); |
| 72 | kfd_process_wq = NULL; |
| 73 | } |
| 74 | } |
| 75 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 76 | static void kfd_process_free_gpuvm(struct kgd_mem *mem, |
| 77 | struct kfd_process_device *pdd) |
| 78 | { |
| 79 | struct kfd_dev *dev = pdd->dev; |
| 80 | |
| 81 | dev->kfd2kgd->unmap_memory_to_gpu(dev->kgd, mem, pdd->vm); |
| 82 | dev->kfd2kgd->free_memory_of_gpu(dev->kgd, mem); |
| 83 | } |
| 84 | |
| 85 | /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process |
| 86 | * This function should be only called right after the process |
| 87 | * is created and when kfd_processes_mutex is still being held |
| 88 | * to avoid concurrency. Because of that exclusiveness, we do |
| 89 | * not need to take p->mutex. |
| 90 | */ |
| 91 | static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd, |
| 92 | uint64_t gpu_va, uint32_t size, |
| 93 | uint32_t flags, void **kptr) |
| 94 | { |
| 95 | struct kfd_dev *kdev = pdd->dev; |
| 96 | struct kgd_mem *mem = NULL; |
| 97 | int handle; |
| 98 | int err; |
| 99 | |
| 100 | err = kdev->kfd2kgd->alloc_memory_of_gpu(kdev->kgd, gpu_va, size, |
| 101 | pdd->vm, &mem, NULL, flags); |
| 102 | if (err) |
| 103 | goto err_alloc_mem; |
| 104 | |
| 105 | err = kdev->kfd2kgd->map_memory_to_gpu(kdev->kgd, mem, pdd->vm); |
| 106 | if (err) |
| 107 | goto err_map_mem; |
| 108 | |
| 109 | err = kdev->kfd2kgd->sync_memory(kdev->kgd, mem, true); |
| 110 | if (err) { |
| 111 | pr_debug("Sync memory failed, wait interrupted by user signal\n"); |
| 112 | goto sync_memory_failed; |
| 113 | } |
| 114 | |
| 115 | /* Create an obj handle so kfd_process_device_remove_obj_handle |
| 116 | * will take care of the bo removal when the process finishes. |
| 117 | * We do not need to take p->mutex, because the process is just |
| 118 | * created and the ioctls have not had the chance to run. |
| 119 | */ |
| 120 | handle = kfd_process_device_create_obj_handle(pdd, mem); |
| 121 | |
| 122 | if (handle < 0) { |
| 123 | err = handle; |
| 124 | goto free_gpuvm; |
| 125 | } |
| 126 | |
| 127 | if (kptr) { |
| 128 | err = kdev->kfd2kgd->map_gtt_bo_to_kernel(kdev->kgd, |
| 129 | (struct kgd_mem *)mem, kptr, NULL); |
| 130 | if (err) { |
| 131 | pr_debug("Map GTT BO to kernel failed\n"); |
| 132 | goto free_obj_handle; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return err; |
| 137 | |
| 138 | free_obj_handle: |
| 139 | kfd_process_device_remove_obj_handle(pdd, handle); |
| 140 | free_gpuvm: |
| 141 | sync_memory_failed: |
| 142 | kfd_process_free_gpuvm(mem, pdd); |
| 143 | return err; |
| 144 | |
| 145 | err_map_mem: |
| 146 | kdev->kfd2kgd->free_memory_of_gpu(kdev->kgd, mem); |
| 147 | err_alloc_mem: |
| 148 | *kptr = NULL; |
| 149 | return err; |
| 150 | } |
| 151 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 152 | struct kfd_process *kfd_create_process(struct file *filep) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 153 | { |
| 154 | struct kfd_process *process; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 155 | struct task_struct *thread = current; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 156 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 157 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 158 | return ERR_PTR(-EINVAL); |
| 159 | |
| 160 | /* Only the pthreads threading model is supported. */ |
| 161 | if (thread->group_leader->mm != thread->mm) |
| 162 | return ERR_PTR(-EINVAL); |
| 163 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 164 | /* |
| 165 | * take kfd processes mutex before starting of process creation |
| 166 | * so there won't be a case where two threads of the same process |
| 167 | * create two kfd_process structures |
| 168 | */ |
| 169 | mutex_lock(&kfd_processes_mutex); |
| 170 | |
| 171 | /* A prior open of /dev/kfd could have already created the process. */ |
| 172 | process = find_process(thread); |
| 173 | if (process) |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 174 | pr_debug("Process already found\n"); |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 175 | else |
| 176 | process = create_process(thread, filep); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 177 | |
| 178 | mutex_unlock(&kfd_processes_mutex); |
| 179 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 180 | return process; |
| 181 | } |
| 182 | |
| 183 | struct kfd_process *kfd_get_process(const struct task_struct *thread) |
| 184 | { |
| 185 | struct kfd_process *process; |
| 186 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 187 | if (!thread->mm) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 188 | return ERR_PTR(-EINVAL); |
| 189 | |
| 190 | /* Only the pthreads threading model is supported. */ |
| 191 | if (thread->group_leader->mm != thread->mm) |
| 192 | return ERR_PTR(-EINVAL); |
| 193 | |
| 194 | process = find_process(thread); |
| 195 | |
| 196 | return process; |
| 197 | } |
| 198 | |
| 199 | static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) |
| 200 | { |
| 201 | struct kfd_process *process; |
| 202 | |
| 203 | hash_for_each_possible_rcu(kfd_processes_table, process, |
| 204 | kfd_processes, (uintptr_t)mm) |
| 205 | if (process->mm == mm) |
| 206 | return process; |
| 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | static struct kfd_process *find_process(const struct task_struct *thread) |
| 212 | { |
| 213 | struct kfd_process *p; |
| 214 | int idx; |
| 215 | |
| 216 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 217 | p = find_process_by_mm(thread->mm); |
| 218 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 219 | |
| 220 | return p; |
| 221 | } |
| 222 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 223 | void kfd_unref_process(struct kfd_process *p) |
| 224 | { |
| 225 | kref_put(&p->ref, kfd_process_ref_release); |
| 226 | } |
| 227 | |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 228 | static void kfd_process_device_free_bos(struct kfd_process_device *pdd) |
| 229 | { |
| 230 | struct kfd_process *p = pdd->process; |
| 231 | void *mem; |
| 232 | int id; |
| 233 | |
| 234 | /* |
| 235 | * Remove all handles from idr and release appropriate |
| 236 | * local memory object |
| 237 | */ |
| 238 | idr_for_each_entry(&pdd->alloc_idr, mem, id) { |
| 239 | struct kfd_process_device *peer_pdd; |
| 240 | |
| 241 | list_for_each_entry(peer_pdd, &p->per_device_data, |
| 242 | per_device_list) { |
| 243 | if (!peer_pdd->vm) |
| 244 | continue; |
| 245 | peer_pdd->dev->kfd2kgd->unmap_memory_to_gpu( |
| 246 | peer_pdd->dev->kgd, mem, peer_pdd->vm); |
| 247 | } |
| 248 | |
| 249 | pdd->dev->kfd2kgd->free_memory_of_gpu(pdd->dev->kgd, mem); |
| 250 | kfd_process_device_remove_obj_handle(pdd, id); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p) |
| 255 | { |
| 256 | struct kfd_process_device *pdd; |
| 257 | |
| 258 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 259 | kfd_process_device_free_bos(pdd); |
| 260 | } |
| 261 | |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 262 | static void kfd_process_destroy_pdds(struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 263 | { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 264 | struct kfd_process_device *pdd, *temp; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 265 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 266 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 267 | per_device_list) { |
| 268 | pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n", |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 269 | pdd->dev->id, p->pasid); |
| 270 | |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 271 | if (pdd->drm_file) |
| 272 | fput(pdd->drm_file); |
| 273 | else if (pdd->vm) |
Felix Kuehling | 403575c | 2018-02-06 20:32:44 -0500 | [diff] [blame] | 274 | pdd->dev->kfd2kgd->destroy_process_vm( |
| 275 | pdd->dev->kgd, pdd->vm); |
| 276 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 277 | list_del(&pdd->per_device_list); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 278 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 279 | if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base) |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 280 | free_pages((unsigned long)pdd->qpd.cwsr_kaddr, |
| 281 | get_order(KFD_CWSR_TBA_TMA_SIZE)); |
| 282 | |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 283 | idr_destroy(&pdd->alloc_idr); |
| 284 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 285 | kfree(pdd); |
| 286 | } |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* No process locking is needed in this function, because the process |
| 290 | * is not findable any more. We must assume that no other thread is |
| 291 | * using it any more, otherwise we couldn't safely free the process |
| 292 | * structure in the end. |
| 293 | */ |
| 294 | static void kfd_process_wq_release(struct work_struct *work) |
| 295 | { |
| 296 | struct kfd_process *p = container_of(work, struct kfd_process, |
| 297 | release_work); |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 298 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 299 | kfd_iommu_unbind_process(p); |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 300 | |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 301 | kfd_process_free_outstanding_kfd_bos(p); |
| 302 | |
Felix Kuehling | de1450a | 2017-11-27 18:29:55 -0500 | [diff] [blame] | 303 | kfd_process_destroy_pdds(p); |
Felix Kuehling | 403575c | 2018-02-06 20:32:44 -0500 | [diff] [blame] | 304 | dma_fence_put(p->ef); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 305 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 306 | kfd_event_free_process(p); |
| 307 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 308 | kfd_pasid_free(p->pasid); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 309 | kfd_free_process_doorbells(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 310 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 311 | mutex_destroy(&p->mutex); |
| 312 | |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 313 | put_task_struct(p->lead_thread); |
| 314 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 315 | kfree(p); |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 316 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 317 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 318 | static void kfd_process_ref_release(struct kref *ref) |
| 319 | { |
| 320 | struct kfd_process *p = container_of(ref, struct kfd_process, ref); |
| 321 | |
| 322 | INIT_WORK(&p->release_work, kfd_process_wq_release); |
| 323 | queue_work(kfd_process_wq, &p->release_work); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void kfd_process_destroy_delayed(struct rcu_head *rcu) |
| 327 | { |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 328 | struct kfd_process *p = container_of(rcu, struct kfd_process, rcu); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 329 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 330 | kfd_unref_process(p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void kfd_process_notifier_release(struct mmu_notifier *mn, |
| 334 | struct mm_struct *mm) |
| 335 | { |
| 336 | struct kfd_process *p; |
Ben Goz | a82918f | 2015-03-25 13:12:20 +0200 | [diff] [blame] | 337 | struct kfd_process_device *pdd = NULL; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * The kfd_process structure can not be free because the |
| 341 | * mmu_notifier srcu is read locked |
| 342 | */ |
| 343 | p = container_of(mn, struct kfd_process, mmu_notifier); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 344 | if (WARN_ON(p->mm != mm)) |
| 345 | return; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 346 | |
| 347 | mutex_lock(&kfd_processes_mutex); |
| 348 | hash_del_rcu(&p->kfd_processes); |
| 349 | mutex_unlock(&kfd_processes_mutex); |
| 350 | synchronize_srcu(&kfd_processes_srcu); |
| 351 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 352 | cancel_delayed_work_sync(&p->eviction_work); |
| 353 | cancel_delayed_work_sync(&p->restore_work); |
| 354 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 355 | mutex_lock(&p->mutex); |
| 356 | |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 357 | /* Iterate over all process device data structures and if the |
| 358 | * pdd is in debug mode, we should first force unregistration, |
| 359 | * then we will be able to destroy the queues |
| 360 | */ |
| 361 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 362 | struct kfd_dev *dev = pdd->dev; |
| 363 | |
| 364 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 365 | if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { |
| 366 | if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { |
| 367 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 368 | dev->dbgmgr = NULL; |
| 369 | } |
| 370 | } |
| 371 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
| 372 | } |
| 373 | |
Felix Kuehling | 9fd3f1bf | 2017-09-27 00:09:52 -0400 | [diff] [blame] | 374 | kfd_process_dequeue_from_all_devices(p); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 375 | pqm_uninit(&p->pqm); |
| 376 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 377 | /* Indicate to other users that MM is no longer valid */ |
| 378 | p->mm = NULL; |
| 379 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 380 | mutex_unlock(&p->mutex); |
| 381 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 382 | mmu_notifier_unregister_no_release(&p->mmu_notifier, mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 383 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 384 | } |
| 385 | |
| 386 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 387 | .release = kfd_process_notifier_release, |
| 388 | }; |
| 389 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 390 | static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep) |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 391 | { |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 392 | unsigned long offset; |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 393 | struct kfd_process_device *pdd; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 394 | |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 395 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 396 | struct kfd_dev *dev = pdd->dev; |
| 397 | struct qcm_process_device *qpd = &pdd->qpd; |
| 398 | |
| 399 | if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base) |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 400 | continue; |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 401 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 402 | offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT; |
| 403 | qpd->tba_addr = (int64_t)vm_mmap(filep, 0, |
| 404 | KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC, |
| 405 | MAP_SHARED, offset); |
| 406 | |
| 407 | if (IS_ERR_VALUE(qpd->tba_addr)) { |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 408 | int err = qpd->tba_addr; |
| 409 | |
| 410 | pr_err("Failure to set tba address. error %d.\n", err); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 411 | qpd->tba_addr = 0; |
| 412 | qpd->cwsr_kaddr = NULL; |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 413 | return err; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); |
| 417 | |
| 418 | qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; |
| 419 | pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", |
| 420 | qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); |
| 421 | } |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 422 | |
| 423 | return 0; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 426 | static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd) |
| 427 | { |
| 428 | struct kfd_dev *dev = pdd->dev; |
| 429 | struct qcm_process_device *qpd = &pdd->qpd; |
| 430 | uint32_t flags = ALLOC_MEM_FLAGS_GTT | |
| 431 | ALLOC_MEM_FLAGS_NO_SUBSTITUTE | ALLOC_MEM_FLAGS_EXECUTABLE; |
| 432 | void *kaddr; |
| 433 | int ret; |
| 434 | |
| 435 | if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base) |
| 436 | return 0; |
| 437 | |
| 438 | /* cwsr_base is only set for dGPU */ |
| 439 | ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base, |
| 440 | KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr); |
| 441 | if (ret) |
| 442 | return ret; |
| 443 | |
| 444 | qpd->cwsr_kaddr = kaddr; |
| 445 | qpd->tba_addr = qpd->cwsr_base; |
| 446 | |
| 447 | memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); |
| 448 | |
| 449 | qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; |
| 450 | pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", |
| 451 | qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 456 | static struct kfd_process *create_process(const struct task_struct *thread, |
| 457 | struct file *filep) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 458 | { |
| 459 | struct kfd_process *process; |
| 460 | int err = -ENOMEM; |
| 461 | |
| 462 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 463 | |
| 464 | if (!process) |
| 465 | goto err_alloc_process; |
| 466 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 467 | process->pasid = kfd_pasid_alloc(); |
| 468 | if (process->pasid == 0) |
| 469 | goto err_alloc_pasid; |
| 470 | |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 471 | if (kfd_alloc_process_doorbells(process) < 0) |
| 472 | goto err_alloc_doorbells; |
| 473 | |
Felix Kuehling | 5ce1068 | 2017-11-27 18:29:51 -0500 | [diff] [blame] | 474 | kref_init(&process->ref); |
| 475 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 476 | mutex_init(&process->mutex); |
| 477 | |
| 478 | process->mm = thread->mm; |
| 479 | |
| 480 | /* register notifier */ |
| 481 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 482 | err = mmu_notifier_register(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 483 | if (err) |
| 484 | goto err_mmu_notifier; |
| 485 | |
| 486 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 487 | (uintptr_t)process->mm); |
| 488 | |
| 489 | process->lead_thread = thread->group_leader; |
Felix Kuehling | c7b1243 | 2017-11-27 18:29:50 -0500 | [diff] [blame] | 490 | get_task_struct(process->lead_thread); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 491 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 492 | INIT_LIST_HEAD(&process->per_device_data); |
| 493 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 494 | kfd_event_init_process(process); |
| 495 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 496 | err = pqm_init(&process->pqm, process); |
| 497 | if (err != 0) |
| 498 | goto err_process_pqm_init; |
| 499 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 500 | /* init process apertures*/ |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 501 | process->is_32bit_user_mode = in_compat_syscall(); |
Dan Carpenter | b312b2b | 2017-06-14 13:58:53 +0300 | [diff] [blame] | 502 | err = kfd_init_apertures(process); |
| 503 | if (err != 0) |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 504 | goto err_init_apertures; |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 505 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 506 | INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker); |
| 507 | INIT_DELAYED_WORK(&process->restore_work, restore_process_worker); |
| 508 | process->last_restore_timestamp = get_jiffies_64(); |
| 509 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 510 | err = kfd_process_init_cwsr_apu(process, filep); |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 511 | if (err) |
| 512 | goto err_init_cwsr; |
| 513 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 514 | return process; |
| 515 | |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 516 | err_init_cwsr: |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 517 | kfd_process_free_outstanding_kfd_bos(process); |
Yong Zhao | c0ede1f | 2017-11-27 18:29:56 -0500 | [diff] [blame] | 518 | kfd_process_destroy_pdds(process); |
Geert Uytterhoeven | 7a10d63 | 2017-06-01 12:28:38 +0200 | [diff] [blame] | 519 | err_init_apertures: |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 520 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 521 | err_process_pqm_init: |
| 522 | hash_del_rcu(&process->kfd_processes); |
| 523 | synchronize_rcu(); |
| 524 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 525 | err_mmu_notifier: |
Oded Gabbay | 7fd5e03 | 2016-06-23 17:54:29 +0300 | [diff] [blame] | 526 | mutex_destroy(&process->mutex); |
Felix Kuehling | a91e70e | 2017-08-26 02:00:57 -0400 | [diff] [blame] | 527 | kfd_free_process_doorbells(process); |
| 528 | err_alloc_doorbells: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 529 | kfd_pasid_free(process->pasid); |
| 530 | err_alloc_pasid: |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 531 | kfree(process); |
| 532 | err_alloc_process: |
| 533 | return ERR_PTR(err); |
| 534 | } |
| 535 | |
| 536 | struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 537 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 538 | { |
| 539 | struct kfd_process_device *pdd = NULL; |
| 540 | |
| 541 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 542 | if (pdd->dev == dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 543 | return pdd; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 544 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 545 | return NULL; |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 549 | struct kfd_process *p) |
| 550 | { |
| 551 | struct kfd_process_device *pdd = NULL; |
| 552 | |
| 553 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
Felix Kuehling | 2d9b36f | 2017-11-27 18:29:54 -0500 | [diff] [blame] | 554 | if (!pdd) |
| 555 | return NULL; |
| 556 | |
| 557 | pdd->dev = dev; |
| 558 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 559 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 560 | pdd->qpd.dqm = dev->dqm; |
| 561 | pdd->qpd.pqm = &p->pqm; |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 562 | pdd->qpd.evicted = 0; |
Felix Kuehling | 2d9b36f | 2017-11-27 18:29:54 -0500 | [diff] [blame] | 563 | pdd->process = p; |
| 564 | pdd->bound = PDD_UNBOUND; |
| 565 | pdd->already_dequeued = false; |
| 566 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 567 | |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 568 | /* Init idr used for memory handle translation */ |
| 569 | idr_init(&pdd->alloc_idr); |
| 570 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 571 | return pdd; |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 572 | } |
Felix Kuehling | 403575c | 2018-02-06 20:32:44 -0500 | [diff] [blame] | 573 | |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 574 | /** |
| 575 | * kfd_process_device_init_vm - Initialize a VM for a process-device |
| 576 | * |
| 577 | * @pdd: The process-device |
| 578 | * @drm_file: Optional pointer to a DRM file descriptor |
| 579 | * |
| 580 | * If @drm_file is specified, it will be used to acquire the VM from |
| 581 | * that file descriptor. If successful, the @pdd takes ownership of |
| 582 | * the file descriptor. |
| 583 | * |
| 584 | * If @drm_file is NULL, a new VM is created. |
| 585 | * |
| 586 | * Returns 0 on success, -errno on failure. |
| 587 | */ |
| 588 | int kfd_process_device_init_vm(struct kfd_process_device *pdd, |
| 589 | struct file *drm_file) |
| 590 | { |
| 591 | struct kfd_process *p; |
| 592 | struct kfd_dev *dev; |
| 593 | int ret; |
| 594 | |
| 595 | if (pdd->vm) |
| 596 | return drm_file ? -EBUSY : 0; |
| 597 | |
| 598 | p = pdd->process; |
| 599 | dev = pdd->dev; |
| 600 | |
| 601 | if (drm_file) |
| 602 | ret = dev->kfd2kgd->acquire_process_vm( |
| 603 | dev->kgd, drm_file, |
| 604 | &pdd->vm, &p->kgd_process_info, &p->ef); |
| 605 | else |
| 606 | ret = dev->kfd2kgd->create_process_vm( |
| 607 | dev->kgd, &pdd->vm, &p->kgd_process_info, &p->ef); |
| 608 | if (ret) { |
| 609 | pr_err("Failed to create process VM object\n"); |
| 610 | return ret; |
| 611 | } |
| 612 | |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 613 | ret = kfd_process_device_init_cwsr_dgpu(pdd); |
| 614 | if (ret) |
| 615 | goto err_init_cwsr; |
| 616 | |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 617 | pdd->drm_file = drm_file; |
| 618 | |
| 619 | return 0; |
Felix Kuehling | f35751b | 2018-03-15 17:27:49 -0400 | [diff] [blame^] | 620 | |
| 621 | err_init_cwsr: |
| 622 | kfd_process_device_free_bos(pdd); |
| 623 | if (!drm_file) |
| 624 | dev->kfd2kgd->destroy_process_vm(dev->kgd, pdd->vm); |
| 625 | pdd->vm = NULL; |
| 626 | |
| 627 | return ret; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 632 | * to the device. |
| 633 | * Unbinding occurs when the process dies or the device is removed. |
| 634 | * |
| 635 | * Assumes that the process lock is held. |
| 636 | */ |
| 637 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 638 | struct kfd_process *p) |
| 639 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 640 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 641 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 642 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 643 | pdd = kfd_get_process_device_data(dev, p); |
| 644 | if (!pdd) { |
| 645 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 646 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 647 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 648 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 649 | err = kfd_iommu_bind_process_to_device(pdd); |
| 650 | if (err) |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 651 | return ERR_PTR(err); |
| 652 | |
Felix Kuehling | b84394e | 2018-03-15 17:27:44 -0400 | [diff] [blame] | 653 | err = kfd_process_device_init_vm(pdd, NULL); |
| 654 | if (err) |
| 655 | return ERR_PTR(err); |
| 656 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 657 | return pdd; |
| 658 | } |
| 659 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 660 | struct kfd_process_device *kfd_get_first_process_device_data( |
| 661 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 662 | { |
| 663 | return list_first_entry(&p->per_device_data, |
| 664 | struct kfd_process_device, |
| 665 | per_device_list); |
| 666 | } |
| 667 | |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 668 | struct kfd_process_device *kfd_get_next_process_device_data( |
| 669 | struct kfd_process *p, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 670 | struct kfd_process_device *pdd) |
| 671 | { |
| 672 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 673 | return NULL; |
| 674 | return list_next_entry(pdd, per_device_list); |
| 675 | } |
| 676 | |
| 677 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 678 | { |
| 679 | return !(list_empty(&p->per_device_data)); |
| 680 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 681 | |
Felix Kuehling | 52b29d7 | 2018-03-15 17:27:48 -0400 | [diff] [blame] | 682 | /* Create specific handle mapped to mem from process local memory idr |
| 683 | * Assumes that the process lock is held. |
| 684 | */ |
| 685 | int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, |
| 686 | void *mem) |
| 687 | { |
| 688 | return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL); |
| 689 | } |
| 690 | |
| 691 | /* Translate specific handle from process local memory idr |
| 692 | * Assumes that the process lock is held. |
| 693 | */ |
| 694 | void *kfd_process_device_translate_handle(struct kfd_process_device *pdd, |
| 695 | int handle) |
| 696 | { |
| 697 | if (handle < 0) |
| 698 | return NULL; |
| 699 | |
| 700 | return idr_find(&pdd->alloc_idr, handle); |
| 701 | } |
| 702 | |
| 703 | /* Remove specific handle from process local memory idr |
| 704 | * Assumes that the process lock is held. |
| 705 | */ |
| 706 | void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, |
| 707 | int handle) |
| 708 | { |
| 709 | if (handle >= 0) |
| 710 | idr_remove(&pdd->alloc_idr, handle); |
| 711 | } |
| 712 | |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 713 | /* This increments the process->ref counter. */ |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 714 | struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) |
| 715 | { |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 716 | struct kfd_process *p, *ret_p = NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 717 | unsigned int temp; |
| 718 | |
| 719 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 720 | |
| 721 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 722 | if (p->pasid == pasid) { |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 723 | kref_get(&p->ref); |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 724 | ret_p = p; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 730 | |
Yong Zhao | 82c16b4 | 2017-11-27 18:29:53 -0500 | [diff] [blame] | 731 | return ret_p; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 732 | } |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 733 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 734 | /* This increments the process->ref counter. */ |
| 735 | struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm) |
| 736 | { |
| 737 | struct kfd_process *p; |
| 738 | |
| 739 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 740 | |
| 741 | p = find_process_by_mm(mm); |
| 742 | if (p) |
| 743 | kref_get(&p->ref); |
| 744 | |
| 745 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 746 | |
| 747 | return p; |
| 748 | } |
| 749 | |
| 750 | /* process_evict_queues - Evict all user queues of a process |
| 751 | * |
| 752 | * Eviction is reference-counted per process-device. This means multiple |
| 753 | * evictions from different sources can be nested safely. |
| 754 | */ |
| 755 | static int process_evict_queues(struct kfd_process *p) |
| 756 | { |
| 757 | struct kfd_process_device *pdd; |
| 758 | int r = 0; |
| 759 | unsigned int n_evicted = 0; |
| 760 | |
| 761 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 762 | r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm, |
| 763 | &pdd->qpd); |
| 764 | if (r) { |
| 765 | pr_err("Failed to evict process queues\n"); |
| 766 | goto fail; |
| 767 | } |
| 768 | n_evicted++; |
| 769 | } |
| 770 | |
| 771 | return r; |
| 772 | |
| 773 | fail: |
| 774 | /* To keep state consistent, roll back partial eviction by |
| 775 | * restoring queues |
| 776 | */ |
| 777 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 778 | if (n_evicted == 0) |
| 779 | break; |
| 780 | if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, |
| 781 | &pdd->qpd)) |
| 782 | pr_err("Failed to restore queues\n"); |
| 783 | |
| 784 | n_evicted--; |
| 785 | } |
| 786 | |
| 787 | return r; |
| 788 | } |
| 789 | |
| 790 | /* process_restore_queues - Restore all user queues of a process */ |
| 791 | static int process_restore_queues(struct kfd_process *p) |
| 792 | { |
| 793 | struct kfd_process_device *pdd; |
| 794 | int r, ret = 0; |
| 795 | |
| 796 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) { |
| 797 | r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, |
| 798 | &pdd->qpd); |
| 799 | if (r) { |
| 800 | pr_err("Failed to restore process queues\n"); |
| 801 | if (!ret) |
| 802 | ret = r; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | static void evict_process_worker(struct work_struct *work) |
| 810 | { |
| 811 | int ret; |
| 812 | struct kfd_process *p; |
| 813 | struct delayed_work *dwork; |
| 814 | |
| 815 | dwork = to_delayed_work(work); |
| 816 | |
| 817 | /* Process termination destroys this worker thread. So during the |
| 818 | * lifetime of this thread, kfd_process p will be valid |
| 819 | */ |
| 820 | p = container_of(dwork, struct kfd_process, eviction_work); |
| 821 | WARN_ONCE(p->last_eviction_seqno != p->ef->seqno, |
| 822 | "Eviction fence mismatch\n"); |
| 823 | |
| 824 | /* Narrow window of overlap between restore and evict work |
| 825 | * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos |
| 826 | * unreserves KFD BOs, it is possible to evicted again. But |
| 827 | * restore has few more steps of finish. So lets wait for any |
| 828 | * previous restore work to complete |
| 829 | */ |
| 830 | flush_delayed_work(&p->restore_work); |
| 831 | |
| 832 | pr_debug("Started evicting pasid %d\n", p->pasid); |
| 833 | ret = process_evict_queues(p); |
| 834 | if (!ret) { |
| 835 | dma_fence_signal(p->ef); |
| 836 | dma_fence_put(p->ef); |
| 837 | p->ef = NULL; |
| 838 | schedule_delayed_work(&p->restore_work, |
| 839 | msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)); |
| 840 | |
| 841 | pr_debug("Finished evicting pasid %d\n", p->pasid); |
| 842 | } else |
| 843 | pr_err("Failed to evict queues of pasid %d\n", p->pasid); |
| 844 | } |
| 845 | |
| 846 | static void restore_process_worker(struct work_struct *work) |
| 847 | { |
| 848 | struct delayed_work *dwork; |
| 849 | struct kfd_process *p; |
| 850 | struct kfd_process_device *pdd; |
| 851 | int ret = 0; |
| 852 | |
| 853 | dwork = to_delayed_work(work); |
| 854 | |
| 855 | /* Process termination destroys this worker thread. So during the |
| 856 | * lifetime of this thread, kfd_process p will be valid |
| 857 | */ |
| 858 | p = container_of(dwork, struct kfd_process, restore_work); |
| 859 | |
| 860 | /* Call restore_process_bos on the first KGD device. This function |
| 861 | * takes care of restoring the whole process including other devices. |
| 862 | * Restore can fail if enough memory is not available. If so, |
| 863 | * reschedule again. |
| 864 | */ |
| 865 | pdd = list_first_entry(&p->per_device_data, |
| 866 | struct kfd_process_device, |
| 867 | per_device_list); |
| 868 | |
| 869 | pr_debug("Started restoring pasid %d\n", p->pasid); |
| 870 | |
| 871 | /* Setting last_restore_timestamp before successful restoration. |
| 872 | * Otherwise this would have to be set by KGD (restore_process_bos) |
| 873 | * before KFD BOs are unreserved. If not, the process can be evicted |
| 874 | * again before the timestamp is set. |
| 875 | * If restore fails, the timestamp will be set again in the next |
| 876 | * attempt. This would mean that the minimum GPU quanta would be |
| 877 | * PROCESS_ACTIVE_TIME_MS - (time to execute the following two |
| 878 | * functions) |
| 879 | */ |
| 880 | |
| 881 | p->last_restore_timestamp = get_jiffies_64(); |
| 882 | ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info, |
| 883 | &p->ef); |
| 884 | if (ret) { |
| 885 | pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n", |
| 886 | p->pasid, PROCESS_BACK_OFF_TIME_MS); |
| 887 | ret = schedule_delayed_work(&p->restore_work, |
| 888 | msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS)); |
| 889 | WARN(!ret, "reschedule restore work failed\n"); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | ret = process_restore_queues(p); |
| 894 | if (!ret) |
| 895 | pr_debug("Finished restoring pasid %d\n", p->pasid); |
| 896 | else |
| 897 | pr_err("Failed to restore queues of pasid %d\n", p->pasid); |
| 898 | } |
| 899 | |
| 900 | void kfd_suspend_all_processes(void) |
| 901 | { |
| 902 | struct kfd_process *p; |
| 903 | unsigned int temp; |
| 904 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 905 | |
| 906 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 907 | cancel_delayed_work_sync(&p->eviction_work); |
| 908 | cancel_delayed_work_sync(&p->restore_work); |
| 909 | |
| 910 | if (process_evict_queues(p)) |
| 911 | pr_err("Failed to suspend process %d\n", p->pasid); |
| 912 | dma_fence_signal(p->ef); |
| 913 | dma_fence_put(p->ef); |
| 914 | p->ef = NULL; |
| 915 | } |
| 916 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 917 | } |
| 918 | |
| 919 | int kfd_resume_all_processes(void) |
| 920 | { |
| 921 | struct kfd_process *p; |
| 922 | unsigned int temp; |
| 923 | int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu); |
| 924 | |
| 925 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 926 | if (!schedule_delayed_work(&p->restore_work, 0)) { |
| 927 | pr_err("Restore process %d failed during resume\n", |
| 928 | p->pasid); |
| 929 | ret = -EFAULT; |
| 930 | } |
| 931 | } |
| 932 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 933 | return ret; |
| 934 | } |
| 935 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 936 | int kfd_reserved_mem_mmap(struct kfd_process *process, |
| 937 | struct vm_area_struct *vma) |
| 938 | { |
| 939 | struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff); |
| 940 | struct kfd_process_device *pdd; |
| 941 | struct qcm_process_device *qpd; |
| 942 | |
| 943 | if (!dev) |
| 944 | return -EINVAL; |
| 945 | if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) { |
| 946 | pr_err("Incorrect CWSR mapping size.\n"); |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | pdd = kfd_get_process_device_data(dev, process); |
| 951 | if (!pdd) |
| 952 | return -EINVAL; |
| 953 | qpd = &pdd->qpd; |
| 954 | |
| 955 | qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 956 | get_order(KFD_CWSR_TBA_TMA_SIZE)); |
| 957 | if (!qpd->cwsr_kaddr) { |
| 958 | pr_err("Error allocating per process CWSR buffer.\n"); |
| 959 | return -ENOMEM; |
| 960 | } |
| 961 | |
| 962 | vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND |
| 963 | | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP; |
| 964 | /* Mapping pages to user process */ |
| 965 | return remap_pfn_range(vma, vma->vm_start, |
| 966 | PFN_DOWN(__pa(qpd->cwsr_kaddr)), |
| 967 | KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot); |
| 968 | } |
Felix Kuehling | 851a645 | 2017-11-27 18:29:49 -0500 | [diff] [blame] | 969 | |
Felix Kuehling | 403575c | 2018-02-06 20:32:44 -0500 | [diff] [blame] | 970 | void kfd_flush_tlb(struct kfd_process_device *pdd) |
| 971 | { |
| 972 | struct kfd_dev *dev = pdd->dev; |
| 973 | const struct kfd2kgd_calls *f2g = dev->kfd2kgd; |
| 974 | |
| 975 | if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { |
| 976 | /* Nothing to flush until a VMID is assigned, which |
| 977 | * only happens when the first queue is created. |
| 978 | */ |
| 979 | if (pdd->qpd.vmid) |
| 980 | f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid); |
| 981 | } else { |
| 982 | f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid); |
| 983 | } |
| 984 | } |
| 985 | |
Felix Kuehling | 851a645 | 2017-11-27 18:29:49 -0500 | [diff] [blame] | 986 | #if defined(CONFIG_DEBUG_FS) |
| 987 | |
| 988 | int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data) |
| 989 | { |
| 990 | struct kfd_process *p; |
| 991 | unsigned int temp; |
| 992 | int r = 0; |
| 993 | |
| 994 | int idx = srcu_read_lock(&kfd_processes_srcu); |
| 995 | |
| 996 | hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { |
| 997 | seq_printf(m, "Process %d PASID %d:\n", |
| 998 | p->lead_thread->tgid, p->pasid); |
| 999 | |
| 1000 | mutex_lock(&p->mutex); |
| 1001 | r = pqm_debugfs_mqds(m, &p->pqm); |
| 1002 | mutex_unlock(&p->mutex); |
| 1003 | |
| 1004 | if (r) |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 1009 | |
| 1010 | return r; |
| 1011 | } |
| 1012 | |
| 1013 | #endif |