1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4 * Copyright (C) 2007 The Regents of the University of California.
5 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
7 * UCRL-CODE-235197
8 *
9 * This file is part of the SPL, Solaris Porting Layer.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Solaris Porting Layer (SPL) Thread Implementation.
25 */
26
27 #include <sys/thread.h>
28 #include <sys/kmem.h>
29 #include <sys/tsd.h>
30 #include <sys/string.h>
31 #include <sys/misc.h>
32 #include <linux/kthread_compat.h>
33
34 /*
35 * Thread interfaces
36 */
37 typedef struct thread_priv_s {
38 unsigned long tp_magic; /* Magic */
39 int tp_name_size; /* Name size */
40 char *tp_name; /* Name (without _thread suffix) */
41 void (*tp_func)(void *); /* Registered function */
42 void *tp_args; /* Args to be passed to function */
43 size_t tp_len; /* Len to be passed to function */
44 int tp_state; /* State to start thread at */
45 pri_t tp_pri; /* Priority to start threat at */
46 } thread_priv_t;
47
48 static int
thread_generic_wrapper(void * arg)49 thread_generic_wrapper(void *arg)
50 {
51 thread_priv_t *tp = (thread_priv_t *)arg;
52 void (*func)(void *);
53 void *args;
54
55 ASSERT(tp->tp_magic == TP_MAGIC);
56 func = tp->tp_func;
57 args = tp->tp_args;
58 set_current_state(tp->tp_state);
59 set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri));
60 kmem_free(tp->tp_name, tp->tp_name_size);
61 kmem_free(tp, sizeof (thread_priv_t));
62
63 if (func) {
64 scoped_with_init_fs()
65 func(args);
66 }
67
68 return (0);
69 }
70
71 /*
72 * thread_create() may block forever if it cannot create a thread or
73 * allocate memory. This is preferable to returning a NULL which Solaris
74 * style callers likely never check for... since it can't fail.
75 */
76 kthread_t *
__thread_create(caddr_t stk,size_t stksize,thread_func_t func,const char * name,void * args,size_t len,proc_t * pp,int state,pri_t pri)77 __thread_create(caddr_t stk, size_t stksize, thread_func_t func,
78 const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri)
79 {
80 thread_priv_t *tp;
81 struct task_struct *tsk;
82 char *p;
83
84 /* Option pp is simply ignored */
85 /* Variable stack size unsupported */
86 ASSERT0P(stk);
87
88 tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE);
89 if (tp == NULL)
90 return (NULL);
91
92 tp->tp_magic = TP_MAGIC;
93 tp->tp_name_size = strlen(name) + 1;
94
95 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);
96 if (tp->tp_name == NULL) {
97 kmem_free(tp, sizeof (thread_priv_t));
98 return (NULL);
99 }
100
101 strlcpy(tp->tp_name, name, tp->tp_name_size);
102
103 /*
104 * Strip trailing "_thread" from passed name which will be the func
105 * name since the exposed API has no parameter for passing a name.
106 */
107 p = strstr(tp->tp_name, "_thread");
108 if (p)
109 p[0] = '\0';
110
111 tp->tp_func = func;
112 tp->tp_args = args;
113 tp->tp_len = len;
114 tp->tp_state = state;
115 tp->tp_pri = pri;
116
117 tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,
118 "%s", tp->tp_name);
119 if (IS_ERR(tsk))
120 return (NULL);
121
122 wake_up_process(tsk);
123 return ((kthread_t *)tsk);
124 }
125 EXPORT_SYMBOL(__thread_create);
126
127 /*
128 * spl_kthread_create - Wrapper providing pre-3.13 semantics for
129 * kthread_create() in which it is not killable and less likely
130 * to return -ENOMEM.
131 */
132 struct task_struct *
spl_kthread_create(int (* func)(void *),void * data,const char namefmt[],...)133 spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...)
134 {
135 struct task_struct *tsk;
136 va_list args;
137 char name[TASK_COMM_LEN];
138
139 va_start(args, namefmt);
140 vsnprintf(name, sizeof (name), namefmt, args);
141 va_end(args);
142 do {
143 tsk = kthread_create(func, data, "%s", name);
144 if (IS_ERR(tsk)) {
145 if (signal_pending(current)) {
146 clear_thread_flag(TIF_SIGPENDING);
147 continue;
148 }
149 if (PTR_ERR(tsk) == -ENOMEM)
150 continue;
151 return (NULL);
152 } else {
153 return (tsk);
154 }
155 } while (1);
156 }
157 EXPORT_SYMBOL(spl_kthread_create);
158
159 /*
160 * Extract the next pending signal from p_sig into p_cursig; stop the process
161 * if a stop has been requested or if a traced signal is pending.
162 */
163 int
issig(void)164 issig(void)
165 {
166
167 if (!signal_pending(current))
168 return (0);
169
170 spl_kernel_siginfo_t __info;
171 sigset_t set;
172 siginitsetinv(&set, 1ULL << (SIGSTOP - 1) | 1ULL << (SIGTSTP - 1));
173 sigorsets(&set, ¤t->blocked, &set);
174
175 spin_lock_irq(¤t->sighand->siglock);
176 #if defined(HAVE_DEQUEUE_SIGNAL_4ARG)
177 enum pid_type __type;
178 if (dequeue_signal(current, &set, &__info, &__type) != 0) {
179 #elif defined(HAVE_DEQUEUE_SIGNAL_3ARG_TYPE)
180 enum pid_type __type;
181 if (dequeue_signal(&set, &__info, &__type) != 0) {
182 #else
183 if (dequeue_signal(current, &set, &__info) != 0) {
184 #endif
185 spin_unlock_irq(¤t->sighand->siglock);
186 kernel_signal_stop();
187
188 /*
189 * Dequeued SIGSTOP/SIGTSTP.
190 * Check if process has other singal pending.
191 */
192 if (signal_pending(current))
193 return (1);
194
195 return (0);
196 }
197
198 spin_unlock_irq(¤t->sighand->siglock);
199
200 return (1);
201 }
202
203 EXPORT_SYMBOL(issig);
204
205 /*
206 * Check if the current thread is a memory reclaim thread.
207 * Returns true if current thread is kswapd.
208 */
209 int
210 current_is_reclaim_thread(void)
211 {
212 return (current_is_kswapd());
213 }
214 EXPORT_SYMBOL(current_is_reclaim_thread);
215