From patchwork Mon Nov 23 19:52:55 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: 41170 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 69779383E800; Mon, 23 Nov 2020 19:53:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69779383E800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606161205; bh=cDn++xrhCF7PhS8rXAeI/EV74wwLAadcDS5mZw6MrH8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=OBkJa+vT3lmM/VR1O6vjYMULEMf4BuWNKqJsOiUVT0GnaIttXQvodOAfKrDQYmYM7 J0Sx2tppZ8M7kqK3lStbnQuTE9qfli2ScnFw917bfv1tEo6tJ6EyWNNINeNtxjlDEb CXtYU+tjDMVAWvYGm2DFhUTbIQcZiMRgu+Anl68E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x742.google.com (mail-qk1-x742.google.com [IPv6:2607:f8b0:4864:20::742]) by sourceware.org (Postfix) with ESMTPS id C4146383F86C for ; Mon, 23 Nov 2020 19:53:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C4146383F86C Received: by mail-qk1-x742.google.com with SMTP id d9so18175772qke.8 for ; Mon, 23 Nov 2020 11:53:22 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=cDn++xrhCF7PhS8rXAeI/EV74wwLAadcDS5mZw6MrH8=; b=solafm1tUn4h4FO0f+1bcUFcU9N3Xe8ujBc38NGq/HPxe0Tbd03Tb1/7kXHEBFl4MF O5AZRtBPHAWFjEoFEwCtA6lukSYhfqEvkOrcJuge0guv50Pe4HcB8++ErNytHizHn3c+ cbdn3xmgCJyj/G4iQwC3DtzXevEyWKVErS5S+hZW0hXAV+ENThw79m7VKM/CsKNDphXF t6+Ikc8wFzIT3yv8thPTuEUmXvD89PVOutgJ4oiFaLvaQtxhIPZFdkYNL2BYA7s6qmpt 5hOgrwnGd4GtP/CS6r4iY3S5Dgq0T7HEwWGwlQdSfoGC/cxzNSVFzzpDiYbjz70K+wrB eHEw== X-Gm-Message-State: AOAM5318XRe4jpIGVlhDH+d68tVFP+8cllkInXA1np0/3kiXMoWUKGTF gWpGZ9rp0Da4ZKWC1qkAJLY4L2lwz5m0sg== X-Google-Smtp-Source: ABdhPJz6n7Ow+EP6B3w/WcKD3csu3s+iMbFetX+yI9ovV7LubodhH22A1jONz2VWD5ZmWpAEOOb02g== X-Received: by 2002:a05:620a:15a8:: with SMTP id f8mr1222240qkk.346.1606161202157; Mon, 23 Nov 2020 11:53:22 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id v9sm10440432qkv.34.2020.11.23.11.53.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 11:53:21 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 12/13] nptl: Return EINVAL for pthread_mutex_clocklock/PI with CLOCK_MONOTONIC [BZ #26801] Date: Mon, 23 Nov 2020 16:52:55 -0300 Message-Id: <20201123195256.3336217-12-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201123195256.3336217-1-adhemerval.zanella@linaro.org> References: <20201123195256.3336217-1-adhemerval.zanella@linaro.org> 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, 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 Cc: Michael Kerrisk Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Changes from previous version: - Replace ENOTSUP by EINVAL Reviewed-by: Lukasz Majewski Reviewed-by: Lukasz Majewski --- 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 968768d33b..a48426a396 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -265,7 +265,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 b42862193a..aaaafa21ce 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -313,6 +313,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 EINVAL; + 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..84ba1dfa97 --- /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), + EINVAL); + + 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; }