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