From mboxrd@z Thu Jan 1 00:00:00 1970 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 E7E28C54EBD for ; Fri, 6 Jan 2023 22:21:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 787038E0003; Fri, 6 Jan 2023 17:21:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7373A8E0001; Fri, 6 Jan 2023 17:21:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FF4B8E0003; Fri, 6 Jan 2023 17:21:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 50F058E0001 for ; Fri, 6 Jan 2023 17:21:06 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 257D7160F2B for ; Fri, 6 Jan 2023 22:21:06 +0000 (UTC) X-FDA: 80325795732.27.85E5AEF Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf07.hostedemail.com (Postfix) with ESMTP id EFD3E40008 for ; Fri, 6 Jan 2023 22:21:03 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dItFYgO0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of dave.hansen@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=dave.hansen@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673043664; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2tn6kbjCb4YqK3+AOwyPwtEdJYVdMfLe+9ditDN2u4s=; b=S2KoSmRhP+vrK/3s2Sms8vIUdjvOovsDe7D2vu5103XBxHbFKQaW+AoTrHQKJrtI5VFGEW 29xNR9qYLG7WvR0igqvbl6MuEvgLPTq6IPnZnwECOHHK0xsm0cwr3oTseVXBYsD/Xr8IMU 9qroUwE/gyQYpGuirdMiz7aFJ2Y4bKA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=dItFYgO0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of dave.hansen@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=dave.hansen@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673043664; a=rsa-sha256; cv=none; b=5tnzDSzm1yi3mFvAjI3v8WQG6WzbGCpdETm/WtsLvZkLRI4hij9bbMhLHyI1EDOIAomR7Z ZDKaR+7HcgtEPZbSnI19s+qdbvSnGgina71t0lMYE7GW0aHuvylmMDqqS0QVK9vlbNpxE0 CpXrQ2xKmgcLJjYsb2PL0sNZNB6bSu4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673043664; x=1704579664; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=fsHOPPQFiulqGu7o78qgPixVmSbGcMUL81E6/CjfVe4=; b=dItFYgO0cM8way7pRZm5bTG1tBuiTi3CKicVkRtHqJiZEBCycDWmF5Jl EIQwhCM80Q/6QrcjGSyBEWY2QhtuieWvh0CEz8zceOKn0HVvJsDZcoA8e 0sPG71s+3+zdeGA6h2w1JVqCwyG+l+rbBYPEKrJy+6XTitDeqEGEOB9Jr anXSmkPMJJ9AayfHxlCWZWPi+LB2qEvNMpD/zFi2OvE27doPGSXkyEea8 TZcq5t/AHDKci+eL6k/F0zCWF1Q5KT0uGmEHxv588fgsIyFCs5l9uzaAP uJ9aJ0/tvN7DnZ+fJ7iBTPbEfr2hrRxiX9uaK3bohpLeeTH895wTLQ5Ym A==; X-IronPort-AV: E=McAfee;i="6500,9779,10582"; a="310370550" X-IronPort-AV: E=Sophos;i="5.96,306,1665471600"; d="scan'208";a="310370550" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jan 2023 14:21:02 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10582"; a="606025616" X-IronPort-AV: E=Sophos;i="5.96,306,1665471600"; d="scan'208";a="606025616" Received: from xiangyuy-mobl.amr.corp.intel.com (HELO [10.212.251.186]) ([10.212.251.186]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jan 2023 14:21:01 -0800 Message-ID: Date: Fri, 6 Jan 2023 14:21:01 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v8 12/16] x86/virt/tdx: Designate the global KeyID and configure the TDX module Content-Language: en-US To: Kai Huang , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, 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, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com References: From: Dave Hansen In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: EFD3E40008 X-Stat-Signature: 9a5h74959u1bcp45h9sowubmigsu3q7y X-HE-Tag: 1673043663-332229 X-HE-Meta: U2FsdGVkX1/H2XIL3nf6tLdiRVoqfO/DigNTtgkooAH9tOBMIrXUCVydybRvnLpPZalc2NSpyK0bj/MJNfYfXPfw9NvjUksZ4c+C92Td9VeXg14P+Fc69wtZtnTIzFWkMDnQnNPPn+yV8DDom+EmD3DhQFLp0L8rAg75SNnYrMsQMJ1DGYUlnsddGbMlFEfjwnQ2uSc37UAJwtkJAasnFwsQ3sUvN+vPYd8NUC6XcLV9wCt11RNQNYPoBm7YX2fi5K2zGXHvM72jH2TKhOY8+IVBoDkuJj74ox8H7GEJH+cg75Qzbj5ce9AriSZifZphna8PySdPi+9lWpSa5uc/6tLVPn2rGJgSVigTFfk5PujoGVBO2U3gIHmnsiAuOKLLCcZs+9XWdnok7usnku5xkGGj6VUerTCGLFeqv4rfyWlJsl/r09eH7fb2Xdn7zfaGLxy9MJPkOTgdMXspOY6rM2RaHCdkGIRBZ8GWPdyZ2ok4O0cImdQ1KU4acq/iK8koIiwqv3qHRp+UmXbX+4nrCTjEAAI3UyjS7Y4FuywffqmGmrs661MeDcJmC9+KoIBaPkqKMIbEFrNK0Ekss4hvkrso1CDe8BQ4nNl//3TZRkkVcGVRSM7lfz+wi6kp780D/uWpxoObHM4DvFnDK2xKRh0YjoJ97t2hDbEE+PhIMXwkGzHMzFcPtPulq3ts3nPNYTkgKzTIC3cy737qe0TclL0O9OoljE7HUM582JbxKrb764TW7GjYateEJE3qm6eUNEOLmdY2mWgzg0wTxvkT6kyFFYtgSoxDeHXAUq3w3j/STorocsoewwlA2P7FOg4H80XXQX5HgGTcUdfFXl78TVFAkATVOZtjOrJiltjw/9w= 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: On 12/8/22 22:52, Kai Huang wrote: > After a list of "TD Memory Regions" (TDMRs) has been constructed to > cover all TDX-usable memory regions, the next step is to pick up a TDX > private KeyID as the "global KeyID" (which protects, i.e. TDX module's > metadata), and configure it to the TDX module along with the TDMRs. For whatever reason, whenever I see "i.e." in a changelog, it's usually going off the rails. This is no exception. Let's also get rid of the passive voice: The next step After constructing a list of "TD Memory Regions" (TDMRs) to cover all TDX-usable memory regions is to designate a TDX private KeyID as the "global KeyID". This KeyID is used by the TDX module for mapping things like the PAMT and other TDX metadata. This KeyID is passed to the TDX module at the same time as the TDMRs. > To keep things simple, just use the first TDX KeyID as the global KeyID. > --- > arch/x86/virt/vmx/tdx/tdx.c | 41 +++++++++++++++++++++++++++++++++++-- > arch/x86/virt/vmx/tdx/tdx.h | 2 ++ > 2 files changed, 41 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c > index 620b35e2a61b..ab961443fed5 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.c > +++ b/arch/x86/virt/vmx/tdx/tdx.c > @@ -916,6 +916,36 @@ static int construct_tdmrs(struct list_head *tmb_list, > return ret; > } > > +static int config_tdx_module(struct tdmr_info_list *tdmr_list, u64 global_keyid) > +{ > + u64 *tdmr_pa_array, *p; > + size_t array_sz; > + int i, ret; > + > + /* > + * TDMRs are passed to the TDX module via an array of physical > + * addresses of each TDMR. The array itself has alignment > + * requirement. > + */ > + array_sz = tdmr_list->nr_tdmrs * sizeof(u64) + > + TDMR_INFO_PA_ARRAY_ALIGNMENT - 1; One other way of doing this which might be a wee bit less messy: array_sz = roundup_pow_of_two(array_sz); if (array_sz < TDMR_INFO_PA_ARRAY_ALIGNMENT) array_sz = TDMR_INFO_PA_ARRAY_ALIGNMENT; Since that keeps 'array_sz' at a power-of-two, then kzalloc() will give you all the alignment you need, except if the array is too small, in which case you can just bloat it to the alignment requirement. This would get rid of the PTR_ALIGN() below too. Your choice. What you have works too. > + p = kzalloc(array_sz, GFP_KERNEL); > + if (!p) > + return -ENOMEM; > + > + tdmr_pa_array = PTR_ALIGN(p, TDMR_INFO_PA_ARRAY_ALIGNMENT); > + for (i = 0; i < tdmr_list->nr_tdmrs; i++) > + tdmr_pa_array[i] = __pa(tdmr_entry(tdmr_list, i)); > + > + ret = seamcall(TDH_SYS_CONFIG, __pa(tdmr_pa_array), tdmr_list->nr_tdmrs, > + global_keyid, 0, NULL, NULL); > + > + /* Free the array as it is not required anymore. */ > + kfree(p); > + > + return ret; > +} > + > static int init_tdx_module(void) > { > /* > @@ -960,17 +990,24 @@ static int init_tdx_module(void) > if (ret) > goto out_free_tdmrs; > > + /* > + * Use the first private KeyID as the global KeyID, and pass > + * it along with the TDMRs to the TDX module. > + */ > + ret = config_tdx_module(&tdmr_list, tdx_keyid_start); > + if (ret) > + goto out_free_pamts; This is "consuming" tdx_keyid_start. Does it need to get incremented since the first guest can't use this KeyID now? > /* > * TODO: > * > - * - Pick up one TDX private KeyID as the global KeyID. > - * - Configure the TDMRs and the global KeyID to the TDX module. > * - Configure the global KeyID on all packages. > * - Initialize all TDMRs. > * > * Return error before all steps are done. > */ > ret = -EINVAL; > +out_free_pamts: > if (ret) > tdmrs_free_pamt_all(&tdmr_list); > else > diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h > index d0c762f1a94c..4d2edd477480 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.h > +++ b/arch/x86/virt/vmx/tdx/tdx.h > @@ -20,6 +20,7 @@ > * TDX module SEAMCALL leaf functions > */ > #define TDH_SYS_INFO 32 > +#define TDH_SYS_CONFIG 45 > > struct cmr_info { > u64 base; > @@ -96,6 +97,7 @@ struct tdmr_reserved_area { > } __packed; > > #define TDMR_INFO_ALIGNMENT 512 > +#define TDMR_INFO_PA_ARRAY_ALIGNMENT 512 > > struct tdmr_info { > u64 base;