From patchwork Wed Oct 30 20:00:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 178129 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp1755860ill; Wed, 30 Oct 2019 13:02:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqweMMJcHjvDXOL8q2HogBD8bAneLJAD2LB/CxLdn6i5Mj9rrboh6trm/tNwC4KugR3UOzCA X-Received: by 2002:a50:ec8d:: with SMTP id e13mr1794935edr.54.1572465767461; Wed, 30 Oct 2019 13:02:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572465767; cv=none; d=google.com; s=arc-20160816; b=i05ZdIIQ7WgxrtpGdergCTTS9w6iiwL8mSvDXsAiiDEwilsD/N1RdwiigmalQL9hHp ovZadeGdNGceWrQIb15V8Jqf+VZPslZW5Nr985IzJuHzajTqRlmC2QC5JhCiPHwV4Q9w CI6Dv1/ykIpNZW5jbXfn6aeQVg9XpGE0NeVJ7Tcoke5yvsGNPpl8sOls78hGzxqKYux5 YTbz7O/jJoWXRq44H/2NjaRrGvTyAfvYbrKgCpZSISgNC65Vnd1IJ3JiopcmkbcOtEkW XRu/rP4VS2eNAwX+fR7gSx5ekY4nc84Jcq/x5qWEVRqM2ioMpWwOGnZJ06gFXE0tBpT5 nMxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature; bh=Rq3iPkjFISWoQT5s9szu4YZSOdDMY8VQEVKcTd8omG0=; b=hjSUsCbTT7a9Q6cdu7py13Prywed6XHJ82bnNx3PYtxmI6W1yy60He+gkhx80qx9VA 6z3scXVomK39XvauvUkYlhYm12ekjFcJ0DRTXtiJ1ITD4A86XwQqJK0vHzSrhfz04AXX duq4kdm+jp/u2EXnC7+G7cGOa0LLO7kLvOzIG8mn7XZP8V2NyZvZ55LCjikeOii9W9Xm 540dcwJkrHHfQoGiJiuKGB8T1DRkCflBK9XoqiJKAkeg2Do+9nmxJg2ezy7Dvp0ItgQU FqXGGXKDo+Ll2tXz/KgBYxlUbW0D1i3FRb1dcW500GWvjXnUueUPEq8vd2U3BdE2Wa9T bWYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Kg34nS6Z; dkim=pass header.i=@linaro.org header.s=google header.b=kbmT8L0i; spf=pass (google.com: domain of libc-alpha-return-106481-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106481-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q26si2051045ejz.43.2019.10.30.13.02.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 13:02:47 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-106481-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Kg34nS6Z; dkim=pass header.i=@linaro.org header.s=google header.b=kbmT8L0i; spf=pass (google.com: domain of libc-alpha-return-106481-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106481-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; q=dns; s= default; b=ex027B3p1uD8VHBx6+AdsHTTfLOD4e9b1pyv0y0MfgDqRouMDZhiV am3HOuX5MHOT9UrD0tS6nETiI3SqAZSo6jtG6NN7d/bgY1FDZ4zVbsk4HZRyORCb J2XAs/wuQAbI7Bt+nF1c3K7slC5v5LFTRqk2A/Wb51iMxYdUwIJ4bY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; s=default; bh=6a4EbN1vms1exhiSl+HOuWONfOM=; b=Kg34nS6ZHh95sIn3oFdkRawMOOyC OzNbiLMjE0ZN/hHqkWkpxiPnu/5945+R0J8Du45+64g694q0wsLGCnF3omfWJX76 5AaULeItPhFokyuqxpTTHd4nO3tUZWk7UZl0FByGftfswB8HjwlftDwto4emSnIt wFXIB+BGJV11gsE= Received: (qmail 83509 invoked by alias); 30 Oct 2019 20:01:37 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 78821 invoked by uid 89); 30 Oct 2019 20:01:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-qk1-f194.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=Rq3iPkjFISWoQT5s9szu4YZSOdDMY8VQEVKcTd8omG0=; b=kbmT8L0i+XlwVDci+BsUgHxbNej++Kb25/wOff1Y4A2s7TWZzObOyf/k43P33wl/y8 RiC6Ml8ze9CCv94sVnHRfdAt231bSSxpKOm07nWL3VwD4o7zYKJZsFMk2b08HV7diMs5 MlkilLpvm9WPbPaHTI+kjLcE6SLtqcNrFaQtRvKjCu9LrNCOI5HNzFlg5gN4nSG7IeOu UqVnDVc5BLKYsMABQWbiHMU3NWWTVH0IuxzhiFuglUMTtHGV/YH2EoR12HicDyrVRV2v CisjbDDAwnKRFd4dkbhvg6Qv8UjNS1EdtiWaOcgVtCcOy+JOIteR2krZ+hab2isafkAT gwFw== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis Subject: [PATCH 1/5] Consolidate futex-internal.h Date: Wed, 30 Oct 2019 17:00:48 -0300 Message-Id: <20191030200052.497-1-adhemerval.zanella@linaro.org> NPTL is already Linux specific, there is no need to parametrize futex operations and add a sysdep Linux specific implementation. This patch moves the relevant Linux code to nptl one. Checked on x86_64-linux-gnu and i686-linux-gnu. --- sysdeps/nptl/futex-internal.h | 211 +++++++++++++++++-- sysdeps/unix/sysv/linux/futex-internal.h | 255 ----------------------- 2 files changed, 196 insertions(+), 270 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/futex-internal.h -- 2.17.1 Reviewed-by: Carlos O'Donell diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index 4e6e8fa324..76921466f0 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -74,11 +74,35 @@ # error FUTEX_PRIVATE must be equal to 0 #endif +/* Calls __libc_fatal with an error message. Convenience function for + concrete implementations of the futex interface. */ +static __always_inline __attribute__ ((__noreturn__)) void +futex_fatal_error (void) +{ + __libc_fatal ("The futex facility returned an unexpected error code.\n"); +} + + +/* The Linux kernel treats provides absolute timeouts based on the + CLOCK_REALTIME clock and relative timeouts measured against the + CLOCK_MONOTONIC clock. + + We expect a Linux kernel version of 2.6.22 or more recent (since this + version, EINTR is not returned on spurious wake-ups anymore). */ + /* Returns EINVAL if PSHARED is neither PTHREAD_PROCESS_PRIVATE nor PTHREAD_PROCESS_SHARED; otherwise, returns 0 if PSHARED is supported, and ENOTSUP if not. */ static __always_inline int -futex_supports_pshared (int pshared); +futex_supports_pshared (int pshared) +{ + if (__glibc_likely (pshared == PTHREAD_PROCESS_PRIVATE)) + return 0; + else if (pshared == PTHREAD_PROCESS_SHARED) + return 0; + else + return EINVAL; +} /* Atomically wrt other futex operations on the same futex, this blocks iff the value *FUTEX_WORD matches the expected value. This is @@ -112,7 +136,27 @@ futex_supports_pshared (int pshared); a futex_wait call when synchronizing similar to Dekker synchronization. However, we make no such guarantee here. */ static __always_inline int -futex_wait (unsigned int *futex_word, unsigned int expected, int private); +futex_wait (unsigned int *futex_word, unsigned int expected, int private) +{ + int err = lll_futex_timed_wait (futex_word, expected, NULL, private); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + return -err; + + case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */ + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. 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 (); + } +} /* Like futex_wait but does not provide any indication why we stopped waiting. Thus, when this function returns, you have to always check FUTEX_WORD to @@ -132,7 +176,30 @@ futex_wait_simple (unsigned int *futex_word, unsigned int expected, /* Like futex_wait but is a POSIX cancellation point. */ static __always_inline int futex_wait_cancelable (unsigned int *futex_word, unsigned int expected, - int private); + int private) +{ + int oldtype; + oldtype = __pthread_enable_asynccancel (); + int err = lll_futex_timed_wait (futex_word, expected, NULL, private); + __pthread_disable_asynccancel (oldtype); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + return -err; + + case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */ + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. 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 (); + } +} /* Like futex_wait, but will eventually time out (i.e., stop being blocked) after the duration of time provided (i.e., RELTIME) has @@ -144,32 +211,134 @@ futex_wait_cancelable (unsigned int *futex_word, unsigned int expected, */ static __always_inline int futex_reltimed_wait (unsigned int* futex_word, unsigned int expected, - const struct timespec* reltime, int private); + const struct timespec* reltime, int private) +{ + int err = lll_futex_timed_wait (futex_word, expected, reltime, private); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + return -err; + + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. 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 (); + } +} /* Like futex_reltimed_wait but is a POSIX cancellation point. */ static __always_inline int futex_reltimed_wait_cancelable (unsigned int* futex_word, unsigned int expected, - const struct timespec* reltime, int private); + const struct timespec* reltime, int private) +{ + int oldtype; + oldtype = LIBC_CANCEL_ASYNC (); + int err = lll_futex_timed_wait (futex_word, expected, reltime, private); + LIBC_CANCEL_RESET (oldtype); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + return -err; + + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. 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 (); + } +} /* Check whether the specified clockid is supported by futex_abstimed_wait and futex_abstimed_wait_cancelable. */ static __always_inline int -futex_abstimed_supported_clockid (clockid_t clockid); +futex_abstimed_supported_clockid (clockid_t clockid) +{ + return lll_futex_supported_clockid (clockid); +} /* Like futex_reltimed_wait, but the provided timeout (ABSTIME) is an absolute point in time; a call will time out after this point in time. */ static __always_inline int futex_abstimed_wait (unsigned int* futex_word, unsigned int expected, clockid_t clockid, - const struct timespec* abstime, int private); + const struct timespec* abstime, int private) +{ + /* Work around the fact that the kernel rejects negative timeout values + despite them being valid. */ + if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0))) + return ETIMEDOUT; + int err = lll_futex_clock_wait_bitset (futex_word, expected, + clockid, abstime, + private); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + return -err; + + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment, unsupported + clockid or due to the timeout not being + normalized. 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 (); + } +} /* Like futex_reltimed_wait but is a POSIX cancellation point. */ static __always_inline int futex_abstimed_wait_cancelable (unsigned int* futex_word, unsigned int expected, clockid_t clockid, - const struct timespec* abstime, int private); + const struct timespec* abstime, int private) +{ + /* Work around the fact that the kernel rejects negative timeout values + despite them being valid. */ + if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0))) + return ETIMEDOUT; + int oldtype; + oldtype = __pthread_enable_asynccancel (); + int err = lll_futex_clock_wait_bitset (futex_word, expected, + clockid, abstime, + private); + __pthread_disable_asynccancel (oldtype); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + return -err; + + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. 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 (); + } +} /* Atomically wrt other futex operations on the same futex, this unblocks the specified number of processes, or all processes blocked on this futex if @@ -190,14 +359,26 @@ futex_abstimed_wait_cancelable (unsigned int* futex_word, object destruction (see above); therefore, we must not report or abort on most errors. */ static __always_inline void -futex_wake (unsigned int* futex_word, int processes_to_wake, int private); - -/* Calls __libc_fatal with an error message. Convenience function for - concrete implementations of the futex interface. */ -static __always_inline __attribute__ ((__noreturn__)) void -futex_fatal_error (void) +futex_wake (unsigned int* futex_word, int processes_to_wake, int private) { - __libc_fatal ("The futex facility returned an unexpected error code.\n"); + int res = lll_futex_wake (futex_word, processes_to_wake, private); + /* No error. Ignore the number of woken processes. */ + if (res >= 0) + return; + switch (res) + { + case -EFAULT: /* Could have happened due to memory reuse. */ + case -EINVAL: /* Could be either due to incorrect alignment (a bug in + glibc or in the application) or due to memory being + reused for a PI futex. We cannot distinguish between the + two causes, and one of them is correct use, so we do not + act in this case. */ + return; + case -ENOSYS: /* Must have been caused by a glibc bug. */ + /* No other errors are documented at this time. */ + default: + futex_fatal_error (); + } } #endif /* futex-internal.h */ diff --git a/sysdeps/unix/sysv/linux/futex-internal.h b/sysdeps/unix/sysv/linux/futex-internal.h deleted file mode 100644 index 5a4f4ff818..0000000000 --- a/sysdeps/unix/sysv/linux/futex-internal.h +++ /dev/null @@ -1,255 +0,0 @@ -/* futex operations for glibc-internal use. Linux version. - Copyright (C) 2014-2019 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 - . */ - -#ifndef FUTEX_INTERNAL_H -#define FUTEX_INTERNAL_H - -#include -#include -#include -#include - -/* See sysdeps/nptl/futex-internal.h for documentation; this file only - contains Linux-specific comments. - - The Linux kernel treats provides absolute timeouts based on the - CLOCK_REALTIME clock and relative timeouts measured against the - CLOCK_MONOTONIC clock. - - We expect a Linux kernel version of 2.6.22 or more recent (since this - version, EINTR is not returned on spurious wake-ups anymore). */ - -/* FUTEX_SHARED is always supported by the Linux kernel. */ -static __always_inline int -futex_supports_pshared (int pshared) -{ - if (__glibc_likely (pshared == PTHREAD_PROCESS_PRIVATE)) - return 0; - else if (pshared == PTHREAD_PROCESS_SHARED) - return 0; - else - return EINVAL; -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_wait (unsigned int *futex_word, unsigned int expected, int private) -{ - int err = lll_futex_timed_wait (futex_word, expected, NULL, private); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - return -err; - - case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */ - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment or due to the timeout not - being normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_wait_cancelable (unsigned int *futex_word, unsigned int expected, - int private) -{ - int oldtype; - oldtype = __pthread_enable_asynccancel (); - int err = lll_futex_timed_wait (futex_word, expected, NULL, private); - __pthread_disable_asynccancel (oldtype); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - return -err; - - case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */ - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment or due to the timeout not - being normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_reltimed_wait (unsigned int *futex_word, unsigned int expected, - const struct timespec *reltime, int private) -{ - int err = lll_futex_timed_wait (futex_word, expected, reltime, private); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment or due to the timeout not - being normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_reltimed_wait_cancelable (unsigned int *futex_word, - unsigned int expected, - const struct timespec *reltime, int private) -{ - int oldtype; - oldtype = LIBC_CANCEL_ASYNC (); - int err = lll_futex_timed_wait (futex_word, expected, reltime, private); - LIBC_CANCEL_RESET (oldtype); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment or due to the timeout not - being normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_abstimed_supported_clockid (clockid_t clockid) -{ - return lll_futex_supported_clockid (clockid); -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_abstimed_wait (unsigned int *futex_word, unsigned int expected, - clockid_t clockid, - const struct timespec *abstime, int private) -{ - /* Work around the fact that the kernel rejects negative timeout values - despite them being valid. */ - if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0))) - return ETIMEDOUT; - int err = lll_futex_clock_wait_bitset (futex_word, expected, - clockid, abstime, - private); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment, unsupported - clockid or due to the timeout not being - normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline int -futex_abstimed_wait_cancelable (unsigned int *futex_word, - unsigned int expected, - clockid_t clockid, - const struct timespec *abstime, int private) -{ - /* Work around the fact that the kernel rejects negative timeout values - despite them being valid. */ - if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0))) - return ETIMEDOUT; - int oldtype; - oldtype = __pthread_enable_asynccancel (); - int err = lll_futex_clock_wait_bitset (futex_word, expected, - clockid, abstime, - private); - __pthread_disable_asynccancel (oldtype); - switch (err) - { - case 0: - case -EAGAIN: - case -EINTR: - case -ETIMEDOUT: - return -err; - - case -EFAULT: /* Must have been caused by a glibc or application bug. */ - case -EINVAL: /* Either due to wrong alignment or due to the timeout not - being normalized. 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 (); - } -} - -/* See sysdeps/nptl/futex-internal.h for details. */ -static __always_inline void -futex_wake (unsigned int *futex_word, int processes_to_wake, int private) -{ - int res = lll_futex_wake (futex_word, processes_to_wake, private); - /* No error. Ignore the number of woken processes. */ - if (res >= 0) - return; - switch (res) - { - case -EFAULT: /* Could have happened due to memory reuse. */ - case -EINVAL: /* Could be either due to incorrect alignment (a bug in - glibc or in the application) or due to memory being - reused for a PI futex. We cannot distinguish between the - two causes, and one of them is correct use, so we do not - act in this case. */ - return; - case -ENOSYS: /* Must have been caused by a glibc bug. */ - /* No other errors are documented at this time. */ - default: - futex_fatal_error (); - } -} - -#endif /* futex-internal.h */ From patchwork Wed Oct 30 20:00:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 178128 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp1755418ill; Wed, 30 Oct 2019 13:02:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/6TVAb+EvF0LiqZEnm99WmOhuOD1BwtgZso1mbi7t/rbmwoO/PRI2MZaX7xU4sJdZjZuu X-Received: by 2002:a50:a9e3:: with SMTP id n90mr1737080edc.52.1572465748890; Wed, 30 Oct 2019 13:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572465748; cv=none; d=google.com; s=arc-20160816; b=mfW1POC2dsC78mm5CLbRt7+0L4BiVZAnH20UL0Rdw3pnoRFWm9PfJRCy7rVZV8qgLF OteBGC4v8zQxlGDj0BZGfLcoTclX2iQoVijbNQlZUvZOrtTZ1Kl7oBEWDPFj7LYvIBkB p1gqXgqtoWWVUIKSBSDB5U8DUMbQC0SptIZ3IoSTOau+FQVKNpJ9sY+M1YXuP/sc0Mh8 A4YYYBPQO7/rE8DYxaDkPNLg/wW00fuXHarLbEjpLqEWtrMNrthwInMO595ukkz25sdk 7uTX61lI37BwRkY+vTRcrQ2Vvf/2YbccAXbsqf/WUhDLdcuGicHhVJVxoKgM94ZCV4KF HAbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=v/W3gNn2tsSk4aA0PXEwDFn0QRziyq5xeEnfkDgEI1s=; b=m08klWOWUaFlDRS1JseOVvgAb+hG4RvTQ+ke8hHQlRaZGM6hBgmHBmqw5R8Rfl5Aie puzd1JFv7PPq95NqnaLMB2AuaNsXqK3BJo0ul/M/ma1p62Yc39ugwUW+G972ipMhrL13 /uXwqnFY9wa19saI2FjY+o6N3pHosaRhMpspL0jyUVs0M0c4PqrPxhiMJiiPI/CUkOqg d0npJYdfJr73ekLjl1/6HK2v7bp1jHRzxbZKJWZB30NV8JnsKoV3+m9yBQPl9XMyHqQK O1vt0gC9AsOmbtskOlkrDdkMoiHESKani9hj0iBCwJvE+9vGKl7G2LaEdfbPnO8H7dks rrMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=wajD3+bu; dkim=pass header.i=@linaro.org header.s=google header.b=QdSruEoK; spf=pass (google.com: domain of libc-alpha-return-106480-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106480-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id m16si2099668ejd.306.2019.10.30.13.02.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 13:02:28 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-106480-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=wajD3+bu; dkim=pass header.i=@linaro.org header.s=google header.b=QdSruEoK; spf=pass (google.com: domain of libc-alpha-return-106480-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106480-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=h/oLWV/6v2v6OOLxwoXCPKBYzSgnCf3 7Kf/doHZbj8iDcFJhJmaM0HMytLZq3/3JjAP/k1XKi4tR1bqiczKAAY76sul/TjU X5hG4saucjtk+He4Phf50k+E4AtUNpv15Qpd7mIvgh26XofdVk2pbm19NdxekD1x uqb98VQ5Oej0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=9MwHDOu/fmRk1/8Rz857MUc2A7U=; b=wajD3 +buXeIcS9d/1/4qMVFkE4T/HddhrEqraWrMSinr6eT9KlnbzD5VVLWqxF8tr9WRU 90qCMxUAERBVTxz2xPXm+AOthN8uRrggwdGXe7oZYIGOKkFjFVUKLlFt9YNcRiw0 0rPxhzd5rnD22sjrhNFcST8uZ4p7INYRL1g8iQ= Received: (qmail 83398 invoked by alias); 30 Oct 2019 20:01:37 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 78694 invoked by uid 89); 30 Oct 2019 20:01:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=lowlevel, low-level X-HELO: mail-qt1-f196.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=v/W3gNn2tsSk4aA0PXEwDFn0QRziyq5xeEnfkDgEI1s=; b=QdSruEoKs7o5EZHVDfM4gfF1mG1HKwAkwvbOCBkUPcMuUGZD+hnfBj4JPYL8M9SJtE WmTY2doTESFzZjbF8+hWtz8UDw/uDLGqy2iwGLViV24O51gK+S+PEmDiZ6kzd9mObeup 6cNciweqqWSQKUxLE4s8twQ9oeoqtx4Gy/XDrNxDnd1D+hD9M8nwiFVQojYFpmrjx9f2 FrmsGllMdGIW09wGqImW7Ut1ltvHyFJgceP3qLr/fENmboqcv0jMU2kjHg+ffgDqrYiE az3BHsfWbNQ3ZjRVtrPhHJBK7WEYrRMBJYpGf8yk97fiJOKcPhySiL/r7Lo3pog1ItK5 mUOg== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis Subject: [PATCH 2/5] Consolidate lowlevellock-futex.h Date: Wed, 30 Oct 2019 17:00:49 -0300 Message-Id: <20191030200052.497-2-adhemerval.zanella@linaro.org> In-Reply-To: <20191030200052.497-1-adhemerval.zanella@linaro.org> References: <20191030200052.497-1-adhemerval.zanella@linaro.org> NPTL is already Linux specific, there is no need to parametrize low level lock futex operations and add a sysdep Linux specific implementation. This patch moves the relevant Linux code to nptl one. Checked on x86_64-linux-gnu and i686-linux-gnu. --- sysdeps/nptl/lowlevellock-futex.h | 165 ++++++++++++++---- sysdeps/unix/sysv/linux/lowlevellock-futex.h | 168 ------------------- 2 files changed, 128 insertions(+), 205 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/lowlevellock-futex.h -- 2.17.1 Reviewed-by: Carlos O'Donell diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h index 4cceade0db..392277e8a5 100644 --- a/sysdeps/nptl/lowlevellock-futex.h +++ b/sysdeps/nptl/lowlevellock-futex.h @@ -19,14 +19,60 @@ #ifndef _LOWLEVELLOCK_FUTEX_H #define _LOWLEVELLOCK_FUTEX_H 1 -#include - - -/* Values for 'private' parameter of locking macros. Note pthreadP.h - optimizes for these exact values, though they are not required. */ -#define LLL_PRIVATE 0 -#define LLL_SHARED 128 - +#ifndef __ASSEMBLER__ +# include +# include +# include +#endif + +#define FUTEX_WAIT 0 +#define FUTEX_WAKE 1 +#define FUTEX_REQUEUE 3 +#define FUTEX_CMP_REQUEUE 4 +#define FUTEX_WAKE_OP 5 +#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1) +#define FUTEX_LOCK_PI 6 +#define FUTEX_UNLOCK_PI 7 +#define FUTEX_TRYLOCK_PI 8 +#define FUTEX_WAIT_BITSET 9 +#define FUTEX_WAKE_BITSET 10 +#define FUTEX_WAIT_REQUEUE_PI 11 +#define FUTEX_CMP_REQUEUE_PI 12 +#define FUTEX_PRIVATE_FLAG 128 +#define FUTEX_CLOCK_REALTIME 256 + +#define FUTEX_BITSET_MATCH_ANY 0xffffffff + +/* Values for 'private' parameter of locking macros. Yes, the + definition seems to be backwards. But it is not. The bit will be + reversed before passing to the system call. */ +#define LLL_PRIVATE 0 +#define LLL_SHARED FUTEX_PRIVATE_FLAG + +#ifndef __ASSEMBLER__ + +# if IS_IN (libc) || IS_IN (rtld) +/* In libc.so or ld.so all futexes are private. */ +# define __lll_private_flag(fl, private) \ + ({ \ + /* Prevent warnings in callers of this macro. */ \ + int __lll_private_flag_priv __attribute__ ((unused)); \ + __lll_private_flag_priv = (private); \ + ((fl) | FUTEX_PRIVATE_FLAG); \ + }) +# else +# define __lll_private_flag(fl, private) \ + (((fl) | FUTEX_PRIVATE_FLAG) ^ (private)) +# endif + +# define lll_futex_syscall(nargs, futexp, op, ...) \ + ({ \ + INTERNAL_SYSCALL_DECL (__err); \ + long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \ + __VA_ARGS__); \ + (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \ + ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \ + }) /* For most of these macros, the return value is never really used. Nevertheless, the protocol is that each one returns a negated errno @@ -36,62 +82,107 @@ /* Wait while *FUTEXP == VAL for an lll_futex_wake call on FUTEXP. */ -#define lll_futex_wait(futexp, val, private) \ +# define lll_futex_wait(futexp, val, private) \ lll_futex_timed_wait (futexp, val, NULL, private) -/* Wait until a lll_futex_wake call on FUTEXP, or TIMEOUT elapses. */ -#define lll_futex_timed_wait(futexp, val, timeout, private) \ - -ENOSYS +# define lll_futex_timed_wait(futexp, val, timeout, private) \ + lll_futex_syscall (4, futexp, \ + __lll_private_flag (FUTEX_WAIT, private), \ + val, timeout) /* Verify whether the supplied clockid is supported by lll_futex_clock_wait_bitset. */ -#define lll_futex_supported_clockid(clockid) \ - (0) - -/* Wait until a lll_futex_wake call on FUTEXP, or the absolute TIMEOUT - measured against CLOCKID elapses. CLOCKID may be CLOCK_REALTIME or - CLOCK_MONOTONIC. */ -#define lll_futex_clock_wait_bitset(futexp, val, clockid, timeout, private) \ - -ENOSYS +# define lll_futex_supported_clockid(clockid) \ + ((clockid) == CLOCK_REALTIME || (clockid) == CLOCK_MONOTONIC) + +/* The kernel currently only supports CLOCK_MONOTONIC or + CLOCK_REALTIME timeouts for FUTEX_WAIT_BITSET. We could attempt to + convert others here but currently do not. */ +# define lll_futex_clock_wait_bitset(futexp, val, clockid, timeout, private) \ + ({ \ + long int __ret; \ + if (lll_futex_supported_clockid (clockid)) \ + { \ + const unsigned int clockbit = \ + (clockid == CLOCK_REALTIME) ? FUTEX_CLOCK_REALTIME : 0; \ + const int op = \ + __lll_private_flag (FUTEX_WAIT_BITSET | clockbit, private); \ + \ + __ret = lll_futex_syscall (6, futexp, op, val, \ + timeout, NULL /* Unused. */, \ + FUTEX_BITSET_MATCH_ANY); \ + } \ + else \ + __ret = -EINVAL; \ + __ret; \ + }) /* Wake up up to NR waiters on FUTEXP. */ -#define lll_futex_wake(futexp, nr, private) \ - -ENOSYS +# define lll_futex_wake(futexp, nr, private) \ + lll_futex_syscall (4, futexp, \ + __lll_private_flag (FUTEX_WAKE, private), nr, 0) /* Wake up up to NR_WAKE waiters on FUTEXP. Move up to NR_MOVE of the - rest from waiting on FUTEXP to waiting on MUTEX (a different futex). */ -#define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \ - -ENOSYS + rest from waiting on FUTEXP to waiting on MUTEX (a different futex). + Returns non-zero if error happened, zero if success. */ +# define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \ + lll_futex_syscall (6, futexp, \ + __lll_private_flag (FUTEX_CMP_REQUEUE, private), \ + nr_wake, nr_move, mutex, val) + +/* Wake up up to NR_WAKE waiters on FUTEXP and NR_WAKE2 on FUTEXP2. + Returns non-zero if error happened, zero if success. */ +# define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \ + lll_futex_syscall (6, futexp, \ + __lll_private_flag (FUTEX_WAKE_OP, private), \ + nr_wake, nr_wake2, futexp2, \ + FUTEX_OP_CLEAR_WAKE_IF_GT_ONE) -/* Wake up up to NR_WAKE waiters on FUTEXP and NR_WAKE2 on FUTEXP2. */ -#define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \ - -ENOSYS +/* Priority Inheritance support. */ /* Like lll_futex_wait (FUTEXP, VAL, PRIVATE) but with the expectation that lll_futex_cmp_requeue_pi (FUTEXP, _, _, MUTEX, _, PRIVATE) will be used to do the wakeup. Confers priority-inheritance behavior on the waiter. */ -#define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \ +# define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \ lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private) /* Like lll_futex_wait_requeue_pi, but with a timeout. */ -#define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \ +# define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \ mutex, private) \ - -ENOSYS + lll_futex_syscall (5, futexp, \ + __lll_private_flag (FUTEX_WAIT_REQUEUE_PI \ + | (clockbit), private), \ + val, timeout, mutex) /* Like lll_futex_requeue, but pairs with lll_futex_wait_requeue_pi and inherits priority from the waiter. */ -#define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \ +# define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \ val, private) \ - -ENOSYS + lll_futex_syscall (6, futexp, \ + __lll_private_flag (FUTEX_CMP_REQUEUE_PI, \ + private), \ + nr_wake, nr_move, mutex, val) /* Like lll_futex_wait, but acting as a cancellable entrypoint. */ -#define lll_futex_wait_cancel(futexp, val, private) \ - -ENOSYS +# define lll_futex_wait_cancel(futexp, val, private) \ + ({ \ + int __oldtype = CANCEL_ASYNC (); \ + long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \ + CANCEL_RESET (__oldtype); \ + __err; \ + }) /* Like lll_futex_timed_wait, but acting as a cancellable entrypoint. */ -#define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \ - -ENOSYS +# define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \ + ({ \ + int __oldtype = CANCEL_ASYNC (); \ + long int __err = lll_futex_timed_wait (futexp, val, timeout, private); \ + CANCEL_RESET (__oldtype); \ + __err; \ + }) + +#endif /* !__ASSEMBLER__ */ #endif /* lowlevellock-futex.h */ diff --git a/sysdeps/unix/sysv/linux/lowlevellock-futex.h b/sysdeps/unix/sysv/linux/lowlevellock-futex.h deleted file mode 100644 index b423673ed4..0000000000 --- a/sysdeps/unix/sysv/linux/lowlevellock-futex.h +++ /dev/null @@ -1,168 +0,0 @@ -/* Low-level locking access to futex facilities. Linux version. - Copyright (C) 2005-2019 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 - . */ - -#ifndef _LOWLEVELLOCK_FUTEX_H -#define _LOWLEVELLOCK_FUTEX_H 1 - -#ifndef __ASSEMBLER__ -#include -#include -#include -#endif - -#define FUTEX_WAIT 0 -#define FUTEX_WAKE 1 -#define FUTEX_REQUEUE 3 -#define FUTEX_CMP_REQUEUE 4 -#define FUTEX_WAKE_OP 5 -#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1) -#define FUTEX_LOCK_PI 6 -#define FUTEX_UNLOCK_PI 7 -#define FUTEX_TRYLOCK_PI 8 -#define FUTEX_WAIT_BITSET 9 -#define FUTEX_WAKE_BITSET 10 -#define FUTEX_WAIT_REQUEUE_PI 11 -#define FUTEX_CMP_REQUEUE_PI 12 -#define FUTEX_PRIVATE_FLAG 128 -#define FUTEX_CLOCK_REALTIME 256 - -#define FUTEX_BITSET_MATCH_ANY 0xffffffff - -/* Values for 'private' parameter of locking macros. Yes, the - definition seems to be backwards. But it is not. The bit will be - reversed before passing to the system call. */ -#define LLL_PRIVATE 0 -#define LLL_SHARED FUTEX_PRIVATE_FLAG - -#ifndef __ASSEMBLER__ - -#if IS_IN (libc) || IS_IN (rtld) -/* In libc.so or ld.so all futexes are private. */ -# define __lll_private_flag(fl, private) \ - ({ \ - /* Prevent warnings in callers of this macro. */ \ - int __lll_private_flag_priv __attribute__ ((unused)); \ - __lll_private_flag_priv = (private); \ - ((fl) | FUTEX_PRIVATE_FLAG); \ - }) -#else -# define __lll_private_flag(fl, private) \ - (((fl) | FUTEX_PRIVATE_FLAG) ^ (private)) -#endif - -#define lll_futex_syscall(nargs, futexp, op, ...) \ - ({ \ - INTERNAL_SYSCALL_DECL (__err); \ - long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \ - __VA_ARGS__); \ - (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \ - ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \ - }) - -#define lll_futex_wait(futexp, val, private) \ - lll_futex_timed_wait (futexp, val, NULL, private) - -#define lll_futex_timed_wait(futexp, val, timeout, private) \ - lll_futex_syscall (4, futexp, \ - __lll_private_flag (FUTEX_WAIT, private), \ - val, timeout) - -/* Verify whether the supplied clockid is supported by - lll_futex_clock_wait_bitset. */ -#define lll_futex_supported_clockid(clockid) \ - ((clockid) == CLOCK_REALTIME || (clockid) == CLOCK_MONOTONIC) - -/* The kernel currently only supports CLOCK_MONOTONIC or - CLOCK_REALTIME timeouts for FUTEX_WAIT_BITSET. We could attempt to - convert others here but currently do not. */ -#define lll_futex_clock_wait_bitset(futexp, val, clockid, timeout, private) \ - ({ \ - long int __ret; \ - if (lll_futex_supported_clockid (clockid)) \ - { \ - const unsigned int clockbit = \ - (clockid == CLOCK_REALTIME) ? FUTEX_CLOCK_REALTIME : 0; \ - const int op = \ - __lll_private_flag (FUTEX_WAIT_BITSET | clockbit, private); \ - \ - __ret = lll_futex_syscall (6, futexp, op, val, \ - timeout, NULL /* Unused. */, \ - FUTEX_BITSET_MATCH_ANY); \ - } \ - else \ - __ret = -EINVAL; \ - __ret; \ - }) - -#define lll_futex_wake(futexp, nr, private) \ - lll_futex_syscall (4, futexp, \ - __lll_private_flag (FUTEX_WAKE, private), nr, 0) - -/* Returns non-zero if error happened, zero if success. */ -#define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \ - lll_futex_syscall (6, futexp, \ - __lll_private_flag (FUTEX_CMP_REQUEUE, private), \ - nr_wake, nr_move, mutex, val) - -/* Returns non-zero if error happened, zero if success. */ -#define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \ - lll_futex_syscall (6, futexp, \ - __lll_private_flag (FUTEX_WAKE_OP, private), \ - nr_wake, nr_wake2, futexp2, \ - FUTEX_OP_CLEAR_WAKE_IF_GT_ONE) - -/* Priority Inheritance support. */ -#define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \ - lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private) - -#define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \ - mutex, private) \ - lll_futex_syscall (5, futexp, \ - __lll_private_flag (FUTEX_WAIT_REQUEUE_PI \ - | (clockbit), private), \ - val, timeout, mutex) - - -#define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \ - val, private) \ - lll_futex_syscall (6, futexp, \ - __lll_private_flag (FUTEX_CMP_REQUEUE_PI, \ - private), \ - nr_wake, nr_move, mutex, val) - - -/* Cancellable futex macros. */ -#define lll_futex_wait_cancel(futexp, val, private) \ - ({ \ - int __oldtype = CANCEL_ASYNC (); \ - long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \ - CANCEL_RESET (__oldtype); \ - __err; \ - }) - -#define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \ - ({ \ - int __oldtype = CANCEL_ASYNC (); \ - long int __err = lll_futex_timed_wait (futexp, val, timeout, private); \ - CANCEL_RESET (__oldtype); \ - __err; \ - }) - -#endif /* !__ASSEMBLER__ */ - -#endif /* lowlevellock-futex.h */ From patchwork Wed Oct 30 20:00:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 178130 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp1756238ill; Wed, 30 Oct 2019 13:03:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqx++KOGC2ihd6rf8jzI+a9+kYrmaZku4JZXOf74ooDKcjaO05gMqvBGGpWhPVvECUoI0yaW X-Received: by 2002:a17:906:2793:: with SMTP id j19mr11639ejc.55.1572465784323; Wed, 30 Oct 2019 13:03:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572465784; cv=none; d=google.com; s=arc-20160816; b=arRm2W27R+l+Y2aiMa+1smg/9miFVQkQRslwbfqtEh8Vvhfg4jE1CUQsUSw0tkYIGz B7X0govKDJ1iuVkjwkj/peVwNromzIMyWD8H25UO4eiB9HEKAW2K2JwXZRR4LHO/x9BQ JBWTCOMXZaOrVlq0jninVeTi0ZIMmpbLCiY5WO2CW6MOHYC1DoFxK0iQsFFBt8Hi4vRf gRKMcEJAGnx0/qZImFoDX98wnwsfu23BpJkVDX7r0t+1iu6btl0WVDsa8fIRP5uGuAaU 7ADvDJzSoe6zAKTFj2gChkA2SkY+sLAzw5WTVNNO07ifPqCl84jiYnKjXmbpthiNlWNO XSUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=ECsuoWy9mf5dueOC+3sp8CpJI/QDI1ASArqHFHCf0Z8=; b=KP32laLMTH96268AYquKcapz/VyfI9o28zW+BsPGo92XYfmGjAa8MxBel3FzkFTOKM baw6RnSixc3DElII1KS6FyeUAQHFllzsy6kOi19syTsh3NAaVR7kYGaC1AIiSutKw+wu mLnUkSFCaBxGMV1l8l4RW7y+MU81On/pTB5TOgAYC9kkF0FTem2EMmogm3Xjkx3IBGH8 rmToMSSR5FzFfReQOFSbSQgSePsYR0IupzhGV0xa/SSi/XflFld5NqG4BR+UrAp3sJQK n+8dNyYi6MkDoNCtgNdpIrdlxochSs54Q+wZPvtpLS8yI/1mam9zQqlSTFwDQ8eqdhqe r2+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=yrEEDQE3; dkim=pass header.i=@linaro.org header.s=google header.b=yQ4b2X4l; spf=pass (google.com: domain of libc-alpha-return-106482-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106482-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id k1si1971128eja.146.2019.10.30.13.03.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 13:03:04 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-106482-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=yrEEDQE3; dkim=pass header.i=@linaro.org header.s=google header.b=yQ4b2X4l; spf=pass (google.com: domain of libc-alpha-return-106482-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106482-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=Ce+he1B4nHkOZHk9fb97jt0XeDjlTcJ BSvt3AjCzUPQ+YZ5FudlIy6TLlPOXTBEk1JxTWCrmsgTo7V9DTYfj93qP2WakDdE gplkU6XJ8ZBwBmCPbHF2WB32QeoWgb50ZuDzPJ7VNb/etyqcB7lvFy/sO781a0MX yyE5DpqmempM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=40naVJiwa63BUd93CqDg/ViiM1o=; b=yrEED QE3r5QB7BPbvrZ+WzR7edgWhRj6uGG8zxAKz2lyq8i43mcaGW8QfUfHIAq1swqIQ nTQrybA/q8vsukAx3DJ2yb/H8uCaSUz032mWLZNWYRZQpyPEr87WyTiW93VovKL+ ESSmpnrLSnfN8Poy6HZlCq67SRoaG8B4Ws90P4= Received: (qmail 83600 invoked by alias); 30 Oct 2019 20:01:37 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 78650 invoked by uid 89); 30 Oct 2019 20:01:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=lowlevel, tsp, low-level X-HELO: mail-qt1-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ECsuoWy9mf5dueOC+3sp8CpJI/QDI1ASArqHFHCf0Z8=; b=yQ4b2X4luOi10TfZjpENzvQM1Z0MKreB8b15fJG79zHXNf29h8E8kjYTI+SJi4oZmB 7TnPoXwn5w2zEd1jRm2/HLG62Ne7rv/rKcwDWJWsgz0v8QVw5Vt/4p9eGTN9MB2Ljk+F 2TeL7GLX3VS9Ta7QfCWQXfO3tapBvRWb1GMrWOrxWo44ssh42dbJLGagxZnYjotSP6+a sbh7DdllC5dwe+ZN/474XMEt+pp1kTprfup97k4NFBlcTc3wfbdc1oQqRnP87YSfdzVV rwZ+uugqzAQkxZoRSeWz+5Q9q2rl63MovXzmbknLNHlRdnXpzb/hrUME7W5/Kkvn1uF1 J8dA== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis Subject: [PATCH 3/5] nptl: Replace non cancellable pause/nanosleep with futex Date: Wed, 30 Oct 2019 17:00:50 -0300 Message-Id: <20191030200052.497-3-adhemerval.zanella@linaro.org> In-Reply-To: <20191030200052.497-1-adhemerval.zanella@linaro.org> References: <20191030200052.497-1-adhemerval.zanella@linaro.org> To help y2038 work avoid duplicate all the logic of nanosleep on non cancellable version, the patch replaces it with a new futex operation, lll_timedwait. The changes are: - Add a expected value for __lll_clocklock_wait, so it can be used to wait for generic values. - Remove its internal atomic operation and move the logic to __lll_clocklock. It makes __lll_clocklock_wait even more generic and __lll_clocklock slight faster on fast-path (since it won't require a function call anymore). - Add lll_timedwait, which uses __lll_clocklock_wait, to replace both __pause_nocancel and __nanosleep_nocancel. It also allows remove the sparc32 __lll_clocklock_wait implementation (since it is similar to the generic one). Checked on x86_64-linux-gnu, sparcv9-linux-gnu, and i686-linux-gnu. --- nptl/lll_timedlock_wait.c | 35 ++++++++-------- nptl/pthread_mutex_lock.c | 3 +- nptl/pthread_mutex_timedlock.c | 20 ++-------- sysdeps/nptl/lowlevellock.h | 15 ++++++- sysdeps/sparc/sparc32/lll_timedlock_wait.c | 1 - sysdeps/sparc/sparc32/lowlevellock.c | 42 -------------------- sysdeps/unix/sysv/linux/sparc/lowlevellock.h | 22 +++++++++- 7 files changed, 56 insertions(+), 82 deletions(-) delete mode 100644 sysdeps/sparc/sparc32/lll_timedlock_wait.c -- 2.17.1 Reviewed-by: Carlos O'Donell diff --git a/nptl/lll_timedlock_wait.c b/nptl/lll_timedlock_wait.c index cd3cc3d371..952b042555 100644 --- a/nptl/lll_timedlock_wait.c +++ b/nptl/lll_timedlock_wait.c @@ -25,39 +25,38 @@ int -__lll_clocklock_wait (int *futex, clockid_t clockid, +__lll_clocklock_wait (int *futex, int val, clockid_t clockid, const struct timespec *abstime, int private) { - /* Reject invalid timeouts. */ - if (! valid_nanoseconds (abstime->tv_nsec)) - return EINVAL; + struct timespec ts, *tsp = NULL; - /* Try locking. */ - while (atomic_exchange_acq (futex, 2) != 0) + if (abstime != NULL) { - struct timespec ts; + /* Reject invalid timeouts. */ + if (! valid_nanoseconds (abstime->tv_nsec)) + return EINVAL; - /* Get the current time. This can only fail if clockid is not - valid. */ + /* Get the current time. This can only fail if clockid is not valid. */ if (__glibc_unlikely (__clock_gettime (clockid, &ts) != 0)) return EINVAL; /* Compute relative timeout. */ - struct timespec rt; - rt.tv_sec = abstime->tv_sec - ts.tv_sec; - rt.tv_nsec = abstime->tv_nsec - ts.tv_nsec; - if (rt.tv_nsec < 0) + ts.tv_sec = abstime->tv_sec - ts.tv_sec; + ts.tv_nsec = abstime->tv_nsec - ts.tv_nsec; + if (ts.tv_nsec < 0) { - rt.tv_nsec += 1000000000; - --rt.tv_sec; + ts.tv_nsec += 1000000000; + --ts.tv_sec; } - if (rt.tv_sec < 0) + if (ts.tv_sec < 0) return ETIMEDOUT; - /* If *futex == 2, wait until woken or timeout. */ - lll_futex_timed_wait (futex, 2, &rt, private); + tsp = &ts; } + /* If *futex == val, wait until woken or timeout. */ + lll_futex_timed_wait (futex, val, tsp, private); + return 0; } diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index 0ab890d815..ace436d5a6 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -434,7 +434,8 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) /* Delay the thread indefinitely. */ while (1) - __pause_nocancel (); + lll_timedwait (&(int){0}, 0, 0 /* ignored */, NULL, + private); } oldval = mutex->__data.__lock; diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index c9bb3b9176..490064d8cf 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -401,22 +401,10 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, /* Delay the thread until the timeout is reached. Then return ETIMEDOUT. */ - struct timespec reltime; - struct timespec now; - - INTERNAL_SYSCALL (clock_gettime, __err, 2, clockid, - &now); - reltime.tv_sec = abstime->tv_sec - now.tv_sec; - reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec; - if (reltime.tv_nsec < 0) - { - reltime.tv_nsec += 1000000000; - --reltime.tv_sec; - } - if (reltime.tv_sec >= 0) - while (__nanosleep_nocancel (&reltime, &reltime) != 0) - continue; - + do + e = lll_timedwait (&(int){0}, 0, clockid, abstime, + private); + while (e != ETIMEDOUT); return ETIMEDOUT; } diff --git a/sysdeps/nptl/lowlevellock.h b/sysdeps/nptl/lowlevellock.h index e3f006537a..92298f3b7e 100644 --- a/sysdeps/nptl/lowlevellock.h +++ b/sysdeps/nptl/lowlevellock.h @@ -21,6 +21,7 @@ #include #include +#include /* Low-level locks use a combination of atomic operations (to acquire and release lock ownership) and futex operations (to block until the state @@ -121,10 +122,12 @@ extern void __lll_lock_wait (int *futex, int private) attribute_hidden; #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private) -extern int __lll_clocklock_wait (int *futex, clockid_t, +extern int __lll_clocklock_wait (int *futex, int val, clockid_t, const struct timespec *, int private) attribute_hidden; +#define lll_timedwait(futex, val, clockid, abstime, private) \ + __lll_clocklock_wait (futex, val, clockid, abstime, private) /* As __lll_lock, but with an absolute timeout measured against the clock specified in CLOCKID. If the timeout occurs then return ETIMEDOUT. If @@ -136,7 +139,15 @@ extern int __lll_clocklock_wait (int *futex, clockid_t, \ if (__glibc_unlikely \ (atomic_compare_and_exchange_bool_acq (__futex, 1, 0))) \ - __val = __lll_clocklock_wait (__futex, clockid, abstime, private); \ + { \ + while (atomic_exchange_acq (futex, 2) != 0) \ + { \ + __val = __lll_clocklock_wait (__futex, 2, clockid, \ + abstime, private); \ + if (__val == EINVAL || __val == ETIMEDOUT) \ + break; \ + } \ + } \ __val; \ }) #define lll_clocklock(futex, clockid, abstime, private) \ diff --git a/sysdeps/sparc/sparc32/lll_timedlock_wait.c b/sysdeps/sparc/sparc32/lll_timedlock_wait.c deleted file mode 100644 index bd639a7091..0000000000 --- a/sysdeps/sparc/sparc32/lll_timedlock_wait.c +++ /dev/null @@ -1 +0,0 @@ -/* __lll_clocklock_wait is in lowlevellock.c. */ diff --git a/sysdeps/sparc/sparc32/lowlevellock.c b/sysdeps/sparc/sparc32/lowlevellock.c index 074ecf0636..1a0ab4d9f2 100644 --- a/sysdeps/sparc/sparc32/lowlevellock.c +++ b/sysdeps/sparc/sparc32/lowlevellock.c @@ -50,46 +50,4 @@ __lll_lock_wait (int *futex, int private) } while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0); } - - -int -__lll_clocklock_wait (int *futex, clockid_t clockid, - const struct timespec *abstime, int private) -{ - /* Reject invalid timeouts. */ - if (! valid_nanoseconds (abstime->tv_nsec)) - return EINVAL; - - do - { - struct timespec ts; - struct timespec rt; - - /* Get the current time. This can only fail if clockid is not - valid. */ - if (__glibc_unlikely (__clock_gettime (clockid, &ts) != 0)) - return EINVAL; - - /* Compute relative timeout. */ - rt.tv_sec = abstime->tv_sec - ts.tv_sec; - rt.tv_nsec = abstime->tv_nsec - ts.tv_nsec; - if (rt.tv_nsec < 0) - { - rt.tv_nsec += 1000000000; - --rt.tv_sec; - } - - /* Already timed out? */ - if (rt.tv_sec < 0) - return ETIMEDOUT; - - /* Wait. */ - int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1); - if (oldval != 0) - lll_futex_timed_wait (futex, 2, &rt, private); - } - while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0); - - return 0; -} #endif diff --git a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h index 34441f8ff5..6c005e8ffa 100644 --- a/sysdeps/unix/sysv/linux/sparc/lowlevellock.h +++ b/sysdeps/unix/sysv/linux/sparc/lowlevellock.h @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -75,9 +76,13 @@ __lll_cond_lock (int *futex, int private) #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private) -extern int __lll_clocklock_wait (int *futex, clockid_t, const struct timespec *, +extern int __lll_clocklock_wait (int *futex, clockid_t, int val, + const struct timespec *, int private) attribute_hidden; +#define lll_timedwait(futex, val, clockid, abstime, private) \ + __lll_clocklock_wait (futex, val, clockid, abstime, private) + static inline int __attribute__ ((always_inline)) __lll_clocklock (int *futex, clockid_t clockid, @@ -87,7 +92,20 @@ __lll_clocklock (int *futex, clockid_t clockid, int result = 0; if (__glibc_unlikely (val != 0)) - result = __lll_clocklock_wait (futex, clockid, abstime, private); + { + do + { + int oldval = atomic_compare_and_exchange_val_24_acq (futex, val, 1); + if (oldval != 0) + { + result = __lll_clocklock_wait (futex, 2, clockid, abstime, + private); + if (result == EINVAL || result == ETIMEDOUT) + break; + } + } + while (atomic_compare_and_exchange_val_24_acq (futex, val, 0) != 0); + } return result; } #define lll_clocklock(futex, clockid, abstime, private) \ From patchwork Wed Oct 30 20:00:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 178126 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp1754649ill; Wed, 30 Oct 2019 13:01:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqy0ydOBOZ9fXWD7t7eL6NkfCHbaf7B3V6yJM8NJHfRV+1lBLxK+XPr50Mvu4yK0R3tJpzRd X-Received: by 2002:a50:b634:: with SMTP id b49mr1691651ede.77.1572465713643; Wed, 30 Oct 2019 13:01:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572465713; cv=none; d=google.com; s=arc-20160816; b=XIrr6mzvFh3iXwJFSq95zNk95M/RHWqmiMV1uNPnTpI3AA5cn9zMquBw9buOLjaK/2 bsbfWO8jYGx2Hi9x/j6Dmnys+fkScbe/M/fQZw+syu+jB4Wm6yMvFMm7PM2MXcRQ+J+l 3OVm9HKKLkMCvoKmgpBejINcfktXIxmZt9dHgfNfSFp2FPCW+NxP2VJfRwB6f58MggyH 8lQk6+BmWgAvLVYbx6LHRGomZUKmOV39urulKW3KdS9l+S3hguyOE8BhW9dz5IX85rTo f71ey/n4EUQ6wf/DY5kTRt2amdjotr56IFglO5G0iTuLY4sDg1SpVoUyAlra2QlTohKE HG5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=HMORJRzuUp22hpsBb0hH5UhVOgJg45YFpuVBvMQwtus=; b=zwCW77gs+3sJCRhftyU49JGOYvUioWEved0FVBfe7tCDkqH5rtStiiCN1NKub+ZJmQ sQCeKx5Ipbp/y8HYUEEcRPAEFf7oFPNsmmbVuxK5oBjgg4y1aFCwVywz/sAP4nFovCIZ oXLwDDb2oVPtbxPo7FGQr6kYrys9+Sclwdeduiyq2OIOX5TlzBLtNS0rf51ijbyZToOl lMehYZbQWTM8ZoIrIy91QRyWKk0auiKFIld7HHMhNO0gJu++2Ad4NrTUgK/h5feeAuAl 5mDYVjg049lOy0xZNITdQNjmpJ7tvV2utwd5hBJ4AEpGfzt9UG9qemyX8C1iIJILWi7M D0Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=WnRez6xZ; dkim=pass header.i=@linaro.org header.s=google header.b=AxnukSJ3; spf=pass (google.com: domain of libc-alpha-return-106478-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106478-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id n19si1840779ejk.264.2019.10.30.13.01.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 13:01:53 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-106478-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=WnRez6xZ; dkim=pass header.i=@linaro.org header.s=google header.b=AxnukSJ3; spf=pass (google.com: domain of libc-alpha-return-106478-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106478-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=lTeW32f6h5/DfgtOb3ArqwPl0wDJNzF yEnICJ+bIhzOsQXyvT8n4WerbUqOl2upYUCGWygEdsBL6TehMUEBo4xzP/tTe/JT HMsG078eolUHREoyEjoPQlUcyjXKXmL/2G4Hn3Spx332YtLvzKn58Hzd1ZsbRWub 1r9YzDvm9O7M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=2yrUQWudG1ZinOAS2XtcCo39LgI=; b=WnRez 6xZj0OEpQR2Vl5goOqyI/liLjvj3gPLxfdX5o7+ighUehdjhEggGf8zDrFF5d2ss O05RmtEslDZejRv5xMeg36vJdv73MxM1poIiq4EuBExw4BE92aIX2yynqDKgXDg1 u6shJqil1mV4yr+/ZehAdHlxFpfb750G78MEjk= Received: (qmail 81883 invoked by alias); 30 Oct 2019 20:01:31 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 77050 invoked by uid 89); 30 Oct 2019 20:01:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-qk1-f195.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HMORJRzuUp22hpsBb0hH5UhVOgJg45YFpuVBvMQwtus=; b=AxnukSJ3PAlycEhk6Jv+RbGMlL9NJdpYCo/udifCdsZ5MAnrV7e2PBX73GrchlnJyb bD3QEIyP+lB5T7FaIDdfAaoo5+5aqVTCrQjh9yxFr+w+rZzvt2Akg5MN81oyYUn+Q2Ae jd7WS110pgmWbn5CE/qfyONH6P6qVY2xI1ooTJsT/LL3PdjID9a0E+vLNtwlg6Hmq1fq RP5eQXRaXQwuB2z7gmV0u0srSQE+8qz8Q9lIaQzYYWapvOjCnpPM48/XNyEXiqWpYS2t MoN3egYUImu2hAtgyXAkPnDsxJPeQqGAxQM4pW323Oky0SrpERtwmExTBYQJjajSComz ds3Q== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis Subject: [PATCH 4/5] Remove pause and nanosleep not cancel wrappers Date: Wed, 30 Oct 2019 17:00:51 -0300 Message-Id: <20191030200052.497-4-adhemerval.zanella@linaro.org> In-Reply-To: <20191030200052.497-1-adhemerval.zanella@linaro.org> References: <20191030200052.497-1-adhemerval.zanella@linaro.org> Since they are not used any longer. --- sysdeps/generic/not-cancel.h | 4 --- sysdeps/unix/sysv/linux/Makefile | 4 +-- sysdeps/unix/sysv/linux/nanosleep_nocancel.c | 29 ----------------- sysdeps/unix/sysv/linux/not-cancel.h | 8 ----- sysdeps/unix/sysv/linux/pause_nocancel.c | 33 -------------------- 5 files changed, 2 insertions(+), 76 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/nanosleep_nocancel.c delete mode 100644 sysdeps/unix/sysv/linux/pause_nocancel.c -- 2.17.1 Reviewed-by: Carlos O'Donell diff --git a/sysdeps/generic/not-cancel.h b/sysdeps/generic/not-cancel.h index 5df48f3da7..d3b53e48cf 100644 --- a/sysdeps/generic/not-cancel.h +++ b/sysdeps/generic/not-cancel.h @@ -49,10 +49,6 @@ (void) __writev (fd, iov, n) # define __waitpid_nocancel(pid, stat_loc, options) \ __waitpid (pid, stat_loc, options) -#define __pause_nocancel() \ - __pause () -#define __nanosleep_nocancel(requested_time, remaining) \ - __nanosleep (requested_time, remaining) #define __fcntl64_nocancel(fd, cmd, ...) \ __fcntl64 (fd, cmd, __VA_ARGS__) diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index bbab8f8cc3..ad6821ecf6 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -212,10 +212,10 @@ endif ifeq ($(subdir),io) sysdep_routines += xstatconv internal_statvfs internal_statvfs64 \ sync_file_range fallocate fallocate64 \ - close_nocancel fcntl_nocancel nanosleep_nocancel \ + close_nocancel fcntl_nocancel \ open_nocancel open64_nocancel \ openat_nocancel openat64_nocancel \ - pause_nocancel read_nocancel pread64_nocancel \ + read_nocancel pread64_nocancel \ waitpid_nocancel write_nocancel statx_cp sysdep_headers += bits/fcntl-linux.h diff --git a/sysdeps/unix/sysv/linux/nanosleep_nocancel.c b/sysdeps/unix/sysv/linux/nanosleep_nocancel.c deleted file mode 100644 index d6442bf4f7..0000000000 --- a/sysdeps/unix/sysv/linux/nanosleep_nocancel.c +++ /dev/null @@ -1,29 +0,0 @@ -/* Linux nanosleep syscall implementation -- non-cancellable. - Copyright (C) 2018-2019 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 - -int -__nanosleep_nocancel (const struct timespec *requested_time, - struct timespec *remaining) -{ - return INLINE_SYSCALL_CALL (nanosleep, requested_time, remaining); -} -hidden_def (__nanosleep_nocancel) diff --git a/sysdeps/unix/sysv/linux/not-cancel.h b/sysdeps/unix/sysv/linux/not-cancel.h index 041fd00111..192a0a5195 100644 --- a/sysdeps/unix/sysv/linux/not-cancel.h +++ b/sysdeps/unix/sysv/linux/not-cancel.h @@ -72,12 +72,6 @@ __writev_nocancel_nostatus (int fd, const struct iovec *iov, int iovcnt) /* Uncancelable waitpid. */ __typeof (waitpid) __waitpid_nocancel; -/* Uncancelable pause. */ -__typeof (pause) __pause_nocancel; - -/* Uncancelable nanosleep. */ -__typeof (__nanosleep) __nanosleep_nocancel; - /* Uncancelable fcntl. */ __typeof (__fcntl) __fcntl64_nocancel; @@ -91,8 +85,6 @@ hidden_proto (__pread64_nocancel) hidden_proto (__write_nocancel) hidden_proto (__close_nocancel) hidden_proto (__waitpid_nocancel) -hidden_proto (__pause_nocancel) -hidden_proto (__nanosleep_nocancel) hidden_proto (__fcntl64_nocancel) #endif diff --git a/sysdeps/unix/sysv/linux/pause_nocancel.c b/sysdeps/unix/sysv/linux/pause_nocancel.c deleted file mode 100644 index 04f6fcf5e3..0000000000 --- a/sysdeps/unix/sysv/linux/pause_nocancel.c +++ /dev/null @@ -1,33 +0,0 @@ -/* Linux pause syscall implementation -- non-cancellable. - Copyright (C) 2018-2019 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 - -int -__pause_nocancel (void) -{ -#ifdef __NR_pause - return INLINE_SYSCALL_CALL (pause); -#else - return INLINE_SYSCALL_CALL (ppoll, NULL, 0, NULL, NULL); -#endif -} -hidden_def (__pause_nocancel) From patchwork Wed Oct 30 20:00:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 178127 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp1755015ill; Wed, 30 Oct 2019 13:02:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqzR6U+msbZTYGIQAKc7Zx4vTBkJ92e8U784rp6aLDHZGI6wyIZgGzb0KblWTgA+PWbUzoqv X-Received: by 2002:a17:907:20f3:: with SMTP id rh19mr1330371ejb.289.1572465730607; Wed, 30 Oct 2019 13:02:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572465730; cv=none; d=google.com; s=arc-20160816; b=L8h3ASvnhD+pPLYkBXjSSnvkn3YXB5bn1fuEuNXWyPNBkAfMQTeeYO7tvABQMGPmeT u/fHhxLOEbUJh1p674uHg3sm0grLSYNr4J8FzFAv+vNhlwHoCJxKtrtZwmqvgJN+iEQ6 VqeAT9AdnShIiXSNm7/kFQb4sZVis0WzbmtmWlpuRAWFz7ozBuuXsRIEX1N61rskS4K7 oParKvjV5e5/rMVYHtQio6IUTsAKARLNIdaZNeAi0mOVbp+dczz/7wP/TcYOAL8YdInW bRmZHE4OWHMRvBIWrzAptcA2oWNvzNYshiqr6kg5w1dSA911j5QdweVNh5oxEBESr0Qc dFQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=voaG7fzUcufsQHM3gig2Rk66hRvZi32m9xy6rCDoox8=; b=mJ2XAEOpiFmFAyRemp9xNDKVQIXlcIi8vi3G8zKHJ/LH4LfAF6HDhJEdwvBfsXm6i1 FGDRAPs2WSCQtNK0TicO71flhmHgWQF8Tb/Yd9zBnxI/PBCSuh+5+ean0hdCe58Thpn8 GcQh8p0+BfoIxMhvYGHzfYBAdorNbjYJUW6rq+OJg+a3ldtOyFCXh8h9crGYpx5zX6X6 1OsBEekqNPqka10MPMjUoeQEdI8cFh/yARdseHMHffZA1Z01Li+baifa8T669eD9vi4c 8ep6yOw+nCLUo3EvfUhgsUgLBlSzByF3fOxatDuEUzHTbGUbgpbTrI1Y6g7mc/eue2ei sIgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=qEyraRX4; dkim=pass header.i=@linaro.org header.s=google header.b=E3zguMUX; spf=pass (google.com: domain of libc-alpha-return-106479-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106479-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id f11si1961766ejd.111.2019.10.30.13.02.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 13:02:10 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-106479-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=qEyraRX4; dkim=pass header.i=@linaro.org header.s=google header.b=E3zguMUX; spf=pass (google.com: domain of libc-alpha-return-106479-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-106479-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=azywEbBoR1xm0+OqEG8H+c8ChBALZsl vdrida6IqgVVj//6+JnuhYHjL8jQED4AqxMHDNxh4qfkJ9leVQ+jLjtXz0lfLXw5 dUt88G+lGVNua8y2pGOFKgz5hQLYN0WInSLAbYr3ZLYKRJUYIpDZNMzTBWhmhHBk h9cl0j2YN424= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=xPT/ZBEdyWx7c9P766JyqYnfPHI=; b=qEyra RX4LoU+k2DCmmwnEwgOh7z/agcug7NZd215qYlMIVRg/KUxYXjEkkmBX2Y55yw9b WKMWZYzF/wh9QuNqPVSuKyQQVZ7LTr529J5c4UmJ3sDyo6jf+ZQG6ClVQeNb3n0u Ftn0vdhLjTu2avnU85QHoFFZ0p9hH5gxdYfMmM= Received: (qmail 83239 invoked by alias); 30 Oct 2019 20:01:36 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 79438 invoked by uid 89); 30 Oct 2019 20:01:21 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=ac, caller's X-HELO: mail-qk1-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=voaG7fzUcufsQHM3gig2Rk66hRvZi32m9xy6rCDoox8=; b=E3zguMUX15j1FzMdVGJ/FDtyfS/5Fi73KzAdN00OsCeRdEn7JOjfBpcFzdE/dbI1if 07wPXKvkB//8AmxtiQjROq14NH35pyybUODyOpgGqsc5XGWdr1/sKuI9/j/jeddPPp2j HUkqS3YmxR2MQUd9MWSsrfaYjwinUKs1PezC4ghTUboiGHWajalNjH93RRduFvDuV8KX eV3XKnEYnW8mLT7HsH3Of5H526RArn6jXUNeG8XHEfO1n0GdKbqmG3Lnc6FhBEYgj+5t TtlRwZ7MIZilswF6I6iqd7EVn5qKERRfG7tFO6sWzHwOcR2j5V4JCpFsMBWqxkFxZNpB B8ag== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis Subject: [PATCH 5/5] Refactor PI mutexes internal defintions Date: Wed, 30 Oct 2019 17:00:52 -0300 Message-Id: <20191030200052.497-5-adhemerval.zanella@linaro.org> In-Reply-To: <20191030200052.497-1-adhemerval.zanella@linaro.org> References: <20191030200052.497-1-adhemerval.zanella@linaro.org> This patch adds the generic futex_lock_pi and futex_unlock_pi to wrap around the syscall machinery required to issue the syscall calls. It simplifies a bit the futex code required to implement PI mutexes. No function changes, checked on x86_64-linux-gnu. --- nptl/pthread_mutex_init.c | 13 ++--- nptl/pthread_mutex_lock.c | 24 +++------ nptl/pthread_mutex_timedlock.c | 60 +++++++++------------ nptl/pthread_mutex_trylock.c | 8 ++- nptl/pthread_mutex_unlock.c | 6 +-- sysdeps/nptl/futex-internal.h | 86 +++++++++++++++++++++++++++++++ sysdeps/nptl/lowlevellock-futex.h | 9 ++++ 7 files changed, 137 insertions(+), 69 deletions(-) -- 2.17.1 Reviewed-by: Carlos O'Donell diff --git a/nptl/pthread_mutex_init.c b/nptl/pthread_mutex_init.c index fe4eeee37c..20800b80f5 100644 --- a/nptl/pthread_mutex_init.c +++ b/nptl/pthread_mutex_init.c @@ -24,6 +24,7 @@ #include "pthreadP.h" #include #include +#include #include @@ -37,19 +38,13 @@ static const struct pthread_mutexattr default_mutexattr = static bool prio_inherit_missing (void) { -#ifdef __NR_futex static int tpi_supported; - if (__glibc_unlikely (tpi_supported == 0)) + if (__glibc_unlikely (atomic_load_relaxed (&tpi_supported) == 0)) { - int lock = 0; - INTERNAL_SYSCALL_DECL (err); - int ret = INTERNAL_SYSCALL (futex, err, 4, &lock, FUTEX_UNLOCK_PI, 0, 0); - assert (INTERNAL_SYSCALL_ERROR_P (ret, err)); - tpi_supported = INTERNAL_SYSCALL_ERRNO (ret, err) == ENOSYS ? -1 : 1; + int e = futex_unlock_pi (&(unsigned int){0}, 0); + atomic_store_relaxed (&tpi_supported, e == ENOSYS ? -1 : 1); } return __glibc_unlikely (tpi_supported < 0); -#endif - return true; } int diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index ace436d5a6..05bba50666 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -24,7 +24,7 @@ #include #include "pthreadP.h" #include -#include +#include #include #ifndef lll_lock_elision @@ -416,21 +416,16 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - INTERNAL_SYSCALL_DECL (__err); - int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, - __lll_private_flag (FUTEX_LOCK_PI, - private), 1, 0); - - if (INTERNAL_SYSCALL_ERROR_P (e, __err) - && (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH - || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)) + int e = futex_lock_pi ((unsigned int *) &mutex->__data.__lock, + NULL, private); + if (e == ESRCH || e == EDEADLK) { - assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK + assert (e != EDEADLK || (kind != PTHREAD_MUTEX_ERRORCHECK_NP && kind != PTHREAD_MUTEX_RECURSIVE_NP)); /* ESRCH can happen only for non-robust PI mutexes where the owner of the lock died. */ - assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH || !robust); + assert (e != ESRCH || !robust); /* Delay the thread indefinitely. */ while (1) @@ -479,11 +474,8 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) /* This mutex is now not recoverable. */ mutex->__data.__count = 0; - INTERNAL_SYSCALL_DECL (__err); - INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, - __lll_private_flag (FUTEX_UNLOCK_PI, - PTHREAD_ROBUST_MUTEX_PSHARED (mutex)), - 0, 0); + futex_unlock_pi ((unsigned int *) &mutex->__data.__lock, + PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); /* To the kernel, this will be visible after the kernel has acquired the mutex in the syscall. */ diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index 490064d8cf..112175eb11 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -377,39 +378,29 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - INTERNAL_SYSCALL_DECL (__err); - - int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, - __lll_private_flag (FUTEX_LOCK_PI, - private), 1, - abstime); - if (INTERNAL_SYSCALL_ERROR_P (e, __err)) + int e = futex_lock_pi ((unsigned int *) &mutex->__data.__lock, + abstime, private); + if (e == ETIMEDOUT) + return ETIMEDOUT; + else if (e == ESRCH || e == EDEADLK) { - if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT) - return ETIMEDOUT; - - if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH - || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK) - { - assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK - || (kind != PTHREAD_MUTEX_ERRORCHECK_NP - && kind != PTHREAD_MUTEX_RECURSIVE_NP)); - /* ESRCH can happen only for non-robust PI mutexes where - the owner of the lock died. */ - assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH - || !robust); - - /* Delay the thread until the timeout is reached. - Then return ETIMEDOUT. */ - do - e = lll_timedwait (&(int){0}, 0, clockid, abstime, - private); - while (e != ETIMEDOUT); - return ETIMEDOUT; - } - - return INTERNAL_SYSCALL_ERRNO (e, __err); + assert (e != EDEADLK + || (kind != PTHREAD_MUTEX_ERRORCHECK_NP + && kind != PTHREAD_MUTEX_RECURSIVE_NP)); + /* ESRCH can happen only for non-robust PI mutexes where + the owner of the lock died. */ + assert (e != ESRCH || !robust); + + /* Delay the thread until the timeout is reached. Then return + ETIMEDOUT. */ + do + e = lll_timedwait (&(int){0}, 0, clockid, abstime, + private); + while (e != ETIMEDOUT); + return ETIMEDOUT; } + else if (e != 0) + return e; oldval = mutex->__data.__lock; @@ -447,11 +438,8 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, /* This mutex is now not recoverable. */ mutex->__data.__count = 0; - INTERNAL_SYSCALL_DECL (__err); - INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, - __lll_private_flag (FUTEX_UNLOCK_PI, - PTHREAD_ROBUST_MUTEX_PSHARED (mutex)), - 0, 0); + futex_unlock_pi ((unsigned int *) &mutex->__data.__lock, + PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); /* To the kernel, this will be visible after the kernel has acquired the mutex in the syscall. */ diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c index 87e87c013a..d24bb58a8b 100644 --- a/nptl/pthread_mutex_trylock.c +++ b/nptl/pthread_mutex_trylock.c @@ -21,6 +21,7 @@ #include #include "pthreadP.h" #include +#include #ifndef lll_trylock_elision #define lll_trylock_elision(a,t) lll_trylock(a) @@ -346,11 +347,8 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex) /* This mutex is now not recoverable. */ mutex->__data.__count = 0; - INTERNAL_SYSCALL_DECL (__err); - INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, - __lll_private_flag (FUTEX_UNLOCK_PI, - PTHREAD_ROBUST_MUTEX_PSHARED (mutex)), - 0, 0); + futex_unlock_pi ((unsigned int *) &mutex->__data.__lock, + PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); /* To the kernel, this will be visible after the kernel has acquired the mutex in the syscall. */ diff --git a/nptl/pthread_mutex_unlock.c b/nptl/pthread_mutex_unlock.c index 71038f92e4..53f8b868e4 100644 --- a/nptl/pthread_mutex_unlock.c +++ b/nptl/pthread_mutex_unlock.c @@ -22,6 +22,7 @@ #include "pthreadP.h" #include #include +#include #ifndef lll_unlock_elision #define lll_unlock_elision(a,b,c) ({ lll_unlock (a,c); 0; }) @@ -277,9 +278,8 @@ __pthread_mutex_unlock_full (pthread_mutex_t *mutex, int decr) if (((l & FUTEX_WAITERS) != 0) || (l != THREAD_GETMEM (THREAD_SELF, tid))) { - INTERNAL_SYSCALL_DECL (__err); - INTERNAL_SYSCALL (futex, __err, 2, &mutex->__data.__lock, - __lll_private_flag (FUTEX_UNLOCK_PI, private)); + futex_unlock_pi ((unsigned int *) &mutex->__data.__lock, + private); break; } } diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index 76921466f0..2a8e3fb323 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -381,4 +381,90 @@ futex_wake (unsigned int* futex_word, int processes_to_wake, int private) } } +/* The operation checks the value of the futer, if the value is 0, then the + it is atomically set to the caller's thread ID. If the futex value is + nonzero, it is atomically sets the FUTEX_WAITERS bit, which signals wrt + other futex owner that it cannot unlock the futex in user space + atomically by setting its value to 0. + + If more than one wait operations is issued, the enqueueing of the waiters + 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. + + Returns: + + - 0 if woken by a PI unlock operation or spuriously. + - EAGAIN if the futex owner thread ID is about to exit, but has not yet + handled the state cleanup. + - EDEADLK if the futex is already locked by the caller. + - ESRCH if the thread ID int he futex does not exist. + - EINVAL is the state is corrupted or if there is a waiter on the + 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 (); + } +} + +/* Wakes the top priority waiter that called a futex_lock_pi operation on + the futex. + + Returns the same values as futex_lock_pi under those same conditions; + additionally, returns EPERM when the caller is not allowed to attach + itself to the futex. */ +static __always_inline int +futex_unlock_pi (unsigned int *futex_word, int private) +{ + int err = lll_futex_timed_unlock_pi (futex_word, private); + switch (err) + { + case 0: + case -EAGAIN: + case -EINTR: + case -ETIMEDOUT: + case -ESRCH: + case -EDEADLK: + case -ENOSYS: + case -EPERM: /* The caller is not allowed to attach itself to the futex. + Used to check if PI futexes are supported by the + kernel. */ + return -err; + + case -EINVAL: /* Either due to wrong alignment or due to the timeout not + being normalized. Must have been caused by a glibc or + application bug. */ + case -EFAULT: /* Must have been caused by a glibc or application bug. */ + /* No other errors are documented at this time. */ + default: + futex_fatal_error (); + } +} + #endif /* futex-internal.h */ diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h index 392277e8a5..ff0fd4edc4 100644 --- a/sysdeps/nptl/lowlevellock-futex.h +++ b/sysdeps/nptl/lowlevellock-futex.h @@ -140,6 +140,15 @@ /* Priority Inheritance support. */ +#define lll_futex_timed_lock_pi(futexp, abstime, private) \ + lll_futex_syscall (4, futexp, \ + __lll_private_flag (FUTEX_LOCK_PI, private), \ + 0, abstime) + +#define lll_futex_timed_unlock_pi(futexp, private) \ + lll_futex_syscall (4, futexp, \ + __lll_private_flag (FUTEX_UNLOCK_PI, private), \ + 0, 0) /* Like lll_futex_wait (FUTEXP, VAL, PRIVATE) but with the expectation that lll_futex_cmp_requeue_pi (FUTEXP, _, _, MUTEX, _, PRIVATE) will