public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: Heather McIntyre <hsm2@rice.edu>
Cc: elfutils-devel@sourceware.org
Subject: Re: [PATCH 11/16] libdw: Add locking around __libdw_dieabbrev for dwarf_hasattr
Date: Fri, 20 Oct 2023 00:06:55 +0200	[thread overview]
Message-ID: <20231019220655.GE14066@gnu.wildebeest.org> (raw)
In-Reply-To: <CAK-+vz3EcFrJFCO6bu4fZ438+WLS7RM4dLDXkysTcu5bERPTGA@mail.gmail.com>

Hi Heather,

On Tue, Oct 17, 2023 at 02:57:39PM -0500, Heather McIntyre wrote:
> I see now that this is incomplete considering the other places that also
> call this function. I do agree that global locking may be heavy if 1)
> implemented in all of these locations, or 2) implemented directly in
> __libdw_dieabbrev. We could use atomics here directly in __libdw_dieabbrev.
> I have given this a try and it is currently passing all tests, including
> the new ones I added for data race detection.
> 
> I know you mentioned that taking any pthread lock at all might be a big
> overhead, but since I implemented a per dwarf struct lock, would using that
> be a possibility? Assuming multiple calls to __libdw_dieabbrev will be
> working on different dwarf objects.

I have been thinking about this issue and I think we made a mistake in
designing how a Dwarf_Die is lazy initialized. The abbrev field of a
Dwarf_Die is only set when needed by calling __libdw_dieabbrev, which
means we need some kind of locking or atomic swapping whenever we try
to use that field. I assume the idea originally was that calling
__libdw_dieabbrev is fairly "heavy" (it is, potentially reading the
whole .debug_abbrev for the CU). So we try to postpone it till it is
really needed.

But in practice it is always needed. Without the abbrev field set you
can just call dwarf_dieoffset, dwarf_cuoffset, dwarf_diecu and
dwarf_getabbrev. In theory you could avoid adding the abbrev for the
initial CU DIE for a Dwarf_CU when you are iterating over all CUs and
know you don't need the CU without inspecting the initial CU DIE. But
the Dwarf_Abbrev_Hash for the Dwarf_CU will already be
initialized. And normally the abbrev for the first DIE will also be
the first abbrev, so searching for it should be really quick.

So I think setting the Dwarf_Die abbrev field lazy is not really
helpful and makes the code needlessly complex. If we set the abbrev
field when a Dwarf_Die is created we can simplify the code and don't
need all this locking when we just want to access the field.

This of course is still a lot of coding, we'll have to check every
place that initializes a new Dwarf_Die. Which will have to call
__libdw_dieabbrev directly. But I think that will not need any extra
locking because the Dwarf_Abbrev_Hash used is already thread-safe (it
was written by Srđan Milaković also from Rice University).

What do you think?

Cheers,

Mark

  reply	other threads:[~2023-10-19 22:06 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-08 17:07 [PATCH] Fix thread-safety for elfutils Heather McIntyre
2023-08-21 22:08 ` John Mellor-Crummey
2023-08-25 14:10   ` Mark Wielaard
2023-10-10 13:40 ` Mark Wielaard
2023-10-10 13:42   ` [PATCH 01/16] lib: Add new once_define and once macros to eu-config.h Mark Wielaard
2023-10-10 13:42     ` [PATCH 02/16] libelf: Make elf_version thread-safe Mark Wielaard
2023-10-10 14:00       ` Mark Wielaard
2023-10-17 19:05         ` Heather McIntyre
2023-10-19 21:00           ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 03/16] libelf: Fix deadlock in __libelf_readall Mark Wielaard
2023-10-10 15:06       ` Mark Wielaard
2023-10-17 19:11         ` Heather McIntyre
2023-11-09 13:26           ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 04/16] libelf: Fix deadlock in elf_cntl Mark Wielaard
2023-10-10 15:23       ` Mark Wielaard
2023-10-17 19:14         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 05/16] libelf: Fix elf_end deadlock Mark Wielaard
2023-10-10 15:28       ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 06/16] libelf: Make elf32_getchdr and elf64_getchdr thread-safe Mark Wielaard
2023-10-10 16:28       ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 07/16] lib: Add eu_tsearch and eu_tfind Mark Wielaard
2023-10-10 16:51       ` Mark Wielaard
2023-10-17 20:52         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 08/16] libcpu: Change calls for tsearch/tfind to eu_tsearch/eu_tfind Mark Wielaard
2023-10-10 21:10       ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 09/16] src: Use eu-search in nm and findtextrel Mark Wielaard
2023-10-10 21:25       ` Mark Wielaard
2023-10-17 19:20         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 10/16] libdw: make dwarf_getalt thread-safe Mark Wielaard
2023-10-10 22:02       ` Mark Wielaard
2023-10-17 19:25         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 11/16] libdw: Add locking around __libdw_dieabbrev for dwarf_hasattr Mark Wielaard
2023-10-11 15:10       ` Mark Wielaard
2023-10-17 19:57       ` Heather McIntyre
2023-10-19 22:06         ` Mark Wielaard [this message]
2023-10-10 13:42     ` [PATCH 12/16] libdw: Make libdw_find_split_unit thread-safe Mark Wielaard
2023-10-11 17:17       ` Mark Wielaard
2023-10-17 20:01         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 13/16] libdw: Make libdw_findcu thread-safe Mark Wielaard
2023-10-12 22:02       ` Mark Wielaard
2023-10-17 20:10         ` Heather McIntyre
2023-10-10 13:42     ` [PATCH 14/16] libdw,libdwfl: Use eu-search for thread-safety Mark Wielaard
2023-10-12 22:05       ` Mark Wielaard
2023-10-10 13:42     ` [PATCH 15/16] tests: Add eu-search tests Mark Wielaard
2023-10-13 14:38       ` Mark Wielaard
2023-10-10 13:43     ` [PATCH 16/16] configure: No longer mark --enable-thread-safety as EXPERIMENTAL Mark Wielaard
2023-10-12 22:09       ` Mark Wielaard
2023-10-10 13:54     ` [PATCH 01/16] lib: Add new once_define and once macros to eu-config.h Mark Wielaard
2023-10-14 15:39   ` [PATCH] Fix thread-safety for elfutils Mark Wielaard
2023-10-14 18:29     ` Heather McIntyre
2023-10-17 15:04       ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231019220655.GE14066@gnu.wildebeest.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=hsm2@rice.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).