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