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/mm_types.h> 25 #include <linux/slab.h> 26 #include <linux/types.h> 27 #include <linux/sched/signal.h> 28 #include <linux/sched/mm.h> 29 #include <linux/uaccess.h> 30 #include <linux/mman.h> 31 #include <linux/memory.h> 32 #include "kfd_priv.h" 33 #include "kfd_events.h" 34 #include "kfd_device_queue_manager.h" 35 #include <linux/device.h> 36 37 /* 38 * Wrapper around wait_queue_entry_t 39 */ 40 struct kfd_event_waiter { 41 wait_queue_entry_t wait; 42 struct kfd_event *event; /* Event to wait for */ 43 bool activated; /* Becomes true when event is signaled */ 44 bool event_age_enabled; /* set to true when last_event_age is non-zero */ 45 }; 46 47 /* 48 * Each signal event needs a 64-bit signal slot where the signaler will write 49 * a 1 before sending an interrupt. (This is needed because some interrupts 50 * do not contain enough spare data bits to identify an event.) 51 * We get whole pages and map them to the process VA. 52 * Individual signal events use their event_id as slot index. 53 */ 54 struct kfd_signal_page { 55 uint64_t *kernel_address; 56 uint64_t __user *user_address; 57 bool need_to_free_pages; 58 }; 59 60 static uint64_t *page_slots(struct kfd_signal_page *page) 61 { 62 return page->kernel_address; 63 } 64 65 static struct kfd_signal_page *allocate_signal_page(struct kfd_process *p) 66 { 67 void *backing_store; 68 struct kfd_signal_page *page; 69 70 page = kzalloc_obj(*page); 71 if (!page) 72 return NULL; 73 74 backing_store = (void *) __get_free_pages(GFP_KERNEL, 75 get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); 76 if (!backing_store) 77 goto fail_alloc_signal_store; 78 79 /* Initialize all events to unsignaled */ 80 memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT, 81 KFD_SIGNAL_EVENT_LIMIT * 8); 82 83 page->kernel_address = backing_store; 84 page->need_to_free_pages = true; 85 pr_debug("Allocated new event signal page at %p, for process %p\n", 86 page, p); 87 88 return page; 89 90 fail_alloc_signal_store: 91 kfree(page); 92 return NULL; 93 } 94 95 static int allocate_event_notification_slot(struct kfd_process *p, 96 struct kfd_event *ev, 97 const int *restore_id) 98 { 99 int id; 100 101 if (!p->signal_page) { 102 p->signal_page = allocate_signal_page(p); 103 if (!p->signal_page) 104 return -ENOMEM; 105 /* Oldest user mode expects 256 event slots */ 106 p->signal_mapped_size = 256*8; 107 } 108 109 if (restore_id) { 110 if (*restore_id >= KFD_SIGNAL_EVENT_LIMIT) 111 return -EINVAL; 112 113 id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1, 114 GFP_KERNEL); 115 } else { 116 /* 117 * Compatibility with old user mode: Only use signal slots 118 * user mode has mapped, may be less than 119 * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase 120 * of the event limit without breaking user mode. 121 */ 122 id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8, 123 GFP_KERNEL); 124 } 125 if (id < 0) 126 return id; 127 128 ev->event_id = id; 129 page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT; 130 131 return 0; 132 } 133 134 /* 135 * Assumes that p->event_mutex or rcu_readlock is held and of course that p is 136 * not going away. 137 */ 138 static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id) 139 { 140 return idr_find(&p->event_idr, id); 141 } 142 143 /** 144 * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID 145 * @p: Pointer to struct kfd_process 146 * @id: ID to look up 147 * @bits: Number of valid bits in @id 148 * @signal_mailbox_updated: flag indicates if FW updates signal mailbox entry 149 * 150 * Finds the first signaled event with a matching partial ID. If no 151 * matching signaled event is found, returns NULL. In that case the 152 * caller should assume that the partial ID is invalid and do an 153 * exhaustive search of all siglaned events. 154 * 155 * If multiple events with the same partial ID signal at the same 156 * time, they will be found one interrupt at a time, not necessarily 157 * in the same order the interrupts occurred. As long as the number of 158 * interrupts is correct, all signaled events will be seen by the 159 * driver. 160 */ 161 static struct kfd_event *lookup_signaled_event_by_partial_id( 162 struct kfd_process *p, uint32_t id, uint32_t bits, 163 bool signal_mailbox_updated) 164 { 165 struct kfd_event *ev; 166 167 if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT) 168 return NULL; 169 170 /* Fast path for the common case that @id is not a partial ID 171 * and we only need a single lookup. 172 */ 173 if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) { 174 if (signal_mailbox_updated && 175 page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT) 176 return NULL; 177 178 return idr_find(&p->event_idr, id); 179 } 180 181 /* General case for partial IDs: Iterate over all matching IDs 182 * and find the first one that has signaled. 183 */ 184 for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) { 185 if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT) 186 continue; 187 188 ev = idr_find(&p->event_idr, id); 189 } 190 191 return ev; 192 } 193 194 static int create_signal_event(struct file *devkfd, struct kfd_process *p, 195 struct kfd_event *ev, const int *restore_id) 196 { 197 int ret; 198 199 if (p->signal_mapped_size && 200 p->signal_event_count == p->signal_mapped_size / 8) { 201 if (!p->signal_event_limit_reached) { 202 pr_debug("Signal event wasn't created because limit was reached\n"); 203 p->signal_event_limit_reached = true; 204 } 205 return -ENOSPC; 206 } 207 208 ret = allocate_event_notification_slot(p, ev, restore_id); 209 if (ret) { 210 pr_warn("Failed to create signal event notification slot\n"); 211 return ret; 212 } 213 214 p->signal_event_count++; 215 216 ev->user_signal_address = &p->signal_page->user_address[ev->event_id]; 217 pr_debug("Signal event number %zu created with id %d, address %p\n", 218 p->signal_event_count, ev->event_id, 219 ev->user_signal_address); 220 221 return 0; 222 } 223 224 static int create_other_event(struct kfd_process *p, struct kfd_event *ev, const int *restore_id) 225 { 226 int id; 227 228 if (restore_id) 229 id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1, 230 GFP_KERNEL); 231 else 232 /* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an 233 * intentional integer overflow to -1 without a compiler 234 * warning. idr_alloc treats a negative value as "maximum 235 * signed integer". 236 */ 237 id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID, 238 (uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1, 239 GFP_KERNEL); 240 241 if (id < 0) 242 return id; 243 ev->event_id = id; 244 245 return 0; 246 } 247 248 int kfd_event_init_process(struct kfd_process *p) 249 { 250 int id; 251 252 mutex_init(&p->event_mutex); 253 idr_init(&p->event_idr); 254 p->signal_page = NULL; 255 p->signal_event_count = 1; 256 /* Allocate event ID 0. It is used for a fast path to ignore bogus events 257 * that are sent by the CP without a context ID 258 */ 259 id = idr_alloc(&p->event_idr, NULL, 0, 1, GFP_KERNEL); 260 if (id < 0) { 261 idr_destroy(&p->event_idr); 262 mutex_destroy(&p->event_mutex); 263 return id; 264 } 265 return 0; 266 } 267 268 static void destroy_event(struct kfd_process *p, struct kfd_event *ev) 269 { 270 struct kfd_event_waiter *waiter; 271 272 /* Wake up pending waiters. They will return failure */ 273 spin_lock(&ev->lock); 274 list_for_each_entry(waiter, &ev->wq.head, wait.entry) 275 WRITE_ONCE(waiter->event, NULL); 276 wake_up_all(&ev->wq); 277 spin_unlock(&ev->lock); 278 279 if (ev->type == KFD_EVENT_TYPE_SIGNAL || 280 ev->type == KFD_EVENT_TYPE_DEBUG) 281 p->signal_event_count--; 282 283 idr_remove(&p->event_idr, ev->event_id); 284 kfree_rcu(ev, rcu); 285 } 286 287 static void destroy_events(struct kfd_process *p) 288 { 289 struct kfd_event *ev; 290 uint32_t id; 291 292 idr_for_each_entry(&p->event_idr, ev, id) 293 if (ev) 294 destroy_event(p, ev); 295 idr_destroy(&p->event_idr); 296 mutex_destroy(&p->event_mutex); 297 } 298 299 /* 300 * We assume that the process is being destroyed and there is no need to 301 * unmap the pages or keep bookkeeping data in order. 302 */ 303 static void shutdown_signal_page(struct kfd_process *p) 304 { 305 struct kfd_signal_page *page = p->signal_page; 306 307 if (page) { 308 if (page->need_to_free_pages) 309 free_pages((unsigned long)page->kernel_address, 310 get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); 311 kfree(page); 312 } 313 } 314 315 void kfd_event_free_process(struct kfd_process *p) 316 { 317 destroy_events(p); 318 shutdown_signal_page(p); 319 } 320 321 static bool event_can_be_gpu_signaled(const struct kfd_event *ev) 322 { 323 return ev->type == KFD_EVENT_TYPE_SIGNAL || 324 ev->type == KFD_EVENT_TYPE_DEBUG; 325 } 326 327 static bool event_can_be_cpu_signaled(const struct kfd_event *ev) 328 { 329 return ev->type == KFD_EVENT_TYPE_SIGNAL; 330 } 331 332 static int kfd_event_page_set(struct kfd_process *p, void *kernel_address, 333 uint64_t size, uint64_t user_handle) 334 { 335 struct kfd_signal_page *page; 336 337 if (p->signal_page) 338 return -EBUSY; 339 340 if (size < KFD_SIGNAL_EVENT_LIMIT * 8) { 341 pr_err("Event page size %llu is too small, need at least %lu bytes\n", 342 size, (unsigned long)(KFD_SIGNAL_EVENT_LIMIT * 8)); 343 return -EINVAL; 344 } 345 346 page = kzalloc_obj(*page); 347 if (!page) 348 return -ENOMEM; 349 350 /* Initialize all events to unsignaled */ 351 memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT, 352 KFD_SIGNAL_EVENT_LIMIT * 8); 353 354 page->kernel_address = kernel_address; 355 356 p->signal_page = page; 357 p->signal_mapped_size = size; 358 p->signal_handle = user_handle; 359 return 0; 360 } 361 362 int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset) 363 { 364 struct kfd_node *kfd; 365 struct kfd_process_device *pdd; 366 void *mem, *kern_addr; 367 uint64_t size; 368 int err = 0; 369 370 if (p->signal_page) { 371 pr_err("Event page is already set\n"); 372 return -EINVAL; 373 } 374 375 pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(event_page_offset)); 376 if (!pdd) { 377 pr_err("Getting device by id failed in %s\n", __func__); 378 return -EINVAL; 379 } 380 kfd = pdd->dev; 381 382 pdd = kfd_bind_process_to_device(kfd, p); 383 if (IS_ERR(pdd)) 384 return PTR_ERR(pdd); 385 386 mem = kfd_process_device_translate_handle(pdd, 387 GET_IDR_HANDLE(event_page_offset)); 388 if (!mem) { 389 pr_err("Can't find BO, offset is 0x%llx\n", event_page_offset); 390 return -EINVAL; 391 } 392 393 err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(mem, &kern_addr, &size); 394 if (err) { 395 pr_err("Failed to map event page to kernel\n"); 396 return err; 397 } 398 399 err = kfd_event_page_set(p, kern_addr, size, event_page_offset); 400 if (err) { 401 pr_err("Failed to set event page\n"); 402 amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem); 403 return err; 404 } 405 return err; 406 } 407 408 int kfd_event_create(struct file *devkfd, struct kfd_process *p, 409 uint32_t event_type, bool auto_reset, uint32_t node_id, 410 uint32_t *event_id, uint32_t *event_trigger_data, 411 uint64_t *event_page_offset, uint32_t *event_slot_index) 412 { 413 int ret = 0; 414 struct kfd_event *ev = kzalloc_obj(*ev); 415 416 if (!ev) 417 return -ENOMEM; 418 419 ev->type = event_type; 420 ev->auto_reset = auto_reset; 421 ev->signaled = false; 422 423 spin_lock_init(&ev->lock); 424 init_waitqueue_head(&ev->wq); 425 426 *event_page_offset = 0; 427 428 mutex_lock(&p->event_mutex); 429 430 switch (event_type) { 431 case KFD_EVENT_TYPE_SIGNAL: 432 case KFD_EVENT_TYPE_DEBUG: 433 ret = create_signal_event(devkfd, p, ev, NULL); 434 if (!ret) { 435 *event_page_offset = KFD_MMAP_TYPE_EVENTS; 436 *event_slot_index = ev->event_id; 437 } 438 break; 439 default: 440 ret = create_other_event(p, ev, NULL); 441 break; 442 } 443 444 if (!ret) { 445 *event_id = ev->event_id; 446 *event_trigger_data = ev->event_id; 447 ev->event_age = 1; 448 } else { 449 kfree(ev); 450 } 451 452 mutex_unlock(&p->event_mutex); 453 454 return ret; 455 } 456 457 int kfd_criu_restore_event(struct file *devkfd, 458 struct kfd_process *p, 459 uint8_t __user *user_priv_ptr, 460 uint64_t *priv_data_offset, 461 uint64_t max_priv_data_size) 462 { 463 struct kfd_criu_event_priv_data *ev_priv; 464 struct kfd_event *ev = NULL; 465 int ret = 0; 466 467 ev_priv = kmalloc_obj(*ev_priv); 468 if (!ev_priv) 469 return -ENOMEM; 470 471 ev = kzalloc_obj(*ev); 472 if (!ev) { 473 ret = -ENOMEM; 474 goto exit; 475 } 476 477 if (*priv_data_offset + sizeof(*ev_priv) > max_priv_data_size) { 478 ret = -EINVAL; 479 goto exit; 480 } 481 482 ret = copy_from_user(ev_priv, user_priv_ptr + *priv_data_offset, sizeof(*ev_priv)); 483 if (ret) { 484 ret = -EFAULT; 485 goto exit; 486 } 487 *priv_data_offset += sizeof(*ev_priv); 488 489 if (ev_priv->event_id > INT_MAX) { 490 ret = -EINVAL; 491 goto exit; 492 } 493 494 if (ev_priv->user_handle) { 495 ret = kfd_kmap_event_page(p, ev_priv->user_handle); 496 if (ret) 497 goto exit; 498 } 499 500 ev->type = ev_priv->type; 501 ev->auto_reset = ev_priv->auto_reset; 502 ev->signaled = ev_priv->signaled; 503 504 spin_lock_init(&ev->lock); 505 init_waitqueue_head(&ev->wq); 506 507 mutex_lock(&p->event_mutex); 508 switch (ev->type) { 509 case KFD_EVENT_TYPE_SIGNAL: 510 case KFD_EVENT_TYPE_DEBUG: 511 ret = create_signal_event(devkfd, p, ev, &ev_priv->event_id); 512 break; 513 case KFD_EVENT_TYPE_MEMORY: 514 memcpy(&ev->memory_exception_data, 515 &ev_priv->memory_exception_data, 516 sizeof(struct kfd_hsa_memory_exception_data)); 517 518 ret = create_other_event(p, ev, &ev_priv->event_id); 519 break; 520 case KFD_EVENT_TYPE_HW_EXCEPTION: 521 memcpy(&ev->hw_exception_data, 522 &ev_priv->hw_exception_data, 523 sizeof(struct kfd_hsa_hw_exception_data)); 524 525 ret = create_other_event(p, ev, &ev_priv->event_id); 526 break; 527 default: 528 ret = -EINVAL; 529 break; 530 } 531 mutex_unlock(&p->event_mutex); 532 533 exit: 534 if (ret) 535 kfree(ev); 536 537 kfree(ev_priv); 538 539 return ret; 540 } 541 542 int kfd_criu_checkpoint_events(struct kfd_process *p, 543 uint8_t __user *user_priv_data, 544 uint64_t *priv_data_offset) 545 { 546 struct kfd_criu_event_priv_data *ev_privs; 547 int i = 0; 548 int ret = 0; 549 struct kfd_event *ev; 550 uint32_t ev_id; 551 uint32_t num_events; 552 553 /* Serialize the count and the walk below against concurrent event 554 * create/destroy. Those paths take only p->event_mutex, not the 555 * p->mutex held by the CRIU checkpoint caller, so without this the 556 * event_idr can grow between kfd_get_num_events() and the loop and the 557 * walk writes past the ev_privs allocation. 558 */ 559 mutex_lock(&p->event_mutex); 560 561 num_events = kfd_get_num_events(p); 562 if (!num_events) { 563 mutex_unlock(&p->event_mutex); 564 return 0; 565 } 566 567 ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL); 568 if (!ev_privs) { 569 mutex_unlock(&p->event_mutex); 570 return -ENOMEM; 571 } 572 573 574 idr_for_each_entry(&p->event_idr, ev, ev_id) { 575 struct kfd_criu_event_priv_data *ev_priv; 576 577 /* 578 * Currently, all events have same size of private_data, but the current ioctl's 579 * and CRIU plugin supports private_data of variable sizes 580 */ 581 ev_priv = &ev_privs[i]; 582 583 ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT; 584 585 /* We store the user_handle with the first event */ 586 if (i == 0 && p->signal_page) 587 ev_priv->user_handle = p->signal_handle; 588 589 ev_priv->event_id = ev->event_id; 590 ev_priv->auto_reset = ev->auto_reset; 591 ev_priv->type = ev->type; 592 ev_priv->signaled = ev->signaled; 593 594 if (ev_priv->type == KFD_EVENT_TYPE_MEMORY) 595 memcpy(&ev_priv->memory_exception_data, 596 &ev->memory_exception_data, 597 sizeof(struct kfd_hsa_memory_exception_data)); 598 else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION) 599 memcpy(&ev_priv->hw_exception_data, 600 &ev->hw_exception_data, 601 sizeof(struct kfd_hsa_hw_exception_data)); 602 603 pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n", 604 i, 605 ev_priv->event_id, 606 ev_priv->auto_reset, 607 ev_priv->type, 608 ev_priv->signaled); 609 i++; 610 } 611 612 mutex_unlock(&p->event_mutex); 613 614 ret = copy_to_user(user_priv_data + *priv_data_offset, 615 ev_privs, num_events * sizeof(*ev_privs)); 616 if (ret) { 617 pr_err("Failed to copy events priv to user\n"); 618 ret = -EFAULT; 619 } 620 621 *priv_data_offset += num_events * sizeof(*ev_privs); 622 623 kvfree(ev_privs); 624 return ret; 625 } 626 627 int kfd_get_num_events(struct kfd_process *p) 628 { 629 struct kfd_event *ev; 630 uint32_t id; 631 u32 num_events = 0; 632 633 idr_for_each_entry(&p->event_idr, ev, id) 634 num_events++; 635 636 return num_events; 637 } 638 639 /* Assumes that p is current. */ 640 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id) 641 { 642 struct kfd_event *ev; 643 int ret = 0; 644 645 mutex_lock(&p->event_mutex); 646 647 ev = lookup_event_by_id(p, event_id); 648 649 if (ev) 650 destroy_event(p, ev); 651 else 652 ret = -EINVAL; 653 654 mutex_unlock(&p->event_mutex); 655 return ret; 656 } 657 658 static void set_event(struct kfd_event *ev) 659 { 660 struct kfd_event_waiter *waiter; 661 662 /* Auto reset if the list is non-empty and we're waking 663 * someone. waitqueue_active is safe here because we're 664 * protected by the ev->lock, which is also held when 665 * updating the wait queues in kfd_wait_on_events. 666 */ 667 ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq); 668 if (!(++ev->event_age)) { 669 /* Never wrap back to reserved/default event age 0/1 */ 670 ev->event_age = 2; 671 WARN_ONCE(1, "event_age wrap back!"); 672 } 673 674 list_for_each_entry(waiter, &ev->wq.head, wait.entry) 675 WRITE_ONCE(waiter->activated, true); 676 677 wake_up_all(&ev->wq); 678 } 679 680 /* Assumes that p is current. */ 681 int kfd_set_event(struct kfd_process *p, uint32_t event_id) 682 { 683 int ret = 0; 684 struct kfd_event *ev; 685 686 rcu_read_lock(); 687 688 ev = lookup_event_by_id(p, event_id); 689 if (!ev) { 690 ret = -EINVAL; 691 goto unlock_rcu; 692 } 693 spin_lock(&ev->lock); 694 695 if (event_can_be_cpu_signaled(ev)) 696 set_event(ev); 697 else 698 ret = -EINVAL; 699 700 spin_unlock(&ev->lock); 701 unlock_rcu: 702 rcu_read_unlock(); 703 return ret; 704 } 705 706 static void reset_event(struct kfd_event *ev) 707 { 708 ev->signaled = false; 709 } 710 711 /* Assumes that p is current. */ 712 int kfd_reset_event(struct kfd_process *p, uint32_t event_id) 713 { 714 int ret = 0; 715 struct kfd_event *ev; 716 717 rcu_read_lock(); 718 719 ev = lookup_event_by_id(p, event_id); 720 if (!ev) { 721 ret = -EINVAL; 722 goto unlock_rcu; 723 } 724 spin_lock(&ev->lock); 725 726 if (event_can_be_cpu_signaled(ev)) 727 reset_event(ev); 728 else 729 ret = -EINVAL; 730 731 spin_unlock(&ev->lock); 732 unlock_rcu: 733 rcu_read_unlock(); 734 return ret; 735 736 } 737 738 static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev) 739 { 740 WRITE_ONCE(page_slots(p->signal_page)[ev->event_id], UNSIGNALED_EVENT_SLOT); 741 } 742 743 static void set_event_from_interrupt(struct kfd_process *p, 744 struct kfd_event *ev) 745 { 746 if (ev && event_can_be_gpu_signaled(ev)) { 747 acknowledge_signal(p, ev); 748 spin_lock(&ev->lock); 749 set_event(ev); 750 spin_unlock(&ev->lock); 751 } 752 } 753 754 void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id, 755 uint32_t valid_id_bits, bool signal_mailbox_updated) 756 { 757 struct kfd_event *ev = NULL; 758 759 /* 760 * Because we are called from arbitrary context (workqueue) as opposed 761 * to process context, kfd_process could attempt to exit while we are 762 * running so the lookup function increments the process ref count. 763 */ 764 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL); 765 766 if (!p) 767 return; /* Presumably process exited. */ 768 769 rcu_read_lock(); 770 771 if (valid_id_bits) 772 ev = lookup_signaled_event_by_partial_id(p, partial_id, 773 valid_id_bits, 774 signal_mailbox_updated); 775 if (ev) { 776 set_event_from_interrupt(p, ev); 777 } else if (p->signal_page) { 778 /* 779 * Partial ID lookup failed. Assume that the event ID 780 * in the interrupt payload was invalid and do an 781 * exhaustive search of signaled events. 782 */ 783 uint64_t *slots = page_slots(p->signal_page); 784 uint32_t id; 785 786 if (valid_id_bits) 787 pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n", 788 partial_id, valid_id_bits); 789 790 if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) { 791 /* With relatively few events, it's faster to 792 * iterate over the event IDR 793 */ 794 idr_for_each_entry(&p->event_idr, ev, id) { 795 if (id >= KFD_SIGNAL_EVENT_LIMIT) 796 break; 797 798 if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) 799 set_event_from_interrupt(p, ev); 800 } 801 } else { 802 /* With relatively many events, it's faster to 803 * iterate over the signal slots and lookup 804 * only signaled events from the IDR. 805 */ 806 for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++) 807 if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) { 808 ev = lookup_event_by_id(p, id); 809 set_event_from_interrupt(p, ev); 810 } 811 } 812 } 813 814 rcu_read_unlock(); 815 kfd_unref_process(p); 816 } 817 818 static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events) 819 { 820 struct kfd_event_waiter *event_waiters; 821 uint32_t i; 822 823 if (num_events > KFD_SIGNAL_EVENT_LIMIT) 824 return NULL; 825 event_waiters = kzalloc_objs(struct kfd_event_waiter, num_events); 826 if (!event_waiters) 827 return NULL; 828 829 for (i = 0; i < num_events; i++) 830 init_wait(&event_waiters[i].wait); 831 832 return event_waiters; 833 } 834 835 static int init_event_waiter(struct kfd_process *p, 836 struct kfd_event_waiter *waiter, 837 struct kfd_event_data *event_data) 838 { 839 struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id); 840 841 if (!ev) 842 return -EINVAL; 843 844 spin_lock(&ev->lock); 845 waiter->event = ev; 846 waiter->activated = ev->signaled; 847 ev->signaled = ev->signaled && !ev->auto_reset; 848 849 /* last_event_age = 0 reserved for backward compatible */ 850 if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL && 851 event_data->signal_event_data.last_event_age) { 852 waiter->event_age_enabled = true; 853 if (ev->event_age != event_data->signal_event_data.last_event_age) 854 waiter->activated = true; 855 } 856 857 if (!waiter->activated) 858 add_wait_queue(&ev->wq, &waiter->wait); 859 spin_unlock(&ev->lock); 860 861 return 0; 862 } 863 864 /* test_event_condition - Test condition of events being waited for 865 * @all: Return completion only if all events have signaled 866 * @num_events: Number of events to wait for 867 * @event_waiters: Array of event waiters, one per event 868 * 869 * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have 870 * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all) 871 * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of 872 * the events have been destroyed. 873 */ 874 static uint32_t test_event_condition(bool all, uint32_t num_events, 875 struct kfd_event_waiter *event_waiters) 876 { 877 uint32_t i; 878 uint32_t activated_count = 0; 879 880 for (i = 0; i < num_events; i++) { 881 if (!READ_ONCE(event_waiters[i].event)) 882 return KFD_IOC_WAIT_RESULT_FAIL; 883 884 if (READ_ONCE(event_waiters[i].activated)) { 885 if (!all) 886 return KFD_IOC_WAIT_RESULT_COMPLETE; 887 888 activated_count++; 889 } 890 } 891 892 return activated_count == num_events ? 893 KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT; 894 } 895 896 /* 897 * Copy event specific data, if defined. 898 * Currently only memory exception events have additional data to copy to user 899 */ 900 static int copy_signaled_event_data(uint32_t num_events, 901 struct kfd_event_waiter *event_waiters, 902 struct kfd_event_data __user *data) 903 { 904 void *src; 905 void __user *dst; 906 struct kfd_event_waiter *waiter; 907 struct kfd_event *event; 908 uint32_t i, size = 0; 909 910 for (i = 0; i < num_events; i++) { 911 waiter = &event_waiters[i]; 912 event = waiter->event; 913 if (!event) 914 return -EINVAL; /* event was destroyed */ 915 if (waiter->activated) { 916 if (event->type == KFD_EVENT_TYPE_MEMORY) { 917 dst = &data[i].memory_exception_data; 918 src = &event->memory_exception_data; 919 size = sizeof(struct kfd_hsa_memory_exception_data); 920 } else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) { 921 dst = &data[i].memory_exception_data; 922 src = &event->hw_exception_data; 923 size = sizeof(struct kfd_hsa_hw_exception_data); 924 } else if (event->type == KFD_EVENT_TYPE_SIGNAL && 925 waiter->event_age_enabled) { 926 dst = &data[i].signal_event_data.last_event_age; 927 src = &event->event_age; 928 size = sizeof(u64); 929 } 930 if (size && copy_to_user(dst, src, size)) 931 return -EFAULT; 932 } 933 } 934 935 return 0; 936 } 937 938 static long user_timeout_to_jiffies(uint32_t user_timeout_ms) 939 { 940 if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE) 941 return 0; 942 943 if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE) 944 return MAX_SCHEDULE_TIMEOUT; 945 946 /* 947 * msecs_to_jiffies interprets all values above 2^31-1 as infinite, 948 * but we consider them finite. 949 * This hack is wrong, but nobody is likely to notice. 950 */ 951 user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF); 952 953 return msecs_to_jiffies(user_timeout_ms) + 1; 954 } 955 956 static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters, 957 bool undo_auto_reset) 958 { 959 uint32_t i; 960 961 for (i = 0; i < num_events; i++) 962 if (waiters[i].event) { 963 spin_lock(&waiters[i].event->lock); 964 remove_wait_queue(&waiters[i].event->wq, 965 &waiters[i].wait); 966 if (undo_auto_reset && waiters[i].activated && 967 waiters[i].event && waiters[i].event->auto_reset) 968 set_event(waiters[i].event); 969 spin_unlock(&waiters[i].event->lock); 970 } 971 972 kfree(waiters); 973 } 974 975 int kfd_wait_on_events(struct kfd_process *p, 976 uint32_t num_events, void __user *data, 977 bool all, uint32_t *user_timeout_ms, 978 uint32_t *wait_result) 979 { 980 struct kfd_event_data __user *events = 981 (struct kfd_event_data __user *) data; 982 uint32_t i; 983 int ret = 0; 984 985 struct kfd_event_waiter *event_waiters = NULL; 986 long timeout = user_timeout_to_jiffies(*user_timeout_ms); 987 988 event_waiters = alloc_event_waiters(num_events); 989 if (!event_waiters) { 990 ret = -ENOMEM; 991 goto out; 992 } 993 994 /* Use p->event_mutex here to protect against concurrent creation and 995 * destruction of events while we initialize event_waiters. 996 */ 997 mutex_lock(&p->event_mutex); 998 999 for (i = 0; i < num_events; i++) { 1000 struct kfd_event_data event_data; 1001 1002 if (copy_from_user(&event_data, &events[i], 1003 sizeof(struct kfd_event_data))) { 1004 ret = -EFAULT; 1005 goto out_unlock; 1006 } 1007 1008 ret = init_event_waiter(p, &event_waiters[i], &event_data); 1009 if (ret) 1010 goto out_unlock; 1011 } 1012 1013 /* Check condition once. */ 1014 *wait_result = test_event_condition(all, num_events, event_waiters); 1015 if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) { 1016 ret = copy_signaled_event_data(num_events, 1017 event_waiters, events); 1018 goto out_unlock; 1019 } else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) { 1020 /* This should not happen. Events shouldn't be 1021 * destroyed while we're holding the event_mutex 1022 */ 1023 goto out_unlock; 1024 } 1025 1026 mutex_unlock(&p->event_mutex); 1027 1028 while (true) { 1029 if (fatal_signal_pending(current)) { 1030 ret = -EINTR; 1031 break; 1032 } 1033 1034 if (signal_pending(current)) { 1035 ret = -ERESTARTSYS; 1036 if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE && 1037 *user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE) 1038 *user_timeout_ms = jiffies_to_msecs( 1039 max(0l, timeout-1)); 1040 break; 1041 } 1042 1043 /* Set task state to interruptible sleep before 1044 * checking wake-up conditions. A concurrent wake-up 1045 * will put the task back into runnable state. In that 1046 * case schedule_timeout will not put the task to 1047 * sleep and we'll get a chance to re-check the 1048 * updated conditions almost immediately. Otherwise, 1049 * this race condition would lead to a soft hang or a 1050 * very long sleep. 1051 */ 1052 set_current_state(TASK_INTERRUPTIBLE); 1053 1054 *wait_result = test_event_condition(all, num_events, 1055 event_waiters); 1056 if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT) 1057 break; 1058 1059 if (timeout <= 0) 1060 break; 1061 1062 timeout = schedule_timeout(timeout); 1063 } 1064 __set_current_state(TASK_RUNNING); 1065 1066 mutex_lock(&p->event_mutex); 1067 /* copy_signaled_event_data may sleep. So this has to happen 1068 * after the task state is set back to RUNNING. 1069 * 1070 * The event may also have been destroyed after signaling. So 1071 * copy_signaled_event_data also must confirm that the event 1072 * still exists. Therefore this must be under the p->event_mutex 1073 * which is also held when events are destroyed. 1074 */ 1075 if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) 1076 ret = copy_signaled_event_data(num_events, 1077 event_waiters, events); 1078 1079 out_unlock: 1080 free_waiters(num_events, event_waiters, ret == -ERESTARTSYS); 1081 mutex_unlock(&p->event_mutex); 1082 out: 1083 if (ret) 1084 *wait_result = KFD_IOC_WAIT_RESULT_FAIL; 1085 else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL) 1086 ret = -EIO; 1087 1088 return ret; 1089 } 1090 1091 int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma) 1092 { 1093 unsigned long pfn; 1094 struct kfd_signal_page *page; 1095 int ret; 1096 1097 /* check required size doesn't exceed the allocated size */ 1098 if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) < 1099 get_order(vma->vm_end - vma->vm_start)) { 1100 pr_err("Event page mmap requested illegal size\n"); 1101 return -EINVAL; 1102 } 1103 1104 page = p->signal_page; 1105 if (!page) { 1106 /* Probably KFD bug, but mmap is user-accessible. */ 1107 pr_debug("Signal page could not be found\n"); 1108 return -EINVAL; 1109 } 1110 1111 pfn = __pa(page->kernel_address); 1112 pfn >>= PAGE_SHIFT; 1113 1114 vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE 1115 | VM_DONTDUMP | VM_PFNMAP); 1116 1117 pr_debug("Mapping signal page\n"); 1118 pr_debug(" start user address == 0x%08lx\n", vma->vm_start); 1119 pr_debug(" end user address == 0x%08lx\n", vma->vm_end); 1120 pr_debug(" pfn == 0x%016lX\n", pfn); 1121 pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags); 1122 pr_debug(" size == 0x%08lX\n", 1123 vma->vm_end - vma->vm_start); 1124 1125 page->user_address = (uint64_t __user *)vma->vm_start; 1126 1127 /* mapping the page to user process */ 1128 ret = remap_pfn_range(vma, vma->vm_start, pfn, 1129 vma->vm_end - vma->vm_start, vma->vm_page_prot); 1130 if (!ret) 1131 p->signal_mapped_size = vma->vm_end - vma->vm_start; 1132 1133 return ret; 1134 } 1135 1136 /* 1137 * Assumes that p is not going away. 1138 */ 1139 static void lookup_events_by_type_and_signal(struct kfd_process *p, 1140 int type, void *event_data) 1141 { 1142 struct kfd_hsa_memory_exception_data *ev_data; 1143 struct kfd_event *ev; 1144 uint32_t id; 1145 bool send_signal = true; 1146 1147 ev_data = (struct kfd_hsa_memory_exception_data *) event_data; 1148 1149 rcu_read_lock(); 1150 1151 id = KFD_FIRST_NONSIGNAL_EVENT_ID; 1152 idr_for_each_entry_continue(&p->event_idr, ev, id) 1153 if (ev->type == type) { 1154 send_signal = false; 1155 dev_dbg(kfd_device, 1156 "Event found: id %X type %d", 1157 ev->event_id, ev->type); 1158 spin_lock(&ev->lock); 1159 set_event(ev); 1160 if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data) 1161 ev->memory_exception_data = *ev_data; 1162 spin_unlock(&ev->lock); 1163 } 1164 1165 if (type == KFD_EVENT_TYPE_MEMORY) { 1166 dev_warn(kfd_device, 1167 "Sending SIGSEGV to process pid %d", 1168 p->lead_thread->pid); 1169 send_sig(SIGSEGV, p->lead_thread, 0); 1170 } 1171 1172 /* Send SIGTERM no event of type "type" has been found*/ 1173 if (send_signal) { 1174 if (send_sigterm) { 1175 dev_warn(kfd_device, 1176 "Sending SIGTERM to process pid %d", 1177 p->lead_thread->pid); 1178 send_sig(SIGTERM, p->lead_thread, 0); 1179 } else { 1180 dev_err(kfd_device, 1181 "Process pid %d got unhandled exception", 1182 p->lead_thread->pid); 1183 } 1184 } 1185 1186 rcu_read_unlock(); 1187 } 1188 1189 void kfd_signal_hw_exception_event(u32 pasid) 1190 { 1191 /* 1192 * Because we are called from arbitrary context (workqueue) as opposed 1193 * to process context, kfd_process could attempt to exit while we are 1194 * running so the lookup function increments the process ref count. 1195 */ 1196 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL); 1197 1198 if (!p) 1199 return; /* Presumably process exited. */ 1200 1201 lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL); 1202 kfd_unref_process(p); 1203 } 1204 1205 void kfd_signal_vm_fault_event_with_userptr(struct kfd_process *p, uint64_t gpu_va) 1206 { 1207 struct kfd_process_device *pdd; 1208 struct kfd_hsa_memory_exception_data exception_data; 1209 int i; 1210 1211 memset(&exception_data, 0, sizeof(exception_data)); 1212 exception_data.va = gpu_va; 1213 exception_data.failure.NotPresent = 1; 1214 1215 // Send VM seg fault to all kfd process device 1216 for (i = 0; i < p->n_pdds; i++) { 1217 pdd = p->pdds[i]; 1218 exception_data.gpu_id = pdd->user_gpu_id; 1219 kfd_evict_process_device(pdd); 1220 kfd_signal_vm_fault_event(pdd, NULL, &exception_data); 1221 } 1222 } 1223 1224 void kfd_signal_vm_fault_event(struct kfd_process_device *pdd, 1225 struct kfd_vm_fault_info *info, 1226 struct kfd_hsa_memory_exception_data *data) 1227 { 1228 struct kfd_event *ev; 1229 uint32_t id; 1230 struct kfd_process *p = pdd->process; 1231 struct kfd_hsa_memory_exception_data memory_exception_data; 1232 int user_gpu_id; 1233 1234 user_gpu_id = kfd_process_get_user_gpu_id(p, pdd->dev->id); 1235 if (unlikely(user_gpu_id == -EINVAL)) { 1236 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", 1237 pdd->dev->id); 1238 return; 1239 } 1240 1241 /* SoC15 chips and onwards will pass in data from now on. */ 1242 if (!data) { 1243 memset(&memory_exception_data, 0, sizeof(memory_exception_data)); 1244 memory_exception_data.gpu_id = user_gpu_id; 1245 memory_exception_data.failure.imprecise = true; 1246 1247 /* Set failure reason */ 1248 if (info) { 1249 memory_exception_data.va = (info->page_addr) << 1250 PAGE_SHIFT; 1251 memory_exception_data.failure.NotPresent = 1252 info->prot_valid ? 1 : 0; 1253 memory_exception_data.failure.NoExecute = 1254 info->prot_exec ? 1 : 0; 1255 memory_exception_data.failure.ReadOnly = 1256 info->prot_write ? 1 : 0; 1257 memory_exception_data.failure.imprecise = 0; 1258 } 1259 } 1260 1261 rcu_read_lock(); 1262 1263 id = KFD_FIRST_NONSIGNAL_EVENT_ID; 1264 idr_for_each_entry_continue(&p->event_idr, ev, id) 1265 if (ev->type == KFD_EVENT_TYPE_MEMORY) { 1266 spin_lock(&ev->lock); 1267 ev->memory_exception_data = data ? *data : 1268 memory_exception_data; 1269 set_event(ev); 1270 spin_unlock(&ev->lock); 1271 } 1272 1273 rcu_read_unlock(); 1274 } 1275 1276 void kfd_signal_reset_event(struct kfd_node *dev) 1277 { 1278 struct kfd_hsa_hw_exception_data hw_exception_data; 1279 struct kfd_hsa_memory_exception_data memory_exception_data; 1280 struct kfd_process *p; 1281 struct kfd_event *ev; 1282 unsigned int temp; 1283 uint32_t id, idx; 1284 int reset_cause = atomic_read(&dev->sram_ecc_flag) ? 1285 KFD_HW_EXCEPTION_ECC : 1286 KFD_HW_EXCEPTION_GPU_HANG; 1287 1288 /* Whole gpu reset caused by GPU hang and memory is lost */ 1289 memset(&hw_exception_data, 0, sizeof(hw_exception_data)); 1290 hw_exception_data.memory_lost = 1; 1291 hw_exception_data.reset_cause = reset_cause; 1292 1293 memset(&memory_exception_data, 0, sizeof(memory_exception_data)); 1294 memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC; 1295 memory_exception_data.failure.imprecise = true; 1296 1297 idx = srcu_read_lock(&kfd_processes_srcu); 1298 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) { 1299 int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id); 1300 struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p); 1301 1302 if (unlikely(user_gpu_id == -EINVAL)) { 1303 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id); 1304 continue; 1305 } 1306 1307 if (unlikely(!pdd)) { 1308 WARN_ONCE(1, "Could not get device data from process pid:%d\n", 1309 p->lead_thread->pid); 1310 continue; 1311 } 1312 1313 if (dev->dqm->detect_hang_count && !pdd->has_reset_queue) 1314 continue; 1315 1316 if (dev->dqm->detect_hang_count) { 1317 struct amdgpu_task_info *ti; 1318 struct amdgpu_fpriv *drv_priv; 1319 1320 if (unlikely(amdgpu_file_to_fpriv(pdd->drm_file, &drv_priv))) { 1321 WARN_ONCE(1, "Could not get vm for device %x from pid:%d\n", 1322 dev->id, p->lead_thread->pid); 1323 continue; 1324 } 1325 1326 ti = amdgpu_vm_get_task_info_vm(&drv_priv->vm); 1327 if (ti) { 1328 dev_err(dev->adev->dev, 1329 "Queues reset on process %s tid %d thread %s pid %d\n", 1330 ti->process_name, ti->tgid, ti->task.comm, ti->task.pid); 1331 amdgpu_vm_put_task_info(ti); 1332 } 1333 } 1334 1335 rcu_read_lock(); 1336 1337 id = KFD_FIRST_NONSIGNAL_EVENT_ID; 1338 idr_for_each_entry_continue(&p->event_idr, ev, id) { 1339 if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) { 1340 spin_lock(&ev->lock); 1341 ev->hw_exception_data = hw_exception_data; 1342 ev->hw_exception_data.gpu_id = user_gpu_id; 1343 set_event(ev); 1344 spin_unlock(&ev->lock); 1345 } 1346 if (ev->type == KFD_EVENT_TYPE_MEMORY && 1347 reset_cause == KFD_HW_EXCEPTION_ECC) { 1348 spin_lock(&ev->lock); 1349 ev->memory_exception_data = memory_exception_data; 1350 ev->memory_exception_data.gpu_id = user_gpu_id; 1351 set_event(ev); 1352 spin_unlock(&ev->lock); 1353 } 1354 } 1355 1356 rcu_read_unlock(); 1357 } 1358 srcu_read_unlock(&kfd_processes_srcu, idx); 1359 } 1360 1361 void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid) 1362 { 1363 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL); 1364 struct kfd_hsa_memory_exception_data memory_exception_data; 1365 struct kfd_hsa_hw_exception_data hw_exception_data; 1366 struct kfd_event *ev; 1367 uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID; 1368 int user_gpu_id; 1369 1370 if (!p) { 1371 dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid); 1372 return; /* Presumably process exited. */ 1373 } 1374 1375 user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id); 1376 if (unlikely(user_gpu_id == -EINVAL)) { 1377 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id); 1378 kfd_unref_process(p); 1379 return; 1380 } 1381 1382 memset(&hw_exception_data, 0, sizeof(hw_exception_data)); 1383 hw_exception_data.gpu_id = user_gpu_id; 1384 hw_exception_data.memory_lost = 1; 1385 hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC; 1386 1387 memset(&memory_exception_data, 0, sizeof(memory_exception_data)); 1388 memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED; 1389 memory_exception_data.gpu_id = user_gpu_id; 1390 memory_exception_data.failure.imprecise = true; 1391 1392 rcu_read_lock(); 1393 1394 idr_for_each_entry_continue(&p->event_idr, ev, id) { 1395 if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) { 1396 spin_lock(&ev->lock); 1397 ev->hw_exception_data = hw_exception_data; 1398 set_event(ev); 1399 spin_unlock(&ev->lock); 1400 } 1401 1402 if (ev->type == KFD_EVENT_TYPE_MEMORY) { 1403 spin_lock(&ev->lock); 1404 ev->memory_exception_data = memory_exception_data; 1405 set_event(ev); 1406 spin_unlock(&ev->lock); 1407 } 1408 } 1409 1410 dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n", 1411 p->lead_thread->comm, pasid); 1412 rcu_read_unlock(); 1413 1414 /* user application will handle SIGBUS signal */ 1415 send_sig(SIGBUS, p->lead_thread, 0); 1416 1417 kfd_unref_process(p); 1418 } 1419 1420 /* signal KFD_EVENT_TYPE_SIGNAL events from process p 1421 * send signal SIGBUS to correspondent user space process 1422 */ 1423 void kfd_signal_process_terminate_event(struct kfd_process *p) 1424 { 1425 struct kfd_event *ev; 1426 u32 id; 1427 1428 rcu_read_lock(); 1429 1430 /* iterate from id 1 for KFD_EVENT_TYPE_SIGNAL events */ 1431 id = 1; 1432 idr_for_each_entry_continue(&p->event_idr, ev, id) 1433 if (ev->type == KFD_EVENT_TYPE_SIGNAL) { 1434 spin_lock(&ev->lock); 1435 set_event(ev); 1436 spin_unlock(&ev->lock); 1437 } 1438 1439 /* Send SIGBUS to p->lead_thread */ 1440 dev_notice(kfd_device, 1441 "Sending SIGBUS to process %d", 1442 p->lead_thread->pid); 1443 1444 send_sig(SIGBUS, p->lead_thread, 0); 1445 1446 rcu_read_unlock(); 1447 } 1448