linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Rich Felker <dalias@libc.org>
Cc: carlos <carlos@redhat.com>, Florian Weimer <fweimer@redhat.com>,
	Joseph Myers <joseph@codesourcery.com>,
	Szabolcs Nagy <szabolcs.nagy@arm.com>,
	libc-alpha <libc-alpha@sourceware.org>,
	Thomas Gleixner <tglx@linutronix.de>, Ben Maurer <bmaurer@fb.com>,
	Peter Zijlstra <peterz@infradead.org>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Boqun Feng <boqun.feng@gmail.com>,
	Will Deacon <will.deacon@arm.com>,
	Dave Watson <davejwatson@fb.com>, Paul Turner <pjt@google.com>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	linux-api <linux-api@vger.kernel.org>
Subject: Re: [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation
Date: Thu, 22 Nov 2018 10:04:16 -0500 (EST)	[thread overview]
Message-ID: <782067422.9852.1542899056778.JavaMail.zimbra@efficios.com> (raw)
In-Reply-To: <20181122143603.GD23599@brightrain.aerifal.cx>

----- On Nov 22, 2018, at 9:36 AM, Rich Felker dalias@libc.org wrote:

> On Wed, Nov 21, 2018 at 01:39:32PM -0500, Mathieu Desnoyers wrote:
>> Register rseq(2) TLS for each thread (including main), and unregister
>> for each thread (excluding main). "rseq" stands for Restartable
>> Sequences.
> 
> Maybe I'm missing something obvious, but "unregister" does not seem to
> be a meaningful operation. Can you clarify what it's for?

There are really two ways rseq TLS can end up being unregistered: either
through an explicit call to the rseq "unregister", or when the OS frees the
thread's task struct.

You bring an interesting point here: do we need to explicitly unregister
rseq at thread exit, or can we leave that to the OS ?

The key thing to look for here is whether it's valid to access the
TLS area of the thread from preemption or signal delivery happening
at the very end of START_THREAD_DEFN. If it's OK to access it until
the very end of the thread lifetime, then we could do without an
explicit unregistration. However, if at any given point of the late
thread lifetime we end up in a situation where reading or writing to
that TLS area can cause corruption, then we need to carefully
unregister it before that memory is reclaimed/reused.

What we have below the current location for the __rseq_unregister_current_thread ()
call is as follows. I'm not all that convinced that it's valid to access the TLS
area up until __exit_thread () at the very end, especially after setting
setxid_futex back to 0.

Thoughts ?

  /* Unregister rseq TLS from kernel. */
  if (has_rseq && __rseq_unregister_current_thread ())
    abort();

  advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd,
                      pd->guardsize);

  /* If the thread is detached free the TCB.  */
  if (IS_DETACHED (pd))
    /* Free the TCB.  */
    __free_tcb (pd);
  else if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK))
    {
      /* Some other thread might call any of the setXid functions and expect
         us to reply.  In this case wait until we did that.  */
      do
        /* XXX This differs from the typical futex_wait_simple pattern in that
           the futex_wait condition (setxid_futex) is different from the
           condition used in the surrounding loop (cancelhandling).  We need
           to check and document why this is correct.  */
        futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE);
      while (pd->cancelhandling & SETXID_BITMASK);

      /* Reset the value so that the stack can be reused.  */
      pd->setxid_futex = 0;
    }

  /* We cannot call '_exit' here.  '_exit' will terminate the process.

     The 'exit' implementation in the kernel will signal when the
     process is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID
     flag.  The 'tid' field in the TCB will be set to zero.

     The exit code is zero since in case all threads exit by calling
     'pthread_exit' the exit status must be 0 (zero).  */
  __exit_thread ();

  /* NOTREACHED */

Thanks,

Mathieu


-- 
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

  reply	other threads:[~2018-11-22 15:04 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-21 18:39 [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation Mathieu Desnoyers
2018-11-21 18:39 ` [RFC PATCH v4 2/5] glibc: sched_getcpu(): use rseq cpu_id TLS on Linux Mathieu Desnoyers
2018-11-22 14:36 ` [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation Rich Felker
2018-11-22 15:04   ` Mathieu Desnoyers [this message]
2018-11-22 15:11     ` Florian Weimer
2018-11-22 15:17       ` Rich Felker
2018-11-22 15:21         ` Florian Weimer
2018-11-22 15:33           ` Mathieu Desnoyers
2018-11-22 15:44             ` Rich Felker
2018-11-22 16:24             ` Szabolcs Nagy
2018-11-22 18:35               ` Mathieu Desnoyers
2018-11-22 19:01                 ` Rich Felker
2018-11-22 19:36                   ` Mathieu Desnoyers
2018-11-22 15:14     ` Rich Felker
2018-11-22 15:47       ` Mathieu Desnoyers
2018-11-22 16:28         ` Florian Weimer
2018-11-22 16:47           ` Mathieu Desnoyers
2018-11-22 16:59             ` Florian Weimer
2018-11-22 17:10               ` Rich Felker
2018-11-23 13:10                 ` Florian Weimer
2018-11-23 14:28                   ` Rich Felker
2018-11-23 17:05                     ` Mathieu Desnoyers
2018-11-23 17:30                       ` Rich Felker
2018-11-23 17:39                         ` Florian Weimer
2018-11-23 17:44                           ` Rich Felker
2018-11-23 18:01                             ` Florian Weimer
2018-11-23 17:52                         ` Mathieu Desnoyers
2018-11-23 18:35                           ` Rich Felker
2018-11-23 21:09                             ` Mathieu Desnoyers
2018-11-26  8:28                               ` Florian Weimer
2018-11-26 15:51                                 ` Mathieu Desnoyers
2018-11-26 16:03                                   ` Florian Weimer
2018-11-26 17:10                                     ` Rich Felker
2018-11-26 19:22                                       ` Mathieu Desnoyers
2018-12-03 21:30                                         ` Mathieu Desnoyers
2018-11-26 16:30                                   ` Mathieu Desnoyers
2018-11-26 17:07                                     ` Rich Felker
2018-12-05 17:24                                       ` Mathieu Desnoyers
2018-11-26 11:56                               ` Szabolcs Nagy
2018-11-22 17:29               ` Mathieu Desnoyers
2018-11-23 13:29                 ` Florian Weimer

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=782067422.9852.1542899056778.JavaMail.zimbra@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bmaurer@fb.com \
    --cc=boqun.feng@gmail.com \
    --cc=carlos@redhat.com \
    --cc=dalias@libc.org \
    --cc=davejwatson@fb.com \
    --cc=fweimer@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=pjt@google.com \
    --cc=szabolcs.nagy@arm.com \
    --cc=tglx@linutronix.de \
    --cc=will.deacon@arm.com \
    /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 NNTP newsgroup(s).