public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2] i386: Use pthread_barrier for synchronization on tst-bz21269
Date: Wed, 22 Feb 2023 20:08:57 -0500	[thread overview]
Message-ID: <xnsfex89xy.fsf@greed.delorie.com> (raw)
In-Reply-To: <20230221192640.3650934-1-adhemerval.zanella@linaro.org>

Adhemerval Zanella <adhemerval.zanella@linaro.org> writes:

> -   - C11 atomics instead of plain access.
> +   - Use pthread_barrier instead of atomic and futexes.

Ok.

> -#include <stdatomic.h>
> -
>  #include <asm/ldt.h>
> -#include <linux/futex.h>

Ok.

> +#include <support/xsignal.h>
> +
> +#define NITER 5

Ok.

> -static int
> -futex (int *uaddr, int futex_op, int val, void *timeout, int *uaddr2,
> -	int val3)
> -{
> -  return syscall (SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
> -}
> -

Ok.

> -  TEST_VERIFY_EXIT (sigaction (sig, &sa, 0) == 0);
> +  xsigaction (sig, &sa, 0);

Ok.

> -/* Possible values of futex:
> -   0: thread is idle.
> -   1: thread armed.
> -   2: thread should clear LDT entry 0.
> -   3: thread should exit.  */
> -static atomic_uint ftx;
> +static pthread_barrier_t barrier;

Ok.

> -  while (1)
> +  for (int i = 0; i < NITER; i++)

Matches the loop in main, ok.

> -      futex ((int *) &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
> -      while (atomic_load (&ftx) != 2)
> -	{
> -	  if (atomic_load (&ftx) >= 3)
> -	    return NULL;
> -	}
> +      xpthread_barrier_wait (&barrier);

First barrier, ok.  Both threads start here

>        /* clear LDT entry 0.  */
>        const struct user_desc desc = { 0 };
>        xmodify_ldt (1, &desc, sizeof (desc));

While we're doing this, main thread is also modifying its ldt?

> -      /* If ftx == 2, set it to zero,  If ftx == 100, quit.  */
> -      if (atomic_fetch_add (&ftx, -2) != 2)
> -	return NULL;
> +      /* Wait for 'ss' set in main thread.  */
> +      xpthread_barrier_wait (&barrier);

Second barrier.  Ok.  After this, the main thread sets SS...

I think the barriers are still in the wrong locations.

the original code did this (wrt main):

* wait for thread to be idle
* xmodify_ldt()
* set thread to "armed"
* set SS to something "funny"
* tell thread to run (=2)
- thread calls xmodify_ltd and goes idle (=0)
* wait for thread idle

Then, it sets SS *before* the thread clears the LDT.

So I think the barriers need to be placed like this:

  -main-  -thread-

for (;;) {
  xmodify_ldt (set it)  -
  set SS       -
  -- barrier --
  -         xmodify_ldt (clear it)
  -- barrier --
  fail        -
}


  reply	other threads:[~2023-02-23  1:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-21 19:26 Adhemerval Zanella
2023-02-23  1:08 ` DJ Delorie [this message]
2023-02-24 15:45   ` Adhemerval Zanella Netto

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=xnsfex89xy.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /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).