From patchwork Mon Mar 6 14:13:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13161226 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F64EC61DA4 for ; Mon, 6 Mar 2023 14:15:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CD40280002; Mon, 6 Mar 2023 09:15:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 97CCB6B0073; Mon, 6 Mar 2023 09:15:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81DA0280002; Mon, 6 Mar 2023 09:15:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 740916B0071 for ; Mon, 6 Mar 2023 09:15:26 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1A9BB1A06FA for ; Mon, 6 Mar 2023 14:15:26 +0000 (UTC) X-FDA: 80538671052.02.77498F7 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by imf05.hostedemail.com (Postfix) with ESMTP id A96F1100022 for ; Mon, 6 Mar 2023 14:15:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XmkXFWrg; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678112124; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YRRma2OhPr390AGf4Gk3ep5P9W7KsdniIp6ESwb/ncE=; b=W9ZcEblvyCeL+16M7SzP4eKjHzmb4xLIiWZmczBx8BTpjR8b7kAXTFYzYYKYGLPMoiMJq/ MXG+a+/hZ01Acm1Bt4CWNCf/RA7gyOmSuV6ywUl+bY7lbKMMjpMBLKcCafhDhexmDoJZXE 5i6DXir0sTyubqJi4ndqfZh5BIE4LPg= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XmkXFWrg; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678112124; a=rsa-sha256; cv=none; b=RcnMIOJ9SEdodlPDB4tyYr2v82CcATcyyowbC0+cF+zfy57kwVtXPWlE2jf1qqaD8o6GBO UBcQ5d7uztJUKvo4xD7pt6dRx5XQzrC9WXdjwKY/LqWR5h+rAhB9APuOQwVjjpO4XZwI15 cUum69IkSqi4PpFZxQRgsAAHEWppstQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678112123; x=1709648123; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xci5WG0k+f/0Um3+k+Oe8afMa9mtUe76NlmilpanBT8=; b=XmkXFWrgd2P+iPI2OHyT9QTqkJZrY8T+6EaWApKzcNnqQ4kkZSpuQw6D VClOI+Bwx2ixFzOW3HvcTMH0Wj0UwkOP5IH6YFoNIIgN9mvkCJFO0cdXi AAjdQe8x8FWOuZCdkuRjzaIC8GXCHSofBNMMhXh6QvdULbGQicpPdfP/2 xZ2SqxjTw6Pd3a32tNyJRQfNjWm3wNwci/SYnxpxDRfKT4+EDVLETMnL3 rW5spVMjurL7UaB5Uklk0DOACYbxBSUteOPucdbAkPiTaaxKhQr8WZDd0 uF+T8NBaiVnR6AmqNbtiv2MvgVve1sTC4VZLVptW7iPa5RBwBV2Hlt2yy w==; X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="337080240" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="337080240" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 06:15:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="765232434" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="765232434" Received: from jwhisle1-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.92.57]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 06:15:18 -0800 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, dave.hansen@intel.com, peterz@infradead.org, tglx@linutronix.de, seanjc@google.com, pbonzini@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, kirill.shutemov@linux.intel.com, ying.huang@intel.com, reinette.chatre@intel.com, len.brown@intel.com, tony.luck@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, david@redhat.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v10 11/16] x86/virt/tdx: Designate reserved areas for all TDMRs Date: Tue, 7 Mar 2023 03:13:56 +1300 Message-Id: <602d53e58b15d0593a489f59ef27953d6b2b0ece.1678111292.git.kai.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: A96F1100022 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 6tz96ybwbupybj7dptmcnwfo6axo4zq5 X-HE-Tag: 1678112123-21287 X-HE-Meta: U2FsdGVkX1/Lx3hZUqSvchUOsqInlWSQf9kwOaDEM6bPO67aqifU6FacszZA6RlHxw+jJ7ULmDuQUzhg3/UApwng+kb+Rm8baSSAvSw17hpXAKYAfUH78p96vL+xUI+f2iUcBajNt36ZntwPLWQFDXE5u9zeozcm3T4yX17eOzGK5DFc8WwkFYTX3VTf0Fb0cuN4jgeiIVWReVX4mukiyAVphX0y8Dcv1m1oCXWkUaRakY+lnveHYiFYTjqcRhk2lTBdhY/fyXRla0eyRO8OswQV4vWydiU7nkD5/t1l00C43WBL/KbMkf3QS27NLkm4G1gmwwg+LfwlNQ0hydtsdvZbOCpViYBeY6ftzB2rnpSqTPUfK68Ll+EhITY36/yPiOfO59r0+4TLpZe4tNgz0om2xeJ5EyKB9rRZ3CQwjGFqhRD7j2Kh8qbfTbXXHAxgcDubYMV3rxlb+j0KjZfNlaTcdLLbCe/zjI76LyYuyw5OYQe/Wgvs566KysK1vrv1tmOl/JuGKWuUeLyKRJO2VYlybo+ja8oMa8CQ2DX44IIp79HwHIh4KHZ+YoQIW18rbCGR03tbfbIG973aaf+FOVxPGwK7Gj3g5F03PUaYK/t1x4S+uq7FQpNvbCfl8GMkl0fzbzNMksZcZyiqBlOI8fPJ2h4abxaAYEoKPArLJA906x/MabfowNbytV0byelLRoOFE5MuzZKKbIlIbOElnTRSeUmCkV6dE9I/CXy+qGe+Jm+awAf6HZB/gweoJYUmUvP7kKy3iWWqxuK3+CIwRu+/BSKKdzGcaOA+Kpbus+XT/JAGY7HfAkxR/5aEfi6C4Pfqw0I+2PiyBvcRa1dodwLiixvxnQYWka1/ejC+n0a+U3SfqBSzhWzzzxAnNmJmk0RUWE/3/drl10KjUllqqwLbJmUJDIcpoQFmnh4qdYOTbmQCVTPwsaYRFZYyFu789lLrl97ooFfySO1g/OF 9YcAYLS9 HteI4sPgQmQ1tzbgj+oBIZXkG2MsKBoiBhhzSqNnsf0qyfFs2jtlffee5zkoXzh062VACerq94b3s/eyIHek1PTgLCnMpZQaUUsDhAsOrytXno6Jvi2S+tJcQgDpVsoo9OgVqj4L0yZDiU1+3ZLvKOZQbwqxSpD+yklFgZvJ431O7LKI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: As the last step of constructing TDMRs, populate reserved areas for all TDMRs. For each TDMR, put all memory holes within this TDMR to the reserved areas. And for all PAMTs which overlap with this TDMR, put all the overlapping parts to reserved areas too. Signed-off-by: Kai Huang Reviewed-by: Isaku Yamahata --- v9 -> v10: - No change. v8 -> v9: - Added comment around 'tdmr_add_rsvd_area()' to point out it doesn't do optimization to save reserved areas. (Dave). v7 -> v8: (Dave) - "set_up" -> "populate" in function name change (Dave). - Improved comment suggested by Dave. - Other changes due to 'struct tdmr_info_list'. v6 -> v7: - No change. v5 -> v6: - Rebase due to using 'tdx_memblock' instead of memblock. - Split tdmr_set_up_rsvd_areas() into two functions to handle memory hole and PAMT respectively. - Added Isaku's Reviewed-by. --- arch/x86/virt/vmx/tdx/tdx.c | 220 ++++++++++++++++++++++++++++++++++-- 1 file changed, 212 insertions(+), 8 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 8f66cab1902e..99d2e8d939d3 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -792,6 +793,210 @@ static unsigned long tdmrs_count_pamt_pages(struct tdmr_info_list *tdmr_list) return pamt_npages; } +static int tdmr_add_rsvd_area(struct tdmr_info *tdmr, int *p_idx, u64 addr, + u64 size, u16 max_reserved_per_tdmr) +{ + struct tdmr_reserved_area *rsvd_areas = tdmr->reserved_areas; + int idx = *p_idx; + + /* Reserved area must be 4K aligned in offset and size */ + if (WARN_ON(addr & ~PAGE_MASK || size & ~PAGE_MASK)) + return -EINVAL; + + if (idx >= max_reserved_per_tdmr) { + pr_warn("initialization failed: TDMR [0x%llx, 0x%llx): reserved areas exhausted.\n", + tdmr->base, tdmr_end(tdmr)); + return -ENOSPC; + } + + /* + * Consume one reserved area per call. Make no effort to + * optimize or reduce the number of reserved areas which are + * consumed by contiguous reserved areas, for instance. + */ + rsvd_areas[idx].offset = addr - tdmr->base; + rsvd_areas[idx].size = size; + + *p_idx = idx + 1; + + return 0; +} + +/* + * Go through @tmb_list to find holes between memory areas. If any of + * those holes fall within @tdmr, set up a TDMR reserved area to cover + * the hole. + */ +static int tdmr_populate_rsvd_holes(struct list_head *tmb_list, + struct tdmr_info *tdmr, + int *rsvd_idx, + u16 max_reserved_per_tdmr) +{ + struct tdx_memblock *tmb; + u64 prev_end; + int ret; + + /* + * Start looking for reserved blocks at the + * beginning of the TDMR. + */ + prev_end = tdmr->base; + list_for_each_entry(tmb, tmb_list, list) { + u64 start, end; + + start = PFN_PHYS(tmb->start_pfn); + end = PFN_PHYS(tmb->end_pfn); + + /* Break if this region is after the TDMR */ + if (start >= tdmr_end(tdmr)) + break; + + /* Exclude regions before this TDMR */ + if (end < tdmr->base) + continue; + + /* + * Skip over memory areas that + * have already been dealt with. + */ + if (start <= prev_end) { + prev_end = end; + continue; + } + + /* Add the hole before this region */ + ret = tdmr_add_rsvd_area(tdmr, rsvd_idx, prev_end, + start - prev_end, + max_reserved_per_tdmr); + if (ret) + return ret; + + prev_end = end; + } + + /* Add the hole after the last region if it exists. */ + if (prev_end < tdmr_end(tdmr)) { + ret = tdmr_add_rsvd_area(tdmr, rsvd_idx, prev_end, + tdmr_end(tdmr) - prev_end, + max_reserved_per_tdmr); + if (ret) + return ret; + } + + return 0; +} + +/* + * Go through @tdmr_list to find all PAMTs. If any of those PAMTs + * overlaps with @tdmr, set up a TDMR reserved area to cover the + * overlapping part. + */ +static int tdmr_populate_rsvd_pamts(struct tdmr_info_list *tdmr_list, + struct tdmr_info *tdmr, + int *rsvd_idx, + u16 max_reserved_per_tdmr) +{ + int i, ret; + + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) { + struct tdmr_info *tmp = tdmr_entry(tdmr_list, i); + unsigned long pamt_start_pfn, pamt_npages; + u64 pamt_start, pamt_end; + + tdmr_get_pamt(tmp, &pamt_start_pfn, &pamt_npages); + /* Each TDMR must already have PAMT allocated */ + WARN_ON_ONCE(!pamt_npages || !pamt_start_pfn); + + pamt_start = PFN_PHYS(pamt_start_pfn); + pamt_end = PFN_PHYS(pamt_start_pfn + pamt_npages); + + /* Skip PAMTs outside of the given TDMR */ + if ((pamt_end <= tdmr->base) || + (pamt_start >= tdmr_end(tdmr))) + continue; + + /* Only mark the part within the TDMR as reserved */ + if (pamt_start < tdmr->base) + pamt_start = tdmr->base; + if (pamt_end > tdmr_end(tdmr)) + pamt_end = tdmr_end(tdmr); + + ret = tdmr_add_rsvd_area(tdmr, rsvd_idx, pamt_start, + pamt_end - pamt_start, + max_reserved_per_tdmr); + if (ret) + return ret; + } + + return 0; +} + +/* Compare function called by sort() for TDMR reserved areas */ +static int rsvd_area_cmp_func(const void *a, const void *b) +{ + struct tdmr_reserved_area *r1 = (struct tdmr_reserved_area *)a; + struct tdmr_reserved_area *r2 = (struct tdmr_reserved_area *)b; + + if (r1->offset + r1->size <= r2->offset) + return -1; + if (r1->offset >= r2->offset + r2->size) + return 1; + + /* Reserved areas cannot overlap. The caller must guarantee. */ + WARN_ON_ONCE(1); + return -1; +} + +/* + * Populate reserved areas for the given @tdmr, including memory holes + * (via @tmb_list) and PAMTs (via @tdmr_list). + */ +static int tdmr_populate_rsvd_areas(struct tdmr_info *tdmr, + struct list_head *tmb_list, + struct tdmr_info_list *tdmr_list, + u16 max_reserved_per_tdmr) +{ + int ret, rsvd_idx = 0; + + ret = tdmr_populate_rsvd_holes(tmb_list, tdmr, &rsvd_idx, + max_reserved_per_tdmr); + if (ret) + return ret; + + ret = tdmr_populate_rsvd_pamts(tdmr_list, tdmr, &rsvd_idx, + max_reserved_per_tdmr); + if (ret) + return ret; + + /* TDX requires reserved areas listed in address ascending order */ + sort(tdmr->reserved_areas, rsvd_idx, sizeof(struct tdmr_reserved_area), + rsvd_area_cmp_func, NULL); + + return 0; +} + +/* + * Populate reserved areas for all TDMRs in @tdmr_list, including memory + * holes (via @tmb_list) and PAMTs. + */ +static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list, + struct list_head *tmb_list, + u16 max_reserved_per_tdmr) +{ + int i; + + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) { + int ret; + + ret = tdmr_populate_rsvd_areas(tdmr_entry(tdmr_list, i), + tmb_list, tdmr_list, max_reserved_per_tdmr); + if (ret) + return ret; + } + + return 0; +} + /* * Construct a list of TDMRs on the preallocated space in @tdmr_list * to cover all TDX memory regions in @tmb_list based on the TDX module @@ -811,14 +1016,13 @@ static int construct_tdmrs(struct list_head *tmb_list, sysinfo->pamt_entry_size); if (ret) return ret; - /* - * TODO: - * - * - Designate reserved areas for each TDMR. - * - * Return -EINVAL until constructing TDMRs is done - */ - return -EINVAL; + + ret = tdmrs_populate_rsvd_areas_all(tdmr_list, tmb_list, + sysinfo->max_reserved_per_tdmr); + if (ret) + tdmrs_free_pamt_all(tdmr_list); + + return ret; } static int init_tdx_module(void)