boilerplate: introduce big lock to serialze dso init calls
PATCH 1/1 boilerplate: introduce big lock to serialze dso init calls
When using dlopen() to include xenomai libraries into an application, __xenomai_init could be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.
Introduce a big lock to serialize all calls to __xenomai_init.
--- lib/boilerplate/setup.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/boilerplate/setup.c +5 -0
@@ -55,6 +55,9 @@ static int main_init_done;static DEFINE_PRIVATE_LIST (setup_list); +// serialize dlopen() init calls, which come potentially multi-threaded +static pthread_mutex_t dso_init_lock =PTHREAD_MUTEX_INITIALIZER ; +static const struct option base_options[] = { { #define help_opt 0
@@ -657,7 +660,9 @@ void xenomai_init(int *argcp, char *const **argvp)void xenomai_init_dso (int *argcp,char *const **argvp) { +write_lock_nocancel (&dso_init_lock);__xenomai_init (argcp, argvp,"DSO" ); +write_unlock (&dso_init_lock);trace_me ("DSO bootstrap done" ); }
Signature · 1 line
2.13.6
Quoted 3 lines · Henning Schild
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.
So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run xenomai_init_dso manually in that case?
Quoted 13 lines
Introduce a big lock to serialize all calls to
__xenomai_init.Signed-off-by Henning Schild <[email protected]>--- lib/boilerplate/setup.c | 5 +++++ 1 file changed, 5 insertions(+)lib/boilerplate/setup.c +0
-0
lib/boilerplate/setup.c static int main_init_done; · line 58
Quoted 4 lines
lib/boilerplate/setup.c +1
-0@@ -55,6 +55,9 @@ static int main_init_done;static DEFINE_PRIVATE_LIST (setup_list); +// serialize dlopen() init calls, which come potentially multi-threaded
Style: we use /* */ comments.
Quoted 7 lines
lib/boilerplate/setup.c +2
-0+static pthread_mutex_t dso_init_lock =PTHREAD_MUTEX_INITIALIZER ; +static const struct option base_options[] = { { #define help_opt 0
lib/boilerplate/setup.c void xenomai_init(int *argcp, char *const **argvp) · line 668
Quoted 9 lines
lib/boilerplate/setup.c +2
-0@@ -657,7 +660,9 @@ void xenomai_init(int *argcp, char *const **argvp)void xenomai_init_dso (int *argcp,char *const **argvp) { +write_lock_nocancel (&dso_init_lock);__xenomai_init (argcp, argvp,"DSO" ); +write_unlock (&dso_init_lock);trace_me ("DSO bootstrap done" ); }
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
Am Fri, 9 Feb 2018 17:48:22 +0100
schrieb Jan Kiszka <[email protected]>:
Quoted 9 lines · Henning Schild
Quoted 4 lines
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run
xenomai_init_dsomanually in that case?
According to its man-page dlopen is MT-Safe, but i do not think that necessarily means it will serialize the init functions for us. I think i will have to read the code to answer this one.
Henning
Quoted 39 lines
Quoted 17 lines
Introduce a big lock to serialize all calls to
__xenomai_init.Signed-off-by Henning Schild <[email protected]>--- lib/boilerplate/setup.c | 5 +++++ 1 file changed, 5 insertions(+)lib/boilerplate/setup.c +1
-0@@ -55,6 +55,9 @@ static int main_init_done;static DEFINE_PRIVATE_LIST (setup_list); +// serialize dlopen() init calls, which come potentially Quoted 1 line
multi-threaded
Style: we use /* */ comments.
Quoted 7 lines
lib/boilerplate/setup.c +2
-0+static pthread_mutex_t dso_init_lock =PTHREAD_MUTEX_INITIALIZER ; +static const struct option base_options[] = { { #define help_opt 0lib/boilerplate/setup.c void xenomai_init(int *argcp, char *const · line 668
Quoted 9 lines
lib/boilerplate/setup.c +2
-0@@ -657,7 +660,9 @@ void xenomai_init(int *argcp, char *const **argvp)void xenomai_init_dso (int *argcp,char *const **argvp) { +write_lock_nocancel (&dso_init_lock);__xenomai_init (argcp, argvp,"DSO" ); +write_unlock (&dso_init_lock);trace_me ("DSO bootstrap done" ); }Jan
Am Fri, 9 Feb 2018 18:23:47 +0100
schrieb "[ext] Henning Schild" <[email protected]>:
Quoted 9 lines
Am Fri, 9 Feb 2018 17:48:22 +0100
schrieb Jan Kiszka <[email protected]>:Quoted 6 lines · Henning Schild
Quoted 4 lines
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.
Quoted 7 lines
Quoted 3 lines
So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run
xenomai_init_dsomanually in that case?According to its man-page dlopen is MT-Safe, but i do not think that necessarily means it will serialize the init functions for us. I think i will have to read the code to answer this one.
I just had a look at glibc, and it looks like dlopen contains a big dlopen-lock as well.
dl_open_worker will be called only under that lock and that is where a function named _dl_init gets called. uclibc also uses a big lock around all dlopen activity.
It all looks like my patch is not required.
Henning
Quoted 47 lines
Henning
Quoted 31 lines
Quoted 17 lines
Introduce a big lock to serialize all calls to
__xenomai_init.Signed-off-by Henning Schild <[email protected]>--- lib/boilerplate/setup.c | 5 +++++ 1 file changed, 5 insertions(+)lib/boilerplate/setup.c +1
-0@@ -55,6 +55,9 @@ static int main_init_done;static DEFINE_PRIVATE_LIST (setup_list); +// serialize dlopen() init calls, which come potentially Quoted 1 line
multi-threaded
Style: we use /* */ comments.
Quoted 10 lines
lib/boilerplate/setup.c +3
-0+static pthread_mutex_t dso_init_lock =PTHREAD_MUTEX_INITIALIZER ; +static const struct option base_options[] = { { #define help_opt 0@@ -657,7 +660,9 @@ void xenomai_init(int *argcp, char *const **argvp)void xenomai_init_dso (int *argcp,char *const **argvp) { +write_lock_nocancel (&dso_init_lock);Quoted 8 lines
lib/boilerplate/setup.c void xenomai_init(int *argcp, char *const **argvp) · line 668
Quoted 6 lines
lib/boilerplate/setup.c +1
-0__xenomai_init (argcp, argvp,"DSO" ); +write_unlock (&dso_init_lock);trace_me ("DSO bootstrap done" ); }Jan
_______________________________________________
Xenomai mailing list
[email protected]
https://xenomai.org/mailman/listinfo/xenomai
Quoted 23 lines · Henning Schild
Am Fri, 9 Feb 2018 18:23:47 +0100
schrieb "[ext] Henning Schild" <[email protected]>:Quoted 9 lines
Am Fri, 9 Feb 2018 17:48:22 +0100
schrieb Jan Kiszka <[email protected]>:Quoted 6 lines · Henning Schild
Quoted 4 lines
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.Quoted 7 lines
Quoted 3 lines
So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run
xenomai_init_dsomanually in that case?According to its man-page dlopen is MT-Safe, but i do not think that necessarily means it will serialize the init functions for us. I think i will have to read the code to answer this one.
I just had a look at glibc, and it looks like dlopen contains a big dlopen-lock as well.
Quoted 8 lines
dl_open_workerwill be called only under that lock and that is where a function named_dl_initgets called. uclibc also uses a big lock around all dlopen activity.It all looks like my patch is not required.
The MT-safety issue may be with the setup calls, which run on behalf of library constructors. Multiple threads calling __register_setup_call() concurrently may be a problem, which may happen when multiple DSOs are loaded concurrently (assuming their is nothing serializing the dl work).
#define core_setup_call(__name) __setup_call(__name, 0)
#define boilerplate_setup_call(__name) __setup_call(__name, 1)
#define copperplate_setup_call(__name) __setup_call(__name, 2)
#define interface_setup_call(__name) __setup_call(__name, 3)
#define post_setup_call(__name) __setup_call(__name, 4)
#define user_setup_call(__name) __setup_call(__name, 5)
Signature · 1 line
Philippe.
Am Sat, 10 Feb 2018 11:33:00 +0100
schrieb Philippe Gerum <[email protected]>:
Quoted 39 lines · Henning Schild
Quoted 23 lines
Am Fri, 9 Feb 2018 18:23:47 +0100
schrieb "[ext] Henning Schild" <[email protected]>:Quoted 9 lines
Am Fri, 9 Feb 2018 17:48:22 +0100
schrieb Jan Kiszka <[email protected]>:Quoted 6 lines · Henning Schild
Quoted 4 lines
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.Quoted 7 lines
Quoted 3 lines
So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run
xenomai_init_dsomanually in that case?According to its man-page dlopen is MT-Safe, but i do not think that necessarily means it will serialize the init functions for us. I think i will have to read the code to answer this one.
I just had a look at glibc, and it looks like dlopen contains a big dlopen-lock as well.
Quoted 8 lines
dl_open_workerwill be called only under that lock and that is where a function named_dl_initgets called. uclibc also uses a big lock around all dlopen activity.It all looks like my patch is not required.
The MT-safety issue may be with the setup calls, which run on behalf of library constructors. Multiple threads calling
__register_setup_call()concurrently may be a problem, which may happen when multiple DSOs are loaded concurrently (assuming their is nothing serializing the dl work).
Quoted 6 lines
#define
core_setup_call(__name)__setup_call(__name, 0)
#defineboilerplate_setup_call(__name)__setup_call(__name, 1)
#definecopperplate_setup_call(__name)__setup_call(__name, 2)
#defineinterface_setup_call(__name)__setup_call(__name, 3)
#definepost_setup_call(__name)__setup_call(__name, 4)
#defineuser_setup_call(__name)__setup_call(__name, 5)
Found the problem, the setup_list was inconsistent because the application did a dlclose() on one of the xenomai-libs. And there are no destructors and no unregister_setup_call ...
Now i am wondering whether the setup_call list would be the only thing to consider when adding dtors. Or whether we could just have a dtor that returns -EBUSY to prevent dlclose().
Henning
I am now working on a fix for that issue. That will probably come with a testcase for dlopen, seems that whole code-path is not covered by any testcases.
Henning
Am Mon, 12 Feb 2018 16:57:34 +0100
schrieb "[ext] Henning Schild" <[email protected]>:
Quoted 65 lines
Am Sat, 10 Feb 2018 11:33:00 +0100
schrieb Philippe Gerum <[email protected]>:Quoted 39 lines · Henning Schild
Quoted 23 lines
Am Fri, 9 Feb 2018 18:23:47 +0100
schrieb "[ext] Henning Schild" <[email protected]>:Quoted 9 lines
Am Fri, 9 Feb 2018 17:48:22 +0100
schrieb Jan Kiszka <[email protected]>:Quoted 6 lines · Henning Schild
Quoted 4 lines
When using
dlopen()to include xenomai libraries into an application,__xenomai_initcould be running in multiple threads at a time. That happens if the application dlopens multiple libs in multiple threads.Quoted 7 lines
Quoted 3 lines
So, dlopen itself is thread-safe, but it does not guarantee atomicity for the init functions it executes? Or would we run
xenomai_init_dsomanually in that case?According to its man-page dlopen is MT-Safe, but i do not think that necessarily means it will serialize the init functions for us. I think i will have to read the code to answer this one.
I just had a look at glibc, and it looks like dlopen contains a big dlopen-lock as well.
Quoted 8 lines
dl_open_workerwill be called only under that lock and that is where a function named_dl_initgets called. uclibc also uses a big lock around all dlopen activity.It all looks like my patch is not required.
The MT-safety issue may be with the setup calls, which run on behalf of library constructors. Multiple threads calling
__register_setup_call()concurrently may be a problem, which may happen when multiple DSOs are loaded concurrently (assuming their is nothing serializing the dl work).Quoted 8 lines
#define
core_setup_call(__name)__setup_call(__name,
0) #defineboilerplate_setup_call(__name)__setup_call(__name, 1)#definecopperplate_setup_call(__name)__setup_call(__name, 2)
#defineinterface_setup_call(__name)__setup_call(__name, 3)
#definepost_setup_call(__name)__setup_call(__name,
4) #defineuser_setup_call(__name)__setup_call(__name, 5)Found the problem, the
setup_listwas inconsistent because the application did adlclose()on one of the xenomai-libs. And there are no destructors and nounregister_setup_call...Now i am wondering whether the
setup_calllist would be the only thing to consider when adding dtors. Or whether we could just have a dtor that returns-EBUSYto preventdlclose().Henning
_______________________________________________
Xenomai mailing list
[email protected]
https://xenomai.org/mailman/listinfo/xenomai