1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2021 Vladimir Kondratyev <wulf@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef _LINUX_SEQLOCK_H__ 30 #define _LINUX_SEQLOCK_H__ 31 32 #include <sys/param.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/seqc.h> 36 37 struct lock_class_key; 38 39 struct seqcount { 40 seqc_t seqc; 41 }; 42 typedef struct seqcount seqcount_t; 43 44 struct seqlock { 45 struct mtx seql_lock; 46 struct seqcount seql_count; 47 }; 48 typedef struct seqlock seqlock_t; 49 50 static inline void 51 __seqcount_init(struct seqcount *seqcount, const char *name __unused, 52 struct lock_class_key *key __unused) 53 { 54 seqcount->seqc = 0; 55 } 56 #define seqcount_init(seqcount) __seqcount_init(seqcount, NULL, NULL) 57 58 static inline void 59 write_seqcount_begin(struct seqcount *seqcount) 60 { 61 seqc_sleepable_write_begin(&seqcount->seqc); 62 } 63 64 static inline void 65 write_seqcount_end(struct seqcount *seqcount) 66 { 67 seqc_sleepable_write_end(&seqcount->seqc); 68 } 69 70 /* 71 * XXX: Are predicts from inline functions still not honored by clang? 72 */ 73 #define __read_seqcount_retry(seqcount, gen) \ 74 (!seqc_consistent_nomb(&(seqcount)->seqc, gen)) 75 #define read_seqcount_retry(seqcount, gen) \ 76 (!seqc_consistent(&(seqcount)->seqc, gen)) 77 78 static inline unsigned 79 read_seqcount_begin(const struct seqcount *seqcount) 80 { 81 return (seqc_read(&seqcount->seqc)); 82 } 83 84 static inline unsigned 85 raw_read_seqcount(const struct seqcount *seqcount) 86 { 87 return (seqc_read_any(&seqcount->seqc)); 88 } 89 90 static inline void 91 seqlock_init(struct seqlock *seqlock) 92 { 93 /* 94 * Don't enroll to witness(4) to avoid orphaned references after struct 95 * seqlock has been freed. There is no seqlock destructor exists so we 96 * can't expect automatic mtx_destroy() execution before free(). 97 */ 98 mtx_init(&seqlock->seql_lock, "seqlock", NULL, MTX_DEF|MTX_NOWITNESS); 99 seqcount_init(&seqlock->seql_count); 100 } 101 102 static inline void 103 write_seqlock(struct seqlock *seqlock) 104 { 105 mtx_lock(&seqlock->seql_lock); 106 write_seqcount_begin(&seqlock->seql_count); 107 } 108 109 static inline void 110 write_sequnlock(struct seqlock *seqlock) 111 { 112 write_seqcount_end(&seqlock->seql_count); 113 mtx_unlock(&seqlock->seql_lock); 114 } 115 116 #define write_seqlock_irqsave(seqlock, flags) do { \ 117 (flags) = 0; \ 118 write_seqlock(seqlock); \ 119 } while (0) 120 121 static inline void 122 write_sequnlock_irqrestore(struct seqlock *seqlock, 123 unsigned long flags __unused) 124 { 125 write_sequnlock(seqlock); 126 } 127 128 static inline unsigned 129 read_seqbegin(const struct seqlock *seqlock) 130 { 131 return (read_seqcount_begin(&seqlock->seql_count)); 132 } 133 134 #define read_seqretry(seqlock, gen) \ 135 read_seqcount_retry(&(seqlock)->seql_count, gen) 136 137 #endif /* _LINUX_SEQLOCK_H__ */ 138