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