1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * sysctl.c: General linux system control interface
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Begun 24 March 1995, Stephen Tweedie
61da177e4SLinus Torvalds * Added /proc support, Dec 1995
71da177e4SLinus Torvalds * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
81da177e4SLinus Torvalds * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
91da177e4SLinus Torvalds * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
101da177e4SLinus Torvalds * Dynamic registration fixes, Stephen Tweedie.
111da177e4SLinus Torvalds * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
121da177e4SLinus Torvalds * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
131da177e4SLinus Torvalds * Horn.
141da177e4SLinus Torvalds * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
151da177e4SLinus Torvalds * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
161da177e4SLinus Torvalds * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
171da177e4SLinus Torvalds * Wendling.
181da177e4SLinus Torvalds * The list_for_each() macro wasn't appropriate for the sysctl loop.
191da177e4SLinus Torvalds * Removed it and replaced it with older style, 03/23/00, Bill Wendling
201da177e4SLinus Torvalds */
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/sysctl.h>
241da177e4SLinus Torvalds #include <linux/bitmap.h>
251da177e4SLinus Torvalds #include <linux/signal.h>
261da177e4SLinus Torvalds #include <linux/panic.h>
275a04cca6SAkinobu Mita #include <linux/printk.h>
28d33ed52dSDave Young #include <linux/proc_fs.h>
29f39650deSAndy Shevchenko #include <linux/security.h>
30455cd5abSDan Rosenberg #include <linux/ctype.h>
311da177e4SLinus Torvalds #include <linux/filter.h>
3272c2d582SAndrew Morgan #include <linux/fs.h>
331da177e4SLinus Torvalds #include <linux/init.h>
34fd4b616bSSteven Rostedt #include <linux/kernel.h>
35b6459415SJakub Kicinski #include <linux/kobject.h>
3662239ac2SAdrian Bunk #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/sysrq.h>
381da177e4SLinus Torvalds #include <linux/highuid.h>
390296b228SKay Sievers #include <linux/writeback.h>
4020380731SArnaldo Carvalho de Melo #include <linux/ratelimit.h>
411da177e4SLinus Torvalds #include <linux/initrd.h>
421da177e4SLinus Torvalds #include <linux/key.h>
431da177e4SLinus Torvalds #include <linux/times.h>
443fff4c42SIngo Molnar #include <linux/limits.h>
451da177e4SLinus Torvalds #include <linux/syscalls.h>
461da177e4SLinus Torvalds #include <linux/nfs_fs.h>
470b77f5bfSDavid Howells #include <linux/acpi.h>
481da177e4SLinus Torvalds #include <linux/reboot.h>
491da177e4SLinus Torvalds #include <linux/ftrace.h>
501da177e4SLinus Torvalds #include <linux/kmod.h>
511da177e4SLinus Torvalds #include <linux/capability.h>
52c748e134SAdrian Bunk #include <linux/binfmts.h>
53c255d844SPavel Machek #include <linux/sched/sysctl.h>
54c255d844SPavel Machek #include <linux/mount.h>
5510a0a8d4SJeremy Fitzhardinge #include <linux/pid.h>
56b0fc494fSSteven Rostedt
57cdd6c482SIngo Molnar #include "../lib/kstrtox.h"
588e4228e1SDavid Rientjes
5917f60a7dSEric Paris #include <linux/uaccess.h>
6073efc039SDan Ballard #include <asm/processor.h>
6140401530SAl Viro
62cf4aebc2SClark Williams #ifdef CONFIG_X86
63d2921684SEric W. Biederman #include <asm/nmi.h>
64cefdca0aSPeter Xu #include <asm/stacktrace.h>
652374c09bSChristoph Hellwig #include <asm/io.h>
661da177e4SLinus Torvalds #endif
677f2923c4SChristian Brauner #ifdef CONFIG_SPARC
687f2923c4SChristian Brauner #include <asm/setup.h>
697c0f6ba6SLinus Torvalds #endif
701da177e4SLinus Torvalds #ifdef CONFIG_RT_MUTEXES
711da177e4SLinus Torvalds #include <linux/rtmutex.h>
7229cbc78bSAndi Kleen #endif
7329cbc78bSAndi Kleen
740741f4d2SChuck Ebbert /* shared constants to be used in various sysctls */
756e7c4025SIngo Molnar const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
7629cbc78bSAndi Kleen EXPORT_SYMBOL(sysctl_vals);
77d550bbd4SDavid Howells
78d550bbd4SDavid Howells const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
79d550bbd4SDavid Howells EXPORT_SYMBOL_GPL(sysctl_long_vals);
804f0e056fSDave Young
814f0e056fSDave Young #if defined(CONFIG_SYSCTL)
824f0e056fSDave Young
83504d7cf1SDon Zickus /* Constants used for minimum and maximum */
84b13bc7cbSLiu Shixin static const int ngroups_max = NGROUPS_MAX;
85b13bc7cbSLiu Shixin static const int cap_last_cap = CAP_LAST_CAP;
86b13bc7cbSLiu Shixin
87b13bc7cbSLiu Shixin #ifdef CONFIG_PROC_SYSCTL
88b13bc7cbSLiu Shixin
89b13bc7cbSLiu Shixin /**
90b13bc7cbSLiu Shixin * enum sysctl_writes_mode - supported sysctl write modes
911da177e4SLinus Torvalds *
921da177e4SLinus Torvalds * @SYSCTL_WRITES_LEGACY: each write syscall must fully contain the sysctl value
93c4f3b63fSRavikiran G Thirumalai * to be written, and multiple writes on the same sysctl file descriptor
94c4f3b63fSRavikiran G Thirumalai * will rewrite the sysctl value, regardless of file position. No warning
95c5dfd78eSArnaldo Carvalho de Melo * is issued when the initial position is not 0.
96d73840ecSXiaoming Ni * @SYSCTL_WRITES_WARN: same as above but warn when the initial file position is
97c5dfd78eSArnaldo Carvalho de Melo * not 0.
98c4f3b63fSRavikiran G Thirumalai * @SYSCTL_WRITES_STRICT: writes to numeric sysctl entries must always be at
999e4a5bdaSAndrea Righi * file position 0 and the value must be fully contained in the buffer
100f628867dSStephen Kitt * sent to the write syscall. If dealing with strings respect the file
10173efc039SDan Ballard * position, but restrict this to the max length of the buffer, anything
1021da177e4SLinus Torvalds * passed the max length will be ignored. Multiple writes will append
103d6f8ff73SRandy Dunlap * to the buffer.
104f4aacea2SKees Cook *
105a19ac337SLuis R. Rodriguez * These write modes control how current file position affects the behavior of
106a19ac337SLuis R. Rodriguez * updating sysctl values through the proc interface on each write.
107a19ac337SLuis R. Rodriguez */
108a19ac337SLuis R. Rodriguez enum sysctl_writes_mode {
109a19ac337SLuis R. Rodriguez SYSCTL_WRITES_LEGACY = -1,
110a19ac337SLuis R. Rodriguez SYSCTL_WRITES_WARN = 0,
111a19ac337SLuis R. Rodriguez SYSCTL_WRITES_STRICT = 1,
112a19ac337SLuis R. Rodriguez };
113a19ac337SLuis R. Rodriguez
114a19ac337SLuis R. Rodriguez static enum sysctl_writes_mode sysctl_writes_strict = SYSCTL_WRITES_STRICT;
115a19ac337SLuis R. Rodriguez #endif /* CONFIG_PROC_SYSCTL */
116a19ac337SLuis R. Rodriguez #endif /* CONFIG_SYSCTL */
117a19ac337SLuis R. Rodriguez
11865f50f25SWeitao Hou /*
119a19ac337SLuis R. Rodriguez * /proc/sys support
120a19ac337SLuis R. Rodriguez */
121a19ac337SLuis R. Rodriguez
122a19ac337SLuis R. Rodriguez #ifdef CONFIG_PROC_SYSCTL
123a19ac337SLuis R. Rodriguez
_proc_do_string(char * data,int maxlen,int write,char * buffer,size_t * lenp,loff_t * ppos)124a19ac337SLuis R. Rodriguez static int _proc_do_string(char *data, int maxlen, int write,
125a19ac337SLuis R. Rodriguez char *buffer, size_t *lenp, loff_t *ppos)
126a19ac337SLuis R. Rodriguez {
127a19ac337SLuis R. Rodriguez size_t len;
128a19ac337SLuis R. Rodriguez char c, *p;
129f4aacea2SKees Cook
130a19ac337SLuis R. Rodriguez if (!data || !maxlen || !*lenp) {
131f461d2dcSChristoph Hellwig *lenp = 0;
132ceb18132SLuis R. Rodriguez return 0;
13367f3977fSAlexandre Ghiti }
13467f3977fSAlexandre Ghiti
1351da177e4SLinus Torvalds if (write) {
1361da177e4SLinus Torvalds if (sysctl_writes_strict == SYSCTL_WRITES_STRICT) {
1371da177e4SLinus Torvalds /* Only continue writes not past the end of buffer. */
138f461d2dcSChristoph Hellwig len = strlen(data);
139f461d2dcSChristoph Hellwig if (len > maxlen - 1)
140f461d2dcSChristoph Hellwig len = maxlen - 1;
141f461d2dcSChristoph Hellwig
142f461d2dcSChristoph Hellwig if (*ppos > len)
143f461d2dcSChristoph Hellwig return 0;
144f461d2dcSChristoph Hellwig len = *ppos;
145f461d2dcSChristoph Hellwig } else {
146f461d2dcSChristoph Hellwig /* Start writing from beginning of buffer. */
14732927393SChristoph Hellwig len = 0;
148f461d2dcSChristoph Hellwig }
149f461d2dcSChristoph Hellwig
15032927393SChristoph Hellwig *ppos += *lenp;
151f461d2dcSChristoph Hellwig p = buffer;
152f461d2dcSChristoph Hellwig while ((p - buffer) < *lenp && len < maxlen - 1) {
153f461d2dcSChristoph Hellwig c = *(p++);
154f461d2dcSChristoph Hellwig if (c == 0 || c == '\n')
155f461d2dcSChristoph Hellwig break;
156f461d2dcSChristoph Hellwig data[len++] = c;
157f461d2dcSChristoph Hellwig }
158f461d2dcSChristoph Hellwig data[len] = 0;
159f461d2dcSChristoph Hellwig } else {
160f461d2dcSChristoph Hellwig len = strlen(data);
161f461d2dcSChristoph Hellwig if (len > maxlen)
162f461d2dcSChristoph Hellwig len = maxlen;
163f461d2dcSChristoph Hellwig
164f461d2dcSChristoph Hellwig if (*ppos > len) {
165f461d2dcSChristoph Hellwig *lenp = 0;
166f461d2dcSChristoph Hellwig return 0;
167f461d2dcSChristoph Hellwig }
168f461d2dcSChristoph Hellwig
169f461d2dcSChristoph Hellwig data += *ppos;
170f461d2dcSChristoph Hellwig len -= *ppos;
171f461d2dcSChristoph Hellwig
172f461d2dcSChristoph Hellwig if (len > *lenp)
173f461d2dcSChristoph Hellwig len = *lenp;
174f461d2dcSChristoph Hellwig if (len)
17532927393SChristoph Hellwig memcpy(buffer, data, len);
176f461d2dcSChristoph Hellwig if (len < *lenp) {
177f461d2dcSChristoph Hellwig buffer[len] = '\n';
178f461d2dcSChristoph Hellwig len++;
179f461d2dcSChristoph Hellwig }
180f461d2dcSChristoph Hellwig *lenp = len;
181f461d2dcSChristoph Hellwig *ppos += len;
182f461d2dcSChristoph Hellwig }
183f461d2dcSChristoph Hellwig return 0;
184f461d2dcSChristoph Hellwig }
185f461d2dcSChristoph Hellwig
warn_sysctl_write(const struct ctl_table * table)186f461d2dcSChristoph Hellwig static void warn_sysctl_write(const struct ctl_table *table)
187f461d2dcSChristoph Hellwig {
188f461d2dcSChristoph Hellwig pr_warn_once("%s wrote to %s when file position was not 0!\n"
189f461d2dcSChristoph Hellwig "This will not be supported in the future. To silence this\n"
190f461d2dcSChristoph Hellwig "warning, set kernel.sysctl_writes_strict = -1\n",
191f461d2dcSChristoph Hellwig current->comm, table->procname);
192f461d2dcSChristoph Hellwig }
193f461d2dcSChristoph Hellwig
194f461d2dcSChristoph Hellwig /**
195f461d2dcSChristoph Hellwig * proc_first_pos_non_zero_ignore - check if first position is allowed
196f461d2dcSChristoph Hellwig * @ppos: file position
19732927393SChristoph Hellwig * @table: the sysctl table
198f461d2dcSChristoph Hellwig *
19932927393SChristoph Hellwig * Returns true if the first position is non-zero and the sysctl_writes_strict
200f461d2dcSChristoph Hellwig * mode indicates this is not allowed for numeric input types. String proc
201f461d2dcSChristoph Hellwig * handlers can ignore the return value.
202f461d2dcSChristoph Hellwig */
proc_first_pos_non_zero_ignore(loff_t * ppos,const struct ctl_table * table)203f461d2dcSChristoph Hellwig static bool proc_first_pos_non_zero_ignore(loff_t *ppos,
204f461d2dcSChristoph Hellwig const struct ctl_table *table)
205f461d2dcSChristoph Hellwig {
206f461d2dcSChristoph Hellwig if (!*ppos)
207f461d2dcSChristoph Hellwig return false;
20832fe9152SThomas Weißschuh
209f461d2dcSChristoph Hellwig switch (sysctl_writes_strict) {
210f461d2dcSChristoph Hellwig case SYSCTL_WRITES_STRICT:
211f461d2dcSChristoph Hellwig return true;
212f461d2dcSChristoph Hellwig case SYSCTL_WRITES_WARN:
213f461d2dcSChristoph Hellwig warn_sysctl_write(table);
214f461d2dcSChristoph Hellwig return false;
215f461d2dcSChristoph Hellwig default:
216f461d2dcSChristoph Hellwig return false;
217f461d2dcSChristoph Hellwig }
218f461d2dcSChristoph Hellwig }
219f461d2dcSChristoph Hellwig
220f461d2dcSChristoph Hellwig /**
221f461d2dcSChristoph Hellwig * proc_dostring - read a string sysctl
222f461d2dcSChristoph Hellwig * @table: the sysctl table
223f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
224f461d2dcSChristoph Hellwig * @buffer: the user buffer
225f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
22632fe9152SThomas Weißschuh * @ppos: file position
227f461d2dcSChristoph Hellwig *
228f461d2dcSChristoph Hellwig * Reads/writes a string from/to the user buffer. If the kernel
229f461d2dcSChristoph Hellwig * buffer provided is not large enough to hold the string, the
230f461d2dcSChristoph Hellwig * string is truncated. The copied string is %NULL-terminated.
231f461d2dcSChristoph Hellwig * If the string is being read by the user process, it is copied
232f461d2dcSChristoph Hellwig * and a newline '\n' is added. It is truncated if the buffer is
233f461d2dcSChristoph Hellwig * not large enough.
234f461d2dcSChristoph Hellwig *
235f461d2dcSChristoph Hellwig * Returns 0 on success.
236f461d2dcSChristoph Hellwig */
proc_dostring(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)237f461d2dcSChristoph Hellwig int proc_dostring(const struct ctl_table *table, int write,
238f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
239f461d2dcSChristoph Hellwig {
240f461d2dcSChristoph Hellwig if (write)
241f461d2dcSChristoph Hellwig proc_first_pos_non_zero_ignore(ppos, table);
242f461d2dcSChristoph Hellwig
243f461d2dcSChristoph Hellwig return _proc_do_string(table->data, table->maxlen, write, buffer, lenp,
244f461d2dcSChristoph Hellwig ppos);
245f461d2dcSChristoph Hellwig }
246f461d2dcSChristoph Hellwig
proc_skip_spaces(char ** buf,size_t * size)247f461d2dcSChristoph Hellwig static void proc_skip_spaces(char **buf, size_t *size)
248f461d2dcSChristoph Hellwig {
249f461d2dcSChristoph Hellwig while (*size) {
250f461d2dcSChristoph Hellwig if (!isspace(**buf))
251f461d2dcSChristoph Hellwig break;
252f461d2dcSChristoph Hellwig (*size)--;
253f461d2dcSChristoph Hellwig (*buf)++;
254f461d2dcSChristoph Hellwig }
255f461d2dcSChristoph Hellwig }
256f461d2dcSChristoph Hellwig
proc_skip_char(char ** buf,size_t * size,const char v)257f461d2dcSChristoph Hellwig static void proc_skip_char(char **buf, size_t *size, const char v)
258f461d2dcSChristoph Hellwig {
25978eb4ea2SJoel Granados while (*size) {
26032927393SChristoph Hellwig if (**buf != v)
261f461d2dcSChristoph Hellwig break;
262f461d2dcSChristoph Hellwig (*size)--;
263f461d2dcSChristoph Hellwig (*buf)++;
264f461d2dcSChristoph Hellwig }
26532927393SChristoph Hellwig }
26632927393SChristoph Hellwig
267f461d2dcSChristoph Hellwig /**
268f461d2dcSChristoph Hellwig * strtoul_lenient - parse an ASCII formatted integer from a buffer and only
269bce93322SLinus Torvalds * fail on overflow
270f461d2dcSChristoph Hellwig *
271bce93322SLinus Torvalds * @cp: kernel buffer containing the string to parse
272bce93322SLinus Torvalds * @endp: pointer to store the trailing characters
273bce93322SLinus Torvalds * @base: the base to use
274bce93322SLinus Torvalds * @res: where the parsed integer will be stored
275bce93322SLinus Torvalds *
276bce93322SLinus Torvalds * In case of success 0 is returned and @res will contain the parsed integer,
277f461d2dcSChristoph Hellwig * @endp will hold any trailing characters.
278f461d2dcSChristoph Hellwig * This function will fail the parse on overflow. If there wasn't an overflow
279f461d2dcSChristoph Hellwig * the function will defer the decision what characters count as invalid to the
280f461d2dcSChristoph Hellwig * caller.
281f461d2dcSChristoph Hellwig */
strtoul_lenient(const char * cp,char ** endp,unsigned int base,unsigned long * res)282f461d2dcSChristoph Hellwig static int strtoul_lenient(const char *cp, char **endp, unsigned int base,
283f461d2dcSChristoph Hellwig unsigned long *res)
284f461d2dcSChristoph Hellwig {
285f461d2dcSChristoph Hellwig unsigned long long result;
286f461d2dcSChristoph Hellwig unsigned int rv;
287f461d2dcSChristoph Hellwig
288f461d2dcSChristoph Hellwig cp = _parse_integer_fixup_radix(cp, &base);
289f461d2dcSChristoph Hellwig rv = _parse_integer(cp, base, &result);
290f461d2dcSChristoph Hellwig if ((rv & KSTRTOX_OVERFLOW) || (result != (unsigned long)result))
291f461d2dcSChristoph Hellwig return -ERANGE;
292f461d2dcSChristoph Hellwig
293f461d2dcSChristoph Hellwig cp += rv;
294f461d2dcSChristoph Hellwig
295f461d2dcSChristoph Hellwig if (endp)
296f461d2dcSChristoph Hellwig *endp = (char *)cp;
297f461d2dcSChristoph Hellwig
298f461d2dcSChristoph Hellwig *res = (unsigned long)result;
299f461d2dcSChristoph Hellwig return 0;
300f461d2dcSChristoph Hellwig }
301f461d2dcSChristoph Hellwig
302f461d2dcSChristoph Hellwig #define TMPBUFLEN 22
303f461d2dcSChristoph Hellwig /**
304f461d2dcSChristoph Hellwig * proc_get_long - reads an ASCII formatted integer from a user buffer
305f461d2dcSChristoph Hellwig *
306f461d2dcSChristoph Hellwig * @buf: a kernel buffer
307f461d2dcSChristoph Hellwig * @size: size of the kernel buffer
308f461d2dcSChristoph Hellwig * @val: this is where the number will be stored
309f461d2dcSChristoph Hellwig * @neg: set to %TRUE if number is negative
310f461d2dcSChristoph Hellwig * @perm_tr: a vector which contains the allowed trailers
311f461d2dcSChristoph Hellwig * @perm_tr_len: size of the perm_tr vector
312f461d2dcSChristoph Hellwig * @tr: pointer to store the trailer character
313f461d2dcSChristoph Hellwig *
314f461d2dcSChristoph Hellwig * In case of success %0 is returned and @buf and @size are updated with
315f461d2dcSChristoph Hellwig * the amount of bytes read. If @tr is non-NULL and a trailing
316f461d2dcSChristoph Hellwig * character exists (size is non-zero after returning from this
317f461d2dcSChristoph Hellwig * function), @tr is updated with the trailing character.
318f461d2dcSChristoph Hellwig */
proc_get_long(char ** buf,size_t * size,unsigned long * val,bool * neg,const char * perm_tr,unsigned perm_tr_len,char * tr)319f461d2dcSChristoph Hellwig static int proc_get_long(char **buf, size_t *size,
320f461d2dcSChristoph Hellwig unsigned long *val, bool *neg,
321f461d2dcSChristoph Hellwig const char *perm_tr, unsigned perm_tr_len, char *tr)
322f461d2dcSChristoph Hellwig {
323f461d2dcSChristoph Hellwig char *p, tmp[TMPBUFLEN];
324f461d2dcSChristoph Hellwig ssize_t len = *size;
325f461d2dcSChristoph Hellwig
326f461d2dcSChristoph Hellwig if (len <= 0)
327f461d2dcSChristoph Hellwig return -EINVAL;
328f461d2dcSChristoph Hellwig
329f461d2dcSChristoph Hellwig if (len > TMPBUFLEN - 1)
330f461d2dcSChristoph Hellwig len = TMPBUFLEN - 1;
331f461d2dcSChristoph Hellwig
332f461d2dcSChristoph Hellwig memcpy(tmp, *buf, len);
333f461d2dcSChristoph Hellwig
334f461d2dcSChristoph Hellwig tmp[len] = 0;
335f461d2dcSChristoph Hellwig p = tmp;
336f461d2dcSChristoph Hellwig if (*p == '-' && *size > 1) {
337f461d2dcSChristoph Hellwig *neg = true;
338f461d2dcSChristoph Hellwig p++;
339f461d2dcSChristoph Hellwig } else
340f461d2dcSChristoph Hellwig *neg = false;
341f461d2dcSChristoph Hellwig if (!isdigit(*p))
342f461d2dcSChristoph Hellwig return -EINVAL;
343f461d2dcSChristoph Hellwig
344f461d2dcSChristoph Hellwig if (strtoul_lenient(p, &p, 0, val))
345f461d2dcSChristoph Hellwig return -EINVAL;
346e6cfaf34SLinus Torvalds
347f461d2dcSChristoph Hellwig len = p - tmp;
348e6cfaf34SLinus Torvalds
349f461d2dcSChristoph Hellwig /* We don't know if the next char is whitespace thus we may accept
350f461d2dcSChristoph Hellwig * invalid integers (e.g. 1234...a) or two integers instead of one
351f461d2dcSChristoph Hellwig * (e.g. 123...1). So lets not allow such large numbers. */
352f461d2dcSChristoph Hellwig if (len == TMPBUFLEN - 1)
353f461d2dcSChristoph Hellwig return -EINVAL;
354f461d2dcSChristoph Hellwig
355f461d2dcSChristoph Hellwig if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
356f461d2dcSChristoph Hellwig return -EINVAL;
357f461d2dcSChristoph Hellwig
358f461d2dcSChristoph Hellwig if (tr && (len < *size))
359f461d2dcSChristoph Hellwig *tr = *p;
360f461d2dcSChristoph Hellwig
361f461d2dcSChristoph Hellwig *buf += len;
362f461d2dcSChristoph Hellwig *size -= len;
363f461d2dcSChristoph Hellwig
364f461d2dcSChristoph Hellwig return 0;
365f461d2dcSChristoph Hellwig }
366f461d2dcSChristoph Hellwig
367f461d2dcSChristoph Hellwig /**
368f461d2dcSChristoph Hellwig * proc_put_long - converts an integer to a decimal ASCII formatted string
369f461d2dcSChristoph Hellwig *
370f461d2dcSChristoph Hellwig * @buf: the user buffer
371f461d2dcSChristoph Hellwig * @size: the size of the user buffer
372f461d2dcSChristoph Hellwig * @val: the integer to be converted
373f461d2dcSChristoph Hellwig * @neg: sign of the number, %TRUE for negative
374f461d2dcSChristoph Hellwig *
375f461d2dcSChristoph Hellwig * In case of success @buf and @size are updated with the amount of bytes
376f461d2dcSChristoph Hellwig * written.
377f461d2dcSChristoph Hellwig */
proc_put_long(void ** buf,size_t * size,unsigned long val,bool neg)378f461d2dcSChristoph Hellwig static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
379f461d2dcSChristoph Hellwig {
380f461d2dcSChristoph Hellwig int len;
381f461d2dcSChristoph Hellwig char tmp[TMPBUFLEN], *p = tmp;
382f461d2dcSChristoph Hellwig
383f461d2dcSChristoph Hellwig sprintf(p, "%s%lu", neg ? "-" : "", val);
384f461d2dcSChristoph Hellwig len = strlen(tmp);
385f461d2dcSChristoph Hellwig if (len > *size)
386f461d2dcSChristoph Hellwig len = *size;
387f461d2dcSChristoph Hellwig memcpy(*buf, tmp, len);
388f461d2dcSChristoph Hellwig *size -= len;
389f461d2dcSChristoph Hellwig *buf += len;
390f461d2dcSChristoph Hellwig }
391f461d2dcSChristoph Hellwig #undef TMPBUFLEN
392f461d2dcSChristoph Hellwig
proc_put_char(void ** buf,size_t * size,char c)393f461d2dcSChristoph Hellwig static void proc_put_char(void **buf, size_t *size, char c)
394f461d2dcSChristoph Hellwig {
395f461d2dcSChristoph Hellwig if (*size) {
396f461d2dcSChristoph Hellwig char **buffer = (char **)buf;
39732927393SChristoph Hellwig **buffer = c;
39832927393SChristoph Hellwig
399f461d2dcSChristoph Hellwig (*size)--;
40032927393SChristoph Hellwig (*buffer)++;
401f461d2dcSChristoph Hellwig *buf = *buffer;
402f461d2dcSChristoph Hellwig }
403f461d2dcSChristoph Hellwig }
404f461d2dcSChristoph Hellwig
do_proc_dointvec_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)405f461d2dcSChristoph Hellwig static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
406f461d2dcSChristoph Hellwig int *valp,
407f461d2dcSChristoph Hellwig int write, void *data)
408f461d2dcSChristoph Hellwig {
40932927393SChristoph Hellwig if (write) {
410f461d2dcSChristoph Hellwig if (*negp) {
411f461d2dcSChristoph Hellwig if (*lvalp > (unsigned long) INT_MAX + 1)
412f461d2dcSChristoph Hellwig return -EINVAL;
413f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, -*lvalp);
414f461d2dcSChristoph Hellwig } else {
41532927393SChristoph Hellwig if (*lvalp > (unsigned long) INT_MAX)
416f461d2dcSChristoph Hellwig return -EINVAL;
417f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, *lvalp);
41832927393SChristoph Hellwig }
41932927393SChristoph Hellwig } else {
42032927393SChristoph Hellwig int val = READ_ONCE(*valp);
42132927393SChristoph Hellwig if (val < 0) {
42232927393SChristoph Hellwig *negp = true;
423f461d2dcSChristoph Hellwig *lvalp = -(unsigned long)val;
424f461d2dcSChristoph Hellwig } else {
425f461d2dcSChristoph Hellwig *negp = false;
426f461d2dcSChristoph Hellwig *lvalp = (unsigned long)val;
427f461d2dcSChristoph Hellwig }
428f461d2dcSChristoph Hellwig }
429f461d2dcSChristoph Hellwig return 0;
430f461d2dcSChristoph Hellwig }
431f461d2dcSChristoph Hellwig
do_proc_douintvec_conv(unsigned long * lvalp,unsigned int * valp,int write,void * data)432f461d2dcSChristoph Hellwig static int do_proc_douintvec_conv(unsigned long *lvalp,
433f461d2dcSChristoph Hellwig unsigned int *valp,
434f461d2dcSChristoph Hellwig int write, void *data)
4351f1be04bSKuniyuki Iwashima {
436f461d2dcSChristoph Hellwig if (write) {
437f461d2dcSChristoph Hellwig if (*lvalp > UINT_MAX)
438f461d2dcSChristoph Hellwig return -EINVAL;
4391f1be04bSKuniyuki Iwashima WRITE_ONCE(*valp, *lvalp);
440f461d2dcSChristoph Hellwig } else {
441f461d2dcSChristoph Hellwig unsigned int val = READ_ONCE(*valp);
4421f1be04bSKuniyuki Iwashima *lvalp = (unsigned long)val;
443f461d2dcSChristoph Hellwig }
444f461d2dcSChristoph Hellwig return 0;
445f461d2dcSChristoph Hellwig }
446f461d2dcSChristoph Hellwig
447f461d2dcSChristoph Hellwig static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
448f461d2dcSChristoph Hellwig
__do_proc_dointvec(void * tbl_data,const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(bool * negp,unsigned long * lvalp,int * valp,int write,void * data),void * data)449f461d2dcSChristoph Hellwig static int __do_proc_dointvec(void *tbl_data, const struct ctl_table *table,
450f461d2dcSChristoph Hellwig int write, void *buffer,
451f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos,
452f461d2dcSChristoph Hellwig int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
453f461d2dcSChristoph Hellwig int write, void *data),
454f461d2dcSChristoph Hellwig void *data)
455f461d2dcSChristoph Hellwig {
456f461d2dcSChristoph Hellwig int *i, vleft, first = 1, err = 0;
457f461d2dcSChristoph Hellwig size_t left;
458f461d2dcSChristoph Hellwig char *p;
459f461d2dcSChristoph Hellwig
460f461d2dcSChristoph Hellwig if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
4614762b532SKuniyuki Iwashima *lenp = 0;
462f461d2dcSChristoph Hellwig return 0;
4634762b532SKuniyuki Iwashima }
464f461d2dcSChristoph Hellwig
465f461d2dcSChristoph Hellwig i = (int *) tbl_data;
466f461d2dcSChristoph Hellwig vleft = table->maxlen / sizeof(*i);
467f461d2dcSChristoph Hellwig left = *lenp;
468f461d2dcSChristoph Hellwig
469f461d2dcSChristoph Hellwig if (!conv)
470f461d2dcSChristoph Hellwig conv = do_proc_dointvec_conv;
47132fe9152SThomas Weißschuh
47232927393SChristoph Hellwig if (write) {
473f461d2dcSChristoph Hellwig if (proc_first_pos_non_zero_ignore(ppos, table))
474f461d2dcSChristoph Hellwig goto out;
475f461d2dcSChristoph Hellwig
476f461d2dcSChristoph Hellwig if (left > PAGE_SIZE - 1)
477f461d2dcSChristoph Hellwig left = PAGE_SIZE - 1;
478f461d2dcSChristoph Hellwig p = buffer;
479f461d2dcSChristoph Hellwig }
48032927393SChristoph Hellwig
481f461d2dcSChristoph Hellwig for (; left && vleft--; i++, first=0) {
482f461d2dcSChristoph Hellwig unsigned long lval;
483f461d2dcSChristoph Hellwig bool neg;
484f461d2dcSChristoph Hellwig
485f461d2dcSChristoph Hellwig if (write) {
486f461d2dcSChristoph Hellwig proc_skip_spaces(&p, &left);
487f461d2dcSChristoph Hellwig
488f461d2dcSChristoph Hellwig if (!left)
489f461d2dcSChristoph Hellwig break;
490f461d2dcSChristoph Hellwig err = proc_get_long(&p, &left, &lval, &neg,
491f461d2dcSChristoph Hellwig proc_wspace_sep,
492f461d2dcSChristoph Hellwig sizeof(proc_wspace_sep), NULL);
493f461d2dcSChristoph Hellwig if (err)
494f461d2dcSChristoph Hellwig break;
495f461d2dcSChristoph Hellwig if (conv(&neg, &lval, i, 1, data)) {
496f461d2dcSChristoph Hellwig err = -EINVAL;
497f461d2dcSChristoph Hellwig break;
498f461d2dcSChristoph Hellwig }
499f461d2dcSChristoph Hellwig } else {
50032927393SChristoph Hellwig if (conv(&neg, &lval, i, 0, data)) {
501f461d2dcSChristoph Hellwig err = -EINVAL;
502f461d2dcSChristoph Hellwig break;
503f461d2dcSChristoph Hellwig }
504f461d2dcSChristoph Hellwig if (!first)
505f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '\t');
506f461d2dcSChristoph Hellwig proc_put_long(&buffer, &left, lval, neg);
507f461d2dcSChristoph Hellwig }
508bce93322SLinus Torvalds }
509f461d2dcSChristoph Hellwig
510f461d2dcSChristoph Hellwig if (!write && !first && left && !err)
511f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '\n');
512f461d2dcSChristoph Hellwig if (write && !err && left)
513f461d2dcSChristoph Hellwig proc_skip_spaces(&p, &left);
514f461d2dcSChristoph Hellwig if (write && first)
515f461d2dcSChristoph Hellwig return err ? : -EINVAL;
516f461d2dcSChristoph Hellwig *lenp -= left;
517f461d2dcSChristoph Hellwig out:
518f461d2dcSChristoph Hellwig *ppos += *lenp;
519f461d2dcSChristoph Hellwig return err;
520f461d2dcSChristoph Hellwig }
521f461d2dcSChristoph Hellwig
do_proc_dointvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(bool * negp,unsigned long * lvalp,int * valp,int write,void * data),void * data)522f461d2dcSChristoph Hellwig static int do_proc_dointvec(const struct ctl_table *table, int write,
523f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos,
524f461d2dcSChristoph Hellwig int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
525f461d2dcSChristoph Hellwig int write, void *data),
526f461d2dcSChristoph Hellwig void *data)
52732927393SChristoph Hellwig {
52832927393SChristoph Hellwig return __do_proc_dointvec(table->data, table, write,
529f461d2dcSChristoph Hellwig buffer, lenp, ppos, conv, data);
530f461d2dcSChristoph Hellwig }
531f461d2dcSChristoph Hellwig
do_proc_douintvec_w(unsigned int * tbl_data,const struct ctl_table * table,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(unsigned long * lvalp,unsigned int * valp,int write,void * data),void * data)532f461d2dcSChristoph Hellwig static int do_proc_douintvec_w(unsigned int *tbl_data,
53332927393SChristoph Hellwig const struct ctl_table *table,
534f461d2dcSChristoph Hellwig void *buffer,
535bce93322SLinus Torvalds size_t *lenp, loff_t *ppos,
53632927393SChristoph Hellwig int (*conv)(unsigned long *lvalp,
537f461d2dcSChristoph Hellwig unsigned int *valp,
538f461d2dcSChristoph Hellwig int write, void *data),
539f461d2dcSChristoph Hellwig void *data)
540f461d2dcSChristoph Hellwig {
541f461d2dcSChristoph Hellwig unsigned long lval;
542f461d2dcSChristoph Hellwig int err = 0;
543f461d2dcSChristoph Hellwig size_t left;
54432fe9152SThomas Weißschuh bool neg;
54532927393SChristoph Hellwig char *p = buffer;
546f461d2dcSChristoph Hellwig
547f461d2dcSChristoph Hellwig left = *lenp;
548f461d2dcSChristoph Hellwig
549f461d2dcSChristoph Hellwig if (proc_first_pos_non_zero_ignore(ppos, table))
550f461d2dcSChristoph Hellwig goto bail_early;
551f461d2dcSChristoph Hellwig
552f461d2dcSChristoph Hellwig if (left > PAGE_SIZE - 1)
553f461d2dcSChristoph Hellwig left = PAGE_SIZE - 1;
554f461d2dcSChristoph Hellwig
55532fe9152SThomas Weißschuh proc_skip_spaces(&p, &left);
55632927393SChristoph Hellwig if (!left) {
557f461d2dcSChristoph Hellwig err = -EINVAL;
558f461d2dcSChristoph Hellwig goto out_free;
559f461d2dcSChristoph Hellwig }
560f461d2dcSChristoph Hellwig
561f461d2dcSChristoph Hellwig err = proc_get_long(&p, &left, &lval, &neg,
562f461d2dcSChristoph Hellwig proc_wspace_sep,
563f461d2dcSChristoph Hellwig sizeof(proc_wspace_sep), NULL);
564f461d2dcSChristoph Hellwig if (err || neg) {
565f461d2dcSChristoph Hellwig err = -EINVAL;
566f461d2dcSChristoph Hellwig goto out_free;
56732927393SChristoph Hellwig }
568f461d2dcSChristoph Hellwig
569f461d2dcSChristoph Hellwig if (conv(&lval, tbl_data, 1, data)) {
570f461d2dcSChristoph Hellwig err = -EINVAL;
571f461d2dcSChristoph Hellwig goto out_free;
572f461d2dcSChristoph Hellwig }
573f461d2dcSChristoph Hellwig
574f461d2dcSChristoph Hellwig if (!err && left)
575f461d2dcSChristoph Hellwig proc_skip_spaces(&p, &left);
576f461d2dcSChristoph Hellwig
577bce93322SLinus Torvalds out_free:
578f461d2dcSChristoph Hellwig if (err)
579f461d2dcSChristoph Hellwig return -EINVAL;
580f461d2dcSChristoph Hellwig
581f461d2dcSChristoph Hellwig return 0;
582f461d2dcSChristoph Hellwig
583f461d2dcSChristoph Hellwig /* This is in keeping with old __do_proc_dointvec() */
584f461d2dcSChristoph Hellwig bail_early:
585f461d2dcSChristoph Hellwig *ppos += *lenp;
586f461d2dcSChristoph Hellwig return err;
587f461d2dcSChristoph Hellwig }
588f461d2dcSChristoph Hellwig
do_proc_douintvec_r(unsigned int * tbl_data,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(unsigned long * lvalp,unsigned int * valp,int write,void * data),void * data)589f461d2dcSChristoph Hellwig static int do_proc_douintvec_r(unsigned int *tbl_data, void *buffer,
590f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos,
591f461d2dcSChristoph Hellwig int (*conv)(unsigned long *lvalp,
592f461d2dcSChristoph Hellwig unsigned int *valp,
593f461d2dcSChristoph Hellwig int write, void *data),
594f461d2dcSChristoph Hellwig void *data)
595f461d2dcSChristoph Hellwig {
596f461d2dcSChristoph Hellwig unsigned long lval;
597bce93322SLinus Torvalds int err = 0;
598f461d2dcSChristoph Hellwig size_t left;
599f461d2dcSChristoph Hellwig
600f461d2dcSChristoph Hellwig left = *lenp;
601f461d2dcSChristoph Hellwig
602f461d2dcSChristoph Hellwig if (conv(&lval, tbl_data, 0, data)) {
603f461d2dcSChristoph Hellwig err = -EINVAL;
604f461d2dcSChristoph Hellwig goto out;
605f461d2dcSChristoph Hellwig }
606f461d2dcSChristoph Hellwig
607f461d2dcSChristoph Hellwig proc_put_long(&buffer, &left, lval, false);
608f461d2dcSChristoph Hellwig if (!left)
609f461d2dcSChristoph Hellwig goto out;
610f461d2dcSChristoph Hellwig
61132927393SChristoph Hellwig proc_put_char(&buffer, &left, '\n');
612f461d2dcSChristoph Hellwig
613f461d2dcSChristoph Hellwig out:
614f461d2dcSChristoph Hellwig *lenp -= left;
615f461d2dcSChristoph Hellwig *ppos += *lenp;
616f461d2dcSChristoph Hellwig
617f461d2dcSChristoph Hellwig return err;
618f461d2dcSChristoph Hellwig }
619f461d2dcSChristoph Hellwig
__do_proc_douintvec(void * tbl_data,const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(unsigned long * lvalp,unsigned int * valp,int write,void * data),void * data)620f461d2dcSChristoph Hellwig static int __do_proc_douintvec(void *tbl_data, const struct ctl_table *table,
621f461d2dcSChristoph Hellwig int write, void *buffer,
622f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos,
623f461d2dcSChristoph Hellwig int (*conv)(unsigned long *lvalp,
624f461d2dcSChristoph Hellwig unsigned int *valp,
625f461d2dcSChristoph Hellwig int write, void *data),
626f461d2dcSChristoph Hellwig void *data)
627f461d2dcSChristoph Hellwig {
628f461d2dcSChristoph Hellwig unsigned int *i, vleft;
62932927393SChristoph Hellwig
63032927393SChristoph Hellwig if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
631f461d2dcSChristoph Hellwig *lenp = 0;
632f461d2dcSChristoph Hellwig return 0;
63332927393SChristoph Hellwig }
634f461d2dcSChristoph Hellwig
635f461d2dcSChristoph Hellwig i = (unsigned int *) tbl_data;
636f461d2dcSChristoph Hellwig vleft = table->maxlen / sizeof(*i);
637f461d2dcSChristoph Hellwig
638f461d2dcSChristoph Hellwig /*
639f461d2dcSChristoph Hellwig * Arrays are not supported, keep this simple. *Do not* add
640f461d2dcSChristoph Hellwig * support for them.
641f461d2dcSChristoph Hellwig */
64232fe9152SThomas Weißschuh if (vleft != 1) {
64332927393SChristoph Hellwig *lenp = 0;
644f461d2dcSChristoph Hellwig return -EINVAL;
645f461d2dcSChristoph Hellwig }
646f461d2dcSChristoph Hellwig
647f461d2dcSChristoph Hellwig if (!conv)
648f461d2dcSChristoph Hellwig conv = do_proc_douintvec_conv;
649f461d2dcSChristoph Hellwig
650f461d2dcSChristoph Hellwig if (write)
651f461d2dcSChristoph Hellwig return do_proc_douintvec_w(i, table, buffer, lenp, ppos,
652f461d2dcSChristoph Hellwig conv, data);
653f461d2dcSChristoph Hellwig return do_proc_douintvec_r(i, buffer, lenp, ppos, conv, data);
654f461d2dcSChristoph Hellwig }
655f461d2dcSChristoph Hellwig
do_proc_douintvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,int (* conv)(unsigned long * lvalp,unsigned int * valp,int write,void * data),void * data)656f461d2dcSChristoph Hellwig int do_proc_douintvec(const struct ctl_table *table, int write,
657f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos,
658f461d2dcSChristoph Hellwig int (*conv)(unsigned long *lvalp,
659f461d2dcSChristoph Hellwig unsigned int *valp,
660f461d2dcSChristoph Hellwig int write, void *data),
661f461d2dcSChristoph Hellwig void *data)
662f461d2dcSChristoph Hellwig {
663f461d2dcSChristoph Hellwig return __do_proc_douintvec(table->data, table, write,
664f461d2dcSChristoph Hellwig buffer, lenp, ppos, conv, data);
665f461d2dcSChristoph Hellwig }
666f461d2dcSChristoph Hellwig
667f461d2dcSChristoph Hellwig /**
668f461d2dcSChristoph Hellwig * proc_dobool - read/write a bool
669f461d2dcSChristoph Hellwig * @table: the sysctl table
670f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
671f461d2dcSChristoph Hellwig * @buffer: the user buffer
672f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
673f461d2dcSChristoph Hellwig * @ppos: file position
674f461d2dcSChristoph Hellwig *
675f461d2dcSChristoph Hellwig * Reads/writes one integer value from/to the user buffer,
676f461d2dcSChristoph Hellwig * treated as an ASCII string.
677f461d2dcSChristoph Hellwig *
67832fe9152SThomas Weißschuh * table->data must point to a bool variable and table->maxlen must
67932927393SChristoph Hellwig * be sizeof(bool).
680f461d2dcSChristoph Hellwig *
681f461d2dcSChristoph Hellwig * Returns 0 on success.
682f461d2dcSChristoph Hellwig */
proc_dobool(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)683f461d2dcSChristoph Hellwig int proc_dobool(const struct ctl_table *table, int write, void *buffer,
684f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos)
685f461d2dcSChristoph Hellwig {
686f461d2dcSChristoph Hellwig struct ctl_table tmp;
687f461d2dcSChristoph Hellwig bool *data = table->data;
688f461d2dcSChristoph Hellwig int res, val;
689f461d2dcSChristoph Hellwig
690a2071573SJia He /* Do not support arrays yet. */
691a2071573SJia He if (table->maxlen != sizeof(bool))
692a2071573SJia He return -EINVAL;
693a2071573SJia He
694a2071573SJia He tmp = *table;
695a2071573SJia He tmp.maxlen = sizeof(val);
696a2071573SJia He tmp.data = &val;
697f1aa2eb5SOndrej Mosnacek
698f1aa2eb5SOndrej Mosnacek val = READ_ONCE(*data);
699f1aa2eb5SOndrej Mosnacek res = proc_dointvec(&tmp, write, buffer, lenp, ppos);
700f1aa2eb5SOndrej Mosnacek if (res)
701f1aa2eb5SOndrej Mosnacek return res;
702a2071573SJia He if (write)
703a2071573SJia He WRITE_ONCE(*data, val);
704a2071573SJia He return 0;
70578eb4ea2SJoel Granados }
706a2071573SJia He
707a2071573SJia He /**
708f1aa2eb5SOndrej Mosnacek * proc_dointvec - read a vector of integers
709f1aa2eb5SOndrej Mosnacek * @table: the sysctl table
710f1aa2eb5SOndrej Mosnacek * @write: %TRUE if this is a write to the sysctl file
711f1aa2eb5SOndrej Mosnacek * @buffer: the user buffer
712f1aa2eb5SOndrej Mosnacek * @lenp: the size of the user buffer
713f1aa2eb5SOndrej Mosnacek * @ppos: file position
714f1aa2eb5SOndrej Mosnacek *
715f1aa2eb5SOndrej Mosnacek * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
716f1aa2eb5SOndrej Mosnacek * values from/to the user buffer, treated as an ASCII string.
717f1aa2eb5SOndrej Mosnacek *
718f1aa2eb5SOndrej Mosnacek * Returns 0 on success.
719f1aa2eb5SOndrej Mosnacek */
proc_dointvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)720f1aa2eb5SOndrej Mosnacek int proc_dointvec(const struct ctl_table *table, int write, void *buffer,
721f1aa2eb5SOndrej Mosnacek size_t *lenp, loff_t *ppos)
722f1aa2eb5SOndrej Mosnacek {
723f1aa2eb5SOndrej Mosnacek return do_proc_dointvec(table, write, buffer, lenp, ppos, NULL, NULL);
724f1aa2eb5SOndrej Mosnacek }
725f1aa2eb5SOndrej Mosnacek
726f1aa2eb5SOndrej Mosnacek /**
727a2071573SJia He * proc_douintvec - read a vector of unsigned integers
728a2071573SJia He * @table: the sysctl table
729a2071573SJia He * @write: %TRUE if this is a write to the sysctl file
730f461d2dcSChristoph Hellwig * @buffer: the user buffer
731f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
732f461d2dcSChristoph Hellwig * @ppos: file position
733f461d2dcSChristoph Hellwig *
734f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
735f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string.
736f461d2dcSChristoph Hellwig *
737f461d2dcSChristoph Hellwig * Returns 0 on success.
738f461d2dcSChristoph Hellwig */
proc_douintvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)739f461d2dcSChristoph Hellwig int proc_douintvec(const struct ctl_table *table, int write, void *buffer,
740f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos)
741f461d2dcSChristoph Hellwig {
74278eb4ea2SJoel Granados return do_proc_douintvec(table, write, buffer, lenp, ppos,
74332927393SChristoph Hellwig do_proc_douintvec_conv, NULL);
744f461d2dcSChristoph Hellwig }
745f461d2dcSChristoph Hellwig
746f461d2dcSChristoph Hellwig /*
747f461d2dcSChristoph Hellwig * Taint values can only be increased
748f461d2dcSChristoph Hellwig * This means we can safely use a temporary.
749f461d2dcSChristoph Hellwig */
proc_taint(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)750f461d2dcSChristoph Hellwig static int proc_taint(const struct ctl_table *table, int write,
751f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
752f461d2dcSChristoph Hellwig {
753f461d2dcSChristoph Hellwig struct ctl_table t;
754f461d2dcSChristoph Hellwig unsigned long tmptaint = get_taint();
755f461d2dcSChristoph Hellwig int err;
756f461d2dcSChristoph Hellwig
757f461d2dcSChristoph Hellwig if (write && !capable(CAP_SYS_ADMIN))
758f461d2dcSChristoph Hellwig return -EPERM;
759f461d2dcSChristoph Hellwig
760f461d2dcSChristoph Hellwig t = *table;
76178eb4ea2SJoel Granados t.data = &tmptaint;
76232927393SChristoph Hellwig err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
763f461d2dcSChristoph Hellwig if (err < 0)
764f461d2dcSChristoph Hellwig return err;
765f461d2dcSChristoph Hellwig
766f461d2dcSChristoph Hellwig if (write) {
767f461d2dcSChristoph Hellwig int i;
768f461d2dcSChristoph Hellwig
769f461d2dcSChristoph Hellwig /*
770f461d2dcSChristoph Hellwig * If we are relying on panic_on_taint not producing
771f461d2dcSChristoph Hellwig * false positives due to userspace input, bail out
77278eb4ea2SJoel Granados * before setting the requested taint flags.
77332927393SChristoph Hellwig */
774f461d2dcSChristoph Hellwig if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint))
775f461d2dcSChristoph Hellwig return -EINVAL;
776f461d2dcSChristoph Hellwig
777f461d2dcSChristoph Hellwig /*
778f461d2dcSChristoph Hellwig * Poor man's atomic or. Not worth adding a primitive
779f461d2dcSChristoph Hellwig * to everyone's atomic.h for this
780f461d2dcSChristoph Hellwig */
781f461d2dcSChristoph Hellwig for (i = 0; i < TAINT_FLAGS_COUNT; i++)
782f461d2dcSChristoph Hellwig if ((1UL << i) & tmptaint)
783f461d2dcSChristoph Hellwig add_taint(i, LOCKDEP_STILL_OK);
784f461d2dcSChristoph Hellwig }
785f461d2dcSChristoph Hellwig
786f461d2dcSChristoph Hellwig return err;
787f461d2dcSChristoph Hellwig }
788f461d2dcSChristoph Hellwig
789db38d5c1SRafael Aquini /**
790db38d5c1SRafael Aquini * struct do_proc_dointvec_minmax_conv_param - proc_dointvec_minmax() range checking structure
791db38d5c1SRafael Aquini * @min: pointer to minimum allowable value
792db38d5c1SRafael Aquini * @max: pointer to maximum allowable value
793db38d5c1SRafael Aquini *
794db38d5c1SRafael Aquini * The do_proc_dointvec_minmax_conv_param structure provides the
795db38d5c1SRafael Aquini * minimum and maximum values for doing range checking for those sysctl
796db38d5c1SRafael Aquini * parameters that use the proc_dointvec_minmax() handler.
797db38d5c1SRafael Aquini */
798db38d5c1SRafael Aquini struct do_proc_dointvec_minmax_conv_param {
799f461d2dcSChristoph Hellwig int *min;
800f461d2dcSChristoph Hellwig int *max;
801f461d2dcSChristoph Hellwig };
802f461d2dcSChristoph Hellwig
do_proc_dointvec_minmax_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)803e77132e7SRafael Aquini static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
804e77132e7SRafael Aquini int *valp,
805f461d2dcSChristoph Hellwig int write, void *data)
806f461d2dcSChristoph Hellwig {
807f461d2dcSChristoph Hellwig int tmp, ret;
808f461d2dcSChristoph Hellwig struct do_proc_dointvec_minmax_conv_param *param = data;
809f461d2dcSChristoph Hellwig /*
810f461d2dcSChristoph Hellwig * If writing, first do so via a temporary local int so we can
811f461d2dcSChristoph Hellwig * bounds-check it before touching *valp.
812f461d2dcSChristoph Hellwig */
813f461d2dcSChristoph Hellwig int *ip = write ? &tmp : valp;
814f461d2dcSChristoph Hellwig
815f461d2dcSChristoph Hellwig ret = do_proc_dointvec_conv(negp, lvalp, ip, write, data);
816f461d2dcSChristoph Hellwig if (ret)
817f461d2dcSChristoph Hellwig return ret;
818f461d2dcSChristoph Hellwig
819f461d2dcSChristoph Hellwig if (write) {
820f461d2dcSChristoph Hellwig if ((param->min && *param->min > tmp) ||
821f461d2dcSChristoph Hellwig (param->max && *param->max < tmp))
822f461d2dcSChristoph Hellwig return -EINVAL;
823f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, tmp);
824f461d2dcSChristoph Hellwig }
825f461d2dcSChristoph Hellwig
826f461d2dcSChristoph Hellwig return 0;
827f461d2dcSChristoph Hellwig }
828f461d2dcSChristoph Hellwig
829f461d2dcSChristoph Hellwig /**
830f461d2dcSChristoph Hellwig * proc_dointvec_minmax - read a vector of integers with min/max values
831f461d2dcSChristoph Hellwig * @table: the sysctl table
832f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
833f461d2dcSChristoph Hellwig * @buffer: the user buffer
834f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
835f461d2dcSChristoph Hellwig * @ppos: file position
836f461d2dcSChristoph Hellwig *
837f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
838f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string.
839f461d2dcSChristoph Hellwig *
840f461d2dcSChristoph Hellwig * This routine will ensure the values are within the range specified by
841f461d2dcSChristoph Hellwig * table->extra1 (min) and table->extra2 (max).
842f461d2dcSChristoph Hellwig *
843f461d2dcSChristoph Hellwig * Returns 0 on success or -EINVAL on write when the range check fails.
844f461d2dcSChristoph Hellwig */
proc_dointvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)845f613d86dSKuniyuki Iwashima int proc_dointvec_minmax(const struct ctl_table *table, int write,
846f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
847f461d2dcSChristoph Hellwig {
848f461d2dcSChristoph Hellwig struct do_proc_dointvec_minmax_conv_param param = {
849f461d2dcSChristoph Hellwig .min = (int *) table->extra1,
850f461d2dcSChristoph Hellwig .max = (int *) table->extra2,
851f461d2dcSChristoph Hellwig };
852f461d2dcSChristoph Hellwig return do_proc_dointvec(table, write, buffer, lenp, ppos,
853f461d2dcSChristoph Hellwig do_proc_dointvec_minmax_conv, ¶m);
854f461d2dcSChristoph Hellwig }
855f461d2dcSChristoph Hellwig
856f461d2dcSChristoph Hellwig /**
857f461d2dcSChristoph Hellwig * struct do_proc_douintvec_minmax_conv_param - proc_douintvec_minmax() range checking structure
858f461d2dcSChristoph Hellwig * @min: pointer to minimum allowable value
859f461d2dcSChristoph Hellwig * @max: pointer to maximum allowable value
860f461d2dcSChristoph Hellwig *
861f461d2dcSChristoph Hellwig * The do_proc_douintvec_minmax_conv_param structure provides the
862f461d2dcSChristoph Hellwig * minimum and maximum values for doing range checking for those sysctl
863f461d2dcSChristoph Hellwig * parameters that use the proc_douintvec_minmax() handler.
864f461d2dcSChristoph Hellwig */
865f461d2dcSChristoph Hellwig struct do_proc_douintvec_minmax_conv_param {
866f461d2dcSChristoph Hellwig unsigned int *min;
86778eb4ea2SJoel Granados unsigned int *max;
86832927393SChristoph Hellwig };
869f461d2dcSChristoph Hellwig
do_proc_douintvec_minmax_conv(unsigned long * lvalp,unsigned int * valp,int write,void * data)870f461d2dcSChristoph Hellwig static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
871f461d2dcSChristoph Hellwig unsigned int *valp,
872f461d2dcSChristoph Hellwig int write, void *data)
873f461d2dcSChristoph Hellwig {
874f461d2dcSChristoph Hellwig int ret;
875f461d2dcSChristoph Hellwig unsigned int tmp;
876f461d2dcSChristoph Hellwig struct do_proc_douintvec_minmax_conv_param *param = data;
877f461d2dcSChristoph Hellwig /* write via temporary local uint for bounds-checking */
878f461d2dcSChristoph Hellwig unsigned int *up = write ? &tmp : valp;
879f461d2dcSChristoph Hellwig
880f461d2dcSChristoph Hellwig ret = do_proc_douintvec_conv(lvalp, up, write, data);
881f461d2dcSChristoph Hellwig if (ret)
882f461d2dcSChristoph Hellwig return ret;
883f461d2dcSChristoph Hellwig
884f461d2dcSChristoph Hellwig if (write) {
885f461d2dcSChristoph Hellwig if ((param->min && *param->min > tmp) ||
886f461d2dcSChristoph Hellwig (param->max && *param->max < tmp))
887f461d2dcSChristoph Hellwig return -ERANGE;
888f461d2dcSChristoph Hellwig
889f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, tmp);
890f461d2dcSChristoph Hellwig }
891f461d2dcSChristoph Hellwig
892f461d2dcSChristoph Hellwig return 0;
893f461d2dcSChristoph Hellwig }
894f461d2dcSChristoph Hellwig
895f461d2dcSChristoph Hellwig /**
896f461d2dcSChristoph Hellwig * proc_douintvec_minmax - read a vector of unsigned ints with min/max values
897f461d2dcSChristoph Hellwig * @table: the sysctl table
898f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
899f461d2dcSChristoph Hellwig * @buffer: the user buffer
900f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
901f461d2dcSChristoph Hellwig * @ppos: file position
902f461d2dcSChristoph Hellwig *
903f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
904f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string. Negative
905f461d2dcSChristoph Hellwig * strings are not allowed.
906f461d2dcSChristoph Hellwig *
907f461d2dcSChristoph Hellwig * This routine will ensure the values are within the range specified by
908f461d2dcSChristoph Hellwig * table->extra1 (min) and table->extra2 (max). There is a final sanity
909f461d2dcSChristoph Hellwig * check for UINT_MAX to avoid having to support wrap around uses from
910f461d2dcSChristoph Hellwig * userspace.
9112d3b559dSKuniyuki Iwashima *
912f461d2dcSChristoph Hellwig * Returns 0 on success or -ERANGE on write when the range check fails.
913f461d2dcSChristoph Hellwig */
proc_douintvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)914f461d2dcSChristoph Hellwig int proc_douintvec_minmax(const struct ctl_table *table, int write,
915f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
916f461d2dcSChristoph Hellwig {
917f461d2dcSChristoph Hellwig struct do_proc_douintvec_minmax_conv_param param = {
918f461d2dcSChristoph Hellwig .min = (unsigned int *) table->extra1,
919f461d2dcSChristoph Hellwig .max = (unsigned int *) table->extra2,
920f461d2dcSChristoph Hellwig };
921f461d2dcSChristoph Hellwig return do_proc_douintvec(table, write, buffer, lenp, ppos,
922f461d2dcSChristoph Hellwig do_proc_douintvec_minmax_conv, ¶m);
923f461d2dcSChristoph Hellwig }
924f461d2dcSChristoph Hellwig
925f461d2dcSChristoph Hellwig /**
926f461d2dcSChristoph Hellwig * proc_dou8vec_minmax - read a vector of unsigned chars with min/max values
927f461d2dcSChristoph Hellwig * @table: the sysctl table
928f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
929f461d2dcSChristoph Hellwig * @buffer: the user buffer
930f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
931f461d2dcSChristoph Hellwig * @ppos: file position
932f461d2dcSChristoph Hellwig *
933f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(u8) unsigned chars
934f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string. Negative
935f461d2dcSChristoph Hellwig * strings are not allowed.
93678eb4ea2SJoel Granados *
93732927393SChristoph Hellwig * This routine will ensure the values are within the range specified by
938f461d2dcSChristoph Hellwig * table->extra1 (min) and table->extra2 (max).
939f461d2dcSChristoph Hellwig *
940f461d2dcSChristoph Hellwig * Returns 0 on success or an error on write when the range check fails.
941f461d2dcSChristoph Hellwig */
proc_dou8vec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)942f461d2dcSChristoph Hellwig int proc_dou8vec_minmax(const struct ctl_table *table, int write,
943f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
944f461d2dcSChristoph Hellwig {
945f461d2dcSChristoph Hellwig struct ctl_table tmp;
946f461d2dcSChristoph Hellwig unsigned int min = 0, max = 255U, val;
947cb944413SEric Dumazet u8 *data = table->data;
948cb944413SEric Dumazet struct do_proc_douintvec_minmax_conv_param param = {
949cb944413SEric Dumazet .min = &min,
950cb944413SEric Dumazet .max = &max,
951cb944413SEric Dumazet };
952cb944413SEric Dumazet int res;
953cb944413SEric Dumazet
954cb944413SEric Dumazet /* Do not support arrays yet. */
955cb944413SEric Dumazet if (table->maxlen != sizeof(u8))
956cb944413SEric Dumazet return -EINVAL;
957cb944413SEric Dumazet
958cb944413SEric Dumazet if (table->extra1)
959cb944413SEric Dumazet min = *(unsigned int *) table->extra1;
960cb944413SEric Dumazet if (table->extra2)
961cb944413SEric Dumazet max = *(unsigned int *) table->extra2;
962cb944413SEric Dumazet
963cb944413SEric Dumazet tmp = *table;
96478eb4ea2SJoel Granados
965cb944413SEric Dumazet tmp.maxlen = sizeof(val);
966cb944413SEric Dumazet tmp.data = &val;
967cb944413SEric Dumazet val = READ_ONCE(*data);
968cb944413SEric Dumazet res = do_proc_douintvec(&tmp, write, buffer, lenp, ppos,
969cb944413SEric Dumazet do_proc_douintvec_minmax_conv, ¶m);
970cb944413SEric Dumazet if (res)
971cb944413SEric Dumazet return res;
972cb944413SEric Dumazet if (write)
973cb944413SEric Dumazet WRITE_ONCE(*data, val);
974cb944413SEric Dumazet return 0;
975cb944413SEric Dumazet }
976cb944413SEric Dumazet EXPORT_SYMBOL_GPL(proc_dou8vec_minmax);
977cb944413SEric Dumazet
978cb944413SEric Dumazet #ifdef CONFIG_MAGIC_SYSRQ
sysrq_sysctl_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)979cb944413SEric Dumazet static int sysrq_sysctl_handler(const struct ctl_table *table, int write,
980b5ffbd13SWen Yang void *buffer, size_t *lenp, loff_t *ppos)
981cb944413SEric Dumazet {
982b5ffbd13SWen Yang int tmp, ret;
983cb944413SEric Dumazet
984cb944413SEric Dumazet tmp = sysrq_mask();
985cb944413SEric Dumazet
986cb944413SEric Dumazet ret = __do_proc_dointvec(&tmp, table, write, buffer,
987cb944413SEric Dumazet lenp, ppos, NULL, NULL);
988cb944413SEric Dumazet if (ret || !write)
9897dee5d77SKuniyuki Iwashima return ret;
990cb944413SEric Dumazet
991cb944413SEric Dumazet if (write)
992cb944413SEric Dumazet sysrq_toggle_support(tmp);
993cb944413SEric Dumazet
994cb944413SEric Dumazet return 0;
9957dee5d77SKuniyuki Iwashima }
996cb944413SEric Dumazet #endif
997cb944413SEric Dumazet
__do_proc_doulongvec_minmax(void * data,const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,unsigned long convmul,unsigned long convdiv)998cb944413SEric Dumazet static int __do_proc_doulongvec_minmax(void *data,
999cb944413SEric Dumazet const struct ctl_table *table, int write,
1000f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos,
100178eb4ea2SJoel Granados unsigned long convmul, unsigned long convdiv)
100232927393SChristoph Hellwig {
1003f461d2dcSChristoph Hellwig unsigned long *i, *min, *max;
1004f461d2dcSChristoph Hellwig int vleft, first = 1, err = 0;
1005f461d2dcSChristoph Hellwig size_t left;
1006f461d2dcSChristoph Hellwig char *p;
1007f461d2dcSChristoph Hellwig
1008f461d2dcSChristoph Hellwig if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
1009f461d2dcSChristoph Hellwig *lenp = 0;
1010f461d2dcSChristoph Hellwig return 0;
1011f461d2dcSChristoph Hellwig }
1012f461d2dcSChristoph Hellwig
1013f461d2dcSChristoph Hellwig i = data;
1014f461d2dcSChristoph Hellwig min = table->extra1;
1015f461d2dcSChristoph Hellwig max = table->extra2;
1016f461d2dcSChristoph Hellwig vleft = table->maxlen / sizeof(unsigned long);
1017f461d2dcSChristoph Hellwig left = *lenp;
1018f461d2dcSChristoph Hellwig
1019f461d2dcSChristoph Hellwig if (write) {
102032fe9152SThomas Weißschuh if (proc_first_pos_non_zero_ignore(ppos, table))
102132fe9152SThomas Weißschuh goto out;
102232fe9152SThomas Weißschuh
102332927393SChristoph Hellwig if (left > PAGE_SIZE - 1)
1024f461d2dcSChristoph Hellwig left = PAGE_SIZE - 1;
1025f461d2dcSChristoph Hellwig p = buffer;
1026f461d2dcSChristoph Hellwig }
1027f461d2dcSChristoph Hellwig
102832927393SChristoph Hellwig for (; left && vleft--; i++, first = 0) {
1029f461d2dcSChristoph Hellwig unsigned long val;
1030f461d2dcSChristoph Hellwig
1031f461d2dcSChristoph Hellwig if (write) {
1032f461d2dcSChristoph Hellwig bool neg;
1033f461d2dcSChristoph Hellwig
1034f461d2dcSChristoph Hellwig proc_skip_spaces(&p, &left);
10358ebc4123SDong Chuanjian if (!left)
10368ebc4123SDong Chuanjian break;
10378ebc4123SDong Chuanjian
1038f461d2dcSChristoph Hellwig err = proc_get_long(&p, &left, &val, &neg,
1039f461d2dcSChristoph Hellwig proc_wspace_sep,
1040f461d2dcSChristoph Hellwig sizeof(proc_wspace_sep), NULL);
1041f461d2dcSChristoph Hellwig if (err || neg) {
1042f461d2dcSChristoph Hellwig err = -EINVAL;
1043f461d2dcSChristoph Hellwig break;
1044f461d2dcSChristoph Hellwig }
1045f461d2dcSChristoph Hellwig
1046f461d2dcSChristoph Hellwig val = convmul * val / convdiv;
104732927393SChristoph Hellwig if ((min && val < *min) || (max && val > *max)) {
1048f461d2dcSChristoph Hellwig err = -EINVAL;
1049f461d2dcSChristoph Hellwig break;
1050f461d2dcSChristoph Hellwig }
1051f461d2dcSChristoph Hellwig WRITE_ONCE(*i, val);
1052f461d2dcSChristoph Hellwig } else {
1053f461d2dcSChristoph Hellwig val = convdiv * READ_ONCE(*i) / convmul;
1054f461d2dcSChristoph Hellwig if (!first)
1055f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '\t');
1056bce93322SLinus Torvalds proc_put_long(&buffer, &left, val, false);
1057f461d2dcSChristoph Hellwig }
1058f461d2dcSChristoph Hellwig }
1059f461d2dcSChristoph Hellwig
1060f461d2dcSChristoph Hellwig if (!write && !first && left && !err)
1061f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '\n');
1062f461d2dcSChristoph Hellwig if (write && !err)
10631622ed7dSBaokun Li proc_skip_spaces(&p, &left);
10641622ed7dSBaokun Li if (write && first)
1065f461d2dcSChristoph Hellwig return err ? : -EINVAL;
10661622ed7dSBaokun Li *lenp -= left;
10671622ed7dSBaokun Li out:
1068f461d2dcSChristoph Hellwig *ppos += *lenp;
1069f461d2dcSChristoph Hellwig return err;
1070f461d2dcSChristoph Hellwig }
1071f461d2dcSChristoph Hellwig
do_proc_doulongvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos,unsigned long convmul,unsigned long convdiv)1072f461d2dcSChristoph Hellwig static int do_proc_doulongvec_minmax(const struct ctl_table *table, int write,
1073c31bcc8fSKuniyuki Iwashima void *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul,
1074f461d2dcSChristoph Hellwig unsigned long convdiv)
1075c31bcc8fSKuniyuki Iwashima {
107632927393SChristoph Hellwig return __do_proc_doulongvec_minmax(table->data, table, write,
107732927393SChristoph Hellwig buffer, lenp, ppos, convmul, convdiv);
107832927393SChristoph Hellwig }
1079f461d2dcSChristoph Hellwig
1080f461d2dcSChristoph Hellwig /**
1081f461d2dcSChristoph Hellwig * proc_doulongvec_minmax - read a vector of long integers with min/max values
1082f461d2dcSChristoph Hellwig * @table: the sysctl table
108332927393SChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
1084f461d2dcSChristoph Hellwig * @buffer: the user buffer
1085bce93322SLinus Torvalds * @lenp: the size of the user buffer
108632927393SChristoph Hellwig * @ppos: file position
1087f461d2dcSChristoph Hellwig *
1088f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1089f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string.
1090f461d2dcSChristoph Hellwig *
1091f461d2dcSChristoph Hellwig * This routine will ensure the values are within the range specified by
1092f461d2dcSChristoph Hellwig * table->extra1 (min) and table->extra2 (max).
1093f461d2dcSChristoph Hellwig *
109432fe9152SThomas Weißschuh * Returns 0 on success.
109532927393SChristoph Hellwig */
proc_doulongvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1096f461d2dcSChristoph Hellwig int proc_doulongvec_minmax(const struct ctl_table *table, int write,
1097f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1098f461d2dcSChristoph Hellwig {
1099f461d2dcSChristoph Hellwig return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
1100f461d2dcSChristoph Hellwig }
1101f461d2dcSChristoph Hellwig
1102f461d2dcSChristoph Hellwig /**
1103f461d2dcSChristoph Hellwig * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1104f461d2dcSChristoph Hellwig * @table: the sysctl table
1105f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
1106f461d2dcSChristoph Hellwig * @buffer: the user buffer
1107f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
1108f461d2dcSChristoph Hellwig * @ppos: file position
1109f461d2dcSChristoph Hellwig *
1110f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1111f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string. The values
1112f461d2dcSChristoph Hellwig * are treated as milliseconds, and converted to jiffies when they are stored.
1113f461d2dcSChristoph Hellwig *
1114f461d2dcSChristoph Hellwig * This routine will ensure the values are within the range specified by
1115f461d2dcSChristoph Hellwig * table->extra1 (min) and table->extra2 (max).
1116f461d2dcSChristoph Hellwig *
1117f461d2dcSChristoph Hellwig * Returns 0 on success.
111878eb4ea2SJoel Granados */
proc_doulongvec_ms_jiffies_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)111932927393SChristoph Hellwig int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1120f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1121f461d2dcSChristoph Hellwig {
1122f461d2dcSChristoph Hellwig return do_proc_doulongvec_minmax(table, write, buffer,
1123f461d2dcSChristoph Hellwig lenp, ppos, HZ, 1000l);
1124f461d2dcSChristoph Hellwig }
1125f461d2dcSChristoph Hellwig
1126f461d2dcSChristoph Hellwig
do_proc_dointvec_jiffies_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)1127f461d2dcSChristoph Hellwig static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
1128f461d2dcSChristoph Hellwig int *valp,
1129f461d2dcSChristoph Hellwig int write, void *data)
1130f461d2dcSChristoph Hellwig {
1131f461d2dcSChristoph Hellwig if (write) {
1132f461d2dcSChristoph Hellwig if (*lvalp > INT_MAX / HZ)
1133f461d2dcSChristoph Hellwig return 1;
1134f461d2dcSChristoph Hellwig if (*negp)
1135f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, -*lvalp * HZ);
1136f461d2dcSChristoph Hellwig else
1137f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, *lvalp * HZ);
1138f461d2dcSChristoph Hellwig } else {
1139f461d2dcSChristoph Hellwig int val = READ_ONCE(*valp);
1140f461d2dcSChristoph Hellwig unsigned long lval;
114178eb4ea2SJoel Granados if (val < 0) {
114232927393SChristoph Hellwig *negp = true;
1143f461d2dcSChristoph Hellwig lval = -(unsigned long)val;
1144f461d2dcSChristoph Hellwig } else {
1145f461d2dcSChristoph Hellwig *negp = false;
1146f461d2dcSChristoph Hellwig lval = (unsigned long)val;
1147f461d2dcSChristoph Hellwig }
1148f461d2dcSChristoph Hellwig *lvalp = lval / HZ;
1149f461d2dcSChristoph Hellwig }
1150f461d2dcSChristoph Hellwig return 0;
1151f461d2dcSChristoph Hellwig }
1152f461d2dcSChristoph Hellwig
do_proc_dointvec_userhz_jiffies_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)1153f461d2dcSChristoph Hellwig static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
1154f461d2dcSChristoph Hellwig int *valp,
1155f461d2dcSChristoph Hellwig int write, void *data)
1156e8778208SKuniyuki Iwashima {
1157e8778208SKuniyuki Iwashima if (write) {
1158e8778208SKuniyuki Iwashima if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
1159e8778208SKuniyuki Iwashima return 1;
1160f461d2dcSChristoph Hellwig *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
1161e8778208SKuniyuki Iwashima } else {
1162f461d2dcSChristoph Hellwig int val = *valp;
1163f461d2dcSChristoph Hellwig unsigned long lval;
1164f461d2dcSChristoph Hellwig if (val < 0) {
1165f461d2dcSChristoph Hellwig *negp = true;
1166f461d2dcSChristoph Hellwig lval = -(unsigned long)val;
1167f461d2dcSChristoph Hellwig } else {
1168f461d2dcSChristoph Hellwig *negp = false;
1169f461d2dcSChristoph Hellwig lval = (unsigned long)val;
1170f461d2dcSChristoph Hellwig }
1171f461d2dcSChristoph Hellwig *lvalp = jiffies_to_clock_t(lval);
1172f461d2dcSChristoph Hellwig }
1173f461d2dcSChristoph Hellwig return 0;
1174f461d2dcSChristoph Hellwig }
1175f461d2dcSChristoph Hellwig
do_proc_dointvec_ms_jiffies_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)1176f461d2dcSChristoph Hellwig static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
1177f461d2dcSChristoph Hellwig int *valp,
1178f461d2dcSChristoph Hellwig int write, void *data)
1179f461d2dcSChristoph Hellwig {
1180f461d2dcSChristoph Hellwig if (write) {
1181f461d2dcSChristoph Hellwig unsigned long jif = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
1182f461d2dcSChristoph Hellwig
1183f461d2dcSChristoph Hellwig if (jif > INT_MAX)
1184f461d2dcSChristoph Hellwig return 1;
1185f461d2dcSChristoph Hellwig WRITE_ONCE(*valp, (int)jif);
1186f461d2dcSChristoph Hellwig } else {
1187f461d2dcSChristoph Hellwig int val = READ_ONCE(*valp);
1188f461d2dcSChristoph Hellwig unsigned long lval;
1189f461d2dcSChristoph Hellwig if (val < 0) {
1190f461d2dcSChristoph Hellwig *negp = true;
1191f461d2dcSChristoph Hellwig lval = -(unsigned long)val;
1192f461d2dcSChristoph Hellwig } else {
1193f461d2dcSChristoph Hellwig *negp = false;
1194f461d2dcSChristoph Hellwig lval = (unsigned long)val;
1195f461d2dcSChristoph Hellwig }
1196f461d2dcSChristoph Hellwig *lvalp = jiffies_to_msecs(lval);
1197f461d2dcSChristoph Hellwig }
1198f461d2dcSChristoph Hellwig return 0;
1199f461d2dcSChristoph Hellwig }
1200f461d2dcSChristoph Hellwig
do_proc_dointvec_ms_jiffies_minmax_conv(bool * negp,unsigned long * lvalp,int * valp,int write,void * data)1201f461d2dcSChristoph Hellwig static int do_proc_dointvec_ms_jiffies_minmax_conv(bool *negp, unsigned long *lvalp,
1202f461d2dcSChristoph Hellwig int *valp, int write, void *data)
1203f461d2dcSChristoph Hellwig {
1204f461d2dcSChristoph Hellwig int tmp, ret;
1205f461d2dcSChristoph Hellwig struct do_proc_dointvec_minmax_conv_param *param = data;
1206f461d2dcSChristoph Hellwig /*
12077d1025e5SKuniyuki Iwashima * If writing, first do so via a temporary local int so we can
1208f461d2dcSChristoph Hellwig * bounds-check it before touching *valp.
12097d1025e5SKuniyuki Iwashima */
1210f461d2dcSChristoph Hellwig int *ip = write ? &tmp : valp;
1211f461d2dcSChristoph Hellwig
1212f461d2dcSChristoph Hellwig ret = do_proc_dointvec_ms_jiffies_conv(negp, lvalp, ip, write, data);
1213f461d2dcSChristoph Hellwig if (ret)
1214f461d2dcSChristoph Hellwig return ret;
1215f461d2dcSChristoph Hellwig
1216f461d2dcSChristoph Hellwig if (write) {
1217f461d2dcSChristoph Hellwig if ((param->min && *param->min > tmp) ||
1218f461d2dcSChristoph Hellwig (param->max && *param->max < tmp))
1219f461d2dcSChristoph Hellwig return -EINVAL;
1220f461d2dcSChristoph Hellwig *valp = tmp;
1221f461d2dcSChristoph Hellwig }
1222f461d2dcSChristoph Hellwig return 0;
1223c381d02bSYuwei Wang }
1224c381d02bSYuwei Wang
1225c381d02bSYuwei Wang /**
1226c381d02bSYuwei Wang * proc_dointvec_jiffies - read a vector of integers as seconds
1227c381d02bSYuwei Wang * @table: the sysctl table
1228c381d02bSYuwei Wang * @write: %TRUE if this is a write to the sysctl file
1229c381d02bSYuwei Wang * @buffer: the user buffer
1230c381d02bSYuwei Wang * @lenp: the size of the user buffer
1231c381d02bSYuwei Wang * @ppos: file position
1232c381d02bSYuwei Wang *
1233c381d02bSYuwei Wang * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1234c381d02bSYuwei Wang * values from/to the user buffer, treated as an ASCII string.
1235c381d02bSYuwei Wang * The values read are assumed to be in seconds, and are converted into
1236c381d02bSYuwei Wang * jiffies.
1237c381d02bSYuwei Wang *
1238c381d02bSYuwei Wang * Returns 0 on success.
1239c381d02bSYuwei Wang */
proc_dointvec_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1240c381d02bSYuwei Wang int proc_dointvec_jiffies(const struct ctl_table *table, int write,
1241c381d02bSYuwei Wang void *buffer, size_t *lenp, loff_t *ppos)
1242c381d02bSYuwei Wang {
1243c381d02bSYuwei Wang return do_proc_dointvec(table,write,buffer,lenp,ppos,
1244c381d02bSYuwei Wang do_proc_dointvec_jiffies_conv,NULL);
1245c381d02bSYuwei Wang }
1246c381d02bSYuwei Wang
proc_dointvec_ms_jiffies_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1247f461d2dcSChristoph Hellwig int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1248f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1249f461d2dcSChristoph Hellwig {
1250f461d2dcSChristoph Hellwig struct do_proc_dointvec_minmax_conv_param param = {
1251f461d2dcSChristoph Hellwig .min = (int *) table->extra1,
1252f461d2dcSChristoph Hellwig .max = (int *) table->extra2,
1253f461d2dcSChristoph Hellwig };
1254f461d2dcSChristoph Hellwig return do_proc_dointvec(table, write, buffer, lenp, ppos,
1255f461d2dcSChristoph Hellwig do_proc_dointvec_ms_jiffies_minmax_conv, ¶m);
1256f461d2dcSChristoph Hellwig }
1257f461d2dcSChristoph Hellwig
1258f461d2dcSChristoph Hellwig /**
1259f461d2dcSChristoph Hellwig * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
1260f461d2dcSChristoph Hellwig * @table: the sysctl table
1261f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
126278eb4ea2SJoel Granados * @buffer: the user buffer
126332927393SChristoph Hellwig * @lenp: the size of the user buffer
1264f461d2dcSChristoph Hellwig * @ppos: pointer to the file position
1265f461d2dcSChristoph Hellwig *
1266f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1267f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string.
1268f461d2dcSChristoph Hellwig * The values read are assumed to be in 1/USER_HZ seconds, and
126978eb4ea2SJoel Granados * are converted into jiffies.
1270c381d02bSYuwei Wang *
1271c381d02bSYuwei Wang * Returns 0 on success.
1272c381d02bSYuwei Wang */
proc_dointvec_userhz_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1273c381d02bSYuwei Wang int proc_dointvec_userhz_jiffies(const struct ctl_table *table, int write,
1274c381d02bSYuwei Wang void *buffer, size_t *lenp, loff_t *ppos)
1275c381d02bSYuwei Wang {
1276c381d02bSYuwei Wang return do_proc_dointvec(table, write, buffer, lenp, ppos,
1277c381d02bSYuwei Wang do_proc_dointvec_userhz_jiffies_conv, NULL);
1278c381d02bSYuwei Wang }
1279c381d02bSYuwei Wang
1280f461d2dcSChristoph Hellwig /**
1281f461d2dcSChristoph Hellwig * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
1282f461d2dcSChristoph Hellwig * @table: the sysctl table
1283f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
1284f461d2dcSChristoph Hellwig * @buffer: the user buffer
1285f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
1286f461d2dcSChristoph Hellwig * @ppos: the current position in the file
1287f461d2dcSChristoph Hellwig *
1288f461d2dcSChristoph Hellwig * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1289f461d2dcSChristoph Hellwig * values from/to the user buffer, treated as an ASCII string.
1290f461d2dcSChristoph Hellwig * The values read are assumed to be in 1/1000 seconds, and
1291f461d2dcSChristoph Hellwig * are converted into jiffies.
1292f461d2dcSChristoph Hellwig *
1293f461d2dcSChristoph Hellwig * Returns 0 on success.
1294f461d2dcSChristoph Hellwig */
proc_dointvec_ms_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)129578eb4ea2SJoel Granados int proc_dointvec_ms_jiffies(const struct ctl_table *table, int write, void *buffer,
129632927393SChristoph Hellwig size_t *lenp, loff_t *ppos)
1297f461d2dcSChristoph Hellwig {
1298f461d2dcSChristoph Hellwig return do_proc_dointvec(table, write, buffer, lenp, ppos,
1299f461d2dcSChristoph Hellwig do_proc_dointvec_ms_jiffies_conv, NULL);
1300f461d2dcSChristoph Hellwig }
1301f461d2dcSChristoph Hellwig
proc_do_cad_pid(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1302f461d2dcSChristoph Hellwig static int proc_do_cad_pid(const struct ctl_table *table, int write, void *buffer,
1303f461d2dcSChristoph Hellwig size_t *lenp, loff_t *ppos)
1304f461d2dcSChristoph Hellwig {
1305f461d2dcSChristoph Hellwig struct pid *new_pid;
1306f461d2dcSChristoph Hellwig pid_t tmp;
1307f461d2dcSChristoph Hellwig int r;
1308f461d2dcSChristoph Hellwig
1309f461d2dcSChristoph Hellwig tmp = pid_vnr(cad_pid);
1310f461d2dcSChristoph Hellwig
1311f461d2dcSChristoph Hellwig r = __do_proc_dointvec(&tmp, table, write, buffer,
1312f461d2dcSChristoph Hellwig lenp, ppos, NULL, NULL);
1313f461d2dcSChristoph Hellwig if (r || !write)
1314f461d2dcSChristoph Hellwig return r;
1315f461d2dcSChristoph Hellwig
1316f461d2dcSChristoph Hellwig new_pid = find_get_pid(tmp);
131778eb4ea2SJoel Granados if (!new_pid)
131832927393SChristoph Hellwig return -ESRCH;
1319f461d2dcSChristoph Hellwig
1320f461d2dcSChristoph Hellwig put_pid(xchg(&cad_pid, new_pid));
1321f461d2dcSChristoph Hellwig return 0;
1322f461d2dcSChristoph Hellwig }
1323f461d2dcSChristoph Hellwig
132478eb4ea2SJoel Granados /**
132532927393SChristoph Hellwig * proc_do_large_bitmap - read/write from/to a large bitmap
1326f461d2dcSChristoph Hellwig * @table: the sysctl table
1327f461d2dcSChristoph Hellwig * @write: %TRUE if this is a write to the sysctl file
1328f461d2dcSChristoph Hellwig * @buffer: the user buffer
1329f461d2dcSChristoph Hellwig * @lenp: the size of the user buffer
1330f461d2dcSChristoph Hellwig * @ppos: file position
1331f461d2dcSChristoph Hellwig *
1332f461d2dcSChristoph Hellwig * The bitmap is stored at table->data and the bitmap length (in bits)
1333f461d2dcSChristoph Hellwig * in table->maxlen.
1334f461d2dcSChristoph Hellwig *
1335f461d2dcSChristoph Hellwig * We use a range comma separated format (e.g. 1,3-4,10-10) so that
1336f461d2dcSChristoph Hellwig * large bitmaps may be represented in a compact manner. Writing into
1337f461d2dcSChristoph Hellwig * the file will clear the bitmap then update it with the given input.
1338f461d2dcSChristoph Hellwig *
1339f461d2dcSChristoph Hellwig * Returns 0 on success.
1340f461d2dcSChristoph Hellwig */
proc_do_large_bitmap(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1341f461d2dcSChristoph Hellwig int proc_do_large_bitmap(const struct ctl_table *table, int write,
1342f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1343f461d2dcSChristoph Hellwig {
1344f461d2dcSChristoph Hellwig int err = 0;
1345f461d2dcSChristoph Hellwig size_t left = *lenp;
1346f461d2dcSChristoph Hellwig unsigned long bitmap_len = table->maxlen;
1347f461d2dcSChristoph Hellwig unsigned long *bitmap = *(unsigned long **) table->data;
1348f461d2dcSChristoph Hellwig unsigned long *tmp_bitmap = NULL;
1349f461d2dcSChristoph Hellwig char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
1350f461d2dcSChristoph Hellwig
1351f461d2dcSChristoph Hellwig if (!bitmap || !bitmap_len || !left || (*ppos && !write)) {
1352f461d2dcSChristoph Hellwig *lenp = 0;
1353f461d2dcSChristoph Hellwig return 0;
1354f461d2dcSChristoph Hellwig }
1355f461d2dcSChristoph Hellwig
1356f461d2dcSChristoph Hellwig if (write) {
1357f461d2dcSChristoph Hellwig char *p = buffer;
1358f461d2dcSChristoph Hellwig size_t skipped = 0;
1359f461d2dcSChristoph Hellwig
1360f461d2dcSChristoph Hellwig if (left > PAGE_SIZE - 1) {
1361f461d2dcSChristoph Hellwig left = PAGE_SIZE - 1;
1362f461d2dcSChristoph Hellwig /* How much of the buffer we'll skip this pass */
136378eb4ea2SJoel Granados skipped = *lenp - left;
136432927393SChristoph Hellwig }
1365f461d2dcSChristoph Hellwig
1366f461d2dcSChristoph Hellwig tmp_bitmap = bitmap_zalloc(bitmap_len, GFP_KERNEL);
1367f461d2dcSChristoph Hellwig if (!tmp_bitmap)
1368f461d2dcSChristoph Hellwig return -ENOMEM;
1369f461d2dcSChristoph Hellwig proc_skip_char(&p, &left, '\n');
1370f461d2dcSChristoph Hellwig while (!err && left) {
1371f461d2dcSChristoph Hellwig unsigned long val_a, val_b;
1372f461d2dcSChristoph Hellwig bool neg;
1373f461d2dcSChristoph Hellwig size_t saved_left;
1374f461d2dcSChristoph Hellwig
1375f461d2dcSChristoph Hellwig /* In case we stop parsing mid-number, we can reset */
1376f461d2dcSChristoph Hellwig saved_left = left;
1377f461d2dcSChristoph Hellwig err = proc_get_long(&p, &left, &val_a, &neg, tr_a,
1378f461d2dcSChristoph Hellwig sizeof(tr_a), &c);
137932927393SChristoph Hellwig /*
1380f461d2dcSChristoph Hellwig * If we consumed the entirety of a truncated buffer or
1381f461d2dcSChristoph Hellwig * only one char is left (may be a "-"), then stop here,
1382f461d2dcSChristoph Hellwig * reset, & come back for more.
1383f461d2dcSChristoph Hellwig */
1384f461d2dcSChristoph Hellwig if ((left <= 1) && skipped) {
1385f461d2dcSChristoph Hellwig left = saved_left;
1386f461d2dcSChristoph Hellwig break;
1387f461d2dcSChristoph Hellwig }
1388f461d2dcSChristoph Hellwig
138932927393SChristoph Hellwig if (err)
1390f461d2dcSChristoph Hellwig break;
1391f461d2dcSChristoph Hellwig if (val_a >= bitmap_len || neg) {
1392f461d2dcSChristoph Hellwig err = -EINVAL;
1393f461d2dcSChristoph Hellwig break;
1394f461d2dcSChristoph Hellwig }
1395f461d2dcSChristoph Hellwig
1396f461d2dcSChristoph Hellwig val_b = val_a;
1397f461d2dcSChristoph Hellwig if (left) {
1398f461d2dcSChristoph Hellwig p++;
1399f461d2dcSChristoph Hellwig left--;
1400f461d2dcSChristoph Hellwig }
1401f461d2dcSChristoph Hellwig
1402f461d2dcSChristoph Hellwig if (c == '-') {
1403f461d2dcSChristoph Hellwig err = proc_get_long(&p, &left, &val_b,
1404f461d2dcSChristoph Hellwig &neg, tr_b, sizeof(tr_b),
1405f461d2dcSChristoph Hellwig &c);
1406f461d2dcSChristoph Hellwig /*
1407f461d2dcSChristoph Hellwig * If we consumed all of a truncated buffer or
1408f461d2dcSChristoph Hellwig * then stop here, reset, & come back for more.
1409f461d2dcSChristoph Hellwig */
1410f461d2dcSChristoph Hellwig if (!left && skipped) {
1411f461d2dcSChristoph Hellwig left = saved_left;
1412f461d2dcSChristoph Hellwig break;
1413f461d2dcSChristoph Hellwig }
1414f461d2dcSChristoph Hellwig
1415f461d2dcSChristoph Hellwig if (err)
1416f461d2dcSChristoph Hellwig break;
1417f461d2dcSChristoph Hellwig if (val_b >= bitmap_len || neg ||
1418f461d2dcSChristoph Hellwig val_a > val_b) {
1419f461d2dcSChristoph Hellwig err = -EINVAL;
1420f461d2dcSChristoph Hellwig break;
1421f461d2dcSChristoph Hellwig }
1422f461d2dcSChristoph Hellwig if (left) {
1423f461d2dcSChristoph Hellwig p++;
1424f461d2dcSChristoph Hellwig left--;
1425f461d2dcSChristoph Hellwig }
1426f461d2dcSChristoph Hellwig }
1427f461d2dcSChristoph Hellwig
1428f461d2dcSChristoph Hellwig bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
1429f461d2dcSChristoph Hellwig proc_skip_char(&p, &left, '\n');
1430f461d2dcSChristoph Hellwig }
1431f461d2dcSChristoph Hellwig left += skipped;
1432f461d2dcSChristoph Hellwig } else {
1433f461d2dcSChristoph Hellwig unsigned long bit_a, bit_b = 0;
1434f461d2dcSChristoph Hellwig bool first = 1;
1435f461d2dcSChristoph Hellwig
1436f461d2dcSChristoph Hellwig while (left) {
1437f461d2dcSChristoph Hellwig bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
1438f461d2dcSChristoph Hellwig if (bit_a >= bitmap_len)
1439f461d2dcSChristoph Hellwig break;
1440f461d2dcSChristoph Hellwig bit_b = find_next_zero_bit(bitmap, bitmap_len,
1441f461d2dcSChristoph Hellwig bit_a + 1) - 1;
1442f461d2dcSChristoph Hellwig
1443f461d2dcSChristoph Hellwig if (!first)
1444f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, ',');
1445f461d2dcSChristoph Hellwig proc_put_long(&buffer, &left, bit_a, false);
1446f461d2dcSChristoph Hellwig if (bit_a != bit_b) {
1447f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '-');
1448f461d2dcSChristoph Hellwig proc_put_long(&buffer, &left, bit_b, false);
1449f461d2dcSChristoph Hellwig }
1450f461d2dcSChristoph Hellwig
1451f461d2dcSChristoph Hellwig first = 0; bit_b++;
1452f461d2dcSChristoph Hellwig }
1453f461d2dcSChristoph Hellwig proc_put_char(&buffer, &left, '\n');
1454f461d2dcSChristoph Hellwig }
1455f461d2dcSChristoph Hellwig
14569a52c5f3SJiapeng Chong if (!err) {
1457f461d2dcSChristoph Hellwig if (write) {
1458f461d2dcSChristoph Hellwig if (*ppos)
1459f461d2dcSChristoph Hellwig bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
1460f461d2dcSChristoph Hellwig else
1461f461d2dcSChristoph Hellwig bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
1462f461d2dcSChristoph Hellwig }
1463f461d2dcSChristoph Hellwig *lenp -= left;
1464f461d2dcSChristoph Hellwig *ppos += *lenp;
146532927393SChristoph Hellwig }
146632927393SChristoph Hellwig
146732927393SChristoph Hellwig bitmap_free(tmp_bitmap);
1468f461d2dcSChristoph Hellwig return err;
146932927393SChristoph Hellwig }
147032927393SChristoph Hellwig
1471f461d2dcSChristoph Hellwig #else /* CONFIG_PROC_SYSCTL */
1472f461d2dcSChristoph Hellwig
proc_dostring(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1473f461d2dcSChristoph Hellwig int proc_dostring(const struct ctl_table *table, int write,
1474f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
147532927393SChristoph Hellwig {
1476f461d2dcSChristoph Hellwig return -ENOSYS;
1477f461d2dcSChristoph Hellwig }
1478f461d2dcSChristoph Hellwig
proc_dobool(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1479f461d2dcSChristoph Hellwig int proc_dobool(const struct ctl_table *table, int write,
1480f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1481f461d2dcSChristoph Hellwig {
1482f461d2dcSChristoph Hellwig return -ENOSYS;
1483f461d2dcSChristoph Hellwig }
1484f461d2dcSChristoph Hellwig
proc_dointvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1485f461d2dcSChristoph Hellwig int proc_dointvec(const struct ctl_table *table, int write,
1486f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1487f461d2dcSChristoph Hellwig {
1488f461d2dcSChristoph Hellwig return -ENOSYS;
1489f461d2dcSChristoph Hellwig }
1490f461d2dcSChristoph Hellwig
proc_douintvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1491f461d2dcSChristoph Hellwig int proc_douintvec(const struct ctl_table *table, int write,
1492f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1493f461d2dcSChristoph Hellwig {
1494f461d2dcSChristoph Hellwig return -ENOSYS;
149578eb4ea2SJoel Granados }
149632927393SChristoph Hellwig
proc_dointvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1497f461d2dcSChristoph Hellwig int proc_dointvec_minmax(const struct ctl_table *table, int write,
1498f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1499f461d2dcSChristoph Hellwig {
1500f461d2dcSChristoph Hellwig return -ENOSYS;
150178eb4ea2SJoel Granados }
1502a2071573SJia He
proc_douintvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1503a2071573SJia He int proc_douintvec_minmax(const struct ctl_table *table, int write,
1504a2071573SJia He void *buffer, size_t *lenp, loff_t *ppos)
1505a2071573SJia He {
1506a2071573SJia He return -ENOSYS;
150778eb4ea2SJoel Granados }
150832927393SChristoph Hellwig
proc_dou8vec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1509f461d2dcSChristoph Hellwig int proc_dou8vec_minmax(const struct ctl_table *table, int write,
1510f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1511f461d2dcSChristoph Hellwig {
1512f461d2dcSChristoph Hellwig return -ENOSYS;
151378eb4ea2SJoel Granados }
151432927393SChristoph Hellwig
proc_dointvec_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1515f461d2dcSChristoph Hellwig int proc_dointvec_jiffies(const struct ctl_table *table, int write,
1516f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1517f461d2dcSChristoph Hellwig {
1518f461d2dcSChristoph Hellwig return -ENOSYS;
151978eb4ea2SJoel Granados }
152032927393SChristoph Hellwig
proc_dointvec_ms_jiffies_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1521f461d2dcSChristoph Hellwig int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1522f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1523f461d2dcSChristoph Hellwig {
1524f461d2dcSChristoph Hellwig return -ENOSYS;
152578eb4ea2SJoel Granados }
152632927393SChristoph Hellwig
proc_dointvec_userhz_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1527f461d2dcSChristoph Hellwig int proc_dointvec_userhz_jiffies(const struct ctl_table *table, int write,
1528f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1529f461d2dcSChristoph Hellwig {
1530f461d2dcSChristoph Hellwig return -ENOSYS;
153178eb4ea2SJoel Granados }
1532cb944413SEric Dumazet
proc_dointvec_ms_jiffies(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1533cb944413SEric Dumazet int proc_dointvec_ms_jiffies(const struct ctl_table *table, int write,
1534cb944413SEric Dumazet void *buffer, size_t *lenp, loff_t *ppos)
1535cb944413SEric Dumazet {
1536cb944413SEric Dumazet return -ENOSYS;
153778eb4ea2SJoel Granados }
153832927393SChristoph Hellwig
proc_doulongvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1539f461d2dcSChristoph Hellwig int proc_doulongvec_minmax(const struct ctl_table *table, int write,
1540f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1541f461d2dcSChristoph Hellwig {
1542f461d2dcSChristoph Hellwig return -ENOSYS;
154378eb4ea2SJoel Granados }
1544c381d02bSYuwei Wang
proc_doulongvec_ms_jiffies_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1545c381d02bSYuwei Wang int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int write,
1546c381d02bSYuwei Wang void *buffer, size_t *lenp, loff_t *ppos)
1547c381d02bSYuwei Wang {
1548c381d02bSYuwei Wang return -ENOSYS;
154978eb4ea2SJoel Granados }
155032927393SChristoph Hellwig
proc_do_large_bitmap(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1551f461d2dcSChristoph Hellwig int proc_do_large_bitmap(const struct ctl_table *table, int write,
1552f461d2dcSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
1553f461d2dcSChristoph Hellwig {
1554f461d2dcSChristoph Hellwig return -ENOSYS;
155578eb4ea2SJoel Granados }
155632927393SChristoph Hellwig
1557f461d2dcSChristoph Hellwig #endif /* CONFIG_PROC_SYSCTL */
1558f461d2dcSChristoph Hellwig
1559f461d2dcSChristoph Hellwig #if defined(CONFIG_SYSCTL)
proc_do_static_key(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1560f461d2dcSChristoph Hellwig int proc_do_static_key(const struct ctl_table *table, int write,
156178eb4ea2SJoel Granados void *buffer, size_t *lenp, loff_t *ppos)
156232927393SChristoph Hellwig {
1563f461d2dcSChristoph Hellwig struct static_key *key = (struct static_key *)table->data;
1564f461d2dcSChristoph Hellwig static DEFINE_MUTEX(static_key_mutex);
1565f461d2dcSChristoph Hellwig int val, ret;
1566f461d2dcSChristoph Hellwig struct ctl_table tmp = {
156778eb4ea2SJoel Granados .data = &val,
156832927393SChristoph Hellwig .maxlen = sizeof(val),
1569f461d2dcSChristoph Hellwig .mode = table->mode,
1570f461d2dcSChristoph Hellwig .extra1 = SYSCTL_ZERO,
1571f461d2dcSChristoph Hellwig .extra2 = SYSCTL_ONE,
1572f461d2dcSChristoph Hellwig };
157378eb4ea2SJoel Granados
157432927393SChristoph Hellwig if (write && !capable(CAP_SYS_ADMIN))
1575f461d2dcSChristoph Hellwig return -EPERM;
1576f461d2dcSChristoph Hellwig
1577f461d2dcSChristoph Hellwig mutex_lock(&static_key_mutex);
1578f461d2dcSChristoph Hellwig val = static_key_enabled(key);
1579f461d2dcSChristoph Hellwig ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1580f461d2dcSChristoph Hellwig if (write && !ret) {
1581f461d2dcSChristoph Hellwig if (val)
158278eb4ea2SJoel Granados static_key_enable(key);
158332927393SChristoph Hellwig else
1584f461d2dcSChristoph Hellwig static_key_disable(key);
1585f461d2dcSChristoph Hellwig }
1586f461d2dcSChristoph Hellwig mutex_unlock(&static_key_mutex);
1587f461d2dcSChristoph Hellwig return ret;
1588f461d2dcSChristoph Hellwig }
1589f461d2dcSChristoph Hellwig
1590f461d2dcSChristoph Hellwig static const struct ctl_table kern_table[] = {
1591f461d2dcSChristoph Hellwig {
1592f461d2dcSChristoph Hellwig .procname = "panic",
1593f461d2dcSChristoph Hellwig .data = &panic_timeout,
1594f461d2dcSChristoph Hellwig .maxlen = sizeof(int),
1595f461d2dcSChristoph Hellwig .mode = 0644,
1596f461d2dcSChristoph Hellwig .proc_handler = proc_dointvec,
1597f461d2dcSChristoph Hellwig },
1598f461d2dcSChristoph Hellwig #ifdef CONFIG_PROC_SYSCTL
1599f461d2dcSChristoph Hellwig {
1600f461d2dcSChristoph Hellwig .procname = "tainted",
1601f461d2dcSChristoph Hellwig .maxlen = sizeof(long),
1602f461d2dcSChristoph Hellwig .mode = 0644,
1603f461d2dcSChristoph Hellwig .proc_handler = proc_taint,
1604f461d2dcSChristoph Hellwig },
1605f461d2dcSChristoph Hellwig {
1606f461d2dcSChristoph Hellwig .procname = "sysctl_writes_strict",
1607f461d2dcSChristoph Hellwig .data = &sysctl_writes_strict,
1608f461d2dcSChristoph Hellwig .maxlen = sizeof(int),
1609f461d2dcSChristoph Hellwig .mode = 0644,
1610f461d2dcSChristoph Hellwig .proc_handler = proc_dointvec_minmax,
1611f461d2dcSChristoph Hellwig .extra1 = SYSCTL_NEG_ONE,
1612*1751f872SJoel Granados .extra2 = SYSCTL_ONE,
16131799e35dSIngo Molnar },
16141da177e4SLinus Torvalds #endif
16151da177e4SLinus Torvalds {
16161da177e4SLinus Torvalds .procname = "print-fatal-signals",
16171da177e4SLinus Torvalds .data = &print_fatal_signals,
16186d456111SEric W. Biederman .maxlen = sizeof(int),
16191da177e4SLinus Torvalds .mode = 0644,
162034f5a398STheodore Ts'o .proc_handler = proc_dointvec,
16211da177e4SLinus Torvalds },
16221da177e4SLinus Torvalds #ifdef CONFIG_SPARC
162325ddbb18SAndi Kleen {
162434f5a398STheodore Ts'o .procname = "reboot-cmd",
16256d456111SEric W. Biederman .data = reboot_command,
16261da177e4SLinus Torvalds .maxlen = 256,
1627f4aacea2SKees Cook .mode = 0644,
1628f4aacea2SKees Cook .proc_handler = proc_dostring,
1629f4aacea2SKees Cook },
1630f4aacea2SKees Cook {
1631f4aacea2SKees Cook .procname = "stop-a",
1632f4aacea2SKees Cook .data = &stop_a_enabled,
163378e36f3bSXiaoming Ni .maxlen = sizeof (int),
1634eec4844fSMatteo Croce .mode = 0644,
1635f4aacea2SKees Cook .proc_handler = proc_dointvec,
163634f5a398STheodore Ts'o },
163745807a1dSIngo Molnar {
163845807a1dSIngo Molnar .procname = "scons-poweroff",
163945807a1dSIngo Molnar .data = &scons_pwroff,
164045807a1dSIngo Molnar .maxlen = sizeof (int),
164145807a1dSIngo Molnar .mode = 0644,
16426d456111SEric W. Biederman .proc_handler = proc_dointvec,
164345807a1dSIngo Molnar },
164472c57ed5SDavid S. Miller #endif
16451da177e4SLinus Torvalds #ifdef CONFIG_SPARC64
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds .procname = "tsb-ratio",
16481da177e4SLinus Torvalds .data = &sysctl_tsb_ratio,
16491da177e4SLinus Torvalds .maxlen = sizeof (int),
16506d456111SEric W. Biederman .mode = 0644,
16511da177e4SLinus Torvalds .proc_handler = proc_dointvec,
16521da177e4SLinus Torvalds },
16531da177e4SLinus Torvalds #endif
16541da177e4SLinus Torvalds #ifdef CONFIG_PARISC
16551da177e4SLinus Torvalds {
16561da177e4SLinus Torvalds .procname = "soft-power",
16576d456111SEric W. Biederman .data = &pwrsw_enabled,
16581da177e4SLinus Torvalds .maxlen = sizeof (int),
16591da177e4SLinus Torvalds .mode = 0644,
16601da177e4SLinus Torvalds .proc_handler = proc_dointvec,
16611da177e4SLinus Torvalds },
16621da177e4SLinus Torvalds #endif
16631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
16646d456111SEric W. Biederman {
16651da177e4SLinus Torvalds .procname = "unaligned-trap",
16661da177e4SLinus Torvalds .data = &unaligned_enabled,
16670871420fSDavid S. Miller .maxlen = sizeof (int),
16680871420fSDavid S. Miller .mode = 0644,
16690871420fSDavid S. Miller .proc_handler = proc_dointvec,
16700871420fSDavid S. Miller },
16710871420fSDavid S. Miller #endif
16720871420fSDavid S. Miller #ifdef CONFIG_STACK_TRACER
16736d456111SEric W. Biederman {
16740871420fSDavid S. Miller .procname = "stack_tracer_enabled",
16750871420fSDavid S. Miller .data = &stack_tracer_enabled,
1676b67114dbSHelge Deller .maxlen = sizeof(int),
16771da177e4SLinus Torvalds .mode = 0644,
16781da177e4SLinus Torvalds .proc_handler = stack_trace_sysctl,
16791da177e4SLinus Torvalds },
16801da177e4SLinus Torvalds #endif
16811da177e4SLinus Torvalds #ifdef CONFIG_TRACING
16826d456111SEric W. Biederman {
16831da177e4SLinus Torvalds .procname = "ftrace_dump_on_oops",
1684bf14e3b9SVineet Gupta .data = &ftrace_dump_on_oops,
1685bf14e3b9SVineet Gupta .maxlen = MAX_TRACER_SIZE,
16861da177e4SLinus Torvalds .mode = 0644,
16871da177e4SLinus Torvalds .proc_handler = proc_dostring,
16881da177e4SLinus Torvalds },
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds .procname = "traceoff_on_warning",
16916d456111SEric W. Biederman .data = &__disable_trace_on_warning,
16921da177e4SLinus Torvalds .maxlen = sizeof(__disable_trace_on_warning),
16931da177e4SLinus Torvalds .mode = 0644,
1694f38f1d2aSSteven Rostedt .proc_handler = proc_dointvec,
1695f38f1d2aSSteven Rostedt },
1696f38f1d2aSSteven Rostedt {
1697f38f1d2aSSteven Rostedt .procname = "tracepoint_printk",
1698f38f1d2aSSteven Rostedt .data = &tracepoint_printk,
1699f38f1d2aSSteven Rostedt .maxlen = sizeof(tracepoint_printk),
17006d456111SEric W. Biederman .mode = 0644,
1701f38f1d2aSSteven Rostedt .proc_handler = tracepoint_printk_sysctl,
1702f38f1d2aSSteven Rostedt },
1703944ac425SSteven Rostedt #endif
1704944ac425SSteven Rostedt #ifdef CONFIG_MODULES
17053299b4ddSPeter Zijlstra {
1706944ac425SSteven Rostedt .procname = "modprobe",
170719f0423fSHuang Yiwei .data = &modprobe_path,
1708944ac425SSteven Rostedt .maxlen = KMOD_PATH_LEN,
170919f0423fSHuang Yiwei .mode = 0644,
1710944ac425SSteven Rostedt .proc_handler = proc_dostring,
1711de7edd31SSteven Rostedt (Red Hat) },
1712de7edd31SSteven Rostedt (Red Hat) {
1713de7edd31SSteven Rostedt (Red Hat) .procname = "modules_disabled",
1714de7edd31SSteven Rostedt (Red Hat) .data = &modules_disabled,
1715de7edd31SSteven Rostedt (Red Hat) .maxlen = sizeof(int),
1716de7edd31SSteven Rostedt (Red Hat) .mode = 0644,
1717de7edd31SSteven Rostedt (Red Hat) /* only handle a transition from default "0" to "1" */
17180daa2302SSteven Rostedt (Red Hat) .proc_handler = proc_dointvec_minmax,
17190daa2302SSteven Rostedt (Red Hat) .extra1 = SYSCTL_ONE,
17200daa2302SSteven Rostedt (Red Hat) .extra2 = SYSCTL_ONE,
17210daa2302SSteven Rostedt (Red Hat) },
17220daa2302SSteven Rostedt (Red Hat) #endif
172342391745SSteven Rostedt (Red Hat) #ifdef CONFIG_UEVENT_HELPER
17240daa2302SSteven Rostedt (Red Hat) {
1725944ac425SSteven Rostedt .procname = "hotplug",
1726a1ef5adbSJohannes Berg .data = &uevent_helper,
17271da177e4SLinus Torvalds .maxlen = UEVENT_HELPER_PATH_LEN,
17281da177e4SLinus Torvalds .mode = 0644,
17291da177e4SLinus Torvalds .proc_handler = proc_dostring,
17301da177e4SLinus Torvalds },
17311da177e4SLinus Torvalds #endif
17326d456111SEric W. Biederman #ifdef CONFIG_MAGIC_SYSRQ
17331da177e4SLinus Torvalds {
17343d43321bSKees Cook .procname = "sysrq",
17353d43321bSKees Cook .data = NULL,
17363d43321bSKees Cook .maxlen = sizeof (int),
17373d43321bSKees Cook .mode = 0644,
17383d43321bSKees Cook .proc_handler = sysrq_sysctl_handler,
17393d43321bSKees Cook },
17406d456111SEric W. Biederman #endif
1741eec4844fSMatteo Croce #ifdef CONFIG_PROC_SYSCTL
1742eec4844fSMatteo Croce {
17433d43321bSKees Cook .procname = "cad_pid",
17441da177e4SLinus Torvalds .data = NULL,
174586d56134SMichael Marineau .maxlen = sizeof (int),
17461da177e4SLinus Torvalds .mode = 0600,
17471da177e4SLinus Torvalds .proc_handler = proc_do_cad_pid,
1748312c004dSKay Sievers },
1749312c004dSKay Sievers #endif
17501da177e4SLinus Torvalds {
17516d456111SEric W. Biederman .procname = "threads-max",
17521da177e4SLinus Torvalds .data = NULL,
175386d56134SMichael Marineau .maxlen = sizeof(int),
17541da177e4SLinus Torvalds .mode = 0644,
17551da177e4SLinus Torvalds .proc_handler = sysctl_max_threads,
17561da177e4SLinus Torvalds },
1757eaee4172SDmitry Safonov {
17581da177e4SLinus Torvalds .procname = "overflowuid",
17591da177e4SLinus Torvalds .data = &overflowuid,
176097f5f0cdSDmitry Torokhov .maxlen = sizeof(int),
17611da177e4SLinus Torvalds .mode = 0644,
17621da177e4SLinus Torvalds .proc_handler = proc_dointvec_minmax,
1763d6f8ff73SRandy Dunlap .extra1 = SYSCTL_ZERO,
17641da177e4SLinus Torvalds .extra2 = SYSCTL_MAXOLDUID,
17651da177e4SLinus Torvalds },
17669ec52099SCedric Le Goater {
17671da177e4SLinus Torvalds .procname = "overflowgid",
17681da177e4SLinus Torvalds .data = &overflowgid,
17696d456111SEric W. Biederman .maxlen = sizeof(int),
17701da177e4SLinus Torvalds .mode = 0644,
1771d6f8ff73SRandy Dunlap .proc_handler = proc_dointvec_minmax,
17721da177e4SLinus Torvalds .extra1 = SYSCTL_ZERO,
17731da177e4SLinus Torvalds .extra2 = SYSCTL_MAXOLDUID,
177416db3d3fSHeinrich Schuchardt },
17751da177e4SLinus Torvalds {
17761da177e4SLinus Torvalds .procname = "panic_on_oops",
177716db3d3fSHeinrich Schuchardt .data = &panic_on_oops,
17781da177e4SLinus Torvalds .maxlen = sizeof(int),
17791da177e4SLinus Torvalds .mode = 0644,
17801da177e4SLinus Torvalds .proc_handler = proc_dointvec,
17811da177e4SLinus Torvalds },
17821da177e4SLinus Torvalds {
17831da177e4SLinus Torvalds .procname = "panic_print",
17846d456111SEric W. Biederman .data = &panic_print,
17852452dcb9SXiaoming Ni .maxlen = sizeof(unsigned long),
178654771613SLuis Chamberlain .mode = 0644,
17871da177e4SLinus Torvalds .proc_handler = proc_doulongvec_minmax,
17881da177e4SLinus Torvalds },
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds .procname = "ngroups_max",
17911da177e4SLinus Torvalds .data = (void *)&ngroups_max,
17921da177e4SLinus Torvalds .maxlen = sizeof (int),
17936d456111SEric W. Biederman .mode = 0444,
17942452dcb9SXiaoming Ni .proc_handler = proc_dointvec,
179554771613SLuis Chamberlain },
17961da177e4SLinus Torvalds {
17971da177e4SLinus Torvalds .procname = "cap_last_cap",
17981da177e4SLinus Torvalds .data = (void *)&cap_last_cap,
17991da177e4SLinus Torvalds .maxlen = sizeof(int),
18001da177e4SLinus Torvalds .mode = 0444,
18011da177e4SLinus Torvalds .proc_handler = proc_dointvec,
18026d456111SEric W. Biederman },
18031da177e4SLinus Torvalds #if (defined(CONFIG_X86_32) || defined(CONFIG_PARISC)) && \
180481c9d43fSFeng Tang defined(CONFIG_DEBUG_STACKOVERFLOW)
180581c9d43fSFeng Tang {
180681c9d43fSFeng Tang .procname = "panic_on_stackoverflow",
180781c9d43fSFeng Tang .data = &sysctl_panic_on_stackoverflow,
180881c9d43fSFeng Tang .maxlen = sizeof(int),
180981c9d43fSFeng Tang .mode = 0644,
181081c9d43fSFeng Tang .proc_handler = proc_dointvec,
1811eaf06b24SDan Rosenberg },
18121da177e4SLinus Torvalds #endif
1813f628867dSStephen Kitt #if defined(CONFIG_MMU)
18141da177e4SLinus Torvalds {
18151da177e4SLinus Torvalds .procname = "randomize_va_space",
18166d456111SEric W. Biederman .data = &randomize_va_space,
18171da177e4SLinus Torvalds .maxlen = sizeof(int),
181873efc039SDan Ballard .mode = 0644,
181973efc039SDan Ballard .proc_handler = proc_dointvec,
182073efc039SDan Ballard },
182173efc039SDan Ballard #endif
182273efc039SDan Ballard #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
182373efc039SDan Ballard {
182473efc039SDan Ballard .procname = "ignore-unaligned-usertrap",
18255dc30558SDon Zickus .data = &no_unaligned_warning,
18265dc30558SDon Zickus .maxlen = sizeof (int),
18275dc30558SDon Zickus .mode = 0644,
18285dc30558SDon Zickus .proc_handler = proc_dointvec,
18295dc30558SDon Zickus },
18305dc30558SDon Zickus #endif
18315dc30558SDon Zickus #ifdef CONFIG_RT_MUTEXES
18325dc30558SDon Zickus {
1833504d7cf1SDon Zickus .procname = "max_lock_depth",
1834b6522fa4SXiaoming Ni .data = &max_lock_depth,
1835b6522fa4SXiaoming Ni .maxlen = sizeof(int),
1836b6522fa4SXiaoming Ni .mode = 0644,
1837b6522fa4SXiaoming Ni .proc_handler = proc_dointvec,
1838b6522fa4SXiaoming Ni },
1839b6522fa4SXiaoming Ni #endif
1840b6522fa4SXiaoming Ni {
1841b6522fa4SXiaoming Ni .procname = "panic_on_warn",
1842b6522fa4SXiaoming Ni .data = &panic_on_warn,
1843b6522fa4SXiaoming Ni .maxlen = sizeof(int),
1844b6522fa4SXiaoming Ni .mode = 0644,
18451da177e4SLinus Torvalds .proc_handler = proc_dointvec_minmax,
18461da177e4SLinus Torvalds .extra1 = SYSCTL_ZERO,
18478da5addaSDon Zickus .extra2 = SYSCTL_ONE,
18488da5addaSDon Zickus },
18498da5addaSDon Zickus #ifdef CONFIG_TREE_RCU
18508da5addaSDon Zickus {
18516d456111SEric W. Biederman .procname = "panic_on_rcu_stall",
18528da5addaSDon Zickus .data = &sysctl_panic_on_rcu_stall,
18538da5addaSDon Zickus .maxlen = sizeof(sysctl_panic_on_rcu_stall),
18545211a242SKurt Garloff .mode = 0644,
18555211a242SKurt Garloff .proc_handler = proc_dointvec_minmax,
18565211a242SKurt Garloff .extra1 = SYSCTL_ZERO,
18575211a242SKurt Garloff .extra2 = SYSCTL_ONE,
18586d456111SEric W. Biederman },
18595211a242SKurt Garloff {
18605211a242SKurt Garloff .procname = "max_rcu_stall_to_panic",
18611da177e4SLinus Torvalds .data = &sysctl_max_rcu_stall_to_panic,
18621da177e4SLinus Torvalds .maxlen = sizeof(sysctl_max_rcu_stall_to_panic),
18631da177e4SLinus Torvalds .mode = 0644,
18641da177e4SLinus Torvalds .proc_handler = proc_dointvec_minmax,
18656d456111SEric W. Biederman .extra1 = SYSCTL_ONE,
18661da177e4SLinus Torvalds .extra2 = SYSCTL_INT_MAX,
18670741f4d2SChuck Ebbert },
18685031296cSH. Peter Anvin #endif
18695031296cSH. Peter Anvin };
18705031296cSH. Peter Anvin
sysctl_init_bases(void)18715031296cSH. Peter Anvin int __init sysctl_init_bases(void)
18726d456111SEric W. Biederman {
18735031296cSH. Peter Anvin register_sysctl_init("kernel", kern_table);
18745031296cSH. Peter Anvin
18756e7c4025SIngo Molnar return 0;
18766e7c4025SIngo Molnar }
18776e7c4025SIngo Molnar #endif /* CONFIG_SYSCTL */
18786e7c4025SIngo Molnar /*
18796d456111SEric W. Biederman * No sense putting this after each symbol definition, twice,
18806e7c4025SIngo Molnar * exception granted :-)
18811da177e4SLinus Torvalds */
18827a9166e3SLuke Yang EXPORT_SYMBOL(proc_dobool);
18831da177e4SLinus Torvalds EXPORT_SYMBOL(proc_dointvec);
18841da177e4SLinus Torvalds EXPORT_SYMBOL(proc_douintvec);
18851da177e4SLinus Torvalds EXPORT_SYMBOL(proc_dointvec_jiffies);
18861da177e4SLinus Torvalds EXPORT_SYMBOL(proc_dointvec_minmax);
18871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
18886d456111SEric W. Biederman EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
18891da177e4SLinus Torvalds EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
18907a9166e3SLuke Yang EXPORT_SYMBOL(proc_dostring);
1891673d5b43SLen Brown EXPORT_SYMBOL(proc_doulongvec_minmax);
1892c255d844SPavel Machek EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
1893c255d844SPavel Machek EXPORT_SYMBOL(proc_do_large_bitmap);
189477afcf78SPavel Machek