kfd_smi_events.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2020-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/poll.h>
  24. #include <linux/wait.h>
  25. #include <linux/anon_inodes.h>
  26. #include <uapi/linux/kfd_ioctl.h>
  27. #include "amdgpu.h"
  28. #include "amdgpu_vm.h"
  29. #include "kfd_priv.h"
  30. #include "kfd_smi_events.h"
  31. #include "amdgpu_reset.h"
  32. struct kfd_smi_client {
  33. struct list_head list;
  34. struct kfifo fifo;
  35. wait_queue_head_t wait_queue;
  36. /* events enabled */
  37. uint64_t events;
  38. struct kfd_node *dev;
  39. spinlock_t lock;
  40. struct rcu_head rcu;
  41. pid_t pid;
  42. bool suser;
  43. };
  44. #define MAX_KFIFO_SIZE 1024
  45. static __poll_t kfd_smi_ev_poll(struct file *, struct poll_table_struct *);
  46. static ssize_t kfd_smi_ev_read(struct file *, char __user *, size_t, loff_t *);
  47. static ssize_t kfd_smi_ev_write(struct file *, const char __user *, size_t,
  48. loff_t *);
  49. static int kfd_smi_ev_release(struct inode *, struct file *);
  50. static const char kfd_smi_name[] = "kfd_smi_ev";
  51. static const struct file_operations kfd_smi_ev_fops = {
  52. .owner = THIS_MODULE,
  53. .poll = kfd_smi_ev_poll,
  54. .read = kfd_smi_ev_read,
  55. .write = kfd_smi_ev_write,
  56. .release = kfd_smi_ev_release
  57. };
  58. static __poll_t kfd_smi_ev_poll(struct file *filep,
  59. struct poll_table_struct *wait)
  60. {
  61. struct kfd_smi_client *client = filep->private_data;
  62. __poll_t mask = 0;
  63. poll_wait(filep, &client->wait_queue, wait);
  64. spin_lock(&client->lock);
  65. if (!kfifo_is_empty(&client->fifo))
  66. mask = EPOLLIN | EPOLLRDNORM;
  67. spin_unlock(&client->lock);
  68. return mask;
  69. }
  70. static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
  71. size_t size, loff_t *offset)
  72. {
  73. int ret;
  74. size_t to_copy;
  75. struct kfd_smi_client *client = filep->private_data;
  76. unsigned char *buf;
  77. size = min_t(size_t, size, MAX_KFIFO_SIZE);
  78. buf = kmalloc(size, GFP_KERNEL);
  79. if (!buf)
  80. return -ENOMEM;
  81. /* kfifo_to_user can sleep so we can't use spinlock protection around
  82. * it. Instead, we kfifo out as spinlocked then copy them to the user.
  83. */
  84. spin_lock(&client->lock);
  85. to_copy = kfifo_len(&client->fifo);
  86. if (!to_copy) {
  87. spin_unlock(&client->lock);
  88. ret = -EAGAIN;
  89. goto ret_err;
  90. }
  91. to_copy = min(size, to_copy);
  92. ret = kfifo_out(&client->fifo, buf, to_copy);
  93. spin_unlock(&client->lock);
  94. if (ret <= 0) {
  95. ret = -EAGAIN;
  96. goto ret_err;
  97. }
  98. ret = copy_to_user(user, buf, to_copy);
  99. if (ret) {
  100. ret = -EFAULT;
  101. goto ret_err;
  102. }
  103. kfree(buf);
  104. return to_copy;
  105. ret_err:
  106. kfree(buf);
  107. return ret;
  108. }
  109. static ssize_t kfd_smi_ev_write(struct file *filep, const char __user *user,
  110. size_t size, loff_t *offset)
  111. {
  112. struct kfd_smi_client *client = filep->private_data;
  113. uint64_t events;
  114. if (!access_ok(user, size) || size < sizeof(events))
  115. return -EFAULT;
  116. if (copy_from_user(&events, user, sizeof(events)))
  117. return -EFAULT;
  118. WRITE_ONCE(client->events, events);
  119. return sizeof(events);
  120. }
  121. static void kfd_smi_ev_client_free(struct rcu_head *p)
  122. {
  123. struct kfd_smi_client *ev = container_of(p, struct kfd_smi_client, rcu);
  124. kfifo_free(&ev->fifo);
  125. kfree(ev);
  126. }
  127. static int kfd_smi_ev_release(struct inode *inode, struct file *filep)
  128. {
  129. struct kfd_smi_client *client = filep->private_data;
  130. struct kfd_node *dev = client->dev;
  131. spin_lock(&dev->smi_lock);
  132. list_del_rcu(&client->list);
  133. spin_unlock(&dev->smi_lock);
  134. call_rcu(&client->rcu, kfd_smi_ev_client_free);
  135. return 0;
  136. }
  137. static bool kfd_smi_ev_enabled(pid_t pid, struct kfd_smi_client *client,
  138. unsigned int event)
  139. {
  140. uint64_t all = KFD_SMI_EVENT_MASK_FROM_INDEX(KFD_SMI_EVENT_ALL_PROCESS);
  141. uint64_t events = READ_ONCE(client->events);
  142. if (pid && client->pid != pid && !(client->suser && (events & all)))
  143. return false;
  144. return events & KFD_SMI_EVENT_MASK_FROM_INDEX(event);
  145. }
  146. static void add_event_to_kfifo(pid_t pid, struct kfd_node *dev,
  147. unsigned int smi_event, char *event_msg, int len)
  148. {
  149. struct kfd_smi_client *client;
  150. rcu_read_lock();
  151. list_for_each_entry_rcu(client, &dev->smi_clients, list) {
  152. if (!kfd_smi_ev_enabled(pid, client, smi_event))
  153. continue;
  154. spin_lock(&client->lock);
  155. if (kfifo_avail(&client->fifo) >= len) {
  156. kfifo_in(&client->fifo, event_msg, len);
  157. wake_up_all(&client->wait_queue);
  158. } else {
  159. pr_debug("smi_event(EventID: %u): no space left\n",
  160. smi_event);
  161. }
  162. spin_unlock(&client->lock);
  163. }
  164. rcu_read_unlock();
  165. }
  166. __printf(4, 5)
  167. static void kfd_smi_event_add(pid_t pid, struct kfd_node *dev,
  168. unsigned int event, char *fmt, ...)
  169. {
  170. char fifo_in[KFD_SMI_EVENT_MSG_SIZE];
  171. int len;
  172. va_list args;
  173. if (list_empty(&dev->smi_clients))
  174. return;
  175. len = snprintf(fifo_in, sizeof(fifo_in), "%x ", event);
  176. va_start(args, fmt);
  177. len += vsnprintf(fifo_in + len, sizeof(fifo_in) - len, fmt, args);
  178. va_end(args);
  179. add_event_to_kfifo(pid, dev, event, fifo_in, len);
  180. }
  181. void kfd_smi_event_update_gpu_reset(struct kfd_node *dev, bool post_reset,
  182. struct amdgpu_reset_context *reset_context)
  183. {
  184. unsigned int event;
  185. char reset_cause[64];
  186. if (post_reset) {
  187. event = KFD_SMI_EVENT_GPU_POST_RESET;
  188. } else {
  189. event = KFD_SMI_EVENT_GPU_PRE_RESET;
  190. ++(dev->reset_seq_num);
  191. }
  192. memset(reset_cause, 0, sizeof(reset_cause));
  193. if (reset_context)
  194. amdgpu_reset_get_desc(reset_context, reset_cause,
  195. sizeof(reset_cause));
  196. kfd_smi_event_add(0, dev, event, KFD_EVENT_FMT_UPDATE_GPU_RESET(
  197. dev->reset_seq_num, reset_cause));
  198. }
  199. void kfd_smi_event_update_thermal_throttling(struct kfd_node *dev,
  200. uint64_t throttle_bitmask)
  201. {
  202. kfd_smi_event_add(0, dev, KFD_SMI_EVENT_THERMAL_THROTTLE, KFD_EVENT_FMT_THERMAL_THROTTLING(
  203. throttle_bitmask,
  204. amdgpu_dpm_get_thermal_throttling_counter(dev->adev)));
  205. }
  206. void kfd_smi_event_update_vmfault(struct kfd_node *dev, uint16_t pasid)
  207. {
  208. struct amdgpu_task_info *task_info;
  209. task_info = amdgpu_vm_get_task_info_pasid(dev->adev, pasid);
  210. if (task_info) {
  211. /* Report VM faults from user applications, not retry from kernel */
  212. if (task_info->pid)
  213. kfd_smi_event_add(0, dev, KFD_SMI_EVENT_VMFAULT, KFD_EVENT_FMT_VMFAULT(
  214. task_info->pid, task_info->task_name));
  215. amdgpu_vm_put_task_info(task_info);
  216. }
  217. }
  218. void kfd_smi_event_page_fault_start(struct kfd_node *node, pid_t pid,
  219. unsigned long address, bool write_fault,
  220. ktime_t ts)
  221. {
  222. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_PAGE_FAULT_START,
  223. KFD_EVENT_FMT_PAGEFAULT_START(ktime_to_ns(ts), pid,
  224. address, node->id, write_fault ? 'W' : 'R'));
  225. }
  226. void kfd_smi_event_page_fault_end(struct kfd_node *node, pid_t pid,
  227. unsigned long address, bool migration)
  228. {
  229. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_PAGE_FAULT_END,
  230. KFD_EVENT_FMT_PAGEFAULT_END(ktime_get_boottime_ns(),
  231. pid, address, node->id, migration ? 'M' : 'U'));
  232. }
  233. void kfd_smi_event_migration_start(struct kfd_node *node, pid_t pid,
  234. unsigned long start, unsigned long end,
  235. uint32_t from, uint32_t to,
  236. uint32_t prefetch_loc, uint32_t preferred_loc,
  237. uint32_t trigger)
  238. {
  239. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_MIGRATE_START,
  240. KFD_EVENT_FMT_MIGRATE_START(
  241. ktime_get_boottime_ns(), pid, start, end - start,
  242. from, to, prefetch_loc, preferred_loc, trigger));
  243. }
  244. void kfd_smi_event_migration_end(struct kfd_node *node, pid_t pid,
  245. unsigned long start, unsigned long end,
  246. uint32_t from, uint32_t to, uint32_t trigger)
  247. {
  248. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_MIGRATE_END,
  249. KFD_EVENT_FMT_MIGRATE_END(
  250. ktime_get_boottime_ns(), pid, start, end - start,
  251. from, to, trigger));
  252. }
  253. void kfd_smi_event_queue_eviction(struct kfd_node *node, pid_t pid,
  254. uint32_t trigger)
  255. {
  256. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_QUEUE_EVICTION,
  257. KFD_EVENT_FMT_QUEUE_EVICTION(ktime_get_boottime_ns(), pid,
  258. node->id, trigger));
  259. }
  260. void kfd_smi_event_queue_restore(struct kfd_node *node, pid_t pid)
  261. {
  262. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_QUEUE_RESTORE,
  263. KFD_EVENT_FMT_QUEUE_RESTORE(ktime_get_boottime_ns(), pid,
  264. node->id, 0));
  265. }
  266. void kfd_smi_event_queue_restore_rescheduled(struct mm_struct *mm)
  267. {
  268. struct kfd_process *p;
  269. int i;
  270. p = kfd_lookup_process_by_mm(mm);
  271. if (!p)
  272. return;
  273. for (i = 0; i < p->n_pdds; i++) {
  274. struct kfd_process_device *pdd = p->pdds[i];
  275. kfd_smi_event_add(p->lead_thread->pid, pdd->dev,
  276. KFD_SMI_EVENT_QUEUE_RESTORE,
  277. KFD_EVENT_FMT_QUEUE_RESTORE(ktime_get_boottime_ns(),
  278. p->lead_thread->pid, pdd->dev->id, 'R'));
  279. }
  280. kfd_unref_process(p);
  281. }
  282. void kfd_smi_event_unmap_from_gpu(struct kfd_node *node, pid_t pid,
  283. unsigned long address, unsigned long last,
  284. uint32_t trigger)
  285. {
  286. kfd_smi_event_add(pid, node, KFD_SMI_EVENT_UNMAP_FROM_GPU,
  287. KFD_EVENT_FMT_UNMAP_FROM_GPU(ktime_get_boottime_ns(),
  288. pid, address, last - address + 1, node->id, trigger));
  289. }
  290. int kfd_smi_event_open(struct kfd_node *dev, uint32_t *fd)
  291. {
  292. struct kfd_smi_client *client;
  293. int ret;
  294. client = kzalloc(sizeof(struct kfd_smi_client), GFP_KERNEL);
  295. if (!client)
  296. return -ENOMEM;
  297. INIT_LIST_HEAD(&client->list);
  298. ret = kfifo_alloc(&client->fifo, MAX_KFIFO_SIZE, GFP_KERNEL);
  299. if (ret) {
  300. kfree(client);
  301. return ret;
  302. }
  303. init_waitqueue_head(&client->wait_queue);
  304. spin_lock_init(&client->lock);
  305. client->events = 0;
  306. client->dev = dev;
  307. client->pid = current->tgid;
  308. client->suser = capable(CAP_SYS_ADMIN);
  309. spin_lock(&dev->smi_lock);
  310. list_add_rcu(&client->list, &dev->smi_clients);
  311. spin_unlock(&dev->smi_lock);
  312. ret = anon_inode_getfd(kfd_smi_name, &kfd_smi_ev_fops, (void *)client,
  313. O_RDWR);
  314. if (ret < 0) {
  315. spin_lock(&dev->smi_lock);
  316. list_del_rcu(&client->list);
  317. spin_unlock(&dev->smi_lock);
  318. synchronize_rcu();
  319. kfifo_free(&client->fifo);
  320. kfree(client);
  321. return ret;
  322. }
  323. *fd = ret;
  324. return 0;
  325. }