Skip to content

Commit 16db3d3

Browse files
xyprontorvalds
authored andcommitted
kernel/sysctl.c: threads-max observe limits
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>
1 parent ac1b398 commit 16db3d3

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎include/linux/sysctl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,7 @@ static inline void setup_sysctl_set(struct ctl_table_set *p,
212212

213213
#endif /* CONFIG_SYSCTL */
214214

215+
int sysctl_max_threads(struct ctl_table *table, int write,
216+
void __user *buffer, size_t *lenp, loff_t *ppos);
217+
215218
#endif /* _LINUX_SYSCTL_H */

‎kernel/fork.c‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <linux/uprobes.h>
7575
#include <linux/aio.h>
7676
#include <linux/compiler.h>
77+
#include <linux/sysctl.h>
7778

7879
#include <asm/pgtable.h>
7980
#include <asm/pgalloc.h>
@@ -266,7 +267,7 @@ void __init __weak arch_task_cache_init(void) { }
266267
/*
267268
* set_max_threads
268269
*/
269-
static void set_max_threads(void)
270+
static void set_max_threads(unsigned int max_threads_suggested)
270271
{
271272
u64 threads;
272273

@@ -280,6 +281,9 @@ static void set_max_threads(void)
280281
threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
281282
(u64) THREAD_SIZE * 8UL);
282283

284+
if (threads > max_threads_suggested)
285+
threads = max_threads_suggested;
286+
283287
max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
284288
}
285289

@@ -298,7 +302,7 @@ void __init fork_init(void)
298302
/* do the arch specific task caches init */
299303
arch_task_cache_init();
300304

301-
set_max_threads();
305+
set_max_threads(MAX_THREADS);
302306

303307
init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
304308
init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
@@ -2020,3 +2024,26 @@ int unshare_files(struct files_struct **displaced)
20202024
task_unlock(task);
20212025
return 0;
20222026
}
2027+
2028+
int sysctl_max_threads(struct ctl_table *table, int write,
2029+
void __user *buffer, size_t *lenp, loff_t *ppos)
2030+
{
2031+
struct ctl_table t;
2032+
int ret;
2033+
int threads = max_threads;
2034+
int min = MIN_THREADS;
2035+
int max = MAX_THREADS;
2036+
2037+
t = *table;
2038+
t.data = &threads;
2039+
t.extra1 = &min;
2040+
t.extra2 = &max;
2041+
2042+
ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2043+
if (ret || !write)
2044+
return ret;
2045+
2046+
set_max_threads(threads);
2047+
2048+
return 0;
2049+
}

‎kernel/sysctl.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@
9393
#include <linux/nmi.h>
9494
#endif
9595

96-
9796
#if defined(CONFIG_SYSCTL)
9897

9998
/* External variables not in a header file. */
100-
extern int max_threads;
10199
extern int suid_dumpable;
102100
#ifdef CONFIG_COREDUMP
103101
extern int core_uses_pid;
@@ -710,10 +708,10 @@ static struct ctl_table kern_table[] = {
710708
#endif
711709
{
712710
.procname = "threads-max",
713-
.data = &max_threads,
711+
.data = NULL,
714712
.maxlen = sizeof(int),
715713
.mode = 0644,
716-
.proc_handler = proc_dointvec,
714+
.proc_handler = sysctl_max_threads,
717715
},
718716
{
719717
.procname = "random",

0 commit comments

Comments
 (0)