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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
From 5ba0e8a943582df2fdb7103dd2fa8174d83e9edf Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Sat, 23 Nov 2013 16:12:55 +0100
Subject: [PATCH hurd 26/29] libports: use protected payloads to optimize the
object lookup
* libports/create-internal.c (_ports_create_port_internal): Set the
protected payload to the objects address.
* libports/import-port.c (ports_import_port): Likewise.
* libports/reallocate-from-external.c (ports_reallocate_from_external):
Likewise.
* libports/reallocate-port.c (ports_reallocate_port): Likewise.
* libports/transfer-right.c (ports_transfer_right): Likewise.
* libports/manage-multithread.c (ports_manage_port_operations_multithread):
Use the protected payload as the objects address if provided.
* libports/manage-one-thread.c (ports_manage_port_operations_one_thread):
Likewise.
---
libports/complete-deallocate.c | 2 +-
libports/create-internal.c | 6 +-
libports/destroy-right.c | 146 +++++++++++++++++++++++++++++++++++-
libports/import-port.c | 6 +-
libports/manage-multithread.c | 22 +++++-
libports/manage-one-thread.c | 22 +++++-
libports/reallocate-from-external.c | 4 +
libports/reallocate-port.c | 4 +
libports/transfer-right.c | 3 +
9 files changed, 206 insertions(+), 9 deletions(-)
diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c
index 0d852f5..6799dfd 100644
--- a/libports/complete-deallocate.c
+++ b/libports/complete-deallocate.c
@@ -27,7 +27,7 @@ _ports_complete_deallocate (struct port_info *pi)
{
assert ((pi->flags & PORT_HAS_SENDRIGHTS) == 0);
- if (pi->port_right)
+ if (MACH_PORT_VALID (pi->port_right))
{
struct references result;
diff --git a/libports/create-internal.c b/libports/create-internal.c
index 2d85931..d79dc78 100644
--- a/libports/create-internal.c
+++ b/libports/create-internal.c
@@ -99,7 +99,11 @@ _ports_create_port_internal (struct port_class *class,
bucket->count++;
class->count++;
pthread_mutex_unlock (&_ports_lock);
-
+
+ /* This is an optimization. It may fail. */
+ mach_port_set_protected_payload (mach_task_self (), port,
+ (unsigned long) pi);
+
if (install)
{
err = mach_port_move_member (mach_task_self (), pi->port_right,
diff --git a/libports/destroy-right.c b/libports/destroy-right.c
index 448b379..9eb29eb 100644
--- a/libports/destroy-right.c
+++ b/libports/destroy-right.c
@@ -1,5 +1,5 @@
/*
- Copyright (C) 1995, 1996, 1999 Free Software Foundation, Inc.
+ Copyright (C) 1995, 1996, 1999, 2014 Free Software Foundation, Inc.
Written by Michael I. Bushnell.
This file is part of the GNU Hurd.
@@ -22,30 +22,168 @@
#include <hurd/ihash.h>
#include <assert.h>
+#include <pthread.h>
+#include <error.h>
+#include <time.h>
+
+/* To prevent protected payloads from becoming stale, we defer the
+ derefercing of port_info objects. */
+static error_t defer_dereferencing (struct port_info *pi);
+
error_t
ports_destroy_right (void *portstruct)
{
struct port_info *pi = portstruct;
error_t err;
+ mach_port_clear_protected_payload (mach_task_self (),
+ pi->port_right);
+
+ pthread_mutex_lock (&_ports_lock);
if (pi->port_right != MACH_PORT_NULL)
{
pthread_rwlock_wrlock (&_ports_htable_lock);
hurd_ihash_locp_remove (&_ports_htable, pi->ports_htable_entry);
hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry);
pthread_rwlock_unlock (&_ports_htable_lock);
+
err = mach_port_mod_refs (mach_task_self (), pi->port_right,
MACH_PORT_RIGHT_RECEIVE, -1);
assert_perror (err);
- pi->port_right = MACH_PORT_NULL;
-
if (pi->flags & PORT_HAS_SENDRIGHTS)
{
pi->flags &= ~PORT_HAS_SENDRIGHTS;
- ports_port_deref (pi);
+
+ /* There are outstanding send rights, so we might get a
+ no-senders notification. Attached to the notification
+ is a reference to the port_info object. Of course we
+ destroyed the receive right these were send to above, but
+ the message could already have been send and dequeued.
+
+ Previously, those messages would have carried an stale
+ name, which would have caused a hash table lookup
+ failure. However, stale payloads results in port_info
+ use-after-free. Therefore, we cannot release the
+ reference here, but defer that instead. */
+ defer_dereferencing (pi);
}
+
+ pi->port_right = MACH_PORT_DEAD;
}
+ pthread_mutex_unlock (&_ports_lock);
+
+ return 0;
+}
+
+/* Simple lock-less generational garbage collection. */
+
+/* We maintain three lists of objects. Producers add objects to the
+ current generation G using defer_dereferencing. G-1 holds old
+ objects, G-2 holds garbage. */
+static struct deferred_dereference
+{
+ struct deferred_dereference *next;
+ struct port_info *pi; /* We hold a reference for these objects. */
+} *generations[3]; /* Must be accessed using atomic
+ operations. */
+
+/* The current generation. Must be accessed using atomic operations. */
+static int generation;
+
+/* The garbage collection thread. Does not return. */
+static void *
+gc_loop (void *arg)
+{
+ while (1)
+ {
+ int old, garbage;
+ struct deferred_dereference *d;
+
+ sleep (5);
+
+ /* We are the only one updating generation, so this is safe. */
+ old = generation;
+
+ /* Update generation. */
+ __atomic_store_n (&generation, (old + 1) % 3, __ATOMIC_RELAXED);
+
+ /* This is the garbage generation. As all writers are long
+ gone, we do not need to bother with atomic operations. */
+ garbage = (old + 2) % 3;
+ d = generations[garbage];
+ generations[garbage] = NULL;
+ while (d != NULL)
+ {
+ struct deferred_dereference *next = d->next;
+ struct references refs;
+
+ refcounts_references (&d->pi->refcounts, &refs);
+
+ // XXX: Need to think more about this.
+ assert (refs.hard <= 2 || !"reference to destroyed right leaked");
+
+ if (refs.hard == 2)
+ {
+ /* XXX: does this actually happen? if so, why?? */
+ /* Get rid of the hash table reference. */
+ ports_port_deref (d->pi);
+ }
+
+ /* Get rid of our reference. */
+ ports_port_deref (d->pi);
+
+ free (d);
+ d = next;
+ }
+ }
+
+ assert (! "reached");
+ return NULL;
+}
+
+/* Start the gc thread. */
+static void
+start_gc (void)
+{
+ error_t err;
+ pthread_attr_t attr;
+ pthread_t thread;
+
+ pthread_attr_init (&attr);
+#define STACK_SIZE (64 * 1024)
+ pthread_attr_setstacksize (&attr, STACK_SIZE);
+#undef STACK_SIZE
+
+ err = pthread_create (&thread, &attr, gc_loop, NULL);
+ assert_perror (err);
+ err = pthread_detach (thread);
+ assert_perror (err);
+}
+
+/* Defer the derefercing of port_info objects. */
+static error_t
+defer_dereferencing (struct port_info *pi)
+{
+ static pthread_once_t once = PTHREAD_ONCE_INIT;
+ int g;
+ struct deferred_dereference *d = malloc (sizeof *d);
+ if (d == NULL)
+ return ENOMEM;
+
+ pthread_once (&once, start_gc);
+
+ ports_port_ref (pi);
+ d->pi = pi;
+
+ retry:
+ /* Append to the current generation. */
+ g = __atomic_load_n (&generation, __ATOMIC_RELAXED);
+
+ d->next = __atomic_load_n (&generations[g], __ATOMIC_RELAXED);
+ if (! __atomic_compare_exchange_n (&generations[g], &d->next, d,
+ 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
+ goto retry;
return 0;
}
diff --git a/libports/import-port.c b/libports/import-port.c
index c337c85..2c638e1 100644
--- a/libports/import-port.c
+++ b/libports/import-port.c
@@ -93,7 +93,11 @@ ports_import_port (struct port_class *class, struct port_bucket *bucket,
bucket->count++;
class->count++;
pthread_mutex_unlock (&_ports_lock);
-
+
+ /* This is an optimization. It may fail. */
+ mach_port_set_protected_payload (mach_task_self (), port,
+ (unsigned long) pi);
+
mach_port_move_member (mach_task_self (), port, bucket->portset);
if (stat.mps_srights)
diff --git a/libports/manage-multithread.c b/libports/manage-multithread.c
index 2067cba..90b3044 100644
--- a/libports/manage-multithread.c
+++ b/libports/manage-multithread.c
@@ -162,7 +162,27 @@ ports_manage_port_operations_multithread (struct port_bucket *bucket,
outp->RetCodeType = RetCodeType;
outp->RetCode = MIG_BAD_ID;
- pi = ports_lookup_port (bucket, inp->msgh_local_port, 0);
+ if (MACH_MSGH_BITS_LOCAL (inp->msgh_bits) ==
+ MACH_MSG_TYPE_PROTECTED_PAYLOAD)
+ {
+ pi = (struct port_info *) inp->msgh_protected_payload;
+ if (pi && pi->bucket == bucket)
+ ports_port_ref (pi);
+ else
+ pi = NULL;
+ }
+ else
+ {
+ pi = ports_lookup_port (bucket, inp->msgh_local_port, 0);
+ if (pi)
+ {
+ inp->msgh_bits = MACH_MSGH_BITS (
+ MACH_MSGH_BITS_REMOTE (inp->msgh_bits),
+ MACH_MSG_TYPE_PROTECTED_PAYLOAD);
+ inp->msgh_protected_payload = (unsigned long) pi;
+ }
+ }
+
if (pi)
{
error_t err = ports_begin_rpc (pi, inp->msgh_id, &link);
diff --git a/libports/manage-one-thread.c b/libports/manage-one-thread.c
index cbd2df7..58c0f36 100644
--- a/libports/manage-one-thread.c
+++ b/libports/manage-one-thread.c
@@ -57,7 +57,27 @@ ports_manage_port_operations_one_thread (struct port_bucket *bucket,
outp->RetCodeType = RetCodeType;
outp->RetCode = MIG_BAD_ID;
- pi = ports_lookup_port (bucket, inp->msgh_local_port, 0);
+ if (MACH_MSGH_BITS_LOCAL (inp->msgh_bits) ==
+ MACH_MSG_TYPE_PROTECTED_PAYLOAD)
+ {
+ pi = (struct port_info *) inp->msgh_protected_payload;
+ if (pi && pi->bucket == bucket)
+ ports_port_ref (pi);
+ else
+ pi = NULL;
+ }
+ else
+ {
+ pi = ports_lookup_port (bucket, inp->msgh_local_port, 0);
+ if (pi)
+ {
+ inp->msgh_bits = MACH_MSGH_BITS (
+ MACH_MSGH_BITS_REMOTE (inp->msgh_bits),
+ MACH_MSG_TYPE_PROTECTED_PAYLOAD);
+ inp->msgh_protected_payload = (unsigned long) pi;
+ }
+ }
+
if (pi)
{
err = ports_begin_rpc (pi, inp->msgh_id, &link);
diff --git a/libports/reallocate-from-external.c b/libports/reallocate-from-external.c
index 7205bd9..d0fae1a 100644
--- a/libports/reallocate-from-external.c
+++ b/libports/reallocate-from-external.c
@@ -71,6 +71,10 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive)
pthread_mutex_unlock (&_ports_lock);
assert_perror (err);
+ /* This is an optimization. It may fail. */
+ mach_port_set_protected_payload (mach_task_self (), pi->port_right,
+ (unsigned long) pi);
+
mach_port_move_member (mach_task_self (), receive, pi->bucket->portset);
if (stat.mps_srights)
diff --git a/libports/reallocate-port.c b/libports/reallocate-port.c
index cc534eb..4e859a1 100644
--- a/libports/reallocate-port.c
+++ b/libports/reallocate-port.c
@@ -59,6 +59,10 @@ ports_reallocate_port (void *portstruct)
pthread_mutex_unlock (&_ports_lock);
assert_perror (err);
+ /* This is an optimization. It may fail. */
+ mach_port_set_protected_payload (mach_task_self (), pi->port_right,
+ (unsigned long) pi);
+
err = mach_port_move_member (mach_task_self (), pi->port_right,
pi->bucket->portset);
assert_perror (err);
diff --git a/libports/transfer-right.c b/libports/transfer-right.c
index 776a8d2..64de7f7 100644
--- a/libports/transfer-right.c
+++ b/libports/transfer-right.c
@@ -91,6 +91,9 @@ ports_transfer_right (void *tostruct,
err = hurd_ihash_add (&topi->bucket->htable, port, topi);
pthread_rwlock_unlock (&_ports_htable_lock);
assert_perror (err);
+ /* This is an optimization. It may fail. */
+ mach_port_set_protected_payload (mach_task_self (), port,
+ (unsigned long) topi);
if (topi->bucket != frompi->bucket)
{
err = mach_port_move_member (mach_task_self (), port,
--
2.1.3
|