Re: [RFC V3 PATCH 6/6] sefltests: kvm: x86: Add selftest for private memory
From: Sean Christopherson
Date: Thu Oct 06 2022 - 16:24:07 EST
On Fri, Aug 19, 2022, Vishal Annapurve wrote:
> +static bool verify_mem_contents(void *mem, uint32_t size, uint8_t pat)
As per feedback in v1[*], spell out "pattern".
[*] https://lore.kernel.org/all/YtiJx11AZHslcGnN@xxxxxxxxxx
> +{
> + uint8_t *buf = (uint8_t *)mem;
> +
> + for (uint32_t i = 0; i < size; i++) {
> + if (buf[i] != pat)
> + return false;
> + }
> +
> + return true;
> +}
> +
> +/*
> + * Add custom implementation for memset to avoid using standard/builtin memset
> + * which may use features like SSE/GOT that don't work with guest vm execution
> + * within selftests.
> + */
> +void *memset(void *mem, int byte, size_t size)
> +{
> + uint8_t *buf = (uint8_t *)mem;
> +
> + for (uint32_t i = 0; i < size; i++)
> + buf[i] = byte;
> +
> + return buf;
> +}
memset(), memcpy(), and memcmp() are safe to use as of commit 6b6f71484bf4 ("KVM:
selftests: Implement memcmp(), memcpy(), and memset() for guest use").
Note the "fun" with gcc "optimizing" into infinite recursion... :-)
> +
> +static void populate_test_area(void *test_area_base, uint64_t pat)
> +{
> + memset(test_area_base, pat, TEST_AREA_SIZE);
> +}
> +
> +static void populate_guest_test_mem(void *guest_test_mem, uint64_t pat)
> +{
> + memset(guest_test_mem, pat, GUEST_TEST_MEM_SIZE);
> +}
> +
> +static bool verify_test_area(void *test_area_base, uint64_t area_pat,
> + uint64_t guest_pat)
Again, avoid "pat".
> +{
> + void *test_area1_base = test_area_base;
> + uint64_t test_area1_size = GUEST_TEST_MEM_OFFSET;
> + void *guest_test_mem = test_area_base + test_area1_size;
> + uint64_t guest_test_size = GUEST_TEST_MEM_SIZE;
> + void *test_area2_base = guest_test_mem + guest_test_size;
> + uint64_t test_area2_size = (TEST_AREA_SIZE - (GUEST_TEST_MEM_OFFSET +
> + GUEST_TEST_MEM_SIZE));
This is all amazingly hard to read. AFAICT, the local variables are largely useless.
Actually, why even take in @test_area_base, isn't it hardcoded to TEST_AREA_GPA?
Then everything except the pattern can be hardcoded.
> + return (verify_mem_contents(test_area1_base, test_area1_size, area_pat) &&
> + verify_mem_contents(guest_test_mem, guest_test_size, guest_pat) &&
> + verify_mem_contents(test_area2_base, test_area2_size, area_pat));
> +}