xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_events.c (revision 81aa6f190aa0e9ff752fcd0a9eda676458e23133)
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 	}
456 	mutex_unlock(&p->event_mutex);
457 
458 exit:
459 	if (ret)
460 		kfree(ev);
461 
462 	kfree(ev_priv);
463 
464 	return ret;
465 }
466 
467 int kfd_criu_checkpoint_events(struct kfd_process *p,
468 			 uint8_t __user *user_priv_data,
469 			 uint64_t *priv_data_offset)
470 {
471 	struct kfd_criu_event_priv_data *ev_privs;
472 	int i = 0;
473 	int ret =  0;
474 	struct kfd_event *ev;
475 	uint32_t ev_id;
476 
477 	uint32_t num_events = kfd_get_num_events(p);
478 
479 	if (!num_events)
480 		return 0;
481 
482 	ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL);
483 	if (!ev_privs)
484 		return -ENOMEM;
485 
486 
487 	idr_for_each_entry(&p->event_idr, ev, ev_id) {
488 		struct kfd_criu_event_priv_data *ev_priv;
489 
490 		/*
491 		 * Currently, all events have same size of private_data, but the current ioctl's
492 		 * and CRIU plugin supports private_data of variable sizes
493 		 */
494 		ev_priv = &ev_privs[i];
495 
496 		ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT;
497 
498 		/* We store the user_handle with the first event */
499 		if (i == 0 && p->signal_page)
500 			ev_priv->user_handle = p->signal_handle;
501 
502 		ev_priv->event_id = ev->event_id;
503 		ev_priv->auto_reset = ev->auto_reset;
504 		ev_priv->type = ev->type;
505 		ev_priv->signaled = ev->signaled;
506 
507 		if (ev_priv->type == KFD_EVENT_TYPE_MEMORY)
508 			memcpy(&ev_priv->memory_exception_data,
509 				&ev->memory_exception_data,
510 				sizeof(struct kfd_hsa_memory_exception_data));
511 		else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION)
512 			memcpy(&ev_priv->hw_exception_data,
513 				&ev->hw_exception_data,
514 				sizeof(struct kfd_hsa_hw_exception_data));
515 
516 		pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n",
517 			  i,
518 			  ev_priv->event_id,
519 			  ev_priv->auto_reset,
520 			  ev_priv->type,
521 			  ev_priv->signaled);
522 		i++;
523 	}
524 
525 	ret = copy_to_user(user_priv_data + *priv_data_offset,
526 			   ev_privs, num_events * sizeof(*ev_privs));
527 	if (ret) {
528 		pr_err("Failed to copy events priv to user\n");
529 		ret = -EFAULT;
530 	}
531 
532 	*priv_data_offset += num_events * sizeof(*ev_privs);
533 
534 	kvfree(ev_privs);
535 	return ret;
536 }
537 
538 int kfd_get_num_events(struct kfd_process *p)
539 {
540 	struct kfd_event *ev;
541 	uint32_t id;
542 	u32 num_events = 0;
543 
544 	idr_for_each_entry(&p->event_idr, ev, id)
545 		num_events++;
546 
547 	return num_events;
548 }
549 
550 /* Assumes that p is current. */
551 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
552 {
553 	struct kfd_event *ev;
554 	int ret = 0;
555 
556 	mutex_lock(&p->event_mutex);
557 
558 	ev = lookup_event_by_id(p, event_id);
559 
560 	if (ev)
561 		destroy_event(p, ev);
562 	else
563 		ret = -EINVAL;
564 
565 	mutex_unlock(&p->event_mutex);
566 	return ret;
567 }
568 
569 static void set_event(struct kfd_event *ev)
570 {
571 	struct kfd_event_waiter *waiter;
572 
573 	/* Auto reset if the list is non-empty and we're waking
574 	 * someone. waitqueue_active is safe here because we're
575 	 * protected by the ev->lock, which is also held when
576 	 * updating the wait queues in kfd_wait_on_events.
577 	 */
578 	ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
579 	if (!(++ev->event_age)) {
580 		/* Never wrap back to reserved/default event age 0/1 */
581 		ev->event_age = 2;
582 		WARN_ONCE(1, "event_age wrap back!");
583 	}
584 
585 	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
586 		WRITE_ONCE(waiter->activated, true);
587 
588 	wake_up_all(&ev->wq);
589 }
590 
591 /* Assumes that p is current. */
592 int kfd_set_event(struct kfd_process *p, uint32_t event_id)
593 {
594 	int ret = 0;
595 	struct kfd_event *ev;
596 
597 	rcu_read_lock();
598 
599 	ev = lookup_event_by_id(p, event_id);
600 	if (!ev) {
601 		ret = -EINVAL;
602 		goto unlock_rcu;
603 	}
604 	spin_lock(&ev->lock);
605 
606 	if (event_can_be_cpu_signaled(ev))
607 		set_event(ev);
608 	else
609 		ret = -EINVAL;
610 
611 	spin_unlock(&ev->lock);
612 unlock_rcu:
613 	rcu_read_unlock();
614 	return ret;
615 }
616 
617 static void reset_event(struct kfd_event *ev)
618 {
619 	ev->signaled = false;
620 }
621 
622 /* Assumes that p is current. */
623 int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
624 {
625 	int ret = 0;
626 	struct kfd_event *ev;
627 
628 	rcu_read_lock();
629 
630 	ev = lookup_event_by_id(p, event_id);
631 	if (!ev) {
632 		ret = -EINVAL;
633 		goto unlock_rcu;
634 	}
635 	spin_lock(&ev->lock);
636 
637 	if (event_can_be_cpu_signaled(ev))
638 		reset_event(ev);
639 	else
640 		ret = -EINVAL;
641 
642 	spin_unlock(&ev->lock);
643 unlock_rcu:
644 	rcu_read_unlock();
645 	return ret;
646 
647 }
648 
649 static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
650 {
651 	WRITE_ONCE(p->signal_page[ev->event_id], UNSIGNALED_EVENT_SLOT);
652 }
653 
654 static void set_event_from_interrupt(struct kfd_process *p,
655 					struct kfd_event *ev)
656 {
657 	if (ev && event_can_be_gpu_signaled(ev)) {
658 		acknowledge_signal(p, ev);
659 		spin_lock(&ev->lock);
660 		set_event(ev);
661 		spin_unlock(&ev->lock);
662 	}
663 }
664 
665 void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
666 				uint32_t valid_id_bits, bool signal_mailbox_updated)
667 {
668 	struct kfd_event *ev = NULL;
669 
670 	/*
671 	 * Because we are called from arbitrary context (workqueue) as opposed
672 	 * to process context, kfd_process could attempt to exit while we are
673 	 * running so the lookup function increments the process ref count.
674 	 */
675 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);
676 
677 	if (!p)
678 		return; /* Presumably process exited. */
679 
680 	rcu_read_lock();
681 
682 	if (valid_id_bits)
683 		ev = lookup_signaled_event_by_partial_id(p, partial_id,
684 							 valid_id_bits,
685 							 signal_mailbox_updated);
686 	if (ev) {
687 		set_event_from_interrupt(p, ev);
688 	} else if (p->signal_page) {
689 		/*
690 		 * Partial ID lookup failed. Assume that the event ID
691 		 * in the interrupt payload was invalid and do an
692 		 * exhaustive search of signaled events.
693 		 */
694 		uint64_t *slots = p->signal_page;
695 		uint32_t id;
696 
697 		if (valid_id_bits)
698 			pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",
699 					     partial_id, valid_id_bits);
700 
701 		if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {
702 			/* With relatively few events, it's faster to
703 			 * iterate over the event IDR
704 			 */
705 			idr_for_each_entry(&p->event_idr, ev, id) {
706 				if (id >= KFD_SIGNAL_EVENT_LIMIT)
707 					break;
708 
709 				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)
710 					set_event_from_interrupt(p, ev);
711 			}
712 		} else {
713 			/* With relatively many events, it's faster to
714 			 * iterate over the signal slots and lookup
715 			 * only signaled events from the IDR.
716 			 */
717 			for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)
718 				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {
719 					ev = lookup_event_by_id(p, id);
720 					set_event_from_interrupt(p, ev);
721 				}
722 		}
723 	}
724 
725 	rcu_read_unlock();
726 	kfd_unref_process(p);
727 }
728 
729 static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
730 {
731 	struct kfd_event_waiter *event_waiters;
732 	uint32_t i;
733 
734 	if (num_events > KFD_SIGNAL_EVENT_LIMIT)
735 		return NULL;
736 	event_waiters = kzalloc_objs(struct kfd_event_waiter, num_events);
737 	if (!event_waiters)
738 		return NULL;
739 
740 	for (i = 0; i < num_events; i++)
741 		init_wait(&event_waiters[i].wait);
742 
743 	return event_waiters;
744 }
745 
746 static int init_event_waiter(struct kfd_process *p,
747 		struct kfd_event_waiter *waiter,
748 		struct kfd_event_data *event_data)
749 {
750 	struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id);
751 
752 	if (!ev)
753 		return -EINVAL;
754 
755 	spin_lock(&ev->lock);
756 	waiter->event = ev;
757 	waiter->activated = ev->signaled;
758 	ev->signaled = ev->signaled && !ev->auto_reset;
759 
760 	/* last_event_age = 0 reserved for backward compatible */
761 	if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL &&
762 		event_data->signal_event_data.last_event_age) {
763 		waiter->event_age_enabled = true;
764 		if (ev->event_age != event_data->signal_event_data.last_event_age)
765 			waiter->activated = true;
766 	}
767 
768 	if (!waiter->activated)
769 		add_wait_queue(&ev->wq, &waiter->wait);
770 	spin_unlock(&ev->lock);
771 
772 	return 0;
773 }
774 
775 /* test_event_condition - Test condition of events being waited for
776  * @all:           Return completion only if all events have signaled
777  * @num_events:    Number of events to wait for
778  * @event_waiters: Array of event waiters, one per event
779  *
780  * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have
781  * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)
782  * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of
783  * the events have been destroyed.
784  */
785 static uint32_t test_event_condition(bool all, uint32_t num_events,
786 				struct kfd_event_waiter *event_waiters)
787 {
788 	uint32_t i;
789 	uint32_t activated_count = 0;
790 
791 	for (i = 0; i < num_events; i++) {
792 		if (!READ_ONCE(event_waiters[i].event))
793 			return KFD_IOC_WAIT_RESULT_FAIL;
794 
795 		if (READ_ONCE(event_waiters[i].activated)) {
796 			if (!all)
797 				return KFD_IOC_WAIT_RESULT_COMPLETE;
798 
799 			activated_count++;
800 		}
801 	}
802 
803 	return activated_count == num_events ?
804 		KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;
805 }
806 
807 /*
808  * Copy event specific data, if defined.
809  * Currently only memory exception events have additional data to copy to user
810  */
811 static int copy_signaled_event_data(uint32_t num_events,
812 		struct kfd_event_waiter *event_waiters,
813 		struct kfd_event_data __user *data)
814 {
815 	void *src;
816 	void __user *dst;
817 	struct kfd_event_waiter *waiter;
818 	struct kfd_event *event;
819 	uint32_t i, size = 0;
820 
821 	for (i = 0; i < num_events; i++) {
822 		waiter = &event_waiters[i];
823 		event = waiter->event;
824 		if (!event)
825 			return -EINVAL; /* event was destroyed */
826 		if (waiter->activated) {
827 			if (event->type == KFD_EVENT_TYPE_MEMORY) {
828 				dst = &data[i].memory_exception_data;
829 				src = &event->memory_exception_data;
830 				size = sizeof(struct kfd_hsa_memory_exception_data);
831 			} else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
832 				dst = &data[i].memory_exception_data;
833 				src = &event->hw_exception_data;
834 				size = sizeof(struct kfd_hsa_hw_exception_data);
835 			} else if (event->type == KFD_EVENT_TYPE_SIGNAL &&
836 				waiter->event_age_enabled) {
837 				dst = &data[i].signal_event_data.last_event_age;
838 				src = &event->event_age;
839 				size = sizeof(u64);
840 			}
841 			if (size && copy_to_user(dst, src, size))
842 				return -EFAULT;
843 		}
844 	}
845 
846 	return 0;
847 }
848 
849 static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
850 {
851 	if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
852 		return 0;
853 
854 	if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
855 		return MAX_SCHEDULE_TIMEOUT;
856 
857 	/*
858 	 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
859 	 * but we consider them finite.
860 	 * This hack is wrong, but nobody is likely to notice.
861 	 */
862 	user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
863 
864 	return msecs_to_jiffies(user_timeout_ms) + 1;
865 }
866 
867 static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,
868 			 bool undo_auto_reset)
869 {
870 	uint32_t i;
871 
872 	for (i = 0; i < num_events; i++)
873 		if (waiters[i].event) {
874 			spin_lock(&waiters[i].event->lock);
875 			remove_wait_queue(&waiters[i].event->wq,
876 					  &waiters[i].wait);
877 			if (undo_auto_reset && waiters[i].activated &&
878 			    waiters[i].event && waiters[i].event->auto_reset)
879 				set_event(waiters[i].event);
880 			spin_unlock(&waiters[i].event->lock);
881 		}
882 
883 	kfree(waiters);
884 }
885 
886 int kfd_wait_on_events(struct kfd_process *p,
887 		       uint32_t num_events, void __user *data,
888 		       bool all, uint32_t *user_timeout_ms,
889 		       uint32_t *wait_result)
890 {
891 	struct kfd_event_data __user *events =
892 			(struct kfd_event_data __user *) data;
893 	uint32_t i;
894 	int ret = 0;
895 
896 	struct kfd_event_waiter *event_waiters = NULL;
897 	long timeout = user_timeout_to_jiffies(*user_timeout_ms);
898 
899 	event_waiters = alloc_event_waiters(num_events);
900 	if (!event_waiters) {
901 		ret = -ENOMEM;
902 		goto out;
903 	}
904 
905 	/* Use p->event_mutex here to protect against concurrent creation and
906 	 * destruction of events while we initialize event_waiters.
907 	 */
908 	mutex_lock(&p->event_mutex);
909 
910 	for (i = 0; i < num_events; i++) {
911 		struct kfd_event_data event_data;
912 
913 		if (copy_from_user(&event_data, &events[i],
914 				sizeof(struct kfd_event_data))) {
915 			ret = -EFAULT;
916 			goto out_unlock;
917 		}
918 
919 		ret = init_event_waiter(p, &event_waiters[i], &event_data);
920 		if (ret)
921 			goto out_unlock;
922 	}
923 
924 	/* Check condition once. */
925 	*wait_result = test_event_condition(all, num_events, event_waiters);
926 	if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {
927 		ret = copy_signaled_event_data(num_events,
928 					       event_waiters, events);
929 		goto out_unlock;
930 	} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {
931 		/* This should not happen. Events shouldn't be
932 		 * destroyed while we're holding the event_mutex
933 		 */
934 		goto out_unlock;
935 	}
936 
937 	mutex_unlock(&p->event_mutex);
938 
939 	while (true) {
940 		if (fatal_signal_pending(current)) {
941 			ret = -EINTR;
942 			break;
943 		}
944 
945 		if (signal_pending(current)) {
946 			ret = -ERESTARTSYS;
947 			if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&
948 			    *user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)
949 				*user_timeout_ms = jiffies_to_msecs(
950 					max(0l, timeout-1));
951 			break;
952 		}
953 
954 		/* Set task state to interruptible sleep before
955 		 * checking wake-up conditions. A concurrent wake-up
956 		 * will put the task back into runnable state. In that
957 		 * case schedule_timeout will not put the task to
958 		 * sleep and we'll get a chance to re-check the
959 		 * updated conditions almost immediately. Otherwise,
960 		 * this race condition would lead to a soft hang or a
961 		 * very long sleep.
962 		 */
963 		set_current_state(TASK_INTERRUPTIBLE);
964 
965 		*wait_result = test_event_condition(all, num_events,
966 						    event_waiters);
967 		if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)
968 			break;
969 
970 		if (timeout <= 0)
971 			break;
972 
973 		timeout = schedule_timeout(timeout);
974 	}
975 	__set_current_state(TASK_RUNNING);
976 
977 	mutex_lock(&p->event_mutex);
978 	/* copy_signaled_event_data may sleep. So this has to happen
979 	 * after the task state is set back to RUNNING.
980 	 *
981 	 * The event may also have been destroyed after signaling. So
982 	 * copy_signaled_event_data also must confirm that the event
983 	 * still exists. Therefore this must be under the p->event_mutex
984 	 * which is also held when events are destroyed.
985 	 */
986 	if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)
987 		ret = copy_signaled_event_data(num_events,
988 					       event_waiters, events);
989 
990 out_unlock:
991 	free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);
992 	mutex_unlock(&p->event_mutex);
993 out:
994 	if (ret)
995 		*wait_result = KFD_IOC_WAIT_RESULT_FAIL;
996 	else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)
997 		ret = -EIO;
998 
999 	return ret;
1000 }
1001 
1002 /*
1003  * Assumes that p is not going away.
1004  */
1005 static void lookup_events_by_type_and_signal(struct kfd_process *p,
1006 		int type, void *event_data)
1007 {
1008 	struct kfd_hsa_memory_exception_data *ev_data;
1009 	struct kfd_event *ev;
1010 	uint32_t id;
1011 	bool send_signal = true;
1012 
1013 	ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
1014 
1015 	rcu_read_lock();
1016 
1017 	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1018 	idr_for_each_entry_continue(&p->event_idr, ev, id)
1019 		if (ev->type == type) {
1020 			send_signal = false;
1021 			dev_dbg(kfd_device,
1022 					"Event found: id %X type %d",
1023 					ev->event_id, ev->type);
1024 			spin_lock(&ev->lock);
1025 			set_event(ev);
1026 			if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
1027 				ev->memory_exception_data = *ev_data;
1028 			spin_unlock(&ev->lock);
1029 		}
1030 
1031 	if (type == KFD_EVENT_TYPE_MEMORY) {
1032 		dev_warn(kfd_device,
1033 			"Sending SIGSEGV to process pid %d",
1034 				p->lead_thread->pid);
1035 		send_sig(SIGSEGV, p->lead_thread, 0);
1036 	}
1037 
1038 	/* Send SIGTERM no event of type "type" has been found*/
1039 	if (send_signal) {
1040 		if (send_sigterm) {
1041 			dev_warn(kfd_device,
1042 				"Sending SIGTERM to process pid %d",
1043 					p->lead_thread->pid);
1044 			send_sig(SIGTERM, p->lead_thread, 0);
1045 		} else {
1046 			dev_err(kfd_device,
1047 				"Process pid %d got unhandled exception",
1048 				p->lead_thread->pid);
1049 		}
1050 	}
1051 
1052 	rcu_read_unlock();
1053 }
1054 
1055 void kfd_signal_hw_exception_event(u32 pasid)
1056 {
1057 	/*
1058 	 * Because we are called from arbitrary context (workqueue) as opposed
1059 	 * to process context, kfd_process could attempt to exit while we are
1060 	 * running so the lookup function increments the process ref count.
1061 	 */
1062 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);
1063 
1064 	if (!p)
1065 		return; /* Presumably process exited. */
1066 
1067 	lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
1068 	kfd_unref_process(p);
1069 }
1070 
1071 void kfd_signal_vm_fault_event_with_userptr(struct kfd_process *p, uint64_t gpu_va)
1072 {
1073 	struct kfd_process_device *pdd;
1074 	struct kfd_hsa_memory_exception_data exception_data;
1075 	int i;
1076 
1077 	memset(&exception_data, 0, sizeof(exception_data));
1078 	exception_data.va = gpu_va;
1079 	exception_data.failure.NotPresent = 1;
1080 
1081 	// Send VM seg fault to all kfd process device
1082 	for (i = 0; i < p->n_pdds; i++) {
1083 		pdd = p->pdds[i];
1084 		exception_data.gpu_id = pdd->user_gpu_id;
1085 		kfd_evict_process_device(pdd);
1086 		kfd_signal_vm_fault_event(pdd, NULL, &exception_data);
1087 	}
1088 }
1089 
1090 void kfd_signal_vm_fault_event(struct kfd_process_device *pdd,
1091 				struct kfd_vm_fault_info *info,
1092 				struct kfd_hsa_memory_exception_data *data)
1093 {
1094 	struct kfd_event *ev;
1095 	uint32_t id;
1096 	struct kfd_process *p = pdd->process;
1097 	struct kfd_hsa_memory_exception_data memory_exception_data;
1098 	int user_gpu_id;
1099 
1100 	user_gpu_id = kfd_process_get_user_gpu_id(p, pdd->dev->id);
1101 	if (unlikely(user_gpu_id == -EINVAL)) {
1102 		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n",
1103 			  pdd->dev->id);
1104 		return;
1105 	}
1106 
1107 	/* SoC15 chips and onwards will pass in data from now on. */
1108 	if (!data) {
1109 		memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1110 		memory_exception_data.gpu_id = user_gpu_id;
1111 		memory_exception_data.failure.imprecise = true;
1112 
1113 		/* Set failure reason */
1114 		if (info) {
1115 			memory_exception_data.va = (info->page_addr) <<
1116 								PAGE_SHIFT;
1117 			memory_exception_data.failure.NotPresent =
1118 				info->prot_valid ? 1 : 0;
1119 			memory_exception_data.failure.NoExecute =
1120 				info->prot_exec ? 1 : 0;
1121 			memory_exception_data.failure.ReadOnly =
1122 				info->prot_write ? 1 : 0;
1123 			memory_exception_data.failure.imprecise = 0;
1124 		}
1125 	}
1126 
1127 	rcu_read_lock();
1128 
1129 	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1130 	idr_for_each_entry_continue(&p->event_idr, ev, id)
1131 		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1132 			spin_lock(&ev->lock);
1133 			ev->memory_exception_data = data ? *data :
1134 							memory_exception_data;
1135 			set_event(ev);
1136 			spin_unlock(&ev->lock);
1137 		}
1138 
1139 	rcu_read_unlock();
1140 }
1141 
1142 void kfd_signal_reset_event(struct kfd_node *dev)
1143 {
1144 	struct kfd_hsa_hw_exception_data hw_exception_data;
1145 	struct kfd_hsa_memory_exception_data memory_exception_data;
1146 	struct kfd_process *p;
1147 	struct kfd_event *ev;
1148 	unsigned int temp;
1149 	uint32_t id, idx;
1150 	int reset_cause = atomic_read(&dev->sram_ecc_flag) ?
1151 			KFD_HW_EXCEPTION_ECC :
1152 			KFD_HW_EXCEPTION_GPU_HANG;
1153 
1154 	/* Whole gpu reset caused by GPU hang and memory is lost */
1155 	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1156 	hw_exception_data.memory_lost = 1;
1157 	hw_exception_data.reset_cause = reset_cause;
1158 
1159 	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1160 	memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;
1161 	memory_exception_data.failure.imprecise = true;
1162 
1163 	idx = srcu_read_lock(&kfd_processes_srcu);
1164 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1165 		int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1166 		struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p);
1167 
1168 		if (unlikely(user_gpu_id == -EINVAL)) {
1169 			WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1170 			continue;
1171 		}
1172 
1173 		if (unlikely(!pdd)) {
1174 			WARN_ONCE(1, "Could not get device data from process pid:%d\n",
1175 				  p->lead_thread->pid);
1176 			continue;
1177 		}
1178 
1179 		if (dev->dqm->detect_hang_count && !pdd->has_reset_queue)
1180 			continue;
1181 
1182 		if (dev->dqm->detect_hang_count) {
1183 			struct amdgpu_task_info *ti;
1184 			struct amdgpu_fpriv *drv_priv;
1185 
1186 			if (unlikely(amdgpu_file_to_fpriv(pdd->drm_file, &drv_priv))) {
1187 				WARN_ONCE(1, "Could not get vm for device %x from pid:%d\n",
1188 					  dev->id, p->lead_thread->pid);
1189 				continue;
1190 			}
1191 
1192 			ti = amdgpu_vm_get_task_info_vm(&drv_priv->vm);
1193 			if (ti) {
1194 				dev_err(dev->adev->dev,
1195 					"Queues reset on process %s tid %d thread %s pid %d\n",
1196 					ti->process_name, ti->tgid, ti->task.comm, ti->task.pid);
1197 				amdgpu_vm_put_task_info(ti);
1198 			}
1199 		}
1200 
1201 		rcu_read_lock();
1202 
1203 		id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1204 		idr_for_each_entry_continue(&p->event_idr, ev, id) {
1205 			if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1206 				spin_lock(&ev->lock);
1207 				ev->hw_exception_data = hw_exception_data;
1208 				ev->hw_exception_data.gpu_id = user_gpu_id;
1209 				set_event(ev);
1210 				spin_unlock(&ev->lock);
1211 			}
1212 			if (ev->type == KFD_EVENT_TYPE_MEMORY &&
1213 			    reset_cause == KFD_HW_EXCEPTION_ECC) {
1214 				spin_lock(&ev->lock);
1215 				ev->memory_exception_data = memory_exception_data;
1216 				ev->memory_exception_data.gpu_id = user_gpu_id;
1217 				set_event(ev);
1218 				spin_unlock(&ev->lock);
1219 			}
1220 		}
1221 
1222 		rcu_read_unlock();
1223 	}
1224 	srcu_read_unlock(&kfd_processes_srcu, idx);
1225 }
1226 
1227 /*
1228  * Per-process opt-in for poison-consumption SIGBUS handling.
1229  *
1230  * Default: kernel sends SIGBUS to the process immediately when poison is
1231  * consumed, in addition to delivering the KFD HW/MEMORY exception events.
1232  *
1233  * Userspace (ROCr) can opt-in per-process via the
1234  * DRM_IOCTL_AMDGPU_PROC_OPTIONS / AMDGPU_PROC_OPTIONS_OP_KFD_SIGBUS_DELAY
1235  * option. This lets the app's registered system-event callback handle the
1236  * RAS error first, instead of being killed by SIGBUS.
1237  *
1238  * Encoded value (stored on the kfd_process):
1239  *   0          - default: SIGBUS immediately (no opt-in)
1240  *   0xFFFFFFFF - opt-in, never escalate to SIGBUS
1241  *   N (other)  - opt-in, escalate to SIGBUS after N ms if app does not
1242  *                handle the error in time (safety timeout)
1243  */
1244 
1245 void kfd_signal_sigbus_delayed_fn(struct work_struct *work)
1246 {
1247 	struct kfd_process *p = container_of(to_delayed_work(work),
1248 				struct kfd_process, signal_work);
1249 
1250 	if (p->lead_thread)
1251 		send_sig(SIGBUS, p->lead_thread, 0);
1252 
1253 	kfd_unref_process(p);
1254 }
1255 
1256 static void kfd_signal_sigbus_with_delay(struct kfd_node *dev,
1257 					 struct kfd_process *p)
1258 {
1259 	u32 delay_ms = atomic_read(&p->kfd_sigbus_delay_ms);
1260 
1261 	if (delay_ms == AMDGPU_PROC_OPTIONS_KFD_SIGBUS_DELAY_DISABLED) {
1262 		dev_info(dev->adev->dev,
1263 			 "SIGBUS suppressed for process %s(pid:%d): app opted in to handle RAS error\n",
1264 			 p->lead_thread->comm, p->lead_thread->pid);
1265 		return;
1266 	}
1267 
1268 	if (delay_ms == 0)
1269 		goto send_now;
1270 
1271 	/*
1272 	 * Take an extra reference for the delayed worker. If the work is
1273 	 * already pending (e.g. another device of this process consumed poison
1274 	 * just before), drop the reference and skip rescheduling - the process
1275 	 * only needs to be notified once.
1276 	 */
1277 	kref_get(&p->ref);
1278 	if (!schedule_delayed_work(&p->signal_work, msecs_to_jiffies(delay_ms))) {
1279 		kfd_unref_process(p);
1280 		return;
1281 	}
1282 
1283 	dev_info(dev->adev->dev,
1284 		 "Deferring SIGBUS to process %s(pid:%d) by %u ms (RAS error opt-in safety timeout)\n",
1285 		 p->lead_thread->comm, p->lead_thread->pid, delay_ms);
1286 	return;
1287 
1288 send_now:
1289 	send_sig(SIGBUS, p->lead_thread, 0);
1290 }
1291 
1292 void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)
1293 {
1294 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);
1295 	struct kfd_hsa_memory_exception_data memory_exception_data;
1296 	struct kfd_hsa_hw_exception_data hw_exception_data;
1297 	struct kfd_event *ev;
1298 	uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1299 	int user_gpu_id;
1300 
1301 	if (!p) {
1302 		dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid);
1303 		return; /* Presumably process exited. */
1304 	}
1305 
1306 	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
1307 	if (unlikely(user_gpu_id == -EINVAL)) {
1308 		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
1309 		kfd_unref_process(p);
1310 		return;
1311 	}
1312 
1313 	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1314 	hw_exception_data.gpu_id = user_gpu_id;
1315 	hw_exception_data.memory_lost = 1;
1316 	hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;
1317 
1318 	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1319 	memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;
1320 	memory_exception_data.gpu_id = user_gpu_id;
1321 	memory_exception_data.failure.imprecise = true;
1322 
1323 	rcu_read_lock();
1324 
1325 	idr_for_each_entry_continue(&p->event_idr, ev, id) {
1326 		if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1327 			spin_lock(&ev->lock);
1328 			ev->hw_exception_data = hw_exception_data;
1329 			set_event(ev);
1330 			spin_unlock(&ev->lock);
1331 		}
1332 
1333 		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1334 			spin_lock(&ev->lock);
1335 			ev->memory_exception_data = memory_exception_data;
1336 			set_event(ev);
1337 			spin_unlock(&ev->lock);
1338 		}
1339 	}
1340 
1341 	dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n",
1342 		p->lead_thread->comm, pasid);
1343 	rcu_read_unlock();
1344 
1345 	/* user application will handle SIGBUS signal */
1346 	kfd_signal_sigbus_with_delay(dev, p);
1347 
1348 	kfd_unref_process(p);
1349 }
1350 
1351 /* signal KFD_EVENT_TYPE_SIGNAL events from process p
1352  * send signal SIGBUS to correspondent user space process
1353  */
1354 void kfd_signal_process_terminate_event(struct kfd_process *p)
1355 {
1356 	struct kfd_event *ev;
1357 	u32 id;
1358 
1359 	rcu_read_lock();
1360 
1361 	/* iterate from id 1 for KFD_EVENT_TYPE_SIGNAL events */
1362 	id = 1;
1363 	idr_for_each_entry_continue(&p->event_idr, ev, id)
1364 		if (ev->type == KFD_EVENT_TYPE_SIGNAL) {
1365 			spin_lock(&ev->lock);
1366 			set_event(ev);
1367 			spin_unlock(&ev->lock);
1368 		}
1369 
1370 	/* Send SIGBUS to p->lead_thread */
1371 	dev_notice(kfd_device,
1372 		   "Sending SIGBUS to process %d",
1373 		   p->lead_thread->pid);
1374 
1375 	send_sig(SIGBUS, p->lead_thread, 0);
1376 
1377 	rcu_read_unlock();
1378 }
1379