| 1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation. |
| 3 | // |
| 4 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | |
| 6 | #ifndef _URCU_BP_STATIC_H |
| 7 | #define _URCU_BP_STATIC_H |
| 8 | |
| 9 | /* |
| 10 | * Userspace RCU header. |
| 11 | * |
| 12 | * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU |
| 13 | * RELEASE. See urcu.h for linking dynamically with the userspace rcu library. |
| 14 | * |
| 15 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <pthread.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <urcu/debug.h> |
| 23 | #include <urcu/config.h> |
| 24 | #include <urcu/compiler.h> |
| 25 | #include <urcu/arch.h> |
| 26 | #include <urcu/system.h> |
| 27 | #include <urcu/uatomic.h> |
| 28 | #include <urcu/list.h> |
| 29 | #include <urcu/tls-compat.h> |
| 30 | |
| 31 | /* |
| 32 | * This code section can only be included in LGPL 2.1 compatible source code. |
| 33 | * See below for the function call wrappers which can be used in code meant to |
| 34 | * be only linked with the Userspace RCU library. This comes with a small |
| 35 | * performance degradation on the read-side due to the added function calls. |
| 36 | * This is required to permit relinking with newer versions of the library. |
| 37 | */ |
| 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | enum urcu_bp_state { |
| 44 | URCU_BP_READER_ACTIVE_CURRENT, |
| 45 | URCU_BP_READER_ACTIVE_OLD, |
| 46 | URCU_BP_READER_INACTIVE, |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * The trick here is that URCU_BP_GP_CTR_PHASE must be a multiple of 8 so we can use a |
| 51 | * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits. |
| 52 | */ |
| 53 | #define URCU_BP_GP_COUNT (1UL << 0) |
| 54 | /* Use the amount of bits equal to half of the architecture long size */ |
| 55 | #define URCU_BP_GP_CTR_PHASE (1UL << (sizeof(long) << 2)) |
| 56 | #define URCU_BP_GP_CTR_NEST_MASK (URCU_BP_GP_CTR_PHASE - 1) |
| 57 | |
| 58 | /* |
| 59 | * Used internally by _urcu_bp_read_lock. |
| 60 | */ |
| 61 | extern void urcu_bp_register(void); |
| 62 | |
| 63 | struct urcu_bp_gp { |
| 64 | /* |
| 65 | * Global grace period counter. |
| 66 | * Contains the current URCU_BP_GP_CTR_PHASE. |
| 67 | * Also has a URCU_BP_GP_COUNT of 1, to accelerate the reader fast path. |
| 68 | * Written to only by writer with mutex taken. |
| 69 | * Read by both writer and readers. |
| 70 | */ |
| 71 | unsigned long ctr; |
| 72 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
| 73 | |
| 74 | extern struct urcu_bp_gp urcu_bp_gp; |
| 75 | |
| 76 | struct urcu_bp_reader { |
| 77 | /* Data used by both reader and urcu_bp_synchronize_rcu() */ |
| 78 | unsigned long ctr; |
| 79 | /* Data used for registry */ |
| 80 | struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
| 81 | pthread_t tid; |
| 82 | int alloc; /* registry entry allocated */ |
| 83 | }; |
| 84 | |
| 85 | /* |
| 86 | * Bulletproof version keeps a pointer to a registry not part of the TLS. |
| 87 | * Adds a pointer dereference on the read-side, but won't require to unregister |
| 88 | * the reader thread. |
| 89 | */ |
| 90 | extern DECLARE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader); |
| 91 | |
| 92 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
| 93 | #define urcu_bp_has_sys_membarrier 1 |
| 94 | #else |
| 95 | extern int urcu_bp_has_sys_membarrier; |
| 96 | #endif |
| 97 | |
| 98 | static inline void urcu_bp_smp_mb_slave(void) |
| 99 | { |
| 100 | if (caa_likely(urcu_bp_has_sys_membarrier)) |
| 101 | cmm_barrier(); |
| 102 | else |
| 103 | cmm_smp_mb(); |
| 104 | } |
| 105 | |
| 106 | static inline enum urcu_bp_state urcu_bp_reader_state(unsigned long *ctr) |
| 107 | { |
| 108 | unsigned long v; |
| 109 | |
| 110 | if (ctr == NULL) |
| 111 | return URCU_BP_READER_INACTIVE; |
| 112 | /* |
| 113 | * Make sure both tests below are done on the same version of *value |
| 114 | * to insure consistency. |
| 115 | */ |
| 116 | v = CMM_LOAD_SHARED(*ctr); |
| 117 | if (!(v & URCU_BP_GP_CTR_NEST_MASK)) |
| 118 | return URCU_BP_READER_INACTIVE; |
| 119 | if (!((v ^ urcu_bp_gp.ctr) & URCU_BP_GP_CTR_PHASE)) |
| 120 | return URCU_BP_READER_ACTIVE_CURRENT; |
| 121 | return URCU_BP_READER_ACTIVE_OLD; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Helper for _urcu_bp_read_lock(). The format of urcu_bp_gp.ctr (as well as |
| 126 | * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of |
| 127 | * _urcu_bp_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit |
| 128 | * that contains either zero or one. The smp_mb_slave() ensures that the accesses in |
| 129 | * _urcu_bp_read_lock() happen before the subsequent read-side critical section. |
| 130 | */ |
| 131 | static inline void _urcu_bp_read_lock_update(unsigned long tmp) |
| 132 | { |
| 133 | if (caa_likely(!(tmp & URCU_BP_GP_CTR_NEST_MASK))) { |
| 134 | _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, _CMM_LOAD_SHARED(urcu_bp_gp.ctr)); |
| 135 | urcu_bp_smp_mb_slave(); |
| 136 | } else |
| 137 | _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, tmp + URCU_BP_GP_COUNT); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Enter an RCU read-side critical section. |
| 142 | * |
| 143 | * The first cmm_barrier() call ensures that the compiler does not reorder |
| 144 | * the body of _urcu_bp_read_lock() with a mutex. |
| 145 | * |
| 146 | * This function and its helper are both less than 10 lines long. The |
| 147 | * intent is that this function meets the 10-line criterion in LGPL, |
| 148 | * allowing this function to be invoked directly from non-LGPL code. |
| 149 | */ |
| 150 | static inline void _urcu_bp_read_lock(void) |
| 151 | { |
| 152 | unsigned long tmp; |
| 153 | |
| 154 | if (caa_unlikely(!URCU_TLS(urcu_bp_reader))) |
| 155 | urcu_bp_register(); /* If not yet registered. */ |
| 156 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
| 157 | tmp = URCU_TLS(urcu_bp_reader)->ctr; |
| 158 | urcu_assert_debug((tmp & URCU_BP_GP_CTR_NEST_MASK) != URCU_BP_GP_CTR_NEST_MASK); |
| 159 | _urcu_bp_read_lock_update(tmp); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Exit an RCU read-side critical section. This function is less than |
| 164 | * 10 lines of code, and is intended to be usable by non-LGPL code, as |
| 165 | * called out in LGPL. |
| 166 | */ |
| 167 | static inline void _urcu_bp_read_unlock(void) |
| 168 | { |
| 169 | unsigned long tmp; |
| 170 | |
| 171 | tmp = URCU_TLS(urcu_bp_reader)->ctr; |
| 172 | urcu_assert_debug(tmp & URCU_BP_GP_CTR_NEST_MASK); |
| 173 | /* Finish using rcu before decrementing the pointer. */ |
| 174 | urcu_bp_smp_mb_slave(); |
| 175 | _CMM_STORE_SHARED(URCU_TLS(urcu_bp_reader)->ctr, tmp - URCU_BP_GP_COUNT); |
| 176 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Returns whether within a RCU read-side critical section. |
| 181 | * |
| 182 | * This function is less than 10 lines long. The intent is that this |
| 183 | * function meets the 10-line criterion for LGPL, allowing this function |
| 184 | * to be invoked directly from non-LGPL code. |
| 185 | */ |
| 186 | static inline int _urcu_bp_read_ongoing(void) |
| 187 | { |
| 188 | if (caa_unlikely(!URCU_TLS(urcu_bp_reader))) |
| 189 | urcu_bp_register(); /* If not yet registered. */ |
| 190 | return URCU_TLS(urcu_bp_reader)->ctr & URCU_BP_GP_CTR_NEST_MASK; |
| 191 | } |
| 192 | |
| 193 | #ifdef __cplusplus |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | #endif /* _URCU_BP_STATIC_H */ |