Re: [PATCH v5 06/10] KVM: selftests: Add NX huge pages test

From: Sean Christopherson
Date: Wed Apr 13 2022 - 18:35:49 EST


On Wed, Apr 13, 2022, Ben Gardon wrote:
> There's currently no test coverage of NX hugepages in KVM selftests, so
> add a basic test to ensure that the feature works as intended.

...

> diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
> new file mode 100644
> index 000000000000..7f80e48781fd
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
> @@ -0,0 +1,166 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * tools/testing/selftests/kvm/nx_huge_page_test.c
> + *
> + * Usage: to be run via nx_huge_page_test.sh, which does the necessary
> + * environment setup and teardown

It would be really nice if this test could either (a) do something useful without
having to manually set /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages,
or (b) refuse to run unless it's (likely) been invoked by the script. E.g. maybe
add a magic token that must be passed in? That way just running the bare test
will provide a helpful skip message, but someone that wants to fiddle with it can
still run it manually.

> +int main(int argc, char **argv)
> +{
> + struct kvm_vm *vm;
> + struct timespec ts;
> + void *hva;

This needs to check if the workaround is actually enabled via module param. Not
as big a deal if there's a magic number, but it's also not too hard to query a
module param. Or at least, it shouldn't be, I'm fairly certain that's one of the
things I want to address in the selftests overhaul.

Aha! Actually, IIUC, the patch that validates the per-VM override adds full support
for the module param being turned off.

So, how about pull in the tweaks to the expected number to this patch, and then
the per-VM override test just makes disable_nx a logical OR of the module param
beyond off or the test using the per-VM override.

> diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh
> new file mode 100755
> index 000000000000..19fc95723fcb
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh
> @@ -0,0 +1,25 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0-only */
> +
> +# tools/testing/selftests/kvm/nx_huge_page_test.sh
> +# Copyright (C) 2022, Google LLC.

This should either check for root or use sudo.

> +NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages)
> +NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio)
> +NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms)
> +HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)
> +
> +echo 1 > /sys/module/kvm/parameters/nx_huge_pages
> +echo 1 > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio
> +echo 100 > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms
> +echo 200 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
> +
> +./nx_huge_pages_test

I would much prefer this find its path and use that to reference the test, e.g. this
fails if invoking the script from anything but the x86_64 subdirectory. I'd provide
a snippet of how to do that, but my scripting skills are garbage :-)

> +RET=$?
> +
> +echo $NX_HUGE_PAGES > /sys/module/kvm/parameters/nx_huge_pages
> +echo $NX_HUGE_PAGES_RECOVERY_RATIO > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio
> +echo $NX_HUGE_PAGES_RECOVERY_PERIOD > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms
> +echo $HUGE_PAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
> +
> +exit $RET
> --
> 2.35.1.1178.g4f1659d476-goog
>