Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 9 Feb 2019 22:40:45 +0100
From: Szabolcs Nagy <nsz@...t70.net>
To: musl@...ts.openwall.com
Cc: Alexey Izbyshev <izbyshev@...ras.ru>
Subject: Re: __synccall: deadlock and reliance on racy /proc/self/task

* Alexey Izbyshev <izbyshev@...ras.ru> [2019-02-09 21:33:32 +0300]:
> On 2019-02-09 19:21, Szabolcs Nagy wrote:
> > * Rich Felker <dalias@...c.org> [2019-02-08 13:33:57 -0500]:
> > > On Fri, Feb 08, 2019 at 09:14:48PM +0300, Alexey Izbyshev wrote:
> > > > On 2/7/19 9:36 PM, Rich Felker wrote:
> > > > >Does it work if we force two iterations of the readdir loop with no
> > > > >tasks missed, rather than just one, to catch the case of missed
> > > > >concurrent additions? I'm not sure. But all this makes me really
> > > > >uncomfortable with the current approach.
> > > >
> > > > I've tested with 0, 1, 2 and 3 retries of the main loop if miss_cnt
> > > > == 0. The test eventually failed in all cases, with 0 retries
> > > > requiring only a handful of iterations, 1 -- on the order of 100, 2
> > > > -- on the order of 10000 and 3 -- on the order of 100000.
> > > 
> > > Do you have a theory on the mechanism of failure here? I'm guessing
> > > it's something like this: there's a thread that goes unseen in the
> > > first round, and during the second round, it creates a new thread and
> > > exits itself. The exit gets seen (again, it doesn't show up in the
> > > dirents) but the new thread it created still doesn't. Is that right?
> > > 
> > > In any case, it looks like the whole mechanism we're using is
> > > unreliable, so something needs to be done. My leaning is to go with
> > > the global thread list and atomicity of list-unlock with exit.
> > 
> > yes that sounds possible, i added some instrumentation to musl
> > and the trace shows situations like that before the deadlock,
> > exiting threads can even cause old (previously seen) entries to
> > disappear from the dir.
> > 
> Thanks for the thorough instrumentation! Your traces confirm both my theory
> about the deadlock and unreliability of /proc/self/task.
> 
> I'd also done a very light instrumentation just before I got your email, but
> it took me a while to understand the output I got (see below).

the attached patch fixes the issue on my machine.
i don't know if this is just luck.

the assumption is that if /proc/self/task is read twice such that
all tids in it seem to be active and caught, then all the active
threads of the process are caught (no new threads that are already
started but not visible there yet)

> Now, about the strange output I mentioned. Consider one of the above
> fragments:
> --iter: 4
> exit 15977
> retry 0
> tid 15977
> tid 15978
> exit 15978
> retry 1
> tid 15978
> tgkill: ESRCH
> mismatch: tid 15979: 0 != 23517
> 
> Note that "tid 15978" is printed two times. Recall that it's printed only if
> we haven't seen it in the chain. But how it's possible that we haven't seen
> it *two* times? Since we waited on the futex the first time and we got the
> lock, the signal handler must have unlocked it. There is even a comment
> before futex() call:
> 
> /* Obtaining the lock means the thread responded. ESRCH
>  * means the target thread exited, which is okay too. */
> 
> If it the signal handler reached futex unlock code, it must have updated the
> chain, and we must see the tid in the chain on the next retry and don't
> print it.
> 
> Apparently, there is another reason of futex(FUTEX_LOCK_PI) success: the
> owner is exiting concurrently (which is also indicated by the subsequent
> failure of tgkill with ESRCH). So obtaining the lock doesn't necessarily
> mean that the owner responded: it may also mean that the owner is (about to
> be?) dead.

so tgkill succeeds but the target exits before handling the signal.
i'd expect ESRCH then not success from the futex.
interesting.

anyway i had to retry until there are no exiting threads in dir to
reliably fix the deadlock.

View attachment "0001-more-robust-synccall.patch" of type "text/x-diff" (1638 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.