kfd_events.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. #include <linux/mm_types.h>
  23. #include <linux/slab.h>
  24. #include <linux/types.h>
  25. #include <linux/sched.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/mm.h>
  28. #include <linux/mman.h>
  29. #include <linux/memory.h>
  30. #include "kfd_priv.h"
  31. #include "kfd_events.h"
  32. #include <linux/device.h>
  33. /*
  34. * A task can only be on a single wait_queue at a time, but we need to support
  35. * waiting on multiple events (any/all).
  36. * Instead of each event simply having a wait_queue with sleeping tasks, it
  37. * has a singly-linked list of tasks.
  38. * A thread that wants to sleep creates an array of these, one for each event
  39. * and adds one to each event's waiter chain.
  40. */
  41. struct kfd_event_waiter {
  42. struct list_head waiters;
  43. struct task_struct *sleeping_task;
  44. /* Transitions to true when the event this belongs to is signaled. */
  45. bool activated;
  46. /* Event */
  47. struct kfd_event *event;
  48. uint32_t input_index;
  49. };
  50. /*
  51. * Over-complicated pooled allocator for event notification slots.
  52. *
  53. * Each signal event needs a 64-bit signal slot where the signaler will write
  54. * a 1 before sending an interrupt.l (This is needed because some interrupts
  55. * do not contain enough spare data bits to identify an event.)
  56. * We get whole pages from vmalloc and map them to the process VA.
  57. * Individual signal events are then allocated a slot in a page.
  58. */
  59. struct signal_page {
  60. struct list_head event_pages; /* kfd_process.signal_event_pages */
  61. uint64_t *kernel_address;
  62. uint64_t __user *user_address;
  63. uint32_t page_index; /* Index into the mmap aperture. */
  64. unsigned int free_slots;
  65. unsigned long used_slot_bitmap[0];
  66. };
  67. #define SLOTS_PER_PAGE KFD_SIGNAL_EVENT_LIMIT
  68. #define SLOT_BITMAP_SIZE BITS_TO_LONGS(SLOTS_PER_PAGE)
  69. #define BITS_PER_PAGE (ilog2(SLOTS_PER_PAGE)+1)
  70. #define SIGNAL_PAGE_SIZE (sizeof(struct signal_page) + \
  71. SLOT_BITMAP_SIZE * sizeof(long))
  72. /*
  73. * For signal events, the event ID is used as the interrupt user data.
  74. * For SQ s_sendmsg interrupts, this is limited to 8 bits.
  75. */
  76. #define INTERRUPT_DATA_BITS 8
  77. #define SIGNAL_EVENT_ID_SLOT_SHIFT 0
  78. static uint64_t *page_slots(struct signal_page *page)
  79. {
  80. return page->kernel_address;
  81. }
  82. static bool allocate_free_slot(struct kfd_process *process,
  83. struct signal_page **out_page,
  84. unsigned int *out_slot_index)
  85. {
  86. struct signal_page *page;
  87. list_for_each_entry(page, &process->signal_event_pages, event_pages) {
  88. if (page->free_slots > 0) {
  89. unsigned int slot =
  90. find_first_zero_bit(page->used_slot_bitmap,
  91. SLOTS_PER_PAGE);
  92. __set_bit(slot, page->used_slot_bitmap);
  93. page->free_slots--;
  94. page_slots(page)[slot] = UNSIGNALED_EVENT_SLOT;
  95. *out_page = page;
  96. *out_slot_index = slot;
  97. pr_debug("allocated event signal slot in page %p, slot %d\n",
  98. page, slot);
  99. return true;
  100. }
  101. }
  102. pr_debug("No free event signal slots were found for process %p\n",
  103. process);
  104. return false;
  105. }
  106. #define list_tail_entry(head, type, member) \
  107. list_entry((head)->prev, type, member)
  108. static bool allocate_signal_page(struct file *devkfd, struct kfd_process *p)
  109. {
  110. void *backing_store;
  111. struct signal_page *page;
  112. page = kzalloc(SIGNAL_PAGE_SIZE, GFP_KERNEL);
  113. if (!page)
  114. goto fail_alloc_signal_page;
  115. page->free_slots = SLOTS_PER_PAGE;
  116. backing_store = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  117. get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
  118. if (!backing_store)
  119. goto fail_alloc_signal_store;
  120. /* prevent user-mode info leaks */
  121. memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT,
  122. KFD_SIGNAL_EVENT_LIMIT * 8);
  123. page->kernel_address = backing_store;
  124. if (list_empty(&p->signal_event_pages))
  125. page->page_index = 0;
  126. else
  127. page->page_index = list_tail_entry(&p->signal_event_pages,
  128. struct signal_page,
  129. event_pages)->page_index + 1;
  130. pr_debug("allocated new event signal page at %p, for process %p\n",
  131. page, p);
  132. pr_debug("page index is %d\n", page->page_index);
  133. list_add(&page->event_pages, &p->signal_event_pages);
  134. return true;
  135. fail_alloc_signal_store:
  136. kfree(page);
  137. fail_alloc_signal_page:
  138. return false;
  139. }
  140. static bool allocate_event_notification_slot(struct file *devkfd,
  141. struct kfd_process *p,
  142. struct signal_page **page,
  143. unsigned int *signal_slot_index)
  144. {
  145. bool ret;
  146. ret = allocate_free_slot(p, page, signal_slot_index);
  147. if (ret == false) {
  148. ret = allocate_signal_page(devkfd, p);
  149. if (ret == true)
  150. ret = allocate_free_slot(p, page, signal_slot_index);
  151. }
  152. return ret;
  153. }
  154. /* Assumes that the process's event_mutex is locked. */
  155. static void release_event_notification_slot(struct signal_page *page,
  156. size_t slot_index)
  157. {
  158. __clear_bit(slot_index, page->used_slot_bitmap);
  159. page->free_slots++;
  160. /* We don't free signal pages, they are retained by the process
  161. * and reused until it exits. */
  162. }
  163. static struct signal_page *lookup_signal_page_by_index(struct kfd_process *p,
  164. unsigned int page_index)
  165. {
  166. struct signal_page *page;
  167. /*
  168. * This is safe because we don't delete signal pages until the
  169. * process exits.
  170. */
  171. list_for_each_entry(page, &p->signal_event_pages, event_pages)
  172. if (page->page_index == page_index)
  173. return page;
  174. return NULL;
  175. }
  176. /*
  177. * Assumes that p->event_mutex is held and of course that p is not going
  178. * away (current or locked).
  179. */
  180. static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)
  181. {
  182. struct kfd_event *ev;
  183. hash_for_each_possible(p->events, ev, events, id)
  184. if (ev->event_id == id)
  185. return ev;
  186. return NULL;
  187. }
  188. static u32 make_signal_event_id(struct signal_page *page,
  189. unsigned int signal_slot_index)
  190. {
  191. return page->page_index |
  192. (signal_slot_index << SIGNAL_EVENT_ID_SLOT_SHIFT);
  193. }
  194. /*
  195. * Produce a kfd event id for a nonsignal event.
  196. * These are arbitrary numbers, so we do a sequential search through
  197. * the hash table for an unused number.
  198. */
  199. static u32 make_nonsignal_event_id(struct kfd_process *p)
  200. {
  201. u32 id;
  202. for (id = p->next_nonsignal_event_id;
  203. id < KFD_LAST_NONSIGNAL_EVENT_ID &&
  204. lookup_event_by_id(p, id) != NULL;
  205. id++)
  206. ;
  207. if (id < KFD_LAST_NONSIGNAL_EVENT_ID) {
  208. /*
  209. * What if id == LAST_NONSIGNAL_EVENT_ID - 1?
  210. * Then next_nonsignal_event_id = LAST_NONSIGNAL_EVENT_ID so
  211. * the first loop fails immediately and we proceed with the
  212. * wraparound loop below.
  213. */
  214. p->next_nonsignal_event_id = id + 1;
  215. return id;
  216. }
  217. for (id = KFD_FIRST_NONSIGNAL_EVENT_ID;
  218. id < KFD_LAST_NONSIGNAL_EVENT_ID &&
  219. lookup_event_by_id(p, id) != NULL;
  220. id++)
  221. ;
  222. if (id < KFD_LAST_NONSIGNAL_EVENT_ID) {
  223. p->next_nonsignal_event_id = id + 1;
  224. return id;
  225. }
  226. p->next_nonsignal_event_id = KFD_FIRST_NONSIGNAL_EVENT_ID;
  227. return 0;
  228. }
  229. static struct kfd_event *lookup_event_by_page_slot(struct kfd_process *p,
  230. struct signal_page *page,
  231. unsigned int signal_slot)
  232. {
  233. return lookup_event_by_id(p, make_signal_event_id(page, signal_slot));
  234. }
  235. static int create_signal_event(struct file *devkfd,
  236. struct kfd_process *p,
  237. struct kfd_event *ev)
  238. {
  239. if (p->signal_event_count == KFD_SIGNAL_EVENT_LIMIT) {
  240. pr_warn("amdkfd: Signal event wasn't created because limit was reached\n");
  241. return -ENOMEM;
  242. }
  243. if (!allocate_event_notification_slot(devkfd, p, &ev->signal_page,
  244. &ev->signal_slot_index)) {
  245. pr_warn("amdkfd: Signal event wasn't created because out of kernel memory\n");
  246. return -ENOMEM;
  247. }
  248. p->signal_event_count++;
  249. ev->user_signal_address =
  250. &ev->signal_page->user_address[ev->signal_slot_index];
  251. ev->event_id = make_signal_event_id(ev->signal_page,
  252. ev->signal_slot_index);
  253. pr_debug("signal event number %zu created with id %d, address %p\n",
  254. p->signal_event_count, ev->event_id,
  255. ev->user_signal_address);
  256. pr_debug("signal event number %zu created with id %d, address %p\n",
  257. p->signal_event_count, ev->event_id,
  258. ev->user_signal_address);
  259. return 0;
  260. }
  261. /*
  262. * No non-signal events are supported yet.
  263. * We create them as events that never signal.
  264. * Set event calls from user-mode are failed.
  265. */
  266. static int create_other_event(struct kfd_process *p, struct kfd_event *ev)
  267. {
  268. ev->event_id = make_nonsignal_event_id(p);
  269. if (ev->event_id == 0)
  270. return -ENOMEM;
  271. return 0;
  272. }
  273. void kfd_event_init_process(struct kfd_process *p)
  274. {
  275. mutex_init(&p->event_mutex);
  276. hash_init(p->events);
  277. INIT_LIST_HEAD(&p->signal_event_pages);
  278. p->next_nonsignal_event_id = KFD_FIRST_NONSIGNAL_EVENT_ID;
  279. p->signal_event_count = 0;
  280. }
  281. static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
  282. {
  283. if (ev->signal_page != NULL) {
  284. release_event_notification_slot(ev->signal_page,
  285. ev->signal_slot_index);
  286. p->signal_event_count--;
  287. }
  288. /*
  289. * Abandon the list of waiters. Individual waiting threads will
  290. * clean up their own data.
  291. */
  292. list_del(&ev->waiters);
  293. hash_del(&ev->events);
  294. kfree(ev);
  295. }
  296. static void destroy_events(struct kfd_process *p)
  297. {
  298. struct kfd_event *ev;
  299. struct hlist_node *tmp;
  300. unsigned int hash_bkt;
  301. hash_for_each_safe(p->events, hash_bkt, tmp, ev, events)
  302. destroy_event(p, ev);
  303. }
  304. /*
  305. * We assume that the process is being destroyed and there is no need to
  306. * unmap the pages or keep bookkeeping data in order.
  307. */
  308. static void shutdown_signal_pages(struct kfd_process *p)
  309. {
  310. struct signal_page *page, *tmp;
  311. list_for_each_entry_safe(page, tmp, &p->signal_event_pages,
  312. event_pages) {
  313. free_pages((unsigned long)page->kernel_address,
  314. get_order(KFD_SIGNAL_EVENT_LIMIT * 8));
  315. kfree(page);
  316. }
  317. }
  318. void kfd_event_free_process(struct kfd_process *p)
  319. {
  320. destroy_events(p);
  321. shutdown_signal_pages(p);
  322. }
  323. static bool event_can_be_gpu_signaled(const struct kfd_event *ev)
  324. {
  325. return ev->type == KFD_EVENT_TYPE_SIGNAL ||
  326. ev->type == KFD_EVENT_TYPE_DEBUG;
  327. }
  328. static bool event_can_be_cpu_signaled(const struct kfd_event *ev)
  329. {
  330. return ev->type == KFD_EVENT_TYPE_SIGNAL;
  331. }
  332. int kfd_event_create(struct file *devkfd, struct kfd_process *p,
  333. uint32_t event_type, bool auto_reset, uint32_t node_id,
  334. uint32_t *event_id, uint32_t *event_trigger_data,
  335. uint64_t *event_page_offset, uint32_t *event_slot_index)
  336. {
  337. int ret = 0;
  338. struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  339. if (!ev)
  340. return -ENOMEM;
  341. ev->type = event_type;
  342. ev->auto_reset = auto_reset;
  343. ev->signaled = false;
  344. INIT_LIST_HEAD(&ev->waiters);
  345. *event_page_offset = 0;
  346. mutex_lock(&p->event_mutex);
  347. switch (event_type) {
  348. case KFD_EVENT_TYPE_SIGNAL:
  349. case KFD_EVENT_TYPE_DEBUG:
  350. ret = create_signal_event(devkfd, p, ev);
  351. if (!ret) {
  352. *event_page_offset = (ev->signal_page->page_index |
  353. KFD_MMAP_EVENTS_MASK);
  354. *event_page_offset <<= PAGE_SHIFT;
  355. *event_slot_index = ev->signal_slot_index;
  356. }
  357. break;
  358. default:
  359. ret = create_other_event(p, ev);
  360. break;
  361. }
  362. if (!ret) {
  363. hash_add(p->events, &ev->events, ev->event_id);
  364. *event_id = ev->event_id;
  365. *event_trigger_data = ev->event_id;
  366. } else {
  367. kfree(ev);
  368. }
  369. mutex_unlock(&p->event_mutex);
  370. return ret;
  371. }
  372. /* Assumes that p is current. */
  373. int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)
  374. {
  375. struct kfd_event *ev;
  376. int ret = 0;
  377. mutex_lock(&p->event_mutex);
  378. ev = lookup_event_by_id(p, event_id);
  379. if (ev)
  380. destroy_event(p, ev);
  381. else
  382. ret = -EINVAL;
  383. mutex_unlock(&p->event_mutex);
  384. return ret;
  385. }
  386. static void set_event(struct kfd_event *ev)
  387. {
  388. struct kfd_event_waiter *waiter;
  389. struct kfd_event_waiter *next;
  390. /* Auto reset if the list is non-empty and we're waking someone. */
  391. ev->signaled = !ev->auto_reset || list_empty(&ev->waiters);
  392. list_for_each_entry_safe(waiter, next, &ev->waiters, waiters) {
  393. waiter->activated = true;
  394. /* _init because free_waiters will call list_del */
  395. list_del_init(&waiter->waiters);
  396. wake_up_process(waiter->sleeping_task);
  397. }
  398. }
  399. /* Assumes that p is current. */
  400. int kfd_set_event(struct kfd_process *p, uint32_t event_id)
  401. {
  402. int ret = 0;
  403. struct kfd_event *ev;
  404. mutex_lock(&p->event_mutex);
  405. ev = lookup_event_by_id(p, event_id);
  406. if (ev && event_can_be_cpu_signaled(ev))
  407. set_event(ev);
  408. else
  409. ret = -EINVAL;
  410. mutex_unlock(&p->event_mutex);
  411. return ret;
  412. }
  413. static void reset_event(struct kfd_event *ev)
  414. {
  415. ev->signaled = false;
  416. }
  417. /* Assumes that p is current. */
  418. int kfd_reset_event(struct kfd_process *p, uint32_t event_id)
  419. {
  420. int ret = 0;
  421. struct kfd_event *ev;
  422. mutex_lock(&p->event_mutex);
  423. ev = lookup_event_by_id(p, event_id);
  424. if (ev && event_can_be_cpu_signaled(ev))
  425. reset_event(ev);
  426. else
  427. ret = -EINVAL;
  428. mutex_unlock(&p->event_mutex);
  429. return ret;
  430. }
  431. static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
  432. {
  433. page_slots(ev->signal_page)[ev->signal_slot_index] =
  434. UNSIGNALED_EVENT_SLOT;
  435. }
  436. static bool is_slot_signaled(struct signal_page *page, unsigned int index)
  437. {
  438. return page_slots(page)[index] != UNSIGNALED_EVENT_SLOT;
  439. }
  440. static void set_event_from_interrupt(struct kfd_process *p,
  441. struct kfd_event *ev)
  442. {
  443. if (ev && event_can_be_gpu_signaled(ev)) {
  444. acknowledge_signal(p, ev);
  445. set_event(ev);
  446. }
  447. }
  448. void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
  449. uint32_t valid_id_bits)
  450. {
  451. struct kfd_event *ev;
  452. /*
  453. * Because we are called from arbitrary context (workqueue) as opposed
  454. * to process context, kfd_process could attempt to exit while we are
  455. * running so the lookup function returns a locked process.
  456. */
  457. struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
  458. if (!p)
  459. return; /* Presumably process exited. */
  460. mutex_lock(&p->event_mutex);
  461. if (valid_id_bits >= INTERRUPT_DATA_BITS) {
  462. /* Partial ID is a full ID. */
  463. ev = lookup_event_by_id(p, partial_id);
  464. set_event_from_interrupt(p, ev);
  465. } else {
  466. /*
  467. * Partial ID is in fact partial. For now we completely
  468. * ignore it, but we could use any bits we did receive to
  469. * search faster.
  470. */
  471. struct signal_page *page;
  472. unsigned i;
  473. list_for_each_entry(page, &p->signal_event_pages, event_pages)
  474. for (i = 0; i < SLOTS_PER_PAGE; i++)
  475. if (is_slot_signaled(page, i)) {
  476. ev = lookup_event_by_page_slot(p,
  477. page, i);
  478. set_event_from_interrupt(p, ev);
  479. }
  480. }
  481. mutex_unlock(&p->event_mutex);
  482. mutex_unlock(&p->mutex);
  483. }
  484. static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)
  485. {
  486. struct kfd_event_waiter *event_waiters;
  487. uint32_t i;
  488. event_waiters = kmalloc_array(num_events,
  489. sizeof(struct kfd_event_waiter),
  490. GFP_KERNEL);
  491. for (i = 0; (event_waiters) && (i < num_events) ; i++) {
  492. INIT_LIST_HEAD(&event_waiters[i].waiters);
  493. event_waiters[i].sleeping_task = current;
  494. event_waiters[i].activated = false;
  495. }
  496. return event_waiters;
  497. }
  498. static int init_event_waiter(struct kfd_process *p,
  499. struct kfd_event_waiter *waiter,
  500. uint32_t event_id,
  501. uint32_t input_index)
  502. {
  503. struct kfd_event *ev = lookup_event_by_id(p, event_id);
  504. if (!ev)
  505. return -EINVAL;
  506. waiter->event = ev;
  507. waiter->input_index = input_index;
  508. waiter->activated = ev->signaled;
  509. ev->signaled = ev->signaled && !ev->auto_reset;
  510. list_add(&waiter->waiters, &ev->waiters);
  511. return 0;
  512. }
  513. static bool test_event_condition(bool all, uint32_t num_events,
  514. struct kfd_event_waiter *event_waiters)
  515. {
  516. uint32_t i;
  517. uint32_t activated_count = 0;
  518. for (i = 0; i < num_events; i++) {
  519. if (event_waiters[i].activated) {
  520. if (!all)
  521. return true;
  522. activated_count++;
  523. }
  524. }
  525. return activated_count == num_events;
  526. }
  527. /*
  528. * Copy event specific data, if defined.
  529. * Currently only memory exception events have additional data to copy to user
  530. */
  531. static bool copy_signaled_event_data(uint32_t num_events,
  532. struct kfd_event_waiter *event_waiters,
  533. struct kfd_event_data __user *data)
  534. {
  535. struct kfd_hsa_memory_exception_data *src;
  536. struct kfd_hsa_memory_exception_data __user *dst;
  537. struct kfd_event_waiter *waiter;
  538. struct kfd_event *event;
  539. uint32_t i;
  540. for (i = 0; i < num_events; i++) {
  541. waiter = &event_waiters[i];
  542. event = waiter->event;
  543. if (waiter->activated && event->type == KFD_EVENT_TYPE_MEMORY) {
  544. dst = &data[waiter->input_index].memory_exception_data;
  545. src = &event->memory_exception_data;
  546. if (copy_to_user(dst, src,
  547. sizeof(struct kfd_hsa_memory_exception_data)))
  548. return false;
  549. }
  550. }
  551. return true;
  552. }
  553. static long user_timeout_to_jiffies(uint32_t user_timeout_ms)
  554. {
  555. if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)
  556. return 0;
  557. if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)
  558. return MAX_SCHEDULE_TIMEOUT;
  559. /*
  560. * msecs_to_jiffies interprets all values above 2^31-1 as infinite,
  561. * but we consider them finite.
  562. * This hack is wrong, but nobody is likely to notice.
  563. */
  564. user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);
  565. return msecs_to_jiffies(user_timeout_ms) + 1;
  566. }
  567. static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters)
  568. {
  569. uint32_t i;
  570. for (i = 0; i < num_events; i++)
  571. list_del(&waiters[i].waiters);
  572. kfree(waiters);
  573. }
  574. int kfd_wait_on_events(struct kfd_process *p,
  575. uint32_t num_events, void __user *data,
  576. bool all, uint32_t user_timeout_ms,
  577. enum kfd_event_wait_result *wait_result)
  578. {
  579. struct kfd_event_data __user *events =
  580. (struct kfd_event_data __user *) data;
  581. uint32_t i;
  582. int ret = 0;
  583. struct kfd_event_waiter *event_waiters = NULL;
  584. long timeout = user_timeout_to_jiffies(user_timeout_ms);
  585. mutex_lock(&p->event_mutex);
  586. event_waiters = alloc_event_waiters(num_events);
  587. if (!event_waiters) {
  588. ret = -ENOMEM;
  589. goto fail;
  590. }
  591. for (i = 0; i < num_events; i++) {
  592. struct kfd_event_data event_data;
  593. if (copy_from_user(&event_data, &events[i],
  594. sizeof(struct kfd_event_data))) {
  595. ret = -EFAULT;
  596. goto fail;
  597. }
  598. ret = init_event_waiter(p, &event_waiters[i],
  599. event_data.event_id, i);
  600. if (ret)
  601. goto fail;
  602. }
  603. mutex_unlock(&p->event_mutex);
  604. while (true) {
  605. if (fatal_signal_pending(current)) {
  606. ret = -EINTR;
  607. break;
  608. }
  609. if (signal_pending(current)) {
  610. /*
  611. * This is wrong when a nonzero, non-infinite timeout
  612. * is specified. We need to use
  613. * ERESTARTSYS_RESTARTBLOCK, but struct restart_block
  614. * contains a union with data for each user and it's
  615. * in generic kernel code that I don't want to
  616. * touch yet.
  617. */
  618. ret = -ERESTARTSYS;
  619. break;
  620. }
  621. if (test_event_condition(all, num_events, event_waiters)) {
  622. if (copy_signaled_event_data(num_events,
  623. event_waiters, events))
  624. *wait_result = KFD_WAIT_COMPLETE;
  625. else
  626. *wait_result = KFD_WAIT_ERROR;
  627. break;
  628. }
  629. if (timeout <= 0) {
  630. *wait_result = KFD_WAIT_TIMEOUT;
  631. break;
  632. }
  633. timeout = schedule_timeout_interruptible(timeout);
  634. }
  635. __set_current_state(TASK_RUNNING);
  636. mutex_lock(&p->event_mutex);
  637. free_waiters(num_events, event_waiters);
  638. mutex_unlock(&p->event_mutex);
  639. return ret;
  640. fail:
  641. if (event_waiters)
  642. free_waiters(num_events, event_waiters);
  643. mutex_unlock(&p->event_mutex);
  644. *wait_result = KFD_WAIT_ERROR;
  645. return ret;
  646. }
  647. int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)
  648. {
  649. unsigned int page_index;
  650. unsigned long pfn;
  651. struct signal_page *page;
  652. /* check required size is logical */
  653. if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) !=
  654. get_order(vma->vm_end - vma->vm_start)) {
  655. pr_err("amdkfd: event page mmap requested illegal size\n");
  656. return -EINVAL;
  657. }
  658. page_index = vma->vm_pgoff;
  659. page = lookup_signal_page_by_index(p, page_index);
  660. if (!page) {
  661. /* Probably KFD bug, but mmap is user-accessible. */
  662. pr_debug("signal page could not be found for page_index %u\n",
  663. page_index);
  664. return -EINVAL;
  665. }
  666. pfn = __pa(page->kernel_address);
  667. pfn >>= PAGE_SHIFT;
  668. vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE
  669. | VM_DONTDUMP | VM_PFNMAP;
  670. pr_debug("mapping signal page\n");
  671. pr_debug(" start user address == 0x%08lx\n", vma->vm_start);
  672. pr_debug(" end user address == 0x%08lx\n", vma->vm_end);
  673. pr_debug(" pfn == 0x%016lX\n", pfn);
  674. pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags);
  675. pr_debug(" size == 0x%08lX\n",
  676. vma->vm_end - vma->vm_start);
  677. page->user_address = (uint64_t __user *)vma->vm_start;
  678. /* mapping the page to user process */
  679. return remap_pfn_range(vma, vma->vm_start, pfn,
  680. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  681. }
  682. /*
  683. * Assumes that p->event_mutex is held and of course
  684. * that p is not going away (current or locked).
  685. */
  686. static void lookup_events_by_type_and_signal(struct kfd_process *p,
  687. int type, void *event_data)
  688. {
  689. struct kfd_hsa_memory_exception_data *ev_data;
  690. struct kfd_event *ev;
  691. int bkt;
  692. bool send_signal = true;
  693. ev_data = (struct kfd_hsa_memory_exception_data *) event_data;
  694. hash_for_each(p->events, bkt, ev, events)
  695. if (ev->type == type) {
  696. send_signal = false;
  697. dev_dbg(kfd_device,
  698. "Event found: id %X type %d",
  699. ev->event_id, ev->type);
  700. set_event(ev);
  701. if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
  702. ev->memory_exception_data = *ev_data;
  703. }
  704. /* Send SIGTERM no event of type "type" has been found*/
  705. if (send_signal) {
  706. if (send_sigterm) {
  707. dev_warn(kfd_device,
  708. "Sending SIGTERM to HSA Process with PID %d ",
  709. p->lead_thread->pid);
  710. send_sig(SIGTERM, p->lead_thread, 0);
  711. } else {
  712. dev_err(kfd_device,
  713. "HSA Process (PID %d) got unhandled exception",
  714. p->lead_thread->pid);
  715. }
  716. }
  717. }
  718. void kfd_signal_iommu_event(struct kfd_dev *dev, unsigned int pasid,
  719. unsigned long address, bool is_write_requested,
  720. bool is_execute_requested)
  721. {
  722. struct kfd_hsa_memory_exception_data memory_exception_data;
  723. struct vm_area_struct *vma;
  724. /*
  725. * Because we are called from arbitrary context (workqueue) as opposed
  726. * to process context, kfd_process could attempt to exit while we are
  727. * running so the lookup function returns a locked process.
  728. */
  729. struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
  730. if (!p)
  731. return; /* Presumably process exited. */
  732. memset(&memory_exception_data, 0, sizeof(memory_exception_data));
  733. down_read(&p->mm->mmap_sem);
  734. vma = find_vma(p->mm, address);
  735. memory_exception_data.gpu_id = dev->id;
  736. memory_exception_data.va = address;
  737. /* Set failure reason */
  738. memory_exception_data.failure.NotPresent = 1;
  739. memory_exception_data.failure.NoExecute = 0;
  740. memory_exception_data.failure.ReadOnly = 0;
  741. if (vma) {
  742. if (vma->vm_start > address) {
  743. memory_exception_data.failure.NotPresent = 1;
  744. memory_exception_data.failure.NoExecute = 0;
  745. memory_exception_data.failure.ReadOnly = 0;
  746. } else {
  747. memory_exception_data.failure.NotPresent = 0;
  748. if (is_write_requested && !(vma->vm_flags & VM_WRITE))
  749. memory_exception_data.failure.ReadOnly = 1;
  750. else
  751. memory_exception_data.failure.ReadOnly = 0;
  752. if (is_execute_requested && !(vma->vm_flags & VM_EXEC))
  753. memory_exception_data.failure.NoExecute = 1;
  754. else
  755. memory_exception_data.failure.NoExecute = 0;
  756. }
  757. }
  758. up_read(&p->mm->mmap_sem);
  759. mutex_lock(&p->event_mutex);
  760. /* Lookup events by type and signal them */
  761. lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_MEMORY,
  762. &memory_exception_data);
  763. mutex_unlock(&p->event_mutex);
  764. mutex_unlock(&p->mutex);
  765. }
  766. void kfd_signal_hw_exception_event(unsigned int pasid)
  767. {
  768. /*
  769. * Because we are called from arbitrary context (workqueue) as opposed
  770. * to process context, kfd_process could attempt to exit while we are
  771. * running so the lookup function returns a locked process.
  772. */
  773. struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
  774. if (!p)
  775. return; /* Presumably process exited. */
  776. mutex_lock(&p->event_mutex);
  777. /* Lookup events by type and signal them */
  778. lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);
  779. mutex_unlock(&p->event_mutex);
  780. mutex_unlock(&p->mutex);
  781. }