Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1238404rdb; Fri, 16 Feb 2024 09:12:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXJkduM083c5X5nDmkwY1AcMQZM7Szz6GdxG5TU4M0jUoAsc5xTySLxWSXHvj53+HbtFGA5IQ5+jNN7623mNHr9MHX3S1vKUm1KdCOtcg== X-Google-Smtp-Source: AGHT+IGwaZwIZ7OKzozwZYqXNYMlJf0Y1rStCn+D2WdeSdktfM+ZIcS0aeWW765kSk4yDaAg+91b X-Received: by 2002:a17:902:e94d:b0:1db:8eae:b98e with SMTP id b13-20020a170902e94d00b001db8eaeb98emr5822319pll.38.1708103540688; Fri, 16 Feb 2024 09:12:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708103540; cv=pass; d=google.com; s=arc-20160816; b=b6r7yJrkeB6amzkg/IKkmLPhc8kyc+vLYuqVplgaiEYxbSU3nWSeL2FjSVIpgJqcZG EOpjZxSvfp8hA2tUmGnClQPH5RVLqHRjWJqv2w5GM1TSHpAoQSza3bgANox03uBYz0iE 5t2cMUjMv0df+Vu5lJzqDd4PZrP3nS58ifklL4UB8E3Mt9AG2nIKTamVOU40Yg/yx8L9 sRIzCfRqLMU8FKuh8e+E5yphKljrhawrcL2ngIP+0+jwPGs03fzJ9vI81cBaPeVeag4V D5ASulzoitWcNpAM4qPlxLil0w6UA4V5+NVzRsdVt9WqZrWxATDwt/1fwAaQB2YpAU9W vE+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=jSjwEV6mZP9dDWBaOFoNSK/PtiDDO4b/rWPQcC9pvsg=; fh=taDT4oPSmDTodU8JpzCAflFp+fcsgiiWyAjFgyuL8q4=; b=Jlj5W9Y+YKSkBC5CDiwrTaKulIBjT1IA2ozjLMKZh07JciRzV7hMZFVukfBA3FfdNU 0J4d9Qj5mqML/1OXHJ6DY+8mx1Z1Osvh8Twn4WctjoATd54ml0DstllwD85MkQpMqGm0 Y1GOyKUKMMrE4ryCEnANlxm+NYnVk81ZN1syHmr/hUUKENFEE3Kb/6PSA++HDQ7J3RFk wVT9b8XwD5maTVIaU2kwdyMXhjkvw9mdeAV0Im+0E9jm3HekED03HOttgtLaIMgfvQ2k 58OMnFG5bkwZ6Tfh/KAFRXXvsJXSgGxk4BszQVwaGhZPL0TapohTUXYs/mA9OoD8eHJ/ FyPQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JFhP09vz; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-69008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ix3-20020a170902f80300b001da2a3e4c55si131796plb.216.2024.02.16.09.12.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 09:12:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JFhP09vz; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-69008-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69008-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id B5019B20EFD for ; Fri, 16 Feb 2024 17:03:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D51F1130E3C; Fri, 16 Feb 2024 17:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JFhP09vz" 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 61CCF2E641 for ; Fri, 16 Feb 2024 17:03:34 +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=1708103015; cv=none; b=m3eBdVOJEaiBH3Z5mIWOm8x//Gi8YWaqH7Lvho4bUtA81UYuJcbXtww1V6I1R1Dbb2V5YyjKs0kdLbwft+01xfsjUAFerwzPyp4rKF+6Yl2+uicEd+YJI5TfQhDFO++0yk02g7ZajcB5s5py+lh7idY8e3uhuZ36Ju+9vR0V/8Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708103015; c=relaxed/simple; bh=x+OyUheZkOLmrmxRXV2DwJisBsuE5dlS0nGw3qHzNmA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hRnJHaCQIaudr49azIonyheOTvEdXoREj3n67JQDsW6WU7t5HK/5HRNcVzAEUN9elt5VdP5EEMIOzvMedir0+35A0ozVTAY3AiKLzON3C8BAg82Lz3OWtqHvIlXvj4RxLcaz0fSCDD6LOtwALClMAxuo+DrQ8fqx0SA3tH9191E= 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=JFhP09vz; 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-603c0e020a6so10713607b3.0 for ; Fri, 16 Feb 2024 09:03:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708103013; x=1708707813; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jSjwEV6mZP9dDWBaOFoNSK/PtiDDO4b/rWPQcC9pvsg=; b=JFhP09vzd8Ur/NChpd8ZrTmHN7LSXQyyGUDRWoXCv3UwIU/X8jYeyykWqKJMTLV7M2 DJyR4U481v9BB7G++dF4fFfhueGJrwYUtQbrg9q2KBvuT1fraKzxwywW7GDlYYvQxKzD 31T1Zbj0uMwLRMvLBhuzRsYgygaoGtgM5u6rBFCuKxlfhmMOzFO1bM0TLc4MxcUWLMtR CUv1jvPz6GASn6ROREA2xaBnErmRsjFRn+Xw7NG5VFA3iMjh93PboTHS3X0Ib54YDoxq NAkxzqFXFZTUdgY3g/jL0M2oz+1dLf6LRdHNbtdT7ROPsiOwBVzaFRa+kXgIWK9CEV+j cMLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708103013; x=1708707813; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jSjwEV6mZP9dDWBaOFoNSK/PtiDDO4b/rWPQcC9pvsg=; b=wRgvAFo14uAG7p4kyFiJ/ayFtpDNAcDojQK6ZnbRkkKBQFqQHEFladYm1QjepR/BwF 9l4GkBCP+lidabRC4KndqZwW0asywFrWY1/TY4GMoMQlccscnAM2wOsltskkxF89th/4 Z1Jdm0ZDkU18f0ObRujqy1ZyRR2az8HzaHV+LCWb0V14jpqaQb3RY4bsPSU4IgaS/kZz /JCs2EJktPLhggvXXn9D/su3M7AtACPTvFTmjXvCZeV0LUKlz++ypO470I4Hje3eldwd akycA4zDOHeWa8xawuOhUt7NsgAuuh96ALi46VPxDh7yQA/4EBxHuk88picTzeDPNKOf yOkQ== X-Forwarded-Encrypted: i=1; AJvYcCXYr6BnWiAKJSs5j0Ymqkr2vpAxB90qSvLxrMJCLiXI3iYrWLkeRNDtW+QsTwbfwvxOK6tcb8kE8NOulMcGYSMmo+lSP4FkmG3eLR2J X-Gm-Message-State: AOJu0YweOKqppQ7Mr6Vam4yxzWIYsKppyGTMzUayEaAPcKYEBGTMsN7p Ly5Qah8PdJ61KEeuakeHhqnI1VwQskftBMAsvsIwqFBVXaxsAiBddYgdnDNtwCpw1OiH9JalSp4 s8g== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:10c3:b0:dc6:ebd4:cca2 with SMTP id w3-20020a05690210c300b00dc6ebd4cca2mr197453ybu.11.1708103013199; Fri, 16 Feb 2024 09:03:33 -0800 (PST) Date: Fri, 16 Feb 2024 09:03:31 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240215010004.1456078-1-seanjc@google.com> <20240215010004.1456078-3-seanjc@google.com> Message-ID: Subject: Re: [PATCH 2/2] KVM: selftests: Test forced instruction emulation in dirty log test (x86 only) From: Sean Christopherson To: Oliver Upton Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Pasha Tatashin , Michael Krebs Content-Type: text/plain; charset="us-ascii" On Fri, Feb 16, 2024, Oliver Upton wrote: > On Thu, Feb 15, 2024 at 04:26:02PM -0800, Sean Christopherson wrote: > > On Thu, Feb 15, 2024, Oliver Upton wrote: > > > On Thu, Feb 15, 2024 at 01:33:48PM -0800, Sean Christopherson wrote: > > > > > > [...] > > > > > > > +/* TODO: Expand this madness to also support u8, u16, and u32 operands. */ > > > > +#define vcpu_arch_put_guest(mem, val, rand) \ > > > > +do { \ > > > > + if (!is_forced_emulation_enabled || !(rand & 1)) { \ > > > > + *mem = val; \ > > > > + } else if (rand & 2) { \ > > > > + __asm__ __volatile__(KVM_FEP "movq %1, %0" \ > > > > + : "+m" (*mem) \ > > > > + : "r" (val) : "memory"); \ > > > > + } else { \ > > > > + uint64_t __old = READ_ONCE(*mem); \ > > > > + \ > > > > + __asm__ __volatile__(KVM_FEP LOCK_PREFIX "cmpxchgq %[new], %[ptr]" \ > > > > + : [ptr] "+m" (*mem), [old] "+a" (__old) \ > > > > + : [new]"r" (val) : "memory", "cc"); \ > > > > + } \ > > > > +} while (0) > > > > + > > > > > > Last bit of bikeshedding then I'll go... Can you just use a C function > > > and #define it so you can still do ifdeffery to slam in a default > > > implementation? > > > > Yes, but the macro shenanigans aren't to create a default, they're to set the > > stage for expanding to other sizes without having to do: > > > > vcpu_arch_put_guest{8,16,32,64}() > > > > or if we like bytes instead of bits: > > > > vcpu_arch_put_guest{1,2,4,8}() > > > > I'm not completely against that approach; it's not _that_ much copy+paste > > boilerplate, but it's enough that I think that macros would be a clear win, > > especially if we want to expand what instructions are used. > > Oh, I see what you're after. Yeah, macro shenanigans are the only way > out then. Wasn't clear to me if the interface you wanted w/ the selftest > was a u64 write that you cracked into multiple writes behind the > scenes. I don't want to split u64 into multiple writes, as that would really violate the principle of least surprise. Even the RMW of the CMPXCHG is pushing things. What I want is to provide an API that can be used by tests to generate guest writes for the native/common sizes. E.g. so that xen_shinfo_test can write 8-bit fields using the APIs (don't ask me how long it took me to find a decent example that wasn't using a 64-bit value :-) ). struct vcpu_info { uint8_t evtchn_upcall_pending; uint8_t evtchn_upcall_mask; unsigned long evtchn_pending_sel; struct arch_vcpu_info arch; struct pvclock_vcpu_time_info time; }; /* 64 bytes (x86) */ vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0); vcpu_arch_put_guest(vi->evtchn_pending_sel, 0); And of course fleshing that out poked a bunch of holes in my plan, so after a bit of scope screep... --- #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) --- Where guest_rng is a global pRNG instance struct guest_random_state { uint32_t seed; }; extern uint32_t guest_random_seed; extern struct guest_random_state guest_rng; that's configured with a completely random seed by default, but can be overriden by tests for determinism, e.g. in dirty_log_perf_test 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(); } and automatically configured for each VM. 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); Long term, I want to get to the point where the library code supports specifying a seed for every test, i.e. so that every test that uses the pRNG can be as deterministic as possible. But that's definitely a future problem :-)