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 15c2d8089dSPaul E. McKenney /* Offset to allow distinguishing irq vs. task-based idle entry/exit. */ 1684585aa8SPaul E. McKenney #define DYNTICK_IRQ_NONIDLE ((LONG_MAX / 2) + 1) 176136d6e4SPaul E. McKenney 182e8c28c2SPaul E. McKenney 192e8c28c2SPaul E. McKenney /* 202e8c28c2SPaul E. McKenney * Grace-period counter management. 212e8c28c2SPaul E. McKenney */ 222e8c28c2SPaul E. McKenney 23f1ec57a4SPaul E. McKenney #define RCU_SEQ_CTR_SHIFT 2 24031aeee0SPaul E. McKenney #define RCU_SEQ_STATE_MASK ((1 << RCU_SEQ_CTR_SHIFT) - 1) 25031aeee0SPaul E. McKenney 26031aeee0SPaul E. McKenney /* 27031aeee0SPaul E. McKenney * Return the counter portion of a sequence number previously returned 28031aeee0SPaul E. McKenney * by rcu_seq_snap() or rcu_seq_current(). 29031aeee0SPaul E. McKenney */ 30031aeee0SPaul E. McKenney static inline unsigned long rcu_seq_ctr(unsigned long s) 31031aeee0SPaul E. McKenney { 32031aeee0SPaul E. McKenney return s >> RCU_SEQ_CTR_SHIFT; 33031aeee0SPaul E. McKenney } 34031aeee0SPaul E. McKenney 35031aeee0SPaul E. McKenney /* 36031aeee0SPaul E. McKenney * Return the state portion of a sequence number previously returned 37031aeee0SPaul E. McKenney * by rcu_seq_snap() or rcu_seq_current(). 38031aeee0SPaul E. McKenney */ 39031aeee0SPaul E. McKenney static inline int rcu_seq_state(unsigned long s) 40031aeee0SPaul E. McKenney { 41031aeee0SPaul E. McKenney return s & RCU_SEQ_STATE_MASK; 42031aeee0SPaul E. McKenney } 43031aeee0SPaul E. McKenney 4480a7956fSPaul E. McKenney /* 4580a7956fSPaul E. McKenney * Set the state portion of the pointed-to sequence number. 4680a7956fSPaul E. McKenney * The caller is responsible for preventing conflicting updates. 4780a7956fSPaul E. McKenney */ 4880a7956fSPaul E. McKenney static inline void rcu_seq_set_state(unsigned long *sp, int newstate) 4980a7956fSPaul E. McKenney { 5080a7956fSPaul E. McKenney WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK); 5180a7956fSPaul E. McKenney WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate); 5280a7956fSPaul E. McKenney } 5380a7956fSPaul E. McKenney 542e8c28c2SPaul E. McKenney /* Adjust sequence number for start of update-side operation. */ 552e8c28c2SPaul E. McKenney static inline void rcu_seq_start(unsigned long *sp) 562e8c28c2SPaul E. McKenney { 572e8c28c2SPaul E. McKenney WRITE_ONCE(*sp, *sp + 1); 582e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation after counter increment. */ 59031aeee0SPaul E. McKenney WARN_ON_ONCE(rcu_seq_state(*sp) != 1); 602e8c28c2SPaul E. McKenney } 612e8c28c2SPaul E. McKenney 629a414201SPaul E. McKenney /* Compute the end-of-grace-period value for the specified sequence number. */ 639a414201SPaul E. McKenney static inline unsigned long rcu_seq_endval(unsigned long *sp) 649a414201SPaul E. McKenney { 659a414201SPaul E. McKenney return (*sp | RCU_SEQ_STATE_MASK) + 1; 669a414201SPaul E. McKenney } 679a414201SPaul E. McKenney 682e8c28c2SPaul E. McKenney /* Adjust sequence number for end of update-side operation. */ 692e8c28c2SPaul E. McKenney static inline void rcu_seq_end(unsigned long *sp) 702e8c28c2SPaul E. McKenney { 712e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation before counter increment. */ 72031aeee0SPaul E. McKenney WARN_ON_ONCE(!rcu_seq_state(*sp)); 739a414201SPaul E. McKenney WRITE_ONCE(*sp, rcu_seq_endval(sp)); 742e8c28c2SPaul E. McKenney } 752e8c28c2SPaul E. McKenney 760d805a70SJoel Fernandes (Google) /* 770d805a70SJoel Fernandes (Google) * rcu_seq_snap - Take a snapshot of the update side's sequence number. 780d805a70SJoel Fernandes (Google) * 790d805a70SJoel Fernandes (Google) * This function returns the earliest value of the grace-period sequence number 800d805a70SJoel Fernandes (Google) * that will indicate that a full grace period has elapsed since the current 810d805a70SJoel Fernandes (Google) * time. Once the grace-period sequence number has reached this value, it will 820d805a70SJoel Fernandes (Google) * be safe to invoke all callbacks that have been registered prior to the 830d805a70SJoel Fernandes (Google) * current time. This value is the current grace-period number plus two to the 840d805a70SJoel Fernandes (Google) * power of the number of low-order bits reserved for state, then rounded up to 850d805a70SJoel Fernandes (Google) * the next value in which the state bits are all zero. 860d805a70SJoel Fernandes (Google) */ 872e8c28c2SPaul E. McKenney static inline unsigned long rcu_seq_snap(unsigned long *sp) 882e8c28c2SPaul E. McKenney { 892e8c28c2SPaul E. McKenney unsigned long s; 902e8c28c2SPaul E. McKenney 91031aeee0SPaul E. McKenney s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK; 922e8c28c2SPaul E. McKenney smp_mb(); /* Above access must not bleed into critical section. */ 932e8c28c2SPaul E. McKenney return s; 942e8c28c2SPaul E. McKenney } 952e8c28c2SPaul E. McKenney 968660b7d8SPaul E. McKenney /* Return the current value the update side's sequence number, no ordering. */ 978660b7d8SPaul E. McKenney static inline unsigned long rcu_seq_current(unsigned long *sp) 988660b7d8SPaul E. McKenney { 998660b7d8SPaul E. McKenney return READ_ONCE(*sp); 1008660b7d8SPaul E. McKenney } 1018660b7d8SPaul E. McKenney 1022e8c28c2SPaul E. McKenney /* 1032e3e5e55SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not the 1042e3e5e55SPaul E. McKenney * corresponding update-side operation has started. 1052e3e5e55SPaul E. McKenney */ 1062e3e5e55SPaul E. McKenney static inline bool rcu_seq_started(unsigned long *sp, unsigned long s) 1072e3e5e55SPaul E. McKenney { 1082e3e5e55SPaul E. McKenney return ULONG_CMP_LT((s - 1) & ~RCU_SEQ_STATE_MASK, READ_ONCE(*sp)); 1092e3e5e55SPaul E. McKenney } 1102e3e5e55SPaul E. McKenney 1112e3e5e55SPaul E. McKenney /* 1122e8c28c2SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not a 1132e8c28c2SPaul E. McKenney * full update-side operation has occurred. 1142e8c28c2SPaul E. McKenney */ 1152e8c28c2SPaul E. McKenney static inline bool rcu_seq_done(unsigned long *sp, unsigned long s) 1162e8c28c2SPaul E. McKenney { 1172e8c28c2SPaul E. McKenney return ULONG_CMP_GE(READ_ONCE(*sp), s); 1182e8c28c2SPaul E. McKenney } 1192e8c28c2SPaul E. McKenney 1204102adabSPaul E. McKenney /* 12167e14c1eSPaul E. McKenney * Has a grace period completed since the time the old gp_seq was collected? 12267e14c1eSPaul E. McKenney */ 12367e14c1eSPaul E. McKenney static inline bool rcu_seq_completed_gp(unsigned long old, unsigned long new) 12467e14c1eSPaul E. McKenney { 12567e14c1eSPaul E. McKenney return ULONG_CMP_LT(old, new & ~RCU_SEQ_STATE_MASK); 12667e14c1eSPaul E. McKenney } 12767e14c1eSPaul E. McKenney 12867e14c1eSPaul E. McKenney /* 12967e14c1eSPaul E. McKenney * Has a grace period started since the time the old gp_seq was collected? 13067e14c1eSPaul E. McKenney */ 13167e14c1eSPaul E. McKenney static inline bool rcu_seq_new_gp(unsigned long old, unsigned long new) 13267e14c1eSPaul E. McKenney { 13367e14c1eSPaul E. McKenney return ULONG_CMP_LT((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK, 13467e14c1eSPaul E. McKenney new); 13567e14c1eSPaul E. McKenney } 13667e14c1eSPaul E. McKenney 13767e14c1eSPaul E. McKenney /* 138d7219312SPaul E. McKenney * Roughly how many full grace periods have elapsed between the collection 139d7219312SPaul E. McKenney * of the two specified grace periods? 140d7219312SPaul E. McKenney */ 141d7219312SPaul E. McKenney static inline unsigned long rcu_seq_diff(unsigned long new, unsigned long old) 142d7219312SPaul E. McKenney { 1432ee5aca5SPaul E. McKenney unsigned long rnd_diff; 1442ee5aca5SPaul E. McKenney 1452ee5aca5SPaul E. McKenney if (old == new) 1462ee5aca5SPaul E. McKenney return 0; 1472ee5aca5SPaul E. McKenney /* 1482ee5aca5SPaul E. McKenney * Compute the number of grace periods (still shifted up), plus 1492ee5aca5SPaul E. McKenney * one if either of new and old is not an exact grace period. 1502ee5aca5SPaul E. McKenney */ 1512ee5aca5SPaul E. McKenney rnd_diff = (new & ~RCU_SEQ_STATE_MASK) - 1522ee5aca5SPaul E. McKenney ((old + RCU_SEQ_STATE_MASK) & ~RCU_SEQ_STATE_MASK) + 1532ee5aca5SPaul E. McKenney ((new & RCU_SEQ_STATE_MASK) || (old & RCU_SEQ_STATE_MASK)); 1542ee5aca5SPaul E. McKenney if (ULONG_CMP_GE(RCU_SEQ_STATE_MASK, rnd_diff)) 1552ee5aca5SPaul E. McKenney return 1; /* Definitely no grace period has elapsed. */ 1562ee5aca5SPaul E. McKenney return ((rnd_diff - RCU_SEQ_STATE_MASK - 1) >> RCU_SEQ_CTR_SHIFT) + 2; 157d7219312SPaul E. McKenney } 158d7219312SPaul E. McKenney 159d7219312SPaul E. McKenney /* 1604102adabSPaul E. McKenney * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally 1617f87c036SPaul E. McKenney * by call_rcu() and rcu callback execution, and are therefore not part 1627f87c036SPaul E. McKenney * of the RCU API. These are in rcupdate.h because they are used by all 1637f87c036SPaul E. McKenney * RCU implementations. 1644102adabSPaul E. McKenney */ 1654102adabSPaul E. McKenney 1664102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 1674102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY 0 1684102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED 1 1694102adabSPaul E. McKenney 170f9e62f31SStephen Boyd extern const struct debug_obj_descr rcuhead_debug_descr; 1714102adabSPaul E. McKenney 1724102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 1734102adabSPaul E. McKenney { 1744102adabSPaul E. McKenney int r1; 1754102adabSPaul E. McKenney 1764102adabSPaul E. McKenney r1 = debug_object_activate(head, &rcuhead_debug_descr); 1774102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1784102adabSPaul E. McKenney STATE_RCU_HEAD_READY, 1794102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED); 1804102adabSPaul E. McKenney return r1; 1814102adabSPaul E. McKenney } 1824102adabSPaul E. McKenney 1834102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head) 1844102adabSPaul E. McKenney { 1854102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1864102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED, 1874102adabSPaul E. McKenney STATE_RCU_HEAD_READY); 1884102adabSPaul E. McKenney debug_object_deactivate(head, &rcuhead_debug_descr); 1894102adabSPaul E. McKenney } 1904102adabSPaul E. McKenney #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 1914102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 1924102adabSPaul E. McKenney { 1934102adabSPaul E. McKenney return 0; 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 } 1994102adabSPaul E. McKenney #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 2004102adabSPaul E. McKenney 20158c53360SPaul E. McKenney extern int rcu_cpu_stall_suppress_at_boot; 20258c53360SPaul E. McKenney 20358c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed_at_boot(void) 20458c53360SPaul E. McKenney { 20558c53360SPaul E. McKenney return rcu_cpu_stall_suppress_at_boot && !rcu_inkernel_boot_has_ended(); 20658c53360SPaul E. McKenney } 20758c53360SPaul E. McKenney 2084102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON 2094102adabSPaul E. McKenney 210cdc694b2SPaul E. McKenney extern int rcu_cpu_stall_ftrace_dump; 2114102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress; 21210462d6fSPaul E. McKenney extern int rcu_cpu_stall_timeout; 21328b3ae42SUladzislau Rezki extern int rcu_exp_cpu_stall_timeout; 2144102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void); 21528b3ae42SUladzislau Rezki int rcu_exp_jiffies_till_stall_check(void); 2164102adabSPaul E. McKenney 21758c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void) 21858c53360SPaul E. McKenney { 21958c53360SPaul E. McKenney return rcu_stall_is_suppressed_at_boot() || rcu_cpu_stall_suppress; 22058c53360SPaul E. McKenney } 22158c53360SPaul E. McKenney 222f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() \ 223f22ce091SPaul E. McKenney do { \ 224f22ce091SPaul E. McKenney if (!rcu_cpu_stall_suppress) \ 225f22ce091SPaul E. McKenney rcu_cpu_stall_suppress = 3; \ 226f22ce091SPaul E. McKenney } while (0) 227f22ce091SPaul E. McKenney 228f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() \ 229f22ce091SPaul E. McKenney do { \ 230f22ce091SPaul E. McKenney if (rcu_cpu_stall_suppress == 3) \ 231f22ce091SPaul E. McKenney rcu_cpu_stall_suppress = 0; \ 232f22ce091SPaul E. McKenney } while (0) 233f22ce091SPaul E. McKenney 234f22ce091SPaul E. McKenney #else /* #endif #ifdef CONFIG_RCU_STALL_COMMON */ 23558c53360SPaul E. McKenney 23658c53360SPaul E. McKenney static inline bool rcu_stall_is_suppressed(void) 23758c53360SPaul E. McKenney { 23858c53360SPaul E. McKenney return rcu_stall_is_suppressed_at_boot(); 23958c53360SPaul E. McKenney } 240f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_suppress() 241f22ce091SPaul E. McKenney #define rcu_ftrace_dump_stall_unsuppress() 2424102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */ 2434102adabSPaul E. McKenney 2444102adabSPaul E. McKenney /* 2454102adabSPaul E. McKenney * Strings used in tracepoints need to be exported via the 2464102adabSPaul E. McKenney * tracing system such that tools like perf and trace-cmd can 2474102adabSPaul E. McKenney * translate the string address pointers to actual text. 2484102adabSPaul E. McKenney */ 2494102adabSPaul E. McKenney #define TPS(x) tracepoint_string(x) 2504102adabSPaul E. McKenney 251b8989b76SPaul E. McKenney /* 252b8989b76SPaul E. McKenney * Dump the ftrace buffer, but only one time per callsite per boot. 253b8989b76SPaul E. McKenney */ 254b8989b76SPaul E. McKenney #define rcu_ftrace_dump(oops_dump_mode) \ 255b8989b76SPaul E. McKenney do { \ 256b8989b76SPaul E. McKenney static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \ 257b8989b76SPaul E. McKenney \ 258b8989b76SPaul E. McKenney if (!atomic_read(&___rfd_beenhere) && \ 25983b6ca1fSPaul E. McKenney !atomic_xchg(&___rfd_beenhere, 1)) { \ 26083b6ca1fSPaul E. McKenney tracing_off(); \ 261f22ce091SPaul E. McKenney rcu_ftrace_dump_stall_suppress(); \ 262b8989b76SPaul E. McKenney ftrace_dump(oops_dump_mode); \ 263f22ce091SPaul E. McKenney rcu_ftrace_dump_stall_unsuppress(); \ 26483b6ca1fSPaul E. McKenney } \ 265b8989b76SPaul E. McKenney } while (0) 266b8989b76SPaul E. McKenney 267aa23c6fbSPranith Kumar void rcu_early_boot_tests(void); 26852d7e48bSPaul E. McKenney void rcu_test_sync_prims(void); 269aa23c6fbSPranith Kumar 2705f6130faSLai Jiangshan /* 2715f6130faSLai Jiangshan * This function really isn't for public consumption, but RCU is special in 2725f6130faSLai Jiangshan * that context switches can allow the state machine to make progress. 2735f6130faSLai Jiangshan */ 2745f6130faSLai Jiangshan extern void resched_cpu(int cpu); 2755f6130faSLai Jiangshan 276822175e7SLai Jiangshan #if defined(CONFIG_SRCU) || !defined(CONFIG_TINY_RCU) 2772b34c43cSPaul E. McKenney 2782b34c43cSPaul E. McKenney #include <linux/rcu_node_tree.h> 2792b34c43cSPaul E. McKenney 2802b34c43cSPaul E. McKenney extern int rcu_num_lvls; 281e95d68d2SPaul E. McKenney extern int num_rcu_lvl[]; 2822b34c43cSPaul E. McKenney extern int rcu_num_nodes; 2832b34c43cSPaul E. McKenney static bool rcu_fanout_exact; 2842b34c43cSPaul E. McKenney static int rcu_fanout_leaf; 2852b34c43cSPaul E. McKenney 2862b34c43cSPaul E. McKenney /* 2872b34c43cSPaul E. McKenney * Compute the per-level fanout, either using the exact fanout specified 2882b34c43cSPaul E. McKenney * or balancing the tree, depending on the rcu_fanout_exact boot parameter. 2892b34c43cSPaul E. McKenney */ 2902b34c43cSPaul E. McKenney static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt) 2912b34c43cSPaul E. McKenney { 2922b34c43cSPaul E. McKenney int i; 2932b34c43cSPaul E. McKenney 29436b5dae6SPaul E. McKenney for (i = 0; i < RCU_NUM_LVLS; i++) 29536b5dae6SPaul E. McKenney levelspread[i] = INT_MIN; 2962b34c43cSPaul E. McKenney if (rcu_fanout_exact) { 2972b34c43cSPaul E. McKenney levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf; 2982b34c43cSPaul E. McKenney for (i = rcu_num_lvls - 2; i >= 0; i--) 2992b34c43cSPaul E. McKenney levelspread[i] = RCU_FANOUT; 3002b34c43cSPaul E. McKenney } else { 3012b34c43cSPaul E. McKenney int ccur; 3022b34c43cSPaul E. McKenney int cprv; 3032b34c43cSPaul E. McKenney 3042b34c43cSPaul E. McKenney cprv = nr_cpu_ids; 3052b34c43cSPaul E. McKenney for (i = rcu_num_lvls - 1; i >= 0; i--) { 3062b34c43cSPaul E. McKenney ccur = levelcnt[i]; 3072b34c43cSPaul E. McKenney levelspread[i] = (cprv + ccur - 1) / ccur; 3082b34c43cSPaul E. McKenney cprv = ccur; 3092b34c43cSPaul E. McKenney } 3102b34c43cSPaul E. McKenney } 3112b34c43cSPaul E. McKenney } 3122b34c43cSPaul E. McKenney 313b5befe84SFrederic Weisbecker extern void rcu_init_geometry(void); 314b5befe84SFrederic Weisbecker 3157f87c036SPaul E. McKenney /* Returns a pointer to the first leaf rcu_node structure. */ 316aedf4ba9SPaul E. McKenney #define rcu_first_leaf_node() (rcu_state.level[rcu_num_lvls - 1]) 3175b4c11d5SPaul E. McKenney 3185b4c11d5SPaul E. McKenney /* Is this rcu_node a leaf? */ 3195b4c11d5SPaul E. McKenney #define rcu_is_leaf_node(rnp) ((rnp)->level == rcu_num_lvls - 1) 3205b4c11d5SPaul E. McKenney 3215257514dSPaul E. McKenney /* Is this rcu_node the last leaf? */ 322aedf4ba9SPaul E. McKenney #define rcu_is_last_leaf_node(rnp) ((rnp) == &rcu_state.node[rcu_num_nodes - 1]) 3235257514dSPaul E. McKenney 324efbe451dSPaul E. McKenney /* 325aedf4ba9SPaul E. McKenney * Do a full breadth-first scan of the {s,}rcu_node structures for the 3267f87c036SPaul E. McKenney * specified state structure (for SRCU) or the only rcu_state structure 3277f87c036SPaul E. McKenney * (for RCU). 328efbe451dSPaul E. McKenney */ 329aedf4ba9SPaul E. McKenney #define srcu_for_each_node_breadth_first(sp, rnp) \ 330aedf4ba9SPaul E. McKenney for ((rnp) = &(sp)->node[0]; \ 331aedf4ba9SPaul E. McKenney (rnp) < &(sp)->node[rcu_num_nodes]; (rnp)++) 332aedf4ba9SPaul E. McKenney #define rcu_for_each_node_breadth_first(rnp) \ 333aedf4ba9SPaul E. McKenney srcu_for_each_node_breadth_first(&rcu_state, rnp) 334efbe451dSPaul E. McKenney 335efbe451dSPaul E. McKenney /* 3367f87c036SPaul E. McKenney * Scan the leaves of the rcu_node hierarchy for the rcu_state structure. 3377f87c036SPaul E. McKenney * Note that if there is a singleton rcu_node tree with but one rcu_node 3387f87c036SPaul E. McKenney * structure, this loop -will- visit the rcu_node structure. It is still 3397f87c036SPaul E. McKenney * a leaf node, even if it is also the root node. 340efbe451dSPaul E. McKenney */ 341aedf4ba9SPaul E. McKenney #define rcu_for_each_leaf_node(rnp) \ 342aedf4ba9SPaul E. McKenney for ((rnp) = rcu_first_leaf_node(); \ 343aedf4ba9SPaul E. McKenney (rnp) < &rcu_state.node[rcu_num_nodes]; (rnp)++) 344efbe451dSPaul E. McKenney 345efbe451dSPaul E. McKenney /* 346efbe451dSPaul E. McKenney * Iterate over all possible CPUs in a leaf RCU node. 347efbe451dSPaul E. McKenney */ 348efbe451dSPaul E. McKenney #define for_each_leaf_node_possible_cpu(rnp, cpu) \ 34982dd8419SPaul E. McKenney for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \ 35082dd8419SPaul E. McKenney (cpu) = cpumask_next((rnp)->grplo - 1, cpu_possible_mask); \ 35165963d24SPaul E. McKenney (cpu) <= rnp->grphi; \ 35265963d24SPaul E. McKenney (cpu) = cpumask_next((cpu), cpu_possible_mask)) 35365963d24SPaul E. McKenney 35465963d24SPaul E. McKenney /* 35565963d24SPaul E. McKenney * Iterate over all CPUs in a leaf RCU node's specified mask. 35665963d24SPaul E. McKenney */ 35765963d24SPaul E. McKenney #define rcu_find_next_bit(rnp, cpu, mask) \ 35865963d24SPaul E. McKenney ((rnp)->grplo + find_next_bit(&(mask), BITS_PER_LONG, (cpu))) 35965963d24SPaul E. McKenney #define for_each_leaf_node_cpu_mask(rnp, cpu, mask) \ 36082dd8419SPaul E. McKenney for (WARN_ON_ONCE(!rcu_is_leaf_node(rnp)), \ 36182dd8419SPaul E. McKenney (cpu) = rcu_find_next_bit((rnp), 0, (mask)); \ 36265963d24SPaul E. McKenney (cpu) <= rnp->grphi; \ 36365963d24SPaul E. McKenney (cpu) = rcu_find_next_bit((rnp), (cpu) + 1 - (rnp->grplo), (mask))) 364efbe451dSPaul E. McKenney 36583d40bd3SPaul E. McKenney /* 36683d40bd3SPaul E. McKenney * Wrappers for the rcu_node::lock acquire and release. 36783d40bd3SPaul E. McKenney * 36883d40bd3SPaul E. McKenney * Because the rcu_nodes form a tree, the tree traversal locking will observe 36983d40bd3SPaul E. McKenney * different lock values, this in turn means that an UNLOCK of one level 37083d40bd3SPaul E. McKenney * followed by a LOCK of another level does not imply a full memory barrier; 37183d40bd3SPaul E. McKenney * and most importantly transitivity is lost. 37283d40bd3SPaul E. McKenney * 37383d40bd3SPaul E. McKenney * In order to restore full ordering between tree levels, augment the regular 37483d40bd3SPaul E. McKenney * lock acquire functions with smp_mb__after_unlock_lock(). 37583d40bd3SPaul E. McKenney * 37683d40bd3SPaul E. McKenney * As ->lock of struct rcu_node is a __private field, therefore one should use 37783d40bd3SPaul E. McKenney * these wrappers rather than directly call raw_spin_{lock,unlock}* on ->lock. 37883d40bd3SPaul E. McKenney */ 37983d40bd3SPaul E. McKenney #define raw_spin_lock_rcu_node(p) \ 38083d40bd3SPaul E. McKenney do { \ 38183d40bd3SPaul E. McKenney raw_spin_lock(&ACCESS_PRIVATE(p, lock)); \ 38283d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 38383d40bd3SPaul E. McKenney } while (0) 38483d40bd3SPaul E. McKenney 3857dffe017SPaul E. McKenney #define raw_spin_unlock_rcu_node(p) \ 3867dffe017SPaul E. McKenney do { \ 3877dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 3887dffe017SPaul E. McKenney raw_spin_unlock(&ACCESS_PRIVATE(p, lock)); \ 3897dffe017SPaul E. McKenney } while (0) 39083d40bd3SPaul E. McKenney 39183d40bd3SPaul E. McKenney #define raw_spin_lock_irq_rcu_node(p) \ 39283d40bd3SPaul E. McKenney do { \ 39383d40bd3SPaul E. McKenney raw_spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \ 39483d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 39583d40bd3SPaul E. McKenney } while (0) 39683d40bd3SPaul E. McKenney 39783d40bd3SPaul E. McKenney #define raw_spin_unlock_irq_rcu_node(p) \ 3987dffe017SPaul E. McKenney do { \ 3997dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 4007dffe017SPaul E. McKenney raw_spin_unlock_irq(&ACCESS_PRIVATE(p, lock)); \ 4017dffe017SPaul E. McKenney } while (0) 40283d40bd3SPaul E. McKenney 4034e4bea74SPaul E. McKenney #define raw_spin_lock_irqsave_rcu_node(p, flags) \ 40483d40bd3SPaul E. McKenney do { \ 4054e4bea74SPaul E. McKenney raw_spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \ 40683d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 40783d40bd3SPaul E. McKenney } while (0) 40883d40bd3SPaul E. McKenney 4094e4bea74SPaul E. McKenney #define raw_spin_unlock_irqrestore_rcu_node(p, flags) \ 4107dffe017SPaul E. McKenney do { \ 4117dffe017SPaul E. McKenney lockdep_assert_irqs_disabled(); \ 4127dffe017SPaul E. McKenney raw_spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags); \ 4137dffe017SPaul E. McKenney } while (0) 41483d40bd3SPaul E. McKenney 41583d40bd3SPaul E. McKenney #define raw_spin_trylock_rcu_node(p) \ 41683d40bd3SPaul E. McKenney ({ \ 41783d40bd3SPaul E. McKenney bool ___locked = raw_spin_trylock(&ACCESS_PRIVATE(p, lock)); \ 41883d40bd3SPaul E. McKenney \ 41983d40bd3SPaul E. McKenney if (___locked) \ 42083d40bd3SPaul E. McKenney smp_mb__after_unlock_lock(); \ 42183d40bd3SPaul E. McKenney ___locked; \ 42283d40bd3SPaul E. McKenney }) 42383d40bd3SPaul E. McKenney 424a32e01eeSMatthew Wilcox #define raw_lockdep_assert_held_rcu_node(p) \ 425a32e01eeSMatthew Wilcox lockdep_assert_held(&ACCESS_PRIVATE(p, lock)) 426a32e01eeSMatthew Wilcox 427822175e7SLai Jiangshan #endif /* #if defined(CONFIG_SRCU) || !defined(CONFIG_TINY_RCU) */ 4282b34c43cSPaul E. McKenney 42925c36329SPaul E. McKenney #ifdef CONFIG_TINY_RCU 43025c36329SPaul E. McKenney /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */ 4317414fac0SPaul E. McKenney static inline bool rcu_gp_is_normal(void) { return true; } 4327414fac0SPaul E. McKenney static inline bool rcu_gp_is_expedited(void) { return false; } 4337414fac0SPaul E. McKenney static inline void rcu_expedite_gp(void) { } 4347414fac0SPaul E. McKenney static inline void rcu_unexpedite_gp(void) { } 435bfbd767dSPaul E. McKenney static inline void rcu_request_urgent_qs_task(struct task_struct *t) { } 43625c36329SPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */ 43725c36329SPaul E. McKenney bool rcu_gp_is_normal(void); /* Internal RCU use. */ 43825c36329SPaul E. McKenney bool rcu_gp_is_expedited(void); /* Internal RCU use. */ 43925c36329SPaul E. McKenney void rcu_expedite_gp(void); 44025c36329SPaul E. McKenney void rcu_unexpedite_gp(void); 44125c36329SPaul E. McKenney void rcupdate_announce_bootup_oddness(void); 442474d0997SPaul E. McKenney #ifdef CONFIG_TASKS_RCU_GENERIC 443e21408ceSPaul E. McKenney void show_rcu_tasks_gp_kthreads(void); 444474d0997SPaul E. McKenney #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */ 445474d0997SPaul E. McKenney static inline void show_rcu_tasks_gp_kthreads(void) {} 446474d0997SPaul E. McKenney #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */ 447bfbd767dSPaul E. McKenney void rcu_request_urgent_qs_task(struct task_struct *t); 44825c36329SPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */ 44925c36329SPaul E. McKenney 45082118249SPaul E. McKenney #define RCU_SCHEDULER_INACTIVE 0 45182118249SPaul E. McKenney #define RCU_SCHEDULER_INIT 1 45282118249SPaul E. McKenney #define RCU_SCHEDULER_RUNNING 2 45382118249SPaul E. McKenney 454cad7b389SPaul E. McKenney enum rcutorture_type { 455cad7b389SPaul E. McKenney RCU_FLAVOR, 456cad7b389SPaul E. McKenney RCU_TASKS_FLAVOR, 4573d6e43c7SPaul E. McKenney RCU_TASKS_RUDE_FLAVOR, 458c1a76c0bSPaul E. McKenney RCU_TASKS_TRACING_FLAVOR, 459c682db55SPaul E. McKenney RCU_TRIVIAL_FLAVOR, 460cad7b389SPaul E. McKenney SRCU_FLAVOR, 461cad7b389SPaul E. McKenney INVALID_RCU_FLAVOR 462cad7b389SPaul E. McKenney }; 463cad7b389SPaul E. McKenney 464b3e627d3SLai Jiangshan #if defined(CONFIG_TREE_RCU) 465cad7b389SPaul E. McKenney void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags, 466aebc8264SPaul E. McKenney unsigned long *gp_seq); 467cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename, 468cad7b389SPaul E. McKenney struct rcu_head *rhp, 469cad7b389SPaul E. McKenney unsigned long secs, 470cad7b389SPaul E. McKenney unsigned long c_old, 471cad7b389SPaul E. McKenney unsigned long c); 47255b2dcf5SPaul E. McKenney void rcu_gp_set_torture_wait(int duration); 473cad7b389SPaul E. McKenney #else 474cad7b389SPaul E. McKenney static inline void rcutorture_get_gp_data(enum rcutorture_type test_type, 475aebc8264SPaul E. McKenney int *flags, unsigned long *gp_seq) 476cad7b389SPaul E. McKenney { 477cad7b389SPaul E. McKenney *flags = 0; 478aebc8264SPaul E. McKenney *gp_seq = 0; 479cad7b389SPaul E. McKenney } 480cad7b389SPaul E. McKenney #ifdef CONFIG_RCU_TRACE 481cad7b389SPaul E. McKenney void do_trace_rcu_torture_read(const char *rcutorturename, 482cad7b389SPaul E. McKenney struct rcu_head *rhp, 483cad7b389SPaul E. McKenney unsigned long secs, 484cad7b389SPaul E. McKenney unsigned long c_old, 485cad7b389SPaul E. McKenney unsigned long c); 486cad7b389SPaul E. McKenney #else 487cad7b389SPaul E. McKenney #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \ 488cad7b389SPaul E. McKenney do { } while (0) 489cad7b389SPaul E. McKenney #endif 49055b2dcf5SPaul E. McKenney static inline void rcu_gp_set_torture_wait(int duration) { } 491cad7b389SPaul E. McKenney #endif 492cad7b389SPaul E. McKenney 493c682db55SPaul E. McKenney #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST) 494c682db55SPaul E. McKenney long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask); 495c682db55SPaul E. McKenney #endif 496c682db55SPaul E. McKenney 497cad7b389SPaul E. McKenney #ifdef CONFIG_TINY_SRCU 498cad7b389SPaul E. McKenney 499cad7b389SPaul E. McKenney static inline void srcutorture_get_gp_data(enum rcutorture_type test_type, 500cad7b389SPaul E. McKenney struct srcu_struct *sp, int *flags, 501aebc8264SPaul E. McKenney unsigned long *gp_seq) 502cad7b389SPaul E. McKenney { 503cad7b389SPaul E. McKenney if (test_type != SRCU_FLAVOR) 504cad7b389SPaul E. McKenney return; 505cad7b389SPaul E. McKenney *flags = 0; 506aebc8264SPaul E. McKenney *gp_seq = sp->srcu_idx; 507cad7b389SPaul E. McKenney } 508cad7b389SPaul E. McKenney 509cad7b389SPaul E. McKenney #elif defined(CONFIG_TREE_SRCU) 510cad7b389SPaul E. McKenney 511cad7b389SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type, 512cad7b389SPaul E. McKenney struct srcu_struct *sp, int *flags, 513aebc8264SPaul E. McKenney unsigned long *gp_seq); 514cad7b389SPaul E. McKenney 515cad7b389SPaul E. McKenney #endif 516cad7b389SPaul E. McKenney 517e3c8d51eSPaul E. McKenney #ifdef CONFIG_TINY_RCU 5187d0c9c50SPaul E. McKenney static inline bool rcu_dynticks_zero_in_eqs(int cpu, int *vp) { return false; } 51917ef2fe9SPaul E. McKenney static inline unsigned long rcu_get_gp_seq(void) { return 0; } 5207414fac0SPaul E. McKenney static inline unsigned long rcu_exp_batches_completed(void) { return 0; } 5217414fac0SPaul E. McKenney static inline unsigned long 5227414fac0SPaul E. McKenney srcu_batches_completed(struct srcu_struct *sp) { return 0; } 5237414fac0SPaul E. McKenney static inline void rcu_force_quiescent_state(void) { } 5240260b92eSPaul E. McKenney static inline bool rcu_check_boost_fail(unsigned long gp_state, int *cpup) { return true; } 5257414fac0SPaul E. McKenney static inline void show_rcu_gp_kthreads(void) { } 5264babd855SJoel Fernandes (Google) static inline int rcu_get_gp_kthreads_prio(void) { return 0; } 527e0aff973SPaul E. McKenney static inline void rcu_fwd_progress_check(unsigned long j) { } 528e3c8d51eSPaul E. McKenney #else /* #ifdef CONFIG_TINY_RCU */ 5297d0c9c50SPaul E. McKenney bool rcu_dynticks_zero_in_eqs(int cpu, int *vp); 53017ef2fe9SPaul E. McKenney unsigned long rcu_get_gp_seq(void); 531e3c8d51eSPaul E. McKenney unsigned long rcu_exp_batches_completed(void); 5325a0465e1SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp); 5330260b92eSPaul E. McKenney bool rcu_check_boost_fail(unsigned long gp_state, int *cpup); 534e3c8d51eSPaul E. McKenney void show_rcu_gp_kthreads(void); 5354babd855SJoel Fernandes (Google) int rcu_get_gp_kthreads_prio(void); 536e0aff973SPaul E. McKenney void rcu_fwd_progress_check(unsigned long j); 537e3c8d51eSPaul E. McKenney void rcu_force_quiescent_state(void); 538ad7c946bSPaul E. McKenney extern struct workqueue_struct *rcu_gp_wq; 539*9621fbeeSKalesh Singh #ifdef CONFIG_RCU_EXP_KTHREAD 540*9621fbeeSKalesh Singh extern struct kthread_worker *rcu_exp_gp_kworker; 541*9621fbeeSKalesh Singh extern struct kthread_worker *rcu_exp_par_gp_kworker; 542*9621fbeeSKalesh Singh #else /* !CONFIG_RCU_EXP_KTHREAD */ 54325f3d7efSPaul E. McKenney extern struct workqueue_struct *rcu_par_gp_wq; 544*9621fbeeSKalesh Singh #endif /* CONFIG_RCU_EXP_KTHREAD */ 545e3c8d51eSPaul E. McKenney #endif /* #else #ifdef CONFIG_TINY_RCU */ 546e3c8d51eSPaul E. McKenney 54744c65ff2SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU 5483d54f798SPaul E. McKenney bool rcu_is_nocb_cpu(int cpu); 5495ab7ab83SPaul E. McKenney void rcu_bind_current_to_nocb(void); 5503d54f798SPaul E. McKenney #else 5513d54f798SPaul E. McKenney static inline bool rcu_is_nocb_cpu(int cpu) { return false; } 5525ab7ab83SPaul E. McKenney static inline void rcu_bind_current_to_nocb(void) { } 5533d54f798SPaul E. McKenney #endif 5543d54f798SPaul E. McKenney 55527c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_RCU) 55627c0f144SPaul E. McKenney void show_rcu_tasks_classic_gp_kthread(void); 55727c0f144SPaul E. McKenney #else 55827c0f144SPaul E. McKenney static inline void show_rcu_tasks_classic_gp_kthread(void) {} 55927c0f144SPaul E. McKenney #endif 56027c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_RUDE_RCU) 56127c0f144SPaul E. McKenney void show_rcu_tasks_rude_gp_kthread(void); 56227c0f144SPaul E. McKenney #else 56327c0f144SPaul E. McKenney static inline void show_rcu_tasks_rude_gp_kthread(void) {} 56427c0f144SPaul E. McKenney #endif 56527c0f144SPaul E. McKenney #if !defined(CONFIG_TINY_RCU) && defined(CONFIG_TASKS_TRACE_RCU) 56627c0f144SPaul E. McKenney void show_rcu_tasks_trace_gp_kthread(void); 56727c0f144SPaul E. McKenney #else 56827c0f144SPaul E. McKenney static inline void show_rcu_tasks_trace_gp_kthread(void) {} 56927c0f144SPaul E. McKenney #endif 57027c0f144SPaul E. McKenney 5714102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */ 572