From d9c66eb76971ea54849c6a2166e1aad91d6f6050 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Tue, 19 Jul 2022 17:18:00 -0700 Subject: [PATCH] cpuid: cache host cpuid features HostFeatureSet() returns a fixed set that is initialized at startup. Here results fro the signal_benchmark on the kvm platform: Before: BM_FaultSignalFixup/real_time 8353 ns 8383 ns 94234 After: BM_FaultSignalFixup/real_time 6127 ns 6087 ns 111708 PiperOrigin-RevId: 462012597 --- pkg/cpuid/native_amd64.go | 17 +++++++++++++---- pkg/sentry/platform/kvm/kvm_amd64_test.go | 4 +++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/cpuid/native_amd64.go b/pkg/cpuid/native_amd64.go index b0102feee..d7cd60b29 100644 --- a/pkg/cpuid/native_amd64.go +++ b/pkg/cpuid/native_amd64.go @@ -162,13 +162,13 @@ func (fs FeatureSet) query(fn cpuidFunction) (uint32, uint32, uint32, uint32) { return out.Eax, out.Ebx, out.Ecx, out.Edx } +var hostFeatureSet FeatureSet + // HostFeatureSet returns a host CPUID. // //go:nosplit func HostFeatureSet() FeatureSet { - return FeatureSet{ - Function: &Native{}, - } + return hostFeatureSet } var ( @@ -179,7 +179,7 @@ var ( // Reads max cpu frequency from host /proc/cpuinfo. Must run before syscall // filter installation. This value is used to create the fake /proc/cpuinfo // from a FeatureSet. -func init() { +func readMaxCPUFreq() { cpuinfob, err := ioutil.ReadFile("/proc/cpuinfo") if err != nil { // Leave it as 0... the VDSO bails out in the same way. @@ -212,4 +212,13 @@ func init() { } } log.Warningf("Could not parse /proc/cpuinfo, it is empty or does not contain cpu MHz") + +} + +func init() { + hostFeatureSet = FeatureSet{ + Function: &Native{}, + }.Fixed() + + readMaxCPUFreq() } diff --git a/pkg/sentry/platform/kvm/kvm_amd64_test.go b/pkg/sentry/platform/kvm/kvm_amd64_test.go index 163e0d58e..8c1df11f3 100644 --- a/pkg/sentry/platform/kvm/kvm_amd64_test.go +++ b/pkg/sentry/platform/kvm/kvm_amd64_test.go @@ -99,7 +99,9 @@ func nestedVirtIsOn(c *vCPU, fs *cpuid.FeatureSet) bool { func TestKernelCPUID(t *testing.T) { bluepillTest(t, func(c *vCPU) { - fs := cpuid.HostFeatureSet() + fs := cpuid.FeatureSet{ + Function: &cpuid.Native{}, + } if nestedVirtIsOn(c, &fs) { t.Fatalf("Nested virtualization is enabled") }