From cbc3fee7f780593872b5cc25572460606df86911 Mon Sep 17 00:00:00 2001 From: Syed Rameez Mustafa Date: Fri, 16 Dec 2016 11:59:05 -0800 Subject: [PATCH] cpu-hotplug: Always use real time scheduling when hotplugging a CPU CPU hotplug operations take place in preemptible context. This leaves the hotplugging thread at the mercy of overall system load and CPU availability. If the hotplugging thread does not get an opportunity to execute after it has already begun a hotplug operation, CPUs can end up being stuck in a quasi online state. In the worst case a CPU can be stuck in a state where the migration thread is parked while another task is executing and changing affinity in a loop. This combination can result in unbounded execution time for the running task until the hotplugging thread gets the chance to run to complete the hotplug operation. Fix the said problem by ensuring that hotplug can only occur from threads belonging to the RT sched class. This allows the hotplugging thread priority on the CPU no matter what the system load or the number of available CPUs are. If a SCHED_NORMAL task attempts to hotplug a CPU, we temporarily elevate it's scheduling policy to RT. Furthermore, we disallow hotplugging operations to begin if the calling task belongs to the idle and deadline classes or those that use the SCHED_BATCH policy. Change-Id: Idbb1384626e6ddff46c0d2ce752eee68396c78af Signed-off-by: Syed Rameez Mustafa --- kernel/cpu.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/kernel/cpu.c b/kernel/cpu.c index 8b6940755e4a..0ca3599cee1f 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -530,9 +530,41 @@ out: return ret; } +static int switch_to_rt_policy(void) +{ + struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; + unsigned int policy = current->policy; + int err; + + /* Nobody should be attempting hotplug from these policy contexts. */ + if (policy == SCHED_BATCH || policy == SCHED_IDLE || + policy == SCHED_DEADLINE) + return -EPERM; + + if (policy == SCHED_FIFO || policy == SCHED_RR) + return 1; + + /* Only SCHED_NORMAL left. */ + err = sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m); + return err; + +} + +static int switch_to_fair_policy(void) +{ + struct sched_param param = { .sched_priority = 0 }; + + return sched_setscheduler_nocheck(current, SCHED_NORMAL, ¶m); +} + int cpu_up(unsigned int cpu) { int err = 0; + int switch_err = 0; + + switch_err = switch_to_rt_policy(); + if (switch_err < 0) + return switch_err; if (!cpu_possible(cpu)) { pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n", @@ -558,6 +590,13 @@ int cpu_up(unsigned int cpu) out: cpu_maps_update_done(); + + if (!switch_err) { + switch_err = switch_to_fair_policy(); + pr_err("Hotplug policy switch err. Task %s pid=%d\n", + current->comm, current->pid); + } + return err; } EXPORT_SYMBOL_GPL(cpu_up);