From patchwork Thu Oct 29 19:42:07 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: 40927 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 CAF1B386F43B; Thu, 29 Oct 2020 19:42:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CAF1B386F43B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1604000536; bh=+ZFz9pjVjgDlViyvGWAoGUGuPnurtOfOdrb7nu16V48=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=K7hMhzOJkVRPvMpOV8zdllaoYtZysPnVbbXw2Lw06SZxzrwV+EqiepZwTOXyOR3Rg JJaD5mf+ZiKWJdixS4T7m4JgkCTmCwFQbWZfCnHAJfmBW+HpPwmTdWZ4GQt6B8FeiA kRY09jYsjyVcvEJHZzbzw6vC5SM/H5Fvg324pC+I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x743.google.com (mail-qk1-x743.google.com [IPv6:2607:f8b0:4864:20::743]) by sourceware.org (Postfix) with ESMTPS id 8DCA83858020 for ; Thu, 29 Oct 2020 19:42:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8DCA83858020 Received: by mail-qk1-x743.google.com with SMTP id a65so767054qkg.13 for ; Thu, 29 Oct 2020 12:42:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=+ZFz9pjVjgDlViyvGWAoGUGuPnurtOfOdrb7nu16V48=; b=P3d9DpYBDIVrEqm8m28Z0/gCprQQA31BvOSQhApGvWi/Q1rYnymgJsU1VTVsf5G8jk YbTI2yQh3jc+o1RE86hIjmBubXdiCcZ+yDQkgMV77ul+YO/5XAC9ApMRzZxSVz71as4O ivuEJJtcZRUuqHKfzggGb8CkdeW2ue6k/Z/Ax+UGrlY7IlX+I42vXuzjyuw/Xitpu+0x UIzVqWBFg7U6Bf2EIm0SMXhfv2ipYeYQEgjfIPKqzug6cqw0oAEY6uXJP1Rx2Dj/bQh9 g2qqWfnhbpjhaVNV9DKnqMbUYH8htqQ3i/1uZ0v8pRIIcf066Istm74Vs1Tv/A+so8pD ap+g== X-Gm-Message-State: AOAM532ikV7Iay9VKGOEXg88Uyx/elkZKViEPwIF+oClr6wocTmH4+Gr iw/sSZKXYCpRKgTcBcuBRfI6Ib1A8tARfQ== X-Google-Smtp-Source: ABdhPJw8mxCEh8zEugPTz0smQ/JzQfyLUMhmEWWc6W7+82qCH5p8mdTdfuqad9Cr9Ofd6ihTyq07lw== X-Received: by 2002:a37:e20d:: with SMTP id g13mr5290118qki.325.1604000533748; Thu, 29 Oct 2020 12:42:13 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id v65sm1656433qkb.88.2020.10.29.12.42.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 12:42:13 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/2] nptl: Replace futex_lock_pi with __futex_lock_pi64 Date: Thu, 29 Oct 2020 16:42:07 -0300 Message-Id: <20201029194208.2715606-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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 Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" And also move __futex_lock_pi64 to futex-internal.c, it decreases the libpthread code since there is no much gain in optimizing the slow path. Checked on x86_64-linux-gnu and i686-linux-gnu. --- nptl/pthread_mutex_lock.c | 3 +- nptl/pthread_mutex_timedlock.c | 2 +- sysdeps/nptl/futex-internal.c | 45 +++++++++++++++++++++ sysdeps/nptl/futex-internal.h | 74 +--------------------------------- 4 files changed, 49 insertions(+), 75 deletions(-) diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index 7858abd528..8b6bf45795 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -416,8 +416,7 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - int e = futex_lock_pi ((unsigned int *) &mutex->__data.__lock, - NULL, private); + int e = __futex_lock_pi64 (&mutex->__data.__lock, NULL, private); if (e == ESRCH || e == EDEADLK) { assert (e != EDEADLK diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index fe9e651f6c..d839e6862f 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -378,7 +378,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - int e = futex_lock_pi64 (&mutex->__data.__lock, abstime, private); + int e = __futex_lock_pi64 (&mutex->__data.__lock, abstime, private); if (e == ETIMEDOUT) return ETIMEDOUT; else if (e == ESRCH || e == EDEADLK) diff --git a/sysdeps/nptl/futex-internal.c b/sysdeps/nptl/futex-internal.c index 457cd3cd69..87de7e2fac 100644 --- a/sysdeps/nptl/futex-internal.c +++ b/sysdeps/nptl/futex-internal.c @@ -268,3 +268,48 @@ __futex_clock_wait_bitset64 (int *futexp, int val, clockid_t clockid, #endif return ret; } + +int +__futex_lock_pi64 (int *futex_word, const struct __timespec64 *abstime, + int private) +{ + int err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, + __lll_private_flag + (FUTEX_LOCK_PI, private), 0, abstime); +#ifndef __ASSUME_TIME64_SYSCALLS + if (err == -ENOSYS) + { + if (abstime != NULL && ! in_time_t_range (abstime->tv_sec)) + return EOVERFLOW; + + struct timespec ts32; + if (abstime != NULL) + ts32 = valid_timespec64_to_timespec (*abstime); + + err = INTERNAL_SYSCALL_CALL (futex, futex_word, __lll_private_flag + (FUTEX_LOCK_PI, private), 0, + abstime != NULL ? &ts32 : NULL); + } +#endif + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + case -ESRCH: + case -EDEADLK: + case -EINVAL: /* This indicates either state corruption or that the kernel + found a waiter on futex address which is waiting via + FUTEX_WAIT or FUTEX_WAIT_BITSET. This is reported on + some futex_lock_pi usage (pthread_mutex_timedlock for + instance). */ + return -err; + + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -ENOSYS: /* Must have been caused by a glibc bug. */ + /* No other errors are documented at this time. */ + default: + futex_fatal_error (); + } +} diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index cd356e4fa8..f0e3c1458a 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -409,78 +409,8 @@ futex_wake (unsigned int* futex_word, int processes_to_wake, int private) futex. - ETIMEDOUT if the ABSTIME expires. */ -static __always_inline int -futex_lock_pi (unsigned int *futex_word, const struct timespec *abstime, - int private) -{ - int err = lll_futex_timed_lock_pi (futex_word, abstime, private); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - case -ESRCH: - case -EDEADLK: - case -EINVAL: /* This indicates either state corruption or that the kernel - found a waiter on futex address which is waiting via - FUTEX_WAIT or FUTEX_WAIT_BITSET. This is reported on - some futex_lock_pi usage (pthread_mutex_timedlock for - instance). */ - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -ENOSYS: /* Must have been caused by a glibc bug. */ - /* No other errors are documented at this time. */ - default: - futex_fatal_error (); - } -} - -static __always_inline int -futex_lock_pi64 (int *futex_word, const struct __timespec64 *abstime, - int private) -{ - int err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, - __lll_private_flag - (FUTEX_LOCK_PI, private), 0, abstime); -#ifndef __ASSUME_TIME64_SYSCALLS - if (err == -ENOSYS) - { - if (abstime != NULL && ! in_time_t_range (abstime->tv_sec)) - return EOVERFLOW; - - struct timespec ts32; - if (abstime != NULL) - ts32 = valid_timespec64_to_timespec (*abstime); - - err = INTERNAL_SYSCALL_CALL (futex, futex_word, __lll_private_flag - (FUTEX_LOCK_PI, private), 0, - abstime != NULL ? &ts32 : NULL); - } -#endif - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - case -ESRCH: - case -EDEADLK: - case -EINVAL: /* This indicates either state corruption or that the kernel - found a waiter on futex address which is waiting via - FUTEX_WAIT or FUTEX_WAIT_BITSET. This is reported on - some futex_lock_pi usage (pthread_mutex_timedlock for - instance). */ - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -ENOSYS: /* Must have been caused by a glibc bug. */ - /* No other errors are documented at this time. */ - default: - futex_fatal_error (); - } -} +int __futex_lock_pi64 (int *futex_word, const struct __timespec64 *abstime, + int private) attribute_hidden; /* Wakes the top priority waiter that called a futex_lock_pi operation on the futex. From patchwork Thu Oct 29 19:42:08 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: 40928 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 DA5D03986813; Thu, 29 Oct 2020 19:42:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DA5D03986813 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1604000539; bh=ZmVy9b5qxWOuajX4bfA24HRhezrxpcbMvKyAO1lRerc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Ce7MkDMHhZYzxv9XHkuZ6AYjcm0hz7oAEV0OusqOl3eoDCtPkbRxk20Qs2SXk5Yuu dWxOWce6ta/msOinF5pt6vrWX/fsNk2fRvL86pDSVmUzmfcrPspRFuiXWvnU5tB3hb ijbi3ZxxN37ULqR+cZDhb9iBb2rHstwptlAvrFTs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x844.google.com (mail-qt1-x844.google.com [IPv6:2607:f8b0:4864:20::844]) by sourceware.org (Postfix) with ESMTPS id CF2443861001 for ; Thu, 29 Oct 2020 19:42:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CF2443861001 Received: by mail-qt1-x844.google.com with SMTP id m65so2604568qte.11 for ; Thu, 29 Oct 2020 12:42:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZmVy9b5qxWOuajX4bfA24HRhezrxpcbMvKyAO1lRerc=; b=e/vMk0hPQSNYygUHD/4hCZAVQj55PAihH3wPTZP3UNRIZkhyfMxXI15YxvVBWPj1Ty LJjK2XqPW8KpkLqUXVOn9Vy+PQezar49bbjm4YW8oHElTVzKifYgfZwYNDiTZHeGvZ9/ qd+U9gDdOHHC03shyp6yq/Wfxp37VEVBlAdISUZ5AUD7PBMkViHzCovLI/YA0E4soJO4 +RjFuo9XTkqHSXHSma76UTA0vvpkpJYe8GMb6g8AmqdB533UGOnVoBgxI27nvUGJ36bV aDnKr1Ov9Xr38rUyZOFO0Zeo82Zx+9BtPPfdh7MYShR2lnXItXG/t8aFz2CyhuHDb3rd LNIw== X-Gm-Message-State: AOAM531twvjXe6FvaMdk4qp9hEznkdbA6m1kccHaaxNg2Tj1hmZryiIC ZSb2HRGQSXVzzDWhL3R/LJLQUvD8pY7jsg== X-Google-Smtp-Source: ABdhPJxAHj5B+RVvxU+nXkf2/j3d0V08A47tLcNxkaQVfzW2L3oV4P6b+6qlRzf2uW1b9AjpLz1aRw== X-Received: by 2002:ac8:7b5b:: with SMTP id m27mr5150301qtu.153.1604000535161; Thu, 29 Oct 2020 12:42:15 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id v65sm1656433qkb.88.2020.10.29.12.42.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 12:42:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/2] nptl: Return ENOTSUP for pthread_mutex_clocklock/PI with CLOCK_MONOTONIC [BZ #26801] Date: Thu, 29 Oct 2020 16:42:08 -0300 Message-Id: <20201029194208.2715606-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201029194208.2715606-1-adhemerval.zanella@linaro.org> References: <20201029194208.2715606-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Linux futex FUTEX_LOCK_PI operation only supports CLOCK_REALTIME, so pthread_mutex_clocklock operation with priority aware mutexes may fail depending of the input timeout. Also, it is not possible to convert a CLOCK_MONOTONIC to a CLOCK_REALTIME due the possible wall clock time change which might invalid the requested timeout. Checked on x86_64-linux-gnu and i686-linux-gnu. --- nptl/Makefile | 2 +- nptl/pthread_mutex_timedlock.c | 7 ++++ nptl/tst-mutexpi10.c | 68 ++++++++++++++++++++++++++++++++++ sysdeps/pthread/tst-mutex5.c | 2 + sysdeps/pthread/tst-mutex9.c | 2 + 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 nptl/tst-mutexpi10.c diff --git a/nptl/Makefile b/nptl/Makefile index d47e8a81d9..1ca0ec3950 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -266,7 +266,7 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-mutex5a tst-mutex7a \ tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 \ tst-mutexpi5 tst-mutexpi5a tst-mutexpi6 tst-mutexpi7 tst-mutexpi7a \ - tst-mutexpi9 \ + tst-mutexpi9 tst-mutexpi10 \ tst-cond22 tst-cond26 \ tst-robustpi1 tst-robustpi2 tst-robustpi3 tst-robustpi4 tst-robustpi5 \ tst-robustpi6 tst-robustpi7 tst-robustpi9 \ diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index d839e6862f..923be4f734 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -315,6 +315,13 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP: { + /* Currently futex FUTEX_LOCK_PI operation only provides support for + CLOCK_REALTIME and trying to emulate by converting a + CLOCK_MONOTONIC to CLOCK_REALTIME will take in account possible + changes to the wall clock. */ + if (__glibc_unlikely (clockid != CLOCK_REALTIME)) + return ENOTSUP; + int kind, robust; { /* See concurrency notes regarding __kind in struct __pthread_mutex_s diff --git a/nptl/tst-mutexpi10.c b/nptl/tst-mutexpi10.c new file mode 100644 index 0000000000..409ec97d47 --- /dev/null +++ b/nptl/tst-mutexpi10.c @@ -0,0 +1,68 @@ +/* Check if pthread_mutex_clocklock with PRIO_INHERIT fails with clock + different than CLOCK_REALTIME. + Copyright (C) 2015-2020 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 +#include + +static int +do_test (void) +{ + const int types[] = { + PTHREAD_MUTEX_NORMAL, + PTHREAD_MUTEX_ERRORCHECK, + PTHREAD_MUTEX_RECURSIVE, + PTHREAD_MUTEX_ADAPTIVE_NP + }; + const int robust[] = { + PTHREAD_MUTEX_STALLED, + PTHREAD_MUTEX_ROBUST + }; + + + for (int t = 0; t < array_length (types); t++) + for (int r = 0; r < array_length (robust); r++) + { + pthread_mutexattr_t attr; + + xpthread_mutexattr_init (&attr); + xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT); + xpthread_mutexattr_settype (&attr, types[t]); + xpthread_mutexattr_setrobust (&attr, robust[r]); + + pthread_mutex_t mtx; + xpthread_mutex_init (&mtx, &attr); + + struct timespec tmo = timespec_add (xclock_now (CLOCK_MONOTONIC), + make_timespec (0, 100000000)); + + TEST_COMPARE (pthread_mutex_clocklock (&mtx, CLOCK_MONOTONIC, &tmo), + ENOTSUP); + + xpthread_mutex_destroy (&mtx); + } + + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c index 14490768c3..bfe1a79fa4 100644 --- a/sysdeps/pthread/tst-mutex5.c +++ b/sysdeps/pthread/tst-mutex5.c @@ -112,7 +112,9 @@ static int do_test (void) { do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); +#ifndef ENABLE_PI do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)"); +#endif return 0; } diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c index 2d7927b7c2..bfc01f8c75 100644 --- a/sysdeps/pthread/tst-mutex9.c +++ b/sysdeps/pthread/tst-mutex9.c @@ -133,7 +133,9 @@ do_test (void) { do_test_clock (CLOCK_USE_TIMEDLOCK); do_test_clock (CLOCK_REALTIME); +#ifndef ENABLE_PI do_test_clock (CLOCK_MONOTONIC); +#endif return 0; }