Skip to contentSkip to conversation

urcu: add notice to URCU_TLS() for it is not async-signal-safe

PATCH New Lai Jiangshan 4 participants 10 messages Aug 8, 2012 – Sep 6, 2012 7 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/2 urcu: add notice to URCU_TLS() for it is not async-signal-safe

---
 urcu/tls-compat.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
urcu/tls-compat.h +14 -0
index 192a536..b7bf363 100644
@@ -59,6 +59,20 @@ extern "C" {
 
 #else /* #ifndef CONFIG_RCU_TLS */
 
+/*
+ * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it
+ * inside any function which can be called from signal handler.
+ *
+ * But if pthread_getspecific() is async-signal-safe in your
+ * platform, you can make URCU_TLS() async-signal-safe via:
+ * ensuring the first call to URCU_TLS() of a given TLS variable of
+ * all threads is called earliest from a non-signal handler function.
+ *
+ * Exmaple: In any thread, the first call of URCU_TLS(rcu_reader)
+ * is called from rcu_register_thread(), so we can ensure all later
+ * URCU_TLS(rcu_reader) in any thread is async-signal-safe.
+ */
+
 # include <pthread.h>
 
 struct urcu_tls {
Signature · 1 line
1.7.4.4
#2 lore raw

Re: [PATCH 1/2] urcu: add hint to DEFINE_URCU_TLS() for compound types

In reply to a message not in the archive
Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Just a hint.

merged, thanks!

Mathieu

Quoted 35 lines
---
 urcu/tls-compat.h |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
urcu/tls-compat.h +15 -0
index 9686eca..192a536 100644
@@ -34,6 +34,21 @@ extern "C" {
 
 #ifdef CONFIG_RCU_TLS	/* Based on ax_tls.m4 */
 
+/*
+ * Hint: How to define/declare TLS variables of compound types
+ *       such as array or function pointers?
+ *
+ * Answer: Use typedef to assign a type_name to the compound type.
+ * Example: Define a TLS variable which is an int array with len=4:
+ *
+ * 	typedef int my_int_array_type[4];
+ * 	DEFINE_URCU_TLS(my_int_array_type, var_name);
+ *
+ * Another exmaple:
+ * 	typedef void (*call_rcu_flavor)(struct rcu_head *, XXXX);
+ * 	DECLARE_URCU_TLS(call_rcu_flavor, p_call_rcu);
+ */
+
 # define DECLARE_URCU_TLS(type, name)	\
 	CONFIG_RCU_TLS type name
 
Signature · 1 line
1.7.4.4
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 15 lines · * Lai Jiangshan ([email protected]) wrote:
---
 urcu/tls-compat.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
urcu/tls-compat.h +2 -0
index 192a536..b7bf363 100644
@@ -59,6 +59,20 @@ extern "C" {
 
 #else /* #ifndef CONFIG_RCU_TLS */
 
+/*
+ * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it

urcu/tls-compat.h extern "C" { · line 73

Quoted 10 lines
urcu/tls-compat.h +10 -0
+ * inside any function which can be called from signal handler.
+ *
+ * But if pthread_getspecific() is async-signal-safe in your
+ * platform, you can make URCU_TLS() async-signal-safe via:
+ * ensuring the first call to URCU_TLS() of a given TLS variable of
+ * all threads is called earliest from a non-signal handler function.
+ *
+ * Exmaple: In any thread, the first call of URCU_TLS(rcu_reader)
+ * is called from rcu_register_thread(), so we can ensure all later
+ * URCU_TLS(rcu_reader) in any thread is async-signal-safe.

Hrm. We could also just block all signals within type *__tls_access_ ## name(void) (in tls-compat.h) and make sure it is async-signal-safe I guess ? I would prefer that solution: it would make the code more robust for a rarely taken performance hit.

Thoughts ?

Thanks,

Mathieu

Quoted 8 lines
urcu/tls-compat.h +2 -0
+ */
+
 # include <pthread.h>
 
 struct urcu_tls {
Signature · 1 line
1.7.4.4
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20120809141417.GC9064@Krystal

#6 lore raw

OK for me. Please do it.

Quoted 52 lines · Mathieu Desnoyers
Quoted 15 lines · * Lai Jiangshan ([email protected]) wrote:
---
 urcu/tls-compat.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
urcu/tls-compat.h +2 -0
index 192a536..b7bf363 100644
@@ -59,6 +59,20 @@ extern "C" {
 
 #else /* #ifndef CONFIG_RCU_TLS */
 
+/*
+ * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it

urcu/tls-compat.h extern "C" { · line 73

Quoted 10 lines
urcu/tls-compat.h +10 -0
+ * inside any function which can be called from signal handler.
+ *
+ * But if pthread_getspecific() is async-signal-safe in your
+ * platform, you can make URCU_TLS() async-signal-safe via:
+ * ensuring the first call to URCU_TLS() of a given TLS variable of
+ * all threads is called earliest from a non-signal handler function.
+ *
+ * Exmaple: In any thread, the first call of URCU_TLS(rcu_reader)
+ * is called from rcu_register_thread(), so we can ensure all later
+ * URCU_TLS(rcu_reader) in any thread is async-signal-safe.

Hrm. We could also just block all signals within type *__tls_access_ ## name(void) (in tls-compat.h) and make sure it is async-signal-safe I guess ? I would prefer that solution: it would make the code more robust for a rarely taken performance hit.

Thoughts ?

Thanks,

Mathieu

Quoted 8 lines
urcu/tls-compat.h +2 -1
+ */
+
 # include <pthread.h>
 
 struct urcu_tls {
--

1.7.4.4

--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): CACvQF53CY6UuoZzDzT-VE25wAyaNa_r=0vsOdTVzVF6dmScH1g@mail.gmail.com

#8 lore raw

Looking at the result of a quick google search:

http://curl.haxx.se/mail/lib-2006-09/0224.html
http://www.slamb.org/projects/sigsafe/api/patternref.html

"Additionally, it makes the same assumption as all other methods for handling thread-directed signals (with the exception of kevent(2) handling), that pthread_getspecific(2) is async signal-safe. This is not guaranteed by SUSv3."

and

https://groups.google.com/forum/?fromgroups#!topic/comp.os.linux.development/nZfmndKbzJw[1-25]

it looks like using pthread_getspecific from a signal handler is not always safe, mainly due to possible use of sigaltstack. So disabling signals works for the "pthread_key_create" part, but we still have an issue with pthread_getspecific.

Ideas are welcome on how to best deal with this issue.

Thanks!

Mathieu

Quoted 61 lines · * Lai Jiangshan ([email protected]) wrote:

OK for me. Please do it.

Quoted 47 lines · Mathieu Desnoyers
Quoted 15 lines · * Lai Jiangshan ([email protected]) wrote:
---
 urcu/tls-compat.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
urcu/tls-compat.h +2 -0
index 192a536..b7bf363 100644
@@ -59,6 +59,20 @@ extern "C" {
 
 #else /* #ifndef CONFIG_RCU_TLS */
 
+/*
+ * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it

urcu/tls-compat.h extern "C" { · line 73

Quoted 10 lines
urcu/tls-compat.h +10 -0
+ * inside any function which can be called from signal handler.
+ *
+ * But if pthread_getspecific() is async-signal-safe in your
+ * platform, you can make URCU_TLS() async-signal-safe via:
+ * ensuring the first call to URCU_TLS() of a given TLS variable of
+ * all threads is called earliest from a non-signal handler function.
+ *
+ * Exmaple: In any thread, the first call of URCU_TLS(rcu_reader)
+ * is called from rcu_register_thread(), so we can ensure all later
+ * URCU_TLS(rcu_reader) in any thread is async-signal-safe.

Hrm. We could also just block all signals within type *__tls_access_ ## name(void) (in tls-compat.h) and make sure it is async-signal-safe I guess ? I would prefer that solution: it would make the code more robust for a rarely taken performance hit.

Thoughts ?

Thanks,

Mathieu

Quoted 8 lines
urcu/tls-compat.h +2 -1
+ */
+
 # include <pthread.h>
 
 struct urcu_tls {
--

1.7.4.4

Quoted 5 lines

--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20120809200211.GA14409@Krystal

#10 lore raw
Quoted 12 lines · Mathieu Desnoyers

Looking at the result of a quick google search:

http://curl.haxx.se/mail/lib-2006-09/0224.html
http://www.slamb.org/projects/sigsafe/api/patternref.html

"Additionally, it makes the same assumption as all other methods for handling thread-directed signals (with the exception of kevent(2) handling), that pthread_getspecific(2) is async signal-safe. This is not guaranteed by SUSv3."

and

Quoted 8 lines

https://groups.google.com/forum/?fromgroups#!topic/comp.os.linux.development/nZfmndKbzJw[1-25]

it looks like using pthread_getspecific from a signal handler is not always safe, mainly due to possible use of sigaltstack. So disabling signals works for the "pthread_key_create" part, but we still have an issue with pthread_getspecific.

Ideas are welcome on how to best deal with this issue.

What's the problem with disabling signals + pthread_getspecific()? Waht's the problem with disabling signals + __tls_access_ ## name()?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

(for the records: this discussion is about userspace RCU tls-compat.h, which uses TLS on systems supporting it, and fall back to pthread_key_create/getspecific/setspecific if not. The culprit of the issue is that we want to allow reading the tls-compat variable from signal handlers, and thus async-signal-safety of pthread_key_* and TLS.

The code we refer to is:
http://git.lttng.org/?p=userspace-rcu.git;a=blob;f=urcu/tls-compat.h;h=192a53609fb5f6bc445f98fdd6bc26918126687e;hb=HEAD)

Quoted 20 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 19 lines · Mathieu Desnoyers

Looking at the result of a quick google search:

http://curl.haxx.se/mail/lib-2006-09/0224.html
http://www.slamb.org/projects/sigsafe/api/patternref.html

"Additionally, it makes the same assumption as all other methods for handling thread-directed signals (with the exception of kevent(2) handling), that pthread_getspecific(2) is async signal-safe. This is not guaranteed by SUSv3."

and

https://groups.google.com/forum/?fromgroups#!topic/comp.os.linux.development/nZfmndKbzJw[1-25]

it looks like using pthread_getspecific from a signal handler is not always safe, mainly due to possible use of sigaltstack. So disabling signals works for the "pthread_key_create" part, but we still have an issue with pthread_getspecific.

Quoted 4 lines
Quoted 1 line

Ideas are welcome on how to best deal with this issue.

What's the problem with disabling signals + pthread_getspecific()? Waht's the problem with disabling signals + __tls_access_ ## name()?

* pthread_key_* fallback

Disabling signals would allow us to be reentrant with respect to signals, which actually solves part of the problem (reentrancy) for the pthread_key_create part (protected by lock).

Disabling signals, AFAIU, (and if we disregard the SUSv3 standard for a minute) should not be strictly required around the pthread_getspecific call on most architectures (no lock taken). We should carefully review the implementations for each architecture we support if we want to assume this though. If the getspecific returns NULL. we should disable signals and call pthread_getspecific again with signals disabled, and then call pthread_setspecific if necessary, before re-enabling signals. The benefit of not _always_ disabling signals around pthread_getspecific() is significant gain in performance in the common
case: the entire hot path of rcu_read_lock/unlock all happens in userspace, without any system call, and uses tls-compat variables.

The other part of the problem with pthread_getspecific and signal handlers is that it does not seem to be SUSv3-compliant to use pthread_getspecific() from within a signal handler. One example that can lead to problems is if the signal handler is setup with sigaltstack(2).

We might want to simply document this limitation:

"RCU read-side critical sections can be used in signals handlers, except those setup with sigaltstack(2)."

* TLS

Userspace RCU always touch the TLS variables from thread context (from within rcu_register_thread()) before they are allowed to be touched by signal handlers nested over threads. This ensures that issues with lazy binding and dynamic linker lock are not encountered
(ref. http://sourceware.org/ml/libc-alpha/2012-06/msg00372.html). I did the same within my LTTng-UST use of TLS variables: they are touched by a constructor once so we don't run into deadlocks between UST lock and the libc lock protecting dynamic linking (recursive mutex also taken around the constructor calls, within which we needed to take the UST lock, thus causing deadlocks).

Feedback is welcome,

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20120810141615.GA27969@Krystal

#14 lore raw
Quoted 58 lines · Mathieu Desnoyers

(for the records: this discussion is about userspace RCU tls-compat.h, which uses TLS on systems supporting it, and fall back to pthread_key_create/getspecific/setspecific if not. The culprit of the issue is that we want to allow reading the tls-compat variable from signal handlers, and thus async-signal-safety of pthread_key_* and TLS.

The code we refer to is:
http://git.lttng.org/?p=userspace-rcu.git;a=blob;f=urcu/tls-compat.h;h=192a53609fb5f6bc445f98fdd6bc26918126687e;hb=HEAD)

Quoted 20 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 19 lines · Mathieu Desnoyers

Looking at the result of a quick google search:

http://curl.haxx.se/mail/lib-2006-09/0224.html
http://www.slamb.org/projects/sigsafe/api/patternref.html

"Additionally, it makes the same assumption as all other methods for handling thread-directed signals (with the exception of kevent(2) handling), that pthread_getspecific(2) is async signal-safe. This is not guaranteed by SUSv3."

and

https://groups.google.com/forum/?fromgroups#!topic/comp.os.linux.development/nZfmndKbzJw[1-25]

it looks like using pthread_getspecific from a signal handler is not always safe, mainly due to possible use of sigaltstack. So disabling signals works for the "pthread_key_create" part, but we still have an issue with pthread_getspecific.

Quoted 4 lines
Quoted 1 line

Ideas are welcome on how to best deal with this issue.

What's the problem with disabling signals + pthread_getspecific()? Waht's the problem with disabling signals + __tls_access_ ## name()?

* pthread_key_* fallback

Disabling signals would allow us to be reentrant with respect to signals, which actually solves part of the problem (reentrancy) for the pthread_key_create part (protected by lock).

Disabling signals, AFAIU, (and if we disregard the SUSv3 standard for a minute) should not be strictly required around the pthread_getspecific call on most architectures (no lock taken). We should carefully review the implementations for each architecture we support if we want to assume this though. If the getspecific returns NULL. we should disable signals and call pthread_getspecific again with signals disabled, and then call pthread_setspecific if necessary, before re-enabling signals. The benefit of not _always_ disabling signals around pthread_getspecific() is significant gain in performance in the common
case: the entire hot path of rcu_read_lock/unlock all happens in userspace, without any system call, and uses tls-compat variables.

The other part of the problem with pthread_getspecific and signal handlers is that it does not seem to be SUSv3-compliant to use pthread_getspecific() from within a signal handler. One example that can lead to problems is if the signal handler is setup with sigaltstack(2).

Quoted 4 lines

We might want to simply document this limitation:

"RCU read-side critical sections can be used in signals handlers, except those setup with sigaltstack(2)."

Seems to me to be a reasonable limitation. Easier to relax limitations than to add new ones!

Quoted 6 lines

* TLS

Userspace RCU always touch the TLS variables from thread context (from within rcu_register_thread()) before they are allowed to be touched by signal handlers nested over threads. This ensures that issues with lazy binding and dynamic linker lock are not encountered

s/ensures that/avoids/
s/and dynamic/and the dynamic/
s/ are not encountered//

Quoted 1 line

(ref. http://sourceware.org/ml/libc-alpha/2012-06/msg00372.html).

And stop here.

Quoted 9 lines

I did
the same within my LTTng-UST use of TLS variables: they are touched by a constructor once so we don't run into deadlocks between UST lock and the libc lock protecting dynamic linking (recursive mutex also taken around the constructor calls, within which we needed to take the UST lock, thus causing deadlocks).

Feedback is welcome,

Looks good!

Thanx, Paul

Quoted 10 lines

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#15 lore raw
In reply to a message not in the archive
Quoted 58 lines · * Mathieu Desnoyers ([email protected]) wrote:

(for the records: this discussion is about userspace RCU tls-compat.h, which uses TLS on systems supporting it, and fall back to pthread_key_create/getspecific/setspecific if not. The culprit of the issue is that we want to allow reading the tls-compat variable from signal handlers, and thus async-signal-safety of pthread_key_* and TLS.

The code we refer to is:
http://git.lttng.org/?p=userspace-rcu.git;a=blob;f=urcu/tls-compat.h;h=192a53609fb5f6bc445f98fdd6bc26918126687e;hb=HEAD)

Quoted 20 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 19 lines · Mathieu Desnoyers

Looking at the result of a quick google search:

http://curl.haxx.se/mail/lib-2006-09/0224.html
http://www.slamb.org/projects/sigsafe/api/patternref.html

"Additionally, it makes the same assumption as all other methods for handling thread-directed signals (with the exception of kevent(2) handling), that pthread_getspecific(2) is async signal-safe. This is not guaranteed by SUSv3."

and

https://groups.google.com/forum/?fromgroups#!topic/comp.os.linux.development/nZfmndKbzJw[1-25]

it looks like using pthread_getspecific from a signal handler is not always safe, mainly due to possible use of sigaltstack. So disabling signals works for the "pthread_key_create" part, but we still have an issue with pthread_getspecific.

Quoted 4 lines
Quoted 1 line

Ideas are welcome on how to best deal with this issue.

What's the problem with disabling signals + pthread_getspecific()? Waht's the problem with disabling signals + __tls_access_ ## name()?

* pthread_key_* fallback

Disabling signals would allow us to be reentrant with respect to signals, which actually solves part of the problem (reentrancy) for the pthread_key_create part (protected by lock).

Disabling signals, AFAIU, (and if we disregard the SUSv3 standard for a minute) should not be strictly required around the pthread_getspecific call on most architectures (no lock taken). We should carefully review the implementations for each architecture we support if we want to assume this though. If the getspecific returns NULL. we should disable signals and call pthread_getspecific again with signals disabled, and then call pthread_setspecific if necessary, before re-enabling signals. The benefit of not _always_ disabling signals around pthread_getspecific() is significant gain in performance in the common
case: the entire hot path of rcu_read_lock/unlock all happens in userspace, without any system call, and uses tls-compat variables.

The other part of the problem with pthread_getspecific and signal handlers is that it does not seem to be SUSv3-compliant to use pthread_getspecific() from within a signal handler. One example that can lead to problems is if the signal handler is setup with sigaltstack(2).

Quoted 4 lines

We might want to simply document this limitation:

"RCU read-side critical sections can be used in signals handlers, except those setup with sigaltstack(2)."

Pushed a documentation change to that effect:

commit 88ecbe6daa6da4850ea975b635cd1af8e4ca9bfe

Author: Mathieu Desnoyers <[email protected]>
Date:   Thu Sep 6 09:58:36 2012 -0400

Document sigaltstack(2) limitation

Quoted 4 lines

* TLS

Quoted 11 lines

Userspace RCU always touch the TLS variables from thread context (from within rcu_register_thread()) before they are allowed to be touched by signal handlers nested over threads. This ensures that issues with lazy binding and dynamic linker lock are not encountered
(ref. http://sourceware.org/ml/libc-alpha/2012-06/msg00372.html). I did the same within my LTTng-UST use of TLS variables: they are touched by a constructor once so we don't run into deadlocks between UST lock and the libc lock protecting dynamic linking (recursive mutex also taken around the constructor calls, within which we needed to take the UST lock, thus causing deadlocks).

This one should therefore just work (unless someone thinks otherwise).

Thanks,

Mathieu

Quoted 17 lines

Feedback is welcome,

Thanks,

Mathieu

Signature · 9 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#16 lore raw
In reply to a message not in the archive
Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Finally, I'm merging this patch, and I won't go for the signal-disabling implementation, mainly because we have the same kind of behavior with the TLS fixup for TLS variables in DSO. So this comment apply both to the TLS and pthread getspecific implementation of the URCU_TLS, so I'm moving it slightly higher in the header file.

Thanks!

Mathieu

Quoted 37 lines
---
 urcu/tls-compat.h |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
urcu/tls-compat.h +14 -0
index 192a536..b7bf363 100644
@@ -59,6 +59,20 @@ extern "C" {
 
 #else /* #ifndef CONFIG_RCU_TLS */
 
+/*
+ * NOTE: URCU_TLS() is NOT async-signal-safe, you can't use it
+ * inside any function which can be called from signal handler.
+ *
+ * But if pthread_getspecific() is async-signal-safe in your
+ * platform, you can make URCU_TLS() async-signal-safe via:
+ * ensuring the first call to URCU_TLS() of a given TLS variable of
+ * all threads is called earliest from a non-signal handler function.
+ *
+ * Exmaple: In any thread, the first call of URCU_TLS(rcu_reader)
+ * is called from rcu_register_thread(), so we can ensure all later
+ * URCU_TLS(rcu_reader) in any thread is async-signal-safe.
+ */
+
 # include <pthread.h>
 
 struct urcu_tls {
Signature · 7 lines
1.7.4.4


_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com