1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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>
33 #include "kfd_events.h"
34 #include "kfd_device_queue_manager.h"
35 #include <linux/device.h>
38 * Wrapper around wait_queue_entry_t
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 */
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.
54 struct kfd_signal_page {
55 uint64_t *kernel_address;
56 uint64_t __user *user_address;
57 bool need_to_free_pages;
60 static uint64_t *page_slots(struct kfd_signal_page *page)
62 return page->kernel_address;
65 static struct kfd_signal_page *allocate_signal_page(struct kfd_process *p)
68 struct kfd_signal_page *page;
70 page = kzalloc(sizeof(*page), GFP_KERNEL);
74 backing_store = (void *) __get_free_pages(GFP_KERNEL,
75 get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
77 goto fail_alloc_signal_store;
79 /* Initialize all events to unsignaled */
80 memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT,
81 KFD_SIGNAL_EVENT_LIMIT * 8);
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",
90 fail_alloc_signal_store:
95 static int allocate_event_notification_slot(struct kfd_process *p,
97 const int *restore_id)
101 if (!p->signal_page) {
102 p->signal_page = allocate_signal_page(p);
105 /* Oldest user mode expects 256 event slots */
106 p->signal_mapped_size = 256*8;
110 id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
114 * Compatibility with old user mode: Only use signal slots
115 * user mode has mapped, may be less than
116 * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase
117 * of the event limit without breaking user mode.
119 id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8,
126 page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT;
132 * Assumes that p->event_mutex or rcu_readlock is held and of course that p is
135 static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)
137 return idr_find(&p->event_idr, id);
141 * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID
142 * @p: Pointer to struct kfd_process
144 * @bits: Number of valid bits in @id
146 * Finds the first signaled event with a matching partial ID. If no
147 * matching signaled event is found, returns NULL. In that case the
148 * caller should assume that the partial ID is invalid and do an
149 * exhaustive search of all siglaned events.
151 * If multiple events with the same partial ID signal at the same
152 * time, they will be found one interrupt at a time, not necessarily
153 * in the same order the interrupts occurred. As long as the number of
154 * interrupts is correct, all signaled events will be seen by the
157 static struct kfd_event *lookup_signaled_event_by_partial_id(
158 struct kfd_process *p, uint32_t id, uint32_t bits)
160 struct kfd_event *ev;
162 if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT)
165 /* Fast path for the common case that @id is not a partial ID
166 * and we only need a single lookup.
168 if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) {
169 if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
172 return idr_find(&p->event_idr, id);
175 /* General case for partial IDs: Iterate over all matching IDs
176 * and find the first one that has signaled.
178 for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) {
179 if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
182 ev = idr_find(&p->event_idr, id);
188 static int create_signal_event(struct file *devkfd, struct kfd_process *p,
189 struct kfd_event *ev, const int *restore_id)
193 if (p->signal_mapped_size &&
194 p->signal_event_count == p->signal_mapped_size / 8) {
195 if (!p->signal_event_limit_reached) {
196 pr_debug("Signal event wasn't created because limit was reached\n");
197 p->signal_event_limit_reached = true;
202 ret = allocate_event_notification_slot(p, ev, restore_id);
204 pr_warn("Signal event wasn't created because out of kernel memory\n");
208 p->signal_event_count++;
210 ev->user_signal_address = &p->signal_page->user_address[ev->event_id];
211 pr_debug("Signal event number %zu created with id %d, address %p\n",
212 p->signal_event_count, ev->event_id,
213 ev->user_signal_address);
218 static int create_other_event(struct kfd_process *p, struct kfd_event *ev, const int *restore_id)
223 id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
226 /* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an
227 * intentional integer overflow to -1 without a compiler
228 * warning. idr_alloc treats a negative value as "maximum
231 id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID,
232 (uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1,
242 int kfd_event_init_process(struct kfd_process *p)
246 mutex_init(&p->event_mutex);
247 idr_init(&p->event_idr);
248 p->signal_page = NULL;
249 p->signal_event_count = 1;
250 /* Allocate event ID 0. It is used for a fast path to ignore bogus events
251 * that are sent by the CP without a context ID
253 id = idr_alloc(&p->event_idr, NULL, 0, 1, GFP_KERNEL);
255 idr_destroy(&p->event_idr);
256 mutex_destroy(&p->event_mutex);
262 static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
264 struct kfd_event_waiter *waiter;
266 /* Wake up pending waiters. They will return failure */
267 spin_lock(&ev->lock);
268 list_for_each_entry(waiter, &ev->wq.head, wait.entry)
269 WRITE_ONCE(waiter->event, NULL);
270 wake_up_all(&ev->wq);
271 spin_unlock(&ev->lock);
273 if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
274 ev->type == KFD_EVENT_TYPE_DEBUG)
275 p->signal_event_count--;
277 idr_remove(&p->event_idr, ev->event_id);
281 static void destroy_events(struct kfd_process *p)
283 struct kfd_event *ev;
286 idr_for_each_entry(&p->event_idr, ev, id)
288 destroy_event(p, ev);
289 idr_destroy(&p->event_idr);
290 mutex_destroy(&p->event_mutex);
294 * We assume that the process is being destroyed and there is no need to
295 * unmap the pages or keep bookkeeping data in order.
297 static void shutdown_signal_page(struct kfd_process *p)
299 struct kfd_signal_page *page = p->signal_page;
302 if (page->need_to_free_pages)
303 free_pages((unsigned long)page->kernel_address,
304 get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
309 void kfd_event_free_process(struct kfd_process *p)
312 shutdown_signal_page(p);
315 static bool event_can_be_gpu_signaled(const struct kfd_event *ev)
317 return ev->type == KFD_EVENT_TYPE_SIGNAL ||
318 ev->type == KFD_EVENT_TYPE_DEBUG;
321 static bool event_can_be_cpu_signaled(const struct kfd_event *ev)
323 return ev->type == KFD_EVENT_TYPE_SIGNAL;
326 static int kfd_event_page_set(struct kfd_process *p, void *kernel_address,
327 uint64_t size, uint64_t user_handle)
329 struct kfd_signal_page *page;
334 page = kzalloc(sizeof(*page), GFP_KERNEL);
338 /* Initialize all events to unsignaled */
339 memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT,
340 KFD_SIGNAL_EVENT_LIMIT * 8);
342 page->kernel_address = kernel_address;
344 p->signal_page = page;
345 p->signal_mapped_size = size;
346 p->signal_handle = user_handle;
350 int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset)
352 struct kfd_node *kfd;
353 struct kfd_process_device *pdd;
354 void *mem, *kern_addr;
358 if (p->signal_page) {
359 pr_err("Event page is already set\n");
363 pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(event_page_offset));
365 pr_err("Getting device by id failed in %s\n", __func__);
370 pdd = kfd_bind_process_to_device(kfd, p);
374 mem = kfd_process_device_translate_handle(pdd,
375 GET_IDR_HANDLE(event_page_offset));
377 pr_err("Can't find BO, offset is 0x%llx\n", event_page_offset);
381 err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(mem, &kern_addr, &size);
383 pr_err("Failed to map event page to kernel\n");
387 err = kfd_event_page_set(p, kern_addr, size, event_page_offset);
389 pr_err("Failed to set event page\n");
390 amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);
396 int kfd_event_create(struct file *devkfd, struct kfd_process *p,
397 uint32_t event_type, bool auto_reset, uint32_t node_id,
398 uint32_t *event_id, uint32_t *event_trigger_data,
399 uint64_t *event_page_offset, uint32_t *event_slot_index)
402 struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);
407 ev->type = event_type;
408 ev->auto_reset = auto_reset;
409 ev->signaled = false;
411 spin_lock_init(&ev->lock);
412 init_waitqueue_head(&ev->wq);
414 *event_page_offset = 0;
416 mutex_lock(&p->event_mutex);
418 switch (event_type) {
419 case KFD_EVENT_TYPE_SIGNAL:
420 case KFD_EVENT_TYPE_DEBUG:
421 ret = create_signal_event(devkfd, p, ev, NULL);
423 *event_page_offset = KFD_MMAP_TYPE_EVENTS;
424 *event_slot_index = ev->event_id;
428 ret = create_other_event(p, ev, NULL);
433 *event_id = ev->event_id;
434 *event_trigger_data = ev->event_id;
440 mutex_unlock(&p->event_mutex);
445 int kfd_criu_restore_event(struct file *devkfd,
446 struct kfd_process *p,
447 uint8_t __user *user_priv_ptr,
448 uint64_t *priv_data_offset,
449 uint64_t max_priv_data_size)
451 struct kfd_criu_event_priv_data *ev_priv;
452 struct kfd_event *ev = NULL;
455 ev_priv = kmalloc(sizeof(*ev_priv), GFP_KERNEL);
459 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
465 if (*priv_data_offset + sizeof(*ev_priv) > max_priv_data_size) {
470 ret = copy_from_user(ev_priv, user_priv_ptr + *priv_data_offset, sizeof(*ev_priv));
475 *priv_data_offset += sizeof(*ev_priv);
477 if (ev_priv->user_handle) {
478 ret = kfd_kmap_event_page(p, ev_priv->user_handle);
483 ev->type = ev_priv->type;
484 ev->auto_reset = ev_priv->auto_reset;
485 ev->signaled = ev_priv->signaled;
487 spin_lock_init(&ev->lock);
488 init_waitqueue_head(&ev->wq);
490 mutex_lock(&p->event_mutex);
492 case KFD_EVENT_TYPE_SIGNAL:
493 case KFD_EVENT_TYPE_DEBUG:
494 ret = create_signal_event(devkfd, p, ev, &ev_priv->event_id);
496 case KFD_EVENT_TYPE_MEMORY:
497 memcpy(&ev->memory_exception_data,
498 &ev_priv->memory_exception_data,
499 sizeof(struct kfd_hsa_memory_exception_data));
501 ret = create_other_event(p, ev, &ev_priv->event_id);
503 case KFD_EVENT_TYPE_HW_EXCEPTION:
504 memcpy(&ev->hw_exception_data,
505 &ev_priv->hw_exception_data,
506 sizeof(struct kfd_hsa_hw_exception_data));
508 ret = create_other_event(p, ev, &ev_priv->event_id);
511 mutex_unlock(&p->event_mutex);
522 int kfd_criu_checkpoint_events(struct kfd_process *p,
523 uint8_t __user *user_priv_data,
524 uint64_t *priv_data_offset)
526 struct kfd_criu_event_priv_data *ev_privs;
529 struct kfd_event *ev;
532 uint32_t num_events = kfd_get_num_events(p);
537 ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL);
542 idr_for_each_entry(&p->event_idr, ev, ev_id) {
543 struct kfd_criu_event_priv_data *ev_priv;
546 * Currently, all events have same size of private_data, but the current ioctl's
547 * and CRIU plugin supports private_data of variable sizes
549 ev_priv = &ev_privs[i];
551 ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT;
553 /* We store the user_handle with the first event */
554 if (i == 0 && p->signal_page)
555 ev_priv->user_handle = p->signal_handle;
557 ev_priv->event_id = ev->event_id;
558 ev_priv->auto_reset = ev->auto_reset;
559 ev_priv->type = ev->type;
560 ev_priv->signaled = ev->signaled;
562 if (ev_priv->type == KFD_EVENT_TYPE_MEMORY)
563 memcpy(&ev_priv->memory_exception_data,
564 &ev->memory_exception_data,
565 sizeof(struct kfd_hsa_memory_exception_data));
566 else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION)
567 memcpy(&ev_priv->hw_exception_data,
568 &ev->hw_exception_data,
569 sizeof(struct kfd_hsa_hw_exception_data));
571 pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n",
580 ret = copy_to_user(user_priv_data + *priv_data_offset,
581 ev_privs, num_events * sizeof(*ev_privs));
583 pr_err("Failed to copy events priv to user\n");
587 *priv_data_offset += num_events * sizeof(*ev_privs);
593 int kfd_get_num_events(struct kfd_process *p)
595 struct kfd_event *ev;
599 idr_for_each_entry(&p->event_idr, ev, id)
605 /* Assumes that p is current. */
606 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
608 struct kfd_event *ev;
611 mutex_lock(&p->event_mutex);
613 ev = lookup_event_by_id(p, event_id);
616 destroy_event(p, ev);
620 mutex_unlock(&p->event_mutex);
624 static void set_event(struct kfd_event *ev)
626 struct kfd_event_waiter *waiter;
628 /* Auto reset if the list is non-empty and we're waking
629 * someone. waitqueue_active is safe here because we're
630 * protected by the ev->lock, which is also held when
631 * updating the wait queues in kfd_wait_on_events.
633 ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
634 if (!(++ev->event_age)) {
635 /* Never wrap back to reserved/default event age 0/1 */
637 WARN_ONCE(1, "event_age wrap back!");
640 list_for_each_entry(waiter, &ev->wq.head, wait.entry)
641 WRITE_ONCE(waiter->activated, true);
643 wake_up_all(&ev->wq);
646 /* Assumes that p is current. */
647 int kfd_set_event(struct kfd_process *p, uint32_t event_id)
650 struct kfd_event *ev;
654 ev = lookup_event_by_id(p, event_id);
659 spin_lock(&ev->lock);
661 if (event_can_be_cpu_signaled(ev))
666 spin_unlock(&ev->lock);
672 static void reset_event(struct kfd_event *ev)
674 ev->signaled = false;
677 /* Assumes that p is current. */
678 int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
681 struct kfd_event *ev;
685 ev = lookup_event_by_id(p, event_id);
690 spin_lock(&ev->lock);
692 if (event_can_be_cpu_signaled(ev))
697 spin_unlock(&ev->lock);
704 static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
706 WRITE_ONCE(page_slots(p->signal_page)[ev->event_id], UNSIGNALED_EVENT_SLOT);
709 static void set_event_from_interrupt(struct kfd_process *p,
710 struct kfd_event *ev)
712 if (ev && event_can_be_gpu_signaled(ev)) {
713 acknowledge_signal(p, ev);
714 spin_lock(&ev->lock);
716 spin_unlock(&ev->lock);
720 void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
721 uint32_t valid_id_bits)
723 struct kfd_event *ev = NULL;
726 * Because we are called from arbitrary context (workqueue) as opposed
727 * to process context, kfd_process could attempt to exit while we are
728 * running so the lookup function increments the process ref count.
730 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
733 return; /* Presumably process exited. */
738 ev = lookup_signaled_event_by_partial_id(p, partial_id,
741 set_event_from_interrupt(p, ev);
742 } else if (p->signal_page) {
744 * Partial ID lookup failed. Assume that the event ID
745 * in the interrupt payload was invalid and do an
746 * exhaustive search of signaled events.
748 uint64_t *slots = page_slots(p->signal_page);
752 pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",
753 partial_id, valid_id_bits);
755 if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {
756 /* With relatively few events, it's faster to
757 * iterate over the event IDR
759 idr_for_each_entry(&p->event_idr, ev, id) {
760 if (id >= KFD_SIGNAL_EVENT_LIMIT)
763 if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)
764 set_event_from_interrupt(p, ev);
767 /* With relatively many events, it's faster to
768 * iterate over the signal slots and lookup
769 * only signaled events from the IDR.
771 for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)
772 if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {
773 ev = lookup_event_by_id(p, id);
774 set_event_from_interrupt(p, ev);
780 kfd_unref_process(p);
783 static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
785 struct kfd_event_waiter *event_waiters;
788 event_waiters = kcalloc(num_events, sizeof(struct kfd_event_waiter),
793 for (i = 0; i < num_events; i++)
794 init_wait(&event_waiters[i].wait);
796 return event_waiters;
799 static int init_event_waiter(struct kfd_process *p,
800 struct kfd_event_waiter *waiter,
801 struct kfd_event_data *event_data)
803 struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id);
808 spin_lock(&ev->lock);
810 waiter->activated = ev->signaled;
811 ev->signaled = ev->signaled && !ev->auto_reset;
813 /* last_event_age = 0 reserved for backward compatible */
814 if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL &&
815 event_data->signal_event_data.last_event_age) {
816 waiter->event_age_enabled = true;
817 if (ev->event_age != event_data->signal_event_data.last_event_age)
818 waiter->activated = true;
821 if (!waiter->activated)
822 add_wait_queue(&ev->wq, &waiter->wait);
823 spin_unlock(&ev->lock);
828 /* test_event_condition - Test condition of events being waited for
829 * @all: Return completion only if all events have signaled
830 * @num_events: Number of events to wait for
831 * @event_waiters: Array of event waiters, one per event
833 * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have
834 * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)
835 * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of
836 * the events have been destroyed.
838 static uint32_t test_event_condition(bool all, uint32_t num_events,
839 struct kfd_event_waiter *event_waiters)
842 uint32_t activated_count = 0;
844 for (i = 0; i < num_events; i++) {
845 if (!READ_ONCE(event_waiters[i].event))
846 return KFD_IOC_WAIT_RESULT_FAIL;
848 if (READ_ONCE(event_waiters[i].activated)) {
850 return KFD_IOC_WAIT_RESULT_COMPLETE;
856 return activated_count == num_events ?
857 KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;
861 * Copy event specific data, if defined.
862 * Currently only memory exception events have additional data to copy to user
864 static int copy_signaled_event_data(uint32_t num_events,
865 struct kfd_event_waiter *event_waiters,
866 struct kfd_event_data __user *data)
870 struct kfd_event_waiter *waiter;
871 struct kfd_event *event;
872 uint32_t i, size = 0;
874 for (i = 0; i < num_events; i++) {
875 waiter = &event_waiters[i];
876 event = waiter->event;
878 return -EINVAL; /* event was destroyed */
879 if (waiter->activated) {
880 if (event->type == KFD_EVENT_TYPE_MEMORY) {
881 dst = &data[i].memory_exception_data;
882 src = &event->memory_exception_data;
883 size = sizeof(struct kfd_hsa_memory_exception_data);
884 } else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
885 dst = &data[i].memory_exception_data;
886 src = &event->hw_exception_data;
887 size = sizeof(struct kfd_hsa_hw_exception_data);
888 } else if (event->type == KFD_EVENT_TYPE_SIGNAL &&
889 waiter->event_age_enabled) {
890 dst = &data[i].signal_event_data.last_event_age;
891 src = &event->event_age;
894 if (size && copy_to_user(dst, src, size))
902 static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
904 if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
907 if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
908 return MAX_SCHEDULE_TIMEOUT;
911 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
912 * but we consider them finite.
913 * This hack is wrong, but nobody is likely to notice.
915 user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
917 return msecs_to_jiffies(user_timeout_ms) + 1;
920 static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,
921 bool undo_auto_reset)
925 for (i = 0; i < num_events; i++)
926 if (waiters[i].event) {
927 spin_lock(&waiters[i].event->lock);
928 remove_wait_queue(&waiters[i].event->wq,
930 if (undo_auto_reset && waiters[i].activated &&
931 waiters[i].event && waiters[i].event->auto_reset)
932 set_event(waiters[i].event);
933 spin_unlock(&waiters[i].event->lock);
939 int kfd_wait_on_events(struct kfd_process *p,
940 uint32_t num_events, void __user *data,
941 bool all, uint32_t *user_timeout_ms,
942 uint32_t *wait_result)
944 struct kfd_event_data __user *events =
945 (struct kfd_event_data __user *) data;
949 struct kfd_event_waiter *event_waiters = NULL;
950 long timeout = user_timeout_to_jiffies(*user_timeout_ms);
952 event_waiters = alloc_event_waiters(num_events);
953 if (!event_waiters) {
958 /* Use p->event_mutex here to protect against concurrent creation and
959 * destruction of events while we initialize event_waiters.
961 mutex_lock(&p->event_mutex);
963 for (i = 0; i < num_events; i++) {
964 struct kfd_event_data event_data;
966 if (copy_from_user(&event_data, &events[i],
967 sizeof(struct kfd_event_data))) {
972 ret = init_event_waiter(p, &event_waiters[i], &event_data);
977 /* Check condition once. */
978 *wait_result = test_event_condition(all, num_events, event_waiters);
979 if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {
980 ret = copy_signaled_event_data(num_events,
981 event_waiters, events);
983 } else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {
984 /* This should not happen. Events shouldn't be
985 * destroyed while we're holding the event_mutex
990 mutex_unlock(&p->event_mutex);
993 if (fatal_signal_pending(current)) {
998 if (signal_pending(current)) {
1000 if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&
1001 *user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)
1002 *user_timeout_ms = jiffies_to_msecs(
1003 max(0l, timeout-1));
1007 /* Set task state to interruptible sleep before
1008 * checking wake-up conditions. A concurrent wake-up
1009 * will put the task back into runnable state. In that
1010 * case schedule_timeout will not put the task to
1011 * sleep and we'll get a chance to re-check the
1012 * updated conditions almost immediately. Otherwise,
1013 * this race condition would lead to a soft hang or a
1016 set_current_state(TASK_INTERRUPTIBLE);
1018 *wait_result = test_event_condition(all, num_events,
1020 if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)
1026 timeout = schedule_timeout(timeout);
1028 __set_current_state(TASK_RUNNING);
1030 mutex_lock(&p->event_mutex);
1031 /* copy_signaled_event_data may sleep. So this has to happen
1032 * after the task state is set back to RUNNING.
1034 * The event may also have been destroyed after signaling. So
1035 * copy_signaled_event_data also must confirm that the event
1036 * still exists. Therefore this must be under the p->event_mutex
1037 * which is also held when events are destroyed.
1039 if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)
1040 ret = copy_signaled_event_data(num_events,
1041 event_waiters, events);
1044 free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);
1045 mutex_unlock(&p->event_mutex);
1048 *wait_result = KFD_IOC_WAIT_RESULT_FAIL;
1049 else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)
1055 int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
1058 struct kfd_signal_page *page;
1061 /* check required size doesn't exceed the allocated size */
1062 if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <
1063 get_order(vma->vm_end - vma->vm_start)) {
1064 pr_err("Event page mmap requested illegal size\n");
1068 page = p->signal_page;
1070 /* Probably KFD bug, but mmap is user-accessible. */
1071 pr_debug("Signal page could not be found\n");
1075 pfn = __pa(page->kernel_address);
1078 vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
1079 | VM_DONTDUMP | VM_PFNMAP);
1081 pr_debug("Mapping signal page\n");
1082 pr_debug(" start user address == 0x%08lx\n", vma->vm_start);
1083 pr_debug(" end user address == 0x%08lx\n", vma->vm_end);
1084 pr_debug(" pfn == 0x%016lX\n", pfn);
1085 pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags);
1086 pr_debug(" size == 0x%08lX\n",
1087 vma->vm_end - vma->vm_start);
1089 page->user_address = (uint64_t __user *)vma->vm_start;
1091 /* mapping the page to user process */
1092 ret = remap_pfn_range(vma, vma->vm_start, pfn,
1093 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1095 p->signal_mapped_size = vma->vm_end - vma->vm_start;
1101 * Assumes that p is not going away.
1103 static void lookup_events_by_type_and_signal(struct kfd_process *p,
1104 int type, void *event_data)
1106 struct kfd_hsa_memory_exception_data *ev_data;
1107 struct kfd_event *ev;
1109 bool send_signal = true;
1111 ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
1115 id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1116 idr_for_each_entry_continue(&p->event_idr, ev, id)
1117 if (ev->type == type) {
1118 send_signal = false;
1120 "Event found: id %X type %d",
1121 ev->event_id, ev->type);
1122 spin_lock(&ev->lock);
1124 if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
1125 ev->memory_exception_data = *ev_data;
1126 spin_unlock(&ev->lock);
1129 if (type == KFD_EVENT_TYPE_MEMORY) {
1130 dev_warn(kfd_device,
1131 "Sending SIGSEGV to process %d (pasid 0x%x)",
1132 p->lead_thread->pid, p->pasid);
1133 send_sig(SIGSEGV, p->lead_thread, 0);
1136 /* Send SIGTERM no event of type "type" has been found*/
1139 dev_warn(kfd_device,
1140 "Sending SIGTERM to process %d (pasid 0x%x)",
1141 p->lead_thread->pid, p->pasid);
1142 send_sig(SIGTERM, p->lead_thread, 0);
1145 "Process %d (pasid 0x%x) got unhandled exception",
1146 p->lead_thread->pid, p->pasid);
1153 void kfd_signal_hw_exception_event(u32 pasid)
1156 * Because we are called from arbitrary context (workqueue) as opposed
1157 * to process context, kfd_process could attempt to exit while we are
1158 * running so the lookup function increments the process ref count.
1160 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1163 return; /* Presumably process exited. */
1165 lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
1166 kfd_unref_process(p);
1169 void kfd_signal_vm_fault_event(struct kfd_node *dev, u32 pasid,
1170 struct kfd_vm_fault_info *info,
1171 struct kfd_hsa_memory_exception_data *data)
1173 struct kfd_event *ev;
1175 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1176 struct kfd_hsa_memory_exception_data memory_exception_data;
1180 return; /* Presumably process exited. */
1182 user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1183 if (unlikely(user_gpu_id == -EINVAL)) {
1184 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1188 /* SoC15 chips and onwards will pass in data from now on. */
1190 memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1191 memory_exception_data.gpu_id = user_gpu_id;
1192 memory_exception_data.failure.imprecise = true;
1194 /* Set failure reason */
1196 memory_exception_data.va = (info->page_addr) <<
1198 memory_exception_data.failure.NotPresent =
1199 info->prot_valid ? 1 : 0;
1200 memory_exception_data.failure.NoExecute =
1201 info->prot_exec ? 1 : 0;
1202 memory_exception_data.failure.ReadOnly =
1203 info->prot_write ? 1 : 0;
1204 memory_exception_data.failure.imprecise = 0;
1210 id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1211 idr_for_each_entry_continue(&p->event_idr, ev, id)
1212 if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1213 spin_lock(&ev->lock);
1214 ev->memory_exception_data = data ? *data :
1215 memory_exception_data;
1217 spin_unlock(&ev->lock);
1221 kfd_unref_process(p);
1224 void kfd_signal_reset_event(struct kfd_node *dev)
1226 struct kfd_hsa_hw_exception_data hw_exception_data;
1227 struct kfd_hsa_memory_exception_data memory_exception_data;
1228 struct kfd_process *p;
1229 struct kfd_event *ev;
1232 int reset_cause = atomic_read(&dev->sram_ecc_flag) ?
1233 KFD_HW_EXCEPTION_ECC :
1234 KFD_HW_EXCEPTION_GPU_HANG;
1236 /* Whole gpu reset caused by GPU hang and memory is lost */
1237 memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1238 hw_exception_data.memory_lost = 1;
1239 hw_exception_data.reset_cause = reset_cause;
1241 memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1242 memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;
1243 memory_exception_data.failure.imprecise = true;
1245 idx = srcu_read_lock(&kfd_processes_srcu);
1246 hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1247 int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1248 struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p);
1250 if (unlikely(user_gpu_id == -EINVAL)) {
1251 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1255 if (unlikely(!pdd)) {
1256 WARN_ONCE(1, "Could not get device data from pasid:0x%x\n", p->pasid);
1260 if (dev->dqm->detect_hang_count && !pdd->has_reset_queue)
1263 if (dev->dqm->detect_hang_count) {
1264 struct amdgpu_task_info *ti;
1266 ti = amdgpu_vm_get_task_info_pasid(dev->adev, p->pasid);
1268 dev_err(dev->adev->dev,
1269 "Queues reset on process %s tid %d thread %s pid %d\n",
1270 ti->process_name, ti->tgid, ti->task_name, ti->pid);
1271 amdgpu_vm_put_task_info(ti);
1277 id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1278 idr_for_each_entry_continue(&p->event_idr, ev, id) {
1279 if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1280 spin_lock(&ev->lock);
1281 ev->hw_exception_data = hw_exception_data;
1282 ev->hw_exception_data.gpu_id = user_gpu_id;
1284 spin_unlock(&ev->lock);
1286 if (ev->type == KFD_EVENT_TYPE_MEMORY &&
1287 reset_cause == KFD_HW_EXCEPTION_ECC) {
1288 spin_lock(&ev->lock);
1289 ev->memory_exception_data = memory_exception_data;
1290 ev->memory_exception_data.gpu_id = user_gpu_id;
1292 spin_unlock(&ev->lock);
1298 srcu_read_unlock(&kfd_processes_srcu, idx);
1301 void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)
1303 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1304 struct kfd_hsa_memory_exception_data memory_exception_data;
1305 struct kfd_hsa_hw_exception_data hw_exception_data;
1306 struct kfd_event *ev;
1307 uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1311 dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid);
1312 return; /* Presumably process exited. */
1315 user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1316 if (unlikely(user_gpu_id == -EINVAL)) {
1317 WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1321 memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1322 hw_exception_data.gpu_id = user_gpu_id;
1323 hw_exception_data.memory_lost = 1;
1324 hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;
1326 memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1327 memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;
1328 memory_exception_data.gpu_id = user_gpu_id;
1329 memory_exception_data.failure.imprecise = true;
1333 idr_for_each_entry_continue(&p->event_idr, ev, id) {
1334 if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1335 spin_lock(&ev->lock);
1336 ev->hw_exception_data = hw_exception_data;
1338 spin_unlock(&ev->lock);
1341 if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1342 spin_lock(&ev->lock);
1343 ev->memory_exception_data = memory_exception_data;
1345 spin_unlock(&ev->lock);
1349 dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n",
1350 p->lead_thread->comm, pasid);
1353 /* user application will handle SIGBUS signal */
1354 send_sig(SIGBUS, p->lead_thread, 0);
1356 kfd_unref_process(p);