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