Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 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> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 26 | #include <linux/sched/signal.h> |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 27 | #include <linux/sched/mm.h> |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 29 | #include <linux/mman.h> |
| 30 | #include <linux/memory.h> |
| 31 | #include "kfd_priv.h" |
| 32 | #include "kfd_events.h" |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 33 | #include "kfd_iommu.h" |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 34 | #include <linux/device.h> |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 35 | |
| 36 | /* |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 37 | * Wrapper around wait_queue_entry_t |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 38 | */ |
| 39 | struct kfd_event_waiter { |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 40 | wait_queue_entry_t wait; |
| 41 | struct kfd_event *event; /* Event to wait for */ |
| 42 | bool activated; /* Becomes true when event is signaled */ |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 46 | * Each signal event needs a 64-bit signal slot where the signaler will write |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 47 | * a 1 before sending an interrupt. (This is needed because some interrupts |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 48 | * do not contain enough spare data bits to identify an event.) |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 49 | * We get whole pages and map them to the process VA. |
| 50 | * Individual signal events use their event_id as slot index. |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 51 | */ |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 52 | struct kfd_signal_page { |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 53 | uint64_t *kernel_address; |
| 54 | uint64_t __user *user_address; |
Felix Kuehling | 0fc8011 | 2018-03-15 17:27:52 -0400 | [diff] [blame] | 55 | bool need_to_free_pages; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 56 | }; |
| 57 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 58 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 59 | static uint64_t *page_slots(struct kfd_signal_page *page) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 60 | { |
| 61 | return page->kernel_address; |
| 62 | } |
| 63 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 64 | static struct kfd_signal_page *allocate_signal_page(struct kfd_process *p) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 65 | { |
| 66 | void *backing_store; |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 67 | struct kfd_signal_page *page; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 68 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 69 | page = kzalloc(sizeof(*page), GFP_KERNEL); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 70 | if (!page) |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 71 | return NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 72 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 73 | backing_store = (void *) __get_free_pages(GFP_KERNEL, |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 74 | get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); |
| 75 | if (!backing_store) |
| 76 | goto fail_alloc_signal_store; |
| 77 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 78 | /* Initialize all events to unsignaled */ |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 79 | memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT, |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 80 | KFD_SIGNAL_EVENT_LIMIT * 8); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 81 | |
| 82 | page->kernel_address = backing_store; |
Felix Kuehling | 0fc8011 | 2018-03-15 17:27:52 -0400 | [diff] [blame] | 83 | page->need_to_free_pages = true; |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 84 | pr_debug("Allocated new event signal page at %p, for process %p\n", |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 85 | page, p); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 86 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 87 | return page; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 88 | |
| 89 | fail_alloc_signal_store: |
| 90 | kfree(page); |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 91 | return NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 92 | } |
| 93 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 94 | static int allocate_event_notification_slot(struct kfd_process *p, |
| 95 | struct kfd_event *ev) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 96 | { |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 97 | int id; |
| 98 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 99 | if (!p->signal_page) { |
| 100 | p->signal_page = allocate_signal_page(p); |
| 101 | if (!p->signal_page) |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 102 | return -ENOMEM; |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 103 | /* Oldest user mode expects 256 event slots */ |
| 104 | p->signal_mapped_size = 256*8; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 105 | } |
| 106 | |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 107 | /* |
| 108 | * Compatibility with old user mode: Only use signal slots |
| 109 | * user mode has mapped, may be less than |
| 110 | * KFD_SIGNAL_EVENT_LIMIT. This also allows future increase |
| 111 | * of the event limit without breaking user mode. |
| 112 | */ |
| 113 | id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8, |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 114 | GFP_KERNEL); |
| 115 | if (id < 0) |
| 116 | return id; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 117 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 118 | ev->event_id = id; |
| 119 | page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 120 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 121 | return 0; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 122 | } |
| 123 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 124 | /* |
| 125 | * Assumes that p->event_mutex is held and of course that p is not going |
| 126 | * away (current or locked). |
| 127 | */ |
| 128 | static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id) |
| 129 | { |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 130 | return idr_find(&p->event_idr, id); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 131 | } |
| 132 | |
Felix Kuehling | 3f04f96 | 2017-10-27 19:35:28 -0400 | [diff] [blame] | 133 | /** |
| 134 | * lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID |
| 135 | * @p: Pointer to struct kfd_process |
| 136 | * @id: ID to look up |
| 137 | * @bits: Number of valid bits in @id |
| 138 | * |
| 139 | * Finds the first signaled event with a matching partial ID. If no |
| 140 | * matching signaled event is found, returns NULL. In that case the |
| 141 | * caller should assume that the partial ID is invalid and do an |
| 142 | * exhaustive search of all siglaned events. |
| 143 | * |
| 144 | * If multiple events with the same partial ID signal at the same |
| 145 | * time, they will be found one interrupt at a time, not necessarily |
| 146 | * in the same order the interrupts occurred. As long as the number of |
| 147 | * interrupts is correct, all signaled events will be seen by the |
| 148 | * driver. |
| 149 | */ |
| 150 | static struct kfd_event *lookup_signaled_event_by_partial_id( |
| 151 | struct kfd_process *p, uint32_t id, uint32_t bits) |
| 152 | { |
| 153 | struct kfd_event *ev; |
| 154 | |
| 155 | if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT) |
| 156 | return NULL; |
| 157 | |
| 158 | /* Fast path for the common case that @id is not a partial ID |
| 159 | * and we only need a single lookup. |
| 160 | */ |
| 161 | if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) { |
| 162 | if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT) |
| 163 | return NULL; |
| 164 | |
| 165 | return idr_find(&p->event_idr, id); |
| 166 | } |
| 167 | |
| 168 | /* General case for partial IDs: Iterate over all matching IDs |
| 169 | * and find the first one that has signaled. |
| 170 | */ |
| 171 | for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) { |
| 172 | if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT) |
| 173 | continue; |
| 174 | |
| 175 | ev = idr_find(&p->event_idr, id); |
| 176 | } |
| 177 | |
| 178 | return ev; |
| 179 | } |
| 180 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 181 | static int create_signal_event(struct file *devkfd, |
| 182 | struct kfd_process *p, |
| 183 | struct kfd_event *ev) |
| 184 | { |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 185 | int ret; |
| 186 | |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 187 | if (p->signal_mapped_size && |
| 188 | p->signal_event_count == p->signal_mapped_size / 8) { |
Felix Kuehling | c986169 | 2017-09-20 18:10:22 -0400 | [diff] [blame] | 189 | if (!p->signal_event_limit_reached) { |
| 190 | pr_warn("Signal event wasn't created because limit was reached\n"); |
| 191 | p->signal_event_limit_reached = true; |
| 192 | } |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 193 | return -ENOSPC; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 194 | } |
| 195 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 196 | ret = allocate_event_notification_slot(p, ev); |
| 197 | if (ret) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 198 | pr_warn("Signal event wasn't created because out of kernel memory\n"); |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 199 | return ret; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | p->signal_event_count++; |
| 203 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 204 | ev->user_signal_address = &p->signal_page->user_address[ev->event_id]; |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 205 | pr_debug("Signal event number %zu created with id %d, address %p\n", |
Oded Gabbay | 6235e15 | 2015-04-30 18:05:36 +0300 | [diff] [blame] | 206 | p->signal_event_count, ev->event_id, |
| 207 | ev->user_signal_address); |
| 208 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 212 | static int create_other_event(struct kfd_process *p, struct kfd_event *ev) |
| 213 | { |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 214 | /* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an |
| 215 | * intentional integer overflow to -1 without a compiler |
| 216 | * warning. idr_alloc treats a negative value as "maximum |
| 217 | * signed integer". |
| 218 | */ |
| 219 | int id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID, |
| 220 | (uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1, |
| 221 | GFP_KERNEL); |
| 222 | |
| 223 | if (id < 0) |
| 224 | return id; |
| 225 | ev->event_id = id; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | void kfd_event_init_process(struct kfd_process *p) |
| 231 | { |
| 232 | mutex_init(&p->event_mutex); |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 233 | idr_init(&p->event_idr); |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 234 | p->signal_page = NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 235 | p->signal_event_count = 0; |
| 236 | } |
| 237 | |
| 238 | static void destroy_event(struct kfd_process *p, struct kfd_event *ev) |
| 239 | { |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 240 | struct kfd_event_waiter *waiter; |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 241 | |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 242 | /* Wake up pending waiters. They will return failure */ |
| 243 | list_for_each_entry(waiter, &ev->wq.head, wait.entry) |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 244 | waiter->event = NULL; |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 245 | wake_up_all(&ev->wq); |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 246 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 247 | if (ev->type == KFD_EVENT_TYPE_SIGNAL || |
| 248 | ev->type == KFD_EVENT_TYPE_DEBUG) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 249 | p->signal_event_count--; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 250 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 251 | idr_remove(&p->event_idr, ev->event_id); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 252 | kfree(ev); |
| 253 | } |
| 254 | |
| 255 | static void destroy_events(struct kfd_process *p) |
| 256 | { |
| 257 | struct kfd_event *ev; |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 258 | uint32_t id; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 259 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 260 | idr_for_each_entry(&p->event_idr, ev, id) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 261 | destroy_event(p, ev); |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 262 | idr_destroy(&p->event_idr); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * We assume that the process is being destroyed and there is no need to |
| 267 | * unmap the pages or keep bookkeeping data in order. |
| 268 | */ |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 269 | static void shutdown_signal_page(struct kfd_process *p) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 270 | { |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 271 | struct kfd_signal_page *page = p->signal_page; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 272 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 273 | if (page) { |
Felix Kuehling | 0fc8011 | 2018-03-15 17:27:52 -0400 | [diff] [blame] | 274 | if (page->need_to_free_pages) |
| 275 | free_pages((unsigned long)page->kernel_address, |
| 276 | get_order(KFD_SIGNAL_EVENT_LIMIT * 8)); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 277 | kfree(page); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void kfd_event_free_process(struct kfd_process *p) |
| 282 | { |
| 283 | destroy_events(p); |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 284 | shutdown_signal_page(p); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static bool event_can_be_gpu_signaled(const struct kfd_event *ev) |
| 288 | { |
| 289 | return ev->type == KFD_EVENT_TYPE_SIGNAL || |
| 290 | ev->type == KFD_EVENT_TYPE_DEBUG; |
| 291 | } |
| 292 | |
| 293 | static bool event_can_be_cpu_signaled(const struct kfd_event *ev) |
| 294 | { |
| 295 | return ev->type == KFD_EVENT_TYPE_SIGNAL; |
| 296 | } |
| 297 | |
Felix Kuehling | 0fc8011 | 2018-03-15 17:27:52 -0400 | [diff] [blame] | 298 | int kfd_event_page_set(struct kfd_process *p, void *kernel_address, |
| 299 | uint64_t size) |
| 300 | { |
| 301 | struct kfd_signal_page *page; |
| 302 | |
| 303 | if (p->signal_page) |
| 304 | return -EBUSY; |
| 305 | |
| 306 | page = kzalloc(sizeof(*page), GFP_KERNEL); |
| 307 | if (!page) |
| 308 | return -ENOMEM; |
| 309 | |
| 310 | /* Initialize all events to unsignaled */ |
| 311 | memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT, |
| 312 | KFD_SIGNAL_EVENT_LIMIT * 8); |
| 313 | |
| 314 | page->kernel_address = kernel_address; |
| 315 | |
| 316 | p->signal_page = page; |
| 317 | p->signal_mapped_size = size; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 322 | int kfd_event_create(struct file *devkfd, struct kfd_process *p, |
| 323 | uint32_t event_type, bool auto_reset, uint32_t node_id, |
| 324 | uint32_t *event_id, uint32_t *event_trigger_data, |
| 325 | uint64_t *event_page_offset, uint32_t *event_slot_index) |
| 326 | { |
| 327 | int ret = 0; |
| 328 | struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 329 | |
| 330 | if (!ev) |
| 331 | return -ENOMEM; |
| 332 | |
| 333 | ev->type = event_type; |
| 334 | ev->auto_reset = auto_reset; |
| 335 | ev->signaled = false; |
| 336 | |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 337 | init_waitqueue_head(&ev->wq); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 338 | |
| 339 | *event_page_offset = 0; |
| 340 | |
| 341 | mutex_lock(&p->event_mutex); |
| 342 | |
| 343 | switch (event_type) { |
| 344 | case KFD_EVENT_TYPE_SIGNAL: |
| 345 | case KFD_EVENT_TYPE_DEBUG: |
| 346 | ret = create_signal_event(devkfd, p, ev); |
| 347 | if (!ret) { |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 348 | *event_page_offset = KFD_MMAP_EVENTS_MASK; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 349 | *event_page_offset <<= PAGE_SHIFT; |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 350 | *event_slot_index = ev->event_id; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 351 | } |
| 352 | break; |
| 353 | default: |
| 354 | ret = create_other_event(p, ev); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (!ret) { |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 359 | *event_id = ev->event_id; |
| 360 | *event_trigger_data = ev->event_id; |
| 361 | } else { |
| 362 | kfree(ev); |
| 363 | } |
| 364 | |
| 365 | mutex_unlock(&p->event_mutex); |
| 366 | |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | /* Assumes that p is current. */ |
| 371 | int kfd_event_destroy(struct kfd_process *p, uint32_t event_id) |
| 372 | { |
| 373 | struct kfd_event *ev; |
| 374 | int ret = 0; |
| 375 | |
| 376 | mutex_lock(&p->event_mutex); |
| 377 | |
| 378 | ev = lookup_event_by_id(p, event_id); |
| 379 | |
| 380 | if (ev) |
| 381 | destroy_event(p, ev); |
| 382 | else |
| 383 | ret = -EINVAL; |
| 384 | |
| 385 | mutex_unlock(&p->event_mutex); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static void set_event(struct kfd_event *ev) |
| 390 | { |
| 391 | struct kfd_event_waiter *waiter; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 392 | |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 393 | /* Auto reset if the list is non-empty and we're waking |
| 394 | * someone. waitqueue_active is safe here because we're |
| 395 | * protected by the p->event_mutex, which is also held when |
| 396 | * updating the wait queues in kfd_wait_on_events. |
| 397 | */ |
| 398 | ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 399 | |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 400 | list_for_each_entry(waiter, &ev->wq.head, wait.entry) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 401 | waiter->activated = true; |
| 402 | |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 403 | wake_up_all(&ev->wq); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* Assumes that p is current. */ |
| 407 | int kfd_set_event(struct kfd_process *p, uint32_t event_id) |
| 408 | { |
| 409 | int ret = 0; |
| 410 | struct kfd_event *ev; |
| 411 | |
| 412 | mutex_lock(&p->event_mutex); |
| 413 | |
| 414 | ev = lookup_event_by_id(p, event_id); |
| 415 | |
| 416 | if (ev && event_can_be_cpu_signaled(ev)) |
| 417 | set_event(ev); |
| 418 | else |
| 419 | ret = -EINVAL; |
| 420 | |
| 421 | mutex_unlock(&p->event_mutex); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static void reset_event(struct kfd_event *ev) |
| 426 | { |
| 427 | ev->signaled = false; |
| 428 | } |
| 429 | |
| 430 | /* Assumes that p is current. */ |
| 431 | int kfd_reset_event(struct kfd_process *p, uint32_t event_id) |
| 432 | { |
| 433 | int ret = 0; |
| 434 | struct kfd_event *ev; |
| 435 | |
| 436 | mutex_lock(&p->event_mutex); |
| 437 | |
| 438 | ev = lookup_event_by_id(p, event_id); |
| 439 | |
| 440 | if (ev && event_can_be_cpu_signaled(ev)) |
| 441 | reset_event(ev); |
| 442 | else |
| 443 | ret = -EINVAL; |
| 444 | |
| 445 | mutex_unlock(&p->event_mutex); |
| 446 | return ret; |
| 447 | |
| 448 | } |
| 449 | |
| 450 | static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev) |
| 451 | { |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 452 | page_slots(p->signal_page)[ev->event_id] = UNSIGNALED_EVENT_SLOT; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void set_event_from_interrupt(struct kfd_process *p, |
| 456 | struct kfd_event *ev) |
| 457 | { |
| 458 | if (ev && event_can_be_gpu_signaled(ev)) { |
| 459 | acknowledge_signal(p, ev); |
| 460 | set_event(ev); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id, |
| 465 | uint32_t valid_id_bits) |
| 466 | { |
Felix Kuehling | 3f04f96 | 2017-10-27 19:35:28 -0400 | [diff] [blame] | 467 | struct kfd_event *ev = NULL; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * Because we are called from arbitrary context (workqueue) as opposed |
| 471 | * to process context, kfd_process could attempt to exit while we are |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 472 | * running so the lookup function increments the process ref count. |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 473 | */ |
| 474 | struct kfd_process *p = kfd_lookup_process_by_pasid(pasid); |
| 475 | |
| 476 | if (!p) |
| 477 | return; /* Presumably process exited. */ |
| 478 | |
| 479 | mutex_lock(&p->event_mutex); |
| 480 | |
Felix Kuehling | 3f04f96 | 2017-10-27 19:35:28 -0400 | [diff] [blame] | 481 | if (valid_id_bits) |
| 482 | ev = lookup_signaled_event_by_partial_id(p, partial_id, |
| 483 | valid_id_bits); |
| 484 | if (ev) { |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 485 | set_event_from_interrupt(p, ev); |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 486 | } else if (p->signal_page) { |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 487 | /* |
Felix Kuehling | 3f04f96 | 2017-10-27 19:35:28 -0400 | [diff] [blame] | 488 | * Partial ID lookup failed. Assume that the event ID |
| 489 | * in the interrupt payload was invalid and do an |
| 490 | * exhaustive search of signaled events. |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 491 | */ |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 492 | uint64_t *slots = page_slots(p->signal_page); |
| 493 | uint32_t id; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 494 | |
Felix Kuehling | 3f04f96 | 2017-10-27 19:35:28 -0400 | [diff] [blame] | 495 | if (valid_id_bits) |
| 496 | pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n", |
| 497 | partial_id, valid_id_bits); |
| 498 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 499 | if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT/2) { |
| 500 | /* With relatively few events, it's faster to |
| 501 | * iterate over the event IDR |
| 502 | */ |
| 503 | idr_for_each_entry(&p->event_idr, ev, id) { |
| 504 | if (id >= KFD_SIGNAL_EVENT_LIMIT) |
| 505 | break; |
| 506 | |
| 507 | if (slots[id] != UNSIGNALED_EVENT_SLOT) |
| 508 | set_event_from_interrupt(p, ev); |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 509 | } |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 510 | } else { |
| 511 | /* With relatively many events, it's faster to |
| 512 | * iterate over the signal slots and lookup |
| 513 | * only signaled events from the IDR. |
| 514 | */ |
| 515 | for (id = 0; id < KFD_SIGNAL_EVENT_LIMIT; id++) |
| 516 | if (slots[id] != UNSIGNALED_EVENT_SLOT) { |
| 517 | ev = lookup_event_by_id(p, id); |
| 518 | set_event_from_interrupt(p, ev); |
| 519 | } |
| 520 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | mutex_unlock(&p->event_mutex); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 524 | kfd_unref_process(p); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events) |
| 528 | { |
| 529 | struct kfd_event_waiter *event_waiters; |
| 530 | uint32_t i; |
| 531 | |
| 532 | event_waiters = kmalloc_array(num_events, |
| 533 | sizeof(struct kfd_event_waiter), |
| 534 | GFP_KERNEL); |
| 535 | |
| 536 | for (i = 0; (event_waiters) && (i < num_events) ; i++) { |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 537 | init_wait(&event_waiters[i].wait); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 538 | event_waiters[i].activated = false; |
| 539 | } |
| 540 | |
| 541 | return event_waiters; |
| 542 | } |
| 543 | |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 544 | static int init_event_waiter_get_status(struct kfd_process *p, |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 545 | struct kfd_event_waiter *waiter, |
Felix Kuehling | ebf947f | 2017-10-27 19:35:24 -0400 | [diff] [blame] | 546 | uint32_t event_id) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 547 | { |
| 548 | struct kfd_event *ev = lookup_event_by_id(p, event_id); |
| 549 | |
| 550 | if (!ev) |
| 551 | return -EINVAL; |
| 552 | |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 553 | waiter->event = ev; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 554 | waiter->activated = ev->signaled; |
| 555 | ev->signaled = ev->signaled && !ev->auto_reset; |
| 556 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 560 | static void init_event_waiter_add_to_waitlist(struct kfd_event_waiter *waiter) |
| 561 | { |
| 562 | struct kfd_event *ev = waiter->event; |
| 563 | |
| 564 | /* Only add to the wait list if we actually need to |
| 565 | * wait on this event. |
| 566 | */ |
| 567 | if (!waiter->activated) |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 568 | add_wait_queue(&ev->wq, &waiter->wait); |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 571 | /* test_event_condition - Test condition of events being waited for |
| 572 | * @all: Return completion only if all events have signaled |
| 573 | * @num_events: Number of events to wait for |
| 574 | * @event_waiters: Array of event waiters, one per event |
| 575 | * |
| 576 | * Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have |
| 577 | * signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all) |
| 578 | * events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of |
| 579 | * the events have been destroyed. |
| 580 | */ |
| 581 | static uint32_t test_event_condition(bool all, uint32_t num_events, |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 582 | struct kfd_event_waiter *event_waiters) |
| 583 | { |
| 584 | uint32_t i; |
| 585 | uint32_t activated_count = 0; |
| 586 | |
| 587 | for (i = 0; i < num_events; i++) { |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 588 | if (!event_waiters[i].event) |
| 589 | return KFD_IOC_WAIT_RESULT_FAIL; |
| 590 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 591 | if (event_waiters[i].activated) { |
| 592 | if (!all) |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 593 | return KFD_IOC_WAIT_RESULT_COMPLETE; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 594 | |
| 595 | activated_count++; |
| 596 | } |
| 597 | } |
| 598 | |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 599 | return activated_count == num_events ? |
| 600 | KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 601 | } |
| 602 | |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 603 | /* |
| 604 | * Copy event specific data, if defined. |
| 605 | * Currently only memory exception events have additional data to copy to user |
| 606 | */ |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 607 | static int copy_signaled_event_data(uint32_t num_events, |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 608 | struct kfd_event_waiter *event_waiters, |
| 609 | struct kfd_event_data __user *data) |
| 610 | { |
| 611 | struct kfd_hsa_memory_exception_data *src; |
| 612 | struct kfd_hsa_memory_exception_data __user *dst; |
| 613 | struct kfd_event_waiter *waiter; |
| 614 | struct kfd_event *event; |
| 615 | uint32_t i; |
| 616 | |
| 617 | for (i = 0; i < num_events; i++) { |
| 618 | waiter = &event_waiters[i]; |
| 619 | event = waiter->event; |
| 620 | if (waiter->activated && event->type == KFD_EVENT_TYPE_MEMORY) { |
Felix Kuehling | ebf947f | 2017-10-27 19:35:24 -0400 | [diff] [blame] | 621 | dst = &data[i].memory_exception_data; |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 622 | src = &event->memory_exception_data; |
| 623 | if (copy_to_user(dst, src, |
| 624 | sizeof(struct kfd_hsa_memory_exception_data))) |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 625 | return -EFAULT; |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 629 | return 0; |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 630 | |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 635 | static long user_timeout_to_jiffies(uint32_t user_timeout_ms) |
| 636 | { |
| 637 | if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE) |
| 638 | return 0; |
| 639 | |
| 640 | if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE) |
| 641 | return MAX_SCHEDULE_TIMEOUT; |
| 642 | |
| 643 | /* |
| 644 | * msecs_to_jiffies interprets all values above 2^31-1 as infinite, |
| 645 | * but we consider them finite. |
| 646 | * This hack is wrong, but nobody is likely to notice. |
| 647 | */ |
| 648 | user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF); |
| 649 | |
| 650 | return msecs_to_jiffies(user_timeout_ms) + 1; |
| 651 | } |
| 652 | |
| 653 | static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters) |
| 654 | { |
| 655 | uint32_t i; |
| 656 | |
| 657 | for (i = 0; i < num_events; i++) |
Felix Kuehling | 74e4071 | 2017-10-27 19:35:25 -0400 | [diff] [blame] | 658 | if (waiters[i].event) |
| 659 | remove_wait_queue(&waiters[i].event->wq, |
| 660 | &waiters[i].wait); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 661 | |
| 662 | kfree(waiters); |
| 663 | } |
| 664 | |
| 665 | int kfd_wait_on_events(struct kfd_process *p, |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 666 | uint32_t num_events, void __user *data, |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 667 | bool all, uint32_t user_timeout_ms, |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 668 | uint32_t *wait_result) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 669 | { |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 670 | struct kfd_event_data __user *events = |
| 671 | (struct kfd_event_data __user *) data; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 672 | uint32_t i; |
| 673 | int ret = 0; |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 674 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 675 | struct kfd_event_waiter *event_waiters = NULL; |
| 676 | long timeout = user_timeout_to_jiffies(user_timeout_ms); |
| 677 | |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 678 | event_waiters = alloc_event_waiters(num_events); |
| 679 | if (!event_waiters) { |
| 680 | ret = -ENOMEM; |
| 681 | goto out; |
| 682 | } |
| 683 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 684 | mutex_lock(&p->event_mutex); |
| 685 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 686 | for (i = 0; i < num_events; i++) { |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 687 | struct kfd_event_data event_data; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 688 | |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 689 | if (copy_from_user(&event_data, &events[i], |
Pan Bian | 8bf7938 | 2016-12-01 16:10:42 +0800 | [diff] [blame] | 690 | sizeof(struct kfd_event_data))) { |
| 691 | ret = -EFAULT; |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 692 | goto out_unlock; |
Pan Bian | 8bf7938 | 2016-12-01 16:10:42 +0800 | [diff] [blame] | 693 | } |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 694 | |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 695 | ret = init_event_waiter_get_status(p, &event_waiters[i], |
Felix Kuehling | ebf947f | 2017-10-27 19:35:24 -0400 | [diff] [blame] | 696 | event_data.event_id); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 697 | if (ret) |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 698 | goto out_unlock; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 699 | } |
| 700 | |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 701 | /* Check condition once. */ |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 702 | *wait_result = test_event_condition(all, num_events, event_waiters); |
| 703 | if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) { |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 704 | ret = copy_signaled_event_data(num_events, |
| 705 | event_waiters, events); |
| 706 | goto out_unlock; |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 707 | } else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) { |
| 708 | /* This should not happen. Events shouldn't be |
| 709 | * destroyed while we're holding the event_mutex |
| 710 | */ |
| 711 | goto out_unlock; |
Sean Keely | 1f9d09b | 2017-10-27 19:35:20 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 714 | /* Add to wait lists if we need to wait. */ |
| 715 | for (i = 0; i < num_events; i++) |
| 716 | init_event_waiter_add_to_waitlist(&event_waiters[i]); |
| 717 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 718 | mutex_unlock(&p->event_mutex); |
| 719 | |
| 720 | while (true) { |
| 721 | if (fatal_signal_pending(current)) { |
| 722 | ret = -EINTR; |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | if (signal_pending(current)) { |
| 727 | /* |
| 728 | * This is wrong when a nonzero, non-infinite timeout |
| 729 | * is specified. We need to use |
| 730 | * ERESTARTSYS_RESTARTBLOCK, but struct restart_block |
| 731 | * contains a union with data for each user and it's |
| 732 | * in generic kernel code that I don't want to |
| 733 | * touch yet. |
| 734 | */ |
| 735 | ret = -ERESTARTSYS; |
| 736 | break; |
| 737 | } |
| 738 | |
Sean Keely | d9aeec4 | 2017-10-27 19:35:21 -0400 | [diff] [blame] | 739 | /* Set task state to interruptible sleep before |
| 740 | * checking wake-up conditions. A concurrent wake-up |
| 741 | * will put the task back into runnable state. In that |
| 742 | * case schedule_timeout will not put the task to |
| 743 | * sleep and we'll get a chance to re-check the |
| 744 | * updated conditions almost immediately. Otherwise, |
| 745 | * this race condition would lead to a soft hang or a |
| 746 | * very long sleep. |
| 747 | */ |
| 748 | set_current_state(TASK_INTERRUPTIBLE); |
| 749 | |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 750 | *wait_result = test_event_condition(all, num_events, |
| 751 | event_waiters); |
| 752 | if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 753 | break; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 754 | |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 755 | if (timeout <= 0) |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 756 | break; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 757 | |
Sean Keely | d9aeec4 | 2017-10-27 19:35:21 -0400 | [diff] [blame] | 758 | timeout = schedule_timeout(timeout); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 759 | } |
| 760 | __set_current_state(TASK_RUNNING); |
| 761 | |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 762 | /* copy_signaled_event_data may sleep. So this has to happen |
| 763 | * after the task state is set back to RUNNING. |
| 764 | */ |
| 765 | if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) |
| 766 | ret = copy_signaled_event_data(num_events, |
| 767 | event_waiters, events); |
| 768 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 769 | mutex_lock(&p->event_mutex); |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 770 | out_unlock: |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 771 | free_waiters(num_events, event_waiters); |
| 772 | mutex_unlock(&p->event_mutex); |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 773 | out: |
| 774 | if (ret) |
| 775 | *wait_result = KFD_IOC_WAIT_RESULT_FAIL; |
Felix Kuehling | fe528c1 | 2017-10-27 19:35:23 -0400 | [diff] [blame] | 776 | else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL) |
| 777 | ret = -EIO; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 778 | |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma) |
| 783 | { |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 784 | unsigned long pfn; |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 785 | struct kfd_signal_page *page; |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 786 | int ret; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 787 | |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 788 | /* check required size doesn't exceed the allocated size */ |
| 789 | if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) < |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 790 | get_order(vma->vm_end - vma->vm_start)) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 791 | pr_err("Event page mmap requested illegal size\n"); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 792 | return -EINVAL; |
| 793 | } |
| 794 | |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 795 | page = p->signal_page; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 796 | if (!page) { |
| 797 | /* Probably KFD bug, but mmap is user-accessible. */ |
Felix Kuehling | 50cb7dd | 2017-10-27 19:35:26 -0400 | [diff] [blame] | 798 | pr_debug("Signal page could not be found\n"); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 799 | return -EINVAL; |
| 800 | } |
| 801 | |
| 802 | pfn = __pa(page->kernel_address); |
| 803 | pfn >>= PAGE_SHIFT; |
| 804 | |
| 805 | vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
| 806 | | VM_DONTDUMP | VM_PFNMAP; |
| 807 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 808 | pr_debug("Mapping signal page\n"); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 809 | pr_debug(" start user address == 0x%08lx\n", vma->vm_start); |
| 810 | pr_debug(" end user address == 0x%08lx\n", vma->vm_end); |
| 811 | pr_debug(" pfn == 0x%016lX\n", pfn); |
| 812 | pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags); |
| 813 | pr_debug(" size == 0x%08lX\n", |
| 814 | vma->vm_end - vma->vm_start); |
| 815 | |
| 816 | page->user_address = (uint64_t __user *)vma->vm_start; |
| 817 | |
| 818 | /* mapping the page to user process */ |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 819 | ret = remap_pfn_range(vma, vma->vm_start, pfn, |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 820 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
Felix Kuehling | b9a5d0a | 2017-10-27 19:35:29 -0400 | [diff] [blame] | 821 | if (!ret) |
| 822 | p->signal_mapped_size = vma->vm_end - vma->vm_start; |
| 823 | |
| 824 | return ret; |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 825 | } |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 826 | |
| 827 | /* |
| 828 | * Assumes that p->event_mutex is held and of course |
| 829 | * that p is not going away (current or locked). |
| 830 | */ |
| 831 | static void lookup_events_by_type_and_signal(struct kfd_process *p, |
| 832 | int type, void *event_data) |
| 833 | { |
| 834 | struct kfd_hsa_memory_exception_data *ev_data; |
| 835 | struct kfd_event *ev; |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 836 | uint32_t id; |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 837 | bool send_signal = true; |
| 838 | |
| 839 | ev_data = (struct kfd_hsa_memory_exception_data *) event_data; |
| 840 | |
Felix Kuehling | 482f077 | 2017-10-27 19:35:27 -0400 | [diff] [blame] | 841 | id = KFD_FIRST_NONSIGNAL_EVENT_ID; |
| 842 | idr_for_each_entry_continue(&p->event_idr, ev, id) |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 843 | if (ev->type == type) { |
| 844 | send_signal = false; |
| 845 | dev_dbg(kfd_device, |
| 846 | "Event found: id %X type %d", |
| 847 | ev->event_id, ev->type); |
| 848 | set_event(ev); |
| 849 | if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data) |
| 850 | ev->memory_exception_data = *ev_data; |
| 851 | } |
| 852 | |
| 853 | /* Send SIGTERM no event of type "type" has been found*/ |
| 854 | if (send_signal) { |
Oded Gabbay | 8166301 | 2014-12-24 13:30:52 +0200 | [diff] [blame] | 855 | if (send_sigterm) { |
| 856 | dev_warn(kfd_device, |
| 857 | "Sending SIGTERM to HSA Process with PID %d ", |
| 858 | p->lead_thread->pid); |
| 859 | send_sig(SIGTERM, p->lead_thread, 0); |
| 860 | } else { |
| 861 | dev_err(kfd_device, |
| 862 | "HSA Process (PID %d) got unhandled exception", |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 863 | p->lead_thread->pid); |
Oded Gabbay | 8166301 | 2014-12-24 13:30:52 +0200 | [diff] [blame] | 864 | } |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 868 | #ifdef KFD_SUPPORT_IOMMU_V2 |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 869 | void kfd_signal_iommu_event(struct kfd_dev *dev, unsigned int pasid, |
| 870 | unsigned long address, bool is_write_requested, |
| 871 | bool is_execute_requested) |
| 872 | { |
| 873 | struct kfd_hsa_memory_exception_data memory_exception_data; |
| 874 | struct vm_area_struct *vma; |
| 875 | |
| 876 | /* |
| 877 | * Because we are called from arbitrary context (workqueue) as opposed |
| 878 | * to process context, kfd_process could attempt to exit while we are |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 879 | * running so the lookup function increments the process ref count. |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 880 | */ |
| 881 | struct kfd_process *p = kfd_lookup_process_by_pasid(pasid); |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 882 | struct mm_struct *mm; |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 883 | |
| 884 | if (!p) |
| 885 | return; /* Presumably process exited. */ |
| 886 | |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 887 | /* Take a safe reference to the mm_struct, which may otherwise |
| 888 | * disappear even while the kfd_process is still referenced. |
| 889 | */ |
| 890 | mm = get_task_mm(p->lead_thread); |
| 891 | if (!mm) { |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 892 | kfd_unref_process(p); |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 893 | return; /* Process is exiting */ |
| 894 | } |
| 895 | |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 896 | memset(&memory_exception_data, 0, sizeof(memory_exception_data)); |
| 897 | |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 898 | down_read(&mm->mmap_sem); |
| 899 | vma = find_vma(mm, address); |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 900 | |
| 901 | memory_exception_data.gpu_id = dev->id; |
| 902 | memory_exception_data.va = address; |
| 903 | /* Set failure reason */ |
| 904 | memory_exception_data.failure.NotPresent = 1; |
| 905 | memory_exception_data.failure.NoExecute = 0; |
| 906 | memory_exception_data.failure.ReadOnly = 0; |
| 907 | if (vma) { |
| 908 | if (vma->vm_start > address) { |
| 909 | memory_exception_data.failure.NotPresent = 1; |
| 910 | memory_exception_data.failure.NoExecute = 0; |
| 911 | memory_exception_data.failure.ReadOnly = 0; |
| 912 | } else { |
| 913 | memory_exception_data.failure.NotPresent = 0; |
| 914 | if (is_write_requested && !(vma->vm_flags & VM_WRITE)) |
| 915 | memory_exception_data.failure.ReadOnly = 1; |
| 916 | else |
| 917 | memory_exception_data.failure.ReadOnly = 0; |
| 918 | if (is_execute_requested && !(vma->vm_flags & VM_EXEC)) |
| 919 | memory_exception_data.failure.NoExecute = 1; |
| 920 | else |
| 921 | memory_exception_data.failure.NoExecute = 0; |
| 922 | } |
| 923 | } |
| 924 | |
Felix Kuehling | 9b56bb1 | 2017-10-27 19:35:19 -0400 | [diff] [blame] | 925 | up_read(&mm->mmap_sem); |
| 926 | mmput(mm); |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 927 | |
| 928 | mutex_lock(&p->event_mutex); |
| 929 | |
| 930 | /* Lookup events by type and signal them */ |
| 931 | lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_MEMORY, |
| 932 | &memory_exception_data); |
| 933 | |
| 934 | mutex_unlock(&p->event_mutex); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 935 | kfd_unref_process(p); |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 936 | } |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 937 | #endif /* KFD_SUPPORT_IOMMU_V2 */ |
Alexey Skidanov | 930c5ff | 2014-11-25 10:34:31 +0200 | [diff] [blame] | 938 | |
| 939 | void kfd_signal_hw_exception_event(unsigned int pasid) |
| 940 | { |
| 941 | /* |
| 942 | * Because we are called from arbitrary context (workqueue) as opposed |
| 943 | * to process context, kfd_process could attempt to exit while we are |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 944 | * running so the lookup function increments the process ref count. |
Alexey Skidanov | 930c5ff | 2014-11-25 10:34:31 +0200 | [diff] [blame] | 945 | */ |
| 946 | struct kfd_process *p = kfd_lookup_process_by_pasid(pasid); |
| 947 | |
| 948 | if (!p) |
| 949 | return; /* Presumably process exited. */ |
| 950 | |
| 951 | mutex_lock(&p->event_mutex); |
| 952 | |
| 953 | /* Lookup events by type and signal them */ |
| 954 | lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL); |
| 955 | |
| 956 | mutex_unlock(&p->event_mutex); |
Felix Kuehling | abb208a | 2017-11-27 18:29:52 -0500 | [diff] [blame] | 957 | kfd_unref_process(p); |
Alexey Skidanov | 930c5ff | 2014-11-25 10:34:31 +0200 | [diff] [blame] | 958 | } |