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