kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: selftests: Fix hang in hardware_disable_test
@ 2021-05-14 23:05 David Matlack
  2021-05-17  6:15 ` Andrew Jones
  0 siblings, 1 reply; 2+ messages in thread
From: David Matlack @ 2021-05-14 23:05 UTC (permalink / raw)
  To: kvm; +Cc: Paolo Bonzini, Andrew Jones, Ignacio Alvarado, David Matlack

If /dev/kvm is not available then hardware_disable_test will hang
indefinitely because the child process exits before posting to the
semaphore for which the parent is waiting.

Fix this by making the parent periodically check if the child has
exited. We have to be careful to forward the child's exit status to
preserve a KSFT_SKIP status.

I considered just checking for /dev/kvm before creating the child
process, but there are so many other reasons why the child could exit
early that it seemed better to handle that as general case.

Tested:

$ ./hardware_disable_test
/dev/kvm not available, skipping test
$ echo $?
4
$ modprobe kvm_intel
$ ./hardware_disable_test
$ echo $?
0

Signed-off-by: David Matlack <dmatlack@google.com>
---
 .../selftests/kvm/hardware_disable_test.c     | 32 ++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/hardware_disable_test.c b/tools/testing/selftests/kvm/hardware_disable_test.c
index 5aadf84c91c0..4b8db3bce610 100644
--- a/tools/testing/selftests/kvm/hardware_disable_test.c
+++ b/tools/testing/selftests/kvm/hardware_disable_test.c
@@ -132,6 +132,36 @@ static void run_test(uint32_t run)
 	TEST_ASSERT(false, "%s: [%d] child escaped the ninja\n", __func__, run);
 }
 
+void wait_for_child_setup(pid_t pid)
+{
+	/*
+	 * Wait for the child to post to the semaphore, but wake up periodically
+	 * to check if the child exited prematurely.
+	 */
+	for (;;) {
+		const struct timespec wait_period = { .tv_sec = 1 };
+		int status;
+
+		if (!sem_timedwait(sem, &wait_period))
+			return;
+
+		/* Child is still running, keep waiting. */
+		if (pid != waitpid(pid, &status, WNOHANG))
+			continue;
+
+		/*
+		 * Child is no longer running, which is not expected.
+		 *
+		 * If it exited with a non-zero status, we explicitly forward
+		 * the child's status in case it exited with KSFT_SKIP.
+		 */
+		if (WIFEXITED(status))
+			exit(WEXITSTATUS(status));
+		else
+			TEST_ASSERT(false, "Child exited unexpectedly");
+	}
+}
+
 int main(int argc, char **argv)
 {
 	uint32_t i;
@@ -148,7 +178,7 @@ int main(int argc, char **argv)
 			run_test(i); /* This function always exits */
 
 		pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
-		sem_wait(sem);
+		wait_for_child_setup(pid);
 		r = (rand() % DELAY_US_MAX) + 1;
 		pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
 		usleep(r);
-- 
2.31.1.751.gd2f1c929bd-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] KVM: selftests: Fix hang in hardware_disable_test
  2021-05-14 23:05 [PATCH] KVM: selftests: Fix hang in hardware_disable_test David Matlack
@ 2021-05-17  6:15 ` Andrew Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Jones @ 2021-05-17  6:15 UTC (permalink / raw)
  To: David Matlack; +Cc: kvm, Paolo Bonzini, Ignacio Alvarado

On Fri, May 14, 2021 at 11:05:21PM +0000, David Matlack wrote:
> If /dev/kvm is not available then hardware_disable_test will hang
> indefinitely because the child process exits before posting to the
> semaphore for which the parent is waiting.
> 
> Fix this by making the parent periodically check if the child has
> exited. We have to be careful to forward the child's exit status to
> preserve a KSFT_SKIP status.
> 
> I considered just checking for /dev/kvm before creating the child
> process, but there are so many other reasons why the child could exit
> early that it seemed better to handle that as general case.
> 
> Tested:
> 
> $ ./hardware_disable_test
> /dev/kvm not available, skipping test
> $ echo $?
> 4
> $ modprobe kvm_intel
> $ ./hardware_disable_test
> $ echo $?
> 0
> 
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---
>  .../selftests/kvm/hardware_disable_test.c     | 32 ++++++++++++++++++-
>  1 file changed, 31 insertions(+), 1 deletion(-)
>

Reviewed-by: Andrew Jones <drjones@redhat.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-17  6:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-14 23:05 [PATCH] KVM: selftests: Fix hang in hardware_disable_test David Matlack
2021-05-17  6:15 ` Andrew Jones

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).