From patchwork Mon Sep 6 20:47:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44877 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 005E53857C60 for ; Mon, 6 Sep 2021 20:51:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 005E53857C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630961464; bh=fsO86smwyqQM3kffeAWS8Jv0NLkkbf2pF4qtLq3p1vs=; 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=KegOL9WoiVJ4qCAvF+wYwnYbWBsj0IpZmpHftR3UxtvH1b310YtkjYT6bf1vE/1aV sKQZB67MwQYhDHzenEwkVqLDrDt9gKhtovlUuXOT4sBTEvKNvzfjlTYoggHeVgz383 ejK/ozZNUO6TfGa1tyNjm1flxNOIGiT4DTE6F8vw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [IPv6:2607:f8b0:4864:20::82a]) by sourceware.org (Postfix) with ESMTPS id 28D6E385AC35 for ; Mon, 6 Sep 2021 20:47:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 28D6E385AC35 Received: by mail-qt1-x82a.google.com with SMTP id l24so6310870qtj.4 for ; Mon, 06 Sep 2021 13:47:55 -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=fsO86smwyqQM3kffeAWS8Jv0NLkkbf2pF4qtLq3p1vs=; b=bn0y0gISjYcKn73epnjBmHPsHJYoTnhhBcziaBUSx9uI5TqQb1YDQvYSvCsHoEKs8A 0asjGoYoSUZtmnByqWRhN6ZnnmkTHJnsDR+zfWcs87apyR9xmoJRXCwK1A5GHKKhxXLG d/oQ82/z6jVAscBYr/m8BCoHwdUa3iYfKcW+rNe+FU+8/7w9C59sJfkwGtCokAV5gpKD f4RjJkkidm3pc+RUfANwJNMa94frNDMTgblC0agKDZiugClbyUE4Ri0fkM/mHrWV+XA2 /jqVo4KCBA/Wr15AAlIRrqOh9qCxz5/HOdV/HoIPAOkE4i9uacJm0tRPeb0QD9wu9dda XvwA== X-Gm-Message-State: AOAM533TVdS++ggGQovtmUVf8MpwqcSCfZEXdVc2Re3wgjZzoXBBNSbg SycG5aZrDFNaslDh4qieo8Smb8lyF4D6gg== X-Google-Smtp-Source: ABdhPJyTq17Ufcz8LBo3iesbHAkYA153wge8l0cKk0MhpksRyYV3chcFE8WdY9T6ln/qWUlQNII4+g== X-Received: by 2002:ac8:4354:: with SMTP id a20mr12675228qtn.188.1630961274508; Mon, 06 Sep 2021 13:47:54 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:733d:210:e147:8627:2d6]) by smtp.gmail.com with ESMTPSA id w129sm7468416qkb.61.2021.09.06.13.47.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 13:47:54 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Date: Mon, 6 Sep 2021 17:47:40 -0300 Message-Id: <20210906204740.3988273-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210906204740.3988273-1-adhemerval.zanella@linaro.org> References: <20210906204740.3988273-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Linux added FUTEX_LOCK_PI2 to support clock selection (commit bf22a6976897977b0a3f1aeba6823c959fc4fdae). With the new flag we can now proper support CLOCK_MONOTONIC for pthread_mutex_clocklock with Priority Inheritance. If kernel does not support, EINVAL is returned instead. The difference is the futex operation will be issued and the kernel will advertise the missing support (instead of hard-code error return). Checked on x86_64-linux-gnu and i686-linux-gnu on Linux 5.14, 5.11, and 4.15. --- nptl/pthread_mutex_timedlock.c | 7 ------ nptl/tst-mutexpi10.c | 42 +++++++++++++++++++++------------- sysdeps/pthread/tst-mutex5.c | 23 ++++++++++++------- sysdeps/pthread/tst-mutex9.c | 20 ++++++++++------ 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index a695faeb7e..57f3f28869 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -299,13 +299,6 @@ __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 index da781d0d7a..34a7bea21b 100644 --- a/nptl/tst-mutexpi10.c +++ b/nptl/tst-mutexpi10.c @@ -38,29 +38,39 @@ do_test (void) PTHREAD_MUTEX_STALLED, PTHREAD_MUTEX_ROBUST }; - + const struct { + int clk; + int r; + } clocks[] = { + { CLOCK_REALTIME, 0 }, + { CLOCK_MONOTONIC, 0 }, + { CLOCK_REALTIME_COARSE, EINVAL } + }; 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]); + for (int c = 0; c < array_length (clocks); c++) + { + 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); + pthread_mutex_t mtx; + xpthread_mutex_init (&mtx, &attr); - struct timespec tmo = timespec_add (xclock_now (CLOCK_MONOTONIC), - make_timespec (0, 100000000)); + /* Uncontended case does not trigger any futex call. */ + struct timespec tmo = timespec_add (xclock_now (clocks[c].clk), + make_timespec (0, 100000000)); - TEST_COMPARE (pthread_mutex_clocklock (&mtx, CLOCK_MONOTONIC, &tmo), - EINVAL); + TEST_COMPARE (pthread_mutex_clocklock (&mtx, clocks[c].clk, &tmo), + clocks[c].r); + if (clocks[c].r == 0) + TEST_COMPARE (pthread_mutex_unlock (&mtx), 0); - xpthread_mutex_destroy (&mtx); - } + xpthread_mutex_destroy (&mtx); + } return 0; } diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c index b2a06b3650..89ad03fd8f 100644 --- a/sysdeps/pthread/tst-mutex5.c +++ b/sysdeps/pthread/tst-mutex5.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -39,7 +40,7 @@ #define CLOCK_USE_TIMEDLOCK (-1) static int -do_test_clock (clockid_t clockid, const char *fnname) +do_test_clock (clockid_t clockid, const char *fnname, int tmo_result) { pthread_mutex_t m; pthread_mutexattr_t a; @@ -76,11 +77,12 @@ do_test_clock (clockid_t clockid, const char *fnname) make_timespec (2, 0)); if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), tmo_result); else TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), - ETIMEDOUT); - TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); + tmo_result); + if (tmo_result == ETIMEDOUT) + TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); /* The following makes the ts value invalid. */ ts_timeout.tv_nsec += 1000000000; @@ -119,11 +121,16 @@ static int do_test (void) init_tpp_test (); #endif - do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); - do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); -#ifndef ENABLE_PI - do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)"); + int monotonic_result = +#ifdef ENABLE_PI + support_mutex_pi_monotonic () ? ETIMEDOUT : EINVAL; +#else + ETIMEDOUT; #endif + + do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock", ETIMEDOUT); + do_test_clock (CLOCK_REALTIME, "clocklock(realtime)", ETIMEDOUT); + do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)", monotonic_result); return 0; } diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c index 63bac69b6d..b29d6efcc8 100644 --- a/sysdeps/pthread/tst-mutex9.c +++ b/sysdeps/pthread/tst-mutex9.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -39,7 +40,7 @@ #define CLOCK_USE_TIMEDLOCK (-1) static void -do_test_clock (clockid_t clockid) +do_test_clock (clockid_t clockid, int tmo_result) { const clockid_t clockid_for_get = (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; @@ -111,9 +112,9 @@ do_test_clock (clockid_t clockid) make_timespec (0, 500000000)); if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_timedlock (m, &ts), tmo_result); else - TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), tmo_result); alarm (1); @@ -141,11 +142,16 @@ do_test (void) init_tpp_test (); #endif - do_test_clock (CLOCK_USE_TIMEDLOCK); - do_test_clock (CLOCK_REALTIME); -#ifndef ENABLE_PI - do_test_clock (CLOCK_MONOTONIC); + int monotonic_result = +#ifdef ENABLE_PI + support_mutex_pi_monotonic () ? ETIMEDOUT : EINVAL; +#else + ETIMEDOUT; #endif + + do_test_clock (CLOCK_USE_TIMEDLOCK, ETIMEDOUT); + do_test_clock (CLOCK_REALTIME, ETIMEDOUT); + do_test_clock (CLOCK_MONOTONIC, monotonic_result); return 0; }