From patchwork Mon Nov 30 18:59:11 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: 41236 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 3A4443899073; Mon, 30 Nov 2020 18:59:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A4443899073 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606762761; bh=8lXkXDfG7g1uHDGuGPuDSQV1MZhiqTdlZWmaiKxzCGg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Y2X5FV5A0TSmbZtk1dRiov5xQwbsMTvH8by+F/SmHIYCG7mv5Mr5KVPcMBlLwp4YU sJlR6UIsusVP0uOQ6qavdSg4yiOFJd/RCnp0wmfG6V8idXpnJl4BKAdCf2kDfR0ScE GftiIYpvUg6nb3PhAIblvJUB8OpBIy1hnr4YjGSo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 55097388A43D for ; Mon, 30 Nov 2020 18:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 55097388A43D Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0AUIhALg088645; Mon, 30 Nov 2020 13:59:15 -0500 Received: from ppma04wdc.us.ibm.com (1a.90.2fa9.ip4.static.sl-reverse.com [169.47.144.26]) by mx0a-001b2d01.pphosted.com with ESMTP id 35568r0e8t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Nov 2020 13:59:15 -0500 Received: from pps.filterd (ppma04wdc.us.ibm.com [127.0.0.1]) by ppma04wdc.us.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0AUIXHBi016599; Mon, 30 Nov 2020 18:59:14 GMT Received: from b01cxnp22033.gho.pok.ibm.com (b01cxnp22033.gho.pok.ibm.com [9.57.198.23]) by ppma04wdc.us.ibm.com with ESMTP id 354ysu2y8h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Nov 2020 18:59:14 +0000 Received: from b01ledav005.gho.pok.ibm.com (b01ledav005.gho.pok.ibm.com [9.57.199.110]) by b01cxnp22033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0AUIxDgg32375210 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Nov 2020 18:59:14 GMT Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DAD57AE06A; Mon, 30 Nov 2020 18:59:12 +0000 (GMT) Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7C35EAE05C; Mon, 30 Nov 2020 18:59:12 +0000 (GMT) Received: from localhost (unknown [9.80.215.224]) by b01ledav005.gho.pok.ibm.com (Postfix) with ESMTP; Mon, 30 Nov 2020 18:59:12 +0000 (GMT) To: libc-alpha@sourceware.org Subject: [PATCH v2] nptl: Fix __futex_clocklock64 return error check [BZ #26964] Date: Mon, 30 Nov 2020 15:59:11 -0300 Message-Id: <20201130185911.1916882-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-30_08:2020-11-30, 2020-11-30 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 mlxscore=0 bulkscore=0 clxscore=1011 impostorscore=0 adultscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 lowpriorityscore=0 priorityscore=1501 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011300120 X-Spam-Status: No, score=-11.1 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. Reviewed-by: Adhemerval Zanella --- nptl/Makefile | 2 +- nptl/tst-pthread-timedlock-lockloop.c | 135 ++++++++++++++++++++++++++ sysdeps/nptl/futex-internal.h | 4 +- 3 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 nptl/tst-pthread-timedlock-lockloop.c diff --git a/nptl/Makefile b/nptl/Makefile index a48426a396..42e30a4ce1 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..133aa25f1c --- /dev/null +++ b/nptl/tst-pthread-timedlock-lockloop.c @@ -0,0 +1,135 @@ +/* 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 +#include +#include +#include +#include +#include + +#define NANO_PER_SEC 1000000000LL +#define TIMEOUT (NANO_PER_SEC / 1000LL) +#define NUM_THREADS 50 +#define RETEST_TIMES 100 + +pthread_mutex_t mutex; +int runs; +clockid_t clockid; + +void +signal_handler (int sig_num) +{ + TEST_COMPARE (sig_num, SIGUSR1); +} + +/* Call pthread_mutex_timedlock()/pthread_mutex_unlock() repetitively, hoping + that one of them returns EAGAIN or EINTR unexpectedly. */ +static void * +worker_timedlock (void *arg) +{ + for (unsigned run = 0; run < runs; run++) + { + struct timespec abs_time = timespec_add (xclock_now (CLOCK_REALTIME), + make_timespec (0, 1000000)); + + int ret = pthread_mutex_timedlock (&mutex, &abs_time); + + if (ret == 0) + xpthread_mutex_unlock (&mutex); + + TEST_VERIFY_EXIT (ret == 0 || ret == ETIMEDOUT); + } + return NULL; +} + +static void * +worker_clocklock (void *arg) +{ + for (unsigned run = 0; run < runs; run++) + { + struct timespec time = + timespec_add (xclock_now (clockid), make_timespec (0, 1000000)); + + int ret = pthread_mutex_clocklock (&mutex, clockid, &time); + + if (ret == 0) + xpthread_mutex_unlock (&mutex); + + TEST_VERIFY_EXIT (ret == 0 || ret == ETIMEDOUT); + } + return NULL; +} + +static int +run_test_set (void *(*worker) (void *)) +{ + pthread_t workers[NUM_THREADS]; + + pthread_mutex_init (&mutex, NULL); + + signal (SIGUSR1, signal_handler); + + /* 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. */ + runs = 100; + for (int run = 0; run < RETEST_TIMES; run++) + { + for (int i = 0; i < NUM_THREADS; i++) + workers[i] = xpthread_create (NULL, worker, NULL); + for (int i = 0; i < NUM_THREADS; i++) + xpthread_join (workers[i]); + } + + /* 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; + runs = 1; + for (int i = 0; i < RETEST_TIMES * 1000; i++) + { + xpthread_mutex_lock (&mutex); + thread = xpthread_create (NULL, worker, NULL); + /* Sleep just a little bit to reach the lock on the worker thread. */ + usleep (10); + pthread_kill (thread, SIGUSR1); + xpthread_mutex_unlock (&mutex); + xpthread_join (thread); + } + + return 0; +} + +static int +do_test (void) +{ + run_test_set (worker_timedlock); + clockid = CLOCK_REALTIME; + run_test_set (worker_clocklock); + clockid = CLOCK_MONOTONIC; + run_test_set (worker_clocklock); + return 0; +} + +#include diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index e67803952f..0c9764057c 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -427,10 +427,10 @@ __futex_clocklock64 (int *futex, clockid_t clockid, err = __futex_abstimed_wait64 ((unsigned int *) futex, 2, clockid, abstime, private); if (err == EINVAL || err == ETIMEDOUT || err == EOVERFLOW) - break; + return err; } } - return err; + return 0; } #endif /* futex-internal.h */