From patchwork Thu Mar 14 18:54:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13592666 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A269757E3 for ; Thu, 14 Mar 2024 18:55:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442506; cv=none; b=OWHcdUf5OBBVzLd0YbN1rdoEsFziGc4zgi6Rv1i20fgcsnvtXaN/xi83406mZg9ngqI4Iw0xwnygmWSUDg+hNOnTVZQI3H3NP1YC6g87stD0BQ36p0TtnfP1bWzl23JKaApC4NVNmcOX1mJrIwfpOOQSzk+bPO8NSEJZoHe3Jaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442506; c=relaxed/simple; bh=EMJRx8SSjyWKJEA47Fx++/ID68qFGbsXeFFdrGx+uJQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sCvSjKcIi666UfINLHXvPQU2CK9b0FzD2fa+Gcs1QFzaRhEKv4Wbr8Odx0jmj1ShQJWpRH23VFGb0KGjcNTFqzGivTmP95BrbYFtSRyaMCKclaB12XtB5ldiDZ+Ab8cgYPJHSmSMOh2f2oDTuxrrfV6zbYY2GhAXuHU6lOMUOQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lh6mZLRY; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lh6mZLRY" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60cc00203faso29486637b3.2 for ; Thu, 14 Mar 2024 11:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710442503; x=1711047303; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=NvzHDkK63Oa7MmelAXu2DrtJh2IlG1wR0jNzRcE2MMA=; b=lh6mZLRY0bGTWGQuwoZU3yM4E+n++cSp1xtPx9YJPmaYuUIefffACf3i9Q9njC/enq GRau56i4k6IL7wQ7DJIIk2vSUDLQAIWNuFCMlxrz9OIk5pJ+T1rslpYz0Nvb9mdw+Ct3 OElTvjagz72M+T/ivYVGAl+Vw7F+MZPlFcf2X0QWGmm0AUN9caqvG/zkgmh/w+OgIlky 21XAYMu668q/4896PxDm+a83rHQuH8HY6/cwVXELUYCKsucuXwfvjum0ga9xYAK8oEBZ dARoaG2IvzaQ4PiG5qUz4LP604gZG6Cc5+XSb+0cdfaVXytTWAcsbmQbGRgW3uQ7xEJW bymA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710442503; x=1711047303; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NvzHDkK63Oa7MmelAXu2DrtJh2IlG1wR0jNzRcE2MMA=; b=R1wOtJgLcZBnJv83Pz+qCVogNyLasbP1P12K+6RWMU8PUxG2sj2mG3NmFmd4LEKi01 5QTqxdjrTX4YdlBZgE5Gq287ixJ8CYh6WhMvZcmBEjkYbcltWF0gVzApVt/0xt99ZKsV /HQTT1IfVYpoK4zp78Ry0/SDSuM2fDvDBl+XCqGWuU8ausyU0RESs+d5DROVRMHdxSpL +uDcOnDjlykYZlT2WacJsIEcCrpK7CDGrng6rmmWXQGLMniwlwdVIiEi3rCuy//nelCm /wBa73rbw8OXX0huuXVZNgUaP++Jdc8v0U4IYl5LoeEUyPiu6Gwg47Beb9Ow0S4T6qf8 dr0A== X-Gm-Message-State: AOJu0YwmwmYyGDBNRrgcnxpHajkJd/DGB+ldlIMtpmjB/MImM5gW9uSw h6Yxxo3CZ22gLr94Hn6G9zfz6Pw/Idj50DSsD4+pihA1swclamWGLv3DrLz3dTDhWtXgKdJAz64 y9A== X-Google-Smtp-Source: AGHT+IGoIsxHFIUNId9qdYbp+v67IVJuW3NrOEBDdlgtrs6pUVvhUIMquDklED8pzE9AvlSgmXc9mQGDCIo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1004:b0:dc6:44d4:bee0 with SMTP id w4-20020a056902100400b00dc644d4bee0mr197ybt.7.1710442503231; Thu, 14 Mar 2024 11:55:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Mar 2024 11:54:54 -0700 In-Reply-To: <20240314185459.2439072-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240314185459.2439072-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240314185459.2439072-2-seanjc@google.com> Subject: [PATCH 1/5] KVM: selftests: Provide a global pseudo-RNG instance for all tests From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Add a global guest_random_state instance, i.e. a pseudo-RNG, so that an RNG is available for *all* tests. This will allow randomizing behavior in core library code, e.g. x86 will utilize the pRNG to conditionally force emulation of writes from within common guest code. To allow for deterministic runs, and to be compatible with existing tests, allow tests to override the seed used to initialize the pRNG. Note, the seed *must* be overwritten before a VM is created in order for the seed to take effect, though it's perfectly fine for a test to initialize multiple VMs with different seeds. And as evidenced by memstress_guest_code(), it's also a-ok to instantiate more RNGs using the global seed (or a modified version of it). The goal of the global RNG is purely to ensure that _a_ source of random numbers is available, it doesn't have to be the _only_ RNG. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/dirty_log_perf_test.c | 9 ++++----- tools/testing/selftests/kvm/dirty_log_test.c | 17 +---------------- tools/testing/selftests/kvm/include/memstress.h | 1 - tools/testing/selftests/kvm/include/test_util.h | 8 ++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 9 +++++++++ tools/testing/selftests/kvm/lib/memstress.c | 8 +------- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 504f6fe980e8..5cda9780c378 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -132,7 +132,6 @@ struct test_params { enum vm_mem_backing_src_type backing_src; int slots; uint32_t write_percent; - uint32_t random_seed; bool random_access; }; @@ -156,8 +155,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) p->slots, p->backing_src, p->partition_vcpu_memory_access); - pr_info("Random seed: %u\n", p->random_seed); - memstress_set_random_seed(vm, p->random_seed); memstress_set_write_percent(vm, p->write_percent); guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift; @@ -346,11 +343,13 @@ int main(int argc, char *argv[]) .partition_vcpu_memory_access = true, .backing_src = DEFAULT_VM_MEM_SRC, .slots = 1, - .random_seed = 1, .write_percent = 100, }; int opt; + /* Override the seed to be deterministic by default. */ + guest_random_seed = 1; + dirty_log_manual_caps = kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | @@ -395,7 +394,7 @@ int main(int argc, char *argv[]) p.phys_offset = strtoull(optarg, NULL, 0); break; case 'r': - p.random_seed = atoi_positive("Random seed", optarg); + guest_random_seed = atoi_positive("Random seed", optarg); break; case 's': p.backing_src = parse_backing_src_type(optarg); diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index eaad5b20854c..525d92dda646 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -76,7 +76,6 @@ static uint64_t host_page_size; static uint64_t guest_page_size; static uint64_t guest_num_pages; -static uint64_t random_array[TEST_PAGES_PER_LOOP]; static uint64_t iteration; /* @@ -115,13 +114,12 @@ static void guest_code(void) while (true) { for (i = 0; i < TEST_PAGES_PER_LOOP; i++) { addr = guest_test_virt_mem; - addr += (READ_ONCE(random_array[i]) % guest_num_pages) + addr += (guest_random_u64(&guest_rng) % guest_num_pages) * guest_page_size; addr = align_down(addr, host_page_size); *(uint64_t *)addr = READ_ONCE(iteration); } - /* Tell the host that we need more random numbers */ GUEST_SYNC(1); } } @@ -508,20 +506,10 @@ static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) mode->after_vcpu_run(vcpu, ret, err); } -static void generate_random_array(uint64_t *guest_array, uint64_t size) -{ - uint64_t i; - - for (i = 0; i < size; i++) - guest_array[i] = random(); -} - static void *vcpu_worker(void *data) { int ret; struct kvm_vcpu *vcpu = data; - struct kvm_vm *vm = vcpu->vm; - uint64_t *guest_array; uint64_t pages_count = 0; struct kvm_signal_mask *sigmask = alloca(offsetof(struct kvm_signal_mask, sigset) + sizeof(sigset_t)); @@ -540,11 +528,8 @@ static void *vcpu_worker(void *data) sigemptyset(sigset); sigaddset(sigset, SIG_IPI); - guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array); - while (!READ_ONCE(host_quit)) { /* Clear any existing kick signals */ - generate_random_array(guest_array, TEST_PAGES_PER_LOOP); pages_count += TEST_PAGES_PER_LOOP; /* Let the guest dirty the random pages */ ret = __vcpu_run(vcpu); diff --git a/tools/testing/selftests/kvm/include/memstress.h b/tools/testing/selftests/kvm/include/memstress.h index ce4e603050ea..9071eb6dea60 100644 --- a/tools/testing/selftests/kvm/include/memstress.h +++ b/tools/testing/selftests/kvm/include/memstress.h @@ -62,7 +62,6 @@ struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus, void memstress_destroy_vm(struct kvm_vm *vm); void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent); -void memstress_set_random_seed(struct kvm_vm *vm, uint32_t random_seed); void memstress_set_random_access(struct kvm_vm *vm, bool random_access); void memstress_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct memstress_vcpu_args *)); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 8a6e30612c86..4b78fb7e539e 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -91,9 +91,17 @@ struct guest_random_state { uint32_t seed; }; +extern uint32_t guest_random_seed; +extern struct guest_random_state guest_rng; + struct guest_random_state new_guest_random_state(uint32_t seed); uint32_t guest_random_u32(struct guest_random_state *state); +static inline uint64_t guest_random_u64(struct guest_random_state *state) +{ + return ((uint64_t)guest_random_u32(state) << 32) | guest_random_u32(state); +} + enum vm_mem_backing_src_type { VM_MEM_SRC_ANONYMOUS, VM_MEM_SRC_ANONYMOUS_THP, diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index b2262b5fad9e..02d7fa70d440 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -20,6 +20,9 @@ #define KVM_UTIL_MIN_PFN 2 +uint32_t guest_random_seed; +struct guest_random_state guest_rng; + static int vcpu_mmap_sz(void); int open_path_or_exit(const char *path, int flags) @@ -433,6 +436,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, slot0 = memslot2region(vm, 0); ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size); + pr_info("Random seed: 0x%x\n", guest_random_seed); + guest_rng = new_guest_random_state(guest_random_seed); + sync_global_to_guest(vm, guest_rng); + kvm_arch_vm_post_create(vm); return vm; @@ -2306,6 +2313,8 @@ void __attribute((constructor)) kvm_selftest_init(void) /* Tell stdout not to buffer its content. */ setbuf(stdout, NULL); + guest_random_seed = random(); + kvm_selftest_arch_init(); } diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/selftests/kvm/lib/memstress.c index cf2c73971308..f8bfb4988368 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -56,7 +56,7 @@ void memstress_guest_code(uint32_t vcpu_idx) uint64_t page; int i; - rand_state = new_guest_random_state(args->random_seed + vcpu_idx); + rand_state = new_guest_random_state(guest_random_seed + vcpu_idx); gva = vcpu_args->gva; pages = vcpu_args->pages; @@ -243,12 +243,6 @@ void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent) sync_global_to_guest(vm, memstress_args.write_percent); } -void memstress_set_random_seed(struct kvm_vm *vm, uint32_t random_seed) -{ - memstress_args.random_seed = random_seed; - sync_global_to_guest(vm, memstress_args.random_seed); -} - void memstress_set_random_access(struct kvm_vm *vm, bool random_access) { memstress_args.random_access = random_access; From patchwork Thu Mar 14 18:54:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13592667 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27EA175818 for ; Thu, 14 Mar 2024 18:55:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442507; cv=none; b=uE0plQfV563Hu4Pij9Katsb4D23ZGJoQdP3nh8q3USfKD2zgOWIWQfwYPD6ITymUmqT+umddc8fzfnAP+C9Co2povlACn16SXuCJ+Cb0HSBa/967I7tS8XlRZj+CLGXQEeVJbq/rB3M8UswByWSkNoyuS3klS9J+YgApQUOWWKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442507; c=relaxed/simple; bh=g8+m6wVhn62/YB61tWYmVaCN82OA3SBWyU5t/Mgkl/s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p+5ODj7MD6HU0En23xi1a2WBM6gDdC991N32eFrsXT9xykGz0sIW8bepVS5ief/4XCnQp9yFXTf7LjSUfMtUce2Xl+nRazZcg2GE7lK25gtCPs90ipwoH9/5iWLju5Dmqr1T81dswbezLXFOwxdAU/Ot/sg2Q+OJgW0YVB47gdU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FonB+BZt; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FonB+BZt" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dce775fa8adso2069701276.1 for ; Thu, 14 Mar 2024 11:55:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710442505; x=1711047305; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6+Az/OLv9UsNduwOpIQqghv3e+Z9TgGHRylS0Lw0x08=; b=FonB+BZtePmgYv5jIv+EUyqLqQ9398WDd1y6lA4H1jBd/Fc8hGwfUjoYIZrUyAKcMW 5HQFTdFENZ6vAwH2IPsrF9r+xc7qwZ0WQhMwqZRiz6uT1LtljZ3ayjDkgea+iHX8ln1O IpE43p01lie9t3vBQADzwSlFT2Vqx5GbJiEK1V6kfxIVn8OiVCixXUke7lQe9ayBt/U+ v+z9INrzhpjbN6rEjeSemRw5mRBk0ZGryJ4t/p4Z+M6L5fb0WZdDvuh0UP3XOc+tmoGD shAzyJSGRSW4dugK2Y8+bSnQA0vvuuujsebFMpN2uEqsp05zLFVyXpRD/msDob7sXkv6 m29A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710442505; x=1711047305; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6+Az/OLv9UsNduwOpIQqghv3e+Z9TgGHRylS0Lw0x08=; b=CsTX7jaXFOJnPP1XqWFFkoEUOgjgg0kPK1KA6EM+eXKUgH/+Wo/dAVssvIoXPu0Mo2 feCTOYHSJ85ahfYLUDUb+fmkjV1mVyE7Q6liqMYHI1963fHn8w5hnI3PK8kr/e1jusuC FPfmB+mgURbeJ3IKGdWuwRLbYbp99w/wWAcWj6rKATW9/CutwLGFVcUO6vXkKV2hO/Qb OWY9cp6d9w8CvSWjH03YPZ2j1IrlstUcgoYchpYkx3/fKZxohztFI16P/4iAcCwBJ+MA dMard3jGbuxsrnXWnQ5ORNFr1kPMjvy+4uC9kDiAdU/vHuQXDPXMZv73H8tB12w+AXEQ 3XKA== X-Gm-Message-State: AOJu0YwYkhNhm4Ci7dqYr/Z1UdzvpzkLk2OBUVM4chE4imBKJfj++yOO x/3oPic34SEI5ayx321KaZLcz9B3Jt5MnmZRfUW1J7AnKQlZ9H1cO1IKdvFaG+ceVSyIfde0apR 5hg== X-Google-Smtp-Source: AGHT+IEhmzPk/UcLVfOZhpWA1P7NVg8eJi7RqkeVDRHqMqz9dDS4VG4S19mrC8/iJZ2xuyNfKfSSOWHjSIU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1b91:b0:dcc:6065:2b3d with SMTP id ei17-20020a0569021b9100b00dcc60652b3dmr1781ybb.8.1710442505154; Thu, 14 Mar 2024 11:55:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Mar 2024 11:54:55 -0700 In-Reply-To: <20240314185459.2439072-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240314185459.2439072-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240314185459.2439072-3-seanjc@google.com> Subject: [PATCH 2/5] KVM: selftests: Provide an API for getting a random bool from an RNG From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Move memstress' random bool logic into common code to avoid reinventing the wheel for basic yes/no decisions. Provide an outer wrapper to handle the basic/common case of just wanting a 50/50 chance of something happening. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/test_util.h | 11 +++++++++++ tools/testing/selftests/kvm/lib/memstress.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 4b78fb7e539e..3e473058849f 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -97,6 +97,17 @@ extern struct guest_random_state guest_rng; struct guest_random_state new_guest_random_state(uint32_t seed); uint32_t guest_random_u32(struct guest_random_state *state); +static inline bool __guest_random_bool(struct guest_random_state *state, + uint8_t percent) +{ + return (guest_random_u32(state) % 100) < percent; +} + +static inline bool guest_random_bool(struct guest_random_state *state) +{ + return __guest_random_bool(state, 50); +} + static inline uint64_t guest_random_u64(struct guest_random_state *state) { return ((uint64_t)guest_random_u32(state) << 32) | guest_random_u32(state); diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/selftests/kvm/lib/memstress.c index f8bfb4988368..2d49a5643b71 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -76,7 +76,7 @@ void memstress_guest_code(uint32_t vcpu_idx) addr = gva + (page * args->guest_page_size); - if (guest_random_u32(&rand_state) % 100 < args->write_percent) + if (__guest_random_bool(&rand_state, args->write_percent)) *(uint64_t *)addr = 0x0123456789ABCDEF; else READ_ONCE(*(uint64_t *)addr); From patchwork Thu Mar 14 18:54:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13592668 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9A7876047 for ; Thu, 14 Mar 2024 18:55:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442509; cv=none; b=ZLgkCZe7UBHGjyJ8HHOl+GJsoM9qP6K9Xz94jbMxL4O4XLdwWXuA5HlQTc0D2lZvIA16gNte6xTmKq6CssxrWWssqDbZpNiW09bE8vIOlqf90AgbEoY4fovq5OuEo7W3CSSU31FB60bXg7W5fkLH0VB1Gp2yp6z/xf9pT/cKaUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442509; c=relaxed/simple; bh=fSwPPzmsRz/TgahTC4xFleLaimU9HNgia1ALtn+LPvo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b2KNn8uZ6Stp5F7MCiF9QwNpiJRI1V6jaVf/IaBGkCcVBlaRmGEy3VuXZji7H8K5S3O96AR20/1mAopZK1Ixc2LNzKMMEQhMkrSiyL2uOJj6z1glTJYXLZdBtIH6YkMGaXrmCWZwKYQH6hofmqIpyT8FWWlgQygsFD0PUmPHjD0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=01U4SkH4; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="01U4SkH4" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcf22e5b70bso2037126276.1 for ; Thu, 14 Mar 2024 11:55:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710442507; x=1711047307; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=VbXUdBanzmQlcr1pluWbD2KPY1+lV9seEY77FVlY+o0=; b=01U4SkH4xybtGVPXPMl3N7LndttGH0Y1Qm7RF2S6xluKthfJp/UvsReNww/k+sNYET m7LEzD+6xuRee7VlHfPLnWPbSqgcUSyGNN+XXfloDF0QtGGejfg+9RcUN6Def4df8M31 Rl3JYMJ+thrFXWADNgKn5QOxh9v8ju2hg286miWlvCyNUmDzWTdgkubb/5na+aO6pLkH kreczGEkVTtAnoohfev7wMDtaCNq+W8ZXCHS1y7CBgylImKVtAQ4ht/Gpl6XDXrpp15B Y5Gse4dO3LzcXd+xCMJ15tWhjpuQ4WJL4LLgeaQuILcNfCmSJmXKIhnjtn2xi1A29Wo3 cuKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710442507; x=1711047307; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VbXUdBanzmQlcr1pluWbD2KPY1+lV9seEY77FVlY+o0=; b=IwcxTrFuLnO0/JAHQh5PFWYdBNIvvT2OmEIHtMDWgh43YZx/w85Ca2rXHTk7lN+OP1 RaAdwTMmDssnIRnZt0DcgWhwZmWlRdU2Fmo42ElvB7VManrE2IWiKDfQZ3hqf9ZPTI8x 0JMadT5j9OHOG2Pseqc8e1/UDdPt51+gOyJmlnOQFqVFN5venJjEG1ftUrMBhc+7ChKU 6Faucb1yGplHmcIwR5UY6aAA855E9WZAvR2rcb/YlxJSyDuMLpE6uBbOAoHJYOxLhRxT cOe67/VOdAHzdii44gX1V+3/wFQX7Lv1sj1RNiQOgysBRxvuWYKukRpesKfscvk0nWrf F6Bw== X-Gm-Message-State: AOJu0Yw19k6cEju+Ls8oodfBMmcViOtR4yrAXuuLyMFpLuHms52aJBwl GcrgKKBWlm4eOARu0QHQ6W1S4aVLPqfjEAeP7Vg7J8dkZ+fWk0f+s56KiY5OZcIfkvjOgyQaaYV cTQ== X-Google-Smtp-Source: AGHT+IEZ1OSipPSTe0kz7Kmt0BvaB8Qd172jYpkSyu+Jxyc/YLpAFg+WYFLQGLhtIK+ZYeLsJcygKAry7Dg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1502:b0:dc6:e823:9edb with SMTP id q2-20020a056902150200b00dc6e8239edbmr29ybu.12.1710442507014; Thu, 14 Mar 2024 11:55:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Mar 2024 11:54:56 -0700 In-Reply-To: <20240314185459.2439072-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240314185459.2439072-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240314185459.2439072-4-seanjc@google.com> Subject: [PATCH 3/5] KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled() From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Add a global snapshot of kvm_is_forced_emulation_enabled() and sync it to all VMs by default so that core library code can force emulation, e.g. to allow for easier testing of the intersections between emulation and other features in KVM. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/kvm_util_arch.h | 2 ++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 3 +++ tools/testing/selftests/kvm/x86_64/pmu_counters_test.c | 3 --- .../selftests/kvm/x86_64/userspace_msr_exit_test.c | 10 ++-------- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h index 9f1725192aa2..41aba476640a 100644 --- a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h @@ -5,6 +5,8 @@ #include #include +extern bool is_forced_emulation_enabled; + struct kvm_vm_arch { uint64_t c_bit; uint64_t s_bit; diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 74a4c736c9ae..452d092ae050 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -23,6 +23,7 @@ vm_vaddr_t exception_handlers; bool host_cpu_is_amd; bool host_cpu_is_intel; +bool is_forced_emulation_enabled; static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent) { @@ -577,6 +578,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm) vm_create_irqchip(vm); sync_global_to_guest(vm, host_cpu_is_intel); sync_global_to_guest(vm, host_cpu_is_amd); + sync_global_to_guest(vm, is_forced_emulation_enabled); if (vm->subtype == VM_SUBTYPE_SEV) sev_vm_init(vm); @@ -1344,6 +1346,7 @@ void kvm_selftest_arch_init(void) { host_cpu_is_intel = this_cpu_is_intel(); host_cpu_is_amd = this_cpu_is_amd(); + is_forced_emulation_enabled = kvm_is_forced_emulation_enabled(); } bool sys_clocksource_is_based_on_tsc(void) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 29609b52f8fa..7f7d4348e85c 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -21,7 +21,6 @@ static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; -static bool is_forced_emulation_enabled; static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code, @@ -35,7 +34,6 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, vcpu_init_descriptor_tables(*vcpu); sync_global_to_guest(vm, kvm_pmu_version); - sync_global_to_guest(vm, is_forced_emulation_enabled); /* * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling @@ -612,7 +610,6 @@ int main(int argc, char *argv[]) kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM); - is_forced_emulation_enabled = kvm_is_forced_emulation_enabled(); test_intel_counters(); diff --git a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c index f4f61a2d2464..69917bde69dc 100644 --- a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +++ b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c @@ -13,8 +13,6 @@ #include "kvm_util.h" #include "vmx.h" -static bool fep_available; - #define MSR_NON_EXISTENT 0x474f4f00 static u64 deny_bits = 0; @@ -258,7 +256,7 @@ static void guest_code_filter_allow(void) GUEST_ASSERT(data == 2); GUEST_ASSERT(guest_exception_count == 0); - if (fep_available) { + if (is_forced_emulation_enabled) { /* Let userspace know we aren't done. */ GUEST_SYNC(0); @@ -520,8 +518,6 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow) uint64_t cmd; int rc; - sync_global_to_guest(vm, fep_available); - rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available"); vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER); @@ -551,7 +547,7 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow) vcpu_run(vcpu); cmd = process_ucall(vcpu); - if (fep_available) { + if (is_forced_emulation_enabled) { TEST_ASSERT_EQ(cmd, UCALL_SYNC); vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler); @@ -774,7 +770,5 @@ KVM_ONE_VCPU_TEST(user_msr, user_exit_msr_flags, NULL) int main(int argc, char *argv[]) { - fep_available = kvm_is_forced_emulation_enabled(); - return test_harness_run(argc, argv); } From patchwork Thu Mar 14 18:54:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13592669 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D825E762E5 for ; Thu, 14 Mar 2024 18:55:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442511; cv=none; b=vACsj/wht8a5OpcnyLO70ijr3fxhmkTAGQyHPJ7SrbPPfxWZhAqgpjdI343/Qp00bnKrjM/0s3g1+zMoqOG80JP4TKY5XUzUWXqDt7A3+wYNGt2wv8BTLHsN1fZqiQOZrXtaQm+hm03sA0wAZGW+LNiu/wyLvY9VAbUuvnWEywM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442511; c=relaxed/simple; bh=tB5vCpIDawJ2Zn4FC4jcvXThVZF9ir69az8ODScDTto=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iCcLITxg0/nVhCM1Dir+4mkCsmCnT0GXADHO7vl6aUiKEvMG4XGF2PHRywa9enygrW397wndibMyJFtpyi429kdehmU4AXMhELJD+vKaHTQ4cF8zWXIqN/XTla6/yjIC3nIDW8DLhjCCr9gj2Ex3BO6rPNj8WHaFUoXAeaGk1SM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u/efeT/8; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u/efeT/8" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc647f65573so2187724276.2 for ; Thu, 14 Mar 2024 11:55:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710442509; x=1711047309; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=47LgpMdPt6+0tTjJu46B3a8kb1KpMWWpVGbXQ15D2KA=; b=u/efeT/8yYhrqQANmjQ364UC3bwKLszKZFvp2uiVJTQ56QdbnR7Ds4plt4zOX+312V YuLvkdmdy5S2rm9gfZHMl667xt8g0Q6IDsbviZFB8whvOUjRiQTIU6XoUxZpACgn+PXR HsYw2BduettdbDDXzcxPfF788y/E2tzsaWoDx8tsGgiBQZYZ2oqr2XKcC4nhfWIrVnN4 RQnTlQppRnkXH4r+c/+XMIleOaRuIMzq6lAkGHUbLTHk8YAJD/UdjrGAj47nvaiPSLoD llWPrNEeqrMVlLWTISO/aaoeUy3oKReuxPKsOyjRMlAGofhMHvkBw4RGsDf7NWfqLB33 u7Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710442509; x=1711047309; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=47LgpMdPt6+0tTjJu46B3a8kb1KpMWWpVGbXQ15D2KA=; b=B8n+gXf/ZLHLM+hBrP7xaJfwoZ9vRetGTywj/Y9Nm1p2m/iG/mGFcyO7UjP1KnOdF1 GOBVQi7m1Ql3ivDtjp4eIauXtyq/mGG5EnOAw+sQFVYB7Gf+tQM0FWMylNUWwgJxAkYO A1Gijt8c0chLotupblIBQG6bnWslkhmzp87s5wmBt/u9/x+nfKjFmTlPSMJav1T+ByG/ Pzpi+5TTYsdt4h7tbszbW96NC1U3RAkUO3cwkHZ6B8WFRjv20V5dTdomtVRbDJ5OrCQn BnE9kKHS4Md/7h5zECKwPkh7ThwUftkrchRCkfYaTlEAk3bVCqCzjQsZdCkE2Rgd0cAR y3Wg== X-Gm-Message-State: AOJu0Yy/wX/l+aGQIkrkt0cpjPpYkbYWTn1YZpxx1E4g+slabZJ+ovyP F+REwOedL6jGvicoVDdUqP0kt5y7EdIKEENDdX1+X1KUp9pZUlBHfn8Ml/lx5+i2uliaPKq4Sb7 Kkg== X-Google-Smtp-Source: AGHT+IFG1gwNWejmEeXNYIbGT5+ffFKp9NP8xWrD+Xcx7i4gHHpwj3Qo+aaK7DVPCOkh88w9LweM7mdd1S0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2848:b0:dcf:b5b8:f825 with SMTP id ee8-20020a056902284800b00dcfb5b8f825mr743146ybb.0.1710442508932; Thu, 14 Mar 2024 11:55:08 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Mar 2024 11:54:57 -0700 In-Reply-To: <20240314185459.2439072-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240314185459.2439072-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240314185459.2439072-5-seanjc@google.com> Subject: [PATCH 4/5] KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Introduce a macro, vcpu_arch_put_guest(), for "putting" values to memory from guest code in "interesting" situations, e.g. when writing memory that is being dirty logged. Structure the macro so that arch code can provide a custom implementation, e.g. x86 will use the macro to force emulation of the access. Use the helper in dirty_log_test, which is of particular interest (see above), and in xen_shinfo_test, which isn't all that interesting, but provides a second usage of the macro with a different size operand (uint8_t versus uint64_t), i.e. to help verify that the macro works for more than just 64-bit values. Use "put" as the verb to align with the kernel's {get,put}_user() terminology. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 5 +++-- tools/testing/selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index 525d92dda646..e3f67f4584fb 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -108,7 +108,7 @@ static void guest_code(void) */ for (i = 0; i < guest_num_pages; i++) { addr = guest_test_virt_mem + i * guest_page_size; - *(uint64_t *)addr = READ_ONCE(iteration); + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); } while (true) { @@ -117,7 +117,8 @@ static void guest_code(void) addr += (guest_random_u64(&guest_rng) % guest_num_pages) * guest_page_size; addr = align_down(addr, host_page_size); - *(uint64_t *)addr = READ_ONCE(iteration); + + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); } GUEST_SYNC(1); diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 3e0db283a46a..9110efa1bc12 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -610,6 +610,9 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); +#ifndef vcpu_arch_put_guest +#define vcpu_arch_put_guest(mem, val) do { (mem) = (val); } while (0) +#endif static inline vm_paddr_t vm_untag_gpa(struct kvm_vm *vm, vm_paddr_t gpa) { diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index d2ea0435f4f7..1ba06551526b 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -171,8 +171,9 @@ static volatile bool guest_saw_irq; static void evtchn_handler(struct ex_regs *regs) { struct vcpu_info *vi = (void *)VCPU_INFO_VADDR; - vi->evtchn_upcall_pending = 0; - vi->evtchn_pending_sel = 0; + + vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0); + vcpu_arch_put_guest(vi->evtchn_pending_sel, 0); guest_saw_irq = true; GUEST_SYNC(TEST_GUEST_SAW_IRQ); From patchwork Thu Mar 14 18:54:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13592670 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A17E9763F6 for ; Thu, 14 Mar 2024 18:55:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442513; cv=none; b=Tvg153j/HNu39hv0Ls2LpBqMdFEtAgw4ABssAwNrpAFwR43CHsVqDgc3iRxjOIcm9rWCexKcP3yjtHziTAk+6C+LTZzuZTYodA3jlxf8/pUwYiqmQLCPbKvJ0UmTwgQW0+kHAZWaYMhs5tp+fUxBr9QHIo1Yz5ffpOr37mCztig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710442513; c=relaxed/simple; bh=iOEWUnvMcKbJVvSSFCN/LBW8KrePKwutjmximUKTeCI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OmJp/TngSSHy6rfFADVEggSsczBH8NwBFed00U4P9uva33gj2WfUKTq5jSiP9Z0MOkJK5g8Ad0mTMYOpnwiown7ZD7bBkCNCH44K2pKzEuokr8pGv10EH4t+BtNZ7JsFZeW578C6z+CLCBnfpfgge9903xbXB7Lt/9i7Yp/hTH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=I7h1zFPT; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="I7h1zFPT" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a20c33f0bso22916337b3.2 for ; Thu, 14 Mar 2024 11:55:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710442510; x=1711047310; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=SXV3IRJxaCu6BKITriq5GONv2Stn6rDuSzNVATcf3ok=; b=I7h1zFPT+IO1DoZIfs1y7xma6seA+BGighd9b9wjtK5bip3FDL3/AUY8EEI8MbYv34 PAaQkuRohSboGdw8TvMn0JeRLU92MpnawPUpFWgg+oINmbAARSCpZmBQxHoFQVWmzt/q UmcszwU+s6kYQzvRQuOp4Ym9GaGWN/XyUbK74Dl5EAYq+h8MAQozpwJYPZTXoA8Ro0KM 8zt+vlpbVRP4GHq6pMKyDcxdj5aSKzTP4KjQl+65QqALtDpD2mynsusb/4YcOixOsWmv VAiSREdj9ZEYNaK7wONQOSMKuk2pAaHrGnRB/whJkgwjybHU63ftffEccDWNOrPGLi8L efug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710442510; x=1711047310; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SXV3IRJxaCu6BKITriq5GONv2Stn6rDuSzNVATcf3ok=; b=wvqeBFBSigaGdiZK1qoggNv/0Lb2oaligy+ZvFyM/VSPHQP3O5f1B8DUYg0LwqlnYT DkO1wWFUtbonV4RK1am5xMCrHg8d0jwJOmyTvixAVcFGvuIw4yHlJxxyh56BGznHjakV aoUbM+PzLGwcgvvtEUQ/DVnOOk6TYKlPA3ZJkUQruH68rbocI8yW2zCDJHJrg5OD9NNu /xxKcqoLb1gYK+i8TlMTjOCwv32ovS5LptFGR5ozAN4/K/5S3xNFpBHQTKvj3kJjVyO4 1N+Un1FGJhq6AFVNv8KEzpp1Q7pHQt2GYx/ArjGmOMP12z0xazjQrj8i8OnWnb5cf2uH Grbg== X-Gm-Message-State: AOJu0Yxp4xjzUYjIxG6FBnHHEcr4FvAvmUpMc3TBttx8bLdl+k1flAS1 nT1YT0CTpnJNOdr7AVC9Wv+z6NMiQ8M0uO2vP69QneNhyH46zJWbwoyW6P8Kqyox1lMeA0z3uIQ Q+w== X-Google-Smtp-Source: AGHT+IFNVe74LE8W8P7Yaw4D0J9QhZPSNUyoxo07UooabXpjGUfUAjGuX8DB8V93Lg5hvCPkskdsrUZukWY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1004:b0:dc7:5aad:8965 with SMTP id w4-20020a056902100400b00dc75aad8965mr106806ybt.0.1710442510714; Thu, 14 Mar 2024 11:55:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 14 Mar 2024 11:54:58 -0700 In-Reply-To: <20240314185459.2439072-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240314185459.2439072-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240314185459.2439072-6-seanjc@google.com> Subject: [PATCH 5/5] KVM: selftests: Randomly force emulation on x86 writes from guest code From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Override vcpu_arch_put_guest() to randomly force emulation on supported accesses. Force emulation of LOCK CMPXCHG as well as a regular MOV to stress KVM's emulation of atomic accesses, which has a unique path in KVM's emulator. Arbitrarily give all the decisions 50/50 odds; absent much, much more sophisticated infrastructure for generating random numbers, it's highly unlikely that doing more than a coin flip with affect selftests' ability to find KVM bugs. This is effectively a regression test for commit 910c57dfa4d1 ("KVM: x86: Mark target gfn of emulated atomic instruction as dirty"). Signed-off-by: Sean Christopherson --- .../kvm/include/x86_64/kvm_util_arch.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h index 41aba476640a..d0b587c38e07 100644 --- a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h @@ -5,6 +5,8 @@ #include #include +#include "test_util.h" + extern bool is_forced_emulation_enabled; struct kvm_vm_arch { @@ -22,4 +24,23 @@ static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch) #define vm_arch_has_protected_memory(vm) \ __vm_arch_has_protected_memory(&(vm)->arch) +#define vcpu_arch_put_guest(mem, __val) \ +do { \ + const typeof(mem) val = (__val); \ + \ + if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \ + (mem) = val; \ + } else if (guest_random_bool(&guest_rng)) { \ + __asm__ __volatile__(KVM_FEP "mov %1, %0" \ + : "+m" (mem) \ + : "r" (val) : "memory"); \ + } else { \ + uint64_t __old = READ_ONCE(mem); \ + \ + __asm__ __volatile__(KVM_FEP LOCK_PREFIX "cmpxchg %[new], %[ptr]" \ + : [ptr] "+m" (mem), [old] "+a" (__old) \ + : [new]"r" (val) : "memory", "cc"); \ + } \ +} while (0) + #endif // SELFTEST_KVM_UTIL_ARCH_H