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
33 /*
34 * Thread interfaces
35 */
36 typedef struct thread_priv_s {
37 unsigned long tp_magic; /* Magic */
38 int tp_name_size; /* Name size */
39 char *tp_name; /* Name (without _thread suffix) */
40 void (*tp_func)(void *); /* Registered function */
41 void *tp_args; /* Args to be passed to function */
42 size_t tp_len; /* Len to be passed to function */
43 int tp_state; /* State to start thread at */
44 pri_t tp_pri; /* Priority to start threat at */
45 } thread_priv_t;
46
47 static int
thread_generic_wrapper(void * arg)48 thread_generic_wrapper(void *arg)
49 {
50 thread_priv_t *tp = (thread_priv_t *)arg;
51 void (*func)(void *);
52 void *args;
53
54 ASSERT(tp->tp_magic == TP_MAGIC);
55 func = tp->tp_func;
56 args = tp->tp_args;
57 set_current_state(tp->tp_state);
58 set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri));
59 kmem_free(tp->tp_name, tp->tp_name_size);
60 kmem_free(tp, sizeof (thread_priv_t));
61
62 if (func)
63 func(args);
64
65 return (0);
66 }
67
68 /*
69 * thread_create() may block forever if it cannot create a thread or
70 * allocate memory. This is preferable to returning a NULL which Solaris
71 * style callers likely never check for... since it can't fail.
72 */
73 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)74 __thread_create(caddr_t stk, size_t stksize, thread_func_t func,
75 const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri)
76 {
77 thread_priv_t *tp;
78 struct task_struct *tsk;
79 char *p;
80
81 /* Option pp is simply ignored */
82 /* Variable stack size unsupported */
83 ASSERT0P(stk);
84
85 tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE);
86 if (tp == NULL)
87 return (NULL);
88
89 tp->tp_magic = TP_MAGIC;
90 tp->tp_name_size = strlen(name) + 1;
91
92 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);
93 if (tp->tp_name == NULL) {
94 kmem_free(tp, sizeof (thread_priv_t));
95 return (NULL);
96 }
97
98 strlcpy(tp->tp_name, name, tp->tp_name_size);
99
100 /*
101 * Strip trailing "_thread" from passed name which will be the func
102 * name since the exposed API has no parameter for passing a name.
103 */
104 p = strstr(tp->tp_name, "_thread");
105 if (p)
106 p[0] = '\0';
107
108 tp->tp_func = func;
109 tp->tp_args = args;
110 tp->tp_len = len;
111 tp->tp_state = state;
112 tp->tp_pri = pri;
113
114 tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,
115 "%s", tp->tp_name);
116 if (IS_ERR(tsk))
117 return (NULL);
118
119 wake_up_process(tsk);
120 return ((kthread_t *)tsk);
121 }
122 EXPORT_SYMBOL(__thread_create);
123
124 /*
125 * spl_kthread_create - Wrapper providing pre-3.13 semantics for
126 * kthread_create() in which it is not killable and less likely
127 * to return -ENOMEM.
128 */
129 struct task_struct *
spl_kthread_create(int (* func)(void *),void * data,const char namefmt[],...)130 spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...)
131 {
132 struct task_struct *tsk;
133 va_list args;
134 char name[TASK_COMM_LEN];
135
136 va_start(args, namefmt);
137 vsnprintf(name, sizeof (name), namefmt, args);
138 va_end(args);
139 do {
140 tsk = kthread_create(func, data, "%s", name);
141 if (IS_ERR(tsk)) {
142 if (signal_pending(current)) {
143 clear_thread_flag(TIF_SIGPENDING);
144 continue;
145 }
146 if (PTR_ERR(tsk) == -ENOMEM)
147 continue;
148 return (NULL);
149 } else {
150 return (tsk);
151 }
152 } while (1);
153 }
154 EXPORT_SYMBOL(spl_kthread_create);
155
156 /*
157 * Extract the next pending signal from p_sig into p_cursig; stop the process
158 * if a stop has been requested or if a traced signal is pending.
159 */
160 int
issig(void)161 issig(void)
162 {
163
164 if (!signal_pending(current))
165 return (0);
166
167 spl_kernel_siginfo_t __info;
168 sigset_t set;
169 siginitsetinv(&set, 1ULL << (SIGSTOP - 1) | 1ULL << (SIGTSTP - 1));
170 sigorsets(&set, ¤t->blocked, &set);
171
172 spin_lock_irq(¤t->sighand->siglock);
173 #if defined(HAVE_DEQUEUE_SIGNAL_4ARG)
174 enum pid_type __type;
175 if (dequeue_signal(current, &set, &__info, &__type) != 0) {
176 #elif defined(HAVE_DEQUEUE_SIGNAL_3ARG_TYPE)
177 enum pid_type __type;
178 if (dequeue_signal(&set, &__info, &__type) != 0) {
179 #else
180 if (dequeue_signal(current, &set, &__info) != 0) {
181 #endif
182 spin_unlock_irq(¤t->sighand->siglock);
183 kernel_signal_stop();
184
185 /*
186 * Dequeued SIGSTOP/SIGTSTP.
187 * Check if process has other singal pending.
188 */
189 if (signal_pending(current))
190 return (1);
191
192 return (0);
193 }
194
195 spin_unlock_irq(¤t->sighand->siglock);
196
197 return (1);
198 }
199
200 EXPORT_SYMBOL(issig);
201
202 /*
203 * Check if the current thread is a memory reclaim thread.
204 * Returns true if current thread is kswapd.
205 */
206 int
207 current_is_reclaim_thread(void)
208 {
209 return (current_is_kswapd());
210 }
211 EXPORT_SYMBOL(current_is_reclaim_thread);
212