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: stable/11/sys/compat/linuxkpi/common/src/linux_current.c 363153 2020-07-13 15:37:59Z hselasky $");
29
30#include <linux/compat.h>
31#include <linux/completion.h>
32#include <linux/mm.h>
33#include <linux/kthread.h>
34
35#include <sys/kernel.h>
36#include <sys/eventhandler.h>
37#include <sys/malloc.h>
38
39static eventhandler_tag linuxkpi_thread_dtor_tag;
40
41static MALLOC_DEFINE(M_LINUX_CURRENT, "linuxcurrent", "LinuxKPI task structure");
42
43int
44linux_alloc_current(struct thread *td, int flags)
45{
46	struct proc *proc;
47	struct thread *td_other;
48	struct task_struct *ts;
49	struct task_struct *ts_other;
50	struct mm_struct *mm;
51	struct mm_struct *mm_other;
52
53	MPASS(td->td_lkpi_task == NULL);
54
55	ts = malloc(sizeof(*ts), M_LINUX_CURRENT, flags | M_ZERO);
56	if (ts == NULL)
57		return (ENOMEM);
58
59	mm = malloc(sizeof(*mm), M_LINUX_CURRENT, flags | M_ZERO);
60	if (mm == NULL) {
61		free(ts, M_LINUX_CURRENT);
62		return (ENOMEM);
63	}
64
65	/* setup new task structure */
66	atomic_set(&ts->kthread_flags, 0);
67	ts->task_thread = td;
68	ts->comm = td->td_name;
69	ts->pid = td->td_tid;
70	atomic_set(&ts->usage, 1);
71	atomic_set(&ts->state, TASK_RUNNING);
72	init_completion(&ts->parked);
73	init_completion(&ts->exited);
74
75	proc = td->td_proc;
76
77	/* check if another thread already has a mm_struct */
78	PROC_LOCK(proc);
79	FOREACH_THREAD_IN_PROC(proc, td_other) {
80		ts_other = td_other->td_lkpi_task;
81		if (ts_other == NULL)
82			continue;
83
84		mm_other = ts_other->mm;
85		if (mm_other == NULL)
86			continue;
87
88		/* try to share other mm_struct */
89		if (atomic_inc_not_zero(&mm_other->mm_users)) {
90			/* set mm_struct pointer */
91			ts->mm = mm_other;
92			break;
93		}
94	}
95
96	/* use allocated mm_struct as a fallback */
97	if (ts->mm == NULL) {
98		/* setup new mm_struct */
99		init_rwsem(&mm->mmap_sem);
100		atomic_set(&mm->mm_count, 1);
101		atomic_set(&mm->mm_users, 1);
102		/* set mm_struct pointer */
103		ts->mm = mm;
104		/* clear pointer to not free memory */
105		mm = NULL;
106	}
107
108	/* store pointer to task struct */
109	td->td_lkpi_task = ts;
110	PROC_UNLOCK(proc);
111
112	/* free mm_struct pointer, if any */
113	free(mm, M_LINUX_CURRENT);
114
115	return (0);
116}
117
118struct mm_struct *
119linux_get_task_mm(struct task_struct *task)
120{
121	struct mm_struct *mm;
122
123	mm = task->mm;
124	if (mm != NULL) {
125		atomic_inc(&mm->mm_users);
126		return (mm);
127	}
128	return (NULL);
129}
130
131void
132linux_mm_dtor(struct mm_struct *mm)
133{
134	free(mm, M_LINUX_CURRENT);
135}
136
137void
138linux_free_current(struct task_struct *ts)
139{
140	mmput(ts->mm);
141	free(ts, M_LINUX_CURRENT);
142}
143
144static void
145linuxkpi_thread_dtor(void *arg __unused, struct thread *td)
146{
147	struct task_struct *ts;
148
149	ts = td->td_lkpi_task;
150	if (ts == NULL)
151		return;
152
153	td->td_lkpi_task = NULL;
154	put_task_struct(ts);
155}
156
157struct task_struct *
158linux_pid_task(pid_t pid)
159{
160	struct thread *td;
161	struct proc *p;
162
163	/* try to find corresponding thread */
164	td = tdfind(pid, -1);
165	if (td != NULL) {
166		struct task_struct *ts = td->td_lkpi_task;
167		PROC_UNLOCK(td->td_proc);
168		return (ts);
169	}
170
171	/* try to find corresponding procedure */
172	p = pfind(pid);
173	if (p != NULL) {
174		FOREACH_THREAD_IN_PROC(p, td) {
175			struct task_struct *ts = td->td_lkpi_task;
176			if (ts != NULL) {
177				PROC_UNLOCK(p);
178				return (ts);
179			}
180		}
181		PROC_UNLOCK(p);
182	}
183	return (NULL);
184}
185
186struct task_struct *
187linux_get_pid_task(pid_t pid)
188{
189	struct thread *td;
190	struct proc *p;
191
192	/* try to find corresponding thread */
193	td = tdfind(pid, -1);
194	if (td != NULL) {
195		struct task_struct *ts = td->td_lkpi_task;
196		if (ts != NULL)
197			get_task_struct(ts);
198		PROC_UNLOCK(td->td_proc);
199		return (ts);
200	}
201
202	/* try to find corresponding procedure */
203	p = pfind(pid);
204	if (p != NULL) {
205		FOREACH_THREAD_IN_PROC(p, td) {
206			struct task_struct *ts = td->td_lkpi_task;
207			if (ts != NULL) {
208				get_task_struct(ts);
209				PROC_UNLOCK(p);
210				return (ts);
211			}
212		}
213		PROC_UNLOCK(p);
214	}
215	return (NULL);
216}
217
218bool
219linux_task_exiting(struct task_struct *task)
220{
221	struct thread *td;
222	struct proc *p;
223	bool ret;
224
225	ret = false;
226
227	/* try to find corresponding thread */
228	td = tdfind(task->pid, -1);
229	if (td != NULL) {
230		p = td->td_proc;
231	} else {
232		/* try to find corresponding procedure */
233		p = pfind(task->pid);
234	}
235
236	if (p != NULL) {
237		if ((p->p_flag & P_WEXIT) != 0)
238			ret = true;
239		PROC_UNLOCK(p);
240	}
241	return (ret);
242}
243
244static void
245linux_current_init(void *arg __unused)
246{
247	linuxkpi_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
248	    linuxkpi_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
249}
250SYSINIT(linux_current, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_current_init, NULL);
251
252static void
253linux_current_uninit(void *arg __unused)
254{
255	struct proc *p;
256	struct task_struct *ts;
257	struct thread *td;
258
259	sx_slock(&allproc_lock);
260	FOREACH_PROC_IN_SYSTEM(p) {
261		PROC_LOCK(p);
262		FOREACH_THREAD_IN_PROC(p, td) {
263			if ((ts = td->td_lkpi_task) != NULL) {
264				td->td_lkpi_task = NULL;
265				put_task_struct(ts);
266			}
267		}
268		PROC_UNLOCK(p);
269	}
270	sx_sunlock(&allproc_lock);
271
272	EVENTHANDLER_DEREGISTER(thread_dtor, linuxkpi_thread_dtor_tag);
273}
274SYSUNINIT(linux_current, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_current_uninit, NULL);
275