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