Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /* CPU control.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * (C) 2001, 2002, 2003, 2004 Rusty Russell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * This code is licenced under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/sched/hotplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/sched/isolation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/sched/smt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/oom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/stop_machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/smpboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/relay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/scs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/percpu-rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/cpuset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <uapi/linux/sched/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <trace/events/power.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <trace/events/cpuhp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <trace/hooks/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <trace/hooks/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "smpboot.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * cpuhp_cpu_state - Per cpu hotplug state storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * @state:	The current cpu state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * @target:	The target state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * @thread:	Pointer to the hotplug thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * @should_run:	Thread should execute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * @rollback:	Perform a rollback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * @single:	Single callback invocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * @bringup:	Single callback bringup or teardown selector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * @cb_state:	The state for a single callback (install/uninstall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * @result:	Result of the operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * @done_up:	Signal completion to the issuer of the task for cpu-up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * @done_down:	Signal completion to the issuer of the task for cpu-down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) struct cpuhp_cpu_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	enum cpuhp_state	state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	enum cpuhp_state	target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	enum cpuhp_state	fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct task_struct	*thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	bool			should_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	bool			rollback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	bool			single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	bool			bringup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	struct hlist_node	*node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct hlist_node	*last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	enum cpuhp_state	cb_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	int			result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct completion	done_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	struct completion	done_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	.fail = CPUHP_INVALID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) cpumask_t cpus_booted_once_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static struct lockdep_map cpuhp_state_up_map =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static struct lockdep_map cpuhp_state_down_map =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) static inline void cpuhp_lock_acquire(bool bringup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) static inline void cpuhp_lock_release(bool bringup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static inline void cpuhp_lock_acquire(bool bringup) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) static inline void cpuhp_lock_release(bool bringup) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * cpuhp_step - Hotplug state machine step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * @name:	Name of the step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * @startup:	Startup function of the step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * @teardown:	Teardown function of the step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * @cant_stop:	Bringup/teardown can't be stopped at this step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) struct cpuhp_step {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	const char		*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		int		(*single)(unsigned int cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		int		(*multi)(unsigned int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 					 struct hlist_node *node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	} startup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		int		(*single)(unsigned int cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		int		(*multi)(unsigned int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 					 struct hlist_node *node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	} teardown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct hlist_head	list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	bool			cant_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	bool			multi_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) static DEFINE_MUTEX(cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) static struct cpuhp_step cpuhp_hp_states[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	return cpuhp_hp_states + state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * cpuhp_invoke_callback _ Invoke the callbacks for a given state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * @cpu:	The cpu for which the callback should be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * @state:	The state to do callbacks for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * @bringup:	True if the bringup callback should be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * @node:	For multi-instance, do a single entry callback for install/remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * @lastp:	For multi-instance rollback, remember how far we got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * Called from cpu hotplug and from the state register machinery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 				 bool bringup, struct hlist_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 				 struct hlist_node **lastp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct cpuhp_step *step = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	int (*cbm)(unsigned int cpu, struct hlist_node *node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	int (*cb)(unsigned int cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	int ret, cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	if (st->fail == state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		st->fail = CPUHP_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		if (!(bringup ? step->startup.single : step->teardown.single))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	if (!step->multi_instance) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		WARN_ON_ONCE(lastp && *lastp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		cb = bringup ? step->startup.single : step->teardown.single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		if (!cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		trace_cpuhp_enter(cpu, st->target, state, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		ret = cb(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		trace_cpuhp_exit(cpu, st->state, state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	cbm = bringup ? step->startup.multi : step->teardown.multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	if (!cbm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	/* Single invocation for instance add/remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		WARN_ON_ONCE(lastp && *lastp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		ret = cbm(cpu, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		trace_cpuhp_exit(cpu, st->state, state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	/* State transition. Invoke on all instances */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	hlist_for_each(node, &step->list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		if (lastp && node == *lastp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		ret = cbm(cpu, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		trace_cpuhp_exit(cpu, st->state, state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			if (!lastp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			*lastp = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	if (lastp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		*lastp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	/* Rollback the instances if one failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	cbm = !bringup ? step->startup.multi : step->teardown.multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	if (!cbm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	hlist_for_each(node, &step->list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		if (!cnt--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		ret = cbm(cpu, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		trace_cpuhp_exit(cpu, st->state, state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		 * Rollback must not fail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		WARN_ON_ONCE(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static bool cpuhp_is_ap_state(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	 * purposes as that state is handled explicitly in cpu_down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	struct completion *done = bringup ? &st->done_up : &st->done_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	wait_for_completion(done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	struct completion *done = bringup ? &st->done_up : &st->done_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	complete(done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) static bool cpuhp_is_atomic_state(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) /* Serializes the updates to cpu_online_mask, cpu_present_mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static DEFINE_MUTEX(cpu_add_remove_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) bool cpuhp_tasks_frozen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  * The following two APIs (cpu_maps_update_begin/done) must be used when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) void cpu_maps_update_begin(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	mutex_lock(&cpu_add_remove_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) EXPORT_SYMBOL_GPL(cpu_maps_update_begin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) void cpu_maps_update_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	mutex_unlock(&cpu_add_remove_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) EXPORT_SYMBOL_GPL(cpu_maps_update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * Should always be manipulated under cpu_add_remove_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) static int cpu_hotplug_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) void cpus_read_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	percpu_down_read(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) EXPORT_SYMBOL_GPL(cpus_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) int cpus_read_trylock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	return percpu_down_read_trylock(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) EXPORT_SYMBOL_GPL(cpus_read_trylock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) void cpus_read_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	percpu_up_read(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) EXPORT_SYMBOL_GPL(cpus_read_unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) void cpus_write_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	percpu_down_write(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) void cpus_write_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	percpu_up_write(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) void lockdep_assert_cpus_held(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	 * We can't have hotplug operations before userspace starts running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	 * and some init codepaths will knowingly not take the hotplug lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	 * This is all valid, so mute lockdep until it makes sense to report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * unheld locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (system_state < SYSTEM_RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	percpu_rwsem_assert_held(&cpu_hotplug_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static void lockdep_acquire_cpus_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) static void lockdep_release_cpus_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	rwsem_release(&cpu_hotplug_lock.dep_map, _THIS_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * Wait for currently running CPU hotplug operations to complete (if any) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * hotplug path before performing hotplug operations. So acquiring that lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * guarantees mutual exclusion from any currently running hotplug operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) void cpu_hotplug_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	cpu_hotplug_disabled++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static void __cpu_hotplug_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	cpu_hotplug_disabled--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) void cpu_hotplug_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	__cpu_hotplug_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) static void lockdep_acquire_cpus_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) static void lockdep_release_cpus_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) #endif	/* CONFIG_HOTPLUG_CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * Architectures that need SMT-specific errata handling during SMT hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * should override this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) void __weak arch_smt_update(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) #ifdef CONFIG_HOTPLUG_SMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) void __init cpu_smt_disable(bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (!cpu_smt_possible())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (force) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		pr_info("SMT: Force disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		cpu_smt_control = CPU_SMT_FORCE_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		pr_info("SMT: disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		cpu_smt_control = CPU_SMT_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * The decision whether SMT is supported can only be done after the full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * CPU identification. Called from architecture code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) void __init cpu_smt_check_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (!topology_smt_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) static int __init smt_cmdline_disable(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	cpu_smt_disable(str && !strcmp(str, "force"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) early_param("nosmt", smt_cmdline_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) static inline bool cpu_smt_allowed(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	if (cpu_smt_control == CPU_SMT_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (topology_is_primary_thread(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	 * On x86 it's required to boot all logical CPUs at least once so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	 * that the init code can get a chance to set CR4.MCE on each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	 * CPU. Otherwise, a broadcasted MCE observing CR4.MCE=0b on any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	 * core will shutdown the machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	return !cpumask_test_cpu(cpu, &cpus_booted_once_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) /* Returns true if SMT is not supported of forcefully (irreversibly) disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) bool cpu_smt_possible(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	return cpu_smt_control != CPU_SMT_FORCE_DISABLED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		cpu_smt_control != CPU_SMT_NOT_SUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) EXPORT_SYMBOL_GPL(cpu_smt_possible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) static inline enum cpuhp_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	enum cpuhp_state prev_state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	st->rollback = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	st->last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	st->target = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	st->single = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	st->bringup = st->state < target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	return prev_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	st->rollback = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	 * If we have st->last we need to undo partial multi_instance of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	 * state first. Otherwise start undo at the previous state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (!st->last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		if (st->bringup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			st->state--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			st->state++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	st->target = prev_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	st->bringup = !st->bringup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) /* Regular hotplug invocation of the AP hotplug thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (!st->single && st->state == st->target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	st->result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	 * Make sure the above stores are visible before should_run becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	 * true. Paired with the mb() above in cpuhp_thread_fun()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	st->should_run = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	wake_up_process(st->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	wait_for_ap_thread(st, st->bringup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	enum cpuhp_state prev_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	prev_state = cpuhp_set_state(st, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	__cpuhp_kick_ap(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if ((ret = st->result)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		cpuhp_reset_state(st, prev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		__cpuhp_kick_ap(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) static int bringup_wait_for_ap(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	/* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	wait_for_ap_thread(st, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	if (WARN_ON_ONCE((!cpu_online(cpu))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	/* Unpark the hotplug thread of the target cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	kthread_unpark(st->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 * SMT soft disabling on X86 requires to bring the CPU out of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * CPU marked itself as booted_once in notify_cpu_starting() so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 * cpu_smt_allowed() check will now return false if this is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	 * primary sibling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (!cpu_smt_allowed(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (st->target <= CPUHP_AP_ONLINE_IDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return cpuhp_kick_ap(st, st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static int bringup_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	struct task_struct *idle = idle_thread_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 * Reset stale stack state from the last time this CPU was online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	scs_task_reset(idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	kasan_unpoison_task_stack(idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	 * Some architectures have to walk the irq descriptors to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	 * setup the vector space for the cpu which comes online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	 * Prevent irq alloc/free across the bringup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	irq_lock_sparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	/* Arch-specific enabling code. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	ret = __cpu_up(cpu, idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	irq_unlock_sparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	return bringup_wait_for_ap(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) static int finish_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct task_struct *idle = idle_thread_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	struct mm_struct *mm = idle->active_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	 * idle_task_exit() will have switched to &init_mm, now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	 * clean up any remaining active_mm state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	if (mm != &init_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		idle->active_mm = &init_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	mmdrop(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * Hotplug state machine related functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	for (st->state--; st->state > st->target; st->state--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	 * When CPU hotplug is disabled, then taking the CPU down is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	 * possible because takedown_cpu() and the architecture and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	 * subsystem specific mechanisms are not available. So the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	 * which would be completely unplugged again needs to stay around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	 * in the current state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	return st->state <= CPUHP_BRINGUP_CPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			      enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	enum cpuhp_state prev_state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	while (st->state < target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		st->state++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			if (can_rollback_cpu(st)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				st->target = prev_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 				undo_cpu_up(cpu, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643)  * The cpu hotplug threads manage the bringup and teardown of the cpus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) static void cpuhp_create(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	init_completion(&st->done_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	init_completion(&st->done_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) static int cpuhp_should_run(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return st->should_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * callbacks when a state gets [un]installed at runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * Each invocation of this function by the smpboot thread does a single AP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * state callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * It has 3 modes of operation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  *  - single: runs st->cb_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  *  - up:     runs ++st->state, while st->state < st->target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  *  - down:   runs st->state--, while st->state > st->target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  * When complete or on error, should_run is cleared and the completion is fired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static void cpuhp_thread_fun(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	bool bringup = st->bringup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	enum cpuhp_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	if (WARN_ON_ONCE(!st->should_run))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	 * that if we see ->should_run we also see the rest of the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 * The BP holds the hotplug lock, but we're now running on the AP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 * ensure that anybody asserting the lock is held, will actually find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 * it so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	lockdep_acquire_cpus_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	cpuhp_lock_acquire(bringup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (st->single) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		state = st->cb_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		st->should_run = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		if (bringup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			st->state++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			st->should_run = (st->state < st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 			WARN_ON_ONCE(st->state > st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			st->state--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			st->should_run = (st->state > st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			WARN_ON_ONCE(st->state < st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	WARN_ON_ONCE(!cpuhp_is_ap_state(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (cpuhp_is_atomic_state(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		 * STARTING/DYING must not fail!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		WARN_ON_ONCE(st->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (st->result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		 * If we fail on a rollback, we're up a creek without no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		 * paddle, no way forward, no way back. We loose, thanks for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * playing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		WARN_ON_ONCE(st->rollback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		st->should_run = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	cpuhp_lock_release(bringup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	lockdep_release_cpus_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (!st->should_run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		complete_ap_thread(st, bringup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) /* Invoke a single callback on a remote cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			 struct hlist_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (!cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	cpuhp_lock_acquire(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	cpuhp_lock_release(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	cpuhp_lock_acquire(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	cpuhp_lock_release(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	 * If we are up and running, use the hotplug thread. For early calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	 * we invoke the thread function directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	if (!st->thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	st->rollback = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	st->last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	st->node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	st->bringup = bringup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	st->cb_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	st->single = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	__cpuhp_kick_ap(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	 * If we failed and did a partial, do a rollback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	if ((ret = st->result) && st->last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		st->rollback = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		st->bringup = !bringup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		__cpuhp_kick_ap(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	 * Clean up the leftovers so the next hotplug operation wont use stale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	 * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	st->node = st->last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static int cpuhp_kick_ap_work(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	enum cpuhp_state prev_state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	cpuhp_lock_acquire(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	cpuhp_lock_release(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	cpuhp_lock_acquire(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	cpuhp_lock_release(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	ret = cpuhp_kick_ap(st, st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	trace_cpuhp_exit(cpu, st->state, prev_state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) static struct smp_hotplug_thread cpuhp_threads = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	.store			= &cpuhp_state.thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	.create			= &cpuhp_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	.thread_should_run	= cpuhp_should_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	.thread_fn		= cpuhp_thread_fun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	.thread_comm		= "cpuhp/%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	.selfparking		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) void __init cpuhp_threads_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	kthread_unpark(this_cpu_read(cpuhp_state.thread));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  * Serialize hotplug trainwrecks outside of the cpu_hotplug_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  * protected region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  * The operation is still serialized against concurrent CPU hotplug via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  * cpu_add_remove_lock, i.e. CPU map protection.  But it is _not_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * serialized against other hotplug related activity like adding or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  * removing of state callbacks and state instances, which invoke either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * startup or the teardown callback of the affected state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  * This is required for subsystems which are unfixable vs. CPU hotplug and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * evade lock inversion problems by scheduling work which has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * completed _before_ cpu_up()/_cpu_down() returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  * Don't even think about adding anything to this for any new code or even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * drivers. It's only purpose is to keep existing lock order trainwrecks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * working.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * For cpu_down() there might be valid reasons to finish cleanups which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  * not required to be done under cpu_hotplug_lock, but that's a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853)  * story and would be not invoked via this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	 * cpusets delegate hotplug operations to a worker to "solve" the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	 * lock order problems. Wait for the worker, but only if tasks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	 * _not_ frozen (suspend, hibernate) as that would wait forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 * The wait is required because otherwise the hotplug operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 * returns with inconsistent state, which could even be observed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	 * user space when a new CPU is brought up. The CPU plug uevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 * would be delivered and user space reacting on it would fail to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	 * move tasks to the newly plugged CPU up to the point where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	 * work has finished because up to that point the newly plugged CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	 * is not assignable in cpusets/cgroups. On unplug that's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	 * necessarily a visible issue, but it is still inconsistent state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	 * which is the real problem which needs to be "fixed". This can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	 * prevent the transient state between scheduling the work and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	 * returning from waiting for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	if (!tasks_frozen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		cpuset_wait_for_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) #ifndef arch_clear_mm_cpumask_cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884)  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885)  * @cpu: a CPU id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887)  * This function walks all processes, finds a valid mm struct for each one and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888)  * then clears a corresponding bit in mm's cpumask.  While this all sounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889)  * trivial, there are various non-obvious corner cases, which this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890)  * tries to solve in a safe manner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892)  * Also note that the function uses a somewhat relaxed locking scheme, so it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893)  * be called only for an already offlined CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) void clear_tasks_mm_cpumask(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	struct task_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 * This function is called after the cpu is taken down and marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 * offline, so its not like new tasks will ever get this cpu set in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	 * their mm mask. -- Peter Zijlstra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	 * full-fledged tasklist_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	WARN_ON(cpu_online(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	for_each_process(p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		struct task_struct *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		 * Main thread might exit, but other threads may still have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		 * a valid mm. Find one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		t = find_lock_task_mm(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		arch_clear_mm_cpumask_cpu(cpu, t->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		task_unlock(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) /* Take this CPU down. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) static int take_cpu_down(void *_param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	int err, cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	/* Ensure this CPU doesn't handle any more interrupts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	err = __cpu_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	 * do this step again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	st->state--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	/* Invoke the former CPU_DYING callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	for (; st->state > target; st->state--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		 * DYING must not fail!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		WARN_ON_ONCE(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	/* Give up timekeeping duties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	tick_handover_do_timer();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	/* Remove CPU from timer broadcasting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	tick_offline_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	/* Park the stopper thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	stop_machine_park(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) static int takedown_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	/* Park the smpboot threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	 * Prevent irq alloc/free while the dying cpu reorganizes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	 * interrupt affinities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	irq_lock_sparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	 * So now all preempt/rcu users must observe !cpu_active().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		/* CPU refused to die */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		irq_unlock_sparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		/* Unpark the hotplug thread so we can rollback there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	BUG_ON(cpu_online(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	 * all runnable tasks from the CPU, there's only the idle task left now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	 * that the migration thread is done doing the stop_machine thing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	 * Wait for the stop thread to go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	wait_for_ap_thread(st, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	/* Interrupts are moved away from the dying cpu, reenable alloc/free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	irq_unlock_sparse();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	hotplug_cpu__broadcast_tick_pull(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	/* This actually kills the CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	__cpu_die(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	tick_cleanup_dead_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	rcutree_migrate_callbacks(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static void cpuhp_complete_idle_dead(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	struct cpuhp_cpu_state *st = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	complete_ap_thread(st, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) void cpuhp_report_idle_dead(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	BUG_ON(st->state != CPUHP_AP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	rcu_report_dead(smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	st->state = CPUHP_AP_IDLE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * We cannot call complete after rcu_report_dead() so we delegate it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 * to an online cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	smp_call_function_single(cpumask_first(cpu_online_mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 				 cpuhp_complete_idle_dead, st, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	for (st->state++; st->state < st->target; st->state++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	enum cpuhp_state prev_state = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	for (; st->state > target; st->state--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			st->target = prev_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			if (st->state < prev_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				undo_cpu_down(cpu, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /* Requires cpu_add_remove_lock to be held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			   enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	int prev_state, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	if (num_active_cpus() == 1 && cpu_active(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (!cpu_present(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	cpus_write_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	cpuhp_tasks_frozen = tasks_frozen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	prev_state = cpuhp_set_state(st, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	 * If the current CPU state is in the range of the AP hotplug thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	 * then we need to kick the thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	if (st->state > CPUHP_TEARDOWN_CPU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		st->target = max((int)target, CPUHP_TEARDOWN_CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		ret = cpuhp_kick_ap_work(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		 * The AP side has done the error rollback already. Just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		 * return the error code..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		 * We might have stopped still in the range of the AP hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		 * thread. Nothing to do anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		if (st->state > CPUHP_TEARDOWN_CPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		st->target = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	 * to do the further cleanups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	ret = cpuhp_down_callbacks(cpu, st, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		cpuhp_reset_state(st, prev_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		__cpuhp_kick_ap(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	cpus_write_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	 * Do post unplug cleanup. This is still protected against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	 * concurrent CPU hotplug via cpu_add_remove_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	lockup_detector_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	arch_smt_update();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	cpu_up_down_serialize_trainwrecks(tasks_frozen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (cpu_hotplug_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	return _cpu_down(cpu, 0, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) static int cpu_down(unsigned int cpu, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	err = cpu_down_maps_locked(cpu, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)  * cpu_device_down - Bring down a cpu device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)  * @dev: Pointer to the cpu device to offline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)  * This function is meant to be used by device core cpu subsystem only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)  * Other subsystems should use remove_cpu() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) int cpu_device_down(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	return cpu_down(dev->id, CPUHP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) int remove_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	lock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	ret = device_offline(get_cpu_device(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) EXPORT_SYMBOL_GPL(remove_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) extern bool dl_cpu_busy(unsigned int cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) int __pause_drain_rq(struct cpumask *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	 * Disabling preemption avoids that one of the stopper, started from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 * sched_cpu_drain_rq(), blocks firing draining for the whole cpumask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		err = sched_cpu_drain_rq(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) void __wait_drain_rq(struct cpumask *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		sched_cpu_drain_rq_wait(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) /* if rt task, set to cfs and return previous prio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) static int pause_reduce_prio(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	int prev_prio = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	if (current->prio < MAX_RT_PRIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		struct sched_param param = { .sched_priority = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		prev_prio = current->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		sched_setscheduler_nocheck(current, SCHED_NORMAL, &param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	return prev_prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) /* if previous prio was set, restore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static void pause_restore_prio(int prev_prio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	if (prev_prio >= 0 && prev_prio < MAX_RT_PRIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		struct sched_param param = { .sched_priority = MAX_RT_PRIO-1-prev_prio };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) int pause_cpus(struct cpumask *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	u64 start_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	int prev_prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	start_time = sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	if (cpu_hotplug_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	/* Pausing an already inactive CPU isn't an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	cpumask_and(cpus, cpus, cpu_active_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		if (!cpu_online(cpu) || dl_cpu_busy(cpu) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			get_cpu_device(cpu)->offline_disabled == true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	if (cpumask_weight(cpus) >= num_active_cpus()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	if (cpumask_empty(cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	 * Lazy migration:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	 * We do care about how fast a CPU can go idle and stay this in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	 * state. If we try to take the cpus_write_lock() here, we would have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	 * to wait for a few dozens of ms, as this function might schedule.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	 * However, we can, as a first step, flip the active mask and migrate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	 * anything currently on the run-queue, to give a chance to the paused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	 * CPUs to reach quickly an idle state. There's a risk meanwhile for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	 * another CPU to observe an out-of-date active_mask or to incompletely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	 * update a cpuset. Both problems would be resolved later in the slow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	 * path, which ensures active_mask synchronization, triggers a cpuset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	 * rebuild and migrate any task that would have escaped the lazy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	 * migration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		set_cpu_active(cpu, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	err = __pause_drain_rq(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		__wait_drain_rq(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			set_cpu_active(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	prev_prio = pause_reduce_prio();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	 * Slow path deactivation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	 * Now that paused CPUs are most likely idle, we can go through a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	 * complete scheduler deactivation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	 * The cpu_active_mask being already set and cpus_write_lock calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	 * synchronize_rcu(), we know that all preempt-disabled and RCU users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	 * will observe the updated value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	cpus_write_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	__wait_drain_rq(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	cpuhp_tasks_frozen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	if (sched_cpus_deactivate_nosync(cpus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		goto err_cpus_write_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	err = __pause_drain_rq(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	__wait_drain_rq(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			sched_cpu_activate(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		goto err_cpus_write_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	 * Even if living on the side of the regular HP path, pause is using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	 * one of the HP step (CPUHP_AP_ACTIVE). This should be reflected on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	 * current state of the CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		st->state = CPUHP_AP_ACTIVE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		st->target = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) err_cpus_write_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	cpus_write_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	pause_restore_prio(prev_prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) err_cpu_maps_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	trace_cpuhp_pause(cpus, start_time, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) EXPORT_SYMBOL_GPL(pause_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) int resume_cpus(struct cpumask *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	u64 start_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	int prev_prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	start_time = sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	if (cpu_hotplug_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	/* Resuming an already active CPU isn't an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	cpumask_andnot(cpus, cpus, cpu_active_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		if (!cpu_online(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	if (cpumask_empty(cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		set_cpu_active(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	trace_android_rvh_resume_cpus(cpus, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		goto err_cpu_maps_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	prev_prio = pause_reduce_prio();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	/* Lazy Resume. Build domains through schedule a workqueue on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	 * resuming cpu. This is so that the resuming cpu can work more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	 * early, and cannot add additional load to other busy cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	cpuset_update_active_cpus_affine(cpumask_first(cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	cpus_write_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	cpuhp_tasks_frozen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (sched_cpus_activate(cpus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		goto err_cpus_write_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	 * see pause_cpus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		st->state = CPUHP_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		st->target = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) err_cpus_write_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	cpus_write_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	pause_restore_prio(prev_prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) err_cpu_maps_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	trace_cpuhp_pause(cpus, start_time, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) EXPORT_SYMBOL_GPL(resume_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) void smp_shutdown_nonboot_cpus(unsigned int primary_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	 * Make certain the cpu I'm about to reboot on is online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	 * This is inline to what migrate_to_reboot_cpu() already do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	if (!cpu_online(primary_cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		primary_cpu = cpumask_first(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (cpu == primary_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			pr_err("Failed to offline CPU%d - error=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 				cpu, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	 * Ensure all but the reboot CPU are offline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	BUG_ON(num_online_cpus() > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	 * Make sure the CPUs won't be enabled by someone else after this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	 * point. Kexec will reboot to a new kernel shortly resetting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	 * everything along the way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	cpu_hotplug_disabled++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) #define takedown_cpu		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) #endif /*CONFIG_HOTPLUG_CPU*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)  * @cpu: cpu that just started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)  * It must be called by the arch code on the new cpu, before the new cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)  * enables interrupts and before the "boot" cpu returns from __cpu_up().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) void notify_cpu_starting(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	rcu_cpu_starting(cpu);	/* Enables RCU usage on this CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	cpumask_set_cpu(cpu, &cpus_booted_once_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	while (st->state < target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		st->state++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		 * STARTING must not fail!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		WARN_ON_ONCE(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * Called from the idle task. Wake up the controlling task which brings the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  * hotplug thread of the upcoming CPU up and then delegates the rest of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)  * online bringup to the hotplug thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) void cpuhp_online_idle(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	/* Happens for the boot cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (state != CPUHP_AP_ONLINE_IDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	 * Unpart the stopper thread before we start the idle loop (and start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	 * scheduling); this ensures the stopper task is always available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	stop_machine_unpark(smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	st->state = CPUHP_AP_ONLINE_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	complete_ap_thread(st, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) static int switch_to_rt_policy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	unsigned int policy = current->policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	if (policy == SCHED_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		/* Switch to SCHED_FIFO from SCHED_NORMAL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		return sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) static int switch_to_fair_policy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct sched_param param = { .sched_priority = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	return sched_setscheduler_nocheck(current, SCHED_NORMAL, &param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) /* Requires cpu_add_remove_lock to be held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	struct task_struct *idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	cpus_write_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	if (!cpu_present(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	 * The caller of cpu_up() might have raced with another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	 * caller. Nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (st->state >= target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (st->state == CPUHP_OFFLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		/* Let it fail before we try to bring the cpu up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		idle = idle_thread_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		if (IS_ERR(idle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			ret = PTR_ERR(idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	cpuhp_tasks_frozen = tasks_frozen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	cpuhp_set_state(st, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	 * If the current CPU state is in the range of the AP hotplug thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	 * then we need to kick the thread once more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (st->state > CPUHP_BRINGUP_CPU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		ret = cpuhp_kick_ap_work(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		 * The AP side has done the error rollback already. Just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		 * return the error code..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	 * Try to reach the target state. We max out on the BP at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	 * responsible for bringing it up to the target state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	target = min((int)target, CPUHP_BRINGUP_CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	ret = cpuhp_up_callbacks(cpu, st, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	cpus_write_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	arch_smt_update();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	cpu_up_down_serialize_trainwrecks(tasks_frozen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) static int cpu_up(unsigned int cpu, enum cpuhp_state target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	int switch_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	if (!cpu_possible(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		       cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) #if defined(CONFIG_IA64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		pr_err("please check additional_cpus= boot parameter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	trace_android_vh_cpu_up(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	 * CPU hotplug operations consists of many steps and each step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	 * calls a callback of core kernel subsystem. CPU hotplug-in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	 * operation may get preempted by other CFS tasks and whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	 * operation of cpu hotplug in CPU gets delayed. Switch the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	 * current task to SCHED_FIFO from SCHED_NORMAL, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	 * hotplug in operation may complete quickly in heavy loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	 * conditions and new CPU will start handle the workload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	switch_err = switch_to_rt_policy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	err = try_online_node(cpu_to_node(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		goto switch_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	if (cpu_hotplug_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	if (!cpu_smt_allowed(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	err = _cpu_up(cpu, 0, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) switch_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	if (!switch_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		switch_err = switch_to_fair_policy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		if (switch_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 			pr_err("Hotplug policy switch err=%d Task %s pid=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 				switch_err, current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)  * cpu_device_up - Bring up a cpu device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)  * @dev: Pointer to the cpu device to online
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)  * This function is meant to be used by device core cpu subsystem only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)  * Other subsystems should use add_cpu() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) int cpu_device_up(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	return cpu_up(dev->id, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) int add_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	lock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	ret = device_online(get_cpu_device(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) EXPORT_SYMBOL_GPL(add_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  * bringup_hibernate_cpu - Bring up the CPU that we hibernated on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  * @sleep_cpu: The cpu we hibernated on and should be brought up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)  * On some architectures like arm64, we can hibernate on any CPU, but on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)  * wake up the CPU we hibernated on might be offline as a side effect of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668)  * using maxcpus= for example.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) int bringup_hibernate_cpu(unsigned int sleep_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	if (!cpu_online(sleep_cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		ret = cpu_up(sleep_cpu, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			pr_err("Failed to bring hibernate-CPU up!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) void bringup_nonboot_cpus(unsigned int setup_max_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		if (num_online_cpus() >= setup_max_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		if (!cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 			cpu_up(cpu, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) #ifdef CONFIG_PM_SLEEP_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) static cpumask_var_t frozen_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) int freeze_secondary_cpus(int primary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	int cpu, error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	if (primary == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		primary = cpumask_first(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		if (!housekeeping_cpu(primary, HK_FLAG_TIMER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			primary = housekeeping_any_cpu(HK_FLAG_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		if (!cpu_online(primary))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 			primary = cpumask_first(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	 * We take down all of the non-boot CPUs in one shot to avoid races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	 * with the userspace trying to use the CPU hotplug at the same time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	cpumask_clear(frozen_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	pr_info("Disabling non-boot CPUs ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		if (cpu == primary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		if (pm_wakeup_pending()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			pr_info("Wakeup pending. Abort CPU freeze\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			cpumask_set_cpu(cpu, frozen_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		BUG_ON(num_online_cpus() > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		pr_err("Non-boot CPUs are not disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	 * Make sure the CPUs won't be enabled by someone else. We need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	 * this even in case of failure as all freeze_secondary_cpus() users are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	 * supposed to do thaw_secondary_cpus() on the failure path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	cpu_hotplug_disabled++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) void __weak arch_thaw_secondary_cpus_begin(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) void __weak arch_thaw_secondary_cpus_end(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) void thaw_secondary_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	int cpu, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	struct device *cpu_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	/* Allow everyone to use the CPU hotplug again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	__cpu_hotplug_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	if (cpumask_empty(frozen_cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	pr_info("Enabling non-boot CPUs ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	arch_thaw_secondary_cpus_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	for_each_cpu(cpu, frozen_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		error = _cpu_up(cpu, 1, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 			pr_info("CPU%d is up\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 			cpu_device = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 			if (!cpu_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 				pr_err("%s: failed to get cpu%d device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 				       __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 				kobject_uevent(&cpu_device->kobj, KOBJ_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	arch_thaw_secondary_cpus_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	cpumask_clear(frozen_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) static int __init alloc_frozen_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) core_initcall(alloc_frozen_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814)  * When callbacks for CPU hotplug notifications are being executed, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)  * ensure that the state of the system with respect to the tasks being frozen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)  * or not, as reported by the notification, remains unchanged *throughout the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)  * duration* of the execution of the callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)  * This synchronization is implemented by mutually excluding regular CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)  * Hibernate notifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) cpu_hotplug_pm_callback(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			unsigned long action, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	case PM_SUSPEND_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	case PM_HIBERNATION_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		cpu_hotplug_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	case PM_POST_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	case PM_POST_HIBERNATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		cpu_hotplug_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) static int __init cpu_hotplug_pm_sync_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	 * cpu_hotplug_pm_callback has higher priority than x86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	 * to disable cpu hotplug to avoid cpu hotplug race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	pm_notifier(cpu_hotplug_pm_callback, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) core_initcall(cpu_hotplug_pm_sync_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) #endif /* CONFIG_PM_SLEEP_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) int __boot_cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) #endif /* CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) /* Boot processor state steps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) static struct cpuhp_step cpuhp_hp_states[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	[CPUHP_OFFLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		.name			= "offline",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		.startup.single		= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	[CPUHP_CREATE_THREADS]= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		.name			= "threads:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		.startup.single		= smpboot_create_threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		.cant_stop		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	[CPUHP_PERF_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		.name			= "perf:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		.startup.single		= perf_event_init_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		.teardown.single	= perf_event_exit_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	[CPUHP_WORKQUEUE_PREP] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		.name			= "workqueue:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		.startup.single		= workqueue_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	[CPUHP_HRTIMERS_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		.name			= "hrtimers:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		.startup.single		= hrtimers_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		.teardown.single	= hrtimers_dead_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	[CPUHP_SMPCFD_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		.name			= "smpcfd:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		.startup.single		= smpcfd_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		.teardown.single	= smpcfd_dead_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	[CPUHP_RELAY_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		.name			= "relay:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		.startup.single		= relay_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	[CPUHP_SLAB_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		.name			= "slab:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		.startup.single		= slab_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		.teardown.single	= slab_dead_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	[CPUHP_RCUTREE_PREP] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		.name			= "RCU/tree:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		.startup.single		= rcutree_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		.teardown.single	= rcutree_dead_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	 * On the tear-down path, timers_dead_cpu() must be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	 * before blk_mq_queue_reinit_notify() from notify_dead(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	 * otherwise a RCU stall occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	[CPUHP_TIMERS_PREPARE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		.name			= "timers:prepare",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		.startup.single		= timers_prepare_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		.teardown.single	= timers_dead_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	/* Kicks the plugged cpu into life */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	[CPUHP_BRINGUP_CPU] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		.name			= "cpu:bringup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		.startup.single		= bringup_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		.teardown.single	= finish_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		.cant_stop		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	/* Final state before CPU kills itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	[CPUHP_AP_IDLE_DEAD] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		.name			= "idle:dead",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	 * Last state before CPU enters the idle loop to die. Transient state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	 * for synchronization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	[CPUHP_AP_OFFLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		.name			= "ap:offline",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		.cant_stop		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	/* First state is scheduler control. Interrupts are disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	[CPUHP_AP_SCHED_STARTING] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		.name			= "sched:starting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		.startup.single		= sched_cpu_starting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		.teardown.single	= sched_cpu_dying,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	[CPUHP_AP_RCUTREE_DYING] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		.name			= "RCU/tree:dying",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		.startup.single		= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		.teardown.single	= rcutree_dying_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	[CPUHP_AP_SMPCFD_DYING] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		.name			= "smpcfd:dying",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		.startup.single		= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		.teardown.single	= smpcfd_dying_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	/* Entry state on starting. Interrupts enabled from here on. Transient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	 * state for synchronsization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	[CPUHP_AP_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		.name			= "ap:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	 * Handled on controll processor until the plugged processor manages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	 * this itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	[CPUHP_TEARDOWN_CPU] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		.name			= "cpu:teardown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		.startup.single		= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		.teardown.single	= takedown_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		.cant_stop		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	/* Handle smpboot threads park/unpark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	[CPUHP_AP_SMPBOOT_THREADS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		.name			= "smpboot/threads:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		.startup.single		= smpboot_unpark_threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		.teardown.single	= smpboot_park_threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	[CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		.name			= "irq/affinity:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		.startup.single		= irq_affinity_online_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	[CPUHP_AP_PERF_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		.name			= "perf:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		.startup.single		= perf_event_init_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		.teardown.single	= perf_event_exit_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	[CPUHP_AP_WATCHDOG_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		.name			= "lockup_detector:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		.startup.single		= lockup_detector_online_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		.teardown.single	= lockup_detector_offline_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	[CPUHP_AP_WORKQUEUE_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		.name			= "workqueue:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		.startup.single		= workqueue_online_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		.teardown.single	= workqueue_offline_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	[CPUHP_AP_RCUTREE_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		.name			= "RCU/tree:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		.startup.single		= rcutree_online_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		.teardown.single	= rcutree_offline_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	 * The dynamically registered state space is here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	/* Last state is scheduler control setting the cpu active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	[CPUHP_AP_ACTIVE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		.name			= "sched:active",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		.startup.single		= sched_cpu_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		.teardown.single	= sched_cpu_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	/* CPU is fully up and running. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	[CPUHP_ONLINE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		.name			= "online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		.startup.single		= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 		.teardown.single	= NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) /* Sanity check for callbacks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) static int cpuhp_cb_check(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)  * Returns a free for dynamic slot assignment of the Online state. The states
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038)  * are protected by the cpuhp_slot_states mutex and an empty slot is identified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)  * by having no name assigned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) static int cpuhp_reserve_state(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	enum cpuhp_state i, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	struct cpuhp_step *step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	case CPUHP_AP_ONLINE_DYN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 		end = CPUHP_AP_ONLINE_DYN_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	case CPUHP_BP_PREPARE_DYN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		end = CPUHP_BP_PREPARE_DYN_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	for (i = state; i <= end; i++, step++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		if (!step->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	WARN(1, "No more dynamic states available for CPU hotplug\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 				 int (*startup)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 				 int (*teardown)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				 bool multi_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	/* (Un)Install the callbacks for further cpu hotplug operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	struct cpuhp_step *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	 * If name is NULL, then the state gets removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	 * the first allocation from these dynamic ranges, so the removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	 * would trigger a new allocation and clear the wrong (already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	 * empty) state, leaving the callbacks of the to be cleared state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	 * dangling, which causes wreckage on the next hotplug operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	if (name && (state == CPUHP_AP_ONLINE_DYN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		     state == CPUHP_BP_PREPARE_DYN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		ret = cpuhp_reserve_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		state = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	sp = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	if (name && sp->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	sp->startup.single = startup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	sp->teardown.single = teardown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	sp->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	sp->multi_instance = multi_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	INIT_HLIST_HEAD(&sp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	return cpuhp_get_step(state)->teardown.single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)  * Call the startup/teardown function for a step either on the AP or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111)  * on the current CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 			    struct hlist_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	struct cpuhp_step *sp = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	 * If there's nothing to do, we done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	 * Relies on the union for multi_instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	if ((bringup && !sp->startup.single) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	    (!bringup && !sp->teardown.single))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	 * The non AP bound callbacks can fail on bringup. On teardown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	 * e.g. module removal we crash for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	if (cpuhp_is_ap_state(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	BUG_ON(ret && !bringup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)  * Called from __cpuhp_setup_state on a recoverable failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * Note: The teardown callbacks for rollback are not allowed to fail!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 				   struct hlist_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	/* Roll back the already executed steps on the other cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		int cpustate = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		if (cpu >= failedcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		/* Did we invoke the startup call on that cpu ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		if (cpustate >= state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 			cpuhp_issue_call(cpu, state, false, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 					  struct hlist_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 					  bool invoke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	struct cpuhp_step *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	lockdep_assert_cpus_held();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	sp = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	if (sp->multi_instance == false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	if (!invoke || !sp->startup.multi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		goto add_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	 * Try to call the startup callback for each present cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	 * depending on the hotplug state of the cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		int cpustate = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		if (cpustate < state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		ret = cpuhp_issue_call(cpu, state, true, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			if (sp->teardown.multi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 				cpuhp_rollback_install(cpu, state, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) add_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	hlist_add_head(node, &sp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 			       bool invoke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)  * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)  * @state:		The state to setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)  * @invoke:		If true, the startup function is invoked for cpus where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227)  *			cpu state >= @state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)  * @startup:		startup callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)  * @teardown:		teardown callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230)  * @multi_instance:	State is set up for multiple instances which get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231)  *			added afterwards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233)  * The caller needs to hold cpus read locked while calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235)  *   On success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236)  *      Positive state number if @state is CPUHP_AP_ONLINE_DYN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)  *      0 for all other states
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)  *   On failure: proper (negative) error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 				   const char *name, bool invoke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 				   int (*startup)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 				   int (*teardown)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 				   bool multi_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	int cpu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	bool dynstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	lockdep_assert_cpus_held();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	if (cpuhp_cb_check(state) || !name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	ret = cpuhp_store_callbacks(state, name, startup, teardown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 				    multi_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	dynstate = state == CPUHP_AP_ONLINE_DYN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	if (ret > 0 && dynstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		state = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	if (ret || !invoke || !startup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	 * Try to call the startup callback for each present cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	 * depending on the hotplug state of the cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		int cpustate = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		if (cpustate < state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 		ret = cpuhp_issue_call(cpu, state, true, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 			if (teardown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 				cpuhp_rollback_install(cpu, state, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 			cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	 * dynamically allocated state in case of success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	if (!ret && dynstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) int __cpuhp_setup_state(enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			const char *name, bool invoke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 			int (*startup)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 			int (*teardown)(unsigned int cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 			bool multi_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 					     teardown, multi_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) EXPORT_SYMBOL(__cpuhp_setup_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) int __cpuhp_state_remove_instance(enum cpuhp_state state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 				  struct hlist_node *node, bool invoke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	struct cpuhp_step *sp = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	BUG_ON(cpuhp_cb_check(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	if (!sp->multi_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	if (!invoke || !cpuhp_get_teardown_cb(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 		goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	 * Call the teardown callback for each present cpu depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	 * on the hotplug state of the cpu. This function is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	 * allowed to fail currently!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		int cpustate = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		if (cpustate >= state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 			cpuhp_issue_call(cpu, state, false, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	hlist_del(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354)  * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)  * @state:	The state to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356)  * @invoke:	If true, the teardown function is invoked for cpus where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357)  *		cpu state >= @state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359)  * The caller needs to hold cpus read locked while calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360)  * The teardown callback is currently not allowed to fail. Think
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361)  * about module removal!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	struct cpuhp_step *sp = cpuhp_get_step(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	BUG_ON(cpuhp_cb_check(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	lockdep_assert_cpus_held();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	if (sp->multi_instance) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		WARN(!hlist_empty(&sp->list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		     "Error: Removing state %d which has instances left.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		     state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	if (!invoke || !cpuhp_get_teardown_cb(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 		goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	 * Call the teardown callback for each present cpu depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	 * on the hotplug state of the cpu. This function is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	 * allowed to fail currently!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		int cpustate = st->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		if (cpustate >= state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			cpuhp_issue_call(cpu, state, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	__cpuhp_remove_state_cpuslocked(state, invoke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) EXPORT_SYMBOL(__cpuhp_remove_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) #ifdef CONFIG_HOTPLUG_SMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) static void cpuhp_offline_cpu_device(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	struct device *dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	dev->offline = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	/* Tell user space about the state change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) static void cpuhp_online_cpu_device(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	struct device *dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	dev->offline = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	/* Tell user space about the state change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	kobject_uevent(&dev->kobj, KOBJ_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	int cpu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		if (topology_is_primary_thread(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 		 * As this needs to hold the cpu maps lock it's impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		 * to call device_offline() because that ends up calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 		 * cpu_down() which takes cpu maps lock. cpu maps lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 		 * needs to be held as this might race against in kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 		 * abusers of the hotplug machinery (thermal management).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		 * So nothing would update device:offline state. That would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 		 * leave the sysfs entry stale and prevent onlining after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		 * smt control has been changed to 'off' again. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		 * called under the sysfs hotplug lock, so it is properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		 * serialized against the regular offline usage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		cpuhp_offline_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		cpu_smt_control = ctrlval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) int cpuhp_smt_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	int cpu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	cpu_maps_update_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	cpu_smt_control = CPU_SMT_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 		/* Skip online CPUs and CPUs on offline nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		/* See comment in cpuhp_smt_disable() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		cpuhp_online_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	cpu_maps_update_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) static ssize_t show_cpuhp_state(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	return sprintf(buf, "%d\n", st->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) static ssize_t write_cpuhp_target(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 				  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	struct cpuhp_step *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	int target, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	ret = kstrtoint(buf, 10, &target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	ret = lock_device_hotplug_sysfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	sp = cpuhp_get_step(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	if (st->state < target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		ret = cpu_up(dev->id, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		ret = cpu_down(dev->id, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) static ssize_t show_cpuhp_target(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 				 struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	return sprintf(buf, "%d\n", st->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) static ssize_t write_cpuhp_fail(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 				const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	struct cpuhp_step *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	int fail, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	ret = kstrtoint(buf, 10, &fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	 * Cannot fail STARTING/DYING callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	if (cpuhp_is_atomic_state(fail))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	 * Cannot fail anything that doesn't have callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	sp = cpuhp_get_step(fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	if (!sp->startup.single && !sp->teardown.single)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	st->fail = fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) static ssize_t show_cpuhp_fail(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			       struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	return sprintf(buf, "%d\n", st->fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) static struct attribute *cpuhp_cpu_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	&dev_attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	&dev_attr_target.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	&dev_attr_fail.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) static const struct attribute_group cpuhp_cpu_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	.attrs = cpuhp_cpu_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	.name = "hotplug",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) static ssize_t show_cpuhp_states(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 				 struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	ssize_t cur, res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	mutex_lock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 		struct cpuhp_step *sp = cpuhp_get_step(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		if (sp->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			cur = sprintf(buf, "%3d: %s\n", i, sp->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 			buf += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 			res += cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	mutex_unlock(&cpuhp_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) static struct attribute *cpuhp_cpu_root_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	&dev_attr_states.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) static const struct attribute_group cpuhp_cpu_root_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	.attrs = cpuhp_cpu_root_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	.name = "hotplug",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) #ifdef CONFIG_HOTPLUG_SMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) __store_smt_control(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	int ctrlval, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	if (sysfs_streq(buf, "on"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		ctrlval = CPU_SMT_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	else if (sysfs_streq(buf, "off"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		ctrlval = CPU_SMT_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	else if (sysfs_streq(buf, "forceoff"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		ctrlval = CPU_SMT_FORCE_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	ret = lock_device_hotplug_sysfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	if (ctrlval != cpu_smt_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		switch (ctrlval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 		case CPU_SMT_ENABLED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 			ret = cpuhp_smt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		case CPU_SMT_DISABLED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		case CPU_SMT_FORCE_DISABLED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 			ret = cpuhp_smt_disable(ctrlval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) #else /* !CONFIG_HOTPLUG_SMT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) __store_smt_control(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 		    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) #endif /* CONFIG_HOTPLUG_SMT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) static const char *smt_states[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	[CPU_SMT_ENABLED]		= "on",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	[CPU_SMT_DISABLED]		= "off",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	[CPU_SMT_FORCE_DISABLED]	= "forceoff",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	[CPU_SMT_NOT_SUPPORTED]		= "notsupported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	[CPU_SMT_NOT_IMPLEMENTED]	= "notimplemented",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	const char *state = smt_states[cpu_smt_control];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) store_smt_control(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 		  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	return __store_smt_control(dev, attr, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) static struct attribute *cpuhp_smt_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	&dev_attr_control.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	&dev_attr_active.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) static const struct attribute_group cpuhp_smt_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	.attrs = cpuhp_smt_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	.name = "smt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) static int __init cpu_smt_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	return sysfs_create_group(&cpu_subsys.dev_root->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 				  &cpuhp_smt_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) static int __init cpuhp_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	ret = cpu_smt_sysfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 				 &cpuhp_cpu_root_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		struct device *dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 		ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) device_initcall(cpuhp_sysfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762)  * cpu_bit_bitmap[] is a special, "compressed" data structure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763)  * represents all NR_CPUS bits binary values of 1<<nr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765)  * It is used by cpumask_of() to get a constant address to a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766)  * mask value that has a single bit set only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) /* cpu_bit_bitmap[0] is empty - so we can back into it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) #if BITS_PER_LONG > 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) EXPORT_SYMBOL(cpu_all_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) #ifdef CONFIG_INIT_ALL_POSSIBLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) struct cpumask __cpu_possible_mask __read_mostly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	= {CPU_BITS_ALL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) struct cpumask __cpu_possible_mask __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) EXPORT_SYMBOL(__cpu_possible_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) struct cpumask __cpu_online_mask __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) EXPORT_SYMBOL(__cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) struct cpumask __cpu_present_mask __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) EXPORT_SYMBOL(__cpu_present_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) struct cpumask __cpu_active_mask __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) EXPORT_SYMBOL(__cpu_active_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) atomic_t __num_online_cpus __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) EXPORT_SYMBOL(__num_online_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) void init_cpu_present(const struct cpumask *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	cpumask_copy(&__cpu_present_mask, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) void init_cpu_possible(const struct cpumask *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	cpumask_copy(&__cpu_possible_mask, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) void init_cpu_online(const struct cpumask *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	cpumask_copy(&__cpu_online_mask, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) void set_cpu_online(unsigned int cpu, bool online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	 * atomic_inc/dec() is required to handle the horrid abuse of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	 * function by the reboot and kexec code which invoke it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	 * IPI/NMI broadcasts when shutting down CPUs. Invocation from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	 * regular CPU hotplug is properly serialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	 * Note, that the fact that __num_online_cpus is of type atomic_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	 * does not protect readers which are not serialized against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	 * concurrent hotplug operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	if (online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 		if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 			atomic_inc(&__num_online_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 			atomic_dec(&__num_online_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846)  * Activate the first processor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) void __init boot_cpu_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	/* Mark the boot cpu "present", "online" etc for SMP and UP case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	set_cpu_online(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	set_cpu_active(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	set_cpu_present(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	set_cpu_possible(cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	__boot_cpu_id = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864)  * Must be called _AFTER_ setting up the per_cpu areas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) void __init boot_cpu_hotplug_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875)  * These are used for a global "mitigations=" cmdline option for toggling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876)  * optional CPU mitigations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) enum cpu_mitigations {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	CPU_MITIGATIONS_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	CPU_MITIGATIONS_AUTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	CPU_MITIGATIONS_AUTO_NOSMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) static enum cpu_mitigations cpu_mitigations __ro_after_init =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	CPU_MITIGATIONS_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) static int __init mitigations_parse_cmdline(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	if (!strcmp(arg, "off"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		cpu_mitigations = CPU_MITIGATIONS_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	else if (!strcmp(arg, "auto"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		cpu_mitigations = CPU_MITIGATIONS_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	else if (!strcmp(arg, "auto,nosmt"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 		cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 			arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) early_param("mitigations", mitigations_parse_cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) /* mitigations=off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) bool cpu_mitigations_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 	return cpu_mitigations == CPU_MITIGATIONS_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) EXPORT_SYMBOL_GPL(cpu_mitigations_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) /* mitigations=auto,nosmt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) bool cpu_mitigations_auto_nosmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) static ATOMIC_NOTIFIER_HEAD(idle_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) void idle_notifier_register(struct notifier_block *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	atomic_notifier_chain_register(&idle_notifier, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) EXPORT_SYMBOL_GPL(idle_notifier_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) void idle_notifier_unregister(struct notifier_block *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	atomic_notifier_chain_unregister(&idle_notifier, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) EXPORT_SYMBOL_GPL(idle_notifier_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) void idle_notifier_call_chain(unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	atomic_notifier_call_chain(&idle_notifier, val, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) EXPORT_SYMBOL_GPL(idle_notifier_call_chain);