1123 lines · 34.8 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0+ *//* * RCU expedited grace periods * * Copyright IBM Corporation, 2016 * * Authors: Paul E. McKenney <paulmck@linux.ibm.com> */ #include <linux/console.h>#include <linux/lockdep.h> static void rcu_exp_handler(void *unused);static int rcu_print_task_exp_stall(struct rcu_node *rnp);static void rcu_exp_print_detail_task_stall_rnp(struct rcu_node *rnp); /* * Record the start of an expedited grace period. */static void rcu_exp_gp_seq_start(void){	rcu_seq_start(&rcu_state.expedited_sequence);	rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_exp_snap);} /* * Return the value that the expedited-grace-period counter will have * at the end of the current grace period. */static __maybe_unused unsigned long rcu_exp_gp_seq_endval(void){	return rcu_seq_endval(&rcu_state.expedited_sequence);} /* * Record the end of an expedited grace period. */static void rcu_exp_gp_seq_end(void){	rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_exp_snap);	rcu_seq_end(&rcu_state.expedited_sequence);	smp_mb(); /* Ensure that consecutive grace periods serialize. */} /* * Take a snapshot of the expedited-grace-period counter, which is the * earliest value that will indicate that a full grace period has * elapsed since the current time. */static unsigned long rcu_exp_gp_seq_snap(void){	unsigned long s; 	smp_mb(); /* Caller's modifications seen first by other CPUs. */	s = rcu_seq_snap(&rcu_state.expedited_sequence);	trace_rcu_exp_grace_period(rcu_state.name, s, TPS("snap"));	return s;} /* * Given a counter snapshot from rcu_exp_gp_seq_snap(), return true * if a full expedited grace period has elapsed since that snapshot * was taken. */static bool rcu_exp_gp_seq_done(unsigned long s){	return rcu_seq_done(&rcu_state.expedited_sequence, s);} /* * Reset the ->expmaskinit values in the rcu_node tree to reflect any * recent CPU-online activity.  Note that these masks are not cleared * when CPUs go offline, so they reflect the union of all CPUs that have * ever been online.  This means that this function normally takes its * no-work-to-do fastpath. */static void sync_exp_reset_tree_hotplug(void){	bool done;	unsigned long flags;	unsigned long mask;	unsigned long oldmask;	int ncpus = smp_load_acquire(&rcu_state.ncpus); /* Order vs. locking. */	struct rcu_node *rnp;	struct rcu_node *rnp_up; 	/* If no new CPUs onlined since last time, nothing to do. */	if (likely(ncpus == rcu_state.ncpus_snap))		return;	rcu_state.ncpus_snap = ncpus; 	/*	 * Each pass through the following loop propagates newly onlined	 * CPUs for the current rcu_node structure up the rcu_node tree.	 */	rcu_for_each_leaf_node(rnp) {		raw_spin_lock_irqsave_rcu_node(rnp, flags);		if (rnp->expmaskinit == rnp->expmaskinitnext) {			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);			continue;  /* No new CPUs, nothing to do. */		} 		/* Update this node's mask, track old value for propagation. */		oldmask = rnp->expmaskinit;		rnp->expmaskinit = rnp->expmaskinitnext;		raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 		/* If was already nonzero, nothing to propagate. */		if (oldmask)			continue; 		/* Propagate the new CPU up the tree. */		mask = rnp->grpmask;		rnp_up = rnp->parent;		done = false;		while (rnp_up) {			raw_spin_lock_irqsave_rcu_node(rnp_up, flags);			if (rnp_up->expmaskinit)				done = true;			rnp_up->expmaskinit |= mask;			raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags);			if (done)				break;			mask = rnp_up->grpmask;			rnp_up = rnp_up->parent;		}	}} /* * Reset the ->expmask values in the rcu_node tree in preparation for * a new expedited grace period. */static void __maybe_unused sync_exp_reset_tree(void){	unsigned long flags;	struct rcu_node *rnp; 	sync_exp_reset_tree_hotplug();	rcu_for_each_node_breadth_first(rnp) {		raw_spin_lock_irqsave_rcu_node(rnp, flags);		WARN_ON_ONCE(rnp->expmask);		WRITE_ONCE(rnp->expmask, rnp->expmaskinit);		/*		 * Need to wait for any blocked tasks as well.	Note that		 * additional blocking tasks will also block the expedited GP		 * until such time as the ->expmask bits are cleared.		 */		if (rcu_is_leaf_node(rnp) && rcu_preempt_has_tasks(rnp))			WRITE_ONCE(rnp->exp_tasks, rnp->blkd_tasks.next);		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);	}} /* * Return non-zero if there is no RCU expedited grace period in progress * for the specified rcu_node structure, in other words, if all CPUs and * tasks covered by the specified rcu_node structure have done their bit * for the current expedited grace period. */static bool sync_rcu_exp_done(struct rcu_node *rnp){	raw_lockdep_assert_held_rcu_node(rnp);	return READ_ONCE(rnp->exp_tasks) == NULL &&	       READ_ONCE(rnp->expmask) == 0;} /* * Like sync_rcu_exp_done(), but where the caller does not hold the * rcu_node's ->lock. */static bool sync_rcu_exp_done_unlocked(struct rcu_node *rnp){	unsigned long flags;	bool ret; 	raw_spin_lock_irqsave_rcu_node(rnp, flags);	ret = sync_rcu_exp_done(rnp);	raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 	return ret;} /* * Report the exit from RCU read-side critical section for the last task * that queued itself during or before the current expedited preemptible-RCU * grace period.  This event is reported either to the rcu_node structure on * which the task was queued or to one of that rcu_node structure's ancestors, * recursively up the tree.  (Calm down, calm down, we do the recursion * iteratively!) */static void __rcu_report_exp_rnp(struct rcu_node *rnp,				 bool wake, unsigned long flags)	__releases(rnp->lock){	unsigned long mask; 	raw_lockdep_assert_held_rcu_node(rnp);	for (;;) {		if (!sync_rcu_exp_done(rnp)) {			if (!rnp->expmask)				rcu_initiate_boost(rnp, flags);			else				raw_spin_unlock_irqrestore_rcu_node(rnp, flags);			break;		}		if (rnp->parent == NULL) {			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);			if (wake)				swake_up_one(&rcu_state.expedited_wq); 			break;		}		mask = rnp->grpmask;		raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */		rnp = rnp->parent;		raw_spin_lock_rcu_node(rnp); /* irqs already disabled */		WARN_ON_ONCE(!(rnp->expmask & mask));		WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask);	}} /* * Report expedited quiescent state for specified node.  This is a * lock-acquisition wrapper function for __rcu_report_exp_rnp(). */static void __maybe_unused rcu_report_exp_rnp(struct rcu_node *rnp, bool wake){	unsigned long flags; 	raw_spin_lock_irqsave_rcu_node(rnp, flags);	__rcu_report_exp_rnp(rnp, wake, flags);} /* * Report expedited quiescent state for multiple CPUs, all covered by the * specified leaf rcu_node structure, which is acquired by the caller. */static void rcu_report_exp_cpu_mult(struct rcu_node *rnp, unsigned long flags,				    unsigned long mask_in, bool wake)				    __releases(rnp->lock){	int cpu;	unsigned long mask;	struct rcu_data *rdp; 	raw_lockdep_assert_held_rcu_node(rnp);	if (!(rnp->expmask & mask_in)) {		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);		return;	}	mask = mask_in & rnp->expmask;	WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask);	for_each_leaf_node_cpu_mask(rnp, cpu, mask) {		rdp = per_cpu_ptr(&rcu_data, cpu);		if (!IS_ENABLED(CONFIG_NO_HZ_FULL) || !rdp->rcu_forced_tick_exp)			continue;		rdp->rcu_forced_tick_exp = false;		tick_dep_clear_cpu(cpu, TICK_DEP_BIT_RCU_EXP);	}	__rcu_report_exp_rnp(rnp, wake, flags); /* Releases rnp->lock. */} /* * Report expedited quiescent state for specified rcu_data (CPU). */static void rcu_report_exp_rdp(struct rcu_data *rdp){	unsigned long flags;	struct rcu_node *rnp = rdp->mynode; 	raw_spin_lock_irqsave_rcu_node(rnp, flags);	WRITE_ONCE(rdp->cpu_no_qs.b.exp, false);	ASSERT_EXCLUSIVE_WRITER(rdp->cpu_no_qs.b.exp);	rcu_report_exp_cpu_mult(rnp, flags, rdp->grpmask, true);} /* Common code for work-done checking. */static bool sync_exp_work_done(unsigned long s){	if (rcu_exp_gp_seq_done(s)) {		trace_rcu_exp_grace_period(rcu_state.name, s, TPS("done"));		/*		 * Order GP completion with preceding accesses. Order also GP		 * completion with post GP update side accesses. Pairs with		 * rcu_seq_end().		 */		smp_mb();		return true;	}	return false;} /* * Funnel-lock acquisition for expedited grace periods.  Returns true * if some other task completed an expedited grace period that this task * can piggy-back on, and with no mutex held.  Otherwise, returns false * with the mutex held, indicating that the caller must actually do the * expedited grace period. */static bool exp_funnel_lock(unsigned long s){	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id());	struct rcu_node *rnp = rdp->mynode;	struct rcu_node *rnp_root = rcu_get_root(); 	/* Low-contention fastpath. */	if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s) &&	    (rnp == rnp_root ||	     ULONG_CMP_LT(READ_ONCE(rnp_root->exp_seq_rq), s)) &&	    mutex_trylock(&rcu_state.exp_mutex))		goto fastpath; 	/*	 * Each pass through the following loop works its way up	 * the rcu_node tree, returning if others have done the work or	 * otherwise falls through to acquire ->exp_mutex.  The mapping	 * from CPU to rcu_node structure can be inexact, as it is just	 * promoting locality and is not strictly needed for correctness.	 */	for (; rnp != NULL; rnp = rnp->parent) {		if (sync_exp_work_done(s))			return true; 		/* Work not done, either wait here or go up. */		spin_lock(&rnp->exp_lock);		if (ULONG_CMP_GE(rnp->exp_seq_rq, s)) { 			/* Someone else doing GP, so wait for them. */			spin_unlock(&rnp->exp_lock);			trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,						  rnp->grplo, rnp->grphi,						  TPS("wait"));			wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],				   sync_exp_work_done(s));			return true;		}		WRITE_ONCE(rnp->exp_seq_rq, s); /* Followers can wait on us. */		spin_unlock(&rnp->exp_lock);		trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,					  rnp->grplo, rnp->grphi, TPS("nxtlvl"));	}	mutex_lock(&rcu_state.exp_mutex);fastpath:	if (sync_exp_work_done(s)) {		mutex_unlock(&rcu_state.exp_mutex);		return true;	}	rcu_exp_gp_seq_start();	trace_rcu_exp_grace_period(rcu_state.name, s, TPS("start"));	return false;} /* * Select the CPUs within the specified rcu_node that the upcoming * expedited grace period needs to wait for. */static void __sync_rcu_exp_select_node_cpus(struct rcu_exp_work *rewp){	int cpu;	unsigned long flags;	unsigned long mask_ofl_test;	unsigned long mask_ofl_ipi;	int ret;	struct rcu_node *rnp = container_of(rewp, struct rcu_node, rew); 	raw_spin_lock_irqsave_rcu_node(rnp, flags); 	/* Each pass checks a CPU for identity, offline, and idle. */	mask_ofl_test = 0;	for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {		struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);		unsigned long mask = rdp->grpmask;		int snap; 		if (raw_smp_processor_id() == cpu ||		    !(rnp->qsmaskinitnext & mask)) {			mask_ofl_test |= mask;		} else {			/*			 * Full ordering between remote CPU's post idle accesses			 * and updater's accesses prior to current GP (and also			 * the started GP sequence number) is enforced by			 * rcu_seq_start() implicit barrier, relayed by kworkers			 * locking and even further by smp_mb__after_unlock_lock()			 * barriers chained all the way throughout the rnp locking			 * tree since sync_exp_reset_tree() and up to the current			 * leaf rnp locking.			 *			 * Ordering between remote CPU's pre idle accesses and			 * post grace period updater's accesses is enforced by the			 * below acquire semantic.			 */			snap = ct_rcu_watching_cpu_acquire(cpu);			if (rcu_watching_snap_in_eqs(snap))				mask_ofl_test |= mask;			else				rdp->exp_watching_snap = snap;		}	}	mask_ofl_ipi = rnp->expmask & ~mask_ofl_test; 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 	/* IPI the remaining CPUs for expedited quiescent state. */	for_each_leaf_node_cpu_mask(rnp, cpu, mask_ofl_ipi) {		struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);		unsigned long mask = rdp->grpmask; retry_ipi:		if (rcu_watching_snap_stopped_since(rdp, rdp->exp_watching_snap)) {			mask_ofl_test |= mask;			continue;		}		if (get_cpu() == cpu) {			mask_ofl_test |= mask;			put_cpu();			continue;		}		ret = smp_call_function_single(cpu, rcu_exp_handler, NULL, 0);		put_cpu();		/* The CPU will report the QS in response to the IPI. */		if (!ret)			continue; 		/* Failed, raced with CPU hotplug operation. */		raw_spin_lock_irqsave_rcu_node(rnp, flags);		if ((rnp->qsmaskinitnext & mask) &&		    (rnp->expmask & mask)) {			/* Online, so delay for a bit and try again. */			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);			trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("selectofl"));			schedule_timeout_idle(1);			goto retry_ipi;		}		/* CPU really is offline, so we must report its QS. */		if (rnp->expmask & mask)			mask_ofl_test |= mask;		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);	}	/* Report quiescent states for those that went offline. */	if (mask_ofl_test) {		raw_spin_lock_irqsave_rcu_node(rnp, flags);		rcu_report_exp_cpu_mult(rnp, flags, mask_ofl_test, false);	}} static void rcu_exp_sel_wait_wake(unsigned long s); static void sync_rcu_exp_select_node_cpus(struct kthread_work *wp){	struct rcu_exp_work *rewp =		container_of(wp, struct rcu_exp_work, rew_work); 	__sync_rcu_exp_select_node_cpus(rewp);} static inline bool rcu_exp_worker_started(void){	return !!READ_ONCE(rcu_exp_gp_kworker);} static inline bool rcu_exp_par_worker_started(struct rcu_node *rnp){	return !!READ_ONCE(rnp->exp_kworker);} static inline void sync_rcu_exp_select_cpus_queue_work(struct rcu_node *rnp){	kthread_init_work(&rnp->rew.rew_work, sync_rcu_exp_select_node_cpus);	/*	 * Use rcu_exp_par_gp_kworker, because flushing a work item from	 * another work item on the same kthread worker can result in	 * deadlock.	 */	kthread_queue_work(READ_ONCE(rnp->exp_kworker), &rnp->rew.rew_work);} static inline void sync_rcu_exp_select_cpus_flush_work(struct rcu_node *rnp){	kthread_flush_work(&rnp->rew.rew_work);} /* * Work-queue handler to drive an expedited grace period forward. */static void wait_rcu_exp_gp(struct kthread_work *wp){	struct rcu_exp_work *rewp; 	rewp = container_of(wp, struct rcu_exp_work, rew_work);	rcu_exp_sel_wait_wake(rewp->rew_s);} static inline void synchronize_rcu_expedited_queue_work(struct rcu_exp_work *rew){	kthread_init_work(&rew->rew_work, wait_rcu_exp_gp);	kthread_queue_work(rcu_exp_gp_kworker, &rew->rew_work);} 

Lines 1–500 of 1123.