From patchwork Fri Nov 4 19:01:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 59962 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 D2471385741E for ; Fri, 4 Nov 2022 19:04:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D2471385741E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667588645; bh=gnmIwmLU9JVdyOReTyN46Fa6aAxsdMNzd6qprRaYUBA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ds8BDeFPZgU3JejVn73/FmctYDfaCGXbXCGp+baFo6GmvTKre2yc+bgwZ5qBm/L6c pXXXmYBgQKtcIdkXRkVsHU66IRPieHfjlWWOxW9xCXy3MwTA2kRsn0/iQZtmUA6kIU +XqOfTCGRju+n61tVHqXKCgbrQnO+dzoL+MhUK/Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id D3D76385842E for ; Fri, 4 Nov 2022 19:01:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D3D76385842E Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-13bd2aea61bso6577027fac.0 for ; Fri, 04 Nov 2022 12:01:22 -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:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gnmIwmLU9JVdyOReTyN46Fa6aAxsdMNzd6qprRaYUBA=; b=zkwVLQkQMBvaejQg80AoErGCa4RTyBDj0QBPkFwMBQFBfP+H3rMHJU/wODpNv2l35G qbIP0ufe6V9Mw9e7ikMPsKPYspCepkH7tZUzMjnH+MY6lWVHoowV0IPNtYJe4LpWBZox emc0ydt9ZqvPLkx5B0CsD0NwHkCmjzhk/dlzLWHMnm01aGv0aKv0XOqSbUes8ZuaVV1O PaGpUXYPuDCRPk7hQeesBUtM72tE3QmxeT8UQO6i3SQLppdW36onaGTiDHLpjHAT4hGT tne8N4Q466jeqqE4VoeXFJLvpK5bgJyPCvBO4Pf7+i6VqcTg40Ebq/Uf4bGBgALNnIOL 98QA== X-Gm-Message-State: ACrzQf184t8JFogcRIe+OLZBzPBIMdoqLqkWiz1tO2wLoRYV4H3d43s+ 3uyn46EVDgzdFjrSuxhpmFa/mv0GNLD6A8Lo X-Google-Smtp-Source: AMsMyM6c9qoMUrjC700i5QLayMq3jdQtMSGWx7jeCplDJkMotHPXmVL7NLolbRaEA+U0pDrpMENOGA== X-Received: by 2002:a05:6870:d285:b0:12b:cdce:63d8 with SMTP id d5-20020a056870d28500b0012bcdce63d8mr31011948oae.140.1667588481928; Fri, 04 Nov 2022 12:01:21 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:a9f4:f725:94bd:4cc1:1762]) by smtp.gmail.com with ESMTPSA id 24-20020aca1018000000b0035763a9a36csm1764713oiq.44.2022.11.04.12.01.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 12:01:21 -0700 (PDT) To: libc-alpha@sourceware.org, Szabolcs Nagy , "H . J . Lu" Subject: [PATCH v2 03/11] Linux: Do not align the stack for __clone3 Date: Fri, 4 Nov 2022 16:01:04 -0300 Message-Id: <20221104190112.2566409-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221104190112.2566409-1-adhemerval.zanella@linaro.org> References: <20221104190112.2566409-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 Reply-To: Adhemerval Zanella Cc: Christian Brauner Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Adjusting the stack on child thread is not async-signal safe, since kernel does not enforce stack alignmnet. Although, all the internal usage of __clone_internal are done with all signal masked, removing stack alignment of __clone3 is still a net gain: is simplifies the function contract detail (mask/unmarsk signals) and simplifies the arch-specific code. The internal users are responsible for correct stack alignment. If glibc ever exports a clone3 wrapper, it should adjust stack alignment. Checked on x86_64-linux-gnu. --- include/clone_internal.h | 5 ++ .../sysv/linux/tst-misalign-clone-internal.c | 74 ------------------- sysdeps/unix/sysv/linux/x86_64/clone3.S | 3 - 3 files changed, 5 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..5a48c7caa5 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -11,6 +11,11 @@ Different than kernel, the implementation also returns EINVAL for an invalid NULL __CL_ARGS or __FUNC (similar to __clone). + The stack is not aligned prior the syscall, different than exported + __clone. Adjusting the stack on child is not async-signal safe and since + it is used only internally the caller is responsible for proper stack + alignment. + 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 e039ca5a3a..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-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 -#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 18fc2ee966..989b62bdf6 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. */