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