Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2631565rdg; Mon, 16 Oct 2023 09:56:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE4AR93GUkjJgjv2l5EY1ID3Jzpwub9y99VZglc4ROJIi4zOa/lkx292n83Oxvn8rM+M8BM X-Received: by 2002:a05:6a00:18a3:b0:690:9a5a:e34e with SMTP id x35-20020a056a0018a300b006909a5ae34emr14947109pfh.12.1697475368591; Mon, 16 Oct 2023 09:56:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697475368; cv=none; d=google.com; s=arc-20160816; b=tCV1ZvkdoHAQLEfVas8u+z6OBNGC2n+/NuXL6MzAy7JQhXpDNoKt2A1uh4qbnMynI0 LmAwMceyeVqbr3xN6Py53mi0ObPEzVkIZ4WP6mCMboiH63xEK6xRngsROckhk7rkebF5 hEtjfkuCcC/XyJuo8F3KWuPH5xWyvBy3buzRAfCN1OgFSsP0lqh7xfy3o00wVVMxzT7b fRtRkCIJZDm2MYrx2qHGXGkKEShQi49UH8dfagGcAzops/O2bRs5mvwcnc4tg2KCFnou CkJnqGkHflDwyFCx5S7+LlrUiId2hKv6fyBudHuw0ehoGOiZRX64TD7fEAOR7EbAiXcC Azkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EJqmISHJLdla7II871axYYtYMhA6aGdulTldB4PIBBc=; fh=PgPONM/W1VtmLglMgmd5Kb29Va4E/NEsDAvR3ZLGW2Y=; b=Vi8REzzQsnONptMkBSnWoi0VPaCUD9+jUz/84hVPdq5iDrKy2Wy/xm7kcoi9H5CNZr SE0Gf9avy64yAT+0+KCkRqc9WWfGzAnnSd8Bo76kj8R8WgoYL0QJ3TCdkoG3A53LjAvd U4LBUaGtrpN13d5iTQUlKDfyvmI34v6G8u9zRGQAwd0fwsxR3azbj2ztiDn3mPs/8yVw UZY7HSN8CociySGccOwrMJezdtepenBB32hAkhPHt1Qqtp9VvDWynJOrN/aoVk7nc4B7 SR7q8xCQmxWRNBjt23SoootLGTee3VCJ0nUfuO48SECC/WrIvSXZijUT8LsTMqxWqxPl 1Lfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=K4wdlT7F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id y37-20020a056a00182500b006bd360e70edsi224179pfa.103.2023.10.16.09.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:56:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=K4wdlT7F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 0F9F48050025; Mon, 16 Oct 2023 09:56:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234249AbjJPQzy (ORCPT + 99 others); Mon, 16 Oct 2023 12:55:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234222AbjJPQzh (ORCPT ); Mon, 16 Oct 2023 12:55:37 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A280C8260; Mon, 16 Oct 2023 09:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473396; x=1729009396; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bH4TweNwAW5PDjRc4LuWzqFQTxFArgLwB4OYW4yKw2M=; b=K4wdlT7FQZqbDahzMoGqS3+xQL/CiW2/HTUE6etAe85A0tiklLvtnKJ9 ZS8cSe6tlAFkuIg4hmsVO1K5k4LiVlwdvSrvddpOpqZUFepOBHtje6BI/ Ar7TdvCPWAt3Cy3ctFjejrhjgz945BMMGSvb6o4EWzpbL+o0Fjnnx3SyL ptKcoXgFxkxbZl9ZduoZGS7q/X1o0Vo9QfTyfLIXMFcIJyOYfKQTLSV/k Q0CR4w9rFSV0oja8XgJ20O+Q3jOn/zqiWXqJOC3/6iNq1+1a7BzznkiTF qqOklVWNsyyH0jzjH/ixoV2nUDoYJsd8IzZ+eEZodPE/3KPwvNXENhQLS g==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="471793165" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="471793165" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:21:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="899569238" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="899569238" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:19:15 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Xiaoyao Li Subject: [RFC PATCH v5 08/16] KVM: TDX: Pin pages via get_page() right before ADD/AUG'ed to TDs Date: Mon, 16 Oct 2023 09:20:59 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:56:06 -0700 (PDT) From: Xiaoyao Li When kvm_faultin_pfn(), it doesn't have the info regarding which page level will the gfn be mapped at. Hence it doesn't know to pin a 4K page or a 2M page. Move the guest private pages pinning logic right before TDH_MEM_PAGE_ADD/AUG() since at that time it knows the page level info. Signed-off-by: Xiaoyao Li --- arch/x86/kvm/vmx/tdx.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index c37b66f9a52a..0558faee5b19 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1435,7 +1435,8 @@ static void tdx_measure_page(struct kvm_tdx *kvm_tdx, hpa_t gpa, int size) } } -static void tdx_unpin(struct kvm *kvm, kvm_pfn_t pfn, int level) +static void tdx_unpin(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, + enum pg_level level) { int i; @@ -1457,7 +1458,7 @@ static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, err = tdh_mem_page_aug(kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out); if (unlikely(err == TDX_ERROR_SEPT_BUSY)) { - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EAGAIN; } if (unlikely(err == (TDX_EPT_ENTRY_NOT_FREE | TDX_OPERAND_ID_RCX))) { @@ -1473,7 +1474,7 @@ static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, &tmpout); if (KVM_BUG_ON(tmp, kvm)) { pr_tdx_error(TDH_MEM_SEPT_RD, tmp, &tmpout); - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EIO; } pr_debug_ratelimited("gfn 0x%llx pg_level %d pfn 0x%llx entry 0x%llx level_stat 0x%llx\n", @@ -1484,7 +1485,7 @@ static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, if (level_state.level == tdx_level && level_state.state == TDX_SEPT_PENDING && entry.leaf && entry.pfn == pfn && entry.sve) { - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); WARN_ON_ONCE(!(to_kvm_tdx(kvm)->attributes & TDX_TD_ATTR_SEPT_VE_DISABLE)); return -EAGAIN; @@ -1492,7 +1493,7 @@ static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, } if (KVM_BUG_ON(err, kvm)) { pr_tdx_error(TDH_MEM_PAGE_AUG, err, &out); - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EIO; } @@ -1528,7 +1529,7 @@ static int tdx_sept_page_add(struct kvm *kvm, gfn_t gfn, * always uses vcpu 0's page table and protected by vcpu->mutex). */ if (KVM_BUG_ON(kvm_tdx->source_pa == INVALID_PAGE, kvm)) { - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EINVAL; } @@ -1546,7 +1547,7 @@ static int tdx_sept_page_add(struct kvm *kvm, gfn_t gfn, } while (unlikely(err == TDX_ERROR_SEPT_BUSY)); if (KVM_BUG_ON(err, kvm)) { pr_tdx_error(TDH_MEM_PAGE_ADD, err, &out); - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EIO; } else if (measure) tdx_measure_page(kvm_tdx, gpa, KVM_HPAGE_SIZE(level)); @@ -1599,7 +1600,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, err = tdx_reclaim_page(hpa, level); if (KVM_BUG_ON(err, kvm)) return -EIO; - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return 0; } @@ -1632,7 +1633,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, r = -EIO; } else { tdx_clear_page(hpa, PAGE_SIZE); - tdx_unpin(kvm, pfn + i, PG_LEVEL_4K); + tdx_unpin(kvm, gfn + i, pfn + i, PG_LEVEL_4K); } hpa += PAGE_SIZE; } -- 2.25.1