[02/16] libelf: Make elf_version thread-safe

Message ID 20231010134300.53830-2-mark@klomp.org
State Committed
Headers
Series [01/16] lib: Add new once_define and once macros to eu-config.h |

Commit Message

Mark Wielaard Oct. 10, 2023, 1:42 p.m. UTC
  From: Heather McIntyre <hsm2@rice.edu>

	* elf_version.c (version_once): Define once.
	(initialize_version): New static function.
	(elf_version): Use initialize_version version_once.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libelf/elf_version.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
  

Comments

Mark Wielaard Oct. 10, 2023, 2 p.m. UTC | #1
Hi Heather,

On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard wrote:
> From: Heather McIntyre <hsm2@rice.edu>
> 
> 	* elf_version.c (version_once): Define once.
> 	(initialize_version): New static function.
> 	(elf_version): Use initialize_version version_once.
> 
> Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
> Signed-off-by: Mark Wielaard <mark@klomp.org>
> ---
>  libelf/elf_version.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/libelf/elf_version.c b/libelf/elf_version.c
> index 6ec534ab..8296bb65 100644
> --- a/libelf/elf_version.c
> +++ b/libelf/elf_version.c
> @@ -32,12 +32,21 @@
>  #endif
>  
>  #include <libelfP.h>
> +#include <pthread.h>
>  
> +/* Multiple threads may initialize __libelf_version.
> +   pthread_once() ensures that __libelf_version is initialized only once. */
> +once_define(static, version_once);
>  
>  /* Currently selected version.  Should be EV_CURRENT.
>     Will be EV_NONE if elf_version () has not been called yet.  */
>  unsigned int __libelf_version = EV_NONE;
>  
> +static void initialize_version(void)
> +{
> +  __libelf_version = EV_CURRENT;
> +}
> +
>  unsigned int
>  elf_version (unsigned int version)
>  {
> @@ -49,7 +58,7 @@ elf_version (unsigned int version)
>        /* Phew, we know this version.  */
>  
>        /* Signal that the version is now initialized.  */
> -      __libelf_version = EV_CURRENT;
> +      once(version_once, initialize_version);
>  
>        /* And return the last (or initial) version.  */
>        return EV_CURRENT;

This is an odd function. The intention clearly was to support more "ELF
versions" at some point. But (luckily) that never happened and I doubt
there will ever be a different (incompatible) ELF version that we'll
have to support. So in the end this will always be EV_CURRENT == 1. But
the function has to be called to make the rest of the library work.

I think this works and is fine. There will most likely never be real
contention calling elf_version because normally a program just calls it
once at the start.

But have you thought about using some atomic operation here instead?

Cheers,

Mark
  
Heather McIntyre Oct. 17, 2023, 7:05 p.m. UTC | #2
John and I discussed that atomic_compare_exchange_strong could have been
used here. I see that this has been pushed to the main branch, but I can
make the change to the atomic operation if you think that is a better
option.

Best,
Heather

On Tue, Oct 10, 2023 at 9:00 AM Mark Wielaard <mark@klomp.org> wrote:

> Hi Heather,
>
> On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard wrote:
> > From: Heather McIntyre <hsm2@rice.edu>
> >
> >       * elf_version.c (version_once): Define once.
> >       (initialize_version): New static function.
> >       (elf_version): Use initialize_version version_once.
> >
> > Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
> > Signed-off-by: Mark Wielaard <mark@klomp.org>
> > ---
> >  libelf/elf_version.c | 11 ++++++++++-
> >  1 file changed, 10 insertions(+), 1 deletion(-)
> >
> > diff --git a/libelf/elf_version.c b/libelf/elf_version.c
> > index 6ec534ab..8296bb65 100644
> > --- a/libelf/elf_version.c
> > +++ b/libelf/elf_version.c
> > @@ -32,12 +32,21 @@
> >  #endif
> >
> >  #include <libelfP.h>
> > +#include <pthread.h>
> >
> > +/* Multiple threads may initialize __libelf_version.
> > +   pthread_once() ensures that __libelf_version is initialized only
> once. */
> > +once_define(static, version_once);
> >
> >  /* Currently selected version.  Should be EV_CURRENT.
> >     Will be EV_NONE if elf_version () has not been called yet.  */
> >  unsigned int __libelf_version = EV_NONE;
> >
> > +static void initialize_version(void)
> > +{
> > +  __libelf_version = EV_CURRENT;
> > +}
> > +
> >  unsigned int
> >  elf_version (unsigned int version)
> >  {
> > @@ -49,7 +58,7 @@ elf_version (unsigned int version)
> >        /* Phew, we know this version.  */
> >
> >        /* Signal that the version is now initialized.  */
> > -      __libelf_version = EV_CURRENT;
> > +      once(version_once, initialize_version);
> >
> >        /* And return the last (or initial) version.  */
> >        return EV_CURRENT;
>
> This is an odd function. The intention clearly was to support more "ELF
> versions" at some point. But (luckily) that never happened and I doubt
> there will ever be a different (incompatible) ELF version that we'll
> have to support. So in the end this will always be EV_CURRENT == 1. But
> the function has to be called to make the rest of the library work.
>
> I think this works and is fine. There will most likely never be real
> contention calling elf_version because normally a program just calls it
> once at the start.
>
> But have you thought about using some atomic operation here instead?
>
> Cheers,
>
> Mark
>
  
Mark Wielaard Oct. 19, 2023, 9 p.m. UTC | #3
Hi Heather,

On Tue, Oct 17, 2023 at 02:05:58PM -0500, Heather McIntyre wrote:
> John and I discussed that atomic_compare_exchange_strong could have been
> used here. I see that this has been pushed to the main branch, but I can
> make the change to the atomic operation if you think that is a better
> option.

It probably doesn't matter for this function since it isn't used
often. I was just wondering whether you thought of using atomics here
since there are really just two states here (__libelf_version ==
EV_NONE or __libelf_version == EV_CURRENT).

I do have one question though. __libelf_version is checked in
elf_begin. Should that check be guarded by a lock too?

Thanks,

Mark
  

Patch

diff --git a/libelf/elf_version.c b/libelf/elf_version.c
index 6ec534ab..8296bb65 100644
--- a/libelf/elf_version.c
+++ b/libelf/elf_version.c
@@ -32,12 +32,21 @@ 
 #endif
 
 #include <libelfP.h>
+#include <pthread.h>
 
+/* Multiple threads may initialize __libelf_version.
+   pthread_once() ensures that __libelf_version is initialized only once. */
+once_define(static, version_once);
 
 /* Currently selected version.  Should be EV_CURRENT.
    Will be EV_NONE if elf_version () has not been called yet.  */
 unsigned int __libelf_version = EV_NONE;
 
+static void initialize_version(void)
+{
+  __libelf_version = EV_CURRENT;
+}
+
 unsigned int
 elf_version (unsigned int version)
 {
@@ -49,7 +58,7 @@  elf_version (unsigned int version)
       /* Phew, we know this version.  */
 
       /* Signal that the version is now initialized.  */
-      __libelf_version = EV_CURRENT;
+      once(version_once, initialize_version);
 
       /* And return the last (or initial) version.  */
       return EV_CURRENT;