Linux Audio

Check our new training course

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