From nobody Sat Nov 30 07:41:02 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FF7D1885BE for ; Wed, 11 Sep 2024 09:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726047079; cv=none; b=ZcMcHbTqRW3Zz7kHKAUpNo20XSEc/blcCBYy8gImAv8NKb5sI5GC+q6yLvYt+ON5nBJkceDZ/TVrLu7aTNgn6/d12MFgvbdBGbq+/iFsbI6+bnBRjhNDc54aVfUc0sLj1lTbeIVkSaFrihn07FC5MezEbuI+Kpfw8qRJpwdvtqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726047079; c=relaxed/simple; bh=nHH6U/9NK1NGegXj6WlTrXArnD2Ybi4HhEFmOD+lSEk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RHgbCQR1I/LLVrkWqKVddg0EL2VELA8cgMAXY/92+RtES8idimhq29WzwFRlI9v3hAJzSf1s/FpavDOrv3xzKDav6LL9IVo10mTaFc8Ih0DHfsvTlSkkZt83JVLL1KHhSx/YGz0UGJ6JNuWybYnZRL7KCOVt/vUHVDqW9lFJpVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=B/OXiipU; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="B/OXiipU" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-374cd315c68so3810176f8f.0 for ; Wed, 11 Sep 2024 02:31:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726047075; x=1726651875; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R0ql8FhOpfg6VpbbTG/ZwJ1DV871rVa0x5nxiQMVov8=; b=B/OXiipUPAKeEVrvcICnuN3go+I6xAYnwGhOrpSLXuKVilgW5kxhGHdx8NX/68FwkN 0gEYtqK7JkYyZ786r5gEyUjBGjZvpOQXaEprBQqZ69nbGzip6ogLjb6WXeAb45+9YNRd hnGvYdI6WCJL5l2EEWUU0QjmBg9dLX+/AYz/2PsRK1f9j3CX4+lzIvJ21ND7ERKa8xOs PvkGjfMZhpfwvJjtoDx/79OzUUe/in3az2NPaA8T01Bu+3fVR84+h0GPY8FZ9c3CEzw5 X1iUGreDPQxx/KnxL7BaavYxMt1mrCBKVGNVqGorSeANN+/ZgkSFT4487gu4DzhoZMHO odAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726047075; x=1726651875; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R0ql8FhOpfg6VpbbTG/ZwJ1DV871rVa0x5nxiQMVov8=; b=PGynvnXAuCz8onufKA0WlJeoBI4JAampXwqKdyXvMgKxdSntiVhdRHC6Id40vaa4nO b9OHT49DMeSQhi7X0iBOyOp+/4VjJpXAHq1xJNiX/maUFuyUyqUlbJSjAvyAsqR29w8t omzPS5oXQ9jTW2zWHOPcCtguubjw/zWXESnL7hatRQN4uwij3sAlrQs2RlRZ/tnMKAu9 RFuJV3WO+fjcSTiQZOiSzGsaUv2AXM6Jz5gCNIZUeR6bDbSNXnGWo+JVV5WbFyznp7E8 6/LJqMNTqVxTBoJW8hOxU3LJ/mCbSKZORyzDiURAuDmgYIfX3vXerc2WS0VqdLxDMuqA 1dIw== X-Forwarded-Encrypted: i=1; AJvYcCXY+CYeIP8rHjNIS+m2Za8zOj6JtKrnfLYaTYXdkGjjR2+dAzyYBzm2Y70L587ob18pB59sFNkxSm/P93E=@vger.kernel.org X-Gm-Message-State: AOJu0Yza9VIApjGYQzbK4PZNjjNpfTUgkbTi6UyDjEEvhljhhnC8HTgg qQ7A/BNnVxwcJd89mHgcS0BZNPAov6c3JvhFlYwv/rygv9NoRQyOEYhyVJQNPcFAZOxfk0SSU/L XICpnWbfS9P6xsHN3yA== X-Google-Smtp-Source: AGHT+IHT7JcnsHOxG+44WSeWAc4200KmqhzTuuadsLCqKhH/XiIelY9/9nsYaSZQ0k9teOOJUozUOlVOw5AEwKK6 X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:adf:ec8c:0:b0:374:bf86:57a2 with SMTP id ffacd0b85a97d-378896eab80mr20744f8f.11.1726047074846; Wed, 11 Sep 2024 02:31:14 -0700 (PDT) Date: Wed, 11 Sep 2024 10:30:29 +0100 In-Reply-To: <20240911093029.3279154-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240911093029.3279154-1-vdonnefort@google.com> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: <20240911093029.3279154-14-vdonnefort@google.com> Subject: [PATCH 13/13] KVM: arm64: Add kselftest for tracefs hyp tracefs From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, maz@kernel.org, oliver.upton@linux.dev Cc: kvmarm@lists.linux.dev, will@kernel.org, qperret@google.com, kernel-team@android.com, linux-kernel@vger.kernel.org, Vincent Donnefort Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test to validate the newly introduced tracefs interface for the pKVM hypervisor. This test covers the usage of extended timestamp and coherence of the tracing clock. Signed-off-by: Vincent Donnefort diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_= asm.h index 3710deb6eaa0..be7d4d2434e7 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -86,6 +86,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_reset_tracing, __KVM_HOST_SMCCC_FUNC___pkvm_swap_reader_tracing, __KVM_HOST_SMCCC_FUNC___pkvm_enable_event, + __KVM_HOST_SMCCC_FUNC___pkvm_selftest_event, }; =20 #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] diff --git a/arch/arm64/include/asm/kvm_hypevents.h b/arch/arm64/include/as= m/kvm_hypevents.h index 0b98a87a1250..1c797b748ff2 100644 --- a/arch/arm64/include/asm/kvm_hypevents.h +++ b/arch/arm64/include/asm/kvm_hypevents.h @@ -28,4 +28,14 @@ HYP_EVENT(hyp_exit, ), HE_PRINTK(" ") ); + +#ifdef CONFIG_PROTECTED_NVHE_TESTING +HYP_EVENT(selftest, + HE_PROTO(void), + HE_STRUCT(), + HE_ASSIGN(), + HE_PRINTK(" ") +); #endif + +#endif /* __ARM64_KVM_HYPEVENTS_H_ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 8304eb342be9..c7ae07a88875 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -66,4 +66,13 @@ config PROTECTED_NVHE_STACKTRACE =20 If unsure, or not using protected nVHE (pKVM), say N. =20 +config PROTECTED_NVHE_TESTING + bool "Protected KVM hypervisor testing infrastructure" + depends on KVM + default n + help + Say Y here to enable pKVM hypervisor testing infrastructure. + + If unsure, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/h= yp-main.c index f9983d4a8d4c..2c040585fdd2 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -431,6 +431,19 @@ static void handle___pkvm_enable_event(struct kvm_cpu_= context *host_ctxt) cpu_reg(host_ctxt, 1) =3D __pkvm_enable_event(id, enable); } =20 +static void handle___pkvm_selftest_event(struct kvm_cpu_context *host_ctxt) +{ + int smc_ret =3D SMCCC_RET_NOT_SUPPORTED, ret =3D -EOPNOTSUPP; + +#ifdef CONFIG_PROTECTED_NVHE_TESTING + trace_selftest(); + smc_ret =3D SMCCC_RET_SUCCESS; + ret =3D 0; +#endif + cpu_reg(host_ctxt, 0) =3D smc_ret; + cpu_reg(host_ctxt, 1) =3D ret; +} + typedef void (*hcall_t)(struct kvm_cpu_context *); =20 #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] =3D (hcall_t)handle_##x @@ -470,6 +483,7 @@ static const hcall_t host_hcall[] =3D { HANDLE_FUNC(__pkvm_reset_tracing), HANDLE_FUNC(__pkvm_swap_reader_tracing), HANDLE_FUNC(__pkvm_enable_event), + HANDLE_FUNC(__pkvm_selftest_event), }; =20 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c index 292f7abc23f4..356ce3042936 100644 --- a/arch/arm64/kvm/hyp_trace.c +++ b/arch/arm64/kvm/hyp_trace.c @@ -887,6 +887,36 @@ static int hyp_trace_clock_show(struct seq_file *m, vo= id *v) } DEFINE_SHOW_ATTRIBUTE(hyp_trace_clock); =20 +#ifdef CONFIG_PROTECTED_NVHE_TESTING +static int selftest_event_open(struct inode *inode, struct file *file) +{ + if (file->f_mode & FMODE_WRITE) + return kvm_call_hyp_nvhe(__pkvm_selftest_event); + + return 0; +} + +static ssize_t selftest_event_write(struct file *f, const char __user *buf, + size_t cnt, loff_t *pos) +{ + return cnt; +} + +static const struct file_operations selftest_event_fops =3D { + .open =3D selftest_event_open, + .write =3D selftest_event_write, + .llseek =3D no_llseek, +}; + +static void hyp_trace_init_testing_tracefs(struct dentry *root) +{ + tracefs_create_file("selftest_event", TRACEFS_MODE_WRITE, root, NULL, + &selftest_event_fops); +} +#else +static void hyp_trace_init_testing_tracefs(struct dentry *root) { } +#endif + int hyp_trace_init_tracefs(void) { struct dentry *root, *per_cpu_root; @@ -945,6 +975,7 @@ int hyp_trace_init_tracefs(void) } =20 hyp_trace_init_event_tracefs(root); + hyp_trace_init_testing_tracefs(root); =20 return 0; } diff --git a/tools/testing/selftests/hyp-trace/Makefile b/tools/testing/sel= ftests/hyp-trace/Makefile new file mode 100644 index 000000000000..2a5b2e29667e --- /dev/null +++ b/tools/testing/selftests/hyp-trace/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +all: + +TEST_PROGS :=3D hyp-trace-test + +include ../lib.mk diff --git a/tools/testing/selftests/hyp-trace/config b/tools/testing/selft= ests/hyp-trace/config new file mode 100644 index 000000000000..39cee8ec30fa --- /dev/null +++ b/tools/testing/selftests/hyp-trace/config @@ -0,0 +1,4 @@ +CONFIG_FTRACE=3Dy +CONFIG_ARM64=3Dy +CONFIG_KVM=3Dy +CONFIG_PROTECTED_NVHE_TESTING=3Dy diff --git a/tools/testing/selftests/hyp-trace/hyp-trace-test b/tools/testi= ng/selftests/hyp-trace/hyp-trace-test new file mode 100755 index 000000000000..868eb81bfb77 --- /dev/null +++ b/tools/testing/selftests/hyp-trace/hyp-trace-test @@ -0,0 +1,254 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0-only + +# hyp-trace-test - Tracefs for pKVM hypervisor test +# +# Copyright (C) 2024 - Google LLC +# Author: Vincent Donnefort +# + +log_and_die() +{ + echo "$1" + + exit 1 +} + +host_clock() +{ + # BOOTTIME clock + awk '/now/ { printf "%.6f\n", $3 / 1000000000 }' /proc/timer_list +} + +page_size() +{ + echo "$(awk '/KernelPageSize/ {print $2; exit}' /proc/self/smaps) * 10= 24" | bc +} + +goto_hyp_trace() +{ + if [ -d "/sys/kernel/debug/tracing/hypervisor" ]; then + cd /sys/kernel/debug/tracing/hypervisor + return + fi + + if [ -d "/sys/kernel/tracing/hypervisor" ]; then + cd /sys/kernel/tracing/hypervisor + return + fi + + echo "ERROR: hyp tracing folder not found!" + + exit 1 +} + +reset_hyp_trace() +{ + echo 0 > tracing_on + echo 0 > trace + for event in events/hypervisor/*; do + echo 0 > $event/enable + done +} + +setup_hyp_trace() +{ + reset_hyp_trace + + echo 16 > buffer_size_kb + echo 1 > events/hypervisor/selftest/enable + echo 1 > tracing_on +} + +stop_hyp_trace() +{ + echo 0 > tracing_on +} + +hyp_trace_loaded() +{ + grep -q "(loaded)" buffer_size_kb +} + +write_events() +{ + local num=3D"$1" + local func=3D"$2" + + for i in $(seq 1 $num); do + echo 1 > selftest_event + [ -z "$func" -o $i -eq $num ] || eval $func + done +} + +consuming_read() +{ + local output=3D$1 + + cat trace_pipe > $output & + + echo $! +} + +run_test_consuming() +{ + local nr_events=3D$1 + local func=3D$2 + local tmp=3D"$(mktemp)" + local start_ts=3D0 + local end_ts=3D0 + local pid=3D0 + + echo "Output trace file: $tmp" + + setup_hyp_trace + pid=3D$(consuming_read $tmp) + + start_ts=3D$(host_clock) + write_events $nr_events $func + stop_hyp_trace + end_ts=3D$(host_clock) + + kill $pid + validate_test $tmp $nr_events $start_ts $end_ts + + rm $tmp +} + +validate_test() +{ + local output=3D$1 + local expected_events=3D$2 + local start_ts=3D$3 + local end_ts=3D$4 + local prev_ts=3D$3 + local ts=3D0 + local num_events=3D0 + + IFS=3D$'\n' + for line in $(cat $output); do + echo "$line" | grep -q -E "^# " && continue + ts=3D$(echo "$line" | awk '{print $2}' | cut -d ':' -f1) + if [ $(echo "$ts<$prev_ts" | bc) -eq 1 ]; then + log_and_die "Error event @$ts < $prev_ts" + fi + prev_ts=3D$ts + num_events=3D$((num_events + 1)) + done + + if [ $(echo "$ts>$end_ts" | bc) -eq 1 ]; then + log_and_die "Error event @$ts > $end_ts" + fi + + if [ $num_events -ne $expected_events ]; then + log_and_die "Expected $expected_events events, got $num_events" + fi +} + +test_ts() +{ + echo "Test Timestamps..." + + run_test_consuming 1000 + + echo "done." +} + +test_extended_ts() +{ + echo "Test Extended Timestamps..." + + run_test_consuming 1000 "sleep 0.1" + + echo "done." +} + +assert_loaded() +{ + hyp_trace_loaded || log_and_die "Expected loaded buffer" +} + +assert_unloaded() +{ + ! hyp_trace_loaded || log_and_die "Expected unloaded buffer" +} + +test_unloading() +{ + local tmp=3D"$(mktemp)" + + echo "Test unloading..." + + setup_hyp_trace + assert_loaded + + echo 0 > tracing_on + assert_unloaded + + pid=3D$(consuming_read $tmp) + sleep 1 + assert_loaded + kill $pid + assert_unloaded + + echo 1 > tracing_on + write_events 1 + echo 0 > trace + assert_loaded + echo 0 > tracing_on + assert_unloaded + + echo "done." +} + +test_reset() +{ + local tmp=3D"$(mktemp)" + + echo "Test Reset..." + + setup_hyp_trace + write_events 5 + echo 0 > trace + write_events 5 + + pid=3D$(consuming_read $tmp) + sleep 1 + stop_hyp_trace + kill $pid + + validate_test $tmp 5 0 $(host_clock) + + rm $tmp + + echo "done." +} + +test_big_bpacking() +{ + local hyp_buffer_page_size=3D48 + local page_size=3D$(page_size) + local min_buf_size=3D$(echo "$page_size * $page_size / ($hyp_buffer_pa= ge_size * $(nproc))" | bc) + + min_buf_size=3D$(echo "$min_buf_size * 2 / 1024" | bc) + + echo "Test loading $min_buf_size kB buffer..." + + reset_hyp_trace + echo $min_buf_size > buffer_size_kb + echo 1 > tracing_on + + stop_hyp_trace + + echo "done." +} + +goto_hyp_trace + +test_reset +test_unloading +test_big_bpacking +test_ts +test_extended_ts + +exit 0 --=20 2.46.0.598.g6f2099f65c-goog