Paul E. McKenney | 8c366db | 2019-01-17 10:39:22 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Sleepable Read-Copy Update mechanism for mutual exclusion, |
| 4 | * tree variant. |
| 5 | * |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 6 | * Copyright (C) IBM Corporation, 2017 |
| 7 | * |
Paul E. McKenney | 8c366db | 2019-01-17 10:39:22 -0800 | [diff] [blame] | 8 | * Author: Paul McKenney <paulmck@linux.ibm.com> |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef _LINUX_SRCU_TREE_H |
| 12 | #define _LINUX_SRCU_TREE_H |
| 13 | |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 14 | #include <linux/rcu_node_tree.h> |
| 15 | #include <linux/completion.h> |
| 16 | |
| 17 | struct srcu_node; |
| 18 | struct srcu_struct; |
| 19 | |
| 20 | /* |
| 21 | * Per-CPU structure feeding into leaf srcu_node, similar in function |
| 22 | * to rcu_node. |
| 23 | */ |
| 24 | struct srcu_data { |
| 25 | /* Read-side state. */ |
| 26 | unsigned long srcu_lock_count[2]; /* Locks per CPU. */ |
| 27 | unsigned long srcu_unlock_count[2]; /* Unlocks per CPU. */ |
| 28 | |
| 29 | /* Update-side state. */ |
Paul E. McKenney | d633198 | 2017-10-10 13:52:30 -0700 | [diff] [blame] | 30 | spinlock_t __private lock ____cacheline_internodealigned_in_smp; |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 31 | struct rcu_segcblist srcu_cblist; /* List of callbacks.*/ |
| 32 | unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */ |
Paul E. McKenney | 1e9a038 | 2017-04-24 16:02:09 -0700 | [diff] [blame] | 33 | unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 34 | bool srcu_cblist_invoking; /* Invoking these CBs? */ |
Sebastian Andrzej Siewior | e81baf4 | 2018-12-11 12:12:38 +0100 | [diff] [blame] | 35 | struct timer_list delay_work; /* Delay for CB invoking */ |
| 36 | struct work_struct work; /* Context for CB invoking. */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 37 | struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */ |
| 38 | struct srcu_node *mynode; /* Leaf srcu_node. */ |
Paul E. McKenney | c7e8806 | 2017-04-18 16:01:46 -0700 | [diff] [blame] | 39 | unsigned long grpmask; /* Mask for leaf srcu_node */ |
| 40 | /* ->srcu_data_have_cbs[]. */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 41 | int cpu; |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 42 | struct srcu_struct *ssp; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Node in SRCU combining tree, similar in function to rcu_data. |
| 47 | */ |
| 48 | struct srcu_node { |
Paul E. McKenney | d633198 | 2017-10-10 13:52:30 -0700 | [diff] [blame] | 49 | spinlock_t __private lock; |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 50 | unsigned long srcu_have_cbs[4]; /* GP seq for children */ |
| 51 | /* having CBs, but only */ |
| 52 | /* is > ->srcu_gq_seq. */ |
Paul E. McKenney | c7e8806 | 2017-04-18 16:01:46 -0700 | [diff] [blame] | 53 | unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs */ |
| 54 | /* have CBs for given GP? */ |
Paul E. McKenney | 1e9a038 | 2017-04-24 16:02:09 -0700 | [diff] [blame] | 55 | unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 56 | struct srcu_node *srcu_parent; /* Next up in tree. */ |
| 57 | int grplo; /* Least CPU for node. */ |
| 58 | int grphi; /* Biggest CPU for node. */ |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * Per-SRCU-domain structure, similar in function to rcu_state. |
| 63 | */ |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 64 | struct srcu_struct { |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 65 | struct srcu_node node[NUM_RCU_NODES]; /* Combining tree. */ |
| 66 | struct srcu_node *level[RCU_NUM_LVLS + 1]; |
| 67 | /* First node at each level. */ |
| 68 | struct mutex srcu_cb_mutex; /* Serialize CB preparation. */ |
Paul E. McKenney | d633198 | 2017-10-10 13:52:30 -0700 | [diff] [blame] | 69 | spinlock_t __private lock; /* Protect counters */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 70 | struct mutex srcu_gp_mutex; /* Serialize GP work. */ |
| 71 | unsigned int srcu_idx; /* Current rdr array element. */ |
| 72 | unsigned long srcu_gp_seq; /* Grace-period seq #. */ |
| 73 | unsigned long srcu_gp_seq_needed; /* Latest gp_seq needed. */ |
Paul E. McKenney | 1e9a038 | 2017-04-24 16:02:09 -0700 | [diff] [blame] | 74 | unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */ |
Paul E. McKenney | 22607d6 | 2017-04-25 14:03:11 -0700 | [diff] [blame] | 75 | unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */ |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 76 | struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */ |
| 77 | unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */ |
| 78 | struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */ |
| 79 | struct completion srcu_barrier_completion; |
| 80 | /* Awaken barrier rq at end. */ |
| 81 | atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */ |
| 82 | /* callback for the barrier */ |
| 83 | /* operation. */ |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 84 | struct delayed_work work; |
| 85 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 86 | struct lockdep_map dep_map; |
| 87 | #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 88 | }; |
| 89 | |
Paul E. McKenney | da915ad | 2017-04-05 09:01:53 -0700 | [diff] [blame] | 90 | /* Values for state variable (bottom bits of ->srcu_gp_seq). */ |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 91 | #define SRCU_STATE_IDLE 0 |
| 92 | #define SRCU_STATE_SCAN1 1 |
| 93 | #define SRCU_STATE_SCAN2 2 |
| 94 | |
Sebastian Andrzej Siewior | 9c80172 | 2018-05-25 12:19:57 +0200 | [diff] [blame] | 95 | #define __SRCU_STRUCT_INIT(name, pcpu_name) \ |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 96 | { \ |
| 97 | .sda = &pcpu_name, \ |
| 98 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
| 99 | .srcu_gp_seq_needed = -1UL, \ |
Paul E. McKenney | 4e6ea4e | 2018-08-14 14:41:49 -0700 | [diff] [blame] | 100 | .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \ |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 101 | __SRCU_DEP_MAP_INIT(name) \ |
| 102 | } |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Define and initialize a srcu struct at build time. |
| 106 | * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it. |
| 107 | * |
| 108 | * Note that although DEFINE_STATIC_SRCU() hides the name from other |
| 109 | * files, the per-CPU variable rules nevertheless require that the |
| 110 | * chosen name be globally unique. These rules also prohibit use of |
| 111 | * DEFINE_STATIC_SRCU() within a function. If these rules are too |
| 112 | * restrictive, declare the srcu_struct manually. For example, in |
| 113 | * each file: |
| 114 | * |
| 115 | * static struct srcu_struct my_srcu; |
| 116 | * |
| 117 | * Then, before the first use of each my_srcu, manually initialize it: |
| 118 | * |
| 119 | * init_srcu_struct(&my_srcu); |
| 120 | * |
| 121 | * See include/linux/percpu-defs.h for the rules on per-CPU variables. |
| 122 | */ |
Paul E. McKenney | fe15b50 | 2019-04-05 16:15:00 -0700 | [diff] [blame] | 123 | #ifdef MODULE |
| 124 | # define __DEFINE_SRCU(name, is_static) \ |
| 125 | is_static struct srcu_struct name; \ |
Joel Fernandes (Google) | 056b89e | 2019-04-11 16:24:21 -0400 | [diff] [blame] | 126 | struct srcu_struct * const __srcu_struct_##name \ |
Paul E. McKenney | fe15b50 | 2019-04-05 16:15:00 -0700 | [diff] [blame] | 127 | __section("___srcu_struct_ptrs") = &name |
| 128 | #else |
| 129 | # define __DEFINE_SRCU(name, is_static) \ |
| 130 | static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \ |
| 131 | is_static struct srcu_struct name = \ |
| 132 | __SRCU_STRUCT_INIT(name, name##_srcu_data) |
| 133 | #endif |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 134 | #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */) |
| 135 | #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static) |
| 136 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 137 | void synchronize_srcu_expedited(struct srcu_struct *ssp); |
| 138 | void srcu_barrier(struct srcu_struct *ssp); |
| 139 | void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 140 | |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 141 | #endif |