Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp665423pxv; Thu, 15 Jul 2021 12:54:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKqPGVJeT7Fy1f+LMOehJvgC4H51WjowawVxrdpdShymjmfrkC6mk3SppInK0+miM05jdF X-Received: by 2002:a05:6402:1bc6:: with SMTP id ch6mr9541020edb.267.1626378865655; Thu, 15 Jul 2021 12:54:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626378865; cv=none; d=google.com; s=arc-20160816; b=UpZOY2DQAec4yTE8JeZORgc/ZgrXjQLl5bgl+z2fJsutfCchffnPo9Y4Hbz+sWUK7U dp95/HGtDyGRjAHamH3fgJodUKlNsJesJwNJaL4ZQYvu4CGhNolO1bFgEDPzi6EqSKwd ADXasW1qTmkxc1oYIZCVykEZRAScmYpwrESILA0cTSIvaGNTeKgomfcQhiY1nXeDrRzy yC5ZCQkmBT+bwEtK51weHXcMmQBKg4OQ0nhkhRFhPNK8XilWO3zf1Oyiua0QVtKySvRL e2bIwyLlVpa3hfM+VKuL99x7ieQS3Bi1ONn2DwtbRCDKzIGrxk0p7OVU5SQ9JASFRC6p 5bJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=elaxZZafDjVLiuc49owUeZ0204o65amFxOUC2qpgTKs=; b=izLuZ8GmyeC7UM2tvRy+00dg9rBjS4vuZeRQzug46lbPVXOKgpG58Zdo6NQMF5qYrn f7hHa0mg3yGbqGFIyZWBkwNs2X+hG8UALdp+0PirfmdChyVMBp7NXI+wTE6DIG/+yVCg EVuT4W6I8jblcvlSnOdSgDC8ATHIR43tV6o0+1ECUH5XcgywfJBnijVTJcdb6TvaFGDB X+TbUUpL5dZsHSt2NH4ajTSM16anAuU4jtPiLKG+eaiXZoln1cRxTa1tOD6gNzMfLiE4 sx7o5AcOO/ag9S8iY3rGoDhnE6PtdPJCIvBeclhQrMq5oHlwdySZPHw/qJGoqrO1wTG5 iS5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q5HupHoP; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p13si8712496edr.88.2021.07.15.12.53.31; Thu, 15 Jul 2021 12:54:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q5HupHoP; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344847AbhGOT4R (ORCPT + 99 others); Thu, 15 Jul 2021 15:56:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346388AbhGOTvU (ORCPT ); Thu, 15 Jul 2021 15:51:20 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A4EDC05BD35 for ; Thu, 15 Jul 2021 12:18:21 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id a6so224396pgw.3 for ; Thu, 15 Jul 2021 12:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=elaxZZafDjVLiuc49owUeZ0204o65amFxOUC2qpgTKs=; b=Q5HupHoPS4LdRZjI9aLLpJl+EXOusDVJvjG4Iz0vi484LYzH9bZJ+UVYAPgxON53tD 3HUhZR3sPDonrp6Rx9HVRmHE8bwdJDYe3XLJrkaVZXuJgNA1I9br3fWYQH0NR8D1K4Bh wpY5QEoNAb+x/8n9tWtUWQ3oLRqyDIIHHMOiw2yPQ7Z+1WXGx+r3bT61UMlsD+Gt9KvD wSosGIOovnI5kM/c/4ZOB1p0adgb7KpSr/rm6WtHEv8nbN2mbwno03HSeDBR3602UD+d /PViOtSgang4WUW2+i6djc478rpbuJrqWD+lJ9Dy+1oTIcTKK+8C3zjn8Ias8jxuPdd5 sHkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=elaxZZafDjVLiuc49owUeZ0204o65amFxOUC2qpgTKs=; b=TVvnXxw1plWf63BRuvahANwSJM+AhdOfyH8PhdGe7HGFQUpXvKGZjjV5FEMscpovtB 2V/NqQ9ASM9iim4YI8+EmOpcec3Gth0WDkQJR3PaOjggJv5UVjIZN9kP6X7PMKfnp+w6 KLxX4feGlZjRK976mS9bWeEghnHcrNeE2Hl+Ing7qyZcPweioKFx/XOxxEOAmlON3hWw ATIGHtnl02PCsfmg/X3KAMCJ28MldpO9sZ21esahNe6BW+2yvMFrSDEobAz5RyNF/5c0 r9W5/EY+OJvpATHZo1WWjUPlNHgNJcoB3RADx+GwoPFv7x2niqKcJ3gHztcmkOpVhZp8 J3mA== X-Gm-Message-State: AOAM532a0yh7PYEyZuvRkuVShdXDeUNHzbXyt4KPjz0SagNA1Gec21IT 293AApTzFIXcmhG2/EMA83Hd1w== X-Received: by 2002:a05:6a00:d5f:b029:32c:7264:2f65 with SMTP id n31-20020a056a000d5fb029032c72642f65mr6181751pfv.40.1626376700235; Thu, 15 Jul 2021 12:18:20 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id i12sm6029283pjj.9.2021.07.15.12.18.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 12:18:19 -0700 (PDT) Date: Thu, 15 Jul 2021 19:18:15 +0000 From: Sean Christopherson To: Dave Hansen Cc: Peter Gonda , Brijesh Singh , x86@kernel.org, linux-kernel@vger.kernel.org, kvm list , linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, Nathaniel McCallum , brijesh.ksingh@gmail.com Subject: Re: [PATCH Part2 RFC v4 06/40] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Message-ID: References: <20210707183616.5620-1-brijesh.singh@amd.com> <20210707183616.5620-7-brijesh.singh@amd.com> <8ab309cd-8465-d543-55c8-5f6529fe74fd@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Thu, Jul 15, 2021, Dave Hansen wrote: > On 7/15/21 11:56 AM, Sean Christopherson wrote: > >>>> + /* Retry if another processor is modifying the RMP entry. */ > >>>> + do { > >>>> + /* Binutils version 2.36 supports the PSMASH mnemonic. */ > >>>> + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" > >>>> + : "=a"(ret) > >>>> + : "a"(spa) > >>>> + : "memory", "cc"); > >>>> + } while (ret == FAIL_INUSE); > >>> Should there be some retry limit here for safety? Or do we know that > >>> we'll never be stuck in this loop? Ditto for the loop in rmpupdate. > >> It's probably fine to just leave this. While you could *theoretically* > >> lose this race forever, it's unlikely to happen in practice. If it > >> does, you'll get an easy-to-understand softlockup backtrace which should > >> point here pretty quickly. > > But should failure here even be tolerated? The TDX cases spin on flows that are > > _not_ due to (direct) contenion, e.g. a pending interrupt while flushing the > > cache or lack of randomness when generating a key. In this case, there are two > > CPUs racing to modify the RMP entry, which implies that the final state of the > > RMP entry is not deterministic. > > I was envisioning that two different CPUs could try to smash two > *different* 4k physical pages, but collide since they share > a 2M page. > > But, in patch 33, this is called via: > > > + write_lock(&kvm->mmu_lock); > > + > > + switch (op) { > > + case SNP_PAGE_STATE_SHARED: > > + rc = snp_make_page_shared(vcpu, gpa, pfn, level); > ... > > Which should make collisions impossible. Did I miss another call-site? Ya, there's more, e.g. sev_snp_write_page_begin() and snp_handle_rmp_page_fault(), both of which run without holding mmu_lock. The PSMASH operation isn't too concerning, but the associated RMPUDATE is most definitely a concern, e.g. if two vCPUs are trying to access different variants of a page. It's ok if KVM's "response" in such a situation does weird things to the guest, but one of the two operations should "win", which I don't think is guaranteed if multiple RMP violations are racing. I'll circle back to this patch after I've gone through the KVM MMU changes.