GNU Linux-libre 4.19.245-gnu1
[releases.git] / kernel / sysctl.c
1 /*
2  * sysctl.c: General linux system control interface
3  *
4  * Begun 24 March 1995, Stephen Tweedie
5  * Added /proc support, Dec 1995
6  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9  * Dynamic registration fixes, Stephen Tweedie.
10  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12  *  Horn.
13  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16  *  Wendling.
17  * The list_for_each() macro wasn't appropriate for the sysctl loop.
18  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
19  */
20
21 #include <linux/module.h>
22 #include <linux/aio.h>
23 #include <linux/mm.h>
24 #include <linux/swap.h>
25 #include <linux/slab.h>
26 #include <linux/sysctl.h>
27 #include <linux/bitmap.h>
28 #include <linux/signal.h>
29 #include <linux/printk.h>
30 #include <linux/proc_fs.h>
31 #include <linux/security.h>
32 #include <linux/ctype.h>
33 #include <linux/kmemleak.h>
34 #include <linux/fs.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/kobject.h>
38 #include <linux/net.h>
39 #include <linux/sysrq.h>
40 #include <linux/highuid.h>
41 #include <linux/writeback.h>
42 #include <linux/ratelimit.h>
43 #include <linux/compaction.h>
44 #include <linux/hugetlb.h>
45 #include <linux/initrd.h>
46 #include <linux/key.h>
47 #include <linux/times.h>
48 #include <linux/limits.h>
49 #include <linux/dcache.h>
50 #include <linux/dnotify.h>
51 #include <linux/syscalls.h>
52 #include <linux/vmstat.h>
53 #include <linux/nfs_fs.h>
54 #include <linux/acpi.h>
55 #include <linux/reboot.h>
56 #include <linux/ftrace.h>
57 #include <linux/perf_event.h>
58 #include <linux/kprobes.h>
59 #include <linux/pipe_fs_i.h>
60 #include <linux/oom.h>
61 #include <linux/kmod.h>
62 #include <linux/capability.h>
63 #include <linux/binfmts.h>
64 #include <linux/sched/sysctl.h>
65 #include <linux/sched/coredump.h>
66 #include <linux/kexec.h>
67 #include <linux/bpf.h>
68 #include <linux/mount.h>
69 #include <linux/pipe_fs_i.h>
70
71 #include "../lib/kstrtox.h"
72
73 #include <linux/uaccess.h>
74 #include <asm/processor.h>
75
76 #ifdef CONFIG_X86
77 #include <asm/nmi.h>
78 #include <asm/stacktrace.h>
79 #include <asm/io.h>
80 #endif
81 #ifdef CONFIG_SPARC
82 #include <asm/setup.h>
83 #endif
84 #ifdef CONFIG_BSD_PROCESS_ACCT
85 #include <linux/acct.h>
86 #endif
87 #ifdef CONFIG_RT_MUTEXES
88 #include <linux/rtmutex.h>
89 #endif
90 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_LOCK_STAT)
91 #include <linux/lockdep.h>
92 #endif
93 #ifdef CONFIG_CHR_DEV_SG
94 #include <scsi/sg.h>
95 #endif
96
97 #ifdef CONFIG_LOCKUP_DETECTOR
98 #include <linux/nmi.h>
99 #endif
100
101 #if defined(CONFIG_SYSCTL)
102
103 /* External variables not in a header file. */
104 extern int suid_dumpable;
105 #ifdef CONFIG_COREDUMP
106 extern int core_uses_pid;
107 extern char core_pattern[];
108 extern unsigned int core_pipe_limit;
109 #endif
110 extern int pid_max;
111 extern int pid_max_min, pid_max_max;
112 extern int percpu_pagelist_fraction;
113 extern int latencytop_enabled;
114 extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
115 #ifndef CONFIG_MMU
116 extern int sysctl_nr_trim_pages;
117 #endif
118
119 /* Constants used for minimum and  maximum */
120 #ifdef CONFIG_LOCKUP_DETECTOR
121 static int sixty = 60;
122 #endif
123
124 static int __maybe_unused neg_one = -1;
125
126 static int zero;
127 static int __maybe_unused one = 1;
128 static int __maybe_unused two = 2;
129 static int __maybe_unused four = 4;
130 static unsigned long zero_ul;
131 static unsigned long one_ul = 1;
132 static unsigned long long_max = LONG_MAX;
133 static int one_hundred = 100;
134 static int one_thousand = 1000;
135 #ifdef CONFIG_PRINTK
136 static int ten_thousand = 10000;
137 #endif
138 #ifdef CONFIG_PERF_EVENTS
139 static int six_hundred_forty_kb = 640 * 1024;
140 #endif
141
142 /* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
143 static unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
144
145 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
146 static int maxolduid = 65535;
147 static int minolduid;
148
149 static int ngroups_max = NGROUPS_MAX;
150 static const int cap_last_cap = CAP_LAST_CAP;
151
152 /*
153  * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
154  * and hung_task_check_interval_secs
155  */
156 #ifdef CONFIG_DETECT_HUNG_TASK
157 static unsigned long hung_task_timeout_max = (LONG_MAX/HZ);
158 #endif
159
160 #ifdef CONFIG_INOTIFY_USER
161 #include <linux/inotify.h>
162 #endif
163 #ifdef CONFIG_SPARC
164 #endif
165
166 #ifdef __hppa__
167 extern int pwrsw_enabled;
168 #endif
169
170 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
171 extern int unaligned_enabled;
172 #endif
173
174 #ifdef CONFIG_IA64
175 extern int unaligned_dump_stack;
176 #endif
177
178 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
179 extern int no_unaligned_warning;
180 #endif
181
182 #ifdef CONFIG_PROC_SYSCTL
183
184 /**
185  * enum sysctl_writes_mode - supported sysctl write modes
186  *
187  * @SYSCTL_WRITES_LEGACY: each write syscall must fully contain the sysctl value
188  *      to be written, and multiple writes on the same sysctl file descriptor
189  *      will rewrite the sysctl value, regardless of file position. No warning
190  *      is issued when the initial position is not 0.
191  * @SYSCTL_WRITES_WARN: same as above but warn when the initial file position is
192  *      not 0.
193  * @SYSCTL_WRITES_STRICT: writes to numeric sysctl entries must always be at
194  *      file position 0 and the value must be fully contained in the buffer
195  *      sent to the write syscall. If dealing with strings respect the file
196  *      position, but restrict this to the max length of the buffer, anything
197  *      passed the max lenght will be ignored. Multiple writes will append
198  *      to the buffer.
199  *
200  * These write modes control how current file position affects the behavior of
201  * updating sysctl values through the proc interface on each write.
202  */
203 enum sysctl_writes_mode {
204         SYSCTL_WRITES_LEGACY            = -1,
205         SYSCTL_WRITES_WARN              = 0,
206         SYSCTL_WRITES_STRICT            = 1,
207 };
208
209 static enum sysctl_writes_mode sysctl_writes_strict = SYSCTL_WRITES_STRICT;
210
211 static int proc_do_cad_pid(struct ctl_table *table, int write,
212                   void __user *buffer, size_t *lenp, loff_t *ppos);
213 static int proc_taint(struct ctl_table *table, int write,
214                                void __user *buffer, size_t *lenp, loff_t *ppos);
215 #endif
216
217 #ifdef CONFIG_PRINTK
218 static int proc_dointvec_minmax_sysadmin(struct ctl_table *table, int write,
219                                 void __user *buffer, size_t *lenp, loff_t *ppos);
220 #endif
221
222 static int proc_dointvec_minmax_coredump(struct ctl_table *table, int write,
223                 void __user *buffer, size_t *lenp, loff_t *ppos);
224 #ifdef CONFIG_COREDUMP
225 static int proc_dostring_coredump(struct ctl_table *table, int write,
226                 void __user *buffer, size_t *lenp, loff_t *ppos);
227 #endif
228 static int proc_dopipe_max_size(struct ctl_table *table, int write,
229                 void __user *buffer, size_t *lenp, loff_t *ppos);
230
231 #ifdef CONFIG_MAGIC_SYSRQ
232 /* Note: sysrq code uses its own private copy */
233 static int __sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE;
234
235 static int sysrq_sysctl_handler(struct ctl_table *table, int write,
236                                 void __user *buffer, size_t *lenp,
237                                 loff_t *ppos)
238 {
239         int error;
240
241         error = proc_dointvec(table, write, buffer, lenp, ppos);
242         if (error)
243                 return error;
244
245         if (write)
246                 sysrq_toggle_support(__sysrq_enabled);
247
248         return 0;
249 }
250
251 #endif
252
253 #ifdef CONFIG_BPF_SYSCALL
254
255 void __weak unpriv_ebpf_notify(int new_state)
256 {
257 }
258
259 static int bpf_unpriv_handler(struct ctl_table *table, int write,
260                              void *buffer, size_t *lenp, loff_t *ppos)
261 {
262         int ret, unpriv_enable = *(int *)table->data;
263         bool locked_state = unpriv_enable == 1;
264         struct ctl_table tmp = *table;
265
266         if (write && !capable(CAP_SYS_ADMIN))
267                 return -EPERM;
268
269         tmp.data = &unpriv_enable;
270         ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
271         if (write && !ret) {
272                 if (locked_state && unpriv_enable != 1)
273                         return -EPERM;
274                 *(int *)table->data = unpriv_enable;
275         }
276
277         unpriv_ebpf_notify(unpriv_enable);
278
279         return ret;
280 }
281 #endif
282
283 static struct ctl_table kern_table[];
284 static struct ctl_table vm_table[];
285 static struct ctl_table fs_table[];
286 static struct ctl_table debug_table[];
287 static struct ctl_table dev_table[];
288 extern struct ctl_table random_table[];
289 #ifdef CONFIG_EPOLL
290 extern struct ctl_table epoll_table[];
291 #endif
292
293 #ifdef CONFIG_FW_LOADER_USER_HELPER
294 extern struct ctl_table firmware_config_table[];
295 #endif
296
297 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
298 int sysctl_legacy_va_layout;
299 #endif
300
301 /* The default sysctl tables: */
302
303 static struct ctl_table sysctl_base_table[] = {
304         {
305                 .procname       = "kernel",
306                 .mode           = 0555,
307                 .child          = kern_table,
308         },
309         {
310                 .procname       = "vm",
311                 .mode           = 0555,
312                 .child          = vm_table,
313         },
314         {
315                 .procname       = "fs",
316                 .mode           = 0555,
317                 .child          = fs_table,
318         },
319         {
320                 .procname       = "debug",
321                 .mode           = 0555,
322                 .child          = debug_table,
323         },
324         {
325                 .procname       = "dev",
326                 .mode           = 0555,
327                 .child          = dev_table,
328         },
329         { }
330 };
331
332 #ifdef CONFIG_SCHED_DEBUG
333 static int min_sched_granularity_ns = 100000;           /* 100 usecs */
334 static int max_sched_granularity_ns = NSEC_PER_SEC;     /* 1 second */
335 static int min_wakeup_granularity_ns;                   /* 0 usecs */
336 static int max_wakeup_granularity_ns = NSEC_PER_SEC;    /* 1 second */
337 #ifdef CONFIG_SMP
338 static int min_sched_tunable_scaling = SCHED_TUNABLESCALING_NONE;
339 static int max_sched_tunable_scaling = SCHED_TUNABLESCALING_END-1;
340 #endif /* CONFIG_SMP */
341 #endif /* CONFIG_SCHED_DEBUG */
342
343 #ifdef CONFIG_COMPACTION
344 static int min_extfrag_threshold;
345 static int max_extfrag_threshold = 1000;
346 #endif
347
348 static struct ctl_table kern_table[] = {
349         {
350                 .procname       = "sched_child_runs_first",
351                 .data           = &sysctl_sched_child_runs_first,
352                 .maxlen         = sizeof(unsigned int),
353                 .mode           = 0644,
354                 .proc_handler   = proc_dointvec,
355         },
356 #ifdef CONFIG_SCHED_DEBUG
357         {
358                 .procname       = "sched_min_granularity_ns",
359                 .data           = &sysctl_sched_min_granularity,
360                 .maxlen         = sizeof(unsigned int),
361                 .mode           = 0644,
362                 .proc_handler   = sched_proc_update_handler,
363                 .extra1         = &min_sched_granularity_ns,
364                 .extra2         = &max_sched_granularity_ns,
365         },
366         {
367                 .procname       = "sched_latency_ns",
368                 .data           = &sysctl_sched_latency,
369                 .maxlen         = sizeof(unsigned int),
370                 .mode           = 0644,
371                 .proc_handler   = sched_proc_update_handler,
372                 .extra1         = &min_sched_granularity_ns,
373                 .extra2         = &max_sched_granularity_ns,
374         },
375         {
376                 .procname       = "sched_wakeup_granularity_ns",
377                 .data           = &sysctl_sched_wakeup_granularity,
378                 .maxlen         = sizeof(unsigned int),
379                 .mode           = 0644,
380                 .proc_handler   = sched_proc_update_handler,
381                 .extra1         = &min_wakeup_granularity_ns,
382                 .extra2         = &max_wakeup_granularity_ns,
383         },
384 #ifdef CONFIG_SMP
385         {
386                 .procname       = "sched_tunable_scaling",
387                 .data           = &sysctl_sched_tunable_scaling,
388                 .maxlen         = sizeof(enum sched_tunable_scaling),
389                 .mode           = 0644,
390                 .proc_handler   = sched_proc_update_handler,
391                 .extra1         = &min_sched_tunable_scaling,
392                 .extra2         = &max_sched_tunable_scaling,
393         },
394         {
395                 .procname       = "sched_migration_cost_ns",
396                 .data           = &sysctl_sched_migration_cost,
397                 .maxlen         = sizeof(unsigned int),
398                 .mode           = 0644,
399                 .proc_handler   = proc_dointvec,
400         },
401         {
402                 .procname       = "sched_nr_migrate",
403                 .data           = &sysctl_sched_nr_migrate,
404                 .maxlen         = sizeof(unsigned int),
405                 .mode           = 0644,
406                 .proc_handler   = proc_dointvec,
407         },
408 #ifdef CONFIG_SCHEDSTATS
409         {
410                 .procname       = "sched_schedstats",
411                 .data           = NULL,
412                 .maxlen         = sizeof(unsigned int),
413                 .mode           = 0644,
414                 .proc_handler   = sysctl_schedstats,
415                 .extra1         = &zero,
416                 .extra2         = &one,
417         },
418 #endif /* CONFIG_SCHEDSTATS */
419 #endif /* CONFIG_SMP */
420 #ifdef CONFIG_NUMA_BALANCING
421         {
422                 .procname       = "numa_balancing_scan_delay_ms",
423                 .data           = &sysctl_numa_balancing_scan_delay,
424                 .maxlen         = sizeof(unsigned int),
425                 .mode           = 0644,
426                 .proc_handler   = proc_dointvec,
427         },
428         {
429                 .procname       = "numa_balancing_scan_period_min_ms",
430                 .data           = &sysctl_numa_balancing_scan_period_min,
431                 .maxlen         = sizeof(unsigned int),
432                 .mode           = 0644,
433                 .proc_handler   = proc_dointvec,
434         },
435         {
436                 .procname       = "numa_balancing_scan_period_max_ms",
437                 .data           = &sysctl_numa_balancing_scan_period_max,
438                 .maxlen         = sizeof(unsigned int),
439                 .mode           = 0644,
440                 .proc_handler   = proc_dointvec,
441         },
442         {
443                 .procname       = "numa_balancing_scan_size_mb",
444                 .data           = &sysctl_numa_balancing_scan_size,
445                 .maxlen         = sizeof(unsigned int),
446                 .mode           = 0644,
447                 .proc_handler   = proc_dointvec_minmax,
448                 .extra1         = &one,
449         },
450         {
451                 .procname       = "numa_balancing",
452                 .data           = NULL, /* filled in by handler */
453                 .maxlen         = sizeof(unsigned int),
454                 .mode           = 0644,
455                 .proc_handler   = sysctl_numa_balancing,
456                 .extra1         = &zero,
457                 .extra2         = &one,
458         },
459 #endif /* CONFIG_NUMA_BALANCING */
460 #endif /* CONFIG_SCHED_DEBUG */
461         {
462                 .procname       = "sched_rt_period_us",
463                 .data           = &sysctl_sched_rt_period,
464                 .maxlen         = sizeof(unsigned int),
465                 .mode           = 0644,
466                 .proc_handler   = sched_rt_handler,
467         },
468         {
469                 .procname       = "sched_rt_runtime_us",
470                 .data           = &sysctl_sched_rt_runtime,
471                 .maxlen         = sizeof(int),
472                 .mode           = 0644,
473                 .proc_handler   = sched_rt_handler,
474         },
475         {
476                 .procname       = "sched_rr_timeslice_ms",
477                 .data           = &sysctl_sched_rr_timeslice,
478                 .maxlen         = sizeof(int),
479                 .mode           = 0644,
480                 .proc_handler   = sched_rr_handler,
481         },
482 #ifdef CONFIG_SCHED_AUTOGROUP
483         {
484                 .procname       = "sched_autogroup_enabled",
485                 .data           = &sysctl_sched_autogroup_enabled,
486                 .maxlen         = sizeof(unsigned int),
487                 .mode           = 0644,
488                 .proc_handler   = proc_dointvec_minmax,
489                 .extra1         = &zero,
490                 .extra2         = &one,
491         },
492 #endif
493 #ifdef CONFIG_CFS_BANDWIDTH
494         {
495                 .procname       = "sched_cfs_bandwidth_slice_us",
496                 .data           = &sysctl_sched_cfs_bandwidth_slice,
497                 .maxlen         = sizeof(unsigned int),
498                 .mode           = 0644,
499                 .proc_handler   = proc_dointvec_minmax,
500                 .extra1         = &one,
501         },
502 #endif
503 #ifdef CONFIG_PROVE_LOCKING
504         {
505                 .procname       = "prove_locking",
506                 .data           = &prove_locking,
507                 .maxlen         = sizeof(int),
508                 .mode           = 0644,
509                 .proc_handler   = proc_dointvec,
510         },
511 #endif
512 #ifdef CONFIG_LOCK_STAT
513         {
514                 .procname       = "lock_stat",
515                 .data           = &lock_stat,
516                 .maxlen         = sizeof(int),
517                 .mode           = 0644,
518                 .proc_handler   = proc_dointvec,
519         },
520 #endif
521         {
522                 .procname       = "panic",
523                 .data           = &panic_timeout,
524                 .maxlen         = sizeof(int),
525                 .mode           = 0644,
526                 .proc_handler   = proc_dointvec,
527         },
528 #ifdef CONFIG_COREDUMP
529         {
530                 .procname       = "core_uses_pid",
531                 .data           = &core_uses_pid,
532                 .maxlen         = sizeof(int),
533                 .mode           = 0644,
534                 .proc_handler   = proc_dointvec,
535         },
536         {
537                 .procname       = "core_pattern",
538                 .data           = core_pattern,
539                 .maxlen         = CORENAME_MAX_SIZE,
540                 .mode           = 0644,
541                 .proc_handler   = proc_dostring_coredump,
542         },
543         {
544                 .procname       = "core_pipe_limit",
545                 .data           = &core_pipe_limit,
546                 .maxlen         = sizeof(unsigned int),
547                 .mode           = 0644,
548                 .proc_handler   = proc_dointvec,
549         },
550 #endif
551 #ifdef CONFIG_PROC_SYSCTL
552         {
553                 .procname       = "tainted",
554                 .maxlen         = sizeof(long),
555                 .mode           = 0644,
556                 .proc_handler   = proc_taint,
557         },
558         {
559                 .procname       = "sysctl_writes_strict",
560                 .data           = &sysctl_writes_strict,
561                 .maxlen         = sizeof(int),
562                 .mode           = 0644,
563                 .proc_handler   = proc_dointvec_minmax,
564                 .extra1         = &neg_one,
565                 .extra2         = &one,
566         },
567 #endif
568 #ifdef CONFIG_LATENCYTOP
569         {
570                 .procname       = "latencytop",
571                 .data           = &latencytop_enabled,
572                 .maxlen         = sizeof(int),
573                 .mode           = 0644,
574                 .proc_handler   = sysctl_latencytop,
575         },
576 #endif
577 #ifdef CONFIG_BLK_DEV_INITRD
578         {
579                 .procname       = "real-root-dev",
580                 .data           = &real_root_dev,
581                 .maxlen         = sizeof(int),
582                 .mode           = 0644,
583                 .proc_handler   = proc_dointvec,
584         },
585 #endif
586         {
587                 .procname       = "print-fatal-signals",
588                 .data           = &print_fatal_signals,
589                 .maxlen         = sizeof(int),
590                 .mode           = 0644,
591                 .proc_handler   = proc_dointvec,
592         },
593 #ifdef CONFIG_SPARC
594         {
595                 .procname       = "reboot-cmd",
596                 .data           = reboot_command,
597                 .maxlen         = 256,
598                 .mode           = 0644,
599                 .proc_handler   = proc_dostring,
600         },
601         {
602                 .procname       = "stop-a",
603                 .data           = &stop_a_enabled,
604                 .maxlen         = sizeof (int),
605                 .mode           = 0644,
606                 .proc_handler   = proc_dointvec,
607         },
608         {
609                 .procname       = "scons-poweroff",
610                 .data           = &scons_pwroff,
611                 .maxlen         = sizeof (int),
612                 .mode           = 0644,
613                 .proc_handler   = proc_dointvec,
614         },
615 #endif
616 #ifdef CONFIG_SPARC64
617         {
618                 .procname       = "tsb-ratio",
619                 .data           = &sysctl_tsb_ratio,
620                 .maxlen         = sizeof (int),
621                 .mode           = 0644,
622                 .proc_handler   = proc_dointvec,
623         },
624 #endif
625 #ifdef __hppa__
626         {
627                 .procname       = "soft-power",
628                 .data           = &pwrsw_enabled,
629                 .maxlen         = sizeof (int),
630                 .mode           = 0644,
631                 .proc_handler   = proc_dointvec,
632         },
633 #endif
634 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW
635         {
636                 .procname       = "unaligned-trap",
637                 .data           = &unaligned_enabled,
638                 .maxlen         = sizeof (int),
639                 .mode           = 0644,
640                 .proc_handler   = proc_dointvec,
641         },
642 #endif
643         {
644                 .procname       = "ctrl-alt-del",
645                 .data           = &C_A_D,
646                 .maxlen         = sizeof(int),
647                 .mode           = 0644,
648                 .proc_handler   = proc_dointvec,
649         },
650 #ifdef CONFIG_FUNCTION_TRACER
651         {
652                 .procname       = "ftrace_enabled",
653                 .data           = &ftrace_enabled,
654                 .maxlen         = sizeof(int),
655                 .mode           = 0644,
656                 .proc_handler   = ftrace_enable_sysctl,
657         },
658 #endif
659 #ifdef CONFIG_STACK_TRACER
660         {
661                 .procname       = "stack_tracer_enabled",
662                 .data           = &stack_tracer_enabled,
663                 .maxlen         = sizeof(int),
664                 .mode           = 0644,
665                 .proc_handler   = stack_trace_sysctl,
666         },
667 #endif
668 #ifdef CONFIG_TRACING
669         {
670                 .procname       = "ftrace_dump_on_oops",
671                 .data           = &ftrace_dump_on_oops,
672                 .maxlen         = sizeof(int),
673                 .mode           = 0644,
674                 .proc_handler   = proc_dointvec,
675         },
676         {
677                 .procname       = "traceoff_on_warning",
678                 .data           = &__disable_trace_on_warning,
679                 .maxlen         = sizeof(__disable_trace_on_warning),
680                 .mode           = 0644,
681                 .proc_handler   = proc_dointvec,
682         },
683         {
684                 .procname       = "tracepoint_printk",
685                 .data           = &tracepoint_printk,
686                 .maxlen         = sizeof(tracepoint_printk),
687                 .mode           = 0644,
688                 .proc_handler   = tracepoint_printk_sysctl,
689         },
690 #endif
691 #ifdef CONFIG_KEXEC_CORE
692         {
693                 .procname       = "kexec_load_disabled",
694                 .data           = &kexec_load_disabled,
695                 .maxlen         = sizeof(int),
696                 .mode           = 0644,
697                 /* only handle a transition from default "0" to "1" */
698                 .proc_handler   = proc_dointvec_minmax,
699                 .extra1         = &one,
700                 .extra2         = &one,
701         },
702 #endif
703 #ifdef CONFIG_MODULES
704         {
705                 .procname       = "modprobe",
706                 .data           = &modprobe_path,
707                 .maxlen         = KMOD_PATH_LEN,
708                 .mode           = 0644,
709                 .proc_handler   = proc_dostring,
710         },
711         {
712                 .procname       = "modules_disabled",
713                 .data           = &modules_disabled,
714                 .maxlen         = sizeof(int),
715                 .mode           = 0644,
716                 /* only handle a transition from default "0" to "1" */
717                 .proc_handler   = proc_dointvec_minmax,
718                 .extra1         = &one,
719                 .extra2         = &one,
720         },
721 #endif
722 #ifdef CONFIG_UEVENT_HELPER
723         {
724                 .procname       = "hotplug",
725                 .data           = &uevent_helper,
726                 .maxlen         = UEVENT_HELPER_PATH_LEN,
727                 .mode           = 0644,
728                 .proc_handler   = proc_dostring,
729         },
730 #endif
731 #ifdef CONFIG_CHR_DEV_SG
732         {
733                 .procname       = "sg-big-buff",
734                 .data           = &sg_big_buff,
735                 .maxlen         = sizeof (int),
736                 .mode           = 0444,
737                 .proc_handler   = proc_dointvec,
738         },
739 #endif
740 #ifdef CONFIG_BSD_PROCESS_ACCT
741         {
742                 .procname       = "acct",
743                 .data           = &acct_parm,
744                 .maxlen         = 3*sizeof(int),
745                 .mode           = 0644,
746                 .proc_handler   = proc_dointvec,
747         },
748 #endif
749 #ifdef CONFIG_MAGIC_SYSRQ
750         {
751                 .procname       = "sysrq",
752                 .data           = &__sysrq_enabled,
753                 .maxlen         = sizeof (int),
754                 .mode           = 0644,
755                 .proc_handler   = sysrq_sysctl_handler,
756         },
757 #endif
758 #ifdef CONFIG_PROC_SYSCTL
759         {
760                 .procname       = "cad_pid",
761                 .data           = NULL,
762                 .maxlen         = sizeof (int),
763                 .mode           = 0600,
764                 .proc_handler   = proc_do_cad_pid,
765         },
766 #endif
767         {
768                 .procname       = "threads-max",
769                 .data           = NULL,
770                 .maxlen         = sizeof(int),
771                 .mode           = 0644,
772                 .proc_handler   = sysctl_max_threads,
773         },
774         {
775                 .procname       = "random",
776                 .mode           = 0555,
777                 .child          = random_table,
778         },
779         {
780                 .procname       = "usermodehelper",
781                 .mode           = 0555,
782                 .child          = usermodehelper_table,
783         },
784 #ifdef CONFIG_FW_LOADER_USER_HELPER
785         {
786                 .procname       = "firmware_config",
787                 .mode           = 0555,
788                 .child          = firmware_config_table,
789         },
790 #endif
791         {
792                 .procname       = "overflowuid",
793                 .data           = &overflowuid,
794                 .maxlen         = sizeof(int),
795                 .mode           = 0644,
796                 .proc_handler   = proc_dointvec_minmax,
797                 .extra1         = &minolduid,
798                 .extra2         = &maxolduid,
799         },
800         {
801                 .procname       = "overflowgid",
802                 .data           = &overflowgid,
803                 .maxlen         = sizeof(int),
804                 .mode           = 0644,
805                 .proc_handler   = proc_dointvec_minmax,
806                 .extra1         = &minolduid,
807                 .extra2         = &maxolduid,
808         },
809 #ifdef CONFIG_S390
810 #ifdef CONFIG_MATHEMU
811         {
812                 .procname       = "ieee_emulation_warnings",
813                 .data           = &sysctl_ieee_emulation_warnings,
814                 .maxlen         = sizeof(int),
815                 .mode           = 0644,
816                 .proc_handler   = proc_dointvec,
817         },
818 #endif
819         {
820                 .procname       = "userprocess_debug",
821                 .data           = &show_unhandled_signals,
822                 .maxlen         = sizeof(int),
823                 .mode           = 0644,
824                 .proc_handler   = proc_dointvec,
825         },
826 #endif
827         {
828                 .procname       = "pid_max",
829                 .data           = &pid_max,
830                 .maxlen         = sizeof (int),
831                 .mode           = 0644,
832                 .proc_handler   = proc_dointvec_minmax,
833                 .extra1         = &pid_max_min,
834                 .extra2         = &pid_max_max,
835         },
836         {
837                 .procname       = "panic_on_oops",
838                 .data           = &panic_on_oops,
839                 .maxlen         = sizeof(int),
840                 .mode           = 0644,
841                 .proc_handler   = proc_dointvec,
842         },
843 #if defined CONFIG_PRINTK
844         {
845                 .procname       = "printk",
846                 .data           = &console_loglevel,
847                 .maxlen         = 4*sizeof(int),
848                 .mode           = 0644,
849                 .proc_handler   = proc_dointvec,
850         },
851         {
852                 .procname       = "printk_ratelimit",
853                 .data           = &printk_ratelimit_state.interval,
854                 .maxlen         = sizeof(int),
855                 .mode           = 0644,
856                 .proc_handler   = proc_dointvec_jiffies,
857         },
858         {
859                 .procname       = "printk_ratelimit_burst",
860                 .data           = &printk_ratelimit_state.burst,
861                 .maxlen         = sizeof(int),
862                 .mode           = 0644,
863                 .proc_handler   = proc_dointvec,
864         },
865         {
866                 .procname       = "printk_delay",
867                 .data           = &printk_delay_msec,
868                 .maxlen         = sizeof(int),
869                 .mode           = 0644,
870                 .proc_handler   = proc_dointvec_minmax,
871                 .extra1         = &zero,
872                 .extra2         = &ten_thousand,
873         },
874         {
875                 .procname       = "printk_devkmsg",
876                 .data           = devkmsg_log_str,
877                 .maxlen         = DEVKMSG_STR_MAX_SIZE,
878                 .mode           = 0644,
879                 .proc_handler   = devkmsg_sysctl_set_loglvl,
880         },
881         {
882                 .procname       = "dmesg_restrict",
883                 .data           = &dmesg_restrict,
884                 .maxlen         = sizeof(int),
885                 .mode           = 0644,
886                 .proc_handler   = proc_dointvec_minmax_sysadmin,
887                 .extra1         = &zero,
888                 .extra2         = &one,
889         },
890         {
891                 .procname       = "kptr_restrict",
892                 .data           = &kptr_restrict,
893                 .maxlen         = sizeof(int),
894                 .mode           = 0644,
895                 .proc_handler   = proc_dointvec_minmax_sysadmin,
896                 .extra1         = &zero,
897                 .extra2         = &two,
898         },
899 #endif
900         {
901                 .procname       = "ngroups_max",
902                 .data           = &ngroups_max,
903                 .maxlen         = sizeof (int),
904                 .mode           = 0444,
905                 .proc_handler   = proc_dointvec,
906         },
907         {
908                 .procname       = "cap_last_cap",
909                 .data           = (void *)&cap_last_cap,
910                 .maxlen         = sizeof(int),
911                 .mode           = 0444,
912                 .proc_handler   = proc_dointvec,
913         },
914 #if defined(CONFIG_LOCKUP_DETECTOR)
915         {
916                 .procname       = "watchdog",
917                 .data           = &watchdog_user_enabled,
918                 .maxlen         = sizeof(int),
919                 .mode           = 0644,
920                 .proc_handler   = proc_watchdog,
921                 .extra1         = &zero,
922                 .extra2         = &one,
923         },
924         {
925                 .procname       = "watchdog_thresh",
926                 .data           = &watchdog_thresh,
927                 .maxlen         = sizeof(int),
928                 .mode           = 0644,
929                 .proc_handler   = proc_watchdog_thresh,
930                 .extra1         = &zero,
931                 .extra2         = &sixty,
932         },
933         {
934                 .procname       = "nmi_watchdog",
935                 .data           = &nmi_watchdog_user_enabled,
936                 .maxlen         = sizeof(int),
937                 .mode           = NMI_WATCHDOG_SYSCTL_PERM,
938                 .proc_handler   = proc_nmi_watchdog,
939                 .extra1         = &zero,
940                 .extra2         = &one,
941         },
942         {
943                 .procname       = "watchdog_cpumask",
944                 .data           = &watchdog_cpumask_bits,
945                 .maxlen         = NR_CPUS,
946                 .mode           = 0644,
947                 .proc_handler   = proc_watchdog_cpumask,
948         },
949 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
950         {
951                 .procname       = "soft_watchdog",
952                 .data           = &soft_watchdog_user_enabled,
953                 .maxlen         = sizeof(int),
954                 .mode           = 0644,
955                 .proc_handler   = proc_soft_watchdog,
956                 .extra1         = &zero,
957                 .extra2         = &one,
958         },
959         {
960                 .procname       = "softlockup_panic",
961                 .data           = &softlockup_panic,
962                 .maxlen         = sizeof(int),
963                 .mode           = 0644,
964                 .proc_handler   = proc_dointvec_minmax,
965                 .extra1         = &zero,
966                 .extra2         = &one,
967         },
968 #ifdef CONFIG_SMP
969         {
970                 .procname       = "softlockup_all_cpu_backtrace",
971                 .data           = &sysctl_softlockup_all_cpu_backtrace,
972                 .maxlen         = sizeof(int),
973                 .mode           = 0644,
974                 .proc_handler   = proc_dointvec_minmax,
975                 .extra1         = &zero,
976                 .extra2         = &one,
977         },
978 #endif /* CONFIG_SMP */
979 #endif
980 #ifdef CONFIG_HARDLOCKUP_DETECTOR
981         {
982                 .procname       = "hardlockup_panic",
983                 .data           = &hardlockup_panic,
984                 .maxlen         = sizeof(int),
985                 .mode           = 0644,
986                 .proc_handler   = proc_dointvec_minmax,
987                 .extra1         = &zero,
988                 .extra2         = &one,
989         },
990 #ifdef CONFIG_SMP
991         {
992                 .procname       = "hardlockup_all_cpu_backtrace",
993                 .data           = &sysctl_hardlockup_all_cpu_backtrace,
994                 .maxlen         = sizeof(int),
995                 .mode           = 0644,
996                 .proc_handler   = proc_dointvec_minmax,
997                 .extra1         = &zero,
998                 .extra2         = &one,
999         },
1000 #endif /* CONFIG_SMP */
1001 #endif
1002 #endif
1003
1004 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
1005         {
1006                 .procname       = "unknown_nmi_panic",
1007                 .data           = &unknown_nmi_panic,
1008                 .maxlen         = sizeof (int),
1009                 .mode           = 0644,
1010                 .proc_handler   = proc_dointvec,
1011         },
1012 #endif
1013 #if defined(CONFIG_X86)
1014         {
1015                 .procname       = "panic_on_unrecovered_nmi",
1016                 .data           = &panic_on_unrecovered_nmi,
1017                 .maxlen         = sizeof(int),
1018                 .mode           = 0644,
1019                 .proc_handler   = proc_dointvec,
1020         },
1021         {
1022                 .procname       = "panic_on_io_nmi",
1023                 .data           = &panic_on_io_nmi,
1024                 .maxlen         = sizeof(int),
1025                 .mode           = 0644,
1026                 .proc_handler   = proc_dointvec,
1027         },
1028 #ifdef CONFIG_DEBUG_STACKOVERFLOW
1029         {
1030                 .procname       = "panic_on_stackoverflow",
1031                 .data           = &sysctl_panic_on_stackoverflow,
1032                 .maxlen         = sizeof(int),
1033                 .mode           = 0644,
1034                 .proc_handler   = proc_dointvec,
1035         },
1036 #endif
1037         {
1038                 .procname       = "bootloader_type",
1039                 .data           = &bootloader_type,
1040                 .maxlen         = sizeof (int),
1041                 .mode           = 0444,
1042                 .proc_handler   = proc_dointvec,
1043         },
1044         {
1045                 .procname       = "bootloader_version",
1046                 .data           = &bootloader_version,
1047                 .maxlen         = sizeof (int),
1048                 .mode           = 0444,
1049                 .proc_handler   = proc_dointvec,
1050         },
1051         {
1052                 .procname       = "io_delay_type",
1053                 .data           = &io_delay_type,
1054                 .maxlen         = sizeof(int),
1055                 .mode           = 0644,
1056                 .proc_handler   = proc_dointvec,
1057         },
1058 #endif
1059 #if defined(CONFIG_MMU)
1060         {
1061                 .procname       = "randomize_va_space",
1062                 .data           = &randomize_va_space,
1063                 .maxlen         = sizeof(int),
1064                 .mode           = 0644,
1065                 .proc_handler   = proc_dointvec,
1066         },
1067 #endif
1068 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
1069         {
1070                 .procname       = "spin_retry",
1071                 .data           = &spin_retry,
1072                 .maxlen         = sizeof (int),
1073                 .mode           = 0644,
1074                 .proc_handler   = proc_dointvec,
1075         },
1076 #endif
1077 #if     defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
1078         {
1079                 .procname       = "acpi_video_flags",
1080                 .data           = &acpi_realmode_flags,
1081                 .maxlen         = sizeof (unsigned long),
1082                 .mode           = 0644,
1083                 .proc_handler   = proc_doulongvec_minmax,
1084         },
1085 #endif
1086 #ifdef CONFIG_SYSCTL_ARCH_UNALIGN_NO_WARN
1087         {
1088                 .procname       = "ignore-unaligned-usertrap",
1089                 .data           = &no_unaligned_warning,
1090                 .maxlen         = sizeof (int),
1091                 .mode           = 0644,
1092                 .proc_handler   = proc_dointvec,
1093         },
1094 #endif
1095 #ifdef CONFIG_IA64
1096         {
1097                 .procname       = "unaligned-dump-stack",
1098                 .data           = &unaligned_dump_stack,
1099                 .maxlen         = sizeof (int),
1100                 .mode           = 0644,
1101                 .proc_handler   = proc_dointvec,
1102         },
1103 #endif
1104 #ifdef CONFIG_DETECT_HUNG_TASK
1105         {
1106                 .procname       = "hung_task_panic",
1107                 .data           = &sysctl_hung_task_panic,
1108                 .maxlen         = sizeof(int),
1109                 .mode           = 0644,
1110                 .proc_handler   = proc_dointvec_minmax,
1111                 .extra1         = &zero,
1112                 .extra2         = &one,
1113         },
1114         {
1115                 .procname       = "hung_task_check_count",
1116                 .data           = &sysctl_hung_task_check_count,
1117                 .maxlen         = sizeof(int),
1118                 .mode           = 0644,
1119                 .proc_handler   = proc_dointvec_minmax,
1120                 .extra1         = &zero,
1121         },
1122         {
1123                 .procname       = "hung_task_timeout_secs",
1124                 .data           = &sysctl_hung_task_timeout_secs,
1125                 .maxlen         = sizeof(unsigned long),
1126                 .mode           = 0644,
1127                 .proc_handler   = proc_dohung_task_timeout_secs,
1128                 .extra2         = &hung_task_timeout_max,
1129         },
1130         {
1131                 .procname       = "hung_task_check_interval_secs",
1132                 .data           = &sysctl_hung_task_check_interval_secs,
1133                 .maxlen         = sizeof(unsigned long),
1134                 .mode           = 0644,
1135                 .proc_handler   = proc_dohung_task_timeout_secs,
1136                 .extra2         = &hung_task_timeout_max,
1137         },
1138         {
1139                 .procname       = "hung_task_warnings",
1140                 .data           = &sysctl_hung_task_warnings,
1141                 .maxlen         = sizeof(int),
1142                 .mode           = 0644,
1143                 .proc_handler   = proc_dointvec_minmax,
1144                 .extra1         = &neg_one,
1145         },
1146 #endif
1147 #ifdef CONFIG_RT_MUTEXES
1148         {
1149                 .procname       = "max_lock_depth",
1150                 .data           = &max_lock_depth,
1151                 .maxlen         = sizeof(int),
1152                 .mode           = 0644,
1153                 .proc_handler   = proc_dointvec,
1154         },
1155 #endif
1156         {
1157                 .procname       = "poweroff_cmd",
1158                 .data           = &poweroff_cmd,
1159                 .maxlen         = POWEROFF_CMD_PATH_LEN,
1160                 .mode           = 0644,
1161                 .proc_handler   = proc_dostring,
1162         },
1163 #ifdef CONFIG_KEYS
1164         {
1165                 .procname       = "keys",
1166                 .mode           = 0555,
1167                 .child          = key_sysctls,
1168         },
1169 #endif
1170 #ifdef CONFIG_PERF_EVENTS
1171         /*
1172          * User-space scripts rely on the existence of this file
1173          * as a feature check for perf_events being enabled.
1174          *
1175          * So it's an ABI, do not remove!
1176          */
1177         {
1178                 .procname       = "perf_event_paranoid",
1179                 .data           = &sysctl_perf_event_paranoid,
1180                 .maxlen         = sizeof(sysctl_perf_event_paranoid),
1181                 .mode           = 0644,
1182                 .proc_handler   = proc_dointvec,
1183         },
1184         {
1185                 .procname       = "perf_event_mlock_kb",
1186                 .data           = &sysctl_perf_event_mlock,
1187                 .maxlen         = sizeof(sysctl_perf_event_mlock),
1188                 .mode           = 0644,
1189                 .proc_handler   = proc_dointvec,
1190         },
1191         {
1192                 .procname       = "perf_event_max_sample_rate",
1193                 .data           = &sysctl_perf_event_sample_rate,
1194                 .maxlen         = sizeof(sysctl_perf_event_sample_rate),
1195                 .mode           = 0644,
1196                 .proc_handler   = perf_proc_update_handler,
1197                 .extra1         = &one,
1198         },
1199         {
1200                 .procname       = "perf_cpu_time_max_percent",
1201                 .data           = &sysctl_perf_cpu_time_max_percent,
1202                 .maxlen         = sizeof(sysctl_perf_cpu_time_max_percent),
1203                 .mode           = 0644,
1204                 .proc_handler   = perf_cpu_time_max_percent_handler,
1205                 .extra1         = &zero,
1206                 .extra2         = &one_hundred,
1207         },
1208         {
1209                 .procname       = "perf_event_max_stack",
1210                 .data           = &sysctl_perf_event_max_stack,
1211                 .maxlen         = sizeof(sysctl_perf_event_max_stack),
1212                 .mode           = 0644,
1213                 .proc_handler   = perf_event_max_stack_handler,
1214                 .extra1         = &zero,
1215                 .extra2         = &six_hundred_forty_kb,
1216         },
1217         {
1218                 .procname       = "perf_event_max_contexts_per_stack",
1219                 .data           = &sysctl_perf_event_max_contexts_per_stack,
1220                 .maxlen         = sizeof(sysctl_perf_event_max_contexts_per_stack),
1221                 .mode           = 0644,
1222                 .proc_handler   = perf_event_max_stack_handler,
1223                 .extra1         = &zero,
1224                 .extra2         = &one_thousand,
1225         },
1226 #endif
1227         {
1228                 .procname       = "panic_on_warn",
1229                 .data           = &panic_on_warn,
1230                 .maxlen         = sizeof(int),
1231                 .mode           = 0644,
1232                 .proc_handler   = proc_dointvec_minmax,
1233                 .extra1         = &zero,
1234                 .extra2         = &one,
1235         },
1236 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
1237         {
1238                 .procname       = "timer_migration",
1239                 .data           = &sysctl_timer_migration,
1240                 .maxlen         = sizeof(unsigned int),
1241                 .mode           = 0644,
1242                 .proc_handler   = timer_migration_handler,
1243                 .extra1         = &zero,
1244                 .extra2         = &one,
1245         },
1246 #endif
1247 #ifdef CONFIG_BPF_SYSCALL
1248         {
1249                 .procname       = "unprivileged_bpf_disabled",
1250                 .data           = &sysctl_unprivileged_bpf_disabled,
1251                 .maxlen         = sizeof(sysctl_unprivileged_bpf_disabled),
1252                 .mode           = 0644,
1253                 .proc_handler   = bpf_unpriv_handler,
1254                 .extra1         = &zero,
1255                 .extra2         = &two,
1256         },
1257 #endif
1258 #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
1259         {
1260                 .procname       = "panic_on_rcu_stall",
1261                 .data           = &sysctl_panic_on_rcu_stall,
1262                 .maxlen         = sizeof(sysctl_panic_on_rcu_stall),
1263                 .mode           = 0644,
1264                 .proc_handler   = proc_dointvec_minmax,
1265                 .extra1         = &zero,
1266                 .extra2         = &one,
1267         },
1268 #endif
1269         { }
1270 };
1271
1272 static struct ctl_table vm_table[] = {
1273         {
1274                 .procname       = "overcommit_memory",
1275                 .data           = &sysctl_overcommit_memory,
1276                 .maxlen         = sizeof(sysctl_overcommit_memory),
1277                 .mode           = 0644,
1278                 .proc_handler   = proc_dointvec_minmax,
1279                 .extra1         = &zero,
1280                 .extra2         = &two,
1281         },
1282         {
1283                 .procname       = "panic_on_oom",
1284                 .data           = &sysctl_panic_on_oom,
1285                 .maxlen         = sizeof(sysctl_panic_on_oom),
1286                 .mode           = 0644,
1287                 .proc_handler   = proc_dointvec_minmax,
1288                 .extra1         = &zero,
1289                 .extra2         = &two,
1290         },
1291         {
1292                 .procname       = "oom_kill_allocating_task",
1293                 .data           = &sysctl_oom_kill_allocating_task,
1294                 .maxlen         = sizeof(sysctl_oom_kill_allocating_task),
1295                 .mode           = 0644,
1296                 .proc_handler   = proc_dointvec,
1297         },
1298         {
1299                 .procname       = "oom_dump_tasks",
1300                 .data           = &sysctl_oom_dump_tasks,
1301                 .maxlen         = sizeof(sysctl_oom_dump_tasks),
1302                 .mode           = 0644,
1303                 .proc_handler   = proc_dointvec,
1304         },
1305         {
1306                 .procname       = "overcommit_ratio",
1307                 .data           = &sysctl_overcommit_ratio,
1308                 .maxlen         = sizeof(sysctl_overcommit_ratio),
1309                 .mode           = 0644,
1310                 .proc_handler   = overcommit_ratio_handler,
1311         },
1312         {
1313                 .procname       = "overcommit_kbytes",
1314                 .data           = &sysctl_overcommit_kbytes,
1315                 .maxlen         = sizeof(sysctl_overcommit_kbytes),
1316                 .mode           = 0644,
1317                 .proc_handler   = overcommit_kbytes_handler,
1318         },
1319         {
1320                 .procname       = "page-cluster", 
1321                 .data           = &page_cluster,
1322                 .maxlen         = sizeof(int),
1323                 .mode           = 0644,
1324                 .proc_handler   = proc_dointvec_minmax,
1325                 .extra1         = &zero,
1326         },
1327         {
1328                 .procname       = "dirty_background_ratio",
1329                 .data           = &dirty_background_ratio,
1330                 .maxlen         = sizeof(dirty_background_ratio),
1331                 .mode           = 0644,
1332                 .proc_handler   = dirty_background_ratio_handler,
1333                 .extra1         = &zero,
1334                 .extra2         = &one_hundred,
1335         },
1336         {
1337                 .procname       = "dirty_background_bytes",
1338                 .data           = &dirty_background_bytes,
1339                 .maxlen         = sizeof(dirty_background_bytes),
1340                 .mode           = 0644,
1341                 .proc_handler   = dirty_background_bytes_handler,
1342                 .extra1         = &one_ul,
1343         },
1344         {
1345                 .procname       = "dirty_ratio",
1346                 .data           = &vm_dirty_ratio,
1347                 .maxlen         = sizeof(vm_dirty_ratio),
1348                 .mode           = 0644,
1349                 .proc_handler   = dirty_ratio_handler,
1350                 .extra1         = &zero,
1351                 .extra2         = &one_hundred,
1352         },
1353         {
1354                 .procname       = "dirty_bytes",
1355                 .data           = &vm_dirty_bytes,
1356                 .maxlen         = sizeof(vm_dirty_bytes),
1357                 .mode           = 0644,
1358                 .proc_handler   = dirty_bytes_handler,
1359                 .extra1         = &dirty_bytes_min,
1360         },
1361         {
1362                 .procname       = "dirty_writeback_centisecs",
1363                 .data           = &dirty_writeback_interval,
1364                 .maxlen         = sizeof(dirty_writeback_interval),
1365                 .mode           = 0644,
1366                 .proc_handler   = dirty_writeback_centisecs_handler,
1367         },
1368         {
1369                 .procname       = "dirty_expire_centisecs",
1370                 .data           = &dirty_expire_interval,
1371                 .maxlen         = sizeof(dirty_expire_interval),
1372                 .mode           = 0644,
1373                 .proc_handler   = proc_dointvec_minmax,
1374                 .extra1         = &zero,
1375         },
1376         {
1377                 .procname       = "dirtytime_expire_seconds",
1378                 .data           = &dirtytime_expire_interval,
1379                 .maxlen         = sizeof(dirtytime_expire_interval),
1380                 .mode           = 0644,
1381                 .proc_handler   = dirtytime_interval_handler,
1382                 .extra1         = &zero,
1383         },
1384         {
1385                 .procname       = "swappiness",
1386                 .data           = &vm_swappiness,
1387                 .maxlen         = sizeof(vm_swappiness),
1388                 .mode           = 0644,
1389                 .proc_handler   = proc_dointvec_minmax,
1390                 .extra1         = &zero,
1391                 .extra2         = &one_hundred,
1392         },
1393 #ifdef CONFIG_HUGETLB_PAGE
1394         {
1395                 .procname       = "nr_hugepages",
1396                 .data           = NULL,
1397                 .maxlen         = sizeof(unsigned long),
1398                 .mode           = 0644,
1399                 .proc_handler   = hugetlb_sysctl_handler,
1400         },
1401 #ifdef CONFIG_NUMA
1402         {
1403                 .procname       = "nr_hugepages_mempolicy",
1404                 .data           = NULL,
1405                 .maxlen         = sizeof(unsigned long),
1406                 .mode           = 0644,
1407                 .proc_handler   = &hugetlb_mempolicy_sysctl_handler,
1408         },
1409         {
1410                 .procname               = "numa_stat",
1411                 .data                   = &sysctl_vm_numa_stat,
1412                 .maxlen                 = sizeof(int),
1413                 .mode                   = 0644,
1414                 .proc_handler   = sysctl_vm_numa_stat_handler,
1415                 .extra1                 = &zero,
1416                 .extra2                 = &one,
1417         },
1418 #endif
1419          {
1420                 .procname       = "hugetlb_shm_group",
1421                 .data           = &sysctl_hugetlb_shm_group,
1422                 .maxlen         = sizeof(gid_t),
1423                 .mode           = 0644,
1424                 .proc_handler   = proc_dointvec,
1425          },
1426         {
1427                 .procname       = "nr_overcommit_hugepages",
1428                 .data           = NULL,
1429                 .maxlen         = sizeof(unsigned long),
1430                 .mode           = 0644,
1431                 .proc_handler   = hugetlb_overcommit_handler,
1432         },
1433 #endif
1434         {
1435                 .procname       = "lowmem_reserve_ratio",
1436                 .data           = &sysctl_lowmem_reserve_ratio,
1437                 .maxlen         = sizeof(sysctl_lowmem_reserve_ratio),
1438                 .mode           = 0644,
1439                 .proc_handler   = lowmem_reserve_ratio_sysctl_handler,
1440         },
1441         {
1442                 .procname       = "drop_caches",
1443                 .data           = &sysctl_drop_caches,
1444                 .maxlen         = sizeof(int),
1445                 .mode           = 0200,
1446                 .proc_handler   = drop_caches_sysctl_handler,
1447                 .extra1         = &one,
1448                 .extra2         = &four,
1449         },
1450 #ifdef CONFIG_COMPACTION
1451         {
1452                 .procname       = "compact_memory",
1453                 .data           = &sysctl_compact_memory,
1454                 .maxlen         = sizeof(int),
1455                 .mode           = 0200,
1456                 .proc_handler   = sysctl_compaction_handler,
1457         },
1458         {
1459                 .procname       = "extfrag_threshold",
1460                 .data           = &sysctl_extfrag_threshold,
1461                 .maxlen         = sizeof(int),
1462                 .mode           = 0644,
1463                 .proc_handler   = sysctl_extfrag_handler,
1464                 .extra1         = &min_extfrag_threshold,
1465                 .extra2         = &max_extfrag_threshold,
1466         },
1467         {
1468                 .procname       = "compact_unevictable_allowed",
1469                 .data           = &sysctl_compact_unevictable_allowed,
1470                 .maxlen         = sizeof(int),
1471                 .mode           = 0644,
1472                 .proc_handler   = proc_dointvec,
1473                 .extra1         = &zero,
1474                 .extra2         = &one,
1475         },
1476
1477 #endif /* CONFIG_COMPACTION */
1478         {
1479                 .procname       = "min_free_kbytes",
1480                 .data           = &min_free_kbytes,
1481                 .maxlen         = sizeof(min_free_kbytes),
1482                 .mode           = 0644,
1483                 .proc_handler   = min_free_kbytes_sysctl_handler,
1484                 .extra1         = &zero,
1485         },
1486         {
1487                 .procname       = "watermark_scale_factor",
1488                 .data           = &watermark_scale_factor,
1489                 .maxlen         = sizeof(watermark_scale_factor),
1490                 .mode           = 0644,
1491                 .proc_handler   = watermark_scale_factor_sysctl_handler,
1492                 .extra1         = &one,
1493                 .extra2         = &one_thousand,
1494         },
1495         {
1496                 .procname       = "percpu_pagelist_fraction",
1497                 .data           = &percpu_pagelist_fraction,
1498                 .maxlen         = sizeof(percpu_pagelist_fraction),
1499                 .mode           = 0644,
1500                 .proc_handler   = percpu_pagelist_fraction_sysctl_handler,
1501                 .extra1         = &zero,
1502         },
1503 #ifdef CONFIG_MMU
1504         {
1505                 .procname       = "max_map_count",
1506                 .data           = &sysctl_max_map_count,
1507                 .maxlen         = sizeof(sysctl_max_map_count),
1508                 .mode           = 0644,
1509                 .proc_handler   = proc_dointvec_minmax,
1510                 .extra1         = &zero,
1511         },
1512 #else
1513         {
1514                 .procname       = "nr_trim_pages",
1515                 .data           = &sysctl_nr_trim_pages,
1516                 .maxlen         = sizeof(sysctl_nr_trim_pages),
1517                 .mode           = 0644,
1518                 .proc_handler   = proc_dointvec_minmax,
1519                 .extra1         = &zero,
1520         },
1521 #endif
1522         {
1523                 .procname       = "laptop_mode",
1524                 .data           = &laptop_mode,
1525                 .maxlen         = sizeof(laptop_mode),
1526                 .mode           = 0644,
1527                 .proc_handler   = proc_dointvec_jiffies,
1528         },
1529         {
1530                 .procname       = "block_dump",
1531                 .data           = &block_dump,
1532                 .maxlen         = sizeof(block_dump),
1533                 .mode           = 0644,
1534                 .proc_handler   = proc_dointvec,
1535                 .extra1         = &zero,
1536         },
1537         {
1538                 .procname       = "vfs_cache_pressure",
1539                 .data           = &sysctl_vfs_cache_pressure,
1540                 .maxlen         = sizeof(sysctl_vfs_cache_pressure),
1541                 .mode           = 0644,
1542                 .proc_handler   = proc_dointvec,
1543                 .extra1         = &zero,
1544         },
1545 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
1546         {
1547                 .procname       = "legacy_va_layout",
1548                 .data           = &sysctl_legacy_va_layout,
1549                 .maxlen         = sizeof(sysctl_legacy_va_layout),
1550                 .mode           = 0644,
1551                 .proc_handler   = proc_dointvec,
1552                 .extra1         = &zero,
1553         },
1554 #endif
1555 #ifdef CONFIG_NUMA
1556         {
1557                 .procname       = "zone_reclaim_mode",
1558                 .data           = &node_reclaim_mode,
1559                 .maxlen         = sizeof(node_reclaim_mode),
1560                 .mode           = 0644,
1561                 .proc_handler   = proc_dointvec,
1562                 .extra1         = &zero,
1563         },
1564         {
1565                 .procname       = "min_unmapped_ratio",
1566                 .data           = &sysctl_min_unmapped_ratio,
1567                 .maxlen         = sizeof(sysctl_min_unmapped_ratio),
1568                 .mode           = 0644,
1569                 .proc_handler   = sysctl_min_unmapped_ratio_sysctl_handler,
1570                 .extra1         = &zero,
1571                 .extra2         = &one_hundred,
1572         },
1573         {
1574                 .procname       = "min_slab_ratio",
1575                 .data           = &sysctl_min_slab_ratio,
1576                 .maxlen         = sizeof(sysctl_min_slab_ratio),
1577                 .mode           = 0644,
1578                 .proc_handler   = sysctl_min_slab_ratio_sysctl_handler,
1579                 .extra1         = &zero,
1580                 .extra2         = &one_hundred,
1581         },
1582 #endif
1583 #ifdef CONFIG_SMP
1584         {
1585                 .procname       = "stat_interval",
1586                 .data           = &sysctl_stat_interval,
1587                 .maxlen         = sizeof(sysctl_stat_interval),
1588                 .mode           = 0644,
1589                 .proc_handler   = proc_dointvec_jiffies,
1590         },
1591         {
1592                 .procname       = "stat_refresh",
1593                 .data           = NULL,
1594                 .maxlen         = 0,
1595                 .mode           = 0600,
1596                 .proc_handler   = vmstat_refresh,
1597         },
1598 #endif
1599 #ifdef CONFIG_MMU
1600         {
1601                 .procname       = "mmap_min_addr",
1602                 .data           = &dac_mmap_min_addr,
1603                 .maxlen         = sizeof(unsigned long),
1604                 .mode           = 0644,
1605                 .proc_handler   = mmap_min_addr_handler,
1606         },
1607 #endif
1608 #ifdef CONFIG_NUMA
1609         {
1610                 .procname       = "numa_zonelist_order",
1611                 .data           = &numa_zonelist_order,
1612                 .maxlen         = NUMA_ZONELIST_ORDER_LEN,
1613                 .mode           = 0644,
1614                 .proc_handler   = numa_zonelist_order_handler,
1615         },
1616 #endif
1617 #if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
1618    (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
1619         {
1620                 .procname       = "vdso_enabled",
1621 #ifdef CONFIG_X86_32
1622                 .data           = &vdso32_enabled,
1623                 .maxlen         = sizeof(vdso32_enabled),
1624 #else
1625                 .data           = &vdso_enabled,
1626                 .maxlen         = sizeof(vdso_enabled),
1627 #endif
1628                 .mode           = 0644,
1629                 .proc_handler   = proc_dointvec,
1630                 .extra1         = &zero,
1631         },
1632 #endif
1633 #ifdef CONFIG_HIGHMEM
1634         {
1635                 .procname       = "highmem_is_dirtyable",
1636                 .data           = &vm_highmem_is_dirtyable,
1637                 .maxlen         = sizeof(vm_highmem_is_dirtyable),
1638                 .mode           = 0644,
1639                 .proc_handler   = proc_dointvec_minmax,
1640                 .extra1         = &zero,
1641                 .extra2         = &one,
1642         },
1643 #endif
1644 #ifdef CONFIG_MEMORY_FAILURE
1645         {
1646                 .procname       = "memory_failure_early_kill",
1647                 .data           = &sysctl_memory_failure_early_kill,
1648                 .maxlen         = sizeof(sysctl_memory_failure_early_kill),
1649                 .mode           = 0644,
1650                 .proc_handler   = proc_dointvec_minmax,
1651                 .extra1         = &zero,
1652                 .extra2         = &one,
1653         },
1654         {
1655                 .procname       = "memory_failure_recovery",
1656                 .data           = &sysctl_memory_failure_recovery,
1657                 .maxlen         = sizeof(sysctl_memory_failure_recovery),
1658                 .mode           = 0644,
1659                 .proc_handler   = proc_dointvec_minmax,
1660                 .extra1         = &zero,
1661                 .extra2         = &one,
1662         },
1663 #endif
1664         {
1665                 .procname       = "user_reserve_kbytes",
1666                 .data           = &sysctl_user_reserve_kbytes,
1667                 .maxlen         = sizeof(sysctl_user_reserve_kbytes),
1668                 .mode           = 0644,
1669                 .proc_handler   = proc_doulongvec_minmax,
1670         },
1671         {
1672                 .procname       = "admin_reserve_kbytes",
1673                 .data           = &sysctl_admin_reserve_kbytes,
1674                 .maxlen         = sizeof(sysctl_admin_reserve_kbytes),
1675                 .mode           = 0644,
1676                 .proc_handler   = proc_doulongvec_minmax,
1677         },
1678 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
1679         {
1680                 .procname       = "mmap_rnd_bits",
1681                 .data           = &mmap_rnd_bits,
1682                 .maxlen         = sizeof(mmap_rnd_bits),
1683                 .mode           = 0600,
1684                 .proc_handler   = proc_dointvec_minmax,
1685                 .extra1         = (void *)&mmap_rnd_bits_min,
1686                 .extra2         = (void *)&mmap_rnd_bits_max,
1687         },
1688 #endif
1689 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
1690         {
1691                 .procname       = "mmap_rnd_compat_bits",
1692                 .data           = &mmap_rnd_compat_bits,
1693                 .maxlen         = sizeof(mmap_rnd_compat_bits),
1694                 .mode           = 0600,
1695                 .proc_handler   = proc_dointvec_minmax,
1696                 .extra1         = (void *)&mmap_rnd_compat_bits_min,
1697                 .extra2         = (void *)&mmap_rnd_compat_bits_max,
1698         },
1699 #endif
1700         { }
1701 };
1702
1703 static struct ctl_table fs_table[] = {
1704         {
1705                 .procname       = "inode-nr",
1706                 .data           = &inodes_stat,
1707                 .maxlen         = 2*sizeof(long),
1708                 .mode           = 0444,
1709                 .proc_handler   = proc_nr_inodes,
1710         },
1711         {
1712                 .procname       = "inode-state",
1713                 .data           = &inodes_stat,
1714                 .maxlen         = 7*sizeof(long),
1715                 .mode           = 0444,
1716                 .proc_handler   = proc_nr_inodes,
1717         },
1718         {
1719                 .procname       = "file-nr",
1720                 .data           = &files_stat,
1721                 .maxlen         = sizeof(files_stat),
1722                 .mode           = 0444,
1723                 .proc_handler   = proc_nr_files,
1724         },
1725         {
1726                 .procname       = "file-max",
1727                 .data           = &files_stat.max_files,
1728                 .maxlen         = sizeof(files_stat.max_files),
1729                 .mode           = 0644,
1730                 .proc_handler   = proc_doulongvec_minmax,
1731                 .extra1         = &zero_ul,
1732                 .extra2         = &long_max,
1733         },
1734         {
1735                 .procname       = "nr_open",
1736                 .data           = &sysctl_nr_open,
1737                 .maxlen         = sizeof(unsigned int),
1738                 .mode           = 0644,
1739                 .proc_handler   = proc_dointvec_minmax,
1740                 .extra1         = &sysctl_nr_open_min,
1741                 .extra2         = &sysctl_nr_open_max,
1742         },
1743         {
1744                 .procname       = "dentry-state",
1745                 .data           = &dentry_stat,
1746                 .maxlen         = 6*sizeof(long),
1747                 .mode           = 0444,
1748                 .proc_handler   = proc_nr_dentry,
1749         },
1750         {
1751                 .procname       = "overflowuid",
1752                 .data           = &fs_overflowuid,
1753                 .maxlen         = sizeof(int),
1754                 .mode           = 0644,
1755                 .proc_handler   = proc_dointvec_minmax,
1756                 .extra1         = &minolduid,
1757                 .extra2         = &maxolduid,
1758         },
1759         {
1760                 .procname       = "overflowgid",
1761                 .data           = &fs_overflowgid,
1762                 .maxlen         = sizeof(int),
1763                 .mode           = 0644,
1764                 .proc_handler   = proc_dointvec_minmax,
1765                 .extra1         = &minolduid,
1766                 .extra2         = &maxolduid,
1767         },
1768 #ifdef CONFIG_FILE_LOCKING
1769         {
1770                 .procname       = "leases-enable",
1771                 .data           = &leases_enable,
1772                 .maxlen         = sizeof(int),
1773                 .mode           = 0644,
1774                 .proc_handler   = proc_dointvec,
1775         },
1776 #endif
1777 #ifdef CONFIG_DNOTIFY
1778         {
1779                 .procname       = "dir-notify-enable",
1780                 .data           = &dir_notify_enable,
1781                 .maxlen         = sizeof(int),
1782                 .mode           = 0644,
1783                 .proc_handler   = proc_dointvec,
1784         },
1785 #endif
1786 #ifdef CONFIG_MMU
1787 #ifdef CONFIG_FILE_LOCKING
1788         {
1789                 .procname       = "lease-break-time",
1790                 .data           = &lease_break_time,
1791                 .maxlen         = sizeof(int),
1792                 .mode           = 0644,
1793                 .proc_handler   = proc_dointvec,
1794         },
1795 #endif
1796 #ifdef CONFIG_AIO
1797         {
1798                 .procname       = "aio-nr",
1799                 .data           = &aio_nr,
1800                 .maxlen         = sizeof(aio_nr),
1801                 .mode           = 0444,
1802                 .proc_handler   = proc_doulongvec_minmax,
1803         },
1804         {
1805                 .procname       = "aio-max-nr",
1806                 .data           = &aio_max_nr,
1807                 .maxlen         = sizeof(aio_max_nr),
1808                 .mode           = 0644,
1809                 .proc_handler   = proc_doulongvec_minmax,
1810         },
1811 #endif /* CONFIG_AIO */
1812 #ifdef CONFIG_INOTIFY_USER
1813         {
1814                 .procname       = "inotify",
1815                 .mode           = 0555,
1816                 .child          = inotify_table,
1817         },
1818 #endif  
1819 #ifdef CONFIG_EPOLL
1820         {
1821                 .procname       = "epoll",
1822                 .mode           = 0555,
1823                 .child          = epoll_table,
1824         },
1825 #endif
1826 #endif
1827         {
1828                 .procname       = "protected_symlinks",
1829                 .data           = &sysctl_protected_symlinks,
1830                 .maxlen         = sizeof(int),
1831                 .mode           = 0600,
1832                 .proc_handler   = proc_dointvec_minmax,
1833                 .extra1         = &zero,
1834                 .extra2         = &one,
1835         },
1836         {
1837                 .procname       = "protected_hardlinks",
1838                 .data           = &sysctl_protected_hardlinks,
1839                 .maxlen         = sizeof(int),
1840                 .mode           = 0600,
1841                 .proc_handler   = proc_dointvec_minmax,
1842                 .extra1         = &zero,
1843                 .extra2         = &one,
1844         },
1845         {
1846                 .procname       = "protected_fifos",
1847                 .data           = &sysctl_protected_fifos,
1848                 .maxlen         = sizeof(int),
1849                 .mode           = 0600,
1850                 .proc_handler   = proc_dointvec_minmax,
1851                 .extra1         = &zero,
1852                 .extra2         = &two,
1853         },
1854         {
1855                 .procname       = "protected_regular",
1856                 .data           = &sysctl_protected_regular,
1857                 .maxlen         = sizeof(int),
1858                 .mode           = 0600,
1859                 .proc_handler   = proc_dointvec_minmax,
1860                 .extra1         = &zero,
1861                 .extra2         = &two,
1862         },
1863         {
1864                 .procname       = "suid_dumpable",
1865                 .data           = &suid_dumpable,
1866                 .maxlen         = sizeof(int),
1867                 .mode           = 0644,
1868                 .proc_handler   = proc_dointvec_minmax_coredump,
1869                 .extra1         = &zero,
1870                 .extra2         = &two,
1871         },
1872 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1873         {
1874                 .procname       = "binfmt_misc",
1875                 .mode           = 0555,
1876                 .child          = sysctl_mount_point,
1877         },
1878 #endif
1879         {
1880                 .procname       = "pipe-max-size",
1881                 .data           = &pipe_max_size,
1882                 .maxlen         = sizeof(pipe_max_size),
1883                 .mode           = 0644,
1884                 .proc_handler   = proc_dopipe_max_size,
1885         },
1886         {
1887                 .procname       = "pipe-user-pages-hard",
1888                 .data           = &pipe_user_pages_hard,
1889                 .maxlen         = sizeof(pipe_user_pages_hard),
1890                 .mode           = 0644,
1891                 .proc_handler   = proc_doulongvec_minmax,
1892         },
1893         {
1894                 .procname       = "pipe-user-pages-soft",
1895                 .data           = &pipe_user_pages_soft,
1896                 .maxlen         = sizeof(pipe_user_pages_soft),
1897                 .mode           = 0644,
1898                 .proc_handler   = proc_doulongvec_minmax,
1899         },
1900         {
1901                 .procname       = "mount-max",
1902                 .data           = &sysctl_mount_max,
1903                 .maxlen         = sizeof(unsigned int),
1904                 .mode           = 0644,
1905                 .proc_handler   = proc_dointvec_minmax,
1906                 .extra1         = &one,
1907         },
1908         { }
1909 };
1910
1911 static struct ctl_table debug_table[] = {
1912 #ifdef CONFIG_SYSCTL_EXCEPTION_TRACE
1913         {
1914                 .procname       = "exception-trace",
1915                 .data           = &show_unhandled_signals,
1916                 .maxlen         = sizeof(int),
1917                 .mode           = 0644,
1918                 .proc_handler   = proc_dointvec
1919         },
1920 #endif
1921 #if defined(CONFIG_OPTPROBES)
1922         {
1923                 .procname       = "kprobes-optimization",
1924                 .data           = &sysctl_kprobes_optimization,
1925                 .maxlen         = sizeof(int),
1926                 .mode           = 0644,
1927                 .proc_handler   = proc_kprobes_optimization_handler,
1928                 .extra1         = &zero,
1929                 .extra2         = &one,
1930         },
1931 #endif
1932         { }
1933 };
1934
1935 static struct ctl_table dev_table[] = {
1936         { }
1937 };
1938
1939 int __init sysctl_init(void)
1940 {
1941         struct ctl_table_header *hdr;
1942
1943         hdr = register_sysctl_table(sysctl_base_table);
1944         kmemleak_not_leak(hdr);
1945         return 0;
1946 }
1947
1948 #endif /* CONFIG_SYSCTL */
1949
1950 /*
1951  * /proc/sys support
1952  */
1953
1954 #ifdef CONFIG_PROC_SYSCTL
1955
1956 static int _proc_do_string(char *data, int maxlen, int write,
1957                            char __user *buffer,
1958                            size_t *lenp, loff_t *ppos)
1959 {
1960         size_t len;
1961         char __user *p;
1962         char c;
1963
1964         if (!data || !maxlen || !*lenp) {
1965                 *lenp = 0;
1966                 return 0;
1967         }
1968
1969         if (write) {
1970                 if (sysctl_writes_strict == SYSCTL_WRITES_STRICT) {
1971                         /* Only continue writes not past the end of buffer. */
1972                         len = strlen(data);
1973                         if (len > maxlen - 1)
1974                                 len = maxlen - 1;
1975
1976                         if (*ppos > len)
1977                                 return 0;
1978                         len = *ppos;
1979                 } else {
1980                         /* Start writing from beginning of buffer. */
1981                         len = 0;
1982                 }
1983
1984                 *ppos += *lenp;
1985                 p = buffer;
1986                 while ((p - buffer) < *lenp && len < maxlen - 1) {
1987                         if (get_user(c, p++))
1988                                 return -EFAULT;
1989                         if (c == 0 || c == '\n')
1990                                 break;
1991                         data[len++] = c;
1992                 }
1993                 data[len] = 0;
1994         } else {
1995                 len = strlen(data);
1996                 if (len > maxlen)
1997                         len = maxlen;
1998
1999                 if (*ppos > len) {
2000                         *lenp = 0;
2001                         return 0;
2002                 }
2003
2004                 data += *ppos;
2005                 len  -= *ppos;
2006
2007                 if (len > *lenp)
2008                         len = *lenp;
2009                 if (len)
2010                         if (copy_to_user(buffer, data, len))
2011                                 return -EFAULT;
2012                 if (len < *lenp) {
2013                         if (put_user('\n', buffer + len))
2014                                 return -EFAULT;
2015                         len++;
2016                 }
2017                 *lenp = len;
2018                 *ppos += len;
2019         }
2020         return 0;
2021 }
2022
2023 static void warn_sysctl_write(struct ctl_table *table)
2024 {
2025         pr_warn_once("%s wrote to %s when file position was not 0!\n"
2026                 "This will not be supported in the future. To silence this\n"
2027                 "warning, set kernel.sysctl_writes_strict = -1\n",
2028                 current->comm, table->procname);
2029 }
2030
2031 /**
2032  * proc_first_pos_non_zero_ignore - check if first position is allowed
2033  * @ppos: file position
2034  * @table: the sysctl table
2035  *
2036  * Returns true if the first position is non-zero and the sysctl_writes_strict
2037  * mode indicates this is not allowed for numeric input types. String proc
2038  * handlers can ignore the return value.
2039  */
2040 static bool proc_first_pos_non_zero_ignore(loff_t *ppos,
2041                                            struct ctl_table *table)
2042 {
2043         if (!*ppos)
2044                 return false;
2045
2046         switch (sysctl_writes_strict) {
2047         case SYSCTL_WRITES_STRICT:
2048                 return true;
2049         case SYSCTL_WRITES_WARN:
2050                 warn_sysctl_write(table);
2051                 return false;
2052         default:
2053                 return false;
2054         }
2055 }
2056
2057 /**
2058  * proc_dostring - read a string sysctl
2059  * @table: the sysctl table
2060  * @write: %TRUE if this is a write to the sysctl file
2061  * @buffer: the user buffer
2062  * @lenp: the size of the user buffer
2063  * @ppos: file position
2064  *
2065  * Reads/writes a string from/to the user buffer. If the kernel
2066  * buffer provided is not large enough to hold the string, the
2067  * string is truncated. The copied string is %NULL-terminated.
2068  * If the string is being read by the user process, it is copied
2069  * and a newline '\n' is added. It is truncated if the buffer is
2070  * not large enough.
2071  *
2072  * Returns 0 on success.
2073  */
2074 int proc_dostring(struct ctl_table *table, int write,
2075                   void __user *buffer, size_t *lenp, loff_t *ppos)
2076 {
2077         if (write)
2078                 proc_first_pos_non_zero_ignore(ppos, table);
2079
2080         return _proc_do_string((char *)(table->data), table->maxlen, write,
2081                                (char __user *)buffer, lenp, ppos);
2082 }
2083
2084 static size_t proc_skip_spaces(char **buf)
2085 {
2086         size_t ret;
2087         char *tmp = skip_spaces(*buf);
2088         ret = tmp - *buf;
2089         *buf = tmp;
2090         return ret;
2091 }
2092
2093 static void proc_skip_char(char **buf, size_t *size, const char v)
2094 {
2095         while (*size) {
2096                 if (**buf != v)
2097                         break;
2098                 (*size)--;
2099                 (*buf)++;
2100         }
2101 }
2102
2103 /**
2104  * strtoul_lenient - parse an ASCII formatted integer from a buffer and only
2105  *                   fail on overflow
2106  *
2107  * @cp: kernel buffer containing the string to parse
2108  * @endp: pointer to store the trailing characters
2109  * @base: the base to use
2110  * @res: where the parsed integer will be stored
2111  *
2112  * In case of success 0 is returned and @res will contain the parsed integer,
2113  * @endp will hold any trailing characters.
2114  * This function will fail the parse on overflow. If there wasn't an overflow
2115  * the function will defer the decision what characters count as invalid to the
2116  * caller.
2117  */
2118 static int strtoul_lenient(const char *cp, char **endp, unsigned int base,
2119                            unsigned long *res)
2120 {
2121         unsigned long long result;
2122         unsigned int rv;
2123
2124         cp = _parse_integer_fixup_radix(cp, &base);
2125         rv = _parse_integer(cp, base, &result);
2126         if ((rv & KSTRTOX_OVERFLOW) || (result != (unsigned long)result))
2127                 return -ERANGE;
2128
2129         cp += rv;
2130
2131         if (endp)
2132                 *endp = (char *)cp;
2133
2134         *res = (unsigned long)result;
2135         return 0;
2136 }
2137
2138 #define TMPBUFLEN 22
2139 /**
2140  * proc_get_long - reads an ASCII formatted integer from a user buffer
2141  *
2142  * @buf: a kernel buffer
2143  * @size: size of the kernel buffer
2144  * @val: this is where the number will be stored
2145  * @neg: set to %TRUE if number is negative
2146  * @perm_tr: a vector which contains the allowed trailers
2147  * @perm_tr_len: size of the perm_tr vector
2148  * @tr: pointer to store the trailer character
2149  *
2150  * In case of success %0 is returned and @buf and @size are updated with
2151  * the amount of bytes read. If @tr is non-NULL and a trailing
2152  * character exists (size is non-zero after returning from this
2153  * function), @tr is updated with the trailing character.
2154  */
2155 static int proc_get_long(char **buf, size_t *size,
2156                           unsigned long *val, bool *neg,
2157                           const char *perm_tr, unsigned perm_tr_len, char *tr)
2158 {
2159         int len;
2160         char *p, tmp[TMPBUFLEN];
2161
2162         if (!*size)
2163                 return -EINVAL;
2164
2165         len = *size;
2166         if (len > TMPBUFLEN - 1)
2167                 len = TMPBUFLEN - 1;
2168
2169         memcpy(tmp, *buf, len);
2170
2171         tmp[len] = 0;
2172         p = tmp;
2173         if (*p == '-' && *size > 1) {
2174                 *neg = true;
2175                 p++;
2176         } else
2177                 *neg = false;
2178         if (!isdigit(*p))
2179                 return -EINVAL;
2180
2181         if (strtoul_lenient(p, &p, 0, val))
2182                 return -EINVAL;
2183
2184         len = p - tmp;
2185
2186         /* We don't know if the next char is whitespace thus we may accept
2187          * invalid integers (e.g. 1234...a) or two integers instead of one
2188          * (e.g. 123...1). So lets not allow such large numbers. */
2189         if (len == TMPBUFLEN - 1)
2190                 return -EINVAL;
2191
2192         if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
2193                 return -EINVAL;
2194
2195         if (tr && (len < *size))
2196                 *tr = *p;
2197
2198         *buf += len;
2199         *size -= len;
2200
2201         return 0;
2202 }
2203
2204 /**
2205  * proc_put_long - converts an integer to a decimal ASCII formatted string
2206  *
2207  * @buf: the user buffer
2208  * @size: the size of the user buffer
2209  * @val: the integer to be converted
2210  * @neg: sign of the number, %TRUE for negative
2211  *
2212  * In case of success %0 is returned and @buf and @size are updated with
2213  * the amount of bytes written.
2214  */
2215 static int proc_put_long(void __user **buf, size_t *size, unsigned long val,
2216                           bool neg)
2217 {
2218         int len;
2219         char tmp[TMPBUFLEN], *p = tmp;
2220
2221         sprintf(p, "%s%lu", neg ? "-" : "", val);
2222         len = strlen(tmp);
2223         if (len > *size)
2224                 len = *size;
2225         if (copy_to_user(*buf, tmp, len))
2226                 return -EFAULT;
2227         *size -= len;
2228         *buf += len;
2229         return 0;
2230 }
2231 #undef TMPBUFLEN
2232
2233 static int proc_put_char(void __user **buf, size_t *size, char c)
2234 {
2235         if (*size) {
2236                 char __user **buffer = (char __user **)buf;
2237                 if (put_user(c, *buffer))
2238                         return -EFAULT;
2239                 (*size)--, (*buffer)++;
2240                 *buf = *buffer;
2241         }
2242         return 0;
2243 }
2244
2245 static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
2246                                  int *valp,
2247                                  int write, void *data)
2248 {
2249         if (write) {
2250                 if (*negp) {
2251                         if (*lvalp > (unsigned long) INT_MAX + 1)
2252                                 return -EINVAL;
2253                         *valp = -*lvalp;
2254                 } else {
2255                         if (*lvalp > (unsigned long) INT_MAX)
2256                                 return -EINVAL;
2257                         *valp = *lvalp;
2258                 }
2259         } else {
2260                 int val = *valp;
2261                 if (val < 0) {
2262                         *negp = true;
2263                         *lvalp = -(unsigned long)val;
2264                 } else {
2265                         *negp = false;
2266                         *lvalp = (unsigned long)val;
2267                 }
2268         }
2269         return 0;
2270 }
2271
2272 static int do_proc_douintvec_conv(unsigned long *lvalp,
2273                                   unsigned int *valp,
2274                                   int write, void *data)
2275 {
2276         if (write) {
2277                 if (*lvalp > UINT_MAX)
2278                         return -EINVAL;
2279                 *valp = *lvalp;
2280         } else {
2281                 unsigned int val = *valp;
2282                 *lvalp = (unsigned long)val;
2283         }
2284         return 0;
2285 }
2286
2287 static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
2288
2289 static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
2290                   int write, void __user *buffer,
2291                   size_t *lenp, loff_t *ppos,
2292                   int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
2293                               int write, void *data),
2294                   void *data)
2295 {
2296         int *i, vleft, first = 1, err = 0;
2297         size_t left;
2298         char *kbuf = NULL, *p;
2299         
2300         if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
2301                 *lenp = 0;
2302                 return 0;
2303         }
2304         
2305         i = (int *) tbl_data;
2306         vleft = table->maxlen / sizeof(*i);
2307         left = *lenp;
2308
2309         if (!conv)
2310                 conv = do_proc_dointvec_conv;
2311
2312         if (write) {
2313                 if (proc_first_pos_non_zero_ignore(ppos, table))
2314                         goto out;
2315
2316                 if (left > PAGE_SIZE - 1)
2317                         left = PAGE_SIZE - 1;
2318                 p = kbuf = memdup_user_nul(buffer, left);
2319                 if (IS_ERR(kbuf))
2320                         return PTR_ERR(kbuf);
2321         }
2322
2323         for (; left && vleft--; i++, first=0) {
2324                 unsigned long lval;
2325                 bool neg;
2326
2327                 if (write) {
2328                         left -= proc_skip_spaces(&p);
2329
2330                         if (!left)
2331                                 break;
2332                         err = proc_get_long(&p, &left, &lval, &neg,
2333                                              proc_wspace_sep,
2334                                              sizeof(proc_wspace_sep), NULL);
2335                         if (err)
2336                                 break;
2337                         if (conv(&neg, &lval, i, 1, data)) {
2338                                 err = -EINVAL;
2339                                 break;
2340                         }
2341                 } else {
2342                         if (conv(&neg, &lval, i, 0, data)) {
2343                                 err = -EINVAL;
2344                                 break;
2345                         }
2346                         if (!first)
2347                                 err = proc_put_char(&buffer, &left, '\t');
2348                         if (err)
2349                                 break;
2350                         err = proc_put_long(&buffer, &left, lval, neg);
2351                         if (err)
2352                                 break;
2353                 }
2354         }
2355
2356         if (!write && !first && left && !err)
2357                 err = proc_put_char(&buffer, &left, '\n');
2358         if (write && !err && left)
2359                 left -= proc_skip_spaces(&p);
2360         if (write) {
2361                 kfree(kbuf);
2362                 if (first)
2363                         return err ? : -EINVAL;
2364         }
2365         *lenp -= left;
2366 out:
2367         *ppos += *lenp;
2368         return err;
2369 }
2370
2371 static int do_proc_dointvec(struct ctl_table *table, int write,
2372                   void __user *buffer, size_t *lenp, loff_t *ppos,
2373                   int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
2374                               int write, void *data),
2375                   void *data)
2376 {
2377         return __do_proc_dointvec(table->data, table, write,
2378                         buffer, lenp, ppos, conv, data);
2379 }
2380
2381 static int do_proc_douintvec_w(unsigned int *tbl_data,
2382                                struct ctl_table *table,
2383                                void __user *buffer,
2384                                size_t *lenp, loff_t *ppos,
2385                                int (*conv)(unsigned long *lvalp,
2386                                            unsigned int *valp,
2387                                            int write, void *data),
2388                                void *data)
2389 {
2390         unsigned long lval;
2391         int err = 0;
2392         size_t left;
2393         bool neg;
2394         char *kbuf = NULL, *p;
2395
2396         left = *lenp;
2397
2398         if (proc_first_pos_non_zero_ignore(ppos, table))
2399                 goto bail_early;
2400
2401         if (left > PAGE_SIZE - 1)
2402                 left = PAGE_SIZE - 1;
2403
2404         p = kbuf = memdup_user_nul(buffer, left);
2405         if (IS_ERR(kbuf))
2406                 return -EINVAL;
2407
2408         left -= proc_skip_spaces(&p);
2409         if (!left) {
2410                 err = -EINVAL;
2411                 goto out_free;
2412         }
2413
2414         err = proc_get_long(&p, &left, &lval, &neg,
2415                              proc_wspace_sep,
2416                              sizeof(proc_wspace_sep), NULL);
2417         if (err || neg) {
2418                 err = -EINVAL;
2419                 goto out_free;
2420         }
2421
2422         if (conv(&lval, tbl_data, 1, data)) {
2423                 err = -EINVAL;
2424                 goto out_free;
2425         }
2426
2427         if (!err && left)
2428                 left -= proc_skip_spaces(&p);
2429
2430 out_free:
2431         kfree(kbuf);
2432         if (err)
2433                 return -EINVAL;
2434
2435         return 0;
2436
2437         /* This is in keeping with old __do_proc_dointvec() */
2438 bail_early:
2439         *ppos += *lenp;
2440         return err;
2441 }
2442
2443 static int do_proc_douintvec_r(unsigned int *tbl_data, void __user *buffer,
2444                                size_t *lenp, loff_t *ppos,
2445                                int (*conv)(unsigned long *lvalp,
2446                                            unsigned int *valp,
2447                                            int write, void *data),
2448                                void *data)
2449 {
2450         unsigned long lval;
2451         int err = 0;
2452         size_t left;
2453
2454         left = *lenp;
2455
2456         if (conv(&lval, tbl_data, 0, data)) {
2457                 err = -EINVAL;
2458                 goto out;
2459         }
2460
2461         err = proc_put_long(&buffer, &left, lval, false);
2462         if (err || !left)
2463                 goto out;
2464
2465         err = proc_put_char(&buffer, &left, '\n');
2466
2467 out:
2468         *lenp -= left;
2469         *ppos += *lenp;
2470
2471         return err;
2472 }
2473
2474 static int __do_proc_douintvec(void *tbl_data, struct ctl_table *table,
2475                                int write, void __user *buffer,
2476                                size_t *lenp, loff_t *ppos,
2477                                int (*conv)(unsigned long *lvalp,
2478                                            unsigned int *valp,
2479                                            int write, void *data),
2480                                void *data)
2481 {
2482         unsigned int *i, vleft;
2483
2484         if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
2485                 *lenp = 0;
2486                 return 0;
2487         }
2488
2489         i = (unsigned int *) tbl_data;
2490         vleft = table->maxlen / sizeof(*i);
2491
2492         /*
2493          * Arrays are not supported, keep this simple. *Do not* add
2494          * support for them.
2495          */
2496         if (vleft != 1) {
2497                 *lenp = 0;
2498                 return -EINVAL;
2499         }
2500
2501         if (!conv)
2502                 conv = do_proc_douintvec_conv;
2503
2504         if (write)
2505                 return do_proc_douintvec_w(i, table, buffer, lenp, ppos,
2506                                            conv, data);
2507         return do_proc_douintvec_r(i, buffer, lenp, ppos, conv, data);
2508 }
2509
2510 static int do_proc_douintvec(struct ctl_table *table, int write,
2511                              void __user *buffer, size_t *lenp, loff_t *ppos,
2512                              int (*conv)(unsigned long *lvalp,
2513                                          unsigned int *valp,
2514                                          int write, void *data),
2515                              void *data)
2516 {
2517         return __do_proc_douintvec(table->data, table, write,
2518                                    buffer, lenp, ppos, conv, data);
2519 }
2520
2521 /**
2522  * proc_dointvec - read a vector of integers
2523  * @table: the sysctl table
2524  * @write: %TRUE if this is a write to the sysctl file
2525  * @buffer: the user buffer
2526  * @lenp: the size of the user buffer
2527  * @ppos: file position
2528  *
2529  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2530  * values from/to the user buffer, treated as an ASCII string. 
2531  *
2532  * Returns 0 on success.
2533  */
2534 int proc_dointvec(struct ctl_table *table, int write,
2535                      void __user *buffer, size_t *lenp, loff_t *ppos)
2536 {
2537         return do_proc_dointvec(table, write, buffer, lenp, ppos, NULL, NULL);
2538 }
2539
2540 /**
2541  * proc_douintvec - read a vector of unsigned integers
2542  * @table: the sysctl table
2543  * @write: %TRUE if this is a write to the sysctl file
2544  * @buffer: the user buffer
2545  * @lenp: the size of the user buffer
2546  * @ppos: file position
2547  *
2548  * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
2549  * values from/to the user buffer, treated as an ASCII string.
2550  *
2551  * Returns 0 on success.
2552  */
2553 int proc_douintvec(struct ctl_table *table, int write,
2554                      void __user *buffer, size_t *lenp, loff_t *ppos)
2555 {
2556         return do_proc_douintvec(table, write, buffer, lenp, ppos,
2557                                  do_proc_douintvec_conv, NULL);
2558 }
2559
2560 /*
2561  * Taint values can only be increased
2562  * This means we can safely use a temporary.
2563  */
2564 static int proc_taint(struct ctl_table *table, int write,
2565                                void __user *buffer, size_t *lenp, loff_t *ppos)
2566 {
2567         struct ctl_table t;
2568         unsigned long tmptaint = get_taint();
2569         int err;
2570
2571         if (write && !capable(CAP_SYS_ADMIN))
2572                 return -EPERM;
2573
2574         t = *table;
2575         t.data = &tmptaint;
2576         err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
2577         if (err < 0)
2578                 return err;
2579
2580         if (write) {
2581                 /*
2582                  * Poor man's atomic or. Not worth adding a primitive
2583                  * to everyone's atomic.h for this
2584                  */
2585                 int i;
2586                 for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) {
2587                         if ((tmptaint >> i) & 1)
2588                                 add_taint(i, LOCKDEP_STILL_OK);
2589                 }
2590         }
2591
2592         return err;
2593 }
2594
2595 #ifdef CONFIG_PRINTK
2596 static int proc_dointvec_minmax_sysadmin(struct ctl_table *table, int write,
2597                                 void __user *buffer, size_t *lenp, loff_t *ppos)
2598 {
2599         if (write && !capable(CAP_SYS_ADMIN))
2600                 return -EPERM;
2601
2602         return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2603 }
2604 #endif
2605
2606 /**
2607  * struct do_proc_dointvec_minmax_conv_param - proc_dointvec_minmax() range checking structure
2608  * @min: pointer to minimum allowable value
2609  * @max: pointer to maximum allowable value
2610  *
2611  * The do_proc_dointvec_minmax_conv_param structure provides the
2612  * minimum and maximum values for doing range checking for those sysctl
2613  * parameters that use the proc_dointvec_minmax() handler.
2614  */
2615 struct do_proc_dointvec_minmax_conv_param {
2616         int *min;
2617         int *max;
2618 };
2619
2620 static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
2621                                         int *valp,
2622                                         int write, void *data)
2623 {
2624         struct do_proc_dointvec_minmax_conv_param *param = data;
2625         if (write) {
2626                 int val;
2627                 if (*negp) {
2628                         if (*lvalp > (unsigned long) INT_MAX + 1)
2629                                 return -EINVAL;
2630                         val = -*lvalp;
2631                 } else {
2632                         if (*lvalp > (unsigned long) INT_MAX)
2633                                 return -EINVAL;
2634                         val = *lvalp;
2635                 }
2636                 if ((param->min && *param->min > val) ||
2637                     (param->max && *param->max < val))
2638                         return -EINVAL;
2639                 *valp = val;
2640         } else {
2641                 int val = *valp;
2642                 if (val < 0) {
2643                         *negp = true;
2644                         *lvalp = -(unsigned long)val;
2645                 } else {
2646                         *negp = false;
2647                         *lvalp = (unsigned long)val;
2648                 }
2649         }
2650         return 0;
2651 }
2652
2653 /**
2654  * proc_dointvec_minmax - read a vector of integers with min/max values
2655  * @table: the sysctl table
2656  * @write: %TRUE if this is a write to the sysctl file
2657  * @buffer: the user buffer
2658  * @lenp: the size of the user buffer
2659  * @ppos: file position
2660  *
2661  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2662  * values from/to the user buffer, treated as an ASCII string.
2663  *
2664  * This routine will ensure the values are within the range specified by
2665  * table->extra1 (min) and table->extra2 (max).
2666  *
2667  * Returns 0 on success or -EINVAL on write when the range check fails.
2668  */
2669 int proc_dointvec_minmax(struct ctl_table *table, int write,
2670                   void __user *buffer, size_t *lenp, loff_t *ppos)
2671 {
2672         struct do_proc_dointvec_minmax_conv_param param = {
2673                 .min = (int *) table->extra1,
2674                 .max = (int *) table->extra2,
2675         };
2676         return do_proc_dointvec(table, write, buffer, lenp, ppos,
2677                                 do_proc_dointvec_minmax_conv, &param);
2678 }
2679
2680 /**
2681  * struct do_proc_douintvec_minmax_conv_param - proc_douintvec_minmax() range checking structure
2682  * @min: pointer to minimum allowable value
2683  * @max: pointer to maximum allowable value
2684  *
2685  * The do_proc_douintvec_minmax_conv_param structure provides the
2686  * minimum and maximum values for doing range checking for those sysctl
2687  * parameters that use the proc_douintvec_minmax() handler.
2688  */
2689 struct do_proc_douintvec_minmax_conv_param {
2690         unsigned int *min;
2691         unsigned int *max;
2692 };
2693
2694 static int do_proc_douintvec_minmax_conv(unsigned long *lvalp,
2695                                          unsigned int *valp,
2696                                          int write, void *data)
2697 {
2698         struct do_proc_douintvec_minmax_conv_param *param = data;
2699
2700         if (write) {
2701                 unsigned int val = *lvalp;
2702
2703                 if (*lvalp > UINT_MAX)
2704                         return -EINVAL;
2705
2706                 if ((param->min && *param->min > val) ||
2707                     (param->max && *param->max < val))
2708                         return -ERANGE;
2709
2710                 *valp = val;
2711         } else {
2712                 unsigned int val = *valp;
2713                 *lvalp = (unsigned long) val;
2714         }
2715
2716         return 0;
2717 }
2718
2719 /**
2720  * proc_douintvec_minmax - read a vector of unsigned ints with min/max values
2721  * @table: the sysctl table
2722  * @write: %TRUE if this is a write to the sysctl file
2723  * @buffer: the user buffer
2724  * @lenp: the size of the user buffer
2725  * @ppos: file position
2726  *
2727  * Reads/writes up to table->maxlen/sizeof(unsigned int) unsigned integer
2728  * values from/to the user buffer, treated as an ASCII string. Negative
2729  * strings are not allowed.
2730  *
2731  * This routine will ensure the values are within the range specified by
2732  * table->extra1 (min) and table->extra2 (max). There is a final sanity
2733  * check for UINT_MAX to avoid having to support wrap around uses from
2734  * userspace.
2735  *
2736  * Returns 0 on success or -ERANGE on write when the range check fails.
2737  */
2738 int proc_douintvec_minmax(struct ctl_table *table, int write,
2739                           void __user *buffer, size_t *lenp, loff_t *ppos)
2740 {
2741         struct do_proc_douintvec_minmax_conv_param param = {
2742                 .min = (unsigned int *) table->extra1,
2743                 .max = (unsigned int *) table->extra2,
2744         };
2745         return do_proc_douintvec(table, write, buffer, lenp, ppos,
2746                                  do_proc_douintvec_minmax_conv, &param);
2747 }
2748
2749 static int do_proc_dopipe_max_size_conv(unsigned long *lvalp,
2750                                         unsigned int *valp,
2751                                         int write, void *data)
2752 {
2753         if (write) {
2754                 unsigned int val;
2755
2756                 val = round_pipe_size(*lvalp);
2757                 if (val == 0)
2758                         return -EINVAL;
2759
2760                 *valp = val;
2761         } else {
2762                 unsigned int val = *valp;
2763                 *lvalp = (unsigned long) val;
2764         }
2765
2766         return 0;
2767 }
2768
2769 static int proc_dopipe_max_size(struct ctl_table *table, int write,
2770                                 void __user *buffer, size_t *lenp, loff_t *ppos)
2771 {
2772         return do_proc_douintvec(table, write, buffer, lenp, ppos,
2773                                  do_proc_dopipe_max_size_conv, NULL);
2774 }
2775
2776 static void validate_coredump_safety(void)
2777 {
2778 #ifdef CONFIG_COREDUMP
2779         if (suid_dumpable == SUID_DUMP_ROOT &&
2780             core_pattern[0] != '/' && core_pattern[0] != '|') {
2781                 printk(KERN_WARNING
2782 "Unsafe core_pattern used with fs.suid_dumpable=2.\n"
2783 "Pipe handler or fully qualified core dump path required.\n"
2784 "Set kernel.core_pattern before fs.suid_dumpable.\n"
2785                 );
2786         }
2787 #endif
2788 }
2789
2790 static int proc_dointvec_minmax_coredump(struct ctl_table *table, int write,
2791                 void __user *buffer, size_t *lenp, loff_t *ppos)
2792 {
2793         int error = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2794         if (!error)
2795                 validate_coredump_safety();
2796         return error;
2797 }
2798
2799 #ifdef CONFIG_COREDUMP
2800 static int proc_dostring_coredump(struct ctl_table *table, int write,
2801                   void __user *buffer, size_t *lenp, loff_t *ppos)
2802 {
2803         int error = proc_dostring(table, write, buffer, lenp, ppos);
2804         if (!error)
2805                 validate_coredump_safety();
2806         return error;
2807 }
2808 #endif
2809
2810 static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int write,
2811                                      void __user *buffer,
2812                                      size_t *lenp, loff_t *ppos,
2813                                      unsigned long convmul,
2814                                      unsigned long convdiv)
2815 {
2816         unsigned long *i, *min, *max;
2817         int vleft, first = 1, err = 0;
2818         size_t left;
2819         char *kbuf = NULL, *p;
2820
2821         if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
2822                 *lenp = 0;
2823                 return 0;
2824         }
2825
2826         i = (unsigned long *) data;
2827         min = (unsigned long *) table->extra1;
2828         max = (unsigned long *) table->extra2;
2829         vleft = table->maxlen / sizeof(unsigned long);
2830         left = *lenp;
2831
2832         if (write) {
2833                 if (proc_first_pos_non_zero_ignore(ppos, table))
2834                         goto out;
2835
2836                 if (left > PAGE_SIZE - 1)
2837                         left = PAGE_SIZE - 1;
2838                 p = kbuf = memdup_user_nul(buffer, left);
2839                 if (IS_ERR(kbuf))
2840                         return PTR_ERR(kbuf);
2841         }
2842
2843         for (; left && vleft--; i++, first = 0) {
2844                 unsigned long val;
2845
2846                 if (write) {
2847                         bool neg;
2848
2849                         left -= proc_skip_spaces(&p);
2850                         if (!left)
2851                                 break;
2852
2853                         err = proc_get_long(&p, &left, &val, &neg,
2854                                              proc_wspace_sep,
2855                                              sizeof(proc_wspace_sep), NULL);
2856                         if (err)
2857                                 break;
2858                         if (neg)
2859                                 continue;
2860                         val = convmul * val / convdiv;
2861                         if ((min && val < *min) || (max && val > *max)) {
2862                                 err = -EINVAL;
2863                                 break;
2864                         }
2865                         *i = val;
2866                 } else {
2867                         val = convdiv * (*i) / convmul;
2868                         if (!first) {
2869                                 err = proc_put_char(&buffer, &left, '\t');
2870                                 if (err)
2871                                         break;
2872                         }
2873                         err = proc_put_long(&buffer, &left, val, false);
2874                         if (err)
2875                                 break;
2876                 }
2877         }
2878
2879         if (!write && !first && left && !err)
2880                 err = proc_put_char(&buffer, &left, '\n');
2881         if (write && !err)
2882                 left -= proc_skip_spaces(&p);
2883         if (write) {
2884                 kfree(kbuf);
2885                 if (first)
2886                         return err ? : -EINVAL;
2887         }
2888         *lenp -= left;
2889 out:
2890         *ppos += *lenp;
2891         return err;
2892 }
2893
2894 static int do_proc_doulongvec_minmax(struct ctl_table *table, int write,
2895                                      void __user *buffer,
2896                                      size_t *lenp, loff_t *ppos,
2897                                      unsigned long convmul,
2898                                      unsigned long convdiv)
2899 {
2900         return __do_proc_doulongvec_minmax(table->data, table, write,
2901                         buffer, lenp, ppos, convmul, convdiv);
2902 }
2903
2904 /**
2905  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2906  * @table: the sysctl table
2907  * @write: %TRUE if this is a write to the sysctl file
2908  * @buffer: the user buffer
2909  * @lenp: the size of the user buffer
2910  * @ppos: file position
2911  *
2912  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2913  * values from/to the user buffer, treated as an ASCII string.
2914  *
2915  * This routine will ensure the values are within the range specified by
2916  * table->extra1 (min) and table->extra2 (max).
2917  *
2918  * Returns 0 on success.
2919  */
2920 int proc_doulongvec_minmax(struct ctl_table *table, int write,
2921                            void __user *buffer, size_t *lenp, loff_t *ppos)
2922 {
2923     return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
2924 }
2925
2926 /**
2927  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2928  * @table: the sysctl table
2929  * @write: %TRUE if this is a write to the sysctl file
2930  * @buffer: the user buffer
2931  * @lenp: the size of the user buffer
2932  * @ppos: file position
2933  *
2934  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2935  * values from/to the user buffer, treated as an ASCII string. The values
2936  * are treated as milliseconds, and converted to jiffies when they are stored.
2937  *
2938  * This routine will ensure the values are within the range specified by
2939  * table->extra1 (min) and table->extra2 (max).
2940  *
2941  * Returns 0 on success.
2942  */
2943 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
2944                                       void __user *buffer,
2945                                       size_t *lenp, loff_t *ppos)
2946 {
2947     return do_proc_doulongvec_minmax(table, write, buffer,
2948                                      lenp, ppos, HZ, 1000l);
2949 }
2950
2951
2952 static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
2953                                          int *valp,
2954                                          int write, void *data)
2955 {
2956         if (write) {
2957                 if (*lvalp > INT_MAX / HZ)
2958                         return 1;
2959                 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2960         } else {
2961                 int val = *valp;
2962                 unsigned long lval;
2963                 if (val < 0) {
2964                         *negp = true;
2965                         lval = -(unsigned long)val;
2966                 } else {
2967                         *negp = false;
2968                         lval = (unsigned long)val;
2969                 }
2970                 *lvalp = lval / HZ;
2971         }
2972         return 0;
2973 }
2974
2975 static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
2976                                                 int *valp,
2977                                                 int write, void *data)
2978 {
2979         if (write) {
2980                 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2981                         return 1;
2982                 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2983         } else {
2984                 int val = *valp;
2985                 unsigned long lval;
2986                 if (val < 0) {
2987                         *negp = true;
2988                         lval = -(unsigned long)val;
2989                 } else {
2990                         *negp = false;
2991                         lval = (unsigned long)val;
2992                 }
2993                 *lvalp = jiffies_to_clock_t(lval);
2994         }
2995         return 0;
2996 }
2997
2998 static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
2999                                             int *valp,
3000                                             int write, void *data)
3001 {
3002         if (write) {
3003                 unsigned long jif = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
3004
3005                 if (jif > INT_MAX)
3006                         return 1;
3007                 *valp = (int)jif;
3008         } else {
3009                 int val = *valp;
3010                 unsigned long lval;
3011                 if (val < 0) {
3012                         *negp = true;
3013                         lval = -(unsigned long)val;
3014                 } else {
3015                         *negp = false;
3016                         lval = (unsigned long)val;
3017                 }
3018                 *lvalp = jiffies_to_msecs(lval);
3019         }
3020         return 0;
3021 }
3022
3023 /**
3024  * proc_dointvec_jiffies - read a vector of integers as seconds
3025  * @table: the sysctl table
3026  * @write: %TRUE if this is a write to the sysctl file
3027  * @buffer: the user buffer
3028  * @lenp: the size of the user buffer
3029  * @ppos: file position
3030  *
3031  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
3032  * values from/to the user buffer, treated as an ASCII string. 
3033  * The values read are assumed to be in seconds, and are converted into
3034  * jiffies.
3035  *
3036  * Returns 0 on success.
3037  */
3038 int proc_dointvec_jiffies(struct ctl_table *table, int write,
3039                           void __user *buffer, size_t *lenp, loff_t *ppos)
3040 {
3041     return do_proc_dointvec(table,write,buffer,lenp,ppos,
3042                             do_proc_dointvec_jiffies_conv,NULL);
3043 }
3044
3045 /**
3046  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
3047  * @table: the sysctl table
3048  * @write: %TRUE if this is a write to the sysctl file
3049  * @buffer: the user buffer
3050  * @lenp: the size of the user buffer
3051  * @ppos: pointer to the file position
3052  *
3053  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
3054  * values from/to the user buffer, treated as an ASCII string. 
3055  * The values read are assumed to be in 1/USER_HZ seconds, and 
3056  * are converted into jiffies.
3057  *
3058  * Returns 0 on success.
3059  */
3060 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
3061                                  void __user *buffer, size_t *lenp, loff_t *ppos)
3062 {
3063     return do_proc_dointvec(table,write,buffer,lenp,ppos,
3064                             do_proc_dointvec_userhz_jiffies_conv,NULL);
3065 }
3066
3067 /**
3068  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
3069  * @table: the sysctl table
3070  * @write: %TRUE if this is a write to the sysctl file
3071  * @buffer: the user buffer
3072  * @lenp: the size of the user buffer
3073  * @ppos: file position
3074  * @ppos: the current position in the file
3075  *
3076  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
3077  * values from/to the user buffer, treated as an ASCII string. 
3078  * The values read are assumed to be in 1/1000 seconds, and 
3079  * are converted into jiffies.
3080  *
3081  * Returns 0 on success.
3082  */
3083 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
3084                              void __user *buffer, size_t *lenp, loff_t *ppos)
3085 {
3086         return do_proc_dointvec(table, write, buffer, lenp, ppos,
3087                                 do_proc_dointvec_ms_jiffies_conv, NULL);
3088 }
3089
3090 static int proc_do_cad_pid(struct ctl_table *table, int write,
3091                            void __user *buffer, size_t *lenp, loff_t *ppos)
3092 {
3093         struct pid *new_pid;
3094         pid_t tmp;
3095         int r;
3096
3097         tmp = pid_vnr(cad_pid);
3098
3099         r = __do_proc_dointvec(&tmp, table, write, buffer,
3100                                lenp, ppos, NULL, NULL);
3101         if (r || !write)
3102                 return r;
3103
3104         new_pid = find_get_pid(tmp);
3105         if (!new_pid)
3106                 return -ESRCH;
3107
3108         put_pid(xchg(&cad_pid, new_pid));
3109         return 0;
3110 }
3111
3112 /**
3113  * proc_do_large_bitmap - read/write from/to a large bitmap
3114  * @table: the sysctl table
3115  * @write: %TRUE if this is a write to the sysctl file
3116  * @buffer: the user buffer
3117  * @lenp: the size of the user buffer
3118  * @ppos: file position
3119  *
3120  * The bitmap is stored at table->data and the bitmap length (in bits)
3121  * in table->maxlen.
3122  *
3123  * We use a range comma separated format (e.g. 1,3-4,10-10) so that
3124  * large bitmaps may be represented in a compact manner. Writing into
3125  * the file will clear the bitmap then update it with the given input.
3126  *
3127  * Returns 0 on success.
3128  */
3129 int proc_do_large_bitmap(struct ctl_table *table, int write,
3130                          void __user *buffer, size_t *lenp, loff_t *ppos)
3131 {
3132         int err = 0;
3133         bool first = 1;
3134         size_t left = *lenp;
3135         unsigned long bitmap_len = table->maxlen;
3136         unsigned long *bitmap = *(unsigned long **) table->data;
3137         unsigned long *tmp_bitmap = NULL;
3138         char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
3139
3140         if (!bitmap || !bitmap_len || !left || (*ppos && !write)) {
3141                 *lenp = 0;
3142                 return 0;
3143         }
3144
3145         if (write) {
3146                 char *kbuf, *p;
3147
3148                 if (left > PAGE_SIZE - 1)
3149                         left = PAGE_SIZE - 1;
3150
3151                 p = kbuf = memdup_user_nul(buffer, left);
3152                 if (IS_ERR(kbuf))
3153                         return PTR_ERR(kbuf);
3154
3155                 tmp_bitmap = kcalloc(BITS_TO_LONGS(bitmap_len),
3156                                      sizeof(unsigned long),
3157                                      GFP_KERNEL);
3158                 if (!tmp_bitmap) {
3159                         kfree(kbuf);
3160                         return -ENOMEM;
3161                 }
3162                 proc_skip_char(&p, &left, '\n');
3163                 while (!err && left) {
3164                         unsigned long val_a, val_b;
3165                         bool neg;
3166
3167                         err = proc_get_long(&p, &left, &val_a, &neg, tr_a,
3168                                              sizeof(tr_a), &c);
3169                         if (err)
3170                                 break;
3171                         if (val_a >= bitmap_len || neg) {
3172                                 err = -EINVAL;
3173                                 break;
3174                         }
3175
3176                         val_b = val_a;
3177                         if (left) {
3178                                 p++;
3179                                 left--;
3180                         }
3181
3182                         if (c == '-') {
3183                                 err = proc_get_long(&p, &left, &val_b,
3184                                                      &neg, tr_b, sizeof(tr_b),
3185                                                      &c);
3186                                 if (err)
3187                                         break;
3188                                 if (val_b >= bitmap_len || neg ||
3189                                     val_a > val_b) {
3190                                         err = -EINVAL;
3191                                         break;
3192                                 }
3193                                 if (left) {
3194                                         p++;
3195                                         left--;
3196                                 }
3197                         }
3198
3199                         bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
3200                         first = 0;
3201                         proc_skip_char(&p, &left, '\n');
3202                 }
3203                 kfree(kbuf);
3204         } else {
3205                 unsigned long bit_a, bit_b = 0;
3206
3207                 while (left) {
3208                         bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
3209                         if (bit_a >= bitmap_len)
3210                                 break;
3211                         bit_b = find_next_zero_bit(bitmap, bitmap_len,
3212                                                    bit_a + 1) - 1;
3213
3214                         if (!first) {
3215                                 err = proc_put_char(&buffer, &left, ',');
3216                                 if (err)
3217                                         break;
3218                         }
3219                         err = proc_put_long(&buffer, &left, bit_a, false);
3220                         if (err)
3221                                 break;
3222                         if (bit_a != bit_b) {
3223                                 err = proc_put_char(&buffer, &left, '-');
3224                                 if (err)
3225                                         break;
3226                                 err = proc_put_long(&buffer, &left, bit_b, false);
3227                                 if (err)
3228                                         break;
3229                         }
3230
3231                         first = 0; bit_b++;
3232                 }
3233                 if (!err)
3234                         err = proc_put_char(&buffer, &left, '\n');
3235         }
3236
3237         if (!err) {
3238                 if (write) {
3239                         if (*ppos)
3240                                 bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
3241                         else
3242                                 bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
3243                 }
3244                 *lenp -= left;
3245                 *ppos += *lenp;
3246         }
3247
3248         kfree(tmp_bitmap);
3249         return err;
3250 }
3251
3252 #else /* CONFIG_PROC_SYSCTL */
3253
3254 int proc_dostring(struct ctl_table *table, int write,
3255                   void __user *buffer, size_t *lenp, loff_t *ppos)
3256 {
3257         return -ENOSYS;
3258 }
3259
3260 int proc_dointvec(struct ctl_table *table, int write,
3261                   void __user *buffer, size_t *lenp, loff_t *ppos)
3262 {
3263         return -ENOSYS;
3264 }
3265
3266 int proc_douintvec(struct ctl_table *table, int write,
3267                   void __user *buffer, size_t *lenp, loff_t *ppos)
3268 {
3269         return -ENOSYS;
3270 }
3271
3272 int proc_dointvec_minmax(struct ctl_table *table, int write,
3273                     void __user *buffer, size_t *lenp, loff_t *ppos)
3274 {
3275         return -ENOSYS;
3276 }
3277
3278 int proc_douintvec_minmax(struct ctl_table *table, int write,
3279                           void __user *buffer, size_t *lenp, loff_t *ppos)
3280 {
3281         return -ENOSYS;
3282 }
3283
3284 int proc_dointvec_jiffies(struct ctl_table *table, int write,
3285                     void __user *buffer, size_t *lenp, loff_t *ppos)
3286 {
3287         return -ENOSYS;
3288 }
3289
3290 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
3291                     void __user *buffer, size_t *lenp, loff_t *ppos)
3292 {
3293         return -ENOSYS;
3294 }
3295
3296 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
3297                              void __user *buffer, size_t *lenp, loff_t *ppos)
3298 {
3299         return -ENOSYS;
3300 }
3301
3302 int proc_doulongvec_minmax(struct ctl_table *table, int write,
3303                     void __user *buffer, size_t *lenp, loff_t *ppos)
3304 {
3305         return -ENOSYS;
3306 }
3307
3308 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
3309                                       void __user *buffer,
3310                                       size_t *lenp, loff_t *ppos)
3311 {
3312     return -ENOSYS;
3313 }
3314
3315
3316 #endif /* CONFIG_PROC_SYSCTL */
3317
3318 /*
3319  * No sense putting this after each symbol definition, twice,
3320  * exception granted :-)
3321  */
3322 EXPORT_SYMBOL(proc_dointvec);
3323 EXPORT_SYMBOL(proc_douintvec);
3324 EXPORT_SYMBOL(proc_dointvec_jiffies);
3325 EXPORT_SYMBOL(proc_dointvec_minmax);
3326 EXPORT_SYMBOL_GPL(proc_douintvec_minmax);
3327 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
3328 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
3329 EXPORT_SYMBOL(proc_dostring);
3330 EXPORT_SYMBOL(proc_doulongvec_minmax);
3331 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);