kfd_events.c 27 KB

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