Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Hemanth Selam <hemanth.selam@gmail.com>
Cc: pbonzini@redhat.com, shuah@kernel.org, kvm@vger.kernel.org,
	 linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] KVM: selftests: Add a test for KVM_CREATE_VM VM type enforcement
Date: Fri, 31 Jul 2026 15:04:15 -0700	[thread overview]
Message-ID: <am0b36OWeFSy2faX@google.com> (raw)
In-Reply-To: <20260710050442.826777-1-hemanth.selam@gmail.com>

A handful of nits, but no need for another version, I'll fixup when applying.

Thanks!

On Fri, Jul 10, 2026, Hemanth Selam wrote:
> diff --git a/tools/testing/selftests/kvm/vm_types_test.c b/tools/testing/selftests/kvm/vm_types_test.c
> new file mode 100644
> index 000000000000..e8ef3b018f6c
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/vm_types_test.c
> @@ -0,0 +1,50 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Verify that KVM_CREATE_VM accepts exactly the VM types enumerated by
> + * KVM_CAP_VM_TYPES, and rejects every other type with -EINVAL.
> + */
> +#include <errno.h>
> +#include <stdbool.h>
> +#include <unistd.h>
> +
> +#include <linux/kvm.h>
> +
> +#include "kvm_util.h"
> +#include "test_util.h"
> +
> +int main(void)
> +{
> +	unsigned int supported_types;
> +	unsigned long type;
> +	int kvm_fd, fd;

Probably worth declaring "fd" inside the for-loop.   Definitely a coin toss on
which is "better".

> +	TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TYPES));
> +
> +	kvm_fd = open_kvm_dev_path_or_exit();
> +	supported_types = kvm_check_cap(KVM_CAP_VM_TYPES);
> +	pr_info("KVM_CAP_VM_TYPES: 0x%x\n", supported_types);

Rather than "KVM_CAP_VM_TYPES" print a more human friendly message, e.g.
"Supported VM Types: ", otherwise it's not entirely obvious that the message is
printing the result of KVM_CAP_VM_TYPES version the raw macro number.

> +
> +	/*
> +	 * KVM_CAP_VM_TYPES is a u32 bitmap, so only types 0..31 can ever be
> +	 * advertised.  Walk past that range as well to confirm that any
> +	 * out-of-range type is rejected rather than silently accepted.

Rather than lean on KVM internals, I think it makes sense to express this limitation
in terms of KVM_CHECK_EXTENSION's return values.  Which is still kinda sorta an
internal detail, but it's at least more visible to userspace.

Specifically, track supported_types as an "unsigned long", and then have the
comment talk about KVM's deliberately retristed return value, not how KVM tracks
its supported types internally.  As a bonus, the loop can iterate on the bits per
supported_types, not a hardcoded "64".

	/*
	 * For compatibility with 32-bit kernels, KVM_CHECK_EXTENSION restricts
	 * its return to 32-bit values, i.e. only types 0..31 can be advertised.
	 * Walk past that range as well to confirm that any out-of-range type is
	 * rejected rather than silently accepted (or truncated).
	 */
	for (type = 0; type < BITS_PER_TYPE(supported_types); type++) {

> +	 */
> +	for (type = 0; type < 64; type++) {
> +		bool supported = type < 32 && (supported_types & (1U << type));

And then if supported_types is an "unsigned long", the "type < 32" goes away.
A bonus to _that_ is that the test will Just Work if future KVM does enumerate
support for types > 31.

The bitwise-AND can also use BIT().

> +
> +		fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)type);
> +
> +		if (supported) {
> +			TEST_ASSERT(fd >= 0,
> +				    "KVM_CREATE_VM(%lu) should succeed, KVM_CAP_VM_TYPES=0x%x",
> +				    type, supported_types);
> +			close(fd);

kvm_close()

> +		} else {
> +			TEST_ASSERT(fd < 0 && errno == EINVAL,
> +				    "KVM_CREATE_VM(%lu) should fail with EINVAL, KVM_CAP_VM_TYPES=0x%x",
> +				    type, supported_types);
> +		}
> +	}
> +
> +	return 0;
> +}
> diff --git a/tools/testing/selftests/kvm/x86/sev_init2_tests.c b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
> index 8db88c355f16..d4227dc922ab 100644
> --- a/tools/testing/selftests/kvm/x86/sev_init2_tests.c
> +++ b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
> @@ -77,10 +77,6 @@ void test_vm_types(void)
>  {
>  	test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){});
>  
> -	/*
> -	 * TODO: check that unsupported types cannot be created.  Probably
> -	 * a separate selftest.
> -	 */
>  	if (have_sev_es)
>  		test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){});
>  
> -- 
> 2.43.7
> 

      reply	other threads:[~2026-07-31 22:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-09 16:47 [PATCH v1 0/2] KVM: selftests: SEV: address two TODOs in the SEV tests Hemanth Selam
2026-07-09 16:47 ` [PATCH v1 1/2] KVM: selftests: SEV: Verify unsupported VM types are rejected at creation Hemanth Selam
2026-07-09 16:55   ` sashiko-bot
2026-07-09 16:47 ` [PATCH v1 2/2] KVM: selftests: SEV: Sanity check the launch measurement Hemanth Selam
2026-07-09 17:49 ` [PATCH v2 0/2] KVM: selftests: SEV: address two TODOs in the SEV tests Hemanth Selam
2026-07-09 17:49   ` [PATCH v2 1/2] KVM: selftests: SEV: Verify unsupported VM types are rejected at creation Hemanth Selam
2026-07-09 20:21     ` Sean Christopherson
2026-07-10  5:06     ` Hemanth Selam
2026-07-09 17:49   ` [PATCH v2 2/2] KVM: selftests: SEV: Sanity check the launch measurement Hemanth Selam
2026-07-10  5:04   ` [PATCH v3] KVM: selftests: Add a test for KVM_CREATE_VM VM type enforcement Hemanth Selam
2026-07-31 22:04     ` Sean Christopherson [this message]

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=am0b36OWeFSy2faX@google.com \
    --to=seanjc@google.com \
    --cc=hemanth.selam@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=shuah@kernel.org \
    /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