xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_process.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/mutex.h>
25 #include <linux/log2.h>
26 #include <linux/sched.h>
27 #include <linux/sched/mm.h>
28 #include <linux/sched/task.h>
29 #include <linux/mmu_context.h>
30 #include <linux/slab.h>
31 #include <linux/notifier.h>
32 #include <linux/compat.h>
33 #include <linux/mman.h>
34 #include <linux/file.h>
35 #include <linux/pm_runtime.h>
36 #include <drm/ttm/ttm_bo.h>
37 #include "amdgpu_amdkfd.h"
38 #include "amdgpu.h"
39 #include "amdgpu_reset.h"
40 
41 struct mm_struct;
42 
43 #include "kfd_priv.h"
44 #include "kfd_device_queue_manager.h"
45 #include "kfd_svm.h"
46 #include "kfd_smi_events.h"
47 #include "kfd_debug.h"
48 
49 /*
50  * List of struct kfd_process (field kfd_process).
51  * Unique/indexed by mm_struct*
52  */
53 DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
54 DEFINE_MUTEX(kfd_processes_mutex);
55 
56 DEFINE_SRCU(kfd_processes_srcu);
57 
58 /* For process termination handling */
59 static struct workqueue_struct *kfd_process_wq;
60 
61 /* Ordered, single-threaded workqueue for restoring evicted
62  * processes. Restoring multiple processes concurrently under memory
63  * pressure can lead to processes blocking each other from validating
64  * their BOs and result in a live-lock situation where processes
65  * remain evicted indefinitely.
66  */
67 static struct workqueue_struct *kfd_restore_wq;
68 
69 static struct kfd_process *find_process(const struct task_struct *thread,
70 					bool ref);
71 static void kfd_process_ref_release(struct kref *ref);
72 
73 static void evict_process_worker(struct work_struct *work);
74 static void restore_process_worker(struct work_struct *work);
75 
76 static void kfd_process_device_destroy_cwsr_dgpu(struct kfd_process_device *pdd);
77 
78 struct kfd_procfs_tree {
79 	struct kobject *kobj;
80 };
81 
82 static struct kfd_procfs_tree procfs;
83 
84 /*
85  * Structure for SDMA activity tracking
86  */
87 struct kfd_sdma_activity_handler_workarea {
88 	struct work_struct sdma_activity_work;
89 	struct kfd_process_device *pdd;
90 	uint64_t sdma_activity_counter;
91 };
92 
93 struct temp_sdma_queue_list {
94 	uint64_t __user *rptr;
95 	uint64_t sdma_val;
96 	unsigned int queue_id;
97 	struct list_head list;
98 };
99 
100 static void kfd_sdma_activity_worker(struct work_struct *work)
101 {
102 	struct kfd_sdma_activity_handler_workarea *workarea;
103 	struct kfd_process_device *pdd;
104 	uint64_t val;
105 	struct mm_struct *mm;
106 	struct queue *q;
107 	struct qcm_process_device *qpd;
108 	struct device_queue_manager *dqm;
109 	int ret = 0;
110 	struct temp_sdma_queue_list sdma_q_list;
111 	struct temp_sdma_queue_list *sdma_q, *next;
112 
113 	workarea = container_of(work, struct kfd_sdma_activity_handler_workarea,
114 				sdma_activity_work);
115 
116 	pdd = workarea->pdd;
117 	if (!pdd)
118 		return;
119 	dqm = pdd->dev->dqm;
120 	qpd = &pdd->qpd;
121 	if (!dqm || !qpd)
122 		return;
123 	/*
124 	 * Total SDMA activity is current SDMA activity + past SDMA activity
125 	 * Past SDMA count is stored in pdd.
126 	 * To get the current activity counters for all active SDMA queues,
127 	 * we loop over all SDMA queues and get their counts from user-space.
128 	 *
129 	 * We cannot call get_user() with dqm_lock held as it can cause
130 	 * a circular lock dependency situation. To read the SDMA stats,
131 	 * we need to do the following:
132 	 *
133 	 * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list,
134 	 *    with dqm_lock/dqm_unlock().
135 	 * 2. Call get_user() for each node in temporary list without dqm_lock.
136 	 *    Save the SDMA count for each node and also add the count to the total
137 	 *    SDMA count counter.
138 	 *    Its possible, during this step, a few SDMA queue nodes got deleted
139 	 *    from the qpd->queues_list.
140 	 * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted.
141 	 *    If any node got deleted, its SDMA count would be captured in the sdma
142 	 *    past activity counter. So subtract the SDMA counter stored in step 2
143 	 *    for this node from the total SDMA count.
144 	 */
145 	INIT_LIST_HEAD(&sdma_q_list.list);
146 
147 	/*
148 	 * Create the temp list of all SDMA queues
149 	 */
150 	dqm_lock(dqm);
151 
152 	list_for_each_entry(q, &qpd->queues_list, list) {
153 		if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
154 		    (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
155 			continue;
156 
157 		if (dqm->dev->kfd2kgd->hqd_sdma_get_counter) {
158 			val = 0;
159 			ret = dqm->dev->kfd2kgd->hqd_sdma_get_counter(
160 					dqm->dev->adev, q->mqd,
161 					dqm->dev->kfd->device_info.num_sdma_queues_per_engine,
162 					&val);
163 
164 			if (ret)
165 				pr_debug("Failed to read SDMA queue active counter %i\n", ret);
166 			else
167 				workarea->sdma_activity_counter += val;
168 
169 			continue;
170 		}
171 
172 		sdma_q = kzalloc_obj(struct temp_sdma_queue_list);
173 		if (!sdma_q) {
174 			dqm_unlock(dqm);
175 			goto cleanup;
176 		}
177 
178 		INIT_LIST_HEAD(&sdma_q->list);
179 		sdma_q->rptr = (uint64_t __user *)q->properties.read_ptr;
180 		sdma_q->queue_id = q->properties.queue_id;
181 		list_add_tail(&sdma_q->list, &sdma_q_list.list);
182 	}
183 
184 	/*
185 	 * If the temp list is empty, then no SDMA queues nodes were found in
186 	 * qpd->queues_list. Return the past activity count as the total sdma
187 	 * count
188 	 */
189 	if (list_empty(&sdma_q_list.list)) {
190 		workarea->sdma_activity_counter += pdd->sdma_past_activity_counter;
191 		dqm_unlock(dqm);
192 		return;
193 	}
194 
195 	dqm_unlock(dqm);
196 
197 	/*
198 	 * Get the usage count for each SDMA queue in temp_list.
199 	 */
200 	mm = get_task_mm(pdd->process->lead_thread);
201 	if (!mm)
202 		goto cleanup;
203 
204 	kthread_use_mm(mm);
205 
206 	list_for_each_entry(sdma_q, &sdma_q_list.list, list) {
207 		val = 0;
208 		ret = read_sdma_queue_counter(sdma_q->rptr, &val);
209 		if (ret) {
210 			pr_debug("Failed to read SDMA queue active counter for queue id: %d",
211 				 sdma_q->queue_id);
212 		} else {
213 			sdma_q->sdma_val = val;
214 			workarea->sdma_activity_counter += val;
215 		}
216 	}
217 
218 	kthread_unuse_mm(mm);
219 	mmput(mm);
220 
221 	/*
222 	 * Do a second iteration over qpd_queues_list to check if any SDMA
223 	 * nodes got deleted while fetching SDMA counter.
224 	 */
225 	dqm_lock(dqm);
226 
227 	workarea->sdma_activity_counter += pdd->sdma_past_activity_counter;
228 
229 	list_for_each_entry(q, &qpd->queues_list, list) {
230 		if (list_empty(&sdma_q_list.list))
231 			break;
232 
233 		if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
234 		    (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
235 			continue;
236 
237 		list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
238 			if (((uint64_t __user *)q->properties.read_ptr == sdma_q->rptr) &&
239 			     (sdma_q->queue_id == q->properties.queue_id)) {
240 				list_del(&sdma_q->list);
241 				kfree(sdma_q);
242 				break;
243 			}
244 		}
245 	}
246 
247 	dqm_unlock(dqm);
248 
249 	/*
250 	 * If temp list is not empty, it implies some queues got deleted
251 	 * from qpd->queues_list during SDMA usage read. Subtract the SDMA
252 	 * count for each node from the total SDMA count.
253 	 */
254 	list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
255 		workarea->sdma_activity_counter -= sdma_q->sdma_val;
256 		list_del(&sdma_q->list);
257 		kfree(sdma_q);
258 	}
259 
260 	return;
261 
262 cleanup:
263 	list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
264 		list_del(&sdma_q->list);
265 		kfree(sdma_q);
266 	}
267 }
268 
269 /**
270  * kfd_get_cu_occupancy - Collect number of waves in-flight on this device
271  * by current process. Translates acquired wave count into number of compute units
272  * that are occupied.
273  *
274  * @attr: Handle of attribute that allows reporting of wave count. The attribute
275  * handle encapsulates GPU device it is associated with, thereby allowing collection
276  * of waves in flight, etc
277  * @buffer: Handle of user provided buffer updated with wave count
278  *
279  * Return: Number of bytes written to user buffer or an error value
280  */
281 static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
282 {
283 	int cu_cnt;
284 	int wave_cnt;
285 	int max_waves_per_cu;
286 	struct kfd_node *dev = NULL;
287 	struct kfd_process *proc = NULL;
288 	struct kfd_process_device *pdd = NULL;
289 	int i;
290 	struct kfd_cu_occupancy *cu_occupancy;
291 	u32 queue_format;
292 
293 	pdd = container_of(attr, struct kfd_process_device, attr_cu_occupancy);
294 	dev = pdd->dev;
295 	if (dev->kfd2kgd->get_cu_occupancy == NULL)
296 		return -EINVAL;
297 
298 	cu_cnt = 0;
299 	proc = pdd->process;
300 	if (pdd->qpd.queue_count == 0) {
301 		pr_debug("Gpu-Id: %d has no active queues for process pid %d\n",
302 			 dev->id, (int)proc->lead_thread->pid);
303 		return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
304 	}
305 
306 	/* Collect wave count from device if it supports */
307 	wave_cnt = 0;
308 	max_waves_per_cu = 0;
309 
310 	cu_occupancy = kzalloc_objs(*cu_occupancy, AMDGPU_MAX_QUEUES);
311 	if (!cu_occupancy)
312 		return -ENOMEM;
313 
314 	/*
315 	 * For GFX 9.4.3, fetch the CU occupancy from the first XCC in the partition.
316 	 * For AQL queues, because of cooperative dispatch we multiply the wave count
317 	 * by number of XCCs in the partition to get the total wave counts across all
318 	 * XCCs in the partition.
319 	 * For PM4 queues, there is no cooperative dispatch so wave_cnt stay as it is.
320 	 */
321 	amdgpu_gfx_off_ctrl(dev->adev, false);
322 	dev->kfd2kgd->get_cu_occupancy(dev->adev, cu_occupancy,
323 			&max_waves_per_cu, ffs(dev->xcc_mask) - 1);
324 	amdgpu_gfx_off_ctrl(dev->adev, true);
325 
326 	for (i = 0; i < AMDGPU_MAX_QUEUES; i++) {
327 		if (cu_occupancy[i].wave_cnt != 0 &&
328 		    kfd_dqm_is_queue_in_process(dev->dqm, &pdd->qpd,
329 						cu_occupancy[i].doorbell_off,
330 						&queue_format)) {
331 			if (unlikely(queue_format == KFD_QUEUE_FORMAT_PM4))
332 				wave_cnt += cu_occupancy[i].wave_cnt;
333 			else
334 				wave_cnt += (NUM_XCC(dev->xcc_mask) *
335 						cu_occupancy[i].wave_cnt);
336 		}
337 	}
338 
339 	/* Translate wave count to number of compute units */
340 	cu_cnt = (wave_cnt + (max_waves_per_cu - 1)) / max_waves_per_cu;
341 	kfree(cu_occupancy);
342 	return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
343 }
344 
345 static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr,
346 			       char *buffer)
347 {
348 	if (strcmp(attr->name, "pasid") == 0)
349 		return snprintf(buffer, PAGE_SIZE, "%d\n", 0);
350 	else if (strncmp(attr->name, "vram_", 5) == 0) {
351 		struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
352 							      attr_vram);
353 		return snprintf(buffer, PAGE_SIZE, "%llu\n", atomic64_read(&pdd->vram_usage));
354 	} else if (strncmp(attr->name, "sdma_", 5) == 0) {
355 		struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
356 							      attr_sdma);
357 		struct kfd_sdma_activity_handler_workarea sdma_activity_work_handler;
358 
359 		INIT_WORK_ONSTACK(&sdma_activity_work_handler.sdma_activity_work,
360 				  kfd_sdma_activity_worker);
361 
362 		sdma_activity_work_handler.pdd = pdd;
363 		sdma_activity_work_handler.sdma_activity_counter = 0;
364 
365 		schedule_work(&sdma_activity_work_handler.sdma_activity_work);
366 
367 		flush_work(&sdma_activity_work_handler.sdma_activity_work);
368 		destroy_work_on_stack(&sdma_activity_work_handler.sdma_activity_work);
369 
370 		return snprintf(buffer, PAGE_SIZE, "%llu\n",
371 				(sdma_activity_work_handler.sdma_activity_counter)/
372 				 SDMA_ACTIVITY_DIVISOR);
373 	} else {
374 		pr_err("Invalid attribute");
375 		return -EINVAL;
376 	}
377 
378 	return 0;
379 }
380 
381 static void kfd_procfs_kobj_release(struct kobject *kobj)
382 {
383 	kfree(kobj);
384 }
385 
386 static const struct sysfs_ops kfd_procfs_ops = {
387 	.show = kfd_procfs_show,
388 };
389 
390 static const struct kobj_type procfs_type = {
391 	.release = kfd_procfs_kobj_release,
392 	.sysfs_ops = &kfd_procfs_ops,
393 };
394 
395 void kfd_procfs_init(void)
396 {
397 	int ret = 0;
398 
399 	procfs.kobj = kfd_alloc_struct(procfs.kobj);
400 	if (!procfs.kobj)
401 		return;
402 
403 	ret = kobject_init_and_add(procfs.kobj, &procfs_type,
404 				   &kfd_device->kobj, "proc");
405 	if (ret) {
406 		pr_warn("Could not create procfs proc folder");
407 		/* If we fail to create the procfs, clean up */
408 		kfd_procfs_shutdown();
409 	}
410 }
411 
412 void kfd_procfs_shutdown(void)
413 {
414 	if (procfs.kobj) {
415 		kobject_del(procfs.kobj);
416 		kobject_put(procfs.kobj);
417 		procfs.kobj = NULL;
418 	}
419 }
420 
421 static ssize_t kfd_procfs_queue_show(struct kobject *kobj,
422 				     struct attribute *attr, char *buffer)
423 {
424 	struct queue *q = container_of(kobj, struct queue, kobj);
425 
426 	if (!strcmp(attr->name, "size"))
427 		return snprintf(buffer, PAGE_SIZE, "%llu",
428 				q->properties.queue_size);
429 	else if (!strcmp(attr->name, "type"))
430 		return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type);
431 	else if (!strcmp(attr->name, "gpuid"))
432 		return snprintf(buffer, PAGE_SIZE, "%u", q->device->id);
433 	else
434 		pr_err("Invalid attribute");
435 
436 	return 0;
437 }
438 
439 static ssize_t kfd_procfs_stats_show(struct kobject *kobj,
440 				     struct attribute *attr, char *buffer)
441 {
442 	if (strcmp(attr->name, "evicted_ms") == 0) {
443 		struct kfd_process_device *pdd = container_of(attr,
444 				struct kfd_process_device,
445 				attr_evict);
446 		uint64_t evict_jiffies;
447 
448 		evict_jiffies = atomic64_read(&pdd->evict_duration_counter);
449 
450 		return snprintf(buffer,
451 				PAGE_SIZE,
452 				"%llu\n",
453 				jiffies64_to_msecs(evict_jiffies));
454 
455 	/* Sysfs handle that gets CU occupancy is per device */
456 	} else if (strcmp(attr->name, "cu_occupancy") == 0) {
457 		return kfd_get_cu_occupancy(attr, buffer);
458 	} else {
459 		pr_err("Invalid attribute");
460 	}
461 
462 	return 0;
463 }
464 
465 static ssize_t kfd_sysfs_counters_show(struct kobject *kobj,
466 				       struct attribute *attr, char *buf)
467 {
468 	struct kfd_process_device *pdd;
469 
470 	if (!strcmp(attr->name, "faults")) {
471 		pdd = container_of(attr, struct kfd_process_device,
472 				   attr_faults);
473 		return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->faults));
474 	}
475 	if (!strcmp(attr->name, "page_in")) {
476 		pdd = container_of(attr, struct kfd_process_device,
477 				   attr_page_in);
478 		return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->page_in));
479 	}
480 	if (!strcmp(attr->name, "page_out")) {
481 		pdd = container_of(attr, struct kfd_process_device,
482 				   attr_page_out);
483 		return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->page_out));
484 	}
485 	return 0;
486 }
487 
488 static struct attribute attr_queue_size = {
489 	.name = "size",
490 	.mode = KFD_SYSFS_FILE_MODE
491 };
492 
493 static struct attribute attr_queue_type = {
494 	.name = "type",
495 	.mode = KFD_SYSFS_FILE_MODE
496 };
497 
498 static struct attribute attr_queue_gpuid = {
499 	.name = "gpuid",
500 	.mode = KFD_SYSFS_FILE_MODE
501 };
502 
503 static struct attribute *procfs_queue_attrs[] = {
504 	&attr_queue_size,
505 	&attr_queue_type,
506 	&attr_queue_gpuid,
507 	NULL
508 };
509 ATTRIBUTE_GROUPS(procfs_queue);
510 
511 static const struct sysfs_ops procfs_queue_ops = {
512 	.show = kfd_procfs_queue_show,
513 };
514 
515 static const struct kobj_type procfs_queue_type = {
516 	.sysfs_ops = &procfs_queue_ops,
517 	.default_groups = procfs_queue_groups,
518 };
519 
520 static const struct sysfs_ops procfs_stats_ops = {
521 	.show = kfd_procfs_stats_show,
522 };
523 
524 static const struct kobj_type procfs_stats_type = {
525 	.sysfs_ops = &procfs_stats_ops,
526 	.release = kfd_procfs_kobj_release,
527 };
528 
529 static const struct sysfs_ops sysfs_counters_ops = {
530 	.show = kfd_sysfs_counters_show,
531 };
532 
533 static const struct kobj_type sysfs_counters_type = {
534 	.sysfs_ops = &sysfs_counters_ops,
535 	.release = kfd_procfs_kobj_release,
536 };
537 
538 int kfd_procfs_add_queue(struct queue *q)
539 {
540 	struct kfd_process *proc;
541 	int ret;
542 
543 	if (!q || !q->process)
544 		return -EINVAL;
545 	proc = q->process;
546 
547 	/* Create proc/<pid>/queues/<queue id> folder */
548 	if (!proc->kobj_queues)
549 		return -EFAULT;
550 	ret = kobject_init_and_add(&q->kobj, &procfs_queue_type,
551 			proc->kobj_queues, "%u", q->properties.queue_id);
552 	if (ret < 0) {
553 		pr_warn("Creating proc/<pid>/queues/%u failed",
554 			q->properties.queue_id);
555 		kobject_put(&q->kobj);
556 		return ret;
557 	}
558 
559 	return 0;
560 }
561 
562 static void kfd_sysfs_create_file(struct kobject *kobj, struct attribute *attr,
563 				 char *name)
564 {
565 	int ret;
566 
567 	if (!kobj || !attr || !name)
568 		return;
569 
570 	attr->name = name;
571 	attr->mode = KFD_SYSFS_FILE_MODE;
572 	sysfs_attr_init(attr);
573 
574 	ret = sysfs_create_file(kobj, attr);
575 	if (ret)
576 		pr_warn("Create sysfs %s/%s failed %d", kobj->name, name, ret);
577 }
578 
579 static void kfd_procfs_add_sysfs_stats(struct kfd_process *p)
580 {
581 	int ret;
582 	int i;
583 	char stats_dir_filename[MAX_SYSFS_FILENAME_LEN];
584 
585 	if (!p || !p->kobj)
586 		return;
587 
588 	/*
589 	 * Create sysfs files for each GPU:
590 	 * - proc/<pid>/stats_<gpuid>/
591 	 * - proc/<pid>/stats_<gpuid>/evicted_ms
592 	 * - proc/<pid>/stats_<gpuid>/cu_occupancy
593 	 */
594 	for (i = 0; i < p->n_pdds; i++) {
595 		struct kfd_process_device *pdd = p->pdds[i];
596 
597 		snprintf(stats_dir_filename, MAX_SYSFS_FILENAME_LEN,
598 				"stats_%u", pdd->dev->id);
599 		pdd->kobj_stats = kfd_alloc_struct(pdd->kobj_stats);
600 		if (!pdd->kobj_stats)
601 			return;
602 
603 		ret = kobject_init_and_add(pdd->kobj_stats,
604 					   &procfs_stats_type,
605 					   p->kobj,
606 					   "%s", stats_dir_filename);
607 
608 		if (ret) {
609 			pr_warn("Creating KFD proc/stats_%s folder failed",
610 				stats_dir_filename);
611 			kobject_put(pdd->kobj_stats);
612 			pdd->kobj_stats = NULL;
613 			return;
614 		}
615 
616 		kfd_sysfs_create_file(pdd->kobj_stats, &pdd->attr_evict,
617 				      "evicted_ms");
618 		/* Add sysfs file to report compute unit occupancy */
619 		if (pdd->dev->kfd2kgd->get_cu_occupancy)
620 			kfd_sysfs_create_file(pdd->kobj_stats,
621 					      &pdd->attr_cu_occupancy,
622 					      "cu_occupancy");
623 	}
624 }
625 
626 static void kfd_procfs_add_sysfs_counters(struct kfd_process *p)
627 {
628 	int ret = 0;
629 	int i;
630 	char counters_dir_filename[MAX_SYSFS_FILENAME_LEN];
631 
632 	if (!p || !p->kobj)
633 		return;
634 
635 	/*
636 	 * Create sysfs files for each GPU which supports SVM
637 	 * - proc/<pid>/counters_<gpuid>/
638 	 * - proc/<pid>/counters_<gpuid>/faults
639 	 * - proc/<pid>/counters_<gpuid>/page_in
640 	 * - proc/<pid>/counters_<gpuid>/page_out
641 	 */
642 	for_each_set_bit(i, p->svms.bitmap_supported, p->n_pdds) {
643 		struct kfd_process_device *pdd = p->pdds[i];
644 		struct kobject *kobj_counters;
645 
646 		snprintf(counters_dir_filename, MAX_SYSFS_FILENAME_LEN,
647 			"counters_%u", pdd->dev->id);
648 		kobj_counters = kfd_alloc_struct(kobj_counters);
649 		if (!kobj_counters)
650 			return;
651 
652 		ret = kobject_init_and_add(kobj_counters, &sysfs_counters_type,
653 					   p->kobj, "%s", counters_dir_filename);
654 		if (ret) {
655 			pr_warn("Creating KFD proc/%s folder failed",
656 				counters_dir_filename);
657 			kobject_put(kobj_counters);
658 			return;
659 		}
660 
661 		pdd->kobj_counters = kobj_counters;
662 		kfd_sysfs_create_file(kobj_counters, &pdd->attr_faults,
663 				      "faults");
664 		kfd_sysfs_create_file(kobj_counters, &pdd->attr_page_in,
665 				      "page_in");
666 		kfd_sysfs_create_file(kobj_counters, &pdd->attr_page_out,
667 				      "page_out");
668 	}
669 }
670 
671 static void kfd_procfs_add_sysfs_files(struct kfd_process *p)
672 {
673 	int i;
674 
675 	if (!p || !p->kobj)
676 		return;
677 
678 	/*
679 	 * Create sysfs files for each GPU:
680 	 * - proc/<pid>/vram_<gpuid>
681 	 * - proc/<pid>/sdma_<gpuid>
682 	 */
683 	for (i = 0; i < p->n_pdds; i++) {
684 		struct kfd_process_device *pdd = p->pdds[i];
685 
686 		snprintf(pdd->vram_filename, MAX_SYSFS_FILENAME_LEN, "vram_%u",
687 			 pdd->dev->id);
688 		kfd_sysfs_create_file(p->kobj, &pdd->attr_vram,
689 				      pdd->vram_filename);
690 
691 		snprintf(pdd->sdma_filename, MAX_SYSFS_FILENAME_LEN, "sdma_%u",
692 			 pdd->dev->id);
693 		kfd_sysfs_create_file(p->kobj, &pdd->attr_sdma,
694 					    pdd->sdma_filename);
695 	}
696 }
697 
698 void kfd_procfs_del_queue(struct queue *q)
699 {
700 	if (!q || !q->process->kobj)
701 		return;
702 
703 	kobject_del(&q->kobj);
704 	kobject_put(&q->kobj);
705 }
706 
707 int kfd_process_create_wq(void)
708 {
709 	if (!kfd_process_wq)
710 		kfd_process_wq = alloc_workqueue("kfd_process_wq", WQ_UNBOUND,
711 						 0);
712 	if (!kfd_restore_wq)
713 		kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq",
714 							 WQ_FREEZABLE);
715 
716 	if (!kfd_process_wq || !kfd_restore_wq) {
717 		kfd_process_destroy_wq();
718 		return -ENOMEM;
719 	}
720 
721 	return 0;
722 }
723 
724 void kfd_process_destroy_wq(void)
725 {
726 	if (kfd_process_wq) {
727 		destroy_workqueue(kfd_process_wq);
728 		kfd_process_wq = NULL;
729 	}
730 	if (kfd_restore_wq) {
731 		destroy_workqueue(kfd_restore_wq);
732 		kfd_restore_wq = NULL;
733 	}
734 }
735 
736 static void kfd_process_free_gpuvm(struct kgd_mem *mem,
737 			struct kfd_process_device *pdd, void **kptr)
738 {
739 	struct kfd_node *dev = pdd->dev;
740 
741 	if (kptr && *kptr) {
742 		amdgpu_amdkfd_gpuvm_unmap_bo_from_kernel(mem);
743 		*kptr = NULL;
744 	}
745 
746 	amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->adev, mem, pdd->drm_priv);
747 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->adev, mem, pdd->drm_priv,
748 					       NULL);
749 }
750 
751 static void kfd_process_free_gpuvm_map(struct kgd_mem *mem,
752 				       struct kfd_process_device *pdd,
753 				       struct iosys_map *map)
754 {
755 	struct kfd_node *dev = pdd->dev;
756 
757 	if (map && !iosys_map_is_null(map)) {
758 		amdgpu_amdkfd_gpuvm_unmap_bo_from_kernel(mem);
759 		iosys_map_clear(map);
760 	}
761 
762 	amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->adev, mem, pdd->drm_priv);
763 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->adev, mem, pdd->drm_priv,
764 					       NULL);
765 }
766 
767 /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
768  *	This function should be only called right after the process
769  *	is created and when kfd_processes_mutex is still being held
770  *	to avoid concurrency. Because of that exclusiveness, we do
771  *	not need to take p->mutex.
772  */
773 static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
774 				   uint64_t gpu_va, uint32_t size,
775 				   uint32_t flags, struct kgd_mem **mem, void **kptr)
776 {
777 	struct kfd_node *kdev = pdd->dev;
778 	int err;
779 
780 	err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->adev, gpu_va, size,
781 						 pdd->drm_priv, mem, NULL,
782 						 flags, false);
783 	if (err)
784 		goto err_alloc_mem;
785 
786 	err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->adev, *mem,
787 			pdd->drm_priv);
788 	if (err)
789 		goto err_map_mem;
790 
791 	err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->adev, *mem, true);
792 	if (err) {
793 		pr_debug("Sync memory failed, wait interrupted by user signal\n");
794 		goto sync_memory_failed;
795 	}
796 
797 	if (kptr) {
798 		u32 domain;
799 
800 		if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM)
801 			domain = AMDGPU_GEM_DOMAIN_VRAM;
802 		else
803 			domain = AMDGPU_GEM_DOMAIN_GTT;
804 		err = amdgpu_amdkfd_gpuvm_map_bo_to_kernel((struct kgd_mem *)*mem,
805 							   kptr, NULL, domain);
806 		if (err) {
807 			pr_debug("Map BO to kernel failed err %d\n", err);
808 			goto sync_memory_failed;
809 		}
810 	}
811 
812 	return err;
813 
814 sync_memory_failed:
815 	amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(kdev->adev, *mem, pdd->drm_priv);
816 
817 err_map_mem:
818 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->adev, *mem, pdd->drm_priv,
819 					       NULL);
820 err_alloc_mem:
821 	*mem = NULL;
822 	*kptr = NULL;
823 	return err;
824 }
825 
826 /* kfd_process_device_reserve_ib_mem - Reserve memory inside the
827  *	process for IB usage The memory reserved is for KFD to submit
828  *	IB to AMDGPU from kernel.  If the memory is reserved
829  *	successfully, ib_kaddr will have the CPU/kernel
830  *	address. Check ib_kaddr before accessing the memory.
831  */
832 static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
833 {
834 	struct qcm_process_device *qpd = &pdd->qpd;
835 	uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT |
836 			KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
837 			KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE |
838 			KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
839 	struct kgd_mem *mem;
840 	void *kaddr;
841 	int ret;
842 
843 	if (qpd->ib_kaddr || !qpd->ib_base)
844 		return 0;
845 
846 	/* ib_base is only set for dGPU */
847 	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
848 				      &mem, &kaddr);
849 	if (ret)
850 		return ret;
851 
852 	qpd->ib_mem = mem;
853 	qpd->ib_kaddr = kaddr;
854 
855 	return 0;
856 }
857 
858 static void kfd_process_device_destroy_ib_mem(struct kfd_process_device *pdd)
859 {
860 	struct qcm_process_device *qpd = &pdd->qpd;
861 
862 	if (!qpd->ib_kaddr || !qpd->ib_base)
863 		return;
864 
865 	kfd_process_free_gpuvm(qpd->ib_mem, pdd, &qpd->ib_kaddr);
866 }
867 
868 int kfd_create_process_sysfs(struct kfd_process *process)
869 {
870 	struct kfd_process *primary_process;
871 	int ret;
872 
873 	if (process->kobj) {
874 		pr_warn("kobject already exists for the kfd_process\n");
875 		return -EINVAL;
876 	}
877 
878 	process->kobj = kfd_alloc_struct(process->kobj);
879 	if (!process->kobj) {
880 		pr_warn("Creating procfs kobject failed");
881 		return -ENOMEM;
882 	}
883 
884 	if (process->context_id == KFD_CONTEXT_ID_PRIMARY)
885 		ret = kobject_init_and_add(process->kobj, &procfs_type,
886 					   procfs.kobj, "%d",
887 					   (int)process->lead_thread->pid);
888 	else {
889 		primary_process = kfd_lookup_process_by_mm(process->lead_thread->mm);
890 		if (!primary_process)
891 			return -ESRCH;
892 
893 		ret = kobject_init_and_add(process->kobj, &procfs_type,
894 					   primary_process->kobj, "context_%u",
895 					   process->context_id);
896 		kfd_unref_process(primary_process);
897 	}
898 
899 	if (ret) {
900 		pr_warn("Creating procfs pid directory failed");
901 		kobject_put(process->kobj);
902 		process->kobj = NULL;
903 		return ret;
904 	}
905 
906 	kfd_sysfs_create_file(process->kobj, &process->attr_pasid,
907 			      "pasid");
908 
909 	process->kobj_queues = kobject_create_and_add("queues",
910 						process->kobj);
911 	if (!process->kobj_queues)
912 		pr_warn("Creating KFD proc/queues folder failed");
913 
914 	kfd_procfs_add_sysfs_stats(process);
915 	kfd_procfs_add_sysfs_files(process);
916 	kfd_procfs_add_sysfs_counters(process);
917 
918 	return 0;
919 }
920 
921 static int kfd_process_alloc_id(struct kfd_process *process)
922 {
923 	int ret;
924 	struct kfd_process *primary_process;
925 
926 	/* already assign 0xFFFF when create */
927 	if (process->context_id == KFD_CONTEXT_ID_PRIMARY)
928 		return 0;
929 
930 	primary_process = kfd_lookup_process_by_mm(process->lead_thread->mm);
931 	if (!primary_process)
932 		return -ESRCH;
933 
934 	/* id range: KFD_CONTEXT_ID_MIN to 0xFFFE */
935 	ret = ida_alloc_range(&primary_process->id_table, KFD_CONTEXT_ID_MIN,
936 	      KFD_CONTEXT_ID_PRIMARY - 1, GFP_KERNEL);
937 	if (ret < 0)
938 		goto out;
939 
940 	process->context_id = ret;
941 	ret = 0;
942 
943 out:
944 	kfd_unref_process(primary_process);
945 
946 	return ret;
947 }
948 
949 static void kfd_process_free_id(struct kfd_process *process)
950 {
951 	struct kfd_process *primary_process;
952 
953 	if (process->context_id == KFD_CONTEXT_ID_PRIMARY)
954 		return;
955 
956 	primary_process = kfd_lookup_process_by_mm(process->lead_thread->mm);
957 	if (!primary_process)
958 		return;
959 
960 	ida_free(&primary_process->id_table, process->context_id);
961 
962 	kfd_unref_process(primary_process);
963 }
964 
965 struct kfd_process *kfd_create_process(struct task_struct *thread)
966 {
967 	struct kfd_process *process;
968 	int ret;
969 
970 	if (!(thread->mm && mmget_not_zero(thread->mm)))
971 		return ERR_PTR(-EINVAL);
972 
973 	/* If the process just called exec(3), it is possible that the
974 	 * cleanup of the kfd_process (following the release of the mm
975 	 * of the old process image) is still in the cleanup work queue.
976 	 * Make sure to drain any job before trying to recreate any
977 	 * resource for this process.
978 	 */
979 	flush_workqueue(kfd_process_wq);
980 
981 	/*
982 	 * take kfd processes mutex before starting of process creation
983 	 * so there won't be a case where two threads of the same process
984 	 * create two kfd_process structures
985 	 */
986 	mutex_lock(&kfd_processes_mutex);
987 
988 	if (kfd_gpu_node_num() <= 0) {
989 		pr_warn("no gpu node! Cannot create KFD process");
990 		process = ERR_PTR(-EINVAL);
991 		goto out;
992 	}
993 
994 	if (kfd_is_locked(NULL)) {
995 		pr_debug("KFD is locked! Cannot create process");
996 		process = ERR_PTR(-EINVAL);
997 		goto out;
998 	}
999 
1000 	/* A prior open of /dev/kfd could have already created the process.
1001 	 * find_process will increase process kref in this case
1002 	 */
1003 	process = find_process(thread, true);
1004 	if (process) {
1005 		pr_debug("Process already found\n");
1006 	} else {
1007 		process = create_process(thread, true);
1008 		if (IS_ERR(process))
1009 			goto out;
1010 
1011 		if (!procfs.kobj)
1012 			goto out;
1013 
1014 		ret = kfd_create_process_sysfs(process);
1015 		if (ret)
1016 			pr_warn("Failed to create sysfs entry for the kfd_process");
1017 
1018 		ret = kfd_debugfs_add_process(process);
1019 		if (ret)
1020 			pr_warn("Failed to create debugfs entry for the kfd_process, ret = %d\n",
1021 				ret);
1022 
1023 		init_waitqueue_head(&process->wait_irq_drain);
1024 	}
1025 out:
1026 	mutex_unlock(&kfd_processes_mutex);
1027 	mmput(thread->mm);
1028 
1029 	return process;
1030 }
1031 
1032 /**
1033  * amdgpu_amdkfd_set_sigbus_delay - Set per-process KFD SIGBUS delay
1034  * @task: task in the target process
1035  * @ms:   encoded delay value (0 = immediate, 0xFFFFFFFF = suppress,
1036  *        otherwise delay in milliseconds)
1037  *
1038  * Stores the SIGBUS delivery option on the kfd_process associated with
1039  * @task. If the calling process has not opened /dev/kfd yet (no
1040  * kfd_process exists), this is a no-op - the option only applies to
1041  * processes that actually use KFD.
1042  */
1043 int amdgpu_amdkfd_set_sigbus_delay(struct task_struct *task, u32 ms)
1044 {
1045 	struct kfd_process *p;
1046 
1047 	if (!task->mm)
1048 		return -EINVAL;
1049 
1050 	p = kfd_lookup_process_by_mm(task->mm);
1051 	if (!p)
1052 		return 0;
1053 
1054 	atomic_set(&p->kfd_sigbus_delay_ms, ms);
1055 	kfd_unref_process(p);
1056 	return 0;
1057 }
1058 
1059 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
1060 {
1061 	struct kfd_process *process;
1062 
1063 	hash_for_each_possible_rcu(kfd_processes_table, process,
1064 					kfd_processes, (uintptr_t)mm)
1065 		if (process->mm == mm && process->context_id == KFD_CONTEXT_ID_PRIMARY)
1066 			return process;
1067 
1068 	return NULL;
1069 }
1070 
1071 static struct kfd_process *find_process(const struct task_struct *thread,
1072 					bool ref)
1073 {
1074 	struct kfd_process *p;
1075 	int idx;
1076 
1077 	idx = srcu_read_lock(&kfd_processes_srcu);
1078 	p = find_process_by_mm(thread->mm);
1079 	if (p && ref)
1080 		kref_get(&p->ref);
1081 	srcu_read_unlock(&kfd_processes_srcu, idx);
1082 
1083 	return p;
1084 }
1085 
1086 void kfd_unref_process(struct kfd_process *p)
1087 {
1088 	kref_put(&p->ref, kfd_process_ref_release);
1089 }
1090 
1091 /* This increments the process->ref counter. */
1092 struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid)
1093 {
1094 	struct task_struct *task = NULL;
1095 	struct kfd_process *p    = NULL;
1096 
1097 	if (!pid) {
1098 		task = current;
1099 		get_task_struct(task);
1100 	} else {
1101 		task = get_pid_task(pid, PIDTYPE_PID);
1102 	}
1103 
1104 	if (task) {
1105 		p = find_process(task, true);
1106 		put_task_struct(task);
1107 	}
1108 
1109 	return p;
1110 }
1111 
1112 static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
1113 {
1114 	struct kfd_process *p = pdd->process;
1115 	void *mem;
1116 	int id;
1117 	int i;
1118 
1119 	/*
1120 	 * Remove all handles from idr and release appropriate
1121 	 * local memory object
1122 	 */
1123 	idr_for_each_entry(&pdd->alloc_idr, mem, id) {
1124 
1125 		for (i = 0; i < p->n_pdds; i++) {
1126 			struct kfd_process_device *peer_pdd = p->pdds[i];
1127 
1128 			if (!peer_pdd->drm_priv)
1129 				continue;
1130 			amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1131 				peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
1132 		}
1133 
1134 		amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, mem,
1135 						       pdd->drm_priv, NULL);
1136 		kfd_process_device_remove_obj_handle(pdd, id);
1137 	}
1138 }
1139 
1140 /*
1141  * Just kunmap and unpin signal BO here. It will be freed in
1142  * kfd_process_free_outstanding_kfd_bos()
1143  */
1144 static void kfd_process_kunmap_signal_bo(struct kfd_process *p)
1145 {
1146 	struct kfd_process_device *pdd;
1147 	struct kfd_node *kdev;
1148 	void *mem;
1149 
1150 	kdev = kfd_device_by_id(GET_GPU_ID(p->signal_handle));
1151 	if (!kdev)
1152 		return;
1153 
1154 	mutex_lock(&p->mutex);
1155 
1156 	pdd = kfd_get_process_device_data(kdev, p);
1157 	if (!pdd)
1158 		goto out;
1159 
1160 	mem = kfd_process_device_translate_handle(
1161 		pdd, GET_IDR_HANDLE(p->signal_handle));
1162 	if (!mem)
1163 		goto out;
1164 
1165 	amdgpu_amdkfd_gpuvm_unmap_bo_from_kernel(mem);
1166 
1167 out:
1168 	mutex_unlock(&p->mutex);
1169 }
1170 
1171 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
1172 {
1173 	int i;
1174 
1175 	for (i = 0; i < p->n_pdds; i++)
1176 		kfd_process_device_free_bos(p->pdds[i]);
1177 }
1178 
1179 static void kfd_process_profiler_release(struct kfd_process *p, struct kfd_process_device *pdd)
1180 {
1181 	mutex_lock(&pdd->dev->kfd->profiler_lock);
1182 	if (pdd->dev->kfd->profiler_process == p) {
1183 		pdd->qpd.dqm->ops.set_perfcount(pdd->qpd.dqm, 0);
1184 		pdd->dev->kfd->profiler_process = NULL;
1185 	}
1186 	mutex_unlock(&pdd->dev->kfd->profiler_lock);
1187 }
1188 
1189 static void kfd_process_destroy_pdds(struct kfd_process *p)
1190 {
1191 	int i;
1192 
1193 	for (i = 0; i < p->n_pdds; i++) {
1194 		struct kfd_process_device *pdd = p->pdds[i];
1195 
1196 		kfd_smi_event_process(pdd, false);
1197 
1198 		pr_debug("Releasing pdd (topology id %d, for pid %d)\n",
1199 			pdd->dev->id, p->lead_thread->pid);
1200 		kfd_process_profiler_release(p, pdd);
1201 
1202 		if (pdd->ptl_disable_req)
1203 			kfd_ptl_disable_release(pdd, p);
1204 
1205 		kfd_process_device_destroy_cwsr_dgpu(pdd);
1206 		kfd_process_device_destroy_ib_mem(pdd);
1207 
1208 		if (pdd->drm_file)
1209 			fput(pdd->drm_file);
1210 
1211 		if (!iosys_map_is_null(&pdd->qpd.cwsr_map) && !pdd->qpd.cwsr_base)
1212 			free_pages((unsigned long)pdd->qpd.cwsr_map.vaddr,
1213 				   get_order(KFD_CWSR_TBA_TMA_SIZE));
1214 
1215 		idr_destroy(&pdd->alloc_idr);
1216 
1217 		kfd_free_process_doorbells(pdd->dev->kfd, pdd);
1218 
1219 		if (pdd->dev->kfd->shared_resources.enable_mes &&
1220 			pdd->proc_ctx_cpu_ptr) {
1221 			amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
1222 						       pdd->proc_ctx_array_index);
1223 			amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
1224 						   &pdd->proc_ctx_bo);
1225 		}
1226 		/*
1227 		 * before destroying pdd, make sure to report availability
1228 		 * for auto suspend
1229 		 */
1230 		if (pdd->runtime_inuse) {
1231 			pm_runtime_put_autosuspend(adev_to_drm(pdd->dev->adev)->dev);
1232 			pdd->runtime_inuse = false;
1233 		}
1234 
1235 		atomic_dec(&pdd->dev->kfd->kfd_processes_count);
1236 
1237 		kfree(pdd);
1238 		p->pdds[i] = NULL;
1239 	}
1240 	p->n_pdds = 0;
1241 }
1242 
1243 static void kfd_process_remove_sysfs(struct kfd_process *p)
1244 {
1245 	struct kfd_process_device *pdd;
1246 	int i;
1247 
1248 	if (!p->kobj)
1249 		return;
1250 
1251 	if (p->kobj_queues) {
1252 		sysfs_remove_file(p->kobj, &p->attr_pasid);
1253 		kobject_del(p->kobj_queues);
1254 		kobject_put(p->kobj_queues);
1255 		p->kobj_queues = NULL;
1256 	}
1257 
1258 	for (i = 0; i < p->n_pdds; i++) {
1259 		pdd = p->pdds[i];
1260 
1261 		sysfs_remove_file(p->kobj, &pdd->attr_vram);
1262 		sysfs_remove_file(p->kobj, &pdd->attr_sdma);
1263 
1264 		if (pdd->kobj_stats) {
1265 			sysfs_remove_file(pdd->kobj_stats, &pdd->attr_evict);
1266 			if (pdd->dev->kfd2kgd->get_cu_occupancy)
1267 				sysfs_remove_file(pdd->kobj_stats,
1268 						  &pdd->attr_cu_occupancy);
1269 			kobject_del(pdd->kobj_stats);
1270 			kobject_put(pdd->kobj_stats);
1271 			pdd->kobj_stats = NULL;
1272 		}
1273 	}
1274 
1275 	for_each_set_bit(i, p->svms.bitmap_supported, p->n_pdds) {
1276 		pdd = p->pdds[i];
1277 		if (!pdd->kobj_counters)
1278 			continue;
1279 
1280 		sysfs_remove_file(pdd->kobj_counters, &pdd->attr_faults);
1281 		sysfs_remove_file(pdd->kobj_counters, &pdd->attr_page_in);
1282 		sysfs_remove_file(pdd->kobj_counters, &pdd->attr_page_out);
1283 		kobject_del(pdd->kobj_counters);
1284 		kobject_put(pdd->kobj_counters);
1285 		pdd->kobj_counters = NULL;
1286 	}
1287 
1288 	kobject_del(p->kobj);
1289 	kobject_put(p->kobj);
1290 	p->kobj = NULL;
1291 }
1292 
1293 /*
1294  * If any GPU is ongoing reset, wait for reset complete.
1295  */
1296 static void kfd_process_wait_gpu_reset_complete(struct kfd_process *p)
1297 {
1298 	int i;
1299 
1300 	for (i = 0; i < p->n_pdds; i++)
1301 		flush_workqueue(p->pdds[i]->dev->adev->reset_domain->wq);
1302 }
1303 
1304 /* No process locking is needed in this function, because the process
1305  * is not findable any more. We must assume that no other thread is
1306  * using it any more, otherwise we couldn't safely free the process
1307  * structure in the end.
1308  */
1309 static void kfd_process_wq_release(struct work_struct *work)
1310 {
1311 	struct kfd_process *p = container_of(work, struct kfd_process,
1312 					     release_work);
1313 	struct dma_fence *ef;
1314 
1315 	/*
1316 	 * If GPU in reset, user queues may still running, wait for reset complete.
1317 	 */
1318 	kfd_process_wait_gpu_reset_complete(p);
1319 
1320 	/* Signal the eviction fence after user mode queues are
1321 	 * destroyed. This allows any BOs to be freed without
1322 	 * triggering pointless evictions or waiting for fences.
1323 	 */
1324 	synchronize_rcu();
1325 	ef = rcu_access_pointer(p->ef);
1326 	if (ef)
1327 		dma_fence_signal(ef);
1328 
1329 	if (p->context_id != KFD_CONTEXT_ID_PRIMARY)
1330 		kfd_process_free_id(p);
1331 	else
1332 		ida_destroy(&p->id_table);
1333 
1334 	kfd_debugfs_remove_process(p);
1335 
1336 	/*
1337 	 * Remove the proc/sysfs entries before destroying PDDs. The removal path
1338 	 * walks the PDD array and sysfs callbacks dereference PDD fields, so the
1339 	 * backing data must remain valid until sysfs removal has completed.
1340 	 */
1341 	kfd_process_remove_sysfs(p);
1342 
1343 	kfd_process_kunmap_signal_bo(p);
1344 	kfd_process_free_outstanding_kfd_bos(p);
1345 	svm_range_list_fini(p);
1346 
1347 	kfd_process_destroy_pdds(p);
1348 	dma_fence_put(ef);
1349 
1350 	kfd_event_free_process(p);
1351 
1352 	mutex_destroy(&p->mutex);
1353 
1354 	put_task_struct(p->lead_thread);
1355 
1356 	kfree(p);
1357 }
1358 
1359 static void kfd_process_ref_release(struct kref *ref)
1360 {
1361 	struct kfd_process *p = container_of(ref, struct kfd_process, ref);
1362 
1363 	INIT_WORK(&p->release_work, kfd_process_wq_release);
1364 	queue_work(kfd_process_wq, &p->release_work);
1365 }
1366 
1367 static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
1368 {
1369 	/* This increments p->ref counter if kfd process p exists */
1370 	struct kfd_process *p = kfd_lookup_process_by_mm(mm);
1371 
1372 	return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
1373 }
1374 
1375 static void kfd_process_free_notifier(struct mmu_notifier *mn)
1376 {
1377 	kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier));
1378 }
1379 
1380 static void kfd_process_table_remove(struct kfd_process *p)
1381 {
1382 	mutex_lock(&kfd_processes_mutex);
1383 	/*
1384 	 * Do early return if table is empty.
1385 	 *
1386 	 * This could potentially happen if this function is called concurrently
1387 	 * by mmu_notifier and by kfd_cleanup_pocesses.
1388 	 *
1389 	 */
1390 	if (hash_empty(kfd_processes_table)) {
1391 		mutex_unlock(&kfd_processes_mutex);
1392 		return;
1393 	}
1394 	hash_del_rcu(&p->kfd_processes);
1395 	mutex_unlock(&kfd_processes_mutex);
1396 	synchronize_srcu(&kfd_processes_srcu);
1397 }
1398 
1399 void kfd_process_notifier_release_internal(struct kfd_process *p)
1400 {
1401 	int i;
1402 
1403 	kfd_process_table_remove(p);
1404 	cancel_delayed_work_sync(&p->eviction_work);
1405 	cancel_delayed_work_sync(&p->restore_work);
1406 	/*
1407 	 * If work pending, cancel it and drop the extra ref
1408 	 */
1409 	if (cancel_delayed_work_sync(&p->signal_work))
1410 		kfd_unref_process(p);
1411 
1412 	/*
1413 	 * Disable debug BEFORE dequeuing/flushing the MES process context.
1414 	 *
1415 	 * kfd_dbg_trap_disable() -> kfd_dbg_trap_deactivate() issues
1416 	 * SET_SHADER_DEBUGGER (process_ctx_flush=0) packets that re-add the
1417 	 * process to the MES scheduler list. kfd_process_dequeue_from_all_devices()
1418 	 * sends the process_ctx_flush=1 that retires the context. If debug
1419 	 * teardown ran after the dequeue, those packets would put the process
1420 	 * back on the MES list and, once proc_ctx_bo is freed, MES would fault
1421 	 * on the freed process context. Retiring the context last avoids this.
1422 	 */
1423 	kfd_dbg_trap_disable(p);
1424 
1425 	if (atomic_read(&p->debugged_process_count) > 0) {
1426 		struct kfd_process *target;
1427 		unsigned int temp;
1428 		int idx = srcu_read_lock(&kfd_processes_srcu);
1429 
1430 		hash_for_each_rcu(kfd_processes_table, temp, target, kfd_processes) {
1431 			if (target->debugger_process && target->debugger_process == p) {
1432 				mutex_lock_nested(&target->mutex, 1);
1433 				kfd_dbg_trap_disable(target);
1434 				mutex_unlock(&target->mutex);
1435 				if (atomic_read(&p->debugged_process_count) == 0)
1436 					break;
1437 			}
1438 		}
1439 
1440 		srcu_read_unlock(&kfd_processes_srcu, idx);
1441 	}
1442 
1443 	/*
1444 	 * Dequeue and destroy user queues, it is not safe for GPU to access
1445 	 * system memory after mmu release notifier callback returns because
1446 	 * exit_mmap free process memory afterwards.
1447 	 */
1448 	kfd_process_dequeue_from_all_devices(p);
1449 	pqm_uninit(&p->pqm);
1450 
1451 	for (i = 0; i < p->n_pdds; i++) {
1452 		struct kfd_process_device *pdd = p->pdds[i];
1453 
1454 		/* re-enable GFX OFF since runtime enable with ttmp setup disabled it. */
1455 		if (!kfd_dbg_is_rlc_restore_supported(pdd->dev) && p->runtime_info.ttmp_setup)
1456 			amdgpu_gfx_off_ctrl(pdd->dev->adev, true);
1457 	}
1458 
1459 	/* Indicate to other users that MM is no longer valid */
1460 	p->mm = NULL;
1461 
1462 	if (p->context_id == KFD_CONTEXT_ID_PRIMARY)
1463 		mmu_notifier_put(&p->mmu_notifier);
1464 }
1465 
1466 static void kfd_process_notifier_release(struct mmu_notifier *mn,
1467 					struct mm_struct *mm)
1468 {
1469 	struct kfd_process *p;
1470 
1471 	/*
1472 	 * The kfd_process structure can not be free because the
1473 	 * mmu_notifier srcu is read locked
1474 	 */
1475 	p = container_of(mn, struct kfd_process, mmu_notifier);
1476 	if (WARN_ON(p->mm != mm))
1477 		return;
1478 
1479 	kfd_process_notifier_release_internal(p);
1480 }
1481 
1482 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
1483 	.release = kfd_process_notifier_release,
1484 	.alloc_notifier = kfd_process_alloc_notifier,
1485 	.free_notifier = kfd_process_free_notifier,
1486 };
1487 
1488 /*
1489  * This code handles the case when driver is being unloaded before all
1490  * mm_struct are released.  We need to safely free the kfd_process and
1491  * avoid race conditions with mmu_notifier that might try to free them.
1492  *
1493  */
1494 void kfd_cleanup_processes(void)
1495 {
1496 	struct kfd_process *p;
1497 	struct hlist_node *p_temp;
1498 	unsigned int temp;
1499 	HLIST_HEAD(cleanup_list);
1500 
1501 	/*
1502 	 * Move all remaining kfd_process from the process table to a
1503 	 * temp list for processing.   Once done, callback from mmu_notifier
1504 	 * release will not see the kfd_process in the table and do early return,
1505 	 * avoiding double free issues.
1506 	 */
1507 	mutex_lock(&kfd_processes_mutex);
1508 	hash_for_each_safe(kfd_processes_table, temp, p_temp, p, kfd_processes) {
1509 		hash_del_rcu(&p->kfd_processes);
1510 		synchronize_srcu(&kfd_processes_srcu);
1511 		hlist_add_head(&p->kfd_processes, &cleanup_list);
1512 	}
1513 	mutex_unlock(&kfd_processes_mutex);
1514 
1515 	hlist_for_each_entry_safe(p, p_temp, &cleanup_list, kfd_processes)
1516 		kfd_process_notifier_release_internal(p);
1517 
1518 	/*
1519 	 * Ensures that all outstanding free_notifier get called, triggering
1520 	 * the release of the kfd_process struct.
1521 	 */
1522 	mmu_notifier_synchronize();
1523 }
1524 
1525 static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
1526 {
1527 	struct kfd_node *dev = pdd->dev;
1528 	struct qcm_process_device *qpd = &pdd->qpd;
1529 	u32 flags = KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE
1530 			| KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
1531 	struct kgd_mem *mem;
1532 	void *kaddr;
1533 	int ret;
1534 
1535 	if (!dev->kfd->cwsr_enabled || !iosys_map_is_null(&qpd->cwsr_map) || !qpd->cwsr_base)
1536 		return 0;
1537 
1538 	if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 4, 2) && !dev->adev->apu_prefer_gtt)
1539 		flags |= KFD_IOC_ALLOC_MEM_FLAGS_VRAM;
1540 	else
1541 		flags |= KFD_IOC_ALLOC_MEM_FLAGS_GTT;
1542 
1543 	/* Allocate CWSR TBA/TMA buffers */
1544 	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
1545 				      KFD_CWSR_TBA_TMA_SIZE, flags, &mem, &kaddr);
1546 	if (ret)
1547 		return ret;
1548 
1549 	qpd->cwsr_mem = mem;
1550 
1551 	/* Set up iosys_map based on whether memory is MMIO or system memory */
1552 	if (mem->bo->kmap.bo_kmap_type & TTM_BO_MAP_IOMEM_MASK)
1553 		iosys_map_set_vaddr_iomem(&qpd->cwsr_map, kaddr);
1554 	else
1555 		iosys_map_set_vaddr(&qpd->cwsr_map, kaddr);
1556 
1557 	qpd->tba_addr = qpd->cwsr_base;
1558 
1559 	/* Copy CWSR ISA to buffer using appropriate accessor */
1560 	iosys_map_memcpy_to(&qpd->cwsr_map, 0, dev->kfd->cwsr_isa,
1561 			    dev->kfd->cwsr_isa_size);
1562 
1563 	kfd_process_set_trap_debug_flag(&pdd->qpd,
1564 					pdd->process->debug_trap_enabled);
1565 
1566 	qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
1567 	pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_map:%s at %p for pqm.\n",
1568 		 qpd->tba_addr, qpd->tma_addr,
1569 		 qpd->cwsr_map.is_iomem ? "iomem" : "system",
1570 		 qpd->cwsr_map.is_iomem ? (void *)qpd->cwsr_map.vaddr_iomem :
1571 					  qpd->cwsr_map.vaddr);
1572 
1573 	return 0;
1574 }
1575 
1576 static void kfd_process_device_destroy_cwsr_dgpu(struct kfd_process_device *pdd)
1577 {
1578 	struct kfd_node *dev = pdd->dev;
1579 	struct qcm_process_device *qpd = &pdd->qpd;
1580 
1581 	if (!dev->kfd->cwsr_enabled || iosys_map_is_null(&qpd->cwsr_map) || !qpd->cwsr_base)
1582 		return;
1583 
1584 	kfd_process_free_gpuvm_map(qpd->cwsr_mem, pdd, &qpd->cwsr_map);
1585 }
1586 
1587 void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
1588 				  uint64_t tba_addr,
1589 				  uint64_t tma_addr)
1590 {
1591 	if (!iosys_map_is_null(&qpd->cwsr_map)) {
1592 		/* KFD trap handler is bound, record as second-level TBA/TMA
1593 		 * in first-level TMA. First-level trap will jump to second.
1594 		 */
1595 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET,
1596 			     uint64_t, tba_addr);
1597 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + sizeof(uint64_t),
1598 			     uint64_t, tma_addr);
1599 	} else {
1600 		/* No trap handler bound, bind as first-level TBA/TMA. */
1601 		qpd->tba_addr = tba_addr;
1602 		qpd->tma_addr = tma_addr;
1603 	}
1604 }
1605 
1606 bool kfd_process_xnack_mode(struct kfd_process *p, bool supported)
1607 {
1608 	int i;
1609 
1610 	/* On most GFXv9 GPUs, the retry mode in the SQ must match the
1611 	 * boot time retry setting. Mixing processes with different
1612 	 * XNACK/retry settings can hang the GPU.
1613 	 *
1614 	 * Different GPUs can have different noretry settings depending
1615 	 * on HW bugs or limitations. We need to find at least one
1616 	 * XNACK mode for this process that's compatible with all GPUs.
1617 	 * Fortunately GPUs with retry enabled (noretry=0) can run code
1618 	 * built for XNACK-off. On GFXv9 it may perform slower.
1619 	 *
1620 	 * Therefore applications built for XNACK-off can always be
1621 	 * supported and will be our fallback if any GPU does not
1622 	 * support retry.
1623 	 */
1624 	for (i = 0; i < p->n_pdds; i++) {
1625 		struct kfd_node *dev = p->pdds[i]->dev;
1626 
1627 		/* Only consider GFXv9 and higher GPUs. Older GPUs don't
1628 		 * support the SVM APIs and don't need to be considered
1629 		 * for the XNACK mode selection.
1630 		 */
1631 		if (!KFD_IS_SOC15(dev))
1632 			continue;
1633 		/* Aldebaran can always support XNACK because it can support
1634 		 * per-process XNACK mode selection. But let the dev->noretry
1635 		 * setting still influence the default XNACK mode.
1636 		 */
1637 		if (supported && KFD_SUPPORT_XNACK_PER_PROCESS(dev)) {
1638 			if (!amdgpu_sriov_xnack_support(dev->kfd->adev)) {
1639 				pr_debug("SRIOV platform xnack not supported\n");
1640 				return false;
1641 			}
1642 			continue;
1643 		}
1644 
1645 		/* GFXv10 and later GPUs do not support shader preemption
1646 		 * during page faults. This can lead to poor QoS for queue
1647 		 * management and memory-manager-related preemptions or
1648 		 * even deadlocks.
1649 		 */
1650 		if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1) &&
1651 		    KFD_GC_VERSION(dev) < IP_VERSION(12, 1, 0))
1652 			return false;
1653 
1654 		if (dev->kfd->noretry)
1655 			return false;
1656 	}
1657 
1658 	return true;
1659 }
1660 
1661 void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
1662 				     bool enabled)
1663 {
1664 	if (!iosys_map_is_null(&qpd->cwsr_map)) {
1665 		iosys_map_wr(&qpd->cwsr_map,
1666 			     KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
1667 			     uint64_t, enabled);
1668 	}
1669 }
1670 
1671 /*
1672  * On return the kfd_process is fully operational and will be freed when the
1673  * mm is released
1674  */
1675 struct kfd_process *create_process(const struct task_struct *thread, bool primary)
1676 {
1677 	struct kfd_process *process;
1678 	struct mmu_notifier *mn;
1679 	int err = -ENOMEM;
1680 
1681 	process = kzalloc_obj(*process);
1682 	if (!process)
1683 		goto err_alloc_process;
1684 
1685 	kref_init(&process->ref);
1686 	mutex_init(&process->mutex);
1687 	process->mm = thread->mm;
1688 	process->lead_thread = thread->group_leader;
1689 	process->n_pdds = 0;
1690 	process->queues_paused = false;
1691 
1692 	INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
1693 	INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
1694 	INIT_DELAYED_WORK(&process->signal_work, kfd_signal_sigbus_delayed_fn);
1695 	process->last_restore_timestamp = get_jiffies_64();
1696 	err = kfd_event_init_process(process);
1697 	if (err)
1698 		goto err_event_init;
1699 	process->is_32bit_user_mode = in_compat_syscall();
1700 	process->debug_trap_enabled = false;
1701 	process->debugger_process = NULL;
1702 	process->exception_enable_mask = 0;
1703 	atomic_set(&process->debugged_process_count, 0);
1704 	sema_init(&process->runtime_enable_sema, 0);
1705 
1706 	err = pqm_init(&process->pqm, process);
1707 	if (err != 0)
1708 		goto err_process_pqm_init;
1709 
1710 	/* init process apertures*/
1711 	err = kfd_init_apertures(process);
1712 	if (err != 0)
1713 		goto err_init_apertures;
1714 
1715 	/* Check XNACK support after PDDs are created in kfd_init_apertures */
1716 	process->xnack_enabled = kfd_process_xnack_mode(process, false);
1717 
1718 	err = svm_range_list_init(process);
1719 	if (err)
1720 		goto err_init_svm_range_list;
1721 
1722 	/* alloc_notifier needs to find the process in the hash table */
1723 	hash_add_rcu(kfd_processes_table, &process->kfd_processes,
1724 			(uintptr_t)process->mm);
1725 
1726 	/* Avoid free_notifier to start kfd_process_wq_release if
1727 	 * mmu_notifier_get failed because of pending signal.
1728 	 */
1729 	kref_get(&process->ref);
1730 
1731 	/* MMU notifier registration must be the last call that can fail
1732 	 * because after this point we cannot unwind the process creation.
1733 	 * After this point, mmu_notifier_put will trigger the cleanup by
1734 	 * dropping the last process reference in the free_notifier.
1735 	 */
1736 	if (primary) {
1737 		process->context_id = KFD_CONTEXT_ID_PRIMARY;
1738 		mn = mmu_notifier_get(&kfd_process_mmu_notifier_ops, process->mm);
1739 		if (IS_ERR(mn)) {
1740 			err = PTR_ERR(mn);
1741 			goto err_register_notifier;
1742 		}
1743 		BUG_ON(mn != &process->mmu_notifier);
1744 		ida_init(&process->id_table);
1745 	}
1746 
1747 	err = kfd_process_alloc_id(process);
1748 	if (err) {
1749 		pr_err("Creating kfd process: failed to alloc an id\n");
1750 		goto err_alloc_id;
1751 	}
1752 
1753 	kfd_unref_process(process);
1754 	get_task_struct(process->lead_thread);
1755 
1756 	INIT_WORK(&process->debug_event_workarea, debug_event_write_work_handler);
1757 
1758 	return process;
1759 
1760 err_alloc_id:
1761 	kfd_process_free_id(process);
1762 err_register_notifier:
1763 	hash_del_rcu(&process->kfd_processes);
1764 	svm_range_list_fini(process);
1765 err_init_svm_range_list:
1766 	kfd_process_free_outstanding_kfd_bos(process);
1767 	kfd_process_destroy_pdds(process);
1768 err_init_apertures:
1769 	pqm_uninit(&process->pqm);
1770 err_process_pqm_init:
1771 	kfd_event_free_process(process);
1772 err_event_init:
1773 	mutex_destroy(&process->mutex);
1774 	kfree(process);
1775 err_alloc_process:
1776 	return ERR_PTR(err);
1777 }
1778 
1779 struct kfd_process_device *kfd_get_process_device_data(struct kfd_node *dev,
1780 							struct kfd_process *p)
1781 {
1782 	int i;
1783 
1784 	for (i = 0; i < p->n_pdds; i++)
1785 		if (p->pdds[i]->dev == dev)
1786 			return p->pdds[i];
1787 
1788 	return NULL;
1789 }
1790 
1791 struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
1792 							struct kfd_process *p)
1793 {
1794 	struct kfd_process_device *pdd = NULL;
1795 
1796 	if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))
1797 		return NULL;
1798 	pdd = kzalloc_obj(*pdd);
1799 	if (!pdd)
1800 		return NULL;
1801 
1802 	pdd->dev = dev;
1803 	INIT_LIST_HEAD(&pdd->qpd.queues_list);
1804 	INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
1805 	pdd->qpd.dqm = dev->dqm;
1806 	pdd->qpd.pqm = &p->pqm;
1807 	pdd->qpd.evicted = 0;
1808 	pdd->qpd.mapped_gws_queue = false;
1809 	pdd->process = p;
1810 	pdd->bound = PDD_UNBOUND;
1811 	pdd->already_dequeued = false;
1812 	pdd->runtime_inuse = false;
1813 	atomic64_set(&pdd->vram_usage, 0);
1814 	pdd->sdma_past_activity_counter = 0;
1815 	pdd->user_gpu_id = dev->id;
1816 	atomic64_set(&pdd->evict_duration_counter, 0);
1817 
1818 	p->pdds[p->n_pdds++] = pdd;
1819 	if (kfd_dbg_is_per_vmid_supported(pdd->dev))
1820 		pdd->spi_dbg_override = pdd->dev->kfd2kgd->disable_debug_trap(
1821 							pdd->dev->adev,
1822 							false,
1823 							0);
1824 
1825 	/* Init idr used for memory handle translation */
1826 	idr_init(&pdd->alloc_idr);
1827 
1828 	atomic_inc(&dev->kfd->kfd_processes_count);
1829 
1830 	return pdd;
1831 }
1832 
1833 /**
1834  * kfd_process_device_init_vm - Initialize a VM for a process-device
1835  *
1836  * @pdd: The process-device
1837  * @drm_file: Optional pointer to a DRM file descriptor
1838  *
1839  * If @drm_file is specified, it will be used to acquire the VM from
1840  * that file descriptor. If successful, the @pdd takes ownership of
1841  * the file descriptor.
1842  *
1843  * If @drm_file is NULL, a new VM is created.
1844  *
1845  * Returns 0 on success, -errno on failure.
1846  */
1847 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
1848 			       struct file *drm_file)
1849 {
1850 	struct amdgpu_fpriv *drv_priv;
1851 	struct amdgpu_vm *avm;
1852 	struct kfd_process *p;
1853 	struct dma_fence *ef;
1854 	struct kfd_node *dev;
1855 	int ret;
1856 
1857 	if (pdd->drm_priv)
1858 		return -EBUSY;
1859 
1860 	ret = amdgpu_file_to_fpriv(drm_file, &drv_priv);
1861 	if (ret)
1862 		return ret;
1863 	avm = &drv_priv->vm;
1864 
1865 	p = pdd->process;
1866 	dev = pdd->dev;
1867 
1868 	ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(dev->adev, avm,
1869 						     &p->kgd_process_info,
1870 						     p->ef ? NULL : &ef);
1871 	if (ret) {
1872 		dev_err(dev->adev->dev, "Failed to create process VM object\n");
1873 		return ret;
1874 	}
1875 
1876 	if (!p->ef)
1877 		RCU_INIT_POINTER(p->ef, ef);
1878 
1879 	pdd->drm_priv = drm_file->private_data;
1880 
1881 	ret = kfd_process_device_reserve_ib_mem(pdd);
1882 	if (ret)
1883 		goto err_reserve_ib_mem;
1884 	ret = kfd_process_device_init_cwsr_dgpu(pdd);
1885 	if (ret)
1886 		goto err_init_cwsr;
1887 
1888 	if (unlikely(!avm->pasid)) {
1889 		dev_warn(pdd->dev->adev->dev, "WARN: vm %p has no pasid associated",
1890 				 avm);
1891 		ret = -EINVAL;
1892 		goto err_get_pasid;
1893 	}
1894 
1895 	pdd->pasid = avm->pasid;
1896 	pdd->drm_file = drm_file;
1897 
1898 	kfd_smi_event_process(pdd, true);
1899 
1900 	return 0;
1901 
1902 err_get_pasid:
1903 	kfd_process_device_destroy_cwsr_dgpu(pdd);
1904 err_init_cwsr:
1905 	kfd_process_device_destroy_ib_mem(pdd);
1906 err_reserve_ib_mem:
1907 	pdd->drm_priv = NULL;
1908 	amdgpu_amdkfd_gpuvm_destroy_cb(dev->adev, avm);
1909 
1910 	return ret;
1911 }
1912 
1913 /*
1914  * Direct the IOMMU to bind the process (specifically the pasid->mm)
1915  * to the device.
1916  * Unbinding occurs when the process dies or the device is removed.
1917  *
1918  * Assumes that the process lock is held.
1919  */
1920 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
1921 							struct kfd_process *p)
1922 {
1923 	struct kfd_process_device *pdd;
1924 	int err;
1925 
1926 	pdd = kfd_get_process_device_data(dev, p);
1927 	if (!pdd) {
1928 		dev_err(dev->adev->dev, "Process device data doesn't exist\n");
1929 		return ERR_PTR(-ENOMEM);
1930 	}
1931 
1932 	if (!pdd->drm_priv)
1933 		return ERR_PTR(-ENODEV);
1934 
1935 	/*
1936 	 * signal runtime-pm system to auto resume and prevent
1937 	 * further runtime suspend once device pdd is created until
1938 	 * pdd is destroyed.
1939 	 */
1940 	if (!pdd->runtime_inuse) {
1941 		err = pm_runtime_get_sync(adev_to_drm(dev->adev)->dev);
1942 		if (err < 0) {
1943 			pm_runtime_put_autosuspend(adev_to_drm(dev->adev)->dev);
1944 			return ERR_PTR(err);
1945 		}
1946 	}
1947 
1948 	/*
1949 	 * make sure that runtime_usage counter is incremented just once
1950 	 * per pdd
1951 	 */
1952 	pdd->runtime_inuse = true;
1953 
1954 	return pdd;
1955 }
1956 
1957 /* Create specific handle mapped to mem from process local memory idr
1958  * Assumes that the process lock is held.
1959  */
1960 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
1961 					void *mem)
1962 {
1963 	return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
1964 }
1965 
1966 /* Translate specific handle from process local memory idr
1967  * Assumes that the process lock is held.
1968  */
1969 void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
1970 					int handle)
1971 {
1972 	if (handle < 0)
1973 		return NULL;
1974 
1975 	return idr_find(&pdd->alloc_idr, handle);
1976 }
1977 
1978 /* Remove specific handle from process local memory idr
1979  * Assumes that the process lock is held.
1980  */
1981 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
1982 					int handle)
1983 {
1984 	if (handle >= 0)
1985 		idr_remove(&pdd->alloc_idr, handle);
1986 }
1987 
1988 static struct kfd_process_device *kfd_lookup_process_device_by_pasid(u32 pasid)
1989 {
1990 	struct kfd_process_device *ret_p = NULL;
1991 	struct kfd_process *p;
1992 	unsigned int temp;
1993 	int i;
1994 
1995 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1996 		for (i = 0; i < p->n_pdds; i++) {
1997 			if (p->pdds[i]->pasid == pasid) {
1998 				ret_p = p->pdds[i];
1999 				break;
2000 			}
2001 		}
2002 		if (ret_p)
2003 			break;
2004 	}
2005 	return ret_p;
2006 }
2007 
2008 /* This increments the process->ref counter. */
2009 struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid,
2010 						struct kfd_process_device **pdd)
2011 {
2012 	struct kfd_process_device *ret_p;
2013 
2014 	int idx = srcu_read_lock(&kfd_processes_srcu);
2015 
2016 	ret_p = kfd_lookup_process_device_by_pasid(pasid);
2017 	if (ret_p) {
2018 		if (pdd)
2019 			*pdd = ret_p;
2020 		kref_get(&ret_p->process->ref);
2021 
2022 		srcu_read_unlock(&kfd_processes_srcu, idx);
2023 		return ret_p->process;
2024 	}
2025 
2026 	srcu_read_unlock(&kfd_processes_srcu, idx);
2027 
2028 	if (pdd)
2029 		*pdd = NULL;
2030 
2031 	return NULL;
2032 }
2033 
2034 /* This increments the process->ref counter. */
2035 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
2036 {
2037 	struct kfd_process *p;
2038 
2039 	int idx = srcu_read_lock(&kfd_processes_srcu);
2040 
2041 	p = find_process_by_mm(mm);
2042 	if (p)
2043 		kref_get(&p->ref);
2044 
2045 	srcu_read_unlock(&kfd_processes_srcu, idx);
2046 
2047 	return p;
2048 }
2049 
2050 /* This increments the process->ref counter. */
2051 struct kfd_process *kfd_lookup_process_by_id(const struct mm_struct *mm, u16 id)
2052 {
2053 	struct kfd_process *p, *ret_p = NULL;
2054 	unsigned int temp;
2055 
2056 	int idx = srcu_read_lock(&kfd_processes_srcu);
2057 
2058 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
2059 		if (p->mm == mm && p->context_id == id) {
2060 			kref_get(&p->ref);
2061 			ret_p = p;
2062 			break;
2063 		}
2064 	}
2065 
2066 	srcu_read_unlock(&kfd_processes_srcu, idx);
2067 
2068 	return ret_p;
2069 }
2070 
2071 /* kfd_process_evict_queues - Evict all user queues of a process
2072  *
2073  * Eviction is reference-counted per process-device. This means multiple
2074  * evictions from different sources can be nested safely.
2075  */
2076 int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger)
2077 {
2078 	int r = 0;
2079 	int i;
2080 	unsigned int n_evicted = 0;
2081 
2082 	for (i = 0; i < p->n_pdds; i++) {
2083 		struct kfd_process_device *pdd = p->pdds[i];
2084 		struct device *dev = pdd->dev->adev->dev;
2085 
2086 		kfd_smi_event_queue_eviction(pdd->dev, p->lead_thread,
2087 					     trigger);
2088 
2089 		r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
2090 							    &pdd->qpd);
2091 		/* evict return -EIO if HWS is hang or asic is resetting, in this case
2092 		 * we would like to set all the queues to be in evicted state to prevent
2093 		 * them been add back since they actually not be saved right now.
2094 		 */
2095 		if (r && r != -EIO) {
2096 			dev_err(dev, "Failed to evict process queues\n");
2097 			goto fail;
2098 		}
2099 		n_evicted++;
2100 
2101 		pdd->dev->dqm->is_hws_hang = false;
2102 	}
2103 
2104 	return r;
2105 
2106 fail:
2107 	/* To keep state consistent, roll back partial eviction by
2108 	 * restoring queues
2109 	 */
2110 	for (i = 0; i < p->n_pdds; i++) {
2111 		struct kfd_process_device *pdd = p->pdds[i];
2112 
2113 		if (n_evicted == 0)
2114 			break;
2115 
2116 		kfd_smi_event_queue_restore(pdd->dev, p->lead_thread);
2117 
2118 		if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
2119 							      &pdd->qpd))
2120 			dev_err(pdd->dev->adev->dev,
2121 				"Failed to restore queues\n");
2122 
2123 		n_evicted--;
2124 	}
2125 
2126 	return r;
2127 }
2128 
2129 /* kfd_process_restore_queues - Restore all user queues of a process */
2130 int kfd_process_restore_queues(struct kfd_process *p)
2131 {
2132 	int r, ret = 0;
2133 	int i;
2134 
2135 	for (i = 0; i < p->n_pdds; i++) {
2136 		struct kfd_process_device *pdd = p->pdds[i];
2137 		struct device *dev = pdd->dev->adev->dev;
2138 
2139 		kfd_smi_event_queue_restore(pdd->dev, p->lead_thread);
2140 
2141 		r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
2142 							      &pdd->qpd);
2143 		if (r) {
2144 			dev_err(dev, "Failed to restore process queues\n");
2145 			if (!ret)
2146 				ret = r;
2147 		}
2148 	}
2149 
2150 	return ret;
2151 }
2152 
2153 int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id)
2154 {
2155 	int i;
2156 
2157 	for (i = 0; i < p->n_pdds; i++)
2158 		if (p->pdds[i] && gpu_id == p->pdds[i]->user_gpu_id)
2159 			return i;
2160 	return -EINVAL;
2161 }
2162 
2163 int
2164 kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node,
2165 			    uint32_t *gpuid, uint32_t *gpuidx)
2166 {
2167 	int i;
2168 
2169 	for (i = 0; i < p->n_pdds; i++)
2170 		if (p->pdds[i] && p->pdds[i]->dev == node) {
2171 			*gpuid = p->pdds[i]->user_gpu_id;
2172 			*gpuidx = i;
2173 			return 0;
2174 		}
2175 	return -EINVAL;
2176 }
2177 
2178 static bool signal_eviction_fence(struct kfd_process *p)
2179 {
2180 	struct dma_fence *ef;
2181 	bool ret;
2182 
2183 	rcu_read_lock();
2184 	ef = dma_fence_get_rcu_safe(&p->ef);
2185 	rcu_read_unlock();
2186 	if (!ef)
2187 		return true;
2188 
2189 	ret = dma_fence_check_and_signal(ef);
2190 	dma_fence_put(ef);
2191 
2192 	return ret;
2193 }
2194 
2195 static void evict_process_worker(struct work_struct *work)
2196 {
2197 	int ret;
2198 	struct kfd_process *p;
2199 	struct delayed_work *dwork;
2200 
2201 	dwork = to_delayed_work(work);
2202 
2203 	/* Process termination destroys this worker thread. So during the
2204 	 * lifetime of this thread, kfd_process p will be valid
2205 	 */
2206 	p = container_of(dwork, struct kfd_process, eviction_work);
2207 
2208 	pr_debug("Started evicting process pid %d\n", p->lead_thread->pid);
2209 	ret = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_TTM);
2210 	if (!ret) {
2211 		/* If another thread already signaled the eviction fence,
2212 		 * they are responsible stopping the queues and scheduling
2213 		 * the restore work.
2214 		 */
2215 		if (signal_eviction_fence(p) ||
2216 		    mod_delayed_work(kfd_restore_wq, &p->restore_work,
2217 				     msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
2218 			kfd_process_restore_queues(p);
2219 
2220 		pr_debug("Finished evicting process pid %d\n", p->lead_thread->pid);
2221 	} else
2222 		pr_err("Failed to evict queues of process pid %d\n", p->lead_thread->pid);
2223 }
2224 
2225 static int restore_process_helper(struct kfd_process *p)
2226 {
2227 	int ret = 0;
2228 
2229 	/* VMs may not have been acquired yet during debugging. */
2230 	if (p->kgd_process_info) {
2231 		ret = amdgpu_amdkfd_gpuvm_restore_process_bos(
2232 			p->kgd_process_info, &p->ef);
2233 		if (ret)
2234 			return ret;
2235 	}
2236 
2237 	ret = kfd_process_restore_queues(p);
2238 	if (!ret)
2239 		pr_debug("Finished restoring process pid %d\n",
2240 			p->lead_thread->pid);
2241 	else
2242 		pr_err("Failed to restore queues of process pid %d\n",
2243 		      p->lead_thread->pid);
2244 
2245 	return ret;
2246 }
2247 
2248 static void restore_process_worker(struct work_struct *work)
2249 {
2250 	struct delayed_work *dwork;
2251 	struct kfd_process *p;
2252 	int ret = 0;
2253 
2254 	dwork = to_delayed_work(work);
2255 
2256 	/* Process termination destroys this worker thread. So during the
2257 	 * lifetime of this thread, kfd_process p will be valid
2258 	 */
2259 	p = container_of(dwork, struct kfd_process, restore_work);
2260 	pr_debug("Started restoring process pasid %d\n", (int)p->lead_thread->pid);
2261 
2262 	/* Setting last_restore_timestamp before successful restoration.
2263 	 * Otherwise this would have to be set by KGD (restore_process_bos)
2264 	 * before KFD BOs are unreserved. If not, the process can be evicted
2265 	 * again before the timestamp is set.
2266 	 * If restore fails, the timestamp will be set again in the next
2267 	 * attempt. This would mean that the minimum GPU quanta would be
2268 	 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
2269 	 * functions)
2270 	 */
2271 
2272 	p->last_restore_timestamp = get_jiffies_64();
2273 
2274 	ret = restore_process_helper(p);
2275 	if (ret) {
2276 		pr_debug("Failed to restore BOs of process pid %d, retry after %d ms\n",
2277 			 p->lead_thread->pid, PROCESS_BACK_OFF_TIME_MS);
2278 		if (mod_delayed_work(kfd_restore_wq, &p->restore_work,
2279 				     msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
2280 			kfd_process_restore_queues(p);
2281 	}
2282 }
2283 
2284 void kfd_suspend_all_processes(void)
2285 {
2286 	struct kfd_process *p;
2287 	unsigned int temp;
2288 	int idx = srcu_read_lock(&kfd_processes_srcu);
2289 
2290 	WARN(debug_evictions, "Evicting all processes");
2291 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
2292 		if (kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_SUSPEND))
2293 			pr_err("Failed to suspend process pid %d\n", p->lead_thread->pid);
2294 		signal_eviction_fence(p);
2295 	}
2296 	srcu_read_unlock(&kfd_processes_srcu, idx);
2297 }
2298 
2299 int kfd_resume_all_processes(void)
2300 {
2301 	struct kfd_process *p;
2302 	unsigned int temp;
2303 	int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
2304 
2305 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
2306 		if (restore_process_helper(p)) {
2307 			pr_err("Restore process pid %d failed during resume\n",
2308 			      p->lead_thread->pid);
2309 			ret = -EFAULT;
2310 		}
2311 	}
2312 	srcu_read_unlock(&kfd_processes_srcu, idx);
2313 	return ret;
2314 }
2315 
2316 /* assumes caller holds process lock. */
2317 int kfd_process_drain_interrupts(struct kfd_process_device *pdd)
2318 {
2319 	uint32_t irq_drain_fence[8];
2320 	uint8_t node_id = 0;
2321 	int r = 0;
2322 
2323 	if (!KFD_IS_SOC15(pdd->dev))
2324 		return 0;
2325 
2326 	pdd->process->irq_drain_is_open = true;
2327 
2328 	memset(irq_drain_fence, 0, sizeof(irq_drain_fence));
2329 	irq_drain_fence[0] = (KFD_IRQ_FENCE_SOURCEID << 8) |
2330 							KFD_IRQ_FENCE_CLIENTID;
2331 	irq_drain_fence[3] = pdd->pasid;
2332 
2333 	/*
2334 	 * For GFX 9.4.3/9.5.0, send the NodeId also in IH cookie DW[3]
2335 	 */
2336 	if (KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 3) ||
2337 	    KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 4) ||
2338 	    KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 5, 0) ||
2339 	    KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(12, 1, 0)) {
2340 		node_id = ffs(pdd->dev->interrupt_bitmap) - 1;
2341 		irq_drain_fence[3] |= node_id << 16;
2342 	}
2343 
2344 	/* ensure stale irqs scheduled KFD interrupts and send drain fence. */
2345 	if (amdgpu_amdkfd_send_close_event_drain_irq(pdd->dev->adev,
2346 						     irq_drain_fence)) {
2347 		pdd->process->irq_drain_is_open = false;
2348 		return 0;
2349 	}
2350 
2351 	r = wait_event_interruptible(pdd->process->wait_irq_drain,
2352 				     !READ_ONCE(pdd->process->irq_drain_is_open));
2353 	if (r)
2354 		pdd->process->irq_drain_is_open = false;
2355 
2356 	return r;
2357 }
2358 
2359 void kfd_process_close_interrupt_drain(unsigned int pasid)
2360 {
2361 	struct kfd_process *p;
2362 
2363 	p = kfd_lookup_process_by_pasid(pasid, NULL);
2364 
2365 	if (!p)
2366 		return;
2367 
2368 	WRITE_ONCE(p->irq_drain_is_open, false);
2369 	wake_up_all(&p->wait_irq_drain);
2370 	kfd_unref_process(p);
2371 }
2372 
2373 struct send_exception_work_handler_workarea {
2374 	struct work_struct work;
2375 	struct kfd_process *p;
2376 	unsigned int queue_id;
2377 	uint64_t error_reason;
2378 };
2379 
2380 static void send_exception_work_handler(struct work_struct *work)
2381 {
2382 	struct send_exception_work_handler_workarea *workarea;
2383 	struct kfd_process *p;
2384 	struct queue *q;
2385 	struct mm_struct *mm;
2386 	struct kfd_context_save_area_header __user *csa_header;
2387 	uint64_t __user *err_payload_ptr;
2388 	uint64_t cur_err;
2389 	uint32_t ev_id;
2390 
2391 	workarea = container_of(work,
2392 				struct send_exception_work_handler_workarea,
2393 				work);
2394 	p = workarea->p;
2395 
2396 	mm = get_task_mm(p->lead_thread);
2397 
2398 	if (!mm)
2399 		return;
2400 
2401 	kthread_use_mm(mm);
2402 
2403 	q = pqm_get_user_queue(&p->pqm, workarea->queue_id);
2404 
2405 	if (!q)
2406 		goto out;
2407 
2408 	csa_header = (void __user *)q->properties.ctx_save_restore_area_address;
2409 
2410 	get_user(err_payload_ptr, (uint64_t __user **)&csa_header->err_payload_addr);
2411 	get_user(cur_err, err_payload_ptr);
2412 	cur_err |= workarea->error_reason;
2413 	put_user(cur_err, err_payload_ptr);
2414 	get_user(ev_id, &csa_header->err_event_id);
2415 
2416 	kfd_set_event(p, ev_id);
2417 
2418 out:
2419 	kthread_unuse_mm(mm);
2420 	mmput(mm);
2421 }
2422 
2423 int kfd_send_exception_to_runtime(struct kfd_process *p,
2424 			unsigned int queue_id,
2425 			uint64_t error_reason)
2426 {
2427 	struct send_exception_work_handler_workarea worker;
2428 
2429 	INIT_WORK_ONSTACK(&worker.work, send_exception_work_handler);
2430 
2431 	worker.p = p;
2432 	worker.queue_id = queue_id;
2433 	worker.error_reason = error_reason;
2434 
2435 	schedule_work(&worker.work);
2436 	flush_work(&worker.work);
2437 	destroy_work_on_stack(&worker.work);
2438 
2439 	return 0;
2440 }
2441 
2442 struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *p, uint32_t gpu_id)
2443 {
2444 	int i;
2445 
2446 	if (gpu_id) {
2447 		for (i = 0; i < p->n_pdds; i++) {
2448 			struct kfd_process_device *pdd = p->pdds[i];
2449 
2450 			if (pdd->user_gpu_id == gpu_id)
2451 				return pdd;
2452 		}
2453 	}
2454 	return NULL;
2455 }
2456 
2457 int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id)
2458 {
2459 	int i;
2460 
2461 	if (!actual_gpu_id)
2462 		return 0;
2463 
2464 	for (i = 0; i < p->n_pdds; i++) {
2465 		struct kfd_process_device *pdd = p->pdds[i];
2466 
2467 		if (pdd->dev->id == actual_gpu_id)
2468 			return pdd->user_gpu_id;
2469 	}
2470 	return -EINVAL;
2471 }
2472 
2473 #if defined(CONFIG_DEBUG_FS)
2474 
2475 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
2476 {
2477 	struct kfd_process *p;
2478 	unsigned int temp;
2479 	int r = 0;
2480 
2481 	int idx = srcu_read_lock(&kfd_processes_srcu);
2482 
2483 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
2484 		seq_printf(m, "Process %d PASID %d:\n",
2485 			   p->lead_thread->tgid, p->lead_thread->pid);
2486 
2487 		mutex_lock(&p->mutex);
2488 		r = pqm_debugfs_mqds(m, &p->pqm);
2489 		mutex_unlock(&p->mutex);
2490 
2491 		if (r)
2492 			break;
2493 	}
2494 
2495 	srcu_read_unlock(&kfd_processes_srcu, idx);
2496 
2497 	return r;
2498 }
2499 
2500 #endif
2501