From patchwork Wed May 26 16:57:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 43589 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 1C6EA384783F; Wed, 26 May 2021 16:57:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C6EA384783F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1622048259; bh=TG3FGP1e32SP9ysExoG4wxFDWI22sTIBCtfnJc0vpk0=; 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=fzlASUAguQ9Ters+38CodCIdHtHlDO1IlLya2wchajQRdZvpvzlOqyGvqTc/4CLq0 qs/OzoFOl2CzlVk/WF8zcXBZCDYNnzIxqmMW+iNTSCPSwJrT3c6hVbWUMqYmx+z7Sp QFcf9CbTY2FuLNuOMifkPWL3kcYJQbmAOYZKsIPI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by sourceware.org (Postfix) with ESMTPS id AD2E4385703A for ; Wed, 26 May 2021 16:57:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AD2E4385703A Received: by mail-qv1-xf32.google.com with SMTP id e8so1076572qvp.7 for ; Wed, 26 May 2021 09:57:35 -0700 (PDT) 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=TG3FGP1e32SP9ysExoG4wxFDWI22sTIBCtfnJc0vpk0=; b=PKtbSeYhB90pgUoS7UQ12OqBwYZBjwbDbgKJ/mv6Q3+bJXTIfUxwe3c8WrqHx6dYRu AsioWu4AVRDFD7uFGBy1tVJgKCCSWt9DbzNS8kPU3NloEgcrbLPq3pERCvx9C0MrE3/f GYSwICMAdzhRQWk8quTIGdXmUp3EC8IGcd+VWHVcNtJukJ9/US1CR/I79QYhD0LnZl5l G2gnu2pwxruVj4Oa67ajCO3uU3plv/UaMRhUyAm85W9tVuoeQdYHsFMrI7EKT4+spkzZ rzDT94Ve6RXLM+AIgnuhtBlGQ93jaI/t/FTo3lNFNMnMsvf48fe9deq7pQGnBMEp/4ly aZ3w== X-Gm-Message-State: AOAM530KMdXS322b4BqrAOV20tPX2atjhxn54sGp4rt3XOIkjMCMU0Bp WNq4QZbACMTHzHQixTgu+KnsFaOhNKIwhg== X-Google-Smtp-Source: ABdhPJwQKHX88EZQSSulG8bLEeNHg2p4wlOrtrDjsVWSg+jvyb6Wi4/CXr7rcdiQOpey0n79K4iW4Q== X-Received: by 2002:ad4:4dc1:: with SMTP id cw1mr43146331qvb.15.1622048254932; Wed, 26 May 2021 09:57:34 -0700 (PDT) Received: from birita.. ([177.194.37.86]) by smtp.googlemail.com with ESMTPSA id k125sm1859761qkf.53.2021.05.26.09.57.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 09:57:34 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 01/11] nptl: Move Linux createthread to nptl Date: Wed, 26 May 2021 13:57:18 -0300 Message-Id: <20210526165728.1772546-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210526165728.1772546-1-adhemerval.zanella@linaro.org> References: <20210526165728.1772546-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" git mv -f sysdeps/unix/sysv/linux/createthread.c nptl/createthread.c No functional change. --- nptl/createthread.c | 124 ++++++++++++++++++-- sysdeps/unix/sysv/linux/createthread.c | 153 ------------------------- 2 files changed, 116 insertions(+), 161 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/createthread.c diff --git a/nptl/createthread.c b/nptl/createthread.c index 46943b33fe..bc3409b326 100644 --- a/nptl/createthread.c +++ b/nptl/createthread.c @@ -1,6 +1,7 @@ -/* Low-level thread creation for NPTL. Stub version. - Copyright (C) 2014-2021 Free Software Foundation, Inc. +/* Low-level thread creation for NPTL. Linux version. + Copyright (C) 2002-2021 Free Software Foundation, Inc. This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,30 +17,137 @@ License along with the GNU C Library; if not, see . */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#ifdef __NR_clone2 +# define ARCH_CLONE __clone2 +#else +# define ARCH_CLONE __clone +#endif + /* See the comments in pthread_create.c for the requirements for these two macros and the create_thread function. */ #define START_THREAD_DEFN \ - static void __attribute__ ((noreturn)) start_thread (void) -#define START_THREAD_SELF THREAD_SELF + static int __attribute__ ((noreturn)) start_thread (void *arg) +#define START_THREAD_SELF arg + +/* pthread_create.c defines this using START_THREAD_DEFN + We need a forward declaration here so we can take its address. */ +static int start_thread (void *arg) __attribute__ ((noreturn)); static int create_thread (struct pthread *pd, const struct pthread_attr *attr, bool *stopped_start, STACK_VARIABLES_PARMS, bool *thread_ran) { - /* If the implementation needs to do some tweaks to the thread after - it has been created at the OS level, it can set STOPPED_START here. */ + /* Determine whether the newly created threads has to be started + stopped since we have to set the scheduling parameters or set the + affinity. */ + bool need_setaffinity = (attr != NULL && attr->extension != NULL + && attr->extension->cpuset != 0); + if (attr != NULL + && (__glibc_unlikely (need_setaffinity) + || __glibc_unlikely ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0))) + *stopped_start = true; pd->stopped_start = *stopped_start; if (__glibc_unlikely (*stopped_start)) - /* See CONCURRENCY NOTES in nptl/pthread_create.c. */ + /* See CONCURRENCY NOTES in nptl/pthread_creat.c. */ lll_lock (pd->lock, LLL_PRIVATE); - return ENOSYS; + /* We rely heavily on various flags the CLONE function understands: + + CLONE_VM, CLONE_FS, CLONE_FILES + These flags select semantics with shared address space and + file descriptors according to what POSIX requires. + + CLONE_SIGHAND, CLONE_THREAD + This flag selects the POSIX signal semantics and various + other kinds of sharing (itimers, POSIX timers, etc.). + + CLONE_SETTLS + The sixth parameter to CLONE determines the TLS area for the + new thread. + + CLONE_PARENT_SETTID + The kernels writes the thread ID of the newly created thread + into the location pointed to by the fifth parameters to CLONE. + + Note that it would be semantically equivalent to use + CLONE_CHILD_SETTID but it is be more expensive in the kernel. + + CLONE_CHILD_CLEARTID + The kernels clears the thread ID of a thread that has called + sys_exit() in the location pointed to by the seventh parameter + to CLONE. + + The termination signal is chosen to be zero which means no signal + is sent. */ + const int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM + | CLONE_SIGHAND | CLONE_THREAD + | CLONE_SETTLS | CLONE_PARENT_SETTID + | CLONE_CHILD_CLEARTID + | 0); + + TLS_DEFINE_INIT_TP (tp, pd); + + if (__glibc_unlikely (ARCH_CLONE (&start_thread, STACK_VARIABLES_ARGS, + clone_flags, pd, &pd->tid, tp, &pd->tid) + == -1)) + return errno; /* It's started now, so if we fail below, we'll have to cancel it and let it clean itself up. */ *thread_ran = true; + /* Now we have the possibility to set scheduling parameters etc. */ + if (attr != NULL) + { + int res; + + /* Set the affinity mask if necessary. */ + if (need_setaffinity) + { + assert (*stopped_start); + + res = INTERNAL_SYSCALL_CALL (sched_setaffinity, pd->tid, + attr->extension->cpusetsize, + attr->extension->cpuset); + + if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res))) + err_out: + { + /* The operation failed. We have to kill the thread. + We let the normal cancellation mechanism do the work. */ + + pid_t pid = __getpid (); + INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid, SIGCANCEL); + + return INTERNAL_SYSCALL_ERRNO (res); + } + } + + /* Set the scheduling parameters. */ + if ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0) + { + assert (*stopped_start); + + res = INTERNAL_SYSCALL_CALL (sched_setscheduler, pd->tid, + pd->schedpolicy, &pd->schedparam); + + if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res))) + goto err_out; + } + } + return 0; } diff --git a/sysdeps/unix/sysv/linux/createthread.c b/sysdeps/unix/sysv/linux/createthread.c deleted file mode 100644 index bc3409b326..0000000000 --- a/sysdeps/unix/sysv/linux/createthread.c +++ /dev/null @@ -1,153 +0,0 @@ -/* Low-level thread creation for NPTL. Linux version. - Copyright (C) 2002-2021 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - -#ifdef __NR_clone2 -# define ARCH_CLONE __clone2 -#else -# define ARCH_CLONE __clone -#endif - -/* See the comments in pthread_create.c for the requirements for these - two macros and the create_thread function. */ - -#define START_THREAD_DEFN \ - static int __attribute__ ((noreturn)) start_thread (void *arg) -#define START_THREAD_SELF arg - -/* pthread_create.c defines this using START_THREAD_DEFN - We need a forward declaration here so we can take its address. */ -static int start_thread (void *arg) __attribute__ ((noreturn)); - -static int -create_thread (struct pthread *pd, const struct pthread_attr *attr, - bool *stopped_start, STACK_VARIABLES_PARMS, bool *thread_ran) -{ - /* Determine whether the newly created threads has to be started - stopped since we have to set the scheduling parameters or set the - affinity. */ - bool need_setaffinity = (attr != NULL && attr->extension != NULL - && attr->extension->cpuset != 0); - if (attr != NULL - && (__glibc_unlikely (need_setaffinity) - || __glibc_unlikely ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0))) - *stopped_start = true; - - pd->stopped_start = *stopped_start; - if (__glibc_unlikely (*stopped_start)) - /* See CONCURRENCY NOTES in nptl/pthread_creat.c. */ - lll_lock (pd->lock, LLL_PRIVATE); - - /* We rely heavily on various flags the CLONE function understands: - - CLONE_VM, CLONE_FS, CLONE_FILES - These flags select semantics with shared address space and - file descriptors according to what POSIX requires. - - CLONE_SIGHAND, CLONE_THREAD - This flag selects the POSIX signal semantics and various - other kinds of sharing (itimers, POSIX timers, etc.). - - CLONE_SETTLS - The sixth parameter to CLONE determines the TLS area for the - new thread. - - CLONE_PARENT_SETTID - The kernels writes the thread ID of the newly created thread - into the location pointed to by the fifth parameters to CLONE. - - Note that it would be semantically equivalent to use - CLONE_CHILD_SETTID but it is be more expensive in the kernel. - - CLONE_CHILD_CLEARTID - The kernels clears the thread ID of a thread that has called - sys_exit() in the location pointed to by the seventh parameter - to CLONE. - - The termination signal is chosen to be zero which means no signal - is sent. */ - const int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM - | CLONE_SIGHAND | CLONE_THREAD - | CLONE_SETTLS | CLONE_PARENT_SETTID - | CLONE_CHILD_CLEARTID - | 0); - - TLS_DEFINE_INIT_TP (tp, pd); - - if (__glibc_unlikely (ARCH_CLONE (&start_thread, STACK_VARIABLES_ARGS, - clone_flags, pd, &pd->tid, tp, &pd->tid) - == -1)) - return errno; - - /* It's started now, so if we fail below, we'll have to cancel it - and let it clean itself up. */ - *thread_ran = true; - - /* Now we have the possibility to set scheduling parameters etc. */ - if (attr != NULL) - { - int res; - - /* Set the affinity mask if necessary. */ - if (need_setaffinity) - { - assert (*stopped_start); - - res = INTERNAL_SYSCALL_CALL (sched_setaffinity, pd->tid, - attr->extension->cpusetsize, - attr->extension->cpuset); - - if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res))) - err_out: - { - /* The operation failed. We have to kill the thread. - We let the normal cancellation mechanism do the work. */ - - pid_t pid = __getpid (); - INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid, SIGCANCEL); - - return INTERNAL_SYSCALL_ERRNO (res); - } - } - - /* Set the scheduling parameters. */ - if ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0) - { - assert (*stopped_start); - - res = INTERNAL_SYSCALL_CALL (sched_setscheduler, pd->tid, - pd->schedpolicy, &pd->schedparam); - - if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res))) - goto err_out; - } - } - - return 0; -}