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 /* 333c2445d38SPaul E. McKenney * Check to see if the just-encountered contention event justifies 334c2445d38SPaul E. McKenney * a transition to SRCU_SIZE_BIG. 3359f2e91d9SPaul E. McKenney */ 336c2445d38SPaul 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 /* 353c2445d38SPaul E. McKenney * Acquire the specified srcu_data structure's ->lock, but check for 354c2445d38SPaul E. McKenney * excessive contention, which results in initiation of a transition 355c2445d38SPaul E. McKenney * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 356c2445d38SPaul E. McKenney * parameter permits this. 357c2445d38SPaul E. McKenney */ 358c2445d38SPaul E. McKenney static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags) 359c2445d38SPaul E. McKenney { 360c2445d38SPaul E. McKenney struct srcu_struct *ssp = sdp->ssp; 361c2445d38SPaul E. McKenney 362c2445d38SPaul E. McKenney if (spin_trylock_irqsave_rcu_node(sdp, *flags)) 363c2445d38SPaul E. McKenney return; 364c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, *flags); 365c2445d38SPaul E. McKenney spin_lock_irqsave_check_contention(ssp); 366c2445d38SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, *flags); 367c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(sdp, *flags); 368c2445d38SPaul E. McKenney } 369c2445d38SPaul E. McKenney 370c2445d38SPaul E. McKenney /* 371c2445d38SPaul E. McKenney * Acquire the specified srcu_struct structure's ->lock, but check for 372c2445d38SPaul E. McKenney * excessive contention, which results in initiation of a transition 373c2445d38SPaul E. McKenney * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 374c2445d38SPaul E. McKenney * parameter permits this. 375c2445d38SPaul E. McKenney */ 376c2445d38SPaul E. McKenney static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags) 377c2445d38SPaul E. McKenney { 378c2445d38SPaul E. McKenney if (spin_trylock_irqsave_rcu_node(ssp, *flags)) 379c2445d38SPaul E. McKenney return; 380c2445d38SPaul E. McKenney spin_lock_irqsave_rcu_node(ssp, *flags); 381c2445d38SPaul E. McKenney spin_lock_irqsave_check_contention(ssp); 382c2445d38SPaul E. McKenney } 383c2445d38SPaul E. McKenney 384c2445d38SPaul 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 514*282d8998SPaul E. McKenney #define SRCU_INTERVAL 1 // Base delay if no expedited GPs pending. 515*282d8998SPaul E. McKenney #define SRCU_MAX_INTERVAL 10 // Maximum incremental delay from slow readers. 516*282d8998SPaul E. McKenney #define SRCU_MAX_NODELAY_PHASE 1 // Maximum per-GP-phase consecutive no-delay instances. 517*282d8998SPaul E. McKenney #define SRCU_MAX_NODELAY 100 // Maximum consecutive no-delay instances. 518dad81a20SPaul E. McKenney 5191e9a038bSPaul E. McKenney /* 5201e9a038bSPaul E. McKenney * Return grace-period delay, zero if there are expedited grace 5211e9a038bSPaul E. McKenney * periods pending, SRCU_INTERVAL otherwise. 5221e9a038bSPaul E. McKenney */ 523aacb5d91SPaul E. McKenney static unsigned long srcu_get_delay(struct srcu_struct *ssp) 5241e9a038bSPaul E. McKenney { 525*282d8998SPaul E. McKenney unsigned long jbase = SRCU_INTERVAL; 526*282d8998SPaul E. McKenney 527cbdc98e9SPaul E. McKenney if (ULONG_CMP_LT(READ_ONCE(ssp->srcu_gp_seq), READ_ONCE(ssp->srcu_gp_seq_needed_exp))) 528*282d8998SPaul E. McKenney jbase = 0; 529*282d8998SPaul E. McKenney if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq))) 530*282d8998SPaul E. McKenney jbase += jiffies - READ_ONCE(ssp->srcu_gp_start); 531*282d8998SPaul E. McKenney if (!jbase) { 532*282d8998SPaul E. McKenney WRITE_ONCE(ssp->srcu_n_exp_nodelay, READ_ONCE(ssp->srcu_n_exp_nodelay) + 1); 533*282d8998SPaul E. McKenney if (READ_ONCE(ssp->srcu_n_exp_nodelay) > SRCU_MAX_NODELAY_PHASE) 534*282d8998SPaul E. McKenney jbase = 1; 535*282d8998SPaul E. McKenney } 536*282d8998SPaul E. McKenney return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase; 5371e9a038bSPaul E. McKenney } 5381e9a038bSPaul E. McKenney 539f5ad3991SPaul E. McKenney /** 540f5ad3991SPaul E. McKenney * cleanup_srcu_struct - deconstruct a sleep-RCU structure 541f5ad3991SPaul E. McKenney * @ssp: structure to clean up. 542f5ad3991SPaul E. McKenney * 543f5ad3991SPaul E. McKenney * Must invoke this after you are finished using a given srcu_struct that 544f5ad3991SPaul E. McKenney * was initialized via init_srcu_struct(), else you leak memory. 545f5ad3991SPaul E. McKenney */ 546f5ad3991SPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *ssp) 547dad81a20SPaul E. McKenney { 548da915ad5SPaul E. McKenney int cpu; 549da915ad5SPaul E. McKenney 550aacb5d91SPaul E. McKenney if (WARN_ON(!srcu_get_delay(ssp))) 551f7194ac3SPaul E. McKenney return; /* Just leak it! */ 552aacb5d91SPaul E. McKenney if (WARN_ON(srcu_readers_active(ssp))) 553f7194ac3SPaul E. McKenney return; /* Just leak it! */ 554aacb5d91SPaul E. McKenney flush_delayed_work(&ssp->work); 555e81baf4cSSebastian Andrzej Siewior for_each_possible_cpu(cpu) { 556e81baf4cSSebastian Andrzej Siewior struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 557e81baf4cSSebastian Andrzej Siewior 558e81baf4cSSebastian Andrzej Siewior del_timer_sync(&sdp->delay_work); 559e81baf4cSSebastian Andrzej Siewior flush_work(&sdp->work); 5605cdfd174SPaul E. McKenney if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist))) 5615cdfd174SPaul E. McKenney return; /* Forgot srcu_barrier(), so just leak it! */ 562f7194ac3SPaul E. McKenney } 563aacb5d91SPaul E. McKenney if (WARN_ON(rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) != SRCU_STATE_IDLE) || 5648ed00760SPaul E. McKenney WARN_ON(rcu_seq_current(&ssp->srcu_gp_seq) != ssp->srcu_gp_seq_needed) || 565aacb5d91SPaul E. McKenney WARN_ON(srcu_readers_active(ssp))) { 5668ed00760SPaul E. McKenney pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n", 5678ed00760SPaul E. McKenney __func__, ssp, rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)), 5688ed00760SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq), ssp->srcu_gp_seq_needed); 569dad81a20SPaul E. McKenney return; /* Caller forgot to stop doing call_srcu()? */ 570dad81a20SPaul E. McKenney } 57146470cf8SPaul E. McKenney if (!ssp->sda_is_static) { 572aacb5d91SPaul E. McKenney free_percpu(ssp->sda); 573aacb5d91SPaul E. McKenney ssp->sda = NULL; 57446470cf8SPaul E. McKenney } 5752ec30311SPaul E. McKenney kfree(ssp->node); 5762ec30311SPaul E. McKenney ssp->node = NULL; 577994f7068SPaul E. McKenney ssp->srcu_size_state = SRCU_SIZE_SMALL; 578dad81a20SPaul E. McKenney } 579f5ad3991SPaul E. McKenney EXPORT_SYMBOL_GPL(cleanup_srcu_struct); 580dad81a20SPaul E. McKenney 581dad81a20SPaul E. McKenney /* 582dad81a20SPaul E. McKenney * Counts the new reader in the appropriate per-CPU element of the 583cdf7abc4SPaolo Bonzini * srcu_struct. 584dad81a20SPaul E. McKenney * Returns an index that must be passed to the matching srcu_read_unlock(). 585dad81a20SPaul E. McKenney */ 586aacb5d91SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *ssp) 587dad81a20SPaul E. McKenney { 588dad81a20SPaul E. McKenney int idx; 589dad81a20SPaul E. McKenney 590aacb5d91SPaul E. McKenney idx = READ_ONCE(ssp->srcu_idx) & 0x1; 591aacb5d91SPaul E. McKenney this_cpu_inc(ssp->sda->srcu_lock_count[idx]); 592dad81a20SPaul E. McKenney smp_mb(); /* B */ /* Avoid leaking the critical section. */ 593dad81a20SPaul E. McKenney return idx; 594dad81a20SPaul E. McKenney } 595dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock); 596dad81a20SPaul E. McKenney 597dad81a20SPaul E. McKenney /* 598dad81a20SPaul E. McKenney * Removes the count for the old reader from the appropriate per-CPU 599dad81a20SPaul E. McKenney * element of the srcu_struct. Note that this may well be a different 600dad81a20SPaul E. McKenney * CPU than that which was incremented by the corresponding srcu_read_lock(). 601dad81a20SPaul E. McKenney */ 602aacb5d91SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *ssp, int idx) 603dad81a20SPaul E. McKenney { 604dad81a20SPaul E. McKenney smp_mb(); /* C */ /* Avoid leaking the critical section. */ 605aacb5d91SPaul E. McKenney this_cpu_inc(ssp->sda->srcu_unlock_count[idx]); 606dad81a20SPaul E. McKenney } 607dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock); 608dad81a20SPaul E. McKenney 609dad81a20SPaul E. McKenney /* 610dad81a20SPaul E. McKenney * We use an adaptive strategy for synchronize_srcu() and especially for 611dad81a20SPaul E. McKenney * synchronize_srcu_expedited(). We spin for a fixed time period 612dad81a20SPaul E. McKenney * (defined below) to allow SRCU readers to exit their read-side critical 613dad81a20SPaul E. McKenney * sections. If there are still some readers after a few microseconds, 614dad81a20SPaul E. McKenney * we repeatedly block for 1-millisecond time periods. 615dad81a20SPaul E. McKenney */ 616dad81a20SPaul E. McKenney #define SRCU_RETRY_CHECK_DELAY 5 617dad81a20SPaul E. McKenney 618dad81a20SPaul E. McKenney /* 619dad81a20SPaul E. McKenney * Start an SRCU grace period. 620dad81a20SPaul E. McKenney */ 621aacb5d91SPaul E. McKenney static void srcu_gp_start(struct srcu_struct *ssp) 622dad81a20SPaul E. McKenney { 623aacb5d91SPaul E. McKenney struct srcu_data *sdp = this_cpu_ptr(ssp->sda); 624dad81a20SPaul E. McKenney int state; 625dad81a20SPaul E. McKenney 626994f7068SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 627994f7068SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, 0); 628994f7068SPaul E. McKenney else 629994f7068SPaul E. McKenney sdp = this_cpu_ptr(ssp->sda); 630aacb5d91SPaul E. McKenney lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock)); 631aacb5d91SPaul E. McKenney WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)); 632eb4c2382SDennis Krein spin_lock_rcu_node(sdp); /* Interrupts already disabled. */ 633da915ad5SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 634aacb5d91SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 635da915ad5SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, 636aacb5d91SPaul E. McKenney rcu_seq_snap(&ssp->srcu_gp_seq)); 637eb4c2382SDennis Krein spin_unlock_rcu_node(sdp); /* Interrupts remain disabled. */ 638*282d8998SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_start, jiffies); 639*282d8998SPaul E. McKenney WRITE_ONCE(ssp->srcu_n_exp_nodelay, 0); 6402da4b2a7SPaul E. McKenney smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */ 641aacb5d91SPaul E. McKenney rcu_seq_start(&ssp->srcu_gp_seq); 64271042606SPaul E. McKenney state = rcu_seq_state(ssp->srcu_gp_seq); 643dad81a20SPaul E. McKenney WARN_ON_ONCE(state != SRCU_STATE_SCAN1); 644dad81a20SPaul E. McKenney } 645dad81a20SPaul E. McKenney 646da915ad5SPaul E. McKenney 647e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t) 648da915ad5SPaul E. McKenney { 649e81baf4cSSebastian Andrzej Siewior struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work); 650e81baf4cSSebastian Andrzej Siewior 651e81baf4cSSebastian Andrzej Siewior queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 652da915ad5SPaul E. McKenney } 653da915ad5SPaul E. McKenney 654e81baf4cSSebastian Andrzej Siewior static void srcu_queue_delayed_work_on(struct srcu_data *sdp, 655da915ad5SPaul E. McKenney unsigned long delay) 656da915ad5SPaul E. McKenney { 657e81baf4cSSebastian Andrzej Siewior if (!delay) { 658e81baf4cSSebastian Andrzej Siewior queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 659e81baf4cSSebastian Andrzej Siewior return; 660e81baf4cSSebastian Andrzej Siewior } 661da915ad5SPaul E. McKenney 662e81baf4cSSebastian Andrzej Siewior timer_reduce(&sdp->delay_work, jiffies + delay); 663da915ad5SPaul E. McKenney } 664da915ad5SPaul E. McKenney 665da915ad5SPaul E. McKenney /* 666da915ad5SPaul E. McKenney * Schedule callback invocation for the specified srcu_data structure, 667da915ad5SPaul E. McKenney * if possible, on the corresponding CPU. 668da915ad5SPaul E. McKenney */ 669da915ad5SPaul E. McKenney static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay) 670da915ad5SPaul E. McKenney { 671e81baf4cSSebastian Andrzej Siewior srcu_queue_delayed_work_on(sdp, delay); 672da915ad5SPaul E. McKenney } 673da915ad5SPaul E. McKenney 674da915ad5SPaul E. McKenney /* 675da915ad5SPaul E. McKenney * Schedule callback invocation for all srcu_data structures associated 676c7e88067SPaul E. McKenney * with the specified srcu_node structure that have callbacks for the 677c7e88067SPaul E. McKenney * just-completed grace period, the one corresponding to idx. If possible, 678c7e88067SPaul E. McKenney * schedule this invocation on the corresponding CPUs. 679da915ad5SPaul E. McKenney */ 680aacb5d91SPaul E. McKenney static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp, 6811e9a038bSPaul E. McKenney unsigned long mask, unsigned long delay) 682da915ad5SPaul E. McKenney { 683da915ad5SPaul E. McKenney int cpu; 684da915ad5SPaul E. McKenney 685c7e88067SPaul E. McKenney for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) { 686c7e88067SPaul E. McKenney if (!(mask & (1 << (cpu - snp->grplo)))) 687c7e88067SPaul E. McKenney continue; 688aacb5d91SPaul E. McKenney srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay); 689da915ad5SPaul E. McKenney } 690c7e88067SPaul E. McKenney } 691da915ad5SPaul E. McKenney 692da915ad5SPaul E. McKenney /* 693da915ad5SPaul E. McKenney * Note the end of an SRCU grace period. Initiates callback invocation 694da915ad5SPaul E. McKenney * and starts a new grace period if needed. 695da915ad5SPaul E. McKenney * 696da915ad5SPaul E. McKenney * The ->srcu_cb_mutex acquisition does not protect any data, but 697da915ad5SPaul E. McKenney * instead prevents more than one grace period from starting while we 698da915ad5SPaul E. McKenney * are initiating callback invocation. This allows the ->srcu_have_cbs[] 699da915ad5SPaul E. McKenney * array to have a finite number of elements. 700da915ad5SPaul E. McKenney */ 701aacb5d91SPaul E. McKenney static void srcu_gp_end(struct srcu_struct *ssp) 702da915ad5SPaul E. McKenney { 7031e9a038bSPaul E. McKenney unsigned long cbdelay; 704da915ad5SPaul E. McKenney bool cbs; 7058ddbd883SIldar Ismagilov bool last_lvl; 706c350c008SPaul E. McKenney int cpu; 707c350c008SPaul E. McKenney unsigned long flags; 708da915ad5SPaul E. McKenney unsigned long gpseq; 709da915ad5SPaul E. McKenney int idx; 710c7e88067SPaul E. McKenney unsigned long mask; 711c350c008SPaul E. McKenney struct srcu_data *sdp; 712cbdc98e9SPaul E. McKenney unsigned long sgsne; 713da915ad5SPaul E. McKenney struct srcu_node *snp; 714e2f63836SPaul E. McKenney int ss_state; 715da915ad5SPaul E. McKenney 716da915ad5SPaul E. McKenney /* Prevent more than one additional grace period. */ 717aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_cb_mutex); 718da915ad5SPaul E. McKenney 719da915ad5SPaul E. McKenney /* End the current grace period. */ 720aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 721aacb5d91SPaul E. McKenney idx = rcu_seq_state(ssp->srcu_gp_seq); 722da915ad5SPaul E. McKenney WARN_ON_ONCE(idx != SRCU_STATE_SCAN2); 723*282d8998SPaul E. McKenney cbdelay = !!srcu_get_delay(ssp); 724844a378dSPaul E. McKenney WRITE_ONCE(ssp->srcu_last_gp_end, ktime_get_mono_fast_ns()); 725aacb5d91SPaul E. McKenney rcu_seq_end(&ssp->srcu_gp_seq); 726aacb5d91SPaul E. McKenney gpseq = rcu_seq_current(&ssp->srcu_gp_seq); 727aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, gpseq)) 7288c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, gpseq); 729aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 730aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 731da915ad5SPaul E. McKenney /* A new grace period can start at this point. But only one. */ 732da915ad5SPaul E. McKenney 733da915ad5SPaul E. McKenney /* Initiate callback invocation as needed. */ 734c69a00a1SPaul E. McKenney ss_state = smp_load_acquire(&ssp->srcu_size_state); 735c69a00a1SPaul E. McKenney if (ss_state < SRCU_SIZE_WAIT_BARRIER) { 736994f7068SPaul E. McKenney srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, 0), cbdelay); 737994f7068SPaul E. McKenney } else { 738da915ad5SPaul E. McKenney idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs); 739aacb5d91SPaul E. McKenney srcu_for_each_node_breadth_first(ssp, snp) { 740d6331980SPaul E. McKenney spin_lock_irq_rcu_node(snp); 741da915ad5SPaul E. McKenney cbs = false; 742aacb5d91SPaul E. McKenney last_lvl = snp >= ssp->level[rcu_num_lvls - 1]; 7438ddbd883SIldar Ismagilov if (last_lvl) 744c69a00a1SPaul E. McKenney cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq; 745da915ad5SPaul E. McKenney snp->srcu_have_cbs[idx] = gpseq; 746da915ad5SPaul E. McKenney rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1); 747cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 748cbdc98e9SPaul E. McKenney if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq)) 7497ff8b450SPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq); 750c69a00a1SPaul E. McKenney if (ss_state < SRCU_SIZE_BIG) 751c69a00a1SPaul E. McKenney mask = ~0; 752c69a00a1SPaul E. McKenney else 753c7e88067SPaul E. McKenney mask = snp->srcu_data_have_cbs[idx]; 754c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] = 0; 755d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(snp); 756a3883df3SPaul E. McKenney if (cbs) 757aacb5d91SPaul E. McKenney srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay); 758994f7068SPaul E. McKenney } 759994f7068SPaul E. McKenney } 760c350c008SPaul E. McKenney 761c350c008SPaul E. McKenney /* Occasionally prevent srcu_data counter wrap. */ 762994f7068SPaul E. McKenney if (!(gpseq & counter_wrap_check)) 763994f7068SPaul E. McKenney for_each_possible_cpu(cpu) { 764aacb5d91SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 765d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(sdp, flags); 766994f7068SPaul E. McKenney if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100)) 767c350c008SPaul E. McKenney sdp->srcu_gp_seq_needed = gpseq; 768994f7068SPaul E. McKenney if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100)) 769a35d13ecSIldar Ismagilov sdp->srcu_gp_seq_needed_exp = gpseq; 770d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(sdp, flags); 771c350c008SPaul E. McKenney } 772da915ad5SPaul E. McKenney 773da915ad5SPaul E. McKenney /* Callback initiation done, allow grace periods after next. */ 774aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_cb_mutex); 775da915ad5SPaul E. McKenney 776da915ad5SPaul E. McKenney /* Start a new grace period if needed. */ 777aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 778aacb5d91SPaul E. McKenney gpseq = rcu_seq_current(&ssp->srcu_gp_seq); 779da915ad5SPaul E. McKenney if (!rcu_seq_state(gpseq) && 780aacb5d91SPaul E. McKenney ULONG_CMP_LT(gpseq, ssp->srcu_gp_seq_needed)) { 781aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 782aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 783aacb5d91SPaul E. McKenney srcu_reschedule(ssp, 0); 784da915ad5SPaul E. McKenney } else { 785aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 786da915ad5SPaul E. McKenney } 787e2f63836SPaul E. McKenney 788e2f63836SPaul E. McKenney /* Transition to big if needed. */ 789e2f63836SPaul E. McKenney if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) { 790e2f63836SPaul E. McKenney if (ss_state == SRCU_SIZE_ALLOC) 791c69a00a1SPaul E. McKenney init_srcu_struct_nodes(ssp, GFP_KERNEL); 792e2f63836SPaul E. McKenney else 793e2f63836SPaul E. McKenney smp_store_release(&ssp->srcu_size_state, ss_state + 1); 794e2f63836SPaul E. McKenney } 795da915ad5SPaul E. McKenney } 796da915ad5SPaul E. McKenney 797da915ad5SPaul E. McKenney /* 7981e9a038bSPaul E. McKenney * Funnel-locking scheme to scalably mediate many concurrent expedited 7991e9a038bSPaul E. McKenney * grace-period requests. This function is invoked for the first known 8001e9a038bSPaul E. McKenney * expedited request for a grace period that has already been requested, 8011e9a038bSPaul E. McKenney * but without expediting. To start a completely new grace period, 8021e9a038bSPaul E. McKenney * whether expedited or not, use srcu_funnel_gp_start() instead. 8031e9a038bSPaul E. McKenney */ 804aacb5d91SPaul E. McKenney static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp, 8051e9a038bSPaul E. McKenney unsigned long s) 8061e9a038bSPaul E. McKenney { 8071e9a038bSPaul E. McKenney unsigned long flags; 808cbdc98e9SPaul E. McKenney unsigned long sgsne; 8091e9a038bSPaul E. McKenney 810994f7068SPaul E. McKenney if (snp) 8111e9a038bSPaul E. McKenney for (; snp != NULL; snp = snp->srcu_parent) { 812cbdc98e9SPaul E. McKenney sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp); 813aacb5d91SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_gp_seq, s) || 814cbdc98e9SPaul E. McKenney (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s))) 8151e9a038bSPaul E. McKenney return; 816d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(snp, flags); 817cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 818cbdc98e9SPaul E. McKenney if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) { 819d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8201e9a038bSPaul E. McKenney return; 8211e9a038bSPaul E. McKenney } 8221e9a038bSPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 823d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8241e9a038bSPaul E. McKenney } 8259f2e91d9SPaul E. McKenney spin_lock_irqsave_ssp_contention(ssp, &flags); 826aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s)) 8278c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s); 828aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 8291e9a038bSPaul E. McKenney } 8301e9a038bSPaul E. McKenney 8311e9a038bSPaul E. McKenney /* 832da915ad5SPaul E. McKenney * Funnel-locking scheme to scalably mediate many concurrent grace-period 833da915ad5SPaul E. McKenney * requests. The winner has to do the work of actually starting grace 834da915ad5SPaul E. McKenney * period s. Losers must either ensure that their desired grace-period 835da915ad5SPaul E. McKenney * number is recorded on at least their leaf srcu_node structure, or they 836da915ad5SPaul E. McKenney * must take steps to invoke their own callbacks. 83717294ce6SPaul E. McKenney * 83817294ce6SPaul E. McKenney * Note that this function also does the work of srcu_funnel_exp_start(), 83917294ce6SPaul E. McKenney * in some cases by directly invoking it. 840da915ad5SPaul E. McKenney */ 841aacb5d91SPaul E. McKenney static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp, 8421e9a038bSPaul E. McKenney unsigned long s, bool do_norm) 843da915ad5SPaul E. McKenney { 844da915ad5SPaul E. McKenney unsigned long flags; 845da915ad5SPaul E. McKenney int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs); 846cbdc98e9SPaul E. McKenney unsigned long sgsne; 8477b9e9b58SPaul E. McKenney struct srcu_node *snp; 8480b56f953SNeeraj Upadhyay struct srcu_node *snp_leaf; 849da915ad5SPaul E. McKenney unsigned long snp_seq; 850da915ad5SPaul E. McKenney 8510b56f953SNeeraj Upadhyay /* Ensure that snp node tree is fully initialized before traversing it */ 8520b56f953SNeeraj Upadhyay if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 8530b56f953SNeeraj Upadhyay snp_leaf = NULL; 8540b56f953SNeeraj Upadhyay else 8550b56f953SNeeraj Upadhyay snp_leaf = sdp->mynode; 8560b56f953SNeeraj Upadhyay 857994f7068SPaul E. McKenney if (snp_leaf) 858da915ad5SPaul E. McKenney /* Each pass through the loop does one level of the srcu_node tree. */ 8597b9e9b58SPaul E. McKenney for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) { 8607b9e9b58SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_gp_seq, s) && snp != snp_leaf) 861da915ad5SPaul E. McKenney return; /* GP already done and CBs recorded. */ 862d6331980SPaul E. McKenney spin_lock_irqsave_rcu_node(snp, flags); 863da915ad5SPaul E. McKenney snp_seq = snp->srcu_have_cbs[idx]; 864cbdc98e9SPaul E. McKenney if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) { 8657b9e9b58SPaul E. McKenney if (snp == snp_leaf && snp_seq == s) 866c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 867d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 8687b9e9b58SPaul E. McKenney if (snp == snp_leaf && snp_seq != s) { 869aeb9b39bSPaul E. McKenney srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0); 8701e9a038bSPaul E. McKenney return; 871da915ad5SPaul E. McKenney } 8721e9a038bSPaul E. McKenney if (!do_norm) 873aacb5d91SPaul E. McKenney srcu_funnel_exp_start(ssp, snp, s); 874da915ad5SPaul E. McKenney return; 875da915ad5SPaul E. McKenney } 876da915ad5SPaul E. McKenney snp->srcu_have_cbs[idx] = s; 8777b9e9b58SPaul E. McKenney if (snp == snp_leaf) 878c7e88067SPaul E. McKenney snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 879cbdc98e9SPaul E. McKenney sgsne = snp->srcu_gp_seq_needed_exp; 880cbdc98e9SPaul E. McKenney if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s))) 8817ff8b450SPaul E. McKenney WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 882d6331980SPaul E. McKenney spin_unlock_irqrestore_rcu_node(snp, flags); 883da915ad5SPaul E. McKenney } 884da915ad5SPaul E. McKenney 885da915ad5SPaul E. McKenney /* Top of tree, must ensure the grace period will be started. */ 8869f2e91d9SPaul E. McKenney spin_lock_irqsave_ssp_contention(ssp, &flags); 887aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed, s)) { 888da915ad5SPaul E. McKenney /* 889da915ad5SPaul E. McKenney * Record need for grace period s. Pair with load 890da915ad5SPaul E. McKenney * acquire setting up for initialization. 891da915ad5SPaul E. McKenney */ 892aacb5d91SPaul E. McKenney smp_store_release(&ssp->srcu_gp_seq_needed, s); /*^^^*/ 893da915ad5SPaul E. McKenney } 894aacb5d91SPaul E. McKenney if (!do_norm && ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s)) 8958c9e0cb3SPaul E. McKenney WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s); 896da915ad5SPaul E. McKenney 897da915ad5SPaul E. McKenney /* If grace period not already done and none in progress, start it. */ 898aacb5d91SPaul E. McKenney if (!rcu_seq_done(&ssp->srcu_gp_seq, s) && 899aacb5d91SPaul E. McKenney rcu_seq_state(ssp->srcu_gp_seq) == SRCU_STATE_IDLE) { 900aacb5d91SPaul E. McKenney WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)); 901aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 902ee5e2448SPaul E. McKenney 903ee5e2448SPaul E. McKenney // And how can that list_add() in the "else" clause 904ee5e2448SPaul E. McKenney // possibly be safe for concurrent execution? Well, 905ee5e2448SPaul E. McKenney // it isn't. And it does not have to be. After all, it 906ee5e2448SPaul E. McKenney // can only be executed during early boot when there is only 907ee5e2448SPaul E. McKenney // the one boot CPU running with interrupts still disabled. 908e0fcba9aSPaul E. McKenney if (likely(srcu_init_done)) 909aacb5d91SPaul E. McKenney queue_delayed_work(rcu_gp_wq, &ssp->work, 910*282d8998SPaul E. McKenney !!srcu_get_delay(ssp)); 911aacb5d91SPaul E. McKenney else if (list_empty(&ssp->work.work.entry)) 912aacb5d91SPaul E. McKenney list_add(&ssp->work.work.entry, &srcu_boot_list); 913da915ad5SPaul E. McKenney } 914aacb5d91SPaul E. McKenney spin_unlock_irqrestore_rcu_node(ssp, flags); 915da915ad5SPaul E. McKenney } 916da915ad5SPaul E. McKenney 917da915ad5SPaul E. McKenney /* 918dad81a20SPaul E. McKenney * Wait until all readers counted by array index idx complete, but 919dad81a20SPaul E. McKenney * loop an additional time if there is an expedited grace period pending. 920da915ad5SPaul E. McKenney * The caller must ensure that ->srcu_idx is not changed while checking. 921dad81a20SPaul E. McKenney */ 922aacb5d91SPaul E. McKenney static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount) 923dad81a20SPaul E. McKenney { 924dad81a20SPaul E. McKenney for (;;) { 925aacb5d91SPaul E. McKenney if (srcu_readers_active_idx_check(ssp, idx)) 926dad81a20SPaul E. McKenney return true; 927aacb5d91SPaul E. McKenney if (--trycount + !srcu_get_delay(ssp) <= 0) 928dad81a20SPaul E. McKenney return false; 929dad81a20SPaul E. McKenney udelay(SRCU_RETRY_CHECK_DELAY); 930dad81a20SPaul E. McKenney } 931dad81a20SPaul E. McKenney } 932dad81a20SPaul E. McKenney 933dad81a20SPaul E. McKenney /* 934da915ad5SPaul E. McKenney * Increment the ->srcu_idx counter so that future SRCU readers will 935da915ad5SPaul E. McKenney * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows 936dad81a20SPaul E. McKenney * us to wait for pre-existing readers in a starvation-free manner. 937dad81a20SPaul E. McKenney */ 938aacb5d91SPaul E. McKenney static void srcu_flip(struct srcu_struct *ssp) 939dad81a20SPaul E. McKenney { 940881ec9d2SPaul E. McKenney /* 941881ec9d2SPaul E. McKenney * Ensure that if this updater saw a given reader's increment 942881ec9d2SPaul E. McKenney * from __srcu_read_lock(), that reader was using an old value 943881ec9d2SPaul E. McKenney * of ->srcu_idx. Also ensure that if a given reader sees the 944881ec9d2SPaul E. McKenney * new value of ->srcu_idx, this updater's earlier scans cannot 945881ec9d2SPaul E. McKenney * have seen that reader's increments (which is OK, because this 946881ec9d2SPaul E. McKenney * grace period need not wait on that reader). 947881ec9d2SPaul E. McKenney */ 948881ec9d2SPaul E. McKenney smp_mb(); /* E */ /* Pairs with B and C. */ 949881ec9d2SPaul E. McKenney 950aacb5d91SPaul E. McKenney WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); 951dad81a20SPaul E. McKenney 952dad81a20SPaul E. McKenney /* 953dad81a20SPaul E. McKenney * Ensure that if the updater misses an __srcu_read_unlock() 954dad81a20SPaul E. McKenney * increment, that task's next __srcu_read_lock() will see the 955dad81a20SPaul E. McKenney * above counter update. Note that both this memory barrier 956dad81a20SPaul E. McKenney * and the one in srcu_readers_active_idx_check() provide the 957dad81a20SPaul E. McKenney * guarantee for __srcu_read_lock(). 958dad81a20SPaul E. McKenney */ 959dad81a20SPaul E. McKenney smp_mb(); /* D */ /* Pairs with C. */ 960dad81a20SPaul E. McKenney } 961dad81a20SPaul E. McKenney 962dad81a20SPaul E. McKenney /* 9632da4b2a7SPaul E. McKenney * If SRCU is likely idle, return true, otherwise return false. 9642da4b2a7SPaul E. McKenney * 9652da4b2a7SPaul E. McKenney * Note that it is OK for several current from-idle requests for a new 9662da4b2a7SPaul E. McKenney * grace period from idle to specify expediting because they will all end 9672da4b2a7SPaul E. McKenney * up requesting the same grace period anyhow. So no loss. 9682da4b2a7SPaul E. McKenney * 9692da4b2a7SPaul E. McKenney * Note also that if any CPU (including the current one) is still invoking 9702da4b2a7SPaul E. McKenney * callbacks, this function will nevertheless say "idle". This is not 9712da4b2a7SPaul E. McKenney * ideal, but the overhead of checking all CPUs' callback lists is even 9722da4b2a7SPaul E. McKenney * less ideal, especially on large systems. Furthermore, the wakeup 9732da4b2a7SPaul E. McKenney * can happen before the callback is fully removed, so we have no choice 9742da4b2a7SPaul E. McKenney * but to accept this type of error. 9752da4b2a7SPaul E. McKenney * 9762da4b2a7SPaul E. McKenney * This function is also subject to counter-wrap errors, but let's face 9772da4b2a7SPaul E. McKenney * it, if this function was preempted for enough time for the counters 9782da4b2a7SPaul E. McKenney * to wrap, it really doesn't matter whether or not we expedite the grace 9792da4b2a7SPaul E. McKenney * period. The extra overhead of a needlessly expedited grace period is 9807fef6cffSEthon Paul * negligible when amortized over that time period, and the extra latency 9812da4b2a7SPaul E. McKenney * of a needlessly non-expedited grace period is similarly negligible. 9822da4b2a7SPaul E. McKenney */ 983aacb5d91SPaul E. McKenney static bool srcu_might_be_idle(struct srcu_struct *ssp) 9842da4b2a7SPaul E. McKenney { 98522607d66SPaul E. McKenney unsigned long curseq; 9862da4b2a7SPaul E. McKenney unsigned long flags; 9872da4b2a7SPaul E. McKenney struct srcu_data *sdp; 98822607d66SPaul E. McKenney unsigned long t; 989844a378dSPaul E. McKenney unsigned long tlast; 9902da4b2a7SPaul E. McKenney 991bde50d8fSSebastian Andrzej Siewior check_init_srcu_struct(ssp); 9922da4b2a7SPaul E. McKenney /* If the local srcu_data structure has callbacks, not idle. */ 993bde50d8fSSebastian Andrzej Siewior sdp = raw_cpu_ptr(ssp->sda); 994bde50d8fSSebastian Andrzej Siewior spin_lock_irqsave_rcu_node(sdp, flags); 9952da4b2a7SPaul E. McKenney if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) { 996bde50d8fSSebastian Andrzej Siewior spin_unlock_irqrestore_rcu_node(sdp, flags); 9972da4b2a7SPaul E. McKenney return false; /* Callbacks already present, so not idle. */ 9982da4b2a7SPaul E. McKenney } 999bde50d8fSSebastian Andrzej Siewior spin_unlock_irqrestore_rcu_node(sdp, flags); 10002da4b2a7SPaul E. McKenney 10012da4b2a7SPaul E. McKenney /* 1002a616aec9SIngo Molnar * No local callbacks, so probabilistically probe global state. 10032da4b2a7SPaul E. McKenney * Exact information would require acquiring locks, which would 1004a616aec9SIngo Molnar * kill scalability, hence the probabilistic nature of the probe. 10052da4b2a7SPaul E. McKenney */ 100622607d66SPaul E. McKenney 100722607d66SPaul E. McKenney /* First, see if enough time has passed since the last GP. */ 100822607d66SPaul E. McKenney t = ktime_get_mono_fast_ns(); 1009844a378dSPaul E. McKenney tlast = READ_ONCE(ssp->srcu_last_gp_end); 101022607d66SPaul E. McKenney if (exp_holdoff == 0 || 1011844a378dSPaul E. McKenney time_in_range_open(t, tlast, tlast + exp_holdoff)) 101222607d66SPaul E. McKenney return false; /* Too soon after last GP. */ 101322607d66SPaul E. McKenney 101422607d66SPaul E. McKenney /* Next, check for probable idleness. */ 1015aacb5d91SPaul E. McKenney curseq = rcu_seq_current(&ssp->srcu_gp_seq); 10162da4b2a7SPaul E. McKenney smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */ 1017aacb5d91SPaul E. McKenney if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_gp_seq_needed))) 10182da4b2a7SPaul E. McKenney return false; /* Grace period in progress, so not idle. */ 10192da4b2a7SPaul E. McKenney smp_mb(); /* Order ->srcu_gp_seq with prior access. */ 1020aacb5d91SPaul E. McKenney if (curseq != rcu_seq_current(&ssp->srcu_gp_seq)) 10212da4b2a7SPaul E. McKenney return false; /* GP # changed, so not idle. */ 10222da4b2a7SPaul E. McKenney return true; /* With reasonable probability, idle! */ 10232da4b2a7SPaul E. McKenney } 10242da4b2a7SPaul E. McKenney 10252da4b2a7SPaul E. McKenney /* 1026a602538eSPaul E. McKenney * SRCU callback function to leak a callback. 1027a602538eSPaul E. McKenney */ 1028a602538eSPaul E. McKenney static void srcu_leak_callback(struct rcu_head *rhp) 1029a602538eSPaul E. McKenney { 1030a602538eSPaul E. McKenney } 1031a602538eSPaul E. McKenney 1032a602538eSPaul E. McKenney /* 103329d2bb94SPaul E. McKenney * Start an SRCU grace period, and also queue the callback if non-NULL. 103429d2bb94SPaul E. McKenney */ 10355358c9faSPaul E. McKenney static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp, 10365358c9faSPaul E. McKenney struct rcu_head *rhp, bool do_norm) 103729d2bb94SPaul E. McKenney { 103829d2bb94SPaul E. McKenney unsigned long flags; 103929d2bb94SPaul E. McKenney int idx; 104029d2bb94SPaul E. McKenney bool needexp = false; 104129d2bb94SPaul E. McKenney bool needgp = false; 104229d2bb94SPaul E. McKenney unsigned long s; 104329d2bb94SPaul E. McKenney struct srcu_data *sdp; 10440b56f953SNeeraj Upadhyay struct srcu_node *sdp_mynode; 10450b56f953SNeeraj Upadhyay int ss_state; 104629d2bb94SPaul E. McKenney 10475358c9faSPaul E. McKenney check_init_srcu_struct(ssp); 104829d2bb94SPaul E. McKenney idx = srcu_read_lock(ssp); 10490b56f953SNeeraj Upadhyay ss_state = smp_load_acquire(&ssp->srcu_size_state); 10500b56f953SNeeraj Upadhyay if (ss_state < SRCU_SIZE_WAIT_CALL) 1051994f7068SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, 0); 1052994f7068SPaul E. McKenney else 105329d2bb94SPaul E. McKenney sdp = raw_cpu_ptr(ssp->sda); 1054c2445d38SPaul E. McKenney spin_lock_irqsave_sdp_contention(sdp, &flags); 10555358c9faSPaul E. McKenney if (rhp) 105629d2bb94SPaul E. McKenney rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp); 105729d2bb94SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 105829d2bb94SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 105929d2bb94SPaul E. McKenney s = rcu_seq_snap(&ssp->srcu_gp_seq); 106029d2bb94SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s); 106129d2bb94SPaul E. McKenney if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) { 106229d2bb94SPaul E. McKenney sdp->srcu_gp_seq_needed = s; 106329d2bb94SPaul E. McKenney needgp = true; 106429d2bb94SPaul E. McKenney } 106529d2bb94SPaul E. McKenney if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) { 106629d2bb94SPaul E. McKenney sdp->srcu_gp_seq_needed_exp = s; 106729d2bb94SPaul E. McKenney needexp = true; 106829d2bb94SPaul E. McKenney } 106929d2bb94SPaul E. McKenney spin_unlock_irqrestore_rcu_node(sdp, flags); 10700b56f953SNeeraj Upadhyay 10710b56f953SNeeraj Upadhyay /* Ensure that snp node tree is fully initialized before traversing it */ 10720b56f953SNeeraj Upadhyay if (ss_state < SRCU_SIZE_WAIT_BARRIER) 10730b56f953SNeeraj Upadhyay sdp_mynode = NULL; 10740b56f953SNeeraj Upadhyay else 10750b56f953SNeeraj Upadhyay sdp_mynode = sdp->mynode; 10760b56f953SNeeraj Upadhyay 107729d2bb94SPaul E. McKenney if (needgp) 107829d2bb94SPaul E. McKenney srcu_funnel_gp_start(ssp, sdp, s, do_norm); 107929d2bb94SPaul E. McKenney else if (needexp) 10800b56f953SNeeraj Upadhyay srcu_funnel_exp_start(ssp, sdp_mynode, s); 108129d2bb94SPaul E. McKenney srcu_read_unlock(ssp, idx); 10825358c9faSPaul E. McKenney return s; 108329d2bb94SPaul E. McKenney } 108429d2bb94SPaul E. McKenney 108529d2bb94SPaul E. McKenney /* 1086da915ad5SPaul E. McKenney * Enqueue an SRCU callback on the srcu_data structure associated with 1087da915ad5SPaul E. McKenney * the current CPU and the specified srcu_struct structure, initiating 1088da915ad5SPaul E. McKenney * grace-period processing if it is not already running. 1089dad81a20SPaul E. McKenney * 1090dad81a20SPaul E. McKenney * Note that all CPUs must agree that the grace period extended beyond 1091dad81a20SPaul E. McKenney * all pre-existing SRCU read-side critical section. On systems with 1092dad81a20SPaul E. McKenney * more than one CPU, this means that when "func()" is invoked, each CPU 1093dad81a20SPaul E. McKenney * is guaranteed to have executed a full memory barrier since the end of 1094dad81a20SPaul E. McKenney * its last corresponding SRCU read-side critical section whose beginning 10955ef98a63SPaul E. McKenney * preceded the call to call_srcu(). It also means that each CPU executing 1096dad81a20SPaul E. McKenney * an SRCU read-side critical section that continues beyond the start of 10975ef98a63SPaul E. McKenney * "func()" must have executed a memory barrier after the call_srcu() 1098dad81a20SPaul E. McKenney * but before the beginning of that SRCU read-side critical section. 1099dad81a20SPaul E. McKenney * Note that these guarantees include CPUs that are offline, idle, or 1100dad81a20SPaul E. McKenney * executing in user mode, as well as CPUs that are executing in the kernel. 1101dad81a20SPaul E. McKenney * 11025ef98a63SPaul E. McKenney * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the 1103dad81a20SPaul E. McKenney * resulting SRCU callback function "func()", then both CPU A and CPU 1104dad81a20SPaul E. McKenney * B are guaranteed to execute a full memory barrier during the time 11055ef98a63SPaul E. McKenney * interval between the call to call_srcu() and the invocation of "func()". 1106dad81a20SPaul E. McKenney * This guarantee applies even if CPU A and CPU B are the same CPU (but 1107dad81a20SPaul E. McKenney * again only if the system has more than one CPU). 1108dad81a20SPaul E. McKenney * 1109dad81a20SPaul E. McKenney * Of course, these guarantees apply only for invocations of call_srcu(), 1110dad81a20SPaul E. McKenney * srcu_read_lock(), and srcu_read_unlock() that are all passed the same 1111dad81a20SPaul E. McKenney * srcu_struct structure. 1112dad81a20SPaul E. McKenney */ 111311b00045SJiang Biao static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 11141e9a038bSPaul E. McKenney rcu_callback_t func, bool do_norm) 1115dad81a20SPaul E. McKenney { 1116a602538eSPaul E. McKenney if (debug_rcu_head_queue(rhp)) { 1117a602538eSPaul E. McKenney /* Probable double call_srcu(), so leak the callback. */ 1118a602538eSPaul E. McKenney WRITE_ONCE(rhp->func, srcu_leak_callback); 1119a602538eSPaul E. McKenney WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n"); 1120a602538eSPaul E. McKenney return; 1121a602538eSPaul E. McKenney } 1122da915ad5SPaul E. McKenney rhp->func = func; 11235358c9faSPaul E. McKenney (void)srcu_gp_start_if_needed(ssp, rhp, do_norm); 11241e9a038bSPaul E. McKenney } 11251e9a038bSPaul E. McKenney 11265a0465e1SPaul E. McKenney /** 11275a0465e1SPaul E. McKenney * call_srcu() - Queue a callback for invocation after an SRCU grace period 1128aacb5d91SPaul E. McKenney * @ssp: srcu_struct in queue the callback 112927fdb35fSPaul E. McKenney * @rhp: structure to be used for queueing the SRCU callback. 11305a0465e1SPaul E. McKenney * @func: function to be invoked after the SRCU grace period 11315a0465e1SPaul E. McKenney * 11325a0465e1SPaul E. McKenney * The callback function will be invoked some time after a full SRCU 11335a0465e1SPaul E. McKenney * grace period elapses, in other words after all pre-existing SRCU 11345a0465e1SPaul E. McKenney * read-side critical sections have completed. However, the callback 11355a0465e1SPaul E. McKenney * function might well execute concurrently with other SRCU read-side 11365a0465e1SPaul E. McKenney * critical sections that started after call_srcu() was invoked. SRCU 11375a0465e1SPaul E. McKenney * read-side critical sections are delimited by srcu_read_lock() and 11385a0465e1SPaul E. McKenney * srcu_read_unlock(), and may be nested. 11395a0465e1SPaul E. McKenney * 11405a0465e1SPaul E. McKenney * The callback will be invoked from process context, but must nevertheless 11415a0465e1SPaul E. McKenney * be fast and must not block. 11425a0465e1SPaul E. McKenney */ 1143aacb5d91SPaul E. McKenney void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 11441e9a038bSPaul E. McKenney rcu_callback_t func) 11451e9a038bSPaul E. McKenney { 1146aacb5d91SPaul E. McKenney __call_srcu(ssp, rhp, func, true); 1147dad81a20SPaul E. McKenney } 1148dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(call_srcu); 1149dad81a20SPaul E. McKenney 1150dad81a20SPaul E. McKenney /* 1151dad81a20SPaul E. McKenney * Helper function for synchronize_srcu() and synchronize_srcu_expedited(). 1152dad81a20SPaul E. McKenney */ 1153aacb5d91SPaul E. McKenney static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm) 1154dad81a20SPaul E. McKenney { 1155dad81a20SPaul E. McKenney struct rcu_synchronize rcu; 1156dad81a20SPaul E. McKenney 1157f505d434SJakub Kicinski RCU_LOCKDEP_WARN(lockdep_is_held(ssp) || 1158dad81a20SPaul E. McKenney lock_is_held(&rcu_bh_lock_map) || 1159dad81a20SPaul E. McKenney lock_is_held(&rcu_lock_map) || 1160dad81a20SPaul E. McKenney lock_is_held(&rcu_sched_lock_map), 1161dad81a20SPaul E. McKenney "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section"); 1162dad81a20SPaul E. McKenney 1163dad81a20SPaul E. McKenney if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 1164dad81a20SPaul E. McKenney return; 1165dad81a20SPaul E. McKenney might_sleep(); 1166aacb5d91SPaul E. McKenney check_init_srcu_struct(ssp); 1167dad81a20SPaul E. McKenney init_completion(&rcu.completion); 1168da915ad5SPaul E. McKenney init_rcu_head_on_stack(&rcu.head); 1169aacb5d91SPaul E. McKenney __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm); 1170dad81a20SPaul E. McKenney wait_for_completion(&rcu.completion); 1171da915ad5SPaul E. McKenney destroy_rcu_head_on_stack(&rcu.head); 117235732cf9SPaul E. McKenney 117335732cf9SPaul E. McKenney /* 117435732cf9SPaul E. McKenney * Make sure that later code is ordered after the SRCU grace 1175d6331980SPaul E. McKenney * period. This pairs with the spin_lock_irq_rcu_node() 117635732cf9SPaul E. McKenney * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed 117735732cf9SPaul E. McKenney * because the current CPU might have been totally uninvolved with 117835732cf9SPaul E. McKenney * (and thus unordered against) that grace period. 117935732cf9SPaul E. McKenney */ 118035732cf9SPaul E. McKenney smp_mb(); 1181dad81a20SPaul E. McKenney } 1182dad81a20SPaul E. McKenney 1183dad81a20SPaul E. McKenney /** 1184dad81a20SPaul E. McKenney * synchronize_srcu_expedited - Brute-force SRCU grace period 1185aacb5d91SPaul E. McKenney * @ssp: srcu_struct with which to synchronize. 1186dad81a20SPaul E. McKenney * 1187dad81a20SPaul E. McKenney * Wait for an SRCU grace period to elapse, but be more aggressive about 1188dad81a20SPaul E. McKenney * spinning rather than blocking when waiting. 1189dad81a20SPaul E. McKenney * 1190dad81a20SPaul E. McKenney * Note that synchronize_srcu_expedited() has the same deadlock and 1191dad81a20SPaul E. McKenney * memory-ordering properties as does synchronize_srcu(). 1192dad81a20SPaul E. McKenney */ 1193aacb5d91SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *ssp) 1194dad81a20SPaul E. McKenney { 1195aacb5d91SPaul E. McKenney __synchronize_srcu(ssp, rcu_gp_is_normal()); 1196dad81a20SPaul E. McKenney } 1197dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu_expedited); 1198dad81a20SPaul E. McKenney 1199dad81a20SPaul E. McKenney /** 1200dad81a20SPaul E. McKenney * synchronize_srcu - wait for prior SRCU read-side critical-section completion 1201aacb5d91SPaul E. McKenney * @ssp: srcu_struct with which to synchronize. 1202dad81a20SPaul E. McKenney * 1203dad81a20SPaul E. McKenney * Wait for the count to drain to zero of both indexes. To avoid the 1204dad81a20SPaul E. McKenney * possible starvation of synchronize_srcu(), it waits for the count of 1205da915ad5SPaul E. McKenney * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first, 1206da915ad5SPaul E. McKenney * and then flip the srcu_idx and wait for the count of the other index. 1207dad81a20SPaul E. McKenney * 1208dad81a20SPaul E. McKenney * Can block; must be called from process context. 1209dad81a20SPaul E. McKenney * 1210dad81a20SPaul E. McKenney * Note that it is illegal to call synchronize_srcu() from the corresponding 1211dad81a20SPaul E. McKenney * SRCU read-side critical section; doing so will result in deadlock. 1212dad81a20SPaul E. McKenney * However, it is perfectly legal to call synchronize_srcu() on one 1213dad81a20SPaul E. McKenney * srcu_struct from some other srcu_struct's read-side critical section, 1214dad81a20SPaul E. McKenney * as long as the resulting graph of srcu_structs is acyclic. 1215dad81a20SPaul E. McKenney * 1216dad81a20SPaul E. McKenney * There are memory-ordering constraints implied by synchronize_srcu(). 1217dad81a20SPaul E. McKenney * On systems with more than one CPU, when synchronize_srcu() returns, 1218dad81a20SPaul E. McKenney * each CPU is guaranteed to have executed a full memory barrier since 12196eb95cc4SPaul E. McKenney * the end of its last corresponding SRCU read-side critical section 1220dad81a20SPaul E. McKenney * whose beginning preceded the call to synchronize_srcu(). In addition, 1221dad81a20SPaul E. McKenney * each CPU having an SRCU read-side critical section that extends beyond 1222dad81a20SPaul E. McKenney * the return from synchronize_srcu() is guaranteed to have executed a 1223dad81a20SPaul E. McKenney * full memory barrier after the beginning of synchronize_srcu() and before 1224dad81a20SPaul E. McKenney * the beginning of that SRCU read-side critical section. Note that these 1225dad81a20SPaul E. McKenney * guarantees include CPUs that are offline, idle, or executing in user mode, 1226dad81a20SPaul E. McKenney * as well as CPUs that are executing in the kernel. 1227dad81a20SPaul E. McKenney * 1228dad81a20SPaul E. McKenney * Furthermore, if CPU A invoked synchronize_srcu(), which returned 1229dad81a20SPaul E. McKenney * to its caller on CPU B, then both CPU A and CPU B are guaranteed 1230dad81a20SPaul E. McKenney * to have executed a full memory barrier during the execution of 1231dad81a20SPaul E. McKenney * synchronize_srcu(). This guarantee applies even if CPU A and CPU B 1232dad81a20SPaul E. McKenney * are the same CPU, but again only if the system has more than one CPU. 1233dad81a20SPaul E. McKenney * 1234dad81a20SPaul E. McKenney * Of course, these memory-ordering guarantees apply only when 1235dad81a20SPaul E. McKenney * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are 1236dad81a20SPaul E. McKenney * passed the same srcu_struct structure. 12372da4b2a7SPaul E. McKenney * 12383d3a0d1bSPaul E. McKenney * Implementation of these memory-ordering guarantees is similar to 12393d3a0d1bSPaul E. McKenney * that of synchronize_rcu(). 12403d3a0d1bSPaul E. McKenney * 12412da4b2a7SPaul E. McKenney * If SRCU is likely idle, expedite the first request. This semantic 12422da4b2a7SPaul E. McKenney * was provided by Classic SRCU, and is relied upon by its users, so TREE 12432da4b2a7SPaul E. McKenney * SRCU must also provide it. Note that detecting idleness is heuristic 12442da4b2a7SPaul E. McKenney * and subject to both false positives and negatives. 1245dad81a20SPaul E. McKenney */ 1246aacb5d91SPaul E. McKenney void synchronize_srcu(struct srcu_struct *ssp) 1247dad81a20SPaul E. McKenney { 1248aacb5d91SPaul E. McKenney if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited()) 1249aacb5d91SPaul E. McKenney synchronize_srcu_expedited(ssp); 1250dad81a20SPaul E. McKenney else 1251aacb5d91SPaul E. McKenney __synchronize_srcu(ssp, true); 1252dad81a20SPaul E. McKenney } 1253dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu); 1254dad81a20SPaul E. McKenney 12555358c9faSPaul E. McKenney /** 12565358c9faSPaul E. McKenney * get_state_synchronize_srcu - Provide an end-of-grace-period cookie 12575358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12585358c9faSPaul E. McKenney * 12595358c9faSPaul E. McKenney * This function returns a cookie that can be passed to 12605358c9faSPaul E. McKenney * poll_state_synchronize_srcu(), which will return true if a full grace 12615358c9faSPaul E. McKenney * period has elapsed in the meantime. It is the caller's responsibility 12625358c9faSPaul E. McKenney * to make sure that grace period happens, for example, by invoking 12635358c9faSPaul E. McKenney * call_srcu() after return from get_state_synchronize_srcu(). 12645358c9faSPaul E. McKenney */ 12655358c9faSPaul E. McKenney unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp) 12665358c9faSPaul E. McKenney { 12675358c9faSPaul E. McKenney // Any prior manipulation of SRCU-protected data must happen 12685358c9faSPaul E. McKenney // before the load from ->srcu_gp_seq. 12695358c9faSPaul E. McKenney smp_mb(); 12705358c9faSPaul E. McKenney return rcu_seq_snap(&ssp->srcu_gp_seq); 12715358c9faSPaul E. McKenney } 12725358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(get_state_synchronize_srcu); 12735358c9faSPaul E. McKenney 12745358c9faSPaul E. McKenney /** 12755358c9faSPaul E. McKenney * start_poll_synchronize_srcu - Provide cookie and start grace period 12765358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12775358c9faSPaul E. McKenney * 12785358c9faSPaul E. McKenney * This function returns a cookie that can be passed to 12795358c9faSPaul E. McKenney * poll_state_synchronize_srcu(), which will return true if a full grace 12805358c9faSPaul E. McKenney * period has elapsed in the meantime. Unlike get_state_synchronize_srcu(), 12815358c9faSPaul E. McKenney * this function also ensures that any needed SRCU grace period will be 12825358c9faSPaul E. McKenney * started. This convenience does come at a cost in terms of CPU overhead. 12835358c9faSPaul E. McKenney */ 12845358c9faSPaul E. McKenney unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp) 12855358c9faSPaul E. McKenney { 12865358c9faSPaul E. McKenney return srcu_gp_start_if_needed(ssp, NULL, true); 12875358c9faSPaul E. McKenney } 12885358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu); 12895358c9faSPaul E. McKenney 12905358c9faSPaul E. McKenney /** 12915358c9faSPaul E. McKenney * poll_state_synchronize_srcu - Has cookie's grace period ended? 12925358c9faSPaul E. McKenney * @ssp: srcu_struct to provide cookie for. 12935358c9faSPaul E. McKenney * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu(). 12945358c9faSPaul E. McKenney * 12955358c9faSPaul E. McKenney * This function takes the cookie that was returned from either 12965358c9faSPaul E. McKenney * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and 12975358c9faSPaul E. McKenney * returns @true if an SRCU grace period elapsed since the time that the 12985358c9faSPaul E. McKenney * cookie was created. 12994e7ccfaeSPaul E. McKenney * 13004e7ccfaeSPaul E. McKenney * Because cookies are finite in size, wrapping/overflow is possible. 13014e7ccfaeSPaul E. McKenney * This is more pronounced on 32-bit systems where cookies are 32 bits, 13024e7ccfaeSPaul E. McKenney * where in theory wrapping could happen in about 14 hours assuming 13034e7ccfaeSPaul E. McKenney * 25-microsecond expedited SRCU grace periods. However, a more likely 13044e7ccfaeSPaul E. McKenney * overflow lower bound is on the order of 24 days in the case of 13054e7ccfaeSPaul E. McKenney * one-millisecond SRCU grace periods. Of course, wrapping in a 64-bit 13064e7ccfaeSPaul E. McKenney * system requires geologic timespans, as in more than seven million years 13074e7ccfaeSPaul E. McKenney * even for expedited SRCU grace periods. 13084e7ccfaeSPaul E. McKenney * 13094e7ccfaeSPaul E. McKenney * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems 13104e7ccfaeSPaul E. McKenney * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU. This uses 13114e7ccfaeSPaul E. McKenney * a 16-bit cookie, which rcutorture routinely wraps in a matter of a 13124e7ccfaeSPaul E. McKenney * few minutes. If this proves to be a problem, this counter will be 13134e7ccfaeSPaul E. McKenney * expanded to the same size as for Tree SRCU. 13145358c9faSPaul E. McKenney */ 13155358c9faSPaul E. McKenney bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie) 13165358c9faSPaul E. McKenney { 13175358c9faSPaul E. McKenney if (!rcu_seq_done(&ssp->srcu_gp_seq, cookie)) 13185358c9faSPaul E. McKenney return false; 13195358c9faSPaul E. McKenney // Ensure that the end of the SRCU grace period happens before 13205358c9faSPaul E. McKenney // any subsequent code that the caller might execute. 13215358c9faSPaul E. McKenney smp_mb(); // ^^^ 13225358c9faSPaul E. McKenney return true; 13235358c9faSPaul E. McKenney } 13245358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu); 13255358c9faSPaul E. McKenney 1326da915ad5SPaul E. McKenney /* 1327da915ad5SPaul E. McKenney * Callback function for srcu_barrier() use. 1328da915ad5SPaul E. McKenney */ 1329da915ad5SPaul E. McKenney static void srcu_barrier_cb(struct rcu_head *rhp) 1330da915ad5SPaul E. McKenney { 1331da915ad5SPaul E. McKenney struct srcu_data *sdp; 1332aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1333da915ad5SPaul E. McKenney 1334da915ad5SPaul E. McKenney sdp = container_of(rhp, struct srcu_data, srcu_barrier_head); 1335aacb5d91SPaul E. McKenney ssp = sdp->ssp; 1336aacb5d91SPaul E. McKenney if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt)) 1337aacb5d91SPaul E. McKenney complete(&ssp->srcu_barrier_completion); 1338da915ad5SPaul E. McKenney } 1339da915ad5SPaul E. McKenney 1340994f7068SPaul E. McKenney /* 1341994f7068SPaul E. McKenney * Enqueue an srcu_barrier() callback on the specified srcu_data 1342994f7068SPaul E. McKenney * structure's ->cblist. but only if that ->cblist already has at least one 1343994f7068SPaul E. McKenney * callback enqueued. Note that if a CPU already has callbacks enqueue, 1344994f7068SPaul E. McKenney * it must have already registered the need for a future grace period, 1345994f7068SPaul E. McKenney * so all we need do is enqueue a callback that will use the same grace 1346994f7068SPaul E. McKenney * period as the last callback already in the queue. 1347994f7068SPaul E. McKenney */ 1348994f7068SPaul E. McKenney static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp) 1349994f7068SPaul E. McKenney { 1350994f7068SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1351994f7068SPaul E. McKenney atomic_inc(&ssp->srcu_barrier_cpu_cnt); 1352994f7068SPaul E. McKenney sdp->srcu_barrier_head.func = srcu_barrier_cb; 1353994f7068SPaul E. McKenney debug_rcu_head_queue(&sdp->srcu_barrier_head); 1354994f7068SPaul E. McKenney if (!rcu_segcblist_entrain(&sdp->srcu_cblist, 1355994f7068SPaul E. McKenney &sdp->srcu_barrier_head)) { 1356994f7068SPaul E. McKenney debug_rcu_head_unqueue(&sdp->srcu_barrier_head); 1357994f7068SPaul E. McKenney atomic_dec(&ssp->srcu_barrier_cpu_cnt); 1358994f7068SPaul E. McKenney } 1359994f7068SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1360994f7068SPaul E. McKenney } 1361994f7068SPaul E. McKenney 1362dad81a20SPaul E. McKenney /** 1363dad81a20SPaul E. McKenney * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete. 1364aacb5d91SPaul E. McKenney * @ssp: srcu_struct on which to wait for in-flight callbacks. 1365dad81a20SPaul E. McKenney */ 1366aacb5d91SPaul E. McKenney void srcu_barrier(struct srcu_struct *ssp) 1367dad81a20SPaul E. McKenney { 1368da915ad5SPaul E. McKenney int cpu; 1369e2f63836SPaul E. McKenney int idx; 1370aacb5d91SPaul E. McKenney unsigned long s = rcu_seq_snap(&ssp->srcu_barrier_seq); 1371da915ad5SPaul E. McKenney 1372aacb5d91SPaul E. McKenney check_init_srcu_struct(ssp); 1373aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_barrier_mutex); 1374aacb5d91SPaul E. McKenney if (rcu_seq_done(&ssp->srcu_barrier_seq, s)) { 1375da915ad5SPaul E. McKenney smp_mb(); /* Force ordering following return. */ 1376aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_barrier_mutex); 1377da915ad5SPaul E. McKenney return; /* Someone else did our work for us. */ 1378da915ad5SPaul E. McKenney } 1379aacb5d91SPaul E. McKenney rcu_seq_start(&ssp->srcu_barrier_seq); 1380aacb5d91SPaul E. McKenney init_completion(&ssp->srcu_barrier_completion); 1381da915ad5SPaul E. McKenney 1382da915ad5SPaul E. McKenney /* Initial count prevents reaching zero until all CBs are posted. */ 1383aacb5d91SPaul E. McKenney atomic_set(&ssp->srcu_barrier_cpu_cnt, 1); 1384da915ad5SPaul E. McKenney 1385e2f63836SPaul E. McKenney idx = srcu_read_lock(ssp); 1386994f7068SPaul E. McKenney if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 1387994f7068SPaul E. McKenney srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, 0)); 1388994f7068SPaul E. McKenney else 1389994f7068SPaul E. McKenney for_each_possible_cpu(cpu) 1390994f7068SPaul E. McKenney srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu)); 1391e2f63836SPaul E. McKenney srcu_read_unlock(ssp, idx); 1392da915ad5SPaul E. McKenney 1393da915ad5SPaul E. McKenney /* Remove the initial count, at which point reaching zero can happen. */ 1394aacb5d91SPaul E. McKenney if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt)) 1395aacb5d91SPaul E. McKenney complete(&ssp->srcu_barrier_completion); 1396aacb5d91SPaul E. McKenney wait_for_completion(&ssp->srcu_barrier_completion); 1397da915ad5SPaul E. McKenney 1398aacb5d91SPaul E. McKenney rcu_seq_end(&ssp->srcu_barrier_seq); 1399aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_barrier_mutex); 1400dad81a20SPaul E. McKenney } 1401dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_barrier); 1402dad81a20SPaul E. McKenney 1403dad81a20SPaul E. McKenney /** 1404dad81a20SPaul E. McKenney * srcu_batches_completed - return batches completed. 1405aacb5d91SPaul E. McKenney * @ssp: srcu_struct on which to report batch completion. 1406dad81a20SPaul E. McKenney * 1407dad81a20SPaul E. McKenney * Report the number of batches, correlated with, but not necessarily 1408dad81a20SPaul E. McKenney * precisely the same as, the number of grace periods that have elapsed. 1409dad81a20SPaul E. McKenney */ 1410aacb5d91SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *ssp) 1411dad81a20SPaul E. McKenney { 141239f91504SPaul E. McKenney return READ_ONCE(ssp->srcu_idx); 1413dad81a20SPaul E. McKenney } 1414dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_batches_completed); 1415dad81a20SPaul E. McKenney 1416dad81a20SPaul E. McKenney /* 1417da915ad5SPaul E. McKenney * Core SRCU state machine. Push state bits of ->srcu_gp_seq 1418da915ad5SPaul E. McKenney * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has 1419da915ad5SPaul E. McKenney * completed in that state. 1420dad81a20SPaul E. McKenney */ 1421aacb5d91SPaul E. McKenney static void srcu_advance_state(struct srcu_struct *ssp) 1422dad81a20SPaul E. McKenney { 1423dad81a20SPaul E. McKenney int idx; 1424dad81a20SPaul E. McKenney 1425aacb5d91SPaul E. McKenney mutex_lock(&ssp->srcu_gp_mutex); 1426da915ad5SPaul E. McKenney 1427dad81a20SPaul E. McKenney /* 1428dad81a20SPaul E. McKenney * Because readers might be delayed for an extended period after 1429da915ad5SPaul E. McKenney * fetching ->srcu_idx for their index, at any point in time there 1430dad81a20SPaul E. McKenney * might well be readers using both idx=0 and idx=1. We therefore 1431dad81a20SPaul E. McKenney * need to wait for readers to clear from both index values before 1432dad81a20SPaul E. McKenney * invoking a callback. 1433dad81a20SPaul E. McKenney * 1434dad81a20SPaul E. McKenney * The load-acquire ensures that we see the accesses performed 1435dad81a20SPaul E. McKenney * by the prior grace period. 1436dad81a20SPaul E. McKenney */ 1437aacb5d91SPaul E. McKenney idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq)); /* ^^^ */ 1438dad81a20SPaul E. McKenney if (idx == SRCU_STATE_IDLE) { 1439aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1440aacb5d91SPaul E. McKenney if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) { 1441aacb5d91SPaul E. McKenney WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq)); 1442aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1443aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1444dad81a20SPaul E. McKenney return; 1445dad81a20SPaul E. McKenney } 1446aacb5d91SPaul E. McKenney idx = rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)); 1447dad81a20SPaul E. McKenney if (idx == SRCU_STATE_IDLE) 1448aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 1449aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1450da915ad5SPaul E. McKenney if (idx != SRCU_STATE_IDLE) { 1451aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1452dad81a20SPaul E. McKenney return; /* Someone else started the grace period. */ 1453dad81a20SPaul E. McKenney } 1454da915ad5SPaul E. McKenney } 1455dad81a20SPaul E. McKenney 1456aacb5d91SPaul E. McKenney if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN1) { 1457aacb5d91SPaul E. McKenney idx = 1 ^ (ssp->srcu_idx & 1); 1458aacb5d91SPaul E. McKenney if (!try_check_zero(ssp, idx, 1)) { 1459aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1460dad81a20SPaul E. McKenney return; /* readers present, retry later. */ 1461da915ad5SPaul E. McKenney } 1462aacb5d91SPaul E. McKenney srcu_flip(ssp); 146371042606SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1464aacb5d91SPaul E. McKenney rcu_seq_set_state(&ssp->srcu_gp_seq, SRCU_STATE_SCAN2); 1465*282d8998SPaul E. McKenney ssp->srcu_n_exp_nodelay = 0; 146671042606SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1467dad81a20SPaul E. McKenney } 1468dad81a20SPaul E. McKenney 1469aacb5d91SPaul E. McKenney if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN2) { 1470dad81a20SPaul E. McKenney 1471dad81a20SPaul E. McKenney /* 1472dad81a20SPaul E. McKenney * SRCU read-side critical sections are normally short, 1473dad81a20SPaul E. McKenney * so check at least twice in quick succession after a flip. 1474dad81a20SPaul E. McKenney */ 1475aacb5d91SPaul E. McKenney idx = 1 ^ (ssp->srcu_idx & 1); 1476aacb5d91SPaul E. McKenney if (!try_check_zero(ssp, idx, 2)) { 1477aacb5d91SPaul E. McKenney mutex_unlock(&ssp->srcu_gp_mutex); 1478da915ad5SPaul E. McKenney return; /* readers present, retry later. */ 1479da915ad5SPaul E. McKenney } 1480*282d8998SPaul E. McKenney ssp->srcu_n_exp_nodelay = 0; 1481aacb5d91SPaul E. McKenney srcu_gp_end(ssp); /* Releases ->srcu_gp_mutex. */ 1482dad81a20SPaul E. McKenney } 1483dad81a20SPaul E. McKenney } 1484dad81a20SPaul E. McKenney 1485dad81a20SPaul E. McKenney /* 1486dad81a20SPaul E. McKenney * Invoke a limited number of SRCU callbacks that have passed through 1487dad81a20SPaul E. McKenney * their grace period. If there are more to do, SRCU will reschedule 1488dad81a20SPaul E. McKenney * the workqueue. Note that needed memory barriers have been executed 1489dad81a20SPaul E. McKenney * in this task's context by srcu_readers_active_idx_check(). 1490dad81a20SPaul E. McKenney */ 1491da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work) 1492dad81a20SPaul E. McKenney { 1493ae5c2341SJoel Fernandes (Google) long len; 1494da915ad5SPaul E. McKenney bool more; 1495dad81a20SPaul E. McKenney struct rcu_cblist ready_cbs; 1496dad81a20SPaul E. McKenney struct rcu_head *rhp; 1497da915ad5SPaul E. McKenney struct srcu_data *sdp; 1498aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1499dad81a20SPaul E. McKenney 1500e81baf4cSSebastian Andrzej Siewior sdp = container_of(work, struct srcu_data, work); 1501e81baf4cSSebastian Andrzej Siewior 1502aacb5d91SPaul E. McKenney ssp = sdp->ssp; 1503dad81a20SPaul E. McKenney rcu_cblist_init(&ready_cbs); 1504d6331980SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1505da915ad5SPaul E. McKenney rcu_segcblist_advance(&sdp->srcu_cblist, 1506aacb5d91SPaul E. McKenney rcu_seq_current(&ssp->srcu_gp_seq)); 1507da915ad5SPaul E. McKenney if (sdp->srcu_cblist_invoking || 1508da915ad5SPaul E. McKenney !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) { 1509d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1510da915ad5SPaul E. McKenney return; /* Someone else on the job or nothing to do. */ 1511da915ad5SPaul E. McKenney } 1512da915ad5SPaul E. McKenney 1513da915ad5SPaul E. McKenney /* We are on the job! Extract and invoke ready callbacks. */ 1514da915ad5SPaul E. McKenney sdp->srcu_cblist_invoking = true; 1515da915ad5SPaul E. McKenney rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs); 1516ae5c2341SJoel Fernandes (Google) len = ready_cbs.len; 1517d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1518dad81a20SPaul E. McKenney rhp = rcu_cblist_dequeue(&ready_cbs); 1519dad81a20SPaul E. McKenney for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) { 1520a602538eSPaul E. McKenney debug_rcu_head_unqueue(rhp); 1521dad81a20SPaul E. McKenney local_bh_disable(); 1522dad81a20SPaul E. McKenney rhp->func(rhp); 1523dad81a20SPaul E. McKenney local_bh_enable(); 1524dad81a20SPaul E. McKenney } 1525ae5c2341SJoel Fernandes (Google) WARN_ON_ONCE(ready_cbs.len); 1526da915ad5SPaul E. McKenney 1527da915ad5SPaul E. McKenney /* 1528da915ad5SPaul E. McKenney * Update counts, accelerate new callbacks, and if needed, 1529da915ad5SPaul E. McKenney * schedule another round of callback invocation. 1530da915ad5SPaul E. McKenney */ 1531d6331980SPaul E. McKenney spin_lock_irq_rcu_node(sdp); 1532ae5c2341SJoel Fernandes (Google) rcu_segcblist_add_len(&sdp->srcu_cblist, -len); 1533da915ad5SPaul E. McKenney (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, 1534aacb5d91SPaul E. McKenney rcu_seq_snap(&ssp->srcu_gp_seq)); 1535da915ad5SPaul E. McKenney sdp->srcu_cblist_invoking = false; 1536da915ad5SPaul E. McKenney more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist); 1537d6331980SPaul E. McKenney spin_unlock_irq_rcu_node(sdp); 1538da915ad5SPaul E. McKenney if (more) 1539da915ad5SPaul E. McKenney srcu_schedule_cbs_sdp(sdp, 0); 1540dad81a20SPaul E. McKenney } 1541dad81a20SPaul E. McKenney 1542dad81a20SPaul E. McKenney /* 1543dad81a20SPaul E. McKenney * Finished one round of SRCU grace period. Start another if there are 1544dad81a20SPaul E. McKenney * more SRCU callbacks queued, otherwise put SRCU into not-running state. 1545dad81a20SPaul E. McKenney */ 1546aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay) 1547dad81a20SPaul E. McKenney { 1548da915ad5SPaul E. McKenney bool pushgp = true; 1549dad81a20SPaul E. McKenney 1550aacb5d91SPaul E. McKenney spin_lock_irq_rcu_node(ssp); 1551aacb5d91SPaul E. McKenney if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) { 1552aacb5d91SPaul E. McKenney if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq))) { 1553da915ad5SPaul E. McKenney /* All requests fulfilled, time to go idle. */ 1554da915ad5SPaul E. McKenney pushgp = false; 1555dad81a20SPaul E. McKenney } 1556aacb5d91SPaul E. McKenney } else if (!rcu_seq_state(ssp->srcu_gp_seq)) { 1557da915ad5SPaul E. McKenney /* Outstanding request and no GP. Start one. */ 1558aacb5d91SPaul E. McKenney srcu_gp_start(ssp); 1559da915ad5SPaul E. McKenney } 1560aacb5d91SPaul E. McKenney spin_unlock_irq_rcu_node(ssp); 1561dad81a20SPaul E. McKenney 1562da915ad5SPaul E. McKenney if (pushgp) 1563aacb5d91SPaul E. McKenney queue_delayed_work(rcu_gp_wq, &ssp->work, delay); 1564dad81a20SPaul E. McKenney } 1565dad81a20SPaul E. McKenney 1566dad81a20SPaul E. McKenney /* 1567dad81a20SPaul E. McKenney * This is the work-queue function that handles SRCU grace periods. 1568dad81a20SPaul E. McKenney */ 15690d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work) 1570dad81a20SPaul E. McKenney { 1571*282d8998SPaul E. McKenney unsigned long curdelay; 1572*282d8998SPaul E. McKenney unsigned long j; 1573aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1574dad81a20SPaul E. McKenney 1575aacb5d91SPaul E. McKenney ssp = container_of(work, struct srcu_struct, work.work); 1576dad81a20SPaul E. McKenney 1577aacb5d91SPaul E. McKenney srcu_advance_state(ssp); 1578*282d8998SPaul E. McKenney curdelay = srcu_get_delay(ssp); 1579*282d8998SPaul E. McKenney if (curdelay) { 1580*282d8998SPaul E. McKenney WRITE_ONCE(ssp->reschedule_count, 0); 1581*282d8998SPaul E. McKenney } else { 1582*282d8998SPaul E. McKenney j = jiffies; 1583*282d8998SPaul E. McKenney if (READ_ONCE(ssp->reschedule_jiffies) == j) { 1584*282d8998SPaul E. McKenney WRITE_ONCE(ssp->reschedule_count, READ_ONCE(ssp->reschedule_count) + 1); 1585*282d8998SPaul E. McKenney if (READ_ONCE(ssp->reschedule_count) > SRCU_MAX_NODELAY) 1586*282d8998SPaul E. McKenney curdelay = 1; 1587*282d8998SPaul E. McKenney } else { 1588*282d8998SPaul E. McKenney WRITE_ONCE(ssp->reschedule_count, 1); 1589*282d8998SPaul E. McKenney WRITE_ONCE(ssp->reschedule_jiffies, j); 1590*282d8998SPaul E. McKenney } 1591*282d8998SPaul E. McKenney } 1592*282d8998SPaul E. McKenney srcu_reschedule(ssp, curdelay); 1593dad81a20SPaul E. McKenney } 15947f6733c3SPaul E. McKenney 15957f6733c3SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type, 1596aacb5d91SPaul E. McKenney struct srcu_struct *ssp, int *flags, 1597aebc8264SPaul E. McKenney unsigned long *gp_seq) 15987f6733c3SPaul E. McKenney { 15997f6733c3SPaul E. McKenney if (test_type != SRCU_FLAVOR) 16007f6733c3SPaul E. McKenney return; 16017f6733c3SPaul E. McKenney *flags = 0; 1602aacb5d91SPaul E. McKenney *gp_seq = rcu_seq_current(&ssp->srcu_gp_seq); 16037f6733c3SPaul E. McKenney } 16047f6733c3SPaul E. McKenney EXPORT_SYMBOL_GPL(srcutorture_get_gp_data); 16051f4f6da1SPaul E. McKenney 16063bedebcfSPaul E. McKenney static const char * const srcu_size_state_name[] = { 16073bedebcfSPaul E. McKenney "SRCU_SIZE_SMALL", 16083bedebcfSPaul E. McKenney "SRCU_SIZE_ALLOC", 16093bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_BARRIER", 16103bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CALL", 16113bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS1", 16123bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS2", 16133bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS3", 16143bedebcfSPaul E. McKenney "SRCU_SIZE_WAIT_CBS4", 16153bedebcfSPaul E. McKenney "SRCU_SIZE_BIG", 16163bedebcfSPaul E. McKenney "SRCU_SIZE_???", 16173bedebcfSPaul E. McKenney }; 16183bedebcfSPaul E. McKenney 1619aacb5d91SPaul E. McKenney void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf) 1620115a1a52SPaul E. McKenney { 1621115a1a52SPaul E. McKenney int cpu; 1622115a1a52SPaul E. McKenney int idx; 1623ac3748c6SPaul E. McKenney unsigned long s0 = 0, s1 = 0; 16243bedebcfSPaul E. McKenney int ss_state = READ_ONCE(ssp->srcu_size_state); 16253bedebcfSPaul E. McKenney int ss_state_idx = ss_state; 1626115a1a52SPaul E. McKenney 1627aacb5d91SPaul E. McKenney idx = ssp->srcu_idx & 0x1; 16283bedebcfSPaul E. McKenney if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name)) 16293bedebcfSPaul E. McKenney ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1; 16304a230f80SPaul E. McKenney pr_alert("%s%s Tree SRCU g%ld state %d (%s)", 16313bedebcfSPaul E. McKenney tt, tf, rcu_seq_current(&ssp->srcu_gp_seq), ss_state, 16324a230f80SPaul E. McKenney srcu_size_state_name[ss_state_idx]); 16334a230f80SPaul E. McKenney if (!ssp->sda) { 16344a230f80SPaul E. McKenney // Called after cleanup_srcu_struct(), perhaps. 16354a230f80SPaul E. McKenney pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n"); 16364a230f80SPaul E. McKenney } else { 16374a230f80SPaul E. McKenney pr_cont(" per-CPU(idx=%d):", idx); 1638115a1a52SPaul E. McKenney for_each_possible_cpu(cpu) { 1639115a1a52SPaul E. McKenney unsigned long l0, l1; 1640115a1a52SPaul E. McKenney unsigned long u0, u1; 1641115a1a52SPaul E. McKenney long c0, c1; 16425ab07a8dSPaul E. McKenney struct srcu_data *sdp; 1643115a1a52SPaul E. McKenney 1644aacb5d91SPaul E. McKenney sdp = per_cpu_ptr(ssp->sda, cpu); 1645b68c6146SPaul E. McKenney u0 = data_race(sdp->srcu_unlock_count[!idx]); 1646b68c6146SPaul E. McKenney u1 = data_race(sdp->srcu_unlock_count[idx]); 1647115a1a52SPaul E. McKenney 1648115a1a52SPaul E. McKenney /* 1649115a1a52SPaul E. McKenney * Make sure that a lock is always counted if the corresponding 1650115a1a52SPaul E. McKenney * unlock is counted. 1651115a1a52SPaul E. McKenney */ 1652115a1a52SPaul E. McKenney smp_rmb(); 1653115a1a52SPaul E. McKenney 1654b68c6146SPaul E. McKenney l0 = data_race(sdp->srcu_lock_count[!idx]); 1655b68c6146SPaul E. McKenney l1 = data_race(sdp->srcu_lock_count[idx]); 1656115a1a52SPaul E. McKenney 1657115a1a52SPaul E. McKenney c0 = l0 - u0; 1658115a1a52SPaul E. McKenney c1 = l1 - u1; 16597e210a65SPaul E. McKenney pr_cont(" %d(%ld,%ld %c)", 16607e210a65SPaul E. McKenney cpu, c0, c1, 16617e210a65SPaul E. McKenney "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]); 1662ac3748c6SPaul E. McKenney s0 += c0; 1663ac3748c6SPaul E. McKenney s1 += c1; 1664115a1a52SPaul E. McKenney } 1665ac3748c6SPaul E. McKenney pr_cont(" T(%ld,%ld)\n", s0, s1); 16664a230f80SPaul E. McKenney } 16679f2e91d9SPaul E. McKenney if (SRCU_SIZING_IS_TORTURE()) 166899659f64SPaul E. McKenney srcu_transition_to_big(ssp); 1669115a1a52SPaul E. McKenney } 1670115a1a52SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_torture_stats_print); 1671115a1a52SPaul E. McKenney 16721f4f6da1SPaul E. McKenney static int __init srcu_bootup_announce(void) 16731f4f6da1SPaul E. McKenney { 16741f4f6da1SPaul E. McKenney pr_info("Hierarchical SRCU implementation.\n"); 16750c8e0e3cSPaul E. McKenney if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF) 16760c8e0e3cSPaul E. McKenney pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff); 16771f4f6da1SPaul E. McKenney return 0; 16781f4f6da1SPaul E. McKenney } 16791f4f6da1SPaul E. McKenney early_initcall(srcu_bootup_announce); 1680e0fcba9aSPaul E. McKenney 1681e0fcba9aSPaul E. McKenney void __init srcu_init(void) 1682e0fcba9aSPaul E. McKenney { 1683aacb5d91SPaul E. McKenney struct srcu_struct *ssp; 1684e0fcba9aSPaul E. McKenney 1685a57ffb3cSPaul E. McKenney /* Decide on srcu_struct-size strategy. */ 1686a57ffb3cSPaul E. McKenney if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) { 1687a57ffb3cSPaul E. McKenney if (nr_cpu_ids >= big_cpu_lim) { 1688a57ffb3cSPaul E. McKenney convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention. 1689a57ffb3cSPaul E. McKenney pr_info("%s: Setting srcu_struct sizes to big.\n", __func__); 1690a57ffb3cSPaul E. McKenney } else { 1691a57ffb3cSPaul E. McKenney convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND; 1692a57ffb3cSPaul E. McKenney pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__); 1693a57ffb3cSPaul E. McKenney } 1694a57ffb3cSPaul E. McKenney } 1695a57ffb3cSPaul E. McKenney 16968e9c01c7SFrederic Weisbecker /* 16978e9c01c7SFrederic Weisbecker * Once that is set, call_srcu() can follow the normal path and 16988e9c01c7SFrederic Weisbecker * queue delayed work. This must follow RCU workqueues creation 16998e9c01c7SFrederic Weisbecker * and timers initialization. 17008e9c01c7SFrederic Weisbecker */ 1701e0fcba9aSPaul E. McKenney srcu_init_done = true; 1702e0fcba9aSPaul E. McKenney while (!list_empty(&srcu_boot_list)) { 1703aacb5d91SPaul E. McKenney ssp = list_first_entry(&srcu_boot_list, struct srcu_struct, 17044e6ea4efSPaul E. McKenney work.work.entry); 1705aacb5d91SPaul E. McKenney list_del_init(&ssp->work.work.entry); 1706a57ffb3cSPaul E. McKenney if (SRCU_SIZING_IS(SRCU_SIZING_INIT) && ssp->srcu_size_state == SRCU_SIZE_SMALL) 1707a57ffb3cSPaul E. McKenney ssp->srcu_size_state = SRCU_SIZE_ALLOC; 1708aacb5d91SPaul E. McKenney queue_work(rcu_gp_wq, &ssp->work.work); 1709e0fcba9aSPaul E. McKenney } 1710e0fcba9aSPaul E. McKenney } 1711fe15b50cSPaul E. McKenney 1712fe15b50cSPaul E. McKenney #ifdef CONFIG_MODULES 1713fe15b50cSPaul E. McKenney 1714fe15b50cSPaul E. McKenney /* Initialize any global-scope srcu_struct structures used by this module. */ 1715fe15b50cSPaul E. McKenney static int srcu_module_coming(struct module *mod) 1716fe15b50cSPaul E. McKenney { 1717fe15b50cSPaul E. McKenney int i; 1718fe15b50cSPaul E. McKenney struct srcu_struct **sspp = mod->srcu_struct_ptrs; 1719fe15b50cSPaul E. McKenney int ret; 1720fe15b50cSPaul E. McKenney 1721fe15b50cSPaul E. McKenney for (i = 0; i < mod->num_srcu_structs; i++) { 1722fe15b50cSPaul E. McKenney ret = init_srcu_struct(*(sspp++)); 1723fe15b50cSPaul E. McKenney if (WARN_ON_ONCE(ret)) 1724fe15b50cSPaul E. McKenney return ret; 1725fe15b50cSPaul E. McKenney } 1726fe15b50cSPaul E. McKenney return 0; 1727fe15b50cSPaul E. McKenney } 1728fe15b50cSPaul E. McKenney 1729fe15b50cSPaul E. McKenney /* Clean up any global-scope srcu_struct structures used by this module. */ 1730fe15b50cSPaul E. McKenney static void srcu_module_going(struct module *mod) 1731fe15b50cSPaul E. McKenney { 1732fe15b50cSPaul E. McKenney int i; 1733fe15b50cSPaul E. McKenney struct srcu_struct **sspp = mod->srcu_struct_ptrs; 1734fe15b50cSPaul E. McKenney 1735fe15b50cSPaul E. McKenney for (i = 0; i < mod->num_srcu_structs; i++) 1736fe15b50cSPaul E. McKenney cleanup_srcu_struct(*(sspp++)); 1737fe15b50cSPaul E. McKenney } 1738fe15b50cSPaul E. McKenney 1739fe15b50cSPaul E. McKenney /* Handle one module, either coming or going. */ 1740fe15b50cSPaul E. McKenney static int srcu_module_notify(struct notifier_block *self, 1741fe15b50cSPaul E. McKenney unsigned long val, void *data) 1742fe15b50cSPaul E. McKenney { 1743fe15b50cSPaul E. McKenney struct module *mod = data; 1744fe15b50cSPaul E. McKenney int ret = 0; 1745fe15b50cSPaul E. McKenney 1746fe15b50cSPaul E. McKenney switch (val) { 1747fe15b50cSPaul E. McKenney case MODULE_STATE_COMING: 1748fe15b50cSPaul E. McKenney ret = srcu_module_coming(mod); 1749fe15b50cSPaul E. McKenney break; 1750fe15b50cSPaul E. McKenney case MODULE_STATE_GOING: 1751fe15b50cSPaul E. McKenney srcu_module_going(mod); 1752fe15b50cSPaul E. McKenney break; 1753fe15b50cSPaul E. McKenney default: 1754fe15b50cSPaul E. McKenney break; 1755fe15b50cSPaul E. McKenney } 1756fe15b50cSPaul E. McKenney return ret; 1757fe15b50cSPaul E. McKenney } 1758fe15b50cSPaul E. McKenney 1759fe15b50cSPaul E. McKenney static struct notifier_block srcu_module_nb = { 1760fe15b50cSPaul E. McKenney .notifier_call = srcu_module_notify, 1761fe15b50cSPaul E. McKenney .priority = 0, 1762fe15b50cSPaul E. McKenney }; 1763fe15b50cSPaul E. McKenney 1764fe15b50cSPaul E. McKenney static __init int init_srcu_module_notifier(void) 1765fe15b50cSPaul E. McKenney { 1766fe15b50cSPaul E. McKenney int ret; 1767fe15b50cSPaul E. McKenney 1768fe15b50cSPaul E. McKenney ret = register_module_notifier(&srcu_module_nb); 1769fe15b50cSPaul E. McKenney if (ret) 1770fe15b50cSPaul E. McKenney pr_warn("Failed to register srcu module notifier\n"); 1771fe15b50cSPaul E. McKenney return ret; 1772fe15b50cSPaul E. McKenney } 1773fe15b50cSPaul E. McKenney late_initcall(init_srcu_module_notifier); 1774fe15b50cSPaul E. McKenney 1775fe15b50cSPaul E. McKenney #endif /* #ifdef CONFIG_MODULES */ 1776