Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp564048rwb; Thu, 12 Jan 2023 09:21:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXvYVrspuwLWertNVxVVg50vnqYnIc/cieg7ObTL/YK3eAFjIWxvLaP/UtuMviyNmtcpVOBc X-Received: by 2002:aa7:c907:0:b0:499:b672:ee3a with SMTP id b7-20020aa7c907000000b00499b672ee3amr12767393edt.12.1673544113962; Thu, 12 Jan 2023 09:21:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673544113; cv=none; d=google.com; s=arc-20160816; b=fJ0OZRMlNpzZItKmMIM3IZtD1Wnk184xHy02Rm/+IZMrfCOkVnVM/8e0jSfyunQyXI AoSzn+9nr4vb8AU92sczMuIncwQiL4WCNT1k0lIK+ndNje6bjkK+tISKgwD/Fx8tiK9C yyvHH/qM/2wpJwZxJRq4sQWPfyC2E6rKheEDXq+xczroDJjxJOhxgSLansnyNXmhwYYv n0IFGy2QK1DQYtsn01/VCOZoy4XDiKhEgNqNy1HoBt5GG6hjGb98zuER7BGHyHaOdNqu Y6EdKCi5MENEaVOEfAm7b6IDD/mTU2rYHFgUtwe0u7bt943z7RIDjXIeHb8jmFtmrsGo kIlw== 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=vqsfX7CEaQCx8AQECTD0tJsGxM/jB2Ua63EaX1k7kNE=; b=DJCrbKzoyq+rkOdqnnKT/qLlF5SaMdtyHhBmuS5aKl6tkL7rHqjb8dFatmhJuluoWq svXBXrkAY1hF2dIsDY33NY2wVwXcWbRI0aJjbh5IaMfFSXCSdgW7NQqHU4LHKtYDS2s8 vK7yIqZEalMOo6ZCLIs18aeV71+ZvqSgCw4ueqi4mQunTkbOrLu59m7yHbo8JG3KmK9F hmEXFz8n5k8GSowN6fp6cbAfPyjr2M/VANAZEmqV09b64rricw5sYzIq8eYFsI3WrjUt DLrc4+Hv4er1wtQnDae2V/iYGQ8Z3bHgJh5uLrMgFkk0w/+G7Gxix6Swv+ojDvfwtbZE oBCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=P6myxIpS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd21-20020a170907629500b0084d0ebd704bsi18896125ejc.71.2023.01.12.09.21.42; Thu, 12 Jan 2023 09:21:53 -0800 (PST) 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=@intel.com header.s=Intel header.b=P6myxIpS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240246AbjALRN5 (ORCPT + 50 others); Thu, 12 Jan 2023 12:13:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232200AbjALRNR (ORCPT ); Thu, 12 Jan 2023 12:13:17 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2D347DE37; Thu, 12 Jan 2023 08:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673542149; x=1705078149; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=H/nxuouj18bYRqF828XvaIlfCKsR/L23rNhgJlNwdfo=; b=P6myxIpSVEp2KVzZQUl10a6eoh3rRlEny7PIgBhqdQzQ0MhzmJPLLmg/ VmxVY9eYEdEeZFebO9UqibpvQ6XtDJoNOvXTueZDZ/7VHCz/HgCdZ5Trb EQxxfbHEangkhcXo414dXJL35kasJ+uSFjr0EtC1borIugbyKxZjCu6dn P9pvSR5wIFQ+9K7/YRARTaL5Pp8QmMBp3DwnCt+J6J6gHqbLgYlAQ/SNA moRgv70u7Vc+5tllctFCVpC0KAfekKOadgNdy3cDeQLy11u2bR0M8E7Vb UE5xpPy/5U1nZKB4OJWEmU27ceRBon0jwAkA1f0y79cgbCgUHEPyK/2OX A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323816312" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323816312" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:44:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="986658344" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="986658344" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:44:17 -0800 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 , Xiaoyao Li Subject: [RFC PATCH v3 08/16] KVM: TDX: Pin pages via get_page() right before ADD/AUG'ed to TDs Date: Thu, 12 Jan 2023 08:44:00 -0800 Message-Id: <49633539246692ba834c812952dcaf8fecc7600b.1673541292.git.isaku.yamahata@intel.com> 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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 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 Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 66a1f8534461..177f98f7c9c2 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1322,7 +1322,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; @@ -1358,12 +1359,12 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, if (likely(is_td_finalized(kvm_tdx))) { err = tdh_mem_page_aug(kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out); if (err == TDX_ERROR_SEPT_BUSY) { - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return -EAGAIN; } 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; } return 0; @@ -1386,7 +1387,7 @@ static int tdx_sept_set_private_spte(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; } @@ -1404,7 +1405,7 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, } while (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)); @@ -1433,7 +1434,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, err = tdx_reclaim_page(hpa, level, false, 0); if (KVM_BUG_ON(err, kvm)) return -EIO; - tdx_unpin(kvm, pfn, level); + tdx_unpin(kvm, gfn, pfn, level); return 0; } @@ -1465,7 +1466,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); r = -EIO; } else - tdx_unpin(kvm, pfn + i, PG_LEVEL_4K); + tdx_unpin(kvm, gfn + i, pfn + i, PG_LEVEL_4K); hpa += PAGE_SIZE; } return r; -- 2.25.1