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