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