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