From patchwork Mon Sep 6 20:47:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44874 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 3254B3885C12 for ; Mon, 6 Sep 2021 20:48:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3254B3885C12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630961335; bh=feeTUsAKsUafbAY2Y/NWZGzubjtkTvsbA0eMcUene0Y=; 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=x326F5Gb3t4a1BxusWZeGlZXTubRjHeZ9PxgOLl5QJ0cQhEyRHD+o25SLjqJxbGJb pzFnUldP3uCk7E/yGyDt8p364U9Qn7+Hxjx6fikQHTk6mppICYXZ2NL+7KNh4NEs2A mEY1VmKtt9fQqgqss41bvyOePYo8UUbRp07Vioc8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by sourceware.org (Postfix) with ESMTPS id 10879385B83D for ; Mon, 6 Sep 2021 20:47:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 10879385B83D Received: by mail-qk1-x72f.google.com with SMTP id t190so7966095qke.7 for ; Mon, 06 Sep 2021 13:47:50 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=feeTUsAKsUafbAY2Y/NWZGzubjtkTvsbA0eMcUene0Y=; b=em43sOhNtcgxxzmYqQQ5dJJ/S4UlVGakI/Uz8inZEfCOlNmNRgrbD4aQKo5e6L/JHJ 1nIMRmxWxY8dRfQ/JAUcm9WgzteT+MEQ211BDTX13EywfiqCW2jI14Ovt49olmdBMIgm 92BA/IMxwdWMJ9a2OA3zRIREr+qPN4NiugaOREwdFNT7KVYgV7udRK/8awbS3nLXq27j a/9xNxpt9lCvlJCb5VK2TVtlkQshumhGV0reu84uL4QHYoslQu6PRqKew9Wf8ZGoElWZ WITHbKoa1ZISmKfNzkS7UERyvUDTKJ8cQ1reFxHa8MbES8satqimbfF8zytGxaU9ne0W kzrQ== X-Gm-Message-State: AOAM532vx5nXuAAqxjrKBhjI9V87jHBQuCkl/bmdKKl9ZKKQjoxeRO6x 1grsp6/Mya4Cxv4K4qCUqwhngEa69cQftQ== X-Google-Smtp-Source: ABdhPJwb1qefIMBXmnQvLEZ5ag+Bm3dDYkU8BWj2DUO0GZUua1aRByUya4hXKqV65zJlqM7+bkHobg== X-Received: by 2002:a37:781:: with SMTP id 123mr13076525qkh.193.1630961269474; Mon, 06 Sep 2021 13:47:49 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 13:47:49 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2 Date: Mon, 6 Sep 2021 17:47:37 -0300 Message-Id: <20210906204740.3988273-2-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.0 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.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 Netto Reply-To: Adhemerval Zanella Cc: Kurt Kanzenbach Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Kurt Kanzenbach Linux v5.14.0 introduced a new futex operation called FUTEX_LOCK_PI2. This kernel feature can be used to implement pthread_mutex_clocklock(MONOTONIC)/PI. Signed-off-by: Kurt Kanzenbach Reviewed-by: Adhemerval Zanella --- sysdeps/unix/sysv/linux/kernel-features.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 3930e14322..ffb6af196b 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -220,4 +220,12 @@ # define __ASSUME_FACCESSAT2 0 #endif +/* The FUTEX_LOCK_PI2 operation was introduced across all architectures in Linux + 5.14. */ +#if __LINUX_KERNEL_VERSION >= 0x050e00 +# define __ASSUME_FUTEX_LOCK_PI2 1 +#else +# define __ASSUME_FUTEX_LOCK_PI2 0 +#endif + #endif /* kernel-features.h */ From patchwork Mon Sep 6 20:47:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44875 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 76C18385C411 for ; Mon, 6 Sep 2021 20:49:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76C18385C411 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630961378; bh=JleYG/kTUpZ4H9nsTXpCvCrB15F/ZG0BxFjv/M5R7pE=; 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=Y9GfvawChCLZFwfPmsqqBVkFYJnO4+dM29XIVlmMIwmVFjWwMBwVedZHfIx6Nmt7s htoNCOESZtVNs6F2xufhF5kMKvF+s9hoWcEQCrurc5oZfm5gFxmw9IvbcGNbEvhQb5 AGtjra/dOeB7wyhjWvqO9+0iGh1Irnk5uwKhbHrk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by sourceware.org (Postfix) with ESMTPS id 13F1A385B83B for ; Mon, 6 Sep 2021 20:47:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 13F1A385B83B Received: by mail-qk1-x736.google.com with SMTP id c10so7958901qko.11 for ; Mon, 06 Sep 2021 13:47:53 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JleYG/kTUpZ4H9nsTXpCvCrB15F/ZG0BxFjv/M5R7pE=; b=sSIMlL6wpPSTZM7xCqD0HH1BpWbqXL9OmYLmqV3eFQ9KThnx7aTYemC/uMg/7Pyl3V SWxHr362cbEVmEW8VO4kpL0U4I4hkQOBI5MXy4z7UfUka4tI48eahxry1TalrylZ1Xwu Rp5uRdZ+nIE686p5B72qBEF4U9Bp0+uJgsHl07U99Alw+rn9nIKAd8nEB/9JFZxycM3b uBx0FEh7SqdGCY7h6d+ynamM+raA8uxW0yHKPQL40EqwqOFtM92vdayiUoovQSx4E3x6 YnBqmzzl2RLaMF2mtSmmyJTxsRjrMo9H62DhUy7L8qhJ9xejmqHQ9n2VYlwSTKMZOfi1 bMEg== X-Gm-Message-State: AOAM532vbb4Z0XVHkZPp6xjY5B+K/Bep/oM9s3EcwQj38wolKee+cpYI IOgT+azBh5OwM3+ihcfXx1T5pDEhOp/LIA== X-Google-Smtp-Source: ABdhPJx3IhIYWHZ4bvyaw7hAWXHJSbpexWAb3nqV1miJqSlwfcizBHAogIcK/Y3ajGYjvIOZxIvO8Q== X-Received: by 2002:a05:620a:1009:: with SMTP id z9mr12712565qkj.483.1630961272461; Mon, 06 Sep 2021 13:47:52 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 13:47:52 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 2/4] nptl: Use FUTEX_LOCK_PI2 when available Date: Mon, 6 Sep 2021 17:47:38 -0300 Message-Id: <20210906204740.3988273-3-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.3 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 Netto Reply-To: Adhemerval Zanella Cc: Kurt Kanzenbach Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This patch uses the new futex PI operation provided by Linux v5.14 when it is required. The futex_lock_pi64() is moved to futex-internal.c (since it used on two different places and its code size might be large depending of the kernel configuration) and clockid is added as an argument. Co-authored-by: Kurt Kanzenbach --- nptl/futex-internal.c | 63 +++++++++++++++++++++++++++++++ nptl/pthread_mutex_lock.c | 3 +- nptl/pthread_mutex_timedlock.c | 3 +- sysdeps/nptl/futex-internal.h | 58 ++-------------------------- sysdeps/nptl/lowlevellock-futex.h | 1 + 5 files changed, 72 insertions(+), 56 deletions(-) diff --git a/nptl/futex-internal.c b/nptl/futex-internal.c index e74647a9d4..58605b2fca 100644 --- a/nptl/futex-internal.c +++ b/nptl/futex-internal.c @@ -140,3 +140,66 @@ __futex_abstimed_wait_cancelable64 (unsigned int* futex_word, abstime, private, true); } libc_hidden_def (__futex_abstimed_wait_cancelable64) + +int +__futex_lock_pi64 (int *futex_word, clockid_t clockid, + const struct __timespec64 *abstime, int private) +{ + int err; + + unsigned int clockbit = clockid == CLOCK_REALTIME + ? FUTEX_CLOCK_REALTIME : 0; + int op_pi2 = __lll_private_flag (FUTEX_LOCK_PI2 | clockbit, private); +#if __ASSUME_FUTEX_LOCK_PI2 + /* Assume __ASSUME_TIME64_SYSCALLS since FUTEX_LOCK_PI2 was added later. */ + err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi2, 0, abstime); +#else + /* FUTEX_LOCK_PI does not support clock selection, so for CLOCK_MONOTONIC + the only option is to use FUTEX_LOCK_PI2. */ + int op_pi1 = __lll_private_flag (FUTEX_LOCK_PI, private); + int op_pi = abstime != NULL && clockid != CLOCK_REALTIME ? op_pi2 : op_pi1; + +# ifdef __ASSUME_TIME64_SYSCALLS + err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi, 0, abstime); +# else + bool need_time64 = abstime != NULL && !in_time_t_range (abstime->tv_sec); + if (need_time64) + err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi, 0, abstime); + else + { + struct timespec ts32, *pts32 = NULL; + if (abstime != NULL) + { + ts32 = valid_timespec64_to_timespec (*abstime); + pts32 = &ts32; + } + err = INTERNAL_SYSCALL_CALL (futex, futex_word, op_pi, 0, pts32); + } +# endif /* __ASSUME_TIME64_SYSCALLS */ + /* FUTEX_LOCK_PI2 is not available on this kernel. */ + if (err == -ENOSYS) + err = -EINVAL; +#endif /* __ASSUME_FUTEX_LOCK_PI2 */ + + 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/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index fbe8a3cd21..2bd41767e0 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -421,7 +421,8 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - int e = futex_lock_pi64 (&mutex->__data.__lock, NULL, private); + int e = __futex_lock_pi64 (&mutex->__data.__lock, 0 /* ununsed */, + 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 bf2af6b048..a695faeb7e 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -369,7 +369,8 @@ __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, clockid, abstime, + private); if (e == ETIMEDOUT) return ETIMEDOUT; else if (e == ESRCH || e == EDEADLK) diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index 79a366604d..a46730cad6 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -236,8 +236,8 @@ futex_wake (unsigned int* futex_word, int processes_to_wake, int private) are done in descending priority order. The ABSTIME arguments provides an absolute timeout (measured against the - CLOCK_REALTIME clock). If TIMEOUT is NULL, the operation will block - indefinitely. + CLOCK_REALTIME or CLOCK_MONOTONIC clock). If TIMEOUT is NULL, the operation + will block indefinitely. Returns: @@ -250,58 +250,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_pi64 (int *futex_word, const struct __timespec64 *abstime, - int private) -{ - int err; -#ifdef __ASSUME_TIME64_SYSCALLS - err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, - __lll_private_flag (FUTEX_LOCK_PI, private), 0, - abstime); -#else - bool need_time64 = abstime != NULL && !in_time_t_range (abstime->tv_sec); - if (need_time64) - { - err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, - __lll_private_flag (FUTEX_LOCK_PI, private), - 0, abstime); - if (err == -ENOSYS) - err = -EOVERFLOW; - } - else - { - 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, clockid_t clockid, + const struct __timespec64 *abstime, int private); /* Wakes the top priority waiter that called a futex_lock_pi operation on the futex. diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h index 66ebfe50f4..abda179e0d 100644 --- a/sysdeps/nptl/lowlevellock-futex.h +++ b/sysdeps/nptl/lowlevellock-futex.h @@ -38,6 +38,7 @@ #define FUTEX_WAKE_BITSET 10 #define FUTEX_WAIT_REQUEUE_PI 11 #define FUTEX_CMP_REQUEUE_PI 12 +#define FUTEX_LOCK_PI2 13 #define FUTEX_PRIVATE_FLAG 128 #define FUTEX_CLOCK_REALTIME 256 From patchwork Mon Sep 6 20:47:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 44876 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 F1AB2385C401 for ; Mon, 6 Sep 2021 20:50:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F1AB2385C401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630961422; bh=sRphFPBS4sglo10DR8SH+DFuQLDbv1VlVoYDSELN5+I=; 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=FbqVMDqPDPfL8Y9l7+KAHi0e5z4KJt99Q/TET3F8G+wMDorW4XrJp6TLcvbQjdCWv QI2HsALExIm4V9jOwDlbdGwde2DLvm+8tzBniYVCFoqBKPcgxznMc0RAZ+ACB4kexb MEV0YDKj72W0JLqdGyqlnNtWdXKJqPdMEMIKWDkw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by sourceware.org (Postfix) with ESMTPS id 116DC385C403 for ; Mon, 6 Sep 2021 20:47:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 116DC385C403 Received: by mail-qk1-x733.google.com with SMTP id m21so7953627qkm.13 for ; Mon, 06 Sep 2021 13:47:54 -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=sRphFPBS4sglo10DR8SH+DFuQLDbv1VlVoYDSELN5+I=; b=OPCtKK+9GQGHx6VlzAOTwi0u5RbGkEFyhbSA6rUXIDdGWx68QmVrAkhv08B9rGoUBh WNTONkr2aax+rjFlBrLOeZvOB35rhdYWrzYVYk4BCAWQ2A8Mh+Hb1PNI8FOJVmnNvdpP sD8A5IYDKj/BBd/4iVdnxBW9QzSSmlrQ49D/BaYYdXL76DPEe+GWQHLwAhACztrep8Wy PCLXzT/EbgcAJC8oeA+53kwM/7rsxOPzKRL2WZkRrNFAHF7HvzldKI++rT2weXdq11zY AJ03gpB0v//8fqECf0ZzkCTBmh/hJt13+DDug2qd8HEqJeUNI2O5qfIIDOIEXOBaFeBl I06g== X-Gm-Message-State: AOAM5310cy1FHbpUODpJOI/acxndSUFKHMX9gUrKjdqFl2KzrxFIwlgZ pzvWSWfXyQXKV1t6WrfecD8RTJEGiaJjvQ== X-Google-Smtp-Source: ABdhPJx9groSV+EkZeyZIA/o2OqPWPneudEAGecXhXuyglH8PxZKCeVqj7KmGuaSagYVoGFQUFNJ0w== X-Received: by 2002:a37:2d04:: with SMTP id t4mr12305204qkh.463.1630961273508; Mon, 06 Sep 2021 13:47:53 -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.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Sep 2021 13:47:53 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 3/4] support: Add support_mutex_pi_monotonic Date: Mon, 6 Sep 2021 17:47:39 -0300 Message-Id: <20210906204740.3988273-4-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.3 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.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 Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Returns true if Priority Inheritance support CLOCK_MONOTONIC. --- support/Makefile | 1 + support/support_mutex_pi_monotonic.c | 33 ++++++++++++++++++++++++++++ support/xthread.h | 4 ++++ 3 files changed, 38 insertions(+) create mode 100644 support/support_mutex_pi_monotonic.c diff --git a/support/Makefile b/support/Makefile index 2a0731796f..fd27c8451e 100644 --- a/support/Makefile +++ b/support/Makefile @@ -63,6 +63,7 @@ libsupport-routines = \ support_format_hostent \ support_format_netent \ support_isolate_in_subprocess \ + support_mutex_pi_monotonic \ support_path_support_time64 \ support_process_state \ support_ptrace \ diff --git a/support/support_mutex_pi_monotonic.c b/support/support_mutex_pi_monotonic.c new file mode 100644 index 0000000000..5535837f92 --- /dev/null +++ b/support/support_mutex_pi_monotonic.c @@ -0,0 +1,33 @@ +/* Returns whether Priority Inheritance support CLOCK_MONOTONIC. + Copyright (C) 2021 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 + +bool +support_mutex_pi_monotonic (void) +{ +#ifdef __linux__ + int r = syscall (__NR_futex, &(unsigned int){0}, 13 /* FUTEX_LOCK_PI2 */, + 0, NULL); + return r == 0; +#else + return false; +#endif +} diff --git a/support/xthread.h b/support/xthread.h index a4a4ec5b1e..30501f5772 100644 --- a/support/xthread.h +++ b/support/xthread.h @@ -21,6 +21,7 @@ #include #include +#include __BEGIN_DECLS @@ -29,6 +30,9 @@ __BEGIN_DECLS exit function, so atexit handlers are executed. */ void delayed_exit (int seconds); +/* Returns true if Priority Inheritance support CLOCK_MONOTONIC. */ +bool support_mutex_pi_monotonic (void); + /* Terminate the process (with exit status 1) if VALUE is not zero. In that case, print a failure message to standard output mentioning FUNCTION. The process is terminated with the exit function, so 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 Netto 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 Netto 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; }