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