aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linuxkpi/common/include/linux/seqlock.h
blob: 4a5385f5e095a71ac7d3cd57ea40718721bd1d2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2021 Vladimir Kondratyev <wulf@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _LINUXKPI_LINUX_SEQLOCK_H__
#define	_LINUXKPI_LINUX_SEQLOCK_H__

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/seqc.h>

#include <linux/mutex.h>

struct lock_class_key;

struct seqcount {
	seqc_t		seqc;
};
typedef struct seqcount seqcount_t;

struct seqlock {
	struct mtx	seql_lock;
	struct seqcount	seql_count;
};
typedef struct seqlock seqlock_t;

struct seqcount_mutex {
	struct mutex	*seqm_lock;
	struct seqcount	 seqm_count;
};
typedef struct seqcount_mutex seqcount_mutex_t;

static inline void
__seqcount_init(struct seqcount *seqcount, const char *name __unused,
    struct lock_class_key *key __unused)
{
	seqcount->seqc = 0;
}
#define	seqcount_init(seqcount)	__seqcount_init(seqcount, NULL, NULL)

static inline void
seqcount_mutex_init(struct seqcount_mutex *seqcount, struct mutex *mutex)
{
	seqcount->seqm_lock = mutex;
	seqcount_init(&seqcount->seqm_count);
}

#define	write_seqcount_begin(s)						\
    _Generic(*(s),							\
	struct seqcount:	lkpi_write_seqcount_begin,		\
	struct seqcount_mutex:	lkpi_write_seqcount_mutex_begin		\
    )(s)

static inline void
lkpi_write_seqcount_begin(struct seqcount *seqcount)
{
	seqc_sleepable_write_begin(&seqcount->seqc);
}

static inline void
lkpi_write_seqcount_mutex_begin(struct seqcount_mutex *seqcount)
{
	mutex_lock(seqcount->seqm_lock);
	lkpi_write_seqcount_begin(&seqcount->seqm_count);
}

#define	write_seqcount_end(s)						\
    _Generic(*(s),							\
	struct seqcount:	lkpi_write_seqcount_end,		\
	struct seqcount_mutex:	lkpi_write_seqcount_mutex_end		\
    )(s)

static inline void
lkpi_write_seqcount_end(struct seqcount *seqcount)
{
	seqc_sleepable_write_end(&seqcount->seqc);
}

static inline void
lkpi_write_seqcount_mutex_end(struct seqcount_mutex *seqcount)
{
	lkpi_write_seqcount_end(&seqcount->seqm_count);
	mutex_unlock(seqcount->seqm_lock);
}

#define	read_seqcount_begin(s)						\
    _Generic(*(s),							\
	struct seqcount:	lkpi_read_seqcount_begin,		\
	struct seqcount_mutex:	lkpi_read_seqcount_mutex_begin		\
    )(s)

static inline unsigned
lkpi_read_seqcount_begin(const struct seqcount *seqcount)
{
	return (seqc_read(&seqcount->seqc));
}

static inline unsigned
lkpi_read_seqcount_mutex_begin(const struct seqcount_mutex *seqcount)
{
	return (lkpi_read_seqcount_begin(&seqcount->seqm_count));
}

static inline unsigned
raw_read_seqcount(const struct seqcount *seqcount)
{
	return (seqc_read_any(&seqcount->seqc));
}

/*
 * XXX: Are predicts from inline functions still not honored by clang?
 */
#define	__read_seqcount_retry(seqcount, gen)	\
	(!seqc_consistent_no_fence(&(seqcount)->seqc, gen))
#define	read_seqcount_retry(s, old)					\
    _Generic(*(s),							\
	struct seqcount:	lkpi_read_seqcount_retry,		\
	struct seqcount_mutex:	lkpi_read_seqcount_mutex_retry		\
    )(s, old)

static inline int
lkpi_read_seqcount_retry(
    const struct seqcount *seqcount, unsigned int old)
{
	return (!seqc_consistent(&seqcount->seqc, old));
}

static inline int
lkpi_read_seqcount_mutex_retry(
    const struct seqcount_mutex *seqcount, unsigned int old)
{
	return (!seqc_consistent(&seqcount->seqm_count.seqc, old));
}

static inline void
seqlock_init(struct seqlock *seqlock)
{
	/*
	 * Don't enroll to witness(4) to avoid orphaned references after struct
	 * seqlock has been freed. There is no seqlock destructor exists so we
	 * can't expect automatic mtx_destroy() execution before free().
	 */
	mtx_init(&seqlock->seql_lock, "seqlock", NULL, MTX_DEF|MTX_NOWITNESS);
	seqcount_init(&seqlock->seql_count);
}

static inline void
lkpi_write_seqlock(struct seqlock *seqlock, const bool irqsave)
{
	mtx_lock(&seqlock->seql_lock);
	if (irqsave)
		critical_enter();
	write_seqcount_begin(&seqlock->seql_count);
}

static inline void
write_seqlock(struct seqlock *seqlock)
{
	lkpi_write_seqlock(seqlock, false);
}

static inline void
lkpi_write_sequnlock(struct seqlock *seqlock, const bool irqsave)
{
	write_seqcount_end(&seqlock->seql_count);
	if (irqsave)
		critical_exit();
	mtx_unlock(&seqlock->seql_lock);
}

static inline void
write_sequnlock(struct seqlock *seqlock)
{
	lkpi_write_sequnlock(seqlock, false);
}

/*
 * Disable preemption when the consumer wants to disable interrupts.  This
 * ensures that the caller won't be starved if it is preempted by a
 * higher-priority reader, but assumes that the caller won't perform any
 * blocking operations while holding the write lock; probably a safe
 * assumption.
 */
#define	write_seqlock_irqsave(seqlock, flags)	do {	\
	(flags) = 0;					\
	lkpi_write_seqlock(seqlock, true);		\
} while (0)

static inline void
write_sequnlock_irqrestore(struct seqlock *seqlock,
    unsigned long flags __unused)
{
	lkpi_write_sequnlock(seqlock, true);
}

static inline unsigned
read_seqbegin(const struct seqlock *seqlock)
{
	return (read_seqcount_begin(&seqlock->seql_count));
}

#define	read_seqretry(seqlock, gen)	\
	read_seqcount_retry(&(seqlock)->seql_count, gen)

#endif	/* _LINUXKPI_LINUX_SEQLOCK_H__ */