From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4737B3858D20 for ; Mon, 20 Feb 2023 22:55:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4737B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676933724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=TKTbETs+1P20+XnvSPrajbr+eDLKfCp3x7Pdnp2kn+k=; b=hWRmhUDGKtuKVhvhrgQoqY/YcOT8QenOiCLexGeX1kWXjEVEa28Nv9yZuN+yaga4I5h/Y+ IF1XT0m/zZLLqWd+MINAjtaQyy3uNi9NJkJaxdRxPaIe7U/aT8a1bWxRDf0S9DMeQnafSO Ht4hlYbt6/D0o9JQhdS9nKjJrpAGOos= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-637-KodR6eM4Ml-2VRZ2j0xpLA-1; Mon, 20 Feb 2023 17:55:16 -0500 X-MC-Unique: KodR6eM4Ml-2VRZ2j0xpLA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9A3DC3C0DDAB; Mon, 20 Feb 2023 22:55:15 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.115]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7DF7A18EC6; Mon, 20 Feb 2023 22:55:15 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 31KMtEmw866893; Mon, 20 Feb 2023 17:55:14 -0500 From: DJ Delorie To: Adhemerval Zanella Cc: libc-alpha@sourceware.org, fweimer@redhat.com Subject: Re: [PATCH] i386: Use pthread_barrier for synchronization on tst-bz21269 In-Reply-To: <20230215124354.2069895-1-adhemerval.zanella@linaro.org> Date: Mon, 20 Feb 2023 17:55:14 -0500 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The resulting test has two calls to pthread_barrier_wait() in the subthread but three calls in main(). These will quickly get out of sync. Adhemerval Zanella via Libc-alpha writes: > - - C11 atomics instead of plain access. > + - Use pthread_barrier instead of atomic and futexes. Is this true relative to the original testcase? Still, merely a comment, so OK. > -#include > - > #include > -#include Ok. > +#include 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); > -} We remove all calls to futex, so no longer need this. 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; Ok. > +static pthread_barrier_t barrier; New, ok. > static void * > threadproc (void *ctx) > { > - while (1) > + for (int i = 0; i < 5; i++) This matches the loop in main. Ok. In the future, a #define loop limit would be appropriate, to prevent these getting out of sync. Or a comment that it has to match main(). > { > - 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. > /* clear LDT entry 0. */ > const struct user_desc desc = { 0 }; > xmodify_ldt (1, &desc, sizeof (desc)); Leave the stuff the thread is actually testing ;-) > - /* 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. > } > + > + return NULL; Ok, moved from above. > } > > > @@ -180,6 +162,8 @@ do_test (void) > /* Some kernels send SIGBUS instead. */ > xsethandler (SIGBUS, sigsegv_handler, 0); > > + xpthread_barrier_init (&barrier, NULL, 2); Initialize; must have two callers. Ok - main and thread both call. > thread = xpthread_create (0, threadproc, 0); > > asm volatile ("mov %%ss, %0" : "=rm" (orig_ss)); > @@ -190,8 +174,7 @@ do_test (void) > continue; > > /* Make sure the thread is ready after the last test. */ > - while (atomic_load (&ftx) != 0) > - ; > + xpthread_barrier_wait (&barrier); First barrier, ok. > struct user_desc desc = { > .entry_number = 0, > @@ -207,28 +190,21 @@ 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); Second barrier, ok. > asm volatile ("mov %0, %%ss" : : "r" (0x7)); > > - /* Fire up thread modify_ldt call. */ > - atomic_store (&ftx, 2); > - > - while (atomic_load (&ftx) != 0) > - ; > + xpthread_barrier_wait (&barrier); Third barrier? This puts main and the thread out of sync. > /* 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); > - Ok. > xpthread_join (thread); > > + xpthread_barrier_destroy (&barrier); > + Ok.