Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp26020345rwd; Mon, 3 Jul 2023 04:31:13 -0700 (PDT) X-Google-Smtp-Source: APBJJlHnd9cSYQ0o9TAFRNQuNlmZsusw2ZDEqjzZryUi1cfMiiZ80xma7slun+x8U6bTxyl5uNQE X-Received: by 2002:a05:6871:91:b0:19f:202:4fb9 with SMTP id u17-20020a056871009100b0019f02024fb9mr12527210oaa.38.1688383873318; Mon, 03 Jul 2023 04:31:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688383873; cv=none; d=google.com; s=arc-20160816; b=FH0zTJ4LX7Kinfr3+zHOhG1Xj91N9Xd6flbh339ZhTX5atsOIZBIeCRsjsDYSrlHn+ 5zi9WPlZ4JibCXQAjmBR+DOGofKGTz9lpA0LOqs2CA7I33gUrhHBCS/e4/uQV85x3qgB B2MKMvaNUi/CtzASRve0zTyPI05U5HMim8Ydlj/yejOlNtfML8EL7R/DZtFYfLjlE8iL s0KB21YtNLvgcyJ2ck/UMn4MW5CO39Ng3eYeQTcSkNM4Q2/8YNtrcr5V8sVd3SxaVIcv vwDk3WPOWcWkas/+YZ77pNAvOYT0b4m0NZQ8s0uzpyDVvsXx9YjetG2z2yJgxPCeiZqN P1rQ== 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=XGI1WRSEFP0+7UgIPESerMMAG67RWWA1FsTubv/5aE0=; fh=qzk3MR59rZYsNry+v+8Ij9bS5TF5Hu8/Fy/xf1iS2OM=; b=0/9wgygM+p6K4NvLbKYpstkMo4XWTm+OvtImLEVwn13YDPYmojwuJ9XjllaCugrxLo 1oWwV46tI6SJok35vqP4D6Gpx/Z9MdCY81kJEHMiwkvnMpnG0Xe35sVlQxNdwYzroCoT qKNhGyrgnIUhyhTus3uiET2BZIjL+TsrZqO50vbdZmtE9CZ48LfDfdYD6t4/NAy3ulT4 WS7ngupR8R6/Uc1/6gDkRswBsU9dLEpf5JxeZxIhvwh/ONzLRg8pbAbO1rKwTmKqpcn8 Yz+H6RBZ+xS8V2zXFap6KWJ512P8u84KB7RlzF+Vl3LhykRwOPUFRgzvw0tEKN9qVAiM t8oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fW1iZcWu; 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 q1-20020a17090a1b0100b0025339314a7bsi20103613pjq.79.2023.07.03.04.30.59; Mon, 03 Jul 2023 04:31:13 -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=fW1iZcWu; 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 S231477AbjGCK6q (ORCPT + 99 others); Mon, 3 Jul 2023 06:58:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230203AbjGCK6n (ORCPT ); Mon, 3 Jul 2023 06:58:43 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFBB6D2; Mon, 3 Jul 2023 03:58:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1688381922; x=1719917922; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GZZoC3iTyzrkENAoUyx8fll+6b0qjLWSGcIt+67G8aY=; b=fW1iZcWuyOmDiB/8SOMcQDjgOnV/B6ct4zUBiILa6IASr0RVj7lJ0qml qGOv/J3bpW7AbGYA7hzSKszzPQftRhnmcsANkcqD9I2urtghP7ovYIA7u wpArnjG0y5xWLbXQ92AWrHOTlQvCIZzCgg7Ve1IzW/UdJEj8oV6KIttLG 4vJ0OAJpNMuN+vH2BZLQlGwYclBbt7AokCw0pkmIvxntQIaazEjxYVIsk R5h09GKPy4r/6xyjiTPqj3Bukg85zAyA+VCXpj9qaAzq/R3ct6PGdsRJg F5ERYdM0clAnbDbUE67gOSIqb/s4BOvKrl9Q9TsxloaSo/guv2eQ2huDx A==; X-IronPort-AV: E=McAfee;i="6600,9927,10759"; a="360329731" X-IronPort-AV: E=Sophos;i="6.01,178,1684825200"; d="scan'208";a="360329731" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2023 03:58:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10759"; a="748108266" X-IronPort-AV: E=Sophos;i="6.01,178,1684825200"; d="scan'208";a="748108266" Received: from agrabezh-mobl1.ccr.corp.intel.com (HELO tkristo-desk.intel.com) ([10.252.48.27]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2023 03:58:30 -0700 From: Tero Kristo To: 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 Subject: [PATCH 2/2] selftests/bpf: Add test for bpf_rdtsc Date: Mon, 3 Jul 2023 13:57:45 +0300 Message-Id: <20230703105745.1314475-3-tero.kristo@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230703105745.1314475-1-tero.kristo@linux.intel.com> References: <20230703105745.1314475-1-tero.kristo@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,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 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)); + return ((u64)high << 32) | low; +} + +static int rdtsc(struct test_rdtsc *skel) +{ + int err, prog_fd; + u64 user_c1, user_c2; + + LIBBPF_OPTS(bpf_test_run_opts, topts); + + err = test_rdtsc__attach(skel); + if (!ASSERT_OK(err, "test_rdtsc_attach")) + return err; + + user_c1 = _rdtsc(); + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run_opts(prog_fd, &topts); + + user_c2 = _rdtsc(); + + ASSERT_OK(err, "test_run"); + ASSERT_EQ(topts.retval, 0, "test_run"); + + test_rdtsc__detach(skel); + + ASSERT_GE(skel->bss->c1, user_c1, "bpf c1 > user c1"); + ASSERT_GE(user_c2, skel->bss->c2, "user c2 > bpf c2"); + ASSERT_GE(skel->bss->c2, user_c1, "bpf c2 > bpf c1"); + ASSERT_GE(user_c2, user_c1, "user c2 > user c1"); + + return 0; +} +#endif + +void test_rdtsc(void) +{ +#ifdef __x86_64__ + struct test_rdtsc *skel; + int err; + + skel = test_rdtsc__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_rdtsc_skel_load")) + goto cleanup; + err = rdtsc(skel); + ASSERT_OK(err, "rdtsc"); + +cleanup: + test_rdtsc__destroy(skel); +#else + printf("%s:SKIP:bpf_rdtsc() kfunc not supported\n", __func__); + test__skip(); +#endif +} diff --git a/tools/testing/selftests/bpf/progs/test_rdtsc.c b/tools/testing/selftests/bpf/progs/test_rdtsc.c new file mode 100644 index 000000000000..14776b83bd3e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_rdtsc.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2023 Intel Corporation */ +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +__u64 c1; +__u64 c2; + +extern __u64 bpf_rdtsc(void) __ksym; + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG2(test1, int, a) +{ + c1 = bpf_rdtsc(); + c2 = bpf_rdtsc(); + + return 0; +} -- 2.25.1