From nobody Sat Feb 7 18:29:15 2026 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 402B574E3D for ; Thu, 14 Mar 2024 18:55:04 +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=1710442506; cv=none; b=rGqoMUL7KRjKnrEOIvCQh5p8YgYf0m5pTD/FIcPILa6IK20K0vTL8u2Qh28YPm34yPlgm/++AD51Bq7fGQv9/zNmaKrCYIjm8plIpp+R4FuOFfIB529wYRsmuPHCVuHhQhalqfS1r4G98T4PKrf5cT3SHVn2OdYJ3gLvowTxj+U= 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.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="lh6mZLRY" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcbee93a3e1so2140349276.3 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=GacvYu24jMtrD7EucFuvYLET4DykDHogXbJZjRoXZA6FZcDaA9rDQRijzxzRJQScNS gcWpMdPg87kcbpDVlZsPOhF9oHkRSGqlKwnb/SYVZyLJwKbZvDwHa87dt07BcJsRxBZf fR2LWJL2bRXRBIK/9QyFqAhrzLOZv0gICRS3NpjqwW/DXlaPdcZTFbPbIypFRv2pTlph 73XXJPtVnoj+CfvrDMYwJAOrdgTxBr2pJz0OdSS/cmsBTQluIOVgjudY53X758gfxcJK 7Ryh27DFLCk1E/MMlHImWcyoFnnFuW93cMN21pbAbzPQnkvrxPx8hjJlfA4iL4M8JdA3 fK2Q== X-Forwarded-Encrypted: i=1; AJvYcCX/Q7xVAyBlMx6Ogc1akN6tSsEBxe1RN+qvlmjBC0I6eKr84V2RBKdsCP8i8SEcYC4uQfT93vYjL+tEXG7My/snpQMBEdZILzcm0muY X-Gm-Message-State: AOJu0YyQAJqmw5Gai2E3KZ3/Tecd65I1U4xNTk6gn4B1neoPMG/2/Jm9 Dp4sP/UDB/fY1AoEdb/Mx6gDIgLpsf/obsDbi2xOdRYlbMPqkcDYc/p35EGeEFDst3tepV2jjhT Qdw== 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: linux-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/test= ing/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; }; =20 @@ -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); =20 - 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); =20 guest_num_pages =3D (nr_vcpus * guest_percpu_mem_size) >> vm->page_shift; @@ -346,11 +343,13 @@ int main(int argc, char *argv[]) .partition_vcpu_memory_access =3D true, .backing_src =3D DEFAULT_VM_MEM_SRC, .slots =3D 1, - .random_seed =3D 1, .write_percent =3D 100, }; int opt; =20 + /* Override the seed to be deterministic by default. */ + guest_random_seed =3D 1; + dirty_log_manual_caps =3D kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); dirty_log_manual_caps &=3D (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | @@ -395,7 +394,7 @@ int main(int argc, char *argv[]) p.phys_offset =3D strtoull(optarg, NULL, 0); break; case 'r': - p.random_seed =3D atoi_positive("Random seed", optarg); + guest_random_seed =3D atoi_positive("Random seed", optarg); break; case 's': p.backing_src =3D parse_backing_src_type(optarg); diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/s= elftests/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; =20 /* @@ -115,13 +114,12 @@ static void guest_code(void) while (true) { for (i =3D 0; i < TEST_PAGES_PER_LOOP; i++) { addr =3D guest_test_virt_mem; - addr +=3D (READ_ONCE(random_array[i]) % guest_num_pages) + addr +=3D (guest_random_u64(&guest_rng) % guest_num_pages) * guest_page_size; addr =3D align_down(addr, host_page_size); *(uint64_t *)addr =3D READ_ONCE(iteration); } =20 - /* 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); } =20 -static void generate_random_array(uint64_t *guest_array, uint64_t size) -{ - uint64_t i; - - for (i =3D 0; i < size; i++) - guest_array[i] =3D random(); -} - static void *vcpu_worker(void *data) { int ret; struct kvm_vcpu *vcpu =3D data; - struct kvm_vm *vm =3D vcpu->vm; - uint64_t *guest_array; uint64_t pages_count =3D 0; struct kvm_signal_mask *sigmask =3D alloca(offsetof(struct kvm_signal_mas= k, sigset) + sizeof(sigset_t)); @@ -540,11 +528,8 @@ static void *vcpu_worker(void *data) sigemptyset(sigset); sigaddset(sigset, SIG_IPI); =20 - guest_array =3D 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 +=3D TEST_PAGES_PER_LOOP; /* Let the guest dirty the random pages */ ret =3D __vcpu_run(vcpu); diff --git a/tools/testing/selftests/kvm/include/memstress.h b/tools/testin= g/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 mod= e, int nr_vcpus, void memstress_destroy_vm(struct kvm_vm *vm); =20 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); =20 void memstress_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct memstr= ess_vcpu_args *)); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/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; }; =20 +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); =20 +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/sel= ftests/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 @@ =20 #define KVM_UTIL_MIN_PFN 2 =20 +uint32_t guest_random_seed; +struct guest_random_state guest_rng; + static int vcpu_mmap_sz(void); =20 int open_path_or_exit(const char *path, int flags) @@ -433,6 +436,10 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint= 32_t nr_runnable_vcpus, slot0 =3D memslot2region(vm, 0); ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size); =20 + pr_info("Random seed: 0x%x\n", guest_random_seed); + guest_rng =3D new_guest_random_state(guest_random_seed); + sync_global_to_guest(vm, guest_rng); + kvm_arch_vm_post_create(vm); =20 return vm; @@ -2306,6 +2313,8 @@ void __attribute((constructor)) kvm_selftest_init(voi= d) /* Tell stdout not to buffer its content. */ setbuf(stdout, NULL); =20 + guest_random_seed =3D random(); + kvm_selftest_arch_init(); } =20 diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/se= lftests/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; =20 - rand_state =3D new_guest_random_state(args->random_seed + vcpu_idx); + rand_state =3D new_guest_random_state(guest_random_seed + vcpu_idx); =20 gva =3D vcpu_args->gva; pages =3D vcpu_args->pages; @@ -243,12 +243,6 @@ void memstress_set_write_percent(struct kvm_vm *vm, ui= nt32_t write_percent) sync_global_to_guest(vm, memstress_args.write_percent); } =20 -void memstress_set_random_seed(struct kvm_vm *vm, uint32_t random_seed) -{ - memstress_args.random_seed =3D 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 =3D random_access; --=20 2.44.0.291.gc1ea87d7ee-goog From nobody Sat Feb 7 18:29:15 2026 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 2B84D76020 for ; Thu, 14 Mar 2024 18:55:05 +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=1710442507; cv=none; b=oJh2pljGh4Wdl125qSMdJCu2AyBK23x2lxeteYw5U8NLIX3IRsA22tdCGiPoZ/HuZdEcecZNlh2jgqjHp92LVLxUgy025yemoe9z9bk7x3EFE3WmQx5oGnr4J7DwywOA8SNqDjJ+BqyFtAD2ILYg8fQ1dB5qZn/jrIHrkri/z1Y= 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.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="FonB+BZt" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so1909491276.0 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=MzoXBkAjwb0Hk8Vag6InRg5yLjMOQK3hRXNedzA2GI6z+s3xvPCQjcCPvwf/WnArwx Qvv3lobiZuomd9IumsefvyNPT0/QTMeq22yuOTlAON2/2uFGSW5PiQ+ahTwmyvlf3lx0 LzUxwqIGb2mqaA+VUJ5V7ZKKGgCIotSr8TgEofN+U22OKBS211+flCBB3eM4OtqnWSHr B8kvjuLB87XUlFWtUWIC5RhLbvkn0YBKR00vF0Suwoe6+niOCHFN06ALOYJ0eM9epQGx iAn4LB4oaTKP9Eo5DUsHwsTwer29syja/wzKs2NpT6vdqf30vMgAGsea10+Jwqr8Pz0B O20A== X-Forwarded-Encrypted: i=1; AJvYcCWwYe6JFWk3NdrgAgsrT1q2xKtI7wliCV1CZRU0D8TEyyP1cUwyYyLGaoTHc7QxKVsLFKxB1vLcOrgltyZVfYbdiVEeFiK8yxGvFSb5 X-Gm-Message-State: AOJu0YwuUK+KbaFyWgp6/doRb675LTTVCzxk8wasmFYizIZNWfmynw1M 0YUOJbcEe1v/RWKg41ueolZkGj/sy759uiqzlWk6ayISyKBqMJLN61H808P/KB0mqHPxX1xlZwq lMA== 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: linux-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/testin= g/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); =20 +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/se= lftests/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) =20 addr =3D gva + (page * args->guest_page_size); =20 - if (guest_random_u32(&rand_state) % 100 < args->write_percent) + if (__guest_random_bool(&rand_state, args->write_percent)) *(uint64_t *)addr =3D 0x0123456789ABCDEF; else READ_ONCE(*(uint64_t *)addr); --=20 2.44.0.291.gc1ea87d7ee-goog From nobody Sat Feb 7 18:29:15 2026 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 CDB3776045 for ; Thu, 14 Mar 2024 18:55:07 +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=1710442509; cv=none; b=tGN80eahdMoiYPjzVSUhHdjBV+xPKmC+vTeYkxH+O8hP0/bYNy6nGw2hPY+54Ppny0OxrKqB2lhIQ3iaCNVSzaVcoKG7OSN8+jUMl8Ac37XeHM0EltmWIJcgSj2UvIspuQr/nFOtbJBnnZ6nh8oBklsAFlbosrEwEfJ2Je5kVoY= 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.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="01U4SkH4" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcbee93a3e1so2140435276.3 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=J5FYFKyXPRgDjrRVm7TrPeRX5iKqTGunU51wJQktknylV5qb2zINozKAhEZyF1I9EE XCL8DW5tlQ+8LoJezLMJtMtgNOt5HBj/Anu6O76kwNjT9KyEt4TYT7hu4t/EjvQEzwgi N6ce5E5w2KHJyJXAJEx9aZLGNxw4GJmB8uyQ4YeAIcx/m34qn25QYrT1+E3PfBIaX7ko 1OBN+PX5LDr5YAesvm+zPk+j0Cb7Uup/hIlIHNF+T+TxlXlJ1KRKlxVNLIY+VyT3jhlW ubSxO6xa5duVLW414gIPF91vZyzJa+DyyNHzvonYtJnoSoFfHgAYJ5MovcylxbR3MRMq +T+A== X-Forwarded-Encrypted: i=1; AJvYcCWvDYeLwdv5jEaRP1dwua2n4dO69PEK9HN7EPW/K4Inph4evBIWGk1cLV0zar4O4qFSsGg+CVOVTNVkRu3H4W99A2937VChKMUWiQHd X-Gm-Message-State: AOJu0YxYt/EJplN7KliUAE1ee5PhzchtPdUbPN9/S/ufqIAII8PaxX5K 316Yi8GlgGx/peU+Nnmhx00lSC99JjEKubnQQg/e/+F4XvmDLhtuDSYE13JvNm55f83BeJAfbud X+w== 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: linux-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/t= ools/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 =20 +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/tes= ting/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; =20 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); =20 if (vm->subtype =3D=3D VM_SUBTYPE_SEV) sev_vm_init(vm); @@ -1344,6 +1346,7 @@ void kvm_selftest_arch_init(void) { host_cpu_is_intel =3D this_cpu_is_intel(); host_cpu_is_amd =3D this_cpu_is_amd(); + is_forced_emulation_enabled =3D kvm_is_forced_emulation_enabled(); } =20 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 @@ =20 static uint8_t kvm_pmu_version; static bool kvm_has_perf_caps; -static bool is_forced_emulation_enabled; =20 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); =20 sync_global_to_guest(vm, kvm_pmu_version); - sync_global_to_guest(vm, is_forced_emulation_enabled); =20 /* * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling @@ -612,7 +610,6 @@ int main(int argc, char *argv[]) =20 kvm_pmu_version =3D kvm_cpu_property(X86_PROPERTY_PMU_VERSION); kvm_has_perf_caps =3D kvm_cpu_has(X86_FEATURE_PDCM); - is_forced_emulation_enabled =3D kvm_is_forced_emulation_enabled(); =20 test_intel_counters(); =20 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" =20 -static bool fep_available; - #define MSR_NON_EXISTENT 0x474f4f00 =20 static u64 deny_bits =3D 0; @@ -258,7 +256,7 @@ static void guest_code_filter_allow(void) GUEST_ASSERT(data =3D=3D 2); GUEST_ASSERT(guest_exception_count =3D=3D 0); =20 - if (fep_available) { + if (is_forced_emulation_enabled) { /* Let userspace know we aren't done. */ GUEST_SYNC(0); =20 @@ -520,8 +518,6 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_cod= e_filter_allow) uint64_t cmd; int rc; =20 - sync_global_to_guest(vm, fep_available); - rc =3D 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_cod= e_filter_allow) vcpu_run(vcpu); cmd =3D process_ucall(vcpu); =20 - 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); =20 @@ -774,7 +770,5 @@ KVM_ONE_VCPU_TEST(user_msr, user_exit_msr_flags, NULL) =20 int main(int argc, char *argv[]) { - fep_available =3D kvm_is_forced_emulation_enabled(); - return test_harness_run(argc, argv); } --=20 2.44.0.291.gc1ea87d7ee-goog From nobody Sat Feb 7 18:29:15 2026 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 C1F0174E26 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-dc693399655so2282434276.1 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=CBIQHawXfzg0ZAFmIlpkkqO7nj19ukbZ82FQ6Wv8bivS4gsy+jjBspGMqsHU+75rBI c1DbupcAlqKNccBC2byVKPqil7GahtfQ1rmRzLSi8ISmm1IRAPFq5wCJFUBr7WyLGCtn oToia6jtdTQYCOTO8KKnIb7LQ2D8sJ0qayGf6/4Dw3TYSe9kYXrBau8U/b/b6cDIotrK nV2GzdDyiw7bSxx3oHOgrLACZ7oWRHVrAkEOQQrHYgehrVlu6h31y3R3DrPyUkHXy4Ld H+3CgLZbJnQOEr+pN1/KQi0v+NwnksDita1KqaD9vgpQdDqGmLRsaXkTumYNLzFiPf4R h4Mw== X-Forwarded-Encrypted: i=1; AJvYcCV4hilsmmSK33Ouvt8M5D21hqMtbzJ8jz54lVzhXScNqZedQFUPwWpGwWVQt2u3CW9rxDKdCv8BvEctjlqxjqPCrL25SK6w6toIrm7y X-Gm-Message-State: AOJu0YzwMes1/eZSLIx0uaEAIdZJGDfUaHiVQAlVpXfkmQV660apbkHu RMXSgOx0TcUKzlpH7YFoccaO7HdfwSKzM9ic2XqRY9/hu4+mtZ5gHDWC5dO+dGwlZCDw0WLLmTC 9pg== 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: linux-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/s= elftests/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 =3D 0; i < guest_num_pages; i++) { addr =3D guest_test_virt_mem + i * guest_page_size; - *(uint64_t *)addr =3D READ_ONCE(iteration); + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); } =20 while (true) { @@ -117,7 +117,8 @@ static void guest_code(void) addr +=3D (guest_random_u64(&guest_rng) % guest_num_pages) * guest_page_size; addr =3D align_down(addr, host_page_size); - *(uint64_t *)addr =3D READ_ONCE(iteration); + + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); } =20 GUEST_SYNC(1); diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/te= sting/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); =20 +#ifndef vcpu_arch_put_guest +#define vcpu_arch_put_guest(mem, val) do { (mem) =3D (val); } while (0) +#endif =20 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/t= esting/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 =3D (void *)VCPU_INFO_VADDR; - vi->evtchn_upcall_pending =3D 0; - vi->evtchn_pending_sel =3D 0; + + vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0); + vcpu_arch_put_guest(vi->evtchn_pending_sel, 0); guest_saw_irq =3D true; =20 GUEST_SYNC(TEST_GUEST_SAW_IRQ); --=20 2.44.0.291.gc1ea87d7ee-goog From nobody Sat Feb 7 18:29:15 2026 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 96F00763F3 for ; Thu, 14 Mar 2024 18:55:11 +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=1710442513; cv=none; b=MBazmkeY/6+VyLFmyrb64HSjBBYFxN9kkZsnW0/jrGlrVCyuVvcdbWdnfWYNKOZoeOuvIu3+Yum1jwOPJinrrL5HI1F/mRw8N7jerzYmLChpZnKSzWaZ2fa5eKb0vRBAtqBfaPDeXxl8qO3nhpHW/yiORHj4qtKSquVdhlSNddM= 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.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="I7h1zFPT" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ceade361so2107418276.0 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=YKCChy+FKMBN9KsrAVMsBjcVgG37bYt4xpvpQ4elI0Q9PG9VAEumqD3j+qiiXLC+e9 9Cz7r5wCNCur0nho8Pk+07uS/nYcTYHCcE9piV7Z6e0+QcUeQ1w6K/vyez6poyoJLCoY L+DnP0ZEsSEgnXkld1XgcUzv4ZC7WIbl/KCZEBrcBkEccwEv+5XuBI0x7kLV4y987p8Y Z2XyIluDXxapF6lfrK7ouvECxaV5JDCOes8v3Q4DQWP6qkHRUmfjhF47j5hjU7XRmvha UY8L7yWPSLiJk0BHeQeOM/SmX1Z4vQzmPGhsVp+MGeLQqPRcLSzDZaFc3OrlN1A43h/k eFDg== X-Forwarded-Encrypted: i=1; AJvYcCU/s43IWNy3OUM1/YAXfPetQK6qFaXL9Z1J6lsRBYZmvAo65GVlIXovKN9Da3nwE0FQKuUNo6xd5fkzjBPzndz8z02m6LTivlk444Ib X-Gm-Message-State: AOJu0YwpD7YHX6q1OZuaGtZ3BN/L59r3YJmfRcIqbngwbQ3qm1Q2r6G+ XV2USZWDgya3zstBS1VDJ9uYfoqy6IV4ldubWzQvOExExcPj7vNi/OC0cg5HHkoqYysBtx1gaYZ uzA== 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: linux-kernel@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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/t= ools/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 =20 +#include "test_util.h" + extern bool is_forced_emulation_enabled; =20 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) =20 +#define vcpu_arch_put_guest(mem, __val) \ +do { \ + const typeof(mem) val =3D (__val); \ + \ + if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \ + (mem) =3D val; \ + } else if (guest_random_bool(&guest_rng)) { \ + __asm__ __volatile__(KVM_FEP "mov %1, %0" \ + : "+m" (mem) \ + : "r" (val) : "memory"); \ + } else { \ + uint64_t __old =3D 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 --=20 2.44.0.291.gc1ea87d7ee-goog