blob: c322bb629d105424782d20543964fbbc7842f4fd [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * sysctl.c: General linux system control interface
4 *
5 * Begun 24 March 1995, Stephen Tweedie
6 * Added /proc support, Dec 1995
7 * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
8 * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
9 * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
10 * Dynamic registration fixes, Stephen Tweedie.
11 * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
12 * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
13 * Horn.
14 * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
15 * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
16 * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
17 * Wendling.
18 * The list_for_each() macro wasn't appropriate for the sysctl loop.
19 * Removed it and replaced it with older style, 03/23/00, Bill Wendling
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/mm.h>
24#include <linux/swap.h>
25#include <linux/slab.h>
26#include <linux/sysctl.h>
Akinobu Mita5a04cca2012-03-28 14:42:50 -070027#include <linux/bitmap.h>
Dave Youngd33ed522010-03-10 15:23:59 -080028#include <linux/signal.h>
Andy Shevchenkof39650d2021-06-30 18:54:59 -070029#include <linux/panic.h>
Dan Rosenberg455cd5a2011-01-12 16:59:41 -080030#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/proc_fs.h>
Andrew Morgan72c2d582007-10-18 03:05:59 -070032#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ctype.h>
Steven Rostedtfd4b6162012-07-30 14:42:48 -070034#include <linux/kmemleak.h>
Jakub Kicinskib6459412021-12-28 16:49:13 -080035#include <linux/filter.h>
Adrian Bunk62239ac2007-07-17 04:03:45 -070036#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/kernel.h>
Kay Sievers0296b222005-11-11 05:33:52 +010039#include <linux/kobject.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030040#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysrq.h>
42#include <linux/highuid.h>
43#include <linux/writeback.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020044#include <linux/ratelimit.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070045#include <linux/compaction.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/initrd.h>
David Howells0b77f5b2008-04-29 01:01:32 -070048#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/times.h>
50#include <linux/limits.h>
51#include <linux/dcache.h>
52#include <linux/syscalls.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070053#include <linux/vmstat.h>
Pavel Machekc255d842006-02-20 18:27:58 -080054#include <linux/nfs_fs.h>
55#include <linux/acpi.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070056#include <linux/reboot.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020057#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058#include <linux/perf_event.h>
David Rientjes8e4228e2010-08-09 17:18:56 -070059#include <linux/oom.h>
Eric Paris17f60a72011-04-01 17:07:50 -040060#include <linux/kmod.h>
Dan Ballard73efc032011-10-31 17:11:20 -070061#include <linux/capability.h>
Al Viro40401532012-02-13 03:58:52 +000062#include <linux/binfmts.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060063#include <linux/sched/sysctl.h>
Kees Cook79847542014-01-23 15:55:59 -080064#include <linux/kexec.h>
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070065#include <linux/bpf.h>
Eric W. Biedermand2921682016-09-28 00:27:17 -050066#include <linux/mount.h>
Peter Xucefdca02019-05-13 17:16:41 -070067#include <linux/userfaultfd_k.h>
Christoph Hellwig2374c092020-04-24 08:43:36 +020068#include <linux/latencytop.h>
69#include <linux/pid.h>
Peter Zijlstra0cd7c742021-05-10 14:01:00 +020070#include <linux/delayacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Christian Brauner7f2923c2019-03-07 16:29:40 -080072#include "../lib/kstrtox.h"
73
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080074#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/processor.h>
76
Andi Kleen29cbc782006-09-30 01:47:55 +020077#ifdef CONFIG_X86
78#include <asm/nmi.h>
Chuck Ebbert0741f4d2006-12-07 02:14:11 +010079#include <asm/stacktrace.h>
Ingo Molnar6e7c4022008-01-30 13:30:05 +010080#include <asm/io.h>
Andi Kleen29cbc782006-09-30 01:47:55 +020081#endif
David Howellsd550bbd2012-03-28 18:30:03 +010082#ifdef CONFIG_SPARC
83#include <asm/setup.h>
84#endif
Dave Youngc55b7c32010-03-10 15:24:08 -080085#ifdef CONFIG_BSD_PROCESS_ACCT
86#include <linux/acct.h>
87#endif
Dave Young4f0e0562010-03-10 15:24:09 -080088#ifdef CONFIG_RT_MUTEXES
89#include <linux/rtmutex.h>
90#endif
Dave Young2edf5e42010-03-10 15:24:10 -080091#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_LOCK_STAT)
92#include <linux/lockdep.h>
93#endif
Don Zickus504d7cf2010-02-12 17:19:19 -050094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#if defined(CONFIG_SYSCTL)
96
Ravikiran G Thirumalaic4f3b632007-10-16 23:26:09 -070097/* Constants used for minimum and maximum */
Ravikiran G Thirumalaic4f3b632007-10-16 23:26:09 -070098
Dave Youngaf913222009-09-22 16:43:33 -070099#ifdef CONFIG_PRINTK
Xiaoming Nid73840e2022-01-21 22:11:14 -0800100static const int ten_thousand = 10000;
Dave Youngaf913222009-09-22 16:43:33 -0700101#endif
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300102#ifdef CONFIG_PERF_EVENTS
Xiaoming Nid73840e2022-01-21 22:11:14 -0800103static const int six_hundred_forty_kb = 640 * 1024;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300104#endif
Ravikiran G Thirumalaic4f3b632007-10-16 23:26:09 -0700105
Andrea Righi9e4a5bd2009-04-30 15:08:57 -0700106/* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
Xiaoming Nid73840e2022-01-21 22:11:14 -0800107static const unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
Andrea Righi9e4a5bd2009-04-30 15:08:57 -0700108
Stephen Kittf6288672022-01-21 22:11:09 -0800109static const int ngroups_max = NGROUPS_MAX;
Dan Ballard73efc032011-10-31 17:11:20 -0700110static const int cap_last_cap = CAP_LAST_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Randy Dunlapd6f8ff72006-10-19 23:28:34 -0700112#ifdef CONFIG_PROC_SYSCTL
Kees Cookf4aacea2014-06-06 14:37:19 -0700113
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700114/**
115 * enum sysctl_writes_mode - supported sysctl write modes
116 *
117 * @SYSCTL_WRITES_LEGACY: each write syscall must fully contain the sysctl value
Weitao Hou65f50f22019-07-16 16:26:54 -0700118 * to be written, and multiple writes on the same sysctl file descriptor
119 * will rewrite the sysctl value, regardless of file position. No warning
120 * is issued when the initial position is not 0.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700121 * @SYSCTL_WRITES_WARN: same as above but warn when the initial file position is
Weitao Hou65f50f22019-07-16 16:26:54 -0700122 * not 0.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700123 * @SYSCTL_WRITES_STRICT: writes to numeric sysctl entries must always be at
Weitao Hou65f50f22019-07-16 16:26:54 -0700124 * file position 0 and the value must be fully contained in the buffer
125 * sent to the write syscall. If dealing with strings respect the file
126 * position, but restrict this to the max length of the buffer, anything
127 * passed the max length will be ignored. Multiple writes will append
128 * to the buffer.
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700129 *
130 * These write modes control how current file position affects the behavior of
131 * updating sysctl values through the proc interface on each write.
132 */
133enum sysctl_writes_mode {
134 SYSCTL_WRITES_LEGACY = -1,
135 SYSCTL_WRITES_WARN = 0,
136 SYSCTL_WRITES_STRICT = 1,
137};
Kees Cookf4aacea2014-06-06 14:37:19 -0700138
Luis R. Rodrigueza19ac332017-07-12 14:33:30 -0700139static enum sysctl_writes_mode sysctl_writes_strict = SYSCTL_WRITES_STRICT;
Christoph Hellwigf461d2d2020-04-24 08:43:37 +0200140#endif /* CONFIG_PROC_SYSCTL */
Luis R. Rodriguezceb18132018-03-10 06:14:51 -0800141
Alexandre Ghiti67f39772019-09-23 15:38:47 -0700142#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
143 defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int sysctl_legacy_va_layout;
145#endif
146
Mel Gorman5e771902010-05-24 14:32:31 -0700147#ifdef CONFIG_COMPACTION
Xiaoming Ni2452dcb2022-01-21 22:11:19 -0800148/* min_extfrag_threshold is SYSCTL_ZERO */;
Xiaoming Nid73840e2022-01-21 22:11:14 -0800149static const int max_extfrag_threshold = 1000;
Mel Gorman5e771902010-05-24 14:32:31 -0700150#endif
151
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700152#endif /* CONFIG_SYSCTL */
153
Arnd Bergmann5447e8e2020-05-05 16:07:12 +0200154#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_SYSCTL)
Song Liud46edd62020-04-30 00:15:04 -0700155static int bpf_stats_handler(struct ctl_table *table, int write,
Tobias Klauser7787b6f2020-08-24 16:20:47 +0200156 void *buffer, size_t *lenp, loff_t *ppos)
Song Liud46edd62020-04-30 00:15:04 -0700157{
158 struct static_key *key = (struct static_key *)table->data;
159 static int saved_val;
160 int val, ret;
161 struct ctl_table tmp = {
162 .data = &val,
163 .maxlen = sizeof(val),
164 .mode = table->mode,
165 .extra1 = SYSCTL_ZERO,
166 .extra2 = SYSCTL_ONE,
167 };
168
169 if (write && !capable(CAP_SYS_ADMIN))
170 return -EPERM;
171
172 mutex_lock(&bpf_stats_enabled_mutex);
173 val = saved_val;
174 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
175 if (write && !ret && val != saved_val) {
176 if (val)
177 static_key_slow_inc(key);
178 else
179 static_key_slow_dec(key);
180 saved_val = val;
181 }
182 mutex_unlock(&bpf_stats_enabled_mutex);
183 return ret;
184}
Daniel Borkmann08389d82021-05-11 22:35:17 +0200185
186static int bpf_unpriv_handler(struct ctl_table *table, int write,
187 void *buffer, size_t *lenp, loff_t *ppos)
188{
189 int ret, unpriv_enable = *(int *)table->data;
190 bool locked_state = unpriv_enable == 1;
191 struct ctl_table tmp = *table;
192
193 if (write && !capable(CAP_SYS_ADMIN))
194 return -EPERM;
195
196 tmp.data = &unpriv_enable;
197 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
198 if (write && !ret) {
199 if (locked_state && unpriv_enable != 1)
200 return -EPERM;
201 *(int *)table->data = unpriv_enable;
202 }
203 return ret;
204}
205#endif /* CONFIG_BPF_SYSCALL && CONFIG_SYSCTL */
Song Liud46edd62020-04-30 00:15:04 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * /proc/sys support
209 */
210
Eric W. Biedermanb89a8172006-09-27 01:51:04 -0700211#ifdef CONFIG_PROC_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kees Cookf8808302014-06-06 14:37:17 -0700213static int _proc_do_string(char *data, int maxlen, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200214 char *buffer, size_t *lenp, loff_t *ppos)
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700215{
216 size_t len;
Christoph Hellwig32927392020-04-24 08:43:38 +0200217 char c, *p;
Oleg Nesterov8d060872007-02-10 01:46:38 -0800218
219 if (!data || !maxlen || !*lenp) {
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700220 *lenp = 0;
221 return 0;
222 }
Oleg Nesterov8d060872007-02-10 01:46:38 -0800223
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700224 if (write) {
Kees Cookf4aacea2014-06-06 14:37:19 -0700225 if (sysctl_writes_strict == SYSCTL_WRITES_STRICT) {
226 /* Only continue writes not past the end of buffer. */
227 len = strlen(data);
228 if (len > maxlen - 1)
229 len = maxlen - 1;
230
231 if (*ppos > len)
232 return 0;
233 len = *ppos;
234 } else {
235 /* Start writing from beginning of buffer. */
236 len = 0;
237 }
238
Kees Cook2ca9bb42014-06-06 14:37:18 -0700239 *ppos += *lenp;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700240 p = buffer;
Kees Cook2ca9bb42014-06-06 14:37:18 -0700241 while ((p - buffer) < *lenp && len < maxlen - 1) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200242 c = *(p++);
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700243 if (c == 0 || c == '\n')
244 break;
Kees Cook2ca9bb42014-06-06 14:37:18 -0700245 data[len++] = c;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700246 }
Kees Cookf8808302014-06-06 14:37:17 -0700247 data[len] = 0;
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700248 } else {
249 len = strlen(data);
250 if (len > maxlen)
251 len = maxlen;
Oleg Nesterov8d060872007-02-10 01:46:38 -0800252
253 if (*ppos > len) {
254 *lenp = 0;
255 return 0;
256 }
257
258 data += *ppos;
259 len -= *ppos;
260
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700261 if (len > *lenp)
262 len = *lenp;
263 if (len)
Christoph Hellwig32927392020-04-24 08:43:38 +0200264 memcpy(buffer, data, len);
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700265 if (len < *lenp) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200266 buffer[len] = '\n';
Sam Vilainf5dd3d62006-10-02 02:18:04 -0700267 len++;
268 }
269 *lenp = len;
270 *ppos += len;
271 }
272 return 0;
273}
274
Kees Cookf4aacea2014-06-06 14:37:19 -0700275static void warn_sysctl_write(struct ctl_table *table)
276{
277 pr_warn_once("%s wrote to %s when file position was not 0!\n"
278 "This will not be supported in the future. To silence this\n"
279 "warning, set kernel.sysctl_writes_strict = -1\n",
280 current->comm, table->procname);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/**
Randy Dunlap5f733e82018-08-21 22:01:06 -0700284 * proc_first_pos_non_zero_ignore - check if first position is allowed
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700285 * @ppos: file position
286 * @table: the sysctl table
287 *
288 * Returns true if the first position is non-zero and the sysctl_writes_strict
289 * mode indicates this is not allowed for numeric input types. String proc
Randy Dunlap5f733e82018-08-21 22:01:06 -0700290 * handlers can ignore the return value.
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700291 */
292static bool proc_first_pos_non_zero_ignore(loff_t *ppos,
293 struct ctl_table *table)
294{
295 if (!*ppos)
296 return false;
297
298 switch (sysctl_writes_strict) {
299 case SYSCTL_WRITES_STRICT:
300 return true;
301 case SYSCTL_WRITES_WARN:
302 warn_sysctl_write(table);
303 return false;
304 default:
305 return false;
306 }
307}
308
309/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * proc_dostring - read a string sysctl
311 * @table: the sysctl table
312 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * @buffer: the user buffer
314 * @lenp: the size of the user buffer
315 * @ppos: file position
316 *
317 * Reads/writes a string from/to the user buffer. If the kernel
318 * buffer provided is not large enough to hold the string, the
319 * string is truncated. The copied string is %NULL-terminated.
320 * If the string is being read by the user process, it is copied
321 * and a newline '\n' is added. It is truncated if the buffer is
322 * not large enough.
323 *
324 * Returns 0 on success.
325 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700326int proc_dostring(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200327 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700329 if (write)
330 proc_first_pos_non_zero_ignore(ppos, table);
Kees Cookf4aacea2014-06-06 14:37:19 -0700331
Christoph Hellwig32927392020-04-24 08:43:38 +0200332 return _proc_do_string(table->data, table->maxlen, write, buffer, lenp,
333 ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Amerigo Wang00b7c332010-05-05 00:26:45 +0000336static size_t proc_skip_spaces(char **buf)
337{
338 size_t ret;
339 char *tmp = skip_spaces(*buf);
340 ret = tmp - *buf;
341 *buf = tmp;
342 return ret;
343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Octavian Purdila9f977fb2010-05-05 00:26:55 +0000345static void proc_skip_char(char **buf, size_t *size, const char v)
346{
347 while (*size) {
348 if (**buf != v)
349 break;
350 (*size)--;
351 (*buf)++;
352 }
353}
354
Christian Brauner7f2923c2019-03-07 16:29:40 -0800355/**
356 * strtoul_lenient - parse an ASCII formatted integer from a buffer and only
357 * fail on overflow
358 *
359 * @cp: kernel buffer containing the string to parse
360 * @endp: pointer to store the trailing characters
361 * @base: the base to use
362 * @res: where the parsed integer will be stored
363 *
364 * In case of success 0 is returned and @res will contain the parsed integer,
365 * @endp will hold any trailing characters.
366 * This function will fail the parse on overflow. If there wasn't an overflow
367 * the function will defer the decision what characters count as invalid to the
368 * caller.
369 */
370static int strtoul_lenient(const char *cp, char **endp, unsigned int base,
371 unsigned long *res)
372{
373 unsigned long long result;
374 unsigned int rv;
375
376 cp = _parse_integer_fixup_radix(cp, &base);
377 rv = _parse_integer(cp, base, &result);
378 if ((rv & KSTRTOX_OVERFLOW) || (result != (unsigned long)result))
379 return -ERANGE;
380
381 cp += rv;
382
383 if (endp)
384 *endp = (char *)cp;
385
386 *res = (unsigned long)result;
387 return 0;
388}
389
Amerigo Wang00b7c332010-05-05 00:26:45 +0000390#define TMPBUFLEN 22
391/**
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700392 * proc_get_long - reads an ASCII formatted integer from a user buffer
Amerigo Wang00b7c332010-05-05 00:26:45 +0000393 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700394 * @buf: a kernel buffer
395 * @size: size of the kernel buffer
396 * @val: this is where the number will be stored
397 * @neg: set to %TRUE if number is negative
398 * @perm_tr: a vector which contains the allowed trailers
399 * @perm_tr_len: size of the perm_tr vector
400 * @tr: pointer to store the trailer character
Amerigo Wang00b7c332010-05-05 00:26:45 +0000401 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700402 * In case of success %0 is returned and @buf and @size are updated with
403 * the amount of bytes read. If @tr is non-NULL and a trailing
404 * character exists (size is non-zero after returning from this
405 * function), @tr is updated with the trailing character.
Amerigo Wang00b7c332010-05-05 00:26:45 +0000406 */
407static int proc_get_long(char **buf, size_t *size,
408 unsigned long *val, bool *neg,
409 const char *perm_tr, unsigned perm_tr_len, char *tr)
410{
411 int len;
412 char *p, tmp[TMPBUFLEN];
413
414 if (!*size)
415 return -EINVAL;
416
417 len = *size;
418 if (len > TMPBUFLEN - 1)
419 len = TMPBUFLEN - 1;
420
421 memcpy(tmp, *buf, len);
422
423 tmp[len] = 0;
424 p = tmp;
425 if (*p == '-' && *size > 1) {
426 *neg = true;
427 p++;
428 } else
429 *neg = false;
430 if (!isdigit(*p))
431 return -EINVAL;
432
Christian Brauner7f2923c2019-03-07 16:29:40 -0800433 if (strtoul_lenient(p, &p, 0, val))
434 return -EINVAL;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000435
436 len = p - tmp;
437
438 /* We don't know if the next char is whitespace thus we may accept
439 * invalid integers (e.g. 1234...a) or two integers instead of one
440 * (e.g. 123...1). So lets not allow such large numbers. */
441 if (len == TMPBUFLEN - 1)
442 return -EINVAL;
443
444 if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
445 return -EINVAL;
446
447 if (tr && (len < *size))
448 *tr = *p;
449
450 *buf += len;
451 *size -= len;
452
453 return 0;
454}
455
456/**
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700457 * proc_put_long - converts an integer to a decimal ASCII formatted string
Amerigo Wang00b7c332010-05-05 00:26:45 +0000458 *
Randy Dunlap0fc377b2010-05-21 11:29:53 -0700459 * @buf: the user buffer
460 * @size: the size of the user buffer
461 * @val: the integer to be converted
462 * @neg: sign of the number, %TRUE for negative
Amerigo Wang00b7c332010-05-05 00:26:45 +0000463 *
Christoph Hellwig32927392020-04-24 08:43:38 +0200464 * In case of success @buf and @size are updated with the amount of bytes
465 * written.
Amerigo Wang00b7c332010-05-05 00:26:45 +0000466 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200467static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000468{
469 int len;
470 char tmp[TMPBUFLEN], *p = tmp;
471
472 sprintf(p, "%s%lu", neg ? "-" : "", val);
473 len = strlen(tmp);
474 if (len > *size)
475 len = *size;
Christoph Hellwig32927392020-04-24 08:43:38 +0200476 memcpy(*buf, tmp, len);
Amerigo Wang00b7c332010-05-05 00:26:45 +0000477 *size -= len;
478 *buf += len;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000479}
480#undef TMPBUFLEN
481
Christoph Hellwig32927392020-04-24 08:43:38 +0200482static void proc_put_char(void **buf, size_t *size, char c)
Amerigo Wang00b7c332010-05-05 00:26:45 +0000483{
484 if (*size) {
Christoph Hellwig32927392020-04-24 08:43:38 +0200485 char **buffer = (char **)buf;
486 **buffer = c;
487
488 (*size)--;
489 (*buffer)++;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000490 *buf = *buffer;
491 }
Amerigo Wang00b7c332010-05-05 00:26:45 +0000492}
493
Jia Hea2071572021-08-03 12:59:36 +0200494static int do_proc_dobool_conv(bool *negp, unsigned long *lvalp,
495 int *valp,
496 int write, void *data)
497{
498 if (write) {
499 *(bool *)valp = *lvalp;
500 } else {
501 int val = *(bool *)valp;
502
503 *lvalp = (unsigned long)val;
504 *negp = false;
505 }
506 return 0;
507}
508
Amerigo Wang00b7c332010-05-05 00:26:45 +0000509static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int *valp,
511 int write, void *data)
512{
513 if (write) {
Heinrich Schuchardt230633d2015-04-16 12:48:07 -0700514 if (*negp) {
515 if (*lvalp > (unsigned long) INT_MAX + 1)
516 return -EINVAL;
517 *valp = -*lvalp;
518 } else {
519 if (*lvalp > (unsigned long) INT_MAX)
520 return -EINVAL;
521 *valp = *lvalp;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else {
524 int val = *valp;
525 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +0000526 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -0700527 *lvalp = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +0000529 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *lvalp = (unsigned long)val;
531 }
532 }
533 return 0;
534}
535
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700536static int do_proc_douintvec_conv(unsigned long *lvalp,
537 unsigned int *valp,
538 int write, void *data)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700539{
540 if (write) {
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700541 if (*lvalp > UINT_MAX)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700542 return -EINVAL;
543 *valp = *lvalp;
544 } else {
545 unsigned int val = *valp;
546 *lvalp = (unsigned long)val;
547 }
548 return 0;
549}
550
Amerigo Wang00b7c332010-05-05 00:26:45 +0000551static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
552
Eric W. Biedermand8217f02007-10-18 03:05:22 -0700553static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200554 int write, void *buffer,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700555 size_t *lenp, loff_t *ppos,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000556 int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int write, void *data),
558 void *data)
559{
Amerigo Wang00b7c332010-05-05 00:26:45 +0000560 int *i, vleft, first = 1, err = 0;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000561 size_t left;
Christoph Hellwig32927392020-04-24 08:43:38 +0200562 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Amerigo Wang00b7c332010-05-05 00:26:45 +0000564 if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *lenp = 0;
566 return 0;
567 }
568
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700569 i = (int *) tbl_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 vleft = table->maxlen / sizeof(*i);
571 left = *lenp;
572
573 if (!conv)
574 conv = do_proc_dointvec_conv;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (write) {
Luis R. Rodriguezd383d482017-07-12 14:33:33 -0700577 if (proc_first_pos_non_zero_ignore(ppos, table))
578 goto out;
Kees Cookf4aacea2014-06-06 14:37:19 -0700579
Amerigo Wang00b7c332010-05-05 00:26:45 +0000580 if (left > PAGE_SIZE - 1)
581 left = PAGE_SIZE - 1;
Christoph Hellwig32927392020-04-24 08:43:38 +0200582 p = buffer;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000583 }
584
585 for (; left && vleft--; i++, first=0) {
586 unsigned long lval;
587 bool neg;
588
589 if (write) {
Al Viro70f6cbb2015-12-24 00:13:10 -0500590 left -= proc_skip_spaces(&p);
Amerigo Wang00b7c332010-05-05 00:26:45 +0000591
J. R. Okajima563b0462010-05-25 16:10:14 -0700592 if (!left)
593 break;
Al Viro70f6cbb2015-12-24 00:13:10 -0500594 err = proc_get_long(&p, &left, &lval, &neg,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000595 proc_wspace_sep,
596 sizeof(proc_wspace_sep), NULL);
597 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000599 if (conv(&neg, &lval, i, 1, data)) {
600 err = -EINVAL;
601 break;
602 }
603 } else {
604 if (conv(&neg, &lval, i, 0, data)) {
605 err = -EINVAL;
606 break;
607 }
608 if (!first)
Christoph Hellwig32927392020-04-24 08:43:38 +0200609 proc_put_char(&buffer, &left, '\t');
610 proc_put_long(&buffer, &left, lval, neg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612 }
Amerigo Wang00b7c332010-05-05 00:26:45 +0000613
614 if (!write && !first && left && !err)
Christoph Hellwig32927392020-04-24 08:43:38 +0200615 proc_put_char(&buffer, &left, '\n');
J. R. Okajima563b0462010-05-25 16:10:14 -0700616 if (write && !err && left)
Al Viro70f6cbb2015-12-24 00:13:10 -0500617 left -= proc_skip_spaces(&p);
Christoph Hellwig32927392020-04-24 08:43:38 +0200618 if (write && first)
619 return err ? : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *lenp -= left;
Kees Cookf4aacea2014-06-06 14:37:19 -0700621out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *ppos += *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +0000623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700626static int do_proc_dointvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200627 void *buffer, size_t *lenp, loff_t *ppos,
Amerigo Wang00b7c332010-05-05 00:26:45 +0000628 int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700629 int write, void *data),
630 void *data)
631{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700632 return __do_proc_dointvec(table->data, table, write,
Kirill Korotaevfcfbd542006-10-02 02:18:23 -0700633 buffer, lenp, ppos, conv, data);
634}
635
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700636static int do_proc_douintvec_w(unsigned int *tbl_data,
637 struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200638 void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700639 size_t *lenp, loff_t *ppos,
640 int (*conv)(unsigned long *lvalp,
641 unsigned int *valp,
642 int write, void *data),
643 void *data)
644{
645 unsigned long lval;
646 int err = 0;
647 size_t left;
648 bool neg;
Christoph Hellwig32927392020-04-24 08:43:38 +0200649 char *p = buffer;
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700650
651 left = *lenp;
652
653 if (proc_first_pos_non_zero_ignore(ppos, table))
654 goto bail_early;
655
656 if (left > PAGE_SIZE - 1)
657 left = PAGE_SIZE - 1;
658
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700659 left -= proc_skip_spaces(&p);
660 if (!left) {
661 err = -EINVAL;
662 goto out_free;
663 }
664
665 err = proc_get_long(&p, &left, &lval, &neg,
666 proc_wspace_sep,
667 sizeof(proc_wspace_sep), NULL);
668 if (err || neg) {
669 err = -EINVAL;
670 goto out_free;
671 }
672
673 if (conv(&lval, tbl_data, 1, data)) {
674 err = -EINVAL;
675 goto out_free;
676 }
677
678 if (!err && left)
679 left -= proc_skip_spaces(&p);
680
681out_free:
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700682 if (err)
683 return -EINVAL;
684
685 return 0;
686
687 /* This is in keeping with old __do_proc_dointvec() */
688bail_early:
689 *ppos += *lenp;
690 return err;
691}
692
Christoph Hellwig32927392020-04-24 08:43:38 +0200693static int do_proc_douintvec_r(unsigned int *tbl_data, void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700694 size_t *lenp, loff_t *ppos,
695 int (*conv)(unsigned long *lvalp,
696 unsigned int *valp,
697 int write, void *data),
698 void *data)
699{
700 unsigned long lval;
701 int err = 0;
702 size_t left;
703
704 left = *lenp;
705
706 if (conv(&lval, tbl_data, 0, data)) {
707 err = -EINVAL;
708 goto out;
709 }
710
Christoph Hellwig32927392020-04-24 08:43:38 +0200711 proc_put_long(&buffer, &left, lval, false);
712 if (!left)
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700713 goto out;
714
Christoph Hellwig32927392020-04-24 08:43:38 +0200715 proc_put_char(&buffer, &left, '\n');
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700716
717out:
718 *lenp -= left;
719 *ppos += *lenp;
720
721 return err;
722}
723
724static int __do_proc_douintvec(void *tbl_data, struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200725 int write, void *buffer,
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700726 size_t *lenp, loff_t *ppos,
727 int (*conv)(unsigned long *lvalp,
728 unsigned int *valp,
729 int write, void *data),
730 void *data)
731{
732 unsigned int *i, vleft;
733
734 if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
735 *lenp = 0;
736 return 0;
737 }
738
739 i = (unsigned int *) tbl_data;
740 vleft = table->maxlen / sizeof(*i);
741
742 /*
743 * Arrays are not supported, keep this simple. *Do not* add
744 * support for them.
745 */
746 if (vleft != 1) {
747 *lenp = 0;
748 return -EINVAL;
749 }
750
751 if (!conv)
752 conv = do_proc_douintvec_conv;
753
754 if (write)
755 return do_proc_douintvec_w(i, table, buffer, lenp, ppos,
756 conv, data);
757 return do_proc_douintvec_r(i, buffer, lenp, ppos, conv, data);
758}
759
Luis Chamberlain1998f192022-01-21 22:13:20 -0800760int do_proc_douintvec(struct ctl_table *table, int write,
761 void *buffer, size_t *lenp, loff_t *ppos,
762 int (*conv)(unsigned long *lvalp,
763 unsigned int *valp,
764 int write, void *data),
765 void *data)
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700766{
767 return __do_proc_douintvec(table->data, table, write,
768 buffer, lenp, ppos, conv, data);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/**
Jia Hea2071572021-08-03 12:59:36 +0200772 * proc_dobool - read/write a bool
773 * @table: the sysctl table
774 * @write: %TRUE if this is a write to the sysctl file
775 * @buffer: the user buffer
776 * @lenp: the size of the user buffer
777 * @ppos: file position
778 *
779 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
780 * values from/to the user buffer, treated as an ASCII string.
781 *
782 * Returns 0 on success.
783 */
784int proc_dobool(struct ctl_table *table, int write, void *buffer,
785 size_t *lenp, loff_t *ppos)
786{
787 return do_proc_dointvec(table, write, buffer, lenp, ppos,
788 do_proc_dobool_conv, NULL);
789}
790
791/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * proc_dointvec - read a vector of integers
793 * @table: the sysctl table
794 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 * @buffer: the user buffer
796 * @lenp: the size of the user buffer
797 * @ppos: file position
798 *
799 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
800 * values from/to the user buffer, treated as an ASCII string.
801 *
802 * Returns 0 on success.
803 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200804int proc_dointvec(struct ctl_table *table, int write, void *buffer,
805 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700807 return do_proc_dointvec(table, write, buffer, lenp, ppos, NULL, NULL);
808}
809
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -0700810#ifdef CONFIG_COMPACTION
811static int proc_dointvec_minmax_warn_RT_change(struct ctl_table *table,
Christoph Hellwig32927392020-04-24 08:43:38 +0200812 int write, void *buffer, size_t *lenp, loff_t *ppos)
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -0700813{
814 int ret, old;
815
816 if (!IS_ENABLED(CONFIG_PREEMPT_RT) || !write)
817 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
818
819 old = *(int *)table->data;
820 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
821 if (ret)
822 return ret;
823 if (old != *(int *)table->data)
824 pr_warn_once("sysctl attribute %s changed by %s[%d]\n",
825 table->procname, current->comm,
826 task_pid_nr(current));
827 return ret;
828}
829#endif
830
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700831/**
832 * proc_douintvec - read a vector of unsigned integers
833 * @table: the sysctl table
834 * @write: %TRUE if this is a write to the sysctl file
835 * @buffer: the user buffer
836 * @lenp: the size of the user buffer
837 * @ppos: file position
838 *
839 * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
840 * values from/to the user buffer, treated as an ASCII string.
841 *
842 * Returns 0 on success.
843 */
Christoph Hellwig32927392020-04-24 08:43:38 +0200844int proc_douintvec(struct ctl_table *table, int write, void *buffer,
845 size_t *lenp, loff_t *ppos)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -0700846{
Luis R. Rodriguez4f2fec02017-07-12 14:33:36 -0700847 return do_proc_douintvec(table, write, buffer, lenp, ppos,
848 do_proc_douintvec_conv, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800851/*
Andi Kleen25ddbb12008-10-15 22:01:41 -0700852 * Taint values can only be increased
853 * This means we can safely use a temporary.
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800854 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700855static int proc_taint(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200856 void *buffer, size_t *lenp, loff_t *ppos)
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800857{
Andi Kleen25ddbb12008-10-15 22:01:41 -0700858 struct ctl_table t;
859 unsigned long tmptaint = get_taint();
860 int err;
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800861
Bastian Blank91fcd412007-04-23 14:41:14 -0700862 if (write && !capable(CAP_SYS_ADMIN))
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800863 return -EPERM;
864
Andi Kleen25ddbb12008-10-15 22:01:41 -0700865 t = *table;
866 t.data = &tmptaint;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700867 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700868 if (err < 0)
869 return err;
870
871 if (write) {
Rafael Aquinidb38d5c2020-06-07 21:40:17 -0700872 int i;
873
874 /*
875 * If we are relying on panic_on_taint not producing
876 * false positives due to userspace input, bail out
877 * before setting the requested taint flags.
878 */
879 if (panic_on_taint_nousertaint && (tmptaint & panic_on_taint))
880 return -EINVAL;
881
Andi Kleen25ddbb12008-10-15 22:01:41 -0700882 /*
883 * Poor man's atomic or. Not worth adding a primitive
884 * to everyone's atomic.h for this
885 */
Rafael Aquinie77132e2020-06-07 21:40:51 -0700886 for (i = 0; i < TAINT_FLAGS_COUNT; i++)
887 if ((1UL << i) & tmptaint)
Rusty Russell373d4d02013-01-21 17:17:39 +1030888 add_taint(i, LOCKDEP_STILL_OK);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700889 }
890
891 return err;
Theodore Ts'o34f5a392007-02-10 01:45:24 -0800892}
893
Waiman Long24704f32018-04-10 16:35:38 -0700894/**
895 * struct do_proc_dointvec_minmax_conv_param - proc_dointvec_minmax() range checking structure
896 * @min: pointer to minimum allowable value
897 * @max: pointer to maximum allowable value
898 *
899 * The do_proc_dointvec_minmax_conv_param structure provides the
900 * minimum and maximum values for doing range checking for those sysctl
901 * parameters that use the proc_dointvec_minmax() handler.
902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903struct do_proc_dointvec_minmax_conv_param {
904 int *min;
905 int *max;
906};
907
Amerigo Wang00b7c332010-05-05 00:26:45 +0000908static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
909 int *valp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int write, void *data)
911{
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700912 int tmp, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct do_proc_dointvec_minmax_conv_param *param = data;
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700914 /*
915 * If writing, first do so via a temporary local int so we can
916 * bounds-check it before touching *valp.
917 */
918 int *ip = write ? &tmp : valp;
919
920 ret = do_proc_dointvec_conv(negp, lvalp, ip, write, data);
921 if (ret)
922 return ret;
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (write) {
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700925 if ((param->min && *param->min > tmp) ||
926 (param->max && *param->max < tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -EINVAL;
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700928 *valp = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
933
934/**
935 * proc_dointvec_minmax - read a vector of integers with min/max values
936 * @table: the sysctl table
937 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * @buffer: the user buffer
939 * @lenp: the size of the user buffer
940 * @ppos: file position
941 *
942 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
943 * values from/to the user buffer, treated as an ASCII string.
944 *
945 * This routine will ensure the values are within the range specified by
946 * table->extra1 (min) and table->extra2 (max).
947 *
Waiman Long24704f32018-04-10 16:35:38 -0700948 * Returns 0 on success or -EINVAL on write when the range check fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700950int proc_dointvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200951 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct do_proc_dointvec_minmax_conv_param param = {
954 .min = (int *) table->extra1,
955 .max = (int *) table->extra2,
956 };
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700957 return do_proc_dointvec(table, write, buffer, lenp, ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 do_proc_dointvec_minmax_conv, &param);
959}
960
Waiman Long24704f32018-04-10 16:35:38 -0700961/**
962 * struct do_proc_douintvec_minmax_conv_param - proc_douintvec_minmax() range checking structure
963 * @min: pointer to minimum allowable value
964 * @max: pointer to maximum allowable value
965 *
966 * The do_proc_douintvec_minmax_conv_param structure provides the
967 * minimum and maximum values for doing range checking for those sysctl
968 * parameters that use the proc_douintvec_minmax() handler.
969 */
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700970struct do_proc_douintvec_minmax_conv_param {
971 unsigned int *min;
972 unsigned int *max;
973};
974
975static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
976 unsigned int *valp,
977 int write, void *data)
978{
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700979 int ret;
980 unsigned int tmp;
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700981 struct do_proc_douintvec_minmax_conv_param *param = data;
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700982 /* write via temporary local uint for bounds-checking */
983 unsigned int *up = write ? &tmp : valp;
984
985 ret = do_proc_douintvec_conv(lvalp, up, write, data);
986 if (ret)
987 return ret;
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700988
989 if (write) {
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700990 if ((param->min && *param->min > tmp) ||
991 (param->max && *param->max < tmp))
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700992 return -ERANGE;
993
Zev Weiss2bc4fc62019-03-11 23:28:06 -0700994 *valp = tmp;
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -0700995 }
996
997 return 0;
998}
999
1000/**
1001 * proc_douintvec_minmax - read a vector of unsigned ints with min/max values
1002 * @table: the sysctl table
1003 * @write: %TRUE if this is a write to the sysctl file
1004 * @buffer: the user buffer
1005 * @lenp: the size of the user buffer
1006 * @ppos: file position
1007 *
1008 * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
1009 * values from/to the user buffer, treated as an ASCII string. Negative
1010 * strings are not allowed.
1011 *
1012 * This routine will ensure the values are within the range specified by
1013 * table->extra1 (min) and table->extra2 (max). There is a final sanity
1014 * check for UINT_MAX to avoid having to support wrap around uses from
1015 * userspace.
1016 *
Waiman Long24704f32018-04-10 16:35:38 -07001017 * Returns 0 on success or -ERANGE on write when the range check fails.
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001018 */
1019int proc_douintvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001020 void *buffer, size_t *lenp, loff_t *ppos)
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001021{
1022 struct do_proc_douintvec_minmax_conv_param param = {
1023 .min = (unsigned int *) table->extra1,
1024 .max = (unsigned int *) table->extra2,
1025 };
1026 return do_proc_douintvec(table, write, buffer, lenp, ppos,
1027 do_proc_douintvec_minmax_conv, &param);
1028}
1029
Eric Dumazetcb944412021-03-25 11:08:13 -07001030/**
1031 * proc_dou8vec_minmax - read a vector of unsigned chars with min/max values
1032 * @table: the sysctl table
1033 * @write: %TRUE if this is a write to the sysctl file
1034 * @buffer: the user buffer
1035 * @lenp: the size of the user buffer
1036 * @ppos: file position
1037 *
1038 * Reads/writes up to table->maxlen/sizeof(u8) unsigned chars
1039 * values from/to the user buffer, treated as an ASCII string. Negative
1040 * strings are not allowed.
1041 *
1042 * This routine will ensure the values are within the range specified by
1043 * table->extra1 (min) and table->extra2 (max).
1044 *
1045 * Returns 0 on success or an error on write when the range check fails.
1046 */
1047int proc_dou8vec_minmax(struct ctl_table *table, int write,
1048 void *buffer, size_t *lenp, loff_t *ppos)
1049{
1050 struct ctl_table tmp;
1051 unsigned int min = 0, max = 255U, val;
1052 u8 *data = table->data;
1053 struct do_proc_douintvec_minmax_conv_param param = {
1054 .min = &min,
1055 .max = &max,
1056 };
1057 int res;
1058
1059 /* Do not support arrays yet. */
1060 if (table->maxlen != sizeof(u8))
1061 return -EINVAL;
1062
1063 if (table->extra1) {
1064 min = *(unsigned int *) table->extra1;
1065 if (min > 255U)
1066 return -EINVAL;
1067 }
1068 if (table->extra2) {
1069 max = *(unsigned int *) table->extra2;
1070 if (max > 255U)
1071 return -EINVAL;
1072 }
1073
1074 tmp = *table;
1075
1076 tmp.maxlen = sizeof(val);
1077 tmp.data = &val;
1078 val = *data;
1079 res = do_proc_douintvec(&tmp, write, buffer, lenp, ppos,
1080 do_proc_douintvec_minmax_conv, &param);
1081 if (res)
1082 return res;
1083 if (write)
1084 *data = val;
1085 return 0;
1086}
1087EXPORT_SYMBOL_GPL(proc_dou8vec_minmax);
1088
Dmitry Safonoveaee4172020-03-02 17:51:34 +00001089#ifdef CONFIG_MAGIC_SYSRQ
1090static int sysrq_sysctl_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001091 void *buffer, size_t *lenp, loff_t *ppos)
Dmitry Safonoveaee4172020-03-02 17:51:34 +00001092{
1093 int tmp, ret;
1094
1095 tmp = sysrq_mask();
1096
1097 ret = __do_proc_dointvec(&tmp, table, write, buffer,
1098 lenp, ppos, NULL, NULL);
1099 if (ret || !write)
1100 return ret;
1101
1102 if (write)
1103 sysrq_toggle_support(tmp);
1104
1105 return 0;
1106}
1107#endif
1108
Christoph Hellwig32927392020-04-24 08:43:38 +02001109static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table,
1110 int write, void *buffer, size_t *lenp, loff_t *ppos,
1111 unsigned long convmul, unsigned long convdiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Amerigo Wang00b7c332010-05-05 00:26:45 +00001113 unsigned long *i, *min, *max;
1114 int vleft, first = 1, err = 0;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001115 size_t left;
Christoph Hellwig32927392020-04-24 08:43:38 +02001116 char *p;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001117
1118 if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *lenp = 0;
1120 return 0;
1121 }
Amerigo Wang00b7c332010-05-05 00:26:45 +00001122
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001123 i = (unsigned long *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 min = (unsigned long *) table->extra1;
1125 max = (unsigned long *) table->extra2;
1126 vleft = table->maxlen / sizeof(unsigned long);
1127 left = *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001128
1129 if (write) {
Luis R. Rodriguezd383d482017-07-12 14:33:33 -07001130 if (proc_first_pos_non_zero_ignore(ppos, table))
1131 goto out;
Kees Cookf4aacea2014-06-06 14:37:19 -07001132
Amerigo Wang00b7c332010-05-05 00:26:45 +00001133 if (left > PAGE_SIZE - 1)
1134 left = PAGE_SIZE - 1;
Christoph Hellwig32927392020-04-24 08:43:38 +02001135 p = buffer;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001136 }
1137
Eric Dumazet27b3d802010-10-07 12:59:29 -07001138 for (; left && vleft--; i++, first = 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001139 unsigned long val;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (write) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001142 bool neg;
1143
Al Viro70f6cbb2015-12-24 00:13:10 -05001144 left -= proc_skip_spaces(&p);
Cheng Lin09be1782019-01-03 15:26:13 -08001145 if (!left)
1146 break;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001147
Al Viro70f6cbb2015-12-24 00:13:10 -05001148 err = proc_get_long(&p, &left, &val, &neg,
Amerigo Wang00b7c332010-05-05 00:26:45 +00001149 proc_wspace_sep,
1150 sizeof(proc_wspace_sep), NULL);
1151 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 if (neg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 continue;
Eric Dumazetff9f8a72017-01-25 18:20:55 -08001155 val = convmul * val / convdiv;
Christian Braunere260ad02019-05-14 15:44:55 -07001156 if ((min && val < *min) || (max && val > *max)) {
1157 err = -EINVAL;
1158 break;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 *i = val;
1161 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001162 val = convdiv * (*i) / convmul;
Christoph Hellwig32927392020-04-24 08:43:38 +02001163 if (!first)
1164 proc_put_char(&buffer, &left, '\t');
1165 proc_put_long(&buffer, &left, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 }
1168
Amerigo Wang00b7c332010-05-05 00:26:45 +00001169 if (!write && !first && left && !err)
Christoph Hellwig32927392020-04-24 08:43:38 +02001170 proc_put_char(&buffer, &left, '\n');
Amerigo Wang00b7c332010-05-05 00:26:45 +00001171 if (write && !err)
Al Viro70f6cbb2015-12-24 00:13:10 -05001172 left -= proc_skip_spaces(&p);
Christoph Hellwig32927392020-04-24 08:43:38 +02001173 if (write && first)
1174 return err ? : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 *lenp -= left;
Kees Cookf4aacea2014-06-06 14:37:19 -07001176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *ppos += *lenp;
Amerigo Wang00b7c332010-05-05 00:26:45 +00001178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001181static int do_proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001182 void *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul,
1183 unsigned long convdiv)
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001184{
1185 return __do_proc_doulongvec_minmax(table->data, table, write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001186 buffer, lenp, ppos, convmul, convdiv);
Kirill Korotaevfcfbd542006-10-02 02:18:23 -07001187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189/**
1190 * proc_doulongvec_minmax - read a vector of long integers with min/max values
1191 * @table: the sysctl table
1192 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * @buffer: the user buffer
1194 * @lenp: the size of the user buffer
1195 * @ppos: file position
1196 *
1197 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1198 * values from/to the user buffer, treated as an ASCII string.
1199 *
1200 * This routine will ensure the values are within the range specified by
1201 * table->extra1 (min) and table->extra2 (max).
1202 *
1203 * Returns 0 on success.
1204 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001205int proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001206 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001208 return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/**
1212 * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
1213 * @table: the sysctl table
1214 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * @buffer: the user buffer
1216 * @lenp: the size of the user buffer
1217 * @ppos: file position
1218 *
1219 * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
1220 * values from/to the user buffer, treated as an ASCII string. The values
1221 * are treated as milliseconds, and converted to jiffies when they are stored.
1222 *
1223 * This routine will ensure the values are within the range specified by
1224 * table->extra1 (min) and table->extra2 (max).
1225 *
1226 * Returns 0 on success.
1227 */
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001228int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001229 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001231 return do_proc_doulongvec_minmax(table, write, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 lenp, ppos, HZ, 1000l);
1233}
1234
1235
Amerigo Wang00b7c332010-05-05 00:26:45 +00001236static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int *valp,
1238 int write, void *data)
1239{
1240 if (write) {
Gao Feng63259452017-05-08 15:54:58 -07001241 if (*lvalp > INT_MAX / HZ)
Bart Samwelcba9f332006-03-24 03:15:50 -08001242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
1244 } else {
1245 int val = *valp;
1246 unsigned long lval;
1247 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001248 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001249 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001251 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 lval = (unsigned long)val;
1253 }
1254 *lvalp = lval / HZ;
1255 }
1256 return 0;
1257}
1258
Amerigo Wang00b7c332010-05-05 00:26:45 +00001259static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int *valp,
1261 int write, void *data)
1262{
1263 if (write) {
Bart Samwelcba9f332006-03-24 03:15:50 -08001264 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
1265 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
1267 } else {
1268 int val = *valp;
1269 unsigned long lval;
1270 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001271 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001272 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001274 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 lval = (unsigned long)val;
1276 }
1277 *lvalp = jiffies_to_clock_t(lval);
1278 }
1279 return 0;
1280}
1281
Amerigo Wang00b7c332010-05-05 00:26:45 +00001282static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int *valp,
1284 int write, void *data)
1285{
1286 if (write) {
Francesco Fuscod738ce82013-07-24 10:39:07 +02001287 unsigned long jif = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
1288
1289 if (jif > INT_MAX)
1290 return 1;
1291 *valp = (int)jif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 } else {
1293 int val = *valp;
1294 unsigned long lval;
1295 if (val < 0) {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001296 *negp = true;
Ilya Dryomov9a5bc722015-09-09 15:39:06 -07001297 lval = -(unsigned long)val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 } else {
Amerigo Wang00b7c332010-05-05 00:26:45 +00001299 *negp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 lval = (unsigned long)val;
1301 }
1302 *lvalp = jiffies_to_msecs(lval);
1303 }
1304 return 0;
1305}
1306
1307/**
1308 * proc_dointvec_jiffies - read a vector of integers as seconds
1309 * @table: the sysctl table
1310 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * @buffer: the user buffer
1312 * @lenp: the size of the user buffer
1313 * @ppos: file position
1314 *
1315 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1316 * values from/to the user buffer, treated as an ASCII string.
1317 * The values read are assumed to be in seconds, and are converted into
1318 * jiffies.
1319 *
1320 * Returns 0 on success.
1321 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001322int proc_dointvec_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001323 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001325 return do_proc_dointvec(table,write,buffer,lenp,ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 do_proc_dointvec_jiffies_conv,NULL);
1327}
1328
1329/**
1330 * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
1331 * @table: the sysctl table
1332 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * @buffer: the user buffer
1334 * @lenp: the size of the user buffer
Randy Dunlap1e5d5332005-11-07 01:01:06 -08001335 * @ppos: pointer to the file position
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1338 * values from/to the user buffer, treated as an ASCII string.
1339 * The values read are assumed to be in 1/USER_HZ seconds, and
1340 * are converted into jiffies.
1341 *
1342 * Returns 0 on success.
1343 */
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001344int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001345 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001347 return do_proc_dointvec(table,write,buffer,lenp,ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 do_proc_dointvec_userhz_jiffies_conv,NULL);
1349}
1350
1351/**
1352 * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
1353 * @table: the sysctl table
1354 * @write: %TRUE if this is a write to the sysctl file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * @buffer: the user buffer
1356 * @lenp: the size of the user buffer
Martin Waitz67be2dd2005-05-01 08:59:26 -07001357 * @ppos: file position
1358 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
1360 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1361 * values from/to the user buffer, treated as an ASCII string.
1362 * The values read are assumed to be in 1/1000 seconds, and
1363 * are converted into jiffies.
1364 *
1365 * Returns 0 on success.
1366 */
Christoph Hellwig32927392020-04-24 08:43:38 +02001367int proc_dointvec_ms_jiffies(struct ctl_table *table, int write, void *buffer,
1368 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001370 return do_proc_dointvec(table, write, buffer, lenp, ppos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 do_proc_dointvec_ms_jiffies_conv, NULL);
1372}
1373
Christoph Hellwig32927392020-04-24 08:43:38 +02001374static int proc_do_cad_pid(struct ctl_table *table, int write, void *buffer,
1375 size_t *lenp, loff_t *ppos)
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001376{
1377 struct pid *new_pid;
1378 pid_t tmp;
1379 int r;
1380
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -08001381 tmp = pid_vnr(cad_pid);
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001382
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001383 r = __do_proc_dointvec(&tmp, table, write, buffer,
Cedric Le Goater9ec52092006-10-02 02:19:00 -07001384 lenp, ppos, NULL, NULL);
1385 if (r || !write)
1386 return r;
1387
1388 new_pid = find_get_pid(tmp);
1389 if (!new_pid)
1390 return -ESRCH;
1391
1392 put_pid(xchg(&cad_pid, new_pid));
1393 return 0;
1394}
1395
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001396/**
1397 * proc_do_large_bitmap - read/write from/to a large bitmap
1398 * @table: the sysctl table
1399 * @write: %TRUE if this is a write to the sysctl file
1400 * @buffer: the user buffer
1401 * @lenp: the size of the user buffer
1402 * @ppos: file position
1403 *
1404 * The bitmap is stored at table->data and the bitmap length (in bits)
1405 * in table->maxlen.
1406 *
1407 * We use a range comma separated format (e.g. 1,3-4,10-10) so that
1408 * large bitmaps may be represented in a compact manner. Writing into
1409 * the file will clear the bitmap then update it with the given input.
1410 *
1411 * Returns 0 on success.
1412 */
1413int proc_do_large_bitmap(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001414 void *buffer, size_t *lenp, loff_t *ppos)
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001415{
1416 int err = 0;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001417 size_t left = *lenp;
1418 unsigned long bitmap_len = table->maxlen;
WANG Cong122ff242014-05-12 16:04:53 -07001419 unsigned long *bitmap = *(unsigned long **) table->data;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001420 unsigned long *tmp_bitmap = NULL;
1421 char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
1422
WANG Cong122ff242014-05-12 16:04:53 -07001423 if (!bitmap || !bitmap_len || !left || (*ppos && !write)) {
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001424 *lenp = 0;
1425 return 0;
1426 }
1427
1428 if (write) {
Christoph Hellwig32927392020-04-24 08:43:38 +02001429 char *p = buffer;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001430 size_t skipped = 0;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001431
Eric Sandeen3116ad32019-05-14 15:45:13 -07001432 if (left > PAGE_SIZE - 1) {
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001433 left = PAGE_SIZE - 1;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001434 /* How much of the buffer we'll skip this pass */
1435 skipped = *lenp - left;
1436 }
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001437
Andy Shevchenko475dae32019-05-14 15:44:52 -07001438 tmp_bitmap = bitmap_zalloc(bitmap_len, GFP_KERNEL);
Christoph Hellwig32927392020-04-24 08:43:38 +02001439 if (!tmp_bitmap)
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001440 return -ENOMEM;
Al Viro70f6cbb2015-12-24 00:13:10 -05001441 proc_skip_char(&p, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001442 while (!err && left) {
1443 unsigned long val_a, val_b;
1444 bool neg;
Eric Sandeen3116ad32019-05-14 15:45:13 -07001445 size_t saved_left;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001446
Eric Sandeen3116ad32019-05-14 15:45:13 -07001447 /* In case we stop parsing mid-number, we can reset */
1448 saved_left = left;
Al Viro70f6cbb2015-12-24 00:13:10 -05001449 err = proc_get_long(&p, &left, &val_a, &neg, tr_a,
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001450 sizeof(tr_a), &c);
Eric Sandeen3116ad32019-05-14 15:45:13 -07001451 /*
1452 * If we consumed the entirety of a truncated buffer or
1453 * only one char is left (may be a "-"), then stop here,
1454 * reset, & come back for more.
1455 */
1456 if ((left <= 1) && skipped) {
1457 left = saved_left;
1458 break;
1459 }
1460
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001461 if (err)
1462 break;
1463 if (val_a >= bitmap_len || neg) {
1464 err = -EINVAL;
1465 break;
1466 }
1467
1468 val_b = val_a;
1469 if (left) {
Al Viro70f6cbb2015-12-24 00:13:10 -05001470 p++;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001471 left--;
1472 }
1473
1474 if (c == '-') {
Al Viro70f6cbb2015-12-24 00:13:10 -05001475 err = proc_get_long(&p, &left, &val_b,
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001476 &neg, tr_b, sizeof(tr_b),
1477 &c);
Eric Sandeen3116ad32019-05-14 15:45:13 -07001478 /*
1479 * If we consumed all of a truncated buffer or
1480 * then stop here, reset, & come back for more.
1481 */
1482 if (!left && skipped) {
1483 left = saved_left;
1484 break;
1485 }
1486
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001487 if (err)
1488 break;
1489 if (val_b >= bitmap_len || neg ||
1490 val_a > val_b) {
1491 err = -EINVAL;
1492 break;
1493 }
1494 if (left) {
Al Viro70f6cbb2015-12-24 00:13:10 -05001495 p++;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001496 left--;
1497 }
1498 }
1499
Akinobu Mita5a04cca2012-03-28 14:42:50 -07001500 bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
Al Viro70f6cbb2015-12-24 00:13:10 -05001501 proc_skip_char(&p, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001502 }
Eric Sandeen3116ad32019-05-14 15:45:13 -07001503 left += skipped;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001504 } else {
1505 unsigned long bit_a, bit_b = 0;
Jiapeng Chong9a52c5f2021-06-30 18:54:53 -07001506 bool first = 1;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001507
1508 while (left) {
1509 bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
1510 if (bit_a >= bitmap_len)
1511 break;
1512 bit_b = find_next_zero_bit(bitmap, bitmap_len,
1513 bit_a + 1) - 1;
1514
Christoph Hellwig32927392020-04-24 08:43:38 +02001515 if (!first)
1516 proc_put_char(&buffer, &left, ',');
1517 proc_put_long(&buffer, &left, bit_a, false);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001518 if (bit_a != bit_b) {
Christoph Hellwig32927392020-04-24 08:43:38 +02001519 proc_put_char(&buffer, &left, '-');
1520 proc_put_long(&buffer, &left, bit_b, false);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001521 }
1522
1523 first = 0; bit_b++;
1524 }
Christoph Hellwig32927392020-04-24 08:43:38 +02001525 proc_put_char(&buffer, &left, '\n');
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001526 }
1527
1528 if (!err) {
1529 if (write) {
1530 if (*ppos)
1531 bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
1532 else
Akinobu Mita5a04cca2012-03-28 14:42:50 -07001533 bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001534 }
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001535 *lenp -= left;
1536 *ppos += *lenp;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001537 }
Ola N. Kaldestadf9eb2fd2017-11-17 15:30:26 -08001538
Andy Shevchenko475dae32019-05-14 15:44:52 -07001539 bitmap_free(tmp_bitmap);
Ola N. Kaldestadf9eb2fd2017-11-17 15:30:26 -08001540 return err;
Octavian Purdila9f977fb2010-05-05 00:26:55 +00001541}
1542
Jovi Zhang55610502011-01-12 17:00:45 -08001543#else /* CONFIG_PROC_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001545int proc_dostring(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001546 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 return -ENOSYS;
1549}
1550
Jia Hea2071572021-08-03 12:59:36 +02001551int proc_dobool(struct ctl_table *table, int write,
1552 void *buffer, size_t *lenp, loff_t *ppos)
1553{
1554 return -ENOSYS;
1555}
1556
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001557int proc_dointvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001558 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 return -ENOSYS;
1561}
1562
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07001563int proc_douintvec(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001564 void *buffer, size_t *lenp, loff_t *ppos)
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07001565{
1566 return -ENOSYS;
1567}
1568
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001569int proc_dointvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001570 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 return -ENOSYS;
1573}
1574
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001575int proc_douintvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001576 void *buffer, size_t *lenp, loff_t *ppos)
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07001577{
1578 return -ENOSYS;
1579}
1580
Eric Dumazetcb944412021-03-25 11:08:13 -07001581int proc_dou8vec_minmax(struct ctl_table *table, int write,
1582 void *buffer, size_t *lenp, loff_t *ppos)
1583{
1584 return -ENOSYS;
1585}
1586
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001587int proc_dointvec_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001588 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 return -ENOSYS;
1591}
1592
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001593int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001594 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 return -ENOSYS;
1597}
1598
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001599int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001600 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 return -ENOSYS;
1603}
1604
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001605int proc_doulongvec_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001606 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 return -ENOSYS;
1609}
1610
Eric W. Biedermand8217f02007-10-18 03:05:22 -07001611int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001612 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Christoph Hellwig32927392020-04-24 08:43:38 +02001614 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04001617int proc_do_large_bitmap(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001618 void *buffer, size_t *lenp, loff_t *ppos)
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04001619{
1620 return -ENOSYS;
1621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Jovi Zhang55610502011-01-12 17:00:45 -08001623#endif /* CONFIG_PROC_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001625#if defined(CONFIG_SYSCTL)
1626int proc_do_static_key(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001627 void *buffer, size_t *lenp, loff_t *ppos)
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001628{
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001629 struct static_key *key = (struct static_key *)table->data;
1630 static DEFINE_MUTEX(static_key_mutex);
1631 int val, ret;
1632 struct ctl_table tmp = {
1633 .data = &val,
1634 .maxlen = sizeof(val),
1635 .mode = table->mode,
Matteo Croceeec48442019-07-18 15:58:50 -07001636 .extra1 = SYSCTL_ZERO,
1637 .extra2 = SYSCTL_ONE,
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001638 };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001639
1640 if (write && !capable(CAP_SYS_ADMIN))
1641 return -EPERM;
1642
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001643 mutex_lock(&static_key_mutex);
1644 val = static_key_enabled(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001645 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1646 if (write && !ret) {
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001647 if (val)
1648 static_key_enable(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001649 else
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001650 static_key_disable(key);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001651 }
Eric Dumazeta8e11e52019-06-14 16:22:18 -07001652 mutex_unlock(&static_key_mutex);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001653 return ret;
1654}
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656static struct ctl_table kern_table[] = {
1657 {
1658 .procname = "sched_child_runs_first",
1659 .data = &sysctl_sched_child_runs_first,
1660 .maxlen = sizeof(unsigned int),
1661 .mode = 0644,
1662 .proc_handler = proc_dointvec,
1663 },
Peter Zijlstra1d1c2502021-03-24 19:47:43 +01001664#ifdef CONFIG_SCHEDSTATS
1665 {
1666 .procname = "sched_schedstats",
1667 .data = NULL,
1668 .maxlen = sizeof(unsigned int),
1669 .mode = 0644,
1670 .proc_handler = sysctl_schedstats,
1671 .extra1 = SYSCTL_ZERO,
1672 .extra2 = SYSCTL_ONE,
1673 },
1674#endif /* CONFIG_SCHEDSTATS */
Peter Zijlstra0cd7c742021-05-10 14:01:00 +02001675#ifdef CONFIG_TASK_DELAY_ACCT
1676 {
1677 .procname = "task_delayacct",
1678 .data = NULL,
1679 .maxlen = sizeof(unsigned int),
1680 .mode = 0644,
1681 .proc_handler = sysctl_delayacct,
1682 .extra1 = SYSCTL_ZERO,
1683 .extra2 = SYSCTL_ONE,
1684 },
1685#endif /* CONFIG_TASK_DELAY_ACCT */
Mel Gormanb7cc6ec2021-03-24 13:39:16 +00001686#ifdef CONFIG_NUMA_BALANCING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 {
1688 .procname = "numa_balancing",
1689 .data = NULL, /* filled in by handler */
1690 .maxlen = sizeof(unsigned int),
1691 .mode = 0644,
1692 .proc_handler = sysctl_numa_balancing,
1693 .extra1 = SYSCTL_ZERO,
1694 .extra2 = SYSCTL_ONE,
1695 },
1696#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 {
1698 .procname = "sched_rt_period_us",
1699 .data = &sysctl_sched_rt_period,
1700 .maxlen = sizeof(unsigned int),
1701 .mode = 0644,
1702 .proc_handler = sched_rt_handler,
1703 },
1704 {
1705 .procname = "sched_rt_runtime_us",
1706 .data = &sysctl_sched_rt_runtime,
1707 .maxlen = sizeof(int),
1708 .mode = 0644,
1709 .proc_handler = sched_rt_handler,
1710 },
1711 {
Peter Zijlstrab4098bf2019-07-26 16:54:10 +02001712 .procname = "sched_deadline_period_max_us",
1713 .data = &sysctl_sched_dl_period_max,
1714 .maxlen = sizeof(unsigned int),
1715 .mode = 0644,
1716 .proc_handler = proc_dointvec,
1717 },
1718 {
1719 .procname = "sched_deadline_period_min_us",
1720 .data = &sysctl_sched_dl_period_min,
1721 .maxlen = sizeof(unsigned int),
1722 .mode = 0644,
1723 .proc_handler = proc_dointvec,
1724 },
1725 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .procname = "sched_rr_timeslice_ms",
1727 .data = &sysctl_sched_rr_timeslice,
1728 .maxlen = sizeof(int),
1729 .mode = 0644,
1730 .proc_handler = sched_rr_handler,
1731 },
1732#ifdef CONFIG_UCLAMP_TASK
1733 {
1734 .procname = "sched_util_clamp_min",
1735 .data = &sysctl_sched_uclamp_util_min,
1736 .maxlen = sizeof(unsigned int),
1737 .mode = 0644,
1738 .proc_handler = sysctl_sched_uclamp_handler,
1739 },
1740 {
1741 .procname = "sched_util_clamp_max",
1742 .data = &sysctl_sched_uclamp_util_max,
1743 .maxlen = sizeof(unsigned int),
1744 .mode = 0644,
1745 .proc_handler = sysctl_sched_uclamp_handler,
1746 },
Qais Yousef13685c42020-07-16 12:03:45 +01001747 {
1748 .procname = "sched_util_clamp_min_rt_default",
1749 .data = &sysctl_sched_uclamp_util_min_rt_default,
1750 .maxlen = sizeof(unsigned int),
1751 .mode = 0644,
1752 .proc_handler = sysctl_sched_uclamp_handler,
1753 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#endif
1755#ifdef CONFIG_SCHED_AUTOGROUP
1756 {
1757 .procname = "sched_autogroup_enabled",
1758 .data = &sysctl_sched_autogroup_enabled,
1759 .maxlen = sizeof(unsigned int),
1760 .mode = 0644,
1761 .proc_handler = proc_dointvec_minmax,
1762 .extra1 = SYSCTL_ZERO,
1763 .extra2 = SYSCTL_ONE,
1764 },
1765#endif
1766#ifdef CONFIG_CFS_BANDWIDTH
1767 {
1768 .procname = "sched_cfs_bandwidth_slice_us",
1769 .data = &sysctl_sched_cfs_bandwidth_slice,
1770 .maxlen = sizeof(unsigned int),
1771 .mode = 0644,
1772 .proc_handler = proc_dointvec_minmax,
1773 .extra1 = SYSCTL_ONE,
1774 },
1775#endif
1776#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
1777 {
1778 .procname = "sched_energy_aware",
1779 .data = &sysctl_sched_energy_aware,
1780 .maxlen = sizeof(unsigned int),
1781 .mode = 0644,
1782 .proc_handler = sched_energy_aware_handler,
1783 .extra1 = SYSCTL_ZERO,
1784 .extra2 = SYSCTL_ONE,
1785 },
1786#endif
1787#ifdef CONFIG_PROVE_LOCKING
1788 {
1789 .procname = "prove_locking",
1790 .data = &prove_locking,
1791 .maxlen = sizeof(int),
1792 .mode = 0644,
1793 .proc_handler = proc_dointvec,
1794 },
1795#endif
1796#ifdef CONFIG_LOCK_STAT
1797 {
1798 .procname = "lock_stat",
1799 .data = &lock_stat,
1800 .maxlen = sizeof(int),
1801 .mode = 0644,
1802 .proc_handler = proc_dointvec,
1803 },
1804#endif
1805 {
1806 .procname = "panic",
1807 .data = &panic_timeout,
1808 .maxlen = sizeof(int),
1809 .mode = 0644,
1810 .proc_handler = proc_dointvec,
1811 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812#ifdef CONFIG_PROC_SYSCTL
1813 {
1814 .procname = "tainted",
1815 .maxlen = sizeof(long),
1816 .mode = 0644,
1817 .proc_handler = proc_taint,
1818 },
1819 {
1820 .procname = "sysctl_writes_strict",
1821 .data = &sysctl_writes_strict,
1822 .maxlen = sizeof(int),
1823 .mode = 0644,
1824 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08001825 .extra1 = SYSCTL_NEG_ONE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .extra2 = SYSCTL_ONE,
1827 },
1828#endif
1829#ifdef CONFIG_LATENCYTOP
1830 {
1831 .procname = "latencytop",
1832 .data = &latencytop_enabled,
1833 .maxlen = sizeof(int),
1834 .mode = 0644,
1835 .proc_handler = sysctl_latencytop,
1836 },
1837#endif
1838#ifdef CONFIG_BLK_DEV_INITRD
1839 {
1840 .procname = "real-root-dev",
1841 .data = &real_root_dev,
1842 .maxlen = sizeof(int),
1843 .mode = 0644,
1844 .proc_handler = proc_dointvec,
1845 },
1846#endif
1847 {
1848 .procname = "print-fatal-signals",
1849 .data = &print_fatal_signals,
1850 .maxlen = sizeof(int),
1851 .mode = 0644,
1852 .proc_handler = proc_dointvec,
1853 },
1854#ifdef CONFIG_SPARC
1855 {
1856 .procname = "reboot-cmd",
1857 .data = reboot_command,
1858 .maxlen = 256,
1859 .mode = 0644,
1860 .proc_handler = proc_dostring,
1861 },
1862 {
1863 .procname = "stop-a",
1864 .data = &stop_a_enabled,
1865 .maxlen = sizeof (int),
1866 .mode = 0644,
1867 .proc_handler = proc_dointvec,
1868 },
1869 {
1870 .procname = "scons-poweroff",
1871 .data = &scons_pwroff,
1872 .maxlen = sizeof (int),
1873 .mode = 0644,
1874 .proc_handler = proc_dointvec,
1875 },
1876#endif
1877#ifdef CONFIG_SPARC64
1878 {
1879 .procname = "tsb-ratio",
1880 .data = &sysctl_tsb_ratio,
1881 .maxlen = sizeof (int),
1882 .mode = 0644,
1883 .proc_handler = proc_dointvec,
1884 },
1885#endif
1886#ifdef CONFIG_PARISC
1887 {
1888 .procname = "soft-power",
1889 .data = &pwrsw_enabled,
1890 .maxlen = sizeof (int),
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02001891 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .proc_handler = proc_dointvec,
1893 },
1894#endif
1895#ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
1896 {
1897 .procname = "unaligned-trap",
1898 .data = &unaligned_enabled,
1899 .maxlen = sizeof (int),
1900 .mode = 0644,
1901 .proc_handler = proc_dointvec,
1902 },
1903#endif
1904 {
1905 .procname = "ctrl-alt-del",
1906 .data = &C_A_D,
1907 .maxlen = sizeof(int),
1908 .mode = 0644,
1909 .proc_handler = proc_dointvec,
1910 },
1911#ifdef CONFIG_FUNCTION_TRACER
1912 {
1913 .procname = "ftrace_enabled",
1914 .data = &ftrace_enabled,
1915 .maxlen = sizeof(int),
1916 .mode = 0644,
1917 .proc_handler = ftrace_enable_sysctl,
1918 },
1919#endif
1920#ifdef CONFIG_STACK_TRACER
1921 {
1922 .procname = "stack_tracer_enabled",
1923 .data = &stack_tracer_enabled,
1924 .maxlen = sizeof(int),
1925 .mode = 0644,
1926 .proc_handler = stack_trace_sysctl,
1927 },
1928#endif
1929#ifdef CONFIG_TRACING
1930 {
1931 .procname = "ftrace_dump_on_oops",
1932 .data = &ftrace_dump_on_oops,
1933 .maxlen = sizeof(int),
1934 .mode = 0644,
1935 .proc_handler = proc_dointvec,
1936 },
1937 {
1938 .procname = "traceoff_on_warning",
1939 .data = &__disable_trace_on_warning,
1940 .maxlen = sizeof(__disable_trace_on_warning),
1941 .mode = 0644,
1942 .proc_handler = proc_dointvec,
1943 },
1944 {
1945 .procname = "tracepoint_printk",
1946 .data = &tracepoint_printk,
1947 .maxlen = sizeof(tracepoint_printk),
1948 .mode = 0644,
1949 .proc_handler = tracepoint_printk_sysctl,
1950 },
1951#endif
1952#ifdef CONFIG_KEXEC_CORE
1953 {
1954 .procname = "kexec_load_disabled",
1955 .data = &kexec_load_disabled,
1956 .maxlen = sizeof(int),
1957 .mode = 0644,
1958 /* only handle a transition from default "0" to "1" */
1959 .proc_handler = proc_dointvec_minmax,
1960 .extra1 = SYSCTL_ONE,
1961 .extra2 = SYSCTL_ONE,
1962 },
1963#endif
1964#ifdef CONFIG_MODULES
1965 {
1966 .procname = "modprobe",
1967 .data = &modprobe_path,
1968 .maxlen = KMOD_PATH_LEN,
1969 .mode = 0644,
1970 .proc_handler = proc_dostring,
1971 },
1972 {
1973 .procname = "modules_disabled",
1974 .data = &modules_disabled,
1975 .maxlen = sizeof(int),
1976 .mode = 0644,
1977 /* only handle a transition from default "0" to "1" */
1978 .proc_handler = proc_dointvec_minmax,
1979 .extra1 = SYSCTL_ONE,
1980 .extra2 = SYSCTL_ONE,
1981 },
1982#endif
1983#ifdef CONFIG_UEVENT_HELPER
1984 {
1985 .procname = "hotplug",
1986 .data = &uevent_helper,
1987 .maxlen = UEVENT_HELPER_PATH_LEN,
1988 .mode = 0644,
1989 .proc_handler = proc_dostring,
1990 },
1991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992#ifdef CONFIG_BSD_PROCESS_ACCT
1993 {
1994 .procname = "acct",
1995 .data = &acct_parm,
1996 .maxlen = 3*sizeof(int),
1997 .mode = 0644,
1998 .proc_handler = proc_dointvec,
1999 },
2000#endif
2001#ifdef CONFIG_MAGIC_SYSRQ
2002 {
2003 .procname = "sysrq",
2004 .data = NULL,
2005 .maxlen = sizeof (int),
2006 .mode = 0644,
2007 .proc_handler = sysrq_sysctl_handler,
2008 },
2009#endif
2010#ifdef CONFIG_PROC_SYSCTL
2011 {
2012 .procname = "cad_pid",
2013 .data = NULL,
2014 .maxlen = sizeof (int),
2015 .mode = 0600,
2016 .proc_handler = proc_do_cad_pid,
2017 },
2018#endif
2019 {
2020 .procname = "threads-max",
2021 .data = NULL,
2022 .maxlen = sizeof(int),
2023 .mode = 0644,
2024 .proc_handler = sysctl_max_threads,
2025 },
2026 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 .procname = "usermodehelper",
2028 .mode = 0555,
2029 .child = usermodehelper_table,
2030 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 {
2032 .procname = "overflowuid",
2033 .data = &overflowuid,
2034 .maxlen = sizeof(int),
2035 .mode = 0644,
2036 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni2452dcb2022-01-21 22:11:19 -08002037 .extra1 = SYSCTL_ZERO,
Luis Chamberlain54771612022-01-21 22:13:03 -08002038 .extra2 = SYSCTL_MAXOLDUID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 },
2040 {
2041 .procname = "overflowgid",
2042 .data = &overflowgid,
2043 .maxlen = sizeof(int),
2044 .mode = 0644,
2045 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni2452dcb2022-01-21 22:11:19 -08002046 .extra1 = SYSCTL_ZERO,
Luis Chamberlain54771612022-01-21 22:13:03 -08002047 .extra2 = SYSCTL_MAXOLDUID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 },
2049#ifdef CONFIG_S390
2050 {
2051 .procname = "userprocess_debug",
2052 .data = &show_unhandled_signals,
2053 .maxlen = sizeof(int),
2054 .mode = 0644,
2055 .proc_handler = proc_dointvec,
2056 },
2057#endif
Guilherme G. Piccoli60c958d2020-06-07 21:40:48 -07002058#ifdef CONFIG_SMP
2059 {
2060 .procname = "oops_all_cpu_backtrace",
2061 .data = &sysctl_oops_all_cpu_backtrace,
2062 .maxlen = sizeof(int),
2063 .mode = 0644,
2064 .proc_handler = proc_dointvec_minmax,
2065 .extra1 = SYSCTL_ZERO,
2066 .extra2 = SYSCTL_ONE,
2067 },
2068#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 {
2070 .procname = "pid_max",
2071 .data = &pid_max,
2072 .maxlen = sizeof (int),
2073 .mode = 0644,
2074 .proc_handler = proc_dointvec_minmax,
2075 .extra1 = &pid_max_min,
2076 .extra2 = &pid_max_max,
2077 },
2078 {
2079 .procname = "panic_on_oops",
2080 .data = &panic_on_oops,
2081 .maxlen = sizeof(int),
2082 .mode = 0644,
2083 .proc_handler = proc_dointvec,
2084 },
2085 {
2086 .procname = "panic_print",
2087 .data = &panic_print,
2088 .maxlen = sizeof(unsigned long),
2089 .mode = 0644,
2090 .proc_handler = proc_doulongvec_minmax,
2091 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 {
2093 .procname = "ngroups_max",
Stephen Kittf6288672022-01-21 22:11:09 -08002094 .data = (void *)&ngroups_max,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 .maxlen = sizeof (int),
2096 .mode = 0444,
2097 .proc_handler = proc_dointvec,
2098 },
2099 {
2100 .procname = "cap_last_cap",
2101 .data = (void *)&cap_last_cap,
2102 .maxlen = sizeof(int),
2103 .mode = 0444,
2104 .proc_handler = proc_dointvec,
2105 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
2107 {
2108 .procname = "unknown_nmi_panic",
2109 .data = &unknown_nmi_panic,
2110 .maxlen = sizeof (int),
2111 .mode = 0644,
2112 .proc_handler = proc_dointvec,
2113 },
2114#endif
Xiaoming Nib6522fa2020-04-11 21:06:19 +08002115
2116#if (defined(CONFIG_X86_32) || defined(CONFIG_PARISC)) && \
2117 defined(CONFIG_DEBUG_STACKOVERFLOW)
2118 {
2119 .procname = "panic_on_stackoverflow",
2120 .data = &sysctl_panic_on_stackoverflow,
2121 .maxlen = sizeof(int),
2122 .mode = 0644,
2123 .proc_handler = proc_dointvec,
2124 },
2125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126#if defined(CONFIG_X86)
2127 {
2128 .procname = "panic_on_unrecovered_nmi",
2129 .data = &panic_on_unrecovered_nmi,
2130 .maxlen = sizeof(int),
2131 .mode = 0644,
2132 .proc_handler = proc_dointvec,
2133 },
2134 {
2135 .procname = "panic_on_io_nmi",
2136 .data = &panic_on_io_nmi,
2137 .maxlen = sizeof(int),
2138 .mode = 0644,
2139 .proc_handler = proc_dointvec,
2140 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 {
2142 .procname = "bootloader_type",
2143 .data = &bootloader_type,
2144 .maxlen = sizeof (int),
2145 .mode = 0444,
2146 .proc_handler = proc_dointvec,
2147 },
2148 {
2149 .procname = "bootloader_version",
2150 .data = &bootloader_version,
2151 .maxlen = sizeof (int),
2152 .mode = 0444,
2153 .proc_handler = proc_dointvec,
2154 },
2155 {
2156 .procname = "io_delay_type",
2157 .data = &io_delay_type,
2158 .maxlen = sizeof(int),
2159 .mode = 0644,
2160 .proc_handler = proc_dointvec,
2161 },
2162#endif
2163#if defined(CONFIG_MMU)
2164 {
2165 .procname = "randomize_va_space",
2166 .data = &randomize_va_space,
2167 .maxlen = sizeof(int),
2168 .mode = 0644,
2169 .proc_handler = proc_dointvec,
2170 },
2171#endif
2172#if defined(CONFIG_S390) && defined(CONFIG_SMP)
2173 {
2174 .procname = "spin_retry",
2175 .data = &spin_retry,
2176 .maxlen = sizeof (int),
2177 .mode = 0644,
2178 .proc_handler = proc_dointvec,
2179 },
2180#endif
2181#if defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
2182 {
2183 .procname = "acpi_video_flags",
2184 .data = &acpi_realmode_flags,
2185 .maxlen = sizeof (unsigned long),
2186 .mode = 0644,
2187 .proc_handler = proc_doulongvec_minmax,
2188 },
2189#endif
2190#ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
2191 {
2192 .procname = "ignore-unaligned-usertrap",
2193 .data = &no_unaligned_warning,
2194 .maxlen = sizeof (int),
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02002195 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 .proc_handler = proc_dointvec,
2197 },
2198#endif
2199#ifdef CONFIG_IA64
2200 {
2201 .procname = "unaligned-dump-stack",
2202 .data = &unaligned_dump_stack,
2203 .maxlen = sizeof (int),
2204 .mode = 0644,
2205 .proc_handler = proc_dointvec,
2206 },
2207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208#ifdef CONFIG_RT_MUTEXES
2209 {
2210 .procname = "max_lock_depth",
2211 .data = &max_lock_depth,
2212 .maxlen = sizeof(int),
2213 .mode = 0644,
2214 .proc_handler = proc_dointvec,
2215 },
2216#endif
2217 {
2218 .procname = "poweroff_cmd",
2219 .data = &poweroff_cmd,
2220 .maxlen = POWEROFF_CMD_PATH_LEN,
2221 .mode = 0644,
2222 .proc_handler = proc_dostring,
2223 },
2224#ifdef CONFIG_KEYS
2225 {
2226 .procname = "keys",
2227 .mode = 0555,
2228 .child = key_sysctls,
2229 },
2230#endif
2231#ifdef CONFIG_PERF_EVENTS
2232 /*
2233 * User-space scripts rely on the existence of this file
2234 * as a feature check for perf_events being enabled.
2235 *
2236 * So it's an ABI, do not remove!
2237 */
2238 {
2239 .procname = "perf_event_paranoid",
2240 .data = &sysctl_perf_event_paranoid,
2241 .maxlen = sizeof(sysctl_perf_event_paranoid),
2242 .mode = 0644,
2243 .proc_handler = proc_dointvec,
2244 },
2245 {
2246 .procname = "perf_event_mlock_kb",
2247 .data = &sysctl_perf_event_mlock,
2248 .maxlen = sizeof(sysctl_perf_event_mlock),
2249 .mode = 0644,
2250 .proc_handler = proc_dointvec,
2251 },
2252 {
2253 .procname = "perf_event_max_sample_rate",
2254 .data = &sysctl_perf_event_sample_rate,
2255 .maxlen = sizeof(sysctl_perf_event_sample_rate),
2256 .mode = 0644,
2257 .proc_handler = perf_proc_update_handler,
2258 .extra1 = SYSCTL_ONE,
2259 },
2260 {
2261 .procname = "perf_cpu_time_max_percent",
2262 .data = &sysctl_perf_cpu_time_max_percent,
2263 .maxlen = sizeof(sysctl_perf_cpu_time_max_percent),
2264 .mode = 0644,
2265 .proc_handler = perf_cpu_time_max_percent_handler,
2266 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002267 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 },
2269 {
2270 .procname = "perf_event_max_stack",
2271 .data = &sysctl_perf_event_max_stack,
2272 .maxlen = sizeof(sysctl_perf_event_max_stack),
2273 .mode = 0644,
2274 .proc_handler = perf_event_max_stack_handler,
2275 .extra1 = SYSCTL_ZERO,
Xiaoming Nid73840e2022-01-21 22:11:14 -08002276 .extra2 = (void *)&six_hundred_forty_kb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 },
2278 {
2279 .procname = "perf_event_max_contexts_per_stack",
2280 .data = &sysctl_perf_event_max_contexts_per_stack,
2281 .maxlen = sizeof(sysctl_perf_event_max_contexts_per_stack),
2282 .mode = 0644,
2283 .proc_handler = perf_event_max_stack_handler,
2284 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002285 .extra2 = SYSCTL_ONE_THOUSAND,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 },
2287#endif
2288 {
2289 .procname = "panic_on_warn",
2290 .data = &panic_on_warn,
2291 .maxlen = sizeof(int),
2292 .mode = 0644,
2293 .proc_handler = proc_dointvec_minmax,
2294 .extra1 = SYSCTL_ZERO,
2295 .extra2 = SYSCTL_ONE,
2296 },
2297#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
2298 {
2299 .procname = "timer_migration",
2300 .data = &sysctl_timer_migration,
2301 .maxlen = sizeof(unsigned int),
2302 .mode = 0644,
2303 .proc_handler = timer_migration_handler,
2304 .extra1 = SYSCTL_ZERO,
2305 .extra2 = SYSCTL_ONE,
2306 },
2307#endif
2308#ifdef CONFIG_BPF_SYSCALL
2309 {
2310 .procname = "unprivileged_bpf_disabled",
2311 .data = &sysctl_unprivileged_bpf_disabled,
2312 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
2313 .mode = 0644,
Daniel Borkmann08389d82021-05-11 22:35:17 +02002314 .proc_handler = bpf_unpriv_handler,
2315 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002316 .extra2 = SYSCTL_TWO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 },
2318 {
2319 .procname = "bpf_stats_enabled",
2320 .data = &bpf_stats_enabled_key.key,
2321 .maxlen = sizeof(bpf_stats_enabled_key),
2322 .mode = 0644,
Song Liud46edd62020-04-30 00:15:04 -07002323 .proc_handler = bpf_stats_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 },
2325#endif
2326#if defined(CONFIG_TREE_RCU)
2327 {
2328 .procname = "panic_on_rcu_stall",
2329 .data = &sysctl_panic_on_rcu_stall,
2330 .maxlen = sizeof(sysctl_panic_on_rcu_stall),
2331 .mode = 0644,
2332 .proc_handler = proc_dointvec_minmax,
2333 .extra1 = SYSCTL_ZERO,
2334 .extra2 = SYSCTL_ONE,
2335 },
2336#endif
chaodfe56402020-08-30 23:41:17 -07002337#if defined(CONFIG_TREE_RCU)
2338 {
2339 .procname = "max_rcu_stall_to_panic",
2340 .data = &sysctl_max_rcu_stall_to_panic,
2341 .maxlen = sizeof(sysctl_max_rcu_stall_to_panic),
2342 .mode = 0644,
2343 .proc_handler = proc_dointvec_minmax,
2344 .extra1 = SYSCTL_ONE,
2345 .extra2 = SYSCTL_INT_MAX,
2346 },
2347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 { }
2349};
2350
2351static struct ctl_table vm_table[] = {
2352 {
2353 .procname = "overcommit_memory",
2354 .data = &sysctl_overcommit_memory,
2355 .maxlen = sizeof(sysctl_overcommit_memory),
2356 .mode = 0644,
Feng Tang56f35472020-08-06 23:23:15 -07002357 .proc_handler = overcommit_policy_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002359 .extra2 = SYSCTL_TWO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 },
2361 {
2362 .procname = "panic_on_oom",
2363 .data = &sysctl_panic_on_oom,
2364 .maxlen = sizeof(sysctl_panic_on_oom),
2365 .mode = 0644,
2366 .proc_handler = proc_dointvec_minmax,
2367 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002368 .extra2 = SYSCTL_TWO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 },
2370 {
2371 .procname = "oom_kill_allocating_task",
2372 .data = &sysctl_oom_kill_allocating_task,
2373 .maxlen = sizeof(sysctl_oom_kill_allocating_task),
2374 .mode = 0644,
2375 .proc_handler = proc_dointvec,
2376 },
2377 {
2378 .procname = "oom_dump_tasks",
2379 .data = &sysctl_oom_dump_tasks,
2380 .maxlen = sizeof(sysctl_oom_dump_tasks),
2381 .mode = 0644,
2382 .proc_handler = proc_dointvec,
2383 },
2384 {
2385 .procname = "overcommit_ratio",
2386 .data = &sysctl_overcommit_ratio,
2387 .maxlen = sizeof(sysctl_overcommit_ratio),
2388 .mode = 0644,
2389 .proc_handler = overcommit_ratio_handler,
2390 },
2391 {
2392 .procname = "overcommit_kbytes",
2393 .data = &sysctl_overcommit_kbytes,
2394 .maxlen = sizeof(sysctl_overcommit_kbytes),
2395 .mode = 0644,
2396 .proc_handler = overcommit_kbytes_handler,
2397 },
2398 {
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02002399 .procname = "page-cluster",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .data = &page_cluster,
2401 .maxlen = sizeof(int),
2402 .mode = 0644,
2403 .proc_handler = proc_dointvec_minmax,
2404 .extra1 = SYSCTL_ZERO,
2405 },
2406 {
2407 .procname = "dirty_background_ratio",
2408 .data = &dirty_background_ratio,
2409 .maxlen = sizeof(dirty_background_ratio),
2410 .mode = 0644,
2411 .proc_handler = dirty_background_ratio_handler,
2412 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002413 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 },
2415 {
2416 .procname = "dirty_background_bytes",
2417 .data = &dirty_background_bytes,
2418 .maxlen = sizeof(dirty_background_bytes),
2419 .mode = 0644,
2420 .proc_handler = dirty_background_bytes_handler,
Luis Chamberlainb1f2aff2022-01-21 22:12:48 -08002421 .extra1 = SYSCTL_LONG_ONE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 },
2423 {
2424 .procname = "dirty_ratio",
2425 .data = &vm_dirty_ratio,
2426 .maxlen = sizeof(vm_dirty_ratio),
2427 .mode = 0644,
2428 .proc_handler = dirty_ratio_handler,
2429 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002430 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 },
2432 {
2433 .procname = "dirty_bytes",
2434 .data = &vm_dirty_bytes,
2435 .maxlen = sizeof(vm_dirty_bytes),
2436 .mode = 0644,
2437 .proc_handler = dirty_bytes_handler,
Xiaoming Nid73840e2022-01-21 22:11:14 -08002438 .extra1 = (void *)&dirty_bytes_min,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 },
2440 {
2441 .procname = "dirty_writeback_centisecs",
2442 .data = &dirty_writeback_interval,
2443 .maxlen = sizeof(dirty_writeback_interval),
2444 .mode = 0644,
2445 .proc_handler = dirty_writeback_centisecs_handler,
2446 },
2447 {
2448 .procname = "dirty_expire_centisecs",
2449 .data = &dirty_expire_interval,
2450 .maxlen = sizeof(dirty_expire_interval),
2451 .mode = 0644,
2452 .proc_handler = proc_dointvec_minmax,
2453 .extra1 = SYSCTL_ZERO,
2454 },
2455 {
2456 .procname = "dirtytime_expire_seconds",
2457 .data = &dirtytime_expire_interval,
2458 .maxlen = sizeof(dirtytime_expire_interval),
2459 .mode = 0644,
2460 .proc_handler = dirtytime_interval_handler,
2461 .extra1 = SYSCTL_ZERO,
2462 },
2463 {
2464 .procname = "swappiness",
2465 .data = &vm_swappiness,
2466 .maxlen = sizeof(vm_swappiness),
2467 .mode = 0644,
2468 .proc_handler = proc_dointvec_minmax,
2469 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002470 .extra2 = SYSCTL_TWO_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 },
2472#ifdef CONFIG_HUGETLB_PAGE
2473 {
2474 .procname = "nr_hugepages",
2475 .data = NULL,
2476 .maxlen = sizeof(unsigned long),
2477 .mode = 0644,
2478 .proc_handler = hugetlb_sysctl_handler,
2479 },
2480#ifdef CONFIG_NUMA
2481 {
2482 .procname = "nr_hugepages_mempolicy",
2483 .data = NULL,
2484 .maxlen = sizeof(unsigned long),
2485 .mode = 0644,
2486 .proc_handler = &hugetlb_mempolicy_sysctl_handler,
2487 },
2488 {
2489 .procname = "numa_stat",
2490 .data = &sysctl_vm_numa_stat,
2491 .maxlen = sizeof(int),
2492 .mode = 0644,
2493 .proc_handler = sysctl_vm_numa_stat_handler,
2494 .extra1 = SYSCTL_ZERO,
2495 .extra2 = SYSCTL_ONE,
2496 },
2497#endif
2498 {
2499 .procname = "hugetlb_shm_group",
2500 .data = &sysctl_hugetlb_shm_group,
2501 .maxlen = sizeof(gid_t),
2502 .mode = 0644,
2503 .proc_handler = proc_dointvec,
2504 },
2505 {
2506 .procname = "nr_overcommit_hugepages",
2507 .data = NULL,
2508 .maxlen = sizeof(unsigned long),
2509 .mode = 0644,
2510 .proc_handler = hugetlb_overcommit_handler,
2511 },
2512#endif
2513 {
2514 .procname = "lowmem_reserve_ratio",
2515 .data = &sysctl_lowmem_reserve_ratio,
2516 .maxlen = sizeof(sysctl_lowmem_reserve_ratio),
2517 .mode = 0644,
2518 .proc_handler = lowmem_reserve_ratio_sysctl_handler,
2519 },
2520 {
2521 .procname = "drop_caches",
2522 .data = &sysctl_drop_caches,
2523 .maxlen = sizeof(int),
2524 .mode = 0200,
2525 .proc_handler = drop_caches_sysctl_handler,
2526 .extra1 = SYSCTL_ONE,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002527 .extra2 = SYSCTL_FOUR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 },
2529#ifdef CONFIG_COMPACTION
2530 {
2531 .procname = "compact_memory",
Pintu Kumaref498432021-05-04 18:36:48 -07002532 .data = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .maxlen = sizeof(int),
2534 .mode = 0200,
2535 .proc_handler = sysctl_compaction_handler,
2536 },
2537 {
Nitin Guptafacdaa92020-08-11 18:31:00 -07002538 .procname = "compaction_proactiveness",
2539 .data = &sysctl_compaction_proactiveness,
Nitin Guptad34c0a72020-08-11 18:31:07 -07002540 .maxlen = sizeof(sysctl_compaction_proactiveness),
Nitin Guptafacdaa92020-08-11 18:31:00 -07002541 .mode = 0644,
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07002542 .proc_handler = compaction_proactiveness_sysctl_handler,
Nitin Guptafacdaa92020-08-11 18:31:00 -07002543 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002544 .extra2 = SYSCTL_ONE_HUNDRED,
Nitin Guptafacdaa92020-08-11 18:31:00 -07002545 },
2546 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 .procname = "extfrag_threshold",
2548 .data = &sysctl_extfrag_threshold,
2549 .maxlen = sizeof(int),
2550 .mode = 0644,
2551 .proc_handler = proc_dointvec_minmax,
Xiaoming Ni2452dcb2022-01-21 22:11:19 -08002552 .extra1 = SYSCTL_ZERO,
Xiaoming Nid73840e2022-01-21 22:11:14 -08002553 .extra2 = (void *)&max_extfrag_threshold,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 },
2555 {
2556 .procname = "compact_unevictable_allowed",
2557 .data = &sysctl_compact_unevictable_allowed,
2558 .maxlen = sizeof(int),
2559 .mode = 0644,
2560 .proc_handler = proc_dointvec_minmax_warn_RT_change,
2561 .extra1 = SYSCTL_ZERO,
2562 .extra2 = SYSCTL_ONE,
2563 },
2564
2565#endif /* CONFIG_COMPACTION */
2566 {
2567 .procname = "min_free_kbytes",
2568 .data = &min_free_kbytes,
2569 .maxlen = sizeof(min_free_kbytes),
2570 .mode = 0644,
2571 .proc_handler = min_free_kbytes_sysctl_handler,
2572 .extra1 = SYSCTL_ZERO,
2573 },
2574 {
2575 .procname = "watermark_boost_factor",
2576 .data = &watermark_boost_factor,
2577 .maxlen = sizeof(watermark_boost_factor),
2578 .mode = 0644,
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02002579 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 .extra1 = SYSCTL_ZERO,
2581 },
2582 {
2583 .procname = "watermark_scale_factor",
2584 .data = &watermark_scale_factor,
2585 .maxlen = sizeof(watermark_scale_factor),
2586 .mode = 0644,
2587 .proc_handler = watermark_scale_factor_sysctl_handler,
2588 .extra1 = SYSCTL_ONE,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002589 .extra2 = SYSCTL_THREE_THOUSAND,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 },
2591 {
Mel Gorman74f44822021-06-28 19:42:24 -07002592 .procname = "percpu_pagelist_high_fraction",
2593 .data = &percpu_pagelist_high_fraction,
2594 .maxlen = sizeof(percpu_pagelist_high_fraction),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 .mode = 0644,
Mel Gorman74f44822021-06-28 19:42:24 -07002596 .proc_handler = percpu_pagelist_high_fraction_sysctl_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 .extra1 = SYSCTL_ZERO,
2598 },
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07002599 {
2600 .procname = "page_lock_unfairness",
2601 .data = &sysctl_page_lock_unfairness,
2602 .maxlen = sizeof(sysctl_page_lock_unfairness),
2603 .mode = 0644,
2604 .proc_handler = proc_dointvec_minmax,
2605 .extra1 = SYSCTL_ZERO,
2606 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607#ifdef CONFIG_MMU
2608 {
2609 .procname = "max_map_count",
2610 .data = &sysctl_max_map_count,
2611 .maxlen = sizeof(sysctl_max_map_count),
2612 .mode = 0644,
2613 .proc_handler = proc_dointvec_minmax,
2614 .extra1 = SYSCTL_ZERO,
2615 },
2616#else
2617 {
2618 .procname = "nr_trim_pages",
2619 .data = &sysctl_nr_trim_pages,
2620 .maxlen = sizeof(sysctl_nr_trim_pages),
2621 .mode = 0644,
2622 .proc_handler = proc_dointvec_minmax,
2623 .extra1 = SYSCTL_ZERO,
2624 },
2625#endif
2626 {
2627 .procname = "laptop_mode",
2628 .data = &laptop_mode,
2629 .maxlen = sizeof(laptop_mode),
2630 .mode = 0644,
2631 .proc_handler = proc_dointvec_jiffies,
2632 },
2633 {
2634 .procname = "vfs_cache_pressure",
2635 .data = &sysctl_vfs_cache_pressure,
2636 .maxlen = sizeof(sysctl_vfs_cache_pressure),
2637 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002638 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 .extra1 = SYSCTL_ZERO,
2640 },
2641#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
2642 defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
2643 {
2644 .procname = "legacy_va_layout",
2645 .data = &sysctl_legacy_va_layout,
2646 .maxlen = sizeof(sysctl_legacy_va_layout),
2647 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002648 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 .extra1 = SYSCTL_ZERO,
2650 },
2651#endif
2652#ifdef CONFIG_NUMA
2653 {
2654 .procname = "zone_reclaim_mode",
2655 .data = &node_reclaim_mode,
2656 .maxlen = sizeof(node_reclaim_mode),
2657 .mode = 0644,
Lin Feng3b3376f2021-02-25 17:20:53 -08002658 .proc_handler = proc_dointvec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .extra1 = SYSCTL_ZERO,
2660 },
2661 {
2662 .procname = "min_unmapped_ratio",
2663 .data = &sysctl_min_unmapped_ratio,
2664 .maxlen = sizeof(sysctl_min_unmapped_ratio),
2665 .mode = 0644,
2666 .proc_handler = sysctl_min_unmapped_ratio_sysctl_handler,
2667 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002668 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 },
2670 {
2671 .procname = "min_slab_ratio",
2672 .data = &sysctl_min_slab_ratio,
2673 .maxlen = sizeof(sysctl_min_slab_ratio),
2674 .mode = 0644,
2675 .proc_handler = sysctl_min_slab_ratio_sysctl_handler,
2676 .extra1 = SYSCTL_ZERO,
Xiaoming Ni78e36f32022-01-21 22:10:55 -08002677 .extra2 = SYSCTL_ONE_HUNDRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 },
2679#endif
2680#ifdef CONFIG_SMP
2681 {
2682 .procname = "stat_interval",
2683 .data = &sysctl_stat_interval,
2684 .maxlen = sizeof(sysctl_stat_interval),
2685 .mode = 0644,
2686 .proc_handler = proc_dointvec_jiffies,
2687 },
2688 {
2689 .procname = "stat_refresh",
2690 .data = NULL,
2691 .maxlen = 0,
2692 .mode = 0600,
2693 .proc_handler = vmstat_refresh,
2694 },
2695#endif
2696#ifdef CONFIG_MMU
2697 {
2698 .procname = "mmap_min_addr",
2699 .data = &dac_mmap_min_addr,
2700 .maxlen = sizeof(unsigned long),
2701 .mode = 0644,
2702 .proc_handler = mmap_min_addr_handler,
2703 },
2704#endif
2705#ifdef CONFIG_NUMA
2706 {
2707 .procname = "numa_zonelist_order",
2708 .data = &numa_zonelist_order,
2709 .maxlen = NUMA_ZONELIST_ORDER_LEN,
2710 .mode = 0644,
2711 .proc_handler = numa_zonelist_order_handler,
2712 },
2713#endif
2714#if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
2715 (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
2716 {
2717 .procname = "vdso_enabled",
2718#ifdef CONFIG_X86_32
2719 .data = &vdso32_enabled,
2720 .maxlen = sizeof(vdso32_enabled),
2721#else
2722 .data = &vdso_enabled,
2723 .maxlen = sizeof(vdso_enabled),
2724#endif
2725 .mode = 0644,
2726 .proc_handler = proc_dointvec,
2727 .extra1 = SYSCTL_ZERO,
2728 },
2729#endif
2730#ifdef CONFIG_HIGHMEM
2731 {
2732 .procname = "highmem_is_dirtyable",
2733 .data = &vm_highmem_is_dirtyable,
2734 .maxlen = sizeof(vm_highmem_is_dirtyable),
2735 .mode = 0644,
2736 .proc_handler = proc_dointvec_minmax,
2737 .extra1 = SYSCTL_ZERO,
2738 .extra2 = SYSCTL_ONE,
2739 },
2740#endif
2741#ifdef CONFIG_MEMORY_FAILURE
2742 {
2743 .procname = "memory_failure_early_kill",
2744 .data = &sysctl_memory_failure_early_kill,
2745 .maxlen = sizeof(sysctl_memory_failure_early_kill),
2746 .mode = 0644,
2747 .proc_handler = proc_dointvec_minmax,
2748 .extra1 = SYSCTL_ZERO,
2749 .extra2 = SYSCTL_ONE,
2750 },
2751 {
2752 .procname = "memory_failure_recovery",
2753 .data = &sysctl_memory_failure_recovery,
2754 .maxlen = sizeof(sysctl_memory_failure_recovery),
2755 .mode = 0644,
2756 .proc_handler = proc_dointvec_minmax,
2757 .extra1 = SYSCTL_ZERO,
2758 .extra2 = SYSCTL_ONE,
2759 },
2760#endif
2761 {
2762 .procname = "user_reserve_kbytes",
2763 .data = &sysctl_user_reserve_kbytes,
2764 .maxlen = sizeof(sysctl_user_reserve_kbytes),
2765 .mode = 0644,
2766 .proc_handler = proc_doulongvec_minmax,
2767 },
2768 {
2769 .procname = "admin_reserve_kbytes",
2770 .data = &sysctl_admin_reserve_kbytes,
2771 .maxlen = sizeof(sysctl_admin_reserve_kbytes),
2772 .mode = 0644,
2773 .proc_handler = proc_doulongvec_minmax,
2774 },
2775#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
2776 {
2777 .procname = "mmap_rnd_bits",
2778 .data = &mmap_rnd_bits,
2779 .maxlen = sizeof(mmap_rnd_bits),
2780 .mode = 0600,
2781 .proc_handler = proc_dointvec_minmax,
2782 .extra1 = (void *)&mmap_rnd_bits_min,
2783 .extra2 = (void *)&mmap_rnd_bits_max,
2784 },
2785#endif
2786#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
2787 {
2788 .procname = "mmap_rnd_compat_bits",
2789 .data = &mmap_rnd_compat_bits,
2790 .maxlen = sizeof(mmap_rnd_compat_bits),
2791 .mode = 0600,
2792 .proc_handler = proc_dointvec_minmax,
2793 .extra1 = (void *)&mmap_rnd_compat_bits_min,
2794 .extra2 = (void *)&mmap_rnd_compat_bits_max,
2795 },
2796#endif
2797#ifdef CONFIG_USERFAULTFD
2798 {
2799 .procname = "unprivileged_userfaultfd",
2800 .data = &sysctl_unprivileged_userfaultfd,
2801 .maxlen = sizeof(sysctl_unprivileged_userfaultfd),
2802 .mode = 0644,
2803 .proc_handler = proc_dointvec_minmax,
2804 .extra1 = SYSCTL_ZERO,
2805 .extra2 = SYSCTL_ONE,
2806 },
2807#endif
2808 { }
2809};
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static struct ctl_table debug_table[] = {
2812#ifdef CONFIG_SYSCTL_EXCEPTION_TRACE
2813 {
2814 .procname = "exception-trace",
2815 .data = &show_unhandled_signals,
2816 .maxlen = sizeof(int),
2817 .mode = 0644,
2818 .proc_handler = proc_dointvec
2819 },
2820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 { }
2822};
2823
2824static struct ctl_table dev_table[] = {
2825 { }
2826};
2827
Luis Chamberlain51cb8df2022-01-21 22:13:24 -08002828DECLARE_SYSCTL_BASE(kernel, kern_table);
2829DECLARE_SYSCTL_BASE(vm, vm_table);
Luis Chamberlain51cb8df2022-01-21 22:13:24 -08002830DECLARE_SYSCTL_BASE(debug, debug_table);
2831DECLARE_SYSCTL_BASE(dev, dev_table);
Christoph Hellwigf461d2d2020-04-24 08:43:37 +02002832
Luis Chamberlaind8c04182022-01-21 22:13:31 -08002833int __init sysctl_init_bases(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Luis Chamberlain51cb8df2022-01-21 22:13:24 -08002835 register_sysctl_base(kernel);
2836 register_sysctl_base(vm);
Luis Chamberlain51cb8df2022-01-21 22:13:24 -08002837 register_sysctl_base(debug);
2838 register_sysctl_base(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return 0;
2841}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843/*
2844 * No sense putting this after each symbol definition, twice,
2845 * exception granted :-)
2846 */
Jia Hea2071572021-08-03 12:59:36 +02002847EXPORT_SYMBOL(proc_dobool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848EXPORT_SYMBOL(proc_dointvec);
Subash Abhinov Kasiviswanathane7d316a2016-08-25 15:16:51 -07002849EXPORT_SYMBOL(proc_douintvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850EXPORT_SYMBOL(proc_dointvec_jiffies);
2851EXPORT_SYMBOL(proc_dointvec_minmax);
Luis R. Rodriguez61d9b562017-07-12 14:33:40 -07002852EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2854EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2855EXPORT_SYMBOL(proc_dostring);
2856EXPORT_SYMBOL(proc_doulongvec_minmax);
2857EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
Stephen Suryaputra0bc19982019-04-17 16:35:49 -04002858EXPORT_SYMBOL(proc_do_large_bitmap);