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