From patchwork Thu Jan 12 13:58:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63097 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 A38AA3854390 for ; Thu, 12 Jan 2023 13:59:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A38AA3854390 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673531963; bh=ZtIhO9j+xvydNqIUsWUpTH9JjRj2L8fr2xNYVR69ATg=; 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=D0+r3RQEcyYqM6jy/G5IzLy9WflxEMmaXPAYPuMi0kdhkp9h0I5sZGdSvS2ClKefj vNVJVTfuS1PTgdqr3tkFLlxp/U5M/pcFnAM3BvegiGbWJdOMUs1kCciSsyewAbncE/ knpRWkFHHDcEJWJfbP6zT0q9hN3F/AJvkXV7FC1E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by sourceware.org (Postfix) with ESMTPS id 0FF123858C27 for ; Thu, 12 Jan 2023 13:59:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0FF123858C27 Received: by mail-oo1-xc34.google.com with SMTP id s10-20020a4aa54a000000b004f240f120b3so1012746oom.1 for ; Thu, 12 Jan 2023 05:59:01 -0800 (PST) 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:message-id:reply-to; bh=ZtIhO9j+xvydNqIUsWUpTH9JjRj2L8fr2xNYVR69ATg=; b=sTJGxCyg0SZIDhduGrcLk8wYfJZF0HhrCLws9vUIY6OWKGnXI70fJLfHx2Kiqjhgn7 WmM0vfZNyC+hbLPTbQOaLWRme9Ij6tNGqxjLH4rK8okT3i1atvH5vQu6qISy+itb3y0x E8T805pZdNl6VT9IxAj1cAPsDfHOEz1iuZdl2bD831iBXNOhD4FNvnaYCL5UD8sFbI7m cp21B6NDJuuALh56Nkbobsv4TeYJjS6MT5wT2spiRdrej9fvujDFAhk5tYctYo5FawAA JFbECc5BcDzC+I3x9VkOvbT5ooWRFz4Qh6TRq+D2ckm0h4jE1iWRGwcey1hga5zPpbxL +b3Q== X-Gm-Message-State: AFqh2kpSMUfUoGbuoj9ElSK2F2byj3jr1RVMreWC58TtUMHRLlQ/1qgv +AmQVvwp09mFrmDxoVZlZRNB5ypo1lNn8rXAnVc= X-Google-Smtp-Source: AMrXdXu/TlaQnLvi4mhAMqkvOFnFKQg8VUn4YiosMxVqjv1Npe202DRi4v/Rokpejjk2mo4eEQ5LRA== X-Received: by 2002:a4a:ce8b:0:b0:4f2:85eb:a with SMTP id f11-20020a4ace8b000000b004f285eb000amr439100oos.4.1673531939808; Thu, 12 Jan 2023 05:58:59 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:58:59 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 1/6] linux: Do not reset signal handler in posix_spawn if it is already SIG_DFL Date: Thu, 12 Jan 2023 10:58:48 -0300 Message-Id: <20230112135853.3786675-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-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 if the disposition is already SIG_DFL. Checked on x86_64-linux-gnu. Reviewed-by: Carlos O'Donell --- 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 b7a6fe320d..a1a14a58ae 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 Thu Jan 12 13:58:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63098 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 24B5A38543AC for ; Thu, 12 Jan 2023 14:00:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24B5A38543AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673532004; bh=ArS7Htt9pTHonptfMMtTrkvyIo6TsDzG6pfd/mCygog=; 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=C600YC1T8XX8ukfvpK/NP4knyqZ2fSo4Ph/ufZjTE6fM9QcSS2//nTawX+OSZmcdz Jw91jlBZ8k1gAUA/dxF0ebrY88j3syGl1hpTFVdfHhqlhr9VM46TIPC3Ej/46xDCLy /bCF34KCmqVpkvTFEzFlw0uqbVXMFEYBYryuwBKs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc29.google.com (mail-oo1-xc29.google.com [IPv6:2607:f8b0:4864:20::c29]) by sourceware.org (Postfix) with ESMTPS id BAD363854387 for ; Thu, 12 Jan 2023 13:59:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BAD363854387 Received: by mail-oo1-xc29.google.com with SMTP id m23-20020a4abc97000000b004bfe105c580so4829502oop.4 for ; Thu, 12 Jan 2023 05:59:02 -0800 (PST) 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:message-id:reply-to; bh=ArS7Htt9pTHonptfMMtTrkvyIo6TsDzG6pfd/mCygog=; b=e9frZPb0g5GquKayVE/QJxfOXA+f7rLBhiBHecCcrMQargTWWAmsqCiwlN2gFxmADL r5xP3d1lDCQk2D6vK/aONjdzmBK5yNXH+Xs56UOR2BLO+F8BnM7OrgrgBqoKF356FeY9 K2Z3b/GHYw7vHOtjJVWh86sLL1CUgNx+NJKTZtOR41OxkKp7mj+5lExmQX1AWKpYkyt5 lQCfEGFEzA/ngqq3cidqJVSzve0oFP9dYeLGEkgX1YIuXaZD8/BirUue+cKI/5357MPj gUApAIMXPIN1VGZQ1oxCDesRD28qj9rDfHLzXjI0DyPL+CQIt9R91zCbDsRlzPg6sl8l /ZtQ== X-Gm-Message-State: AFqh2kqy8bC9Ht/KFkZ+33Q8WMlHZiCab0xmlQu/fYU7jx/yHnrBKWMd u2vk5CTTyR04MX6R46+XiRUOssMFmtYdzzOm0lI= X-Google-Smtp-Source: AMrXdXuFqpxQOl8UNdZVGlF3RVDI//1QiwESPVHAglNg2vOmtUmbXL5RCTMDlkeWBPKW7lG1GJhMGg== X-Received: by 2002:a4a:8883:0:b0:4f2:58d:693f with SMTP id j3-20020a4a8883000000b004f2058d693fmr2681728ooa.1.1673531941435; Thu, 12 Jan 2023 05:59:01 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:59:00 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 2/6] linux: Extend internal clone3 documentation Date: Thu, 12 Jan 2023 10:58:49 -0300 Message-Id: <20230112135853.3786675-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-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, 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" Different than kernel, clone3 returns EINVAL for NULL struct clone_args or function pointer. This is similar to clone interface that return EINVAL for NULL function argument. It also clean up the Linux clone3.h interface, since it not currently exported. Checked on x86_64-linux-gnu. Reviewed-by: Carlos O'Donell --- include/clone_internal.h | 24 +++++++++++++++++++----- sysdeps/unix/sysv/linux/clone3.h | 10 +--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/clone_internal.h b/include/clone_internal.h index 4b23ef33ce..73b8114df4 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -1,10 +1,24 @@ -#ifndef _CLONE3_H -#include_next +#ifndef _CLONE_INTERNAL_H +#define _CLONE_INTERNAL_H -extern __typeof (clone3) __clone3; +#include -/* The internal wrapper of clone/clone2 and clone3. If __clone3 returns - -1 with ENOSYS, fall back to clone or clone2. */ +/* The clone3 syscall provides a superset of the functionality of the clone + interface. The kernel might extend __CL_ARGS struct in the future, with + each version with a diffent __SIZE. If the child is created, it will + start __FUNC function with __ARG arguments. + + Different than kernel, the implementation also returns EINVAL for an + invalid NULL __CL_ARGS or __FUNC (similar to __clone). + + This function is only implemented if the ABI defines HAVE_CLONE3_WRAPPER. +*/ +extern int __clone3 (struct clone_args *__cl_args, size_t __size, + int (*__func) (void *__arg), void *__arg); + +/* The internal wrapper of clone/clone2 and clone3. Different than __clone3, + it will align the stack if required. If __clone3 returns -1 with ENOSYS, + fall back to clone or clone2. */ extern int __clone_internal (struct clone_args *__cl_args, int (*__func) (void *__arg), void *__arg); diff --git a/sysdeps/unix/sysv/linux/clone3.h b/sysdeps/unix/sysv/linux/clone3.h index 42055ea981..e4d642e521 100644 --- a/sysdeps/unix/sysv/linux/clone3.h +++ b/sysdeps/unix/sysv/linux/clone3.h @@ -1,4 +1,4 @@ -/* The wrapper of clone3. +/* The clone3 kernel interface definitions. Copyright (C) 2021-2023 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -23,8 +23,6 @@ #include #include -__BEGIN_DECLS - /* The unsigned 64-bit and 8-byte aligned integer type. */ typedef __U64_TYPE __aligned_uint64_t __attribute__ ((__aligned__ (8))); @@ -58,10 +56,4 @@ struct clone_args __aligned_uint64_t cgroup; }; -/* The wrapper of clone3. */ -extern int clone3 (struct clone_args *__cl_args, size_t __size, - int (*__func) (void *__arg), void *__arg); - -__END_DECLS - #endif /* clone3.h */ From patchwork Thu Jan 12 13:58:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63100 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 7279A385438E for ; Thu, 12 Jan 2023 14:00:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7279A385438E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673532045; bh=lvY6Xw1oERCosZ7DNd/YMRcjWeEFEh1+P3YKtpwgoSE=; 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=pkzmWJqQotpyFJlfbeehWfL8sl+LtLGJCgaqvdHzohSc7ARUdCScdZbXnh6Xniuf9 bd7nHUOruZZTQnjlMRgXqb7y1zcQgukTT58asliVHSTD5CU6+eyH4EcNEy5ldhbZmq M9fR9RJTEiBpNFJK1K61LkiHC32UnKUj9ACu5130= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 9B0ED38543B2 for ; Thu, 12 Jan 2023 13:59:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9B0ED38543B2 Received: by mail-oi1-x22f.google.com with SMTP id c133so15334278oif.1 for ; Thu, 12 Jan 2023 05:59:04 -0800 (PST) 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:message-id:reply-to; bh=lvY6Xw1oERCosZ7DNd/YMRcjWeEFEh1+P3YKtpwgoSE=; b=b3ORbfTAIKWhTeTd+HZWHaYVDysIdRC2ja5ZFJulq12My6oIWF+d5pvetUqgqtVP+7 lHK5xAfof+b/W7Z2QWxdaQ/2taxlWf/tiSLKpCe3wV/gQFKH6zPIvlL2hlNSWqANGogI vNO76XhiQK9yaS0uMPuXuE0ZSShkTePIWRjWZveGAtbNFDlUqWu4+I6soD6r09i113cw +FNfSd5WaolqW9AzOkdYzZn6vGPG++7bD3ABHMZ9rmmVoXRfwRDV1rIqrvaue71nI+ED jYUQFGHFbbCQBa1S2JbWWp2nPpUjvbMmxQFkVRezGg4jGHSSC2owzdpm2CVQlgKc42Bd 6JeA== X-Gm-Message-State: AFqh2krNRqEUpPdyZ/fNeJ0+enEkAaM4S67gPQBABOOoNmr7Lmf27TYn xGkLKwpz6IWyKGDsOUfp1IlkeC6Ax8wqgFCQJgI= X-Google-Smtp-Source: AMrXdXvdAitjbiOPH75mO9CVY7A/r2++gEF7bOR4maE4ls8iSeWV/0qjwyf3Bl5mJU7LCMCkZJya3g== X-Received: by 2002:aca:1c13:0:b0:364:5b7b:194 with SMTP id c19-20020aca1c13000000b003645b7b0194mr4916321oic.44.1673531943204; Thu, 12 Jan 2023 05:59:03 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.59.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:59:02 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 3/6] Linux: Do not align the stack for __clone3 Date: Thu, 12 Jan 2023 10:58:50 -0300 Message-Id: <20230112135853.3786675-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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" All internal callers of __clone3 should provide an already aligned stack. Removing the stack alignment in __clone3 is a net gain: it simplifies the internal function contract (mask/unmask signals) along with the arch-specific code. Checked on x86_64-linux-gnu. Reviewed-by: Carlos O'Donell --- include/clone_internal.h | 3 + .../sysv/linux/tst-misalign-clone-internal.c | 74 ------------------- sysdeps/unix/sysv/linux/x86_64/clone3.S | 3 - 3 files changed, 3 insertions(+), 77 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/tst-misalign-clone-internal.c diff --git a/include/clone_internal.h b/include/clone_internal.h index 73b8114df4..f8198d8059 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -11,6 +11,9 @@ Different than kernel, the implementation also returns EINVAL for an invalid NULL __CL_ARGS or __FUNC (similar to __clone). + All callers are responsible for correctly aligning the stack. The stack is + not aligned prior to the syscall (this differs from the exported __clone). + This function is only implemented if the ABI defines HAVE_CLONE3_WRAPPER. */ extern int __clone3 (struct clone_args *__cl_args, size_t __size, diff --git a/sysdeps/unix/sysv/linux/tst-misalign-clone-internal.c b/sysdeps/unix/sysv/linux/tst-misalign-clone-internal.c deleted file mode 100644 index 8b94a74819..0000000000 --- a/sysdeps/unix/sysv/linux/tst-misalign-clone-internal.c +++ /dev/null @@ -1,74 +0,0 @@ -/* Verify that __clone_internal properly aligns the child stack. - Copyright (C) 2021-2023 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 -#include -#include -#include - -static int -check_stack_alignment (void *arg) -{ - puts ("in f"); - - return TEST_STACK_ALIGN () ? 1 : 0; -} - -static int -do_test (void) -{ - puts ("in do_test"); - - if (TEST_STACK_ALIGN ()) - FAIL_EXIT1 ("stack isn't aligned\n"); - -#ifdef __ia64__ -# define STACK_SIZE (256 * 1024) -#else -# define STACK_SIZE (128 * 1024) -#endif - char st[STACK_SIZE + 1]; - /* NB: Align child stack to 1 byte. */ - char *stack = PTR_ALIGN_UP (&st[0], 2) + 1; - struct clone_args clone_args = - { - .stack = (uintptr_t) stack, - .stack_size = STACK_SIZE, - }; - pid_t p = __clone_internal (&clone_args, check_stack_alignment, 0); - - /* Clone must not fail. */ - TEST_VERIFY_EXIT (p != -1); - - int e; - xwaitpid (p, &e, __WCLONE); - TEST_VERIFY (WIFEXITED (e)); - TEST_COMPARE (WEXITSTATUS (e), 0); - - return 0; -} - -#include diff --git a/sysdeps/unix/sysv/linux/x86_64/clone3.S b/sysdeps/unix/sysv/linux/x86_64/clone3.S index bd4a834e46..802d56cbac 100644 --- a/sysdeps/unix/sysv/linux/x86_64/clone3.S +++ b/sysdeps/unix/sysv/linux/x86_64/clone3.S @@ -73,9 +73,6 @@ L(thread_start): the outermost frame obviously. */ xorl %ebp, %ebp - /* Align stack to 16 bytes per the x86-64 psABI. */ - and $-16, %RSP_LP - /* Set up arguments for the function call. */ mov %R8_LP, %RDI_LP /* Argument. */ call *%rdx /* Call function. */ From patchwork Thu Jan 12 13:58:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63099 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 B24B238493E2 for ; Thu, 12 Jan 2023 14:00:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B24B238493E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673532006; bh=XI3wXZV2TAWMItFWp43r2FAgyZ1TjsYyP+2esnquiak=; 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=MoSJDDVKBcDaRqu6vZscFxCMhuUSJ8y6Kni6WGfvU1etONs0U4+LLSh9OlYLomknM ffiG/HiaF1EAiu6y/VRtk9AT9DX8QOrVLA6WpN0VCYpVj3bQT5T/5yAXQHIP+qf9eP IgmXqLsiZBQBRsme0s2sdzTwNUpvaBd4cL8/Ri0Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id B74663854398 for ; Thu, 12 Jan 2023 13:59:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B74663854398 Received: by mail-oi1-x22e.google.com with SMTP id r205so15323255oib.9 for ; Thu, 12 Jan 2023 05:59:06 -0800 (PST) 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:message-id:reply-to; bh=XI3wXZV2TAWMItFWp43r2FAgyZ1TjsYyP+2esnquiak=; b=LkOTvPOW+rx4s3XuK9uskiEGfhK/yfjAV0giHuoZvILfNQV2I9DVtrXv0mmeKqO+LE vKT+FVLrJDE/b60x+IdICvb6Hoe/K/8vc7+ZOjSo3swgJxky119yWVl4yZjZA/37BytW 0THAdJAphEqMp38Uxj8hMV0GDlDOP7jLdh/ME7UlgBmmeU+bgNf7ZiZ3hXQ8D5FKcGFm FS5wUgu0lTmTAcHvA3eeeoeu5TL1hlW5i8eLZDesi0IrV27v8FWIlzy6JaaNi40JTcwM vSwCnq1mOZF/0vT9aK5b2mO31innocHy4RRa6wes+LChkgeHwb5ihh67qkknO8xE9xkX Ju3w== X-Gm-Message-State: AFqh2krOKcqQ+lNCZaJHoS7f5+cwReD5Zor0Q/xTYfoeAb0q/3iya335 KyjB19rjX4nnUYjscWh0s+7TeJGI9vuNj93euHs= X-Google-Smtp-Source: AMrXdXsjhpugGW2lOpr/qnJKEjw0UR+2MlVi4RIra/DdXaToOC21EtbIzmB47/zc3CEcs8/BMq9bdQ== X-Received: by 2002:aca:db42:0:b0:360:f4ef:f3fe with SMTP id s63-20020acadb42000000b00360f4eff3femr30051518oig.58.1673531945181; Thu, 12 Jan 2023 05:59:05 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.59.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:59:04 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 4/6] linux: Add clone3 CLONE_CLEAR_SIGHAND optimization to posix_spawn Date: Thu, 12 Jan 2023 10:58:51 -0300 Message-Id: <20230112135853.3786675-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-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" 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 had to issue 2 times NSIG sigaction calls (one to obtain the current disposition and another to set either SIG_DFL or SIG_IGN). With POSIX_SPAWN_SETSIGDEF the child will setup the signal for the case where the disposition is SIG_IGN. The code must handle the fallback where clone3 is not available. This is done by splitting __clone_internal_fallback from __clone_internal. Checked on x86_64-linux-gnu. Reviewed-by: Carlos O'Donell --- include/clone_internal.h | 5 + posix/Makefile | 3 +- posix/tst-spawn7.c | 179 +++++++++++++++++++++++ sysdeps/unix/sysv/linux/Makefile | 3 +- sysdeps/unix/sysv/linux/clone-internal.c | 37 +++-- sysdeps/unix/sysv/linux/clone3.h | 7 + sysdeps/unix/sysv/linux/spawni.c | 31 ++-- 7 files changed, 238 insertions(+), 27 deletions(-) create mode 100644 posix/tst-spawn7.c diff --git a/include/clone_internal.h b/include/clone_internal.h index f8198d8059..3b6cd85f02 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -24,6 +24,11 @@ extern int __clone3 (struct clone_args *__cl_args, size_t __size, 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 38a4279599..7e4f252a40 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..a1c0d9d942 --- /dev/null +++ b/posix/tst-spawn7.c @@ -0,0 +1,179 @@ +/* Tests for posix_spawn signal handling. + Copyright (C) 2023 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', '--restart', the check type ('SIG_IGN' or 'SIG_DFL'), 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); + } + exit (EXIT_SUCCESS); + } + 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); + } + + exit (EXIT_FAILURE); +} + +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 to a handler different than + SIG_IGN or SIG_DFL. */ + 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/SIG_DFL. + + * When built with --enable-hardcoded-path-in-tests or issued without + using the loader directly. */ + + if (restart) + 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/Makefile b/sysdeps/unix/sysv/linux/Makefile index f298878e8f..f8bd12d991 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -269,8 +269,7 @@ tests-clone-internal = \ tst-align-clone-internal \ tst-clone2-internal \ tst-clone3-internal \ - tst-getpid1-internal \ - tst-misalign-clone-internal + tst-getpid1-internal tests-internal += $(tests-clone-internal) tests-static += $(tests-clone-internal) diff --git a/sysdeps/unix/sysv/linux/clone-internal.c b/sysdeps/unix/sysv/linux/clone-internal.c index a8611772a2..e125c9125e 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,23 @@ __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 + 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 e4d642e521..451b156405 100644 --- a/sysdeps/unix/sysv/linux/clone3.h +++ b/sysdeps/unix/sysv/linux/clone3.h @@ -23,6 +23,13 @@ #include #include + +/* 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 a1a14a58ae..bc321d4c58 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 is enabled because it + shares memory with parent, so all signal dispositions must be either + SIG_DFL or SIG_IGN. If clone3/CLONE_CLEAR_SIGHAND is used, there is + only the need to set the defined signals POSIX_SPAWN_SETSIGDEF to + SIG_DFL; 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, + /* Unsupported 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 in 5.3 and CLONE_CLEAR_SIGHAND in 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 Thu Jan 12 13:58:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63101 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 7293538493CC for ; Thu, 12 Jan 2023 14:00:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7293538493CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673532053; bh=Rsk0yRx5gO16zT+RO2xJvBBrvyA2e1X1gmNvuqHRlsQ=; 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=qs+Ya6osumhS6qFyEGpI9FZQqmMeCGofr/Zjf4nJwmn9ehUNkF/UPbZUIyG3jlO4o ouCwJ1zgqmf/3/SNkaV+4fkpcxT6jUzQjJWXSEb32w1o+5PrSiBwdyuIJmxZcJg51U kgVB5WIIG7cR6qMhB5KIhedV3WKpHz1RS64ICWlM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by sourceware.org (Postfix) with ESMTPS id 63B083853573 for ; Thu, 12 Jan 2023 13:59:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63B083853573 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-12c8312131fso18949305fac.4 for ; Thu, 12 Jan 2023 05:59:08 -0800 (PST) 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:message-id:reply-to; bh=Rsk0yRx5gO16zT+RO2xJvBBrvyA2e1X1gmNvuqHRlsQ=; b=h1wbSd2mGkfTWmuLczwWuuNPp3cFHtdq3g7oMgaIHkNif3kInnH4yHfniJWw8caXvH P1854gFepfIYGU2EcSMunfkJKzjHO+mWGsi7MP42nQc/m+TQXQfWKzgi2qQERBqUMG/N FIGtN+qQY/vgz17XzTcCCvlBXTWRgez+F2p1ZZri/fleqMsaZfLd2OCumQuZD9MDxsGs f3RhLjmBrWXfxsEsQMowS1TJGbS/j/98aE+2pC52N1CFyktIlcV+XbNdV7/cE7ORku6y FgExySBYTzG/yrosF9BEQ7j/3sAYHuRDoAqBKkDp4fTva+vVGgqyhTgDrkPQqfNjsBdE 5mSA== X-Gm-Message-State: AFqh2ko3Mh+cyaMkL9Qn9TM+bSuRHvN4VD3YLQMGY0utzTYR9rmDPQ9N yonoCZQvoAoI30xkgLwRr4X7GencsOR4EzDyr1w= X-Google-Smtp-Source: AMrXdXtPsZEQt1oOeeP68gr8xAwAbrUyc5EKv+5K3nanyC2v7DPwA0/oz9vdJ9dZAmJDX1BuazBOmg== X-Received: by 2002:a05:6870:2c97:b0:15e:9c48:c9b6 with SMTP id oh23-20020a0568702c9700b0015e9c48c9b6mr3328094oab.34.1673531947200; Thu, 12 Jan 2023 05:59:07 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.59.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:59:06 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 5/6] aarch64: Add the clone3 wrapper Date: Thu, 12 Jan 2023 10:58:52 -0300 Message-Id: <20230112135853.3786675-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-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); Checked on aarch64-linux-gnu. Reviewed-by: Carlos O'Donell --- sysdeps/unix/sysv/linux/aarch64/clone3.S | 85 ++++++++++++++++++++++++ sysdeps/unix/sysv/linux/aarch64/sysdep.h | 2 + 2 files changed, 87 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..88a1fa1e89 --- /dev/null +++ b/sysdeps/unix/sysv/linux/aarch64/clone3.S @@ -0,0 +1,85 @@ +/* The clone3 syscall wrapper. Linux/aarch64 version. + Copyright (C) 2023 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 + + /* 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 6520fa15da..e94d1703ad 100644 --- a/sysdeps/unix/sysv/linux/aarch64/sysdep.h +++ b/sysdeps/unix/sysv/linux/aarch64/sysdep.h @@ -165,6 +165,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 Thu Jan 12 13:58:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63102 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 02FD43857C44 for ; Thu, 12 Jan 2023 14:01:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 02FD43857C44 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673532095; bh=unFABKVu4oCCTf7080SVsIrA2MANfeujeq6072EqDdA=; 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=oxfSsFlfoQKwbDNFVjpNRA94kENDtrpbvVL1KiDOkx2BowP8mhg1MMM96mGRxSSty S6HQwAX9CkVAlEQWUdItX8DsvAiMiYmQMRC4wBSwZJbIvJYROzNkgwkky+9Si93+Xs 4OOr0XFwVNIGeSKn11dtn1QH04bmoaZaVcrgU0aA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc29.google.com (mail-oo1-xc29.google.com [IPv6:2607:f8b0:4864:20::c29]) by sourceware.org (Postfix) with ESMTPS id A0DB7385438C for ; Thu, 12 Jan 2023 13:59:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0DB7385438C Received: by mail-oo1-xc29.google.com with SMTP id m23-20020a4abc97000000b004bfe105c580so4829607oop.4 for ; Thu, 12 Jan 2023 05:59:09 -0800 (PST) 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:message-id:reply-to; bh=unFABKVu4oCCTf7080SVsIrA2MANfeujeq6072EqDdA=; b=NIjd3KGy85NhPcCDx6FeWcKbZnNSCIRTHd3Q6zDVLCOsabER8tDjta/i5twJ0IBIs3 X0ApIPQlAGyFnKK2Yh1t53WK42OAwtDklUIcblN2dNjMJqF2Mdg0/ImtLR25C0tcrnqf PJHo4AmfsyPTrePwAr6zVmXGOuEATUXkhO4v7jvB4hWjOF+3ZJkkJjjBV7aMoaFp6+RI xgHJ0NTKzs1h9ZDq4h7yqM3NPJuxjjvedJnDu6xps2JxlD+jgpfB4bS+2oowQgkxpDvK 3uDzuwQPmF583/V4G5XIhl7SNIIRSYAgby8ogyMYEIyYTSYQxImQxM57coMxmFAltVgm q0rw== X-Gm-Message-State: AFqh2krSxJ7LBpJKnV73lf5MA1BpFACig22/RrfzRQXTPVI84cDTEUxi jvJw09Rjvzsx2NHz1MMXkRC7cD9vdggH6ebB024= X-Google-Smtp-Source: AMrXdXs1zDhoOoH8i8DGoceo2o62kLlaGcWI6IFu3MSF7FB3rwUikKaysJFEhFOAj2mMliiGD806Hw== X-Received: by 2002:a4a:9613:0:b0:4a3:9f7a:add0 with SMTP id q19-20020a4a9613000000b004a39f7aadd0mr33414698ooi.5.1673531948829; Thu, 12 Jan 2023 05:59:08 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id m26-20020a4abc9a000000b004a0aac2d28fsm8335402oop.35.2023.01.12.05.59.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 05:59:08 -0800 (PST) To: libc-alpha@sourceware.org, Carlos O'Donell Subject: [PATCH v4 6/6] Linux: optimize clone3 internal usage Date: Thu, 12 Jan 2023 10:58:53 -0300 Message-Id: <20230112135853.3786675-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112135853.3786675-1-adhemerval.zanella@linaro.org> References: <20230112135853.3786675-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, 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" Add an optimization to avoid calling clone3 when glibc detects that there is no kernel support. It also adds __ASSUME_CLONE3, which allows skipping this optimization and issuing the 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. Reviewed-by: Carlos O'Donell --- include/clone_internal.h | 5 +++++ sysdeps/unix/sysv/linux/clone-internal.c | 24 ++++++++++++++++++++++- sysdeps/unix/sysv/linux/kernel-features.h | 9 +++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/clone_internal.h b/include/clone_internal.h index 3b6cd85f02..dd380f119e 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -24,6 +24,11 @@ extern int __clone3 (struct clone_args *__cl_args, size_t __size, fall back to clone or clone2. */ extern int __clone_internal (struct clone_args *__cl_args, int (*__func) (void *__arg), void *__arg); +/* clone3 wrapper with a sticky check to avoid re-issuing the syscall if + it 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 e125c9125e..790739cfce 100644 --- a/sysdeps/unix/sysv/linux/clone-internal.c +++ b/sysdeps/unix/sysv/linux/clone-internal.c @@ -76,6 +76,28 @@ __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, @@ -83,7 +105,7 @@ __clone_internal (struct clone_args *cl_args, { #ifdef HAVE_CLONE3_WRAPPER 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 a23d0079a1..4b5c4afbc1 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -241,4 +241,13 @@ # define __ASSUME_FUTEX_LOCK_PI2 0 #endif +/* The clone3 system call was introduced across on most architectures in + Linux 5.3. Not all ports implements it, so it should be used along + HAVE_CLONE3_WRAPPER define. */ +#if __LINUX_KERNEL_VERSION >= 0x050300 +# define __ASSUME_CLONE3 1 +#else +# define __ASSUME_CLONE3 0 +#endif + #endif /* kernel-features.h */