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