kfd_process.c 40.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2014 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/mutex.h>
#include <linux/log2.h>
#include <linux/sched.h>
26
#include <linux/sched/mm.h>
27
#include <linux/sched/task.h>
28
#include <linux/mmu_context.h>
29
#include <linux/slab.h>
30
#include <linux/amd-iommu.h>
31
#include <linux/notifier.h>
32
#include <linux/compat.h>
F
Felix Kuehling 已提交
33
#include <linux/mman.h>
34
#include <linux/file.h>
35
#include <linux/pm_runtime.h>
A
Amber Lin 已提交
36
#include "amdgpu_amdkfd.h"
37
#include "amdgpu.h"
38

39 40 41
struct mm_struct;

#include "kfd_priv.h"
42
#include "kfd_device_queue_manager.h"
43
#include "kfd_dbgmgr.h"
44
#include "kfd_iommu.h"
45 46 47 48 49

/*
 * List of struct kfd_process (field kfd_process).
 * Unique/indexed by mm_struct*
 */
50
DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
51 52
static DEFINE_MUTEX(kfd_processes_mutex);

53
DEFINE_SRCU(kfd_processes_srcu);
54

55
/* For process termination handling */
56 57
static struct workqueue_struct *kfd_process_wq;

58 59 60 61 62 63 64 65
/* Ordered, single-threaded workqueue for restoring evicted
 * processes. Restoring multiple processes concurrently under memory
 * pressure can lead to processes blocking each other from validating
 * their BOs and result in a live-lock situation where processes
 * remain evicted indefinitely.
 */
static struct workqueue_struct *kfd_restore_wq;

66
static struct kfd_process *find_process(const struct task_struct *thread);
67
static void kfd_process_ref_release(struct kref *ref);
68 69
static struct kfd_process *create_process(const struct task_struct *thread);
static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep);
F
Felix Kuehling 已提交
70

71 72 73
static void evict_process_worker(struct work_struct *work);
static void restore_process_worker(struct work_struct *work);

74 75 76 77 78 79
struct kfd_procfs_tree {
	struct kobject *kobj;
};

static struct kfd_procfs_tree procfs;

80 81 82 83 84 85 86 87 88
/*
 * Structure for SDMA activity tracking
 */
struct kfd_sdma_activity_handler_workarea {
	struct work_struct sdma_activity_work;
	struct kfd_process_device *pdd;
	uint64_t sdma_activity_counter;
};

89 90 91 92 93 94 95
struct temp_sdma_queue_list {
	uint64_t rptr;
	uint64_t sdma_val;
	unsigned int queue_id;
	struct list_head list;
};

96 97 98 99 100 101 102 103 104 105
static void kfd_sdma_activity_worker(struct work_struct *work)
{
	struct kfd_sdma_activity_handler_workarea *workarea;
	struct kfd_process_device *pdd;
	uint64_t val;
	struct mm_struct *mm;
	struct queue *q;
	struct qcm_process_device *qpd;
	struct device_queue_manager *dqm;
	int ret = 0;
106 107
	struct temp_sdma_queue_list sdma_q_list;
	struct temp_sdma_queue_list *sdma_q, *next;
108 109 110 111 112 113 114

	workarea = container_of(work, struct kfd_sdma_activity_handler_workarea,
				sdma_activity_work);
	if (!workarea)
		return;

	pdd = workarea->pdd;
115 116
	if (!pdd)
		return;
117 118
	dqm = pdd->dev->dqm;
	qpd = &pdd->qpd;
119
	if (!dqm || !qpd)
120
		return;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	/*
	 * Total SDMA activity is current SDMA activity + past SDMA activity
	 * Past SDMA count is stored in pdd.
	 * To get the current activity counters for all active SDMA queues,
	 * we loop over all SDMA queues and get their counts from user-space.
	 *
	 * We cannot call get_user() with dqm_lock held as it can cause
	 * a circular lock dependency situation. To read the SDMA stats,
	 * we need to do the following:
	 *
	 * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list,
	 *    with dqm_lock/dqm_unlock().
	 * 2. Call get_user() for each node in temporary list without dqm_lock.
	 *    Save the SDMA count for each node and also add the count to the total
	 *    SDMA count counter.
	 *    Its possible, during this step, a few SDMA queue nodes got deleted
	 *    from the qpd->queues_list.
	 * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted.
	 *    If any node got deleted, its SDMA count would be captured in the sdma
	 *    past activity counter. So subtract the SDMA counter stored in step 2
	 *    for this node from the total SDMA count.
	 */
	INIT_LIST_HEAD(&sdma_q_list.list);
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	/*
	 * Create the temp list of all SDMA queues
	 */
	dqm_lock(dqm);

	list_for_each_entry(q, &qpd->queues_list, list) {
		if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
		    (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
			continue;

		sdma_q = kzalloc(sizeof(struct temp_sdma_queue_list), GFP_KERNEL);
		if (!sdma_q) {
			dqm_unlock(dqm);
			goto cleanup;
		}

		INIT_LIST_HEAD(&sdma_q->list);
		sdma_q->rptr = (uint64_t)q->properties.read_ptr;
		sdma_q->queue_id = q->properties.queue_id;
		list_add_tail(&sdma_q->list, &sdma_q_list.list);
165 166
	}

167 168 169 170 171 172 173 174 175 176
	/*
	 * If the temp list is empty, then no SDMA queues nodes were found in
	 * qpd->queues_list. Return the past activity count as the total sdma
	 * count
	 */
	if (list_empty(&sdma_q_list.list)) {
		workarea->sdma_activity_counter = pdd->sdma_past_activity_counter;
		dqm_unlock(dqm);
		return;
	}
177

178
	dqm_unlock(dqm);
179 180

	/*
181
	 * Get the usage count for each SDMA queue in temp_list.
182
	 */
183 184 185 186
	mm = get_task_mm(pdd->process->lead_thread);
	if (!mm)
		goto cleanup;

187
	kthread_use_mm(mm);
188 189 190 191 192 193 194 195 196 197 198 199 200

	list_for_each_entry(sdma_q, &sdma_q_list.list, list) {
		val = 0;
		ret = read_sdma_queue_counter(sdma_q->rptr, &val);
		if (ret) {
			pr_debug("Failed to read SDMA queue active counter for queue id: %d",
				 sdma_q->queue_id);
		} else {
			sdma_q->sdma_val = val;
			workarea->sdma_activity_counter += val;
		}
	}

201
	kthread_unuse_mm(mm);
202
	mmput(mm);
203 204

	/*
205 206
	 * Do a second iteration over qpd_queues_list to check if any SDMA
	 * nodes got deleted while fetching SDMA counter.
207
	 */
208 209 210 211
	dqm_lock(dqm);

	workarea->sdma_activity_counter += pdd->sdma_past_activity_counter;

212
	list_for_each_entry(q, &qpd->queues_list, list) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226
		if (list_empty(&sdma_q_list.list))
			break;

		if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
		    (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
			continue;

		list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
			if (((uint64_t)q->properties.read_ptr == sdma_q->rptr) &&
			     (sdma_q->queue_id == q->properties.queue_id)) {
				list_del(&sdma_q->list);
				kfree(sdma_q);
				break;
			}
227 228 229 230
		}
	}

	dqm_unlock(dqm);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

	/*
	 * If temp list is not empty, it implies some queues got deleted
	 * from qpd->queues_list during SDMA usage read. Subtract the SDMA
	 * count for each node from the total SDMA count.
	 */
	list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
		workarea->sdma_activity_counter -= sdma_q->sdma_val;
		list_del(&sdma_q->list);
		kfree(sdma_q);
	}

	return;

cleanup:
	list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
		list_del(&sdma_q->list);
		kfree(sdma_q);
	}
250 251
}

252 253 254 255 256 257
static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr,
			       char *buffer)
{
	if (strcmp(attr->name, "pasid") == 0) {
		struct kfd_process *p = container_of(attr, struct kfd_process,
						     attr_pasid);
258 259 260 261 262

		return snprintf(buffer, PAGE_SIZE, "%d\n", p->pasid);
	} else if (strncmp(attr->name, "vram_", 5) == 0) {
		struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
							      attr_vram);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		return snprintf(buffer, PAGE_SIZE, "%llu\n", READ_ONCE(pdd->vram_usage));
	} else if (strncmp(attr->name, "sdma_", 5) == 0) {
		struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
							      attr_sdma);
		struct kfd_sdma_activity_handler_workarea sdma_activity_work_handler;

		INIT_WORK(&sdma_activity_work_handler.sdma_activity_work,
					kfd_sdma_activity_worker);

		sdma_activity_work_handler.pdd = pdd;

		schedule_work(&sdma_activity_work_handler.sdma_activity_work);

		flush_work(&sdma_activity_work_handler.sdma_activity_work);

		return snprintf(buffer, PAGE_SIZE, "%llu\n",
				(sdma_activity_work_handler.sdma_activity_counter)/
				 SDMA_ACTIVITY_DIVISOR);
281 282 283 284 285
	} else {
		pr_err("Invalid attribute");
		return -EINVAL;
	}

286
	return 0;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
}

static void kfd_procfs_kobj_release(struct kobject *kobj)
{
	kfree(kobj);
}

static const struct sysfs_ops kfd_procfs_ops = {
	.show = kfd_procfs_show,
};

static struct kobj_type procfs_type = {
	.release = kfd_procfs_kobj_release,
	.sysfs_ops = &kfd_procfs_ops,
};

void kfd_procfs_init(void)
{
	int ret = 0;

	procfs.kobj = kfd_alloc_struct(procfs.kobj);
	if (!procfs.kobj)
		return;

	ret = kobject_init_and_add(procfs.kobj, &procfs_type,
				   &kfd_device->kobj, "proc");
	if (ret) {
		pr_warn("Could not create procfs proc folder");
		/* If we fail to create the procfs, clean up */
		kfd_procfs_shutdown();
	}
}

void kfd_procfs_shutdown(void)
{
	if (procfs.kobj) {
		kobject_del(procfs.kobj);
		kobject_put(procfs.kobj);
		procfs.kobj = NULL;
	}
}
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
static ssize_t kfd_procfs_queue_show(struct kobject *kobj,
				     struct attribute *attr, char *buffer)
{
	struct queue *q = container_of(kobj, struct queue, kobj);

	if (!strcmp(attr->name, "size"))
		return snprintf(buffer, PAGE_SIZE, "%llu",
				q->properties.queue_size);
	else if (!strcmp(attr->name, "type"))
		return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type);
	else if (!strcmp(attr->name, "gpuid"))
		return snprintf(buffer, PAGE_SIZE, "%u", q->device->id);
	else
		pr_err("Invalid attribute");

	return 0;
}

static struct attribute attr_queue_size = {
	.name = "size",
	.mode = KFD_SYSFS_FILE_MODE
};

static struct attribute attr_queue_type = {
	.name = "type",
	.mode = KFD_SYSFS_FILE_MODE
};

static struct attribute attr_queue_gpuid = {
	.name = "gpuid",
	.mode = KFD_SYSFS_FILE_MODE
};

static struct attribute *procfs_queue_attrs[] = {
	&attr_queue_size,
	&attr_queue_type,
	&attr_queue_gpuid,
	NULL
};

static const struct sysfs_ops procfs_queue_ops = {
	.show = kfd_procfs_queue_show,
};

static struct kobj_type procfs_queue_type = {
	.sysfs_ops = &procfs_queue_ops,
	.default_attrs = procfs_queue_attrs,
};

int kfd_procfs_add_queue(struct queue *q)
{
	struct kfd_process *proc;
	int ret;

	if (!q || !q->process)
		return -EINVAL;
	proc = q->process;

	/* Create proc/<pid>/queues/<queue id> folder */
	if (!proc->kobj_queues)
		return -EFAULT;
	ret = kobject_init_and_add(&q->kobj, &procfs_queue_type,
			proc->kobj_queues, "%u", q->properties.queue_id);
	if (ret < 0) {
		pr_warn("Creating proc/<pid>/queues/%u failed",
			q->properties.queue_id);
		kobject_put(&q->kobj);
		return ret;
	}

	return 0;
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static int kfd_sysfs_create_file(struct kfd_process *p, struct attribute *attr,
				 char *name)
{
	int ret = 0;

	if (!p || !attr || !name)
		return -EINVAL;

	attr->name = name;
	attr->mode = KFD_SYSFS_FILE_MODE;
	sysfs_attr_init(attr);

	ret = sysfs_create_file(p->kobj, attr);

	return ret;
}

419
static int kfd_procfs_add_sysfs_files(struct kfd_process *p)
420 421 422 423 424 425 426 427 428 429
{
	int ret = 0;
	struct kfd_process_device *pdd;

	if (!p)
		return -EINVAL;

	if (!p->kobj)
		return -EFAULT;

430 431 432 433 434
	/*
	 * Create sysfs files for each GPU:
	 * - proc/<pid>/vram_<gpuid>
	 * - proc/<pid>/sdma_<gpuid>
	 */
435
	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
436
		snprintf(pdd->vram_filename, MAX_SYSFS_FILENAME_LEN, "vram_%u",
437
			 pdd->dev->id);
438
		ret = kfd_sysfs_create_file(p, &pdd->attr_vram, pdd->vram_filename);
439 440 441
		if (ret)
			pr_warn("Creating vram usage for gpu id %d failed",
				(int)pdd->dev->id);
442 443 444 445 446 447 448

		snprintf(pdd->sdma_filename, MAX_SYSFS_FILENAME_LEN, "sdma_%u",
			 pdd->dev->id);
		ret = kfd_sysfs_create_file(p, &pdd->attr_sdma, pdd->sdma_filename);
		if (ret)
			pr_warn("Creating sdma usage for gpu id %d failed",
				(int)pdd->dev->id);
449 450 451 452 453 454
	}

	return ret;
}


455 456 457 458 459 460 461 462 463
void kfd_procfs_del_queue(struct queue *q)
{
	if (!q)
		return;

	kobject_del(&q->kobj);
	kobject_put(&q->kobj);
}

464
int kfd_process_create_wq(void)
465 466
{
	if (!kfd_process_wq)
467
		kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
468 469 470 471 472 473 474 475 476
	if (!kfd_restore_wq)
		kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0);

	if (!kfd_process_wq || !kfd_restore_wq) {
		kfd_process_destroy_wq();
		return -ENOMEM;
	}

	return 0;
477 478 479 480 481 482 483 484
}

void kfd_process_destroy_wq(void)
{
	if (kfd_process_wq) {
		destroy_workqueue(kfd_process_wq);
		kfd_process_wq = NULL;
	}
485 486 487 488
	if (kfd_restore_wq) {
		destroy_workqueue(kfd_restore_wq);
		kfd_restore_wq = NULL;
	}
489 490
}

491 492 493 494 495
static void kfd_process_free_gpuvm(struct kgd_mem *mem,
			struct kfd_process_device *pdd)
{
	struct kfd_dev *dev = pdd->dev;

A
Amber Lin 已提交
496
	amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->kgd, mem, pdd->vm);
497
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem, NULL);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
}

/* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
 *	This function should be only called right after the process
 *	is created and when kfd_processes_mutex is still being held
 *	to avoid concurrency. Because of that exclusiveness, we do
 *	not need to take p->mutex.
 */
static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
				   uint64_t gpu_va, uint32_t size,
				   uint32_t flags, void **kptr)
{
	struct kfd_dev *kdev = pdd->dev;
	struct kgd_mem *mem = NULL;
	int handle;
	int err;

A
Amber Lin 已提交
515
	err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->kgd, gpu_va, size,
516 517 518 519
						 pdd->vm, &mem, NULL, flags);
	if (err)
		goto err_alloc_mem;

A
Amber Lin 已提交
520
	err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->kgd, mem, pdd->vm);
521 522 523
	if (err)
		goto err_map_mem;

A
Amber Lin 已提交
524
	err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->kgd, mem, true);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	if (err) {
		pr_debug("Sync memory failed, wait interrupted by user signal\n");
		goto sync_memory_failed;
	}

	/* Create an obj handle so kfd_process_device_remove_obj_handle
	 * will take care of the bo removal when the process finishes.
	 * We do not need to take p->mutex, because the process is just
	 * created and the ioctls have not had the chance to run.
	 */
	handle = kfd_process_device_create_obj_handle(pdd, mem);

	if (handle < 0) {
		err = handle;
		goto free_gpuvm;
	}

	if (kptr) {
A
Amber Lin 已提交
543
		err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kdev->kgd,
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
				(struct kgd_mem *)mem, kptr, NULL);
		if (err) {
			pr_debug("Map GTT BO to kernel failed\n");
			goto free_obj_handle;
		}
	}

	return err;

free_obj_handle:
	kfd_process_device_remove_obj_handle(pdd, handle);
free_gpuvm:
sync_memory_failed:
	kfd_process_free_gpuvm(mem, pdd);
	return err;

err_map_mem:
561
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem, NULL);
562 563 564 565 566
err_alloc_mem:
	*kptr = NULL;
	return err;
}

567 568 569 570 571 572 573 574 575
/* kfd_process_device_reserve_ib_mem - Reserve memory inside the
 *	process for IB usage The memory reserved is for KFD to submit
 *	IB to AMDGPU from kernel.  If the memory is reserved
 *	successfully, ib_kaddr will have the CPU/kernel
 *	address. Check ib_kaddr before accessing the memory.
 */
static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
{
	struct qcm_process_device *qpd = &pdd->qpd;
576 577 578 579
	uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT |
			KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
			KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE |
			KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	void *kaddr;
	int ret;

	if (qpd->ib_kaddr || !qpd->ib_base)
		return 0;

	/* ib_base is only set for dGPU */
	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);
	if (ret)
		return ret;

	qpd->ib_kaddr = kaddr;

	return 0;
}

F
Felix Kuehling 已提交
597
struct kfd_process *kfd_create_process(struct file *filep)
598 599
{
	struct kfd_process *process;
F
Felix Kuehling 已提交
600
	struct task_struct *thread = current;
601
	int ret;
602

603
	if (!thread->mm)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		return ERR_PTR(-EINVAL);

	/* Only the pthreads threading model is supported. */
	if (thread->group_leader->mm != thread->mm)
		return ERR_PTR(-EINVAL);

	/*
	 * take kfd processes mutex before starting of process creation
	 * so there won't be a case where two threads of the same process
	 * create two kfd_process structures
	 */
	mutex_lock(&kfd_processes_mutex);

	/* A prior open of /dev/kfd could have already created the process. */
	process = find_process(thread);
619
	if (process) {
620
		pr_debug("Process already found\n");
621
	} else {
622 623 624 625 626 627 628 629 630
		process = create_process(thread);
		if (IS_ERR(process))
			goto out;

		ret = kfd_process_init_cwsr_apu(process, filep);
		if (ret) {
			process = ERR_PTR(ret);
			goto out;
		}
631

632 633 634 635 636 637 638 639 640 641 642 643 644
		if (!procfs.kobj)
			goto out;

		process->kobj = kfd_alloc_struct(process->kobj);
		if (!process->kobj) {
			pr_warn("Creating procfs kobject failed");
			goto out;
		}
		ret = kobject_init_and_add(process->kobj, &procfs_type,
					   procfs.kobj, "%d",
					   (int)process->lead_thread->pid);
		if (ret) {
			pr_warn("Creating procfs pid directory failed");
645
			kobject_put(process->kobj);
646 647 648 649 650 651 652 653 654 655
			goto out;
		}

		process->attr_pasid.name = "pasid";
		process->attr_pasid.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&process->attr_pasid);
		ret = sysfs_create_file(process->kobj, &process->attr_pasid);
		if (ret)
			pr_warn("Creating pasid for pid %d failed",
					(int)process->lead_thread->pid);
656 657 658 659 660

		process->kobj_queues = kobject_create_and_add("queues",
							process->kobj);
		if (!process->kobj_queues)
			pr_warn("Creating KFD proc/queues folder failed");
661

662
		ret = kfd_procfs_add_sysfs_files(process);
663
		if (ret)
664
			pr_warn("Creating sysfs usage file for pid %d failed",
665
				(int)process->lead_thread->pid);
666 667
	}
out:
668 669
	if (!IS_ERR(process))
		kref_get(&process->ref);
670 671 672 673 674 675 676 677 678
	mutex_unlock(&kfd_processes_mutex);

	return process;
}

struct kfd_process *kfd_get_process(const struct task_struct *thread)
{
	struct kfd_process *process;

679
	if (!thread->mm)
680 681 682 683 684 685 686
		return ERR_PTR(-EINVAL);

	/* Only the pthreads threading model is supported. */
	if (thread->group_leader->mm != thread->mm)
		return ERR_PTR(-EINVAL);

	process = find_process(thread);
687 688
	if (!process)
		return ERR_PTR(-EINVAL);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

	return process;
}

static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
{
	struct kfd_process *process;

	hash_for_each_possible_rcu(kfd_processes_table, process,
					kfd_processes, (uintptr_t)mm)
		if (process->mm == mm)
			return process;

	return NULL;
}

static struct kfd_process *find_process(const struct task_struct *thread)
{
	struct kfd_process *p;
	int idx;

	idx = srcu_read_lock(&kfd_processes_srcu);
	p = find_process_by_mm(thread->mm);
	srcu_read_unlock(&kfd_processes_srcu, idx);

	return p;
}

717 718 719 720 721
void kfd_unref_process(struct kfd_process *p)
{
	kref_put(&p->ref, kfd_process_ref_release);
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
{
	struct kfd_process *p = pdd->process;
	void *mem;
	int id;

	/*
	 * Remove all handles from idr and release appropriate
	 * local memory object
	 */
	idr_for_each_entry(&pdd->alloc_idr, mem, id) {
		struct kfd_process_device *peer_pdd;

		list_for_each_entry(peer_pdd, &p->per_device_data,
				    per_device_list) {
			if (!peer_pdd->vm)
				continue;
A
Amber Lin 已提交
739
			amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
740 741 742
				peer_pdd->dev->kgd, mem, peer_pdd->vm);
		}

743
		amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem, NULL);
744 745 746 747 748 749 750 751 752 753 754 755
		kfd_process_device_remove_obj_handle(pdd, id);
	}
}

static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
{
	struct kfd_process_device *pdd;

	list_for_each_entry(pdd, &p->per_device_data, per_device_list)
		kfd_process_device_free_bos(pdd);
}

756
static void kfd_process_destroy_pdds(struct kfd_process *p)
757 758 759 760
{
	struct kfd_process_device *pdd, *temp;

	list_for_each_entry_safe(pdd, temp, &p->per_device_data,
761
				 per_device_list) {
762
		pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n",
763 764
				pdd->dev->id, p->pasid);

765
		if (pdd->drm_file) {
A
Amber Lin 已提交
766 767
			amdgpu_amdkfd_gpuvm_release_process_vm(
					pdd->dev->kgd, pdd->vm);
768
			fput(pdd->drm_file);
769
		}
770
		else if (pdd->vm)
A
Amber Lin 已提交
771
			amdgpu_amdkfd_gpuvm_destroy_process_vm(
772 773
				pdd->dev->kgd, pdd->vm);

774
		list_del(&pdd->per_device_list);
F
Felix Kuehling 已提交
775

776
		if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
F
Felix Kuehling 已提交
777 778 779
			free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
				get_order(KFD_CWSR_TBA_TMA_SIZE));

780
		kfree(pdd->qpd.doorbell_bitmap);
781 782
		idr_destroy(&pdd->alloc_idr);

783 784 785 786 787 788 789 790 791 792
		/*
		 * before destroying pdd, make sure to report availability
		 * for auto suspend
		 */
		if (pdd->runtime_inuse) {
			pm_runtime_mark_last_busy(pdd->dev->ddev->dev);
			pm_runtime_put_autosuspend(pdd->dev->ddev->dev);
			pdd->runtime_inuse = false;
		}

793 794
		kfree(pdd);
	}
795 796 797 798 799 800 801 802 803 804 805
}

/* No process locking is needed in this function, because the process
 * is not findable any more. We must assume that no other thread is
 * using it any more, otherwise we couldn't safely free the process
 * structure in the end.
 */
static void kfd_process_wq_release(struct work_struct *work)
{
	struct kfd_process *p = container_of(work, struct kfd_process,
					     release_work);
806
	struct kfd_process_device *pdd;
807

808 809 810
	/* Remove the procfs files */
	if (p->kobj) {
		sysfs_remove_file(p->kobj, &p->attr_pasid);
811 812 813
		kobject_del(p->kobj_queues);
		kobject_put(p->kobj_queues);
		p->kobj_queues = NULL;
814

815
		list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
816
			sysfs_remove_file(p->kobj, &pdd->attr_vram);
817 818
			sysfs_remove_file(p->kobj, &pdd->attr_sdma);
		}
819

820 821 822 823 824
		kobject_del(p->kobj);
		kobject_put(p->kobj);
		p->kobj = NULL;
	}

825
	kfd_iommu_unbind_process(p);
826

827 828
	kfd_process_free_outstanding_kfd_bos(p);

829
	kfd_process_destroy_pdds(p);
830
	dma_fence_put(p->ef);
831

832 833
	kfd_event_free_process(p);

834
	kfd_pasid_free(p->pasid);
835
	kfd_free_process_doorbells(p);
836 837 838

	mutex_destroy(&p->mutex);

839 840
	put_task_struct(p->lead_thread);

841 842 843
	kfree(p);
}

844
static void kfd_process_ref_release(struct kref *ref)
845
{
846
	struct kfd_process *p = container_of(ref, struct kfd_process, ref);
847

848 849 850
	INIT_WORK(&p->release_work, kfd_process_wq_release);
	queue_work(kfd_process_wq, &p->release_work);
}
851

852
static void kfd_process_free_notifier(struct mmu_notifier *mn)
853
{
854
	kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier));
855 856 857 858 859 860
}

static void kfd_process_notifier_release(struct mmu_notifier *mn,
					struct mm_struct *mm)
{
	struct kfd_process *p;
861
	struct kfd_process_device *pdd = NULL;
862 863 864 865 866 867

	/*
	 * The kfd_process structure can not be free because the
	 * mmu_notifier srcu is read locked
	 */
	p = container_of(mn, struct kfd_process, mmu_notifier);
868 869
	if (WARN_ON(p->mm != mm))
		return;
870 871 872 873 874 875

	mutex_lock(&kfd_processes_mutex);
	hash_del_rcu(&p->kfd_processes);
	mutex_unlock(&kfd_processes_mutex);
	synchronize_srcu(&kfd_processes_srcu);

876 877 878
	cancel_delayed_work_sync(&p->eviction_work);
	cancel_delayed_work_sync(&p->restore_work);

879 880
	mutex_lock(&p->mutex);

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	/* Iterate over all process device data structures and if the
	 * pdd is in debug mode, we should first force unregistration,
	 * then we will be able to destroy the queues
	 */
	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
		struct kfd_dev *dev = pdd->dev;

		mutex_lock(kfd_get_dbgmgr_mutex());
		if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
			if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
				kfd_dbgmgr_destroy(dev->dbgmgr);
				dev->dbgmgr = NULL;
			}
		}
		mutex_unlock(kfd_get_dbgmgr_mutex());
	}

898
	kfd_process_dequeue_from_all_devices(p);
899 900
	pqm_uninit(&p->pqm);

901 902
	/* Indicate to other users that MM is no longer valid */
	p->mm = NULL;
903 904 905 906 907
	/* Signal the eviction fence after user mode queues are
	 * destroyed. This allows any BOs to be freed without
	 * triggering pointless evictions or waiting for fences.
	 */
	dma_fence_signal(p->ef);
908

909 910
	mutex_unlock(&p->mutex);

911
	mmu_notifier_put(&p->mmu_notifier);
912 913 914 915
}

static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
	.release = kfd_process_notifier_release,
916
	.free_notifier = kfd_process_free_notifier,
917 918
};

919
static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
F
Felix Kuehling 已提交
920 921
{
	unsigned long  offset;
922
	struct kfd_process_device *pdd;
F
Felix Kuehling 已提交
923

924
	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
925 926 927 928
		struct kfd_dev *dev = pdd->dev;
		struct qcm_process_device *qpd = &pdd->qpd;

		if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
F
Felix Kuehling 已提交
929
			continue;
930

931
		offset = KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id);
F
Felix Kuehling 已提交
932 933 934 935 936
		qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
			KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
			MAP_SHARED, offset);

		if (IS_ERR_VALUE(qpd->tba_addr)) {
937 938 939
			int err = qpd->tba_addr;

			pr_err("Failure to set tba address. error %d.\n", err);
F
Felix Kuehling 已提交
940 941
			qpd->tba_addr = 0;
			qpd->cwsr_kaddr = NULL;
942
			return err;
F
Felix Kuehling 已提交
943 944 945 946 947 948 949 950
		}

		memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);

		qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
		pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
			qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
	}
951 952

	return 0;
F
Felix Kuehling 已提交
953 954
}

955 956 957 958
static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
{
	struct kfd_dev *dev = pdd->dev;
	struct qcm_process_device *qpd = &pdd->qpd;
959 960 961
	uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT
			| KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE
			| KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
	void *kaddr;
	int ret;

	if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
		return 0;

	/* cwsr_base is only set for dGPU */
	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
	if (ret)
		return ret;

	qpd->cwsr_kaddr = kaddr;
	qpd->tba_addr = qpd->cwsr_base;

	memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);

	qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
	pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
		 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);

	return 0;
}

986 987 988 989 990
/*
 * On return the kfd_process is fully operational and will be freed when the
 * mm is released
 */
static struct kfd_process *create_process(const struct task_struct *thread)
991 992 993 994 995 996 997 998
{
	struct kfd_process *process;
	int err = -ENOMEM;

	process = kzalloc(sizeof(*process), GFP_KERNEL);
	if (!process)
		goto err_alloc_process;

999
	kref_init(&process->ref);
1000 1001 1002 1003
	mutex_init(&process->mutex);
	process->mm = thread->mm;
	process->lead_thread = thread->group_leader;
	INIT_LIST_HEAD(&process->per_device_data);
1004 1005 1006
	INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
	INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
	process->last_restore_timestamp = get_jiffies_64();
1007
	kfd_event_init_process(process);
1008 1009 1010 1011 1012 1013 1014 1015
	process->is_32bit_user_mode = in_compat_syscall();

	process->pasid = kfd_pasid_alloc();
	if (process->pasid == 0)
		goto err_alloc_pasid;

	if (kfd_alloc_process_doorbells(process) < 0)
		goto err_alloc_doorbells;
1016

1017 1018 1019 1020
	err = pqm_init(&process->pqm, process);
	if (err != 0)
		goto err_process_pqm_init;

1021
	/* init process apertures*/
1022 1023
	err = kfd_init_apertures(process);
	if (err != 0)
1024
		goto err_init_apertures;
1025

1026 1027 1028
	/* Must be last, have to use release destruction after this */
	process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
	err = mmu_notifier_register(&process->mmu_notifier, process->mm);
1029
	if (err)
1030 1031 1032 1033 1034
		goto err_register_notifier;

	get_task_struct(process->lead_thread);
	hash_add_rcu(kfd_processes_table, &process->kfd_processes,
			(uintptr_t)process->mm);
1035

1036 1037
	return process;

1038
err_register_notifier:
1039
	kfd_process_free_outstanding_kfd_bos(process);
1040
	kfd_process_destroy_pdds(process);
1041
err_init_apertures:
1042
	pqm_uninit(&process->pqm);
1043
err_process_pqm_init:
1044 1045
	kfd_free_process_doorbells(process);
err_alloc_doorbells:
1046 1047
	kfd_pasid_free(process->pasid);
err_alloc_pasid:
1048
	mutex_destroy(&process->mutex);
1049 1050 1051 1052 1053
	kfree(process);
err_alloc_process:
	return ERR_PTR(err);
}

1054 1055 1056 1057
static int init_doorbell_bitmap(struct qcm_process_device *qpd,
			struct kfd_dev *dev)
{
	unsigned int i;
1058 1059
	int range_start = dev->shared_resources.non_cp_doorbells_start;
	int range_end = dev->shared_resources.non_cp_doorbells_end;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

	if (!KFD_IS_SOC15(dev->device_info->asic_family))
		return 0;

	qpd->doorbell_bitmap =
		kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
				     BITS_PER_BYTE), GFP_KERNEL);
	if (!qpd->doorbell_bitmap)
		return -ENOMEM;

1070
	/* Mask out doorbells reserved for SDMA, IH, and VCN on SOC15. */
1071 1072 1073 1074 1075
	pr_debug("reserved doorbell 0x%03x - 0x%03x\n", range_start, range_end);
	pr_debug("reserved doorbell 0x%03x - 0x%03x\n",
			range_start + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
			range_end + KFD_QUEUE_DOORBELL_MIRROR_OFFSET);

1076
	for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS / 2; i++) {
1077
		if (i >= range_start && i <= range_end) {
1078
			set_bit(i, qpd->doorbell_bitmap);
1079 1080
			set_bit(i + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
				qpd->doorbell_bitmap);
1081
		}
1082
	}
1083 1084 1085 1086

	return 0;
}

1087
struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
1088
							struct kfd_process *p)
1089 1090 1091 1092 1093
{
	struct kfd_process_device *pdd = NULL;

	list_for_each_entry(pdd, &p->per_device_data, per_device_list)
		if (pdd->dev == dev)
1094
			return pdd;
1095

1096
	return NULL;
1097 1098 1099 1100 1101 1102 1103 1104
}

struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
							struct kfd_process *p)
{
	struct kfd_process_device *pdd = NULL;

	pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
1105 1106 1107
	if (!pdd)
		return NULL;

1108 1109 1110 1111 1112 1113
	if (init_doorbell_bitmap(&pdd->qpd, dev)) {
		pr_err("Failed to init doorbell for process\n");
		kfree(pdd);
		return NULL;
	}

1114 1115 1116 1117 1118
	pdd->dev = dev;
	INIT_LIST_HEAD(&pdd->qpd.queues_list);
	INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
	pdd->qpd.dqm = dev->dqm;
	pdd->qpd.pqm = &p->pqm;
1119
	pdd->qpd.evicted = 0;
1120
	pdd->qpd.mapped_gws_queue = false;
1121 1122 1123
	pdd->process = p;
	pdd->bound = PDD_UNBOUND;
	pdd->already_dequeued = false;
1124
	pdd->runtime_inuse = false;
1125
	pdd->vram_usage = 0;
1126
	pdd->sdma_past_activity_counter = 0;
1127
	list_add(&pdd->per_device_list, &p->per_device_data);
1128

1129 1130 1131
	/* Init idr used for memory handle translation */
	idr_init(&pdd->alloc_idr);

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	return pdd;
}

/**
 * kfd_process_device_init_vm - Initialize a VM for a process-device
 *
 * @pdd: The process-device
 * @drm_file: Optional pointer to a DRM file descriptor
 *
 * If @drm_file is specified, it will be used to acquire the VM from
 * that file descriptor. If successful, the @pdd takes ownership of
 * the file descriptor.
 *
 * If @drm_file is NULL, a new VM is created.
 *
 * Returns 0 on success, -errno on failure.
 */
int kfd_process_device_init_vm(struct kfd_process_device *pdd,
			       struct file *drm_file)
{
	struct kfd_process *p;
	struct kfd_dev *dev;
	int ret;

	if (pdd->vm)
		return drm_file ? -EBUSY : 0;

	p = pdd->process;
	dev = pdd->dev;

	if (drm_file)
A
Amber Lin 已提交
1163
		ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(
1164
			dev->kgd, drm_file, p->pasid,
1165 1166
			&pdd->vm, &p->kgd_process_info, &p->ef);
	else
A
Amber Lin 已提交
1167 1168
		ret = amdgpu_amdkfd_gpuvm_create_process_vm(dev->kgd, p->pasid,
			&pdd->vm, &p->kgd_process_info, &p->ef);
1169
	if (ret) {
1170
		pr_err("Failed to create process VM object\n");
1171
		return ret;
1172 1173
	}

1174 1175
	amdgpu_vm_set_task_info(pdd->vm);

1176 1177 1178
	ret = kfd_process_device_reserve_ib_mem(pdd);
	if (ret)
		goto err_reserve_ib_mem;
1179 1180 1181 1182
	ret = kfd_process_device_init_cwsr_dgpu(pdd);
	if (ret)
		goto err_init_cwsr;

1183 1184 1185
	pdd->drm_file = drm_file;

	return 0;
1186 1187

err_init_cwsr:
1188
err_reserve_ib_mem:
1189 1190
	kfd_process_device_free_bos(pdd);
	if (!drm_file)
A
Amber Lin 已提交
1191
		amdgpu_amdkfd_gpuvm_destroy_process_vm(dev->kgd, pdd->vm);
1192 1193 1194
	pdd->vm = NULL;

	return ret;
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
}

/*
 * Direct the IOMMU to bind the process (specifically the pasid->mm)
 * to the device.
 * Unbinding occurs when the process dies or the device is removed.
 *
 * Assumes that the process lock is held.
 */
struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
							struct kfd_process *p)
{
1207
	struct kfd_process_device *pdd;
1208
	int err;
1209

1210 1211 1212
	pdd = kfd_get_process_device_data(dev, p);
	if (!pdd) {
		pr_err("Process device data doesn't exist\n");
1213
		return ERR_PTR(-ENOMEM);
1214
	}
1215

1216 1217 1218 1219 1220 1221 1222
	/*
	 * signal runtime-pm system to auto resume and prevent
	 * further runtime suspend once device pdd is created until
	 * pdd is destroyed.
	 */
	if (!pdd->runtime_inuse) {
		err = pm_runtime_get_sync(dev->ddev->dev);
1223 1224
		if (err < 0) {
			pm_runtime_put_autosuspend(dev->ddev->dev);
1225
			return ERR_PTR(err);
1226
		}
1227 1228
	}

1229 1230
	err = kfd_iommu_bind_process_to_device(pdd);
	if (err)
1231
		goto out;
1232

1233 1234
	err = kfd_process_device_init_vm(pdd, NULL);
	if (err)
1235 1236 1237 1238 1239 1240 1241
		goto out;

	/*
	 * make sure that runtime_usage counter is incremented just once
	 * per pdd
	 */
	pdd->runtime_inuse = true;
1242

1243
	return pdd;
1244 1245 1246 1247 1248 1249 1250 1251 1252

out:
	/* balance runpm reference count and exit with error */
	if (!pdd->runtime_inuse) {
		pm_runtime_mark_last_busy(dev->ddev->dev);
		pm_runtime_put_autosuspend(dev->ddev->dev);
	}

	return ERR_PTR(err);
1253 1254
}

1255 1256
struct kfd_process_device *kfd_get_first_process_device_data(
						struct kfd_process *p)
1257 1258 1259 1260 1261 1262
{
	return list_first_entry(&p->per_device_data,
				struct kfd_process_device,
				per_device_list);
}

1263 1264
struct kfd_process_device *kfd_get_next_process_device_data(
						struct kfd_process *p,
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
						struct kfd_process_device *pdd)
{
	if (list_is_last(&pdd->per_device_list, &p->per_device_data))
		return NULL;
	return list_next_entry(pdd, per_device_list);
}

bool kfd_has_process_device_data(struct kfd_process *p)
{
	return !(list_empty(&p->per_device_data));
}
1276

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
/* Create specific handle mapped to mem from process local memory idr
 * Assumes that the process lock is held.
 */
int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
					void *mem)
{
	return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
}

/* Translate specific handle from process local memory idr
 * Assumes that the process lock is held.
 */
void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
					int handle)
{
	if (handle < 0)
		return NULL;

	return idr_find(&pdd->alloc_idr, handle);
}

/* Remove specific handle from process local memory idr
 * Assumes that the process lock is held.
 */
void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
					int handle)
{
	if (handle >= 0)
		idr_remove(&pdd->alloc_idr, handle);
}

1308
/* This increments the process->ref counter. */
1309 1310
struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
{
1311
	struct kfd_process *p, *ret_p = NULL;
1312 1313 1314 1315 1316 1317
	unsigned int temp;

	int idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
		if (p->pasid == pasid) {
1318
			kref_get(&p->ref);
1319
			ret_p = p;
1320 1321 1322 1323 1324 1325
			break;
		}
	}

	srcu_read_unlock(&kfd_processes_srcu, idx);

1326
	return ret_p;
1327
}
F
Felix Kuehling 已提交
1328

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/* This increments the process->ref counter. */
struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
{
	struct kfd_process *p;

	int idx = srcu_read_lock(&kfd_processes_srcu);

	p = find_process_by_mm(mm);
	if (p)
		kref_get(&p->ref);

	srcu_read_unlock(&kfd_processes_srcu, idx);

	return p;
}

1345
/* kfd_process_evict_queues - Evict all user queues of a process
1346 1347 1348 1349
 *
 * Eviction is reference-counted per process-device. This means multiple
 * evictions from different sources can be nested safely.
 */
1350
int kfd_process_evict_queues(struct kfd_process *p)
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
{
	struct kfd_process_device *pdd;
	int r = 0;
	unsigned int n_evicted = 0;

	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
		r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
							    &pdd->qpd);
		if (r) {
			pr_err("Failed to evict process queues\n");
			goto fail;
		}
		n_evicted++;
	}

	return r;

fail:
	/* To keep state consistent, roll back partial eviction by
	 * restoring queues
	 */
	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
		if (n_evicted == 0)
			break;
		if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
							      &pdd->qpd))
			pr_err("Failed to restore queues\n");

		n_evicted--;
	}

	return r;
}

1385
/* kfd_process_restore_queues - Restore all user queues of a process */
1386
int kfd_process_restore_queues(struct kfd_process *p)
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
{
	struct kfd_process_device *pdd;
	int r, ret = 0;

	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
		r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
							      &pdd->qpd);
		if (r) {
			pr_err("Failed to restore process queues\n");
			if (!ret)
				ret = r;
		}
	}

	return ret;
}

static void evict_process_worker(struct work_struct *work)
{
	int ret;
	struct kfd_process *p;
	struct delayed_work *dwork;

	dwork = to_delayed_work(work);

	/* Process termination destroys this worker thread. So during the
	 * lifetime of this thread, kfd_process p will be valid
	 */
	p = container_of(dwork, struct kfd_process, eviction_work);
	WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
		  "Eviction fence mismatch\n");

	/* Narrow window of overlap between restore and evict work
	 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
	 * unreserves KFD BOs, it is possible to evicted again. But
	 * restore has few more steps of finish. So lets wait for any
	 * previous restore work to complete
	 */
	flush_delayed_work(&p->restore_work);

1427
	pr_debug("Started evicting pasid 0x%x\n", p->pasid);
1428
	ret = kfd_process_evict_queues(p);
1429 1430 1431 1432
	if (!ret) {
		dma_fence_signal(p->ef);
		dma_fence_put(p->ef);
		p->ef = NULL;
1433
		queue_delayed_work(kfd_restore_wq, &p->restore_work,
1434 1435
				msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));

1436
		pr_debug("Finished evicting pasid 0x%x\n", p->pasid);
1437
	} else
1438
		pr_err("Failed to evict queues of pasid 0x%x\n", p->pasid);
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
}

static void restore_process_worker(struct work_struct *work)
{
	struct delayed_work *dwork;
	struct kfd_process *p;
	int ret = 0;

	dwork = to_delayed_work(work);

	/* Process termination destroys this worker thread. So during the
	 * lifetime of this thread, kfd_process p will be valid
	 */
	p = container_of(dwork, struct kfd_process, restore_work);
1453
	pr_debug("Started restoring pasid 0x%x\n", p->pasid);
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

	/* Setting last_restore_timestamp before successful restoration.
	 * Otherwise this would have to be set by KGD (restore_process_bos)
	 * before KFD BOs are unreserved. If not, the process can be evicted
	 * again before the timestamp is set.
	 * If restore fails, the timestamp will be set again in the next
	 * attempt. This would mean that the minimum GPU quanta would be
	 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
	 * functions)
	 */

	p->last_restore_timestamp = get_jiffies_64();
A
Amber Lin 已提交
1466
	ret = amdgpu_amdkfd_gpuvm_restore_process_bos(p->kgd_process_info,
1467 1468
						     &p->ef);
	if (ret) {
1469
		pr_debug("Failed to restore BOs of pasid 0x%x, retry after %d ms\n",
1470
			 p->pasid, PROCESS_BACK_OFF_TIME_MS);
1471
		ret = queue_delayed_work(kfd_restore_wq, &p->restore_work,
1472 1473 1474 1475 1476
				msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
		WARN(!ret, "reschedule restore work failed\n");
		return;
	}

1477
	ret = kfd_process_restore_queues(p);
1478
	if (!ret)
1479
		pr_debug("Finished restoring pasid 0x%x\n", p->pasid);
1480
	else
1481
		pr_err("Failed to restore queues of pasid 0x%x\n", p->pasid);
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
}

void kfd_suspend_all_processes(void)
{
	struct kfd_process *p;
	unsigned int temp;
	int idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
		cancel_delayed_work_sync(&p->eviction_work);
		cancel_delayed_work_sync(&p->restore_work);

1494
		if (kfd_process_evict_queues(p))
1495
			pr_err("Failed to suspend process 0x%x\n", p->pasid);
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
		dma_fence_signal(p->ef);
		dma_fence_put(p->ef);
		p->ef = NULL;
	}
	srcu_read_unlock(&kfd_processes_srcu, idx);
}

int kfd_resume_all_processes(void)
{
	struct kfd_process *p;
	unsigned int temp;
	int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1510
		if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) {
1511 1512 1513 1514 1515 1516 1517 1518 1519
			pr_err("Restore process %d failed during resume\n",
			       p->pasid);
			ret = -EFAULT;
		}
	}
	srcu_read_unlock(&kfd_processes_srcu, idx);
	return ret;
}

1520
int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
F
Felix Kuehling 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
			  struct vm_area_struct *vma)
{
	struct kfd_process_device *pdd;
	struct qcm_process_device *qpd;

	if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
		pr_err("Incorrect CWSR mapping size.\n");
		return -EINVAL;
	}

	pdd = kfd_get_process_device_data(dev, process);
	if (!pdd)
		return -EINVAL;
	qpd = &pdd->qpd;

	qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
					get_order(KFD_CWSR_TBA_TMA_SIZE));
	if (!qpd->cwsr_kaddr) {
		pr_err("Error allocating per process CWSR buffer.\n");
		return -ENOMEM;
	}

	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
		| VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
	/* Mapping pages to user process */
	return remap_pfn_range(vma, vma->vm_start,
			       PFN_DOWN(__pa(qpd->cwsr_kaddr)),
			       KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
}
1550

1551 1552 1553 1554 1555 1556 1557 1558 1559
void kfd_flush_tlb(struct kfd_process_device *pdd)
{
	struct kfd_dev *dev = pdd->dev;

	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
		/* Nothing to flush until a VMID is assigned, which
		 * only happens when the first queue is created.
		 */
		if (pdd->qpd.vmid)
1560 1561
			amdgpu_amdkfd_flush_gpu_tlb_vmid(dev->kgd,
							pdd->qpd.vmid);
1562
	} else {
1563 1564
		amdgpu_amdkfd_flush_gpu_tlb_pasid(dev->kgd,
						pdd->process->pasid);
1565 1566 1567
	}
}

1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
#if defined(CONFIG_DEBUG_FS)

int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
{
	struct kfd_process *p;
	unsigned int temp;
	int r = 0;

	int idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1579
		seq_printf(m, "Process %d PASID 0x%x:\n",
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
			   p->lead_thread->tgid, p->pasid);

		mutex_lock(&p->mutex);
		r = pqm_debugfs_mqds(m, &p->pqm);
		mutex_unlock(&p->mutex);

		if (r)
			break;
	}

	srcu_read_unlock(&kfd_processes_srcu, idx);

	return r;
}

#endif
1596