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 #include "kfd_svm.h" 46 47 /* 48 * List of struct kfd_process (field kfd_process). 49 * Unique/indexed by mm_struct* 50 */ 51 DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); 52 static DEFINE_MUTEX(kfd_processes_mutex); 53 54 DEFINE_SRCU(kfd_processes_srcu); 55 56 /* For process termination handling */ 57 static struct workqueue_struct *kfd_process_wq; 58 59 /* Ordered, single-threaded workqueue for restoring evicted 60 * processes. Restoring multiple processes concurrently under memory 61 * pressure can lead to processes blocking each other from validating 62 * their BOs and result in a live-lock situation where processes 63 * remain evicted indefinitely. 64 */ 65 static struct workqueue_struct *kfd_restore_wq; 66 67 static struct kfd_process *find_process(const struct task_struct *thread); 68 static void kfd_process_ref_release(struct kref *ref); 69 static struct kfd_process *create_process(const struct task_struct *thread); 70 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep); 71 72 static void evict_process_worker(struct work_struct *work); 73 static void restore_process_worker(struct work_struct *work); 74 75 struct kfd_procfs_tree { 76 struct kobject *kobj; 77 }; 78 79 static struct kfd_procfs_tree procfs; 80 81 /* 82 * Structure for SDMA activity tracking 83 */ 84 struct kfd_sdma_activity_handler_workarea { 85 struct work_struct sdma_activity_work; 86 struct kfd_process_device *pdd; 87 uint64_t sdma_activity_counter; 88 }; 89 90 struct temp_sdma_queue_list { 91 uint64_t __user *rptr; 92 uint64_t sdma_val; 93 unsigned int queue_id; 94 struct list_head list; 95 }; 96 97 static void kfd_sdma_activity_worker(struct work_struct *work) 98 { 99 struct kfd_sdma_activity_handler_workarea *workarea; 100 struct kfd_process_device *pdd; 101 uint64_t val; 102 struct mm_struct *mm; 103 struct queue *q; 104 struct qcm_process_device *qpd; 105 struct device_queue_manager *dqm; 106 int ret = 0; 107 struct temp_sdma_queue_list sdma_q_list; 108 struct temp_sdma_queue_list *sdma_q, *next; 109 110 workarea = container_of(work, struct kfd_sdma_activity_handler_workarea, 111 sdma_activity_work); 112 113 pdd = workarea->pdd; 114 if (!pdd) 115 return; 116 dqm = pdd->dev->dqm; 117 qpd = &pdd->qpd; 118 if (!dqm || !qpd) 119 return; 120 /* 121 * Total SDMA activity is current SDMA activity + past SDMA activity 122 * Past SDMA count is stored in pdd. 123 * To get the current activity counters for all active SDMA queues, 124 * we loop over all SDMA queues and get their counts from user-space. 125 * 126 * We cannot call get_user() with dqm_lock held as it can cause 127 * a circular lock dependency situation. To read the SDMA stats, 128 * we need to do the following: 129 * 130 * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list, 131 * with dqm_lock/dqm_unlock(). 132 * 2. Call get_user() for each node in temporary list without dqm_lock. 133 * Save the SDMA count for each node and also add the count to the total 134 * SDMA count counter. 135 * Its possible, during this step, a few SDMA queue nodes got deleted 136 * from the qpd->queues_list. 137 * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted. 138 * If any node got deleted, its SDMA count would be captured in the sdma 139 * past activity counter. So subtract the SDMA counter stored in step 2 140 * for this node from the total SDMA count. 141 */ 142 INIT_LIST_HEAD(&sdma_q_list.list); 143 144 /* 145 * Create the temp list of all SDMA queues 146 */ 147 dqm_lock(dqm); 148 149 list_for_each_entry(q, &qpd->queues_list, list) { 150 if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) && 151 (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI)) 152 continue; 153 154 sdma_q = kzalloc(sizeof(struct temp_sdma_queue_list), GFP_KERNEL); 155 if (!sdma_q) { 156 dqm_unlock(dqm); 157 goto cleanup; 158 } 159 160 INIT_LIST_HEAD(&sdma_q->list); 161 sdma_q->rptr = (uint64_t __user *)q->properties.read_ptr; 162 sdma_q->queue_id = q->properties.queue_id; 163 list_add_tail(&sdma_q->list, &sdma_q_list.list); 164 } 165 166 /* 167 * If the temp list is empty, then no SDMA queues nodes were found in 168 * qpd->queues_list. Return the past activity count as the total sdma 169 * count 170 */ 171 if (list_empty(&sdma_q_list.list)) { 172 workarea->sdma_activity_counter = pdd->sdma_past_activity_counter; 173 dqm_unlock(dqm); 174 return; 175 } 176 177 dqm_unlock(dqm); 178 179 /* 180 * Get the usage count for each SDMA queue in temp_list. 181 */ 182 mm = get_task_mm(pdd->process->lead_thread); 183 if (!mm) 184 goto cleanup; 185 186 kthread_use_mm(mm); 187 188 list_for_each_entry(sdma_q, &sdma_q_list.list, list) { 189 val = 0; 190 ret = read_sdma_queue_counter(sdma_q->rptr, &val); 191 if (ret) { 192 pr_debug("Failed to read SDMA queue active counter for queue id: %d", 193 sdma_q->queue_id); 194 } else { 195 sdma_q->sdma_val = val; 196 workarea->sdma_activity_counter += val; 197 } 198 } 199 200 kthread_unuse_mm(mm); 201 mmput(mm); 202 203 /* 204 * Do a second iteration over qpd_queues_list to check if any SDMA 205 * nodes got deleted while fetching SDMA counter. 206 */ 207 dqm_lock(dqm); 208 209 workarea->sdma_activity_counter += pdd->sdma_past_activity_counter; 210 211 list_for_each_entry(q, &qpd->queues_list, list) { 212 if (list_empty(&sdma_q_list.list)) 213 break; 214 215 if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) && 216 (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI)) 217 continue; 218 219 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 220 if (((uint64_t __user *)q->properties.read_ptr == sdma_q->rptr) && 221 (sdma_q->queue_id == q->properties.queue_id)) { 222 list_del(&sdma_q->list); 223 kfree(sdma_q); 224 break; 225 } 226 } 227 } 228 229 dqm_unlock(dqm); 230 231 /* 232 * If temp list is not empty, it implies some queues got deleted 233 * from qpd->queues_list during SDMA usage read. Subtract the SDMA 234 * count for each node from the total SDMA count. 235 */ 236 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 237 workarea->sdma_activity_counter -= sdma_q->sdma_val; 238 list_del(&sdma_q->list); 239 kfree(sdma_q); 240 } 241 242 return; 243 244 cleanup: 245 list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) { 246 list_del(&sdma_q->list); 247 kfree(sdma_q); 248 } 249 } 250 251 /** 252 * @kfd_get_cu_occupancy - Collect number of waves in-flight on this device 253 * by current process. Translates acquired wave count into number of compute units 254 * that are occupied. 255 * 256 * @atr: Handle of attribute that allows reporting of wave count. The attribute 257 * handle encapsulates GPU device it is associated with, thereby allowing collection 258 * of waves in flight, etc 259 * 260 * @buffer: Handle of user provided buffer updated with wave count 261 * 262 * Return: Number of bytes written to user buffer or an error value 263 */ 264 static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer) 265 { 266 int cu_cnt; 267 int wave_cnt; 268 int max_waves_per_cu; 269 struct kfd_dev *dev = NULL; 270 struct kfd_process *proc = NULL; 271 struct kfd_process_device *pdd = NULL; 272 273 pdd = container_of(attr, struct kfd_process_device, attr_cu_occupancy); 274 dev = pdd->dev; 275 if (dev->kfd2kgd->get_cu_occupancy == NULL) 276 return -EINVAL; 277 278 cu_cnt = 0; 279 proc = pdd->process; 280 if (pdd->qpd.queue_count == 0) { 281 pr_debug("Gpu-Id: %d has no active queues for process %d\n", 282 dev->id, proc->pasid); 283 return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt); 284 } 285 286 /* Collect wave count from device if it supports */ 287 wave_cnt = 0; 288 max_waves_per_cu = 0; 289 dev->kfd2kgd->get_cu_occupancy(dev->kgd, proc->pasid, &wave_cnt, 290 &max_waves_per_cu); 291 292 /* Translate wave count to number of compute units */ 293 cu_cnt = (wave_cnt + (max_waves_per_cu - 1)) / max_waves_per_cu; 294 return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt); 295 } 296 297 static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr, 298 char *buffer) 299 { 300 if (strcmp(attr->name, "pasid") == 0) { 301 struct kfd_process *p = container_of(attr, struct kfd_process, 302 attr_pasid); 303 304 return snprintf(buffer, PAGE_SIZE, "%d\n", p->pasid); 305 } else if (strncmp(attr->name, "vram_", 5) == 0) { 306 struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device, 307 attr_vram); 308 return snprintf(buffer, PAGE_SIZE, "%llu\n", READ_ONCE(pdd->vram_usage)); 309 } else if (strncmp(attr->name, "sdma_", 5) == 0) { 310 struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device, 311 attr_sdma); 312 struct kfd_sdma_activity_handler_workarea sdma_activity_work_handler; 313 314 INIT_WORK(&sdma_activity_work_handler.sdma_activity_work, 315 kfd_sdma_activity_worker); 316 317 sdma_activity_work_handler.pdd = pdd; 318 sdma_activity_work_handler.sdma_activity_counter = 0; 319 320 schedule_work(&sdma_activity_work_handler.sdma_activity_work); 321 322 flush_work(&sdma_activity_work_handler.sdma_activity_work); 323 324 return snprintf(buffer, PAGE_SIZE, "%llu\n", 325 (sdma_activity_work_handler.sdma_activity_counter)/ 326 SDMA_ACTIVITY_DIVISOR); 327 } else { 328 pr_err("Invalid attribute"); 329 return -EINVAL; 330 } 331 332 return 0; 333 } 334 335 static void kfd_procfs_kobj_release(struct kobject *kobj) 336 { 337 kfree(kobj); 338 } 339 340 static const struct sysfs_ops kfd_procfs_ops = { 341 .show = kfd_procfs_show, 342 }; 343 344 static struct kobj_type procfs_type = { 345 .release = kfd_procfs_kobj_release, 346 .sysfs_ops = &kfd_procfs_ops, 347 }; 348 349 void kfd_procfs_init(void) 350 { 351 int ret = 0; 352 353 procfs.kobj = kfd_alloc_struct(procfs.kobj); 354 if (!procfs.kobj) 355 return; 356 357 ret = kobject_init_and_add(procfs.kobj, &procfs_type, 358 &kfd_device->kobj, "proc"); 359 if (ret) { 360 pr_warn("Could not create procfs proc folder"); 361 /* If we fail to create the procfs, clean up */ 362 kfd_procfs_shutdown(); 363 } 364 } 365 366 void kfd_procfs_shutdown(void) 367 { 368 if (procfs.kobj) { 369 kobject_del(procfs.kobj); 370 kobject_put(procfs.kobj); 371 procfs.kobj = NULL; 372 } 373 } 374 375 static ssize_t kfd_procfs_queue_show(struct kobject *kobj, 376 struct attribute *attr, char *buffer) 377 { 378 struct queue *q = container_of(kobj, struct queue, kobj); 379 380 if (!strcmp(attr->name, "size")) 381 return snprintf(buffer, PAGE_SIZE, "%llu", 382 q->properties.queue_size); 383 else if (!strcmp(attr->name, "type")) 384 return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type); 385 else if (!strcmp(attr->name, "gpuid")) 386 return snprintf(buffer, PAGE_SIZE, "%u", q->device->id); 387 else 388 pr_err("Invalid attribute"); 389 390 return 0; 391 } 392 393 static ssize_t kfd_procfs_stats_show(struct kobject *kobj, 394 struct attribute *attr, char *buffer) 395 { 396 if (strcmp(attr->name, "evicted_ms") == 0) { 397 struct kfd_process_device *pdd = container_of(attr, 398 struct kfd_process_device, 399 attr_evict); 400 uint64_t evict_jiffies; 401 402 evict_jiffies = atomic64_read(&pdd->evict_duration_counter); 403 404 return snprintf(buffer, 405 PAGE_SIZE, 406 "%llu\n", 407 jiffies64_to_msecs(evict_jiffies)); 408 409 /* Sysfs handle that gets CU occupancy is per device */ 410 } else if (strcmp(attr->name, "cu_occupancy") == 0) { 411 return kfd_get_cu_occupancy(attr, buffer); 412 } else { 413 pr_err("Invalid attribute"); 414 } 415 416 return 0; 417 } 418 419 static struct attribute attr_queue_size = { 420 .name = "size", 421 .mode = KFD_SYSFS_FILE_MODE 422 }; 423 424 static struct attribute attr_queue_type = { 425 .name = "type", 426 .mode = KFD_SYSFS_FILE_MODE 427 }; 428 429 static struct attribute attr_queue_gpuid = { 430 .name = "gpuid", 431 .mode = KFD_SYSFS_FILE_MODE 432 }; 433 434 static struct attribute *procfs_queue_attrs[] = { 435 &attr_queue_size, 436 &attr_queue_type, 437 &attr_queue_gpuid, 438 NULL 439 }; 440 441 static const struct sysfs_ops procfs_queue_ops = { 442 .show = kfd_procfs_queue_show, 443 }; 444 445 static struct kobj_type procfs_queue_type = { 446 .sysfs_ops = &procfs_queue_ops, 447 .default_attrs = procfs_queue_attrs, 448 }; 449 450 static const struct sysfs_ops procfs_stats_ops = { 451 .show = kfd_procfs_stats_show, 452 }; 453 454 static struct attribute *procfs_stats_attrs[] = { 455 NULL 456 }; 457 458 static struct kobj_type procfs_stats_type = { 459 .sysfs_ops = &procfs_stats_ops, 460 .default_attrs = procfs_stats_attrs, 461 }; 462 463 int kfd_procfs_add_queue(struct queue *q) 464 { 465 struct kfd_process *proc; 466 int ret; 467 468 if (!q || !q->process) 469 return -EINVAL; 470 proc = q->process; 471 472 /* Create proc/<pid>/queues/<queue id> folder */ 473 if (!proc->kobj_queues) 474 return -EFAULT; 475 ret = kobject_init_and_add(&q->kobj, &procfs_queue_type, 476 proc->kobj_queues, "%u", q->properties.queue_id); 477 if (ret < 0) { 478 pr_warn("Creating proc/<pid>/queues/%u failed", 479 q->properties.queue_id); 480 kobject_put(&q->kobj); 481 return ret; 482 } 483 484 return 0; 485 } 486 487 static int kfd_sysfs_create_file(struct kfd_process *p, struct attribute *attr, 488 char *name) 489 { 490 int ret = 0; 491 492 if (!p || !attr || !name) 493 return -EINVAL; 494 495 attr->name = name; 496 attr->mode = KFD_SYSFS_FILE_MODE; 497 sysfs_attr_init(attr); 498 499 ret = sysfs_create_file(p->kobj, attr); 500 501 return ret; 502 } 503 504 static int kfd_procfs_add_sysfs_stats(struct kfd_process *p) 505 { 506 int ret = 0; 507 int i; 508 char stats_dir_filename[MAX_SYSFS_FILENAME_LEN]; 509 510 if (!p) 511 return -EINVAL; 512 513 if (!p->kobj) 514 return -EFAULT; 515 516 /* 517 * Create sysfs files for each GPU: 518 * - proc/<pid>/stats_<gpuid>/ 519 * - proc/<pid>/stats_<gpuid>/evicted_ms 520 * - proc/<pid>/stats_<gpuid>/cu_occupancy 521 */ 522 for (i = 0; i < p->n_pdds; i++) { 523 struct kfd_process_device *pdd = p->pdds[i]; 524 struct kobject *kobj_stats; 525 526 snprintf(stats_dir_filename, MAX_SYSFS_FILENAME_LEN, 527 "stats_%u", pdd->dev->id); 528 kobj_stats = kfd_alloc_struct(kobj_stats); 529 if (!kobj_stats) 530 return -ENOMEM; 531 532 ret = kobject_init_and_add(kobj_stats, 533 &procfs_stats_type, 534 p->kobj, 535 stats_dir_filename); 536 537 if (ret) { 538 pr_warn("Creating KFD proc/stats_%s folder failed", 539 stats_dir_filename); 540 kobject_put(kobj_stats); 541 goto err; 542 } 543 544 pdd->kobj_stats = kobj_stats; 545 pdd->attr_evict.name = "evicted_ms"; 546 pdd->attr_evict.mode = KFD_SYSFS_FILE_MODE; 547 sysfs_attr_init(&pdd->attr_evict); 548 ret = sysfs_create_file(kobj_stats, &pdd->attr_evict); 549 if (ret) 550 pr_warn("Creating eviction stats for gpuid %d failed", 551 (int)pdd->dev->id); 552 553 /* Add sysfs file to report compute unit occupancy */ 554 if (pdd->dev->kfd2kgd->get_cu_occupancy != NULL) { 555 pdd->attr_cu_occupancy.name = "cu_occupancy"; 556 pdd->attr_cu_occupancy.mode = KFD_SYSFS_FILE_MODE; 557 sysfs_attr_init(&pdd->attr_cu_occupancy); 558 ret = sysfs_create_file(kobj_stats, 559 &pdd->attr_cu_occupancy); 560 if (ret) 561 pr_warn("Creating %s failed for gpuid: %d", 562 pdd->attr_cu_occupancy.name, 563 (int)pdd->dev->id); 564 } 565 } 566 err: 567 return ret; 568 } 569 570 571 static int kfd_procfs_add_sysfs_files(struct kfd_process *p) 572 { 573 int ret = 0; 574 int i; 575 576 if (!p) 577 return -EINVAL; 578 579 if (!p->kobj) 580 return -EFAULT; 581 582 /* 583 * Create sysfs files for each GPU: 584 * - proc/<pid>/vram_<gpuid> 585 * - proc/<pid>/sdma_<gpuid> 586 */ 587 for (i = 0; i < p->n_pdds; i++) { 588 struct kfd_process_device *pdd = p->pdds[i]; 589 590 snprintf(pdd->vram_filename, MAX_SYSFS_FILENAME_LEN, "vram_%u", 591 pdd->dev->id); 592 ret = kfd_sysfs_create_file(p, &pdd->attr_vram, pdd->vram_filename); 593 if (ret) 594 pr_warn("Creating vram usage for gpu id %d failed", 595 (int)pdd->dev->id); 596 597 snprintf(pdd->sdma_filename, MAX_SYSFS_FILENAME_LEN, "sdma_%u", 598 pdd->dev->id); 599 ret = kfd_sysfs_create_file(p, &pdd->attr_sdma, pdd->sdma_filename); 600 if (ret) 601 pr_warn("Creating sdma usage for gpu id %d failed", 602 (int)pdd->dev->id); 603 } 604 605 return ret; 606 } 607 608 void kfd_procfs_del_queue(struct queue *q) 609 { 610 if (!q) 611 return; 612 613 kobject_del(&q->kobj); 614 kobject_put(&q->kobj); 615 } 616 617 int kfd_process_create_wq(void) 618 { 619 if (!kfd_process_wq) 620 kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0); 621 if (!kfd_restore_wq) 622 kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0); 623 624 if (!kfd_process_wq || !kfd_restore_wq) { 625 kfd_process_destroy_wq(); 626 return -ENOMEM; 627 } 628 629 return 0; 630 } 631 632 void kfd_process_destroy_wq(void) 633 { 634 if (kfd_process_wq) { 635 destroy_workqueue(kfd_process_wq); 636 kfd_process_wq = NULL; 637 } 638 if (kfd_restore_wq) { 639 destroy_workqueue(kfd_restore_wq); 640 kfd_restore_wq = NULL; 641 } 642 } 643 644 static void kfd_process_free_gpuvm(struct kgd_mem *mem, 645 struct kfd_process_device *pdd) 646 { 647 struct kfd_dev *dev = pdd->dev; 648 649 amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->kgd, mem, pdd->drm_priv); 650 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem, pdd->drm_priv, 651 NULL); 652 } 653 654 /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process 655 * This function should be only called right after the process 656 * is created and when kfd_processes_mutex is still being held 657 * to avoid concurrency. Because of that exclusiveness, we do 658 * not need to take p->mutex. 659 */ 660 static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd, 661 uint64_t gpu_va, uint32_t size, 662 uint32_t flags, void **kptr) 663 { 664 struct kfd_dev *kdev = pdd->dev; 665 struct kgd_mem *mem = NULL; 666 int handle; 667 int err; 668 669 err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->kgd, gpu_va, size, 670 pdd->drm_priv, &mem, NULL, flags); 671 if (err) 672 goto err_alloc_mem; 673 674 err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->kgd, mem, 675 pdd->drm_priv, NULL); 676 if (err) 677 goto err_map_mem; 678 679 err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->kgd, mem, true); 680 if (err) { 681 pr_debug("Sync memory failed, wait interrupted by user signal\n"); 682 goto sync_memory_failed; 683 } 684 685 /* Create an obj handle so kfd_process_device_remove_obj_handle 686 * will take care of the bo removal when the process finishes. 687 * We do not need to take p->mutex, because the process is just 688 * created and the ioctls have not had the chance to run. 689 */ 690 handle = kfd_process_device_create_obj_handle(pdd, mem); 691 692 if (handle < 0) { 693 err = handle; 694 goto free_gpuvm; 695 } 696 697 if (kptr) { 698 err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kdev->kgd, 699 (struct kgd_mem *)mem, kptr, NULL); 700 if (err) { 701 pr_debug("Map GTT BO to kernel failed\n"); 702 goto free_obj_handle; 703 } 704 } 705 706 return err; 707 708 free_obj_handle: 709 kfd_process_device_remove_obj_handle(pdd, handle); 710 free_gpuvm: 711 sync_memory_failed: 712 kfd_process_free_gpuvm(mem, pdd); 713 return err; 714 715 err_map_mem: 716 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem, pdd->drm_priv, 717 NULL); 718 err_alloc_mem: 719 *kptr = NULL; 720 return err; 721 } 722 723 /* kfd_process_device_reserve_ib_mem - Reserve memory inside the 724 * process for IB usage The memory reserved is for KFD to submit 725 * IB to AMDGPU from kernel. If the memory is reserved 726 * successfully, ib_kaddr will have the CPU/kernel 727 * address. Check ib_kaddr before accessing the memory. 728 */ 729 static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd) 730 { 731 struct qcm_process_device *qpd = &pdd->qpd; 732 uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT | 733 KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE | 734 KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE | 735 KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE; 736 void *kaddr; 737 int ret; 738 739 if (qpd->ib_kaddr || !qpd->ib_base) 740 return 0; 741 742 /* ib_base is only set for dGPU */ 743 ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags, 744 &kaddr); 745 if (ret) 746 return ret; 747 748 qpd->ib_kaddr = kaddr; 749 750 return 0; 751 } 752 753 struct kfd_process *kfd_create_process(struct file *filep) 754 { 755 struct kfd_process *process; 756 struct task_struct *thread = current; 757 int ret; 758 759 if (!thread->mm) 760 return ERR_PTR(-EINVAL); 761 762 /* Only the pthreads threading model is supported. */ 763 if (thread->group_leader->mm != thread->mm) 764 return ERR_PTR(-EINVAL); 765 766 /* 767 * take kfd processes mutex before starting of process creation 768 * so there won't be a case where two threads of the same process 769 * create two kfd_process structures 770 */ 771 mutex_lock(&kfd_processes_mutex); 772 773 /* A prior open of /dev/kfd could have already created the process. */ 774 process = find_process(thread); 775 if (process) { 776 pr_debug("Process already found\n"); 777 } else { 778 process = create_process(thread); 779 if (IS_ERR(process)) 780 goto out; 781 782 ret = kfd_process_init_cwsr_apu(process, filep); 783 if (ret) 784 goto out_destroy; 785 786 if (!procfs.kobj) 787 goto out; 788 789 process->kobj = kfd_alloc_struct(process->kobj); 790 if (!process->kobj) { 791 pr_warn("Creating procfs kobject failed"); 792 goto out; 793 } 794 ret = kobject_init_and_add(process->kobj, &procfs_type, 795 procfs.kobj, "%d", 796 (int)process->lead_thread->pid); 797 if (ret) { 798 pr_warn("Creating procfs pid directory failed"); 799 kobject_put(process->kobj); 800 goto out; 801 } 802 803 process->attr_pasid.name = "pasid"; 804 process->attr_pasid.mode = KFD_SYSFS_FILE_MODE; 805 sysfs_attr_init(&process->attr_pasid); 806 ret = sysfs_create_file(process->kobj, &process->attr_pasid); 807 if (ret) 808 pr_warn("Creating pasid for pid %d failed", 809 (int)process->lead_thread->pid); 810 811 process->kobj_queues = kobject_create_and_add("queues", 812 process->kobj); 813 if (!process->kobj_queues) 814 pr_warn("Creating KFD proc/queues folder failed"); 815 816 ret = kfd_procfs_add_sysfs_stats(process); 817 if (ret) 818 pr_warn("Creating sysfs stats dir for pid %d failed", 819 (int)process->lead_thread->pid); 820 821 ret = kfd_procfs_add_sysfs_files(process); 822 if (ret) 823 pr_warn("Creating sysfs usage file for pid %d failed", 824 (int)process->lead_thread->pid); 825 } 826 out: 827 if (!IS_ERR(process)) 828 kref_get(&process->ref); 829 mutex_unlock(&kfd_processes_mutex); 830 831 return process; 832 833 out_destroy: 834 hash_del_rcu(&process->kfd_processes); 835 mutex_unlock(&kfd_processes_mutex); 836 synchronize_srcu(&kfd_processes_srcu); 837 /* kfd_process_free_notifier will trigger the cleanup */ 838 mmu_notifier_put(&process->mmu_notifier); 839 return ERR_PTR(ret); 840 } 841 842 struct kfd_process *kfd_get_process(const struct task_struct *thread) 843 { 844 struct kfd_process *process; 845 846 if (!thread->mm) 847 return ERR_PTR(-EINVAL); 848 849 /* Only the pthreads threading model is supported. */ 850 if (thread->group_leader->mm != thread->mm) 851 return ERR_PTR(-EINVAL); 852 853 process = find_process(thread); 854 if (!process) 855 return ERR_PTR(-EINVAL); 856 857 return process; 858 } 859 860 static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) 861 { 862 struct kfd_process *process; 863 864 hash_for_each_possible_rcu(kfd_processes_table, process, 865 kfd_processes, (uintptr_t)mm) 866 if (process->mm == mm) 867 return process; 868 869 return NULL; 870 } 871 872 static struct kfd_process *find_process(const struct task_struct *thread) 873 { 874 struct kfd_process *p; 875 int idx; 876 877 idx = srcu_read_lock(&kfd_processes_srcu); 878 p = find_process_by_mm(thread->mm); 879 srcu_read_unlock(&kfd_processes_srcu, idx); 880 881 return p; 882 } 883 884 void kfd_unref_process(struct kfd_process *p) 885 { 886 kref_put(&p->ref, kfd_process_ref_release); 887 } 888 889 890 static void kfd_process_device_free_bos(struct kfd_process_device *pdd) 891 { 892 struct kfd_process *p = pdd->process; 893 void *mem; 894 int id; 895 int i; 896 897 /* 898 * Remove all handles from idr and release appropriate 899 * local memory object 900 */ 901 idr_for_each_entry(&pdd->alloc_idr, mem, id) { 902 903 for (i = 0; i < p->n_pdds; i++) { 904 struct kfd_process_device *peer_pdd = p->pdds[i]; 905 906 if (!peer_pdd->drm_priv) 907 continue; 908 amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 909 peer_pdd->dev->kgd, mem, peer_pdd->drm_priv); 910 } 911 912 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem, 913 pdd->drm_priv, NULL); 914 kfd_process_device_remove_obj_handle(pdd, id); 915 } 916 } 917 918 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p) 919 { 920 int i; 921 922 for (i = 0; i < p->n_pdds; i++) 923 kfd_process_device_free_bos(p->pdds[i]); 924 } 925 926 static void kfd_process_destroy_pdds(struct kfd_process *p) 927 { 928 int i; 929 930 for (i = 0; i < p->n_pdds; i++) { 931 struct kfd_process_device *pdd = p->pdds[i]; 932 933 pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n", 934 pdd->dev->id, p->pasid); 935 936 if (pdd->drm_file) { 937 amdgpu_amdkfd_gpuvm_release_process_vm( 938 pdd->dev->kgd, pdd->drm_priv); 939 fput(pdd->drm_file); 940 } 941 942 if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base) 943 free_pages((unsigned long)pdd->qpd.cwsr_kaddr, 944 get_order(KFD_CWSR_TBA_TMA_SIZE)); 945 946 kfree(pdd->qpd.doorbell_bitmap); 947 idr_destroy(&pdd->alloc_idr); 948 949 kfd_free_process_doorbells(pdd->dev, pdd->doorbell_index); 950 951 /* 952 * before destroying pdd, make sure to report availability 953 * for auto suspend 954 */ 955 if (pdd->runtime_inuse) { 956 pm_runtime_mark_last_busy(pdd->dev->ddev->dev); 957 pm_runtime_put_autosuspend(pdd->dev->ddev->dev); 958 pdd->runtime_inuse = false; 959 } 960 961 kfree(pdd); 962 p->pdds[i] = NULL; 963 } 964 p->n_pdds = 0; 965 } 966 967 /* No process locking is needed in this function, because the process 968 * is not findable any more. We must assume that no other thread is 969 * using it any more, otherwise we couldn't safely free the process 970 * structure in the end. 971 */ 972 static void kfd_process_wq_release(struct work_struct *work) 973 { 974 struct kfd_process *p = container_of(work, struct kfd_process, 975 release_work); 976 int i; 977 978 /* Remove the procfs files */ 979 if (p->kobj) { 980 sysfs_remove_file(p->kobj, &p->attr_pasid); 981 kobject_del(p->kobj_queues); 982 kobject_put(p->kobj_queues); 983 p->kobj_queues = NULL; 984 985 for (i = 0; i < p->n_pdds; i++) { 986 struct kfd_process_device *pdd = p->pdds[i]; 987 988 sysfs_remove_file(p->kobj, &pdd->attr_vram); 989 sysfs_remove_file(p->kobj, &pdd->attr_sdma); 990 sysfs_remove_file(p->kobj, &pdd->attr_evict); 991 if (pdd->dev->kfd2kgd->get_cu_occupancy != NULL) 992 sysfs_remove_file(p->kobj, &pdd->attr_cu_occupancy); 993 kobject_del(pdd->kobj_stats); 994 kobject_put(pdd->kobj_stats); 995 pdd->kobj_stats = NULL; 996 } 997 998 kobject_del(p->kobj); 999 kobject_put(p->kobj); 1000 p->kobj = NULL; 1001 } 1002 1003 kfd_iommu_unbind_process(p); 1004 1005 kfd_process_free_outstanding_kfd_bos(p); 1006 svm_range_list_fini(p); 1007 1008 kfd_process_destroy_pdds(p); 1009 dma_fence_put(p->ef); 1010 1011 kfd_event_free_process(p); 1012 1013 kfd_pasid_free(p->pasid); 1014 mutex_destroy(&p->mutex); 1015 1016 put_task_struct(p->lead_thread); 1017 1018 kfree(p); 1019 } 1020 1021 static void kfd_process_ref_release(struct kref *ref) 1022 { 1023 struct kfd_process *p = container_of(ref, struct kfd_process, ref); 1024 1025 INIT_WORK(&p->release_work, kfd_process_wq_release); 1026 queue_work(kfd_process_wq, &p->release_work); 1027 } 1028 1029 static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm) 1030 { 1031 int idx = srcu_read_lock(&kfd_processes_srcu); 1032 struct kfd_process *p = find_process_by_mm(mm); 1033 1034 srcu_read_unlock(&kfd_processes_srcu, idx); 1035 1036 return p ? &p->mmu_notifier : ERR_PTR(-ESRCH); 1037 } 1038 1039 static void kfd_process_free_notifier(struct mmu_notifier *mn) 1040 { 1041 kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier)); 1042 } 1043 1044 static void kfd_process_notifier_release(struct mmu_notifier *mn, 1045 struct mm_struct *mm) 1046 { 1047 struct kfd_process *p; 1048 int i; 1049 1050 /* 1051 * The kfd_process structure can not be free because the 1052 * mmu_notifier srcu is read locked 1053 */ 1054 p = container_of(mn, struct kfd_process, mmu_notifier); 1055 if (WARN_ON(p->mm != mm)) 1056 return; 1057 1058 mutex_lock(&kfd_processes_mutex); 1059 hash_del_rcu(&p->kfd_processes); 1060 mutex_unlock(&kfd_processes_mutex); 1061 synchronize_srcu(&kfd_processes_srcu); 1062 1063 cancel_delayed_work_sync(&p->eviction_work); 1064 cancel_delayed_work_sync(&p->restore_work); 1065 cancel_delayed_work_sync(&p->svms.restore_work); 1066 1067 mutex_lock(&p->mutex); 1068 1069 /* Iterate over all process device data structures and if the 1070 * pdd is in debug mode, we should first force unregistration, 1071 * then we will be able to destroy the queues 1072 */ 1073 for (i = 0; i < p->n_pdds; i++) { 1074 struct kfd_dev *dev = p->pdds[i]->dev; 1075 1076 mutex_lock(kfd_get_dbgmgr_mutex()); 1077 if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) { 1078 if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) { 1079 kfd_dbgmgr_destroy(dev->dbgmgr); 1080 dev->dbgmgr = NULL; 1081 } 1082 } 1083 mutex_unlock(kfd_get_dbgmgr_mutex()); 1084 } 1085 1086 kfd_process_dequeue_from_all_devices(p); 1087 pqm_uninit(&p->pqm); 1088 1089 /* Indicate to other users that MM is no longer valid */ 1090 p->mm = NULL; 1091 /* Signal the eviction fence after user mode queues are 1092 * destroyed. This allows any BOs to be freed without 1093 * triggering pointless evictions or waiting for fences. 1094 */ 1095 dma_fence_signal(p->ef); 1096 1097 mutex_unlock(&p->mutex); 1098 1099 mmu_notifier_put(&p->mmu_notifier); 1100 } 1101 1102 static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { 1103 .release = kfd_process_notifier_release, 1104 .alloc_notifier = kfd_process_alloc_notifier, 1105 .free_notifier = kfd_process_free_notifier, 1106 }; 1107 1108 static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep) 1109 { 1110 unsigned long offset; 1111 int i; 1112 1113 for (i = 0; i < p->n_pdds; i++) { 1114 struct kfd_dev *dev = p->pdds[i]->dev; 1115 struct qcm_process_device *qpd = &p->pdds[i]->qpd; 1116 1117 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base) 1118 continue; 1119 1120 offset = KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id); 1121 qpd->tba_addr = (int64_t)vm_mmap(filep, 0, 1122 KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC, 1123 MAP_SHARED, offset); 1124 1125 if (IS_ERR_VALUE(qpd->tba_addr)) { 1126 int err = qpd->tba_addr; 1127 1128 pr_err("Failure to set tba address. error %d.\n", err); 1129 qpd->tba_addr = 0; 1130 qpd->cwsr_kaddr = NULL; 1131 return err; 1132 } 1133 1134 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); 1135 1136 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; 1137 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", 1138 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); 1139 } 1140 1141 return 0; 1142 } 1143 1144 static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd) 1145 { 1146 struct kfd_dev *dev = pdd->dev; 1147 struct qcm_process_device *qpd = &pdd->qpd; 1148 uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT 1149 | KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE 1150 | KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE; 1151 void *kaddr; 1152 int ret; 1153 1154 if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base) 1155 return 0; 1156 1157 /* cwsr_base is only set for dGPU */ 1158 ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base, 1159 KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr); 1160 if (ret) 1161 return ret; 1162 1163 qpd->cwsr_kaddr = kaddr; 1164 qpd->tba_addr = qpd->cwsr_base; 1165 1166 memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size); 1167 1168 qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET; 1169 pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n", 1170 qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr); 1171 1172 return 0; 1173 } 1174 1175 void kfd_process_set_trap_handler(struct qcm_process_device *qpd, 1176 uint64_t tba_addr, 1177 uint64_t tma_addr) 1178 { 1179 if (qpd->cwsr_kaddr) { 1180 /* KFD trap handler is bound, record as second-level TBA/TMA 1181 * in first-level TMA. First-level trap will jump to second. 1182 */ 1183 uint64_t *tma = 1184 (uint64_t *)(qpd->cwsr_kaddr + KFD_CWSR_TMA_OFFSET); 1185 tma[0] = tba_addr; 1186 tma[1] = tma_addr; 1187 } else { 1188 /* No trap handler bound, bind as first-level TBA/TMA. */ 1189 qpd->tba_addr = tba_addr; 1190 qpd->tma_addr = tma_addr; 1191 } 1192 } 1193 1194 bool kfd_process_xnack_mode(struct kfd_process *p, bool supported) 1195 { 1196 int i; 1197 1198 /* On most GFXv9 GPUs, the retry mode in the SQ must match the 1199 * boot time retry setting. Mixing processes with different 1200 * XNACK/retry settings can hang the GPU. 1201 * 1202 * Different GPUs can have different noretry settings depending 1203 * on HW bugs or limitations. We need to find at least one 1204 * XNACK mode for this process that's compatible with all GPUs. 1205 * Fortunately GPUs with retry enabled (noretry=0) can run code 1206 * built for XNACK-off. On GFXv9 it may perform slower. 1207 * 1208 * Therefore applications built for XNACK-off can always be 1209 * supported and will be our fallback if any GPU does not 1210 * support retry. 1211 */ 1212 for (i = 0; i < p->n_pdds; i++) { 1213 struct kfd_dev *dev = p->pdds[i]->dev; 1214 1215 /* Only consider GFXv9 and higher GPUs. Older GPUs don't 1216 * support the SVM APIs and don't need to be considered 1217 * for the XNACK mode selection. 1218 */ 1219 if (dev->device_info->asic_family < CHIP_VEGA10) 1220 continue; 1221 /* Aldebaran can always support XNACK because it can support 1222 * per-process XNACK mode selection. But let the dev->noretry 1223 * setting still influence the default XNACK mode. 1224 */ 1225 if (supported && 1226 dev->device_info->asic_family == CHIP_ALDEBARAN) 1227 continue; 1228 1229 /* GFXv10 and later GPUs do not support shader preemption 1230 * during page faults. This can lead to poor QoS for queue 1231 * management and memory-manager-related preemptions or 1232 * even deadlocks. 1233 */ 1234 if (dev->device_info->asic_family >= CHIP_NAVI10) 1235 return false; 1236 1237 if (dev->noretry) 1238 return false; 1239 } 1240 1241 return true; 1242 } 1243 1244 /* 1245 * On return the kfd_process is fully operational and will be freed when the 1246 * mm is released 1247 */ 1248 static struct kfd_process *create_process(const struct task_struct *thread) 1249 { 1250 struct kfd_process *process; 1251 struct mmu_notifier *mn; 1252 int err = -ENOMEM; 1253 1254 process = kzalloc(sizeof(*process), GFP_KERNEL); 1255 if (!process) 1256 goto err_alloc_process; 1257 1258 kref_init(&process->ref); 1259 mutex_init(&process->mutex); 1260 process->mm = thread->mm; 1261 process->lead_thread = thread->group_leader; 1262 process->n_pdds = 0; 1263 INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker); 1264 INIT_DELAYED_WORK(&process->restore_work, restore_process_worker); 1265 process->last_restore_timestamp = get_jiffies_64(); 1266 kfd_event_init_process(process); 1267 process->is_32bit_user_mode = in_compat_syscall(); 1268 1269 process->pasid = kfd_pasid_alloc(); 1270 if (process->pasid == 0) 1271 goto err_alloc_pasid; 1272 1273 err = pqm_init(&process->pqm, process); 1274 if (err != 0) 1275 goto err_process_pqm_init; 1276 1277 /* init process apertures*/ 1278 err = kfd_init_apertures(process); 1279 if (err != 0) 1280 goto err_init_apertures; 1281 1282 /* Check XNACK support after PDDs are created in kfd_init_apertures */ 1283 process->xnack_enabled = kfd_process_xnack_mode(process, false); 1284 1285 err = svm_range_list_init(process); 1286 if (err) 1287 goto err_init_svm_range_list; 1288 1289 /* alloc_notifier needs to find the process in the hash table */ 1290 hash_add_rcu(kfd_processes_table, &process->kfd_processes, 1291 (uintptr_t)process->mm); 1292 1293 /* MMU notifier registration must be the last call that can fail 1294 * because after this point we cannot unwind the process creation. 1295 * After this point, mmu_notifier_put will trigger the cleanup by 1296 * dropping the last process reference in the free_notifier. 1297 */ 1298 mn = mmu_notifier_get(&kfd_process_mmu_notifier_ops, process->mm); 1299 if (IS_ERR(mn)) { 1300 err = PTR_ERR(mn); 1301 goto err_register_notifier; 1302 } 1303 BUG_ON(mn != &process->mmu_notifier); 1304 1305 get_task_struct(process->lead_thread); 1306 1307 return process; 1308 1309 err_register_notifier: 1310 hash_del_rcu(&process->kfd_processes); 1311 svm_range_list_fini(process); 1312 err_init_svm_range_list: 1313 kfd_process_free_outstanding_kfd_bos(process); 1314 kfd_process_destroy_pdds(process); 1315 err_init_apertures: 1316 pqm_uninit(&process->pqm); 1317 err_process_pqm_init: 1318 kfd_pasid_free(process->pasid); 1319 err_alloc_pasid: 1320 mutex_destroy(&process->mutex); 1321 kfree(process); 1322 err_alloc_process: 1323 return ERR_PTR(err); 1324 } 1325 1326 static int init_doorbell_bitmap(struct qcm_process_device *qpd, 1327 struct kfd_dev *dev) 1328 { 1329 unsigned int i; 1330 int range_start = dev->shared_resources.non_cp_doorbells_start; 1331 int range_end = dev->shared_resources.non_cp_doorbells_end; 1332 1333 if (!KFD_IS_SOC15(dev->device_info->asic_family)) 1334 return 0; 1335 1336 qpd->doorbell_bitmap = 1337 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, 1338 BITS_PER_BYTE), GFP_KERNEL); 1339 if (!qpd->doorbell_bitmap) 1340 return -ENOMEM; 1341 1342 /* Mask out doorbells reserved for SDMA, IH, and VCN on SOC15. */ 1343 pr_debug("reserved doorbell 0x%03x - 0x%03x\n", range_start, range_end); 1344 pr_debug("reserved doorbell 0x%03x - 0x%03x\n", 1345 range_start + KFD_QUEUE_DOORBELL_MIRROR_OFFSET, 1346 range_end + KFD_QUEUE_DOORBELL_MIRROR_OFFSET); 1347 1348 for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS / 2; i++) { 1349 if (i >= range_start && i <= range_end) { 1350 set_bit(i, qpd->doorbell_bitmap); 1351 set_bit(i + KFD_QUEUE_DOORBELL_MIRROR_OFFSET, 1352 qpd->doorbell_bitmap); 1353 } 1354 } 1355 1356 return 0; 1357 } 1358 1359 struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, 1360 struct kfd_process *p) 1361 { 1362 int i; 1363 1364 for (i = 0; i < p->n_pdds; i++) 1365 if (p->pdds[i]->dev == dev) 1366 return p->pdds[i]; 1367 1368 return NULL; 1369 } 1370 1371 struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, 1372 struct kfd_process *p) 1373 { 1374 struct kfd_process_device *pdd = NULL; 1375 1376 if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE)) 1377 return NULL; 1378 pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); 1379 if (!pdd) 1380 return NULL; 1381 1382 if (kfd_alloc_process_doorbells(dev, &pdd->doorbell_index) < 0) { 1383 pr_err("Failed to alloc doorbell for pdd\n"); 1384 goto err_free_pdd; 1385 } 1386 1387 if (init_doorbell_bitmap(&pdd->qpd, dev)) { 1388 pr_err("Failed to init doorbell for process\n"); 1389 goto err_free_pdd; 1390 } 1391 1392 pdd->dev = dev; 1393 INIT_LIST_HEAD(&pdd->qpd.queues_list); 1394 INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); 1395 pdd->qpd.dqm = dev->dqm; 1396 pdd->qpd.pqm = &p->pqm; 1397 pdd->qpd.evicted = 0; 1398 pdd->qpd.mapped_gws_queue = false; 1399 pdd->process = p; 1400 pdd->bound = PDD_UNBOUND; 1401 pdd->already_dequeued = false; 1402 pdd->runtime_inuse = false; 1403 pdd->vram_usage = 0; 1404 pdd->sdma_past_activity_counter = 0; 1405 atomic64_set(&pdd->evict_duration_counter, 0); 1406 p->pdds[p->n_pdds++] = pdd; 1407 1408 /* Init idr used for memory handle translation */ 1409 idr_init(&pdd->alloc_idr); 1410 1411 return pdd; 1412 1413 err_free_pdd: 1414 kfree(pdd); 1415 return NULL; 1416 } 1417 1418 /** 1419 * kfd_process_device_init_vm - Initialize a VM for a process-device 1420 * 1421 * @pdd: The process-device 1422 * @drm_file: Optional pointer to a DRM file descriptor 1423 * 1424 * If @drm_file is specified, it will be used to acquire the VM from 1425 * that file descriptor. If successful, the @pdd takes ownership of 1426 * the file descriptor. 1427 * 1428 * If @drm_file is NULL, a new VM is created. 1429 * 1430 * Returns 0 on success, -errno on failure. 1431 */ 1432 int kfd_process_device_init_vm(struct kfd_process_device *pdd, 1433 struct file *drm_file) 1434 { 1435 struct kfd_process *p; 1436 struct kfd_dev *dev; 1437 int ret; 1438 1439 if (!drm_file) 1440 return -EINVAL; 1441 1442 if (pdd->drm_priv) 1443 return -EBUSY; 1444 1445 p = pdd->process; 1446 dev = pdd->dev; 1447 1448 ret = amdgpu_amdkfd_gpuvm_acquire_process_vm( 1449 dev->kgd, drm_file, p->pasid, 1450 &p->kgd_process_info, &p->ef); 1451 if (ret) { 1452 pr_err("Failed to create process VM object\n"); 1453 return ret; 1454 } 1455 pdd->drm_priv = drm_file->private_data; 1456 1457 ret = kfd_process_device_reserve_ib_mem(pdd); 1458 if (ret) 1459 goto err_reserve_ib_mem; 1460 ret = kfd_process_device_init_cwsr_dgpu(pdd); 1461 if (ret) 1462 goto err_init_cwsr; 1463 1464 pdd->drm_file = drm_file; 1465 1466 return 0; 1467 1468 err_init_cwsr: 1469 err_reserve_ib_mem: 1470 kfd_process_device_free_bos(pdd); 1471 pdd->drm_priv = NULL; 1472 1473 return ret; 1474 } 1475 1476 /* 1477 * Direct the IOMMU to bind the process (specifically the pasid->mm) 1478 * to the device. 1479 * Unbinding occurs when the process dies or the device is removed. 1480 * 1481 * Assumes that the process lock is held. 1482 */ 1483 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, 1484 struct kfd_process *p) 1485 { 1486 struct kfd_process_device *pdd; 1487 int err; 1488 1489 pdd = kfd_get_process_device_data(dev, p); 1490 if (!pdd) { 1491 pr_err("Process device data doesn't exist\n"); 1492 return ERR_PTR(-ENOMEM); 1493 } 1494 1495 if (!pdd->drm_priv) 1496 return ERR_PTR(-ENODEV); 1497 1498 /* 1499 * signal runtime-pm system to auto resume and prevent 1500 * further runtime suspend once device pdd is created until 1501 * pdd is destroyed. 1502 */ 1503 if (!pdd->runtime_inuse) { 1504 err = pm_runtime_get_sync(dev->ddev->dev); 1505 if (err < 0) { 1506 pm_runtime_put_autosuspend(dev->ddev->dev); 1507 return ERR_PTR(err); 1508 } 1509 } 1510 1511 err = kfd_iommu_bind_process_to_device(pdd); 1512 if (err) 1513 goto out; 1514 1515 /* 1516 * make sure that runtime_usage counter is incremented just once 1517 * per pdd 1518 */ 1519 pdd->runtime_inuse = true; 1520 1521 return pdd; 1522 1523 out: 1524 /* balance runpm reference count and exit with error */ 1525 if (!pdd->runtime_inuse) { 1526 pm_runtime_mark_last_busy(dev->ddev->dev); 1527 pm_runtime_put_autosuspend(dev->ddev->dev); 1528 } 1529 1530 return ERR_PTR(err); 1531 } 1532 1533 /* Create specific handle mapped to mem from process local memory idr 1534 * Assumes that the process lock is held. 1535 */ 1536 int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, 1537 void *mem) 1538 { 1539 return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL); 1540 } 1541 1542 /* Translate specific handle from process local memory idr 1543 * Assumes that the process lock is held. 1544 */ 1545 void *kfd_process_device_translate_handle(struct kfd_process_device *pdd, 1546 int handle) 1547 { 1548 if (handle < 0) 1549 return NULL; 1550 1551 return idr_find(&pdd->alloc_idr, handle); 1552 } 1553 1554 /* Remove specific handle from process local memory idr 1555 * Assumes that the process lock is held. 1556 */ 1557 void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, 1558 int handle) 1559 { 1560 if (handle >= 0) 1561 idr_remove(&pdd->alloc_idr, handle); 1562 } 1563 1564 /* This increments the process->ref counter. */ 1565 struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid) 1566 { 1567 struct kfd_process *p, *ret_p = NULL; 1568 unsigned int temp; 1569 1570 int idx = srcu_read_lock(&kfd_processes_srcu); 1571 1572 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1573 if (p->pasid == pasid) { 1574 kref_get(&p->ref); 1575 ret_p = p; 1576 break; 1577 } 1578 } 1579 1580 srcu_read_unlock(&kfd_processes_srcu, idx); 1581 1582 return ret_p; 1583 } 1584 1585 /* This increments the process->ref counter. */ 1586 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm) 1587 { 1588 struct kfd_process *p; 1589 1590 int idx = srcu_read_lock(&kfd_processes_srcu); 1591 1592 p = find_process_by_mm(mm); 1593 if (p) 1594 kref_get(&p->ref); 1595 1596 srcu_read_unlock(&kfd_processes_srcu, idx); 1597 1598 return p; 1599 } 1600 1601 /* kfd_process_evict_queues - Evict all user queues of a process 1602 * 1603 * Eviction is reference-counted per process-device. This means multiple 1604 * evictions from different sources can be nested safely. 1605 */ 1606 int kfd_process_evict_queues(struct kfd_process *p) 1607 { 1608 int r = 0; 1609 int i; 1610 unsigned int n_evicted = 0; 1611 1612 for (i = 0; i < p->n_pdds; i++) { 1613 struct kfd_process_device *pdd = p->pdds[i]; 1614 1615 r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm, 1616 &pdd->qpd); 1617 if (r) { 1618 pr_err("Failed to evict process queues\n"); 1619 goto fail; 1620 } 1621 n_evicted++; 1622 } 1623 1624 return r; 1625 1626 fail: 1627 /* To keep state consistent, roll back partial eviction by 1628 * restoring queues 1629 */ 1630 for (i = 0; i < p->n_pdds; i++) { 1631 struct kfd_process_device *pdd = p->pdds[i]; 1632 1633 if (n_evicted == 0) 1634 break; 1635 if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, 1636 &pdd->qpd)) 1637 pr_err("Failed to restore queues\n"); 1638 1639 n_evicted--; 1640 } 1641 1642 return r; 1643 } 1644 1645 /* kfd_process_restore_queues - Restore all user queues of a process */ 1646 int kfd_process_restore_queues(struct kfd_process *p) 1647 { 1648 int r, ret = 0; 1649 int i; 1650 1651 for (i = 0; i < p->n_pdds; i++) { 1652 struct kfd_process_device *pdd = p->pdds[i]; 1653 1654 r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm, 1655 &pdd->qpd); 1656 if (r) { 1657 pr_err("Failed to restore process queues\n"); 1658 if (!ret) 1659 ret = r; 1660 } 1661 } 1662 1663 return ret; 1664 } 1665 1666 int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id) 1667 { 1668 int i; 1669 1670 for (i = 0; i < p->n_pdds; i++) 1671 if (p->pdds[i] && gpu_id == p->pdds[i]->dev->id) 1672 return i; 1673 return -EINVAL; 1674 } 1675 1676 int 1677 kfd_process_gpuid_from_kgd(struct kfd_process *p, struct amdgpu_device *adev, 1678 uint32_t *gpuid, uint32_t *gpuidx) 1679 { 1680 struct kgd_dev *kgd = (struct kgd_dev *)adev; 1681 int i; 1682 1683 for (i = 0; i < p->n_pdds; i++) 1684 if (p->pdds[i] && p->pdds[i]->dev->kgd == kgd) { 1685 *gpuid = p->pdds[i]->dev->id; 1686 *gpuidx = i; 1687 return 0; 1688 } 1689 return -EINVAL; 1690 } 1691 1692 static void evict_process_worker(struct work_struct *work) 1693 { 1694 int ret; 1695 struct kfd_process *p; 1696 struct delayed_work *dwork; 1697 1698 dwork = to_delayed_work(work); 1699 1700 /* Process termination destroys this worker thread. So during the 1701 * lifetime of this thread, kfd_process p will be valid 1702 */ 1703 p = container_of(dwork, struct kfd_process, eviction_work); 1704 WARN_ONCE(p->last_eviction_seqno != p->ef->seqno, 1705 "Eviction fence mismatch\n"); 1706 1707 /* Narrow window of overlap between restore and evict work 1708 * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos 1709 * unreserves KFD BOs, it is possible to evicted again. But 1710 * restore has few more steps of finish. So lets wait for any 1711 * previous restore work to complete 1712 */ 1713 flush_delayed_work(&p->restore_work); 1714 1715 pr_debug("Started evicting pasid 0x%x\n", p->pasid); 1716 ret = kfd_process_evict_queues(p); 1717 if (!ret) { 1718 dma_fence_signal(p->ef); 1719 dma_fence_put(p->ef); 1720 p->ef = NULL; 1721 queue_delayed_work(kfd_restore_wq, &p->restore_work, 1722 msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)); 1723 1724 pr_debug("Finished evicting pasid 0x%x\n", p->pasid); 1725 } else 1726 pr_err("Failed to evict queues of pasid 0x%x\n", p->pasid); 1727 } 1728 1729 static void restore_process_worker(struct work_struct *work) 1730 { 1731 struct delayed_work *dwork; 1732 struct kfd_process *p; 1733 int ret = 0; 1734 1735 dwork = to_delayed_work(work); 1736 1737 /* Process termination destroys this worker thread. So during the 1738 * lifetime of this thread, kfd_process p will be valid 1739 */ 1740 p = container_of(dwork, struct kfd_process, restore_work); 1741 pr_debug("Started restoring pasid 0x%x\n", p->pasid); 1742 1743 /* Setting last_restore_timestamp before successful restoration. 1744 * Otherwise this would have to be set by KGD (restore_process_bos) 1745 * before KFD BOs are unreserved. If not, the process can be evicted 1746 * again before the timestamp is set. 1747 * If restore fails, the timestamp will be set again in the next 1748 * attempt. This would mean that the minimum GPU quanta would be 1749 * PROCESS_ACTIVE_TIME_MS - (time to execute the following two 1750 * functions) 1751 */ 1752 1753 p->last_restore_timestamp = get_jiffies_64(); 1754 ret = amdgpu_amdkfd_gpuvm_restore_process_bos(p->kgd_process_info, 1755 &p->ef); 1756 if (ret) { 1757 pr_debug("Failed to restore BOs of pasid 0x%x, retry after %d ms\n", 1758 p->pasid, PROCESS_BACK_OFF_TIME_MS); 1759 ret = queue_delayed_work(kfd_restore_wq, &p->restore_work, 1760 msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS)); 1761 WARN(!ret, "reschedule restore work failed\n"); 1762 return; 1763 } 1764 1765 ret = kfd_process_restore_queues(p); 1766 if (!ret) 1767 pr_debug("Finished restoring pasid 0x%x\n", p->pasid); 1768 else 1769 pr_err("Failed to restore queues of pasid 0x%x\n", p->pasid); 1770 } 1771 1772 void kfd_suspend_all_processes(void) 1773 { 1774 struct kfd_process *p; 1775 unsigned int temp; 1776 int idx = srcu_read_lock(&kfd_processes_srcu); 1777 1778 WARN(debug_evictions, "Evicting all processes"); 1779 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1780 cancel_delayed_work_sync(&p->eviction_work); 1781 cancel_delayed_work_sync(&p->restore_work); 1782 1783 if (kfd_process_evict_queues(p)) 1784 pr_err("Failed to suspend process 0x%x\n", p->pasid); 1785 dma_fence_signal(p->ef); 1786 dma_fence_put(p->ef); 1787 p->ef = NULL; 1788 } 1789 srcu_read_unlock(&kfd_processes_srcu, idx); 1790 } 1791 1792 int kfd_resume_all_processes(void) 1793 { 1794 struct kfd_process *p; 1795 unsigned int temp; 1796 int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu); 1797 1798 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1799 if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) { 1800 pr_err("Restore process %d failed during resume\n", 1801 p->pasid); 1802 ret = -EFAULT; 1803 } 1804 } 1805 srcu_read_unlock(&kfd_processes_srcu, idx); 1806 return ret; 1807 } 1808 1809 int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process, 1810 struct vm_area_struct *vma) 1811 { 1812 struct kfd_process_device *pdd; 1813 struct qcm_process_device *qpd; 1814 1815 if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) { 1816 pr_err("Incorrect CWSR mapping size.\n"); 1817 return -EINVAL; 1818 } 1819 1820 pdd = kfd_get_process_device_data(dev, process); 1821 if (!pdd) 1822 return -EINVAL; 1823 qpd = &pdd->qpd; 1824 1825 qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1826 get_order(KFD_CWSR_TBA_TMA_SIZE)); 1827 if (!qpd->cwsr_kaddr) { 1828 pr_err("Error allocating per process CWSR buffer.\n"); 1829 return -ENOMEM; 1830 } 1831 1832 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND 1833 | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP; 1834 /* Mapping pages to user process */ 1835 return remap_pfn_range(vma, vma->vm_start, 1836 PFN_DOWN(__pa(qpd->cwsr_kaddr)), 1837 KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot); 1838 } 1839 1840 void kfd_flush_tlb(struct kfd_process_device *pdd, enum TLB_FLUSH_TYPE type) 1841 { 1842 struct kfd_dev *dev = pdd->dev; 1843 1844 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) { 1845 /* Nothing to flush until a VMID is assigned, which 1846 * only happens when the first queue is created. 1847 */ 1848 if (pdd->qpd.vmid) 1849 amdgpu_amdkfd_flush_gpu_tlb_vmid(dev->kgd, 1850 pdd->qpd.vmid); 1851 } else { 1852 amdgpu_amdkfd_flush_gpu_tlb_pasid(dev->kgd, 1853 pdd->process->pasid, type); 1854 } 1855 } 1856 1857 #if defined(CONFIG_DEBUG_FS) 1858 1859 int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data) 1860 { 1861 struct kfd_process *p; 1862 unsigned int temp; 1863 int r = 0; 1864 1865 int idx = srcu_read_lock(&kfd_processes_srcu); 1866 1867 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1868 seq_printf(m, "Process %d PASID 0x%x:\n", 1869 p->lead_thread->tgid, p->pasid); 1870 1871 mutex_lock(&p->mutex); 1872 r = pqm_debugfs_mqds(m, &p->pqm); 1873 mutex_unlock(&p->mutex); 1874 1875 if (r) 1876 break; 1877 } 1878 1879 srcu_read_unlock(&kfd_processes_srcu, idx); 1880 1881 return r; 1882 } 1883 1884 #endif 1885 1886