1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3 *
4 * (C) COPYRIGHT 2012-2017, 2019-2022 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 #include <mali_kbase.h>
23
24 #if IS_ENABLED(CONFIG_DEBUG_FS)
25
26 /**
27 * kbasep_mem_profile_seq_show - Show callback for the @c mem_profile debugfs file.
28 *
29 * @sfile: The debugfs entry
30 * @data: Data associated with the entry
31 *
32 * This function is called to get the contents of the @c mem_profile debugfs
33 * file. This is a report of current memory usage and distribution in userspace.
34 *
35 * Return: 0 if it successfully prints data in debugfs entry file, non-zero
36 * otherwise
37 */
kbasep_mem_profile_seq_show(struct seq_file * sfile,void * data)38 static int kbasep_mem_profile_seq_show(struct seq_file *sfile, void *data)
39 {
40 struct kbase_context *kctx = sfile->private;
41
42 mutex_lock(&kctx->mem_profile_lock);
43
44 seq_write(sfile, kctx->mem_profile_data, kctx->mem_profile_size);
45
46 seq_putc(sfile, '\n');
47
48 mutex_unlock(&kctx->mem_profile_lock);
49
50 return 0;
51 }
52
53 /*
54 * File operations related to debugfs entry for mem_profile
55 */
kbasep_mem_profile_debugfs_open(struct inode * in,struct file * file)56 static int kbasep_mem_profile_debugfs_open(struct inode *in, struct file *file)
57 {
58 return single_open(file, kbasep_mem_profile_seq_show, in->i_private);
59 }
60
61 static const struct file_operations kbasep_mem_profile_debugfs_fops = {
62 .owner = THIS_MODULE,
63 .open = kbasep_mem_profile_debugfs_open,
64 .read = seq_read,
65 .llseek = seq_lseek,
66 .release = single_release,
67 };
68
kbasep_mem_profile_debugfs_insert(struct kbase_context * kctx,char * data,size_t size)69 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
70 size_t size)
71 {
72 const mode_t mode = 0444;
73 int err = 0;
74
75 mutex_lock(&kctx->mem_profile_lock);
76
77 dev_dbg(kctx->kbdev->dev, "initialised: %d",
78 kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
79
80 if (!kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED)) {
81 if (IS_ERR_OR_NULL(kctx->kctx_dentry)) {
82 err = -ENOMEM;
83 } else if (IS_ERR_OR_NULL(debugfs_create_file("mem_profile",
84 mode, kctx->kctx_dentry, kctx,
85 &kbasep_mem_profile_debugfs_fops))) {
86 err = -EAGAIN;
87 } else {
88 kbase_ctx_flag_set(kctx,
89 KCTX_MEM_PROFILE_INITIALIZED);
90 }
91 }
92
93 if (kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED)) {
94 kfree(kctx->mem_profile_data);
95 kctx->mem_profile_data = data;
96 kctx->mem_profile_size = size;
97 } else {
98 kfree(data);
99 }
100
101 dev_dbg(kctx->kbdev->dev, "returning: %d, initialised: %d",
102 err, kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
103
104 mutex_unlock(&kctx->mem_profile_lock);
105
106 return err;
107 }
108
kbasep_mem_profile_debugfs_remove(struct kbase_context * kctx)109 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
110 {
111 mutex_lock(&kctx->mem_profile_lock);
112
113 dev_dbg(kctx->kbdev->dev, "initialised: %d",
114 kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
115
116 kfree(kctx->mem_profile_data);
117 kctx->mem_profile_data = NULL;
118 kctx->mem_profile_size = 0;
119
120 mutex_unlock(&kctx->mem_profile_lock);
121 }
122
123 #else /* CONFIG_DEBUG_FS */
124
kbasep_mem_profile_debugfs_insert(struct kbase_context * kctx,char * data,size_t size)125 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
126 size_t size)
127 {
128 kfree(data);
129 return 0;
130 }
131 #endif /* CONFIG_DEBUG_FS */
132