From patchwork Fri Nov 27 14:33:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lucas A. M. Magalhaes" X-Patchwork-Id: 41211 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 E32EE3973040; Fri, 27 Nov 2020 14:34:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E32EE3973040 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606487645; bh=1PiXh+swJg6HO1d1mVXkB60gTPCCxk97dIi+dw9IV/g=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hTWfiLmoYimxyzXNDLXqlUc5wETDvqthHyPI4k5MamTxTMlYnXVZ20TqqKriP91oo E3OAM9SI3yiy1vqh10acWKRQ0RcTf9F1YQ/NXRdamDCzKQjSYxFoHAsn4zuzxBLMac 19jY8C29f7MB7T2tiDFxTnPe/teXpIXcHZItOgQ0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 5AE513858019 for ; Fri, 27 Nov 2020 14:34:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5AE513858019 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0AREWbba137356 for ; Fri, 27 Nov 2020 09:34:00 -0500 Received: from ppma02dal.us.ibm.com (a.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.10]) by mx0a-001b2d01.pphosted.com with ESMTP id 3531tf2wye-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 27 Nov 2020 09:33:59 -0500 Received: from pps.filterd (ppma02dal.us.ibm.com [127.0.0.1]) by ppma02dal.us.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0ARERBMr014495 for ; Fri, 27 Nov 2020 14:33:59 GMT Received: from b03cxnp08028.gho.boulder.ibm.com (b03cxnp08028.gho.boulder.ibm.com [9.17.130.20]) by ppma02dal.us.ibm.com with ESMTP id 34xthatp7r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 27 Nov 2020 14:33:59 +0000 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0AREXvWg10551928 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 27 Nov 2020 14:33:58 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C9CE7BE054 for ; Fri, 27 Nov 2020 14:33:57 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4E6E1BE058 for ; Fri, 27 Nov 2020 14:33:57 +0000 (GMT) Received: from localhost (unknown [9.80.215.224]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP for ; Fri, 27 Nov 2020 14:33:56 +0000 (GMT) To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Fix __futex_clocklock64 return error check [BZ #26964] Date: Fri, 27 Nov 2020 11:33:55 -0300 Message-Id: <20201127143355.1844509-1-lamm@linux.ibm.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312, 18.0.737 definitions=2020-11-27_06:2020-11-26, 2020-11-27 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 adultscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 lowpriorityscore=0 bulkscore=0 phishscore=0 mlxscore=0 suspectscore=0 clxscore=1015 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011270082 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Lucas A. M. Magalhaes via Libc-alpha" From: "Lucas A. M. Magalhaes" Reply-To: "Lucas A. M. Magalhaes" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The earlier implementation of this, __lll_clocklock, calls lll_clockwait that doesn't return the futex syscall error codes. It always tries again if that fails. However in the current implementation, when the futex returns EAGAIN, __futex_clocklock64 will also return EGAIN, even if the futex is taken. This patch fixes the EAGAIN issue and also adds a check for EINTR. As futex syscall can return EINTR if the thread is interrupted by a signal. In this case I'm assuming the function should continue trying to lock as there is no mention to about it on POSIX. Also add a test for both scenarios. --- nptl/Makefile | 2 +- nptl/tst-pthread-timedlock-lockloop.c | 136 ++++++++++++++++++++++++++ sysdeps/nptl/futex-internal.h | 9 ++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 nptl/tst-pthread-timedlock-lockloop.c diff --git a/nptl/Makefile b/nptl/Makefile index a48426a396..91324e09f2 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -298,7 +298,7 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-thread-affinity-sched \ tst-pthread-defaultattr-free \ tst-pthread-attr-sigmask \ - + tst-pthread-timedlock-lockloop \ tests-container = tst-pthread-getattr diff --git a/nptl/tst-pthread-timedlock-lockloop.c b/nptl/tst-pthread-timedlock-lockloop.c new file mode 100644 index 0000000000..abcc5724cf --- /dev/null +++ b/nptl/tst-pthread-timedlock-lockloop.c @@ -0,0 +1,136 @@ +/* Make sure pthrea_mutex_timedlock doesn't return spurious error codes. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ +#include +#include +#include +#include +#include +#include + +#define NANO_PER_SEC 1000000000LL +#define TIMEOUT (NANO_PER_SEC / 1000LL) +#define NUM_THREADS 50 +#define RETEST_TIMES 100 + +struct worker_args +{ + pthread_mutex_t mtx; + int runs; +}; + +void +signal_handler (int sig_num) +{ + if (sig_num != SIGUSR1) + printf ("Unexpected signal"); +} + +/* Call pthread_mutex_timedlock()/pthread_mutex_unlock() repetitively, hoping + that one of them returns EAGAIN or EINTR unexpectedly. */ +static void * +worker (void *arg) +{ + pthread_mutex_t *mtx = &(((struct worker_args *) arg)->mtx); + struct timespec abs_time; + signal (SIGUSR1, signal_handler); + + for (unsigned run = 0; run < ((struct worker_args *) arg)->runs; run++) + { + clock_gettime (CLOCK_REALTIME, &abs_time); + abs_time.tv_nsec += TIMEOUT; + if (abs_time.tv_nsec >= NANO_PER_SEC) + { + abs_time.tv_sec++; + abs_time.tv_nsec -= NANO_PER_SEC; + } + + int ret = pthread_mutex_timedlock (mtx, &abs_time); + + if (ret == 0) + pthread_mutex_unlock (mtx); + + if (ret == EAGAIN || ret == EINTR) + { + printf ("Unexpected return %d\n", ret); + return (void *) 1; + } + } + return NULL; +} + +static int +do_test (void) +{ + pthread_t *workers = + (pthread_t *) malloc (NUM_THREADS * sizeof (pthread_t)); + + struct worker_args args; + pthread_mutex_init (&(args.mtx), NULL); + + int *thread_ret = NULL, ret = 0; + + /* Run the checks to catch an EAGAIN. */ + /* As there is no way to ensure the error condition, just run the test many + times hoping to catch the error. */ + args.runs = 100; + for (int run = 0; run < RETEST_TIMES; run++) + { + for (int i = 0; i < NUM_THREADS; i++) + { + pthread_create (&workers[i], NULL, worker, (void *) &args); + } + for (int i = 0; i < NUM_THREADS; i++) + { + pthread_join (workers[i], (void **) &thread_ret); + if (thread_ret != NULL) + ++ret; + } + if (ret != 0) + goto err; + } + + /* Run the test to check if we catch an EINTR. */ + /* As there is no way to ensure the error condition, just run the test many + times hoping to catch the error. */ + pthread_t thread; + args.runs = 1; + for (int i = 0; i < RETEST_TIMES * 1000; i++) + { + if (pthread_mutex_lock (&(args.mtx)) != 0) + { + printf ("Mutex lock failed\n"); + goto err; + } + pthread_create (&thread, NULL, worker, (void *) &args); + pthread_kill (thread, SIGUSR1); + pthread_mutex_unlock (&(args.mtx)); + pthread_join (thread, (void **) &thread_ret); + if (thread_ret != NULL) + goto err; + } + + free (workers); + return 0; + +err: + free (workers); + return 1; +} + +#include diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index e67803952f..f16d26d994 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -424,10 +424,19 @@ __futex_clocklock64 (int *futex, clockid_t clockid, { while (atomic_exchange_acq (futex, 2) != 0) { + /* At this point we tried to get the futex but failed and set its + value to 2. However the futex value can be changed by other + thread before this calls the futex syscall. If so the syscall + will return EAGAIN. */ err = __futex_abstimed_wait64 ((unsigned int *) futex, 2, clockid, abstime, private); if (err == EINVAL || err == ETIMEDOUT || err == EOVERFLOW) break; + /* If EAGAIN or EINTR is returned here the error code should be reset + as we will try again to acquire the futex and it may success. + Otherwise the mutex will be locked and the return will not be 0. */ + if (err == EAGAIN || err == EINTR) + err = 0; } } return err;