[08/10,v3] libdw: Make libdw_findcu thread-safe

Message ID 20240802233847.690564-8-amerey@redhat.com
State Committed
Delegated to: Mark Wielaard
Headers
Series [01/10,v3] libelf: Fix deadlock in __libelf_readall |

Commit Message

Aaron Merey Aug. 2, 2024, 11:38 p.m. UTC
  From: Heather McIntyre <hsm2@rice.edu>

	* libdw/libdw_findcu.c (__libdw_findcu): Add locking.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Aaron Merey <amerey@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>

---
v3 changes:
Fix indentation and move rwlock_init calls to other patches in this
series.

 libdw/libdw_findcu.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
  

Comments

Mark Wielaard Aug. 16, 2024, 9:38 p.m. UTC | #1
Hi,

On Fri, Aug 02, 2024 at 07:38:07PM -0400, Aaron Merey wrote:
> From: Heather McIntyre <hsm2@rice.edu>
> 
> 	* libdw/libdw_findcu.c (__libdw_findcu): Add locking.
> 
> Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
> Signed-off-by: Aaron Merey <amerey@redhat.com>
> Signed-off-by: Mark Wielaard <mark@klomp.org>
> 
> ---
> v3 changes:
> Fix indentation and move rwlock_init calls to other patches in this
> series.

So this is basically a lock around __libdw_intern_next_unit. But there
is also a call to __libdw_intern_next_unit from dwarf_formref_die. So
there should also be a lock there?

>  libdw/libdw_findcu.c | 38 +++++++++++++++++++++++++-------------
>  1 file changed, 25 insertions(+), 13 deletions(-)
> 
> diff --git a/libdw/libdw_findcu.c b/libdw/libdw_findcu.c
> index c74e895e..bbbbee5d 100644
> --- a/libdw/libdw_findcu.c
> +++ b/libdw/libdw_findcu.c
> @@ -245,27 +245,39 @@ __libdw_findcu (Dwarf *dbg, Dwarf_Off start, bool v4_debug_types)
>    /* Maybe we already know that CU.  */
>    struct Dwarf_CU fake = { .start = start, .end = 0 };
>    struct Dwarf_CU **found = eu_tfind (&fake, tree, findcu_cb);
> +  struct Dwarf_CU *result = NULL;
>    if (found != NULL)
>      return *found;
>  
> +  rwlock_wrlock (dbg->dwarf_lock);
> +
>    if (start < *next_offset)
> +    __libdw_seterrno (DWARF_E_INVALID_DWARF);
> +  else
>      {
> -      __libdw_seterrno (DWARF_E_INVALID_DWARF);
> -      return NULL;
> -    }
> +      /* No.  Then read more CUs.  */
> +      while (1)
> +	{
> +	  struct Dwarf_CU *newp
> +	    = __libdw_intern_next_unit (dbg, v4_debug_types);
>  
> -  /* No.  Then read more CUs.  */
> -  while (1)
> -    {
> -      struct Dwarf_CU *newp = __libdw_intern_next_unit (dbg, v4_debug_types);
> -      if (newp == NULL)
> -	return NULL;
> +	  if (newp == NULL)
> +	    {
> +	      result = NULL;
> +	      break;
> +	    }
>  
> -      /* Is this the one we are looking for?  */
> -      if (start < *next_offset || start == newp->start)
> -	return newp;
> +	  /* Is this the one we are looking for?  */
> +	  if (start < *next_offset || start == newp->start)
> +	    {
> +	      result = newp;
> +	      break;
> +	    }
> +	}
>      }
> -  /* NOTREACHED */
> +
> +  rwlock_unlock (dbg->dwarf_lock);
> +  return result;
>  }
>  
>  struct Dwarf_CU *
> -- 
> 2.45.2
>
  
Aaron Merey Aug. 21, 2024, 12:38 a.m. UTC | #2
Hi Mark,

On Fri, Aug 16, 2024 at 5:38 PM Mark Wielaard <mark@klomp.org> wrote:
>
> Hi,
>
> On Fri, Aug 02, 2024 at 07:38:07PM -0400, Aaron Merey wrote:
> > From: Heather McIntyre <hsm2@rice.edu>
> >
> >       * libdw/libdw_findcu.c (__libdw_findcu): Add locking.
> >
> > Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
> > Signed-off-by: Aaron Merey <amerey@redhat.com>
> > Signed-off-by: Mark Wielaard <mark@klomp.org>
> >
> > ---
> > v3 changes:
> > Fix indentation and move rwlock_init calls to other patches in this
> > series.
>
> So this is basically a lock around __libdw_intern_next_unit. But there
> is also a call to __libdw_intern_next_unit from dwarf_formref_die. So
> there should also be a lock there?

The locking around __libdw_intern_next_unit can be found in commit
2630bce74 (libdw: Make libdw_find_split_unit thread-safe).

This patch was pushed as commit a0a2996d15.

Aaron
  

Patch

diff --git a/libdw/libdw_findcu.c b/libdw/libdw_findcu.c
index c74e895e..bbbbee5d 100644
--- a/libdw/libdw_findcu.c
+++ b/libdw/libdw_findcu.c
@@ -245,27 +245,39 @@  __libdw_findcu (Dwarf *dbg, Dwarf_Off start, bool v4_debug_types)
   /* Maybe we already know that CU.  */
   struct Dwarf_CU fake = { .start = start, .end = 0 };
   struct Dwarf_CU **found = eu_tfind (&fake, tree, findcu_cb);
+  struct Dwarf_CU *result = NULL;
   if (found != NULL)
     return *found;
 
+  rwlock_wrlock (dbg->dwarf_lock);
+
   if (start < *next_offset)
+    __libdw_seterrno (DWARF_E_INVALID_DWARF);
+  else
     {
-      __libdw_seterrno (DWARF_E_INVALID_DWARF);
-      return NULL;
-    }
+      /* No.  Then read more CUs.  */
+      while (1)
+	{
+	  struct Dwarf_CU *newp
+	    = __libdw_intern_next_unit (dbg, v4_debug_types);
 
-  /* No.  Then read more CUs.  */
-  while (1)
-    {
-      struct Dwarf_CU *newp = __libdw_intern_next_unit (dbg, v4_debug_types);
-      if (newp == NULL)
-	return NULL;
+	  if (newp == NULL)
+	    {
+	      result = NULL;
+	      break;
+	    }
 
-      /* Is this the one we are looking for?  */
-      if (start < *next_offset || start == newp->start)
-	return newp;
+	  /* Is this the one we are looking for?  */
+	  if (start < *next_offset || start == newp->start)
+	    {
+	      result = newp;
+	      break;
+	    }
+	}
     }
-  /* NOTREACHED */
+
+  rwlock_unlock (dbg->dwarf_lock);
+  return result;
 }
 
 struct Dwarf_CU *