Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2624125rdg; Mon, 16 Oct 2023 09:42:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdUl4ahPMKMLHgrvTC4T2f0IJKYJ3f7S/k2xqUcm8UyT0jZz/kUCc4n+IuOtrnF8MjvE/D X-Received: by 2002:a05:6a20:7345:b0:16b:e2e5:fe55 with SMTP id v5-20020a056a20734500b0016be2e5fe55mr32515765pzc.51.1697474524341; Mon, 16 Oct 2023 09:42:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474524; cv=none; d=google.com; s=arc-20160816; b=Qifa8lCjmKTn0j8lvYk1u3d4wA9rh94MxHQZoH8e9UoBz3JOvjBh0aavWjAuL60Cgz crr6v6YjAiLAnNwkWMWYtEGs5s+fPuAYXXDhJkgBNIEpjmStn3p00mXLoYBqqrlzZmTx mXym+A85qmyhb977PAnScsXP5t1jO4kNMAQXSYA2NOKaqSGBSFTfwC0YbbyieI9ixid3 AWYwlB4rl2ywAE70NP3C/YRhnPim2qieejuwcjscQa8eXxU041q1r/QcGaHq3JhN8YSz GuS1CYbEqUZcOOJd5IJyTohDy3KIJI+V5tILIGvgagtbRgOsnXyH7RZJr75cUoxqrmlh rURw== 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=JU7cFY69geZsBvwwEG1BH61FoW9r7QHYKDamc5acDZc=; fh=PgPONM/W1VtmLglMgmd5Kb29Va4E/NEsDAvR3ZLGW2Y=; b=OWFR5zXq45dHg/hLfCLCY7gihTO96IhNdgGwi7ePMs/KAEb1UdR27Nrzjuc9v1gAuk 2roqb5OlKraB4r8DF+GRr+FHJRWNajTnwh2VOFXbgOH0ONmc4nH1W6jkIvqqyHsO3NLj Wrxf3Mr6n2xWqnvtjQwZfbzxbbndy8G0vp9MRudKlkRNyohaKp6l/R9iGc9iUtJuLnz2 PlPHJcxC41+tSyd9CkLOWcWYxMiMkL1otUqnOyUv0sPJOBot0zJc2RphlCeXr5AQK7Fs fkn7HA7N0ZPopN+1A9YC3g0v0Yls2WVp4vCHZNhJWj0iQBlpdAyIMHIZkGV11dicNYTj qzNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mKbBevXv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id c5-20020a17090abf0500b00276bdabe471si6455917pjs.163.2023.10.16.09.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:42:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mKbBevXv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 79099807BEF6; Mon, 16 Oct 2023 09:41:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343533AbjJPQkg (ORCPT + 99 others); Mon, 16 Oct 2023 12:40:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343572AbjJPQjP (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 EC83C8258; 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=y35oJUPopyeRbTd34DdLXBVXFicmHGF9J0ehvEF/xG0=; b=mKbBevXv1+UXK5+Lsljn5cYtREcta7uXcH6+Mw16+WEPw0I8zc5XF+S5 jypaqru9S3EyPfQ1TUz3tfyzKtEPhi821/k3792SeRJqLPrWmO/MARkrT 69/XrBgx5hVQQw/zbs+WIGGNtMI7XaxYOsoRIBVsmdXjRJh2S/99rFGck sEmXOXHGuMefECxPsX12bjtoU57eauEnNIV5DaBizMCjJGASzAtqT2WJM xCIe+dthNCitIY677Taq2lRfEx76583nArLvTLUELJCprTmTkIhc4Cb2D U6RSlwwyZukvRQOR8o1A1F9pyU/OPK4WaLxijCH9pry8l7N5uUiNllRUh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="471793146" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="471793146" 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:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="899569231" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="899569231" 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:14 -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 06/16] KVM: TDX: Update tdx_sept_{set,drop}_private_spte() to support large page Date: Mon, 16 Oct 2023 09:20:57 -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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:41:23 -0700 (PDT) From: Xiaoyao Li Allow large page level AUG and REMOVE for TDX pages. Signed-off-by: Xiaoyao Li Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 70 ++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 72672b2c30a1..992cf3ed02f2 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1435,11 +1435,12 @@ 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) +static void tdx_unpin(struct kvm *kvm, kvm_pfn_t pfn, int level) { - struct page *page = pfn_to_page(pfn); + int i; - put_page(page); + for (i = 0; i < KVM_PAGES_PER_HPAGE(level); i++) + put_page(pfn_to_page(pfn + i)); } static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, @@ -1456,7 +1457,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); + tdx_unpin(kvm, pfn, level); return -EAGAIN; } if (unlikely(err == (TDX_EPT_ENTRY_NOT_FREE | TDX_OPERAND_ID_RCX))) { @@ -1472,7 +1473,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); + tdx_unpin(kvm, pfn, level); return -EIO; } pr_debug_ratelimited("gfn 0x%llx pg_level %d pfn 0x%llx entry 0x%llx level_stat 0x%llx\n", @@ -1483,7 +1484,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); + tdx_unpin(kvm, pfn, level); WARN_ON_ONCE(!(to_kvm_tdx(kvm)->attributes & TDX_TD_ATTR_SEPT_VE_DISABLE)); return -EAGAIN; @@ -1491,7 +1492,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); + tdx_unpin(kvm, pfn, level); return -EIO; } @@ -1527,7 +1528,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); + tdx_unpin(kvm, pfn, level); return -EINVAL; } @@ -1545,7 +1546,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); + tdx_unpin(kvm, pfn, level); return -EIO; } else if (measure) tdx_measure_page(kvm_tdx, gpa, KVM_HPAGE_SIZE(level)); @@ -1558,10 +1559,7 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level, kvm_pfn_t pfn) { struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); - - /* TODO: handle large pages. */ - if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm)) - return -EINVAL; + int i; /* * Because restricted mem doesn't support page migration with @@ -1571,7 +1569,8 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, * TODO: Once restricted mem introduces callback on page migration, * implement it and remove get_page/put_page(). */ - get_page(pfn_to_page(pfn)); + for (i = 0; i < KVM_PAGES_PER_HPAGE(level); i++) + get_page(pfn_to_page(pfn + i)); if (likely(is_td_finalized(kvm_tdx))) return tdx_sept_page_aug(kvm, gfn, level, pfn); @@ -1588,11 +1587,9 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, gpa_t gpa = gfn_to_gpa(gfn); hpa_t hpa = pfn_to_hpa(pfn); hpa_t hpa_with_hkid; + int r = 0; u64 err; - - /* TODO: handle large pages. */ - if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm)) - return -EINVAL; + int i; if (unlikely(!is_hkid_assigned(kvm_tdx))) { /* @@ -1602,7 +1599,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); + tdx_unpin(kvm, pfn, level); return 0; } @@ -1619,22 +1616,27 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, return -EIO; } - hpa_with_hkid = set_hkid_to_hpa(hpa, (u16)kvm_tdx->hkid); - do { - /* - * TDX_OPERAND_BUSY can happen on locking PAMT entry. Because - * this page was removed above, other thread shouldn't be - * repeatedly operating on this page. Just retry loop. - */ - err = tdh_phymem_page_wbinvd(hpa_with_hkid); - } while (unlikely(err == (TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX))); - if (KVM_BUG_ON(err, kvm)) { - pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); - return -EIO; + for (i = 0; i < KVM_PAGES_PER_HPAGE(level); i++) { + hpa_with_hkid = set_hkid_to_hpa(hpa, (u16)kvm_tdx->hkid); + do { + /* + * TDX_OPERAND_BUSY can happen on locking PAMT entry. + * Because this page was removed above, other thread + * shouldn't be repeatedly operating on this page. + * Simple retry should work. + */ + err = tdh_phymem_page_wbinvd(hpa_with_hkid); + } while (unlikely(err == (TDX_OPERAND_BUSY | TDX_OPERAND_ID_RCX))); + if (KVM_BUG_ON(err, kvm)) { + pr_tdx_error(TDH_PHYMEM_PAGE_WBINVD, err, NULL); + r = -EIO; + } else { + tdx_clear_page(hpa, PAGE_SIZE); + tdx_unpin(kvm, pfn + i, PG_LEVEL_4K); + } + hpa += PAGE_SIZE; } - tdx_clear_page(hpa, PAGE_SIZE); - tdx_unpin(kvm, pfn); - return 0; + return r; } static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn, -- 2.25.1