[PATCH 4/4 v4] kernel/fork.c: memory hotplug updates max_threads

Heinrich Schuchardt
2015-02-23T20:17:29Z

Memory may be added or removed while the system is online.

With the patch the value of max_threads is updated accordingly.

The limits of the init process are also updated.
This does not include updating limits of other running processes.

Tested with commands like
echo 5000 > /proc/sys/kernel/threads-max
echo 0 > /sys/devices/system/memory/memory7/online
cat /proc/sys/kernel/threads-max
echo 1 > /sys/devices/system/memory/memory7/online
cat /proc/sys/kernel/threads-max

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
kernel/fork.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/kernel/fork.c b/kernel/fork.c
index 33b084e..7fd7c0a9 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -75,6 +75,8 @@
#include <linux/aio.h>
#include <linux/compiler.h>
#include <linux/sysctl.h>
+#include <linux/memory.h>
+#include <linux/notifier.h>

#include <asm/pgtable.h>
#include <asm/pgalloc.h>
@@ -295,6 +297,31 @@ static void set_max_threads(unsigned int max_threads_suggested)
init_task.signal->rlim[RLIMIT_NPROC];
}

+/*
+ * Callback function called for memory hotplug events.
+ */
+static int memory_hotplug_callback(struct notifier_block *self,
+ unsigned long action, void *arg)
+{
+ switch (action) {
+ case MEM_ONLINE:
+ /*
+ * If memory was added, try to maximize the number of allowed
+ * threads.
+ */
+ set_max_threads(UINT_MAX);
+ break;
+ case MEM_OFFLINE:
+ /*
+ * If memory was removed, try to keep current value.
+ */
+ set_max_threads(max_threads);
+ break;
+ }
+
+ return NOTIFY_OK;
+}
+
void __init fork_init(void)
{
#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
@@ -311,6 +338,8 @@ void __init fork_init(void)
arch_task_cache_init();

set_max_threads(UINT_MAX);
+
+ hotplug_memory_notifier(memory_hotplug_callback, 0);
}

int __weak arch_dup_task_struct(struct task_struct *dst,
--
2.1.4