xref: /freebsd/sys/compat/linuxkpi/common/src/linux_kthread.c (revision b7eaed250fccfdef218a62bc2d0af529ad75341c)
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/compat.h>
31 #include <linux/kthread.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
34 
35 #include <sys/bus.h>
36 #include <sys/interrupt.h>
37 #include <sys/priority.h>
38 
39 enum {
40 	KTHREAD_SHOULD_STOP_MASK = (1 << 0),
41 	KTHREAD_SHOULD_PARK_MASK = (1 << 1),
42 	KTHREAD_IS_PARKED_MASK = (1 << 2),
43 };
44 
45 bool
46 kthread_should_stop_task(struct task_struct *task)
47 {
48 
49 	return (atomic_read(&task->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
50 }
51 
52 bool
53 kthread_should_stop(void)
54 {
55 
56 	return (atomic_read(&current->kthread_flags) & KTHREAD_SHOULD_STOP_MASK);
57 }
58 
59 int
60 kthread_stop(struct task_struct *task)
61 {
62 	int retval;
63 
64 	/*
65 	 * Assume task is still alive else caller should not call
66 	 * kthread_stop():
67 	 */
68 	atomic_or(KTHREAD_SHOULD_STOP_MASK, &task->kthread_flags);
69 	wake_up_process(task);
70 	wait_for_completion(&task->exited);
71 
72 	/*
73 	 * Get return code and free task structure:
74 	 */
75 	retval = task->task_ret;
76 	put_task_struct(task);
77 
78 	return (retval);
79 }
80 
81 struct task_struct *
82 linux_kthread_setup_and_run(struct thread *td, linux_task_fn_t *task_fn, void *arg)
83 {
84 	struct task_struct *task;
85 
86 	linux_set_current(td);
87 
88 	task = td->td_lkpi_task;
89 	task->task_fn = task_fn;
90 	task->task_data = arg;
91 
92 	thread_lock(td);
93 	/* make sure the scheduler priority is raised */
94 	sched_prio(td, PI_SWI(SWI_NET));
95 	/* put thread into run-queue */
96 	sched_add(td, SRQ_BORING);
97 	thread_unlock(td);
98 
99 	return (task);
100 }
101 
102 void
103 linux_kthread_fn(void *arg __unused)
104 {
105 	struct task_struct *task = current;
106 
107 	if (kthread_should_stop_task(task) == 0)
108 		task->task_ret = task->task_fn(task->task_data);
109 
110 	if (kthread_should_stop_task(task) != 0) {
111 		struct thread *td = curthread;
112 
113 		/* let kthread_stop() free data */
114 		td->td_lkpi_task = NULL;
115 
116 		/* wakeup kthread_stop() */
117 		complete(&task->exited);
118 	}
119 	kthread_exit();
120 }
121 
122