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