public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: David Matlack <dmatlack@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>,
	David Matlack <dmatlack@google.com>,
	Ben Gardon <bgardon@google.com>,
	Jim Mattson <jmattson@google.com>, Peter Xu <peterx@redhat.com>,
	Yang Zhong <yang.zhong@intel.com>,
	Wei Wang <wei.w.wang@intel.com>,
	kvm@vger.kernel.org
Subject: [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters
Date: Wed, 28 Sep 2022 11:48:52 -0700	[thread overview]
Message-ID: <20220928184853.1681781-3-dmatlack@google.com> (raw)
In-Reply-To: <20220928184853.1681781-1-dmatlack@google.com>

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@google.com>
---
 .../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


  parent reply	other threads:[~2022-09-28 18:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-28 18:48 [PATCH v2 0/3] KVM: selftests: Fix nx_huge_pages_test when TDP is disabled David Matlack
2022-09-28 18:48 ` [PATCH v2 1/3] KVM: selftests: Tell the compiler that code after TEST_FAIL() is unreachable David Matlack
2022-09-28 18:48 ` David Matlack [this message]
2022-09-28 22:51   ` [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters Sean Christopherson
2022-09-29 16:18     ` David Matlack
2022-09-28 18:48 ` [PATCH v2 3/3] KVM: selftests: Fix nx_huge_pages_test on TDP-disabled hosts David Matlack
2022-09-28 22:55   ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220928184853.1681781-3-dmatlack@google.com \
    --to=dmatlack@google.com \
    --cc=bgardon@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=seanjc@google.com \
    --cc=wei.w.wang@intel.com \
    --cc=yang.zhong@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox