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