Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_debugfs.c
26516 views
// SPDX-License-Identifier: GPL-2.0 OR MIT1/*2* Copyright 2016-2022 Advanced Micro Devices, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* 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 OR20* OTHER DEALINGS IN THE SOFTWARE.21*/2223#include <linux/debugfs.h>24#include <linux/uaccess.h>2526#include "kfd_priv.h"2728static struct dentry *debugfs_root;29static struct dentry *debugfs_proc;30static struct list_head procs;3132struct debugfs_proc_entry {33struct list_head list;34struct dentry *proc_dentry;35pid_t pid;36};3738#define MAX_DEBUGFS_FILENAME_LEN 323940static int kfd_debugfs_open(struct inode *inode, struct file *file)41{42int (*show)(struct seq_file *, void *) = inode->i_private;4344return single_open(file, show, NULL);45}46static int kfd_debugfs_hang_hws_read(struct seq_file *m, void *data)47{48seq_puts(m, "echo gpu_id > hang_hws\n");49return 0;50}5152static ssize_t kfd_debugfs_hang_hws_write(struct file *file,53const char __user *user_buf, size_t size, loff_t *ppos)54{55struct kfd_node *dev;56char tmp[16];57uint32_t gpu_id;58int ret = -EINVAL;5960memset(tmp, 0, 16);61if (size >= 16) {62pr_err("Invalid input for gpu id.\n");63goto out;64}65if (copy_from_user(tmp, user_buf, size)) {66ret = -EFAULT;67goto out;68}69if (kstrtoint(tmp, 10, &gpu_id)) {70pr_err("Invalid input for gpu id.\n");71goto out;72}73dev = kfd_device_by_id(gpu_id);74if (dev) {75kfd_debugfs_hang_hws(dev);76ret = size;77} else78pr_err("Cannot find device %d.\n", gpu_id);7980out:81return ret;82}8384static const struct file_operations kfd_debugfs_fops = {85.owner = THIS_MODULE,86.open = kfd_debugfs_open,87.read = seq_read,88.llseek = seq_lseek,89.release = single_release,90};9192static const struct file_operations kfd_debugfs_hang_hws_fops = {93.owner = THIS_MODULE,94.open = kfd_debugfs_open,95.read = seq_read,96.write = kfd_debugfs_hang_hws_write,97.llseek = seq_lseek,98.release = single_release,99};100101void kfd_debugfs_init(void)102{103debugfs_root = debugfs_create_dir("kfd", NULL);104debugfs_proc = debugfs_create_dir("proc", debugfs_root);105INIT_LIST_HEAD(&procs);106107debugfs_create_file("mqds", S_IFREG | 0444, debugfs_root,108kfd_debugfs_mqds_by_process, &kfd_debugfs_fops);109debugfs_create_file("hqds", S_IFREG | 0444, debugfs_root,110kfd_debugfs_hqds_by_device, &kfd_debugfs_fops);111debugfs_create_file("rls", S_IFREG | 0444, debugfs_root,112kfd_debugfs_rls_by_device, &kfd_debugfs_fops);113debugfs_create_file("hang_hws", S_IFREG | 0200, debugfs_root,114kfd_debugfs_hang_hws_read, &kfd_debugfs_hang_hws_fops);115debugfs_create_file("mem_limit", S_IFREG | 0200, debugfs_root,116kfd_debugfs_kfd_mem_limits, &kfd_debugfs_fops);117}118119void kfd_debugfs_fini(void)120{121debugfs_remove_recursive(debugfs_proc);122debugfs_remove_recursive(debugfs_root);123}124125static ssize_t kfd_debugfs_pasid_read(struct file *file, char __user *buf,126size_t count, loff_t *ppos)127{128struct kfd_process_device *pdd = file_inode(file)->i_private;129char tmp[32];130int len;131132len = snprintf(tmp, sizeof(tmp), "%u\n", pdd->pasid);133134return simple_read_from_buffer(buf, count, ppos, tmp, len);135}136137static const struct file_operations kfd_debugfs_pasid_fops = {138.owner = THIS_MODULE,139.read = kfd_debugfs_pasid_read,140};141142void kfd_debugfs_add_process(struct kfd_process *p)143{144int i;145char name[MAX_DEBUGFS_FILENAME_LEN];146struct debugfs_proc_entry *entry;147148entry = kzalloc(sizeof(*entry), GFP_KERNEL);149if (!entry)150return;151152list_add(&entry->list, &procs);153entry->pid = p->lead_thread->pid;154snprintf(name, MAX_DEBUGFS_FILENAME_LEN, "%d",155(int)entry->pid);156entry->proc_dentry = debugfs_create_dir(name, debugfs_proc);157158/* Create debugfs files for each GPU:159* - proc/<pid>/pasid_<gpuid>160*/161for (i = 0; i < p->n_pdds; i++) {162struct kfd_process_device *pdd = p->pdds[i];163164snprintf(name, MAX_DEBUGFS_FILENAME_LEN, "pasid_%u",165pdd->dev->id);166debugfs_create_file((const char *)name, S_IFREG | 0444,167entry->proc_dentry, pdd,168&kfd_debugfs_pasid_fops);169}170}171172void kfd_debugfs_remove_process(struct kfd_process *p)173{174struct debugfs_proc_entry *entry, *next;175176mutex_lock(&kfd_processes_mutex);177list_for_each_entry_safe(entry, next, &procs, list) {178if (entry->pid != p->lead_thread->pid)179continue;180181debugfs_remove_recursive(entry->proc_dentry);182list_del(&entry->list);183kfree(entry);184}185mutex_unlock(&kfd_processes_mutex);186}187188189