From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vs1-xe29.google.com (mail-vs1-xe29.google.com [IPv6:2607:f8b0:4864:20::e29]) by sourceware.org (Postfix) with ESMTPS id DFA98385C421 for ; Fri, 30 Sep 2022 19:26:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DFA98385C421 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-vs1-xe29.google.com with SMTP id h1so5808394vsr.11 for ; Fri, 30 Sep 2022 12:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date; bh=w/89cZOJlnIQWcODKhPufgZxqIZlL4iGoZb2IG/mnPA=; b=KaAcGuiKYWYzY9fsI9Sckq3QGppZyufs3gKKzBRSYHUTuG/2N2vc25VPpSAitoa/kp hcUfAihWykwpRqgFnjzU8qlWV2WfgeBDcCko2FBLT3otG6blFRQ490IfzcZYqkKf55sS GjeqkGvM0u14J4kwHX8BzpRSA4C53gu2xRFxaHztZ5QZhfQYtT8F6k3xPVEiBFrtJt8V EcBKh5wfdjQ6ANjOoErKKl7K1gJZpc83UlSYO5BHJdqCg7iVhv9EF5zVM4J9Or2bWhY3 bqZByPtPh35gvHlNAPQ3rYrsMsq7RgKjmaIqCqsycbZ18ASak2YUBxrC5+G40mqKXRZZ YIwA== 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=w/89cZOJlnIQWcODKhPufgZxqIZlL4iGoZb2IG/mnPA=; b=hPOkjKr6yctbioYtER2VnvJiPZG1AaWzuI+XmJ9EAbkNADoR7lBXrRO4YrCiXzO9dL TMYTbG0p6ltJyvqj8XiOoB4DpEDGPsa8ArIb5BDGAhkFvsngbIRqwipH/n3Au3eh12Zx lfZPmyDC1NbO+9blzXWRtYr39ZIXrvNXfel2wiJ5yI/zvYvBWjpRfrvcAgrrmqNqKJLU viOPheoLmj5egpwp92j+T4E3p7k95b8/YykiPp8b+Dc73d46hEMlxh+J768RrcglIx+o vhAun4hag7AbRepwmwOaRl2BgT2vTnZHI9VgXHJ6BhUvFWjafWg9ui4fIsBQJCE0KFcG obXw== X-Gm-Message-State: ACrzQf0RgNMh8pYjLk+gI7C2Eg+KzOboFY/IcZhfKcGy3EUSwNSEQWND vFM0bR1jF+G0w5KDcMIkra+UeAoDMnqFA22s X-Google-Smtp-Source: AMsMyM4q4UmkTL3MOYfoI6SpUN6TtbqPvrxJiJmxST//6fvhIVrIt4Jvkw1y5hH7v/j9M4tE5+Q8MQ== X-Received: by 2002:a67:e182:0:b0:3a6:37bd:7b45 with SMTP id e2-20020a67e182000000b003a637bd7b45mr333594vsl.10.1664565992025; Fri, 30 Sep 2022 12:26:32 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:3736:cce5:8b83:d63b:2954]) by smtp.gmail.com with ESMTPSA id h197-20020a1f21ce000000b0039ecbaa55d8sm2381545vkh.44.2022.09.30.12.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 12:26:31 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Christian Brauner Subject: [PATCH v2 9/9] Linux: optimize clone3 internal usage Date: Fri, 30 Sep 2022 16:26:13 -0300 Message-Id: <20220930192613.3491147-10-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220930192613.3491147-1-adhemerval.zanella@linaro.org> References: <20220930192613.3491147-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 List-Id: 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 320640e64b..385f3c5589 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 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