From patchwork Tue Dec 27 12:13:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 36871 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1361629wrt; Tue, 27 Dec 2022 04:17:37 -0800 (PST) X-Google-Smtp-Source: AMrXdXuWbA7eaFSFyNG/rD3OPAi0i+Kmsy3rDGl60nU88OZ8UgDV3WxuLZNAZoa0GNiAIBrJreTb X-Received: by 2002:a17:907:9d03:b0:7c1:a0c:a866 with SMTP id kt3-20020a1709079d0300b007c10a0ca866mr18821155ejc.75.1672143457731; Tue, 27 Dec 2022 04:17:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672143457; cv=none; d=google.com; s=arc-20160816; b=bb4dodijcq+iwRkHOouAWaQRytXwrsSbRZ3KEQ2VN7Dw429ks5IAWmwDlV73OfO+Ym y43evzDXpBvkovktUxbiJSVFK3XeNrEDX/D+QP1wYAjxtVBnsaORm4G8MIl2iPnlTSMY fUcOZt+SR8+q39NOjHbPHQnnUFOozh3xcYRb4AgXVRVtAs7ARka+e6O8IBPQIaD6WhGA WMP1fnNHHAK5NA2P2CS3tYZigZas4nsE9RBC6Pv6ShndxQpAwV8nPGcGhXg+twCDdjZV 1EH5PHAXi11cEnQDZSgmGWFHwOSxRsFm3b4L2NiEhOeQE0zTDVQdMm1sCPZ94PFNrrPK 7bSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=mAOa34tc7kBV+mchywOyvV8HysVruET6BguhI5CRWGU=; b=o9so7AGNF1DfjKjYIyeq13wUN+8sG2n7VGK2UZNbK8r9l2cb+3GMmrnQmmwmIOzXG0 ynzMjtfpObo9/e0po+1eCrOKJ5B0qLOuyttizmvbOB0ZVT5u+IXc5aWfK5YZxcrtVvhh r7Q1XrsUFAUR+/eaFU1cEB3aQOzdhNHMP5uHs6tS+/XjMvj5Gd20RJEYMVNE+fhYSLG3 Q8RmqmKoGtI9P4tF2PRWyjLBozflnMvB4tiPw6MXRpLK/E6Wp5yG1C2qITj88nb2yT4r 64nPIfIejvTVttFHplhESe9Ls44Mhj1em2KDPSjKXdNBods7oGd6ZikHzLA0K8/FUw8J s9yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OJr4P1Sh; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020a170906455300b007bf8440bc14si8653620ejq.784.2022.12.27.04.17.13; Tue, 27 Dec 2022 04:17:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OJr4P1Sh; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232116AbiL0MQ0 (ORCPT + 99 others); Tue, 27 Dec 2022 07:16:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231180AbiL0MOl (ORCPT ); Tue, 27 Dec 2022 07:14:41 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EF80F2F; Tue, 27 Dec 2022 04:13:56 -0800 (PST) Date: Tue, 27 Dec 2022 12:13:52 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1672143232; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mAOa34tc7kBV+mchywOyvV8HysVruET6BguhI5CRWGU=; b=OJr4P1Shj3ZlAiZ9xlChqHUsSzwayvujzQ1psrD4ZL3g9PH7NL3i3STU6QwsznOTtjVFL9 l0TcLI+APhEmMrIV52hlTuqFc6Im5SRo6Ti1EbmG+O86rkKQcTMFKy3Ol/0pF9R13xPeTA A5LUmm3lMv4DIPrwvubfPUzrUXI1X/TsUlGAJmtUnbBNPdME8s0qDOscObRuM1WeQYT349 gCbjIyua94Pce3ffhS+Rn6p2HHq8kIN3PNrOR/QHASvJjWkU/ua0KXh3HQ0finiT3xLEiP T6VYV6hPNlzYdXPzpuvHOKH2lMy7FXCtf8yXQK2zWM8Mu9xBjdttrm99taODgA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1672143232; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mAOa34tc7kBV+mchywOyvV8HysVruET6BguhI5CRWGU=; b=aGi57LfKsdmBq5J8i+JzjvrUi9Xo1But+Z4+O0onWAHuluBh6lEB5W+NPAOkKBXRIK9tzQ Q3O7fRzsDHcsE2AQ== From: "tip-bot2 for Mathieu Desnoyers" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] selftests/rseq: Use ELF auxiliary vector for extensible rseq Cc: Mathieu Desnoyers , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20221122203932.231377-6-mathieu.desnoyers@efficios.com> References: <20221122203932.231377-6-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 Message-ID: <167214323207.4906.10658635695375494462.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750230293681601874?= X-GMAIL-MSGID: =?utf-8?q?1753369498208672218?= The following commit has been merged into the sched/core branch of tip: Commit-ID: 03f5c0272d1b59343144e199becc911dae52c37e Gitweb: https://git.kernel.org/tip/03f5c0272d1b59343144e199becc911dae52c37e Author: Mathieu Desnoyers AuthorDate: Tue, 22 Nov 2022 15:39:07 -05:00 Committer: Peter Zijlstra CommitterDate: Tue, 27 Dec 2022 12:52:11 +01:00 selftests/rseq: Use ELF auxiliary vector for extensible rseq Use the ELF auxiliary vector AT_RSEQ_FEATURE_SIZE to detect the RSEQ features supported by the kernel. Signed-off-by: Mathieu Desnoyers Signed-off-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/r/20221122203932.231377-6-mathieu.desnoyers@efficios.com --- tools/testing/selftests/rseq/rseq-abi.h | 5 ++- tools/testing/selftests/rseq/rseq.c | 67 +++++++++++++++++++++--- tools/testing/selftests/rseq/rseq.h | 18 ++++-- 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/rseq/rseq-abi.h b/tools/testing/selftests/rseq/rseq-abi.h index a8c44d9..00ac846 100644 --- a/tools/testing/selftests/rseq/rseq-abi.h +++ b/tools/testing/selftests/rseq/rseq-abi.h @@ -146,6 +146,11 @@ struct rseq_abi { * this thread. */ __u32 flags; + + /* + * Flexible array member at end of structure, after last feature field. + */ + char end[]; } __attribute__((aligned(4 * sizeof(__u64)))); #endif /* _RSEQ_ABI_H */ diff --git a/tools/testing/selftests/rseq/rseq.c b/tools/testing/selftests/rseq/rseq.c index 376a73f..1e8e326 100644 --- a/tools/testing/selftests/rseq/rseq.c +++ b/tools/testing/selftests/rseq/rseq.c @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include "../kselftest.h" #include "rseq.h" @@ -36,20 +38,38 @@ static const ptrdiff_t *libc_rseq_offset_p; static const unsigned int *libc_rseq_size_p; static const unsigned int *libc_rseq_flags_p; -/* Offset from the thread pointer to the rseq area. */ +/* Offset from the thread pointer to the rseq area. */ ptrdiff_t rseq_offset; -/* Size of the registered rseq area. 0 if the registration was - unsuccessful. */ +/* + * Size of the registered rseq area. 0 if the registration was + * unsuccessful. + */ unsigned int rseq_size = -1U; /* Flags used during rseq registration. */ unsigned int rseq_flags; +/* + * rseq feature size supported by the kernel. 0 if the registration was + * unsuccessful. + */ +unsigned int rseq_feature_size = -1U; + static int rseq_ownership; +static int rseq_reg_success; /* At least one rseq registration has succeded. */ + +/* Allocate a large area for the TLS. */ +#define RSEQ_THREAD_AREA_ALLOC_SIZE 1024 + +/* Original struct rseq feature size is 20 bytes. */ +#define ORIG_RSEQ_FEATURE_SIZE 20 + +/* Original struct rseq allocation size is 32 bytes. */ +#define ORIG_RSEQ_ALLOC_SIZE 32 static -__thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"))) = { +__thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"), aligned(RSEQ_THREAD_AREA_ALLOC_SIZE))) = { .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED, }; @@ -84,10 +104,16 @@ int rseq_register_current_thread(void) /* Treat libc's ownership as a successful registration. */ return 0; } - rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), 0, RSEQ_SIG); - if (rc) + rc = sys_rseq(&__rseq_abi, rseq_size, 0, RSEQ_SIG); + if (rc) { + if (RSEQ_READ_ONCE(rseq_reg_success)) { + /* Incoherent success/failure within process. */ + abort(); + } return -1; + } assert(rseq_current_cpu_raw() >= 0); + RSEQ_WRITE_ONCE(rseq_reg_success, 1); return 0; } @@ -99,12 +125,28 @@ int rseq_unregister_current_thread(void) /* Treat libc's ownership as a successful unregistration. */ return 0; } - rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG); + rc = sys_rseq(&__rseq_abi, rseq_size, RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG); if (rc) return -1; return 0; } +static +unsigned int get_rseq_feature_size(void) +{ + unsigned long auxv_rseq_feature_size, auxv_rseq_align; + + auxv_rseq_align = getauxval(AT_RSEQ_ALIGN); + assert(!auxv_rseq_align || auxv_rseq_align <= RSEQ_THREAD_AREA_ALLOC_SIZE); + + auxv_rseq_feature_size = getauxval(AT_RSEQ_FEATURE_SIZE); + assert(!auxv_rseq_feature_size || auxv_rseq_feature_size <= RSEQ_THREAD_AREA_ALLOC_SIZE); + if (auxv_rseq_feature_size) + return auxv_rseq_feature_size; + else + return ORIG_RSEQ_FEATURE_SIZE; +} + static __attribute__((constructor)) void rseq_init(void) { @@ -117,16 +159,24 @@ void rseq_init(void) rseq_offset = *libc_rseq_offset_p; rseq_size = *libc_rseq_size_p; rseq_flags = *libc_rseq_flags_p; + rseq_feature_size = get_rseq_feature_size(); + if (rseq_feature_size > rseq_size) + rseq_feature_size = rseq_size; return; } rseq_ownership = 1; if (!rseq_available()) { rseq_size = 0; + rseq_feature_size = 0; return; } rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer(); - rseq_size = sizeof(struct rseq_abi); rseq_flags = 0; + rseq_feature_size = get_rseq_feature_size(); + if (rseq_feature_size == ORIG_RSEQ_FEATURE_SIZE) + rseq_size = ORIG_RSEQ_ALLOC_SIZE; + else + rseq_size = RSEQ_THREAD_AREA_ALLOC_SIZE; } static __attribute__((destructor)) @@ -136,6 +186,7 @@ void rseq_exit(void) return; rseq_offset = 0; rseq_size = -1U; + rseq_feature_size = -1U; rseq_ownership = 0; } diff --git a/tools/testing/selftests/rseq/rseq.h b/tools/testing/selftests/rseq/rseq.h index 6f75133..95adc1e 100644 --- a/tools/testing/selftests/rseq/rseq.h +++ b/tools/testing/selftests/rseq/rseq.h @@ -47,14 +47,24 @@ #include "rseq-thread-pointer.h" -/* Offset from the thread pointer to the rseq area. */ +/* Offset from the thread pointer to the rseq area. */ extern ptrdiff_t rseq_offset; -/* Size of the registered rseq area. 0 if the registration was - unsuccessful. */ + +/* + * Size of the registered rseq area. 0 if the registration was + * unsuccessful. + */ extern unsigned int rseq_size; -/* Flags used during rseq registration. */ + +/* Flags used during rseq registration. */ extern unsigned int rseq_flags; +/* + * rseq feature size supported by the kernel. 0 if the registration was + * unsuccessful. + */ +extern unsigned int rseq_feature_size; + static inline struct rseq_abi *rseq_get_abi(void) { return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);