Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp12961415rwd; Fri, 23 Jun 2023 13:22:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5NhCeXsL1AI2x/l68bP1ApSCL9EsCVCEjGI7cwvO+b4P6J0WueOmq3WdpmfS2VBDx/CY8Y X-Received: by 2002:a05:6808:f02:b0:39e:d037:ebf2 with SMTP id m2-20020a0568080f0200b0039ed037ebf2mr15597775oiw.58.1687551737471; Fri, 23 Jun 2023 13:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687551737; cv=none; d=google.com; s=arc-20160816; b=hsQQYuGRozn5nCV+Ii/l1dairB+fOou7ft9gBilR3whHG28mm1e70rIJNnAMLkNTw6 ppEvsh/ykzf51f9dXztGgTn7Cr1LODPRgmk879EcSlxB6r4i01CudMdsOBR2ZIoiWPt3 xnD99dJ6OnVUhofyxUSFIw8HnC5UfloA5XB5grEg2+NFb6zIy2MXrw4ho1dKqIxPe0pL rJnVFZgL21vQy3WhYgZoc6CZWbb3WlgFw0lrbFlQOk/PYLVnyLKAXBec1pux/xMJBpXT zTGpY0xaC8MVXiWHE73cNc+ixNqUDtVaT3vmHQD4O2YIVvyxnnAoz5ZIHzz1/KFplx8s NWww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=EAV+u9FZn1OMDdsvLpiA8ptQ0UAVCWOU9+eOHemeZkc=; fh=MsuMsiJC1J3vjjK+0DkeNEdY5V1IwI4gRgUTiorjzo4=; b=WSlhTfr9t8vL+fji9LeobFLF4XPu0kiA8Jaxjc0LsNzfI8hu4Ub2dDUjG/5v9MXI9A wXpZTzx4kT0VI8chDZhrMUAQ9wkSpIRewxGw4ZRyMsuCW1AVCNfwULqZInwlED9KvP2F /Gb/mY5B1Wl27W83yChY+3zekfyAnY0wMFbUbfTUDd5sQZSDwMv6yyss4rZJaO2PwScK 1AB3620xtBAVAIgsCqiuBjkvhEYiGk34QtjDe/UuZ6RvbHLwx3dgolehZc0JkDtRV3St DzpIAnTo3/VGu9AVsfkXP+3udarctiTogQo4rsoWuDemSVFsAhP2/F7HMYzI5xRDZnaU 1aDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=r2KqjKqv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i184-20020a6387c1000000b005573d0a4de4si180305pge.218.2023.06.23.13.22.02; Fri, 23 Jun 2023 13:22:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=r2KqjKqv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231721AbjFWUEI (ORCPT + 99 others); Fri, 23 Jun 2023 16:04:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbjFWUEG (ORCPT ); Fri, 23 Jun 2023 16:04:06 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7D181FC2 for ; Fri, 23 Jun 2023 13:04:05 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-553d27fe4baso805417a12.2 for ; Fri, 23 Jun 2023 13:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687550645; x=1690142645; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=EAV+u9FZn1OMDdsvLpiA8ptQ0UAVCWOU9+eOHemeZkc=; b=r2KqjKqvT/xeedE+Uetlyqb6JDv1xiVsWPQjpWxl++J6+bumEq73qW8Ny/MMnElmJ6 O9O2t+XH/Ria9AnJC9poUqbrQIzuL18YDpLLO+HlnCySj9MFXVrVxzR/qRL9WemW0s8K 3U/BXFOQch6jmpkQTR/rGoDpfz23FrESAr+lNHWLawIGMzx2fTTjcz1KpK0kxOX0sHxx PHZ14EwZSL9+kcmBhAgmfFWsCBqxCFGX8Kqp3FXP6iK6FOy934HbUBIYW/mYPki2JK4x 3ykr0QYQA2hg1zzqTT7R+u+3plk5LWLv04e+GFSDnf/W5oXUa/ZF4xbNxQ1crQCdeyfB MhKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687550645; x=1690142645; h=content-transfer-encoding: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=EAV+u9FZn1OMDdsvLpiA8ptQ0UAVCWOU9+eOHemeZkc=; b=FJclkDNnRtQ3WOsNr8YJ6mh8UrDtyfFN+YodKj523FoKCtfRmaBheZOqNogNQGjh5p QQYNsfEXvf2di4Jo3yLCro075uoMzIxdmPxt+bxb7Mo0Q9Xgqgz+1g3EVzPA/nelEhPY Ob7nMURWmctOwF0j4f0FBGEcdM2Kuy2Z8w+Pj61FA06cLdy93KtUdUVrRfDbuzy/NiPb /CfSEoDC/1Js+LBeVciJ+3HR1Krl8ViGzZTAxZiQ1kr+3hBNBIDqey8ii4JYJMqFJjou QA1owoxgj2u2/XLGwbJltTzbG5SR0300pTicpsmjOSxAF4Yh93fZOQCx+bNLIwIhlo1K pemQ== X-Gm-Message-State: AC+VfDwxYUxSZ4ShBrqFJqjD3/wL96NM0MmQ4zhZYUdSgp2YsUoV4Xi2 FEac3ZNcl7BBjPkrZ7RoZjPJ0fIyjyk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:1c12:0:b0:53f:6f7c:554e with SMTP id c18-20020a631c12000000b0053f6f7c554emr2532033pgc.12.1687550645192; Fri, 23 Jun 2023 13:04:05 -0700 (PDT) Date: Fri, 23 Jun 2023 13:04:03 -0700 In-Reply-To: Mime-Version: 1.0 References: Message-ID: Subject: Re: [RFC PATCH v2 4/6] KVM: x86: Introduce fault type to indicate kvm page fault is private From: Sean Christopherson To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, linux-coco@lists.linux.dev, Chao Peng , Ackerley Tng , Vishal Annapurve , Michael Roth Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 22, 2023, isaku.yamahata@intel.com wrote: > diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_inter= nal.h > index 7f9ec1e5b136..0ec0b927a391 100644 > --- a/arch/x86/kvm/mmu/mmu_internal.h > +++ b/arch/x86/kvm/mmu/mmu_internal.h > @@ -188,6 +188,13 @@ static inline bool is_nx_huge_page_enabled(struct kv= m *kvm) > return READ_ONCE(nx_huge_pages) && !kvm->arch.disable_nx_huge_pages; > } > =20 > +enum kvm_fault_type { > + KVM_FAULT_MEM_ATTR, > + KVM_FAULT_SHARED, > + KVM_FAULT_SHARED_ALWAYS, > + KVM_FAULT_PRIVATE, This is silly. Just use AMD's error code bit, i.e. PFERR_GUEST_ENC_MASK as= per the SNP series. Bit 34 (ENC): Set to 1 if the guest=E2=80=99s effective C-bit was 1, 0 ot= herwise. Just because Intel's ucode is too crusty to support error codes larger than= 16 bits doesn't mean KVM can't utilize the bits :-) KVM already translates to= AMD's error codes for other things, e.g. error_code |=3D (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) !=3D 0= ? PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; For TDX, handle_ept_violation() can do something like: if (is_tdx(vcpu->kvm)) error_code |=3D (gpa & shared) ? 0 : PFERR_GUEST_ENC_MASK; else if (kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(gpa))) error_code |=3D PFERR_GUEST_ENC_MASK; And that's not even taking into account that TDX might have a separate entr= y point, i.e. the "is_tdx()" check can probably be avoided. As for optimizing kvm_mem_is_private() to avoid unnecessary xarray lookups,= that can and should be done separately, e.g. static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && kvm_guest_has_private_mem(kvm) && kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; } where x86's implementation of kvm_guest_has_private_mem() can be #define kvm_guest_has_private_mem(kvm) (!!(kvm)->vm_type)