Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2623960rdg; Mon, 16 Oct 2023 09:41:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+hONY1rvgPN0pQ1dw54eDa4owy5PlIy4DdOYs50UDk7MuUvEhhYyni1/tofaRhxpqZUh9 X-Received: by 2002:a17:902:cec7:b0:1ca:4092:7200 with SMTP id d7-20020a170902cec700b001ca40927200mr4596624plg.54.1697474507166; Mon, 16 Oct 2023 09:41:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474507; cv=none; d=google.com; s=arc-20160816; b=WpCmD5RiqtXwohUUX/6RcAoujqkjjsIpn0SOX2azjV3yqElYUtzb/QEaGpwYUtDfnv tIV5aYh8BJWWa3wNuWA5E+mH+1yh8F8aZ5+PnXi5gNK9IKxd9Mj/AFj3EZTMsJLSrMak ztZgvBcQv7eii0yaNuOMji5VKnaO887QxevV7SDU+Ov/ZYVhtWPZA/twHAqOEjJwKNSW JbmZXPG6SKsNqNblyg0Js9em1DCsPQFBeVPHQIqO8ZC45Yk51tVXcflo3yxb7nnZmLJc PWckFqV4v4nCNMuBmbDUSAgYeeU+QEn0bzv5aHFNR8lOGVrsyTwD3I+bflt8fNkBT3Wa 6VRA== 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=86Sn3qQhLb3FpHe8SBnZcywoCuuMXfsnqlradydiyAY=; fh=PgPONM/W1VtmLglMgmd5Kb29Va4E/NEsDAvR3ZLGW2Y=; b=VeAX0J++p4a3NVaaQk+SO01t4er/CXLXXWFUtHt0GA6o+kPUjrClmhDa5E3rIllWyT b5j1Li7Nj9eFWq957B8oTQwKn5XDn3SC+POmjf55oTihOaS+ZxG8MgyUFtd0Dkj3/Cvd 0+GWg7A0rt6McNUUnXy7Q4qkPnd2WiqncmoNKrOY52Kc0lwhKhB7BZF/c09mOKZjoNMY p5NvJEAGDN0Ua/A0Muv9FrogW4vfLHV4lbZcfxoTclvQA8T1pZWyClPKuLYOsH3oBtOz o71vlNB0A8T2aMBQmDWULZIABDnWkY3ce6axXsMdDerep82AfiOnLQlNLYR1SM7Z3UVL ZgDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kPlrJBct; 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 q12-20020a17090311cc00b001c72c893ad0si11633870plh.48.2023.10.16.09.41.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:41:47 -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=kPlrJBct; 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 6EE528095F5F; Mon, 16 Oct 2023 09:41:36 -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 S234639AbjJPQke (ORCPT + 99 others); Mon, 16 Oct 2023 12:40:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343571AbjJPQjP (ORCPT ); Mon, 16 Oct 2023 12:39:15 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB1F8256; Mon, 16 Oct 2023 09:23:15 -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=o5ZqZe5kh4rI7M8fMrhNtQT9rpjdbgf6u1T422GjqRE=; b=kPlrJBct9DpxhGfWuoHNfzvtlvvltu7tAa9isS9SLjJo+6Uhi0rjzEGx OBD6+Ta9EZ2PcoxbBhcQTBFpht4sGzD8ZUvbpogRvzrHLdWpaJl5IXsgt ysoBD8Pz7WZLNBSgexKjqiOqL9QdRhL9XViYkM80AsMCisPAA9TeMLmvD iKDbcUSezDabMoXd6dJ0UfucW0pZg875gVF9v2tqK0ld8dsjzead9v9vO tMAv9zDCUwpap2Bo46wWIIoADSF/ZbKLzN6+/7olqNuJ8sDRQMQfohOCd Xa7bXe+gKdf/WRqWuTk/0tfMyLNvL2HuT3ADMvOVoaHZ95aM5N3JdDCmE A==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="471793140" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="471793140" 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:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="899569226" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="899569226" 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:13 -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 05/16] KVM: TDX: Pass size to reclaim_page() Date: Mon, 16 Oct 2023 09:20:56 -0700 Message-Id: <12cd734126366ea7d9b4334002a88be838f31afb.1697473009.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=-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:41:36 -0700 (PDT) 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 | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index bda2c8fa895c..72672b2c30a1 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -205,12 +205,13 @@ static void tdx_disassociate_vp_on_cpu(struct kvm_vcpu *vcpu) smp_call_function_single(cpu, tdx_disassociate_vp_arg, vcpu, 1); } -static void tdx_clear_page(unsigned long page_pa) +static void tdx_clear_page(unsigned long page_pa, int size) { const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); void *page = __va(page_pa); unsigned long i; + WARN_ON_ONCE(size % PAGE_SIZE); /* * When re-assign one page from old keyid to a new keyid, MOVDIR64B is * required to clear/write the page with new keyid to prevent integrity @@ -219,7 +220,7 @@ static void tdx_clear_page(unsigned long page_pa) * clflush doesn't flush cache with HKID set. The cache line could be * poisoned (even without MKTME-i), clear the poison bit. */ - for (i = 0; i < PAGE_SIZE; i += 64) + for (i = 0; i < size; i += 64) movdir64b(page + i, zero_page); /* * MOVDIR64B store uses WC buffer. Prevent following memory reads @@ -228,7 +229,7 @@ static void tdx_clear_page(unsigned long page_pa) __mb(); } -static int __tdx_reclaim_page(hpa_t pa) +static int __tdx_reclaim_page(hpa_t pa, enum pg_level level) { struct tdx_module_args out; u64 err; @@ -246,17 +247,19 @@ static int __tdx_reclaim_page(hpa_t pa) 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)); return 0; } -static int tdx_reclaim_page(hpa_t pa) +static int tdx_reclaim_page(hpa_t pa, enum pg_level level) { int r; - r = __tdx_reclaim_page(pa); + r = __tdx_reclaim_page(pa, level); if (!r) - tdx_clear_page(pa); + tdx_clear_page(pa, KVM_HPAGE_SIZE(level)); return r; } @@ -270,7 +273,7 @@ static void tdx_reclaim_td_page(unsigned long td_page_pa) * was already flushed by TDH.PHYMEM.CACHE.WB before here, So * cache doesn't need to be flushed again. */ - if (tdx_reclaim_page(td_page_pa)) + if (tdx_reclaim_page(td_page_pa, PG_LEVEL_4K)) /* * Leak the page on failure: * tdx_reclaim_page() returns an error if and only if there's an @@ -502,7 +505,7 @@ void tdx_vm_free(struct kvm *kvm) if (!kvm_tdx->tdr_pa) return; - if (__tdx_reclaim_page(kvm_tdx->tdr_pa)) + if (__tdx_reclaim_page(kvm_tdx->tdr_pa, PG_LEVEL_4K)) return; /* * TDX module maps TDR with TDX global HKID. TDX module may access TDR @@ -515,7 +518,7 @@ void tdx_vm_free(struct kvm *kvm) pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); return; } - tdx_clear_page(kvm_tdx->tdr_pa); + tdx_clear_page(kvm_tdx->tdr_pa, PAGE_SIZE); free_page((unsigned long)__va(kvm_tdx->tdr_pa)); kvm_tdx->tdr_pa = 0; @@ -1596,7 +1599,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, * 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(hpa); + err = tdx_reclaim_page(hpa, level); if (KVM_BUG_ON(err, kvm)) return -EIO; tdx_unpin(kvm, pfn); @@ -1629,7 +1632,7 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); return -EIO; } - tdx_clear_page(hpa); + tdx_clear_page(hpa, PAGE_SIZE); tdx_unpin(kvm, pfn); return 0; } @@ -1741,7 +1744,7 @@ static int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn, * already flushed. We don't have to flush again. */ if (!is_hkid_assigned(kvm_tdx)) - return tdx_reclaim_page(__pa(private_spt)); + return tdx_reclaim_page(__pa(private_spt), PG_LEVEL_4K); /* * free_private_spt() is (obviously) called when a shadow page is being -- 2.25.1