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