Add a common helper function for reading boolean module parameters and use it in vm_is_unrestricted_guest() to check the value of kvm_intel.unrestricted_guest. Note that vm_is_unrestricted_guest() will now fail with a TEST_ASSERT() if called on AMD instead of just returning false. However this should not cause any functional change since all of the callers of vm_is_unrestricted_guest() first check is_intel_cpu(). No functional change intended. Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx> --- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 31 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 13 +------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index befc754ce9b3..4f119fd84ae5 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -108,6 +108,7 @@ struct vm_mem_backing_src_alias { #define MIN_RUN_DELAY_NS 200000UL +bool get_module_param_bool(const char *module_name, const char *param); bool thp_configured(void); size_t get_trans_hugepagesz(void); size_t get_def_hugetlb_pagesz(void); diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index 6d23878bbfe1..479e482d3202 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -9,6 +9,7 @@ #include <ctype.h> #include <limits.h> #include <stdlib.h> +#include <stdio.h> #include <time.h> #include <sys/stat.h> #include <sys/syscall.h> @@ -114,6 +115,36 @@ void print_skip(const char *fmt, ...) puts(", skipping test"); } +bool get_module_param_bool(const char *module_name, const char *param) +{ + const int path_size = 1024; + char path[path_size]; + char value; + FILE *f; + int r; + + r = snprintf(path, path_size, "/sys/module/%s/parameters/%s", + module_name, param); + TEST_ASSERT(r < path_size, + "Failed to construct sysfs path in %d bytes.", path_size); + + f = fopen(path, "r"); + TEST_ASSERT(f, "fopen(%s) failed", path); + + r = fread(&value, 1, 1, f); + TEST_ASSERT(r == 1, "fread(%s) failed", path); + + r = fclose(f); + TEST_ASSERT(!r, "fclose(%s) failed", path); + + if (value == 'Y') + return true; + else if (value == 'N') + return false; + + TEST_FAIL("Unrecognized value: %c", value); +} + bool thp_configured(void) { int ret; diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 2e6e61bbe81b..522d3e2009fb 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1294,20 +1294,9 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm) /* Returns true if kvm_intel was loaded with unrestricted_guest=1. */ bool vm_is_unrestricted_guest(struct kvm_vm *vm) { - char val = 'N'; - size_t count; - FILE *f; - /* Ensure that a KVM vendor-specific module is loaded. */ if (vm == NULL) close(open_kvm_dev_path_or_exit()); - f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r"); - if (f) { - count = fread(&val, sizeof(char), 1, f); - TEST_ASSERT(count == 1, "Unable to read from param file."); - fclose(f); - } - - return val == 'Y'; + return get_module_param_bool("kvm_intel", "unrestricted_guest"); } -- 2.37.3.998.g577e59143f-goog