3136 lines · 76.4 KB · C
RawDownload
/* CPU control. * (C) 2001, 2002, 2003, 2004 Rusty Russell * * This code is licenced under the GPL. */#include <linux/sched/mm.h>#include <linux/proc_fs.h>#include <linux/smp.h>#include <linux/init.h>#include <linux/notifier.h>#include <linux/sched/signal.h>#include <linux/sched/hotplug.h>#include <linux/sched/isolation.h>#include <linux/sched/task.h>#include <linux/sched/smt.h>#include <linux/unistd.h>#include <linux/cpu.h>#include <linux/oom.h>#include <linux/rcupdate.h>#include <linux/delay.h>#include <linux/export.h>#include <linux/bug.h>#include <linux/kthread.h>#include <linux/stop_machine.h>#include <linux/mutex.h>#include <linux/gfp.h>#include <linux/suspend.h>#include <linux/lockdep.h>#include <linux/tick.h>#include <linux/irq.h>#include <linux/nmi.h>#include <linux/smpboot.h>#include <linux/relay.h>#include <linux/slab.h>#include <linux/scs.h>#include <linux/percpu-rwsem.h>#include <linux/cpuset.h>#include <linux/random.h>#include <linux/cc_platform.h> #include <trace/events/power.h>#define CREATE_TRACE_POINTS#include <trace/events/cpuhp.h> #include "smpboot.h" /** * struct cpuhp_cpu_state - Per cpu hotplug state storage * @state:	The current cpu state * @target:	The target state * @fail:	Current CPU hotplug callback state * @thread:	Pointer to the hotplug thread * @should_run:	Thread should execute * @rollback:	Perform a rollback * @single:	Single callback invocation * @bringup:	Single callback bringup or teardown selector * @cpu:	CPU number * @node:	Remote CPU node; for multi-instance, do a *		single entry callback for install/remove * @last:	For multi-instance rollback, remember how far we got * @cb_state:	The state for a single callback (install/uninstall) * @result:	Result of the operation * @ap_sync_state:	State for AP synchronization * @done_up:	Signal completion to the issuer of the task for cpu-up * @done_down:	Signal completion to the issuer of the task for cpu-down */struct cpuhp_cpu_state {	enum cpuhp_state	state;	enum cpuhp_state	target;	enum cpuhp_state	fail;#ifdef CONFIG_SMP	struct task_struct	*thread;	bool			should_run;	bool			rollback;	bool			single;	bool			bringup;	struct hlist_node	*node;	struct hlist_node	*last;	enum cpuhp_state	cb_state;	int			result;	atomic_t		ap_sync_state;	struct completion	done_up;	struct completion	done_down;#endif}; static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {	.fail = CPUHP_INVALID,}; #ifdef CONFIG_SMPcpumask_t cpus_booted_once_mask;#endif #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)static struct lockdep_map cpuhp_state_up_map =	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);static struct lockdep_map cpuhp_state_down_map =	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);  static inline void cpuhp_lock_acquire(bool bringup){	lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);} static inline void cpuhp_lock_release(bool bringup){	lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);}#else static inline void cpuhp_lock_acquire(bool bringup) { }static inline void cpuhp_lock_release(bool bringup) { } #endif /** * struct cpuhp_step - Hotplug state machine step * @name:	Name of the step * @startup:	Startup function of the step * @teardown:	Teardown function of the step * @cant_stop:	Bringup/teardown can't be stopped at this step * @multi_instance:	State has multiple instances which get added afterwards */struct cpuhp_step {	const char		*name;	union {		int		(*single)(unsigned int cpu);		int		(*multi)(unsigned int cpu,					 struct hlist_node *node);	} startup;	union {		int		(*single)(unsigned int cpu);		int		(*multi)(unsigned int cpu,					 struct hlist_node *node);	} teardown;	/* private: */	struct hlist_head	list;	/* public: */	bool			cant_stop;	bool			multi_instance;}; static DEFINE_MUTEX(cpuhp_state_mutex);static struct cpuhp_step cpuhp_hp_states[]; static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state){	return cpuhp_hp_states + state;} static bool cpuhp_step_empty(bool bringup, struct cpuhp_step *step){	return bringup ? !step->startup.single : !step->teardown.single;} /** * cpuhp_invoke_callback - Invoke the callbacks for a given state * @cpu:	The cpu for which the callback should be invoked * @state:	The state to do callbacks for * @bringup:	True if the bringup callback should be invoked * @node:	For multi-instance, do a single entry callback for install/remove * @lastp:	For multi-instance rollback, remember how far we got * * Called from cpu hotplug and from the state register machinery. * * Return: %0 on success or a negative errno code */static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,				 bool bringup, struct hlist_node *node,				 struct hlist_node **lastp){	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);	struct cpuhp_step *step = cpuhp_get_step(state);	int (*cbm)(unsigned int cpu, struct hlist_node *node);	int (*cb)(unsigned int cpu);	int ret, cnt; 	if (st->fail == state) {		st->fail = CPUHP_INVALID;		return -EAGAIN;	} 	if (cpuhp_step_empty(bringup, step)) {		WARN_ON_ONCE(1);		return 0;	} 	if (!step->multi_instance) {		WARN_ON_ONCE(lastp && *lastp);		cb = bringup ? step->startup.single : step->teardown.single; 		trace_cpuhp_enter(cpu, st->target, state, cb);		ret = cb(cpu);		trace_cpuhp_exit(cpu, st->state, state, ret);		return ret;	}	cbm = bringup ? step->startup.multi : step->teardown.multi; 	/* Single invocation for instance add/remove */	if (node) {		WARN_ON_ONCE(lastp && *lastp);		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);		ret = cbm(cpu, node);		trace_cpuhp_exit(cpu, st->state, state, ret);		return ret;	} 	/* State transition. Invoke on all instances */	cnt = 0;	hlist_for_each(node, &step->list) {		if (lastp && node == *lastp)			break; 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);		ret = cbm(cpu, node);		trace_cpuhp_exit(cpu, st->state, state, ret);		if (ret) {			if (!lastp)				goto err; 			*lastp = node;			return ret;		}		cnt++;	}	if (lastp)		*lastp = NULL;	return 0;err:	/* Rollback the instances if one failed */	cbm = !bringup ? step->startup.multi : step->teardown.multi;	if (!cbm)		return ret; 	hlist_for_each(node, &step->list) {		if (!cnt--)			break; 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);		ret = cbm(cpu, node);		trace_cpuhp_exit(cpu, st->state, state, ret);		/*		 * Rollback must not fail,		 */		WARN_ON_ONCE(ret);	}	return ret;} #ifdef CONFIG_SMPstatic bool cpuhp_is_ap_state(enum cpuhp_state state){	/*	 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation	 * purposes as that state is handled explicitly in cpu_down.	 */	return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;} static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup){	struct completion *done = bringup ? &st->done_up : &st->done_down;	wait_for_completion(done);} static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup){	struct completion *done = bringup ? &st->done_up : &st->done_down;	complete(done);} /* * The former STARTING/DYING states, ran with IRQs disabled and must not fail. */static bool cpuhp_is_atomic_state(enum cpuhp_state state){	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;} /* Synchronization state management */enum cpuhp_sync_state {	SYNC_STATE_DEAD,	SYNC_STATE_KICKED,	SYNC_STATE_SHOULD_DIE,	SYNC_STATE_ALIVE,	SYNC_STATE_SHOULD_ONLINE,	SYNC_STATE_ONLINE,}; #ifdef CONFIG_HOTPLUG_CORE_SYNC/** * cpuhp_ap_update_sync_state - Update synchronization state during bringup/teardown * @state:	The synchronization state to set * * No synchronization point. Just update of the synchronization state, but implies * a full barrier so that the AP changes are visible before the control CPU proceeds. */static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state){	atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state); 	(void)atomic_xchg(st, state);} void __weak arch_cpuhp_sync_state_poll(void) { cpu_relax(); } static bool cpuhp_wait_for_sync_state(unsigned int cpu, enum cpuhp_sync_state state,				      enum cpuhp_sync_state next_state){	atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);	ktime_t now, end, start = ktime_get();	int sync; 	end = start + 10ULL * NSEC_PER_SEC; 	sync = atomic_read(st);	while (1) {		if (sync == state) {			if (!atomic_try_cmpxchg(st, &sync, next_state))				continue;			return true;		} 		now = ktime_get();		if (now > end) {			/* Timeout. Leave the state unchanged */			return false;		} else if (now - start < NSEC_PER_MSEC) {			/* Poll for one millisecond */			arch_cpuhp_sync_state_poll();		} else {			usleep_range_state(USEC_PER_MSEC, 2 * USEC_PER_MSEC, TASK_UNINTERRUPTIBLE);		}		sync = atomic_read(st);	}	return true;}#else  /* CONFIG_HOTPLUG_CORE_SYNC */static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state) { }#endif /* !CONFIG_HOTPLUG_CORE_SYNC */ #ifdef CONFIG_HOTPLUG_CORE_SYNC_DEAD/** * cpuhp_ap_report_dead - Update synchronization state to DEAD * * No synchronization point. Just update of the synchronization state. */void cpuhp_ap_report_dead(void){	cpuhp_ap_update_sync_state(SYNC_STATE_DEAD);} void __weak arch_cpuhp_cleanup_dead_cpu(unsigned int cpu) { } /* * Late CPU shutdown synchronization point. Cannot use cpuhp_state::done_down * because the AP cannot issue complete() at this stage. */static void cpuhp_bp_sync_dead(unsigned int cpu){	atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);	int sync = atomic_read(st); 	do {		/* CPU can have reported dead already. Don't overwrite that! */		if (sync == SYNC_STATE_DEAD)			break;	} while (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_SHOULD_DIE)); 	if (cpuhp_wait_for_sync_state(cpu, SYNC_STATE_DEAD, SYNC_STATE_DEAD)) {		/* CPU reached dead state. Invoke the cleanup function */		arch_cpuhp_cleanup_dead_cpu(cpu);		return;	} 	/* No further action possible. Emit message and give up. */	pr_err("CPU%u failed to report dead state\n", cpu);}#else /* CONFIG_HOTPLUG_CORE_SYNC_DEAD */static inline void cpuhp_bp_sync_dead(unsigned int cpu) { }#endif /* !CONFIG_HOTPLUG_CORE_SYNC_DEAD */ #ifdef CONFIG_HOTPLUG_CORE_SYNC_FULL/** * cpuhp_ap_sync_alive - Synchronize AP with the control CPU once it is alive * * Updates the AP synchronization state to SYNC_STATE_ALIVE and waits * for the BP to release it. */void cpuhp_ap_sync_alive(void){	atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state); 	cpuhp_ap_update_sync_state(SYNC_STATE_ALIVE); 	/* Wait for the control CPU to release it. */	while (atomic_read(st) != SYNC_STATE_SHOULD_ONLINE)		cpu_relax();} static bool cpuhp_can_boot_ap(unsigned int cpu){	atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);	int sync = atomic_read(st); again:	switch (sync) {	case SYNC_STATE_DEAD:		/* CPU is properly dead */		break;	case SYNC_STATE_KICKED:		/* CPU did not come up in previous attempt */		break;	case SYNC_STATE_ALIVE:		/* CPU is stuck cpuhp_ap_sync_alive(). */		break;	default:		/* CPU failed to report online or dead and is in limbo state. */		return false;	} 	/* Prepare for booting */	if (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_KICKED))		goto again; 	return true;} void __weak arch_cpuhp_cleanup_kick_cpu(unsigned int cpu) { } /* * Early CPU bringup synchronization point. Cannot use cpuhp_state::done_up * because the AP cannot issue complete() so early in the bringup. */static int cpuhp_bp_sync_alive(unsigned int cpu){	int ret = 0; 	if (!IS_ENABLED(CONFIG_HOTPLUG_CORE_SYNC_FULL))		return 0; 	if (!cpuhp_wait_for_sync_state(cpu, SYNC_STATE_ALIVE, SYNC_STATE_SHOULD_ONLINE)) {		pr_err("CPU%u failed to report alive state\n", cpu);		ret = -EIO;	} 	/* Let the architecture cleanup the kick alive mechanics. */	arch_cpuhp_cleanup_kick_cpu(cpu);	return ret;}#else /* CONFIG_HOTPLUG_CORE_SYNC_FULL */static inline int cpuhp_bp_sync_alive(unsigned int cpu) { return 0; }static inline bool cpuhp_can_boot_ap(unsigned int cpu) { return true; }#endif /* !CONFIG_HOTPLUG_CORE_SYNC_FULL */ /* Serializes the updates to cpu_online_mask, cpu_present_mask */static DEFINE_MUTEX(cpu_add_remove_lock);bool cpuhp_tasks_frozen;EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen); /* * The following two APIs (cpu_maps_update_begin/done) must be used when * attempting to serialize the updates to cpu_online_mask & cpu_present_mask. */void cpu_maps_update_begin(void){	mutex_lock(&cpu_add_remove_lock);} void cpu_maps_update_done(void){	mutex_unlock(&cpu_add_remove_lock);} /* * If set, cpu_up and cpu_down will return -EBUSY and do nothing. * Should always be manipulated under cpu_add_remove_lock */static int cpu_hotplug_disabled; #ifdef CONFIG_HOTPLUG_CPU DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock); void cpus_read_lock(void){	percpu_down_read(&cpu_hotplug_lock);}EXPORT_SYMBOL_GPL(cpus_read_lock); int cpus_read_trylock(void){	return percpu_down_read_trylock(&cpu_hotplug_lock);}EXPORT_SYMBOL_GPL(cpus_read_trylock); void cpus_read_unlock(void){

Lines 1–500 of 3136.