Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp27413608rwd; Tue, 4 Jul 2023 02:35:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7dxQmc5P8c4GRwPPJ2r9OSfiSQSeIRnRf40l7hKjVagR7h4Fs5NS9mfhZ5hvivR3vmkbBe X-Received: by 2002:a9d:4b0e:0:b0:6b2:9462:570e with SMTP id q14-20020a9d4b0e000000b006b29462570emr11814465otf.22.1688463336852; Tue, 04 Jul 2023 02:35:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688463336; cv=none; d=google.com; s=arc-20160816; b=vP6qX188IxyOwZVnUx1SqgUos+0BOMOun3779PeeyY012c0PMpNXGJgCEDhHjVUTT1 z+BXYiL/YwQemdbHUJ2U4LqfTieU6oE7FuCJ71BTI5waUv64l1xqXaQOSM5F+TnXjwPI df74yYyb+yuoAiTJijKJsnjdvQIYpaM0QGGLXMjM7SPF051TFvbB1Z9cQaoiQSTkILOj yWwTZuJ2AQ6tsxpiVzeMNDZpRZiZDWh0PUTDkA2hJiA0G/rzFI2GtHRgqNqv6t+XfFT/ BHVxbME+DoHRxxR925lFNvpvEZIKor5+AZMrBAIuq11I50hsWzVLWBYgX725YuHFWN1i qCNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=r1Qh4gE4DbqOrbIqBfre0QTpHleSoAg1o2vnTnNlp9s=; fh=9VWe9Udi5I12a2YQoaN45bLqCoowKkt9+ocRRdoSrXc=; b=tJ5rq9e6khsb+3GbgtHFdGpZH6yXOSon8g/T6xKXtWUO4SW6BwSjwmek6QfPCcjVSh arNs9aVEfMS48F4EexcoWP/S+1R/1FvTPe7ElAvPQ2hzTuAvnkP7UQMr77W88UmJVjCq MUqdQQbv32zo4xqh4gyPmaDqNboh3SkHjSnz4H0TO4kotDdvNmLDoe7NvBGI6BuPBcK5 8HX5RWG2qwzUiAIKYFns0X5j4DQIBeYn4iT4mPs7nRrcysZPlpl9pV/TgQtmagt7HiGg +l0dloMI1ZgC2CoAejIfB/XMb/cBOeBlDiLRNR7yxOuNjjLv4W8tDQTxRKEIlVVN4l5k 41Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SWGLExA3; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w23-20020a63fb57000000b0055397736c02si20454461pgj.842.2023.07.04.02.35.22; Tue, 04 Jul 2023 02:35:36 -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=@intel.com header.s=Intel header.b=SWGLExA3; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231444AbjGDIzP (ORCPT + 99 others); Tue, 4 Jul 2023 04:55:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230477AbjGDIzN (ORCPT ); Tue, 4 Jul 2023 04:55:13 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1FD0115; Tue, 4 Jul 2023 01:55:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1688460913; x=1719996913; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=/i2oSBocuVBzuMPrP1+dQUATQqxsM/cZlogRxGtMF7g=; b=SWGLExA39sviPVKSZqHYSVaVe1kOS/nGGX4JLsHl3CxiHBPE4UmKpX6O HRIvStzSvTkxc3SFafUphy+FG0OK9JniGQtFeri5p9xzQQSwibPHLJK/P EZAmN2Sq0x9fKbzNomMPzecDBdp2JNMeoJrmuCcXxzoJtHDH+plS55UVz M1SaT+ws72f2/8Z7ZQbLg1gZZN4l8KJ2/na3dUiHSG/3dkGxFrdv99PB+ 8lKeaDH/X31Ai4JNh/mNPsDvyKs4V8ZGWZp098C+TlS5PStO2Hxo9vj2w WlCwhXFgJ1x9VvPeIXv4ANyJftfHE/OqmjHfEbVLEK/aGGVmfNine4Djw g==; X-IronPort-AV: E=McAfee;i="6600,9927,10760"; a="365645432" X-IronPort-AV: E=Sophos;i="6.01,180,1684825200"; d="scan'208";a="365645432" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jul 2023 01:55:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10760"; a="808862255" X-IronPort-AV: E=Sophos;i="6.01,180,1684825200"; d="scan'208";a="808862255" Received: from guoqingz-mobl1.ger.corp.intel.com (HELO [10.252.42.140]) ([10.252.42.140]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jul 2023 01:55:06 -0700 Message-ID: <2901d37a-6b5a-9076-1423-0db95b4c12d3@linux.intel.com> Date: Tue, 4 Jul 2023 11:55:03 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH 2/2] selftests/bpf: Add test for bpf_rdtsc Content-Language: en-US To: John Fastabend , shuah@kernel.org, tglx@linutronix.de, x86@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, mingo@redhat.com Cc: ast@kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, andrii@kernel.org, daniel@iogearbox.net, bpf@vger.kernel.org References: <20230703105745.1314475-1-tero.kristo@linux.intel.com> <20230703105745.1314475-3-tero.kristo@linux.intel.com> <64a3450a2a062_65205208a9@john.notmuch> From: Tero Kristo In-Reply-To: <64a3450a2a062_65205208a9@john.notmuch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On 04/07/2023 01:00, John Fastabend wrote: > Tero Kristo wrote: >> Add selftest for bpf_rdtsc() which reads the TSC (Time Stamp Counter) on >> x86_64 architectures. The test reads the TSC from both userspace and the >> BPF program, and verifies the TSC values are in incremental order as >> expected. The test is automatically skipped on architectures that do not >> support the feature. >> >> Signed-off-by: Tero Kristo >> --- >> .../selftests/bpf/prog_tests/test_rdtsc.c | 67 +++++++++++++++++++ >> .../testing/selftests/bpf/progs/test_rdtsc.c | 21 ++++++ >> 2 files changed, 88 insertions(+) >> create mode 100644 tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> create mode 100644 tools/testing/selftests/bpf/progs/test_rdtsc.c >> >> diff --git a/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> new file mode 100644 >> index 000000000000..2b26deb5b35a >> --- /dev/null >> +++ b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> @@ -0,0 +1,67 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* Copyright(c) 2023 Intel Corporation */ >> + >> +#include "test_progs.h" >> +#include "test_rdtsc.skel.h" >> + >> +#ifdef __x86_64__ >> + >> +static inline u64 _rdtsc(void) >> +{ >> + u32 low, high; >> + >> + __asm__ __volatile__("rdtscp" : "=a" (low), "=d" (high)); > I think its ok but note this could fail if user doesn't have > access to rdtscp and iirc that can be restricted? It is possible to restrict RDTSC access from userspace by enabling the TSD bit in CR4 register, and it will cause the userspace process to trap with general protection fault. However, the usage of RDTSC appears to be built-in to C standard libraries (probably some timer routines) and enabling the CR4 TSD makes the system near unusable. Things like sshd + systemd also start generating the same general protection faults if RDTSC is blocked. Also, attempting to run anything at all with the BPF selftest suite causes the same general protection fault; not only the rdtsc test. I tried this with couple of setups, one system running a minimalistic buildroot and another one running a fedora37 installation and the results were similar. -Tero > >> + return ((u64)high << 32) | low; >> +}