From patchwork Wed Aug 11 20:01:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44644 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 0B4463985822 for ; Wed, 11 Aug 2021 20:02:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B4463985822 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628712146; 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=Hk5M/S+BJWrbojtlnMY8RtOstfQKwOd6pNcaGjgt65SegDiutva1Ao+CeNA63yE/h rANSnIh3iHhT/U11BBqpekx74MPPDmoaiyxh6NlgjWTtTfMCHJkQ7pLxL3q0muglve f3dVd+UKDJp0QxNAblhXBnq/hZ7K/SKIcnG+gzBc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id BAF043851C3E for ; Wed, 11 Aug 2021 20:01:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BAF043851C3E Received: by mail-pj1-x102e.google.com with SMTP id fa24-20020a17090af0d8b0290178bfa69d97so6985862pjb.0 for ; Wed, 11 Aug 2021 13:01:42 -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=LVsOq76fbZslEM2odbdkQp0h+bqTFD7woJWoDwx9zfR/aZts5lvhR9gehmAhBHIUZF 6UEebNuG6LnhaqyuyDKD6XfXTJTboQwyEaTTHSIFA1qnq5OnGo6cXjHYx6uaCvs2Wa/h 2kvtH3PCiU8DAvusKd1jikW8wQLe583lBaE7CS6YfNbxkbdOsUjr0/SlaIdCXnY83Ci+ 5ISVGH13STci8VyQlZ3p7hpjTdpzt5SejQwldzcZu+bxoA8Q3//ufpcus7wFRWhrg6QU EO4e19tzjewra0zdqZGApR/jm6c1ZpvQJ6YI+UMuEKQTJ7mc7CYwuzC/4l3XV64c0qSN TORQ== X-Gm-Message-State: AOAM531GTyxRdKXwiO1DlCGNiZ4Z0ji2AxNQOugfRNNXvMpGetrLr7VR EzLQ3IE1QlmrXCji/xAdi1eQBN685wyLpw== X-Google-Smtp-Source: ABdhPJzNXo9qGLziOXUfsxCqX9IfC+FAly80KwD5n2IVwvYfQE/15P5oDEQozmXeMITL4JmmFzmIpw== X-Received: by 2002:a17:90a:b009:: with SMTP id x9mr12032187pjq.97.1628712101782; Wed, 11 Aug 2021 13:01:41 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9dce:70f4:4f7a:652c:7fb]) by smtp.gmail.com with ESMTPSA id r15sm281370pjl.29.2021.08.11.13.01.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 13:01:41 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/4] Linux: Add FUTEX_LOCK_PI2 Date: Wed, 11 Aug 2021 17:01:30 -0300 Message-Id: <20210811200133.3869287-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> References: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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 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 Wed Aug 11 20:01:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44646 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 C3B07398583A for ; Wed, 11 Aug 2021 20:03:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3B07398583A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628712236; bh=kfQhGgR1Vls9GSPgtYZUE99sqejSgjmBoY9T+zAHB0s=; 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=pt/Z+Pxcce1VmuL9X033trsfG8777YnKS1TMi6ZjUbPJmKV+OCjZDQSwP1HmhVY1P UafZr1zc1D9hj6jKQ8iTthiOkrCHOEIf0/lidJwErZhAUHsH+CG8lqSU3Zzk08ShUF 2jz9W0OiMc1WDkq76gbBzZUwXn8CYVCkwbSJqiDo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id 52B73398582C for ; Wed, 11 Aug 2021 20:01:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 52B73398582C Received: by mail-pj1-x1036.google.com with SMTP id oa17so5330978pjb.1 for ; Wed, 11 Aug 2021 13:01:45 -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=kfQhGgR1Vls9GSPgtYZUE99sqejSgjmBoY9T+zAHB0s=; b=F6Yz6YBQvIn8kjycZLTpsILtChH98coYzMd61iHTippR0rQgJ9piZtWqNuJOg7/YUS Hx4AJnsn8lRoqQ4S48ouKVj0LBkXq6Q4q4PYZT11WW2K2XHsxvGfQ+Ot7lLaE2ogri8q wchOciICo3x7AmC6NyGURWLxyDJAk+f4Uu33xXld40ufGP4fAF4AkY6brXupeOX9w/PH NRUvvga6f88wV8kZj2PD71gdnQfrlXD6WuIWUNS9tjTjiEWCXi0efBU0oYeB5qGpBv2y Lh23/9x9yupEAt1XO0nsTqnZEyL1vzYV0dC594bd7jPn3dk6PUvNXAhsWZrLYXtCxUOR Swgw== X-Gm-Message-State: AOAM530bz/rvOMrusffP08hLyIxqsoB4GSRc+xTs972IsZlpoPz3/Lln Er2oLNXfSc90GIPNCxAe7Cr7lJhssqfw+Q== X-Google-Smtp-Source: ABdhPJx2CBAiZqef3RRDumrbt1j0d3R8Vb/DFoOUP9zSHtDU2bgXprWGdlsLZ3AadM9c5OIOm2jrog== X-Received: by 2002:a17:902:8d8f:b029:12d:3d2d:b043 with SMTP id v15-20020a1709028d8fb029012d3d2db043mr483340plo.77.1628712104225; Wed, 11 Aug 2021 13:01:44 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9dce:70f4:4f7a:652c:7fb]) by smtp.gmail.com with ESMTPSA id r15sm281370pjl.29.2021.08.11.13.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 13:01:43 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/4] nptl: Use FUTEX_LOCK_PI2 when available Date: Wed, 11 Aug 2021 17:01:31 -0300 Message-Id: <20210811200133.3869287-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> References: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 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 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 | 54 +------------------------- sysdeps/nptl/lowlevellock-futex.h | 1 + 5 files changed, 70 insertions(+), 54 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 da624f322d..8e2ff2793f 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -422,7 +422,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 11ad7005d0..ca51da6f6c 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -370,7 +370,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..f0b7f814cc 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -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 Wed Aug 11 20:01:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44648 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 6763D398582D for ; Wed, 11 Aug 2021 20:05:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6763D398582D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628712334; bh=gg61FdkoA48HT1Cr+IPNiLtjRoy9beP/cveBQJL1Sow=; 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=WmnJyLTSahqvLrtPO/gFJS7xfhMWACWzNBLl2k0qo9R+PSDx5YdlUeH7OJyr2zlru GBGCa0q9XlhPaydzlo4JYdpApfun3xmXmZQlbpsbJjKtZsiMTChPKZBfyWTkP72DlA Bfay2HeH+QGEcxnYd5yzdJkHcEVUIdHSbwRJqV9k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id B62D33985822 for ; Wed, 11 Aug 2021 20:01:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B62D33985822 Received: by mail-pj1-x1029.google.com with SMTP id bo18so5396132pjb.0 for ; Wed, 11 Aug 2021 13:01:46 -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=gg61FdkoA48HT1Cr+IPNiLtjRoy9beP/cveBQJL1Sow=; b=KSs8hT5WjZ+RWB+e1Zgg+ZzdxUoir+INaDpZP038xIKH7gS4CLUWGuIIVJGa3KmEaW vFwTPWjekmakT7qyQwIbEWG/YoVZv0rp4RcybPut53BINHUvNwdiUMYHGFepsPMwIDbh FwOBTkb3ETUpVLILnDQoCj1ySq8zUwq5Tzk+JbbM/HLXdS4VEx0WhHtZy6tO47BhVLOQ ifhB+C6LGLSO17kgZz6lwWVn1G1/smhm5F13o3JFNqWZB7B6PbOV3rQTB8V6q7wKt5ly mIzxfNqDcL2Zjnk8lQJGXds/2jyh81GF7aWASq0IzyXIdRQFikal3FkNXr71KC1qE1iW 1Ddw== X-Gm-Message-State: AOAM532Zsz0IHasiR1BI+F8pcf7T0vMN41G9/MslAESvNcRZoLynkdSs eMj1jJzhdFJKIyJPM2tyHRGCr1P2Omkm2w== X-Google-Smtp-Source: ABdhPJztihLB8yH/vz2cYlzhn+YVXbT1dn04AFAJGeOiGiDNQqljqGv61jRbKEOvbhCXOlQ/rZJAbg== X-Received: by 2002:a63:ff51:: with SMTP id s17mr370114pgk.415.1628712105703; Wed, 11 Aug 2021 13:01:45 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9dce:70f4:4f7a:652c:7fb]) by smtp.gmail.com with ESMTPSA id r15sm281370pjl.29.2021.08.11.13.01.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 13:01:45 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 3/4] support: Add xpthread_mutex_pi_support_monotonic Date: Wed, 11 Aug 2021 17:01:32 -0300 Message-Id: <20210811200133.3869287-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> References: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 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 Reply-To: Adhemerval Zanella Cc: Kurt Kanzenbach 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/xpthread_mutex_pi_support_monotonic.c | 33 +++++++++++++++++++ support/xthread.h | 4 +++ 3 files changed, 38 insertions(+) create mode 100644 support/xpthread_mutex_pi_support_monotonic.c diff --git a/support/Makefile b/support/Makefile index a462781718..6360e86646 100644 --- a/support/Makefile +++ b/support/Makefile @@ -179,6 +179,7 @@ libsupport-routines = \ xpthread_sigmask \ xpthread_spin_lock \ xpthread_spin_unlock \ + xpthread_mutex_pi_support_monotonic \ xraise \ xreadlink \ xrealloc \ diff --git a/support/xpthread_mutex_pi_support_monotonic.c b/support/xpthread_mutex_pi_support_monotonic.c new file mode 100644 index 0000000000..eb42622913 --- /dev/null +++ b/support/xpthread_mutex_pi_support_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 +xpthread_mutex_pi_support_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..7074720bd9 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 xpthread_mutex_pi_support_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 Wed Aug 11 20:01:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44647 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 D114D3986410 for ; Wed, 11 Aug 2021 20:04:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D114D3986410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628712288; bh=yEEINSXgiTIrg8OtTz3wEf1kOJIQt61GjF/mC5CALtQ=; 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=ynVWBr/rQhPY/aJi+RlN6RYjBJllLOkbcrsoWMw37gLb7JU2FK+rP40JWHF3MsfyS kMoPjcm1l/I9M5/Vh4c+cBkq83gQSRg8xkkBUaIT7jZ4+rygv8K6GgtjRNyWDebJ51 b7ZAHkn9mmwI4eDAHASHk2ymp5KEbb0oUkMpvwOA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 2B2443848009 for ; Wed, 11 Aug 2021 20:01:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B2443848009 Received: by mail-pl1-x62a.google.com with SMTP id d17so4120976plr.12 for ; Wed, 11 Aug 2021 13:01:48 -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=yEEINSXgiTIrg8OtTz3wEf1kOJIQt61GjF/mC5CALtQ=; b=P5BD/XJzgbpZFjoRCbOGrsy+1TemTOt7JxZLsuiV5NJgyTOcXIhNlO/eL8hhnZ+e6o LR/T5q+YmxL1/aW/uW8ydIuBirjWTux4WRDPNTAk+GB6kMBfWYDi9JKY+3xmS3o+7s0m 5sG/pGBOrABfHVyNbTX3rEPfwUADqxUqyiwYwhQ6ipFrFIbUf5CNo163rS+iphRLff11 OgoaXOlM5P7QYSogmRFqvrzC0j9ONpzlUBofRQCUk+kEwrOI7Mn1TeB2jixJSzy/MYtJ bSv2wS299GA6dFo6bxvMkj+kuL5NUcpD+5f7Z7I7G9fjHuHsC18cIsvlfhZD5IX0uagL 2DsQ== X-Gm-Message-State: AOAM5312TO3K/yJCOvezTThK1KigaVSZ55lmfku6tyyZmlgPzUaQCYpy K7XOI8RiITZiBJpmAtzU+NbUfGNjIBISpw== X-Google-Smtp-Source: ABdhPJzpBCAKVFokGFUqEF9H4iRvEd8HvZFKYubqltUU9Zdqwyhs+EsA1MXEMpZqhozoYrF5peZPaQ== X-Received: by 2002:a63:204a:: with SMTP id r10mr370239pgm.365.1628712107113; Wed, 11 Aug 2021 13:01:47 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9dce:70f4:4f7a:652c:7fb]) by smtp.gmail.com with ESMTPSA id r15sm281370pjl.29.2021.08.11.13.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 13:01:46 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Date: Wed, 11 Aug 2021 17:01:33 -0300 Message-Id: <20210811200133.3869287-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> References: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 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 Cc: Kurt Kanzenbach Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With FUTEX_LOCK_PI2 support we can now proper support CLOCK_MONOTONIC for pthread_mutex_clocklock with Priority Inheritance. If kernel does not support, EINVAL is returned instead as previously. The difference is the futex operation will be issued and the kernel will advertise the missing support. 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 ca51da6f6c..a2fd1ada7b 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -300,13 +300,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 7dc5331cfc..5ca8a45eda 100644 --- a/sysdeps/pthread/tst-mutex5.c +++ b/sysdeps/pthread/tst-mutex5.c @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -40,7 +41,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; @@ -77,11 +78,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; @@ -120,11 +122,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 + xpthread_mutex_pi_support_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 58c3a1aec2..5448e4ec16 100644 --- a/sysdeps/pthread/tst-mutex9.c +++ b/sysdeps/pthread/tst-mutex9.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -40,7 +41,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; @@ -112,9 +113,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); @@ -142,11 +143,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 + xpthread_mutex_pi_support_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; }