Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1861573pxb; Fri, 18 Feb 2022 17:51:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDCF4oBUssfPkD9a5pr6LXmoIif6Zy04Na0+uNZ974aX5RgZs0NwxBCR6/enGsr3eGvtOI X-Received: by 2002:a05:6a00:158c:b0:4cc:ed28:f9a1 with SMTP id u12-20020a056a00158c00b004cced28f9a1mr10135159pfk.48.1645235494897; Fri, 18 Feb 2022 17:51:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645235494; cv=none; d=google.com; s=arc-20160816; b=gXsSRBp6jqLXeGeKrQRg7pw6Mzgk4yQm2wAHcnht4lg15YnAw9mKEjZPNwoC2XjiUb zk0kCr0UIzW0otsMvESIC0e4Y8JG690KV/W1WSquuHmcwog1Ve2UjcstyHdodTBVWax6 AgQ4+Ydkgtd8jrIyKDQhfLrvit1fWEeQH7NiU+hOM6hKwYtZcH6QPOzyIsEuwmBOruGu e9bJKOQx6MFKykMghxBGgIhjtV69WKa+cWzkdv3Jmp1s4Ls9jvNCdT44NZp5c8Vh+a5t KtZfqkYOsmoq2lxUVbhgL5bYta5dRh7qMKZrrkucUultUVwZtpgWMkD6Of2+OVr2p76h AxAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature:dkim-filter; bh=jCxgnH0UNMK3Owi2ll3NEMWJYgSQdasrufNioWW7/gE=; b=OWcoOfAIqHT5T7ilGcoMw4iieFx+WR3dqUzNlCEguABuPKf0KFl6Rk84uvOlOJoXJT m/y1RCmU6iLDC9jwCGJk1xjbw3uCtpCHdlBmTAeCLpaPlphBSjXmXjWIVhaYdHbK1hRB 5Vip/wV+wXOsrPi2gnEDemhwPb3Y/NJMGo+VpYL35W0rkkAAz/EOE//d3toNUdiRg7DU IGR/2oh2dCqX/tyS/2WL+XkydklebBEvv+YM6uLpPj3s3SRemaOh5VM6dAi1Zkzn5b9a pZlIikCvghpIFMpdZbpr5ahL93QWyXNNp5BQT+1C5adG/YHAt6yOr2/9rwTdj/9IUgDt q9QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=N6yUry2c; 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=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n12si821756pjv.20.2022.02.18.17.50.31; Fri, 18 Feb 2022 17:51:34 -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=@efficios.com header.s=default header.b=N6yUry2c; 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=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239724AbiBRVQy (ORCPT + 99 others); Fri, 18 Feb 2022 16:16:54 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239663AbiBRVQN (ORCPT ); Fri, 18 Feb 2022 16:16:13 -0500 Received: from mail.efficios.com (mail.efficios.com [167.114.26.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBDAB2569FA; Fri, 18 Feb 2022 13:15:53 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 300323BA92B; Fri, 18 Feb 2022 16:06:44 -0500 (EST) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id gxHMX7oOs1gJ; Fri, 18 Feb 2022 16:06:43 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 9FB843BA956; Fri, 18 Feb 2022 16:06:43 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 9FB843BA956 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1645218403; bh=jCxgnH0UNMK3Owi2ll3NEMWJYgSQdasrufNioWW7/gE=; h=From:To:Date:Message-Id; b=N6yUry2cuukjTzNG3GP6YHRVGCkZTfHMwCN/eeYamC9oOLxJEiV08dhNUnKWRvNvX 1PbqbkgoLwjqcWSPfzU1ihyOK80IFattyAaXDNc4fenUcoxGhpU52jUpB0yBBD/MCb /szseLusBFAaAKWGBIrRkhysTe6vy1/9Nts9Y7Tl0tCod0f3B4vvM4f4Zts42VSxXT kW2hbJXaO9rjSSBNOBlQq8Wf6vAs/VuwkyLz0KcU5UnsOtSB3AHUteharNicDgkoB1 0p5YCaaDJYdnFE9axd8TYZDTDUa4/2v3pW3EPcWml9/Z6qqbCUeYlzAIdNCct1SILd +MJyXlqmw+F4g== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id hLVxdEubDYpf; Fri, 18 Feb 2022 16:06:43 -0500 (EST) Received: from localhost.localdomain (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by mail.efficios.com (Postfix) with ESMTPSA id 388763BAB11; Fri, 18 Feb 2022 16:06:43 -0500 (EST) From: Mathieu Desnoyers To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , "Paul E . McKenney" , Boqun Feng , "H . Peter Anvin" , Paul Turner , linux-api@vger.kernel.org, Christian Brauner , Florian Weimer , David.Laight@ACULAB.COM, carlos@redhat.com, Peter Oskolkov , Mathieu Desnoyers Subject: [RFC PATCH v2 04/11] selftests/rseq: Use ELF auxiliary vector for extensible rseq Date: Fri, 18 Feb 2022 16:06:26 -0500 Message-Id: <20220218210633.23345-5-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220218210633.23345-1-mathieu.desnoyers@efficios.com> References: <20220218210633.23345-1-mathieu.desnoyers@efficios.com> X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Signed-off-by: Mathieu Desnoyers --- tools/testing/selftests/rseq/rseq-abi.h | 5 ++ tools/testing/selftests/rseq/rseq.c | 68 ++++++++++++++++++++++--- tools/testing/selftests/rseq/rseq.h | 15 ++++-- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/rseq/rseq-abi.h b/tools/testing/selftests/rseq/rseq-abi.h index a8c44d9af71f..00ac846d85b0 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 986b9458efb2..506f2b17aea6 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" @@ -39,17 +41,35 @@ static const unsigned int *libc_rseq_flags_p; /* 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. */ +/* 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 succeeded. */ + +/* 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,18 @@ 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(); + } + rseq_size = 0; + rseq_feature_size = 0; return -1; + } assert(rseq_current_cpu_raw() >= 0); + RSEQ_WRITE_ONCE(rseq_reg_success, 1); return 0; } @@ -99,12 +127,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) { @@ -116,14 +160,21 @@ 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; } if (!rseq_available()) return; rseq_ownership = 1; 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)) @@ -133,6 +184,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 9d850b290c2e..e73db2e82a11 100644 --- a/tools/testing/selftests/rseq/rseq.h +++ b/tools/testing/selftests/rseq/rseq.h @@ -47,13 +47,20 @@ #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) { -- 2.17.1