public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Jonathon Anderson <jma14@rice.edu>
To: Mark Wielaard <mark@klomp.org>
Cc: elfutils-devel@sourceware.org, Srdan Milakovic <sm108@rice.edu>
Subject: Re: [PATCH] lib + libdw: Add and use a concurrent version of the dynamic-size hash table.
Date: Fri, 08 Nov 2019 15:29:00 -0000	[thread overview]
Message-ID: <1573226934.2134.0@rice.edu> (raw)
In-Reply-To: <e762f5d48fe7c70a924ac919239902188309a30f.camel@klomp.org>



On Fri, Nov 8, 2019 at 15:07, Mark Wielaard <mark@klomp.org> wrote:
> Hi,
> 
> On Thu, 2019-11-07 at 09:24 -0600, Jonathon Anderson wrote:
>>  On Thu, Nov 7, 2019 at 12:07, Mark Wielaard <mark@klomp.org 
>> <mailto:mark@klomp.org>> wrote:
>>  > Looking at the difference between the previous version and this 
>> one,
>>  > it
>>  > incorporates my simplification of FIND and lookup functions. And 
>> fixes
>>  > it by making insert_helper consistently return -1 when the value 
>> was
>>  > already inserted (hash == hval). And it fixes an issue where we 
>> were
>>  > using the the table entry val_ptr instead of the hashval as hash 
>> (was
>>  > that the typo? it didn't look harmless).
>> 
>>  Yep, those are the changes (plus the Sig8 patch). That typo was
>>  harmless because hash would be overwritten before its next use (or 
>> just
>>  unused), now with the (hash == hval) clause its always read so the 
>> typo
>>  is fixed.
> 
> Thanks for explaining. I have now finally pushed this to master.
> 
>>  Regarding the Sig8 table: I took a close look, and at the moment its
>>  use is in an area that isn't thread-safe anyway (in particular,
>>  __libdw_intern_next_unit). Depending on how that is parallelized 
>> there
>>  might be an issue (if its just wrapped with a separate mutex a 
>> thread
>>  might "miss" a CU if its not already in the table), but since that
>>  region would need inspection at that time anyway I'm fine with 
>> either
>>  option.
> 
> I still kept the code to handle the Sig8 table with the new 
> concurrent-
> safe code, since I think it is better if we use the new code always
> (even in the single threaded case).
> 
> So to fix this we do need some mutex to protect the binary search tree
> when calling tsearch/tfind? Or do you see other issues too?

The search tree can be handled with a mutex, the issue is with 
next_{tu,cu}_offset and the general logic of the function. As an 
example: suppose two threads look up in the Sig8 for A and see that its 
not currently present. They'll both use __libdw_intern_next_unit to 
load CUs (or units, I suppose) until they either find it or run out of 
units.

If the entirety of intern_next_unit is wrapped in a mutex, one of the 
two will load in the missing entry and finish, while the other has 
"missed" it and will keep going until no units remain. The easy 
solution is to have the threads check the table again on next_unit 
failure for whether the entry has been added, but that incurs a 
large-ish overhead for the constant reprobing. The easiest way around 
that is to add an interface on the Sig8 table that returns a "probe" on 
lookup failure that can be continued with only a handful of atomics 
(essentially saving state from the first find). The downside to this 
approach is that unit parsing is fully serialized.

If the next_*_offset is handled with a separate mutex or as an atomic 
(say, using fetch_add), the same issue occurs but without the mutex 
there's no guarantee that another thread isn't currently parsing and 
will write the entry soon, so the easy solution doesn't work. Since the 
Sig8 key is only known after the parsing is complete, we can't even 
insert a "in progress" entry. One solution is to allow for duplicate 
parsing (but then next_*_offset would have to be updated *after* 
Sig8_Hash_insert), another is to use a condition variable on whether 
all the units have been parsed (so threads that don't find what they're 
looking for would block until its certain that it doesn't exist).

Both are viable directions, but neither are trivial.

> 
>>  This isn't an issue for Dwarf_Abbrev, the worst that can happen 
>> there
>>  is a duplicate alloc and parsing (as long as the DWARF doesn't have
>>  erroneous abbrev entries, if it does we would need to thread-safe 
>> the
>>  error handling too).
> 
> Unfortunately we don't always control the data, so bad abbrev entries
> could happen. The extra alloc wouldn't really "leak" because it would
> be freed with the Dwarf. So I am not too concerned about that. Is that
> the worse that can happen in __libdw_getabbrev? When we goto invalid
> the Dwarf_Abbrev would indeed "leak", but it isn't really lost, it 
> will
> get cleaned up when the Dwarf is destroyed.

It wouldn't "leak," but it would be taking up space until the 
dwarf_end. Not that I mind (they're really small).

I'm thinking more of the case where the Abbrev_insert returns -1 (entry 
collision), in that case the new Abbrev would stick around until the 
dwarf_end.

> 
> Thanks,
> 
> Makr

  reply	other threads:[~2019-11-08 15:29 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-16 19:24 [PATCH] libdw: add thread-safety to dwarf_getabbrev() Jonathon Anderson
2019-08-21 11:16 ` Mark Wielaard
2019-08-21 14:21   ` Jonathon Anderson
2019-08-23 21:22     ` Mark Wielaard
     [not found]   ` <1566396518.5389.0@smtp.mail.rice.edu>
2019-08-23 18:25     ` Mark Wielaard
2019-08-23 22:36       ` Jonathon Anderson
2019-08-21 21:50 ` Mark Wielaard
2019-08-21 22:01   ` Mark Wielaard
2019-08-21 22:21   ` Jonathon Anderson
2019-08-23 21:26     ` Mark Wielaard
2019-08-24 23:24 ` Mark Wielaard
2019-08-25  1:11   ` Jonathon Anderson
2019-08-25 10:05     ` Mark Wielaard
2019-08-26  1:25       ` Jonathon Anderson
2019-08-26 13:18         ` Mark Wielaard
2019-08-26 13:37           ` Jonathon Anderson
2019-08-27  3:52             ` Jonathon Anderson
2019-08-29 13:16               ` Mark Wielaard
2019-08-29 13:16                 ` [PATCH 2/3] libdw: Rewrite the memory handler to be thread-safe Mark Wielaard
2019-10-21 16:13                   ` Mark Wielaard
2019-10-21 16:28                     ` Jonathon Anderson
2019-10-21 18:00                       ` Mark Wielaard
2019-10-24 16:47                         ` Mark Wielaard
2019-08-29 13:16                 ` [PATCH 1/3] Add some supporting framework for C11-style atomics Mark Wielaard
2019-10-22 16:31                   ` Mark Wielaard
2019-08-29 13:16                 ` [PATCH 3/3] lib + libdw: Add and use a concurrent version of the dynamic-size hash table Mark Wielaard
2019-10-25 23:50                   ` Mark Wielaard
2019-10-26  4:11                     ` Jonathon Anderson
2019-10-27 16:13                       ` Mark Wielaard
2019-10-27 17:49                         ` Jonathon Anderson
2019-10-28 14:08                           ` Mark Wielaard
2019-10-28 20:12                         ` Mark Wielaard
2019-11-04 16:21                           ` Mark Wielaard
2019-11-04 16:19                       ` Mark Wielaard
2019-11-04 17:03                         ` [PATCH] " Jonathon Anderson
2019-11-07 11:07                           ` Mark Wielaard
2019-11-07 15:25                             ` Jonathon Anderson
2019-11-08 14:07                               ` Mark Wielaard
2019-11-08 15:29                                 ` Jonathon Anderson [this message]
2019-11-10 23:24                                   ` Mark Wielaard
2019-11-11 23:38                                     ` Jonathon Anderson
2019-11-12 21:45                                       ` Mark Wielaard
2019-10-26 10:54               ` [PATCH] libdw: add thread-safety to dwarf_getabbrev() Florian Weimer
2019-10-26 12:06                 ` Mark Wielaard
2019-10-26 16:14                   ` Florian Weimer
2019-10-26 16:45                     ` Jonathon Anderson
2019-10-26 16:50                       ` Florian Weimer
2019-10-26 22:53                         ` Mark Wielaard
2019-10-27  8:59                           ` Florian Weimer
2019-10-27 18:11                             ` Jonathon Anderson
2019-10-27 18:44                               ` Florian Weimer
2019-10-26 22:50                       ` Mark Wielaard
2019-10-27  0:56                         ` Jonathon Anderson
2019-10-28 13:26                           ` Mark Wielaard
2019-10-28 15:32                             ` Jonathon Anderson

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=1573226934.2134.0@rice.edu \
    --to=jma14@rice.edu \
    --cc=elfutils-devel@sourceware.org \
    --cc=mark@klomp.org \
    --cc=sm108@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).