From: Szabolcs Nagy <szabolcs.nagy@arm.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org, Florian Weimer <fweimer@redhat.com>,
Aurelien Jarno <aurelien@aurel32.net>
Subject: Re: [PATCH v2] nptl: Handle spurious EINTR when thread cancellation is disabled (BZ#29029)
Date: Tue, 19 Apr 2022 12:10:45 +0000 [thread overview]
Message-ID: <Yl6mxSlOOAs8SZW6@arm.com> (raw)
In-Reply-To: <20220414154947.2187880-1-adhemerval.zanella@linaro.org>
The 04/14/2022 12:49, Adhemerval Zanella via Libc-alpha wrote:
> --- a/nptl/pthread_cancel.c
> +++ b/nptl/pthread_cancel.c
...
> + /* Some syscalls are never restarted after being interrupted by a signal
> + handler, regardless of the use of SA_RESTART (they always fail with
> + EINTR). So pthread_cancel cannot send SIGCANCEL unless the cancellation
> + is enabled and set as asynchronous (in this case the cancellation will
> + be acted in the cancellation handler instead by the syscall wrapper).
> + Otherwise the target thread is set as 'cancelling' (CANCELING_BITMASK)
> + by atomically setting 'cancelhandling' and the cancelation will be acted
> + upon on next cancellation entrypoing in the target thread.
> +
> + It also requires to atomically check if cancellation is enabled and
> + asynchronous, so both cancellation state and type are tracked on
> + 'cancelhandling'. */
> +
> + int result = 0;
> + int oldval = atomic_load_relaxed (&pd->cancelhandling);
> + int newval;
> + do
> {
> - /* A single-threaded process should be able to kill itself, since there
> - is nothing in the POSIX specification that says that it cannot. So
> - we set multiple_threads to true so that cancellation points get
> - executed. */
> - THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
> + newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
> + if (oldval == newval)
> + break;
> +
> + /* If the cancellation is handled asynchronously just send a
> + signal. We avoid this if possible since it's more
> + expensive. */
> + if (cancel_enabled_and_canceled_and_async (newval))
> + {
> + /* Mark the cancellation as "in progress". */
> + int newval2 = oldval | CANCELING_BITMASK;
> + if (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling,
> + &oldval, newval2))
> + continue;
this continue looks wrong, the cas can fail spuriously
(cancelhandling == oldval) and then continue jumps to...
> +
> + if (pd == THREAD_SELF)
> + /* This is not merely an optimization: An application may
> + call pthread_cancel (pthread_self ()) without calling
> + pthread_create, so the signal handler may not have been
> + set up for a self-cancel. */
> + {
> + pd->result = PTHREAD_CANCELED;
> + if ((newval & CANCELTYPE_BITMASK) != 0)
> + __do_cancel ();
> + }
> + else
> + /* The cancellation handler will take care of marking the
> + thread as canceled. */
> + result = __pthread_kill_internal (th, SIGCANCEL);
> +
> + break;
> + }
> +
> + /* A single-threaded process should be able to kill itself, since
> + there is nothing in the POSIX specification that says that it
> + cannot. So we set multiple_threads to true so that cancellation
> + points get executed. */
> + THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
> #ifndef TLS_MULTIPLE_THREADS_IN_TCB
> __libc_multiple_threads = 1;
> #endif
> -
> - THREAD_SETMEM (pd, result, PTHREAD_CANCELED);
> - if (pd->cancelstate == PTHREAD_CANCEL_ENABLE
> - && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
> - __do_cancel ();
> - return 0;
> }
> + while (!atomic_compare_exchange_weak_acquire (&pd->cancelhandling, &oldval,
> + newval));
...here and this cas updates cancelhandling to newval without
actually doing any cancellation.
>
> - return __pthread_kill_internal (th, SIGCANCEL);
> + return result;
> }
next prev parent reply other threads:[~2022-04-19 12:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-14 15:49 Adhemerval Zanella
2022-04-14 18:26 ` Adhemerval Zanella
2022-04-19 10:44 ` Szabolcs Nagy
2022-04-19 12:18 ` Adhemerval Zanella
2022-04-19 12:23 ` Adhemerval Zanella
2022-04-19 12:10 ` Szabolcs Nagy [this message]
2022-04-19 12:30 ` Adhemerval Zanella
2022-04-19 12:46 ` Szabolcs Nagy
2022-04-19 13:12 ` Adhemerval Zanella
2022-07-12 21:27 ` Noah Goldstein
2022-07-12 21:28 ` Noah Goldstein
2022-07-13 12:57 ` Adhemerval Zanella Netto
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Yl6mxSlOOAs8SZW6@arm.com \
--to=szabolcs.nagy@arm.com \
--cc=adhemerval.zanella@linaro.org \
--cc=aurelien@aurel32.net \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).