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
This commit is contained in:
Andrei Vagin
2022-07-19 17:20:14 -07:00
committed by gVisor bot
parent dc7675bd6c
commit d9c66eb769
2 changed files with 16 additions and 5 deletions
+13 -4
View File
@@ -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()
}
+3 -1
View File
@@ -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")
}