[v2] i386: Use pthread_barrier for synchronization on tst-bz21269
Checks
Context |
Check |
Description |
dj/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
dj/TryBot-32bit |
success
|
Build for i686
|
Commit Message
To improve the false negative in patchwork buildbot.
Checked on i686-linux-gnu.
---
sysdeps/unix/sysv/linux/i386/tst-bz21269.c | 62 +++++++---------------
1 file changed, 19 insertions(+), 43 deletions(-)
Comments
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 -
}
On 22/02/23 22:08, DJ Delorie wrote:
> 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 -
> }
>
Ok I thint it works as well. I will update the patch.
@@ -20,16 +20,13 @@
more specifically in do_multicpu_tests function. The main changes
are:
- - C11 atomics instead of plain access.
+ - Use pthread_barrier instead of atomic and futexes.
- Remove x86_64 support which simplifies the syscall handling
and fallbacks.
- Replicate only the test required to trigger the issue for the
BZ#21269. */
-#include <stdatomic.h>
-
#include <asm/ldt.h>
-#include <linux/futex.h>
#include <setjmp.h>
#include <signal.h>
@@ -40,6 +37,9 @@
#include <support/xunistd.h>
#include <support/check.h>
#include <support/xthread.h>
+#include <support/xsignal.h>
+
+#define NITER 5
static int
xset_thread_area (struct user_desc *u_info)
@@ -55,13 +55,6 @@ xmodify_ldt (int func, const void *ptr, unsigned long bytecount)
TEST_VERIFY_EXIT (syscall (SYS_modify_ldt, 1, ptr, bytecount) == 0);
}
-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);
-}
-
static void
xsethandler (int sig, void (*handler)(int, siginfo_t *, void *), int flags)
{
@@ -69,7 +62,7 @@ xsethandler (int sig, void (*handler)(int, siginfo_t *, void *), int flags)
sa.sa_sigaction = handler;
sa.sa_flags = SA_SIGINFO | flags;
TEST_VERIFY_EXIT (sigemptyset (&sa.sa_mask) == 0);
- TEST_VERIFY_EXIT (sigaction (sig, &sa, 0) == 0);
+ xsigaction (sig, &sa, 0);
}
static jmp_buf jmpbuf;
@@ -123,33 +116,24 @@ setup_low_user_desc (void)
low_user_desc_clear->seg_not_present = 1;
}
-/* 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;
static void *
threadproc (void *ctx)
{
- while (1)
+ for (int i = 0; i < NITER; i++)
{
- 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);
/* clear LDT entry 0. */
const struct user_desc desc = { 0 };
xmodify_ldt (1, &desc, sizeof (desc));
- /* 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);
}
+
+ return NULL;
}
@@ -180,18 +164,19 @@ do_test (void)
/* Some kernels send SIGBUS instead. */
xsethandler (SIGBUS, sigsegv_handler, 0);
+ xpthread_barrier_init (&barrier, NULL, 2);
+
thread = xpthread_create (0, threadproc, 0);
asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
- for (int i = 0; i < 5; i++)
+ for (int i = 0; i < NITER; i++)
{
if (sigsetjmp (jmpbuf, 1) != 0)
continue;
/* Make sure the thread is ready after the last test. */
- while (atomic_load (&ftx) != 0)
- ;
+ xpthread_barrier_wait (&barrier);
struct user_desc desc = {
.entry_number = 0,
@@ -207,28 +192,19 @@ do_test (void)
xmodify_ldt (0x11, &desc, sizeof (desc));
- /* Arm the thread. */
- ftx = 1;
- futex ((int*) &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
+ xpthread_barrier_wait (&barrier);
asm volatile ("mov %0, %%ss" : : "r" (0x7));
- /* Fire up thread modify_ldt call. */
- atomic_store (&ftx, 2);
-
- while (atomic_load (&ftx) != 0)
- ;
-
/* On success, modify_ldt will segfault us synchronously and we will
escape via siglongjmp. */
support_record_failure ();
}
- atomic_store (&ftx, 100);
- futex ((int*) &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
-
xpthread_join (thread);
+ xpthread_barrier_destroy (&barrier);
+
return 0;
}