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