From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id F32E81F910 for ; Fri, 4 Nov 2022 19:03:31 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="XZK94dTK"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4051F3857427 for ; Fri, 4 Nov 2022 19:03:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4051F3857427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667588610; bh=uF584Pe2FhYRMf139O4h3BC4O+5B/Oe/B7HT6TbD91k=; 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=XZK94dTK4zjxD31u5L720i0ml7LiWcZNL3TGUbA1zNjO3TJgQELgSljLgPIhnEM1v 3GzYrgy58ccYPSr6m2Vf+i2DxTqZ45mW02kTohCD3dvIjbO1tSVNUhanqUIMfx5GAT D2pJDtgtvz6q5wqb367v7xlLYhVIpTnPLxpDmEro= 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 99BFB3858429 for ; Fri, 4 Nov 2022 19:01:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 99BFB3858429 Received: by mail-oi1-x22c.google.com with SMTP id b124so6119005oia.4 for ; Fri, 04 Nov 2022 12:01:36 -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=uF584Pe2FhYRMf139O4h3BC4O+5B/Oe/B7HT6TbD91k=; b=k2hV3YZfXuCspCXte4S5RucIxTTZEWSYLkdXxrV3TGhRtl0ThdjGcSOID9XmH3Xkm4 1bQnuB0UF9aCdzjSyPOVslxqd42grq+8LQX+YLcvLLsmqWrldzWbtvNkRYO4jxs1cjq2 oR2eSPlMYv8M32DPguB8BgmKZyzbYojJRy5pPewA+gP0w0oQ9LiKeKYwa8bL7NvQoDQp 4NBKahaEziTketLyAgVeWfzyqD8T0Lb2NL/O68urtHwHwoQZMaOuxsuS0/nBWzomnXJb kEK6jf1L1uv9RyU+rj6uNvPi+mB5Z8mbss+OvydfY41yTl+N56YPT25bd8RY/IJjKtsU 8ilA== X-Gm-Message-State: ACrzQf1h/PzZNx8/BBZ44YrphP61N3llmJeaxfsEJCdRANgNRWF29NyU 2t+C0gyheoQoV1Z9vFOP4HGMln73aU9+91Cr X-Google-Smtp-Source: AMsMyM55CF8lMzmd4y7L8FnnRdjd3vumPWAIVyFZxdko5WHmkCzfUjjARhz8ysHXX8npQi8225k1bg== X-Received: by 2002:a05:6808:1245:b0:350:c462:1528 with SMTP id o5-20020a056808124500b00350c4621528mr19337434oiv.112.1667588496121; Fri, 04 Nov 2022 12:01:36 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 12:01:35 -0700 (PDT) To: libc-alpha@sourceware.org, Szabolcs Nagy , "H . J . Lu" Subject: [PATCH v2 11/11] Linux: optimize clone3 internal usage Date: Fri, 4 Nov 2022 16:01:12 -0300 Message-Id: <20221104190112.2566409-12-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 Content-Transfer-Encoding: 8bit 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: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Cc: Christian Brauner Errors-To: libc-alpha-bounces+e=80x24.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 | 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 81292b3eb1..5f936dd8dc 100644 --- a/include/clone_internal.h +++ b/include/clone_internal.h @@ -26,6 +26,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-issue 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 7bc991e033..39d76733db 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 74adc3956b..4ecd08a98f 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -236,4 +236,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 */ -- 2.34.1