From patchwork Wed Sep 28 20:46:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58121 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 1B367385AC21 for ; Wed, 28 Sep 2022 20:47:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B367385AC21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398064; bh=0woY+EMgt7ar7mF/5FQNhOpufYgjE/zBTuF4VwR2cfE=; 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=HkhmJmtr1aRL/RWuWsRmGJi7gNO+Bb6T4u10d1BbcKAUr/gQik7p2URS53jysFxbY iKd60CfaITq67+szkRXkyNkwIpQr/Pp6J7VLbE3RMwsriLQxjztvcJcfENQQpfu31N ZXCI+g1cvkWrqAjOpjMfmpURBHYBo0PsRE0htO0c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by sourceware.org (Postfix) with ESMTPS id 7F9483858D37 for ; Wed, 28 Sep 2022 20:46:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7F9483858D37 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-12803ac8113so18842563fac.8 for ; Wed, 28 Sep 2022 13:46:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=0woY+EMgt7ar7mF/5FQNhOpufYgjE/zBTuF4VwR2cfE=; b=CTQLUy9PsX+Gb6Xs46hZPEwqH7zcYgo2LT4mp8PkU9OSvRqP8VvhOGlEtgL8kNPwIO CAcV4bMI3n6SE79njCSqZCfa4xgGW39U8lDkv9HVs7e5fs0HVtKp+cCMn2zxEd+9aTy+ /S4zhA0eEEOwPvJORc8M84AbFtUQ/vdZjt0k+v4a2OOLJPKoE7pdtojI2r11pcq1+Wxq PYqrx5v6IEe49HqKvKqCNC54o3OFAmVT8TAjAnJ7/bxFwdmH9nURhk4vv6kxoIIl5ouH 5XXDauEQHPA0uROp0VdDS3Dhw9mgIxvkt5+YsbHVvB7cpC9XvPhs+NJjtu/dK9pyf3+v EUZw== X-Gm-Message-State: ACrzQf3deW7SH14YzdTNk9C3M98kI+UeFfasAtAWDcZwa8csy+v0IcZM y1zEkxKmuVFjt4Bvyt101Xw4o34Szfsgw0bI X-Google-Smtp-Source: AMsMyM66CSUOmNRDw38OZkUy4WG96+k30/MRKq6ELWFTJOokU6moObQjbgxeivWPeoQsU8xBR2OUbQ== X-Received: by 2002:a05:6871:829:b0:12b:1ca6:6cbc with SMTP id q41-20020a056871082900b0012b1ca66cbcmr6470976oap.4.1664397999517; Wed, 28 Sep 2022 13:46:39 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:39 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 1/7] linux: Do set signal handler if it is already SIG_DFL Date: Wed, 28 Sep 2022 17:46:28 -0300 Message-Id: <20220928204634.3036650-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" There is no need to issue another sigaction is the disposition is already SIG_DFL. Checked on x86_64-linux-gnu. --- sysdeps/unix/sysv/linux/spawni.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index ee843a2247..65ee03c804 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -129,7 +129,7 @@ __spawni_child (void *arguments) else { __libc_sigaction (sig, 0, &sa); - if (sa.sa_handler == SIG_IGN) + if (sa.sa_handler == SIG_IGN || sa.sa_handler == SIG_DFL) continue; sa.sa_handler = SIG_DFL; } From patchwork Wed Sep 28 20:46:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58124 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 D2300385AE5D for ; Wed, 28 Sep 2022 20:48:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D2300385AE5D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398106; bh=cRoyJ4+QQ/pxappsLIUqJluYMLeuDQkXq0m9uoHyv84=; 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=MnKDWL+PVsaCBDHySzfiJQUd9Dtc3/BmyvgLK1OuTHMmyxd37Lq06rI+TsdLVmL8Y RnNzqnB8KTiB9MIejVKLFzEn20/iQdEFLxcwIgSc9hQA5K0Ic68onKarZ/OAYuQ4zJ 5ZZpEi6W+ChCefwoett7WS1GgHzyb9EkSzOIE8Qg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc2e.google.com (mail-oo1-xc2e.google.com [IPv6:2607:f8b0:4864:20::c2e]) by sourceware.org (Postfix) with ESMTPS id EC0E53858D39 for ; Wed, 28 Sep 2022 20:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EC0E53858D39 Received: by mail-oo1-xc2e.google.com with SMTP id c17-20020a4aa4d1000000b0047653e7c5f3so2195396oom.1 for ; Wed, 28 Sep 2022 13:46:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=cRoyJ4+QQ/pxappsLIUqJluYMLeuDQkXq0m9uoHyv84=; b=Iz4Hjk8Xb+1tyiCPIN9iVsNsr2A61RcW61rXF+S/R0GwbR7+JeWFUNGvtgrUYQ0HZL naabYsek4B4kTdBSL6J8i6/tmSMy7TpoSx+g8mg7IID8G0gXQZ7+y5ekd7ykYDXTe6ly d4FcbUcHnbtQ/RNl99z9L2nSEZSK7oMpimJd86mpFEJEt/R3JRR/WKksczywHaCCqloH NW9zPxVmiZ3k9yqNTDFau8/nYjAqnQ2tWlyE0WcUwJtwcro1LjJhFiUEI3F3X6NrSiDu KjTgIf6LfUMOnA6yq7VdTO5WROcoKBlkBGThy3FMr6UWK48Nem9Z7OizNZwfYmLFXsTL AP0A== X-Gm-Message-State: ACrzQf0fqsHr+i+NxIJbTCquYw/MCL1W/AQQ4VCy+3js43hSgl6XcBEr szAB3qnTNuviQoZRN1WtBTo8KCEh9VmLHnP5 X-Google-Smtp-Source: AMsMyM6xrFnNpTTrhVpYa9yXJeV4960ompkgdUPtxKAqLhKo2vz3fvUMOvIQWiMeI9OpJl8/IboLLg== X-Received: by 2002:a4a:1787:0:b0:44d:f068:d0e2 with SMTP id 129-20020a4a1787000000b0044df068d0e2mr13864611ooe.27.1664398000817; Wed, 28 Sep 2022 13:46:40 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:40 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 2/7] linux: Add clone3 CLONE_CLEAR_SIGHAND optimization to posix_spawn Date: Wed, 28 Sep 2022 17:46:29 -0300 Message-Id: <20220928204634.3036650-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The clone3 flag resets all signal handlers of the child not set to SIG_IGN to SIG_DFL. It allows to skip most of the sigaction calls to setup child signal handling, where previously a posix_spawn has to issue 2 times NSIG sigaction calls (one to obtain the current disposition and another to set either SIG_DFL or SIG_IGN). The expection is POSIX_SPAWN_SETSIGDEF the child still setup the signal for the case the disposition is SIG_IGN. It also need to handle the fallback where clone3 is not available, to set the fallback in child. This is done by splitting of __clone_internal_fallback from __clone_internal. Checked on x86_64-linux-gnu. --- include/clone_internal.h | 5 + posix/Makefile | 3 +- posix/tst-spawn7.c | 177 +++++++++++++++++++++++ sysdeps/unix/sysv/linux/clone-internal.c | 38 +++-- sysdeps/unix/sysv/linux/clone3.h | 6 + sysdeps/unix/sysv/linux/spawni.c | 31 ++-- 6 files changed, 235 insertions(+), 25 deletions(-) create mode 100644 posix/tst-spawn7.c diff --git a/include/clone_internal.h b/include/clone_internal.h index 4b23ef33ce..320640e64b 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -7,6 +7,11 @@ extern __typeof (clone3) __clone3; -1 with ENOSYS, fall back to clone or clone2. */ extern int __clone_internal (struct clone_args *__cl_args, int (*__func) (void *__arg), void *__arg); +/* The fallback code which calls clone/clone2 based on clone3 arguments. */ +extern int __clone_internal_fallback (struct clone_args *__cl_args, + int (*__func) (void *__arg), + void *__arg) + attribute_hidden; #ifndef _ISOMAC libc_hidden_proto (__clone3) diff --git a/posix/Makefile b/posix/Makefile index d1df7c27cb..7887eb1c8b 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -109,7 +109,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \ bug-regex38 tst-regcomp-truncated tst-spawn-chdir \ tst-wordexp-nocmd tst-execveat tst-spawn5 \ - tst-sched_getaffinity tst-spawn6 + tst-sched_getaffinity tst-spawn6 tst-spawn7 # Test for the glob symbol version that was replaced in glibc 2.27. ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes) @@ -291,6 +291,7 @@ tst-spawn-ARGS = -- $(host-test-program-cmd) tst-spawn-static-ARGS = $(tst-spawn-ARGS) tst-spawn5-ARGS = -- $(host-test-program-cmd) tst-spawn6-ARGS = -- $(host-test-program-cmd) +tst-spawn7-ARGS = -- $(host-test-program-cmd) tst-dir-ARGS = `pwd` `cd $(common-objdir)/$(subdir); pwd` `cd $(common-objdir); pwd` $(objpfx)tst-dir tst-chmod-ARGS = $(objdir) tst-vfork3-ARGS = --test-dir=$(objpfx) diff --git a/posix/tst-spawn7.c b/posix/tst-spawn7.c new file mode 100644 index 0000000000..4b920bf08e --- /dev/null +++ b/posix/tst-spawn7.c @@ -0,0 +1,177 @@ +/* Tests for posix_spawn signal handling. + Copyright (C) 2022 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 + +/* Nonzero if the program gets called via `exec'. */ +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, +static int restart; + +/* Hold the four initial argument used to respawn the process, plus + the extra '--direct' and '--restart', the check type, and a final NULL. */ +static char *spargs[8]; +static int check_type_argc; + +/* Called on process re-execution. */ +_Noreturn static void +handle_restart (int argc, char *argv[]) +{ + assert (argc == 1); + + if (strcmp (argv[0], "SIG_DFL") == 0) + { + for (int i = 1; i < NSIG; i++) + { + struct sigaction sa; + int r = sigaction (i, NULL, &sa); + /* Skip internal signals (such as SIGCANCEL). */ + if (r == -1) + continue; + TEST_VERIFY_EXIT (sa.sa_handler == SIG_DFL); + } + } + else if (strcmp (argv[0], "SIG_IGN") == 0) + { + for (int i = 1; i < NSIG; i++) + { + struct sigaction sa; + int r = sigaction (i, NULL, &sa); + /* Skip internal signals (such as SIGCANCEL). */ + if (r == -1) + continue; + if (i == SIGUSR1 || i == SIGUSR2) + TEST_VERIFY_EXIT (sa.sa_handler == SIG_IGN); + else + TEST_VERIFY_EXIT (sa.sa_handler == SIG_DFL); + } + } + + exit (EXIT_SUCCESS); +} + +static void +spawn_signal_test (const char *type, const posix_spawnattr_t *attr) +{ + spargs[check_type_argc] = (char*) type; + + pid_t pid; + int status; + + TEST_COMPARE (posix_spawn (&pid, spargs[0], NULL, attr, spargs, environ), 0); + TEST_COMPARE (xwaitpid (pid, &status, 0), pid); + TEST_VERIFY (WIFEXITED (status)); + TEST_VERIFY (!WIFSIGNALED (status)); + TEST_COMPARE (WEXITSTATUS (status), 0); +} + +static void +dummy_sa_handler (int) +{ +} + +static void +do_test_signals (void) +{ + { + /* Check if all signals handler are set to SIG_DFL on spawned process. */ + spawn_signal_test ("SIG_DFL", NULL); + } + + { + /* Same as before, but set SIGUSR1 and SIGUSR2. */ + struct sigaction sa = { 0 }; + sa.sa_handler = dummy_sa_handler; + xsigaction (SIGUSR1, &sa, NULL); + xsigaction (SIGUSR2, &sa, NULL); + spawn_signal_test ("SIG_DFL", NULL); + } + + { + /* Check if SIG_IGN is keep as is. */ + struct sigaction sa = { 0 }; + sa.sa_handler = SIG_IGN; + xsigaction (SIGUSR1, &sa, NULL); + xsigaction (SIGUSR2, &sa, NULL); + spawn_signal_test ("SIG_IGN", NULL); + } + + { + /* Check if SIG_IGN handlers are set to SIG_DFL. */ + posix_spawnattr_t attr; + posix_spawnattr_init (&attr); + sigset_t mask; + sigemptyset (&mask); + sigaddset (&mask, SIGUSR1); + sigaddset (&mask, SIGUSR2); + posix_spawnattr_setsigdefault (&attr, &mask); + posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETSIGDEF); + spawn_signal_test ("SIG_DFL", &attr); + posix_spawnattr_destroy (&attr); + } +} + +static int +do_test (int argc, char *argv[]) +{ + /* We must have either: + + - one or four parameters if called initially: + + argv[1]: path for ld.so optional + + argv[2]: "--library-path" optional + + argv[3]: the library path optional + + argv[4]: the application name + + - six parameters left if called through re-execution: + + argv[1]: the application name + + argv[2]: check SIG_IGN + + * When built with --enable-hardcoded-path-in-tests or issued without + using the loader directly. */ + + if (restart) + /* Ignore the application name. */ + handle_restart (argc - 1, &argv[1]); + + TEST_VERIFY_EXIT (argc == 2 || argc == 5); + + int i; + + for (i = 0; i < argc - 1; i++) + spargs[i] = argv[i + 1]; + spargs[i++] = (char *) "--direct"; + spargs[i++] = (char *) "--restart"; + check_type_argc = i++; + spargs[i] = NULL; + + + do_test_signals (); + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/sysdeps/unix/sysv/linux/clone-internal.c b/sysdeps/unix/sysv/linux/clone-internal.c index a71effcbd3..d9eb254ffb 100644 --- a/sysdeps/unix/sysv/linux/clone-internal.c +++ b/sysdeps/unix/sysv/linux/clone-internal.c @@ -44,27 +44,15 @@ _Static_assert (sizeof (struct clone_args) == CLONE_ARGS_SIZE_VER2, "sizeof (struct clone_args) != CLONE_ARGS_SIZE_VER2"); int -__clone_internal (struct clone_args *cl_args, - int (*func) (void *arg), void *arg) +__clone_internal_fallback (struct clone_args *cl_args, + int (*func) (void *arg), void *arg) { - int ret; -#ifdef HAVE_CLONE3_WRAPPER - /* Try clone3 first. */ - int saved_errno = errno; - ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); - if (ret != -1 || errno != ENOSYS) - return ret; - - /* NB: Restore errno since errno may be checked against non-zero - return value. */ - __set_errno (saved_errno); -#endif - /* Map clone3 arguments to clone arguments. NB: No need to check invalid clone3 specific bits in flags nor exit_signal since this is an internal function. */ int flags = cl_args->flags | cl_args->exit_signal; void *stack = cast_to_pointer (cl_args->stack); + int ret; #ifdef __ia64__ ret = __clone2 (func, stack, cl_args->stack_size, @@ -88,4 +76,24 @@ __clone_internal (struct clone_args *cl_args, return ret; } + +int +__clone_internal (struct clone_args *cl_args, + int (*func) (void *arg), void *arg) +{ +#ifdef HAVE_CLONE3_WRAPPER + /* Try clone3 first. */ + int saved_errno = errno; + int ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); + if (ret != -1 || errno != ENOSYS) + return ret; + + /* NB: Restore errno since errno may be checked against non-zero + return value. */ + __set_errno (saved_errno); +#endif + + return __clone_internal_fallback (cl_args, func, arg); +} + libc_hidden_def (__clone_internal) diff --git a/sysdeps/unix/sysv/linux/clone3.h b/sysdeps/unix/sysv/linux/clone3.h index 889014a6a9..a150363f89 100644 --- a/sysdeps/unix/sysv/linux/clone3.h +++ b/sysdeps/unix/sysv/linux/clone3.h @@ -25,6 +25,12 @@ __BEGIN_DECLS +/* Flags for the clone3 syscall. */ +#define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and + reset to SIG_DFL. */ +#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given + the right permissions. */ + /* The unsigned 64-bit and 8-byte aligned integer type. */ typedef __U64_TYPE __aligned_uint64_t __attribute__ ((__aligned__ (8))); diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index 65ee03c804..5251b4c602 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -66,6 +66,7 @@ struct posix_spawn_args ptrdiff_t argc; char *const *envp; int xflags; + bool use_clone3; int err; }; @@ -104,12 +105,11 @@ __spawni_child (void *arguments) const posix_spawnattr_t *restrict attr = args->attr; const posix_spawn_file_actions_t *file_actions = args->fa; - /* The child must ensure that no signal handler are enabled because it shared - memory with parent, so the signal disposition must be either SIG_DFL or - SIG_IGN. It does by iterating over all signals and although it could - possibly be more optimized (by tracking which signal potentially have a - signal handler), it might requires system specific solutions (since the - sigset_t data type can be very different on different architectures). */ + /* The child must ensure that no signal handler are enabled because it + shared memory with parent, so the signal disposition must be either + SIG_DFL or SIG_IGN. If clone3/CLONE_CLEAR_SIGHAND is used, there is + only need to set the defined signals to SIG_DFL if POSIX_SPAWN_SETSIGDEF + is used; otherwise, the code iterates over all signals. */ struct sigaction sa; memset (&sa, '\0', sizeof (sa)); @@ -122,7 +122,7 @@ __spawni_child (void *arguments) { sa.sa_handler = SIG_DFL; } - else if (__sigismember (&hset, sig)) + else if (!args->use_clone3 && __sigismember (&hset, sig)) { if (is_internal_signal (sig)) sa.sa_handler = SIG_IGN; @@ -382,12 +382,25 @@ __spawnix (pid_t * pid, const char *file, for instance). */ struct clone_args clone_args = { - .flags = CLONE_VM | CLONE_VFORK, + /* No supported flags like CLONE_CLEAR_SIGHAND will be cleared up by + __clone_internal_fallback. */ + .flags = CLONE_CLEAR_SIGHAND | CLONE_VM | CLONE_VFORK, .exit_signal = SIGCHLD, .stack = (uintptr_t) stack, .stack_size = stack_size, }; - new_pid = __clone_internal (&clone_args, __spawni_child, &args); +#ifdef HAVE_CLONE3_WRAPPER + args.use_clone3 = true; + new_pid = __clone3 (&clone_args, sizeof (clone_args), __spawni_child, + &args); + /* clone3 was added on 5.3 and CLONE_CLEAR_SIGHAND on 5.5. */ + if (new_pid == -1 && (errno == ENOSYS || errno == EINVAL)) +#endif + { + args.use_clone3 = false; + new_pid = __clone_internal_fallback (&clone_args, __spawni_child, + &args); + } /* It needs to collect the case where the auxiliary process was created but failed to execute the file (due either any preparation step or From patchwork Wed Sep 28 20:46:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58119 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 B947B3858291 for ; Wed, 28 Sep 2022 20:47:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B947B3858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398025; bh=SXvfJ94gN0ZLg0RX29DGDVMF5SKDborn2olvvCKpTnc=; 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=Jq/vRpTAfByCIL7eij9em1qtspUdE/qzPs2W5906HoK8ogMAqaTNAl11rDMLbpGol 5i7LykIo7kF6IFTpTkUNYob9Nhm/lbQjO4xQsK+Gt7tCGMxpHaNKPbVgsVBnWK1tLH TmlcljLjq+yjW3eWPDLwrG50H/G5BdatAm7wnC+s= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by sourceware.org (Postfix) with ESMTPS id ED07A3858C2D for ; Wed, 28 Sep 2022 20:46:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED07A3858C2D Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-131b7bb5077so2174309fac.2 for ; Wed, 28 Sep 2022 13:46:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=SXvfJ94gN0ZLg0RX29DGDVMF5SKDborn2olvvCKpTnc=; b=e6EnDxucOqozuOR1FRhHUcZ8tApSlspBSnS6a7TvTtsDnFBV5MWhxP8zGaNbIp3O4m Wgat253HEU0Ta2Srd9oE73+d2YmamiONx5uE1yidzOZNEM5jtv4O/toN063tnmYPm3d4 k3vwQjNlem1Mrtu3Cc7hCDW/1i0LHt286NFh5ftzavhpMZusrrCqvB5amzPxi+KlPjBS 32GrWVZ7euOdkDlos8V4OSXRyRJdkkQjNTb91hvVFe/j5IKF+F88Tn3AeXKRtuf+6Azx lIRbMOshqiPk03Ey8idCfLRBTkDUfX0iPyvmDdvVBRJK4N07a+JHfF+RGDEKRSqOMdqw gYFw== X-Gm-Message-State: ACrzQf2b1mbcUFoyYUkIX5VPc0OBuPZSHmtYSWLaWpeAu7cooTt6bLQv V/yQ4WMFzx1HD7EhAcJaHOL8458aSozmQnye X-Google-Smtp-Source: AMsMyM5YitNSJmxA3+ACzwKz77NozWti+3QdKDrm3aJbY9FyL8kNRmz589fT6AXIIqMy5NPkhXyICg== X-Received: by 2002:a05:6870:f60e:b0:131:b7cc:f994 with SMTP id ek14-20020a056870f60e00b00131b7ccf994mr1693854oab.113.1664398002079; Wed, 28 Sep 2022 13:46:42 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:41 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 3/7] powerpc64le: Add the clone3 wrapper Date: Wed, 28 Sep 2022 17:46:30 -0300 Message-Id: <20220928204634.3036650-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It follows the internal signature: extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); And x86_64 semantics to return EINVAL if either cl_args or func is NULL. The stack is 16-byte aligned prior executing func. Checked on powerpc64le-linux-gnu. --- .../sysv/linux/powerpc/powerpc64/clone3.S | 145 ++++++++++++++++++ sysdeps/unix/sysv/linux/powerpc/sysdep.h | 1 + 2 files changed, 146 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S new file mode 100644 index 0000000000..a7461a3a42 --- /dev/null +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S @@ -0,0 +1,145 @@ +/* The clone3 syscall wrapper. Linux/powerpc64 version. + Copyright (C) 2022 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 +#define _ERRNO_H 1 +#include + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, size_t size, + int (*func)(void *arg), void *arg); + + the kernel entry is: + int clone3 (struct clone_args *cl_args, size_t size); + + The parameters are passed in registers from userland: + r3: cl_args + r4: size + r5: func + r6: arg */ + + .text +ENTRY(__clone3) + CALL_MCOUNT 4 + + /* Sanity checks args. */ + cmpdi cr0, r3, 0 + cmpdi cr1, r5, 0 + cror cr0*4+eq, cr1*4+eq, cr0*4+eq + beq cr0,L(badargs) + + /* Save some regs in the "red zone". */ +#ifdef USE_PPC_SCV + std r28, -24(r1) + cfi_offset (r28, -24) +#endif + std r29, -16(r1) + std r30, -8(r1) + cfi_offset (r29, -16) + cfi_offset (r30, -8) + + /* Save fn and args across syscall. */ + mr r30, r5 /* Function. */ + mr r29, r6 /* Arguments. */ + + /* End FDE now, because in the child the unwind info will be + wrong. */ + cfi_endproc + + /* Do the system call, the kernel expects: + r0: system call numer + r3: cl_args + r4: size */ + li r0, SYS_ify(clone3) +#ifdef USE_PPC_SCV + CHECK_SCV_SUPPORT r28 0f + /* This is equivalent to DO_CALL_SCV, but we cannot use the macro here + because it uses CFI directives and we just called cfi_endproc. */ + mflr r9 + std r9, FRAME_LR_SAVE(r1) + .machine "push" + .machine "power9" + scv 0 + .machine "pop" + ld r9, FRAME_LR_SAVE(r1) + mtlr r9 + + /* When using scv, error is indicated by negative r3. */ + cmpdi cr1, r3, 0 + b 1f +#endif +0: DO_CALL_SC + + /* With sc, error is indicated by cr0.SO. */ + cmpdi cr1, r3, 0 + crandc cr1*4+eq, cr1*4+eq, cr0*4+so + +1: bne- cr1,L(parent) + + /* Child, load the function and arguments. */ + + /* Align stack. */ + rldicr r1, r1, 0, 59 + + std r2, FRAME_TOC_SAVE(r1) + PPC64_LOAD_FUNCPTR r30 + mr r3, r29 + bctrl + ld r2, FRAME_TOC_SAVE(r1) + + li r0, SYS_ify(exit) + DO_CALL_SC + /* We won't ever get here but provide a nop so that the linker + will insert a toc adjusting stub if necessary. */ + nop + +L(badargs): + cfi_startproc + li r3, EINVAL + TAIL_CALL_SYSCALL_ERROR + +L(parent): + /* Check if svc is available. */ + cmpdi cr1, r28, 0 + + /* Parent. Restore registers & return. */ +#ifdef USE_PPC_SCV + cfi_offset (r28, -24) + ld r28, -24(r1) + cfi_restore (r28) +#endif + cfi_offset (r29,-16) + cfi_offset (r30,-8) + ld r29, -16(r1) + ld r30, -8(r1) + cfi_restore (r29) + cfi_restore (r30) + +#ifdef USE_PPC_SCV + beq cr1, 0f + RET_SCV + b 1f +#endif +0: RET_SC +1: TAIL_CALL_SYSCALL_ERROR + +PSEUDO_END (__clone3) + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/powerpc/sysdep.h b/sysdeps/unix/sysv/linux/powerpc/sysdep.h index 4fb135aa8d..b86951207d 100644 --- a/sysdeps/unix/sysv/linux/powerpc/sysdep.h +++ b/sysdeps/unix/sysv/linux/powerpc/sysdep.h @@ -246,6 +246,7 @@ #if defined(__PPC64__) || defined(__powerpc64__) #define HAVE_CLOCK_GETRES64_VSYSCALL "__kernel_clock_getres" #define HAVE_CLOCK_GETTIME64_VSYSCALL "__kernel_clock_gettime" +#define HAVE_CLONE3_WRAPPER 1 #else #define HAVE_CLOCK_GETRES_VSYSCALL "__kernel_clock_getres" #define HAVE_CLOCK_GETTIME_VSYSCALL "__kernel_clock_gettime" From patchwork Wed Sep 28 20:46:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58120 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 2B7D33857827 for ; Wed, 28 Sep 2022 20:47:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B7D33857827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398032; bh=zLUIaulxjO1t5/REnp7qyUpmHUdNaDMGgfOfVBNh9Gs=; 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=DT+Scv/JE6Sk+m42ntv0FibGatlIR1H5L7mavfCsG5LwoQSHALjwL4dLZaFipPxWW PIzh0rNzZbKCdHJg0T6O4YoKZQdnTuPs8SKonEbIe2a6Z7kMeElZ2GA7FSbBGZSIXD bZpgbLMzBVf7g5OcV72PJGRxfg6L/mRsFN9SthCY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by sourceware.org (Postfix) with ESMTPS id DC61B3858D28 for ; Wed, 28 Sep 2022 20:46:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DC61B3858D28 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-12803ac8113so18842775fac.8 for ; Wed, 28 Sep 2022 13:46:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=zLUIaulxjO1t5/REnp7qyUpmHUdNaDMGgfOfVBNh9Gs=; b=FxzPtSSQQtmhi1hQ19GXoEs8Psk54e99qET5bJrlTEsak9srs1GwOtJWqAhkiD0hSv 2FE8jFh64XKqtcGxCgGLEi+/OHpj+Y5fS5LJ1b4L/fIN4HCttLg3oo+cjd7Frg9qdFJU p+SxvJk1PZyCT4LmXtCTRtIkctoNoDwyGL/S2BhdDyHs7WTvLj8yVbwR7mBsyxPMM9O4 8DR37j9CG5r7g2uQQLnDVl5FCT8NAMTXD3wj69sAqxLMSmaiHA3/j4LUWeXJH9+9Sa20 3EOlZP+dgpfiSftPAB2wn/mPDz1OQaQCio62hj9uUEow2yubsTjYkmT/uEV8vbRTaEOG 3QAA== X-Gm-Message-State: ACrzQf3vzyBPFAzYwCXK00QPzwMzfbvJrAivV8SpazmlCGyhxOz5xaqo NVMPDL6xo9t5zW8JdFjC1zmR3JUHl/+eRItd X-Google-Smtp-Source: AMsMyM45oEvtIaydBcUYfCw0jZDK8aocSdKhsr8n46hRFoMcXeQXuK/AKOoQeK+C8eQwbpDjHkl2NA== X-Received: by 2002:a05:6870:c1c7:b0:12b:91cb:914d with SMTP id i7-20020a056870c1c700b0012b91cb914dmr6479673oad.236.1664398003359; Wed, 28 Sep 2022 13:46:43 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:43 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 4/7] aarch64: Add the clone3 wrapper Date: Wed, 28 Sep 2022 17:46:31 -0300 Message-Id: <20220928204634.3036650-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It follow the internal signature: extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); And x86_64 semantics to return EINVAL if either cl_args or func is NULL. The stack is 16-byte aligned prior executing func. Checked on aarch64-linux-gnu. --- sysdeps/unix/sysv/linux/aarch64/clone3.S | 90 ++++++++++++++++++++++++ sysdeps/unix/sysv/linux/aarch64/sysdep.h | 2 + 2 files changed, 92 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/aarch64/clone3.S diff --git a/sysdeps/unix/sysv/linux/aarch64/clone3.S b/sysdeps/unix/sysv/linux/aarch64/clone3.S new file mode 100644 index 0000000000..dba93430eb --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/clone3.S @@ -0,0 +1,90 @@ +/* The clone3 syscall wrapper. Linux/aarch64 version. + Copyright (C) 2022 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 +#define _ERRNO_H 1 +#include + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, size_t size, + int (*func)(void *arg), void *arg); + + the kernel entry is: + int clone3 (struct clone_args *cl_args, size_t size); + + The parameters are passed in registers from userland: + x0: cl_args + x1: size + x2: func + x3: arg */ + + .text +ENTRY(__clone3) + PTR_ARG (0) + PTR_ARG (1) + PTR_ARG (3) + PTR_ARG (4) + /* Save args for the child. */ + mov x10, x0 /* cl_args */ + mov x11, x2 /* func */ + mov x12, x3 /* args */ + + /* Sanity check args. */ + mov x0, #-EINVAL + cbz x10, .Lsyscall_error /* No NULL cl_args pointer. */ + cbz x2, .Lsyscall_error /* No NULL function pointer. */ + + /* Do the system call, the kernel expects: + x8: system call number + x0: cl_args + x1: size */ + mov x0, x10 + mov x8, #SYS_ify(clone3) + svc 0x0 + + cmp x0, #0 + beq thread_start + blt .Lsyscall_error + RET +PSEUDO_END (__clone3) + + .align 4 + .type thread_start, %function +thread_start: + cfi_startproc + cfi_undefined (x30) + mov x29, 0 + + /* Align sp. */ + mov x0, sp + and x0, x0, -16 + mov sp, x0 + + /* Pick the function arg and execute. */ + mov x0, x12 + blr x11 + + /* We are done, pass the return value through x0. */ + mov x8, #SYS_ify(exit) + svc 0x0 + cfi_endproc + .size thread_start, .-thread_start + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/aarch64/sysdep.h b/sysdeps/unix/sysv/linux/aarch64/sysdep.h index f1853e012f..42bb22f5e6 100644 --- a/sysdeps/unix/sysv/linux/aarch64/sysdep.h +++ b/sysdeps/unix/sysv/linux/aarch64/sysdep.h @@ -164,6 +164,8 @@ # define HAVE_CLOCK_GETTIME64_VSYSCALL "__kernel_clock_gettime" # define HAVE_GETTIMEOFDAY_VSYSCALL "__kernel_gettimeofday" +# define HAVE_CLONE3_WRAPPER 1 + # undef INTERNAL_SYSCALL_RAW # define INTERNAL_SYSCALL_RAW(name, nr, args...) \ ({ long _sys_result; \ From patchwork Wed Sep 28 20:46:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58122 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 B3F723856DF9 for ; Wed, 28 Sep 2022 20:47:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B3F723856DF9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398071; bh=jphfAatt3WwU8gTxwrDpXk7pbGPaIVKK7k1bbpEp4aA=; 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=xFXpZ6c+DlKRRZ9OeAvw3vLk9BozB96EualxnCXzZrBAQ/gHoVXsrGXa87TCOVsvm mN4L0pjzPbXHNfysevlH9+C/myUvBlpLpbiLbqKhFqmmPkpupsJ8XMfdBH8YiaMq3H n4EhS0DZUZf+OZq1CK7KCizTanPilfmHpI3XyOpg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id 7E5053858CDA for ; Wed, 28 Sep 2022 20:46:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E5053858CDA Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-1280590722dso18839789fac.1 for ; Wed, 28 Sep 2022 13:46:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=jphfAatt3WwU8gTxwrDpXk7pbGPaIVKK7k1bbpEp4aA=; b=WQwy+m+VXvvLNdMXBoe+dYIA/J8XzROo9BBBytdla/RMA5l8bXCwLg0vR2QsfVAQEw f2VWmmJPmJzlvbPUiLiua1ZbnQfL6+hxkJpj6b3ZRI801JOJIqgggFpzRdiacKSQw4eY clvwQM21QXWJMw8fk9HURBOheLnH1n2ruJXx+VqMadZEZdIYmBkKFYvwuWpbWO3JQChL tJJ91y6pmgx9JAMS2xijM9HLNRMANYJGuWFx72P2HyhRsOOuPDAAyiix7R/x+f4P4oxs YbqPQfJa/U922cn3iL+LoEJxiYTsxTx7FxTbPyg/jXBhiwOav/Ub1lnVQke5MLLpJJnk +Niw== X-Gm-Message-State: ACrzQf1190JrvvUyUqBOu/F7o4/oVrPG45dsq4ell2s3tIoXB69prL4F sbYnf8os+ff8XTxzs3wXnND1L8xq+Vttr9yt X-Google-Smtp-Source: AMsMyM5Ow8zWCr/FrVVfwe6SAScOBeAVY7QBSF4VuurX8R3UcpE3xfYhVfbQrkufGeTDey0PK82b6Q== X-Received: by 2002:a05:6870:7387:b0:131:727b:ee with SMTP id z7-20020a056870738700b00131727b00eemr6297957oam.32.1664398004666; Wed, 28 Sep 2022 13:46:44 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:44 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 5/7] s390x: Add the clone3 wrapper Date: Wed, 28 Sep 2022 17:46:32 -0300 Message-Id: <20220928204634.3036650-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It follows the internal signature: extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); And x86_64 semantics to return EINVAL if either cl_args or func is NULL. The stack is 16-byte aligned prior executing func. Checked on s390x-linux-gnu. --- sysdeps/unix/sysv/linux/s390/s390-64/clone3.S | 84 +++++++++++++++++++ sysdeps/unix/sysv/linux/s390/sysdep.h | 1 + 2 files changed, 85 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S new file mode 100644 index 0000000000..d338acfc9c --- /dev/null +++ b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S @@ -0,0 +1,84 @@ +/* The clone3 syscall wrapper. Linux/s390x version. + Copyright (C) 2022 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 +#define _ERRNO_H 1 +#include + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, size_t size, + int (*func)(void *arg), void *arg); + + the kernel entry is: + int clone3 (struct clone_args *cl_args, size_t size); + + The parameters are passed in registers from userland: + r2: cl_args + r3: size + r4: func + r5: arg */ + + .text +ENTRY(__clone3) + stg %r6, 48(%r15) + + /* Sanity check args. */ + ltgr %r2, %r2 + je error + ltgr %r6, %r4 + je error + + /* Do the system call, the kernel expects: + r1: system call number + r2: cl_args + r3: size */ + lghi %r1, SYS_ify(clone3) + svc 0 + ltgr %r2,%r2 /* check return code */ + jz thread_start + lg %r6, 48(%r15) + jgm SYSCALL_ERROR_LABEL + br %r14 +error: + lghi %r2,-EINVAL + jg SYSCALL_ERROR_LABEL +PSEUDO_END (__clone3) + + .align 4 + .type thread_start, %function +thread_start: + cfi_startproc + /* Mark r14 as undefined in order to stop unwinding here. */ + cfi_undefined (r14) + + /* Align stack. */ + nill %r15, 0xfff0 + + /* func is in gpr 6, arg in gpr 5. */ + lgr %r2, %r5 + aghi %r15, -160 + xc 0(8,%r15),0(%r15) + basr %r14, %r6 + + DO_CALL (exit, 1) + cfi_endproc + .size thread_start, .-thread_start + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/s390/sysdep.h b/sysdeps/unix/sysv/linux/s390/sysdep.h index 930d7efe03..366c797ef1 100644 --- a/sysdeps/unix/sysv/linux/s390/sysdep.h +++ b/sysdeps/unix/sysv/linux/s390/sysdep.h @@ -72,6 +72,7 @@ #ifdef __s390x__ #define HAVE_CLOCK_GETRES64_VSYSCALL "__kernel_clock_getres" #define HAVE_CLOCK_GETTIME64_VSYSCALL "__kernel_clock_gettime" +#define HAVE_CLONE3_WRAPPER 1 #else #define HAVE_CLOCK_GETRES_VSYSCALL "__kernel_clock_getres" #define HAVE_CLOCK_GETTIME_VSYSCALL "__kernel_clock_gettime" From patchwork Wed Sep 28 20:46:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58123 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 91C463856965 for ; Wed, 28 Sep 2022 20:47:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91C463856965 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398073; bh=Yv0rDeh2vR817wK3VxpGxHCdi5O8Klh9ZGY/VAAhpjQ=; 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=bwN4kzSVMdS92NWhEeNCorMes9KSNOdYDNYmq6rnvNTtU9En4OabQeEGiAnUV+ckZ CmpUNdoE9/m1EiCnarMy0znr2RLmOh8qtrse5YF4gNKKrOvupM5adwSUOXb4PttwNe b5sKBJks6/UzrtQe/4laMA5JU9zU65Z+wNE7XLoc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by sourceware.org (Postfix) with ESMTPS id D57EF3858437 for ; Wed, 28 Sep 2022 20:46:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D57EF3858437 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-1319cf91d8aso4871138fac.5 for ; Wed, 28 Sep 2022 13:46:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Yv0rDeh2vR817wK3VxpGxHCdi5O8Klh9ZGY/VAAhpjQ=; b=kx1kbArsFHKeoy3FosEfZNSlb8qhyj3K74cUB8OlyZs5CbqC3wHEE+N6mvPE9KiJYd H3UmDO/lf3b5nduEbO8+nMt4z3kzUEJN2oT0hVg0Py2W3VC5NuJIKHqviptp7yNoSHZ5 0WL/l9t4dsv6u6hkIuo/3mWeIjh1l3B9kht+i73e6jHfkhhcfxDG9OfSdz1ks5ftBF/y Ll/ohpFUiEshIlPlQx7r9ZZ3MS5j9lvQYg+CEPMCHonrCf0Cp34PJPF8v2QeOf0ue6oa oF2VYxh4lePUWM7kUESUdX31Md7FoWOgI0FK79BBSEDu2wingHcFiBxIBI0AWg/xffk/ HmNQ== X-Gm-Message-State: ACrzQf0eQ+c5r7tnoW6ME78t4vEGJaxLzXkeClPQyIun96yRzHet7TbN fz0fOV/kjCelvw3xH/gjoxRncMThtEjh3xPc X-Google-Smtp-Source: AMsMyM4wXO0CrasSLXGbkctwzfFCGAbMbBGv5YlVLzWBxBV8G0NK5Zwx/ZO3SL/90y2mUIAQVsOFPg== X-Received: by 2002:a05:6870:b508:b0:12d:1c59:90d9 with SMTP id v8-20020a056870b50800b0012d1c5990d9mr6484548oap.199.1664398006027; Wed, 28 Sep 2022 13:46:46 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:45 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 6/7] riscv: Add the clone3 wrapper Date: Wed, 28 Sep 2022 17:46:33 -0300 Message-Id: <20220928204634.3036650-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It follows the internal signature: extern int clone3 (struct clone_args *__cl_args, size_t __size, int (*__func) (void *__arg), void *__arg); And x86_64 semantics to return EINVAL if either cl_args or func is NULL. The stack is 16-byte aligned prior executing func. Checked on riscv64-linux-gnu-rv64imafdc-lp64d. --- sysdeps/unix/sysv/linux/riscv/clone3.S | 83 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/riscv/sysdep.h | 1 + 2 files changed, 84 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S new file mode 100644 index 0000000000..1e6e807aa0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/clone3.S @@ -0,0 +1,83 @@ +/* The clone3 syscall wrapper. Linux/RISC-V version. + Copyright (C) 2022 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 + +/* The userland implementation is: + int clone3 (struct clone_args *cl_args, size_t size, + int (*func)(void *arg), void *arg); + + the kernel entry is: + int clone3 (struct clone_args *cl_args, size_t size); + + The parameters are passed in registers from userland: + a0: cl_args + a1: size + a2: func + a3: arg */ + + .text +ENTRY(__clone3) + /* Sanity check args. */ + beqz a0, L(invalid) /* No NULL cl_args pointer. */ + beqz a2, L(invalid) /* No NULL function pointer. */ + + /* Do the system call, the kernel expects: + a7: system call number + a0: cl_args + a1: size */ + li a7, __NR_clone3 + scall + + bltz a0, L(error) + beqz a0, L(thread_start) + + ret + +L(invalid): + li a0, -EINVAL +L(error): + tail __syscall_error +END (__clone3) + +ENTRY(__thread_start_clone3) +L(thread_start): + /* Terminate call stack by noting ra is undefined. Use a dummy + .cfi_label to force starting the FDE. */ + .cfi_label .Ldummy + cfi_undefined (ra) + + /* Align stack to a 128-bit boundary as per RISC-V ABI. */ + andi sp, sp, ALMASK + + /* Restore the arg for user's function and call the user's + function. */ + mv a1, a2 /* Function pointer. */ + mv a0, a3 /* Argument pointer. */ + jalr a1 + + /* Call exit with the function's return value. */ + li a7, __NR_exit + scall +END(__thread_start_clone3) + +libc_hidden_def (__clone3) +weak_alias (__clone3, clone3) diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h index 9b03b10567..57523dc7b7 100644 --- a/sysdeps/unix/sysv/linux/riscv/sysdep.h +++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h @@ -150,6 +150,7 @@ /* RV32 does not support the gettime VDSO syscalls. */ # endif +# define HAVE_CLONE3_WRAPPER 1 /* List of system calls which are supported as vsyscalls (for RV32 and RV64). */ From patchwork Wed Sep 28 20:46:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58125 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 6CB4E385696E for ; Wed, 28 Sep 2022 20:48:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CB4E385696E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664398115; bh=y42wBR4JH5NbJF48w6l5fh7OHMFOWPuhBFCnDMYLS4g=; 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=YEuh5iO2CXr7Ugky1bkkbE9USvTjqj5QdJ210D2H8EV8EqvD7NhYZJhW0sLRVjivz SHLKmnZFZb5exOTSF3CEooAomT3gO+A/2pX8yoU8YGDdBux+RVgaCQ94HvRpjn6fPm 6VjFtsj5u/1fGbOem8wr0H+OsyfPoi5XHNqhd15o= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 2EC9D3858C39 for ; Wed, 28 Sep 2022 20:46:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2EC9D3858C39 Received: by mail-oi1-x22c.google.com with SMTP id l5so4467600oif.7 for ; Wed, 28 Sep 2022 13:46:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date; bh=y42wBR4JH5NbJF48w6l5fh7OHMFOWPuhBFCnDMYLS4g=; b=MEOuR5OxBoSL6wHq2FomNvnt22koWV8dDNte6goZvLjG5mMBjxohuYvFc42j7SDmQx tgTh4CwUu4oeNOwp2/wlel/N/r03NxOyUt5Ie8Rqfb0bOzQKFuGbVKhwbo3a4+z5KhPw ANto0wByn6+ZD0MJNEsiJYXN5+6TvfWQk/ao4Jl2hmVG9dcu/ER7s61GOMmd67s7lz0c 6tZ+DLEV//HjOIelG9huq4yKUzIvmn6XGJqV/za3EmpvdgdLEV6t3jXV4CM6qh8kAimg jEakNrLRIZ34EOC778yMf9ye3ZOboEP2Cn+s77fG/HXm/xIzs9e9nUidRjLgnccmz0z3 cwgw== X-Gm-Message-State: ACrzQf3jc6bdxe/CTpbVGym3AK4ShuA2YDaOUD9FbLx+ZuaoyuvDrQxi wyH7T7b+wsWeThjPlTxBB1zQ0es0rvyiOuC0 X-Google-Smtp-Source: AMsMyM5Xn2Qd//wh7SpTeAqYw0+6kEN8TuAta5YdwA25vWxjor4ixQnsR3qMhKsqT3Q43sC9OJ2Yrg== X-Received: by 2002:a05:6808:bca:b0:350:b551:3afe with SMTP id o10-20020a0568080bca00b00350b5513afemr5346576oik.153.1664398007385; Wed, 28 Sep 2022 13:46:47 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:3f57:3c77:8813:789c]) by smtp.gmail.com with ESMTPSA id x4-20020a056870434400b0011bde9f5745sm3219345oah.23.2022.09.28.13.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 13:46:47 -0700 (PDT) To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH 7/7] Linux: optimize clone3 internal usage Date: Wed, 28 Sep 2022 17:46:34 -0300 Message-Id: <20220928204634.3036650-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> References: <20220928204634.3036650-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Now that clone3 is used on more architectures, add an optimization to avoid calling when glibc detects that it is no supported by the kernel. It also adds __ASSUME_CLONE3, which allows skip this optimization and issue clone3 syscall directly. It does not handle the the small window between 5.3 and 5.5 for posix_spawn (CLONE_CLEAR_SIGHAND was added in 5.5). Checked on x86_64-linux-gnu. --- include/clone_internal.h | 5 +++++ sysdeps/unix/sysv/linux/clone-internal.c | 25 ++++++++++++++++++++++- sysdeps/unix/sysv/linux/kernel-features.h | 8 ++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/clone_internal.h b/include/clone_internal.h index 320640e64b..71c52211c7 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -7,6 +7,11 @@ extern __typeof (clone3) __clone3; -1 with ENOSYS, fall back to clone or clone2. */ extern int __clone_internal (struct clone_args *__cl_args, int (*__func) (void *__arg), void *__arg); +/* clone3 wrapper that avoid issuing the syscall once clone3 fails with + ENOSYS. */ +extern int __clone3_internal (struct clone_args *cl_args, + int (*func) (void *args), void *arg) + attribute_hidden; /* The fallback code which calls clone/clone2 based on clone3 arguments. */ extern int __clone_internal_fallback (struct clone_args *__cl_args, int (*__func) (void *__arg), diff --git a/sysdeps/unix/sysv/linux/clone-internal.c b/sysdeps/unix/sysv/linux/clone-internal.c index d9eb254ffb..7e20d02025 100644 --- a/sysdeps/unix/sysv/linux/clone-internal.c +++ b/sysdeps/unix/sysv/linux/clone-internal.c @@ -76,6 +76,29 @@ __clone_internal_fallback (struct clone_args *cl_args, return ret; } +int +__clone3_internal (struct clone_args *cl_args, int (*func) (void *args), + void *arg) +{ +#ifdef HAVE_CLONE3_WRAPPER +# if __ASSUME_CLONE3 + return __clone3 (cl_args, sizeof (*cl_args), func, arg); +# else + static int clone3_supported = 1; + if (atomic_load_relaxed (&clone3_supported) == 1) + { + int ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); + if (ret != -1 || errno != ENOSYS) + return ret; + + atomic_store_relaxed (&clone3_supported, 0); + } +# endif +#endif + __set_errno (ENOSYS); + return -1; +} + int __clone_internal (struct clone_args *cl_args, @@ -84,7 +107,7 @@ __clone_internal (struct clone_args *cl_args, #ifdef HAVE_CLONE3_WRAPPER /* Try clone3 first. */ int saved_errno = errno; - int ret = __clone3 (cl_args, sizeof (*cl_args), func, arg); + int ret = __clone3_internal (cl_args, func, arg); if (ret != -1 || errno != ENOSYS) return ret; diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 74adc3956b..496c7f34cb 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -236,4 +236,12 @@ # define __ASSUME_FUTEX_LOCK_PI2 0 #endif +/* The clone3 system call was introduced across all architectures + in Linux 5.3. */ +#if __LINUX_KERNEL_VERSION >= 0x050300 +# define __ASSUME_CLONE3 1 +#else +# define __ASSUME_CLONE3 0 +#endif + #endif /* kernel-features.h */