OSDN Git Service

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