Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4124576rwb; Sun, 7 Aug 2022 15:46:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR6b/SxIABdbuVTr1Da+DF1j8JoIJyDWCs37KbXbBkB4Ww5wrCaAODRCePgx1qGmA5frPMVj X-Received: by 2002:a05:6a00:a8e:b0:527:9d23:c613 with SMTP id b14-20020a056a000a8e00b005279d23c613mr16130901pfl.53.1659912362916; Sun, 07 Aug 2022 15:46:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659912362; cv=none; d=google.com; s=arc-20160816; b=OgWtwehfs01A2IUWSafhTJyIWv5sGk5i6vY3UcvSK1FaoWZ/fyK4fAUCh7jUACAG1/ +n8BQI08rFP58cXNcf9c1mm36HHO59aycd3e1AR36CsCwoOVmurf0hE5Q91vgI6FE7qz 172jHosBKj2FqUng858K3P62jro604R2sRygoEDG8tgSNYCMcTdSdLTNthMgf6Ig/y9V WZWLJ2oznbErcHq5jT3jRoyPFdEtciqmbLculN7Y0/RgfzRGR4Rw09oAkRdArMZncDFl 1g8W113HzeK32Y4LjhX/ANpWs7ZOc1G03RQLzYKTvx+yYlE+XEd4Nr/6mb3RcvZx8Zb8 p3Kg== 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=sxTMt9z2l9D/EtN4eh+F7H88jcyAo8bR3d/Cj9+KNko=; b=Rqkvlkp9skjoSby6rSaDeDfTj3WfRy23/rLoA2o1tcCQjZezSz6fRErKG8s28nBMtM oRJZk25j3CtlT2wgpaHieoboHA39JiCFjIfXkpamOa8T6kqtD2j52RF/6madTdJ3O5qM cPKNakDjI5syGK9Sh9PYz4udGQCjy9CvqAe+n4J7+RCCGsUKvb5gsl8PllbQouT7s9io 51Z8FcNtCTz1t8/vNvemXkDUDg/0pGls40w4smq1bQTZu+BLQ5413mONx27/imRUsX9+ wumxmy6h2Fqmqz9hgbZ6SdR39HhEN09qSY3+yqQeK8f9ZKXGgApwmkfquKjykW7ZrB9q 2jLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fBJAlYAq; 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 h15-20020a63210f000000b0041c4d61263esi8892344pgh.137.2022.08.07.15.45.48; Sun, 07 Aug 2022 15:46:02 -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=@intel.com header.s=Intel header.b=fBJAlYAq; 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 S242395AbiHGWcT (ORCPT + 99 others); Sun, 7 Aug 2022 18:32:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242189AbiHGWbi (ORCPT ); Sun, 7 Aug 2022 18:31:38 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0502118361; Sun, 7 Aug 2022 15:18:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659910730; x=1691446730; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0eQxR6em7YwtSJ4aNxbKq4cfhsibGseChB89myr5XqY=; b=fBJAlYAqZKc//40OFVOSvjZV1LhaOR+Aw2peLMaCLaUsmH3JqS/s7Ahb FRe1D1e+hHAFjqdPzr0l22/H5LOxLWQQJywzKTReOgQEk3JQTG2cyjEfq KoCCjoFkEXsspqGTEOR0qcqwvaPzKOE60HCpuHbnsDLAM7f6erQDnaQ7x 8TR1ygDqCdgbdn+yGXf/WLWZfpapl02YpuYpxdY2b0m2mzfphiFw15EEH SuTul1Cwf/wIxxvZWQD2zLloMkhBIfQ89TkQxMM/OuGFpB3TzRdVDYgpI 4/cq5fPBCbG+u1nBMpFcoPqE44x+9vbVxVlPyxShfBxsK/SwldrreYo4r Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="270852834" X-IronPort-AV: E=Sophos;i="5.93,221,1654585200"; d="scan'208";a="270852834" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:18:49 -0700 X-IronPort-AV: E=Sophos;i="5.93,221,1654585200"; d="scan'208";a="632642317" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:18:49 -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 Subject: [RFC PATCH 06/13] KVM: TDX: Pass size to reclaim_page() Date: Sun, 7 Aug 2022 15:18:39 -0700 Message-Id: <5dda5dbae9b4db639b1f1d4c66d64a164115c024.1659854957.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=-5.0 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,T_SCC_BODY_TEXT_LINE 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 A 2MB large page can be tdh_mem_page_aug()'ed to TD directly. In this case, it needs to reclaim and clear the page as 2MB size. Signed-off-by: Xiaoyao Li Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index b7a75c0adbfa..0b9f9075e1ea 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -189,11 +189,13 @@ void tdx_hardware_disable(void) tdx_disassociate_vp(&tdx->vcpu); } -static void tdx_clear_page(unsigned long page) +static void tdx_clear_page(unsigned long page, int size) { const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); unsigned long i; + WARN_ON_ONCE(size % 64); + /* * Zeroing the page is only necessary for systems with MKTME-i: * when re-assign one page from old keyid to a new keyid, MOVDIR64B is @@ -203,13 +205,14 @@ static void tdx_clear_page(unsigned long page) if (!static_cpu_has(X86_FEATURE_MOVDIR64B)) return; - for (i = 0; i < 4096; i += 64) + for (i = 0; i < size; i += 64) /* MOVDIR64B [rdx], es:rdi */ asm (".byte 0x66, 0x0f, 0x38, 0xf8, 0x3a" : : "d" (zero_page), "D" (page + i) : "memory"); } -static int tdx_reclaim_page(unsigned long va, hpa_t pa, bool do_wb, u16 hkid) +static int tdx_reclaim_page(unsigned long va, hpa_t pa, enum pg_level level, + bool do_wb, u16 hkid) { struct tdx_module_output out; u64 err; @@ -219,8 +222,11 @@ static int tdx_reclaim_page(unsigned long va, hpa_t pa, bool do_wb, u16 hkid) pr_tdx_error(TDH_PHYMEM_PAGE_RECLAIM, err, &out); return -EIO; } + /* out.r8 == tdx sept page level */ + WARN_ON_ONCE(out.r8 != pg_level_to_tdx_sept_level(level)); - if (do_wb) { + /* only TDR page gets into this path */ + if (do_wb && level == PG_LEVEL_4K) { err = tdh_phymem_page_wbinvd(set_hkid_to_hpa(pa, hkid)); if (WARN_ON_ONCE(err)) { pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); @@ -228,7 +234,7 @@ static int tdx_reclaim_page(unsigned long va, hpa_t pa, bool do_wb, u16 hkid) } } - tdx_clear_page(va); + tdx_clear_page(va, KVM_HPAGE_SIZE(level)); return 0; } @@ -257,7 +263,7 @@ static void tdx_reclaim_td_page(struct tdx_td_page *page) * was already flushed by TDH.PHYMEM.CACHE.WB before here, So * cache doesn't need to be flushed again. */ - if (tdx_reclaim_page(page->va, page->pa, false, 0)) + if (tdx_reclaim_page(page->va, page->pa, PG_LEVEL_4K, false, 0)) return; page->added = false; @@ -404,8 +410,8 @@ void tdx_vm_free(struct kvm *kvm) * TDX global HKID is needed. */ if (kvm_tdx->tdr.added && - tdx_reclaim_page(kvm_tdx->tdr.va, kvm_tdx->tdr.pa, true, - tdx_global_keyid)) + tdx_reclaim_page(kvm_tdx->tdr.va, kvm_tdx->tdr.pa, PG_LEVEL_4K, + true, tdx_global_keyid)) return; free_page(kvm_tdx->tdr.va); @@ -1548,7 +1554,8 @@ static void tdx_sept_drop_private_spte( * The HKID assigned to this TD was already freed and cache * was already flushed. We don't have to flush again. */ - err = tdx_reclaim_page((unsigned long)__va(hpa), hpa, false, 0); + err = tdx_reclaim_page((unsigned long)__va(hpa), hpa, level, + false, 0); unlock: spin_unlock(&kvm_tdx->seamcall_lock); @@ -1667,7 +1674,8 @@ static int tdx_sept_free_private_sp(struct kvm *kvm, gfn_t gfn, enum pg_level le * already flushed. We don't have to flush again. */ spin_lock(&kvm_tdx->seamcall_lock); - ret = tdx_reclaim_page((unsigned long)sept_page, __pa(sept_page), false, 0); + ret = tdx_reclaim_page((unsigned long)sept_page, __pa(sept_page), + PG_LEVEL_4K, false, 0); spin_unlock(&kvm_tdx->seamcall_lock); return ret; -- 2.25.1