1 /* 2 * Copyright 2014 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #include <linux/mutex.h> 24 #include <linux/log2.h> 25 #include <linux/sched.h> 26 #include <linux/sched/mm.h> 27 #include <linux/sched/task.h> 28 #include <linux/mmu_context.h> 29 #include <linux/slab.h> 30 #include <linux/amd-iommu.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 "amdgpu_amdkfd.h" 37 #include "amdgpu.h" 38 39 struct mm_struct; 40 41 #include "kfd_priv.h" 42 #include "kfd_device_queue_manager.h" 43 #include "kfd_dbgmgr.h" 44 #include "kfd_iommu.h" 45 46 /* 47 * List of struct kfd_process (field kfd_process). 48 * Unique/indexed by mm_struct* 49 */ 50 DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); 51 static DEFINE_MUTEX(kfd_processes_mutex); 52 53 DEFINE_SRCU(kfd_processes_srcu); 54 55 /* For process termination handling */ 56 static struct workqueue_struct *kfd_process_wq; 57 58 /* Ordered, single-threaded workqueue for restoring evicted 59 * processes. Restoring multiple processes concurrently under memory 60 * pressure can lead to processes blocking each other from validating 61 * their BOs and result in a live-lock situation where processes 62 * remain evicted indefinitely. 63 */ 64 static struct workqueue_struct *kfd_restore_wq; 65 66 static struct kfd_process *find_process(const struct task_struct *thread); 67 static void kfd_process_ref_release(struct kref *ref); 68 static struct kfd_process *create_process(const struct task_struct *thread); 69 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep); 70 71 static void evict_process_worker(struct work_struct *work); 72 static void restore_process_worker(struct work_struct *work); 73 74 struct kfd_procfs_tree { 75 struct kobject *kobj; 76 }; 77 78 static struct kfd_procfs_tree procfs; 79 80 /* 81 * Structure for SDMA activity tracking 82 */ 83 struct kfd_sdma_activity_handler_workarea { 84 struct work_struct sdma_activity_work; 85 struct kfd_process_device *pdd; 86 uint64_t sdma_activity_counter; 87 }; 88 89 struct temp_sdma_queue_list { 90 uint64_t __user *rptr; 91 uint64_t sdma_val; 92 unsigned int queue_id; 93 struct list_head list; 94 }; 95 96 static void kfd_sdma_activity_worker(struct work_struct *work) 97 { 98 struct kfd_sdma_activity_handler_workarea *workarea; 99 struct kfd_process_device *pdd; 100 uint64_t val; 101 struct mm_struct *mm; 102 struct queue *q; 103 struct qcm_process_device *qpd; 104 struct device_queue_manager *dqm; 105 int ret = 0; 106 struct temp_sdma_queue_list sdma_q_list; 107 struct temp_sdma_queue_list *sdma_q, *next; 108 109 workarea = container_of(work, struct kfd_sdma_activity_handler_workarea, 110 sdma_activity_work); 111 if (!workarea) 112 return; 113 114 pdd = workarea->pdd; 115 if (!pdd) 116 return; 117 dqm = pdd->dev->dqm; 118 qpd = &pdd->qpd; 119 if (!dqm || !qpd) 120 return; 121 /* 122 * Total SDMA activity is current SDMA activity + past SDMA activity 123 * Past SDMA count is stored in pdd. 124 * To get the current activity counters for all active SDMA queues, 125 * we loop over all SDMA queues and get their counts from user-space. 126 * 127 * We cannot call get_user() with dqm_lock held as it can cause 128 * a circular lock dependency situation. To read the SDMA stats, 129 * we need to do the following: 130 * 131 * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list, 132 * with dqm_lock/dqm_unlock(). 133 * 2. Call get_user() for each node in temporary list without dqm_lock. 134 * Save the SDMA count for each node and also add the count to the total 135 * SDMA count counter. 136 * Its possible, during this step, a few SDMA queue nodes got deleted 137 * from the qpd->queues_list. 138 * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted. 139 * If any node got deleted, its SDMA count would be captured in the sdma 140 * past activity counter. So subtract the SDMA counter stored in step 2 141 * for this node from the total SDMA count. 142 */ 143 INIT_LIST_HEAD(&sdma_q_list.list); 144 145 /* 146 * Create the temp list of all SDMA queues 147 */ 148 dqm_lock(dqm); 149 150 list_for_each_entry(q, &qpd->queues_list, list) { 151 if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) && 152 (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI)) 153 continue; 154 155 sdma_q = kzalloc(sizeof(struct temp_sdma_queue_list), GFP_KERNEL); 156 if (!sdma_q) { 157 dqm_unlock(dqm); 158 goto cleanup; 159 } 160 161 INIT_LIST_HEAD(&sdma_q->list); 162 sdma_q->rptr = (uint64_t __user *)q->properties.read_ptr; 163 sdma_q->queue_id = q->properties.queue_id; 164 list_add_tail(&sdma_q->list, &sdma_q_list.list); 165 } 166 167 /* 168 * If the temp list is empty, then no SDMA queues nodes were found in 169 * qpd->queues_list. Return the past activity count as the total sdma 170 * count 171 */ 172 if (list_empty(&sdma_q_list.list)) { 173 workarea->sdma_activity_counter = pdd->sdma_past_activity_counter; 174 dqm_unlock(dqm); 175 return; 176 } 177 178 dqm_unlock(dqm); 179 180 /* 181 * Get the usage count for each SDMA queue in temp_list. 182 */ 183 mm = get_task_mm(pdd->process->lead_thread); 184 if (!mm) 185 goto cleanup; 186 187 kthread_use_mm(mm); 188 189 list_for_each_entry(sdma_q, &sdma_q_list.list, list) { 190 val = 0; 191 ret = read_sdma_queue_counter(sdma_q->rptr, &val); 192 if (ret) { 193 pr_debug("Failed to read SDMA queue active counter for queue id: %d", 194 sdma_q->queue_id); 195 } else { 196 sdma_q->sdma_val = val; 197 workarea->sdma_activity_counter += val; 198 } 199 } 200 201 kthread_unuse_mm(mm); 202 mmput(mm); 203 204 /* 205 * Do a second iteration over qpd_queues_list to check if any SDMA 206 * nodes got deleted while fetching SDMA counter. 207 */ 208 dqm_lock(dqm); 209 210 workarea->sdma_activity_counter += pdd->sdma_past_activity_counter; 211 212 list_for_each_entry(q, &qpd->queues_list, list) { 213 if (list_empty(&sdma_q_list.list)) 214 break; 215 216 if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) && 217 (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI)) 218 continue; 219 220 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 221 if (((uint64_t __user *)q->properties.read_ptr == sdma_q->rptr) && 222 (sdma_q->queue_id == q->properties.queue_id)) { 223 list_del(&sdma_q->list); 224 kfree(sdma_q); 225 break; 226 } 227 } 228 } 229 230 dqm_unlock(dqm); 231 232 /* 233 * If temp list is not empty, it implies some queues got deleted 234 * from qpd->queues_list during SDMA usage read. Subtract the SDMA 235 * count for each node from the total SDMA count. 236 */ 237 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 238 workarea->sdma_activity_counter -= sdma_q->sdma_val; 239 list_del(&sdma_q->list); 240 kfree(sdma_q); 241 } 242 243 return; 244 245 cleanup: 246 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 247 list_del(&sdma_q->list); 248 kfree(sdma_q); 249 } 250 } 251 252 static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr, 253 char *buffer) 254 { 255 if (strcmp(attr->name, "pasid") == 0) { 256 struct kfd_process *p = container_of(attr, struct kfd_process, 257 attr_pasid); 258 259 return snprintf(buffer, PAGE_SIZE, "%d\n", p->pasid); 260 } else if (strncmp(attr->name, "vram_", 5) == 0) { 261 struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device, 262 attr_vram); 263 return snprintf(buffer, PAGE_SIZE, "%llu\n", READ_ONCE(pdd->vram_usage)); 264 } else if (strncmp(attr->name, "sdma_", 5) == 0) { 265 struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device, 266 attr_sdma); 267 struct kfd_sdma_activity_handler_workarea sdma_activity_work_handler; 268 269 INIT_WORK(&sdma_activity_work_handler.sdma_activity_work, 270 kfd_sdma_activity_worker); 271 272 sdma_activity_work_handler.pdd = pdd; 273 sdma_activity_work_handler.sdma_activity_counter = 0; 274 275 schedule_work(&sdma_activity_work_handler.sdma_activity_work); 276 277 flush_work(&sdma_activity_work_handler.sdma_activity_work); 278 279 return snprintf(buffer, PAGE_SIZE, "%llu\n", 280 (sdma_activity_work_handler.sdma_activity_counter)/ 281 SDMA_ACTIVITY_DIVISOR); 282 } else { 283 pr_err("Invalid attribute"); 284 return -EINVAL; 285 } 286 287 return 0; 288 } 289 290 static void kfd_procfs_kobj_release(struct kobject *kobj) 291 { 292 kfree(kobj); 293 } 294 295 static const struct sysfs_ops kfd_procfs_ops = { 296 .show = kfd_procfs_show, 297 }; 298 299 static struct kobj_type procfs_type = { 300 .release = kfd_procfs_kobj_release, 301 .sysfs_ops = &kfd_procfs_ops, 302 }; 303 304 void kfd_procfs_init(void) 305 { 306 int ret = 0; 307 308 procfs.kobj = kfd_alloc_struct(procfs.kobj); 309 if (!procfs.kobj) 310 return; 311 312 ret = kobject_init_and_add(procfs.kobj, &procfs_type, 313 &kfd_device->kobj, "proc"); 314 if (ret) { 315 pr_warn("Could not create procfs proc folder"); 316 /* If we fail to create the procfs, clean up */ 317 kfd_procfs_shutdown(); 318 } 319 } 320 321 void kfd_procfs_shutdown(void) 322 { 323 if (procfs.kobj) { 324 kobject_del(procfs.kobj); 325 kobject_put(procfs.kobj); 326 procfs.kobj = NULL; 327 } 328 } 329 330 static ssize_t kfd_procfs_queue_show(struct kobject *kobj, 331 struct attribute *attr, char *buffer) 332 { 333 struct queue *q = container_of(kobj, struct queue, kobj); 334 335 if (!strcmp(attr->name, "size")) 336 return snprintf(buffer, PAGE_SIZE, "%llu", 337 q->properties.queue_size); 338 else if (!strcmp(attr->name, "type")) 339 return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type); 340 else if (!strcmp(attr->name, "gpuid")) 341 return snprintf(buffer, PAGE_SIZE, "%u", q->device->id); 342 else 343 pr_err("Invalid attribute"); 344 345 return 0; 346 } 347 348 static struct attribute attr_queue_size = { 349 .name = "size", 350 .mode = KFD_SYSFS_FILE_MODE 351 }; 352 353 static struct attribute attr_queue_type = { 354 .name = "type", 355 .mode = KFD_SYSFS_FILE_MODE 356 }; 357 358 static struct attribute attr_queue_gpuid = { 359 .name = "gpuid", 360 .mode = KFD_SYSFS_FILE_MODE 361 }; 362 363 static struct attribute *procfs_queue_attrs[] = { 364 &attr_queue_size, 365 &attr_queue_type, 366 &attr_queue_gpuid, 367 NULL 368 }; 369 370 static const struct sysfs_ops procfs_queue_ops = { 371 .show = kfd_procfs_queue_show, 372 }; 373 374 static struct kobj_type procfs_queue_type = { 375 .sysfs_ops = &procfs_queue_ops, 376 .default_attrs = procfs_queue_attrs, 377 }; 378 379 int kfd_procfs_add_queue(struct queue *q) 380 { 381 struct kfd_process *proc; 382 int ret; 383 384 if (!q || !q->process) 385 return -EINVAL; 386 proc = q->process; 387 388 /* Create proc/<pid>/queues/<queue id> folder */ 389 if (!proc->kobj_queues) 390 return -EFAULT; 391 ret = kobject_init_and_add(&q->kobj, &procfs_queue_type, 392 proc->kobj_queues, "%u", q->properties.queue_id); 393 if (ret < 0) { 394 pr_warn("Creating proc/<pid>/queues/%u failed", 395 q->properties.queue_id); 396 kobject_put(&q->kobj); 397 return ret; 398 } 399 400 return 0; 401 } 402 403 static int kfd_sysfs_create_file(struct kfd_process *p, struct attribute *attr, 404 char *name) 405 { 406 int ret = 0; 407 408 if (!p || !attr || !name) 409 return -EINVAL; 410 411 attr->name = name; 412 attr->mode = KFD_SYSFS_FILE_MODE; 413 sysfs_attr_init(attr); 414 415 ret = sysfs_create_file(p->kobj, attr); 416 417 return ret; 418 } 419 420 static int kfd_procfs_add_sysfs_files(struct kfd_process *p) 421 { 422 int ret = 0; 423 struct kfd_process_device *pdd; 424 425 if (!p) 426 return -EINVAL; 427 428 if (!p->kobj) 429 return -EFAULT; 430 431 /* 432 * Create sysfs files for each GPU: 433 * - proc/<pid>/vram_<gpuid> 434 * - proc/<pid>/sdma_<gpuid> 435 */ 436 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 437 snprintf(pdd->vram_filename, MAX_SYSFS_FILENAME_LEN, "vram_%u", 438 pdd->dev->id); 439 ret = kfd_sysfs_create_file(p, &pdd->attr_vram, pdd->vram_filename); 440 if (ret) 441 pr_warn("Creating vram usage for gpu id %d failed", 442 (int)pdd->dev->id); 443 444 snprintf(pdd->sdma_filename, MAX_SYSFS_FILENAME_LEN, "sdma_%u", 445 pdd->dev->id); 446 ret = kfd_sysfs_create_file(p, &pdd->attr_sdma, pdd->sdma_filename); 447 if (ret) 448 pr_warn("Creating sdma usage for gpu id %d failed", 449 (int)pdd->dev->id); 450 } 451 452 return ret; 453 } 454 455 456 void kfd_procfs_del_queue(struct queue *q) 457 { 458 if (!q) 459 return; 460 461 kobject_del(&q->kobj); 462 kobject_put(&q->kobj); 463 } 464 465 int kfd_process_create_wq(void) 466 { 467 if (!kfd_process_wq) 468 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0); 469 if (!kfd_restore_wq) 470 kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0); 471 472 if (!kfd_process_wq || !kfd_restore_wq) { 473 kfd_process_destroy_wq(); 474 return -ENOMEM; 475 } 476 477 return 0; 478 } 479 480 void kfd_process_destroy_wq(void) 481 { 482 if (kfd_process_wq) { 483 destroy_workqueue(kfd_process_wq); 484 kfd_process_wq = NULL; 485 } 486 if (kfd_restore_wq) { 487 destroy_workqueue(kfd_restore_wq); 488 kfd_restore_wq = NULL; 489 } 490 } 491 492 static void kfd_process_free_gpuvm(struct kgd_mem *mem, 493 struct kfd_process_device *pdd) 494 { 495 struct kfd_dev *dev = pdd->dev; 496 497 amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->kgd, mem, pdd->vm); 498 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem, NULL); 499 } 500 501 /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process 502 * This function should be only called right after the process 503 * is created and when kfd_processes_mutex is still being held 504 * to avoid concurrency. Because of that exclusiveness, we do 505 * not need to take p->mutex. 506 */ 507 static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd, 508 uint64_t gpu_va, uint32_t size, 509 uint32_t flags, void **kptr) 510 { 511 struct kfd_dev *kdev = pdd->dev; 512 struct kgd_mem *mem = NULL; 513 int handle; 514 int err; 515 516 err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->kgd, gpu_va, size, 517 pdd->vm, &mem, NULL, flags); 518 if (err) 519 goto err_alloc_mem; 520 521 err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->kgd, mem, pdd->vm); 522 if (err) 523 goto err_map_mem; 524 525 err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->kgd, mem, true); 526 if (err) { 527 pr_debug("Sync memory failed, wait interrupted by user signal\n"); 528 goto sync_memory_failed; 529 } 530 531 /* Create an obj handle so kfd_process_device_remove_obj_handle 532 * will take care of the bo removal when the process finishes. 533 * We do not need to take p->mutex, because the process is just 534 * created and the ioctls have not had the chance to run. 535 */ 536 handle = kfd_process_device_create_obj_handle(pdd, mem); 537 538 if (handle < 0) { 539 err = handle; 540 goto free_gpuvm; 541 } 542 543 if (kptr) { 544 err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kdev->kgd, 545 (struct kgd_mem *)mem, kptr, NULL); 546 if (err) { 547 pr_debug("Map GTT BO to kernel failed\n"); 548 goto free_obj_handle; 549 } 550 } 551 552 return err; 553 554 free_obj_handle: 555 kfd_process_device_remove_obj_handle(pdd, handle); 556 free_gpuvm: 557 sync_memory_failed: 558 kfd_process_free_gpuvm(mem, pdd); 559 return err; 560 561 err_map_mem: 562 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem, NULL); 563 err_alloc_mem: 564 *kptr = NULL; 565 return err; 566 } 567 568 /* kfd_process_device_reserve_ib_mem - Reserve memory inside the 569 * process for IB usage The memory reserved is for KFD to submit 570 * IB to AMDGPU from kernel. If the memory is reserved 571 * successfully, ib_kaddr will have the CPU/kernel 572 * address. Check ib_kaddr before accessing the memory. 573 */ 574 static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd) 575 { 576 struct qcm_process_device *qpd = &pdd->qpd; 577 uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT | 578 KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE | 579 KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE | 580 KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE; 581 void *kaddr; 582 int ret; 583 584 if (qpd->ib_kaddr || !qpd->ib_base) 585 return 0; 586 587 /* ib_base is only set for dGPU */ 588 ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags, 589 &kaddr); 590 if (ret) 591 return ret; 592 593 qpd->ib_kaddr = kaddr; 594 595 return 0; 596 } 597 598 struct kfd_process *kfd_create_process(struct file *filep) 599 { 600 struct kfd_process *process; 601 struct task_struct *thread = current; 602 int ret; 603 604 if (!thread->mm) 605 return ERR_PTR(-EINVAL); 606 607 /* Only the pthreads threading model is supported. */ 608 if (thread->group_leader->mm != thread->mm) 609 return ERR_PTR(-EINVAL); 610 611 /* 612 * take kfd processes mutex before starting of process creation 613 * so there won't be a case where two threads of the same process 614 * create two kfd_process structures 615 */ 616 mutex_lock(&kfd_processes_mutex); 617 618 /* A prior open of /dev/kfd could have already created the process. */ 619 process = find_process(thread); 620 if (process) { 621 pr_debug("Process already found\n"); 622 } else { 623 process = create_process(thread); 624 if (IS_ERR(process)) 625 goto out; 626 627 ret = kfd_process_init_cwsr_apu(process, filep); 628 if (ret) { 629 process = ERR_PTR(ret); 630 goto out; 631 } 632 633 if (!procfs.kobj) 634 goto out; 635 636 process->kobj = kfd_alloc_struct(process->kobj); 637 if (!process->kobj) { 638 pr_warn("Creating procfs kobject failed"); 639 goto out; 640 } 641 ret = kobject_init_and_add(process->kobj, &procfs_type, 642 procfs.kobj, "%d", 643 (int)process->lead_thread->pid); 644 if (ret) { 645 pr_warn("Creating procfs pid directory failed"); 646 kobject_put(process->kobj); 647 goto out; 648 } 649 650 process->attr_pasid.name = "pasid"; 651 process->attr_pasid.mode = KFD_SYSFS_FILE_MODE; 652 sysfs_attr_init(&process->attr_pasid); 653 ret = sysfs_create_file(process->kobj, &process->attr_pasid); 654 if (ret) 655 pr_warn("Creating pasid for pid %d failed", 656 (int)process->lead_thread->pid); 657 658 process->kobj_queues = kobject_create_and_add("queues", 659 process->kobj); 660 if (!process->kobj_queues) 661 pr_warn("Creating KFD proc/queues folder failed"); 662 663 ret = kfd_procfs_add_sysfs_files(process); 664 if (ret) 665 pr_warn("Creating sysfs usage file for pid %d failed", 666 (int)process->lead_thread->pid); 667 } 668 out: 669 if (!IS_ERR(process)) 670 kref_get(&process->ref); 671 mutex_unlock(&kfd_processes_mutex); 672 673 return process; 674 } 675 676 struct kfd_process *kfd_get_process(const struct task_struct *thread) 677 { 678 struct kfd_process *process; 679 680 if (!thread->mm) 681 return ERR_PTR(-EINVAL); 682 683 /* Only the pthreads threading model is supported. */ 684 if (thread->group_leader->mm != thread->mm) 685 return ERR_PTR(-EINVAL); 686 687 process = find_process(thread); 688 if (!process) 689 return ERR_PTR(-EINVAL); 690 691 return process; 692 } 693 694 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) 695 { 696 struct kfd_process *process; 697 698 hash_for_each_possible_rcu(kfd_processes_table, process, 699 kfd_processes, (uintptr_t)mm) 700 if (process->mm == mm) 701 return process; 702 703 return NULL; 704 } 705 706 static struct kfd_process *find_process(const struct task_struct *thread) 707 { 708 struct kfd_process *p; 709 int idx; 710 711 idx = srcu_read_lock(&kfd_processes_srcu); 712 p = find_process_by_mm(thread->mm); 713 srcu_read_unlock(&kfd_processes_srcu, idx); 714 715 return p; 716 } 717 718 void kfd_unref_process(struct kfd_process *p) 719 { 720 kref_put(&p->ref, kfd_process_ref_release); 721 } 722 723 static void kfd_process_device_free_bos(struct kfd_process_device *pdd) 724 { 725 struct kfd_process *p = pdd->process; 726 void *mem; 727 int id; 728 729 /* 730 * Remove all handles from idr and release appropriate 731 * local memory object 732 */ 733 idr_for_each_entry(&pdd->alloc_idr, mem, id) { 734 struct kfd_process_device *peer_pdd; 735 736 list_for_each_entry(peer_pdd, &p->per_device_data, 737 per_device_list) { 738 if (!peer_pdd->vm) 739 continue; 740 amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 741 peer_pdd->dev->kgd, mem, peer_pdd->vm); 742 } 743 744 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem, NULL); 745 kfd_process_device_remove_obj_handle(pdd, id); 746 } 747 } 748 749 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p) 750 { 751 struct kfd_process_device *pdd; 752 753 list_for_each_entry(pdd, &p->per_device_data, per_device_list) 754 kfd_process_device_free_bos(pdd); 755 } 756 757 static void kfd_process_destroy_pdds(struct kfd_process *p) 758 { 759 struct kfd_process_device *pdd, *temp; 760 761 list_for_each_entry_safe(pdd, temp, &p->per_device_data, 762 per_device_list) { 763 pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n", 764 pdd->dev->id, p->pasid); 765 766 if (pdd->drm_file) { 767 amdgpu_amdkfd_gpuvm_release_process_vm( 768 pdd->dev->kgd, pdd->vm); 769 fput(pdd->drm_file); 770 } 771 else if (pdd->vm) 772 amdgpu_amdkfd_gpuvm_destroy_process_vm( 773 pdd->dev->kgd, pdd->vm); 774 775 list_del(&pdd->per_device_list); 776 777 if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base) 778 free_pages((unsigned long)pdd->qpd.cwsr_kaddr, 779 get_order(KFD_CWSR_TBA_TMA_SIZE)); 780 781 kfree(pdd->qpd.doorbell_bitmap); 782 idr_destroy(&pdd->alloc_idr); 783 784 /* 785 * before destroying pdd, make sure to report availability 786 * for auto suspend 787 */ 788 if (pdd->runtime_inuse) { 789 pm_runtime_mark_last_busy(pdd->dev->ddev->dev); 790 pm_runtime_put_autosuspend(pdd->dev->ddev->dev); 791 pdd->runtime_inuse = false; 792 } 793 794 kfree(pdd); 795 } 796 } 797 798 /* No process locking is needed in this function, because the process 799 * is not findable any more. We must assume that no other thread is 800 * using it any more, otherwise we couldn't safely free the process 801 * structure in the end. 802 */ 803 static void kfd_process_wq_release(struct work_struct *work) 804 { 805 struct kfd_process *p = container_of(work, struct kfd_process, 806 release_work); 807 struct kfd_process_device *pdd; 808 809 /* Remove the procfs files */ 810 if (p->kobj) { 811 sysfs_remove_file(p->kobj, &p->attr_pasid); 812 kobject_del(p->kobj_queues); 813 kobject_put(p->kobj_queues); 814 p->kobj_queues = NULL; 815 816 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 817 sysfs_remove_file(p->kobj, &pdd->attr_vram); 818 sysfs_remove_file(p->kobj, &pdd->attr_sdma); 819 } 820 821 kobject_del(p->kobj); 822 kobject_put(p->kobj); 823 p->kobj = NULL; 824 } 825 826 kfd_iommu_unbind_process(p); 827 828 kfd_process_free_outstanding_kfd_bos(p); 829 830 kfd_process_destroy_pdds(p); 831 dma_fence_put(p->ef); 832 833 kfd_event_free_process(p); 834 835 kfd_pasid_free(p->pasid); 836 kfd_free_process_doorbells(p); 837 838 mutex_destroy(&p->mutex); 839 840 put_task_struct(p->lead_thread); 841 842 kfree(p); 843 } 844 845 static void kfd_process_ref_release(struct kref *ref) 846 { 847 struct kfd_process *p = container_of(ref, struct kfd_process, ref); 848 849 INIT_WORK(&p->release_work, kfd_process_wq_release); 850 queue_work(kfd_process_wq, &p->release_work); 851 } 852 853 static void kfd_process_free_notifier(struct mmu_notifier *mn) 854 { 855 kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier)); 856 } 857 858 static void kfd_process_notifier_release(struct mmu_notifier *mn, 859 struct mm_struct *mm) 860 { 861 struct kfd_process *p; 862 struct kfd_process_device *pdd = NULL; 863 864 /* 865 * The kfd_process structure can not be free because the 866 * mmu_notifier srcu is read locked 867 */ 868 p = container_of(mn, struct kfd_process, mmu_notifier); 869 if (WARN_ON(p->mm != mm)) 870 return; 871 872 mutex_lock(&kfd_processes_mutex); 873 hash_del_rcu(&p->kfd_processes); 874 mutex_unlock(&kfd_processes_mutex); 875 synchronize_srcu(&kfd_processes_srcu); 876 877 cancel_delayed_work_sync(&p->eviction_work); 878 cancel_delayed_work_sync(&p->restore_work); 879 880 mutex_lock(&p->mutex); 881 882 /* Iterate over all process device data structures and if the 883 * pdd is in debug mode, we should first force unregistration, 884 * then we will be able to destroy the queues 885 */ 886 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 887 struct kfd_dev *dev = pdd->dev; 888 889 mutex_lock(kfd_get_dbgmgr_mutex()); 890 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { 891 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { 892 kfd_dbgmgr_destroy(dev->dbgmgr); 893 dev->dbgmgr = NULL; 894 } 895 } 896 mutex_unlock(kfd_get_dbgmgr_mutex()); 897 } 898 899 kfd_process_dequeue_from_all_devices(p); 900 pqm_uninit(&p->pqm); 901 902 /* Indicate to other users that MM is no longer valid */ 903 p->mm = NULL; 904 /* Signal the eviction fence after user mode queues are 905 * destroyed. This allows any BOs to be freed without 906 * triggering pointless evictions or waiting for fences. 907 */ 908 dma_fence_signal(p->ef); 909 910 mutex_unlock(&p->mutex); 911 912 mmu_notifier_put(&p->mmu_notifier); 913 } 914 915 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { 916 .release = kfd_process_notifier_release, 917 .free_notifier = kfd_process_free_notifier, 918 }; 919 920 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep) 921 { 922 unsigned long offset; 923 struct kfd_process_device *pdd; 924 925 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 926 struct kfd_dev *dev = pdd->dev; 927 struct qcm_process_device *qpd = &pdd->qpd; 928 929 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base) 930 continue; 931 932 offset = KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id); 933 qpd->tba_addr = (int64_t)vm_mmap(filep, 0, 934 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC, 935 MAP_SHARED, offset); 936 937 if (IS_ERR_VALUE(qpd->tba_addr)) { 938 int err = qpd->tba_addr; 939 940 pr_err("Failure to set tba address. error %d.\n", err); 941 qpd->tba_addr = 0; 942 qpd->cwsr_kaddr = NULL; 943 return err; 944 } 945 946 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); 947 948 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; 949 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", 950 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); 951 } 952 953 return 0; 954 } 955 956 static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd) 957 { 958 struct kfd_dev *dev = pdd->dev; 959 struct qcm_process_device *qpd = &pdd->qpd; 960 uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT 961 | KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE 962 | KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE; 963 void *kaddr; 964 int ret; 965 966 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base) 967 return 0; 968 969 /* cwsr_base is only set for dGPU */ 970 ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base, 971 KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr); 972 if (ret) 973 return ret; 974 975 qpd->cwsr_kaddr = kaddr; 976 qpd->tba_addr = qpd->cwsr_base; 977 978 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); 979 980 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; 981 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", 982 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); 983 984 return 0; 985 } 986 987 /* 988 * On return the kfd_process is fully operational and will be freed when the 989 * mm is released 990 */ 991 static struct kfd_process *create_process(const struct task_struct *thread) 992 { 993 struct kfd_process *process; 994 int err = -ENOMEM; 995 996 process = kzalloc(sizeof(*process), GFP_KERNEL); 997 if (!process) 998 goto err_alloc_process; 999 1000 kref_init(&process->ref); 1001 mutex_init(&process->mutex); 1002 process->mm = thread->mm; 1003 process->lead_thread = thread->group_leader; 1004 INIT_LIST_HEAD(&process->per_device_data); 1005 INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker); 1006 INIT_DELAYED_WORK(&process->restore_work, restore_process_worker); 1007 process->last_restore_timestamp = get_jiffies_64(); 1008 kfd_event_init_process(process); 1009 process->is_32bit_user_mode = in_compat_syscall(); 1010 1011 process->pasid = kfd_pasid_alloc(); 1012 if (process->pasid == 0) 1013 goto err_alloc_pasid; 1014 1015 if (kfd_alloc_process_doorbells(process) < 0) 1016 goto err_alloc_doorbells; 1017 1018 err = pqm_init(&process->pqm, process); 1019 if (err != 0) 1020 goto err_process_pqm_init; 1021 1022 /* init process apertures*/ 1023 err = kfd_init_apertures(process); 1024 if (err != 0) 1025 goto err_init_apertures; 1026 1027 /* Must be last, have to use release destruction after this */ 1028 process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; 1029 err = mmu_notifier_register(&process->mmu_notifier, process->mm); 1030 if (err) 1031 goto err_register_notifier; 1032 1033 get_task_struct(process->lead_thread); 1034 hash_add_rcu(kfd_processes_table, &process->kfd_processes, 1035 (uintptr_t)process->mm); 1036 1037 return process; 1038 1039 err_register_notifier: 1040 kfd_process_free_outstanding_kfd_bos(process); 1041 kfd_process_destroy_pdds(process); 1042 err_init_apertures: 1043 pqm_uninit(&process->pqm); 1044 err_process_pqm_init: 1045 kfd_free_process_doorbells(process); 1046 err_alloc_doorbells: 1047 kfd_pasid_free(process->pasid); 1048 err_alloc_pasid: 1049 mutex_destroy(&process->mutex); 1050 kfree(process); 1051 err_alloc_process: 1052 return ERR_PTR(err); 1053 } 1054 1055 static int init_doorbell_bitmap(struct qcm_process_device *qpd, 1056 struct kfd_dev *dev) 1057 { 1058 unsigned int i; 1059 int range_start = dev->shared_resources.non_cp_doorbells_start; 1060 int range_end = dev->shared_resources.non_cp_doorbells_end; 1061 1062 if (!KFD_IS_SOC15(dev->device_info->asic_family)) 1063 return 0; 1064 1065 qpd->doorbell_bitmap = 1066 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, 1067 BITS_PER_BYTE), GFP_KERNEL); 1068 if (!qpd->doorbell_bitmap) 1069 return -ENOMEM; 1070 1071 /* Mask out doorbells reserved for SDMA, IH, and VCN on SOC15. */ 1072 pr_debug("reserved doorbell 0x%03x - 0x%03x\n", range_start, range_end); 1073 pr_debug("reserved doorbell 0x%03x - 0x%03x\n", 1074 range_start + KFD_QUEUE_DOORBELL_MIRROR_OFFSET, 1075 range_end + KFD_QUEUE_DOORBELL_MIRROR_OFFSET); 1076 1077 for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS / 2; i++) { 1078 if (i >= range_start && i <= range_end) { 1079 set_bit(i, qpd->doorbell_bitmap); 1080 set_bit(i + KFD_QUEUE_DOORBELL_MIRROR_OFFSET, 1081 qpd->doorbell_bitmap); 1082 } 1083 } 1084 1085 return 0; 1086 } 1087 1088 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, 1089 struct kfd_process *p) 1090 { 1091 struct kfd_process_device *pdd = NULL; 1092 1093 list_for_each_entry(pdd, &p->per_device_data, per_device_list) 1094 if (pdd->dev == dev) 1095 return pdd; 1096 1097 return NULL; 1098 } 1099 1100 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, 1101 struct kfd_process *p) 1102 { 1103 struct kfd_process_device *pdd = NULL; 1104 1105 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); 1106 if (!pdd) 1107 return NULL; 1108 1109 if (init_doorbell_bitmap(&pdd->qpd, dev)) { 1110 pr_err("Failed to init doorbell for process\n"); 1111 kfree(pdd); 1112 return NULL; 1113 } 1114 1115 pdd->dev = dev; 1116 INIT_LIST_HEAD(&pdd->qpd.queues_list); 1117 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); 1118 pdd->qpd.dqm = dev->dqm; 1119 pdd->qpd.pqm = &p->pqm; 1120 pdd->qpd.evicted = 0; 1121 pdd->qpd.mapped_gws_queue = false; 1122 pdd->process = p; 1123 pdd->bound = PDD_UNBOUND; 1124 pdd->already_dequeued = false; 1125 pdd->runtime_inuse = false; 1126 pdd->vram_usage = 0; 1127 pdd->sdma_past_activity_counter = 0; 1128 list_add(&pdd->per_device_list, &p->per_device_data); 1129 1130 /* Init idr used for memory handle translation */ 1131 idr_init(&pdd->alloc_idr); 1132 1133 return pdd; 1134 } 1135 1136 /** 1137 * kfd_process_device_init_vm - Initialize a VM for a process-device 1138 * 1139 * @pdd: The process-device 1140 * @drm_file: Optional pointer to a DRM file descriptor 1141 * 1142 * If @drm_file is specified, it will be used to acquire the VM from 1143 * that file descriptor. If successful, the @pdd takes ownership of 1144 * the file descriptor. 1145 * 1146 * If @drm_file is NULL, a new VM is created. 1147 * 1148 * Returns 0 on success, -errno on failure. 1149 */ 1150 int kfd_process_device_init_vm(struct kfd_process_device *pdd, 1151 struct file *drm_file) 1152 { 1153 struct kfd_process *p; 1154 struct kfd_dev *dev; 1155 int ret; 1156 1157 if (pdd->vm) 1158 return drm_file ? -EBUSY : 0; 1159 1160 p = pdd->process; 1161 dev = pdd->dev; 1162 1163 if (drm_file) 1164 ret = amdgpu_amdkfd_gpuvm_acquire_process_vm( 1165 dev->kgd, drm_file, p->pasid, 1166 &pdd->vm, &p->kgd_process_info, &p->ef); 1167 else 1168 ret = amdgpu_amdkfd_gpuvm_create_process_vm(dev->kgd, p->pasid, 1169 &pdd->vm, &p->kgd_process_info, &p->ef); 1170 if (ret) { 1171 pr_err("Failed to create process VM object\n"); 1172 return ret; 1173 } 1174 1175 amdgpu_vm_set_task_info(pdd->vm); 1176 1177 ret = kfd_process_device_reserve_ib_mem(pdd); 1178 if (ret) 1179 goto err_reserve_ib_mem; 1180 ret = kfd_process_device_init_cwsr_dgpu(pdd); 1181 if (ret) 1182 goto err_init_cwsr; 1183 1184 pdd->drm_file = drm_file; 1185 1186 return 0; 1187 1188 err_init_cwsr: 1189 err_reserve_ib_mem: 1190 kfd_process_device_free_bos(pdd); 1191 if (!drm_file) 1192 amdgpu_amdkfd_gpuvm_destroy_process_vm(dev->kgd, pdd->vm); 1193 pdd->vm = NULL; 1194 1195 return ret; 1196 } 1197 1198 /* 1199 * Direct the IOMMU to bind the process (specifically the pasid->mm) 1200 * to the device. 1201 * Unbinding occurs when the process dies or the device is removed. 1202 * 1203 * Assumes that the process lock is held. 1204 */ 1205 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, 1206 struct kfd_process *p) 1207 { 1208 struct kfd_process_device *pdd; 1209 int err; 1210 1211 pdd = kfd_get_process_device_data(dev, p); 1212 if (!pdd) { 1213 pr_err("Process device data doesn't exist\n"); 1214 return ERR_PTR(-ENOMEM); 1215 } 1216 1217 /* 1218 * signal runtime-pm system to auto resume and prevent 1219 * further runtime suspend once device pdd is created until 1220 * pdd is destroyed. 1221 */ 1222 if (!pdd->runtime_inuse) { 1223 err = pm_runtime_get_sync(dev->ddev->dev); 1224 if (err < 0) { 1225 pm_runtime_put_autosuspend(dev->ddev->dev); 1226 return ERR_PTR(err); 1227 } 1228 } 1229 1230 err = kfd_iommu_bind_process_to_device(pdd); 1231 if (err) 1232 goto out; 1233 1234 err = kfd_process_device_init_vm(pdd, NULL); 1235 if (err) 1236 goto out; 1237 1238 /* 1239 * make sure that runtime_usage counter is incremented just once 1240 * per pdd 1241 */ 1242 pdd->runtime_inuse = true; 1243 1244 return pdd; 1245 1246 out: 1247 /* balance runpm reference count and exit with error */ 1248 if (!pdd->runtime_inuse) { 1249 pm_runtime_mark_last_busy(dev->ddev->dev); 1250 pm_runtime_put_autosuspend(dev->ddev->dev); 1251 } 1252 1253 return ERR_PTR(err); 1254 } 1255 1256 struct kfd_process_device *kfd_get_first_process_device_data( 1257 struct kfd_process *p) 1258 { 1259 return list_first_entry(&p->per_device_data, 1260 struct kfd_process_device, 1261 per_device_list); 1262 } 1263 1264 struct kfd_process_device *kfd_get_next_process_device_data( 1265 struct kfd_process *p, 1266 struct kfd_process_device *pdd) 1267 { 1268 if (list_is_last(&pdd->per_device_list, &p->per_device_data)) 1269 return NULL; 1270 return list_next_entry(pdd, per_device_list); 1271 } 1272 1273 bool kfd_has_process_device_data(struct kfd_process *p) 1274 { 1275 return !(list_empty(&p->per_device_data)); 1276 } 1277 1278 /* Create specific handle mapped to mem from process local memory idr 1279 * Assumes that the process lock is held. 1280 */ 1281 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, 1282 void *mem) 1283 { 1284 return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL); 1285 } 1286 1287 /* Translate specific handle from process local memory idr 1288 * Assumes that the process lock is held. 1289 */ 1290 void *kfd_process_device_translate_handle(struct kfd_process_device *pdd, 1291 int handle) 1292 { 1293 if (handle < 0) 1294 return NULL; 1295 1296 return idr_find(&pdd->alloc_idr, handle); 1297 } 1298 1299 /* Remove specific handle from process local memory idr 1300 * Assumes that the process lock is held. 1301 */ 1302 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, 1303 int handle) 1304 { 1305 if (handle >= 0) 1306 idr_remove(&pdd->alloc_idr, handle); 1307 } 1308 1309 /* This increments the process->ref counter. */ 1310 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid) 1311 { 1312 struct kfd_process *p, *ret_p = NULL; 1313 unsigned int temp; 1314 1315 int idx = srcu_read_lock(&kfd_processes_srcu); 1316 1317 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1318 if (p->pasid == pasid) { 1319 kref_get(&p->ref); 1320 ret_p = p; 1321 break; 1322 } 1323 } 1324 1325 srcu_read_unlock(&kfd_processes_srcu, idx); 1326 1327 return ret_p; 1328 } 1329 1330 /* This increments the process->ref counter. */ 1331 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm) 1332 { 1333 struct kfd_process *p; 1334 1335 int idx = srcu_read_lock(&kfd_processes_srcu); 1336 1337 p = find_process_by_mm(mm); 1338 if (p) 1339 kref_get(&p->ref); 1340 1341 srcu_read_unlock(&kfd_processes_srcu, idx); 1342 1343 return p; 1344 } 1345 1346 /* kfd_process_evict_queues - Evict all user queues of a process 1347 * 1348 * Eviction is reference-counted per process-device. This means multiple 1349 * evictions from different sources can be nested safely. 1350 */ 1351 int kfd_process_evict_queues(struct kfd_process *p) 1352 { 1353 struct kfd_process_device *pdd; 1354 int r = 0; 1355 unsigned int n_evicted = 0; 1356 1357 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 1358 r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm, 1359 &pdd->qpd); 1360 if (r) { 1361 pr_err("Failed to evict process queues\n"); 1362 goto fail; 1363 } 1364 n_evicted++; 1365 } 1366 1367 return r; 1368 1369 fail: 1370 /* To keep state consistent, roll back partial eviction by 1371 * restoring queues 1372 */ 1373 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 1374 if (n_evicted == 0) 1375 break; 1376 if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, 1377 &pdd->qpd)) 1378 pr_err("Failed to restore queues\n"); 1379 1380 n_evicted--; 1381 } 1382 1383 return r; 1384 } 1385 1386 /* kfd_process_restore_queues - Restore all user queues of a process */ 1387 int kfd_process_restore_queues(struct kfd_process *p) 1388 { 1389 struct kfd_process_device *pdd; 1390 int r, ret = 0; 1391 1392 list_for_each_entry(pdd, &p->per_device_data, per_device_list) { 1393 r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, 1394 &pdd->qpd); 1395 if (r) { 1396 pr_err("Failed to restore process queues\n"); 1397 if (!ret) 1398 ret = r; 1399 } 1400 } 1401 1402 return ret; 1403 } 1404 1405 static void evict_process_worker(struct work_struct *work) 1406 { 1407 int ret; 1408 struct kfd_process *p; 1409 struct delayed_work *dwork; 1410 1411 dwork = to_delayed_work(work); 1412 1413 /* Process termination destroys this worker thread. So during the 1414 * lifetime of this thread, kfd_process p will be valid 1415 */ 1416 p = container_of(dwork, struct kfd_process, eviction_work); 1417 WARN_ONCE(p->last_eviction_seqno != p->ef->seqno, 1418 "Eviction fence mismatch\n"); 1419 1420 /* Narrow window of overlap between restore and evict work 1421 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos 1422 * unreserves KFD BOs, it is possible to evicted again. But 1423 * restore has few more steps of finish. So lets wait for any 1424 * previous restore work to complete 1425 */ 1426 flush_delayed_work(&p->restore_work); 1427 1428 pr_debug("Started evicting pasid 0x%x\n", p->pasid); 1429 ret = kfd_process_evict_queues(p); 1430 if (!ret) { 1431 dma_fence_signal(p->ef); 1432 dma_fence_put(p->ef); 1433 p->ef = NULL; 1434 queue_delayed_work(kfd_restore_wq, &p->restore_work, 1435 msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)); 1436 1437 pr_debug("Finished evicting pasid 0x%x\n", p->pasid); 1438 } else 1439 pr_err("Failed to evict queues of pasid 0x%x\n", p->pasid); 1440 } 1441 1442 static void restore_process_worker(struct work_struct *work) 1443 { 1444 struct delayed_work *dwork; 1445 struct kfd_process *p; 1446 int ret = 0; 1447 1448 dwork = to_delayed_work(work); 1449 1450 /* Process termination destroys this worker thread. So during the 1451 * lifetime of this thread, kfd_process p will be valid 1452 */ 1453 p = container_of(dwork, struct kfd_process, restore_work); 1454 pr_debug("Started restoring pasid 0x%x\n", p->pasid); 1455 1456 /* Setting last_restore_timestamp before successful restoration. 1457 * Otherwise this would have to be set by KGD (restore_process_bos) 1458 * before KFD BOs are unreserved. If not, the process can be evicted 1459 * again before the timestamp is set. 1460 * If restore fails, the timestamp will be set again in the next 1461 * attempt. This would mean that the minimum GPU quanta would be 1462 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two 1463 * functions) 1464 */ 1465 1466 p->last_restore_timestamp = get_jiffies_64(); 1467 ret = amdgpu_amdkfd_gpuvm_restore_process_bos(p->kgd_process_info, 1468 &p->ef); 1469 if (ret) { 1470 pr_debug("Failed to restore BOs of pasid 0x%x, retry after %d ms\n", 1471 p->pasid, PROCESS_BACK_OFF_TIME_MS); 1472 ret = queue_delayed_work(kfd_restore_wq, &p->restore_work, 1473 msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS)); 1474 WARN(!ret, "reschedule restore work failed\n"); 1475 return; 1476 } 1477 1478 ret = kfd_process_restore_queues(p); 1479 if (!ret) 1480 pr_debug("Finished restoring pasid 0x%x\n", p->pasid); 1481 else 1482 pr_err("Failed to restore queues of pasid 0x%x\n", p->pasid); 1483 } 1484 1485 void kfd_suspend_all_processes(void) 1486 { 1487 struct kfd_process *p; 1488 unsigned int temp; 1489 int idx = srcu_read_lock(&kfd_processes_srcu); 1490 1491 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1492 cancel_delayed_work_sync(&p->eviction_work); 1493 cancel_delayed_work_sync(&p->restore_work); 1494 1495 if (kfd_process_evict_queues(p)) 1496 pr_err("Failed to suspend process 0x%x\n", p->pasid); 1497 dma_fence_signal(p->ef); 1498 dma_fence_put(p->ef); 1499 p->ef = NULL; 1500 } 1501 srcu_read_unlock(&kfd_processes_srcu, idx); 1502 } 1503 1504 int kfd_resume_all_processes(void) 1505 { 1506 struct kfd_process *p; 1507 unsigned int temp; 1508 int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu); 1509 1510 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1511 if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) { 1512 pr_err("Restore process %d failed during resume\n", 1513 p->pasid); 1514 ret = -EFAULT; 1515 } 1516 } 1517 srcu_read_unlock(&kfd_processes_srcu, idx); 1518 return ret; 1519 } 1520 1521 int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process, 1522 struct vm_area_struct *vma) 1523 { 1524 struct kfd_process_device *pdd; 1525 struct qcm_process_device *qpd; 1526 1527 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) { 1528 pr_err("Incorrect CWSR mapping size.\n"); 1529 return -EINVAL; 1530 } 1531 1532 pdd = kfd_get_process_device_data(dev, process); 1533 if (!pdd) 1534 return -EINVAL; 1535 qpd = &pdd->qpd; 1536 1537 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1538 get_order(KFD_CWSR_TBA_TMA_SIZE)); 1539 if (!qpd->cwsr_kaddr) { 1540 pr_err("Error allocating per process CWSR buffer.\n"); 1541 return -ENOMEM; 1542 } 1543 1544 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND 1545 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP; 1546 /* Mapping pages to user process */ 1547 return remap_pfn_range(vma, vma->vm_start, 1548 PFN_DOWN(__pa(qpd->cwsr_kaddr)), 1549 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot); 1550 } 1551 1552 void kfd_flush_tlb(struct kfd_process_device *pdd) 1553 { 1554 struct kfd_dev *dev = pdd->dev; 1555 1556 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 1557 /* Nothing to flush until a VMID is assigned, which 1558 * only happens when the first queue is created. 1559 */ 1560 if (pdd->qpd.vmid) 1561 amdgpu_amdkfd_flush_gpu_tlb_vmid(dev->kgd, 1562 pdd->qpd.vmid); 1563 } else { 1564 amdgpu_amdkfd_flush_gpu_tlb_pasid(dev->kgd, 1565 pdd->process->pasid); 1566 } 1567 } 1568 1569 #if defined(CONFIG_DEBUG_FS) 1570 1571 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data) 1572 { 1573 struct kfd_process *p; 1574 unsigned int temp; 1575 int r = 0; 1576 1577 int idx = srcu_read_lock(&kfd_processes_srcu); 1578 1579 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1580 seq_printf(m, "Process %d PASID 0x%x:\n", 1581 p->lead_thread->tgid, p->pasid); 1582 1583 mutex_lock(&p->mutex); 1584 r = pqm_debugfs_mqds(m, &p->pqm); 1585 mutex_unlock(&p->mutex); 1586 1587 if (r) 1588 break; 1589 } 1590 1591 srcu_read_unlock(&kfd_processes_srcu, idx); 1592 1593 return r; 1594 } 1595 1596 #endif 1597 1598