kfd_events.c 35.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0 OR MIT
2
/*
3
 * Copyright 2014-2022 Advanced Micro Devices, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/mm_types.h>
#include <linux/slab.h>
#include <linux/types.h>
27
#include <linux/sched/signal.h>
28
#include <linux/sched/mm.h>
29 30 31 32 33
#include <linux/uaccess.h>
#include <linux/mman.h>
#include <linux/memory.h>
#include "kfd_priv.h"
#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 42 43
	wait_queue_entry_t wait;
	struct kfd_event *event; /* Event to wait for */
	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 51
 * We get whole pages and map them to the process VA.
 * Individual signal events use their event_id as slot index.
52
 */
53
struct kfd_signal_page {
54 55
	uint64_t *kernel_address;
	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 62 63
{
	return page->kernel_address;
}

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 75 76 77
					get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
	if (!backing_store)
		goto fail_alloc_signal_store;

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 86
			page, p);

87
	return page;
88 89 90

fail_alloc_signal_store:
	kfree(page);
91
	return NULL;
92 93
}

94
static int allocate_event_notification_slot(struct kfd_process *p,
95 96
					    struct kfd_event *ev,
					    const int *restore_id)
97
{
98 99
	int id;

100 101 102
	if (!p->signal_page) {
		p->signal_page = allocate_signal_page(p);
		if (!p->signal_page)
103
			return -ENOMEM;
104 105
		/* Oldest user mode expects 256 event slots */
		p->signal_mapped_size = 256*8;
106 107
	}

108 109 110 111 112 113 114 115 116 117 118 119 120
	if (restore_id) {
		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
				GFP_KERNEL);
	} else {
		/*
		 * Compatibility with old user mode: Only use signal slots
		 * user mode has mapped, may be less than
		 * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase
		 * of the event limit without breaking user mode.
		 */
		id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8,
				GFP_KERNEL);
	}
121 122
	if (id < 0)
		return id;
123

124 125
	ev->event_id = id;
	page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT;
126

127
	return 0;
128 129 130
}

/*
131 132
 * Assumes that p->event_mutex or rcu_readlock is held and of course that p is
 * not going away.
133 134 135
 */
static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)
{
136
	return idr_find(&p->event_idr, id);
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/**
 * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID
 * @p:     Pointer to struct kfd_process
 * @id:    ID to look up
 * @bits:  Number of valid bits in @id
 *
 * Finds the first signaled event with a matching partial ID. If no
 * matching signaled event is found, returns NULL. In that case the
 * caller should assume that the partial ID is invalid and do an
 * exhaustive search of all siglaned events.
 *
 * If multiple events with the same partial ID signal at the same
 * time, they will be found one interrupt at a time, not necessarily
 * in the same order the interrupts occurred. As long as the number of
 * interrupts is correct, all signaled events will be seen by the
 * driver.
 */
static struct kfd_event *lookup_signaled_event_by_partial_id(
	struct kfd_process *p, uint32_t id, uint32_t bits)
{
	struct kfd_event *ev;

	if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT)
		return NULL;

	/* Fast path for the common case that @id is not a partial ID
	 * and we only need a single lookup.
	 */
	if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) {
		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
			return NULL;

		return idr_find(&p->event_idr, id);
	}

	/* General case for partial IDs: Iterate over all matching IDs
	 * and find the first one that has signaled.
	 */
	for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) {
		if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)
			continue;

		ev = idr_find(&p->event_idr, id);
	}

	return ev;
}

187 188
static int create_signal_event(struct file *devkfd, struct kfd_process *p,
				struct kfd_event *ev, const int *restore_id)
189
{
190 191
	int ret;

192 193
	if (p->signal_mapped_size &&
	    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 197
			p->signal_event_limit_reached = true;
		}
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 208
	}

	p->signal_event_count++;

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 212 213
			p->signal_event_count, ev->event_id,
			ev->user_signal_address);

214 215 216
	return 0;
}

217
static int create_other_event(struct kfd_process *p, struct kfd_event *ev, const int *restore_id)
218
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232
	int id;

	if (restore_id)
		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
			GFP_KERNEL);
	else
		/* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an
		 * intentional integer overflow to -1 without a compiler
		 * warning. idr_alloc treats a negative value as "maximum
		 * signed integer".
		 */
		id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID,
				(uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1,
				GFP_KERNEL);
233 234 235 236

	if (id < 0)
		return id;
	ev->event_id = id;
237 238 239 240

	return 0;
}

241
int kfd_event_init_process(struct kfd_process *p)
242
{
243 244
	int id;

245
	mutex_init(&p->event_mutex);
246
	idr_init(&p->event_idr);
247
	p->signal_page = NULL;
248 249 250 251 252 253 254 255 256 257 258
	p->signal_event_count = 1;
	/* Allocate event ID 0. It is used for a fast path to ignore bogus events
	 * that are sent by the CP without a context ID
	 */
	id = idr_alloc(&p->event_idr, NULL, 0, 1, GFP_KERNEL);
	if (id < 0) {
		idr_destroy(&p->event_idr);
		mutex_destroy(&p->event_mutex);
		return id;
	}
	return 0;
259 260 261 262
}

static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
{
263
	struct kfd_event_waiter *waiter;
264

265
	/* Wake up pending waiters. They will return failure */
266
	spin_lock(&ev->lock);
267
	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
268
		WRITE_ONCE(waiter->event, NULL);
269
	wake_up_all(&ev->wq);
270
	spin_unlock(&ev->lock);
271

272 273
	if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
	    ev->type == KFD_EVENT_TYPE_DEBUG)
274 275
		p->signal_event_count--;

276
	idr_remove(&p->event_idr, ev->event_id);
277
	kfree_rcu(ev, rcu);
278 279 280 281 282
}

static void destroy_events(struct kfd_process *p)
{
	struct kfd_event *ev;
283
	uint32_t id;
284

285
	idr_for_each_entry(&p->event_idr, ev, id)
286 287
		if (ev)
			destroy_event(p, ev);
288
	idr_destroy(&p->event_idr);
289
	mutex_destroy(&p->event_mutex);
290 291 292 293 294 295
}

/*
 * We assume that the process is being destroyed and there is no need to
 * unmap the pages or keep bookkeeping data in order.
 */
296
static void shutdown_signal_page(struct kfd_process *p)
297
{
298
	struct kfd_signal_page *page = p->signal_page;
299

300
	if (page) {
301 302 303
		if (page->need_to_free_pages)
			free_pages((unsigned long)page->kernel_address,
				   get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
304 305 306 307 308 309 310
		kfree(page);
	}
}

void kfd_event_free_process(struct kfd_process *p)
{
	destroy_events(p);
311
	shutdown_signal_page(p);
312 313 314 315 316 317 318 319 320 321 322 323 324
}

static bool event_can_be_gpu_signaled(const struct kfd_event *ev)
{
	return ev->type == KFD_EVENT_TYPE_SIGNAL ||
					ev->type == KFD_EVENT_TYPE_DEBUG;
}

static bool event_can_be_cpu_signaled(const struct kfd_event *ev)
{
	return ev->type == KFD_EVENT_TYPE_SIGNAL;
}

325 326
static int kfd_event_page_set(struct kfd_process *p, void *kernel_address,
		       uint64_t size, uint64_t user_handle)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
{
	struct kfd_signal_page *page;

	if (p->signal_page)
		return -EBUSY;

	page = kzalloc(sizeof(*page), GFP_KERNEL);
	if (!page)
		return -ENOMEM;

	/* Initialize all events to unsignaled */
	memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT,
	       KFD_SIGNAL_EVENT_LIMIT * 8);

	page->kernel_address = kernel_address;

	p->signal_page = page;
	p->signal_mapped_size = size;
345
	p->signal_handle = user_handle;
346 347 348
	return 0;
}

349 350 351 352 353 354 355 356 357 358 359 360 361
int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset)
{
	struct kfd_dev *kfd;
	struct kfd_process_device *pdd;
	void *mem, *kern_addr;
	uint64_t size;
	int err = 0;

	if (p->signal_page) {
		pr_err("Event page is already set\n");
		return -EINVAL;
	}

362 363
	pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(event_page_offset));
	if (!pdd) {
364 365 366
		pr_err("Getting device by id failed in %s\n", __func__);
		return -EINVAL;
	}
367
	kfd = pdd->dev;
368 369 370 371 372 373 374 375 376 377 378 379

	pdd = kfd_bind_process_to_device(kfd, p);
	if (IS_ERR(pdd))
		return PTR_ERR(pdd);

	mem = kfd_process_device_translate_handle(pdd,
			GET_IDR_HANDLE(event_page_offset));
	if (!mem) {
		pr_err("Can't find BO, offset is 0x%llx\n", event_page_offset);
		return -EINVAL;
	}

380
	err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(mem, &kern_addr, &size);
381 382 383 384 385 386 387 388
	if (err) {
		pr_err("Failed to map event page to kernel\n");
		return err;
	}

	err = kfd_event_page_set(p, kern_addr, size, event_page_offset);
	if (err) {
		pr_err("Failed to set event page\n");
389
		amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);
390 391 392 393 394
		return err;
	}
	return err;
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
int kfd_event_create(struct file *devkfd, struct kfd_process *p,
		     uint32_t event_type, bool auto_reset, uint32_t node_id,
		     uint32_t *event_id, uint32_t *event_trigger_data,
		     uint64_t *event_page_offset, uint32_t *event_slot_index)
{
	int ret = 0;
	struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);

	if (!ev)
		return -ENOMEM;

	ev->type = event_type;
	ev->auto_reset = auto_reset;
	ev->signaled = false;

410
	spin_lock_init(&ev->lock);
411
	init_waitqueue_head(&ev->wq);
412 413 414 415 416 417 418 419

	*event_page_offset = 0;

	mutex_lock(&p->event_mutex);

	switch (event_type) {
	case KFD_EVENT_TYPE_SIGNAL:
	case KFD_EVENT_TYPE_DEBUG:
420
		ret = create_signal_event(devkfd, p, ev, NULL);
421
		if (!ret) {
422
			*event_page_offset = KFD_MMAP_TYPE_EVENTS;
423
			*event_slot_index = ev->event_id;
424 425 426
		}
		break;
	default:
427
		ret = create_other_event(p, ev, NULL);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
		break;
	}

	if (!ret) {
		*event_id = ev->event_id;
		*event_trigger_data = ev->event_id;
	} else {
		kfree(ev);
	}

	mutex_unlock(&p->event_mutex);

	return ret;
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
int kfd_criu_restore_event(struct file *devkfd,
			   struct kfd_process *p,
			   uint8_t __user *user_priv_ptr,
			   uint64_t *priv_data_offset,
			   uint64_t max_priv_data_size)
{
	struct kfd_criu_event_priv_data *ev_priv;
	struct kfd_event *ev = NULL;
	int ret = 0;

	ev_priv = kmalloc(sizeof(*ev_priv), GFP_KERNEL);
	if (!ev_priv)
		return -ENOMEM;

	ev = kzalloc(sizeof(*ev), GFP_KERNEL);
	if (!ev) {
		ret = -ENOMEM;
		goto exit;
	}

	if (*priv_data_offset + sizeof(*ev_priv) > max_priv_data_size) {
		ret = -EINVAL;
		goto exit;
	}

	ret = copy_from_user(ev_priv, user_priv_ptr + *priv_data_offset, sizeof(*ev_priv));
	if (ret) {
		ret = -EFAULT;
		goto exit;
	}
	*priv_data_offset += sizeof(*ev_priv);

	if (ev_priv->user_handle) {
		ret = kfd_kmap_event_page(p, ev_priv->user_handle);
		if (ret)
			goto exit;
	}

	ev->type = ev_priv->type;
	ev->auto_reset = ev_priv->auto_reset;
	ev->signaled = ev_priv->signaled;

485
	spin_lock_init(&ev->lock);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	init_waitqueue_head(&ev->wq);

	mutex_lock(&p->event_mutex);
	switch (ev->type) {
	case KFD_EVENT_TYPE_SIGNAL:
	case KFD_EVENT_TYPE_DEBUG:
		ret = create_signal_event(devkfd, p, ev, &ev_priv->event_id);
		break;
	case KFD_EVENT_TYPE_MEMORY:
		memcpy(&ev->memory_exception_data,
			&ev_priv->memory_exception_data,
			sizeof(struct kfd_hsa_memory_exception_data));

		ret = create_other_event(p, ev, &ev_priv->event_id);
		break;
	case KFD_EVENT_TYPE_HW_EXCEPTION:
		memcpy(&ev->hw_exception_data,
			&ev_priv->hw_exception_data,
			sizeof(struct kfd_hsa_hw_exception_data));

		ret = create_other_event(p, ev, &ev_priv->event_id);
		break;
	}
509
	mutex_unlock(&p->event_mutex);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

exit:
	if (ret)
		kfree(ev);

	kfree(ev_priv);

	return ret;
}

int kfd_criu_checkpoint_events(struct kfd_process *p,
			 uint8_t __user *user_priv_data,
			 uint64_t *priv_data_offset)
{
	struct kfd_criu_event_priv_data *ev_privs;
	int i = 0;
	int ret =  0;
	struct kfd_event *ev;
	uint32_t ev_id;

	uint32_t num_events = kfd_get_num_events(p);

	if (!num_events)
		return 0;

	ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL);
	if (!ev_privs)
		return -ENOMEM;


	idr_for_each_entry(&p->event_idr, ev, ev_id) {
		struct kfd_criu_event_priv_data *ev_priv;

		/*
		 * Currently, all events have same size of private_data, but the current ioctl's
		 * and CRIU plugin supports private_data of variable sizes
		 */
		ev_priv = &ev_privs[i];

		ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT;

		/* We store the user_handle with the first event */
		if (i == 0 && p->signal_page)
			ev_priv->user_handle = p->signal_handle;

		ev_priv->event_id = ev->event_id;
		ev_priv->auto_reset = ev->auto_reset;
		ev_priv->type = ev->type;
		ev_priv->signaled = ev->signaled;

		if (ev_priv->type == KFD_EVENT_TYPE_MEMORY)
			memcpy(&ev_priv->memory_exception_data,
				&ev->memory_exception_data,
				sizeof(struct kfd_hsa_memory_exception_data));
		else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION)
			memcpy(&ev_priv->hw_exception_data,
				&ev->hw_exception_data,
				sizeof(struct kfd_hsa_hw_exception_data));

		pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n",
			  i,
			  ev_priv->event_id,
			  ev_priv->auto_reset,
			  ev_priv->type,
			  ev_priv->signaled);
		i++;
	}

	ret = copy_to_user(user_priv_data + *priv_data_offset,
			   ev_privs, num_events * sizeof(*ev_privs));
	if (ret) {
		pr_err("Failed to copy events priv to user\n");
		ret = -EFAULT;
	}

	*priv_data_offset += num_events * sizeof(*ev_privs);

	kvfree(ev_privs);
	return ret;
}

int kfd_get_num_events(struct kfd_process *p)
{
	struct kfd_event *ev;
	uint32_t id;
	u32 num_events = 0;

	idr_for_each_entry(&p->event_idr, ev, id)
		num_events++;

	return num_events;
}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
/* Assumes that p is current. */
int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
{
	struct kfd_event *ev;
	int ret = 0;

	mutex_lock(&p->event_mutex);

	ev = lookup_event_by_id(p, event_id);

	if (ev)
		destroy_event(p, ev);
	else
		ret = -EINVAL;

	mutex_unlock(&p->event_mutex);
	return ret;
}

static void set_event(struct kfd_event *ev)
{
	struct kfd_event_waiter *waiter;

626 627
	/* Auto reset if the list is non-empty and we're waking
	 * someone. waitqueue_active is safe here because we're
628
	 * protected by the ev->lock, which is also held when
629 630 631
	 * updating the wait queues in kfd_wait_on_events.
	 */
	ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
632

633
	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
634
		WRITE_ONCE(waiter->activated, true);
635

636
	wake_up_all(&ev->wq);
637 638 639 640 641 642 643 644
}

/* Assumes that p is current. */
int kfd_set_event(struct kfd_process *p, uint32_t event_id)
{
	int ret = 0;
	struct kfd_event *ev;

645
	rcu_read_lock();
646 647

	ev = lookup_event_by_id(p, event_id);
648 649 650 651
	if (!ev) {
		ret = -EINVAL;
		goto unlock_rcu;
	}
652
	spin_lock(&ev->lock);
653

654
	if (event_can_be_cpu_signaled(ev))
655 656 657 658
		set_event(ev);
	else
		ret = -EINVAL;

659
	spin_unlock(&ev->lock);
660
unlock_rcu:
661
	rcu_read_unlock();
662 663 664 665 666 667 668 669 670 671 672 673 674 675
	return ret;
}

static void reset_event(struct kfd_event *ev)
{
	ev->signaled = false;
}

/* Assumes that p is current. */
int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
{
	int ret = 0;
	struct kfd_event *ev;

676
	rcu_read_lock();
677 678

	ev = lookup_event_by_id(p, event_id);
679 680 681 682
	if (!ev) {
		ret = -EINVAL;
		goto unlock_rcu;
	}
683
	spin_lock(&ev->lock);
684

685
	if (event_can_be_cpu_signaled(ev))
686 687 688 689
		reset_event(ev);
	else
		ret = -EINVAL;

690
	spin_unlock(&ev->lock);
691
unlock_rcu:
692
	rcu_read_unlock();
693 694 695 696 697 698
	return ret;

}

static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
{
699
	WRITE_ONCE(page_slots(p->signal_page)[ev->event_id], UNSIGNALED_EVENT_SLOT);
700 701 702 703 704 705 706
}

static void set_event_from_interrupt(struct kfd_process *p,
					struct kfd_event *ev)
{
	if (ev && event_can_be_gpu_signaled(ev)) {
		acknowledge_signal(p, ev);
707
		spin_lock(&ev->lock);
708
		set_event(ev);
709
		spin_unlock(&ev->lock);
710 711 712
	}
}

713
void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
714 715
				uint32_t valid_id_bits)
{
716
	struct kfd_event *ev = NULL;
717 718 719 720

	/*
	 * Because we are called from arbitrary context (workqueue) as opposed
	 * to process context, kfd_process could attempt to exit while we are
721
	 * running so the lookup function increments the process ref count.
722 723 724 725 726 727
	 */
	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);

	if (!p)
		return; /* Presumably process exited. */

728
	rcu_read_lock();
729

730 731 732 733
	if (valid_id_bits)
		ev = lookup_signaled_event_by_partial_id(p, partial_id,
							 valid_id_bits);
	if (ev) {
734
		set_event_from_interrupt(p, ev);
735
	} else if (p->signal_page) {
736
		/*
737 738 739
		 * Partial ID lookup failed. Assume that the event ID
		 * in the interrupt payload was invalid and do an
		 * exhaustive search of signaled events.
740
		 */
741 742
		uint64_t *slots = page_slots(p->signal_page);
		uint32_t id;
743

744 745 746 747
		if (valid_id_bits)
			pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",
					     partial_id, valid_id_bits);

748
		if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {
749 750 751 752 753 754 755
			/* With relatively few events, it's faster to
			 * iterate over the event IDR
			 */
			idr_for_each_entry(&p->event_idr, ev, id) {
				if (id >= KFD_SIGNAL_EVENT_LIMIT)
					break;

756
				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)
757
					set_event_from_interrupt(p, ev);
758
			}
759 760 761 762 763
		} else {
			/* With relatively many events, it's faster to
			 * iterate over the signal slots and lookup
			 * only signaled events from the IDR.
			 */
764
			for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)
765
				if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {
766 767 768 769
					ev = lookup_event_by_id(p, id);
					set_event_from_interrupt(p, ev);
				}
		}
770 771
	}

772
	rcu_read_unlock();
773
	kfd_unref_process(p);
774 775 776 777 778 779 780 781 782 783
}

static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
{
	struct kfd_event_waiter *event_waiters;
	uint32_t i;

	event_waiters = kmalloc_array(num_events,
					sizeof(struct kfd_event_waiter),
					GFP_KERNEL);
784 785
	if (!event_waiters)
		return NULL;
786 787

	for (i = 0; (event_waiters) && (i < num_events) ; i++) {
788
		init_wait(&event_waiters[i].wait);
789 790 791 792 793 794
		event_waiters[i].activated = false;
	}

	return event_waiters;
}

795
static int init_event_waiter(struct kfd_process *p,
796
		struct kfd_event_waiter *waiter,
797
		uint32_t event_id)
798 799 800 801 802 803
{
	struct kfd_event *ev = lookup_event_by_id(p, event_id);

	if (!ev)
		return -EINVAL;

804
	spin_lock(&ev->lock);
805
	waiter->event = ev;
806 807
	waiter->activated = ev->signaled;
	ev->signaled = ev->signaled && !ev->auto_reset;
808 809
	if (!waiter->activated)
		add_wait_queue(&ev->wq, &waiter->wait);
810
	spin_unlock(&ev->lock);
811 812 813 814

	return 0;
}

815 816 817 818 819 820 821 822 823 824 825
/* test_event_condition - Test condition of events being waited for
 * @all:           Return completion only if all events have signaled
 * @num_events:    Number of events to wait for
 * @event_waiters: Array of event waiters, one per event
 *
 * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have
 * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)
 * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of
 * the events have been destroyed.
 */
static uint32_t test_event_condition(bool all, uint32_t num_events,
826 827 828 829 830 831
				struct kfd_event_waiter *event_waiters)
{
	uint32_t i;
	uint32_t activated_count = 0;

	for (i = 0; i < num_events; i++) {
832
		if (!READ_ONCE(event_waiters[i].event))
833 834
			return KFD_IOC_WAIT_RESULT_FAIL;

835
		if (READ_ONCE(event_waiters[i].activated)) {
836
			if (!all)
837
				return KFD_IOC_WAIT_RESULT_COMPLETE;
838 839 840 841 842

			activated_count++;
		}
	}

843 844
	return activated_count == num_events ?
		KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;
845 846
}

847 848 849 850
/*
 * Copy event specific data, if defined.
 * Currently only memory exception events have additional data to copy to user
 */
851
static int copy_signaled_event_data(uint32_t num_events,
852 853 854 855 856 857 858 859 860 861 862 863
		struct kfd_event_waiter *event_waiters,
		struct kfd_event_data __user *data)
{
	struct kfd_hsa_memory_exception_data *src;
	struct kfd_hsa_memory_exception_data __user *dst;
	struct kfd_event_waiter *waiter;
	struct kfd_event *event;
	uint32_t i;

	for (i = 0; i < num_events; i++) {
		waiter = &event_waiters[i];
		event = waiter->event;
864 865
		if (!event)
			return -EINVAL; /* event was destroyed */
866
		if (waiter->activated && event->type == KFD_EVENT_TYPE_MEMORY) {
867
			dst = &data[i].memory_exception_data;
868 869 870
			src = &event->memory_exception_data;
			if (copy_to_user(dst, src,
				sizeof(struct kfd_hsa_memory_exception_data)))
871
				return -EFAULT;
872 873 874
		}
	}

875
	return 0;
876 877
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
{
	if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
		return 0;

	if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
		return MAX_SCHEDULE_TIMEOUT;

	/*
	 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
	 * but we consider them finite.
	 * This hack is wrong, but nobody is likely to notice.
	 */
	user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);

	return msecs_to_jiffies(user_timeout_ms) + 1;
}

896 897
static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,
			 bool undo_auto_reset)
898 899 900 901
{
	uint32_t i;

	for (i = 0; i < num_events; i++)
902 903
		if (waiters[i].event) {
			spin_lock(&waiters[i].event->lock);
904 905
			remove_wait_queue(&waiters[i].event->wq,
					  &waiters[i].wait);
906 907 908
			if (undo_auto_reset && waiters[i].activated &&
			    waiters[i].event && waiters[i].event->auto_reset)
				set_event(waiters[i].event);
909 910
			spin_unlock(&waiters[i].event->lock);
		}
911 912 913 914 915

	kfree(waiters);
}

int kfd_wait_on_events(struct kfd_process *p,
916
		       uint32_t num_events, void __user *data,
917
		       bool all, uint32_t *user_timeout_ms,
918
		       uint32_t *wait_result)
919
{
920 921
	struct kfd_event_data __user *events =
			(struct kfd_event_data __user *) data;
922 923
	uint32_t i;
	int ret = 0;
924

925
	struct kfd_event_waiter *event_waiters = NULL;
926
	long timeout = user_timeout_to_jiffies(*user_timeout_ms);
927

928 929 930 931 932 933
	event_waiters = alloc_event_waiters(num_events);
	if (!event_waiters) {
		ret = -ENOMEM;
		goto out;
	}

934 935 936
	/* Use p->event_mutex here to protect against concurrent creation and
	 * destruction of events while we initialize event_waiters.
	 */
937 938 939
	mutex_lock(&p->event_mutex);

	for (i = 0; i < num_events; i++) {
940
		struct kfd_event_data event_data;
941

942
		if (copy_from_user(&event_data, &events[i],
943 944
				sizeof(struct kfd_event_data))) {
			ret = -EFAULT;
945
			goto out_unlock;
946
		}
947

948 949
		ret = init_event_waiter(p, &event_waiters[i],
					event_data.event_id);
950
		if (ret)
951
			goto out_unlock;
952 953
	}

954
	/* Check condition once. */
955 956
	*wait_result = test_event_condition(all, num_events, event_waiters);
	if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {
957 958 959
		ret = copy_signaled_event_data(num_events,
					       event_waiters, events);
		goto out_unlock;
960 961 962 963 964
	} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {
		/* This should not happen. Events shouldn't be
		 * destroyed while we're holding the event_mutex
		 */
		goto out_unlock;
965 966
	}

967 968 969 970 971 972 973 974 975 976
	mutex_unlock(&p->event_mutex);

	while (true) {
		if (fatal_signal_pending(current)) {
			ret = -EINTR;
			break;
		}

		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
977 978 979 980
			if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&
			    *user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)
				*user_timeout_ms = jiffies_to_msecs(
					max(0l, timeout-1));
981 982 983
			break;
		}

984 985 986 987 988 989 990 991 992 993 994
		/* Set task state to interruptible sleep before
		 * checking wake-up conditions. A concurrent wake-up
		 * will put the task back into runnable state. In that
		 * case schedule_timeout will not put the task to
		 * sleep and we'll get a chance to re-check the
		 * updated conditions almost immediately. Otherwise,
		 * this race condition would lead to a soft hang or a
		 * very long sleep.
		 */
		set_current_state(TASK_INTERRUPTIBLE);

995 996 997
		*wait_result = test_event_condition(all, num_events,
						    event_waiters);
		if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)
998 999
			break;

1000
		if (timeout <= 0)
1001 1002
			break;

1003
		timeout = schedule_timeout(timeout);
1004 1005 1006
	}
	__set_current_state(TASK_RUNNING);

1007
	mutex_lock(&p->event_mutex);
1008 1009
	/* copy_signaled_event_data may sleep. So this has to happen
	 * after the task state is set back to RUNNING.
1010 1011 1012 1013 1014
	 *
	 * The event may also have been destroyed after signaling. So
	 * copy_signaled_event_data also must confirm that the event
	 * still exists. Therefore this must be under the p->event_mutex
	 * which is also held when events are destroyed.
1015 1016 1017 1018 1019 1020
	 */
	if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)
		ret = copy_signaled_event_data(num_events,
					       event_waiters, events);

out_unlock:
1021
	free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);
1022
	mutex_unlock(&p->event_mutex);
1023 1024 1025
out:
	if (ret)
		*wait_result = KFD_IOC_WAIT_RESULT_FAIL;
1026 1027
	else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)
		ret = -EIO;
1028 1029 1030 1031 1032 1033 1034

	return ret;
}

int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
{
	unsigned long pfn;
1035
	struct kfd_signal_page *page;
1036
	int ret;
1037

1038 1039
	/* check required size doesn't exceed the allocated size */
	if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <
1040
			get_order(vma->vm_end - vma->vm_start)) {
1041
		pr_err("Event page mmap requested illegal size\n");
1042 1043 1044
		return -EINVAL;
	}

1045
	page = p->signal_page;
1046 1047
	if (!page) {
		/* Probably KFD bug, but mmap is user-accessible. */
1048
		pr_debug("Signal page could not be found\n");
1049 1050 1051 1052 1053 1054 1055 1056 1057
		return -EINVAL;
	}

	pfn = __pa(page->kernel_address);
	pfn >>= PAGE_SHIFT;

	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
		       | VM_DONTDUMP | VM_PFNMAP;

1058
	pr_debug("Mapping signal page\n");
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	pr_debug("     start user address  == 0x%08lx\n", vma->vm_start);
	pr_debug("     end user address    == 0x%08lx\n", vma->vm_end);
	pr_debug("     pfn                 == 0x%016lX\n", pfn);
	pr_debug("     vm_flags            == 0x%08lX\n", vma->vm_flags);
	pr_debug("     size                == 0x%08lX\n",
			vma->vm_end - vma->vm_start);

	page->user_address = (uint64_t __user *)vma->vm_start;

	/* mapping the page to user process */
1069
	ret = remap_pfn_range(vma, vma->vm_start, pfn,
1070
			vma->vm_end - vma->vm_start, vma->vm_page_prot);
1071 1072 1073 1074
	if (!ret)
		p->signal_mapped_size = vma->vm_end - vma->vm_start;

	return ret;
1075
}
1076 1077

/*
1078
 * Assumes that p is not going away.
1079 1080 1081 1082 1083 1084
 */
static void lookup_events_by_type_and_signal(struct kfd_process *p,
		int type, void *event_data)
{
	struct kfd_hsa_memory_exception_data *ev_data;
	struct kfd_event *ev;
1085
	uint32_t id;
1086 1087 1088 1089
	bool send_signal = true;

	ev_data = (struct kfd_hsa_memory_exception_data *) event_data;

1090 1091
	rcu_read_lock();

1092 1093
	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
	idr_for_each_entry_continue(&p->event_idr, ev, id)
1094 1095 1096 1097 1098
		if (ev->type == type) {
			send_signal = false;
			dev_dbg(kfd_device,
					"Event found: id %X type %d",
					ev->event_id, ev->type);
1099
			spin_lock(&ev->lock);
1100 1101 1102
			set_event(ev);
			if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
				ev->memory_exception_data = *ev_data;
1103
			spin_unlock(&ev->lock);
1104 1105
		}

1106 1107
	if (type == KFD_EVENT_TYPE_MEMORY) {
		dev_warn(kfd_device,
1108 1109
			"Sending SIGSEGV to process %d (pasid 0x%x)",
				p->lead_thread->pid, p->pasid);
1110 1111 1112
		send_sig(SIGSEGV, p->lead_thread, 0);
	}

1113 1114
	/* Send SIGTERM no event of type "type" has been found*/
	if (send_signal) {
1115 1116
		if (send_sigterm) {
			dev_warn(kfd_device,
1117 1118
				"Sending SIGTERM to process %d (pasid 0x%x)",
					p->lead_thread->pid, p->pasid);
1119 1120 1121
			send_sig(SIGTERM, p->lead_thread, 0);
		} else {
			dev_err(kfd_device,
1122 1123
				"Process %d (pasid 0x%x) got unhandled exception",
				p->lead_thread->pid, p->pasid);
1124
		}
1125
	}
1126 1127

	rcu_read_unlock();
1128 1129
}

1130
#ifdef KFD_SUPPORT_IOMMU_V2
1131
void kfd_signal_iommu_event(struct kfd_dev *dev, u32 pasid,
1132 1133 1134 1135 1136
		unsigned long address, bool is_write_requested,
		bool is_execute_requested)
{
	struct kfd_hsa_memory_exception_data memory_exception_data;
	struct vm_area_struct *vma;
1137
	int user_gpu_id;
1138 1139 1140 1141

	/*
	 * Because we are called from arbitrary context (workqueue) as opposed
	 * to process context, kfd_process could attempt to exit while we are
1142
	 * running so the lookup function increments the process ref count.
1143 1144
	 */
	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
1145
	struct mm_struct *mm;
1146 1147 1148 1149

	if (!p)
		return; /* Presumably process exited. */

1150 1151 1152 1153 1154
	/* Take a safe reference to the mm_struct, which may otherwise
	 * disappear even while the kfd_process is still referenced.
	 */
	mm = get_task_mm(p->lead_thread);
	if (!mm) {
1155
		kfd_unref_process(p);
1156 1157 1158
		return; /* Process is exiting */
	}

1159 1160 1161 1162 1163
	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
	if (unlikely(user_gpu_id == -EINVAL)) {
		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
		return;
	}
1164 1165
	memset(&memory_exception_data, 0, sizeof(memory_exception_data));

1166
	mmap_read_lock(mm);
1167
	vma = find_vma(mm, address);
1168

1169
	memory_exception_data.gpu_id = user_gpu_id;
1170 1171 1172 1173 1174
	memory_exception_data.va = address;
	/* Set failure reason */
	memory_exception_data.failure.NotPresent = 1;
	memory_exception_data.failure.NoExecute = 0;
	memory_exception_data.failure.ReadOnly = 0;
1175 1176 1177 1178 1179 1180
	if (vma && address >= vma->vm_start) {
		memory_exception_data.failure.NotPresent = 0;

		if (is_write_requested && !(vma->vm_flags & VM_WRITE))
			memory_exception_data.failure.ReadOnly = 1;
		else
1181
			memory_exception_data.failure.ReadOnly = 0;
1182 1183 1184 1185 1186

		if (is_execute_requested && !(vma->vm_flags & VM_EXEC))
			memory_exception_data.failure.NoExecute = 1;
		else
			memory_exception_data.failure.NoExecute = 0;
1187 1188
	}

1189
	mmap_read_unlock(mm);
1190
	mmput(mm);
1191

1192 1193 1194 1195
	pr_debug("notpresent %d, noexecute %d, readonly %d\n",
			memory_exception_data.failure.NotPresent,
			memory_exception_data.failure.NoExecute,
			memory_exception_data.failure.ReadOnly);
1196

1197 1198 1199
	/* Workaround on Raven to not kill the process when memory is freed
	 * before IOMMU is able to finish processing all the excessive PPRs
	 */
1200 1201 1202

	if (KFD_GC_VERSION(dev) != IP_VERSION(9, 1, 0) &&
	    KFD_GC_VERSION(dev) != IP_VERSION(9, 2, 2) &&
1203
	    KFD_GC_VERSION(dev) != IP_VERSION(9, 3, 0))
1204 1205 1206
		lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_MEMORY,
				&memory_exception_data);

1207
	kfd_unref_process(p);
1208
}
1209
#endif /* KFD_SUPPORT_IOMMU_V2 */
1210

1211
void kfd_signal_hw_exception_event(u32 pasid)
1212 1213 1214 1215
{
	/*
	 * Because we are called from arbitrary context (workqueue) as opposed
	 * to process context, kfd_process could attempt to exit while we are
1216
	 * running so the lookup function increments the process ref count.
1217 1218 1219 1220 1221 1222 1223
	 */
	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);

	if (!p)
		return; /* Presumably process exited. */

	lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
1224
	kfd_unref_process(p);
1225
}
S
shaoyunl 已提交
1226

1227
void kfd_signal_vm_fault_event(struct kfd_dev *dev, u32 pasid,
S
shaoyunl 已提交
1228 1229 1230 1231 1232 1233
				struct kfd_vm_fault_info *info)
{
	struct kfd_event *ev;
	uint32_t id;
	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
	struct kfd_hsa_memory_exception_data memory_exception_data;
1234
	int user_gpu_id;
S
shaoyunl 已提交
1235 1236 1237

	if (!p)
		return; /* Presumably process exited. */
1238 1239 1240 1241 1242 1243 1244

	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
	if (unlikely(user_gpu_id == -EINVAL)) {
		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
		return;
	}

S
shaoyunl 已提交
1245
	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
1246
	memory_exception_data.gpu_id = user_gpu_id;
K
Kent Russell 已提交
1247
	memory_exception_data.failure.imprecise = true;
S
shaoyunl 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	/* Set failure reason */
	if (info) {
		memory_exception_data.va = (info->page_addr) << PAGE_SHIFT;
		memory_exception_data.failure.NotPresent =
			info->prot_valid ? 1 : 0;
		memory_exception_data.failure.NoExecute =
			info->prot_exec ? 1 : 0;
		memory_exception_data.failure.ReadOnly =
			info->prot_write ? 1 : 0;
		memory_exception_data.failure.imprecise = 0;
	}
1259 1260

	rcu_read_lock();
S
shaoyunl 已提交
1261 1262 1263 1264

	id = KFD_FIRST_NONSIGNAL_EVENT_ID;
	idr_for_each_entry_continue(&p->event_idr, ev, id)
		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1265
			spin_lock(&ev->lock);
S
shaoyunl 已提交
1266 1267
			ev->memory_exception_data = memory_exception_data;
			set_event(ev);
1268
			spin_unlock(&ev->lock);
S
shaoyunl 已提交
1269 1270
		}

1271
	rcu_read_unlock();
S
shaoyunl 已提交
1272 1273
	kfd_unref_process(p);
}
1274 1275 1276 1277

void kfd_signal_reset_event(struct kfd_dev *dev)
{
	struct kfd_hsa_hw_exception_data hw_exception_data;
1278
	struct kfd_hsa_memory_exception_data memory_exception_data;
1279 1280 1281 1282
	struct kfd_process *p;
	struct kfd_event *ev;
	unsigned int temp;
	uint32_t id, idx;
1283 1284 1285
	int reset_cause = atomic_read(&dev->sram_ecc_flag) ?
			KFD_HW_EXCEPTION_ECC :
			KFD_HW_EXCEPTION_GPU_HANG;
1286 1287 1288 1289

	/* Whole gpu reset caused by GPU hang and memory is lost */
	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
	hw_exception_data.memory_lost = 1;
1290 1291 1292 1293 1294
	hw_exception_data.reset_cause = reset_cause;

	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
	memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;
	memory_exception_data.failure.imprecise = true;
1295 1296 1297

	idx = srcu_read_lock(&kfd_processes_srcu);
	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
1298 1299 1300 1301 1302 1303 1304
		int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);

		if (unlikely(user_gpu_id == -EINVAL)) {
			WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
			continue;
		}

1305 1306
		rcu_read_lock();

1307
		id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1308
		idr_for_each_entry_continue(&p->event_idr, ev, id) {
1309
			if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1310
				spin_lock(&ev->lock);
1311
				ev->hw_exception_data = hw_exception_data;
1312
				ev->hw_exception_data.gpu_id = user_gpu_id;
1313
				set_event(ev);
1314
				spin_unlock(&ev->lock);
1315
			}
1316 1317
			if (ev->type == KFD_EVENT_TYPE_MEMORY &&
			    reset_cause == KFD_HW_EXCEPTION_ECC) {
1318
				spin_lock(&ev->lock);
1319
				ev->memory_exception_data = memory_exception_data;
1320
				ev->memory_exception_data.gpu_id = user_gpu_id;
1321
				set_event(ev);
1322
				spin_unlock(&ev->lock);
1323 1324
			}
		}
1325 1326

		rcu_read_unlock();
1327 1328 1329
	}
	srcu_read_unlock(&kfd_processes_srcu, idx);
}
1330 1331 1332 1333 1334 1335 1336 1337

void kfd_signal_poison_consumed_event(struct kfd_dev *dev, u32 pasid)
{
	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
	struct kfd_hsa_memory_exception_data memory_exception_data;
	struct kfd_hsa_hw_exception_data hw_exception_data;
	struct kfd_event *ev;
	uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;
1338
	int user_gpu_id;
1339 1340 1341 1342

	if (!p)
		return; /* Presumably process exited. */

1343 1344 1345 1346 1347 1348
	user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);
	if (unlikely(user_gpu_id == -EINVAL)) {
		WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);
		return;
	}

1349
	memset(&hw_exception_data, 0, sizeof(hw_exception_data));
1350
	hw_exception_data.gpu_id = user_gpu_id;
1351 1352 1353 1354 1355
	hw_exception_data.memory_lost = 1;
	hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;

	memset(&memory_exception_data, 0, sizeof(memory_exception_data));
	memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;
1356
	memory_exception_data.gpu_id = user_gpu_id;
1357 1358
	memory_exception_data.failure.imprecise = true;

1359 1360
	rcu_read_lock();

1361 1362
	idr_for_each_entry_continue(&p->event_idr, ev, id) {
		if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
1363
			spin_lock(&ev->lock);
1364 1365
			ev->hw_exception_data = hw_exception_data;
			set_event(ev);
1366
			spin_unlock(&ev->lock);
1367 1368 1369
		}

		if (ev->type == KFD_EVENT_TYPE_MEMORY) {
1370
			spin_lock(&ev->lock);
1371 1372
			ev->memory_exception_data = memory_exception_data;
			set_event(ev);
1373
			spin_unlock(&ev->lock);
1374 1375
		}
	}
1376 1377

	rcu_read_unlock();
1378 1379 1380

	/* user application will handle SIGBUS signal */
	send_sig(SIGBUS, p->lead_thread, 0);
1381 1382

	kfd_unref_process(p);
1383
}