From 672627d11f5f7e31e67aae4469a06148f64b5c35 Mon Sep 17 00:00:00 2001 From: Christoph Lameter Date: Thu, 21 Feb 2019 22:59:23 +0800 Subject: [PATCH] kmod: run usermodehelpers only on cpus allowed for kthreadd V2 hulk inclusion category: feature feature: performance/latency upstream: never bugzilla: 2680,10641 CVE: NA isolate this usermodehelper kernel threads to other cpus, to avoid the latency issue. With this patch, the usermodehelper thread could inherit kthreadd's affinity. For example, if you want to isolate usermodehelper to cpu 1: 1) taskset -cp 1 2 # bind kthreadd task (pid = 2) to cpu 1 2) trigger call usermodhelper threads --------------------------------------------- usermodehelper() threads can currently run on all processors. This is an issue for low latency cores. Spawnig a new thread causes cpu holdoffs in the range of hundreds of microseconds to a few milliseconds. Not good for cores on which processes run that need to react as fast as possible. kthreadd threads can be restricted using taskset to a limited set of processors. Then the kernel thread pool will not fork processes on those anymore thereby protecting those processors from additional latencies. Make usermodehelper() threads obey the limitations that kthreadd is restricted to. Kthreadd is not the parent of usermodehelper threads so we need to explicitly get the allowed processors for kthreadd. Before this patch there is no way to limit the cpus that usermodehelper can run on since the affinity is set when the thread is spawned to all processors. [akpm@linux-foundation.org: set_cpus_allowed() doesn't exist when CONFIG_CPUMASK_OFFSTACK=y] [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Christoph Lameter Cc: Frederic Weisbecker Cc: Mike Galbraith Cc: Thomas Gleixner Cc: Gilad Ben-Yossef Cc: "Paul E. McKenney" Cc: Mike Frysinger Cc: Tejun Heo Cc: Rusty Russell Signed-off-by: Andrew Morton Link: https://patchwork.kernel.org/patch/3153671/ Reported-and-tested-by: Xiangyou Xie [ 1) kmod.c => umh.c 2) ____call_usermodehelper => call_usermodehelper_exec_async ] Signed-off-by: Xie XiuQi Reviewed-by: Li Bin Signed-off-by: Yang Yingliang --- include/linux/kthread.h | 1 + kernel/kthread.c | 9 +++++++++ kernel/umh.c | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index c1961761311d..33e1f0052940 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -66,6 +66,7 @@ void kthread_parkme(void); int kthreadd(void *unused); extern struct task_struct *kthreadd_task; extern int tsk_fork_get_node(struct task_struct *tsk); +extern void set_kthreadd_affinity(void); /* * Simple work processor based on kthread. diff --git a/kernel/kthread.c b/kernel/kthread.c index 087d18d771b5..7973996e061f 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -156,6 +156,15 @@ void *kthread_data(struct task_struct *task) return to_kthread(task)->data; } +/* + * Set the affinity of the calling task to be the same + * as the kthreadd affinities. + */ +void set_kthreadd_affinity(void) +{ + set_cpus_allowed_ptr(current, &kthreadd_task->cpus_allowed); +} + /** * kthread_probe_data - speculative version of kthread_data() * @task: possible kthread task in question diff --git a/kernel/umh.c b/kernel/umh.c index 0baa672e023c..173d55579601 100644 --- a/kernel/umh.c +++ b/kernel/umh.c @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -72,6 +73,15 @@ static int call_usermodehelper_exec_async(void *data) flush_signal_handlers(current, 1); spin_unlock_irq(¤t->sighand->siglock); + /* + * Kthreadd can be restricted to a set of processors if the user wants + * to protect other processors from OS latencies. If that has happened + * then we do not want to disturb the other processors here either so we + * start the usermode helper threads only on the processors allowed for + * kthreadd. + */ + set_kthreadd_affinity(); + /* * Our parent (unbound workqueue) runs with elevated scheduling * priority. Avoid propagating that into the userspace child. -- GitLab