Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp830730pxj; Tue, 18 May 2021 15:21:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPBNCj3Gon5mzt0YXoMZiclZUOjr4szGsxYSvMfP+N0TGlJ4EvrLE70L5PPxeJycRYMw8U X-Received: by 2002:a50:fb0a:: with SMTP id d10mr9540611edq.47.1621376471969; Tue, 18 May 2021 15:21:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621376471; cv=none; d=google.com; s=arc-20160816; b=iERT3Z9QeEZARRCIuswSQidAPdjP1/d811f10tk20vismilJyY4yxyKndaMFqSh3zS EPoAVLBNBh3f4ITbEJjMg/VAzEtAa0+dgWkdzbtRiZtuar0D7gx7tkKdh11ZTGxdxRSi ebvz+2lw6HEsUxp2TzvNT6jKgJReYzxItrTGhcC/p8U7v/eNtpmgl/NWWBlXwUWJJQvE B8pjoq5t/jUHrn7xBRjXplinbPavEkrLxXxDCXnSNPdqvlFshO4osYhdkqSKluQ/BvZR qE6feCOcauGwkiSXimQWVLZ5XzM/ay7fYx4B1fpfcFwsldKyJNQsNuxL1WNAfDIwO0kb gx7g== 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=US+ZHUqvwSWcl6nmtFYdA4IjAezUz41XYjRgC8na4fg=; b=XEfZyvNwVx9ChdqBz11RlUFSjTehSFl4CP8ZpMMfPIdf3OrEU2p9dgEfhPaYpgXaBf lYyef+l0wh7QwFl4RtbF/Sx27wwnie+1p4lchrd7PYkAsAh9PX05qa9EIUvv95gXUcvw /jFqgJVoGvYS2cK6iC4GLBX5LjducGpOsrSN3pbpu5LmKjpOBNUZl3+sdkhT8u1In8jn MkKUgFRdYUwrGzfUrfd19q0Xo8htb/y03ftIhjvb6QDZVYn6/IOkwYWXKWvaj/Exsu8I otwvVa0llHRTIqLm3dmKmPDkrjh4dkabGmRa+gqtJd3gE2/sHhwu4YK6nfMesutOx0he 0dmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NMqzEIt1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 q17si10718185edt.177.2021.05.18.15.20.48; Tue, 18 May 2021 15:21:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=NMqzEIt1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S236731AbhEQSEL (ORCPT + 99 others); Mon, 17 May 2021 14:04:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236683AbhEQSEL (ORCPT ); Mon, 17 May 2021 14:04:11 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B07C2C061573 for ; Mon, 17 May 2021 11:02:54 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id gb21-20020a17090b0615b029015d1a863a91so65721pjb.2 for ; Mon, 17 May 2021 11:02:54 -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=US+ZHUqvwSWcl6nmtFYdA4IjAezUz41XYjRgC8na4fg=; b=NMqzEIt1ko7mRiqXkcP0iSbOZLVLBMBCtABhDbLOPKI1LeZlnpKWLTz0mAzYRGkEU5 pO6sy7F101gqXnb4NPLh8zwGUFPVs9Cz/g5vDPLOCIOtffSoPJzk++Fs7AJ0cygw1w+E jNr2Z46nW636r+hmNsgfKgqEfrRdQUhSkduNzguyJ0hZo7DqhdwcZ0dxbPG4Xim06Lrk S3Qkp0wJZXzbyXxCorzv/2gwAu9W1X2YZFyghxIQjupSmDnBdB0bzXc+CxLJH/+m9IzR +HFfc5Gh5K5VJMklRaMK7KEknd9mMWtKrSoGNgvgi+C5srsN32At5qOIzSCBWbcTN0kk q06A== 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=US+ZHUqvwSWcl6nmtFYdA4IjAezUz41XYjRgC8na4fg=; b=lki0yXZrwuOfk2lu8qANMLak1nD62uSDCGuzuhQ3iJMzCNekigLFjr8SyTeh5yKV8C PlZ8CmZYDQOP97m1Sz3iVSFp3cjOf7xK/r8MUc2P6GfAz4gHyS/zcPvQRgIi4RCdeZbt 148lioF54LxEgDejErgKSNMPFIw5KWCyUbJi/8fZc25ZoE3pfIjmF0zOavl8xnOPK9yS eoptPqfq6j1MmNsid4HAaH8SfSABuryWzcEwH09jJZNUvazF9V04n2e2LtsSvG+zE6c8 Jbuh6CMeXW/3NobwrXIfvTJhv0ls7jlfOt9WWc0e9ZPeSqlTkAtQYq84Saq1if1he11O Bs3g== X-Gm-Message-State: AOAM531UM7yfx26koioM2PPUh8KiSPOUO05Wl6xyGP5L6sL3Pd4EXHTF mZah+HWhPAXC475oX9M/fHZG4g== X-Received: by 2002:a17:90a:d903:: with SMTP id c3mr395701pjv.34.1621274574023; Mon, 17 May 2021 11:02:54 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id w127sm10354642pfw.4.2021.05.17.11.02.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 11:02:53 -0700 (PDT) Date: Mon, 17 May 2021 18:02:49 +0000 From: Sean Christopherson To: Dave Hansen Cc: Paolo Bonzini , Andy Lutomirski , Jon Kohler , Babu Moger , Thomas Gleixner , Ingo Molnar , Borislav Petkov , X86 ML , "H. Peter Anvin" , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Fenghua Yu , Yu-cheng Yu , Tony Luck , Uros Bizjak , Petteri Aimonen , Kan Liang , Andrew Morton , Mike Rapoport , Benjamin Thiel , Fan Yang , Juergen Gross , Dave Jiang , "Peter Zijlstra (Intel)" , Ricardo Neri , Arvind Sankar , LKML , kvm list Subject: Re: [PATCH] KVM: x86: add hint to skip hidden rdpkru under kvm_load_host_xsave_state Message-ID: References: <20210507164456.1033-1-jon@nutanix.com> <5e01d18b-123c-b91f-c7b4-7ec583dd1ec6@redhat.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-kernel@vger.kernel.org On Mon, May 17, 2021, Dave Hansen wrote: > On 5/17/21 10:39 AM, Sean Christopherson wrote: > > On Mon, May 17, 2021, Paolo Bonzini wrote: > >> On 14/05/21 07:11, Andy Lutomirski wrote: > >>> I don't even want to think about what happens if a perf NMI hits and > >>> accesses host user memory while the guest PKRU is live (on VMX -- I > >>> think this can't happen on SVM). > >> This is indeed a problem, which indeed cannot happen on SVM but is there on > >> VMX. Note that the function above is not handling all of the xstate, it's > >> handling the *XSAVE state*, that is XCR0, XSS and PKRU. Thus the window is > >> small, but it's there. > >> > >> Is it solvable at all, without having PKRU fields in the VMCS (and without > >> masking NMIs in the LAPIC which would be too expensive)? Dave, Sean, what > >> do you think? > > The least awful solution would be to have the NMI handler restore the host's > > PKRU. The NMI handler would need to save/restore the register, a la CR2, but the > > whole thing could be optimized to run if and only if the NMI lands in the window > > where the guest's PKRU is loaded. > > What were you thinking about? Something like: > > *this_cpu_ptr(&need_nmi_wpkru) = 1 > // Enter Guest > __write_pkru(vcpu->arch.pkru); > *this_cpu_ptr(&need_nmi_wpkru) = 0 > > And then in the NMI handler: > > u32 pkru; > > if (*this_cpu_ptr(&need_nmi_wpkru)) { > pkru = rdpku(); > __write_pkru(vcpu->arch.pkru); This isn't correct, vcpu->arch.pkru holds the guest value, the NMI handler needs to load the host value. I was thinking KVM would stash away the current host value, and the NMI handler would check the saved value against rdpkru(). > } > ... > copy_*_user_nmi(... whatever ...); > ... > if (*this_cpu_ptr(&need_nmi_wpkru)) > __write_pkru(pkru); > > ? > > I was thinking we could just blow away PKRU without saving/restoring it > in the NMI handler, but that might clobber PKRU in the window between > need_nmi_wpkru=1 and entering the guest. Yep. It would also blow away the guest's value if the guest did WRPKU while it was running since KVM would never get a chance to read/save the guest's new value. > But, the save/restore seems doable especially since we can do it in C > and don't have to mess with the NMI stack or anything.