2203 lines · 77.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * Sleepable Read-Copy Update mechanism for mutual exclusion. * * Copyright (C) IBM Corporation, 2006 * Copyright (C) Fujitsu, 2012 * * Authors: Paul McKenney <paulmck@linux.ibm.com> *	   Lai Jiangshan <laijs@cn.fujitsu.com> * * For detailed explanation of Read-Copy Update mechanism see - *		Documentation/RCU/ *.txt * */ #define pr_fmt(fmt) "rcu: " fmt #include <linux/export.h>#include <linux/mutex.h>#include <linux/percpu.h>#include <linux/preempt.h>#include <linux/irq_work.h>#include <linux/rcupdate_wait.h>#include <linux/sched.h>#include <linux/smp.h>#include <linux/delay.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/srcu.h> #include "rcu.h"#include "rcu_segcblist.h" /* Holdoff in nanoseconds for auto-expediting. */#define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;module_param(exp_holdoff, ulong, 0444); /* Overflow-check frequency.  N bits roughly says every 2**N grace periods. */static ulong counter_wrap_check = (ULONG_MAX >> 2);module_param(counter_wrap_check, ulong, 0444); /* * Control conversion to SRCU_SIZE_BIG: *    0: Don't convert at all. *    1: Convert at init_srcu_struct() time. *    2: Convert when rcutorture invokes srcu_torture_stats_print(). *    3: Decide at boot time based on system shape (default). * 0x1x: Convert when excessive contention encountered. */#define SRCU_SIZING_NONE	0#define SRCU_SIZING_INIT	1#define SRCU_SIZING_TORTURE	2#define SRCU_SIZING_AUTO	3#define SRCU_SIZING_CONTEND	0x10#define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x)#define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE))#define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT))#define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE))#define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND)static int convert_to_big = SRCU_SIZING_AUTO;module_param(convert_to_big, int, 0444); /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */static int big_cpu_lim __read_mostly = 128;module_param(big_cpu_lim, int, 0444); /* Contention events per jiffy to initiate transition to big. */static int small_contention_lim __read_mostly = 100;module_param(small_contention_lim, int, 0444); /* Early-boot callback-management, so early that no lock is required! */static LIST_HEAD(srcu_boot_list);static bool __read_mostly srcu_init_done; static void srcu_invoke_callbacks(struct work_struct *work);static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay);static void process_srcu(struct work_struct *work);static void srcu_irq_work(struct irq_work *work);static void srcu_delay_timer(struct timer_list *t); /* * Initialize SRCU per-CPU data.  Note that statically allocated * srcu_struct structures might already have srcu_read_lock() and * srcu_read_unlock() running against them.  So if the is_static * parameter is set, don't initialize ->srcu_ctrs[].srcu_locks and * ->srcu_ctrs[].srcu_unlocks. */static void init_srcu_struct_data(struct srcu_struct *ssp){	int cpu;	struct srcu_data *sdp; 	/*	 * Initialize the per-CPU srcu_data array, which feeds into the	 * leaves of the srcu_node tree.	 */	for_each_possible_cpu(cpu) {		sdp = per_cpu_ptr(ssp->sda, cpu);		raw_spin_lock_init(&ACCESS_PRIVATE(sdp, lock));		rcu_segcblist_init(&sdp->srcu_cblist);		sdp->srcu_cblist_invoking = false;		sdp->srcu_gp_seq_needed = ssp->srcu_sup->srcu_gp_seq;		sdp->srcu_gp_seq_needed_exp = ssp->srcu_sup->srcu_gp_seq;		sdp->srcu_barrier_head.next = &sdp->srcu_barrier_head;		sdp->mynode = NULL;		sdp->cpu = cpu;		INIT_WORK(&sdp->work, srcu_invoke_callbacks);		timer_setup(&sdp->delay_work, srcu_delay_timer, 0);		sdp->ssp = ssp;	}} /* Invalid seq state, used during snp node initialization */#define SRCU_SNP_INIT_SEQ		0x2 /* * Check whether sequence number corresponding to snp node, * is invalid. */static inline bool srcu_invl_snp_seq(unsigned long s){	return s == SRCU_SNP_INIT_SEQ;} /* * Allocated and initialize SRCU combining tree.  Returns @true if * allocation succeeded and @false otherwise. */static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags){	int cpu;	int i;	int level = 0;	int levelspread[RCU_NUM_LVLS];	struct srcu_data *sdp;	struct srcu_node *snp;	struct srcu_node *snp_first; 	/* Initialize geometry if it has not already been initialized. */	rcu_init_geometry();	ssp->srcu_sup->node = kzalloc_objs(*ssp->srcu_sup->node, rcu_num_nodes,					   gfp_flags);	if (!ssp->srcu_sup->node)		return false; 	/* Work out the overall tree geometry. */	ssp->srcu_sup->level[0] = &ssp->srcu_sup->node[0];	for (i = 1; i < rcu_num_lvls; i++)		ssp->srcu_sup->level[i] = ssp->srcu_sup->level[i - 1] + num_rcu_lvl[i - 1];	rcu_init_levelspread(levelspread, num_rcu_lvl); 	/* Each pass through this loop initializes one srcu_node structure. */	srcu_for_each_node_breadth_first(ssp, snp) {		raw_spin_lock_init(&ACCESS_PRIVATE(snp, lock));		BUILD_BUG_ON(ARRAY_SIZE(snp->srcu_have_cbs) !=			     ARRAY_SIZE(snp->srcu_data_have_cbs));		for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {			snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ;			snp->srcu_data_have_cbs[i] = 0;		}		snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ;		snp->grplo = -1;		snp->grphi = -1;		if (snp == &ssp->srcu_sup->node[0]) {			/* Root node, special case. */			snp->srcu_parent = NULL;			continue;		} 		/* Non-root node. */		if (snp == ssp->srcu_sup->level[level + 1])			level++;		snp->srcu_parent = ssp->srcu_sup->level[level - 1] +				   (snp - ssp->srcu_sup->level[level]) /				   levelspread[level - 1];	} 	/*	 * Initialize the per-CPU srcu_data array, which feeds into the	 * leaves of the srcu_node tree.	 */	level = rcu_num_lvls - 1;	snp_first = ssp->srcu_sup->level[level];	for_each_possible_cpu(cpu) {		sdp = per_cpu_ptr(ssp->sda, cpu);		sdp->mynode = &snp_first[cpu / levelspread[level]];		for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {			if (snp->grplo < 0)				snp->grplo = cpu;			snp->grphi = cpu;		}		sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);	}	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);	return true;} /* * Initialize non-compile-time initialized fields, including the * associated srcu_node and srcu_data structures.  The is_static parameter * tells us that ->sda has already been wired up to srcu_data. */static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static){	if (!is_static)		ssp->srcu_sup = kzalloc_obj(*ssp->srcu_sup);	if (!ssp->srcu_sup)		return -ENOMEM;	if (!is_static)		raw_spin_lock_init(&ACCESS_PRIVATE(ssp->srcu_sup, lock));	ssp->srcu_sup->srcu_size_state = SRCU_SIZE_SMALL;	ssp->srcu_sup->node = NULL;	mutex_init(&ssp->srcu_sup->srcu_cb_mutex);	mutex_init(&ssp->srcu_sup->srcu_gp_mutex);	ssp->srcu_sup->srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL;	ssp->srcu_sup->srcu_barrier_seq = 0;	mutex_init(&ssp->srcu_sup->srcu_barrier_mutex);	atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);	INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);	init_irq_work(&ssp->srcu_sup->irq_work, srcu_irq_work);	ssp->srcu_sup->sda_is_static = is_static;	if (!is_static) {		ssp->sda = alloc_percpu(struct srcu_data);		ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];	}	if (!ssp->sda)		goto err_free_sup;	init_srcu_struct_data(ssp);	ssp->srcu_sup->srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL;	ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns();	if (READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {		if (!preemptible())			WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC);		else if (init_srcu_struct_nodes(ssp, GFP_KERNEL))			WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);		else			goto err_free_sda;	}	ssp->srcu_sup->srcu_ssp = ssp;	smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed,			SRCU_GP_SEQ_INITIAL_VAL); /* Init done. */	return 0; err_free_sda:	if (!is_static) {		free_percpu(ssp->sda);		ssp->sda = NULL;	}err_free_sup:	if (!is_static) {		kfree(ssp->srcu_sup);		ssp->srcu_sup = NULL;	}	return -ENOMEM;} #ifdef CONFIG_DEBUG_LOCK_ALLOC static int__init_srcu_struct_common(struct srcu_struct *ssp, const char *name, struct lock_class_key *key){	/* Don't re-initialize a lock while it is held. */	debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));	lockdep_init_map(&ssp->dep_map, name, key, 0);	return init_srcu_struct_fields(ssp, false);} int __init_srcu_struct(struct srcu_struct *ssp, const char *name, struct lock_class_key *key){	ssp->srcu_reader_flavor = 0;	return __init_srcu_struct_common(ssp, name, key);}EXPORT_SYMBOL_GPL(__init_srcu_struct); int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, struct lock_class_key *key){	ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST;	return __init_srcu_struct_common(ssp, name, key);}EXPORT_SYMBOL_GPL(__init_srcu_struct_fast); int __init_srcu_struct_fast_updown(struct srcu_struct *ssp, const char *name,				   struct lock_class_key *key){	ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST_UPDOWN;	return __init_srcu_struct_common(ssp, name, key);}EXPORT_SYMBOL_GPL(__init_srcu_struct_fast_updown); #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ /** * init_srcu_struct - initialize a sleep-RCU structure * @ssp: structure to initialize. * * Use this in place of DEFINE_SRCU() and DEFINE_STATIC_SRCU() * for non-static srcu_struct structures that are to be passed to * srcu_read_lock(), srcu_read_lock_nmisafe(), and friends.  It is necessary * to invoke this on a given srcu_struct before passing that srcu_struct * to any other function.  Each srcu_struct represents a separate domain * of SRCU protection. */int init_srcu_struct(struct srcu_struct *ssp){	ssp->srcu_reader_flavor = 0;	return init_srcu_struct_fields(ssp, false);}EXPORT_SYMBOL_GPL(init_srcu_struct); /** * init_srcu_struct_fast - initialize a fast-reader sleep-RCU structure * @ssp: structure to initialize. * * Use this in place of DEFINE_SRCU_FAST() and DEFINE_STATIC_SRCU_FAST() * for non-static srcu_struct structures that are to be passed to * srcu_read_lock_fast() and friends.  It is necessary to invoke this on a * given srcu_struct before passing that srcu_struct to any other function. * Each srcu_struct represents a separate domain of SRCU protection. */int init_srcu_struct_fast(struct srcu_struct *ssp){	ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST;	return init_srcu_struct_fields(ssp, false);}EXPORT_SYMBOL_GPL(init_srcu_struct_fast); /** * init_srcu_struct_fast_updown - initialize a fast-reader up/down sleep-RCU structure * @ssp: structure to initialize. * * Use this function in place of DEFINE_SRCU_FAST_UPDOWN() and * DEFINE_STATIC_SRCU_FAST_UPDOWN() for non-static srcu_struct * structures that are to be passed to srcu_read_lock_fast_updown(), * srcu_down_read_fast(), and friends.  It is necessary to invoke this on a * given srcu_struct before passing that srcu_struct to any other function. * Each srcu_struct represents a separate domain of SRCU protection. */int init_srcu_struct_fast_updown(struct srcu_struct *ssp){	ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST_UPDOWN;	return init_srcu_struct_fields(ssp, false);}EXPORT_SYMBOL_GPL(init_srcu_struct_fast_updown); #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ /* * Initiate a transition to SRCU_SIZE_BIG with lock held. */static void __srcu_transition_to_big(struct srcu_struct *ssp){	lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC);} /* * Initiate an idempotent transition to SRCU_SIZE_BIG. */static void srcu_transition_to_big(struct srcu_struct *ssp){	unsigned long flags; 	/* Double-checked locking on ->srcu_size-state. */	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL)		return;	raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) {		raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);		return;	}	__srcu_transition_to_big(ssp);	raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);} /* * Check to see if the just-encountered contention event justifies * a transition to SRCU_SIZE_BIG. */static void raw_spin_lock_irqsave_check_contention(struct srcu_struct *ssp){	unsigned long j; 	if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_sup->srcu_size_state)		return;	j = jiffies;	if (ssp->srcu_sup->srcu_size_jiffies != j) {		ssp->srcu_sup->srcu_size_jiffies = j;		ssp->srcu_sup->srcu_n_lock_retries = 0;	}	if (++ssp->srcu_sup->srcu_n_lock_retries <= small_contention_lim)		return;	__srcu_transition_to_big(ssp);} /* * Acquire the specified srcu_data structure's ->lock, but check for * excessive contention, which results in initiation of a transition * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module * parameter permits this. */static void raw_spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags){	struct srcu_struct *ssp = sdp->ssp; 	if (raw_spin_trylock_irqsave_rcu_node(sdp, *flags))		return;	raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);	raw_spin_lock_irqsave_check_contention(ssp);	raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, *flags);	raw_spin_lock_irqsave_rcu_node(sdp, *flags);} /* * Acquire the specified srcu_struct structure's ->lock, but check for * excessive contention, which results in initiation of a transition * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module * parameter permits this. */static void raw_spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags){	if (raw_spin_trylock_irqsave_rcu_node(ssp->srcu_sup, *flags))		return;	raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);	raw_spin_lock_irqsave_check_contention(ssp);} /* * First-use initialization of statically allocated srcu_struct * structure.  Wiring up the combining tree is more than can be * done with compile-time initialization, so this check is added * to each update-side SRCU primitive.  Use ssp->lock, which -is- * compile-time initialized, to resolve races involving multiple * CPUs trying to garner first-use privileges. */static void check_init_srcu_struct(struct srcu_struct *ssp){	unsigned long flags; 	/* The smp_load_acquire() pairs with the smp_store_release(). */	if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed))) /*^^^*/		return; /* Already initialized. */	raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);	if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq_needed)) {		raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);		return;	}	init_srcu_struct_fields(ssp, true);	raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);} /* * Is the current or any upcoming grace period to be expedited? */static bool srcu_gp_is_expedited(struct srcu_struct *ssp){	struct srcu_usage *sup = ssp->srcu_sup; 	return ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp));} /* * Computes approximate total of the readers' ->srcu_ctrs[].srcu_locks * values for the rank of per-CPU counters specified by idx, and returns * true if the caller did the proper barrier (gp), and if the count of * the locks matches that of the unlocks passed in. */static bool srcu_readers_lock_idx(struct srcu_struct *ssp, int idx, bool gp, unsigned long unlocks){	int cpu;	unsigned long mask = 0;	unsigned long sum = 0; 	for_each_possible_cpu(cpu) {		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 		sum += atomic_long_read(&sdp->srcu_ctrs[idx].srcu_locks);		if (IS_ENABLED(CONFIG_PROVE_RCU))			mask = mask | READ_ONCE(sdp->srcu_reader_flavor);	}	WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask - 1)),		  "Mixed reader flavors for srcu_struct at %ps.\n", ssp);	if (mask & SRCU_READ_FLAVOR_SLOWGP && !gp)		return false;	return sum == unlocks;} /* * Returns approximate total of the readers' ->srcu_ctrs[].srcu_unlocks * values for the rank of per-CPU counters specified by idx. */static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx, unsigned long *rdm){	int cpu;	unsigned long mask = ssp->srcu_reader_flavor;	unsigned long sum = 0; 	for_each_possible_cpu(cpu) {		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 

Lines 1–500 of 2203.