Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp941021pxb; Wed, 6 Apr 2022 04:55:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+wO/sL3lHvUdB8Amt6rPi8SEKcRUtowYBz7akviwxxqKOXaABo3WU/JOaXlkwZAoS+huM X-Received: by 2002:a17:903:124a:b0:154:c7a4:9374 with SMTP id u10-20020a170903124a00b00154c7a49374mr8186739plh.68.1649246156994; Wed, 06 Apr 2022 04:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649246156; cv=none; d=google.com; s=arc-20160816; b=P3MizAn5jqzKakXnUuoamz0uhmGfw2Ps9mQGboGDFdbCKY9DDxVVTtZFVsI4QC2eY4 LHQDEyDQ/1Z3PtNyzvFhMRjwWzC7GX9gFdery+xWm8Epryt9nsrQBbBHN1D9iMmJoLTx Ul3GKbmb8V/QAg46WL2pXJCuU3W2JLfeF5JDs15PdH0LjynneeaPr1B8W/64jG541vg4 QIYJcY0/Mq5VNuiq0VcjBahIz8IwD7JVSG5mDki71ufIp38H+6RAZXSD1JlVlMsUkfQO Kw4krD0ju7JWl14ZQv17W0+8ytAyglqcr0++erlCuXicGXxdpv0rI62mZxkuZoiYSvO/ JCSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=8SLg2V/rMLaWaYTpjzTRS0k/QFp5WFOGLvjyUFHrKJY=; b=VF4ifDf9bW7+/l1iPC1OIU6d0ZVHt7zkvZ9aqHAj0CoFo3sprt4M9+UBmWroaIirBs Topu5z/xYM/0Jb7qhfz99rRO/GsVs3qBOLXUVgio/BlJP8PJYmgC/AnFOqJXSSteaBjc xRCqwWm3OrhA+ks7bPnfPlUaFD/eL86JPRTdLZUEP9MN9tIhdVfckmfnI9JZFNLL492Q bnQisnHrhUg2ANeS0Cf8AFiwYYnBsYCnb0XJCXJ+6D/7TKUC1524ELWONxk+Ulm+Li53 HpXDfOLFZSpjjU20aK+H/8MUwMWemTkpktr8aIP3OoUb/ug45d6ZIh87QUz+JHM9SQ93 Vzyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Vyje00f4; 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=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id o23-20020a63fb17000000b0038288abe5f7si2688237pgh.230.2022.04.06.04.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:55:56 -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=@redhat.com header.s=mimecast20190719 header.b=Vyje00f4; 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=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4ACEE64ECE3; Wed, 6 Apr 2022 03:18:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1589196AbiDFATB (ORCPT + 99 others); Tue, 5 Apr 2022 20:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1386145AbiDEOTO (ORCPT ); Tue, 5 Apr 2022 10:19:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4BD882BD5 for ; Tue, 5 Apr 2022 06:08:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649164115; h=from:from: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; bh=8SLg2V/rMLaWaYTpjzTRS0k/QFp5WFOGLvjyUFHrKJY=; b=Vyje00f4vebmWZc9RYkLZ3OC01/OSe0zL9Z8mZP2cIxa+p4AvHykIPwBN0CUjsbgxerMUi QGlLFiYo4sWaZ4/YXDenGJc6ay18Ywq4qtX0u5vf/JYQX8lQOag1d0LKgzRv5M9IQYINRn dDCCGi8+OSzkauBGEOhAj4nfsP89ZFE= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-650-lQQ0h4bkOma_4doqQbtq9Q-1; Tue, 05 Apr 2022 09:08:34 -0400 X-MC-Unique: lQQ0h4bkOma_4doqQbtq9Q-1 Received: by mail-wm1-f72.google.com with SMTP id h18-20020a05600c351200b0038e82e6321bso178099wmq.5 for ; Tue, 05 Apr 2022 06:08:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=8SLg2V/rMLaWaYTpjzTRS0k/QFp5WFOGLvjyUFHrKJY=; b=do49fFQ97AwtdAp8OSDlkZ3InwknJuwsjtwwB/xsQQDPequ84j72D4MUPweO+26nCu AmfGx5L6BMPvvzfmC/CZrxJwB2t7+pX4FpAkxmm7/hq0n9Y3Z4ZXszf+sRBDmJYUnRYp kK9z8M7n12b2t44yQysYbVAdpoCqejY86lpYUNzyWGq40jLL/pWJmsEQ7oDV8cToZUqK REHvguiF487E9P68afgMbuRHBqCAfuFwkRbKkQKfRCOo6bG+1uxAkb6vePJ6CHAIr8ic ti+vKePgQz05/WuvhuFeG7WVOZlcmHBmi91T96CkRCsxflI4ZQI2a1BGeIx0/36o93Wo 238Q== X-Gm-Message-State: AOAM532ZqCE2Esa6vrE8qD43tT4qAqkusVNukX5+rIIxSJ7q0HNZfTCR OgMiTcL3VUk5fmE0ADtk/SNqHF3pbOszuG3eMpnzz4T87WJwTX7UCPX9k0PBwE1W/aIdEWJFhfE oZPFv4iNMo2+TedM+3OG2hOkD X-Received: by 2002:a7b:cd03:0:b0:37b:fc83:a4e2 with SMTP id f3-20020a7bcd03000000b0037bfc83a4e2mr3047620wmj.193.1649164112937; Tue, 05 Apr 2022 06:08:32 -0700 (PDT) X-Received: by 2002:a7b:cd03:0:b0:37b:fc83:a4e2 with SMTP id f3-20020a7bcd03000000b0037bfc83a4e2mr3047597wmj.193.1649164112715; Tue, 05 Apr 2022 06:08:32 -0700 (PDT) Received: from [10.32.181.87] (nat-pool-mxp-t.redhat.com. [149.6.153.186]) by smtp.googlemail.com with ESMTPSA id m127-20020a1ca385000000b0038e6e7ac0b5sm2141613wme.38.2022.04.05.06.08.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Apr 2022 06:08:31 -0700 (PDT) Message-ID: <5fbf4af9-9e6c-3fc7-c021-869e93662845@redhat.com> Date: Tue, 5 Apr 2022 15:08:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [RFC PATCH v5 023/104] x86/cpu: Add helper functions to allocate/free MKTME keyid Content-Language: en-US To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson References: From: Paolo Bonzini In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 3/4/22 20:48, 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. > > Also export MKTME global keyid that is used to encrypt TDX module and its > memory. > > 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; Reviewed-by: Paolo Bonzini