drm/amdkfd: Add memory exception handling
[deliverable/linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_events.c
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 */
43 struct 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
65 struct 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
88 static uint64_t *page_slots(struct signal_page *page)
89 {
90 return page->kernel_address;
91 }
92
93 static 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
129 static 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
166 fail_alloc_signal_store:
167 kfree(page);
168 fail_alloc_signal_page:
169 return false;
170 }
171
172 static 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. */
190 static 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
200 static 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 */
220 static 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
231 static 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 */
243 static 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
282 static 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
289 static 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 return 0;
317 }
318
319 /*
320 * No non-signal events are supported yet.
321 * We create them as events that never signal.
322 * Set event calls from user-mode are failed.
323 */
324 static int create_other_event(struct kfd_process *p, struct kfd_event *ev)
325 {
326 ev->event_id = make_nonsignal_event_id(p);
327 if (ev->event_id == 0)
328 return -ENOMEM;
329
330 return 0;
331 }
332
333 void kfd_event_init_process(struct kfd_process *p)
334 {
335 mutex_init(&p->event_mutex);
336 hash_init(p->events);
337 INIT_LIST_HEAD(&p->signal_event_pages);
338 p->next_nonsignal_event_id = KFD_FIRST_NONSIGNAL_EVENT_ID;
339 p->signal_event_count = 0;
340 }
341
342 static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
343 {
344 if (ev->signal_page != NULL) {
345 release_event_notification_slot(ev->signal_page,
346 ev->signal_slot_index);
347 p->signal_event_count--;
348 }
349
350 /*
351 * Abandon the list of waiters. Individual waiting threads will
352 * clean up their own data.
353 */
354 list_del(&ev->waiters);
355
356 hash_del(&ev->events);
357 kfree(ev);
358 }
359
360 static void destroy_events(struct kfd_process *p)
361 {
362 struct kfd_event *ev;
363 struct hlist_node *tmp;
364 unsigned int hash_bkt;
365
366 hash_for_each_safe(p->events, hash_bkt, tmp, ev, events)
367 destroy_event(p, ev);
368 }
369
370 /*
371 * We assume that the process is being destroyed and there is no need to
372 * unmap the pages or keep bookkeeping data in order.
373 */
374 static void shutdown_signal_pages(struct kfd_process *p)
375 {
376 struct signal_page *page, *tmp;
377
378 list_for_each_entry_safe(page, tmp, &p->signal_event_pages,
379 event_pages) {
380 free_pages((unsigned long)page->kernel_address,
381 get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
382 kfree(page);
383 }
384 }
385
386 void kfd_event_free_process(struct kfd_process *p)
387 {
388 destroy_events(p);
389 shutdown_signal_pages(p);
390 }
391
392 static bool event_can_be_gpu_signaled(const struct kfd_event *ev)
393 {
394 return ev->type == KFD_EVENT_TYPE_SIGNAL ||
395 ev->type == KFD_EVENT_TYPE_DEBUG;
396 }
397
398 static bool event_can_be_cpu_signaled(const struct kfd_event *ev)
399 {
400 return ev->type == KFD_EVENT_TYPE_SIGNAL;
401 }
402
403 int kfd_event_create(struct file *devkfd, struct kfd_process *p,
404 uint32_t event_type, bool auto_reset, uint32_t node_id,
405 uint32_t *event_id, uint32_t *event_trigger_data,
406 uint64_t *event_page_offset, uint32_t *event_slot_index)
407 {
408 int ret = 0;
409 struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);
410
411 if (!ev)
412 return -ENOMEM;
413
414 ev->type = event_type;
415 ev->auto_reset = auto_reset;
416 ev->signaled = false;
417
418 INIT_LIST_HEAD(&ev->waiters);
419
420 *event_page_offset = 0;
421
422 mutex_lock(&p->event_mutex);
423
424 switch (event_type) {
425 case KFD_EVENT_TYPE_SIGNAL:
426 case KFD_EVENT_TYPE_DEBUG:
427 ret = create_signal_event(devkfd, p, ev);
428 if (!ret) {
429 *event_page_offset = (ev->signal_page->page_index |
430 KFD_MMAP_EVENTS_MASK);
431 *event_page_offset <<= PAGE_SHIFT;
432 *event_slot_index = ev->signal_slot_index;
433 }
434 break;
435 default:
436 ret = create_other_event(p, ev);
437 break;
438 }
439
440 if (!ret) {
441 hash_add(p->events, &ev->events, ev->event_id);
442
443 *event_id = ev->event_id;
444 *event_trigger_data = ev->event_id;
445 } else {
446 kfree(ev);
447 }
448
449 mutex_unlock(&p->event_mutex);
450
451 return ret;
452 }
453
454 /* Assumes that p is current. */
455 int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
456 {
457 struct kfd_event *ev;
458 int ret = 0;
459
460 mutex_lock(&p->event_mutex);
461
462 ev = lookup_event_by_id(p, event_id);
463
464 if (ev)
465 destroy_event(p, ev);
466 else
467 ret = -EINVAL;
468
469 mutex_unlock(&p->event_mutex);
470 return ret;
471 }
472
473 static void set_event(struct kfd_event *ev)
474 {
475 struct kfd_event_waiter *waiter;
476 struct kfd_event_waiter *next;
477
478 /* Auto reset if the list is non-empty and we're waking someone. */
479 ev->signaled = !ev->auto_reset || list_empty(&ev->waiters);
480
481 list_for_each_entry_safe(waiter, next, &ev->waiters, waiters) {
482 waiter->activated = true;
483
484 /* _init because free_waiters will call list_del */
485 list_del_init(&waiter->waiters);
486
487 wake_up_process(waiter->sleeping_task);
488 }
489 }
490
491 /* Assumes that p is current. */
492 int kfd_set_event(struct kfd_process *p, uint32_t event_id)
493 {
494 int ret = 0;
495 struct kfd_event *ev;
496
497 mutex_lock(&p->event_mutex);
498
499 ev = lookup_event_by_id(p, event_id);
500
501 if (ev && event_can_be_cpu_signaled(ev))
502 set_event(ev);
503 else
504 ret = -EINVAL;
505
506 mutex_unlock(&p->event_mutex);
507 return ret;
508 }
509
510 static void reset_event(struct kfd_event *ev)
511 {
512 ev->signaled = false;
513 }
514
515 /* Assumes that p is current. */
516 int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
517 {
518 int ret = 0;
519 struct kfd_event *ev;
520
521 mutex_lock(&p->event_mutex);
522
523 ev = lookup_event_by_id(p, event_id);
524
525 if (ev && event_can_be_cpu_signaled(ev))
526 reset_event(ev);
527 else
528 ret = -EINVAL;
529
530 mutex_unlock(&p->event_mutex);
531 return ret;
532
533 }
534
535 static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
536 {
537 page_slots(ev->signal_page)[ev->signal_slot_index] =
538 UNSIGNALED_EVENT_SLOT;
539 }
540
541 static bool is_slot_signaled(struct signal_page *page, unsigned int index)
542 {
543 return page_slots(page)[index] != UNSIGNALED_EVENT_SLOT;
544 }
545
546 static void set_event_from_interrupt(struct kfd_process *p,
547 struct kfd_event *ev)
548 {
549 if (ev && event_can_be_gpu_signaled(ev)) {
550 acknowledge_signal(p, ev);
551 set_event(ev);
552 }
553 }
554
555 void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
556 uint32_t valid_id_bits)
557 {
558 struct kfd_event *ev;
559
560 /*
561 * Because we are called from arbitrary context (workqueue) as opposed
562 * to process context, kfd_process could attempt to exit while we are
563 * running so the lookup function returns a locked process.
564 */
565 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
566
567 if (!p)
568 return; /* Presumably process exited. */
569
570 mutex_lock(&p->event_mutex);
571
572 if (valid_id_bits >= INTERRUPT_DATA_BITS) {
573 /* Partial ID is a full ID. */
574 ev = lookup_event_by_id(p, partial_id);
575 set_event_from_interrupt(p, ev);
576 } else {
577 /*
578 * Partial ID is in fact partial. For now we completely
579 * ignore it, but we could use any bits we did receive to
580 * search faster.
581 */
582 struct signal_page *page;
583 unsigned i;
584
585 list_for_each_entry(page, &p->signal_event_pages, event_pages)
586 for (i = 0; i < SLOTS_PER_PAGE; i++)
587 if (is_slot_signaled(page, i)) {
588 ev = lookup_event_by_page_slot(p,
589 page, i);
590 set_event_from_interrupt(p, ev);
591 }
592 }
593
594 mutex_unlock(&p->event_mutex);
595 mutex_unlock(&p->mutex);
596 }
597
598 static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
599 {
600 struct kfd_event_waiter *event_waiters;
601 uint32_t i;
602
603 event_waiters = kmalloc_array(num_events,
604 sizeof(struct kfd_event_waiter),
605 GFP_KERNEL);
606
607 for (i = 0; (event_waiters) && (i < num_events) ; i++) {
608 INIT_LIST_HEAD(&event_waiters[i].waiters);
609 event_waiters[i].sleeping_task = current;
610 event_waiters[i].activated = false;
611 }
612
613 return event_waiters;
614 }
615
616 static int init_event_waiter(struct kfd_process *p,
617 struct kfd_event_waiter *waiter,
618 uint32_t event_id,
619 uint32_t input_index)
620 {
621 struct kfd_event *ev = lookup_event_by_id(p, event_id);
622
623 if (!ev)
624 return -EINVAL;
625
626 waiter->event = ev;
627 waiter->input_index = input_index;
628 waiter->activated = ev->signaled;
629 ev->signaled = ev->signaled && !ev->auto_reset;
630
631 list_add(&waiter->waiters, &ev->waiters);
632
633 return 0;
634 }
635
636 static bool test_event_condition(bool all, uint32_t num_events,
637 struct kfd_event_waiter *event_waiters)
638 {
639 uint32_t i;
640 uint32_t activated_count = 0;
641
642 for (i = 0; i < num_events; i++) {
643 if (event_waiters[i].activated) {
644 if (!all)
645 return true;
646
647 activated_count++;
648 }
649 }
650
651 return activated_count == num_events;
652 }
653
654 /*
655 * Copy event specific data, if defined.
656 * Currently only memory exception events have additional data to copy to user
657 */
658 static bool copy_signaled_event_data(uint32_t num_events,
659 struct kfd_event_waiter *event_waiters,
660 struct kfd_event_data __user *data)
661 {
662 struct kfd_hsa_memory_exception_data *src;
663 struct kfd_hsa_memory_exception_data __user *dst;
664 struct kfd_event_waiter *waiter;
665 struct kfd_event *event;
666 uint32_t i;
667
668 for (i = 0; i < num_events; i++) {
669 waiter = &event_waiters[i];
670 event = waiter->event;
671 if (waiter->activated && event->type == KFD_EVENT_TYPE_MEMORY) {
672 dst = &data[waiter->input_index].memory_exception_data;
673 src = &event->memory_exception_data;
674 if (copy_to_user(dst, src,
675 sizeof(struct kfd_hsa_memory_exception_data)))
676 return false;
677 }
678 }
679
680 return true;
681
682 }
683
684
685
686 static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
687 {
688 if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
689 return 0;
690
691 if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
692 return MAX_SCHEDULE_TIMEOUT;
693
694 /*
695 * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
696 * but we consider them finite.
697 * This hack is wrong, but nobody is likely to notice.
698 */
699 user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
700
701 return msecs_to_jiffies(user_timeout_ms) + 1;
702 }
703
704 static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters)
705 {
706 uint32_t i;
707
708 for (i = 0; i < num_events; i++)
709 list_del(&waiters[i].waiters);
710
711 kfree(waiters);
712 }
713
714 int kfd_wait_on_events(struct kfd_process *p,
715 uint32_t num_events, void __user *data,
716 bool all, uint32_t user_timeout_ms,
717 enum kfd_event_wait_result *wait_result)
718 {
719 struct kfd_event_data __user *events =
720 (struct kfd_event_data __user *) data;
721 uint32_t i;
722 int ret = 0;
723 struct kfd_event_waiter *event_waiters = NULL;
724 long timeout = user_timeout_to_jiffies(user_timeout_ms);
725
726 mutex_lock(&p->event_mutex);
727
728 event_waiters = alloc_event_waiters(num_events);
729 if (!event_waiters) {
730 ret = -ENOMEM;
731 goto fail;
732 }
733
734 for (i = 0; i < num_events; i++) {
735 struct kfd_event_data event_data;
736
737 if (copy_from_user(&event_data, &events[i],
738 sizeof(struct kfd_event_data)))
739 goto fail;
740
741 ret = init_event_waiter(p, &event_waiters[i],
742 event_data.event_id, i);
743 if (ret)
744 goto fail;
745 }
746
747 mutex_unlock(&p->event_mutex);
748
749 while (true) {
750 if (fatal_signal_pending(current)) {
751 ret = -EINTR;
752 break;
753 }
754
755 if (signal_pending(current)) {
756 /*
757 * This is wrong when a nonzero, non-infinite timeout
758 * is specified. We need to use
759 * ERESTARTSYS_RESTARTBLOCK, but struct restart_block
760 * contains a union with data for each user and it's
761 * in generic kernel code that I don't want to
762 * touch yet.
763 */
764 ret = -ERESTARTSYS;
765 break;
766 }
767
768 if (test_event_condition(all, num_events, event_waiters)) {
769 if (copy_signaled_event_data(num_events,
770 event_waiters, events))
771 *wait_result = KFD_WAIT_COMPLETE;
772 else
773 *wait_result = KFD_WAIT_ERROR;
774 break;
775 }
776
777 if (timeout <= 0) {
778 *wait_result = KFD_WAIT_TIMEOUT;
779 break;
780 }
781
782 timeout = schedule_timeout_interruptible(timeout);
783 }
784 __set_current_state(TASK_RUNNING);
785
786 mutex_lock(&p->event_mutex);
787 free_waiters(num_events, event_waiters);
788 mutex_unlock(&p->event_mutex);
789
790 return ret;
791
792 fail:
793 if (event_waiters)
794 free_waiters(num_events, event_waiters);
795
796 mutex_unlock(&p->event_mutex);
797
798 *wait_result = KFD_WAIT_ERROR;
799
800 return ret;
801 }
802
803 int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
804 {
805
806 unsigned int page_index;
807 unsigned long pfn;
808 struct signal_page *page;
809
810 /* check required size is logical */
811 if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) !=
812 get_order(vma->vm_end - vma->vm_start)) {
813 pr_err("amdkfd: event page mmap requested illegal size\n");
814 return -EINVAL;
815 }
816
817 page_index = vma->vm_pgoff;
818
819 page = lookup_signal_page_by_index(p, page_index);
820 if (!page) {
821 /* Probably KFD bug, but mmap is user-accessible. */
822 pr_debug("signal page could not be found for page_index %u\n",
823 page_index);
824 return -EINVAL;
825 }
826
827 pfn = __pa(page->kernel_address);
828 pfn >>= PAGE_SHIFT;
829
830 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
831 | VM_DONTDUMP | VM_PFNMAP;
832
833 pr_debug("mapping signal page\n");
834 pr_debug(" start user address == 0x%08lx\n", vma->vm_start);
835 pr_debug(" end user address == 0x%08lx\n", vma->vm_end);
836 pr_debug(" pfn == 0x%016lX\n", pfn);
837 pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags);
838 pr_debug(" size == 0x%08lX\n",
839 vma->vm_end - vma->vm_start);
840
841 page->user_address = (uint64_t __user *)vma->vm_start;
842
843 /* mapping the page to user process */
844 return remap_pfn_range(vma, vma->vm_start, pfn,
845 vma->vm_end - vma->vm_start, vma->vm_page_prot);
846 }
847
848 /*
849 * Assumes that p->event_mutex is held and of course
850 * that p is not going away (current or locked).
851 */
852 static void lookup_events_by_type_and_signal(struct kfd_process *p,
853 int type, void *event_data)
854 {
855 struct kfd_hsa_memory_exception_data *ev_data;
856 struct kfd_event *ev;
857 int bkt;
858 bool send_signal = true;
859
860 ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
861
862 hash_for_each(p->events, bkt, ev, events)
863 if (ev->type == type) {
864 send_signal = false;
865 dev_dbg(kfd_device,
866 "Event found: id %X type %d",
867 ev->event_id, ev->type);
868 set_event(ev);
869 if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
870 ev->memory_exception_data = *ev_data;
871 }
872
873 /* Send SIGTERM no event of type "type" has been found*/
874 if (send_signal) {
875 dev_warn(kfd_device,
876 "Sending SIGTERM to HSA Process with PID %d ",
877 p->lead_thread->pid);
878 send_sig(SIGTERM, p->lead_thread, 0);
879 }
880 }
881
882 void kfd_signal_iommu_event(struct kfd_dev *dev, unsigned int pasid,
883 unsigned long address, bool is_write_requested,
884 bool is_execute_requested)
885 {
886 struct kfd_hsa_memory_exception_data memory_exception_data;
887 struct vm_area_struct *vma;
888
889 /*
890 * Because we are called from arbitrary context (workqueue) as opposed
891 * to process context, kfd_process could attempt to exit while we are
892 * running so the lookup function returns a locked process.
893 */
894 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
895
896 if (!p)
897 return; /* Presumably process exited. */
898
899 memset(&memory_exception_data, 0, sizeof(memory_exception_data));
900
901 down_read(&p->mm->mmap_sem);
902 vma = find_vma(p->mm, address);
903
904 memory_exception_data.gpu_id = dev->id;
905 memory_exception_data.va = address;
906 /* Set failure reason */
907 memory_exception_data.failure.NotPresent = 1;
908 memory_exception_data.failure.NoExecute = 0;
909 memory_exception_data.failure.ReadOnly = 0;
910 if (vma) {
911 if (vma->vm_start > address) {
912 memory_exception_data.failure.NotPresent = 1;
913 memory_exception_data.failure.NoExecute = 0;
914 memory_exception_data.failure.ReadOnly = 0;
915 } else {
916 memory_exception_data.failure.NotPresent = 0;
917 if (is_write_requested && !(vma->vm_flags & VM_WRITE))
918 memory_exception_data.failure.ReadOnly = 1;
919 else
920 memory_exception_data.failure.ReadOnly = 0;
921 if (is_execute_requested && !(vma->vm_flags & VM_EXEC))
922 memory_exception_data.failure.NoExecute = 1;
923 else
924 memory_exception_data.failure.NoExecute = 0;
925 }
926 }
927
928 up_read(&p->mm->mmap_sem);
929
930 mutex_lock(&p->event_mutex);
931
932 /* Lookup events by type and signal them */
933 lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_MEMORY,
934 &memory_exception_data);
935
936 mutex_unlock(&p->event_mutex);
937 mutex_unlock(&p->mutex);
938 }
This page took 0.066989 seconds and 5 git commands to generate.