sched.h revision 345924
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 345924 2019-04-05 11:19:23Z 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
63337897Shselaskystruct work_struct;
64219820Sjeffstruct task_struct {
65328653Shselasky	struct thread *task_thread;
66328653Shselasky	struct mm_struct *mm;
67328653Shselasky	linux_task_fn_t *task_fn;
68328653Shselasky	void   *task_data;
69219820Sjeff	int	task_ret;
70328653Shselasky	atomic_t usage;
71328653Shselasky	atomic_t state;
72328653Shselasky	atomic_t kthread_flags;
73328653Shselasky	pid_t	pid;	/* BSD thread ID */
74299526Shselasky	const char    *comm;
75328653Shselasky	void   *bsd_ioctl_data;
76328653Shselasky	unsigned bsd_ioctl_len;
77328653Shselasky	struct completion parked;
78328653Shselasky	struct completion exited;
79328653Shselasky	TAILQ_ENTRY(task_struct) rcu_entry;
80328653Shselasky	int rcu_recurse;
81329976Shselasky	int bsd_interrupt_value;
82337897Shselasky	struct work_struct *work;	/* current work struct, if set */
83219820Sjeff};
84219820Sjeff
85328653Shselasky#define	current	({ \
86328653Shselasky	struct thread *__td = curthread; \
87328653Shselasky	linux_set_current(__td); \
88328653Shselasky	((struct task_struct *)__td->td_lkpi_task); \
89328653Shselasky})
90219820Sjeff
91328653Shselasky#define	task_pid_group_leader(task) (task)->task_thread->td_proc->p_pid
92328653Shselasky#define	task_pid(task)		((task)->pid)
93328653Shselasky#define	task_pid_nr(task)	((task)->pid)
94335417Shselasky#define	task_pid_vnr(task)	((task)->pid)
95328653Shselasky#define	get_pid(x)		(x)
96328653Shselasky#define	put_pid(x)		do { } while (0)
97328653Shselasky#define	current_euid()	(curthread->td_ucred->cr_uid)
98289993Shselasky
99328653Shselasky#define	set_task_state(task, x)		atomic_set(&(task)->state, (x))
100328653Shselasky#define	__set_task_state(task, x)	((task)->state.counter = (x))
101328653Shselasky#define	set_current_state(x)		set_task_state(current, x)
102328653Shselasky#define	__set_current_state(x)		__set_task_state(current, x)
103219820Sjeff
104328653Shselaskystatic inline void
105328653Shselaskyget_task_struct(struct task_struct *task)
106328653Shselasky{
107328653Shselasky	atomic_inc(&task->usage);
108328653Shselasky}
109219820Sjeff
110328653Shselaskystatic inline void
111328653Shselaskyput_task_struct(struct task_struct *task)
112328653Shselasky{
113328653Shselasky	if (atomic_dec_and_test(&task->usage))
114328653Shselasky		linux_free_current(task);
115328653Shselasky}
116219820Sjeff
117328655Shselasky#define	cond_resched()	do { if (!cold) sched_relinquish(curthread); } while (0)
118219820Sjeff
119328653Shselasky#define	yield()		kern_yield(PRI_UNCHANGED)
120219820Sjeff#define	sched_yield()	sched_relinquish(curthread)
121219820Sjeff
122328653Shselasky#define	need_resched() (curthread->td_flags & TDF_NEEDRESCHED)
123328653Shselasky
124328653Shselaskybool linux_signal_pending(struct task_struct *task);
125328653Shselaskybool linux_fatal_signal_pending(struct task_struct *task);
126328653Shselaskybool linux_signal_pending_state(long state, struct task_struct *task);
127328653Shselaskyvoid linux_send_sig(int signo, struct task_struct *task);
128328653Shselasky
129328653Shselasky#define	signal_pending(task)		linux_signal_pending(task)
130328653Shselasky#define	fatal_signal_pending(task)	linux_fatal_signal_pending(task)
131328653Shselasky#define	signal_pending_state(state, task)		\
132328653Shselasky	linux_signal_pending_state(state, task)
133328653Shselasky#define	send_sig(signo, task, priv) do {		\
134329976Shselasky	CTASSERT((priv) == 0);				\
135328653Shselasky	linux_send_sig(signo, task);			\
136328653Shselasky} while (0)
137328653Shselasky
138328653Shselaskyint linux_schedule_timeout(int timeout);
139328653Shselasky
140329976Shselaskystatic inline void
141329976Shselaskylinux_schedule_save_interrupt_value(struct task_struct *task, int value)
142329976Shselasky{
143329976Shselasky	task->bsd_interrupt_value = value;
144329976Shselasky}
145329976Shselasky
146345924Shselaskybool linux_task_exiting(struct task_struct *task);
147345924Shselasky
148345924Shselasky#define	current_exiting() \
149345924Shselasky	linux_task_exiting(current)
150345924Shselasky
151329976Shselaskystatic inline int
152329976Shselaskylinux_schedule_get_interrupt_value(struct task_struct *task)
153329976Shselasky{
154329976Shselasky	int value = task->bsd_interrupt_value;
155329976Shselasky	task->bsd_interrupt_value = 0;
156329976Shselasky	return (value);
157329976Shselasky}
158329976Shselasky
159328653Shselasky#define	schedule()					\
160328653Shselasky	(void)linux_schedule_timeout(MAX_SCHEDULE_TIMEOUT)
161328653Shselasky#define	schedule_timeout(timeout)			\
162328653Shselasky	linux_schedule_timeout(timeout)
163328653Shselasky#define	schedule_timeout_killable(timeout)		\
164334761Shselasky	schedule_timeout_interruptible(timeout)
165328653Shselasky#define	schedule_timeout_interruptible(timeout) ({	\
166328653Shselasky	set_current_state(TASK_INTERRUPTIBLE);		\
167328653Shselasky	schedule_timeout(timeout);			\
168328653Shselasky})
169328653Shselasky#define	schedule_timeout_uninterruptible(timeout) ({	\
170328653Shselasky	set_current_state(TASK_UNINTERRUPTIBLE);	\
171328653Shselasky	schedule_timeout(timeout);			\
172328653Shselasky})
173328653Shselasky
174328653Shselasky#define	io_schedule()			schedule()
175328653Shselasky#define	io_schedule_timeout(timeout)	schedule_timeout(timeout)
176328653Shselasky
177328653Shselaskystatic inline uint64_t
178328653Shselaskylocal_clock(void)
179289566Shselasky{
180328653Shselasky	struct timespec ts;
181289566Shselasky
182328653Shselasky	nanotime(&ts);
183328653Shselasky	return ((uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec);
184289566Shselasky}
185289566Shselasky
186219820Sjeff#endif	/* _LINUX_SCHED_H_ */
187