kfd_process.c 16.3 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/slab.h>
29
#include <linux/amd-iommu.h>
30
#include <linux/notifier.h>
31
#include <linux/compat.h>
F
Felix Kuehling 已提交
32
#include <linux/mman.h>
33

34 35 36
struct mm_struct;

#include "kfd_priv.h"
37
#include "kfd_dbgmgr.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51

/*
 * List of struct kfd_process (field kfd_process).
 * Unique/indexed by mm_struct*
 */
#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
static DEFINE_MUTEX(kfd_processes_mutex);

DEFINE_STATIC_SRCU(kfd_processes_srcu);

static struct workqueue_struct *kfd_process_wq;

static struct kfd_process *find_process(const struct task_struct *thread);
52
static void kfd_process_ref_release(struct kref *ref);
53 54
static struct kfd_process *create_process(const struct task_struct *thread,
					struct file *filep);
F
Felix Kuehling 已提交
55 56
static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);

57 58 59 60

void kfd_process_create_wq(void)
{
	if (!kfd_process_wq)
61
		kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
62 63 64 65 66 67 68 69 70 71
}

void kfd_process_destroy_wq(void)
{
	if (kfd_process_wq) {
		destroy_workqueue(kfd_process_wq);
		kfd_process_wq = NULL;
	}
}

F
Felix Kuehling 已提交
72
struct kfd_process *kfd_create_process(struct file *filep)
73 74
{
	struct kfd_process *process;
F
Felix Kuehling 已提交
75
	struct task_struct *thread = current;
76

77
	if (!thread->mm)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		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);
	if (process)
94
		pr_debug("Process already found\n");
95 96
	else
		process = create_process(thread, filep);
97 98 99 100 101 102 103 104 105 106

	mutex_unlock(&kfd_processes_mutex);

	return process;
}

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

107
	if (!thread->mm)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		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);

	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;
}

143 144 145 146 147
void kfd_unref_process(struct kfd_process *p)
{
	kref_put(&p->ref, kfd_process_ref_release);
}

148
static void kfd_process_destroy_pdds(struct kfd_process *p)
149 150 151 152
{
	struct kfd_process_device *pdd, *temp;

	list_for_each_entry_safe(pdd, temp, &p->per_device_data,
153 154
				 per_device_list) {
		pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
155 156
				pdd->dev->id, p->pasid);

157
		list_del(&pdd->per_device_list);
F
Felix Kuehling 已提交
158 159 160 161 162

		if (pdd->qpd.cwsr_kaddr)
			free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
				get_order(KFD_CWSR_TBA_TMA_SIZE));

163 164
		kfree(pdd);
	}
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

/* 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);
	struct kfd_process_device *pdd;

	pr_debug("Releasing process (pasid %d) in workqueue\n", p->pasid);

	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
		if (pdd->bound == PDD_BOUND)
			amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
	}

	kfd_process_destroy_pdds(p);
186

187 188
	kfd_event_free_process(p);

189
	kfd_pasid_free(p->pasid);
190
	kfd_free_process_doorbells(p);
191 192 193

	mutex_destroy(&p->mutex);

194 195
	put_task_struct(p->lead_thread);

196 197 198
	kfree(p);
}

199
static void kfd_process_ref_release(struct kref *ref)
200
{
201
	struct kfd_process *p = container_of(ref, struct kfd_process, ref);
202

203 204 205
	INIT_WORK(&p->release_work, kfd_process_wq_release);
	queue_work(kfd_process_wq, &p->release_work);
}
206

207 208 209
static void kfd_process_destroy_delayed(struct rcu_head *rcu)
{
	struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
210

211
	kfd_unref_process(p);
212 213 214 215 216 217
}

static void kfd_process_notifier_release(struct mmu_notifier *mn,
					struct mm_struct *mm)
{
	struct kfd_process *p;
218
	struct kfd_process_device *pdd = NULL;
219 220 221 222 223 224

	/*
	 * 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);
225 226
	if (WARN_ON(p->mm != mm))
		return;
227 228 229 230 231 232

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

233 234
	mutex_lock(&p->mutex);

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	/* 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());
	}

252
	kfd_process_dequeue_from_all_devices(p);
253 254
	pqm_uninit(&p->pqm);

255 256 257
	/* Indicate to other users that MM is no longer valid */
	p->mm = NULL;

258 259
	mutex_unlock(&p->mutex);

260
	mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
261 262 263 264 265 266 267
	mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
}

static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
	.release = kfd_process_notifier_release,
};

F
Felix Kuehling 已提交
268 269 270
static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep)
{
	unsigned long  offset;
271
	struct kfd_process_device *pdd = NULL;
F
Felix Kuehling 已提交
272 273 274
	struct kfd_dev *dev = NULL;
	struct qcm_process_device *qpd = NULL;

275
	list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
F
Felix Kuehling 已提交
276 277 278 279 280 281 282 283 284 285
		dev = pdd->dev;
		qpd = &pdd->qpd;
		if (!dev->cwsr_enabled || qpd->cwsr_kaddr)
			continue;
		offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
		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)) {
286 287 288
			int err = qpd->tba_addr;

			pr_err("Failure to set tba address. error %d.\n", err);
F
Felix Kuehling 已提交
289 290
			qpd->tba_addr = 0;
			qpd->cwsr_kaddr = NULL;
291
			return err;
F
Felix Kuehling 已提交
292 293 294 295 296 297 298 299
		}

		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);
	}
300 301

	return 0;
F
Felix Kuehling 已提交
302 303
}

304 305
static struct kfd_process *create_process(const struct task_struct *thread,
					struct file *filep)
306 307 308 309 310 311 312 313 314 315 316 317 318
{
	struct kfd_process *process;
	int err = -ENOMEM;

	process = kzalloc(sizeof(*process), GFP_KERNEL);

	if (!process)
		goto err_alloc_process;

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

319 320 321
	if (kfd_alloc_process_doorbells(process) < 0)
		goto err_alloc_doorbells;

322 323
	kref_init(&process->ref);

324 325 326 327 328 329
	mutex_init(&process->mutex);

	process->mm = thread->mm;

	/* register notifier */
	process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
330
	err = mmu_notifier_register(&process->mmu_notifier, process->mm);
331 332 333 334 335 336 337
	if (err)
		goto err_mmu_notifier;

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

	process->lead_thread = thread->group_leader;
338
	get_task_struct(process->lead_thread);
339 340 341

	INIT_LIST_HEAD(&process->per_device_data);

342 343
	kfd_event_init_process(process);

344 345 346 347
	err = pqm_init(&process->pqm, process);
	if (err != 0)
		goto err_process_pqm_init;

348
	/* init process apertures*/
349
	process->is_32bit_user_mode = in_compat_syscall();
350 351
	err = kfd_init_apertures(process);
	if (err != 0)
352
		goto err_init_apertures;
353

354 355 356 357
	err = kfd_process_init_cwsr(process, filep);
	if (err)
		goto err_init_cwsr;

358 359
	return process;

360 361
err_init_cwsr:
	kfd_process_destroy_pdds(process);
362
err_init_apertures:
363
	pqm_uninit(&process->pqm);
364 365 366 367
err_process_pqm_init:
	hash_del_rcu(&process->kfd_processes);
	synchronize_rcu();
	mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
368
err_mmu_notifier:
369
	mutex_destroy(&process->mutex);
370 371
	kfd_free_process_doorbells(process);
err_alloc_doorbells:
372 373 374 375 376 377 378 379
	kfd_pasid_free(process->pasid);
err_alloc_pasid:
	kfree(process);
err_alloc_process:
	return ERR_PTR(err);
}

struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
380
							struct kfd_process *p)
381 382 383 384 385
{
	struct kfd_process_device *pdd = NULL;

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

388
	return NULL;
389 390 391 392 393 394 395 396
}

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);
397 398 399 400 401 402 403 404 405 406 407 408
	if (!pdd)
		return NULL;

	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;
	pdd->process = p;
	pdd->bound = PDD_UNBOUND;
	pdd->already_dequeued = false;
	list_add(&pdd->per_device_list, &p->per_device_data);
409 410 411 412 413 414 415 416 417 418 419 420 421 422

	return pdd;
}

/*
 * 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)
{
423
	struct kfd_process_device *pdd;
424
	int err;
425

426 427 428
	pdd = kfd_get_process_device_data(dev, p);
	if (!pdd) {
		pr_err("Process device data doesn't exist\n");
429
		return ERR_PTR(-ENOMEM);
430
	}
431

432
	if (pdd->bound == PDD_BOUND) {
433
		return pdd;
434 435 436 437
	} else if (unlikely(pdd->bound == PDD_BOUND_SUSPENDED)) {
		pr_err("Binding PDD_BOUND_SUSPENDED pdd is unexpected!\n");
		return ERR_PTR(-EINVAL);
	}
438

439 440 441 442
	err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
	if (err < 0)
		return ERR_PTR(err);

443
	pdd->bound = PDD_BOUND;
444 445 446 447

	return pdd;
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/*
 * Bind processes do the device that have been temporarily unbound
 * (PDD_BOUND_SUSPENDED) in kfd_unbind_processes_from_device.
 */
int kfd_bind_processes_to_device(struct kfd_dev *dev)
{
	struct kfd_process_device *pdd;
	struct kfd_process *p;
	unsigned int temp;
	int err = 0;

	int idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
		mutex_lock(&p->mutex);
		pdd = kfd_get_process_device_data(dev, p);
464 465

		if (WARN_ON(!pdd) || pdd->bound != PDD_BOUND_SUSPENDED) {
466 467 468 469 470 471 472
			mutex_unlock(&p->mutex);
			continue;
		}

		err = amd_iommu_bind_pasid(dev->pdev, p->pasid,
				p->lead_thread);
		if (err < 0) {
F
Felix Kuehling 已提交
473
			pr_err("Unexpected pasid %d binding failure\n",
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
					p->pasid);
			mutex_unlock(&p->mutex);
			break;
		}

		pdd->bound = PDD_BOUND;
		mutex_unlock(&p->mutex);
	}

	srcu_read_unlock(&kfd_processes_srcu, idx);

	return err;
}

/*
489 490 491
 * Mark currently bound processes as PDD_BOUND_SUSPENDED. These
 * processes will be restored to PDD_BOUND state in
 * kfd_bind_processes_to_device.
492 493 494 495 496
 */
void kfd_unbind_processes_from_device(struct kfd_dev *dev)
{
	struct kfd_process_device *pdd;
	struct kfd_process *p;
497
	unsigned int temp;
498 499 500 501 502 503

	int idx = srcu_read_lock(&kfd_processes_srcu);

	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
		mutex_lock(&p->mutex);
		pdd = kfd_get_process_device_data(dev, p);
504

505 506 507 508 509
		if (WARN_ON(!pdd)) {
			mutex_unlock(&p->mutex);
			continue;
		}

510 511 512 513 514 515 516 517 518
		if (pdd->bound == PDD_BOUND)
			pdd->bound = PDD_BOUND_SUSPENDED;
		mutex_unlock(&p->mutex);
	}

	srcu_read_unlock(&kfd_processes_srcu, idx);
}

void kfd_process_iommu_unbind_callback(struct kfd_dev *dev, unsigned int pasid)
519 520 521 522
{
	struct kfd_process *p;
	struct kfd_process_device *pdd;

523 524 525 526 527
	/*
	 * Look for the process that matches the pasid. If there is no such
	 * process, we either released it in amdkfd's own notifier, or there
	 * is a bug. Unfortunately, there is no way to tell...
	 */
528 529 530
	p = kfd_lookup_process_by_pasid(pasid);
	if (!p)
		return;
531

532
	pr_debug("Unbinding process %d from IOMMU\n", pasid);
533

534 535 536 537 538 539 540 541 542 543
	mutex_lock(kfd_get_dbgmgr_mutex());

	if (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());
544

545 546
	mutex_lock(&p->mutex);

547
	pdd = kfd_get_process_device_data(dev, p);
548 549 550 551 552
	if (pdd)
		/* For GPU relying on IOMMU, we need to dequeue here
		 * when PASID is still bound.
		 */
		kfd_process_dequeue_from_device(pdd);
553

554
	mutex_unlock(&p->mutex);
555 556

	kfd_unref_process(p);
557 558
}

559 560
struct kfd_process_device *kfd_get_first_process_device_data(
						struct kfd_process *p)
561 562 563 564 565 566
{
	return list_first_entry(&p->per_device_data,
				struct kfd_process_device,
				per_device_list);
}

567 568
struct kfd_process_device *kfd_get_next_process_device_data(
						struct kfd_process *p,
569 570 571 572 573 574 575 576 577 578 579
						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));
}
580

581
/* This increments the process->ref counter. */
582 583
struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
{
584
	struct kfd_process *p, *ret_p = NULL;
585 586 587 588 589 590
	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) {
591
			kref_get(&p->ref);
592
			ret_p = p;
593 594 595 596 597 598
			break;
		}
	}

	srcu_read_unlock(&kfd_processes_srcu, idx);

599
	return ret_p;
600
}
F
Felix Kuehling 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

int kfd_reserved_mem_mmap(struct kfd_process *process,
			  struct vm_area_struct *vma)
{
	struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
	struct kfd_process_device *pdd;
	struct qcm_process_device *qpd;

	if (!dev)
		return -EINVAL;
	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);
}
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

#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) {
		seq_printf(m, "Process %d PASID %d:\n",
			   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