
On 12/15/20 5:24 PM, Tim Wiederhake wrote:
Fixes the leaking file descriptors. Does not silently ignore errors (e.g. permission denied on /dev/cpu/0/msr if run as non-root) and always attempt to read from /dev/kvm if /dev/cpu/0/msr failed.
'gather_msr()' returns a dictionary of values, as a later patch will add more registers to be interrogated.
Signed-off-by: Tim Wiederhake <twiederh@redhat.com> --- tests/cputestdata/cpu-gather.py | 34 ++++++++++++++++++++++++++++ tests/cputestdata/cpu-gather.sh | 40 --------------------------------- 2 files changed, 34 insertions(+), 40 deletions(-)
diff --git a/tests/cputestdata/cpu-gather.py b/tests/cputestdata/cpu-gather.py index 75cf290a28..db2348b460 100755 --- a/tests/cputestdata/cpu-gather.py +++ b/tests/cputestdata/cpu-gather.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3
import argparse +import fcntl import os +import struct import subprocess +import sys
def gather_name(args): @@ -38,6 +41,31 @@ def gather_cpuid_leaves(args): yield line.strip()
+def gather_msr(): + IA32_ARCH_CAPABILITIES_MSR = 0x10a + KVM_GET_MSRS = 0xc008ae88 + + try: + with open("/dev/cpu/0/msr", "rb") as f: + f.seek(IA32_ARCH_CAPABILITIES_MSR) + buf = f.read(8) + msr = struct.unpack("=Q", buf)[0] + return "", {IA32_ARCH_CAPABILITIES_MSR: msr} + except IOError as e: + print("Warning: {}".format(e), file=sys.stderr)
Previously, if /dev/cpu/0/msr was not present, then no warning was written. Now I get this warning, fortunately on stderr so cpu-parse.sh is not affected. I'm not saying it's a bad thing, these scripts are expected to be called by advanced users (if not developers) and this warning may shed more light. Just want to point out it was noticed ;-)
+ + try: + bufIn = struct.pack("=LLLLQ", 1, 0, IA32_ARCH_CAPABILITIES_MSR, 0, 0) + with open("/dev/kvm", "rb") as f: + bufOut = fcntl.ioctl(f, KVM_GET_MSRS, bufIn) + msr = struct.unpack("=LLLLQ", bufOut)[4] + return " via KVM", {IA32_ARCH_CAPABILITIES_MSR: msr} + except IOError as e: + print("Warning: {}".format(e), file=sys.stderr) + + return None, {} + +
Michal