Skip to content

Commit b5e26be

Browse files
KAGA-KOKOgregkh
authored andcommitted
cpu/hotplug: Cure the cpusets trainwreck
commit b22afcd upstream. Alexey and Joshua tried to solve a cpusets related hotplug problem which is user space visible and results in unexpected behaviour for some time after a CPU has been plugged in and the corresponding uevent was delivered. cpusets delegate the hotplug work (rebuilding cpumasks etc.) to a workqueue. This is done because the cpusets code has already a lock nesting of cgroups_mutex -> cpu_hotplug_lock. A synchronous callback or waiting for the work to finish with cpu_hotplug_lock held can and will deadlock because that results in the reverse lock order. As a consequence the uevent can be delivered before cpusets have consistent state which means that a user space invocation of sched_setaffinity() to move a task to the plugged CPU fails up to the point where the scheduled work has been processed. The same is true for CPU unplug, but that does not create user observable failure (yet). It's still inconsistent to claim that an operation is finished before it actually is and that's the real issue at hand. uevents just make it reliably observable. Obviously the problem should be fixed in cpusets/cgroups, but untangling that is pretty much impossible because according to the changelog of the commit which introduced this 8 years ago: 3a5a6d0("cpuset: don't nest cgroup_mutex inside get_online_cpus()") the lock order cgroups_mutex -> cpu_hotplug_lock is a design decision and the whole code is built around that. So bite the bullet and invoke the relevant cpuset function, which waits for the work to finish, in _cpu_up/down() after dropping cpu_hotplug_lock and only when tasks are not frozen by suspend/hibernate because that would obviously wait forever. Waiting there with cpu_add_remove_lock, which is protecting the present and possible CPU maps, held is not a problem at all because neither work queues nor cpusets/cgroups have any lockchains related to that lock. Waiting in the hotplug machinery is not problematic either because there are already state callbacks which wait for hardware queues to drain. It makes the operations slightly slower, but hotplug is slow anyway. This ensures that state is consistent before returning from a hotplug up/down operation. It's still inconsistent during the operation, but that's a different story. Add a large comment which explains why this is done and why this is not a dump ground for the hack of the day to work around half thought out locking schemes. Document also the implications vs. hotplug operations and serialization or the lack of it. Thanks to Alexy and Joshua for analyzing why this temporary sched_setaffinity() failure happened. Fixes: 3a5a6d0("cpuset: don't nest cgroup_mutex inside get_online_cpus()") Reported-by: Alexey Klimov <aklimov@redhat.com> Reported-by: Joshua Baker <jobaker@redhat.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Alexey Klimov <aklimov@redhat.com> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/87tuowcnv3.ffs@nanos.tec.linutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a11a457 commit b5e26be

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

‎kernel/cpu.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/relay.h>
3333
#include <linux/slab.h>
3434
#include <linux/percpu-rwsem.h>
35+
#include <linux/cpuset.h>
3536

3637
#include <trace/events/power.h>
3738
#define CREATE_TRACE_POINTS
@@ -814,6 +815,52 @@ void __init cpuhp_threads_init(void)
814815
kthread_unpark(this_cpu_read(cpuhp_state.thread));
815816
}
816817

818+
/*
819+
*
820+
* Serialize hotplug trainwrecks outside of the cpu_hotplug_lock
821+
* protected region.
822+
*
823+
* The operation is still serialized against concurrent CPU hotplug via
824+
* cpu_add_remove_lock, i.e. CPU map protection. But it is _not_
825+
* serialized against other hotplug related activity like adding or
826+
* removing of state callbacks and state instances, which invoke either the
827+
* startup or the teardown callback of the affected state.
828+
*
829+
* This is required for subsystems which are unfixable vs. CPU hotplug and
830+
* evade lock inversion problems by scheduling work which has to be
831+
* completed _before_ cpu_up()/_cpu_down() returns.
832+
*
833+
* Don't even think about adding anything to this for any new code or even
834+
* drivers. It's only purpose is to keep existing lock order trainwrecks
835+
* working.
836+
*
837+
* For cpu_down() there might be valid reasons to finish cleanups which are
838+
* not required to be done under cpu_hotplug_lock, but that's a different
839+
* story and would be not invoked via this.
840+
*/
841+
static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
842+
{
843+
/*
844+
* cpusets delegate hotplug operations to a worker to "solve" the
845+
* lock order problems. Wait for the worker, but only if tasks are
846+
* _not_ frozen (suspend, hibernate) as that would wait forever.
847+
*
848+
* The wait is required because otherwise the hotplug operation
849+
* returns with inconsistent state, which could even be observed in
850+
* user space when a new CPU is brought up. The CPU plug uevent
851+
* would be delivered and user space reacting on it would fail to
852+
* move tasks to the newly plugged CPU up to the point where the
853+
* work has finished because up to that point the newly plugged CPU
854+
* is not assignable in cpusets/cgroups. On unplug that's not
855+
* necessarily a visible issue, but it is still inconsistent state,
856+
* which is the real problem which needs to be "fixed". This can't
857+
* prevent the transient state between scheduling the work and
858+
* returning from waiting for it.
859+
*/
860+
if (!tasks_frozen)
861+
cpuset_wait_for_hotplug();
862+
}
863+
817864
#ifdef CONFIG_HOTPLUG_CPU
818865
#ifndef arch_clear_mm_cpumask_cpu
819866
#define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
@@ -1051,6 +1098,7 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
10511098
*/
10521099
lockup_detector_cleanup();
10531100
arch_smt_update();
1101+
cpu_up_down_serialize_trainwrecks(tasks_frozen);
10541102
return ret;
10551103
}
10561104

@@ -1247,6 +1295,7 @@ static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
12471295
out:
12481296
cpus_write_unlock();
12491297
arch_smt_update();
1298+
cpu_up_down_serialize_trainwrecks(tasks_frozen);
12501299
return ret;
12511300
}
12521301

0 commit comments

Comments
 (0)