Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp799372rdb; Thu, 15 Feb 2024 16:26:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCULMJMroFZf25wadUgZbdlaEDrhGdO0Nis4ikj1Z2xJbuPQUg6qpmZnKA8s17ndw5P8grCCyeTCiIGm5uDJHUvxRadwBZV5qcfwm2RH8g== X-Google-Smtp-Source: AGHT+IGEadSmacPpIqTm+OaWKgg7brPsJhBPWMEyoJft47bwNxi2pFn3vrMJIa7Y7D1aUszPV/Bl X-Received: by 2002:a17:902:ccc6:b0:1db:8c27:c23f with SMTP id z6-20020a170902ccc600b001db8c27c23fmr4157544ple.19.1708043177304; Thu, 15 Feb 2024 16:26:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708043177; cv=pass; d=google.com; s=arc-20160816; b=yGTE125ebZeQmDMq4kp7dyiCOXlksFii/ApQaDWE/0yU/wYXz+uT1+eVzbc1Nlnv2V C8heJzypgtW/TY7wNXCGQrISuU3E5NC5YytwHhYkAs/OSZc9EsLhjb4GyN6VD2oc+Oju uqC7uVgfF7PuYbZE/MvtECaj8/M9eRtyzpF4AshfhojuXtzBRLgZPvVX2Go2Q+07SQqa W+A+fiGPpwsJEmSf+CQoBOrCxC+oWf99G6y65FwB0Lh+KN0z7kfd6YTyK0wTvKD3D40g Bb7GWVMrkTum5C9hQGtflSN3ZCsxdT/8hvIfBm26FBrG4dfOq1hgwLv1nrbON0zEIHcP LCfA== 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=1wMKCbOl3BdeBvCzqWlNlP+DPN47Ns+GARI7q0YKBYg=; fh=7dEQFbbbX4MeKW3st5BjPBxCsCsVnoKPf4F17JKyiFk=; b=KcPz/Fj/QK3E8dAmUtR1jYGKaqeraSI0/kOkTkUiaFg0u4ZfiatvYV8nGowSPJG4Fy 8Nm6+o0p1lQWRrIRmoj0iSLypWVRC8/rejepIycHbQ+ZTsM6NIc6ulxn/wjIL0tjIURm E7LRqDSsdJ6DfdMfD5M5GCxXcYw02pZYdT6f6BTJ9G1bwsJplfTSvL5YhyLi7/3KvlSE K1QUPbS1+AFGlN07V7HLcgd8yK64gN/wImy5R6cS15irsWyxOTsy6LgjZyq5ZEll42H3 Dv9Q4CT6rjP8LOTZVgoyCdPtJs8zTTEYLgaAXSqp83f8IrddCQwFlp4Aj8ha2DHN0UC0 z1gg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N8AwCKcm; 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-67892-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67892-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id mc14-20020a1709032b0e00b001db6f2bec7fsi1945720plb.588.2024.02.15.16.26.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 16:26:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67892-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N8AwCKcm; 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-67892-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67892-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DFD17283CFD for ; Fri, 16 Feb 2024 00:26:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91E1F53A0; Fri, 16 Feb 2024 00:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="N8AwCKcm" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 D777D647 for ; Fri, 16 Feb 2024 00:26:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708043166; cv=none; b=m4psIN4dSSU1aMels2GtkFtR4ypcW8QIXduvjGBrE+2c2paSFRt7jZqbuhPMC10pCeoP/FB+oKnoKOBJ18zA1Q+8s43XtVJcfUr/iPd5Vut7r08AC97sKFC3WxSyVyPQpi1kk0F9sT8BiNl0EP2AlCEoVw2CsL+Ax4qzc+hg7RE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708043166; c=relaxed/simple; bh=fJ5zoMZkCZXY/8D1JS0Siys+6fkFsartYZuMZcsT9/I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MzMZMx5RzAdVU0NsGAy1ktkbbSEJDVDTPdIQfJUv54ExIVJ+atlE1LbDUqzY0MRW8uWS2QRWjp2SwUpvQA3O6IF+0eBZclhuttbGqJyolCJbkTOaHLz2KOkxsib2SWNd6qSQ5d1ZxgHow6sNYsCCqdTitXpzuwJnQeocPaX5VOI= 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=N8AwCKcm; arc=none smtp.client-ip=209.85.216.73 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-pj1-f73.google.com with SMTP id 98e67ed59e1d1-298d4011c8cso1388841a91.1 for ; Thu, 15 Feb 2024 16:26:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708043164; x=1708647964; 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=1wMKCbOl3BdeBvCzqWlNlP+DPN47Ns+GARI7q0YKBYg=; b=N8AwCKcmMmJrs0ae8k6t7/eOjb/42F1ZaV0yA2dzInKmMPaUxu8viJOx3fQnV6xYr5 RgTvzLB+VJ2vqQWK3tg2aR5PH67tPwrVrC07Db5lGl2xk5RbeT5oxpxTYkjnR/aFnI6E tQK1PEVUmwWZVsacFouWpxqdvqT5K39u1F2Ov8zsyyZJFi/yx8BP0Guf/TJuwKubHthv ycYaz+ICEob1AfWBKEQtsz+N+aJ9nteU/4YqAKc2Px78UPWcxroJobXz/gKoHLjJEU/4 ePXijLn46Rsvg6lLBWN7lWp0rehHRi1w/YcYq5LxnUA0/xXnGXuNk6ZeJherDj/KTfpr Z/vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708043164; x=1708647964; 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=1wMKCbOl3BdeBvCzqWlNlP+DPN47Ns+GARI7q0YKBYg=; b=DgwKoDPiWqlhzS1B/stmYpNwpgB13srmCsl07DDNQagIRvHkfLgmJv/aIthBiHEo/u i3GshEAArcalWw7x2QFoM3JTZ/zsups0yk5x2/n/DSiA3/WEM5djA7bU4IYESu5NGNDl FCBy7CpIE2/gCWrNP87bJn0Nbrp5efO/oP/WoELWh6U9x8z6+N/VnZwmnliM87MS2H/n UAAMYSv+RvXNbuf//mDB4oZ/zEDvHuRhucqb2thCaoNJPHIqurB9y/c5ccm4pp/FvScQ rMtxLm7w9t9u9ba9OOni8E/QeN59C+1bGqai/HcK43Ek7HLTgRLN9ItqN1liH4F0Q0Nd DhKQ== X-Forwarded-Encrypted: i=1; AJvYcCWtP2f6iDfYA8AFN7qsVXtaoW4ZyV0W8P6NTE1MxaHheFc+MdMyUvmUeEzf1v/5Rst7yPkJT3kDEssNWci1kCDJLaCpzIfgpW82r3s8 X-Gm-Message-State: AOJu0YyRm6xIVgXWHKblcbF4vRVg6I5N2xsmiPBn1cuAwad+gHG7AJ6W 5fOvTwt9GPBcZ8O/ceTdHNB+LTujGeOW8yZTNwHgAUTfjUQ48fQxfRR54Ye5Sjb7Vb24v+YQgKG ruw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:2e0f:b0:298:9767:a0cb with SMTP id sl15-20020a17090b2e0f00b002989767a0cbmr52656pjb.2.1708043164115; Thu, 15 Feb 2024 16:26:04 -0800 (PST) Date: Thu, 15 Feb 2024 16:26:02 -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 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. Actually, I take that back, I am against that approach :-) I was expecting to have to do some switch() explosion to get the CMPXCHG stuff working, but I'm pretty sure the mess that is the kernel's unsafe_try_cmpxchg_user() and __put_user_size() is is almost entirely due to needing to support 32-bit kernels, or maybe some need to strictly control the asm constraints. For selftests, AFAICT the below Just Works on gcc and clang for legal sizes. And as a bonus, we can sanity check that the pointer and value are of the same size. Which we definitely should do, otherwise the compiler has a nasty habit of using the size of the value of the right hand side for the asm blobs, e.g. this vcpu_arch_put_guest((u8 *)addr, (u32)val, rand); generates 32-bit accesses. Oof. #define vcpu_arch_put_guest(mem, val, rand) \ do { \ kvm_static_assert(sizeof(*mem) == sizeof(val)); \ if (!is_forced_emulation_enabled || !(rand & 1)) { \ *mem = val; \ } else if (rand & 2) { \ __asm__ __volatile__(KVM_FEP "mov %1, %0" \ : "+m" (*mem) \ : "r" (val) : "memory"); \ } else { \ uint64_t __old = READ_ONCE(*mem); \ \ __asm__ __volatile__(LOCK_PREFIX "cmpxchg %[new], %[ptr]" \ : [ptr] "+m" (*mem), [old] "+a" (__old) \ : [new]"r" (val) : "memory", "cc"); \ } \ } while (0)