Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp517665lqc; Fri, 8 Mar 2024 04:41:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX312xWlPq9fQhw5fmLDb+Hjuw1ta/KJO01k3r+GrNB2ba+TyjhFpgPcFZXTrjmgZiApOIOOyC1eiUhrfzGu9aesz23YodWGhyzwzLBKQ== X-Google-Smtp-Source: AGHT+IFBhuu3wRQRGrWvvu+dWGkBdkhmHMWEP2wgrEvqE2lZflakYjOkNelyGqgtJAxYCbpH7Vk4 X-Received: by 2002:a17:906:ca4b:b0:a3e:8809:525c with SMTP id jx11-20020a170906ca4b00b00a3e8809525cmr13451830ejb.37.1709901709922; Fri, 08 Mar 2024 04:41:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709901709; cv=pass; d=google.com; s=arc-20160816; b=RJ7zh4A4l6wOdeSboV3QgyNHaql7VBOMCK075w9a32I+lPdBkgnxOtKGs9/R4WBa2T T9GNB36YDxAFDW6bMMSboltgF2qxS6D3VS6uzAglAzWKTEkqBvhQHYqSUBJoycDunMpX OE6JRj/CirW290rtuMO6teA56QyQuOXdKw7ZJ4evhH5eeV/Chhwf1yPxvoLodYVMzWhE L4pXFB8V+8S1B1DUCVcZ2FTgMqQqWKsAtRCWinenA2fqVOnA/TZjH4eC291mMkSx0Rhl HseCbVF/AUc3PAde5P/u+MqJJ5zXrF4SzAih4LroBIsLDvF2z7buLLB1VUWJrS6P3vTO h8HQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id:dkim-signature; bh=lMOBxdmbfNXoZvaaCNuudmfkQaG+IpyNIUSCIE2qQ+c=; fh=L/tkX2cOqHKwWqIWddzRExEvwgd471IJL29QYab3GZo=; b=RPCSmFrFW9+xCw9+RrAg46CegTmO7L5eqgjtTQd8yVZw5HBKuzD9M5wuhXQHlbxccD 5cava6jbP2ye90ykWMxTL38hxE7ltxmE9dsIDS25GZux9V3NtXB9Hf4gcZfBk0rBY71R 1aO9xFQyXe9hI9MtA9/5uisjnyOgK/io7WJ/JJCzuB8nlHRxwHtUrZ7HK24Dft6b8+/A 2t5rB4gLqv7fisM5wWQb/cHDRP5shQrtfzkNxvHRGLSYT14PS7fSs+8g7+WfKrcY8sc4 NdySbrsCPWKPfwx0JYniDjJFtemIKTdvNBtsM3Tt0+vi45r3y5Qk/Y/4RhxfN8aMnEn3 Ywfw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hQwJWNDO; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-96653-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96653-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id bq18-20020a170906d0d200b00a45f64b2108si161391ejb.359.2024.03.08.04.41.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 04:41:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96653-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hQwJWNDO; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-96653-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96653-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2B74F1F22215 for ; Fri, 8 Mar 2024 08:32:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 146E9208B8; Fri, 8 Mar 2024 08:32:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hQwJWNDO" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40BDC846C; Fri, 8 Mar 2024 08:31:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709886721; cv=none; b=MHQnrkuauVPxzJiDRYGqLe2ZRKaYcatDTDFMEh0GqWgYuQlfD83kXU91sRXtsvucKTajPs7mCFiY19hpFeH1Z/0wO2N6/cNvYmOjzTMr2kJput/Gfcf9n64NIAqeXvV+TEsEN6lLR/jY+b7pQJ6kO7cfrlm2sHTYvy1XOL0UI3w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709886721; c=relaxed/simple; bh=VHPu8e8Le9aHrIgBfTFS9cP5IuSy27McirEOhMqWaZ8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=etiOQY66vdhaOxoND8oJDwe+HcgOphHzsfNTOaeP4GT9e/QDsLTbR9Cr1rPJxwhCqzOKGVeXh0qZ3GUyZJVpXFZJePch3Q5BbBBmoylSXLUw8F0Npm0NCobXzgWemLs7G8YpRyjXqAqrUqdYl62cBEOlW1rYlVmjeALu/52OobQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hQwJWNDO; arc=none smtp.client-ip=198.175.65.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709886720; x=1741422720; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=VHPu8e8Le9aHrIgBfTFS9cP5IuSy27McirEOhMqWaZ8=; b=hQwJWNDOd0OcGvLHL71ZN9DQYKrUEB39wLY4VZZhlc5LSp3SnnTBfcxi ZVx8k3lON0Cl0zQebabbk0Ajle/+U92Axhfv7jLtjVsnw4Mz1Z20PnMkX 54TBkZEn8fjCo5DRjs3xTxnDSqzbZO3ydbzC4uqFe5BYcrW/u3ywspMd8 K6kMGYB6EqKj7+b3l+v/Q0Qe2dtC15XYnLDSIOnBkM4v5CXnWdzJP1C1t WjCPsX757ezZ4yr88SgQqsN1to0scvqbTJCr0NQ/IyCc4o6eenBzVoAoY +VEj08MvY2nFwYnZClATB/Lzpa2rPEon8bz9CsmNcmXJlKqgHilhAlMbo w==; X-IronPort-AV: E=McAfee;i="6600,9927,11006"; a="22050026" X-IronPort-AV: E=Sophos;i="6.07,109,1708416000"; d="scan'208";a="22050026" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 00:31:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,109,1708416000"; d="scan'208";a="33546033" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.124.242.47]) ([10.124.242.47]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 00:31:55 -0800 Message-ID: <3d331d15-bcbf-412e-be50-9bd80715b1a2@linux.intel.com> Date: Fri, 8 Mar 2024 16:31:52 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v19 005/130] x86/virt/tdx: Export global metadata read infrastructure To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, "Kirill A . Shutemov" References: From: Binbin Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/26/2024 4:25 PM, isaku.yamahata@intel.com wrote: > From: Kai Huang > > KVM will need to read a bunch of non-TDMR related metadata to create and > run TDX guests. Export the metadata read infrastructure for KVM to use. > > Specifically, export two helpers: > > 1) The helper which reads multiple metadata fields to a buffer of a > structure based on the "field ID -> structure member" mapping table. > > 2) The low level helper which just reads a given field ID. > > The two helpers cover cases when the user wants to cache a bunch of > metadata fields to a certain structure and when the user just wants to > query a specific metadata field on demand. They are enough for KVM to > use (and also should be enough for other potential users). > > Signed-off-by: Kai Huang > Reviewed-by: Kirill A. Shutemov > Signed-off-by: Isaku Yamahata Reviewed-by: Binbin Wu > --- > arch/x86/include/asm/tdx.h | 22 ++++++++++++++++++++++ > arch/x86/virt/vmx/tdx/tdx.c | 25 ++++++++----------------- > 2 files changed, 30 insertions(+), 17 deletions(-) > > diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h > index eba178996d84..709b9483f9e4 100644 > --- a/arch/x86/include/asm/tdx.h > +++ b/arch/x86/include/asm/tdx.h > @@ -116,6 +116,28 @@ static inline u64 sc_retry(sc_func_t func, u64 fn, > int tdx_cpu_enable(void); > int tdx_enable(void); > const char *tdx_dump_mce_info(struct mce *m); > + > +struct tdx_metadata_field_mapping { > + u64 field_id; > + int offset; > + int size; > +}; > + > +#define TD_SYSINFO_MAP(_field_id, _struct, _member) \ > + { .field_id = MD_FIELD_ID_##_field_id, \ > + .offset = offsetof(_struct, _member), \ > + .size = sizeof(typeof(((_struct *)0)->_member)) } > + > +/* > + * Read multiple global metadata fields to a buffer of a structure > + * based on the "field ID -> structure member" mapping table. > + */ > +int tdx_sys_metadata_read(const struct tdx_metadata_field_mapping *fields, > + int nr_fields, void *stbuf); > + > +/* Read a single global metadata field */ > +int tdx_sys_metadata_field_read(u64 field_id, u64 *data); > + > #else > static inline void tdx_init(void) { } > static inline int tdx_cpu_enable(void) { return -ENODEV; } > diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c > index a19adc898df6..dc21310776ab 100644 > --- a/arch/x86/virt/vmx/tdx/tdx.c > +++ b/arch/x86/virt/vmx/tdx/tdx.c > @@ -251,7 +251,7 @@ static int build_tdx_memlist(struct list_head *tmb_list) > return ret; > } > > -static int read_sys_metadata_field(u64 field_id, u64 *data) > +int tdx_sys_metadata_field_read(u64 field_id, u64 *data) > { > struct tdx_module_args args = {}; > int ret; > @@ -270,6 +270,7 @@ static int read_sys_metadata_field(u64 field_id, u64 *data) > > return 0; > } > +EXPORT_SYMBOL_GPL(tdx_sys_metadata_field_read); > > /* Return the metadata field element size in bytes */ > static int get_metadata_field_bytes(u64 field_id) > @@ -295,7 +296,7 @@ static int stbuf_read_sys_metadata_field(u64 field_id, > if (WARN_ON_ONCE(get_metadata_field_bytes(field_id) != bytes)) > return -EINVAL; > > - ret = read_sys_metadata_field(field_id, &tmp); > + ret = tdx_sys_metadata_field_read(field_id, &tmp); > if (ret) > return ret; > > @@ -304,19 +305,8 @@ static int stbuf_read_sys_metadata_field(u64 field_id, > return 0; > } > > -struct field_mapping { > - u64 field_id; > - int offset; > - int size; > -}; > - > -#define TD_SYSINFO_MAP(_field_id, _struct, _member) \ > - { .field_id = MD_FIELD_ID_##_field_id, \ > - .offset = offsetof(_struct, _member), \ > - .size = sizeof(typeof(((_struct *)0)->_member)) } > - > -static int read_sys_metadata(struct field_mapping *fields, int nr_fields, > - void *stbuf) > +int tdx_sys_metadata_read(const struct tdx_metadata_field_mapping *fields, > + int nr_fields, void *stbuf) > { > int i, ret; > > @@ -331,6 +321,7 @@ static int read_sys_metadata(struct field_mapping *fields, int nr_fields, > > return 0; > } > +EXPORT_SYMBOL_GPL(tdx_sys_metadata_read); > > #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ > TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) > @@ -338,7 +329,7 @@ static int read_sys_metadata(struct field_mapping *fields, int nr_fields, > static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) > { > /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ > - const struct field_mapping fields[] = { > + const struct tdx_metadata_field_mapping fields[] = { > TD_SYSINFO_MAP_TDMR_INFO(MAX_TDMRS, max_tdmrs), > TD_SYSINFO_MAP_TDMR_INFO(MAX_RESERVED_PER_TDMR, max_reserved_per_tdmr), > TD_SYSINFO_MAP_TDMR_INFO(PAMT_4K_ENTRY_SIZE, pamt_entry_size[TDX_PS_4K]), > @@ -347,7 +338,7 @@ static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) > }; > > /* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ > - return read_sys_metadata(fields, ARRAY_SIZE(fields), tdmr_sysinfo); > + return tdx_sys_metadata_read(fields, ARRAY_SIZE(fields), tdmr_sysinfo); > } > > /* Calculate the actual TDMR size */