* [PATCH] KVM: Documentation: document how KVM is tested
@ 2025-07-10 10:29 Paolo Bonzini
2025-07-10 17:44 ` Edgecombe, Rick P
2025-07-10 22:26 ` Sean Christopherson
0 siblings, 2 replies; 3+ messages in thread
From: Paolo Bonzini @ 2025-07-10 10:29 UTC (permalink / raw)
To: linux-kernel, kvm; +Cc: seanjc, Rick Edgecombe
Proper testing greatly simplifies both patch development and review,
but it can be unclear what kind of userspace or guest support
should accompany new features. Clarify maintainer expectations
in terms of testing expectations; additionally, list the cases in
which open-source userspace support is pretty much a necessity and
its absence can only be mitigated by selftests.
While these ideas have long been followed implicitly by KVM contributors
and maintainers, formalize them in writing to provide consistent (though
not universal) guidelines.
Suggested-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
Documentation/virt/kvm/review-checklist.rst | 92 +++++++++++++++++++--
1 file changed, 87 insertions(+), 5 deletions(-)
diff --git a/Documentation/virt/kvm/review-checklist.rst b/Documentation/virt/kvm/review-checklist.rst
index 7eb9974c676d..87d5aee4366c 100644
--- a/Documentation/virt/kvm/review-checklist.rst
+++ b/Documentation/virt/kvm/review-checklist.rst
@@ -21,8 +21,7 @@ Review checklist for kvm patches
6. New cpu features should be exposed via KVM_GET_SUPPORTED_CPUID2,
or its equivalent for non-x86 architectures
-7. Emulator changes should be accompanied by unit tests for qemu-kvm.git
- kvm/test directory.
+7. The feature should be testable (see below).
8. Changes should be vendor neutral when possible. Changes to common code
are better than duplicating changes to vendor code.
@@ -37,6 +36,89 @@ Review checklist for kvm patches
11. New guest visible features must either be documented in a hardware manual
or be accompanied by documentation.
-12. Features must be robust against reset and kexec - for example, shared
- host/guest memory must be unshared to prevent the host from writing to
- guest memory that the guest has not reserved for this purpose.
+Testing of KVM code
+-------------------
+
+All features contributed to KVM, and in many cases bugfixes too, should be
+accompanied by some kind of tests and/or enablement in open source guests
+and VMMs. KVM is covered by multiple test suites:
+
+*Selftests*
+ These are low level tests included in the kernel tree. While relatively
+ challenging to write, they allow granular testing of kernel APIs. This
+ includes API failure scenarios, invoking APIs after specific guest
+ instructions, and testing multiple calls to ``KVM_CREATE_VM`` within
+ a single test.
+
+``kvm-unit-tests``
+ A collection of small guests that test CPU and emulated device features
+ from a guest's perspective. They run under QEMU or ``kvmtool``,
+ are relatively easy to write. `kvm-`unit-tests`` are generally not
+ KVM-specific; they can be run with any accelerator that QEMU support
+ or even on bare metal, making it possible to compare behavior across
+ hypervisors and processor families.
+
+Functional test suites
+ Various sets of functional tests exist, such as QEMU's ``tests/functional``
+ suite and `avocado-vt <https://avocado-vt.readthedocs.io/en/latest/>`__.
+ These typically involve running a full operating system in a virtual
+ machine.
+
+The best testing approach depends on the feature's complexity and
+operation. Here are some examples and guidelines:
+
+New instructions (no new registers or APIs)
+ The corresponding CPU features (if applicable) should be made available
+ in QEMU. If the instructions require emulation support or other code in
+ KVM, it is worth adding coverage to ``kvm-unit-tests`` or selftests.
+ While selftests are generally larger and harder to write, they may be
+ a better choice if the instructions relate to an API that already
+ has good selftest coverage.
+
+New hardware features (new registers, no new APIs)
+ These should be tested via ``kvm-unit-tests``; this more or less implies
+ supporting them in QEMU and/or ``kvmtool``. In some cases selftests
+ can be used instead, similar to the previous case, or specifically to
+ test corner cases in guest state save/restore.
+
+Bug fixes and performance improvements
+ These usually do not introduce new APIs, but it's worth sharing
+ any benchmarks and tests used to validate your contribution,
+ ideally in the form of regression tests. Tests and benchmarks
+ can be included in either ``kvm-unit-tests`` or selftests, depending
+ on the specifics of your change. Selftests are especially useful for
+ regression tests because they are included directly in Linux's tree.
+
+Large scale internal changes
+ While it's difficult to provide a single policy, you should ensure that
+ the changed code is covered by either ``kvm-unit-tests`` or selftests.
+ In some cases the affected code is run for any guests and functional
+ tests suffice. Explain your testing process in the cover letter,
+ as that can help identify gaps in existing test suites.
+
+New APIs
+ It is important to demonstrate your use case. This can be as simple as
+ explaining that the feature is already in use on bare metal, or it can be
+ a proof-of-concept implementation in userspace. The latter need not be
+ open source, though that is of course preferrable for easier testing.
+ Selftests should test corner cases of the APIs, and should also cover
+ basic guest operation if no open source VMM uses the feature.
+
+Bigger features, usually spanning host and guest
+ These should be supported by Linux guests, with limited exceptions
+ for Hyper-V features that are testable on Windows guests. It is
+ strongly suggested that the feature be usable exclusively with open
+ source code, including in at least one of QEMU or crosvm. Selftests
+ should test at least API error cases. Guest operation can be
+ covered by either selftests of ``kvm-unit-tests`` (this is especially
+ important for paravirtualized and Windows-only features). Strong
+ selftest coverage can also be a replacement for implementation in an
+ open source VMM, but this is generally not recommended.
+
+Following the above suggestions for testing in selftests and
+``kvm-unit-tests`` will make it easier for the maintainers to review
+and accept your code. In fact, even before you contribute your changes
+upstream it will make it easier for you to develop for KVM.
+
+Of course, the KVM maintainers reserve the right to require more tests,
+though they may also waive the requirement from time to time.
--
2.50.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: Documentation: document how KVM is tested
2025-07-10 10:29 [PATCH] KVM: Documentation: document how KVM is tested Paolo Bonzini
@ 2025-07-10 17:44 ` Edgecombe, Rick P
2025-07-10 22:26 ` Sean Christopherson
1 sibling, 0 replies; 3+ messages in thread
From: Edgecombe, Rick P @ 2025-07-10 17:44 UTC (permalink / raw)
To: kvm@vger.kernel.org, pbonzini@redhat.com,
linux-kernel@vger.kernel.org
Cc: seanjc@google.com
On Thu, 2025-07-10 at 12:29 +0200, Paolo Bonzini wrote:
> +Bigger features, usually spanning host and guest
> + These should be supported by Linux guests, with limited exceptions
> + for Hyper-V features that are testable on Windows guests. It is
> + strongly suggested that the feature be usable exclusively with open
> + source code, including in at least one of QEMU or crosvm. Selftests
"be usable exclusively with open source code" might be a little ambiguous. I
think you mean should be usable without any closed source components. But, it
might be read that it should not be used with closed source components.
I think you also don't mean host firmware, etc.
Maybe clearer?
It is strongly suggested that the feature be usable with a full stack of open
source userspace and guest, including in at least one of QEMU or crosvm.
And thanks for clarifying the policy!
> + should test at least API error cases. Guest operation can be
> + covered by either selftests of ``kvm-unit-tests`` (this is especially
> + important for paravirtualized and Windows-only features). Strong
> + selftest coverage can also be a replacement for implementation in an
> + open source VMM, but this is generally not recommended.
> +
> +Following the above suggestions for testing in selftests and
> +``kvm-unit-tests`` will make it easier for the maintainers to review
> +and accept your code. In fact, even before you contribute your changes
> +upstream it will make it easier for you to develop for KVM.
> +
> +Of course, the KVM maintainers reserve the right to require more tests,
> +though they may also waive the requirement from time to time.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: Documentation: document how KVM is tested
2025-07-10 10:29 [PATCH] KVM: Documentation: document how KVM is tested Paolo Bonzini
2025-07-10 17:44 ` Edgecombe, Rick P
@ 2025-07-10 22:26 ` Sean Christopherson
1 sibling, 0 replies; 3+ messages in thread
From: Sean Christopherson @ 2025-07-10 22:26 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: linux-kernel, kvm, Rick Edgecombe
On Thu, Jul 10, 2025, Paolo Bonzini wrote:
> +Testing of KVM code
> +-------------------
> +
> +All features contributed to KVM, and in many cases bugfixes too, should be
> +accompanied by some kind of tests and/or enablement in open source guests
> +and VMMs. KVM is covered by multiple test suites:
> +
> +*Selftests*
> + These are low level tests included in the kernel tree. While relatively
> + challenging to write, they allow granular testing of kernel APIs. This
> + includes API failure scenarios, invoking APIs after specific guest
> + instructions, and testing multiple calls to ``KVM_CREATE_VM`` within
> + a single test.
> +
> +``kvm-unit-tests``
> + A collection of small guests that test CPU and emulated device features
> + from a guest's perspective. They run under QEMU or ``kvmtool``,
> + are relatively easy to write.
IMO, selftests aren't meaningfully harder to write than KVM-Unit-Tests. They
require a bit more boilerplate to get started, but in many ways I would argue
they're easier to write because the C code has full control over the VM.
E.g. doing multi-threaded testing in KUT gets painful because it often requires
using IPIs to coordinate between vCPUs, trying to test different vCPU models
requires writing multiple config entries, etc.
And I think the boilerplate "problem" is mostly solvable, by expanding the KVM
selftests harness to handle the bulk of the work for simple tests, e.g. see
https://lore.kernel.org/all/ZjUwqEXPA5QVItyX@google.com.
The reason I bring this up is that I'd prefer to steer people in the direction
of KVM selftests over KVM-Unit-Tests. Or at the very least, not steer them too
hard towards KVM-Unit-Tests by suggesting that KVM-Unit-Tests will require less
effort.
Because selftests can define the vCPU model and VM capabilities, they can provide
significantly more coverage than KUT in many cases. E.g. one of the things on my
wish list is to rewrite KUT's MSR test as a KVM selftest that tests both guest and
host userspace behavior, and that permutes over various vCPU models.
And from a maintenance perspective, having selftests in-tree is a huge benefit for
me. That alone is honestly reason enough for me to push for selftests over KUT.
All that said, this looks awesome! Thanks for writing this up!
> + `kvm-`unit-tests`` are generally not
> + KVM-specific; they can be run with any accelerator that QEMU support
> + or even on bare metal, making it possible to compare behavior across
> + hypervisors and processor families.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-07-10 22:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-10 10:29 [PATCH] KVM: Documentation: document how KVM is tested Paolo Bonzini
2025-07-10 17:44 ` Edgecombe, Rick P
2025-07-10 22:26 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).