2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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>
37 #include "kfd_dbgmgr.h"
40 * List of struct kfd_process (field kfd_process).
41 * Unique/indexed by mm_struct*
43 #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
44 static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
45 static DEFINE_MUTEX(kfd_processes_mutex);
47 DEFINE_STATIC_SRCU(kfd_processes_srcu);
49 static struct workqueue_struct *kfd_process_wq;
51 static struct kfd_process *find_process(const struct task_struct *thread);
52 static void kfd_process_ref_release(struct kref *ref);
53 static struct kfd_process *create_process(const struct task_struct *thread,
55 static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);
58 void kfd_process_create_wq(void)
61 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
64 void kfd_process_destroy_wq(void)
67 destroy_workqueue(kfd_process_wq);
68 kfd_process_wq = NULL;
72 struct kfd_process *kfd_create_process(struct file *filep)
74 struct kfd_process *process;
75 struct task_struct *thread = current;
78 return ERR_PTR(-EINVAL);
80 /* Only the pthreads threading model is supported. */
81 if (thread->group_leader->mm != thread->mm)
82 return ERR_PTR(-EINVAL);
85 * take kfd processes mutex before starting of process creation
86 * so there won't be a case where two threads of the same process
87 * create two kfd_process structures
89 mutex_lock(&kfd_processes_mutex);
91 /* A prior open of /dev/kfd could have already created the process. */
92 process = find_process(thread);
94 pr_debug("Process already found\n");
96 process = create_process(thread, filep);
98 mutex_unlock(&kfd_processes_mutex);
103 struct kfd_process *kfd_get_process(const struct task_struct *thread)
105 struct kfd_process *process;
108 return ERR_PTR(-EINVAL);
110 /* Only the pthreads threading model is supported. */
111 if (thread->group_leader->mm != thread->mm)
112 return ERR_PTR(-EINVAL);
114 process = find_process(thread);
119 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
121 struct kfd_process *process;
123 hash_for_each_possible_rcu(kfd_processes_table, process,
124 kfd_processes, (uintptr_t)mm)
125 if (process->mm == mm)
131 static struct kfd_process *find_process(const struct task_struct *thread)
133 struct kfd_process *p;
136 idx = srcu_read_lock(&kfd_processes_srcu);
137 p = find_process_by_mm(thread->mm);
138 srcu_read_unlock(&kfd_processes_srcu, idx);
143 void kfd_unref_process(struct kfd_process *p)
145 kref_put(&p->ref, kfd_process_ref_release);
148 static void kfd_process_destroy_pdds(struct kfd_process *p)
150 struct kfd_process_device *pdd, *temp;
152 list_for_each_entry_safe(pdd, temp, &p->per_device_data,
154 pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
155 pdd->dev->id, p->pasid);
157 list_del(&pdd->per_device_list);
159 if (pdd->qpd.cwsr_kaddr)
160 free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
161 get_order(KFD_CWSR_TBA_TMA_SIZE));
167 /* No process locking is needed in this function, because the process
168 * is not findable any more. We must assume that no other thread is
169 * using it any more, otherwise we couldn't safely free the process
170 * structure in the end.
172 static void kfd_process_wq_release(struct work_struct *work)
174 struct kfd_process *p = container_of(work, struct kfd_process,
176 struct kfd_process_device *pdd;
178 pr_debug("Releasing process (pasid %d) in workqueue\n", p->pasid);
180 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
181 if (pdd->bound == PDD_BOUND)
182 amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
185 kfd_process_destroy_pdds(p);
187 kfd_event_free_process(p);
189 kfd_pasid_free(p->pasid);
190 kfd_free_process_doorbells(p);
192 mutex_destroy(&p->mutex);
194 put_task_struct(p->lead_thread);
199 static void kfd_process_ref_release(struct kref *ref)
201 struct kfd_process *p = container_of(ref, struct kfd_process, ref);
203 INIT_WORK(&p->release_work, kfd_process_wq_release);
204 queue_work(kfd_process_wq, &p->release_work);
207 static void kfd_process_destroy_delayed(struct rcu_head *rcu)
209 struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
211 kfd_unref_process(p);
214 static void kfd_process_notifier_release(struct mmu_notifier *mn,
215 struct mm_struct *mm)
217 struct kfd_process *p;
218 struct kfd_process_device *pdd = NULL;
221 * The kfd_process structure can not be free because the
222 * mmu_notifier srcu is read locked
224 p = container_of(mn, struct kfd_process, mmu_notifier);
225 if (WARN_ON(p->mm != mm))
228 mutex_lock(&kfd_processes_mutex);
229 hash_del_rcu(&p->kfd_processes);
230 mutex_unlock(&kfd_processes_mutex);
231 synchronize_srcu(&kfd_processes_srcu);
233 mutex_lock(&p->mutex);
235 /* Iterate over all process device data structures and if the
236 * pdd is in debug mode, we should first force unregistration,
237 * then we will be able to destroy the queues
239 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
240 struct kfd_dev *dev = pdd->dev;
242 mutex_lock(kfd_get_dbgmgr_mutex());
243 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
244 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
245 kfd_dbgmgr_destroy(dev->dbgmgr);
249 mutex_unlock(kfd_get_dbgmgr_mutex());
252 kfd_process_dequeue_from_all_devices(p);
255 /* Indicate to other users that MM is no longer valid */
258 mutex_unlock(&p->mutex);
260 mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
261 mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
264 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
265 .release = kfd_process_notifier_release,
268 static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep)
270 unsigned long offset;
271 struct kfd_process_device *pdd = NULL;
272 struct kfd_dev *dev = NULL;
273 struct qcm_process_device *qpd = NULL;
275 list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
278 if (!dev->cwsr_enabled || qpd->cwsr_kaddr)
280 offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
281 qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
282 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
285 if (IS_ERR_VALUE(qpd->tba_addr)) {
286 int err = qpd->tba_addr;
288 pr_err("Failure to set tba address. error %d.\n", err);
290 qpd->cwsr_kaddr = NULL;
294 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
296 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
297 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
298 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
304 static struct kfd_process *create_process(const struct task_struct *thread,
307 struct kfd_process *process;
310 process = kzalloc(sizeof(*process), GFP_KERNEL);
313 goto err_alloc_process;
315 process->pasid = kfd_pasid_alloc();
316 if (process->pasid == 0)
317 goto err_alloc_pasid;
319 if (kfd_alloc_process_doorbells(process) < 0)
320 goto err_alloc_doorbells;
322 kref_init(&process->ref);
324 mutex_init(&process->mutex);
326 process->mm = thread->mm;
328 /* register notifier */
329 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
330 err = mmu_notifier_register(&process->mmu_notifier, process->mm);
332 goto err_mmu_notifier;
334 hash_add_rcu(kfd_processes_table, &process->kfd_processes,
335 (uintptr_t)process->mm);
337 process->lead_thread = thread->group_leader;
338 get_task_struct(process->lead_thread);
340 INIT_LIST_HEAD(&process->per_device_data);
342 kfd_event_init_process(process);
344 err = pqm_init(&process->pqm, process);
346 goto err_process_pqm_init;
348 /* init process apertures*/
349 process->is_32bit_user_mode = in_compat_syscall();
350 err = kfd_init_apertures(process);
352 goto err_init_apertures;
354 err = kfd_process_init_cwsr(process, filep);
361 kfd_process_destroy_pdds(process);
363 pqm_uninit(&process->pqm);
364 err_process_pqm_init:
365 hash_del_rcu(&process->kfd_processes);
367 mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
369 mutex_destroy(&process->mutex);
370 kfd_free_process_doorbells(process);
372 kfd_pasid_free(process->pasid);
379 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
380 struct kfd_process *p)
382 struct kfd_process_device *pdd = NULL;
384 list_for_each_entry(pdd, &p->per_device_data, per_device_list)
391 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
392 struct kfd_process *p)
394 struct kfd_process_device *pdd = NULL;
396 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
401 INIT_LIST_HEAD(&pdd->qpd.queues_list);
402 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
403 pdd->qpd.dqm = dev->dqm;
404 pdd->qpd.pqm = &p->pqm;
406 pdd->bound = PDD_UNBOUND;
407 pdd->already_dequeued = false;
408 list_add(&pdd->per_device_list, &p->per_device_data);
414 * Direct the IOMMU to bind the process (specifically the pasid->mm)
416 * Unbinding occurs when the process dies or the device is removed.
418 * Assumes that the process lock is held.
420 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
421 struct kfd_process *p)
423 struct kfd_process_device *pdd;
426 pdd = kfd_get_process_device_data(dev, p);
428 pr_err("Process device data doesn't exist\n");
429 return ERR_PTR(-ENOMEM);
432 if (pdd->bound == PDD_BOUND) {
434 } else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
435 pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
436 return ERR_PTR(-EINVAL);
439 err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
443 pdd->bound = PDD_BOUND;
449 * Bind processes do the device that have been temporarily unbound
450 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
452 int kfd_bind_processes_to_device(struct kfd_dev *dev)
454 struct kfd_process_device *pdd;
455 struct kfd_process *p;
459 int idx = srcu_read_lock(&kfd_processes_srcu);
461 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
462 mutex_lock(&p->mutex);
463 pdd = kfd_get_process_device_data(dev, p);
465 if (WARN_ON(!pdd) || pdd->bound != PDD_BOUND_SUSPENDED) {
466 mutex_unlock(&p->mutex);
470 err = amd_iommu_bind_pasid(dev->pdev, p->pasid,
473 pr_err("Unexpected pasid %d binding failure\n",
475 mutex_unlock(&p->mutex);
479 pdd->bound = PDD_BOUND;
480 mutex_unlock(&p->mutex);
483 srcu_read_unlock(&kfd_processes_srcu, idx);
489 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
490 * processes will be restored to PDD_BOUND state in
491 * kfd_bind_processes_to_device.
493 void kfd_unbind_processes_from_device(struct kfd_dev *dev)
495 struct kfd_process_device *pdd;
496 struct kfd_process *p;
499 int idx = srcu_read_lock(&kfd_processes_srcu);
501 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
502 mutex_lock(&p->mutex);
503 pdd = kfd_get_process_device_data(dev, p);
506 mutex_unlock(&p->mutex);
510 if (pdd->bound == PDD_BOUND)
511 pdd->bound = PDD_BOUND_SUSPENDED;
512 mutex_unlock(&p->mutex);
515 srcu_read_unlock(&kfd_processes_srcu, idx);
518 void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid)
520 struct kfd_process *p;
521 struct kfd_process_device *pdd;
524 * Look for the process that matches the pasid. If there is no such
525 * process, we either released it in amdkfd's own notifier, or there
526 * is a bug. Unfortunately, there is no way to tell...
528 p = kfd_lookup_process_by_pasid(pasid);
532 pr_debug("Unbinding process %d from IOMMU\n", pasid);
534 mutex_lock(kfd_get_dbgmgr_mutex());
536 if (dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
537 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
538 kfd_dbgmgr_destroy(dev->dbgmgr);
543 mutex_unlock(kfd_get_dbgmgr_mutex());
545 mutex_lock(&p->mutex);
547 pdd = kfd_get_process_device_data(dev, p);
549 /* For GPU relying on IOMMU, we need to dequeue here
550 * when PASID is still bound.
552 kfd_process_dequeue_from_device(pdd);
554 mutex_unlock(&p->mutex);
556 kfd_unref_process(p);
559 struct kfd_process_device *kfd_get_first_process_device_data(
560 struct kfd_process *p)
562 return list_first_entry(&p->per_device_data,
563 struct kfd_process_device,
567 struct kfd_process_device *kfd_get_next_process_device_data(
568 struct kfd_process *p,
569 struct kfd_process_device *pdd)
571 if (list_is_last(&pdd->per_device_list, &p->per_device_data))
573 return list_next_entry(pdd, per_device_list);
576 bool kfd_has_process_device_data(struct kfd_process *p)
578 return !(list_empty(&p->per_device_data));
581 /* This increments the process->ref counter. */
582 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
584 struct kfd_process *p, *ret_p = NULL;
587 int idx = srcu_read_lock(&kfd_processes_srcu);
589 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
590 if (p->pasid == pasid) {
597 srcu_read_unlock(&kfd_processes_srcu, idx);
602 int kfd_reserved_mem_mmap(struct kfd_process *process,
603 struct vm_area_struct *vma)
605 struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
606 struct kfd_process_device *pdd;
607 struct qcm_process_device *qpd;
611 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
612 pr_err("Incorrect CWSR mapping size.\n");
616 pdd = kfd_get_process_device_data(dev, process);
621 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
622 get_order(KFD_CWSR_TBA_TMA_SIZE));
623 if (!qpd->cwsr_kaddr) {
624 pr_err("Error allocating per process CWSR buffer.\n");
628 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
629 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
630 /* Mapping pages to user process */
631 return remap_pfn_range(vma, vma->vm_start,
632 PFN_DOWN(__pa(qpd->cwsr_kaddr)),
633 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
636 #if defined(CONFIG_DEBUG_FS)
638 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
640 struct kfd_process *p;
644 int idx = srcu_read_lock(&kfd_processes_srcu);
646 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
647 seq_printf(m, "Process %d PASID %d:\n",
648 p->lead_thread->tgid, p->pasid);
650 mutex_lock(&p->mutex);
651 r = pqm_debugfs_mqds(m, &p->pqm);
652 mutex_unlock(&p->mutex);
658 srcu_read_unlock(&kfd_processes_srcu, idx);