kfd_events.c 35 KB

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