sched.h revision 329976
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5329976Shselasky * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/sched.h 329976 2018-02-25 10:40:41Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_SCHED_H_
32219820Sjeff#define	_LINUX_SCHED_H_
33219820Sjeff
34219820Sjeff#include <sys/param.h>
35219820Sjeff#include <sys/systm.h>
36219820Sjeff#include <sys/proc.h>
37219820Sjeff#include <sys/sched.h>
38219820Sjeff#include <sys/sleepqueue.h>
39328653Shselasky#include <sys/time.h>
40219820Sjeff
41328653Shselasky#include <linux/bitmap.h>
42328653Shselasky#include <linux/compat.h>
43328653Shselasky#include <linux/completion.h>
44328653Shselasky#include <linux/mm_types.h>
45328653Shselasky#include <linux/pid.h>
46328653Shselasky#include <linux/slab.h>
47328653Shselasky#include <linux/string.h>
48328653Shselasky#include <linux/time.h>
49219820Sjeff
50328653Shselasky#include <asm/atomic.h>
51219820Sjeff
52328653Shselasky#define	MAX_SCHEDULE_TIMEOUT	INT_MAX
53219820Sjeff
54328653Shselasky#define	TASK_RUNNING		0x0000
55328653Shselasky#define	TASK_INTERRUPTIBLE	0x0001
56328653Shselasky#define	TASK_UNINTERRUPTIBLE	0x0002
57328653Shselasky#define	TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
58328653Shselasky#define	TASK_WAKING		0x0100
59328653Shselasky#define	TASK_PARKED		0x0200
60328653Shselasky
61328653Shselasky#define	TASK_COMM_LEN		(MAXCOMLEN + 1)
62328653Shselasky
63219820Sjeffstruct task_struct {
64328653Shselasky	struct thread *task_thread;
65328653Shselasky	struct mm_struct *mm;
66328653Shselasky	linux_task_fn_t *task_fn;
67328653Shselasky	void   *task_data;
68219820Sjeff	int	task_ret;
69328653Shselasky	atomic_t usage;
70328653Shselasky	atomic_t state;
71328653Shselasky	atomic_t kthread_flags;
72328653Shselasky	pid_t	pid;	/* BSD thread ID */
73299526Shselasky	const char    *comm;
74328653Shselasky	void   *bsd_ioctl_data;
75328653Shselasky	unsigned bsd_ioctl_len;
76328653Shselasky	struct completion parked;
77328653Shselasky	struct completion exited;
78328653Shselasky	TAILQ_ENTRY(task_struct) rcu_entry;
79328653Shselasky	int rcu_recurse;
80329976Shselasky	int bsd_interrupt_value;
81219820Sjeff};
82219820Sjeff
83328653Shselasky#define	current	({ \
84328653Shselasky	struct thread *__td = curthread; \
85328653Shselasky	linux_set_current(__td); \
86328653Shselasky	((struct task_struct *)__td->td_lkpi_task); \
87328653Shselasky})
88219820Sjeff
89328653Shselasky#define	task_pid_group_leader(task) (task)->task_thread->td_proc->p_pid
90328653Shselasky#define	task_pid(task)		((task)->pid)
91328653Shselasky#define	task_pid_nr(task)	((task)->pid)
92328653Shselasky#define	get_pid(x)		(x)
93328653Shselasky#define	put_pid(x)		do { } while (0)
94328653Shselasky#define	current_euid()	(curthread->td_ucred->cr_uid)
95289993Shselasky
96328653Shselasky#define	set_task_state(task, x)		atomic_set(&(task)->state, (x))
97328653Shselasky#define	__set_task_state(task, x)	((task)->state.counter = (x))
98328653Shselasky#define	set_current_state(x)		set_task_state(current, x)
99328653Shselasky#define	__set_current_state(x)		__set_task_state(current, x)
100219820Sjeff
101328653Shselaskystatic inline void
102328653Shselaskyget_task_struct(struct task_struct *task)
103328653Shselasky{
104328653Shselasky	atomic_inc(&task->usage);
105328653Shselasky}
106219820Sjeff
107328653Shselaskystatic inline void
108328653Shselaskyput_task_struct(struct task_struct *task)
109328653Shselasky{
110328653Shselasky	if (atomic_dec_and_test(&task->usage))
111328653Shselasky		linux_free_current(task);
112328653Shselasky}
113219820Sjeff
114328655Shselasky#define	cond_resched()	do { if (!cold) sched_relinquish(curthread); } while (0)
115219820Sjeff
116328653Shselasky#define	yield()		kern_yield(PRI_UNCHANGED)
117219820Sjeff#define	sched_yield()	sched_relinquish(curthread)
118219820Sjeff
119328653Shselasky#define	need_resched() (curthread->td_flags & TDF_NEEDRESCHED)
120328653Shselasky
121328653Shselaskybool linux_signal_pending(struct task_struct *task);
122328653Shselaskybool linux_fatal_signal_pending(struct task_struct *task);
123328653Shselaskybool linux_signal_pending_state(long state, struct task_struct *task);
124328653Shselaskyvoid linux_send_sig(int signo, struct task_struct *task);
125328653Shselasky
126328653Shselasky#define	signal_pending(task)		linux_signal_pending(task)
127328653Shselasky#define	fatal_signal_pending(task)	linux_fatal_signal_pending(task)
128328653Shselasky#define	signal_pending_state(state, task)		\
129328653Shselasky	linux_signal_pending_state(state, task)
130328653Shselasky#define	send_sig(signo, task, priv) do {		\
131329976Shselasky	CTASSERT((priv) == 0);				\
132328653Shselasky	linux_send_sig(signo, task);			\
133328653Shselasky} while (0)
134328653Shselasky
135328653Shselaskyint linux_schedule_timeout(int timeout);
136328653Shselasky
137329976Shselaskystatic inline void
138329976Shselaskylinux_schedule_save_interrupt_value(struct task_struct *task, int value)
139329976Shselasky{
140329976Shselasky	task->bsd_interrupt_value = value;
141329976Shselasky}
142329976Shselasky
143329976Shselaskystatic inline int
144329976Shselaskylinux_schedule_get_interrupt_value(struct task_struct *task)
145329976Shselasky{
146329976Shselasky	int value = task->bsd_interrupt_value;
147329976Shselasky	task->bsd_interrupt_value = 0;
148329976Shselasky	return (value);
149329976Shselasky}
150329976Shselasky
151328653Shselasky#define	schedule()					\
152328653Shselasky	(void)linux_schedule_timeout(MAX_SCHEDULE_TIMEOUT)
153328653Shselasky#define	schedule_timeout(timeout)			\
154328653Shselasky	linux_schedule_timeout(timeout)
155328653Shselasky#define	schedule_timeout_killable(timeout)		\
156328653Shselasky	schedule_timeout_uninterruptible(timeout)
157328653Shselasky#define	schedule_timeout_interruptible(timeout) ({	\
158328653Shselasky	set_current_state(TASK_INTERRUPTIBLE);		\
159328653Shselasky	schedule_timeout(timeout);			\
160328653Shselasky})
161328653Shselasky#define	schedule_timeout_uninterruptible(timeout) ({	\
162328653Shselasky	set_current_state(TASK_UNINTERRUPTIBLE);	\
163328653Shselasky	schedule_timeout(timeout);			\
164328653Shselasky})
165328653Shselasky
166328653Shselasky#define	io_schedule()			schedule()
167328653Shselasky#define	io_schedule_timeout(timeout)	schedule_timeout(timeout)
168328653Shselasky
169328653Shselaskystatic inline uint64_t
170328653Shselaskylocal_clock(void)
171289566Shselasky{
172328653Shselasky	struct timespec ts;
173289566Shselasky
174328653Shselasky	nanotime(&ts);
175328653Shselasky	return ((uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec);
176289566Shselasky}
177289566Shselasky
178219820Sjeff#endif	/* _LINUX_SCHED_H_ */
179