kernel/sysctl.c: threads-max observe limits
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Thu, 16 Apr 2015 19:47:50 +0000 (12:47 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 17 Apr 2015 13:04:07 +0000 (09:04 -0400)
Users can change the maximum number of threads by writing to
/proc/sys/kernel/threads-max.

With the patch the value entered is checked against the same limits that
apply when fork_init is called.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/sysctl.h
kernel/fork.c
kernel/sysctl.c

index b7361f831226d97bab5ff8b4c86d9252e972d877..795d5fea569777f12842d243cc8714bcdcd40701 100644 (file)
@@ -212,4 +212,7 @@ static inline void setup_sysctl_set(struct ctl_table_set *p,
 
 #endif /* CONFIG_SYSCTL */
 
+int sysctl_max_threads(struct ctl_table *table, int write,
+                      void __user *buffer, size_t *lenp, loff_t *ppos);
+
 #endif /* _LINUX_SYSCTL_H */
index c7f2e1a4187ae4aa5436b8e384838ee28edcd4eb..8807a129711bf78053bd9b07ad6fe36bb9c2f592 100644 (file)
@@ -74,6 +74,7 @@
 #include <linux/uprobes.h>
 #include <linux/aio.h>
 #include <linux/compiler.h>
+#include <linux/sysctl.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -266,7 +267,7 @@ void __init __weak arch_task_cache_init(void) { }
 /*
  * set_max_threads
  */
-static void set_max_threads(void)
+static void set_max_threads(unsigned int max_threads_suggested)
 {
        u64 threads;
 
@@ -280,6 +281,9 @@ static void set_max_threads(void)
                threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
                                    (u64) THREAD_SIZE * 8UL);
 
+       if (threads > max_threads_suggested)
+               threads = max_threads_suggested;
+
        max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
 }
 
@@ -298,7 +302,7 @@ void __init fork_init(void)
        /* do the arch specific task caches init */
        arch_task_cache_init();
 
-       set_max_threads();
+       set_max_threads(MAX_THREADS);
 
        init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
        init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
@@ -2020,3 +2024,26 @@ int unshare_files(struct files_struct **displaced)
        task_unlock(task);
        return 0;
 }
+
+int sysctl_max_threads(struct ctl_table *table, int write,
+                      void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+       struct ctl_table t;
+       int ret;
+       int threads = max_threads;
+       int min = MIN_THREADS;
+       int max = MAX_THREADS;
+
+       t = *table;
+       t.data = &threads;
+       t.extra1 = &min;
+       t.extra2 = &max;
+
+       ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
+       if (ret || !write)
+               return ret;
+
+       set_max_threads(threads);
+
+       return 0;
+}
index 42b7fc2860c15a2748e4d8209d29a28179d059e0..3c0998426b57dce8e1dddd187d1d2cb5eaefe138 100644 (file)
 #include <linux/nmi.h>
 #endif
 
-
 #if defined(CONFIG_SYSCTL)
 
 /* External variables not in a header file. */
-extern int max_threads;
 extern int suid_dumpable;
 #ifdef CONFIG_COREDUMP
 extern int core_uses_pid;
@@ -710,10 +708,10 @@ static struct ctl_table kern_table[] = {
 #endif
        {
                .procname       = "threads-max",
-               .data           = &max_threads,
+               .data           = NULL,
                .maxlen         = sizeof(int),
                .mode           = 0644,
-               .proc_handler   = proc_dointvec,
+               .proc_handler   = sysctl_max_threads,
        },
        {
                .procname       = "random",