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