xref: /freebsd/sys/compat/linuxkpi/common/src/linux_kthread.c (revision ab00ac327a66a53edaac95b536b209db3ae2cd9f)
1 /*-
2  * Copyright (c) 2017 Hans Petter Selasky
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <linux/kthread.h>
31 #include <linux/sched.h>
32 #include <linux/compat.h>
33 
34 #include <sys/bus.h>
35 #include <sys/interrupt.h>
36 #include <sys/priority.h>
37 
38 enum {
39 	KTHREAD_SHOULD_STOP_MASK = (1 << 0),
40 	KTHREAD_SHOULD_PARK_MASK = (1 << 1),
41 	KTHREAD_IS_PARKED_MASK = (1 << 2),
42 };
43 
44 bool
45 kthread_should_stop_task(struct task_struct *task)
46 {
47 
48 	return (atomic_read(&task->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
49 }
50 
51 bool
52 kthread_should_stop(void)
53 {
54 
55 	return (atomic_read(&current->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
56 }
57 
58 int
59 kthread_stop(struct task_struct *task)
60 {
61 	int retval;
62 
63 	/*
64 	 * Assume task is still alive else caller should not call
65 	 * kthread_stop():
66 	 */
67 	atomic_or(KTHREAD_SHOULD_STOP_MASK, &task->kthread_flags);
68 	wake_up_process(task);
69 	wait_for_completion(&task->exited);
70 
71 	/*
72 	 * Get return code and free task structure:
73 	 */
74 	retval = task->task_ret;
75 	put_task_struct(task);
76 
77 	return (retval);
78 }
79 
80 struct task_struct *
81 linux_kthread_setup_and_run(struct thread *td, linux_task_fn_t *task_fn, void *arg)
82 {
83 	struct task_struct *task;
84 
85 	linux_set_current(td);
86 
87 	task = td->td_lkpi_task;
88 	task->task_fn = task_fn;
89 	task->task_data = arg;
90 
91 	thread_lock(td);
92 	/* make sure the scheduler priority is raised */
93 	sched_prio(td, PI_SWI(SWI_NET));
94 	/* put thread into run-queue */
95 	sched_add(td, SRQ_BORING);
96 	thread_unlock(td);
97 
98 	return (task);
99 }
100 
101 void
102 linux_kthread_fn(void *arg __unused)
103 {
104 	struct task_struct *task = current;
105 
106 	if (kthread_should_stop_task(task) == 0)
107 		task->task_ret = task->task_fn(task->task_data);
108 
109 	if (kthread_should_stop_task(task) != 0) {
110 		struct thread *td = curthread;
111 
112 		/* let kthread_stop() free data */
113 		td->td_lkpi_task = NULL;
114 
115 		/* wakeup kthread_stop() */
116 		complete(&task->exited);
117 	}
118 	kthread_exit();
119 }
120 
121