[PATCH v2] nptl: Fix __futex_clocklock64 return error check [BZ #26964]
Adhemerval Zanella
adhemerval.zanella@linaro.org
Mon Nov 30 20:04:07 GMT 2020
On 30/11/2020 15:59, Lucas A. M. Magalhaes wrote:
> 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.
LGTM with the above changes, thanks.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> ---
> 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
>
Ok.
> 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.
s/pthrea_/pthread_
> +
> + 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
> + <https://www.gnu.org/licenses/>. */
Maybe a new line after the Copyright header?
> +#include <errno.h>
> +#include <pthread.h>
> +#include <support/check.h>
> +#include <support/xthread.h>
> +#include <signal.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <time.h>
> +#include <support/xtime.h>
> +#include <support/timespec.h>
> +#include <unistd.h>
> +
> +#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);
> +}
Add static modifier for the globals and for the signal handler as well.
> +
> +/* 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++)
Use 'unsigned int'.
> + {
> + 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;
> +}
> +
Ok.
> +static void *
> +worker_clocklock (void *arg)
> +{
> + for (unsigned run = 0; run < runs; run++)
> + {
Use 'unsigned int'.
> + 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;
> +}
> +
Ok.
> +static int
> +run_test_set (void *(*worker) (void *))
> +{
> + pthread_t workers[NUM_THREADS];
> +
> + pthread_mutex_init (&mutex, NULL);
Use xpthread_mutex_init.
> +
> + signal (SIGUSR1, signal_handler);
Move 'signal' call to 'do_test' (you only need to set it once)
and use xsignal instead.
> +
> + /* 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]);
> + }
Ok, so at the end the mutex will be unblocked (the N threads will either
timeout trying to lock or lock/unlock the mutex).
> +
> + /* 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);
> + }
Ok, as before the mutex will be unlocked.
> +
> + return 0;
Either add a xpthread_mutex_destroy or move the pthread_mutex_init out
of run_test_set (initializing a already initialized mutex is UB).
> +}
> +
Ok.
> +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 <support/test-driver.c>
Ok.
> 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);
Move 'err' declaration to within while brackets.
> if (err == EINVAL || err == ETIMEDOUT || err == EOVERFLOW)
> - break;
> + return err;
> }
> }
> - return err;
> + return 0;
> }
>
> #endif /* futex-internal.h */
>
More information about the Libc-alpha
mailing list