diff options
author | Justus Winter <4winter@informatik.uni-hamburg.de> | 2014-05-07 14:38:34 +0200 |
---|---|---|
committer | Justus Winter <4winter@informatik.uni-hamburg.de> | 2014-05-07 14:38:34 +0200 |
commit | 95bfd7c8d8811c6cd183aa3baaa11dacd60dc5af (patch) | |
tree | 07f106f6b16f9cbd5bf22f0693330a2465526975 /debian | |
parent | aaf2362e4d9a6ac0a02004aa85a7e535d77ed371 (diff) |
new libports patch series
Diffstat (limited to 'debian')
6 files changed, 1464 insertions, 0 deletions
diff --git a/debian/patches/0001-libports-use-a-single-hash-table.patch b/debian/patches/0001-libports-use-a-single-hash-table.patch new file mode 100644 index 00000000..492a4cab --- /dev/null +++ b/debian/patches/0001-libports-use-a-single-hash-table.patch @@ -0,0 +1,619 @@ +From 27741dca90e3a6070727fffc22d3105653a33747 Mon Sep 17 00:00:00 2001 +From: Justus Winter <4winter@informatik.uni-hamburg.de> +Date: Sat, 3 May 2014 03:53:41 +0200 +Subject: [PATCH 1/5] libports: use a single hash table + +Previously, libports used a hash table per port bucket. This makes +looking up a port difficult if one does not know the port bucket, as +one has to iterate over all buckets and do a hash table lookup each. + +The having to iterate over the buckets makes it necessary to keep a +list of all buckets, which has to be updated and protected by a lock +as well. + +Also, the current code in _ports_bucket_class_iterate iterates over +the hash table associated with the bucket given. When +ports_class_iterate calls this common function, it obtains a reference +to the bucket from one of the ports in the given class. This will not +work if a class contains ports in different port buckets. This +limitation is not documented as far as I can see. Again, having to +maintain this list has its cost and requires serialization. + +Use a single hash table instead. Furthermore, serialize access to it +using a separate lock. Remove the linked lists of all buckets and all +ports in a class. + +* libports/bucket-iterate.c (ports_bucket_iterate): Acquire +_ports_htable_lock. Also, generalize ports_bucket_iterate to treat +the bucket argument the same way as the class argument. +* libports/class-iterate.c (ports_class_iterate): Just call the +generalized _ports_bucket_class_iterate with NULL as class. +* libports/ports.h (struct port_info): Remove the port class links. +(struct port_bucket): Remove the hash table, and the all buckets link. +(_ports_all_buckets): Remove declaration. +(_ports_htable): New global hash table. +(_ports_htable_lock): Protected by this lock. +* libports/claim-right.c: Adjust accordingly. +* libports/complete-deallocate.c: Likewise. +* libports/create-bucket.c: Likewise. +* libports/create-class.c: Likewise. +* libports/create-internal.c: Likewise. +* libports/destroy-right.c: Likewise. +* libports/import-port.c: Likewise. +* libports/lookup-port.c: Likewise. +* libports/reallocate-from-external.c: Likewise. +* libports/reallocate-port.c: Likewise. +* libports/transfer-right.c: Likewise. +* libports/inhibit-all-rpcs.c: Iterate over the hash table. +* libports/inhibit-bucket-rpcs.c: Likewise, but filter using bucket. +* libports/inhibit-class-rpcs.c: Likewise, but filter using class. +* libports/init.c (_ports_htable): Initialize. +(_ports_htable_lock): Likewise. +--- + libports/bucket-iterate.c | 21 +++++++++++++++------ + libports/claim-right.c | 6 ++++-- + libports/class-iterate.c | 10 +--------- + libports/complete-deallocate.c | 8 +++----- + libports/create-bucket.c | 7 ------- + libports/create-class.c | 1 - + libports/create-internal.c | 9 +++------ + libports/destroy-right.c | 6 +++--- + libports/import-port.c | 9 +++------ + libports/inhibit-all-rpcs.c | 27 +++++++++++++-------------- + libports/inhibit-bucket-rpcs.c | 7 +++++-- + libports/inhibit-class-rpcs.c | 27 ++++++++++++++++++--------- + libports/init.c | 7 ++++++- + libports/lookup-port.c | 21 ++++++++------------- + libports/ports.h | 11 ++++++----- + libports/reallocate-from-external.c | 16 ++++++++++------ + libports/reallocate-port.c | 10 +++++++--- + libports/transfer-right.c | 20 +++++++++++++------- + 18 files changed, 118 insertions(+), 105 deletions(-) + +diff --git a/libports/bucket-iterate.c b/libports/bucket-iterate.c +index babc204..d5f590e 100644 +--- a/libports/bucket-iterate.c ++++ b/libports/bucket-iterate.c +@@ -36,35 +36,44 @@ _ports_bucket_class_iterate (struct port_bucket *bucket, + error_t err; + + pthread_mutex_lock (&_ports_lock); ++ pthread_mutex_lock (&_ports_htable_lock); + +- if (bucket->htable.nr_items == 0) ++ if (_ports_htable.nr_items == 0) + { +- pthread_mutex_unlock (&_ports_lock); ++ pthread_mutex_unlock (&_ports_htable_lock); + return 0; + } + +- nr_items = bucket->htable.nr_items; ++ nr_items = _ports_htable.nr_items; + p = malloc (nr_items * sizeof *p); + if (p == NULL) + { +- pthread_mutex_unlock (&_ports_lock); ++ pthread_mutex_unlock (&_ports_htable_lock); + return ENOMEM; + } + + n = 0; +- HURD_IHASH_ITERATE (&bucket->htable, arg) ++ HURD_IHASH_ITERATE (&_ports_htable, arg) + { + struct port_info *const pi = arg; + +- if (class == 0 || pi->class == class) ++ if ((bucket == NULL || pi->bucket == bucket) ++ && (class == NULL || pi->class == class)) + { + pi->refcnt++; + p[n] = pi; + n++; + } + } ++ pthread_mutex_unlock (&_ports_htable_lock); + pthread_mutex_unlock (&_ports_lock); + ++ if (n == 0) ++ { ++ free (p); ++ return 0; ++ } ++ + if (n != nr_items) + { + /* We allocated too much. Release unused memory. */ +diff --git a/libports/claim-right.c b/libports/claim-right.c +index 4851ea3..f453a82 100644 +--- a/libports/claim-right.c ++++ b/libports/claim-right.c +@@ -34,10 +34,12 @@ ports_claim_right (void *portstruct) + if (ret == MACH_PORT_NULL) + return ret; + +- pthread_mutex_lock (&_ports_lock); +- hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, pi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + err = mach_port_move_member (mach_task_self (), ret, MACH_PORT_NULL); + assert_perror (err); ++ pthread_mutex_lock (&_ports_lock); + pi->port_right = MACH_PORT_NULL; + if (pi->flags & PORT_HAS_SENDRIGHTS) + { +diff --git a/libports/class-iterate.c b/libports/class-iterate.c +index 1f8878a..5cfcfc8 100644 +--- a/libports/class-iterate.c ++++ b/libports/class-iterate.c +@@ -23,13 +23,5 @@ error_t + ports_class_iterate (struct port_class *class, + error_t (*fun)(void *)) + { +- pthread_mutex_lock (&_ports_lock); +- if (class->ports != 0) +- { +- struct port_bucket *bucket = class->ports->bucket; +- pthread_mutex_unlock (&_ports_lock); +- return _ports_bucket_class_iterate (bucket, class, fun); +- } +- pthread_mutex_unlock (&_ports_lock); +- return 0; ++ return _ports_bucket_class_iterate (NULL, class, fun); + } +diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c +index 8ce095b..1371d92 100644 +--- a/libports/complete-deallocate.c ++++ b/libports/complete-deallocate.c +@@ -29,16 +29,14 @@ _ports_complete_deallocate (struct port_info *pi) + + if (pi->port_right) + { +- hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, pi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + mach_port_mod_refs (mach_task_self (), pi->port_right, + MACH_PORT_RIGHT_RECEIVE, -1); + pi->port_right = MACH_PORT_NULL; + } + +- *pi->prevp = pi->next; +- if (pi->next) +- pi->next->prevp = pi->prevp; +- + pi->bucket->count--; + pi->class->count--; + +diff --git a/libports/create-bucket.c b/libports/create-bucket.c +index 52d50c3..9f6821d 100644 +--- a/libports/create-bucket.c ++++ b/libports/create-bucket.c +@@ -46,13 +46,6 @@ ports_create_bucket () + return NULL; + } + +- hurd_ihash_init (&ret->htable, offsetof (struct port_info, hentry)); + ret->rpcs = ret->flags = ret->count = 0; +- +- pthread_mutex_lock (&_ports_lock); +- ret->next = _ports_all_buckets; +- _ports_all_buckets = ret; +- pthread_mutex_unlock (&_ports_lock); +- + return ret; + } +diff --git a/libports/create-class.c b/libports/create-class.c +index 12c8add..782f52b 100644 +--- a/libports/create-class.c ++++ b/libports/create-class.c +@@ -39,7 +39,6 @@ ports_create_class (void (*clean_routine)(void *), + cl->dropweak_routine = dropweak_routine; + cl->flags = 0; + cl->rpcs = 0; +- cl->ports = NULL; + cl->count = 0; + cl->uninhibitable_rpcs = ports_default_uninhibitable_rpcs; + +diff --git a/libports/create-internal.c b/libports/create-internal.c +index 8551297..e773dd6 100644 +--- a/libports/create-internal.c ++++ b/libports/create-internal.c +@@ -81,15 +81,12 @@ _ports_create_port_internal (struct port_class *class, + goto loop; + } + +- err = hurd_ihash_add (&bucket->htable, port, pi); ++ pthread_mutex_lock (&_ports_htable_lock); ++ err = hurd_ihash_add (&_ports_htable, port, pi); ++ pthread_mutex_unlock (&_ports_htable_lock); + if (err) + goto lose; + +- pi->next = class->ports; +- pi->prevp = &class->ports; +- if (class->ports) +- class->ports->prevp = &pi->next; +- class->ports = pi; + bucket->count++; + class->count++; + pthread_mutex_unlock (&_ports_lock); +diff --git a/libports/destroy-right.c b/libports/destroy-right.c +index 65e19c7..6ba7302 100644 +--- a/libports/destroy-right.c ++++ b/libports/destroy-right.c +@@ -30,12 +30,12 @@ ports_destroy_right (void *portstruct) + + if (pi->port_right != MACH_PORT_NULL) + { +- pthread_mutex_lock (&_ports_lock); +- hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, pi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + err = mach_port_mod_refs (mach_task_self (), pi->port_right, + MACH_PORT_RIGHT_RECEIVE, -1); + assert_perror (err); +- pthread_mutex_unlock (&_ports_lock); + + pi->port_right = MACH_PORT_NULL; + +diff --git a/libports/import-port.c b/libports/import-port.c +index 226f47e..d0b2ea4 100644 +--- a/libports/import-port.c ++++ b/libports/import-port.c +@@ -75,15 +75,12 @@ ports_import_port (struct port_class *class, struct port_bucket *bucket, + goto loop; + } + +- err = hurd_ihash_add (&bucket->htable, port, pi); ++ pthread_mutex_lock (&_ports_htable_lock); ++ err = hurd_ihash_add (&_ports_htable, port, pi); ++ pthread_mutex_unlock (&_ports_htable_lock); + if (err) + goto lose; + +- pi->next = class->ports; +- pi->prevp = &class->ports; +- if (class->ports) +- class->ports->prevp = &pi->next; +- class->ports = pi; + bucket->count++; + class->count++; + pthread_mutex_unlock (&_ports_lock); +diff --git a/libports/inhibit-all-rpcs.c b/libports/inhibit-all-rpcs.c +index d4a54ba..83c291f 100644 +--- a/libports/inhibit-all-rpcs.c ++++ b/libports/inhibit-all-rpcs.c +@@ -36,24 +36,23 @@ ports_inhibit_all_rpcs () + struct port_bucket *bucket; + int this_one = 0; + +- for (bucket = _ports_all_buckets; bucket; bucket = bucket->next) ++ pthread_mutex_lock (&_ports_htable_lock); ++ HURD_IHASH_ITERATE (&_ports_htable, portstruct) + { +- HURD_IHASH_ITERATE (&bucket->htable, portstruct) ++ struct rpc_info *rpc; ++ struct port_info *pi = portstruct; ++ ++ for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) + { +- struct rpc_info *rpc; +- struct port_info *pi = portstruct; +- +- for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) +- { +- /* Avoid cancelling the calling thread if it's currently +- handling a RPC. */ +- if (rpc->thread == hurd_thread_self ()) +- this_one = 1; +- else +- hurd_thread_cancel (rpc->thread); +- } ++ /* Avoid cancelling the calling thread if it's currently ++ handling a RPC. */ ++ if (rpc->thread == hurd_thread_self ()) ++ this_one = 1; ++ else ++ hurd_thread_cancel (rpc->thread); + } + } ++ pthread_mutex_unlock (&_ports_htable_lock); + + while (_ports_total_rpcs > this_one) + { +diff --git a/libports/inhibit-bucket-rpcs.c b/libports/inhibit-bucket-rpcs.c +index 965aa03..ed3e29d 100644 +--- a/libports/inhibit-bucket-rpcs.c ++++ b/libports/inhibit-bucket-rpcs.c +@@ -35,10 +35,13 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket) + { + int this_one = 0; + +- HURD_IHASH_ITERATE (&bucket->htable, portstruct) ++ pthread_mutex_lock (&_ports_htable_lock); ++ HURD_IHASH_ITERATE (&_ports_htable, portstruct) + { + struct rpc_info *rpc; + struct port_info *pi = portstruct; ++ if (pi->bucket != bucket) ++ continue; + + for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) + { +@@ -49,7 +52,7 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket) + hurd_thread_cancel (rpc->thread); + } + } +- ++ pthread_mutex_unlock (&_ports_htable_lock); + + while (bucket->rpcs > this_one) + { +diff --git a/libports/inhibit-class-rpcs.c b/libports/inhibit-class-rpcs.c +index 7ee8653..1580bdb 100644 +--- a/libports/inhibit-class-rpcs.c ++++ b/libports/inhibit-class-rpcs.c +@@ -36,15 +36,24 @@ ports_inhibit_class_rpcs (struct port_class *class) + struct rpc_info *rpc; + int this_one = 0; + +- for (pi = class->ports; pi; pi = pi->next) +- for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) +- { +- /* Avoid cancelling the calling thread. */ +- if (rpc->thread == hurd_thread_self ()) +- this_one = 1; +- else +- hurd_thread_cancel (rpc->thread); +- } ++ pthread_mutex_lock (&_ports_htable_lock); ++ HURD_IHASH_ITERATE (&_ports_htable, portstruct) ++ { ++ struct rpc_info *rpc; ++ struct port_info *pi = portstruct; ++ if (pi->class != class) ++ continue; ++ ++ for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) ++ { ++ /* Avoid cancelling the calling thread. */ ++ if (rpc->thread == hurd_thread_self ()) ++ this_one = 1; ++ else ++ hurd_thread_cancel (rpc->thread); ++ } ++ } ++ pthread_mutex_unlock (&_ports_htable_lock); + + while (class->rpcs > this_one) + { +diff --git a/libports/init.c b/libports/init.c +index 3ef5388..8fbc7e8 100644 +--- a/libports/init.c ++++ b/libports/init.c +@@ -19,9 +19,14 @@ + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + + #include "ports.h" ++#include <stddef.h> + + pthread_mutex_t _ports_lock = PTHREAD_MUTEX_INITIALIZER; + pthread_cond_t _ports_block = PTHREAD_COND_INITIALIZER; +-struct port_bucket *_ports_all_buckets; ++ ++struct hurd_ihash _ports_htable = ++ HURD_IHASH_INITIALIZER (offsetof (struct port_info, hentry)); ++pthread_mutex_t _ports_htable_lock = PTHREAD_MUTEX_INITIALIZER; ++ + int _ports_total_rpcs; + int _ports_flags; +diff --git a/libports/lookup-port.c b/libports/lookup-port.c +index f79f6f0..fbb13ef 100644 +--- a/libports/lookup-port.c ++++ b/libports/lookup-port.c +@@ -26,26 +26,21 @@ ports_lookup_port (struct port_bucket *bucket, + mach_port_t port, + struct port_class *class) + { +- struct port_info *pi = 0; +- ++ struct port_info *pi; ++ + pthread_mutex_lock (&_ports_lock); ++ pthread_mutex_lock (&_ports_htable_lock); + +- if (bucket) +- pi = hurd_ihash_find (&bucket->htable, port); +- else +- for (bucket = _ports_all_buckets; bucket; bucket = bucket->next) +- { +- pi = hurd_ihash_find (&bucket->htable, port); +- if (pi) +- break; +- } +- +- if (pi && class && pi->class != class) ++ pi = hurd_ihash_find (&_ports_htable, port); ++ if (pi ++ && ((class && pi->class != class) ++ || (bucket && pi->bucket != bucket))) + pi = 0; + + if (pi) + pi->refcnt++; + ++ pthread_mutex_unlock (&_ports_htable_lock); + pthread_mutex_unlock (&_ports_lock); + + return pi; +diff --git a/libports/ports.h b/libports/ports.h +index 7f13124..9c5f14d 100644 +--- a/libports/ports.h ++++ b/libports/ports.h +@@ -48,7 +48,6 @@ struct port_info + struct rpc_info *current_rpcs; + struct port_bucket *bucket; + hurd_ihash_locp_t hentry; +- struct port_info *next, **prevp; /* links on port_class list */ + }; + typedef struct port_info *port_info_t; + +@@ -61,11 +60,9 @@ typedef struct port_info *port_info_t; + struct port_bucket + { + mach_port_t portset; +- struct hurd_ihash htable; + int rpcs; + int flags; + int count; +- struct port_bucket *next; + }; + /* FLAGS above are the following: */ + #define PORT_BUCKET_INHIBITED PORTS_INHIBITED +@@ -78,7 +75,6 @@ struct port_class + { + int flags; + int rpcs; +- struct port_info *ports; + int count; + void (*clean_routine) (void *); + void (*dropweak_routine) (void *); +@@ -402,7 +398,12 @@ extern kern_return_t + /* Private data */ + extern pthread_mutex_t _ports_lock; + extern pthread_cond_t _ports_block; +-extern struct port_bucket *_ports_all_buckets; ++ ++/* A hash table mapping port names to port_info objects. */ ++extern struct hurd_ihash _ports_htable; ++/* Access to the hash table is protected by this lock. */ ++extern pthread_mutex_t _ports_htable_lock; ++ + extern int _ports_total_rpcs; + extern int _ports_flags; + #define _PORTS_INHIBITED PORTS_INHIBITED +diff --git a/libports/reallocate-from-external.c b/libports/reallocate-from-external.c +index 8cccb2a..bbcf9ba 100644 +--- a/libports/reallocate-from-external.c ++++ b/libports/reallocate-from-external.c +@@ -43,8 +43,10 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive) + MACH_PORT_RIGHT_RECEIVE, -1); + assert_perror (err); + +- hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); +- ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, pi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); ++ + if ((pi->flags & PORT_HAS_SENDRIGHTS) && !stat.mps_srights) + { + dropref = 1; +@@ -59,11 +61,13 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive) + pi->port_right = receive; + pi->cancel_threshold = 0; + pi->mscount = stat.mps_mscount; +- +- err = hurd_ihash_add (&pi->bucket->htable, receive, pi); +- assert_perror (err); ++ ++ pthread_mutex_lock (&_ports_htable_lock); ++ err = hurd_ihash_add (&_ports_htable, receive, pi); ++ pthread_mutex_unlock (&_ports_htable_lock); + pthread_mutex_unlock (&_ports_lock); +- ++ assert_perror (err); ++ + 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 d2adaeb..99c4041 100644 +--- a/libports/reallocate-port.c ++++ b/libports/reallocate-port.c +@@ -36,7 +36,9 @@ ports_reallocate_port (void *portstruct) + MACH_PORT_RIGHT_RECEIVE, -1); + assert_perror (err); + +- hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, pi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + + err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, + &pi->port_right); +@@ -48,9 +50,11 @@ ports_reallocate_port (void *portstruct) + } + pi->cancel_threshold = 0; + pi->mscount = 0; +- err = hurd_ihash_add (&pi->bucket->htable, pi->port_right, pi); +- assert_perror (err); ++ pthread_mutex_lock (&_ports_htable_lock); ++ err = hurd_ihash_add (&_ports_htable, pi->port_right, pi); ++ pthread_mutex_unlock (&_ports_htable_lock); + pthread_mutex_unlock (&_ports_lock); ++ assert_perror (err); + + err = mach_port_move_member (mach_task_self (), pi->port_right, + pi->bucket->portset); +diff --git a/libports/transfer-right.c b/libports/transfer-right.c +index 72488a9..5a7653d 100644 +--- a/libports/transfer-right.c ++++ b/libports/transfer-right.c +@@ -41,7 +41,9 @@ ports_transfer_right (void *tostruct, + port = frompi->port_right; + if (port != MACH_PORT_NULL) + { +- hurd_ihash_locp_remove (&frompi->bucket->htable, frompi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, frompi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + frompi->port_right = MACH_PORT_NULL; + if (frompi->flags & PORT_HAS_SENDRIGHTS) + { +@@ -54,7 +56,9 @@ ports_transfer_right (void *tostruct, + /* Destroy the existing right in TOPI. */ + if (topi->port_right != MACH_PORT_NULL) + { +- hurd_ihash_locp_remove (&topi->bucket->htable, topi->hentry); ++ pthread_mutex_lock (&_ports_htable_lock); ++ hurd_ihash_locp_remove (&_ports_htable, topi->hentry); ++ pthread_mutex_unlock (&_ports_htable_lock); + err = mach_port_mod_refs (mach_task_self (), topi->port_right, + MACH_PORT_RIGHT_RECEIVE, -1); + assert_perror (err); +@@ -74,10 +78,14 @@ ports_transfer_right (void *tostruct, + topi->port_right = port; + topi->cancel_threshold = frompi->cancel_threshold; + topi->mscount = frompi->mscount; +- ++ ++ pthread_mutex_unlock (&_ports_lock); ++ + if (port) + { +- err = hurd_ihash_add (&topi->bucket->htable, port, topi); ++ pthread_mutex_lock (&_ports_htable_lock); ++ err = hurd_ihash_add (&_ports_htable, port, topi); ++ pthread_mutex_unlock (&_ports_htable_lock); + assert_perror (err); + if (topi->bucket != frompi->bucket) + { +@@ -86,9 +94,7 @@ ports_transfer_right (void *tostruct, + assert_perror (err); + } + } +- +- pthread_mutex_unlock (&_ports_lock); +- ++ + /* Take care of any lowered reference counts. */ + if (dereffrompi) + ports_port_deref (frompi); +-- +2.0.0.rc0 + diff --git a/debian/patches/0002-include-add-lock-less-reference-counting-primitives.patch b/debian/patches/0002-include-add-lock-less-reference-counting-primitives.patch new file mode 100644 index 00000000..145d340e --- /dev/null +++ b/debian/patches/0002-include-add-lock-less-reference-counting-primitives.patch @@ -0,0 +1,175 @@ +From 8faea20349dc1d8cfd0117d28a3f1271083192d0 Mon Sep 17 00:00:00 2001 +From: Justus Winter <4winter@informatik.uni-hamburg.de> +Date: Tue, 6 May 2014 19:52:04 +0200 +Subject: [PATCH 2/5] include: add lock-less reference counting primitives + +* include/refcount.h: New file. +--- + include/refcount.h | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++ + 1 file changed, 155 insertions(+) + create mode 100644 include/refcount.h + +diff --git a/include/refcount.h b/include/refcount.h +new file mode 100644 +index 0000000..946938a +--- /dev/null ++++ b/include/refcount.h +@@ -0,0 +1,155 @@ ++/* Lock-less reference counting primitives ++ ++ Copyright (C) 2014 Free Software Foundation, Inc. ++ ++ Written by Justus Winter <4winter@informatik.uni-hamburg.de> ++ ++ This file is part of the GNU Hurd. ++ ++ The GNU Hurd is free software; you can redistribute it and/or ++ modify it under the terms of the GNU General Public License as ++ published by the Free Software Foundation; either version 2, or (at ++ your option) any later version. ++ ++ The GNU Hurd is distributed in the hope that it will be useful, but ++ WITHOUT ANY WARRANTY; without even the implied warranty of ++ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ++ General Public License for more details. ++ ++ You should have received a copy of the GNU General Public License ++ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */ ++ ++#ifndef _HURD_REFCOUNT_H_ ++#define _HURD_REFCOUNT_H_ ++ ++#include <stdint.h> ++ ++/* Simple reference counting. */ ++ ++/* An opaque type. You must not access these values directly. */ ++typedef uint32_t refcount_t; ++ ++/* Initialize REF with REFERENCES. */ ++static inline void ++refcount_init (refcount_t *ref, uint32_t references) ++{ ++ *ref = references; ++} ++ ++/* Increment REF. Return the result of the operation. */ ++static inline uint32_t ++refcount_ref (refcount_t *ref) ++{ ++ return __atomic_add_fetch (ref, 1, __ATOMIC_RELAXED); ++} ++ ++/* Decrement REF. Return the result of the operation. */ ++static inline uint32_t ++refcount_deref (refcount_t *ref) ++{ ++ return __atomic_sub_fetch (ref, 1, __ATOMIC_RELAXED); ++} ++ ++/* Return REF. */ ++static inline uint32_t ++refcount_references (refcount_t *ref) ++{ ++ return __atomic_load_n (ref, __ATOMIC_RELAXED); ++} ++ ++/* Reference counting with weak references. */ ++ ++/* An opaque type. You must not access these values directly. */ ++typedef uint64_t refcounts_t; ++ ++/* Instead, the functions manipulating refcounts_t values write the ++ results into this kind of objects. */ ++struct references { ++ uint32_t hard; ++ uint32_t weak; ++}; ++ ++union _references { ++ struct references refs; ++ refcounts_t rc; ++}; ++ ++/* Initialize REF with HARD and WEAK references. */ ++static inline void ++refcounts_init (refcounts_t *ref, uint32_t hard, uint32_t weak) ++{ ++ ((union _references *) ref)->refs = ++ (struct references) { .hard = hard, .weak = weak }; ++} ++ ++/* Increment the hard reference count of REF. If RESULT is not NULL, ++ the result of the operation is written there. */ ++static inline void ++refcounts_ref (refcounts_t *ref, struct references *result) ++{ ++ const union _references op = { .refs = { .hard = 1 } }; ++ refcounts_t r = __atomic_add_fetch (ref, op.rc, __ATOMIC_RELAXED); ++ if (result) ++ ((union _references *) result)->rc = r; ++} ++ ++/* Decrement the hard reference count of REF. If RESULT is not NULL, ++ the result of the operation is written there. */ ++static inline void ++refcounts_deref (refcounts_t *ref, struct references *result) ++{ ++ const union _references op = { .refs = { .hard = 1 } }; ++ refcounts_t r = __atomic_sub_fetch (ref, op.rc, __ATOMIC_RELAXED); ++ if (result) ++ ((union _references *) result)->rc = r; ++} ++ ++/* Increment the weak reference count of REF. If RESULT is not NULL, ++ the result of the operation is written there. */ ++static inline void ++refcounts_ref_weak (refcounts_t *ref, struct references *result) ++{ ++ const union _references op = { .refs = { .weak = 1 } }; ++ refcounts_t r = __atomic_add_fetch (ref, op.rc, __ATOMIC_RELAXED); ++ if (result) ++ ((union _references *) result)->rc = r; ++} ++ ++/* Decrement the weak reference count of REF. If RESULT is not NULL, ++ the result of the operation is written there. */ ++static inline void ++refcounts_deref_weak (refcounts_t *ref, struct references *result) ++{ ++ const union _references op = { .refs = { .weak = 1 } }; ++ refcounts_t r = __atomic_sub_fetch (ref, op.rc, __ATOMIC_RELAXED); ++ if (result) ++ ((union _references *) result)->rc = r; ++} ++ ++/* Store the current reference counts of REF in RESULT. */ ++static inline void ++refcounts_references (refcounts_t *ref, struct references *result) ++{ ++ ((union _references *) result)->rc = ++ __atomic_load_n (ref, __ATOMIC_RELAXED); ++} ++ ++/* Return the hard reference count of REF. */ ++static inline uint32_t ++refcounts_hard_references (refcounts_t *ref) ++{ ++ struct references result; ++ refcounts_references (ref, &result); ++ return result.hard; ++} ++ ++/* Return the weak reference count of REF. */ ++static inline uint32_t ++refcounts_weak_references (refcounts_t *ref) ++{ ++ struct references result; ++ refcounts_references (ref, &result); ++ return result.weak; ++} ++ ++#endif /* _HURD_REFCOUNT_H_ */ +-- +2.0.0.rc0 + diff --git a/debian/patches/0003-libports-lock-less-reference-counting-for-port_info-.patch b/debian/patches/0003-libports-lock-less-reference-counting-for-port_info-.patch new file mode 100644 index 00000000..9c5754ed --- /dev/null +++ b/debian/patches/0003-libports-lock-less-reference-counting-for-port_info-.patch @@ -0,0 +1,346 @@ +From 2644982be8c7d742f7d5be02ab9211c72bd356d5 Mon Sep 17 00:00:00 2001 +From: Justus Winter <4winter@informatik.uni-hamburg.de> +Date: Sat, 3 May 2014 01:02:35 +0200 +Subject: [PATCH 3/5] libports: lock-less reference counting for port_info + objects + +* libports/refcount.h: New file with reference counting primitives. +* libports/ports.h (struct port_info): Use the new type. +* libports/lookup-port.c: No need to lock _ports_lock anymore. +* libports/bucket-iterate.c: Likewise. +* libports/complete-deallocate.c: Check if someone reacquired a +reference through a hash table lookup. +* libports/create-internal.c: Use the new reference counting primitives. +* libports/get-right.c: Likewise. +* libports/import-port.c: Likewise. +* libports/port-deref-weak.c: Likewise. +* libports/port-deref.c: Likewise. +* libports/port-ref-weak.c: Likewise. +* libports/port-ref.c: Likewise. +* libports/reallocate-from-external.c: Likewise. +* libports/transfer-right.c: Likewise. +* utils/rpctrace.c: Likewise. +--- + libports/bucket-iterate.c | 4 +--- + libports/complete-deallocate.c | 15 +++++++++++++++ + libports/create-internal.c | 3 +-- + libports/get-right.c | 2 +- + libports/import-port.c | 3 +-- + libports/lookup-port.c | 6 ++---- + libports/port-deref-weak.c | 10 +++------- + libports/port-deref.c | 34 ++++++++++++++++------------------ + libports/port-ref-weak.c | 8 +++----- + libports/port-ref.c | 8 +++----- + libports/ports.h | 4 ++-- + libports/reallocate-from-external.c | 2 +- + libports/transfer-right.c | 2 +- + utils/rpctrace.c | 10 ++++++++-- + 14 files changed, 58 insertions(+), 53 deletions(-) + +diff --git a/libports/bucket-iterate.c b/libports/bucket-iterate.c +index d5f590e..2a2b2b6 100644 +--- a/libports/bucket-iterate.c ++++ b/libports/bucket-iterate.c +@@ -35,7 +35,6 @@ _ports_bucket_class_iterate (struct port_bucket *bucket, + size_t i, n, nr_items; + error_t err; + +- pthread_mutex_lock (&_ports_lock); + pthread_mutex_lock (&_ports_htable_lock); + + if (_ports_htable.nr_items == 0) +@@ -60,13 +59,12 @@ _ports_bucket_class_iterate (struct port_bucket *bucket, + if ((bucket == NULL || pi->bucket == bucket) + && (class == NULL || pi->class == class)) + { +- pi->refcnt++; ++ refcounts_ref (&pi->refcounts, NULL); + p[n] = pi; + n++; + } + } + pthread_mutex_unlock (&_ports_htable_lock); +- pthread_mutex_unlock (&_ports_lock); + + if (n == 0) + { +diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c +index 1371d92..6c9074a 100644 +--- a/libports/complete-deallocate.c ++++ b/libports/complete-deallocate.c +@@ -29,14 +29,29 @@ _ports_complete_deallocate (struct port_info *pi) + + if (pi->port_right) + { ++ struct references result; ++ + pthread_mutex_lock (&_ports_htable_lock); ++ ++ refcounts_references (&pi->refcounts, &result); ++ if (result.hard > 0 || result.weak > 0) ++ { ++ /* A reference was reacquired through a hash table lookup. ++ It's fine, we didn't touch anything yet. */ ++ pthread_mutex_unlock (&_ports_htable_lock); ++ return; ++ } ++ + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); ++ + mach_port_mod_refs (mach_task_self (), pi->port_right, + MACH_PORT_RIGHT_RECEIVE, -1); + pi->port_right = MACH_PORT_NULL; + } + ++ pthread_mutex_lock (&_ports_lock); ++ + pi->bucket->count--; + pi->class->count--; + +diff --git a/libports/create-internal.c b/libports/create-internal.c +index e773dd6..9e3824d 100644 +--- a/libports/create-internal.c ++++ b/libports/create-internal.c +@@ -54,8 +54,7 @@ _ports_create_port_internal (struct port_class *class, + } + + pi->class = class; +- pi->refcnt = 1; +- pi->weakrefcnt = 0; ++ refcounts_init (&pi->refcounts, 1, 0); + pi->cancel_threshold = 0; + pi->mscount = 0; + pi->flags = 0; +diff --git a/libports/get-right.c b/libports/get-right.c +index 89050c6..8681f46 100644 +--- a/libports/get-right.c ++++ b/libports/get-right.c +@@ -41,7 +41,7 @@ ports_get_right (void *port) + if ((pi->flags & PORT_HAS_SENDRIGHTS) == 0) + { + pi->flags |= PORT_HAS_SENDRIGHTS; +- pi->refcnt++; ++ refcounts_ref (&pi->refcounts, NULL); + err = mach_port_request_notification (mach_task_self (), + pi->port_right, + MACH_NOTIFY_NO_SENDERS, +diff --git a/libports/import-port.c b/libports/import-port.c +index d0b2ea4..e250b0e 100644 +--- a/libports/import-port.c ++++ b/libports/import-port.c +@@ -48,8 +48,7 @@ ports_import_port (struct port_class *class, struct port_bucket *bucket, + return ENOMEM; + + pi->class = class; +- pi->refcnt = 1 + !!stat.mps_srights; +- pi->weakrefcnt = 0; ++ refcounts_init (&pi->refcounts, 1 + !!stat.mps_srights, 0); + pi->cancel_threshold = 0; + pi->mscount = stat.mps_mscount; + pi->flags = stat.mps_srights ? PORT_HAS_SENDRIGHTS : 0; +diff --git a/libports/lookup-port.c b/libports/lookup-port.c +index fbb13ef..1bf012f 100644 +--- a/libports/lookup-port.c ++++ b/libports/lookup-port.c +@@ -28,7 +28,6 @@ ports_lookup_port (struct port_bucket *bucket, + { + struct port_info *pi; + +- pthread_mutex_lock (&_ports_lock); + pthread_mutex_lock (&_ports_htable_lock); + + pi = hurd_ihash_find (&_ports_htable, port); +@@ -38,10 +37,9 @@ ports_lookup_port (struct port_bucket *bucket, + pi = 0; + + if (pi) +- pi->refcnt++; ++ ports_port_ref (pi); + + pthread_mutex_unlock (&_ports_htable_lock); +- pthread_mutex_unlock (&_ports_lock); +- ++ + return pi; + } +diff --git a/libports/port-deref-weak.c b/libports/port-deref-weak.c +index beb4842..8432660 100644 +--- a/libports/port-deref-weak.c ++++ b/libports/port-deref-weak.c +@@ -25,12 +25,8 @@ void + ports_port_deref_weak (void *portstruct) + { + struct port_info *pi = portstruct; +- +- pthread_mutex_lock (&_ports_lock); +- assert (pi->weakrefcnt); +- pi->weakrefcnt--; +- if (pi->refcnt == 0 && pi->weakrefcnt == 0) ++ struct references result; ++ refcounts_deref_weak (&pi->refcounts, &result); ++ if (result.hard == 0 && result.weak == 0) + _ports_complete_deallocate (pi); +- else +- pthread_mutex_unlock (&_ports_lock); + } +diff --git a/libports/port-deref.c b/libports/port-deref.c +index cf9b238..dd38f55 100644 +--- a/libports/port-deref.c ++++ b/libports/port-deref.c +@@ -25,26 +25,24 @@ void + ports_port_deref (void *portstruct) + { + struct port_info *pi = portstruct; +- int trieddroppingweakrefs = 0; +- +- retry: +- +- pthread_mutex_lock (&_ports_lock); +- +- if (pi->refcnt == 1 && pi->weakrefcnt +- && pi->class->dropweak_routine && !trieddroppingweakrefs) ++ struct references result; ++ ++ /* If we need to call the dropweak routine, we need to hold one ++ reference while doing so. We use a weak reference for this ++ purpose, which we acquire before we release our hard reference. ++ The order is important here to prevent a race. */ ++ if (pi->class->dropweak_routine) ++ refcounts_ref_weak (&pi->refcounts, NULL); ++ ++ refcounts_deref (&pi->refcounts, &result); ++ ++ if (pi->class->dropweak_routine) + { +- pthread_mutex_unlock (&_ports_lock); +- (*pi->class->dropweak_routine) (pi); +- trieddroppingweakrefs = 1; +- goto retry; ++ if (result.hard == 0 && result.weak > 1) ++ (*pi->class->dropweak_routine) (pi); ++ refcounts_deref_weak (&pi->refcounts, &result); + } +- +- assert (pi->refcnt); + +- pi->refcnt--; +- if (pi->refcnt == 0 && pi->weakrefcnt == 0) ++ if (result.hard == 0 && result.weak == 0) + _ports_complete_deallocate (pi); +- else +- pthread_mutex_unlock (&_ports_lock); + } +diff --git a/libports/port-ref-weak.c b/libports/port-ref-weak.c +index c7d3c69..e4b7fc8 100644 +--- a/libports/port-ref-weak.c ++++ b/libports/port-ref-weak.c +@@ -25,9 +25,7 @@ void + ports_port_ref_weak (void *portstruct) + { + struct port_info *pi = portstruct; +- +- pthread_mutex_lock (&_ports_lock); +- assert (pi->refcnt || pi->weakrefcnt); +- pi->weakrefcnt++; +- pthread_mutex_unlock (&_ports_lock); ++ struct references result; ++ refcounts_ref_weak (&pi->refcounts, &result); ++ assert (result.hard > 0 || result.weak > 1); + } +diff --git a/libports/port-ref.c b/libports/port-ref.c +index 92b7118..761c50f 100644 +--- a/libports/port-ref.c ++++ b/libports/port-ref.c +@@ -25,9 +25,7 @@ void + ports_port_ref (void *portstruct) + { + struct port_info *pi = portstruct; +- +- pthread_mutex_lock (&_ports_lock); +- assert (pi->refcnt || pi->weakrefcnt); +- pi->refcnt++; +- pthread_mutex_unlock (&_ports_lock); ++ struct references result; ++ refcounts_ref (&pi->refcounts, &result); ++ assert (result.hard > 1 || result.weak > 0); + } +diff --git a/libports/ports.h b/libports/ports.h +index 9c5f14d..116e920 100644 +--- a/libports/ports.h ++++ b/libports/ports.h +@@ -27,6 +27,7 @@ + #include <hurd/ihash.h> + #include <mach/notify.h> + #include <pthread.h> ++#include <refcount.h> + + /* These are global values for common flags used in the various structures. + Not all of these are meaningful in all flag fields. */ +@@ -39,8 +40,7 @@ + struct port_info + { + struct port_class *class; +- int refcnt; +- int weakrefcnt; ++ refcounts_t refcounts; + mach_port_mscount_t mscount; + mach_msg_seqno_t cancel_threshold; + int flags; +diff --git a/libports/reallocate-from-external.c b/libports/reallocate-from-external.c +index bbcf9ba..5ee579d 100644 +--- a/libports/reallocate-from-external.c ++++ b/libports/reallocate-from-external.c +@@ -55,7 +55,7 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive) + else if (((pi->flags & PORT_HAS_SENDRIGHTS) == 0) && stat.mps_srights) + { + pi->flags |= PORT_HAS_SENDRIGHTS; +- pi->refcnt++; ++ refcounts_ref (&pi->refcounts, NULL); + } + + pi->port_right = receive; +diff --git a/libports/transfer-right.c b/libports/transfer-right.c +index 5a7653d..d3ff0f4 100644 +--- a/libports/transfer-right.c ++++ b/libports/transfer-right.c +@@ -70,7 +70,7 @@ ports_transfer_right (void *tostruct, + else if (((topi->flags & PORT_HAS_SENDRIGHTS) == 0) && hassendrights) + { + topi->flags |= PORT_HAS_SENDRIGHTS; +- topi->refcnt++; ++ refcounts_ref (&topi->refcounts, NULL); + } + } + +diff --git a/utils/rpctrace.c b/utils/rpctrace.c +index fc913e3..b11fea4 100644 +--- a/utils/rpctrace.c ++++ b/utils/rpctrace.c +@@ -431,7 +431,9 @@ destroy_receiver_info (struct receiver_info *info) + while (send_wrapper) + { + struct sender_info *next = send_wrapper->next; +- assert (TRACED_INFO (send_wrapper)->pi.refcnt == 1); ++ assert ( ++ refcounts_hard_references (&TRACED_INFO (send_wrapper)->pi.refcounts) ++ == 1); + /* Reset the receive_right of the send wrapper in advance to avoid + * destroy_receiver_info is called when the port info is destroyed. */ + send_wrapper->receive_right = NULL; +@@ -848,7 +850,11 @@ rewrite_right (mach_port_t *right, mach_msg_type_name_t *type, + hurd_ihash_locp_remove (&traced_names, receiver_info->locp); + + send_wrapper2 = get_send_wrapper (receiver_info, dest, &rr); +- assert (TRACED_INFO (send_wrapper2)->pi.refcnt == 1); ++ assert ( ++ refcounts_hard_references ( ++ &TRACED_INFO (send_wrapper2)->pi.refcounts) ++ == 1); ++ + name = TRACED_INFO (send_wrapper2)->name; + TRACED_INFO (send_wrapper2)->name = NULL; + /* send_wrapper2 isn't destroyed normally, so we need to unlink +-- +2.0.0.rc0 + diff --git a/debian/patches/0004-libdiskfs-lock-less-reference-counting-for-peropen-o.patch b/debian/patches/0004-libdiskfs-lock-less-reference-counting-for-peropen-o.patch new file mode 100644 index 00000000..cd84b4bc --- /dev/null +++ b/debian/patches/0004-libdiskfs-lock-less-reference-counting-for-peropen-o.patch @@ -0,0 +1,143 @@ +From 51331c90f52801c9f65a002629dff5f9e4fec90d Mon Sep 17 00:00:00 2001 +From: Justus Winter <4winter@informatik.uni-hamburg.de> +Date: Tue, 6 May 2014 18:58:10 +0200 +Subject: [PATCH 4/5] libdiskfs: lock-less reference counting for peropen + objects + +* libdiskfs/diskfs.h (struct peropen): Use refcount_t for field refcnt. +* libdiskfs/peropen-make.c (diskfs_make_peropen): Initialize refcnt. +* libdiskfs/peropen-rele.c (diskfs_release_peropen): Adjust accordingly. +* libdiskfs/protid-make.c (diskfs_start_protid): Likewise. Also, the +node must no longer be locked, adjust comment accordingly. +(diskfs_create_protid): Likewise. +--- + libdiskfs/diskfs.h | 7 ++++--- + libdiskfs/peropen-make.c | 2 +- + libdiskfs/peropen-rele.c | 21 ++++++++++----------- + libdiskfs/protid-make.c | 8 ++++---- + 4 files changed, 19 insertions(+), 19 deletions(-) + +diff --git a/libdiskfs/diskfs.h b/libdiskfs/diskfs.h +index 8151ddc..ae1a150 100644 +--- a/libdiskfs/diskfs.h ++++ b/libdiskfs/diskfs.h +@@ -28,6 +28,7 @@ + #include <hurd/iohelp.h> + #include <idvec.h> + #include <features.h> ++#include <refcount.h> + + #ifdef DISKFS_DEFINE_EXTERN_INLINE + #define DISKFS_EXTERN_INLINE +@@ -57,7 +58,7 @@ struct peropen + { + int filepointer; + int lock_status; +- int refcnt; ++ refcount_t refcnt; + int openstat; + + struct node *np; +@@ -792,12 +793,12 @@ diskfs_create_node (struct node *dir, const char *name, mode_t mode, + struct dirstat *ds); + + /* Create and return a protid for an existing peropen PO in CRED, +- referring to user USER. The node PO->np must be locked. */ ++ referring to user USER. */ + error_t diskfs_create_protid (struct peropen *po, struct iouser *user, + struct protid **cred); + + /* Build and return in CRED a protid which has no user identification, for +- peropen PO. The node PO->np must be locked. */ ++ peropen PO. */ + error_t diskfs_start_protid (struct peropen *po, struct protid **cred); + + /* Finish building protid CRED started with diskfs_start_protid; +diff --git a/libdiskfs/peropen-make.c b/libdiskfs/peropen-make.c +index eba037f..6d5ca01 100644 +--- a/libdiskfs/peropen-make.c ++++ b/libdiskfs/peropen-make.c +@@ -31,7 +31,7 @@ diskfs_make_peropen (struct node *np, int flags, struct peropen *context, + + po->filepointer = 0; + po->lock_status = LOCK_UN; +- po->refcnt = 0; ++ refcount_init (&po->refcnt, 0); + po->openstat = flags; + po->np = np; + po->path = NULL; +diff --git a/libdiskfs/peropen-rele.c b/libdiskfs/peropen-rele.c +index d3f7492..877137b 100644 +--- a/libdiskfs/peropen-rele.c ++++ b/libdiskfs/peropen-rele.c +@@ -22,13 +22,8 @@ + void + diskfs_release_peropen (struct peropen *po) + { +- pthread_mutex_lock (&po->np->lock); +- +- if (--po->refcnt) +- { +- pthread_mutex_unlock (&po->np->lock); +- return; +- } ++ if (refcount_deref (&po->refcnt) > 0) ++ return; + + if (po->root_parent) + mach_port_deallocate (mach_task_self (), po->root_parent); +@@ -40,10 +35,14 @@ diskfs_release_peropen (struct peropen *po) + mach_port_deallocate (mach_task_self (), po->shadow_root_parent); + + if (po->lock_status != LOCK_UN) +- fshelp_acquire_lock (&po->np->userlock, &po->lock_status, +- &po->np->lock, LOCK_UN); +- +- diskfs_nput (po->np); ++ { ++ pthread_mutex_lock (&po->np->lock); ++ fshelp_acquire_lock (&po->np->userlock, &po->lock_status, ++ &po->np->lock, LOCK_UN); ++ diskfs_nput (po->np); ++ } ++ else ++ diskfs_nrele (po->np); + + free (po->path); + free (po); +diff --git a/libdiskfs/protid-make.c b/libdiskfs/protid-make.c +index b39b92a..22aaa2e 100644 +--- a/libdiskfs/protid-make.c ++++ b/libdiskfs/protid-make.c +@@ -20,7 +20,7 @@ + #include <assert.h> + + /* Build and return in CRED a protid which has no user identification, for +- peropen PO. The node PO->np must be locked. */ ++ peropen PO. */ + error_t + diskfs_start_protid (struct peropen *po, struct protid **cred) + { +@@ -29,7 +29,7 @@ diskfs_start_protid (struct peropen *po, struct protid **cred) + sizeof (struct protid), cred); + if (! err) + { +- po->refcnt++; ++ refcount_ref (&po->refcnt); + (*cred)->po = po; + (*cred)->shared_object = MACH_PORT_NULL; + (*cred)->mapped = 0; +@@ -55,8 +55,8 @@ diskfs_finish_protid (struct protid *cred, struct iouser *user) + assert_perror (err); + } + +-/* Create and return a protid for an existing peropen PO in CRED for USER. +- The node PO->np must be locked. */ ++/* Create and return a protid for an existing peropen PO in CRED for ++ USER. */ + error_t + diskfs_create_protid (struct peropen *po, struct iouser *user, + struct protid **cred) +-- +2.0.0.rc0 + diff --git a/debian/patches/0005-libtrivfs-lock-less-reference-counting-for-trivfs_pr.patch b/debian/patches/0005-libtrivfs-lock-less-reference-counting-for-trivfs_pr.patch new file mode 100644 index 00000000..d32acdd0 --- /dev/null +++ b/debian/patches/0005-libtrivfs-lock-less-reference-counting-for-trivfs_pr.patch @@ -0,0 +1,176 @@ +From af1a321ffb5df24b3a5fa866ab2aa9c5b03f358c Mon Sep 17 00:00:00 2001 +From: Justus Winter <4winter@informatik.uni-hamburg.de> +Date: Tue, 6 May 2014 19:07:13 +0200 +Subject: [PATCH 5/5] libtrivfs: lock-less reference counting for trivfs_protid + objects + +* libtrivfs/trivfs.h (struct trivfs_protid): Use refcount_t for field +refcnt. +(struct trivfs_control): Remove unused field lock. +* libtrivfs/open.c (trivfs_open): Initialize refcnt. +* libtrivfs/io-reauthenticate.c (trivfs_S_io_reauthenticate): Adjust +accordingly. +* libtrivfs/io-restrict-auth.c (trivfs_S_io_restrict_auth): Likewise. +* libtrivfs/protid-clean.c (trivfs_clean_protid): Likewise. +* libtrivfs/protid-dup.c (trivfs_protid_dup): Likewise. +* libtrivfs/cntl-create.c (trivfs_create_control): Drop the mutex +initialization. +* libtrivfs/peropen-make.c (trivfs_make_peropen): Initialize refcnt. +* libtrivfs/peropen-rele.c (trivfs_release_peropen): Adjust accordingly. +* libtrivfs/protid-make.c (trivfs_start_protid): Likewise. Also, the +node must no longer be locked, adjust comment accordingly. +(trivfs_create_protid): Likewise. +--- + libtrivfs/cntl-create.c | 1 - + libtrivfs/io-reauthenticate.c | 5 +---- + libtrivfs/io-restrict-auth.c | 4 +--- + libtrivfs/open.c | 2 +- + libtrivfs/protid-clean.c | 25 ++++++++++++++----------- + libtrivfs/protid-dup.c | 5 +---- + libtrivfs/trivfs.h | 4 ++-- + 7 files changed, 20 insertions(+), 26 deletions(-) + +diff --git a/libtrivfs/cntl-create.c b/libtrivfs/cntl-create.c +index 910daf3..eb9a834 100644 +--- a/libtrivfs/cntl-create.c ++++ b/libtrivfs/cntl-create.c +@@ -85,7 +85,6 @@ trivfs_create_control (mach_port_t underlying, + } + + (*control)->hook = 0; +- pthread_mutex_init (&(*control)->lock, NULL); + } + + out: +diff --git a/libtrivfs/io-reauthenticate.c b/libtrivfs/io-reauthenticate.c +index 7677697..df0ed2e 100644 +--- a/libtrivfs/io-reauthenticate.c ++++ b/libtrivfs/io-reauthenticate.c +@@ -62,11 +62,8 @@ trivfs_S_io_reauthenticate (struct trivfs_protid *cred, + newcred->isroot = 1; + + newcred->hook = cred->hook; +- +- pthread_mutex_lock (&cred->po->cntl->lock); + newcred->po = cred->po; +- newcred->po->refcnt++; +- pthread_mutex_unlock (&cred->po->cntl->lock); ++ refcount_ref (&newcred->po->refcnt); + + do + err = io_restrict_auth (newcred->po->cntl->underlying, &newcred->realnode, +diff --git a/libtrivfs/io-restrict-auth.c b/libtrivfs/io-restrict-auth.c +index 65b4fd6..39670fe 100644 +--- a/libtrivfs/io-restrict-auth.c ++++ b/libtrivfs/io-restrict-auth.c +@@ -110,10 +110,8 @@ trivfs_S_io_restrict_auth (struct trivfs_protid *cred, + } + + newcred->isroot = 0; +- pthread_mutex_lock (&cred->po->cntl->lock); + newcred->po = cred->po; +- newcred->po->refcnt++; +- pthread_mutex_unlock (&cred->po->cntl->lock); ++ refcount_ref (&newcred->po->refcnt); + if (cred->isroot && idvec_contains (user->uids, 0)) + newcred->isroot = 1; + newcred->user = user; +diff --git a/libtrivfs/open.c b/libtrivfs/open.c +index f64d2ff..97e70a1 100644 +--- a/libtrivfs/open.c ++++ b/libtrivfs/open.c +@@ -40,7 +40,7 @@ trivfs_open (struct trivfs_control *cntl, + + ports_port_ref (cntl); + +- po->refcnt = 1; ++ refcount_init (&po->refcnt, 1); + po->cntl = cntl; + po->openmodes = flags; + po->hook = 0; +diff --git a/libtrivfs/protid-clean.c b/libtrivfs/protid-clean.c +index f98da6a..815e731 100644 +--- a/libtrivfs/protid-clean.c ++++ b/libtrivfs/protid-clean.c +@@ -31,19 +31,22 @@ trivfs_clean_protid (void *arg) + (*trivfs_protid_destroy_hook) (cred); + + /* If we hold the only reference to the peropen, try to get rid of it. */ +- pthread_mutex_lock (&cntl->lock); +- if (cred->po->refcnt == 1 && trivfs_peropen_destroy_hook) ++ if (refcount_deref (&cred->po->refcnt) == 0) + { +- pthread_mutex_unlock (&cntl->lock); +- (*trivfs_peropen_destroy_hook) (cred->po); +- pthread_mutex_lock (&cntl->lock); ++ if (trivfs_protid_destroy_hook) ++ { ++ refcount_ref (&cred->po->refcnt); ++ (*trivfs_peropen_destroy_hook) (cred->po); ++ if (refcount_deref (&cred->po->refcnt) == 0) ++ goto free_po; ++ } ++ else ++ { ++ free_po: ++ ports_port_deref (cntl); ++ free (cred->po); ++ } + } +- if (--cred->po->refcnt == 0) +- { +- ports_port_deref (cntl); +- free (cred->po); +- } +- pthread_mutex_unlock (&cntl->lock); + + iohelp_free_iouser (cred->user); + +diff --git a/libtrivfs/protid-dup.c b/libtrivfs/protid-dup.c +index 6169603..75f3ca8 100644 +--- a/libtrivfs/protid-dup.c ++++ b/libtrivfs/protid-dup.c +@@ -35,11 +35,8 @@ trivfs_protid_dup (struct trivfs_protid *cred, struct trivfs_protid **dup) + + if (! err) + { +- pthread_mutex_lock (&cred->po->cntl->lock); + new->po = cred->po; +- new->po->refcnt++; +- pthread_mutex_unlock (&cred->po->cntl->lock); +- ++ refcount_ref (&new->po->refcnt); + new->isroot = cred->isroot; + + err = iohelp_dup_iouser (&new->user, cred->user); +diff --git a/libtrivfs/trivfs.h b/libtrivfs/trivfs.h +index bb456ff..8902338 100644 +--- a/libtrivfs/trivfs.h ++++ b/libtrivfs/trivfs.h +@@ -24,6 +24,7 @@ + #include <mach/mach.h> + #include <hurd/ports.h> + #include <hurd/iohelp.h> ++#include <refcount.h> + + struct trivfs_protid + { +@@ -41,14 +42,13 @@ struct trivfs_peropen + { + void *hook; /* for user use */ + int openmodes; +- int refcnt; ++ refcount_t refcnt; + struct trivfs_control *cntl; + }; + + struct trivfs_control + { + struct port_info pi; +- pthread_mutex_t lock; + struct port_class *protid_class; + struct port_bucket *protid_bucket; + mach_port_t filesys_id; +-- +2.0.0.rc0 + diff --git a/debian/patches/series b/debian/patches/series index 7462b249..5913ebff 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -41,3 +41,8 @@ xkb-compat.patch mach-defpager-protected-payload.patch #ext2fs-cache-superblock.patch +0001-libports-use-a-single-hash-table.patch +0002-include-add-lock-less-reference-counting-primitives.patch +0003-libports-lock-less-reference-counting-for-port_info-.patch +0004-libdiskfs-lock-less-reference-counting-for-peropen-o.patch +0005-libtrivfs-lock-less-reference-counting-for-trivfs_pr.patch |