summaryrefslogtreecommitdiff
path: root/mali_kbase/mali_kbase_debugfs_helper.c
blob: c846491e78fb31e243bad458740c3b30145c1832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2019-2023 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */

#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#include "mali_kbase_debugfs_helper.h"

/* Arbitrary maximum size to prevent user space allocating too much kernel
 * memory
 */
#define DEBUGFS_MEM_POOLS_MAX_WRITE_SIZE (256u)

/**
 * set_attr_from_string - Parse a string to set elements of an array
 *
 * @buf:         Input string to parse. Must be nul-terminated!
 * @array:       Address of an object that can be accessed like an array.
 * @nelems:      Number of elements in the array.
 * @set_attr_fn: Function to be called back for each array element.
 *
 * This is the core of the implementation of
 * kbase_debugfs_helper_set_attr_from_string. The only difference between the
 * two functions is that this one requires the input string to be writable.
 *
 * Return: 0 if success, negative error code otherwise.
 */
static int
set_attr_from_string(char *const buf, void *const array, size_t const nelems,
		     kbase_debugfs_helper_set_attr_fn * const set_attr_fn)
{
	size_t index, err = 0;
	char *ptr = buf;

	for (index = 0; index < nelems && *ptr; ++index) {
		unsigned long new_size;
		size_t len;
		char sep;

		/* Drop leading spaces */
		while (*ptr == ' ')
			ptr++;

		len = strcspn(ptr, "\n ");
		if (len == 0) {
			/* No more values (allow this) */
			break;
		}

		/* Substitute a nul terminator for a space character
		 * to make the substring valid for kstrtoul.
		 */
		sep = ptr[len];
		if (sep == ' ')
			ptr[len++] = '\0';

		err = kstrtoul(ptr, 0, &new_size);
		if (err)
			break;

		/* Skip the substring (including any premature nul terminator)
		 */
		ptr += len;

		set_attr_fn(array, index, new_size);
	}

	return err;
}

int kbase_debugfs_string_validator(char *const buf)
{
	int err = 0;
	char *ptr = buf;

	while (*ptr) {
		unsigned long test_number;
		size_t len;

		/* Drop leading spaces */
		while (*ptr == ' ')
			ptr++;

		/* Strings passed into the validator will be NULL terminated
		 * by nature, so here strcspn only needs to delimit by
		 * newlines, spaces and NULL terminator (delimited natively).
		 */
		len = strcspn(ptr, "\n ");
		if (len == 0) {
			/* No more values (allow this) */
			break;
		}

		/* Substitute a nul terminator for a space character to make
		 * the substring valid for kstrtoul, and then replace it back.
		 */
		if (ptr[len] == ' ') {
			ptr[len] = '\0';
			err = kstrtoul(ptr, 0, &test_number);
			ptr[len] = ' ';

			/* len should only be incremented if there is a valid
			 * number to follow - otherwise this will skip over
			 * the NULL terminator in cases with no ending newline
			 */
			len++;
		} else {
			/* This would occur at the last element before a space
			 * or a NULL terminator.
			 */
			err = kstrtoul(ptr, 0, &test_number);
		}

		if (err)
			break;
		/* Skip the substring (including any premature nul terminator)
		 */
		ptr += len;
	}
	return err;
}

int kbase_debugfs_helper_set_attr_from_string(
	const char *const buf, void *const array, size_t const nelems,
	kbase_debugfs_helper_set_attr_fn * const set_attr_fn)
{
	char *const wbuf = kstrdup(buf, GFP_KERNEL);
	int err = 0;

	if (!wbuf)
		return -ENOMEM;

	/* validate string before actually writing values */
	err = kbase_debugfs_string_validator(wbuf);
	if (err) {
		kfree(wbuf);
		return err;
	}

	err = set_attr_from_string(wbuf, array, nelems,
		set_attr_fn);

	kfree(wbuf);
	return err;
}

ssize_t kbase_debugfs_helper_get_attr_to_string(
	char *const buf, size_t const size, void *const array,
	size_t const nelems,
	kbase_debugfs_helper_get_attr_fn * const get_attr_fn)
{
	ssize_t total = 0;
	size_t index;

	for (index = 0; index < nelems; ++index) {
		const char *postfix = " ";

		if (index == (nelems-1))
			postfix = "\n";

		total += scnprintf(buf + total, size - total, "%zu%s",
				get_attr_fn(array, index), postfix);
	}

	return total;
}

int kbase_debugfs_helper_seq_write(
	struct file *const file, const char __user *const ubuf,
	size_t const count, size_t const nelems,
	kbase_debugfs_helper_set_attr_fn * const set_attr_fn)
{
	const struct seq_file *const sfile = file->private_data;
	void *const array = sfile->private;
	int err = 0;
	char *buf;

	if (WARN_ON(!array))
		return -EINVAL;

	if (WARN_ON(count > DEBUGFS_MEM_POOLS_MAX_WRITE_SIZE))
		return -EINVAL;

	buf = kmalloc(count + 1, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;

	if (copy_from_user(buf, ubuf, count)) {
		kfree(buf);
		return -EFAULT;
	}

	buf[count] = '\0';

	/* validate string before actually writing values */
	err = kbase_debugfs_string_validator(buf);
	if (err) {
		kfree(buf);
		return err;
	}

	err = set_attr_from_string(buf,
		array, nelems, set_attr_fn);
	kfree(buf);

	return err;
}

int kbase_debugfs_helper_seq_read(
	struct seq_file * const sfile, size_t const nelems,
	kbase_debugfs_helper_get_attr_fn * const get_attr_fn)
{
	void *const array = sfile->private;
	size_t index;

	if (WARN_ON(!array))
		return -EINVAL;

	for (index = 0; index < nelems; ++index) {
		const char *postfix = " ";

		if (index == (nelems-1))
			postfix = "\n";

		seq_printf(sfile, "%zu%s", get_attr_fn(array, index), postfix);
	}
	return 0;
}