From patchwork Tue Dec 11 14:25:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 1011117 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-98200-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="IhaTfQOq"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Dj0H11Nyz9s1c for ; Wed, 12 Dec 2018 01:25:34 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; q=dns; s=default; b=HBgj55lSsUZ7T00gWywpi5mgcUkuq rp6oXQf4TZTqzhmZNe8XUmkjLZSYYtVBxuoPyjX6vPz51zRfON/GJ0E/1IcupXqL NHlV6JzxCJlDVsDz5PCc42vHm9+Z3dnX1YPKcFusuPyjWM2gEwycr6LmywfPR8Se nx7whsP1hCoV1s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; s=default; bh=/Wf5SAnd1T3uAlGBdhAt1mDwus0=; b=Iha TfQOqnidZ+aYD1RgJrF1C1MtLdq+balL/uwNo/O+73LSEOUnvun1wKPNLiB6P+g4 STifTzjaow+d+s0fpGbOfQGG3QLJm6iyNAlcD96NHd9JxqeKYX3CJCRS1XoIKpw8 oxac6S9pQz6isQ2HOsPDYuygsUi325Ft2Y2gLbgQ= Received: (qmail 99173 invoked by alias); 11 Dec 2018 14:25:23 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 99051 invoked by uid 89); 11 Dec 2018 14:25:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.suse.de From: Andreas Schwab To: libc-alpha@sourceware.org Subject: [PATCH] Fix rwlock stall with PREFER_WRITER_NONRECURSIVE_NP (bug 23861) X-Yow: I'm into SOFTWARE! Date: Tue, 11 Dec 2018 15:25:12 +0100 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1.90 (gnu/linux) MIME-Version: 1.0 [BZ #23861] * nptl/pthread_rwlock_common.c (__pthread_rwlock_rdlock_full): Update expected value for __readers while waiting on PTHREAD_RWLOCK_RWAITING. * nptl/tst-rwlock-pwn.c: New file. * nptl/Makefile (tests): Add tst-rwlock-pwn. --- nptl/Makefile | 3 +- nptl/pthread_rwlock_common.c | 2 +- nptl/tst-rwlock-pwn.c | 82 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 nptl/tst-rwlock-pwn.c diff --git a/nptl/Makefile b/nptl/Makefile index 34ae830276..b01f2b0626 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -318,7 +318,8 @@ tests = tst-attr1 tst-attr2 tst-attr3 tst-default-attr \ tst-minstack-throw \ tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-cnd-timedwait tst-thrd-detach tst-mtx-basic tst-thrd-sleep \ - tst-mtx-recursive tst-tss-basic tst-call-once tst-mtx-timedlock + tst-mtx-recursive tst-tss-basic tst-call-once tst-mtx-timedlock \ + tst-rwlock-pwn tests-internal := tst-rwlock19 tst-rwlock20 \ tst-sem11 tst-sem12 tst-sem13 \ diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c index 6662536812..e55bf5431a 100644 --- a/nptl/pthread_rwlock_common.c +++ b/nptl/pthread_rwlock_common.c @@ -314,7 +314,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, harmless because the flag is just about the state of __readers, and all threads set the flag under the same conditions. */ - while ((atomic_load_relaxed (&rwlock->__data.__readers) + while (((r = atomic_load_relaxed (&rwlock->__data.__readers)) & PTHREAD_RWLOCK_RWAITING) != 0) { int private = __pthread_rwlock_get_private (rwlock); diff --git a/nptl/tst-rwlock-pwn.c b/nptl/tst-rwlock-pwn.c new file mode 100644 index 0000000000..57a5034515 --- /dev/null +++ b/nptl/tst-rwlock-pwn.c @@ -0,0 +1,82 @@ +/* Test rwlock with PREFER_WRITER_NONRECURSIVE_NP. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +/* We choose 10 iterations and 3 threads because this happens to be able + to trigger the stall today on modern hardware. */ +#define LOOPS 10 +#define NTHREADS 3 + +volatile int do_exit; +pthread_rwlockattr_t mylock_attr; +pthread_rwlock_t mylock; + +void * +run_loop (void *a) +{ + while (!do_exit) + { + if (random () & 1) + { + xpthread_rwlock_wrlock (&mylock); + xpthread_rwlock_unlock (&mylock); + } + else + { + xpthread_rwlock_rdlock (&mylock); + xpthread_rwlock_unlock (&mylock); + } + } + return NULL; +} + +int +do_test (void) +{ + xpthread_rwlockattr_init (&mylock_attr); + xpthread_rwlockattr_setkind_np (&mylock_attr, + PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); + xpthread_rwlock_init (&mylock, &mylock_attr); + + for (int n = 0; n < LOOPS; n++) + { + pthread_t tids[NTHREADS]; + do_exit = 0; + for (int i = 0; i < NTHREADS; i++) + tids[i] = xpthread_create (NULL, run_loop, NULL); + /* Let the threads run for some time. */ + sleep (1); + printf ("Exiting..."); + fflush (stdout); + do_exit = 1; + for (int i = 0; i < NTHREADS; i++) + xpthread_join (tids[i]); + printf ("done.\n"); + } + pthread_rwlock_destroy (&mylock); + pthread_rwlockattr_destroy (&mylock_attr); + return 0; +} + +#define TIMEOUT (DEFAULT_TIMEOUT + 3 * LOOPS) +#include