[lttng-dev] [PATCH liburcu] Fix pthread_atfork() behaviour

Keir Fraser keir at cohodata.com
Mon Apr 14 09:31:57 EDT 2014


In the process of integrating liburcu into a multi-threaded codebase 
with fork()s I found a couple of problems with liburcu that I could not 
work around without fixing the library. Hence I present the two required 
fixes here (as attachments, sorry!) with some background info about them.

After fork() the child process has no pthreads but the one that called 
fork(). Unfortunately call_rcu_after_fork_child() does not update URCU's 
thread registry to reflect this -- if fork() is called with any threads 
registered with URCU then the child process will inherit a corrupted 
registry containing a linked list through per-thread TLS state which is 
no longer valid allocated memory. Crash or hang soon after is the 
result. Patch 1 therefore simply clears the registry list in the child 
process. Caveats here are that (a) the calling thread cannot be 
registered (it must unregister/re-register itself in the atfork 
handlers); and (b) some flavours of URCU may have more complex 
registries than a simple linked list and so this patch may not be 
sufficient for those. I only tested the memb/mb flavour myself.

A second problem is that although call_rcu threads are paused across 
fork(), the handshaking PAUSED flag is not cleared when their execution 
resumes. Hence a second fork() invocation in the original parent process 
will not spin-wait for call_rcu threads to quiesce (as the atfork 
handler will observe all PAUSED flags already set). Patch 2 fixes this 
with the appropriate clearing handshake on resume, post-fork.

Please feel free to modify or rewrite these patches, or solve the 
described problems in a different way, as you see fit! Cc me on replies 
as I am not a subscriber to this list.

  Regards,
  Keir Fraser
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 1-zap-thread-registry.patch
URL: <http://lists.lttng.org/pipermail/lttng-dev/attachments/20140414/ecd40ea9/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 2-clear-paused-flag.patch
URL: <http://lists.lttng.org/pipermail/lttng-dev/attachments/20140414/ecd40ea9/attachment-0001.ksh>


More information about the lttng-dev mailing list