From patchwork Wed Nov 25 20:27:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 41193 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8A670395CC49; Wed, 25 Nov 2020 20:27:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8A670395CC49 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606336055; bh=BcDhopMUUpEQALVpV3DmJ1aKKd10T4ZVAymcibDjlRo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=pxhDGwUu0lsbYmKN2bovrnK6PDo7AvDi27aywZ0yh/ofwSWJoqzfv3nk6i152Kb9j K/wGc6kCcB4m+h2EBYdbqe+Y1f3NfBiYnMGXOIBiMloUPn34gjCm3fB8aEwIulCUGV NeSsGHKGTz296MSm6MzGKVO3IyXUry+WU9R8YGDA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by sourceware.org (Postfix) with ESMTPS id DA41B396E044 for ; Wed, 25 Nov 2020 20:27:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DA41B396E044 Received: by mail-qt1-x842.google.com with SMTP id 7so2580122qtp.1 for ; Wed, 25 Nov 2020 12:27:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=BcDhopMUUpEQALVpV3DmJ1aKKd10T4ZVAymcibDjlRo=; b=gA67D0YGtg+KZXWaLSi7Yl7jzmzqjsVg/C53sJ5EL/b8pBF7PGvveVZ1UqKH3JQeXw IEF26LFRWcOmr7dPwI2zuyANamTuZHa14SranzoVZTBwZRieI2E8Pt/LNb9jAWOdWpzc BVwth0BRjfQQ554cbOJeS6V/hcOuivqo/12yY/AVZ3soL9P3ZOF2FdzODZ2sTmJUg6eC c2s1KrJd2fO3VJVJo/hG8/iCzcW+ZjdAWf3tkFOt3FXPOWcMtR+zA0RlgUwgx2QjinGu 7npdX/+np5C6UhZdbkvbM/kZKU0H5xMI9hOyjae4xwGNFIqsKPgG5oRdXmqDACqLBSEw v4/Q== X-Gm-Message-State: AOAM533v7UhBiKAOmCydkMI80jnyotPllwNhunXZX1nbHno+NaSHIE5c 9vKTce0x9js5Wv6gs6MgtFJvXZMamJB9yA== X-Google-Smtp-Source: ABdhPJwweGmQNJ4WgX0CuiW576S5SSVKoEI3t+hC4fIs/oQpewyb5h+Ng5fqcr+EdONJqj9oKIoWgg== X-Received: by 2002:ac8:5308:: with SMTP id t8mr631605qtn.85.1606336049140; Wed, 25 Nov 2020 12:27:29 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id z20sm460671qtb.31.2020.11.25.12.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2020 12:27:28 -0800 (PST) To: libc-alpha@sourceware.org, Mike Crowe Subject: [PATCH] nptl: Fix PTHREAD_PRIO_PROTECT timed lock Date: Wed, 25 Nov 2020 17:27:23 -0300 Message-Id: <20201125202723.1513496-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Michael Kerrisk Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The 878fe624d4 changed lll_futex_timed_wait, which expects a relative timeout, with a __futex_abstimed_wait64, which expects an absolute timeout. However the code still passes a relative timeout. Also, the PTHREAD_PRIO_PROTECT support for clocks different than CLOCK_REALTIME was broken since the inclusion of pthread_mutex_clocklock (9d20e22e46) since lll_futex_timed_wait always use CLOCK_REALTIME. This patch fixes by removing the relative time calculation. It also adds some xtests that tests both thread and inter-process usage. Checked on x86_64-linux-gnu. --- nptl/Makefile | 3 ++- nptl/pthread_mutex_timedlock.c | 29 +++++------------------------ nptl/tst-mutexpp5.c | 2 ++ nptl/tst-mutexpp9.c | 2 ++ sysdeps/pthread/tst-mutex5.c | 12 +++++++++++- sysdeps/pthread/tst-mutex9.c | 13 ++++++++++++- 6 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 nptl/tst-mutexpp5.c create mode 100644 nptl/tst-mutexpp9.c diff --git a/nptl/Makefile b/nptl/Makefile index a48426a396..94d805f0d4 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -309,7 +309,8 @@ tests-internal := tst-robustpi8 tst-rwlock19 tst-rwlock20 \ tst-setgetname \ xtests = tst-setuid1 tst-setuid1-static tst-setuid2 \ - tst-mutexpp1 tst-mutexpp6 tst-mutexpp10 tst-setgroups + tst-mutexpp1 tst-mutexpp6 tst-mutexpp10 tst-setgroups \ + tst-mutexpp5 tst-mutexpp9 # This test can run into task limits because of a linux kernel bug # and then cause the make process to fail too, see bug 24537. diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index aaaafa21ce..74adffe790 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -547,30 +547,11 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, goto failpp; } - struct __timespec64 rt; - - /* Get the current time. */ - __clock_gettime64 (CLOCK_REALTIME, &rt); - - /* Compute relative timeout. */ - rt.tv_sec = abstime->tv_sec - rt.tv_sec; - rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec; - if (rt.tv_nsec < 0) - { - rt.tv_nsec += 1000000000; - --rt.tv_sec; - } - - /* Already timed out? */ - if (rt.tv_sec < 0) - { - result = ETIMEDOUT; - goto failpp; - } - - __futex_abstimed_wait64 ( - (unsigned int *) &mutex->__data.__lock, clockid, - ceilval | 2, &rt, PTHREAD_MUTEX_PSHARED (mutex)); + int e = __futex_abstimed_wait64 ( + (unsigned int *) &mutex->__data.__lock, ceilval | 2, + clockid, abstime, PTHREAD_MUTEX_PSHARED (mutex)); + if (e == ETIMEDOUT) + return ETIMEDOUT; } } while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, diff --git a/nptl/tst-mutexpp5.c b/nptl/tst-mutexpp5.c new file mode 100644 index 0000000000..a864a390ca --- /dev/null +++ b/nptl/tst-mutexpp5.c @@ -0,0 +1,2 @@ +#define ENABLE_PP 1 +#include "tst-mutex5.c" diff --git a/nptl/tst-mutexpp9.c b/nptl/tst-mutexpp9.c new file mode 100644 index 0000000000..c848c74c7e --- /dev/null +++ b/nptl/tst-mutexpp9.c @@ -0,0 +1,2 @@ +#define ENABLE_PP 1 +#include "tst-mutex9.c" diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c index bfe1a79fa4..dbd2c3c15f 100644 --- a/sysdeps/pthread/tst-mutex5.c +++ b/sysdeps/pthread/tst-mutex5.c @@ -27,6 +27,9 @@ #include #include +#ifdef ENABLE_PP +#include "tst-tpp.h" +#endif #ifndef TYPE # define TYPE PTHREAD_MUTEX_NORMAL @@ -47,8 +50,11 @@ do_test_clock (clockid_t clockid, const char *fnname) TEST_COMPARE (pthread_mutexattr_init (&a), 0); TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0); -#ifdef ENABLE_PI +#if defined ENABLE_PI TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); +#elif defined ENABLE_PP + TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0); + TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0); #endif int err = pthread_mutex_init (&m, &a); @@ -110,6 +116,10 @@ do_test_clock (clockid_t clockid, const char *fnname) static int do_test (void) { +#ifdef ENABLE_PP + init_tpp_test (); +#endif + do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); #ifndef ENABLE_PI diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c index bfc01f8c75..081aeff0f6 100644 --- a/sysdeps/pthread/tst-mutex9.c +++ b/sysdeps/pthread/tst-mutex9.c @@ -30,6 +30,10 @@ #include #include +#ifdef ENABLE_PP +#include "tst-tpp.h" +#endif + /* A bogus clock value that tells run_test to use pthread_mutex_timedlock rather than pthread_mutex_clocklock. */ @@ -73,8 +77,11 @@ do_test_clock (clockid_t clockid) TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0); -#ifdef ENABLE_PI +#if defined ENABLE_PI TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); +#elif defined ENABLE_PP + TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0); + TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0); #endif int e; @@ -131,6 +138,10 @@ do_test_clock (clockid_t clockid) static int do_test (void) { +#ifdef ENABLE_PP + init_tpp_test (); +#endif + do_test_clock (CLOCK_USE_TIMEDLOCK); do_test_clock (CLOCK_REALTIME); #ifndef ENABLE_PI