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