summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_umtx.c
blob: 8c01669b7c5984b431e59a613d8a56b0cb3d0233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/sysent.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/thr.h>
#include <sys/umtx.h>

int
_umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
    /* struct umtx *umtx */
{
	struct umtx *umtx;
	struct thread *blocked;
	intptr_t owner;
	intptr_t old;
	int error;

	error = 0;

	/*
	 * Care must be exercised when dealing with this structure.  It
	 * can fault on any access.
	 */
	umtx = uap->umtx;	

	PROC_LOCK(td->td_proc);

	for (;;) {
		/*
		 * Try the uncontested case.  This should be done in userland.
		 */
		owner = casuptr((intptr_t *)&umtx->u_owner,
		    UMTX_UNOWNED, (intptr_t)td);

		/* The acquire succeeded. */
		if (owner == UMTX_UNOWNED) {
			error = 0;
			goto out;
		}

		/* The address was invalid. */
		if (owner == -1) {
			error = EFAULT;
			goto out;
		}

		if (owner & UMTX_CONTESTED)
			break;

		/*
		 * Set the contested bit so that a release in user space
		 * knows to use the system call for unlock.  If this fails
		 * either some one else has acquired the lock or it has been
		 * released.
		 */
		old = casuptr((intptr_t *)&umtx->u_owner, owner,
		    owner | UMTX_CONTESTED);

		/* We set the contested bit. */
		if (old == owner)
			break;

		/* The address was invalid. */
		if (old == -1) {
			error = EFAULT;
			goto out;
		}
		/* We didn't set the contested bit, try again. */
	}

	/*
	 * We are now protected from further races via the proc lock.
	 * If userland messes with their mutex without using cmpset
	 * they will deadlock themselves but they will still be
	 * killable via signals.
	 */

	if ((owner = fuword(&umtx->u_blocked)) == -1) {
		error = EFAULT;
		goto out;
	}

	if (owner == UMTX_UNOWNED) {
		if (suword(&umtx->u_blocked, (long)td) == -1) {
			error = EFAULT;
			goto out;
		}
		/*
		 * Other blocked threads will reside here.
		 */
		STAILQ_INIT(&td->td_umtxq);
	} else {
		FOREACH_THREAD_IN_PROC(td->td_proc, blocked)
			if (blocked == (struct thread *)(owner))
				break;

		if (blocked == NULL) {
			error = EINVAL;
			goto out;
		}
		/*
		 * Insert us onto the end of the TAILQ.
		 */
		STAILQ_INSERT_TAIL(&blocked->td_umtxq, td, td_umtx);
	}

	for (;;) {
		/*
		 * Sleep until we can acquire the lock.  We must still deliver
		 * signals so that they are not deferred until we acquire the
		 * lock which may be never.  The threads actual priority is
		 * used to maintain proper ordering.
		 */

		error = msleep(&td->td_umtx, &td->td_proc->p_mtx,
		    td->td_priority | PCATCH, "umtx", 0);

		/*
		 * When we are woken up we need to see if we now own the lock
		 * even if a signal was delivered.
		 */
		if ((owner = fuword(&umtx->u_owner)) == -1) {
			error = EFAULT;
			break;
		}
		owner &= ~UMTX_CONTESTED;
		if ((struct thread *)owner == td) {
			error = 0;
			break;
		}

		/*
		 * We may have signals to deliver.
		 */
		if (error)
			break;
	}

out:
	PROC_UNLOCK(td->td_proc);

	return (error);
}

int
_umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
    /* struct umtx *umtx */
{
	struct thread *td0;
	struct umtx *umtx;
	intptr_t owner;
	intptr_t blocked;
	intptr_t old;
	int error;

	error = 0;
	umtx = uap->umtx;

	PROC_LOCK(td->td_proc);

	/*
	 * Make sure we own this mtx.
	 *
	 * XXX Need a {fu,su}ptr this is not correct on arch where
	 * sizeof(intptr_t) != sizeof(long).
	 */
	if ((owner = fuword(&umtx->u_owner)) == -1) {
		error = EFAULT;
		goto out;
	}
	if ((struct thread *)(owner & ~UMTX_CONTESTED) != td) {
		error = EPERM;
		goto out;
	}
	/*
	 * If we own it but it isn't contested then we can just release and
	 * return.
	 */
	if ((owner & UMTX_CONTESTED) == 0) {
		owner = casuptr((intptr_t *)&umtx->u_owner,
		    (intptr_t)td, UMTX_UNOWNED);

		if (owner == -1)
			error = EFAULT;
		/*
		 * If this failed someone modified the memory without going
		 * through this api.
		 */
		else if (owner != (intptr_t)td)
			error = EINVAL;
		else
			error = 0;

		goto out;
	}

	/*
	 * Since we own the mutex and the proc lock we are free to inspect
	 * the blocked queue.  It must have one valid entry since the
	 * CONTESTED bit was set.
	 */
	blocked = fuword(&umtx->u_blocked);
	if (blocked == -1) { 
		error = EFAULT;
		goto out;
	}
	if (blocked == 0) {
		error = EINVAL;
		goto out;
	}

	FOREACH_THREAD_IN_PROC(td->td_proc, td0)
		if (td0 == (struct thread *)blocked)
			break;

	if (td0 == NULL) {
		error = EINVAL;
		goto out;
	}

	if (!STAILQ_EMPTY(&td0->td_umtxq)) {
		struct thread *next;

		blocked |= UMTX_CONTESTED;
		next = STAILQ_FIRST(&td0->td_umtxq);
		if (suword(&umtx->u_blocked, (long)next) == -1) {
			error = EFAULT;
			goto out;
		}
		STAILQ_REMOVE_HEAD(&td0->td_umtxq, td_umtx);

		/*
		 * Switch the queue over to the next blocked thread.
		 */
		if (!STAILQ_EMPTY(&td0->td_umtxq)) {
			next->td_umtxq = td0->td_umtxq;
			STAILQ_INIT(&td0->td_umtxq);
		} else
			STAILQ_INIT(&next->td_umtxq);
	} else {
		if (suword(&umtx->u_blocked, UMTX_UNOWNED) == -1) {
			error = EFAULT;
			goto out;
		}
	}
	/*
	 * Now directly assign this mutex to the first thread that was
	 * blocked on it.
	 */
	old = casuptr((intptr_t *)&umtx->u_owner, owner, blocked);

	/*
	 * This will only happen if someone modifies the lock without going
	 * through this api.
	 */
	if (old != owner) {
		error = EINVAL;
		goto out;
	}
	if (old == -1) {
		error = EFAULT;
		goto out;
	}
	/* Success. */
	error = 0;
	wakeup(&td0->td_umtx);

out:
	PROC_UNLOCK(td->td_proc);

	return (error);
}
OpenPOWER on IntegriCloud