From patchwork Thu Nov 3 20:03:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 15157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp742037wru; Thu, 3 Nov 2022 13:05:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6p34LiFE5TxH2nh5xAjjnX6zM2AxBiLCE4pju1xB2zmfMQz9QegP4hlSlq99R2kD6p0kVl X-Received: by 2002:a17:902:ef85:b0:187:85d:bd2e with SMTP id iz5-20020a170902ef8500b00187085dbd2emr29346735plb.31.1667505943724; Thu, 03 Nov 2022 13:05:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667505943; cv=none; d=google.com; s=arc-20160816; b=KtzOZIebilz5X5Lk8zVts/WjHEyBVnniVMIDrUjrA9J/srXcFIKzEPCfWJrI2B83fy 1ubLR4TzeEsAfYWz5Fqh/FKIDATuHg47Jwsa8fXHMJuthYgZn6oyIPJTTfdC+5GxJdsU +mIWi876amvnBY+5sj7VQJLX34ZyUDbXx5l7DJ8TXZrG0L8MezvAg0HJXbUKCasE9DLm t29moByTx7BlrSB/t6pXd2OoeNPX3ILJfB1oC6dB2ADNF9jiWHFxTVsPUd/+FIurDTs4 ku0tbKFLAwfeKuRHZLEMvSAnf7mmg71AlQ7tNJC1aYqlrXk5w3lNB5qR09h5M7IyEMkc siLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=C/fhS5HOkaec6eVgEKIZwZlWmSgBGFa9711lZ212m4Y=; b=MolN0CTS0w4STo2PrjcIIwID5Y7bxdsx6H4OrRjkR5b3xhTvDxB/bKPJbgWUTvg4lC fJSq3tsjk9PAeXwTb4QDhkhGBXpSovezz0VtQoEvVGbjoMODNTcLPKU4651rGc0ftW97 ZqbPTJcZxzsPZft0RD1RdULnfXSp0/WwNBLXvprC3oxCfaPAJWcIItoVqujnHtrqr8+i 2pCnhvB8pv1aGPolsylCcY/PIIpqQuC1oPgu6qtqPfDExcHJAgM0d4fLrDvpaCW/T8i9 Kri0z1t40DoePe6ET4vqf5hhW8XeOHn6TwKMsXINwG20xbgqqwcVue6f6nXauwKO66bf GJvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=SevLZvZg; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w10-20020a170902e88a00b00176b26d519dsi2070876plg.350.2022.11.03.13.05.30; Thu, 03 Nov 2022 13:05:43 -0700 (PDT) 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=smtpout1 header.b=SevLZvZg; 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 S231328AbiKCUFL (ORCPT + 99 others); Thu, 3 Nov 2022 16:05:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230383AbiKCUE7 (ORCPT ); Thu, 3 Nov 2022 16:04:59 -0400 Received: from smtpout.efficios.com (smtpout.efficios.com [IPv6:2607:5300:203:5aae::31e5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0D6C13F5C; Thu, 3 Nov 2022 13:04:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1667505896; bh=QFKjPrOBRaJ+RE5VcVuzV69Cm1OaO5G8ZsyHghodm3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SevLZvZgmxJF2FTYxR9iKfeJdJrDvFaQWkjDakFeH53s7rGPiAFqjwHPnEtBqPuvv eAQDp2GQGvEJKlyzfgKXb1OyYm1ayf9oGgjpUzcqGsH/QPJV879LU4e3IumAKld5I6 wwauz0TK2JYBz2pJIe1mS6qny8cEpBqdXUl/LNaZYdlqs/sDVZQC+DYXJ9S5MTUflU wq3I2ea8LbIoDtWy38e6UNn18fSFBDaJKf3uhXuC6nCqsV/NV5gxtkqn2t8rkCqJlT UZohC55K6S27HVfbniBj32qyz3azdo+TcP/csB0LAY96WpsIbOM1o0WpwmA1muhjGs mEGY57Ug8ClzA== Received: from localhost.localdomain (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4N3F945M9tzg71; Thu, 3 Nov 2022 16:04:56 -0400 (EDT) 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 , Alexander Mikhalitsyn , Chris Kennelly , Mathieu Desnoyers Subject: [PATCH v5 05/24] selftests/rseq: Implement rseq numa node id field selftest Date: Thu, 3 Nov 2022 16:03:40 -0400 Message-Id: <20221103200359.328736-6-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221103200359.328736-1-mathieu.desnoyers@efficios.com> References: <20221103200359.328736-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1748506712763609586?= X-GMAIL-MSGID: =?utf-8?q?1748506712763609586?= Test the NUMA node id extension rseq field. Compare it against the value returned by the getcpu(2) system call while pinned on a specific core. Signed-off-by: Mathieu Desnoyers --- tools/testing/selftests/rseq/basic_test.c | 4 ++++ tools/testing/selftests/rseq/rseq-abi.h | 8 +++++++ tools/testing/selftests/rseq/rseq.c | 18 +++++++++++++++ tools/testing/selftests/rseq/rseq.h | 28 +++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/tools/testing/selftests/rseq/basic_test.c b/tools/testing/selftests/rseq/basic_test.c index d8efbfb89193..295eea16466f 100644 --- a/tools/testing/selftests/rseq/basic_test.c +++ b/tools/testing/selftests/rseq/basic_test.c @@ -22,6 +22,8 @@ void test_cpu_pointer(void) CPU_ZERO(&test_affinity); for (i = 0; i < CPU_SETSIZE; i++) { if (CPU_ISSET(i, &affinity)) { + int node; + CPU_SET(i, &test_affinity); sched_setaffinity(0, sizeof(test_affinity), &test_affinity); @@ -29,6 +31,8 @@ void test_cpu_pointer(void) assert(rseq_current_cpu() == i); assert(rseq_current_cpu_raw() == i); assert(rseq_cpu_start() == i); + node = rseq_fallback_current_node(); + assert(rseq_current_node_id() == node); CPU_CLR(i, &test_affinity); } } diff --git a/tools/testing/selftests/rseq/rseq-abi.h b/tools/testing/selftests/rseq/rseq-abi.h index 00ac846d85b0..a1faa9162d52 100644 --- a/tools/testing/selftests/rseq/rseq-abi.h +++ b/tools/testing/selftests/rseq/rseq-abi.h @@ -147,6 +147,14 @@ struct rseq_abi { */ __u32 flags; + /* + * Restartable sequences node_id field. Updated by the kernel. Read by + * user-space with single-copy atomicity semantics. This field should + * only be read by the thread which registered this data structure. + * Aligned on 32-bit. Contains the current NUMA node ID. + */ + __u32 node_id; + /* * Flexible array member at end of structure, after last feature field. */ diff --git a/tools/testing/selftests/rseq/rseq.c b/tools/testing/selftests/rseq/rseq.c index c1c691eca904..191fcc94ce9c 100644 --- a/tools/testing/selftests/rseq/rseq.c +++ b/tools/testing/selftests/rseq/rseq.c @@ -79,6 +79,11 @@ static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len, return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig); } +static int sys_getcpu(unsigned *cpu, unsigned *node) +{ + return syscall(__NR_getcpu, cpu, node, NULL); +} + int rseq_available(void) { int rc; @@ -200,3 +205,16 @@ int32_t rseq_fallback_current_cpu(void) } return cpu; } + +int32_t rseq_fallback_current_node(void) +{ + uint32_t cpu_id, node_id; + int ret; + + ret = sys_getcpu(&cpu_id, &node_id); + if (ret) { + perror("sys_getcpu()"); + return ret; + } + return (int32_t) node_id; +} diff --git a/tools/testing/selftests/rseq/rseq.h b/tools/testing/selftests/rseq/rseq.h index 95adc1e1b0db..fd17d0e54a1b 100644 --- a/tools/testing/selftests/rseq/rseq.h +++ b/tools/testing/selftests/rseq/rseq.h @@ -20,6 +20,15 @@ #include "rseq-abi.h" #include "compiler.h" +#ifndef rseq_sizeof_field +#define rseq_sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) +#endif + +#ifndef rseq_offsetofend +#define rseq_offsetofend(TYPE, MEMBER) \ + (offsetof(TYPE, MEMBER) + rseq_sizeof_field(TYPE, MEMBER)) +#endif + /* * Empty code injection macros, override when testing. * It is important to consider that the ASM injection macros need to be @@ -128,6 +137,11 @@ int rseq_unregister_current_thread(void); */ int32_t rseq_fallback_current_cpu(void); +/* + * Restartable sequence fallback for reading the current node number. + */ +int32_t rseq_fallback_current_node(void); + /* * Values returned can be either the current CPU number, -1 (rseq is * uninitialized), or -2 (rseq initialization has failed). @@ -163,6 +177,20 @@ static inline uint32_t rseq_current_cpu(void) return cpu; } +static inline bool rseq_node_id_available(void) +{ + return (int) rseq_feature_size >= rseq_offsetofend(struct rseq_abi, node_id); +} + +/* + * Current NUMA node number. + */ +static inline uint32_t rseq_current_node_id(void) +{ + assert(rseq_node_id_available()); + return RSEQ_ACCESS_ONCE(rseq_get_abi()->node_id); +} + static inline void rseq_clear_rseq_cs(void) { RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);