[RFC PATCH v2 07/35] KVM: selftests: Return an unused GHCB from the pool

Neeraj Upadhyay posted 35 patches 1 week ago
[RFC PATCH v2 07/35] KVM: selftests: Return an unused GHCB from the pool
Posted by Neeraj Upadhyay 1 week ago
The ghcb_alloc() function in the SEV selftest library always returned
the first GHCB from the pool, regardless of whether it was already in
use. In a multi-vCPU test, this causes all vCPUs to share and corrupt
the same GHCB, leading to unpredictable test failures.

Modify ghcb_alloc() to iterate through the pool and return the first
entry that is not currently marked as in_use. Mark the selected entry
as in use before returning it to prevent other vCPUs from allocating it.

Additionally, correct a minor bug in the memset call which was
incorrectly taking the address of the ghcb pointer instead of using
the pointer value itself.

This ensures that each vCPU in a test gets its own unique GHCB, allowing
multi-threaded SEV-ES tests to run correctly.

Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
 tools/testing/selftests/kvm/lib/x86/sev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/selftests/kvm/lib/x86/sev.c
index de36a6c93839..3090a6518066 100644
--- a/tools/testing/selftests/kvm/lib/x86/sev.c
+++ b/tools/testing/selftests/kvm/lib/x86/sev.c
@@ -68,7 +68,6 @@ static void sev_es_terminate(void)
 
 static struct ghcb_entry *ghcb_alloc(void)
 {
-	return &ghcb_pool->ghcbs[0];
 	struct ghcb_entry *entry;
 	struct ghcb *ghcb;
 	int i;
@@ -81,7 +80,7 @@ static struct ghcb_entry *ghcb_alloc(void)
 			entry = &ghcb_pool->ghcbs[i];
 			ghcb = &entry->ghcb;
 
-			memset(&ghcb, 0, sizeof(*ghcb));
+			memset(ghcb, 0, sizeof(*ghcb));
 			ghcb->ghcb_usage = 0;
 			ghcb->protocol_version = 1;
 
-- 
2.34.1