From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 06E4572 for ; Thu, 15 Jul 2021 19:18:20 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id 70so4166746pgh.2 for ; Thu, 15 Jul 2021 12:18:20 -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=nEnc1KX7fawR0mekT8+7ArSAK2D1PElx4yZGKxbENn6YxS4Aps5jsSjh7yFlSPQ4Sh 5yi3z2nciV40aidRIT9pg6iawSMGuOUjhuXuOaxZjPe1MQLE3b9zjkUuKeZ7nxNM2AMD +U4n/dqtl+UyVg4+aLaljFQLP49R3ku4sZeXKpQEmf8rqG+oer2mVihDWVPaf6MLtz3P VCat8TA111XnUJJDmZi57/Z6B/OnR3nlW2jU64Hg8CBcEeu6bxz0RFHsT9t1YgcOuBJ6 3I08PkyPyGXUElkhUFbpztbtEg4p6frCs7AvyjcAF4Ev0LL884+xQNbTP9CukSbTkT2N 9RtQ== X-Gm-Message-State: AOAM532acCXtLCvHERcUim8r5DwXcj/WIWS+qd27bF9zikVhOPR1p5f3 +J8VVjtk9tz7i2ZbWwB5D29r2Q== X-Google-Smtp-Source: ABdhPJxs5P265vYYEPGZ8x+HkOKGVsrc3wav0sXpzToNYrqG5Ockpl2xyO49rMlEI8CkyKvDo3JDEA== 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> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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.