Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2235375pxb; Wed, 30 Mar 2022 20:13:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkzXaMyz54uKaVGD5Wmdwt4ev9wPg174hR+1NOEJCKb8bIVjtVIC6zeXt808FBgp7Pz9xa X-Received: by 2002:a63:5c4:0:b0:398:c83e:f931 with SMTP id 187-20020a6305c4000000b00398c83ef931mr50756pgf.359.1648696426686; Wed, 30 Mar 2022 20:13:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648696426; cv=none; d=google.com; s=arc-20160816; b=zEbnMqbrar34YLe14Nclej+MAwlkb882fVRi3A9ql2bfJp7DpC4zRC8oJxJLCjoRdB iFSsJCEJl34GuqCdgU0oxBjxoGe8Aewq8SarmfnG8XZJwstKFRr/tvRoUbNHG8DMxe2M 5fe34T1tBCaBmw1+4s3p5AA0uUJj4DIPU6efho52uWyCgqwrIWDpHcJiapQtOpqx30uG 2v34V++3Yu8aJkkSI4vx8bJ0HFWWWD3PT7nwNsBafhWX4AcUAuKlqyx8gGLTuPRZPixi pNcZZRzmG7Q1qMPb/xgM79IZ4dq1klDCOU6Wl5tKIe48Ba23knG25SJbq0aV5Spu8jCp QCgg== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=0r0W5ZWnk71w34Vv6vtWW2epBl3l9UlIyPb9eNlcM0Y=; b=u4C9rylDz29zOZwSrMd0WHBum7sbsLFYP2sr4qHrYXc8PWGTrosg+FaQOKveh/0LzG Q1yYQiRnn7WZ+vsR8LjE1PL8Lm1+AurVXILGFAM0lahboKys+mCGML0/GcsN/NNM3o31 IC7PLXfR/yhkHXPBnDWedJkom6hwRZG9ZCyb83/pLPgbCWCbLFD0T7U+9UlRtTz96vlW 5lVEqEGbfUVOpIkZgJw4F6qj3Mpfuz1R1POM3qGgHoGbT+KGbMl2R6l7xON8Mlj0bz5w dmO6Y6NaeqrLC6Byc5qYUXYxjDCRcbAKKxVKWTlUdBge6Ua3K6nRaim9RfHkDbIph7mA c3XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d+MR2P5i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j36-20020a635964000000b00382b21d7a89si23224820pgm.99.2022.03.30.20.13.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 20:13:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d+MR2P5i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 50B929E9D7; Wed, 30 Mar 2022 19:48:13 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243375AbiCaBXG (ORCPT + 99 others); Wed, 30 Mar 2022 21:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352304AbiCaBXA (ORCPT ); Wed, 30 Mar 2022 21:23:00 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA5F62E699; Wed, 30 Mar 2022 18:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1648689671; x=1680225671; h=message-id:subject:from:to:cc:date:in-reply-to: references:mime-version:content-transfer-encoding; bh=QKRTWdiGnee9ZO7InXWkfsawt1zuvqAXyHGl5Xwe2fQ=; b=d+MR2P5iypt3c/qA4QMTTAC4fDdax4HA03D0hi7/4eoKgmhSzwnDy9Ry SuOCro5k0GlxtB6neNdAWvwjJmVkD2bmd54wW2PqRLDrCDYoAdIUpYp6g dBgPe3BOihMpQHWI8uX2kWuSlDqLvyuyXwFx/N+hRL7V4LBHqSTePhqYH V37C5imvcDRsH2uThnEixa92Ka1cm6upEp6tKkm/24J/+01c0nN5/T1Eo M0vYfWHqhGk8QaG0sghwAvwxW5xsZ0yaYX2cXBDbk6rcTOcWWBfNPVabe tvjbe070Knr/C+dtF8b2JamvnhJ/gwJTXDUMdjiuBVG7rJlT2DtMmBR9q A==; X-IronPort-AV: E=McAfee;i="6200,9189,10302"; a="257265196" X-IronPort-AV: E=Sophos;i="5.90,224,1643702400"; d="scan'208";a="257265196" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2022 18:21:11 -0700 X-IronPort-AV: E=Sophos;i="5.90,224,1643702400"; d="scan'208";a="654268188" Received: from dhathawa-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.254.53.226]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2022 18:21:08 -0700 Message-ID: <2386151bc0a42b2eda895d85b459bf7930306694.camel@intel.com> Subject: Re: [RFC PATCH v5 023/104] x86/cpu: Add helper functions to allocate/free MKTME keyid From: Kai Huang To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Date: Thu, 31 Mar 2022 14:21:06 +1300 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-1.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Fri, 2022-03-04 at 11:48 -0800, isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > MKTME keyid is assigned to guest TD. The memory controller encrypts guest > TD memory with key id. Add helper functions to allocate/free MKTME keyid > so that TDX KVM assign keyid. Using MKTME keyid is wrong, at least not accurate I think. We should use explicitly use "TDX private KeyID", which is clearly documented in the spec: https://software.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf Also, description of IA32_MKTME_KEYID_PARTITIONING MSR clearly says TDX private KeyIDs span the range (NUM_MKTME_KIDS+1) through (NUM_MKTME_KIDS+NUM_TDX_PRIV_KIDS). So please just use TDX private KeyID here. > > Also export MKTME global keyid that is used to encrypt TDX module and its > memory. This needs explanation why the global keyID needs to be exported. > > Signed-off-by: Isaku Yamahata > --- > arch/x86/include/asm/tdx.h | 6 ++++++ > arch/x86/virt/vmx/tdx.c | 33 ++++++++++++++++++++++++++++++++- > 2 files changed, 38 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h > index 9a8dc6afcb63..73bb472bd515 100644 > --- a/arch/x86/include/asm/tdx.h > +++ b/arch/x86/include/asm/tdx.h > @@ -139,6 +139,9 @@ int tdx_detect(void); > int tdx_init(void); > bool platform_has_tdx(void); > const struct tdsysinfo_struct *tdx_get_sysinfo(void); > +u32 tdx_get_global_keyid(void); > +int tdx_keyid_alloc(void); > +void tdx_keyid_free(int keyid); > #else > static inline void tdx_detect_cpu(struct cpuinfo_x86 *c) { } > static inline int tdx_detect(void) { return -ENODEV; } > @@ -146,6 +149,9 @@ static inline int tdx_init(void) { return -ENODEV; } > static inline bool platform_has_tdx(void) { return false; } > struct tdsysinfo_struct; > static inline const struct tdsysinfo_struct *tdx_get_sysinfo(void) { return NULL; } > +static inline u32 tdx_get_global_keyid(void) { return 0; }; > +static inline int tdx_keyid_alloc(void) { return -EOPNOTSUPP; } > +static inline void tdx_keyid_free(int keyid) { } > #endif /* CONFIG_INTEL_TDX_HOST */ > > #endif /* !__ASSEMBLY__ */ > diff --git a/arch/x86/virt/vmx/tdx.c b/arch/x86/virt/vmx/tdx.c > index e45f188479cb..d714106321d4 100644 > --- a/arch/x86/virt/vmx/tdx.c > +++ b/arch/x86/virt/vmx/tdx.c > @@ -113,7 +113,13 @@ static int tdx_cmr_num; > static struct tdsysinfo_struct tdx_sysinfo; > > /* TDX global KeyID to protect TDX metadata */ > -static u32 tdx_global_keyid; > +static u32 __read_mostly tdx_global_keyid; > + > +u32 tdx_get_global_keyid(void) > +{ > + return tdx_global_keyid; > +} > +EXPORT_SYMBOL_GPL(tdx_get_global_keyid); > > static bool enable_tdx_host; > > @@ -189,6 +195,31 @@ static void detect_seam(struct cpuinfo_x86 *c) > detect_seam_ap(c); > } > > +/* TDX KeyID pool */ > +static DEFINE_IDA(tdx_keyid_pool); > + > +int tdx_keyid_alloc(void) > +{ > + if (WARN_ON_ONCE(!tdx_keyid_start || !tdx_keyid_num)) > + return -EINVAL; > + > + /* The first keyID is reserved for the global key. */ > + return ida_alloc_range(&tdx_keyid_pool, tdx_keyid_start + 1, > + tdx_keyid_start + tdx_keyid_num - 1, > + GFP_KERNEL); > +} > +EXPORT_SYMBOL_GPL(tdx_keyid_alloc); > + > +void tdx_keyid_free(int keyid) > +{ > + /* keyid = 0 is reserved. */ > + if (!keyid || keyid <= 0) > + return; > + > + ida_free(&tdx_keyid_pool, keyid); > +} > +EXPORT_SYMBOL_GPL(tdx_keyid_free); > + > static void detect_tdx_keyids_bsp(struct cpuinfo_x86 *c) > { > u64 keyid_part;