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