From patchwork Fri Nov 27 11:36:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 41201 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 A669C3972834; Fri, 27 Nov 2020 11:36:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A669C3972834 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1606476991; bh=4nXs9tUnN+GHMjV/t4unRFga2LpVEoyAKH/5MVSrb34=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=iFfzTxvet8JH18eCEJsjYPYmU2YO1rlwl28qrgLSpeAnQL5RiGA6U5DdoKAQeDD2g V452N6CJl7JTN83vMVCfAtmexa1Z1x6m0W9gnL9cvfqz3UmRF83n84i+VOw7S8osEJ aTpRjChuL2zysnaUoyARPpg7M6lEZwYyPY4Ufwrg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by sourceware.org (Postfix) with ESMTPS id 2B8BF3857801 for ; Fri, 27 Nov 2020 11:36:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2B8BF3857801 Received: by mail-qv1-xf31.google.com with SMTP id n9so2180816qvp.5 for ; Fri, 27 Nov 2020 03:36:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4nXs9tUnN+GHMjV/t4unRFga2LpVEoyAKH/5MVSrb34=; b=jZM3QEmA8JJz7/H9DMuM1FF7GhOyC8IP6xvb9rIgiz90v+54IAX4HHVoCClX3GaDy6 oegIesqiNZ4xDjIob1bbkkI55rJXrIjXjrYrYbrm2C+wsftISAcFjZOmIZTNrmzDFbnn yy7Ez9pSj5moJ6G1RznAeiENMaehOPSzUNzjGEAXcPip6dzhAQzlYk4hHMsipLmAphy4 Z+U9zx17btMquwCoxipE20Rh5wmvyom7qIPWgqEheyJpV6PN0IeKt/f5Qi2+7mR8XxYp dHrCiFAKy14lVkAXe8TPX9vFWJi0EdE6AFlwlvnPSF9HOa4WSL4YzW/1/+3B56yWIDt5 u19A== X-Gm-Message-State: AOAM5324IUwoC665Dj/34NOeaFnG3br481JLXK6RQS5Jjcdz1Yll8wFK pcGGpa7EPghw08V8teQKU6YNcnup2JDohw== X-Google-Smtp-Source: ABdhPJwUbZ6Adfq2HHfSjkAiScdg5W2PgWOcYzcgzZMeJTFD9cAR6u5iOlmwp59qGCoNhGXf08vlGw== X-Received: by 2002:a0c:f3d3:: with SMTP id f19mr7635800qvm.27.1606476987423; Fri, 27 Nov 2020 03:36:27 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id w9sm5714269qti.45.2020.11.27.03.36.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 03:36:26 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2] nptl: Add EOVERFLOW checks for futex calls Date: Fri, 27 Nov 2020 08:36:21 -0300 Message-Id: <20201127113621.914717-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <4d190cba-b57e-3b8e-53e4-9f49b6ffa1b5@linaro.org> References: <4d190cba-b57e-3b8e-53e4-9f49b6ffa1b5@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Changes from previous version: - Handle __futex_abstimed_wait_cancelable64. Reviewed-by: Lukasz Majewski --- Some futex-internal calls require additional check for EOVERFLOW (as indicated by [1] [2] [3]). For both mutex and rwlock code, EOVERFLOW is handle as ETIMEDOUT; since it indicate to the caller that the blocking operation could not be issued. For mutex it avoids a possible issue where PTHREAD_MUTEX_ROBUST_* might assume EOVERFLOW indicate futex has succeed, and for PTHREAD_MUTEX_PP_* it avoid a potential busy infinite loop. For rwlock and semaphores, it also avoids potential busy infinite loops. Checked on x86_64-linux-gnu and i686-linux-gnu, although EOVERFLOW won't be possible with current usage (since all timeouts on 32-bit architectures with 32-bit time_t support will be in the range of 32-bit time_t). [1] https://sourceware.org/pipermail/libc-alpha/2020-November/120079.html [2] https://sourceware.org/pipermail/libc-alpha/2020-November/120080.html [3] https://sourceware.org/pipermail/libc-alpha/2020-November/120127.html --- nptl/pthread_cond_wait.c | 4 ++-- nptl/pthread_mutex_timedlock.c | 6 +++--- nptl/pthread_rwlock_common.c | 14 +++++++------- nptl/sem_waitcommon.c | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/nptl/pthread_cond_wait.c b/nptl/pthread_cond_wait.c index 685dbca32f..02d11c61db 100644 --- a/nptl/pthread_cond_wait.c +++ b/nptl/pthread_cond_wait.c @@ -506,7 +506,7 @@ __pthread_cond_wait_common (pthread_cond_t *cond, pthread_mutex_t *mutex, __pthread_cleanup_pop (&buffer, 0); - if (__glibc_unlikely (err == ETIMEDOUT)) + if (__glibc_unlikely (err == ETIMEDOUT || err == EOVERFLOW)) { __condvar_dec_grefs (cond, g, private); /* If we timed out, we effectively cancel waiting. Note that @@ -515,7 +515,7 @@ __pthread_cond_wait_common (pthread_cond_t *cond, pthread_mutex_t *mutex, __condvar_quiesce_and_switch_g1 and us trying to acquire the lock during cancellation is not possible. */ __condvar_cancel_waiting (cond, seq, g, private); - result = ETIMEDOUT; + result = err; goto done; } else diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index 74adffe790..6c72a36b2b 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -270,7 +270,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, oldval, clockid, abstime, PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); /* The futex call timed out. */ - if (err == ETIMEDOUT) + if (err == ETIMEDOUT || err == EOVERFLOW) return err; /* Reload current lock value. */ oldval = mutex->__data.__lock; @@ -550,8 +550,8 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, int e = __futex_abstimed_wait64 ( (unsigned int *) &mutex->__data.__lock, ceilval | 2, clockid, abstime, PTHREAD_MUTEX_PSHARED (mutex)); - if (e == ETIMEDOUT) - return ETIMEDOUT; + if (e == ETIMEDOUT || e == EOVERFLOW) + return e; } } while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c index 4c9f582d3d..9ef432c474 100644 --- a/nptl/pthread_rwlock_common.c +++ b/nptl/pthread_rwlock_common.c @@ -334,7 +334,7 @@ __pthread_rwlock_rdlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, private); /* We ignore EAGAIN and EINTR. On time-outs, we can just return because we don't need to clean up anything. */ - if (err == ETIMEDOUT) + if (err == ETIMEDOUT || err == EOVERFLOW) return err; } /* It makes sense to not break out of the outer loop here @@ -460,7 +460,7 @@ __pthread_rwlock_rdlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, int err = __futex_abstimed_wait64 (&rwlock->__data.__wrphase_futex, 1 | PTHREAD_RWLOCK_FUTEX_USED, clockid, abstime, private); - if (err == ETIMEDOUT) + if (err == ETIMEDOUT || err == EOVERFLOW) { /* If we timed out, we need to unregister. If no read phase has been installed while we waited, we can just decrement @@ -479,7 +479,7 @@ __pthread_rwlock_rdlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, if (atomic_compare_exchange_weak_relaxed (&rwlock->__data.__readers, &r, r - (1 << PTHREAD_RWLOCK_READER_SHIFT))) - return ETIMEDOUT; + return err; /* TODO Back-off. */ } /* Use the acquire MO fence to mirror the steps taken in the @@ -730,7 +730,7 @@ __pthread_rwlock_wrlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, int err = __futex_abstimed_wait64 (&rwlock->__data.__writers_futex, 1 | PTHREAD_RWLOCK_FUTEX_USED, clockid, abstime, private); - if (err == ETIMEDOUT) + if (err == ETIMEDOUT || err == EOVERFLOW) { if (prefer_writer) { @@ -758,7 +758,7 @@ __pthread_rwlock_wrlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, } /* We cleaned up and cannot have stolen another waiting writer's futex wake-up, so just return. */ - return ETIMEDOUT; + return err; } /* If we got interrupted (EINTR) or the futex word does not have the expected value (EAGAIN), retry after reloading __readers. */ @@ -829,7 +829,7 @@ __pthread_rwlock_wrlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, int err = __futex_abstimed_wait64 (&rwlock->__data.__wrphase_futex, PTHREAD_RWLOCK_FUTEX_USED, clockid, abstime, private); - if (err == ETIMEDOUT) + if (err == ETIMEDOUT || err == EOVERFLOW) { if (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP) { @@ -861,7 +861,7 @@ __pthread_rwlock_wrlock_full64 (pthread_rwlock_t *rwlock, clockid_t clockid, if ((wf & PTHREAD_RWLOCK_FUTEX_USED) != 0) futex_wake (&rwlock->__data.__writers_futex, 1, private); - return ETIMEDOUT; + return err; } /* TODO Back-off. */ } diff --git a/nptl/sem_waitcommon.c b/nptl/sem_waitcommon.c index 6dd4eb97cb..0ac1f139bd 100644 --- a/nptl/sem_waitcommon.c +++ b/nptl/sem_waitcommon.c @@ -191,7 +191,7 @@ __new_sem_wait_slow64 (struct new_sem *sem, clockid_t clockid, documentation. Before Linux 2.6.22, EINTR was also returned on spurious wake-ups; we only support more recent Linux versions, so do not need to consider this here.) */ - if (err == ETIMEDOUT || err == EINTR) + if (err == ETIMEDOUT || err == EINTR || err == EOVERFLOW) { __set_errno (err); err = -1;