aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linuxkpi/common/include/linux/wait.h
blob: 348464fb27df885aaeb6c3f05f9c95c78833bd83 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*-
 * Copyright (c) 2010 Isilon Systems, Inc.
 * Copyright (c) 2010 iX Systems, Inc.
 * Copyright (c) 2010 Panasas, Inc.
 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
 * All rights reserved.
 *
 * 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 unmodified, 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 ``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 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.
 *
 * $FreeBSD$
 */

#ifndef _LINUX_WAIT_H_
#define	_LINUX_WAIT_H_

#include <linux/compiler.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/sched.h>

#include <asm/atomic.h>

#include <sys/param.h>
#include <sys/systm.h>

#define	SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active)

#define	might_sleep()							\
	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")

#define	might_sleep_if(cond) do { \
	if (cond) { might_sleep(); } \
} while (0)

struct wait_queue;
struct wait_queue_head;

#define	wait_queue_entry wait_queue

typedef struct wait_queue wait_queue_t;
typedef struct wait_queue_entry wait_queue_entry_t;
typedef struct wait_queue_head wait_queue_head_t;

typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *);

/*
 * Many API consumers directly reference these fields and those of
 * wait_queue_head.
 */
struct wait_queue {
	unsigned int flags;	/* always 0 */
	void *private;
	wait_queue_func_t *func;
	union {
		struct list_head task_list; /* < v4.13 */
		struct list_head entry; /* >= v4.13 */
	};
};

struct wait_queue_head {
	spinlock_t lock;
	union {
		struct list_head task_list; /* < v4.13 */
		struct list_head head; /* >= v4.13 */
	};
};

/*
 * This function is referenced by at least one DRM driver, so it may not be
 * renamed and furthermore must be the default wait queue callback.
 */
extern wait_queue_func_t autoremove_wake_function;
extern wait_queue_func_t default_wake_function;

#define	DEFINE_WAIT_FUNC(name, function)				\
	wait_queue_t name = {						\
		.private = current,					\
		.func = function,					\
		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
	}

#define	DEFINE_WAIT(name) \
	DEFINE_WAIT_FUNC(name, autoremove_wake_function)

#define	DECLARE_WAITQUEUE(name, task)					\
	wait_queue_t name = {						\
		.private = task,					\
		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
	}

#define	DECLARE_WAIT_QUEUE_HEAD(name)					\
	wait_queue_head_t name = {					\
		.task_list = LINUX_LIST_HEAD_INIT(name.task_list),	\
	};								\
	MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF)

#define	init_waitqueue_head(wqh) do {					\
	mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"),		\
	    NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS);			\
	INIT_LIST_HEAD(&(wqh)->task_list);				\
} while (0)

#define	__init_waitqueue_head(wqh, name, lk) init_waitqueue_head(wqh)

void linux_init_wait_entry(wait_queue_t *, int);
void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool);

#define	init_wait_entry(wq, flags)					\
        linux_init_wait_entry(wq, flags)
#define	wake_up(wqh)							\
	linux_wake_up(wqh, TASK_NORMAL, 1, false)
#define	wake_up_all(wqh)						\
	linux_wake_up(wqh, TASK_NORMAL, 0, false)
#define	wake_up_locked(wqh)						\
	linux_wake_up(wqh, TASK_NORMAL, 1, true)
#define	wake_up_all_locked(wqh)						\
	linux_wake_up(wqh, TASK_NORMAL, 0, true)
#define	wake_up_interruptible(wqh)					\
	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false)
#define	wake_up_interruptible_all(wqh)					\
	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false)

int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int,
    unsigned int, spinlock_t *);

/*
 * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if
 * cond is true after timeout, remaining jiffies (> 0) if cond is true before
 * timeout.
 */
#define	__wait_event_common(wqh, cond, timeout, state, lock) ({	\
	DEFINE_WAIT(__wq);					\
	const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout);	\
	int __start = ticks;					\
	int __ret = 0;						\
								\
	for (;;) {						\
		linux_prepare_to_wait(&(wqh), &__wq, state);	\
		if (cond)					\
			break;					\
		__ret = linux_wait_event_common(&(wqh), &__wq,	\
		    __timeout, state, lock);			\
		if (__ret != 0)					\
			break;					\
	}							\
	linux_finish_wait(&(wqh), &__wq);			\
	if (__timeout != MAX_SCHEDULE_TIMEOUT) {		\
		if (__ret == -EWOULDBLOCK)			\
			__ret = !!(cond);			\
		else if (__ret != -ERESTARTSYS) {		\
			__ret = __timeout + __start - ticks;	\
			/* range check return value */		\
			if (__ret < 1)				\
				__ret = 1;			\
			else if (__ret > __timeout)		\
				__ret = __timeout;		\
		}						\
	}							\
	__ret;							\
})

#define	wait_event(wqh, cond) do {					\
	(void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
	    TASK_UNINTERRUPTIBLE, NULL);				\
} while (0)

#define	wait_event_timeout(wqh, cond, timeout) ({			\
	__wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE,	\
	    NULL);							\
})

#define	wait_event_killable(wqh, cond) ({				\
	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
	    TASK_INTERRUPTIBLE, NULL);					\
})

#define	wait_event_interruptible(wqh, cond) ({				\
	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
	    TASK_INTERRUPTIBLE, NULL);					\
})

#define	wait_event_interruptible_timeout(wqh, cond, timeout) ({		\
	__wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE,	\
	    NULL);							\
})

/*
 * Wait queue is already locked.
 */
#define	wait_event_interruptible_locked(wqh, cond) ({			\
	int __ret;							\
									\
	spin_unlock(&(wqh).lock);					\
	__ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
	    TASK_INTERRUPTIBLE, NULL);					\
	spin_lock(&(wqh).lock);						\
	__ret;								\
})

/*
 * The passed spinlock is held when testing the condition.
 */
#define	wait_event_interruptible_lock_irq(wqh, cond, lock) ({		\
	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
	    TASK_INTERRUPTIBLE, &(lock));				\
})

/*
 * The passed spinlock is held when testing the condition.
 */
#define	wait_event_lock_irq(wqh, cond, lock) ({			\
	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
	    TASK_UNINTERRUPTIBLE, &(lock));			\
})

static inline void
__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
{
	list_add(&wq->task_list, &wqh->task_list);
}

static inline void
add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
{

	spin_lock(&wqh->lock);
	__add_wait_queue(wqh, wq);
	spin_unlock(&wqh->lock);
}

static inline void
__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
{
	list_add_tail(&wq->task_list, &wqh->task_list);
}

static inline void
__add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
{
        list_add_tail(&wq->entry, &wqh->head);
}

static inline void
__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
{
	list_del(&wq->task_list);
}

static inline void
remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
{

	spin_lock(&wqh->lock);
	__remove_wait_queue(wqh, wq);
	spin_unlock(&wqh->lock);
}

bool linux_waitqueue_active(wait_queue_head_t *);

#define	waitqueue_active(wqh)		linux_waitqueue_active(wqh)

void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int);
void linux_finish_wait(wait_queue_head_t *, wait_queue_t *);

#define	prepare_to_wait(wqh, wq, state)	linux_prepare_to_wait(wqh, wq, state)
#define	finish_wait(wqh, wq)		linux_finish_wait(wqh, wq)

void linux_wake_up_bit(void *, int);
int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int);
void linux_wake_up_atomic_t(atomic_t *);
int linux_wait_on_atomic_t(atomic_t *, unsigned int);

#define	wake_up_bit(word, bit)		linux_wake_up_bit(word, bit)
#define	wait_on_bit(word, bit, state)					\
	linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT)
#define	wait_on_bit_timeout(word, bit, state, timeout)			\
	linux_wait_on_bit_timeout(word, bit, state, timeout)
#define	wake_up_atomic_t(a)		linux_wake_up_atomic_t(a)
/*
 * All existing callers have a cb that just schedule()s. To avoid adding
 * complexity, just emulate that internally. The prototype is different so that
 * callers must be manually modified; a cb that does something other than call
 * schedule() will require special treatment.
 */
#define	wait_on_atomic_t(a, state)	linux_wait_on_atomic_t(a, state)

struct task_struct;
bool linux_wake_up_state(struct task_struct *, unsigned int);

#define	wake_up_process(task)		linux_wake_up_state(task, TASK_NORMAL)
#define	wake_up_state(task, state)	linux_wake_up_state(task, state)

#endif /* _LINUX_WAIT_H_ */