1 1.5 riastrad /* $NetBSD: seqlock.h,v 1.5 2021/12/19 01:50:10 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #ifndef _LINUX_SEQLOCK_H_ 33 1.1 riastrad #define _LINUX_SEQLOCK_H_ 34 1.1 riastrad 35 1.2 riastrad #include <sys/types.h> 36 1.2 riastrad #include <sys/atomic.h> 37 1.2 riastrad #include <sys/lock.h> 38 1.2 riastrad 39 1.2 riastrad #include <lib/libkern/libkern.h> 40 1.2 riastrad 41 1.3 riastrad struct seqcount { 42 1.3 riastrad unsigned sqc_gen; 43 1.3 riastrad }; 44 1.3 riastrad 45 1.3 riastrad typedef struct seqcount seqcount_t; 46 1.3 riastrad 47 1.3 riastrad static inline void 48 1.3 riastrad seqcount_init(struct seqcount *seqcount) 49 1.3 riastrad { 50 1.3 riastrad 51 1.3 riastrad seqcount->sqc_gen = 0; 52 1.3 riastrad } 53 1.3 riastrad 54 1.3 riastrad static inline void 55 1.3 riastrad seqcount_destroy(struct seqcount *seqcount) 56 1.3 riastrad { 57 1.3 riastrad 58 1.3 riastrad KASSERT((seqcount->sqc_gen & 1) == 0); 59 1.3 riastrad seqcount->sqc_gen = -1; 60 1.3 riastrad } 61 1.3 riastrad 62 1.3 riastrad static inline void 63 1.3 riastrad write_seqcount_begin(struct seqcount *seqcount) 64 1.3 riastrad { 65 1.3 riastrad 66 1.3 riastrad KASSERT((seqcount->sqc_gen & 1) == 0); 67 1.3 riastrad seqcount->sqc_gen |= 1; 68 1.3 riastrad membar_producer(); 69 1.3 riastrad } 70 1.3 riastrad 71 1.3 riastrad static inline void 72 1.3 riastrad write_seqcount_end(struct seqcount *seqcount) 73 1.3 riastrad { 74 1.3 riastrad 75 1.3 riastrad KASSERT((seqcount->sqc_gen & 1) == 1); 76 1.3 riastrad membar_producer(); 77 1.3 riastrad seqcount->sqc_gen |= 1; /* paranoia */ 78 1.3 riastrad seqcount->sqc_gen++; 79 1.3 riastrad } 80 1.3 riastrad 81 1.3 riastrad static inline unsigned 82 1.5 riastrad __read_seqcount_begin(const struct seqcount *seqcount) 83 1.3 riastrad { 84 1.3 riastrad unsigned gen; 85 1.3 riastrad 86 1.3 riastrad while (__predict_false((gen = seqcount->sqc_gen) & 1)) 87 1.3 riastrad SPINLOCK_BACKOFF_HOOK; 88 1.3 riastrad __insn_barrier(); 89 1.3 riastrad 90 1.3 riastrad return gen; 91 1.3 riastrad } 92 1.3 riastrad 93 1.3 riastrad static inline bool 94 1.5 riastrad __read_seqcount_retry(const struct seqcount *seqcount, unsigned gen) 95 1.3 riastrad { 96 1.3 riastrad 97 1.3 riastrad __insn_barrier(); 98 1.3 riastrad return __predict_false(seqcount->sqc_gen != gen); 99 1.3 riastrad } 100 1.3 riastrad 101 1.3 riastrad static inline unsigned 102 1.5 riastrad read_seqcount_begin(const struct seqcount *seqcount) 103 1.3 riastrad { 104 1.3 riastrad unsigned gen; 105 1.3 riastrad 106 1.3 riastrad gen = __read_seqcount_begin(seqcount); 107 1.3 riastrad membar_consumer(); 108 1.3 riastrad 109 1.3 riastrad return gen; 110 1.3 riastrad } 111 1.3 riastrad 112 1.3 riastrad static inline bool 113 1.5 riastrad read_seqcount_retry(const struct seqcount *seqcount, unsigned gen) 114 1.3 riastrad { 115 1.3 riastrad 116 1.3 riastrad membar_consumer(); 117 1.3 riastrad return __read_seqcount_retry(seqcount, gen); 118 1.3 riastrad } 119 1.3 riastrad 120 1.4 riastrad static inline unsigned 121 1.5 riastrad raw_read_seqcount(const struct seqcount *seqcount) 122 1.4 riastrad { 123 1.4 riastrad unsigned gen; 124 1.4 riastrad 125 1.4 riastrad gen = seqcount->sqc_gen; 126 1.4 riastrad membar_consumer(); 127 1.4 riastrad 128 1.4 riastrad return gen; 129 1.4 riastrad } 130 1.4 riastrad 131 1.2 riastrad struct seqlock { 132 1.3 riastrad kmutex_t sql_lock; 133 1.3 riastrad struct seqcount sql_count; 134 1.2 riastrad }; 135 1.2 riastrad 136 1.2 riastrad typedef struct seqlock seqlock_t; 137 1.2 riastrad 138 1.2 riastrad static inline void 139 1.2 riastrad seqlock_init(struct seqlock *seqlock) 140 1.2 riastrad { 141 1.2 riastrad 142 1.3 riastrad mutex_init(&seqlock->sql_lock, MUTEX_DEFAULT, IPL_VM); 143 1.3 riastrad seqcount_init(&seqlock->sql_count); 144 1.3 riastrad } 145 1.3 riastrad 146 1.3 riastrad static inline void 147 1.3 riastrad seqlock_destroy(struct seqlock *seqlock) 148 1.3 riastrad { 149 1.3 riastrad 150 1.3 riastrad seqcount_destroy(&seqlock->sql_count); 151 1.3 riastrad mutex_destroy(&seqlock->sql_lock); 152 1.2 riastrad } 153 1.2 riastrad 154 1.2 riastrad static inline void 155 1.2 riastrad write_seqlock(struct seqlock *seqlock) 156 1.2 riastrad { 157 1.2 riastrad 158 1.3 riastrad mutex_spin_enter(&seqlock->sql_lock); 159 1.3 riastrad write_seqcount_begin(&seqlock->sql_count); 160 1.2 riastrad } 161 1.2 riastrad 162 1.2 riastrad static inline void 163 1.2 riastrad write_sequnlock(struct seqlock *seqlock) 164 1.2 riastrad { 165 1.2 riastrad 166 1.3 riastrad write_seqcount_end(&seqlock->sql_count); 167 1.3 riastrad mutex_spin_exit(&seqlock->sql_lock); 168 1.2 riastrad } 169 1.2 riastrad 170 1.2 riastrad #define write_seqlock_irqsave(SEQLOCK, FLAGS) do { \ 171 1.2 riastrad (FLAGS) = (unsigned long)splvm(); \ 172 1.2 riastrad write_seqlock(SEQLOCK); \ 173 1.2 riastrad } while (0) 174 1.2 riastrad 175 1.2 riastrad #define write_sequnlock_irqrestore(SEQLOCK, FLAGS) do { \ 176 1.3 riastrad write_sequnlock(SEQLOCK); \ 177 1.2 riastrad splx((int)(FLAGS)); \ 178 1.2 riastrad } while (0) 179 1.2 riastrad 180 1.3 riastrad static inline unsigned 181 1.5 riastrad read_seqbegin(const struct seqlock *seqlock) 182 1.2 riastrad { 183 1.2 riastrad 184 1.3 riastrad return read_seqcount_begin(&seqlock->sql_count); 185 1.2 riastrad } 186 1.2 riastrad 187 1.2 riastrad static inline bool 188 1.5 riastrad read_seqretry(const struct seqlock *seqlock, unsigned gen) 189 1.2 riastrad { 190 1.2 riastrad 191 1.3 riastrad return read_seqcount_retry(&seqlock->sql_count, gen); 192 1.2 riastrad } 193 1.2 riastrad 194 1.1 riastrad #endif /* _LINUX_SEQLOCK_H_ */ 195