1e7ee1501SPaul E. McKenney // SPDX-License-Identifier: GPL-2.0+ 2dad81a20SPaul E. McKenney /* 3dad81a20SPaul E. McKenney * Sleepable Read-Copy Update mechanism for mutual exclusion. 4dad81a20SPaul E. McKenney * 5dad81a20SPaul E. McKenney * Copyright (C) IBM Corporation, 2006 6dad81a20SPaul E. McKenney * Copyright (C) Fujitsu, 2012 7dad81a20SPaul E. McKenney * 865bb0dc4SSeongJae Park * Authors: Paul McKenney <[email protected]> 9dad81a20SPaul E. McKenney * Lai Jiangshan <[email protected]> 10dad81a20SPaul E. McKenney * 11dad81a20SPaul E. McKenney * For detailed explanation of Read-Copy Update mechanism see - 12dad81a20SPaul E. McKenney * Documentation/RCU/ *.txt 13dad81a20SPaul E. McKenney * 14dad81a20SPaul E. McKenney */ 15dad81a20SPaul E. McKenney 16a7538352SJoe Perches #define pr_fmt(fmt) "rcu: " fmt 17a7538352SJoe Perches 18dad81a20SPaul E. McKenney #include <linux/export.h> 19dad81a20SPaul E. McKenney #include <linux/mutex.h> 20dad81a20SPaul E. McKenney #include <linux/percpu.h> 21dad81a20SPaul E. McKenney #include <linux/preempt.h> 22dad81a20SPaul E. McKenney #include <linux/rcupdate_wait.h> 23dad81a20SPaul E. McKenney #include <linux/sched.h> 24dad81a20SPaul E. McKenney #include <linux/smp.h> 25dad81a20SPaul E. McKenney #include <linux/delay.h> 2622607d66SPaul E. McKenney #include <linux/module.h> 272ec30311SPaul E. McKenney #include <linux/slab.h> 28dad81a20SPaul E. McKenney #include <linux/srcu.h> 29dad81a20SPaul E. McKenney 30dad81a20SPaul E. McKenney #include "rcu.h" 3145753c5fSIngo Molnar #include "rcu_segcblist.h" 32dad81a20SPaul E. McKenney 330c8e0e3cSPaul E. McKenney /* Holdoff in nanoseconds for auto-expediting. */ 340c8e0e3cSPaul E. McKenney #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000) 350c8e0e3cSPaul E. McKenney static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF; 3622607d66SPaul E. McKenney module_param(exp_holdoff, ulong, 0444); 3722607d66SPaul E. McKenney 38c350c008SPaul E. McKenney /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */ 39c350c008SPaul E. McKenney static ulong counter_wrap_check = (ULONG_MAX >> 2); 40c350c008SPaul E. McKenney module_param(counter_wrap_check, ulong, 0444); 41c350c008SPaul E. McKenney 42c69a00a1SPaul E. McKenney /* 43c69a00a1SPaul E. McKenney * Control conversion to SRCU_SIZE_BIG: 44a57ffb3cSPaul E. McKenney * 0: Don't convert at all. 45c69a00a1SPaul E. McKenney * 1: Convert at init_srcu_struct() time. 46c69a00a1SPaul E. McKenney * 2: Convert when rcutorture invokes srcu_torture_stats_print(). 47a57ffb3cSPaul E. McKenney * 3: Decide at boot time based on system shape (default). 489f2e91d9SPaul E. McKenney * 0x1x: Convert when excessive contention encountered. 49c69a00a1SPaul E. McKenney */ 509f2e91d9SPaul E. McKenney #define SRCU_SIZING_NONE 0 519f2e91d9SPaul E. McKenney #define SRCU_SIZING_INIT 1 529f2e91d9SPaul E. McKenney #define SRCU_SIZING_TORTURE 2 539f2e91d9SPaul E. McKenney #define SRCU_SIZING_AUTO 3 549f2e91d9SPaul E. McKenney #define SRCU_SIZING_CONTEND 0x10 559f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x) 569f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE)) 579f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT)) 589f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE)) 599f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND) 60a57ffb3cSPaul E. McKenney static int convert_to_big = SRCU_SIZING_AUTO; 61c69a00a1SPaul E. McKenney module_param(convert_to_big, int, 0444); 62c69a00a1SPaul E. McKenney 63a57ffb3cSPaul E. McKenney /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */ 64a57ffb3cSPaul E. McKenney static int big_cpu_lim __read_mostly = 128; 65a57ffb3cSPaul E. McKenney module_param(big_cpu_lim, int, 0444); 66a57ffb3cSPaul E. McKenney 679f2e91d9SPaul E. McKenney /* Contention events per jiffy to initiate transition to big. */ 689f2e91d9SPaul E. McKenney static int small_contention_lim __read_mostly = 100; 699f2e91d9SPaul E. McKenney module_param(small_contention_lim, int, 0444); 709f2e91d9SPaul E. McKenney 71e0fcba9aSPaul E. McKenney /* Early-boot callback-management, so early that no lock is required! */ 72e0fcba9aSPaul E. McKenney static LIST_HEAD(srcu_boot_list); 73e0fcba9aSPaul E. McKenney static bool __read_mostly srcu_init_done; 74e0fcba9aSPaul E. McKenney 75da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work); 76aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay); 770d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work); 78e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t); 79da915ad5SPaul E. McKenney 80d6331980SPaul E. McKenney /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */ 81d6331980SPaul E. McKenney #define spin_lock_rcu_node(p) \ 82d6331980SPaul E. McKenney do { \ 83d6331980SPaul E. McKenney spin_lock(&ACCESS_PRIVATE(p, lock)); \ 84d6331980SPaul E. McKenney smp_mb__after_unlock_lock(); \ 85d6331980SPaul E. McKenney } while (0) 86d6331980SPaul E. McKenney 87d6331980SPaul E. McKenney #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock)) 88d6331980SPaul E. McKenney 89d6331980SPaul E. McKenney #define spin_lock_irq_rcu_node(p) \ 90d6331980SPaul E. McKenney do { \ 91d6331980SPaul E. McKenney spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \ 92d6331980SPaul E. McKenney smp_mb__after_unlock_lock(); \ 93d6331980SPaul E. McKenney } while (0) 94d6331980SPaul E. McKenney 95d6331980SPaul E. McKenney #define spin_unlock_irq_rcu_node(p) \ 96d6331980SPaul E. McKenney spin_unlock_irq(&ACCESS_PRIVATE(p, lock)) 97d6331980SPaul E. McKenney 98d6331980SPaul E. McKenney #define spin_lock_irqsave_rcu_node(p, flags) \ 99d6331980SPaul E. McKenney do { \ 100d6331980SPaul E. McKenney spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \ 101d6331980SPaul E. McKenney smp_mb__after_unlock_lock(); \ 102d6331980SPaul E. McKenney } while (0) 103d6331980SPaul E. McKenney 1049f2e91d9SPaul E. McKenney #define spin_trylock_irqsave_rcu_node(p, flags) \ 1059f2e91d9SPaul E. McKenney ({ \ 1069f2e91d9SPaul E. McKenney bool ___locked = spin_trylock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \ 1079f2e91d9SPaul E. McKenney \ 1089f2e91d9SPaul E. McKenney if (___locked) \ 1099f2e91d9SPaul E. McKenney smp_mb__after_unlock_lock(); \ 1109f2e91d9SPaul E. McKenney ___locked; \ 1119f2e91d9SPaul E. McKenney }) 1129f2e91d9SPaul E. McKenney 113d6331980SPaul E. McKenney #define spin_unlock_irqrestore_rcu_node(p, flags) \ 114d6331980SPaul E. McKenney spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \ 115d6331980SPaul E. McKenney 116da915ad5SPaul E. McKenney /* 1172ec30311SPaul E. McKenney * Initialize SRCU per-CPU data. Note that statically allocated 118da915ad5SPaul E. McKenney * srcu_struct structures might already have srcu_read_lock() and 119da915ad5SPaul E. McKenney * srcu_read_unlock() running against them. So if the is_static parameter 120da915ad5SPaul E. McKenney * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[]. 121da915ad5SPaul E. McKenney */ 1222ec30311SPaul E. McKenney static void init_srcu_struct_data(struct srcu_struct *ssp) 1232ec30311SPaul E. McKenney { 1242ec30311SPaul E. McKenney int cpu; 1252ec30311SPaul E. McKenney struct srcu_data *sdp; 1262ec30311SPaul E. McKenney 1272ec30311SPaul E. McKenney /* 1282ec30311SPaul E. McKenney * Initialize the per-CPU srcu_data array, which feeds into the 1292ec30311SPaul E. McKenney * leaves of the srcu_node tree. 1302ec30311SPaul E. McKenney */ 1312ec30311SPaul E. McKenney WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) != 1322ec30311SPaul E. McKenney ARRAY_SIZE(sdp->srcu_unlock_count)); 1332ec30311SPaul E. McKenney for_each_possible_cpu(cpu) { 1342ec30311SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 1352ec30311SPaul E. McKenney spin_lock_init(&ACCESS_PRIVATE(sdp, lock)); 1362ec30311SPaul E. McKenney rcu_segcblist_init(&sdp->srcu_cblist); 1372ec30311SPaul E. McKenney sdp->srcu_cblist_invoking = false; 1382ec30311SPaul E. McKenney sdp->srcu_gp_seq_needed = ssp->srcu_gp_seq; 1392ec30311SPaul E. McKenney sdp->srcu_gp_seq_needed_exp = ssp->srcu_gp_seq; 1402ec30311SPaul E. McKenney sdp->mynode = NULL; 1412ec30311SPaul E. McKenney sdp->cpu = cpu; 1422ec30311SPaul E. McKenney INIT_WORK(&sdp->work, srcu_invoke_callbacks); 1432ec30311SPaul E. McKenney timer_setup(&sdp->delay_work, srcu_delay_timer, 0); 1442ec30311SPaul E. McKenney sdp->ssp = ssp; 1452ec30311SPaul E. McKenney } 1462ec30311SPaul E. McKenney } 1472ec30311SPaul E. McKenney 148cbdc98e9SPaul E. McKenney /* Invalid seq state, used during snp node initialization */ 149cbdc98e9SPaul E. McKenney #define SRCU_SNP_INIT_SEQ 0x2 150cbdc98e9SPaul E. McKenney 151cbdc98e9SPaul E. McKenney /* 152cbdc98e9SPaul E. McKenney * Check whether sequence number corresponding to snp node, 153cbdc98e9SPaul E. McKenney * is invalid. 154cbdc98e9SPaul E. McKenney */ 155cbdc98e9SPaul E. McKenney static inline bool srcu_invl_snp_seq(unsigned long s) 156cbdc98e9SPaul E. McKenney { 157cbdc98e9SPaul E. McKenney return rcu_seq_state(s) == SRCU_SNP_INIT_SEQ; 158cbdc98e9SPaul E. McKenney } 159cbdc98e9SPaul E. McKenney 1602ec30311SPaul E. McKenney /* 1612ec30311SPaul E. McKenney * Allocated and initialize SRCU combining tree. Returns @true if 1622ec30311SPaul E. McKenney * allocation succeeded and @false otherwise. 1632ec30311SPaul E. McKenney */ 164c69a00a1SPaul E. McKenney static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags) 165dad81a20SPaul E. McKenney { 166da915ad5SPaul E. McKenney int cpu; 167da915ad5SPaul E. McKenney int i; 168da915ad5SPaul E. McKenney int level = 0; 169da915ad5SPaul E. McKenney int levelspread[RCU_NUM_LVLS]; 170da915ad5SPaul E. McKenney struct srcu_data *sdp; 171da915ad5SPaul E. McKenney struct srcu_node *snp; 172da915ad5SPaul E. McKenney struct srcu_node *snp_first; 173da915ad5SPaul E. McKenney 174b5befe84SFrederic Weisbecker /* Initialize geometry if it has not already been initialized. */ 175b5befe84SFrederic Weisbecker rcu_init_geometry(); 176c69a00a1SPaul E. McKenney ssp->node = kcalloc(rcu_num_nodes, sizeof(*ssp->node), gfp_flags); 1772ec30311SPaul E. McKenney if (!ssp->node) 1782ec30311SPaul E. McKenney return false; 179b5befe84SFrederic Weisbecker 180da915ad5SPaul E. McKenney /* Work out the overall tree geometry. */ 181aacb5d91SPaul E. McKenney ssp->level[0] = &ssp->node[0]; 182da915ad5SPaul E. McKenney for (i = 1; i < rcu_num_lvls; i++) 183aacb5d91SPaul E. McKenney ssp->level[i] = ssp->level[i - 1] + num_rcu_lvl[i - 1]; 184da915ad5SPaul E. McKenney rcu_init_levelspread(levelspread, num_rcu_lvl); 185da915ad5SPaul E. McKenney 186da915ad5SPaul E. McKenney /* Each pass through this loop initializes one srcu_node structure. */ 187aacb5d91SPaul E. McKenney srcu_for_each_node_breadth_first(ssp, snp) { 188d6331980SPaul E. McKenney spin_lock_init(&ACCESS_PRIVATE(snp, lock)); 189c7e88067SPaul E. McKenney WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) != 190c7e88067SPaul E. McKenney ARRAY_SIZE(snp->srcu_data_have_cbs)); 191c7e88067SPaul E. McKenney for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) { 192cbdc98e9SPaul E. McKenney snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ; 193c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[i] = 0; 194c7e88067SPaul E. McKenney } 195cbdc98e9SPaul E. McKenney snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ; 196da915ad5SPaul E. McKenney snp->grplo = -1; 197da915ad5SPaul E. McKenney snp->grphi = -1; 198aacb5d91SPaul E. McKenney if (snp == &ssp->node[0]) { 199da915ad5SPaul E. McKenney /* Root node, special case. */ 200da915ad5SPaul E. McKenney snp->srcu_parent = NULL; 201da915ad5SPaul E. McKenney continue; 202da915ad5SPaul E. McKenney } 203da915ad5SPaul E. McKenney 204da915ad5SPaul E. McKenney /* Non-root node. */ 205aacb5d91SPaul E. McKenney if (snp == ssp->level[level + 1]) 206da915ad5SPaul E. McKenney level++; 207aacb5d91SPaul E. McKenney snp->srcu_parent = ssp->level[level - 1] + 208aacb5d91SPaul E. McKenney (snp - ssp->level[level]) / 209da915ad5SPaul E. McKenney levelspread[level - 1]; 210da915ad5SPaul E. McKenney } 211da915ad5SPaul E. McKenney 212da915ad5SPaul E. McKenney /* 213da915ad5SPaul E. McKenney * Initialize the per-CPU srcu_data array, which feeds into the 214da915ad5SPaul E. McKenney * leaves of the srcu_node tree. 215da915ad5SPaul E. McKenney */ 216da915ad5SPaul E. McKenney level = rcu_num_lvls - 1; 217aacb5d91SPaul E. McKenney snp_first = ssp->level[level]; 218da915ad5SPaul E. McKenney for_each_possible_cpu(cpu) { 219aacb5d91SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 220da915ad5SPaul E. McKenney sdp->mynode = &snp_first[cpu / levelspread[level]]; 221da915ad5SPaul E. McKenney for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) { 222da915ad5SPaul E. McKenney if (snp->grplo < 0) 223da915ad5SPaul E. McKenney snp->grplo = cpu; 224da915ad5SPaul E. McKenney snp->grphi = cpu; 225da915ad5SPaul E. McKenney } 226c7e88067SPaul E. McKenney sdp->grpmask = 1 << (cpu - sdp->mynode->grplo); 227da915ad5SPaul E. McKenney } 228994f7068SPaul E. McKenney smp_store_release(&ssp->srcu_size_state, SRCU_SIZE_WAIT_BARRIER); 2292ec30311SPaul E. McKenney return true; 230da915ad5SPaul E. McKenney } 231da915ad5SPaul E. McKenney 232da915ad5SPaul E. McKenney /* 233da915ad5SPaul E. McKenney * Initialize non-compile-time initialized fields, including the 234994f7068SPaul E. McKenney * associated srcu_node and srcu_data structures. The is_static parameter 235994f7068SPaul E. McKenney * tells us that ->sda has already been wired up to srcu_data. 236da915ad5SPaul E. McKenney */ 237aacb5d91SPaul E. McKenney static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static) 238da915ad5SPaul E. McKenney { 239994f7068SPaul E. McKenney ssp->srcu_size_state = SRCU_SIZE_SMALL; 2402ec30311SPaul E. McKenney ssp->node = NULL; 241aacb5d91SPaul E. McKenney mutex_init(&ssp->srcu_cb_mutex); 242aacb5d91SPaul E. McKenney mutex_init(&ssp->srcu_gp_mutex); 243aacb5d91SPaul E. McKenney ssp->srcu_idx = 0; 244aacb5d91SPaul E. McKenney ssp->srcu_gp_seq = 0; 245aacb5d91SPaul E. McKenney ssp->srcu_barrier_seq = 0; 246aacb5d91SPaul E. McKenney mutex_init(&ssp->srcu_barrier_mutex); 247aacb5d91SPaul E. McKenney atomic_set(&ssp->srcu_barrier_cpu_cnt, 0); 248aacb5d91SPaul E. McKenney INIT_DELAYED_WORK(&ssp->work, process_srcu); 24946470cf8SPaul E. McKenney ssp->sda_is_static = is_static; 250da915ad5SPaul E. McKenney if (!is_static) 251aacb5d91SPaul E. McKenney ssp->sda = alloc_percpu(struct srcu_data); 25250edb988SPaul E. McKenney if (!ssp->sda) 25350edb988SPaul E. McKenney return -ENOMEM; 2542ec30311SPaul E. McKenney init_srcu_struct_data(ssp); 255c69a00a1SPaul E. McKenney ssp->srcu_gp_seq_needed_exp = 0; 256c69a00a1SPaul E. McKenney ssp->srcu_last_gp_end = ktime_get_mono_fast_ns(); 2579f2e91d9SPaul E. McKenney if (READ_ONCE(ssp->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) { 258c69a00a1SPaul E. McKenney if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC)) { 25946470cf8SPaul E. McKenney if (!ssp->sda_is_static) { 2602ec30311SPaul E. McKenney free_percpu(ssp->sda); 2612ec30311SPaul E. McKenney ssp->sda = NULL; 2622ec30311SPaul E. McKenney return -ENOMEM; 2632ec30311SPaul E. McKenney } 2642ec30311SPaul E. McKenney } else { 265c69a00a1SPaul E. McKenney WRITE_ONCE(ssp->srcu_size_state, SRCU_SIZE_BIG); 2662ec30311SPaul E. McKenney } 267c69a00a1SPaul E. McKenney } 268aacb5d91SPaul E. McKenney smp_store_release(&ssp->srcu_gp_seq_needed, 0); /* Init done. */ 26950edb988SPaul E. McKenney return 0; 270dad81a20SPaul E. McKenney } 271dad81a20SPaul E. McKenney 272dad81a20SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 273dad81a20SPaul E. McKenney 274aacb5d91SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *ssp, const char *name, 275dad81a20SPaul E. McKenney struct lock_class_key *key) 276dad81a20SPaul E. McKenney { 277dad81a20SPaul E. McKenney /* Don't re-initialize a lock while it is held. */ 278aacb5d91SPaul E. McKenney debug_check_no_locks_freed((void *)ssp, sizeof(*ssp)); 279aacb5d91SPaul E. McKenney lockdep_init_map(&ssp->dep_map, name, key, 0); 280aacb5d91SPaul E. McKenney spin_lock_init(&ACCESS_PRIVATE(ssp, lock)); 281aacb5d91SPaul E. McKenney return init_srcu_struct_fields(ssp, false); 282dad81a20SPaul E. McKenney } 283dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__init_srcu_struct); 284dad81a20SPaul E. McKenney 285dad81a20SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 286dad81a20SPaul E. McKenney 287dad81a20SPaul E. McKenney /** 288dad81a20SPaul E. McKenney * init_srcu_struct - initialize a sleep-RCU structure 289aacb5d91SPaul E. McKenney * @ssp: structure to initialize. 290dad81a20SPaul E. McKenney * 291dad81a20SPaul E. McKenney * Must invoke this on a given srcu_struct before passing that srcu_struct 292dad81a20SPaul E. McKenney * to any other function. Each srcu_struct represents a separate domain 293dad81a20SPaul E. McKenney * of SRCU protection. 294dad81a20SPaul E. McKenney */ 295aacb5d91SPaul E. McKenney int init_srcu_struct(struct srcu_struct *ssp) 296dad81a20SPaul E. McKenney { 297aacb5d91SPaul E. McKenney spin_lock_init(&ACCESS_PRIVATE(ssp, lock)); 298aacb5d91SPaul E. McKenney return init_srcu_struct_fields(ssp, false); 299dad81a20SPaul E. McKenney } 300dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(init_srcu_struct); 301dad81a20SPaul E. McKenney 302dad81a20SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 303dad81a20SPaul E. McKenney 304dad81a20SPaul E. McKenney /* 3059f2e91d9SPaul E. McKenney * Initiate a transition to SRCU_SIZE_BIG with lock held. 3069f2e91d9SPaul E. McKenney */ 3079f2e91d9SPaul E. McKenney static void __srcu_transition_to_big(struct srcu_struct *ssp) 3089f2e91d9SPaul E. McKenney { 3099f2e91d9SPaul E. McKenney lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock)); 3109f2e91d9SPaul E. McKenney smp_store_release(&ssp->srcu_size_state, SRCU_SIZE_ALLOC); 3119f2e91d9SPaul E. McKenney } 3129f2e91d9SPaul E. McKenney 3139f2e91d9SPaul E. McKenney /* 31499659f64SPaul E. McKenney * Initiate an idempotent transition to SRCU_SIZE_BIG. 31599659f64SPaul E. McKenney */ 31699659f64SPaul E. McKenney static void srcu_transition_to_big(struct srcu_struct *ssp) 31799659f64SPaul E. McKenney { 31899659f64SPaul E. McKenney unsigned long flags; 31999659f64SPaul E. McKenney 32099659f64SPaul E. McKenney /* Double-checked locking on ->srcu_size-state. */ 32199659f64SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) != SRCU_SIZE_SMALL) 32299659f64SPaul E. McKenney return; 32399659f64SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, flags); 32499659f64SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) != SRCU_SIZE_SMALL) { 32599659f64SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 32699659f64SPaul E. McKenney return; 32799659f64SPaul E. McKenney } 3289f2e91d9SPaul E. McKenney __srcu_transition_to_big(ssp); 32999659f64SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 33099659f64SPaul E. McKenney } 33199659f64SPaul E. McKenney 33299659f64SPaul E. McKenney /* 333*c2445d38SPaul E. McKenney * Check to see if the just-encountered contention event justifies 334*c2445d38SPaul E. McKenney * a transition to SRCU_SIZE_BIG. 3359f2e91d9SPaul E. McKenney */ 336*c2445d38SPaul E. McKenney static void spin_lock_irqsave_check_contention(struct srcu_struct *ssp) 3379f2e91d9SPaul E. McKenney { 3389f2e91d9SPaul E. McKenney unsigned long j; 3399f2e91d9SPaul E. McKenney 3409f2e91d9SPaul E. McKenney if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_size_state) 3419f2e91d9SPaul E. McKenney return; 3429f2e91d9SPaul E. McKenney j = jiffies; 3439f2e91d9SPaul E. McKenney if (ssp->srcu_size_jiffies != j) { 3449f2e91d9SPaul E. McKenney ssp->srcu_size_jiffies = j; 3459f2e91d9SPaul E. McKenney ssp->srcu_n_lock_retries = 0; 3469f2e91d9SPaul E. McKenney } 3479f2e91d9SPaul E. McKenney if (++ssp->srcu_n_lock_retries <= small_contention_lim) 3489f2e91d9SPaul E. McKenney return; 3499f2e91d9SPaul E. McKenney __srcu_transition_to_big(ssp); 3509f2e91d9SPaul E. McKenney } 3519f2e91d9SPaul E. McKenney 3529f2e91d9SPaul E. McKenney /* 353*c2445d38SPaul E. McKenney * Acquire the specified srcu_data structure's ->lock, but check for 354*c2445d38SPaul E. McKenney * excessive contention, which results in initiation of a transition 355*c2445d38SPaul E. McKenney * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 356*c2445d38SPaul E. McKenney * parameter permits this. 357*c2445d38SPaul E. McKenney */ 358*c2445d38SPaul E. McKenney static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags) 359*c2445d38SPaul E. McKenney { 360*c2445d38SPaul E. McKenney struct srcu_struct *ssp = sdp->ssp; 361*c2445d38SPaul E. McKenney 362*c2445d38SPaul E. McKenney if (spin_trylock_irqsave_rcu_node(sdp, *flags)) 363*c2445d38SPaul E. McKenney return; 364*c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, *flags); 365*c2445d38SPaul E. McKenney spin_lock_irqsave_check_contention(ssp); 366*c2445d38SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, *flags); 367*c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(sdp, *flags); 368*c2445d38SPaul E. McKenney } 369*c2445d38SPaul E. McKenney 370*c2445d38SPaul E. McKenney /* 371*c2445d38SPaul E. McKenney * Acquire the specified srcu_struct structure's ->lock, but check for 372*c2445d38SPaul E. McKenney * excessive contention, which results in initiation of a transition 373*c2445d38SPaul E. McKenney * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 374*c2445d38SPaul E. McKenney * parameter permits this. 375*c2445d38SPaul E. McKenney */ 376*c2445d38SPaul E. McKenney static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags) 377*c2445d38SPaul E. McKenney { 378*c2445d38SPaul E. McKenney if (spin_trylock_irqsave_rcu_node(ssp, *flags)) 379*c2445d38SPaul E. McKenney return; 380*c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, *flags); 381*c2445d38SPaul E. McKenney spin_lock_irqsave_check_contention(ssp); 382*c2445d38SPaul E. McKenney } 383*c2445d38SPaul E. McKenney 384*c2445d38SPaul E. McKenney /* 385da915ad5SPaul E. McKenney * First-use initialization of statically allocated srcu_struct 386da915ad5SPaul E. McKenney * structure. Wiring up the combining tree is more than can be 387da915ad5SPaul E. McKenney * done with compile-time initialization, so this check is added 388aacb5d91SPaul E. McKenney * to each update-side SRCU primitive. Use ssp->lock, which -is- 389da915ad5SPaul E. McKenney * compile-time initialized, to resolve races involving multiple 390da915ad5SPaul E. McKenney * CPUs trying to garner first-use privileges. 391da915ad5SPaul E. McKenney */ 392aacb5d91SPaul E. McKenney static void check_init_srcu_struct(struct srcu_struct *ssp) 393da915ad5SPaul E. McKenney { 394da915ad5SPaul E. McKenney unsigned long flags; 395da915ad5SPaul E. McKenney 396da915ad5SPaul E. McKenney /* The smp_load_acquire() pairs with the smp_store_release(). */ 397aacb5d91SPaul E. McKenney if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq_needed))) /*^^^*/ 398da915ad5SPaul E. McKenney return; /* Already initialized. */ 399aacb5d91SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, flags); 400aacb5d91SPaul E. McKenney if (!rcu_seq_state(ssp->srcu_gp_seq_needed)) { 401aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 402da915ad5SPaul E. McKenney return; 403da915ad5SPaul E. McKenney } 404aacb5d91SPaul E. McKenney init_srcu_struct_fields(ssp, true); 405aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 406da915ad5SPaul E. McKenney } 407da915ad5SPaul E. McKenney 408da915ad5SPaul E. McKenney /* 409da915ad5SPaul E. McKenney * Returns approximate total of the readers' ->srcu_lock_count[] values 410da915ad5SPaul E. McKenney * for the rank of per-CPU counters specified by idx. 411dad81a20SPaul E. McKenney */ 412aacb5d91SPaul E. McKenney static unsigned long srcu_readers_lock_idx(struct srcu_struct *ssp, int idx) 413dad81a20SPaul E. McKenney { 414dad81a20SPaul E. McKenney int cpu; 415dad81a20SPaul E. McKenney unsigned long sum = 0; 416dad81a20SPaul E. McKenney 417dad81a20SPaul E. McKenney for_each_possible_cpu(cpu) { 418aacb5d91SPaul E. McKenney struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu); 419dad81a20SPaul E. McKenney 420da915ad5SPaul E. McKenney sum += READ_ONCE(cpuc->srcu_lock_count[idx]); 421dad81a20SPaul E. McKenney } 422dad81a20SPaul E. McKenney return sum; 423dad81a20SPaul E. McKenney } 424dad81a20SPaul E. McKenney 425dad81a20SPaul E. McKenney /* 426da915ad5SPaul E. McKenney * Returns approximate total of the readers' ->srcu_unlock_count[] values 427da915ad5SPaul E. McKenney * for the rank of per-CPU counters specified by idx. 428dad81a20SPaul E. McKenney */ 429aacb5d91SPaul E. McKenney static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx) 430dad81a20SPaul E. McKenney { 431dad81a20SPaul E. McKenney int cpu; 432dad81a20SPaul E. McKenney unsigned long sum = 0; 433dad81a20SPaul E. McKenney 434dad81a20SPaul E. McKenney for_each_possible_cpu(cpu) { 435aacb5d91SPaul E. McKenney struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu); 436dad81a20SPaul E. McKenney 437da915ad5SPaul E. McKenney sum += READ_ONCE(cpuc->srcu_unlock_count[idx]); 438dad81a20SPaul E. McKenney } 439dad81a20SPaul E. McKenney return sum; 440dad81a20SPaul E. McKenney } 441dad81a20SPaul E. McKenney 442dad81a20SPaul E. McKenney /* 443dad81a20SPaul E. McKenney * Return true if the number of pre-existing readers is determined to 444dad81a20SPaul E. McKenney * be zero. 445dad81a20SPaul E. McKenney */ 446aacb5d91SPaul E. McKenney static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx) 447dad81a20SPaul E. McKenney { 448dad81a20SPaul E. McKenney unsigned long unlocks; 449dad81a20SPaul E. McKenney 450aacb5d91SPaul E. McKenney unlocks = srcu_readers_unlock_idx(ssp, idx); 451dad81a20SPaul E. McKenney 452dad81a20SPaul E. McKenney /* 453dad81a20SPaul E. McKenney * Make sure that a lock is always counted if the corresponding 454dad81a20SPaul E. McKenney * unlock is counted. Needs to be a smp_mb() as the read side may 455dad81a20SPaul E. McKenney * contain a read from a variable that is written to before the 456dad81a20SPaul E. McKenney * synchronize_srcu() in the write side. In this case smp_mb()s 457dad81a20SPaul E. McKenney * A and B act like the store buffering pattern. 458dad81a20SPaul E. McKenney * 459dad81a20SPaul E. McKenney * This smp_mb() also pairs with smp_mb() C to prevent accesses 460dad81a20SPaul E. McKenney * after the synchronize_srcu() from being executed before the 461dad81a20SPaul E. McKenney * grace period ends. 462dad81a20SPaul E. McKenney */ 463dad81a20SPaul E. McKenney smp_mb(); /* A */ 464dad81a20SPaul E. McKenney 465dad81a20SPaul E. McKenney /* 466dad81a20SPaul E. McKenney * If the locks are the same as the unlocks, then there must have 467dad81a20SPaul E. McKenney * been no readers on this index at some time in between. This does 468dad81a20SPaul E. McKenney * not mean that there are no more readers, as one could have read 469dad81a20SPaul E. McKenney * the current index but not have incremented the lock counter yet. 470dad81a20SPaul E. McKenney * 471881ec9d2SPaul E. McKenney * So suppose that the updater is preempted here for so long 472881ec9d2SPaul E. McKenney * that more than ULONG_MAX non-nested readers come and go in 473881ec9d2SPaul E. McKenney * the meantime. It turns out that this cannot result in overflow 474881ec9d2SPaul E. McKenney * because if a reader modifies its unlock count after we read it 475881ec9d2SPaul E. McKenney * above, then that reader's next load of ->srcu_idx is guaranteed 476881ec9d2SPaul E. McKenney * to get the new value, which will cause it to operate on the 477881ec9d2SPaul E. McKenney * other bank of counters, where it cannot contribute to the 478881ec9d2SPaul E. McKenney * overflow of these counters. This means that there is a maximum 479881ec9d2SPaul E. McKenney * of 2*NR_CPUS increments, which cannot overflow given current 480881ec9d2SPaul E. McKenney * systems, especially not on 64-bit systems. 481881ec9d2SPaul E. McKenney * 482881ec9d2SPaul E. McKenney * OK, how about nesting? This does impose a limit on nesting 483881ec9d2SPaul E. McKenney * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient, 484881ec9d2SPaul E. McKenney * especially on 64-bit systems. 485dad81a20SPaul E. McKenney */ 486aacb5d91SPaul E. McKenney return srcu_readers_lock_idx(ssp, idx) == unlocks; 487dad81a20SPaul E. McKenney } 488dad81a20SPaul E. McKenney 489dad81a20SPaul E. McKenney /** 490dad81a20SPaul E. McKenney * srcu_readers_active - returns true if there are readers. and false 491dad81a20SPaul E. McKenney * otherwise 492aacb5d91SPaul E. McKenney * @ssp: which srcu_struct to count active readers (holding srcu_read_lock). 493dad81a20SPaul E. McKenney * 494dad81a20SPaul E. McKenney * Note that this is not an atomic primitive, and can therefore suffer 495dad81a20SPaul E. McKenney * severe errors when invoked on an active srcu_struct. That said, it 496dad81a20SPaul E. McKenney * can be useful as an error check at cleanup time. 497dad81a20SPaul E. McKenney */ 498aacb5d91SPaul E. McKenney static bool srcu_readers_active(struct srcu_struct *ssp) 499dad81a20SPaul E. McKenney { 500dad81a20SPaul E. McKenney int cpu; 501dad81a20SPaul E. McKenney unsigned long sum = 0; 502dad81a20SPaul E. McKenney 503dad81a20SPaul E. McKenney for_each_possible_cpu(cpu) { 504aacb5d91SPaul E. McKenney struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu); 505dad81a20SPaul E. McKenney 506da915ad5SPaul E. McKenney sum += READ_ONCE(cpuc->srcu_lock_count[0]); 507da915ad5SPaul E. McKenney sum += READ_ONCE(cpuc->srcu_lock_count[1]); 508da915ad5SPaul E. McKenney sum -= READ_ONCE(cpuc->srcu_unlock_count[0]); 509da915ad5SPaul E. McKenney sum -= READ_ONCE(cpuc->srcu_unlock_count[1]); 510dad81a20SPaul E. McKenney } 511dad81a20SPaul E. McKenney return sum; 512dad81a20SPaul E. McKenney } 513dad81a20SPaul E. McKenney 514dad81a20SPaul E. McKenney #define SRCU_INTERVAL 1 515dad81a20SPaul E. McKenney 5161e9a038bSPaul E. McKenney /* 5171e9a038bSPaul E. McKenney * Return grace-period delay, zero if there are expedited grace 5181e9a038bSPaul E. McKenney * periods pending, SRCU_INTERVAL otherwise. 5191e9a038bSPaul E. McKenney */ 520aacb5d91SPaul E. McKenney static unsigned long srcu_get_delay(struct srcu_struct *ssp) 5211e9a038bSPaul E. McKenney { 522cbdc98e9SPaul E. McKenney if (ULONG_CMP_LT(READ_ONCE(ssp->srcu_gp_seq), READ_ONCE(ssp->srcu_gp_seq_needed_exp))) 5231e9a038bSPaul E. McKenney return 0; 5241e9a038bSPaul E. McKenney return SRCU_INTERVAL; 5251e9a038bSPaul E. McKenney } 5261e9a038bSPaul E. McKenney 527f5ad3991SPaul E. McKenney /** 528f5ad3991SPaul E. McKenney * cleanup_srcu_struct - deconstruct a sleep-RCU structure 529f5ad3991SPaul E. McKenney * @ssp: structure to clean up. 530f5ad3991SPaul E. McKenney * 531f5ad3991SPaul E. McKenney * Must invoke this after you are finished using a given srcu_struct that 532f5ad3991SPaul E. McKenney * was initialized via init_srcu_struct(), else you leak memory. 533f5ad3991SPaul E. McKenney */ 534f5ad3991SPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *ssp) 535dad81a20SPaul E. McKenney { 536da915ad5SPaul E. McKenney int cpu; 537da915ad5SPaul E. McKenney 538aacb5d91SPaul E. McKenney if (WARN_ON(!srcu_get_delay(ssp))) 539f7194ac3SPaul E. McKenney return; /* Just leak it! */ 540aacb5d91SPaul E. McKenney if (WARN_ON(srcu_readers_active(ssp))) 541f7194ac3SPaul E. McKenney return; /* Just leak it! */ 542aacb5d91SPaul E. McKenney flush_delayed_work(&ssp->work); 543e81baf4cSSebastian Andrzej Siewior for_each_possible_cpu(cpu) { 544e81baf4cSSebastian Andrzej Siewior struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 545e81baf4cSSebastian Andrzej Siewior 546e81baf4cSSebastian Andrzej Siewior del_timer_sync(&sdp->delay_work); 547e81baf4cSSebastian Andrzej Siewior flush_work(&sdp->work); 5485cdfd174SPaul E. McKenney if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist))) 5495cdfd174SPaul E. McKenney return; /* Forgot srcu_barrier(), so just leak it! */ 550f7194ac3SPaul E. McKenney } 551aacb5d91SPaul E. McKenney if (WARN_ON(rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) != SRCU_STATE_IDLE) || 5528ed00760SPaul E. McKenney WARN_ON(rcu_seq_current(&ssp->srcu_gp_seq) != ssp->srcu_gp_seq_needed) || 553aacb5d91SPaul E. McKenney WARN_ON(srcu_readers_active(ssp))) { 5548ed00760SPaul E. McKenney pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n", 5558ed00760SPaul E. McKenney __func__, ssp, rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)), 5568ed00760SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq), ssp->srcu_gp_seq_needed); 557dad81a20SPaul E. McKenney return; /* Caller forgot to stop doing call_srcu()? */ 558dad81a20SPaul E. McKenney } 55946470cf8SPaul E. McKenney if (!ssp->sda_is_static) { 560aacb5d91SPaul E. McKenney free_percpu(ssp->sda); 561aacb5d91SPaul E. McKenney ssp->sda = NULL; 56246470cf8SPaul E. McKenney } 5632ec30311SPaul E. McKenney kfree(ssp->node); 5642ec30311SPaul E. McKenney ssp->node = NULL; 565994f7068SPaul E. McKenney ssp->srcu_size_state = SRCU_SIZE_SMALL; 566dad81a20SPaul E. McKenney } 567f5ad3991SPaul E. McKenney EXPORT_SYMBOL_GPL(cleanup_srcu_struct); 568dad81a20SPaul E. McKenney 569dad81a20SPaul E. McKenney /* 570dad81a20SPaul E. McKenney * Counts the new reader in the appropriate per-CPU element of the 571cdf7abc4SPaolo Bonzini * srcu_struct. 572dad81a20SPaul E. McKenney * Returns an index that must be passed to the matching srcu_read_unlock(). 573dad81a20SPaul E. McKenney */ 574aacb5d91SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *ssp) 575dad81a20SPaul E. McKenney { 576dad81a20SPaul E. McKenney int idx; 577dad81a20SPaul E. McKenney 578aacb5d91SPaul E. McKenney idx = READ_ONCE(ssp->srcu_idx) & 0x1; 579aacb5d91SPaul E. McKenney this_cpu_inc(ssp->sda->srcu_lock_count[idx]); 580dad81a20SPaul E. McKenney smp_mb(); /* B */ /* Avoid leaking the critical section. */ 581dad81a20SPaul E. McKenney return idx; 582dad81a20SPaul E. McKenney } 583dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock); 584dad81a20SPaul E. McKenney 585dad81a20SPaul E. McKenney /* 586dad81a20SPaul E. McKenney * Removes the count for the old reader from the appropriate per-CPU 587dad81a20SPaul E. McKenney * element of the srcu_struct. Note that this may well be a different 588dad81a20SPaul E. McKenney * CPU than that which was incremented by the corresponding srcu_read_lock(). 589dad81a20SPaul E. McKenney */ 590aacb5d91SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *ssp, int idx) 591dad81a20SPaul E. McKenney { 592dad81a20SPaul E. McKenney smp_mb(); /* C */ /* Avoid leaking the critical section. */ 593aacb5d91SPaul E. McKenney this_cpu_inc(ssp->sda->srcu_unlock_count[idx]); 594dad81a20SPaul E. McKenney } 595dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock); 596dad81a20SPaul E. McKenney 597dad81a20SPaul E. McKenney /* 598dad81a20SPaul E. McKenney * We use an adaptive strategy for synchronize_srcu() and especially for 599dad81a20SPaul E. McKenney * synchronize_srcu_expedited(). We spin for a fixed time period 600dad81a20SPaul E. McKenney * (defined below) to allow SRCU readers to exit their read-side critical 601dad81a20SPaul E. McKenney * sections. If there are still some readers after a few microseconds, 602dad81a20SPaul E. McKenney * we repeatedly block for 1-millisecond time periods. 603dad81a20SPaul E. McKenney */ 604dad81a20SPaul E. McKenney #define SRCU_RETRY_CHECK_DELAY 5 605dad81a20SPaul E. McKenney 606dad81a20SPaul E. McKenney /* 607dad81a20SPaul E. McKenney * Start an SRCU grace period. 608dad81a20SPaul E. McKenney */ 609aacb5d91SPaul E. McKenney static void srcu_gp_start(struct srcu_struct *ssp) 610dad81a20SPaul E. McKenney { 611aacb5d91SPaul E. McKenney struct srcu_data *sdp = this_cpu_ptr(ssp->sda); 612dad81a20SPaul E. McKenney int state; 613dad81a20SPaul E. McKenney 614994f7068SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 615994f7068SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, 0); 616994f7068SPaul E. McKenney else 617994f7068SPaul E. McKenney sdp = this_cpu_ptr(ssp->sda); 618aacb5d91SPaul E. McKenney lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock)); 619aacb5d91SPaul E. McKenney WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)); 620eb4c2382SDennis Krein spin_lock_rcu_node(sdp); /* Interrupts already disabled. */ 621da915ad5SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 622aacb5d91SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 623da915ad5SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, 624aacb5d91SPaul E. McKenney rcu_seq_snap(&ssp->srcu_gp_seq)); 625eb4c2382SDennis Krein spin_unlock_rcu_node(sdp); /* Interrupts remain disabled. */ 6262da4b2a7SPaul E. McKenney smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */ 627aacb5d91SPaul E. McKenney rcu_seq_start(&ssp->srcu_gp_seq); 62871042606SPaul E. McKenney state = rcu_seq_state(ssp->srcu_gp_seq); 629dad81a20SPaul E. McKenney WARN_ON_ONCE(state != SRCU_STATE_SCAN1); 630dad81a20SPaul E. McKenney } 631dad81a20SPaul E. McKenney 632da915ad5SPaul E. McKenney 633e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t) 634da915ad5SPaul E. McKenney { 635e81baf4cSSebastian Andrzej Siewior struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work); 636e81baf4cSSebastian Andrzej Siewior 637e81baf4cSSebastian Andrzej Siewior queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 638da915ad5SPaul E. McKenney } 639da915ad5SPaul E. McKenney 640e81baf4cSSebastian Andrzej Siewior static void srcu_queue_delayed_work_on(struct srcu_data *sdp, 641da915ad5SPaul E. McKenney unsigned long delay) 642da915ad5SPaul E. McKenney { 643e81baf4cSSebastian Andrzej Siewior if (!delay) { 644e81baf4cSSebastian Andrzej Siewior queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 645e81baf4cSSebastian Andrzej Siewior return; 646e81baf4cSSebastian Andrzej Siewior } 647da915ad5SPaul E. McKenney 648e81baf4cSSebastian Andrzej Siewior timer_reduce(&sdp->delay_work, jiffies + delay); 649da915ad5SPaul E. McKenney } 650da915ad5SPaul E. McKenney 651da915ad5SPaul E. McKenney /* 652da915ad5SPaul E. McKenney * Schedule callback invocation for the specified srcu_data structure, 653da915ad5SPaul E. McKenney * if possible, on the corresponding CPU. 654da915ad5SPaul E. McKenney */ 655da915ad5SPaul E. McKenney static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay) 656da915ad5SPaul E. McKenney { 657e81baf4cSSebastian Andrzej Siewior srcu_queue_delayed_work_on(sdp, delay); 658da915ad5SPaul E. McKenney } 659da915ad5SPaul E. McKenney 660da915ad5SPaul E. McKenney /* 661da915ad5SPaul E. McKenney * Schedule callback invocation for all srcu_data structures associated 662c7e88067SPaul E. McKenney * with the specified srcu_node structure that have callbacks for the 663c7e88067SPaul E. McKenney * just-completed grace period, the one corresponding to idx. If possible, 664c7e88067SPaul E. McKenney * schedule this invocation on the corresponding CPUs. 665da915ad5SPaul E. McKenney */ 666aacb5d91SPaul E. McKenney static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp, 6671e9a038bSPaul E. McKenney unsigned long mask, unsigned long delay) 668da915ad5SPaul E. McKenney { 669da915ad5SPaul E. McKenney int cpu; 670da915ad5SPaul E. McKenney 671c7e88067SPaul E. McKenney for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) { 672c7e88067SPaul E. McKenney if (!(mask & (1 << (cpu - snp->grplo)))) 673c7e88067SPaul E. McKenney continue; 674aacb5d91SPaul E. McKenney srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay); 675da915ad5SPaul E. McKenney } 676c7e88067SPaul E. McKenney } 677da915ad5SPaul E. McKenney 678da915ad5SPaul E. McKenney /* 679da915ad5SPaul E. McKenney * Note the end of an SRCU grace period. Initiates callback invocation 680da915ad5SPaul E. McKenney * and starts a new grace period if needed. 681da915ad5SPaul E. McKenney * 682da915ad5SPaul E. McKenney * The ->srcu_cb_mutex acquisition does not protect any data, but 683da915ad5SPaul E. McKenney * instead prevents more than one grace period from starting while we 684da915ad5SPaul E. McKenney * are initiating callback invocation. This allows the ->srcu_have_cbs[] 685da915ad5SPaul E. McKenney * array to have a finite number of elements. 686da915ad5SPaul E. McKenney */ 687aacb5d91SPaul E. McKenney static void srcu_gp_end(struct srcu_struct *ssp) 688da915ad5SPaul E. McKenney { 6891e9a038bSPaul E. McKenney unsigned long cbdelay; 690da915ad5SPaul E. McKenney bool cbs; 6918ddbd883SIldar Ismagilov bool last_lvl; 692c350c008SPaul E. McKenney int cpu; 693c350c008SPaul E. McKenney unsigned long flags; 694da915ad5SPaul E. McKenney unsigned long gpseq; 695da915ad5SPaul E. McKenney int idx; 696c7e88067SPaul E. McKenney unsigned long mask; 697c350c008SPaul E. McKenney struct srcu_data *sdp; 698cbdc98e9SPaul E. McKenney unsigned long sgsne; 699da915ad5SPaul E. McKenney struct srcu_node *snp; 700e2f63836SPaul E. McKenney int ss_state; 701da915ad5SPaul E. McKenney 702da915ad5SPaul E. McKenney /* Prevent more than one additional grace period. */ 703aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_cb_mutex); 704da915ad5SPaul E. McKenney 705da915ad5SPaul E. McKenney /* End the current grace period. */ 706aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 707aacb5d91SPaul E. McKenney idx = rcu_seq_state(ssp->srcu_gp_seq); 708da915ad5SPaul E. McKenney WARN_ON_ONCE(idx != SRCU_STATE_SCAN2); 709aacb5d91SPaul E. McKenney cbdelay = srcu_get_delay(ssp); 710844a378dSPaul E. McKenney WRITE_ONCE(ssp->srcu_last_gp_end, ktime_get_mono_fast_ns()); 711aacb5d91SPaul E. McKenney rcu_seq_end(&ssp->srcu_gp_seq); 712aacb5d91SPaul E. McKenney gpseq = rcu_seq_current(&ssp->srcu_gp_seq); 713aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, gpseq)) 7148c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, gpseq); 715aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 716aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 717da915ad5SPaul E. McKenney /* A new grace period can start at this point. But only one. */ 718da915ad5SPaul E. McKenney 719da915ad5SPaul E. McKenney /* Initiate callback invocation as needed. */ 720c69a00a1SPaul E. McKenney ss_state = smp_load_acquire(&ssp->srcu_size_state); 721c69a00a1SPaul E. McKenney if (ss_state < SRCU_SIZE_WAIT_BARRIER) { 722994f7068SPaul E. McKenney srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, 0), cbdelay); 723994f7068SPaul E. McKenney } else { 724da915ad5SPaul E. McKenney idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs); 725aacb5d91SPaul E. McKenney srcu_for_each_node_breadth_first(ssp, snp) { 726d6331980SPaul E. McKenney spin_lock_irq_rcu_node(snp); 727da915ad5SPaul E. McKenney cbs = false; 728aacb5d91SPaul E. McKenney last_lvl = snp >= ssp->level[rcu_num_lvls - 1]; 7298ddbd883SIldar Ismagilov if (last_lvl) 730c69a00a1SPaul E. McKenney cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq; 731da915ad5SPaul E. McKenney snp->srcu_have_cbs[idx] = gpseq; 732da915ad5SPaul E. McKenney rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1); 733cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 734cbdc98e9SPaul E. McKenney if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq)) 7357ff8b450SPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq); 736c69a00a1SPaul E. McKenney if (ss_state < SRCU_SIZE_BIG) 737c69a00a1SPaul E. McKenney mask = ~0; 738c69a00a1SPaul E. McKenney else 739c7e88067SPaul E. McKenney mask = snp->srcu_data_have_cbs[idx]; 740c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] = 0; 741d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(snp); 742a3883df3SPaul E. McKenney if (cbs) 743aacb5d91SPaul E. McKenney srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay); 744994f7068SPaul E. McKenney } 745994f7068SPaul E. McKenney } 746c350c008SPaul E. McKenney 747c350c008SPaul E. McKenney /* Occasionally prevent srcu_data counter wrap. */ 748994f7068SPaul E. McKenney if (!(gpseq & counter_wrap_check)) 749994f7068SPaul E. McKenney for_each_possible_cpu(cpu) { 750aacb5d91SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 751d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(sdp, flags); 752994f7068SPaul E. McKenney if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100)) 753c350c008SPaul E. McKenney sdp->srcu_gp_seq_needed = gpseq; 754994f7068SPaul E. McKenney if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100)) 755a35d13ecSIldar Ismagilov sdp->srcu_gp_seq_needed_exp = gpseq; 756d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(sdp, flags); 757c350c008SPaul E. McKenney } 758da915ad5SPaul E. McKenney 759da915ad5SPaul E. McKenney /* Callback initiation done, allow grace periods after next. */ 760aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_cb_mutex); 761da915ad5SPaul E. McKenney 762da915ad5SPaul E. McKenney /* Start a new grace period if needed. */ 763aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 764aacb5d91SPaul E. McKenney gpseq = rcu_seq_current(&ssp->srcu_gp_seq); 765da915ad5SPaul E. McKenney if (!rcu_seq_state(gpseq) && 766aacb5d91SPaul E. McKenney ULONG_CMP_LT(gpseq, ssp->srcu_gp_seq_needed)) { 767aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 768aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 769aacb5d91SPaul E. McKenney srcu_reschedule(ssp, 0); 770da915ad5SPaul E. McKenney } else { 771aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 772da915ad5SPaul E. McKenney } 773e2f63836SPaul E. McKenney 774e2f63836SPaul E. McKenney /* Transition to big if needed. */ 775e2f63836SPaul E. McKenney if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) { 776e2f63836SPaul E. McKenney if (ss_state == SRCU_SIZE_ALLOC) 777c69a00a1SPaul E. McKenney init_srcu_struct_nodes(ssp, GFP_KERNEL); 778e2f63836SPaul E. McKenney else 779e2f63836SPaul E. McKenney smp_store_release(&ssp->srcu_size_state, ss_state + 1); 780e2f63836SPaul E. McKenney } 781da915ad5SPaul E. McKenney } 782da915ad5SPaul E. McKenney 783da915ad5SPaul E. McKenney /* 7841e9a038bSPaul E. McKenney * Funnel-locking scheme to scalably mediate many concurrent expedited 7851e9a038bSPaul E. McKenney * grace-period requests. This function is invoked for the first known 7861e9a038bSPaul E. McKenney * expedited request for a grace period that has already been requested, 7871e9a038bSPaul E. McKenney * but without expediting. To start a completely new grace period, 7881e9a038bSPaul E. McKenney * whether expedited or not, use srcu_funnel_gp_start() instead. 7891e9a038bSPaul E. McKenney */ 790aacb5d91SPaul E. McKenney static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp, 7911e9a038bSPaul E. McKenney unsigned long s) 7921e9a038bSPaul E. McKenney { 7931e9a038bSPaul E. McKenney unsigned long flags; 794cbdc98e9SPaul E. McKenney unsigned long sgsne; 7951e9a038bSPaul E. McKenney 796994f7068SPaul E. McKenney if (snp) 7971e9a038bSPaul E. McKenney for (; snp != NULL; snp = snp->srcu_parent) { 798cbdc98e9SPaul E. McKenney sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp); 799aacb5d91SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_gp_seq, s) || 800cbdc98e9SPaul E. McKenney (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s))) 8011e9a038bSPaul E. McKenney return; 802d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(snp, flags); 803cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 804cbdc98e9SPaul E. McKenney if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) { 805d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8061e9a038bSPaul E. McKenney return; 8071e9a038bSPaul E. McKenney } 8081e9a038bSPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 809d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8101e9a038bSPaul E. McKenney } 8119f2e91d9SPaul E. McKenney spin_lock_irqsave_ssp_contention(ssp, &flags); 812aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s)) 8138c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s); 814aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 8151e9a038bSPaul E. McKenney } 8161e9a038bSPaul E. McKenney 8171e9a038bSPaul E. McKenney /* 818da915ad5SPaul E. McKenney * Funnel-locking scheme to scalably mediate many concurrent grace-period 819da915ad5SPaul E. McKenney * requests. The winner has to do the work of actually starting grace 820da915ad5SPaul E. McKenney * period s. Losers must either ensure that their desired grace-period 821da915ad5SPaul E. McKenney * number is recorded on at least their leaf srcu_node structure, or they 822da915ad5SPaul E. McKenney * must take steps to invoke their own callbacks. 82317294ce6SPaul E. McKenney * 82417294ce6SPaul E. McKenney * Note that this function also does the work of srcu_funnel_exp_start(), 82517294ce6SPaul E. McKenney * in some cases by directly invoking it. 826da915ad5SPaul E. McKenney */ 827aacb5d91SPaul E. McKenney static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp, 8281e9a038bSPaul E. McKenney unsigned long s, bool do_norm) 829da915ad5SPaul E. McKenney { 830da915ad5SPaul E. McKenney unsigned long flags; 831da915ad5SPaul E. McKenney int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs); 832cbdc98e9SPaul E. McKenney unsigned long sgsne; 8337b9e9b58SPaul E. McKenney struct srcu_node *snp; 8340b56f953SNeeraj Upadhyay struct srcu_node *snp_leaf; 835da915ad5SPaul E. McKenney unsigned long snp_seq; 836da915ad5SPaul E. McKenney 8370b56f953SNeeraj Upadhyay /* Ensure that snp node tree is fully initialized before traversing it */ 8380b56f953SNeeraj Upadhyay if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 8390b56f953SNeeraj Upadhyay snp_leaf = NULL; 8400b56f953SNeeraj Upadhyay else 8410b56f953SNeeraj Upadhyay snp_leaf = sdp->mynode; 8420b56f953SNeeraj Upadhyay 843994f7068SPaul E. McKenney if (snp_leaf) 844da915ad5SPaul E. McKenney /* Each pass through the loop does one level of the srcu_node tree. */ 8457b9e9b58SPaul E. McKenney for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) { 8467b9e9b58SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_gp_seq, s) && snp != snp_leaf) 847da915ad5SPaul E. McKenney return; /* GP already done and CBs recorded. */ 848d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(snp, flags); 849da915ad5SPaul E. McKenney snp_seq = snp->srcu_have_cbs[idx]; 850cbdc98e9SPaul E. McKenney if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) { 8517b9e9b58SPaul E. McKenney if (snp == snp_leaf && snp_seq == s) 852c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 853d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8547b9e9b58SPaul E. McKenney if (snp == snp_leaf && snp_seq != s) { 855aeb9b39bSPaul E. McKenney srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0); 8561e9a038bSPaul E. McKenney return; 857da915ad5SPaul E. McKenney } 8581e9a038bSPaul E. McKenney if (!do_norm) 859aacb5d91SPaul E. McKenney srcu_funnel_exp_start(ssp, snp, s); 860da915ad5SPaul E. McKenney return; 861da915ad5SPaul E. McKenney } 862da915ad5SPaul E. McKenney snp->srcu_have_cbs[idx] = s; 8637b9e9b58SPaul E. McKenney if (snp == snp_leaf) 864c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 865cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 866cbdc98e9SPaul E. McKenney if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s))) 8677ff8b450SPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 868d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 869da915ad5SPaul E. McKenney } 870da915ad5SPaul E. McKenney 871da915ad5SPaul E. McKenney /* Top of tree, must ensure the grace period will be started. */ 8729f2e91d9SPaul E. McKenney spin_lock_irqsave_ssp_contention(ssp, &flags); 873aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed, s)) { 874da915ad5SPaul E. McKenney /* 875da915ad5SPaul E. McKenney * Record need for grace period s. Pair with load 876da915ad5SPaul E. McKenney * acquire setting up for initialization. 877da915ad5SPaul E. McKenney */ 878aacb5d91SPaul E. McKenney smp_store_release(&ssp->srcu_gp_seq_needed, s); /*^^^*/ 879da915ad5SPaul E. McKenney } 880aacb5d91SPaul E. McKenney if (!do_norm && ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s)) 8818c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s); 882da915ad5SPaul E. McKenney 883da915ad5SPaul E. McKenney /* If grace period not already done and none in progress, start it. */ 884aacb5d91SPaul E. McKenney if (!rcu_seq_done(&ssp->srcu_gp_seq, s) && 885aacb5d91SPaul E. McKenney rcu_seq_state(ssp->srcu_gp_seq) == SRCU_STATE_IDLE) { 886aacb5d91SPaul E. McKenney WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)); 887aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 888ee5e2448SPaul E. McKenney 889ee5e2448SPaul E. McKenney // And how can that list_add() in the "else" clause 890ee5e2448SPaul E. McKenney // possibly be safe for concurrent execution? Well, 891ee5e2448SPaul E. McKenney // it isn't. And it does not have to be. After all, it 892ee5e2448SPaul E. McKenney // can only be executed during early boot when there is only 893ee5e2448SPaul E. McKenney // the one boot CPU running with interrupts still disabled. 894e0fcba9aSPaul E. McKenney if (likely(srcu_init_done)) 895aacb5d91SPaul E. McKenney queue_delayed_work(rcu_gp_wq, &ssp->work, 896aacb5d91SPaul E. McKenney srcu_get_delay(ssp)); 897aacb5d91SPaul E. McKenney else if (list_empty(&ssp->work.work.entry)) 898aacb5d91SPaul E. McKenney list_add(&ssp->work.work.entry, &srcu_boot_list); 899da915ad5SPaul E. McKenney } 900aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 901da915ad5SPaul E. McKenney } 902da915ad5SPaul E. McKenney 903da915ad5SPaul E. McKenney /* 904dad81a20SPaul E. McKenney * Wait until all readers counted by array index idx complete, but 905dad81a20SPaul E. McKenney * loop an additional time if there is an expedited grace period pending. 906da915ad5SPaul E. McKenney * The caller must ensure that ->srcu_idx is not changed while checking. 907dad81a20SPaul E. McKenney */ 908aacb5d91SPaul E. McKenney static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount) 909dad81a20SPaul E. McKenney { 910dad81a20SPaul E. McKenney for (;;) { 911aacb5d91SPaul E. McKenney if (srcu_readers_active_idx_check(ssp, idx)) 912dad81a20SPaul E. McKenney return true; 913aacb5d91SPaul E. McKenney if (--trycount + !srcu_get_delay(ssp) <= 0) 914dad81a20SPaul E. McKenney return false; 915dad81a20SPaul E. McKenney udelay(SRCU_RETRY_CHECK_DELAY); 916dad81a20SPaul E. McKenney } 917dad81a20SPaul E. McKenney } 918dad81a20SPaul E. McKenney 919dad81a20SPaul E. McKenney /* 920da915ad5SPaul E. McKenney * Increment the ->srcu_idx counter so that future SRCU readers will 921da915ad5SPaul E. McKenney * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows 922dad81a20SPaul E. McKenney * us to wait for pre-existing readers in a starvation-free manner. 923dad81a20SPaul E. McKenney */ 924aacb5d91SPaul E. McKenney static void srcu_flip(struct srcu_struct *ssp) 925dad81a20SPaul E. McKenney { 926881ec9d2SPaul E. McKenney /* 927881ec9d2SPaul E. McKenney * Ensure that if this updater saw a given reader's increment 928881ec9d2SPaul E. McKenney * from __srcu_read_lock(), that reader was using an old value 929881ec9d2SPaul E. McKenney * of ->srcu_idx. Also ensure that if a given reader sees the 930881ec9d2SPaul E. McKenney * new value of ->srcu_idx, this updater's earlier scans cannot 931881ec9d2SPaul E. McKenney * have seen that reader's increments (which is OK, because this 932881ec9d2SPaul E. McKenney * grace period need not wait on that reader). 933881ec9d2SPaul E. McKenney */ 934881ec9d2SPaul E. McKenney smp_mb(); /* E */ /* Pairs with B and C. */ 935881ec9d2SPaul E. McKenney 936aacb5d91SPaul E. McKenney WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); 937dad81a20SPaul E. McKenney 938dad81a20SPaul E. McKenney /* 939dad81a20SPaul E. McKenney * Ensure that if the updater misses an __srcu_read_unlock() 940dad81a20SPaul E. McKenney * increment, that task's next __srcu_read_lock() will see the 941dad81a20SPaul E. McKenney * above counter update. Note that both this memory barrier 942dad81a20SPaul E. McKenney * and the one in srcu_readers_active_idx_check() provide the 943dad81a20SPaul E. McKenney * guarantee for __srcu_read_lock(). 944dad81a20SPaul E. McKenney */ 945dad81a20SPaul E. McKenney smp_mb(); /* D */ /* Pairs with C. */ 946dad81a20SPaul E. McKenney } 947dad81a20SPaul E. McKenney 948dad81a20SPaul E. McKenney /* 9492da4b2a7SPaul E. McKenney * If SRCU is likely idle, return true, otherwise return false. 9502da4b2a7SPaul E. McKenney * 9512da4b2a7SPaul E. McKenney * Note that it is OK for several current from-idle requests for a new 9522da4b2a7SPaul E. McKenney * grace period from idle to specify expediting because they will all end 9532da4b2a7SPaul E. McKenney * up requesting the same grace period anyhow. So no loss. 9542da4b2a7SPaul E. McKenney * 9552da4b2a7SPaul E. McKenney * Note also that if any CPU (including the current one) is still invoking 9562da4b2a7SPaul E. McKenney * callbacks, this function will nevertheless say "idle". This is not 9572da4b2a7SPaul E. McKenney * ideal, but the overhead of checking all CPUs' callback lists is even 9582da4b2a7SPaul E. McKenney * less ideal, especially on large systems. Furthermore, the wakeup 9592da4b2a7SPaul E. McKenney * can happen before the callback is fully removed, so we have no choice 9602da4b2a7SPaul E. McKenney * but to accept this type of error. 9612da4b2a7SPaul E. McKenney * 9622da4b2a7SPaul E. McKenney * This function is also subject to counter-wrap errors, but let's face 9632da4b2a7SPaul E. McKenney * it, if this function was preempted for enough time for the counters 9642da4b2a7SPaul E. McKenney * to wrap, it really doesn't matter whether or not we expedite the grace 9652da4b2a7SPaul E. McKenney * period. The extra overhead of a needlessly expedited grace period is 9667fef6cffSEthon Paul * negligible when amortized over that time period, and the extra latency 9672da4b2a7SPaul E. McKenney * of a needlessly non-expedited grace period is similarly negligible. 9682da4b2a7SPaul E. McKenney */ 969aacb5d91SPaul E. McKenney static bool srcu_might_be_idle(struct srcu_struct *ssp) 9702da4b2a7SPaul E. McKenney { 97122607d66SPaul E. McKenney unsigned long curseq; 9722da4b2a7SPaul E. McKenney unsigned long flags; 9732da4b2a7SPaul E. McKenney struct srcu_data *sdp; 97422607d66SPaul E. McKenney unsigned long t; 975844a378dSPaul E. McKenney unsigned long tlast; 9762da4b2a7SPaul E. McKenney 977bde50d8fSSebastian Andrzej Siewior check_init_srcu_struct(ssp); 9782da4b2a7SPaul E. McKenney /* If the local srcu_data structure has callbacks, not idle. */ 979bde50d8fSSebastian Andrzej Siewior sdp = raw_cpu_ptr(ssp->sda); 980bde50d8fSSebastian Andrzej Siewior spin_lock_irqsave_rcu_node(sdp, flags); 9812da4b2a7SPaul E. McKenney if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) { 982bde50d8fSSebastian Andrzej Siewior spin_unlock_irqrestore_rcu_node(sdp, flags); 9832da4b2a7SPaul E. McKenney return false; /* Callbacks already present, so not idle. */ 9842da4b2a7SPaul E. McKenney } 985bde50d8fSSebastian Andrzej Siewior spin_unlock_irqrestore_rcu_node(sdp, flags); 9862da4b2a7SPaul E. McKenney 9872da4b2a7SPaul E. McKenney /* 988a616aec9SIngo Molnar * No local callbacks, so probabilistically probe global state. 9892da4b2a7SPaul E. McKenney * Exact information would require acquiring locks, which would 990a616aec9SIngo Molnar * kill scalability, hence the probabilistic nature of the probe. 9912da4b2a7SPaul E. McKenney */ 99222607d66SPaul E. McKenney 99322607d66SPaul E. McKenney /* First, see if enough time has passed since the last GP. */ 99422607d66SPaul E. McKenney t = ktime_get_mono_fast_ns(); 995844a378dSPaul E. McKenney tlast = READ_ONCE(ssp->srcu_last_gp_end); 99622607d66SPaul E. McKenney if (exp_holdoff == 0 || 997844a378dSPaul E. McKenney time_in_range_open(t, tlast, tlast + exp_holdoff)) 99822607d66SPaul E. McKenney return false; /* Too soon after last GP. */ 99922607d66SPaul E. McKenney 100022607d66SPaul E. McKenney /* Next, check for probable idleness. */ 1001aacb5d91SPaul E. McKenney curseq = rcu_seq_current(&ssp->srcu_gp_seq); 10022da4b2a7SPaul E. McKenney smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */ 1003aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_gp_seq_needed))) 10042da4b2a7SPaul E. McKenney return false; /* Grace period in progress, so not idle. */ 10052da4b2a7SPaul E. McKenney smp_mb(); /* Order ->srcu_gp_seq with prior access. */ 1006aacb5d91SPaul E. McKenney if (curseq != rcu_seq_current(&ssp->srcu_gp_seq)) 10072da4b2a7SPaul E. McKenney return false; /* GP # changed, so not idle. */ 10082da4b2a7SPaul E. McKenney return true; /* With reasonable probability, idle! */ 10092da4b2a7SPaul E. McKenney } 10102da4b2a7SPaul E. McKenney 10112da4b2a7SPaul E. McKenney /* 1012a602538eSPaul E. McKenney * SRCU callback function to leak a callback. 1013a602538eSPaul E. McKenney */ 1014a602538eSPaul E. McKenney static void srcu_leak_callback(struct rcu_head *rhp) 1015a602538eSPaul E. McKenney { 1016a602538eSPaul E. McKenney } 1017a602538eSPaul E. McKenney 1018a602538eSPaul E. McKenney /* 101929d2bb94SPaul E. McKenney * Start an SRCU grace period, and also queue the callback if non-NULL. 102029d2bb94SPaul E. McKenney */ 10215358c9faSPaul E. McKenney static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp, 10225358c9faSPaul E. McKenney struct rcu_head *rhp, bool do_norm) 102329d2bb94SPaul E. McKenney { 102429d2bb94SPaul E. McKenney unsigned long flags; 102529d2bb94SPaul E. McKenney int idx; 102629d2bb94SPaul E. McKenney bool needexp = false; 102729d2bb94SPaul E. McKenney bool needgp = false; 102829d2bb94SPaul E. McKenney unsigned long s; 102929d2bb94SPaul E. McKenney struct srcu_data *sdp; 10300b56f953SNeeraj Upadhyay struct srcu_node *sdp_mynode; 10310b56f953SNeeraj Upadhyay int ss_state; 103229d2bb94SPaul E. McKenney 10335358c9faSPaul E. McKenney check_init_srcu_struct(ssp); 103429d2bb94SPaul E. McKenney idx = srcu_read_lock(ssp); 10350b56f953SNeeraj Upadhyay ss_state = smp_load_acquire(&ssp->srcu_size_state); 10360b56f953SNeeraj Upadhyay if (ss_state < SRCU_SIZE_WAIT_CALL) 1037994f7068SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, 0); 1038994f7068SPaul E. McKenney else 103929d2bb94SPaul E. McKenney sdp = raw_cpu_ptr(ssp->sda); 1040*c2445d38SPaul E. McKenney spin_lock_irqsave_sdp_contention(sdp, &flags); 10415358c9faSPaul E. McKenney if (rhp) 104229d2bb94SPaul E. McKenney rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp); 104329d2bb94SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 104429d2bb94SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 104529d2bb94SPaul E. McKenney s = rcu_seq_snap(&ssp->srcu_gp_seq); 104629d2bb94SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s); 104729d2bb94SPaul E. McKenney if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) { 104829d2bb94SPaul E. McKenney sdp->srcu_gp_seq_needed = s; 104929d2bb94SPaul E. McKenney needgp = true; 105029d2bb94SPaul E. McKenney } 105129d2bb94SPaul E. McKenney if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) { 105229d2bb94SPaul E. McKenney sdp->srcu_gp_seq_needed_exp = s; 105329d2bb94SPaul E. McKenney needexp = true; 105429d2bb94SPaul E. McKenney } 105529d2bb94SPaul E. McKenney spin_unlock_irqrestore_rcu_node(sdp, flags); 10560b56f953SNeeraj Upadhyay 10570b56f953SNeeraj Upadhyay /* Ensure that snp node tree is fully initialized before traversing it */ 10580b56f953SNeeraj Upadhyay if (ss_state < SRCU_SIZE_WAIT_BARRIER) 10590b56f953SNeeraj Upadhyay sdp_mynode = NULL; 10600b56f953SNeeraj Upadhyay else 10610b56f953SNeeraj Upadhyay sdp_mynode = sdp->mynode; 10620b56f953SNeeraj Upadhyay 106329d2bb94SPaul E. McKenney if (needgp) 106429d2bb94SPaul E. McKenney srcu_funnel_gp_start(ssp, sdp, s, do_norm); 106529d2bb94SPaul E. McKenney else if (needexp) 10660b56f953SNeeraj Upadhyay srcu_funnel_exp_start(ssp, sdp_mynode, s); 106729d2bb94SPaul E. McKenney srcu_read_unlock(ssp, idx); 10685358c9faSPaul E. McKenney return s; 106929d2bb94SPaul E. McKenney } 107029d2bb94SPaul E. McKenney 107129d2bb94SPaul E. McKenney /* 1072da915ad5SPaul E. McKenney * Enqueue an SRCU callback on the srcu_data structure associated with 1073da915ad5SPaul E. McKenney * the current CPU and the specified srcu_struct structure, initiating 1074da915ad5SPaul E. McKenney * grace-period processing if it is not already running. 1075dad81a20SPaul E. McKenney * 1076dad81a20SPaul E. McKenney * Note that all CPUs must agree that the grace period extended beyond 1077dad81a20SPaul E. McKenney * all pre-existing SRCU read-side critical section. On systems with 1078dad81a20SPaul E. McKenney * more than one CPU, this means that when "func()" is invoked, each CPU 1079dad81a20SPaul E. McKenney * is guaranteed to have executed a full memory barrier since the end of 1080dad81a20SPaul E. McKenney * its last corresponding SRCU read-side critical section whose beginning 10815ef98a63SPaul E. McKenney * preceded the call to call_srcu(). It also means that each CPU executing 1082dad81a20SPaul E. McKenney * an SRCU read-side critical section that continues beyond the start of 10835ef98a63SPaul E. McKenney * "func()" must have executed a memory barrier after the call_srcu() 1084dad81a20SPaul E. McKenney * but before the beginning of that SRCU read-side critical section. 1085dad81a20SPaul E. McKenney * Note that these guarantees include CPUs that are offline, idle, or 1086dad81a20SPaul E. McKenney * executing in user mode, as well as CPUs that are executing in the kernel. 1087dad81a20SPaul E. McKenney * 10885ef98a63SPaul E. McKenney * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the 1089dad81a20SPaul E. McKenney * resulting SRCU callback function "func()", then both CPU A and CPU 1090dad81a20SPaul E. McKenney * B are guaranteed to execute a full memory barrier during the time 10915ef98a63SPaul E. McKenney * interval between the call to call_srcu() and the invocation of "func()". 1092dad81a20SPaul E. McKenney * This guarantee applies even if CPU A and CPU B are the same CPU (but 1093dad81a20SPaul E. McKenney * again only if the system has more than one CPU). 1094dad81a20SPaul E. McKenney * 1095dad81a20SPaul E. McKenney * Of course, these guarantees apply only for invocations of call_srcu(), 1096dad81a20SPaul E. McKenney * srcu_read_lock(), and srcu_read_unlock() that are all passed the same 1097dad81a20SPaul E. McKenney * srcu_struct structure. 1098dad81a20SPaul E. McKenney */ 109911b00045SJiang Biao static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 11001e9a038bSPaul E. McKenney rcu_callback_t func, bool do_norm) 1101dad81a20SPaul E. McKenney { 1102a602538eSPaul E. McKenney if (debug_rcu_head_queue(rhp)) { 1103a602538eSPaul E. McKenney /* Probable double call_srcu(), so leak the callback. */ 1104a602538eSPaul E. McKenney WRITE_ONCE(rhp->func, srcu_leak_callback); 1105a602538eSPaul E. McKenney WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n"); 1106a602538eSPaul E. McKenney return; 1107a602538eSPaul E. McKenney } 1108da915ad5SPaul E. McKenney rhp->func = func; 11095358c9faSPaul E. McKenney (void)srcu_gp_start_if_needed(ssp, rhp, do_norm); 11101e9a038bSPaul E. McKenney } 11111e9a038bSPaul E. McKenney 11125a0465e1SPaul E. McKenney /** 11135a0465e1SPaul E. McKenney * call_srcu() - Queue a callback for invocation after an SRCU grace period 1114aacb5d91SPaul E. McKenney * @ssp: srcu_struct in queue the callback 111527fdb35fSPaul E. McKenney * @rhp: structure to be used for queueing the SRCU callback. 11165a0465e1SPaul E. McKenney * @func: function to be invoked after the SRCU grace period 11175a0465e1SPaul E. McKenney * 11185a0465e1SPaul E. McKenney * The callback function will be invoked some time after a full SRCU 11195a0465e1SPaul E. McKenney * grace period elapses, in other words after all pre-existing SRCU 11205a0465e1SPaul E. McKenney * read-side critical sections have completed. However, the callback 11215a0465e1SPaul E. McKenney * function might well execute concurrently with other SRCU read-side 11225a0465e1SPaul E. McKenney * critical sections that started after call_srcu() was invoked. SRCU 11235a0465e1SPaul E. McKenney * read-side critical sections are delimited by srcu_read_lock() and 11245a0465e1SPaul E. McKenney * srcu_read_unlock(), and may be nested. 11255a0465e1SPaul E. McKenney * 11265a0465e1SPaul E. McKenney * The callback will be invoked from process context, but must nevertheless 11275a0465e1SPaul E. McKenney * be fast and must not block. 11285a0465e1SPaul E. McKenney */ 1129aacb5d91SPaul E. McKenney void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 11301e9a038bSPaul E. McKenney rcu_callback_t func) 11311e9a038bSPaul E. McKenney { 1132aacb5d91SPaul E. McKenney __call_srcu(ssp, rhp, func, true); 1133dad81a20SPaul E. McKenney } 1134dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(call_srcu); 1135dad81a20SPaul E. McKenney 1136dad81a20SPaul E. McKenney /* 1137dad81a20SPaul E. McKenney * Helper function for synchronize_srcu() and synchronize_srcu_expedited(). 1138dad81a20SPaul E. McKenney */ 1139aacb5d91SPaul E. McKenney static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm) 1140dad81a20SPaul E. McKenney { 1141dad81a20SPaul E. McKenney struct rcu_synchronize rcu; 1142dad81a20SPaul E. McKenney 1143f505d434SJakub Kicinski RCU_LOCKDEP_WARN(lockdep_is_held(ssp) || 1144dad81a20SPaul E. McKenney lock_is_held(&rcu_bh_lock_map) || 1145dad81a20SPaul E. McKenney lock_is_held(&rcu_lock_map) || 1146dad81a20SPaul E. McKenney lock_is_held(&rcu_sched_lock_map), 1147dad81a20SPaul E. McKenney "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section"); 1148dad81a20SPaul E. McKenney 1149dad81a20SPaul E. McKenney if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 1150dad81a20SPaul E. McKenney return; 1151dad81a20SPaul E. McKenney might_sleep(); 1152aacb5d91SPaul E. McKenney check_init_srcu_struct(ssp); 1153dad81a20SPaul E. McKenney init_completion(&rcu.completion); 1154da915ad5SPaul E. McKenney init_rcu_head_on_stack(&rcu.head); 1155aacb5d91SPaul E. McKenney __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm); 1156dad81a20SPaul E. McKenney wait_for_completion(&rcu.completion); 1157da915ad5SPaul E. McKenney destroy_rcu_head_on_stack(&rcu.head); 115835732cf9SPaul E. McKenney 115935732cf9SPaul E. McKenney /* 116035732cf9SPaul E. McKenney * Make sure that later code is ordered after the SRCU grace 1161d6331980SPaul E. McKenney * period. This pairs with the spin_lock_irq_rcu_node() 116235732cf9SPaul E. McKenney * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed 116335732cf9SPaul E. McKenney * because the current CPU might have been totally uninvolved with 116435732cf9SPaul E. McKenney * (and thus unordered against) that grace period. 116535732cf9SPaul E. McKenney */ 116635732cf9SPaul E. McKenney smp_mb(); 1167dad81a20SPaul E. McKenney } 1168dad81a20SPaul E. McKenney 1169dad81a20SPaul E. McKenney /** 1170dad81a20SPaul E. McKenney * synchronize_srcu_expedited - Brute-force SRCU grace period 1171aacb5d91SPaul E. McKenney * @ssp: srcu_struct with which to synchronize. 1172dad81a20SPaul E. McKenney * 1173dad81a20SPaul E. McKenney * Wait for an SRCU grace period to elapse, but be more aggressive about 1174dad81a20SPaul E. McKenney * spinning rather than blocking when waiting. 1175dad81a20SPaul E. McKenney * 1176dad81a20SPaul E. McKenney * Note that synchronize_srcu_expedited() has the same deadlock and 1177dad81a20SPaul E. McKenney * memory-ordering properties as does synchronize_srcu(). 1178dad81a20SPaul E. McKenney */ 1179aacb5d91SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *ssp) 1180dad81a20SPaul E. McKenney { 1181aacb5d91SPaul E. McKenney __synchronize_srcu(ssp, rcu_gp_is_normal()); 1182dad81a20SPaul E. McKenney } 1183dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu_expedited); 1184dad81a20SPaul E. McKenney 1185dad81a20SPaul E. McKenney /** 1186dad81a20SPaul E. McKenney * synchronize_srcu - wait for prior SRCU read-side critical-section completion 1187aacb5d91SPaul E. McKenney * @ssp: srcu_struct with which to synchronize. 1188dad81a20SPaul E. McKenney * 1189dad81a20SPaul E. McKenney * Wait for the count to drain to zero of both indexes. To avoid the 1190dad81a20SPaul E. McKenney * possible starvation of synchronize_srcu(), it waits for the count of 1191da915ad5SPaul E. McKenney * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first, 1192da915ad5SPaul E. McKenney * and then flip the srcu_idx and wait for the count of the other index. 1193dad81a20SPaul E. McKenney * 1194dad81a20SPaul E. McKenney * Can block; must be called from process context. 1195dad81a20SPaul E. McKenney * 1196dad81a20SPaul E. McKenney * Note that it is illegal to call synchronize_srcu() from the corresponding 1197dad81a20SPaul E. McKenney * SRCU read-side critical section; doing so will result in deadlock. 1198dad81a20SPaul E. McKenney * However, it is perfectly legal to call synchronize_srcu() on one 1199dad81a20SPaul E. McKenney * srcu_struct from some other srcu_struct's read-side critical section, 1200dad81a20SPaul E. McKenney * as long as the resulting graph of srcu_structs is acyclic. 1201dad81a20SPaul E. McKenney * 1202dad81a20SPaul E. McKenney * There are memory-ordering constraints implied by synchronize_srcu(). 1203dad81a20SPaul E. McKenney * On systems with more than one CPU, when synchronize_srcu() returns, 1204dad81a20SPaul E. McKenney * each CPU is guaranteed to have executed a full memory barrier since 12056eb95cc4SPaul E. McKenney * the end of its last corresponding SRCU read-side critical section 1206dad81a20SPaul E. McKenney * whose beginning preceded the call to synchronize_srcu(). In addition, 1207dad81a20SPaul E. McKenney * each CPU having an SRCU read-side critical section that extends beyond 1208dad81a20SPaul E. McKenney * the return from synchronize_srcu() is guaranteed to have executed a 1209dad81a20SPaul E. McKenney * full memory barrier after the beginning of synchronize_srcu() and before 1210dad81a20SPaul E. McKenney * the beginning of that SRCU read-side critical section. Note that these 1211dad81a20SPaul E. McKenney * guarantees include CPUs that are offline, idle, or executing in user mode, 1212dad81a20SPaul E. McKenney * as well as CPUs that are executing in the kernel. 1213dad81a20SPaul E. McKenney * 1214dad81a20SPaul E. McKenney * Furthermore, if CPU A invoked synchronize_srcu(), which returned 1215dad81a20SPaul E. McKenney * to its caller on CPU B, then both CPU A and CPU B are guaranteed 1216dad81a20SPaul E. McKenney * to have executed a full memory barrier during the execution of 1217dad81a20SPaul E. McKenney * synchronize_srcu(). This guarantee applies even if CPU A and CPU B 1218dad81a20SPaul E. McKenney * are the same CPU, but again only if the system has more than one CPU. 1219dad81a20SPaul E. McKenney * 1220dad81a20SPaul E. McKenney * Of course, these memory-ordering guarantees apply only when 1221dad81a20SPaul E. McKenney * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are 1222dad81a20SPaul E. McKenney * passed the same srcu_struct structure. 12232da4b2a7SPaul E. McKenney * 12243d3a0d1bSPaul E. McKenney * Implementation of these memory-ordering guarantees is similar to 12253d3a0d1bSPaul E. McKenney * that of synchronize_rcu(). 12263d3a0d1bSPaul E. McKenney * 12272da4b2a7SPaul E. McKenney * If SRCU is likely idle, expedite the first request. This semantic 12282da4b2a7SPaul E. McKenney * was provided by Classic SRCU, and is relied upon by its users, so TREE 12292da4b2a7SPaul E. McKenney * SRCU must also provide it. Note that detecting idleness is heuristic 12302da4b2a7SPaul E. McKenney * and subject to both false positives and negatives. 1231dad81a20SPaul E. McKenney */ 1232aacb5d91SPaul E. McKenney void synchronize_srcu(struct srcu_struct *ssp) 1233dad81a20SPaul E. McKenney { 1234aacb5d91SPaul E. McKenney if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited()) 1235aacb5d91SPaul E. McKenney synchronize_srcu_expedited(ssp); 1236dad81a20SPaul E. McKenney else 1237aacb5d91SPaul E. McKenney __synchronize_srcu(ssp, true); 1238dad81a20SPaul E. McKenney } 1239dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu); 1240dad81a20SPaul E. McKenney 12415358c9faSPaul E. McKenney /** 12425358c9faSPaul E. McKenney * get_state_synchronize_srcu - Provide an end-of-grace-period cookie 12435358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12445358c9faSPaul E. McKenney * 12455358c9faSPaul E. McKenney * This function returns a cookie that can be passed to 12465358c9faSPaul E. McKenney * poll_state_synchronize_srcu(), which will return true if a full grace 12475358c9faSPaul E. McKenney * period has elapsed in the meantime. It is the caller's responsibility 12485358c9faSPaul E. McKenney * to make sure that grace period happens, for example, by invoking 12495358c9faSPaul E. McKenney * call_srcu() after return from get_state_synchronize_srcu(). 12505358c9faSPaul E. McKenney */ 12515358c9faSPaul E. McKenney unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp) 12525358c9faSPaul E. McKenney { 12535358c9faSPaul E. McKenney // Any prior manipulation of SRCU-protected data must happen 12545358c9faSPaul E. McKenney // before the load from ->srcu_gp_seq. 12555358c9faSPaul E. McKenney smp_mb(); 12565358c9faSPaul E. McKenney return rcu_seq_snap(&ssp->srcu_gp_seq); 12575358c9faSPaul E. McKenney } 12585358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(get_state_synchronize_srcu); 12595358c9faSPaul E. McKenney 12605358c9faSPaul E. McKenney /** 12615358c9faSPaul E. McKenney * start_poll_synchronize_srcu - Provide cookie and start grace period 12625358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12635358c9faSPaul E. McKenney * 12645358c9faSPaul E. McKenney * This function returns a cookie that can be passed to 12655358c9faSPaul E. McKenney * poll_state_synchronize_srcu(), which will return true if a full grace 12665358c9faSPaul E. McKenney * period has elapsed in the meantime. Unlike get_state_synchronize_srcu(), 12675358c9faSPaul E. McKenney * this function also ensures that any needed SRCU grace period will be 12685358c9faSPaul E. McKenney * started. This convenience does come at a cost in terms of CPU overhead. 12695358c9faSPaul E. McKenney */ 12705358c9faSPaul E. McKenney unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp) 12715358c9faSPaul E. McKenney { 12725358c9faSPaul E. McKenney return srcu_gp_start_if_needed(ssp, NULL, true); 12735358c9faSPaul E. McKenney } 12745358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu); 12755358c9faSPaul E. McKenney 12765358c9faSPaul E. McKenney /** 12775358c9faSPaul E. McKenney * poll_state_synchronize_srcu - Has cookie's grace period ended? 12785358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12795358c9faSPaul E. McKenney * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu(). 12805358c9faSPaul E. McKenney * 12815358c9faSPaul E. McKenney * This function takes the cookie that was returned from either 12825358c9faSPaul E. McKenney * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and 12835358c9faSPaul E. McKenney * returns @true if an SRCU grace period elapsed since the time that the 12845358c9faSPaul E. McKenney * cookie was created. 12854e7ccfaeSPaul E. McKenney * 12864e7ccfaeSPaul E. McKenney * Because cookies are finite in size, wrapping/overflow is possible. 12874e7ccfaeSPaul E. McKenney * This is more pronounced on 32-bit systems where cookies are 32 bits, 12884e7ccfaeSPaul E. McKenney * where in theory wrapping could happen in about 14 hours assuming 12894e7ccfaeSPaul E. McKenney * 25-microsecond expedited SRCU grace periods. However, a more likely 12904e7ccfaeSPaul E. McKenney * overflow lower bound is on the order of 24 days in the case of 12914e7ccfaeSPaul E. McKenney * one-millisecond SRCU grace periods. Of course, wrapping in a 64-bit 12924e7ccfaeSPaul E. McKenney * system requires geologic timespans, as in more than seven million years 12934e7ccfaeSPaul E. McKenney * even for expedited SRCU grace periods. 12944e7ccfaeSPaul E. McKenney * 12954e7ccfaeSPaul E. McKenney * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems 12964e7ccfaeSPaul E. McKenney * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU. This uses 12974e7ccfaeSPaul E. McKenney * a 16-bit cookie, which rcutorture routinely wraps in a matter of a 12984e7ccfaeSPaul E. McKenney * few minutes. If this proves to be a problem, this counter will be 12994e7ccfaeSPaul E. McKenney * expanded to the same size as for Tree SRCU. 13005358c9faSPaul E. McKenney */ 13015358c9faSPaul E. McKenney bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie) 13025358c9faSPaul E. McKenney { 13035358c9faSPaul E. McKenney if (!rcu_seq_done(&ssp->srcu_gp_seq, cookie)) 13045358c9faSPaul E. McKenney return false; 13055358c9faSPaul E. McKenney // Ensure that the end of the SRCU grace period happens before 13065358c9faSPaul E. McKenney // any subsequent code that the caller might execute. 13075358c9faSPaul E. McKenney smp_mb(); // ^^^ 13085358c9faSPaul E. McKenney return true; 13095358c9faSPaul E. McKenney } 13105358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu); 13115358c9faSPaul E. McKenney 1312da915ad5SPaul E. McKenney /* 1313da915ad5SPaul E. McKenney * Callback function for srcu_barrier() use. 1314da915ad5SPaul E. McKenney */ 1315da915ad5SPaul E. McKenney static void srcu_barrier_cb(struct rcu_head *rhp) 1316da915ad5SPaul E. McKenney { 1317da915ad5SPaul E. McKenney struct srcu_data *sdp; 1318aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1319da915ad5SPaul E. McKenney 1320da915ad5SPaul E. McKenney sdp = container_of(rhp, struct srcu_data, srcu_barrier_head); 1321aacb5d91SPaul E. McKenney ssp = sdp->ssp; 1322aacb5d91SPaul E. McKenney if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt)) 1323aacb5d91SPaul E. McKenney complete(&ssp->srcu_barrier_completion); 1324da915ad5SPaul E. McKenney } 1325da915ad5SPaul E. McKenney 1326994f7068SPaul E. McKenney /* 1327994f7068SPaul E. McKenney * Enqueue an srcu_barrier() callback on the specified srcu_data 1328994f7068SPaul E. McKenney * structure's ->cblist. but only if that ->cblist already has at least one 1329994f7068SPaul E. McKenney * callback enqueued. Note that if a CPU already has callbacks enqueue, 1330994f7068SPaul E. McKenney * it must have already registered the need for a future grace period, 1331994f7068SPaul E. McKenney * so all we need do is enqueue a callback that will use the same grace 1332994f7068SPaul E. McKenney * period as the last callback already in the queue. 1333994f7068SPaul E. McKenney */ 1334994f7068SPaul E. McKenney static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp) 1335994f7068SPaul E. McKenney { 1336994f7068SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1337994f7068SPaul E. McKenney atomic_inc(&ssp->srcu_barrier_cpu_cnt); 1338994f7068SPaul E. McKenney sdp->srcu_barrier_head.func = srcu_barrier_cb; 1339994f7068SPaul E. McKenney debug_rcu_head_queue(&sdp->srcu_barrier_head); 1340994f7068SPaul E. McKenney if (!rcu_segcblist_entrain(&sdp->srcu_cblist, 1341994f7068SPaul E. McKenney &sdp->srcu_barrier_head)) { 1342994f7068SPaul E. McKenney debug_rcu_head_unqueue(&sdp->srcu_barrier_head); 1343994f7068SPaul E. McKenney atomic_dec(&ssp->srcu_barrier_cpu_cnt); 1344994f7068SPaul E. McKenney } 1345994f7068SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1346994f7068SPaul E. McKenney } 1347994f7068SPaul E. McKenney 1348dad81a20SPaul E. McKenney /** 1349dad81a20SPaul E. McKenney * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete. 1350aacb5d91SPaul E. McKenney * @ssp: srcu_struct on which to wait for in-flight callbacks. 1351dad81a20SPaul E. McKenney */ 1352aacb5d91SPaul E. McKenney void srcu_barrier(struct srcu_struct *ssp) 1353dad81a20SPaul E. McKenney { 1354da915ad5SPaul E. McKenney int cpu; 1355e2f63836SPaul E. McKenney int idx; 1356aacb5d91SPaul E. McKenney unsigned long s = rcu_seq_snap(&ssp->srcu_barrier_seq); 1357da915ad5SPaul E. McKenney 1358aacb5d91SPaul E. McKenney check_init_srcu_struct(ssp); 1359aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_barrier_mutex); 1360aacb5d91SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_barrier_seq, s)) { 1361da915ad5SPaul E. McKenney smp_mb(); /* Force ordering following return. */ 1362aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_barrier_mutex); 1363da915ad5SPaul E. McKenney return; /* Someone else did our work for us. */ 1364da915ad5SPaul E. McKenney } 1365aacb5d91SPaul E. McKenney rcu_seq_start(&ssp->srcu_barrier_seq); 1366aacb5d91SPaul E. McKenney init_completion(&ssp->srcu_barrier_completion); 1367da915ad5SPaul E. McKenney 1368da915ad5SPaul E. McKenney /* Initial count prevents reaching zero until all CBs are posted. */ 1369aacb5d91SPaul E. McKenney atomic_set(&ssp->srcu_barrier_cpu_cnt, 1); 1370da915ad5SPaul E. McKenney 1371e2f63836SPaul E. McKenney idx = srcu_read_lock(ssp); 1372994f7068SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 1373994f7068SPaul E. McKenney srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, 0)); 1374994f7068SPaul E. McKenney else 1375994f7068SPaul E. McKenney for_each_possible_cpu(cpu) 1376994f7068SPaul E. McKenney srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu)); 1377e2f63836SPaul E. McKenney srcu_read_unlock(ssp, idx); 1378da915ad5SPaul E. McKenney 1379da915ad5SPaul E. McKenney /* Remove the initial count, at which point reaching zero can happen. */ 1380aacb5d91SPaul E. McKenney if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt)) 1381aacb5d91SPaul E. McKenney complete(&ssp->srcu_barrier_completion); 1382aacb5d91SPaul E. McKenney wait_for_completion(&ssp->srcu_barrier_completion); 1383da915ad5SPaul E. McKenney 1384aacb5d91SPaul E. McKenney rcu_seq_end(&ssp->srcu_barrier_seq); 1385aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_barrier_mutex); 1386dad81a20SPaul E. McKenney } 1387dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_barrier); 1388dad81a20SPaul E. McKenney 1389dad81a20SPaul E. McKenney /** 1390dad81a20SPaul E. McKenney * srcu_batches_completed - return batches completed. 1391aacb5d91SPaul E. McKenney * @ssp: srcu_struct on which to report batch completion. 1392dad81a20SPaul E. McKenney * 1393dad81a20SPaul E. McKenney * Report the number of batches, correlated with, but not necessarily 1394dad81a20SPaul E. McKenney * precisely the same as, the number of grace periods that have elapsed. 1395dad81a20SPaul E. McKenney */ 1396aacb5d91SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *ssp) 1397dad81a20SPaul E. McKenney { 139839f91504SPaul E. McKenney return READ_ONCE(ssp->srcu_idx); 1399dad81a20SPaul E. McKenney } 1400dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_batches_completed); 1401dad81a20SPaul E. McKenney 1402dad81a20SPaul E. McKenney /* 1403da915ad5SPaul E. McKenney * Core SRCU state machine. Push state bits of ->srcu_gp_seq 1404da915ad5SPaul E. McKenney * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has 1405da915ad5SPaul E. McKenney * completed in that state. 1406dad81a20SPaul E. McKenney */ 1407aacb5d91SPaul E. McKenney static void srcu_advance_state(struct srcu_struct *ssp) 1408dad81a20SPaul E. McKenney { 1409dad81a20SPaul E. McKenney int idx; 1410dad81a20SPaul E. McKenney 1411aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_gp_mutex); 1412da915ad5SPaul E. McKenney 1413dad81a20SPaul E. McKenney /* 1414dad81a20SPaul E. McKenney * Because readers might be delayed for an extended period after 1415da915ad5SPaul E. McKenney * fetching ->srcu_idx for their index, at any point in time there 1416dad81a20SPaul E. McKenney * might well be readers using both idx=0 and idx=1. We therefore 1417dad81a20SPaul E. McKenney * need to wait for readers to clear from both index values before 1418dad81a20SPaul E. McKenney * invoking a callback. 1419dad81a20SPaul E. McKenney * 1420dad81a20SPaul E. McKenney * The load-acquire ensures that we see the accesses performed 1421dad81a20SPaul E. McKenney * by the prior grace period. 1422dad81a20SPaul E. McKenney */ 1423aacb5d91SPaul E. McKenney idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq)); /* ^^^ */ 1424dad81a20SPaul E. McKenney if (idx == SRCU_STATE_IDLE) { 1425aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1426aacb5d91SPaul E. McKenney if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) { 1427aacb5d91SPaul E. McKenney WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq)); 1428aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1429aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1430dad81a20SPaul E. McKenney return; 1431dad81a20SPaul E. McKenney } 1432aacb5d91SPaul E. McKenney idx = rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)); 1433dad81a20SPaul E. McKenney if (idx == SRCU_STATE_IDLE) 1434aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 1435aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1436da915ad5SPaul E. McKenney if (idx != SRCU_STATE_IDLE) { 1437aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1438dad81a20SPaul E. McKenney return; /* Someone else started the grace period. */ 1439dad81a20SPaul E. McKenney } 1440da915ad5SPaul E. McKenney } 1441dad81a20SPaul E. McKenney 1442aacb5d91SPaul E. McKenney if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN1) { 1443aacb5d91SPaul E. McKenney idx = 1 ^ (ssp->srcu_idx & 1); 1444aacb5d91SPaul E. McKenney if (!try_check_zero(ssp, idx, 1)) { 1445aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1446dad81a20SPaul E. McKenney return; /* readers present, retry later. */ 1447da915ad5SPaul E. McKenney } 1448aacb5d91SPaul E. McKenney srcu_flip(ssp); 144971042606SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1450aacb5d91SPaul E. McKenney rcu_seq_set_state(&ssp->srcu_gp_seq, SRCU_STATE_SCAN2); 145171042606SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1452dad81a20SPaul E. McKenney } 1453dad81a20SPaul E. McKenney 1454aacb5d91SPaul E. McKenney if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN2) { 1455dad81a20SPaul E. McKenney 1456dad81a20SPaul E. McKenney /* 1457dad81a20SPaul E. McKenney * SRCU read-side critical sections are normally short, 1458dad81a20SPaul E. McKenney * so check at least twice in quick succession after a flip. 1459dad81a20SPaul E. McKenney */ 1460aacb5d91SPaul E. McKenney idx = 1 ^ (ssp->srcu_idx & 1); 1461aacb5d91SPaul E. McKenney if (!try_check_zero(ssp, idx, 2)) { 1462aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1463da915ad5SPaul E. McKenney return; /* readers present, retry later. */ 1464da915ad5SPaul E. McKenney } 1465aacb5d91SPaul E. McKenney srcu_gp_end(ssp); /* Releases ->srcu_gp_mutex. */ 1466dad81a20SPaul E. McKenney } 1467dad81a20SPaul E. McKenney } 1468dad81a20SPaul E. McKenney 1469dad81a20SPaul E. McKenney /* 1470dad81a20SPaul E. McKenney * Invoke a limited number of SRCU callbacks that have passed through 1471dad81a20SPaul E. McKenney * their grace period. If there are more to do, SRCU will reschedule 1472dad81a20SPaul E. McKenney * the workqueue. Note that needed memory barriers have been executed 1473dad81a20SPaul E. McKenney * in this task's context by srcu_readers_active_idx_check(). 1474dad81a20SPaul E. McKenney */ 1475da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work) 1476dad81a20SPaul E. McKenney { 1477ae5c2341SJoel Fernandes (Google) long len; 1478da915ad5SPaul E. McKenney bool more; 1479dad81a20SPaul E. McKenney struct rcu_cblist ready_cbs; 1480dad81a20SPaul E. McKenney struct rcu_head *rhp; 1481da915ad5SPaul E. McKenney struct srcu_data *sdp; 1482aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1483dad81a20SPaul E. McKenney 1484e81baf4cSSebastian Andrzej Siewior sdp = container_of(work, struct srcu_data, work); 1485e81baf4cSSebastian Andrzej Siewior 1486aacb5d91SPaul E. McKenney ssp = sdp->ssp; 1487dad81a20SPaul E. McKenney rcu_cblist_init(&ready_cbs); 1488d6331980SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1489da915ad5SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 1490aacb5d91SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 1491da915ad5SPaul E. McKenney if (sdp->srcu_cblist_invoking || 1492da915ad5SPaul E. McKenney !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) { 1493d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1494da915ad5SPaul E. McKenney return; /* Someone else on the job or nothing to do. */ 1495da915ad5SPaul E. McKenney } 1496da915ad5SPaul E. McKenney 1497da915ad5SPaul E. McKenney /* We are on the job! Extract and invoke ready callbacks. */ 1498da915ad5SPaul E. McKenney sdp->srcu_cblist_invoking = true; 1499da915ad5SPaul E. McKenney rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs); 1500ae5c2341SJoel Fernandes (Google) len = ready_cbs.len; 1501d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1502dad81a20SPaul E. McKenney rhp = rcu_cblist_dequeue(&ready_cbs); 1503dad81a20SPaul E. McKenney for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) { 1504a602538eSPaul E. McKenney debug_rcu_head_unqueue(rhp); 1505dad81a20SPaul E. McKenney local_bh_disable(); 1506dad81a20SPaul E. McKenney rhp->func(rhp); 1507dad81a20SPaul E. McKenney local_bh_enable(); 1508dad81a20SPaul E. McKenney } 1509ae5c2341SJoel Fernandes (Google) WARN_ON_ONCE(ready_cbs.len); 1510da915ad5SPaul E. McKenney 1511da915ad5SPaul E. McKenney /* 1512da915ad5SPaul E. McKenney * Update counts, accelerate new callbacks, and if needed, 1513da915ad5SPaul E. McKenney * schedule another round of callback invocation. 1514da915ad5SPaul E. McKenney */ 1515d6331980SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1516ae5c2341SJoel Fernandes (Google) rcu_segcblist_add_len(&sdp->srcu_cblist, -len); 1517da915ad5SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, 1518aacb5d91SPaul E. McKenney rcu_seq_snap(&ssp->srcu_gp_seq)); 1519da915ad5SPaul E. McKenney sdp->srcu_cblist_invoking = false; 1520da915ad5SPaul E. McKenney more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist); 1521d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1522da915ad5SPaul E. McKenney if (more) 1523da915ad5SPaul E. McKenney srcu_schedule_cbs_sdp(sdp, 0); 1524dad81a20SPaul E. McKenney } 1525dad81a20SPaul E. McKenney 1526dad81a20SPaul E. McKenney /* 1527dad81a20SPaul E. McKenney * Finished one round of SRCU grace period. Start another if there are 1528dad81a20SPaul E. McKenney * more SRCU callbacks queued, otherwise put SRCU into not-running state. 1529dad81a20SPaul E. McKenney */ 1530aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay) 1531dad81a20SPaul E. McKenney { 1532da915ad5SPaul E. McKenney bool pushgp = true; 1533dad81a20SPaul E. McKenney 1534aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1535aacb5d91SPaul E. McKenney if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) { 1536aacb5d91SPaul E. McKenney if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq))) { 1537da915ad5SPaul E. McKenney /* All requests fulfilled, time to go idle. */ 1538da915ad5SPaul E. McKenney pushgp = false; 1539dad81a20SPaul E. McKenney } 1540aacb5d91SPaul E. McKenney } else if (!rcu_seq_state(ssp->srcu_gp_seq)) { 1541da915ad5SPaul E. McKenney /* Outstanding request and no GP. Start one. */ 1542aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 1543da915ad5SPaul E. McKenney } 1544aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1545dad81a20SPaul E. McKenney 1546da915ad5SPaul E. McKenney if (pushgp) 1547aacb5d91SPaul E. McKenney queue_delayed_work(rcu_gp_wq, &ssp->work, delay); 1548dad81a20SPaul E. McKenney } 1549dad81a20SPaul E. McKenney 1550dad81a20SPaul E. McKenney /* 1551dad81a20SPaul E. McKenney * This is the work-queue function that handles SRCU grace periods. 1552dad81a20SPaul E. McKenney */ 15530d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work) 1554dad81a20SPaul E. McKenney { 1555aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1556dad81a20SPaul E. McKenney 1557aacb5d91SPaul E. McKenney ssp = container_of(work, struct srcu_struct, work.work); 1558dad81a20SPaul E. McKenney 1559aacb5d91SPaul E. McKenney srcu_advance_state(ssp); 1560aacb5d91SPaul E. McKenney srcu_reschedule(ssp, srcu_get_delay(ssp)); 1561dad81a20SPaul E. McKenney } 15627f6733c3SPaul E. McKenney 15637f6733c3SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type, 1564aacb5d91SPaul E. McKenney struct srcu_struct *ssp, int *flags, 1565aebc8264SPaul E. McKenney unsigned long *gp_seq) 15667f6733c3SPaul E. McKenney { 15677f6733c3SPaul E. McKenney if (test_type != SRCU_FLAVOR) 15687f6733c3SPaul E. McKenney return; 15697f6733c3SPaul E. McKenney *flags = 0; 1570aacb5d91SPaul E. McKenney *gp_seq = rcu_seq_current(&ssp->srcu_gp_seq); 15717f6733c3SPaul E. McKenney } 15727f6733c3SPaul E. McKenney EXPORT_SYMBOL_GPL(srcutorture_get_gp_data); 15731f4f6da1SPaul E. McKenney 15743bedebcfSPaul E. McKenney static const char * const srcu_size_state_name[] = { 15753bedebcfSPaul E. McKenney "SRCU_SIZE_SMALL", 15763bedebcfSPaul E. McKenney "SRCU_SIZE_ALLOC", 15773bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_BARRIER", 15783bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CALL", 15793bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS1", 15803bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS2", 15813bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS3", 15823bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS4", 15833bedebcfSPaul E. McKenney "SRCU_SIZE_BIG", 15843bedebcfSPaul E. McKenney "SRCU_SIZE_???", 15853bedebcfSPaul E. McKenney }; 15863bedebcfSPaul E. McKenney 1587aacb5d91SPaul E. McKenney void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf) 1588115a1a52SPaul E. McKenney { 1589115a1a52SPaul E. McKenney int cpu; 1590115a1a52SPaul E. McKenney int idx; 1591ac3748c6SPaul E. McKenney unsigned long s0 = 0, s1 = 0; 15923bedebcfSPaul E. McKenney int ss_state = READ_ONCE(ssp->srcu_size_state); 15933bedebcfSPaul E. McKenney int ss_state_idx = ss_state; 1594115a1a52SPaul E. McKenney 1595aacb5d91SPaul E. McKenney idx = ssp->srcu_idx & 0x1; 15963bedebcfSPaul E. McKenney if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name)) 15973bedebcfSPaul E. McKenney ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1; 15984a230f80SPaul E. McKenney pr_alert("%s%s Tree SRCU g%ld state %d (%s)", 15993bedebcfSPaul E. McKenney tt, tf, rcu_seq_current(&ssp->srcu_gp_seq), ss_state, 16004a230f80SPaul E. McKenney srcu_size_state_name[ss_state_idx]); 16014a230f80SPaul E. McKenney if (!ssp->sda) { 16024a230f80SPaul E. McKenney // Called after cleanup_srcu_struct(), perhaps. 16034a230f80SPaul E. McKenney pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n"); 16044a230f80SPaul E. McKenney } else { 16054a230f80SPaul E. McKenney pr_cont(" per-CPU(idx=%d):", idx); 1606115a1a52SPaul E. McKenney for_each_possible_cpu(cpu) { 1607115a1a52SPaul E. McKenney unsigned long l0, l1; 1608115a1a52SPaul E. McKenney unsigned long u0, u1; 1609115a1a52SPaul E. McKenney long c0, c1; 16105ab07a8dSPaul E. McKenney struct srcu_data *sdp; 1611115a1a52SPaul E. McKenney 1612aacb5d91SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 1613b68c6146SPaul E. McKenney u0 = data_race(sdp->srcu_unlock_count[!idx]); 1614b68c6146SPaul E. McKenney u1 = data_race(sdp->srcu_unlock_count[idx]); 1615115a1a52SPaul E. McKenney 1616115a1a52SPaul E. McKenney /* 1617115a1a52SPaul E. McKenney * Make sure that a lock is always counted if the corresponding 1618115a1a52SPaul E. McKenney * unlock is counted. 1619115a1a52SPaul E. McKenney */ 1620115a1a52SPaul E. McKenney smp_rmb(); 1621115a1a52SPaul E. McKenney 1622b68c6146SPaul E. McKenney l0 = data_race(sdp->srcu_lock_count[!idx]); 1623b68c6146SPaul E. McKenney l1 = data_race(sdp->srcu_lock_count[idx]); 1624115a1a52SPaul E. McKenney 1625115a1a52SPaul E. McKenney c0 = l0 - u0; 1626115a1a52SPaul E. McKenney c1 = l1 - u1; 16277e210a65SPaul E. McKenney pr_cont(" %d(%ld,%ld %c)", 16287e210a65SPaul E. McKenney cpu, c0, c1, 16297e210a65SPaul E. McKenney "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]); 1630ac3748c6SPaul E. McKenney s0 += c0; 1631ac3748c6SPaul E. McKenney s1 += c1; 1632115a1a52SPaul E. McKenney } 1633ac3748c6SPaul E. McKenney pr_cont(" T(%ld,%ld)\n", s0, s1); 16344a230f80SPaul E. McKenney } 16359f2e91d9SPaul E. McKenney if (SRCU_SIZING_IS_TORTURE()) 163699659f64SPaul E. McKenney srcu_transition_to_big(ssp); 1637115a1a52SPaul E. McKenney } 1638115a1a52SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_torture_stats_print); 1639115a1a52SPaul E. McKenney 16401f4f6da1SPaul E. McKenney static int __init srcu_bootup_announce(void) 16411f4f6da1SPaul E. McKenney { 16421f4f6da1SPaul E. McKenney pr_info("Hierarchical SRCU implementation.\n"); 16430c8e0e3cSPaul E. McKenney if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF) 16440c8e0e3cSPaul E. McKenney pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff); 16451f4f6da1SPaul E. McKenney return 0; 16461f4f6da1SPaul E. McKenney } 16471f4f6da1SPaul E. McKenney early_initcall(srcu_bootup_announce); 1648e0fcba9aSPaul E. McKenney 1649e0fcba9aSPaul E. McKenney void __init srcu_init(void) 1650e0fcba9aSPaul E. McKenney { 1651aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1652e0fcba9aSPaul E. McKenney 1653a57ffb3cSPaul E. McKenney /* Decide on srcu_struct-size strategy. */ 1654a57ffb3cSPaul E. McKenney if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) { 1655a57ffb3cSPaul E. McKenney if (nr_cpu_ids >= big_cpu_lim) { 1656a57ffb3cSPaul E. McKenney convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention. 1657a57ffb3cSPaul E. McKenney pr_info("%s: Setting srcu_struct sizes to big.\n", __func__); 1658a57ffb3cSPaul E. McKenney } else { 1659a57ffb3cSPaul E. McKenney convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND; 1660a57ffb3cSPaul E. McKenney pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__); 1661a57ffb3cSPaul E. McKenney } 1662a57ffb3cSPaul E. McKenney } 1663a57ffb3cSPaul E. McKenney 16648e9c01c7SFrederic Weisbecker /* 16658e9c01c7SFrederic Weisbecker * Once that is set, call_srcu() can follow the normal path and 16668e9c01c7SFrederic Weisbecker * queue delayed work. This must follow RCU workqueues creation 16678e9c01c7SFrederic Weisbecker * and timers initialization. 16688e9c01c7SFrederic Weisbecker */ 1669e0fcba9aSPaul E. McKenney srcu_init_done = true; 1670e0fcba9aSPaul E. McKenney while (!list_empty(&srcu_boot_list)) { 1671aacb5d91SPaul E. McKenney ssp = list_first_entry(&srcu_boot_list, struct srcu_struct, 16724e6ea4efSPaul E. McKenney work.work.entry); 1673aacb5d91SPaul E. McKenney list_del_init(&ssp->work.work.entry); 1674a57ffb3cSPaul E. McKenney if (SRCU_SIZING_IS(SRCU_SIZING_INIT) && ssp->srcu_size_state == SRCU_SIZE_SMALL) 1675a57ffb3cSPaul E. McKenney ssp->srcu_size_state = SRCU_SIZE_ALLOC; 1676aacb5d91SPaul E. McKenney queue_work(rcu_gp_wq, &ssp->work.work); 1677e0fcba9aSPaul E. McKenney } 1678e0fcba9aSPaul E. McKenney } 1679fe15b50cSPaul E. McKenney 1680fe15b50cSPaul E. McKenney #ifdef CONFIG_MODULES 1681fe15b50cSPaul E. McKenney 1682fe15b50cSPaul E. McKenney /* Initialize any global-scope srcu_struct structures used by this module. */ 1683fe15b50cSPaul E. McKenney static int srcu_module_coming(struct module *mod) 1684fe15b50cSPaul E. McKenney { 1685fe15b50cSPaul E. McKenney int i; 1686fe15b50cSPaul E. McKenney struct srcu_struct **sspp = mod->srcu_struct_ptrs; 1687fe15b50cSPaul E. McKenney int ret; 1688fe15b50cSPaul E. McKenney 1689fe15b50cSPaul E. McKenney for (i = 0; i < mod->num_srcu_structs; i++) { 1690fe15b50cSPaul E. McKenney ret = init_srcu_struct(*(sspp++)); 1691fe15b50cSPaul E. McKenney if (WARN_ON_ONCE(ret)) 1692fe15b50cSPaul E. McKenney return ret; 1693fe15b50cSPaul E. McKenney } 1694fe15b50cSPaul E. McKenney return 0; 1695fe15b50cSPaul E. McKenney } 1696fe15b50cSPaul E. McKenney 1697fe15b50cSPaul E. McKenney /* Clean up any global-scope srcu_struct structures used by this module. */ 1698fe15b50cSPaul E. McKenney static void srcu_module_going(struct module *mod) 1699fe15b50cSPaul E. McKenney { 1700fe15b50cSPaul E. McKenney int i; 1701fe15b50cSPaul E. McKenney struct srcu_struct **sspp = mod->srcu_struct_ptrs; 1702fe15b50cSPaul E. McKenney 1703fe15b50cSPaul E. McKenney for (i = 0; i < mod->num_srcu_structs; i++) 1704fe15b50cSPaul E. McKenney cleanup_srcu_struct(*(sspp++)); 1705fe15b50cSPaul E. McKenney } 1706fe15b50cSPaul E. McKenney 1707fe15b50cSPaul E. McKenney /* Handle one module, either coming or going. */ 1708fe15b50cSPaul E. McKenney static int srcu_module_notify(struct notifier_block *self, 1709fe15b50cSPaul E. McKenney unsigned long val, void *data) 1710fe15b50cSPaul E. McKenney { 1711fe15b50cSPaul E. McKenney struct module *mod = data; 1712fe15b50cSPaul E. McKenney int ret = 0; 1713fe15b50cSPaul E. McKenney 1714fe15b50cSPaul E. McKenney switch (val) { 1715fe15b50cSPaul E. McKenney case MODULE_STATE_COMING: 1716fe15b50cSPaul E. McKenney ret = srcu_module_coming(mod); 1717fe15b50cSPaul E. McKenney break; 1718fe15b50cSPaul E. McKenney case MODULE_STATE_GOING: 1719fe15b50cSPaul E. McKenney srcu_module_going(mod); 1720fe15b50cSPaul E. McKenney break; 1721fe15b50cSPaul E. McKenney default: 1722fe15b50cSPaul E. McKenney break; 1723fe15b50cSPaul E. McKenney } 1724fe15b50cSPaul E. McKenney return ret; 1725fe15b50cSPaul E. McKenney } 1726fe15b50cSPaul E. McKenney 1727fe15b50cSPaul E. McKenney static struct notifier_block srcu_module_nb = { 1728fe15b50cSPaul E. McKenney .notifier_call = srcu_module_notify, 1729fe15b50cSPaul E. McKenney .priority = 0, 1730fe15b50cSPaul E. McKenney }; 1731fe15b50cSPaul E. McKenney 1732fe15b50cSPaul E. McKenney static __init int init_srcu_module_notifier(void) 1733fe15b50cSPaul E. McKenney { 1734fe15b50cSPaul E. McKenney int ret; 1735fe15b50cSPaul E. McKenney 1736fe15b50cSPaul E. McKenney ret = register_module_notifier(&srcu_module_nb); 1737fe15b50cSPaul E. McKenney if (ret) 1738fe15b50cSPaul E. McKenney pr_warn("Failed to register srcu module notifier\n"); 1739fe15b50cSPaul E. McKenney return ret; 1740fe15b50cSPaul E. McKenney } 1741fe15b50cSPaul E. McKenney late_initcall(init_srcu_module_notifier); 1742fe15b50cSPaul E. McKenney 1743fe15b50cSPaul E. McKenney #endif /* #ifdef CONFIG_MODULES */ 1744