From: Sean Christopherson <seanjc@google.com>
To: David Matlack <dmatlack@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.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: Re: [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters
Date: Wed, 28 Sep 2022 22:51:31 +0000 [thread overview]
Message-ID: <YzTP8xBkBkxzB1gn@google.com> (raw)
In-Reply-To: <20220928184853.1681781-3-dmatlack@google.com>
On Wed, Sep 28, 2022, David Matlack wrote:
> @@ -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");
Any particular reason for using fopen()? Oh, because that's what the existing
code does. More below.
> + TEST_ASSERT(f, "fopen(%s) failed", path);
I don't actually care myself, but for consistency this should probably be a
skip condition. The easiest thing would be to use open_path_or_exit().
At that point, assuming read() instead of fread() does the right thin, that seems
like the easiest solution.
> + TEST_FAIL("Unrecognized value: %c", value);
Maybe be slightly more verbose? E.g.
TEST_FAIL("Unrecognized value '%c' for boolean module param", 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");
Since there are only three possible modules, what about providing wrappers to
handle "kvm", "kvm_amd", and "kvm_intel"? I'm guessing we'll end up with wrappers
for each param we care about, but one fewer strings to get right would be nice.
next prev parent reply other threads:[~2022-09-28 22:51 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 ` [PATCH v2 2/3] KVM: selftests: Add helper to read boolean module parameters David Matlack
2022-09-28 22:51 ` Sean Christopherson [this message]
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=YzTP8xBkBkxzB1gn@google.com \
--to=seanjc@google.com \
--cc=bgardon@google.com \
--cc=dmatlack@google.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.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