1b5b11890SPaul E. McKenney /* SPDX-License-Identifier: GPL-2.0+ */ 24102adabSPaul E. McKenney /* 34102adabSPaul E. McKenney * Read-Copy Update definitions shared among RCU implementations. 44102adabSPaul E. McKenney * 54102adabSPaul E. McKenney * Copyright IBM Corporation, 2011 64102adabSPaul E. McKenney * 7b5b11890SPaul E. McKenney * Author: Paul E. McKenney <paulmck@linux.ibm.com> 84102adabSPaul E. McKenney */ 94102adabSPaul E. McKenney 104102adabSPaul E. McKenney #ifndef __LINUX_RCU_H 114102adabSPaul E. McKenney #define __LINUX_RCU_H 124102adabSPaul E. McKenney 135cb5c6e1SPaul Gortmaker #include <trace/events/rcu.h> 144102adabSPaul E. McKenney 152e8c28c2SPaul E. McKenney /* 162e8c28c2SPaul E. McKenney * Grace-period counter management. 172e8c28c2SPaul E. McKenney */ 182e8c28c2SPaul E. McKenney 19f1ec57a4SPaul E. McKenney #define RCU_SEQ_CTR_SHIFT 2 20031aeee0SPaul E. McKenney #define RCU_SEQ_STATE_MASK ((1 << RCU_SEQ_CTR_SHIFT) - 1) 21031aeee0SPaul E. McKenney 22414c1238SPaul E. McKenney /* Low-order bit definition for polled grace-period APIs. */ 23414c1238SPaul E. McKenney #define RCU_GET_STATE_COMPLETED 0x1 24414c1238SPaul E. McKenney 25d9ab0e63SZhen Ni extern int sysctl_sched_rt_runtime; 26d9ab0e63SZhen Ni 27031aeee0SPaul E. McKenney /* 28031aeee0SPaul E. McKenney * Return the counter portion of a sequence number previously returned 29031aeee0SPaul E. McKenney * by rcu_seq_snap() or rcu_seq_current(). 30031aeee0SPaul E. McKenney */ 31031aeee0SPaul E. McKenney static inline unsigned long rcu_seq_ctr(unsigned long s) 32031aeee0SPaul E. McKenney { 33031aeee0SPaul E. McKenney return s >> RCU_SEQ_CTR_SHIFT; 34031aeee0SPaul E. McKenney } 35031aeee0SPaul E. McKenney 36031aeee0SPaul E. McKenney /* 37031aeee0SPaul E. McKenney * Return the state portion of a sequence number previously returned 38031aeee0SPaul E. McKenney * by rcu_seq_snap() or rcu_seq_current(). 39031aeee0SPaul E. McKenney */ 40031aeee0SPaul E. McKenney static inline int rcu_seq_state(unsigned long s) 41031aeee0SPaul E. McKenney { 42031aeee0SPaul E. McKenney return s & RCU_SEQ_STATE_MASK; 43031aeee0SPaul E. McKenney } 44031aeee0SPaul E. McKenney 4580a7956fSPaul E. McKenney /* 4680a7956fSPaul E. McKenney * Set the state portion of the pointed-to sequence number. 4780a7956fSPaul E. McKenney * The caller is responsible for preventing conflicting updates. 4880a7956fSPaul E. McKenney */ 4980a7956fSPaul E. McKenney static inline void rcu_seq_set_state(unsigned long *sp, int newstate) 5080a7956fSPaul E. McKenney { 5180a7956fSPaul E. McKenney WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK); 5280a7956fSPaul E. McKenney WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate); 5380a7956fSPaul E. McKenney } 5480a7956fSPaul E. McKenney 552e8c28c2SPaul E. McKenney /* Adjust sequence number for start of update-side operation. */ 562e8c28c2SPaul E. McKenney static inline void rcu_seq_start(unsigned long *sp) 572e8c28c2SPaul E. McKenney { 582e8c28c2SPaul E. McKenney WRITE_ONCE(*sp, *sp + 1); 592e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation after counter increment. */ 60031aeee0SPaul E. McKenney WARN_ON_ONCE(rcu_seq_state(*sp) != 1); 612e8c28c2SPaul E. McKenney } 622e8c28c2SPaul E. McKenney 639a414201SPaul E. McKenney /* Compute the end-of-grace-period value for the specified sequence number. */ 649a414201SPaul E. McKenney static inline unsigned long rcu_seq_endval(unsigned long *sp) 659a414201SPaul E. McKenney { 669a414201SPaul E. McKenney return (*sp | RCU_SEQ_STATE_MASK) + 1; 679a414201SPaul E. McKenney } 689a414201SPaul E. McKenney 692e8c28c2SPaul E. McKenney /* Adjust sequence number for end of update-side operation. */ 702e8c28c2SPaul E. McKenney static inline void rcu_seq_end(unsigned long *sp) 712e8c28c2SPaul E. McKenney { 722e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation before counter increment. */ 73031aeee0SPaul E. McKenney WARN_ON_ONCE(!rcu_seq_state(*sp)); 749a414201SPaul E. McKenney WRITE_ONCE(*sp, rcu_seq_endval(sp)); 752e8c28c2SPaul E. McKenney } 762e8c28c2SPaul E. McKenney 770d805a70SJoel Fernandes (Google) /* 780d805a70SJoel Fernandes (Google) * rcu_seq_snap - Take a snapshot of the update side's sequence number. 790d805a70SJoel Fernandes (Google) * 800d805a70SJoel Fernandes (Google) * This function returns the earliest value of the grace-period sequence number 810d805a70SJoel Fernandes (Google) * that will indicate that a full grace period has elapsed since the current 820d805a70SJoel Fernandes (Google) * time. Once the grace-period sequence number has reached this value, it will 830d805a70SJoel Fernandes (Google) * be safe to invoke all callbacks that have been registered prior to the 840d805a70SJoel Fernandes (Google) * current time. This value is the current grace-period number plus two to the 850d805a70SJoel Fernandes (Google) * power of the number of low-order bits reserved for state, then rounded up to 860d805a70SJoel Fernandes (Google) * the next value in which the state bits are all zero. 870d805a70SJoel Fernandes (Google) */ 882e8c28c2SPaul E. McKenney static inline unsigned long rcu_seq_snap(unsigned long *sp) 892e8c28c2SPaul E. McKenney { 902e8c28c2SPaul E. McKenney unsigned long s; 912e8c28c2SPaul E. McKenney 92031aeee0SPaul E. McKenney s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK; 932e8c28c2SPaul E. McKenney smp_mb(); /* Above access must not bleed into critical section. */ 942e8c28c2SPaul E. McKenney return s; 952e8c28c2SPaul E. McKenney } 962e8c28c2SPaul E. McKenney 978660b7d8SPaul E. McKenney /* Return the current value the update side's sequence number, no ordering. */ 988660b7d8SPaul E. McKenney static inline unsigned long rcu_seq_current(unsigned long *sp) 998660b7d8SPaul E. McKenney { 1008660b7d8SPaul E. McKenney return READ_ONCE(*sp); 1018660b7d8SPaul E. McKenney } 1028660b7d8SPaul E. McKenney 1032e8c28c2SPaul E. McKenney /* 1042e3e5e55SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not the 1052e3e5e55SPaul E. McKenney * corresponding update-side operation has started. 1062e3e5e55SPaul E. McKenney */ 1072e3e5e55SPaul E. McKenney static inline bool rcu_seq_started(unsigned long *sp, unsigned long s) 1082e3e5e55SPaul E. McKenney { 1092e3e5e55SPaul E. McKenney return ULONG_CMP_LT((s - 1) & ~RCU_SEQ_STATE_MASK, READ_ONCE(*sp)); 1102e3e5e55SPaul E. McKenney } 1112e3e5e55SPaul E. McKenney 1122e3e5e55SPaul E. McKenney /* 1132e8c28c2SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not a 1142e8c28c2SPaul E. McKenney * full update-side operation has occurred. 1152e8c28c2SPaul E. McKenney */ 1162e8c28c2SPaul E. McKenney static inline bool rcu_seq_done(unsigned long *sp, unsigned long s) 1172e8c28c2SPaul E. McKenney { 1182e8c28c2SPaul E. McKenney return ULONG_CMP_GE(READ_ONCE(*sp), s); 1192e8c28c2SPaul E. McKenney } 1202e8c28c2SPaul E. McKenney 1214102adabSPaul E. McKenney /* 1222403e804SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not a 1232403e804SPaul E. McKenney * full update-side operation has occurred, but do not allow the 1242403e804SPaul E. McKenney * (ULONG_MAX / 2) safety-factor/guard-band. 1252403e804SPaul E. McKenney */ 1262403e804SPaul E. McKenney static inline bool rcu_seq_done_exact(unsigned long *sp, unsigned long s) 1272403e804SPaul E. McKenney { 1282403e804SPaul E. McKenney unsigned long cur_s = READ_ONCE(*sp); 1292403e804SPaul E. McKenney 1302403e804SPaul E. McKenney return ULONG_CMP_GE(cur_s, s) || ULONG_CMP_LT(cur_s, s - (2 * RCU_SEQ_STATE_MASK + 1)); 1312403e804SPaul E. McKenney } 1322403e804SPaul E. McKenney 1332403e804SPaul E. McKenney /* 13467e14c1eSPaul E. McKenney * Has a grace period completed since the time the old gp_seq was collected? 13567e14c1eSPaul E. McKenney */ 13667e14c1eSPaul E. McKenney static inline bool rcu_seq_completed_gp(unsigned long old, unsigned long new) 13767e14c1eSPaul E. McKenney { 13867e14c1eSPaul E. McKenney return ULONG_CMP_LT(old, new & ~RCU_SEQ_STATE_MASK); 13967e14c1eSPaul E. McKenney } 14067e14c1eSPaul E. McKenney 14167e14c1eSPaul E. McKenney /* 14267e14c1eSPaul E. McKenney * Has a grace period started since the time the old gp_seq was collected? 14367e14c1eSPaul E. McKenney */ 14467e14c1eSPaul E. McKenney static inline bool rcu_seq_new_gp(unsigned long old, unsigned long new) 14567e14c1eSPaul E. McKenney { 14667e14c1eSPaul E. McKenney return ULONG_CMP_LT((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK, 14767e14c1eSPaul E. McKenney new); 14867e14c1eSPaul E. McKenney } 14967e14c1eSPaul E. McKenney 15067e14c1eSPaul E. McKenney /* 151d7219312SPaul E. McKenney * Roughly how many full grace periods have elapsed between the collection 152d7219312SPaul E. McKenney * of the two specified grace periods? 153d7219312SPaul E. McKenney */ 154d7219312SPaul E. McKenney static inline unsigned long rcu_seq_diff(unsigned long new, unsigned long old) 155d7219312SPaul E. McKenney { 1562ee5aca5SPaul E. McKenney unsigned long rnd_diff; 1572ee5aca5SPaul E. McKenney 1582ee5aca5SPaul E. McKenney if (old == new) 1592ee5aca5SPaul E. McKenney return 0; 1602ee5aca5SPaul E. McKenney /* 1612ee5aca5SPaul E. McKenney * Compute the number of grace periods (still shifted up), plus 1622ee5aca5SPaul E. McKenney * one if either of new and old is not an exact grace period. 1632ee5aca5SPaul E. McKenney */ 1642ee5aca5SPaul E. McKenney rnd_diff = (new & ~RCU_SEQ_STATE_MASK) - 1652ee5aca5SPaul E. McKenney ((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK) + 1662ee5aca5SPaul E. McKenney ((new & RCU_SEQ_STATE_MASK) || (old & RCU_SEQ_STATE_MASK)); 1672ee5aca5SPaul E. McKenney if (ULONG_CMP_GE(RCU_SEQ_STATE_MASK, rnd_diff)) 1682ee5aca5SPaul E. McKenney return 1; /* Definitely no grace period has elapsed. */ 1692ee5aca5SPaul E. McKenney return ((rnd_diff - RCU_SEQ_STATE_MASK - 1) >> RCU_SEQ_CTR_SHIFT) + 2; 170d7219312SPaul E. McKenney } 171d7219312SPaul E. McKenney 172d7219312SPaul E. McKenney /* 1734102adabSPaul E. McKenney * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally 1747f87c036SPaul E. McKenney * by call_rcu() and rcu callback execution, and are therefore not part 1757f87c036SPaul E. McKenney * of the RCU API. These are in rcupdate.h because they are used by all 1767f87c036SPaul E. McKenney * RCU implementations. 1774102adabSPaul E. McKenney */ 1784102adabSPaul E. McKenney 1794102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 1804102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY 0 1814102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED 1 1824102adabSPaul E. McKenney 183f9e62f31SStephen Boyd extern const struct debug_obj_descr rcuhead_debug_descr; 1844102adabSPaul E. McKenney 1854102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 1864102adabSPaul E. McKenney { 1874102adabSPaul E. McKenney int r1; 1884102adabSPaul E. McKenney 1894102adabSPaul E. McKenney r1 = debug_object_activate(head, &rcuhead_debug_descr); 1904102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1914102adabSPaul E. McKenney STATE_RCU_HEAD_READY, 1924102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED); 1934102adabSPaul E. McKenney return r1; 1944102adabSPaul E. McKenney } 1954102adabSPaul E. McKenney 1964102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head) 1974102adabSPaul E. McKenney { 1984102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1994102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED, 2004102adabSPaul E. McKenney STATE_RCU_HEAD_READY); 2014102adabSPaul E. McKenney debug_object_deactivate(head, &rcuhead_debug_descr); 2024102adabSPaul E. McKenney } 2034102adabSPaul E. McKenney #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 2044102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 2054102adabSPaul E. McKenney { 2064102adabSPaul E. McKenney return 0; 2074102adabSPaul E. McKenney } 2084102adabSPaul E. McKenney 2094102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head) 2104102adabSPaul E. McKenney { 2114102adabSPaul E. McKenney } 2124102adabSPaul E. McKenney #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 2134102adabSPaul E. McKenney 21458c53360SPaul E. McKenney extern int rcu_cpu_stall_suppress_at_boot; 21558c53360SPaul E. McKenney 21658c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed_at_boot(void) 21758c53360SPaul E. McKenney { 21858c53360SPaul E. McKenney return rcu_cpu_stall_suppress_at_boot && !rcu_inkernel_boot_has_ended(); 21958c53360SPaul E. McKenney } 22058c53360SPaul E. McKenney 2214102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON 2224102adabSPaul E. McKenney 223cdc694b2SPaul E. McKenney extern int rcu_cpu_stall_ftrace_dump; 2244102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress; 22510462d6fSPaul E. McKenney extern int rcu_cpu_stall_timeout; 22628b3ae42SUladzislau Rezki extern int rcu_exp_cpu_stall_timeout; 227be42f00bSZhen Lei extern int rcu_cpu_stall_cputime; 22892987fe8SPaul E. McKenney extern bool rcu_exp_stall_task_details __read_mostly; 2294102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void); 23028b3ae42SUladzislau Rezki int rcu_exp_jiffies_till_stall_check(void); 2314102adabSPaul E. McKenney 23258c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void) 23358c53360SPaul E. McKenney { 23458c53360SPaul E. McKenney return rcu_stall_is_suppressed_at_boot() || rcu_cpu_stall_suppress; 23558c53360SPaul E. McKenney } 23658c53360SPaul E. McKenney 237f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \ 238f22ce091SPaul E. McKenney do { \ 239f22ce091SPaul E. McKenney if (!rcu_cpu_stall_suppress) \ 240f22ce091SPaul E. McKenney rcu_cpu_stall_suppress = 3; \ 241f22ce091SPaul E. McKenney } while (0) 242f22ce091SPaul E. McKenney 243f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \ 244f22ce091SPaul E. McKenney do { \ 245f22ce091SPaul E. McKenney if (rcu_cpu_stall_suppress == 3) \ 246f22ce091SPaul E. McKenney rcu_cpu_stall_suppress = 0; \ 247f22ce091SPaul E. McKenney } while (0) 248f22ce091SPaul E. McKenney 249f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */ 25058c53360SPaul E. McKenney 25158c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void) 25258c53360SPaul E. McKenney { 25358c53360SPaul E. McKenney return rcu_stall_is_suppressed_at_boot(); 25458c53360SPaul E. McKenney } 255f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() 256f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() 2574102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */ 2584102adabSPaul E. McKenney 2594102adabSPaul E. McKenney /* 2604102adabSPaul E. McKenney * Strings used in tracepoints need to be exported via the 2614102adabSPaul E. McKenney * tracing system such that tools like perf and trace-cmd can 2624102adabSPaul E. McKenney * translate the string address pointers to actual text. 2634102adabSPaul E. McKenney */ 2644102adabSPaul E. McKenney #define TPS(x) tracepoint_string(x) 2654102adabSPaul E. McKenney 266b8989b76SPaul E. McKenney /* 267b8989b76SPaul E. McKenney * Dump the ftrace buffer, but only one time per callsite per boot. 268b8989b76SPaul E. McKenney */ 269b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \ 270b8989b76SPaul E. McKenney do { \ 271b8989b76SPaul E. McKenney static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \ 272b8989b76SPaul E. McKenney \ 273b8989b76SPaul E. McKenney if (!atomic_read(&___rfd_beenhere) && \ 27483b6ca1fSPaul E. McKenney !atomic_xchg(&___rfd_beenhere, 1)) { \ 27583b6ca1fSPaul E. McKenney tracing_off(); \ 276f22ce091SPaul E. McKenney rcu_ftrace_dump_stall_suppress(); \ 277b8989b76SPaul E. McKenney ftrace_dump(oops_dump_mode); \ 278f22ce091SPaul E. McKenney rcu_ftrace_dump_stall_unsuppress(); \ 27983b6ca1fSPaul E. McKenney } \ 280b8989b76SPaul E. McKenney } while (0) 281b8989b76SPaul E. McKenney 282aa23c6fbSPranith Kumar void rcu_early_boot_tests(void); 28352d7e48bSPaul E. McKenney void rcu_test_sync_prims(void); 284aa23c6fbSPranith Kumar 2855f6130faSLai Jiangshan /* 2865f6130faSLai Jiangshan * This function really isn't for public consumption, but RCU is special in 2875f6130faSLai Jiangshan * that context switches can allow the state machine to make progress. 2885f6130faSLai Jiangshan */ 2895f6130faSLai Jiangshan extern void resched_cpu(int cpu); 2905f6130faSLai Jiangshan 2910cd7e350SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) 2922b34c43cSPaul E. McKenney 2932b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h> 2942b34c43cSPaul E. McKenney 2952b34c43cSPaul E. McKenney extern int rcu_num_lvls; 296e95d68d2SPaul E. McKenney extern int num_rcu_lvl[]; 2972b34c43cSPaul E. McKenney extern int rcu_num_nodes; 2982b34c43cSPaul E. McKenney static bool rcu_fanout_exact; 2992b34c43cSPaul E. McKenney static int rcu_fanout_leaf; 3002b34c43cSPaul E. McKenney 3012b34c43cSPaul E. McKenney /* 3022b34c43cSPaul E. McKenney * Compute the per-level fanout, either using the exact fanout specified 3032b34c43cSPaul E. McKenney * or balancing the tree, depending on the rcu_fanout_exact boot parameter. 3042b34c43cSPaul E. McKenney */ 3052b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt) 3062b34c43cSPaul E. McKenney { 3072b34c43cSPaul E. McKenney int i; 3082b34c43cSPaul E. McKenney 30936b5dae6SPaul E. McKenney for (i = 0; i < RCU_NUM_LVLS; i++) 31036b5dae6SPaul E. McKenney levelspread[i] = INT_MIN; 3112b34c43cSPaul E. McKenney if (rcu_fanout_exact) { 3122b34c43cSPaul E. McKenney levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf; 3132b34c43cSPaul E. McKenney for (i = rcu_num_lvls - 2; i >= 0; i--) 3142b34c43cSPaul E. McKenney levelspread[i] = RCU_FANOUT; 3152b34c43cSPaul E. McKenney } else { 3162b34c43cSPaul E. McKenney int ccur; 3172b34c43cSPaul E. McKenney int cprv; 3182b34c43cSPaul E. McKenney 3192b34c43cSPaul E. McKenney cprv = nr_cpu_ids; 3202b34c43cSPaul E. McKenney for (i = rcu_num_lvls - 1; i >= 0; i--) { 3212b34c43cSPaul E. McKenney ccur = levelcnt[i]; 3222b34c43cSPaul E. McKenney levelspread[i] = (cprv + ccur - 1) / ccur; 3232b34c43cSPaul E. McKenney cprv = ccur; 3242b34c43cSPaul E. McKenney } 3252b34c43cSPaul E. McKenney } 3262b34c43cSPaul E. McKenney } 3272b34c43cSPaul E. McKenney 328b5befe84SFrederic Weisbecker extern void rcu_init_geometry(void); 329b5befe84SFrederic Weisbecker 3307f87c036SPaul E. McKenney /* Returns a pointer to the first leaf rcu_node structure. */ 331aedf4ba9SPaul E. McKenney #define rcu_first_leaf_node() (rcu_state.level[rcu_num_lvls - 1]) 3325b4c11d5SPaul E. McKenney 3335b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */ 3345b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1) 3355b4c11d5SPaul E. McKenney 3365257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */ 337aedf4ba9SPaul E. McKenney #define rcu_is_last_leaf_node(rnp) ((rnp) == &rcu_state.node[rcu_num_nodes - 1]) 3385257514dSPaul E. McKenney 339efbe451dSPaul E. McKenney /* 340aedf4ba9SPaul E. McKenney * Do a full breadth-first scan of the {s,}rcu_node structures for the 3417f87c036SPaul E. McKenney * specified state structure (for SRCU) or the only rcu_state structure 3427f87c036SPaul E. McKenney * (for RCU). 343efbe451dSPaul E. McKenney */ 344*95433f72SPaul E. McKenney #define _rcu_for_each_node_breadth_first(sp, rnp) \ 345aedf4ba9SPaul E. McKenney for ((rnp) = &(sp)->node[0]; \ 346aedf4ba9SPaul E. McKenney (rnp) < &(sp)->node[rcu_num_nodes]; (rnp)++) 347aedf4ba9SPaul E. McKenney #define rcu_for_each_node_breadth_first(rnp) \ 348*95433f72SPaul E. McKenney _rcu_for_each_node_breadth_first(&rcu_state, rnp) 349*95433f72SPaul E. McKenney #define srcu_for_each_node_breadth_first(ssp, rnp) \ 350*95433f72SPaul E. McKenney _rcu_for_each_node_breadth_first(ssp->srcu_sup, rnp) 351efbe451dSPaul E. McKenney 352efbe451dSPaul E. McKenney /* 3537f87c036SPaul E. McKenney * Scan the leaves of the rcu_node hierarchy for the rcu_state structure. 3547f87c036SPaul E. McKenney * Note that if there is a singleton rcu_node tree with but one rcu_node 3557f87c036SPaul E. McKenney * structure, this loop -will- visit the rcu_node structure. It is still 3567f87c036SPaul E. McKenney * a leaf node, even if it is also the root node. 357efbe451dSPaul E. McKenney */ 358aedf4ba9SPaul E. McKenney #define rcu_for_each_leaf_node(rnp) \ 359aedf4ba9SPaul E. McKenney for ((rnp) = rcu_first_leaf_node(); \ 360aedf4ba9SPaul E. McKenney (rnp) < &rcu_state.node[rcu_num_nodes]; (rnp)++) 361efbe451dSPaul E. McKenney 362efbe451dSPaul E. McKenney /* 363efbe451dSPaul E. McKenney * Iterate over all possible CPUs in a leaf RCU node. 364efbe451dSPaul E. McKenney */ 365efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \ 36682dd8419SPaul E. McKenney for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \ 36782dd8419SPaul E. McKenney (cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \ 36865963d24SPaul E. McKenney (cpu) <= rnp->grphi; \ 36965963d24SPaul E. McKenney (cpu) = cpumask_next((cpu), cpu_possible_mask)) 37065963d24SPaul E. McKenney 37165963d24SPaul E. McKenney /* 37265963d24SPaul E. McKenney * Iterate over all CPUs in a leaf RCU node's specified mask. 37365963d24SPaul E. McKenney */ 37465963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \ 37565963d24SPaul E. McKenney ((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu))) 37665963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \ 37782dd8419SPaul E. McKenney for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \ 37882dd8419SPaul E. McKenney (cpu) = rcu_find_next_bit((rnp), 0, (mask)); \ 37965963d24SPaul E. McKenney (cpu) <= rnp->grphi; \ 38065963d24SPaul E. McKenney (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask))) 381efbe451dSPaul E. McKenney 3820cd7e350SPaul E. McKenney #endif /* !defined(CONFIG_TINY_RCU) */ 3830cd7e350SPaul E. McKenney 3840cd7e350SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_TASKS_RCU_GENERIC) 3850cd7e350SPaul E. McKenney 38683d40bd3SPaul E. McKenney /* 38783d40bd3SPaul E. McKenney * Wrappers for the rcu_node::lock acquire and release. 38883d40bd3SPaul E. McKenney * 38983d40bd3SPaul E. McKenney * Because the rcu_nodes form a tree, the tree traversal locking will observe 39083d40bd3SPaul E. McKenney * different lock values, this in turn means that an UNLOCK of one level 39183d40bd3SPaul E. McKenney * followed by a LOCK of another level does not imply a full memory barrier; 39283d40bd3SPaul E. McKenney * and most importantly transitivity is lost. 39383d40bd3SPaul E. McKenney * 39483d40bd3SPaul E. McKenney * In order to restore full ordering between tree levels, augment the regular 39583d40bd3SPaul E. McKenney * lock acquire functions with smp_mb__after_unlock_lock(). 39683d40bd3SPaul E. McKenney * 39783d40bd3SPaul E. McKenney * As ->lock of struct rcu_node is a __private field, therefore one should use 39883d40bd3SPaul E. McKenney * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock. 39983d40bd3SPaul E. McKenney */ 40083d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p) \ 40183d40bd3SPaul E. McKenney do { \ 40283d40bd3SPaul E. McKenney raw_spin_lock(&ACCESS_PRIVATE(p, lock)); \ 40383d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 40483d40bd3SPaul E. McKenney } while (0) 40583d40bd3SPaul E. McKenney 4067dffe017SPaul E. McKenney #define raw_spin_unlock_rcu_node(p) \ 4077dffe017SPaul E. McKenney do { \ 4087dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 4097dffe017SPaul E. McKenney raw_spin_unlock(&ACCESS_PRIVATE(p, lock)); \ 4107dffe017SPaul E. McKenney } while (0) 41183d40bd3SPaul E. McKenney 41283d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p) \ 41383d40bd3SPaul E. McKenney do { \ 41483d40bd3SPaul E. McKenney raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \ 41583d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 41683d40bd3SPaul E. McKenney } while (0) 41783d40bd3SPaul E. McKenney 41883d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p) \ 4197dffe017SPaul E. McKenney do { \ 4207dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 4217dffe017SPaul E. McKenney raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock)); \ 4227dffe017SPaul E. McKenney } while (0) 42383d40bd3SPaul E. McKenney 4244e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags) \ 42583d40bd3SPaul E. McKenney do { \ 4264e4bea74SPaul E. McKenney raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \ 42783d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 42883d40bd3SPaul E. McKenney } while (0) 42983d40bd3SPaul E. McKenney 4304e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags) \ 4317dffe017SPaul E. McKenney do { \ 4327dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 4337dffe017SPaul E. McKenney raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags); \ 4347dffe017SPaul E. McKenney } while (0) 43583d40bd3SPaul E. McKenney 43683d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p) \ 43783d40bd3SPaul E. McKenney ({ \ 43883d40bd3SPaul E. McKenney bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock)); \ 43983d40bd3SPaul E. McKenney \ 44083d40bd3SPaul E. McKenney if (___locked) \ 44183d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 44283d40bd3SPaul E. McKenney ___locked; \ 44383d40bd3SPaul E. McKenney }) 44483d40bd3SPaul E. McKenney 445a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p) \ 446a32e01eeSMatthew Wilcox lockdep_assert_held(&ACCESS_PRIVATE(p, lock)) 447a32e01eeSMatthew Wilcox 4480cd7e350SPaul E. McKenney #endif // #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_TASKS_RCU_GENERIC) 4492b34c43cSPaul E. McKenney 45025c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU 45125c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */ 4527414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; } 4537414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; } 4546efdda8bSJoel Fernandes (Google) static inline bool rcu_async_should_hurry(void) { return false; } 4557414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { } 4567414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { } 4576efdda8bSJoel Fernandes (Google) static inline void rcu_async_hurry(void) { } 4586efdda8bSJoel Fernandes (Google) static inline void rcu_async_relax(void) { } 459bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { } 46025c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */ 46125c36329SPaul E. McKenney bool rcu_gp_is_normal(void); /* Internal RCU use. */ 46225c36329SPaul E. McKenney bool rcu_gp_is_expedited(void); /* Internal RCU use. */ 4636efdda8bSJoel Fernandes (Google) bool rcu_async_should_hurry(void); /* Internal RCU use. */ 46425c36329SPaul E. McKenney void rcu_expedite_gp(void); 46525c36329SPaul E. McKenney void rcu_unexpedite_gp(void); 4666efdda8bSJoel Fernandes (Google) void rcu_async_hurry(void); 4676efdda8bSJoel Fernandes (Google) void rcu_async_relax(void); 46825c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void); 469474d0997SPaul E. McKenney #ifdef CONFIG_TASKS_RCU_GENERIC 470e21408ceSPaul E. McKenney void show_rcu_tasks_gp_kthreads(void); 471474d0997SPaul E. McKenney #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */ 472474d0997SPaul E. McKenney static inline void show_rcu_tasks_gp_kthreads(void) {} 473474d0997SPaul E. McKenney #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */ 474bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t); 47525c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */ 47625c36329SPaul E. McKenney 47782118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE 0 47882118249SPaul E. McKenney #define RCU_SCHEDULER_INIT 1 47982118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING 2 48082118249SPaul E. McKenney 481cad7b389SPaul E. McKenney enum rcutorture_type { 482cad7b389SPaul E. McKenney RCU_FLAVOR, 483cad7b389SPaul E. McKenney RCU_TASKS_FLAVOR, 4843d6e43c7SPaul E. McKenney RCU_TASKS_RUDE_FLAVOR, 485c1a76c0bSPaul E. McKenney RCU_TASKS_TRACING_FLAVOR, 486c682db55SPaul E. McKenney RCU_TRIVIAL_FLAVOR, 487cad7b389SPaul E. McKenney SRCU_FLAVOR, 488cad7b389SPaul E. McKenney INVALID_RCU_FLAVOR 489cad7b389SPaul E. McKenney }; 490cad7b389SPaul E. McKenney 4913cb278e7SJoel Fernandes (Google) #if defined(CONFIG_RCU_LAZY) 4923cb278e7SJoel Fernandes (Google) unsigned long rcu_lazy_get_jiffies_till_flush(void); 4933cb278e7SJoel Fernandes (Google) void rcu_lazy_set_jiffies_till_flush(unsigned long j); 4943cb278e7SJoel Fernandes (Google) #else 4953cb278e7SJoel Fernandes (Google) static inline unsigned long rcu_lazy_get_jiffies_till_flush(void) { return 0; } 4963cb278e7SJoel Fernandes (Google) static inline void rcu_lazy_set_jiffies_till_flush(unsigned long j) { } 4973cb278e7SJoel Fernandes (Google) #endif 4983cb278e7SJoel Fernandes (Google) 499b3e627d3SLai Jiangshan #if defined(CONFIG_TREE_RCU) 500cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags, 501aebc8264SPaul E. McKenney unsigned long *gp_seq); 502cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename, 503cad7b389SPaul E. McKenney struct rcu_head *rhp, 504cad7b389SPaul E. McKenney unsigned long secs, 505cad7b389SPaul E. McKenney unsigned long c_old, 506cad7b389SPaul E. McKenney unsigned long c); 50755b2dcf5SPaul E. McKenney void rcu_gp_set_torture_wait(int duration); 508cad7b389SPaul E. McKenney #else 509cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type, 510aebc8264SPaul E. McKenney int *flags, unsigned long *gp_seq) 511cad7b389SPaul E. McKenney { 512cad7b389SPaul E. McKenney *flags = 0; 513aebc8264SPaul E. McKenney *gp_seq = 0; 514cad7b389SPaul E. McKenney } 515cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE 516cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename, 517cad7b389SPaul E. McKenney struct rcu_head *rhp, 518cad7b389SPaul E. McKenney unsigned long secs, 519cad7b389SPaul E. McKenney unsigned long c_old, 520cad7b389SPaul E. McKenney unsigned long c); 521cad7b389SPaul E. McKenney #else 522cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \ 523cad7b389SPaul E. McKenney do { } while (0) 524cad7b389SPaul E. McKenney #endif 52555b2dcf5SPaul E. McKenney static inline void rcu_gp_set_torture_wait(int duration) { } 526cad7b389SPaul E. McKenney #endif 527cad7b389SPaul E. McKenney 528c682db55SPaul E. McKenney #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST) 529c682db55SPaul E. McKenney long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask); 530c682db55SPaul E. McKenney #endif 531c682db55SPaul E. McKenney 532cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU 533cad7b389SPaul E. McKenney 534cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type, 535cad7b389SPaul E. McKenney struct srcu_struct *sp, int *flags, 536aebc8264SPaul E. McKenney unsigned long *gp_seq) 537cad7b389SPaul E. McKenney { 538cad7b389SPaul E. McKenney if (test_type != SRCU_FLAVOR) 539cad7b389SPaul E. McKenney return; 540cad7b389SPaul E. McKenney *flags = 0; 541aebc8264SPaul E. McKenney *gp_seq = sp->srcu_idx; 542cad7b389SPaul E. McKenney } 543cad7b389SPaul E. McKenney 544cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU) 545cad7b389SPaul E. McKenney 546cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type, 547cad7b389SPaul E. McKenney struct srcu_struct *sp, int *flags, 548aebc8264SPaul E. McKenney unsigned long *gp_seq); 549cad7b389SPaul E. McKenney 550cad7b389SPaul E. McKenney #endif 551cad7b389SPaul E. McKenney 552e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU 5537d0c9c50SPaul E. McKenney static inline bool rcu_dynticks_zero_in_eqs(int cpu, int *vp) { return false; } 55417ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; } 5557414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(void) { return 0; } 5567414fac0SPaul E. McKenney static inline unsigned long 5577414fac0SPaul E. McKenney srcu_batches_completed(struct srcu_struct *sp) { return 0; } 5587414fac0SPaul E. McKenney static inline void rcu_force_quiescent_state(void) { } 5590260b92eSPaul E. McKenney static inline bool rcu_check_boost_fail(unsigned long gp_state, int *cpup) { return true; } 5607414fac0SPaul E. McKenney static inline void show_rcu_gp_kthreads(void) { } 5614babd855SJoel Fernandes (Google) static inline int rcu_get_gp_kthreads_prio(void) { return 0; } 562e0aff973SPaul E. McKenney static inline void rcu_fwd_progress_check(unsigned long j) { } 56399d6a2acSPaul E. McKenney static inline void rcu_gp_slow_register(atomic_t *rgssp) { } 56499d6a2acSPaul E. McKenney static inline void rcu_gp_slow_unregister(atomic_t *rgssp) { } 565e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */ 5667d0c9c50SPaul E. McKenney bool rcu_dynticks_zero_in_eqs(int cpu, int *vp); 56717ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void); 568e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void); 5695a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp); 5700260b92eSPaul E. McKenney bool rcu_check_boost_fail(unsigned long gp_state, int *cpup); 571e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void); 5724babd855SJoel Fernandes (Google) int rcu_get_gp_kthreads_prio(void); 573e0aff973SPaul E. McKenney void rcu_fwd_progress_check(unsigned long j); 574e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void); 575ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq; 5769621fbeeSKalesh Singh #ifdef CONFIG_RCU_EXP_KTHREAD 5779621fbeeSKalesh Singh extern struct kthread_worker *rcu_exp_gp_kworker; 5789621fbeeSKalesh Singh extern struct kthread_worker *rcu_exp_par_gp_kworker; 5799621fbeeSKalesh Singh #else /* !CONFIG_RCU_EXP_KTHREAD */ 58025f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq; 5819621fbeeSKalesh Singh #endif /* CONFIG_RCU_EXP_KTHREAD */ 58299d6a2acSPaul E. McKenney void rcu_gp_slow_register(atomic_t *rgssp); 58399d6a2acSPaul E. McKenney void rcu_gp_slow_unregister(atomic_t *rgssp); 584e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */ 585e3c8d51eSPaul E. McKenney 58644c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU 5875ab7ab83SPaul E. McKenney void rcu_bind_current_to_nocb(void); 5883d54f798SPaul E. McKenney #else 5895ab7ab83SPaul E. McKenney static inline void rcu_bind_current_to_nocb(void) { } 5903d54f798SPaul E. McKenney #endif 5913d54f798SPaul E. McKenney 59227c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_RCU) 59327c0f144SPaul E. McKenney void show_rcu_tasks_classic_gp_kthread(void); 59427c0f144SPaul E. McKenney #else 59527c0f144SPaul E. McKenney static inline void show_rcu_tasks_classic_gp_kthread(void) {} 59627c0f144SPaul E. McKenney #endif 59727c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_RUDE_RCU) 59827c0f144SPaul E. McKenney void show_rcu_tasks_rude_gp_kthread(void); 59927c0f144SPaul E. McKenney #else 60027c0f144SPaul E. McKenney static inline void show_rcu_tasks_rude_gp_kthread(void) {} 60127c0f144SPaul E. McKenney #endif 60227c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_TRACE_RCU) 60327c0f144SPaul E. McKenney void show_rcu_tasks_trace_gp_kthread(void); 60427c0f144SPaul E. McKenney #else 60527c0f144SPaul E. McKenney static inline void show_rcu_tasks_trace_gp_kthread(void) {} 60627c0f144SPaul E. McKenney #endif 60727c0f144SPaul E. McKenney 6084102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */ 609