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 9F8DCC001B0 for ; Tue, 27 Jun 2023 11:38:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A96C8D0002; Tue, 27 Jun 2023 07:38:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 358DE8D0001; Tue, 27 Jun 2023 07:38:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2218F8D0002; Tue, 27 Jun 2023 07:38:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 126338D0001 for ; Tue, 27 Jun 2023 07:38:24 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CEFB51C8A5E for ; Tue, 27 Jun 2023 11:38:23 +0000 (UTC) X-FDA: 80948329686.28.D6D4351 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf20.hostedemail.com (Postfix) with ESMTP id 067C11C0005 for ; Tue, 27 Jun 2023 11:38:20 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k638G7nA; spf=none (imf20.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687865901; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=etaClFRdECbBSiR5opIL2L4ep+Tkj6/dFLFETee89QQ=; b=iEByd9Pn2BizoB7ua0TFKiNSjHWsuEp8zFDJMwJ+tbESe2ks8mPd1vv5V66yhI43Y5AXnJ 4IS3kRXe4nJAkzqW2v9dlr6OVUYHRMjLpBouFNefA2ZwMMYY4XPUZXbt+I5t0MgG8+PAaD ytZi8+qPMGIA7Oz7I1b3Sckc2udLbBc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687865901; a=rsa-sha256; cv=none; b=nLqy8IGbzqtbYw4SbnLkMBVOPY7lgf1yyV0FL3QLycFqesssVE4V0qEz+sY7qOuiozrEUf jws3AR6BOOGOiMKzBfHCi3NZpVhJfYvqTC9TJ8jhvqAAKom8r9uPVOwwXgPbpxydkrj6v/ idALwcXWTkUDUTEfTStm/ECobgOo6tg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k638G7nA; spf=none (imf20.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687865901; x=1719401901; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=pblGJiVM9kZOdgL3JsS2LvFnPRI4Ksm9ReZEZ35fwAc=; b=k638G7nAIQpgivpndQx44n9J33hAmukqN2TxSFyB6YaUKAmHOckdQXoO r+YXI5qoFnz87GmlreWBRO2exK/uKU5Mx7S+RSBq+nstnZcpZfs77L4ux qekhzxBTaBP4OWKGrmFAHNCEp328EdpM19X1qW/MpIIzlzmwBLBG6b4to TvB+sr+k+qldpJJBcBfN22GIv/TGFIlKSwG3ZoBbENtLBUFUPNGaOsqEU qoe3c3v+z4S/q6NjkhDPF5KJYkX/o+HYeLyAEPz361SW8kDDAU74z8oyi Cpqht8Ba5PRJNwI8AnnCh/kHPpRHuoAkJCaonY8lv1Zd+zgbn7k9ljPue Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="425216428" X-IronPort-AV: E=Sophos;i="6.01,162,1684825200"; d="scan'208";a="425216428" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2023 04:38:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="710620519" X-IronPort-AV: E=Sophos;i="6.01,162,1684825200"; d="scan'208";a="710620519" Received: from rbhaumik-mobl2.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.217.121]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2023 04:37:55 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 05756103738; Tue, 27 Jun 2023 14:37:53 +0300 (+03) Date: Tue, 27 Jun 2023 14:37:52 +0300 From: "kirill.shutemov@linux.intel.com" To: "Huang, Kai" Cc: "kvm@vger.kernel.org" , "Raj, Ashok" , "Hansen, Dave" , "david@redhat.com" , "bagasdotme@gmail.com" , "Luck, Tony" , "ak@linux.intel.com" , "Wysocki, Rafael J" , "linux-kernel@vger.kernel.org" , "Christopherson,, Sean" , "mingo@redhat.com" , "pbonzini@redhat.com" , "linux-mm@kvack.org" , "tglx@linutronix.de" , "Yamahata, Isaku" , "Chatre, Reinette" , "nik.borisov@suse.com" , "hpa@zytor.com" , "peterz@infradead.org" , "Shahar, Sagi" , "imammedo@redhat.com" , "bp@alien8.de" , "Gao, Chao" , "Brown, Len" , "sathyanarayanan.kuppuswamy@linux.intel.com" , "Huang, Ying" , "x86@kernel.org" , "Williams, Dan J" Subject: Re: [PATCH v12 08/22] x86/virt/tdx: Get information about TDX module and TDX-capable memory Message-ID: <20230627113752.djyxgt4io6aiixwy@box.shutemov.name> References: <20230627095124.nhiypr6ivi4kdfrw@box.shutemov.name> <49f197f7756ac05b99717a9f63d56cfb860ab88b.camel@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <49f197f7756ac05b99717a9f63d56cfb860ab88b.camel@intel.com> X-Stat-Signature: 99y36gxc7g53o88u4x6s6u5xuq39th1k X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 067C11C0005 X-Rspam-User: X-HE-Tag: 1687865900-189961 X-HE-Meta: U2FsdGVkX1/ufRkf27jQL2keq4ycD3Z/5Q+dKozZ+HLD33v/y/E+H8gn6Hj1V2mv0L2xBSJTPB0rKtt76NLKwltot9newK+O5s3hwtj0x7Kh9Gkuv1Ufyoh9LosRIZi01k1447NtgUBnBsQoYm0a1YaXgul4MndwOWzPA5zNRE/Rm+tzHUkP2vIeuTos1WxMpXVUiXQPS7Qz08TME4SCwMKRZfKBm57YYHlXbyDb3Kq3qCLWaIabgu56UAwGILGjqvackJhM9elfI99+NhWcY5pd1qkpFXWw4uN2AIhyBJvfsX9bX9dPuRFAoB6dIRf1wO/1uyTOpg58iU6PsJoNXQuh+u2JTG1eHK63UE9s08Go1R1QTHF4iASpamhikbVQGFwDHBEBXniXJ9C2KlGoZISBTp23QQg33uybi5xprmq/lZU39p26w8R0PssXXwju626Oes95LAynq9t+XaX1tBd/QanLNlkSLTrQXD9cCtKkEU5tmBzxzaN1hVu/Y4hZsBcg+MCGU2sCXdA60Ggp7OcY77onVl9fDCPuu3n/Lj2fllTQca9WaDIkW10oyrrW2A/5o6K9cA1GGNEhwpw+4DHw6uuZc7miAnGpa7WzGYqW7qrJ5lxyeYu5ymtRtkN/+Kqzb627L/tvDhJrC7q1vlqR2GRHnGDHUi3OqNTJmu35cHGfAoDYvJ3vIvCUGviUz+8ziX1MY2V2f9VsNUyUM3CtmK/McqWfc4JpsMq+5f89zxTHbcOA24isuCYFfeYdiLNgd2cs65xMK/Bkj6crFdhHA34zqecxsgF1k4gJZYZ+t22cbqGrqhhtbNqF3ZSABTmlnKQdSuOEe1w5AGteFG0XhAhFaXcZT0CLlLLfQEdoh6Av0BrAoX4vjyY2LBCaB9L3SOS8+bfjHYiQA6Q/WzW8zJD0+D0I5SUMBadLWjNTRwVEpqPuKZ5LCrBSirnw1xCSdfMYlIu4PcsvVtQ XMwB3rw9 Pwws3z+uJf+eYpct3hmnIW4NtKPo1eFrts7N5Urx/nE9BSLpLRpvKOuPvtqYuOs0TXQ9qMzTVrUBmz40to8PFgwNKgtXCX08ksE6GdFdourIXq+TLTFkzOOTYhMRQAiLDjAPXHVaQw2NF4ZAOyH2UuWT0k3s1FthmrsKYVjUbfxTgsI3+554cGHgXdQ== 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 Tue, Jun 27, 2023 at 10:45:33AM +0000, Huang, Kai wrote: > On Tue, 2023-06-27 at 12:51 +0300, kirill.shutemov@linux.intel.com wrote: > > On Tue, Jun 27, 2023 at 02:12:38AM +1200, Kai Huang wrote: > > > static int init_tdx_module(void) > > > { > > > + struct tdsysinfo_struct *sysinfo; > > > + struct cmr_info *cmr_array; > > > + int ret; > > > + > > > + /* > > > + * Get the TDSYSINFO_STRUCT and CMRs from the TDX module. > > > + * > > > + * The buffers of the TDSYSINFO_STRUCT and the CMR array passed > > > + * to the TDX module must be 1024-bytes and 512-bytes aligned > > > + * respectively. Allocate one page to accommodate them both and > > > + * also meet those alignment requirements. > > > + */ > > > + sysinfo = (struct tdsysinfo_struct *)__get_free_page(GFP_KERNEL); > > > + if (!sysinfo) > > > + return -ENOMEM; > > > + cmr_array = (struct cmr_info *)((unsigned long)sysinfo + PAGE_SIZE / 2); > > > + > > > + BUILD_BUG_ON(PAGE_SIZE / 2 < TDSYSINFO_STRUCT_SIZE); > > > + BUILD_BUG_ON(PAGE_SIZE / 2 < sizeof(struct cmr_info) * MAX_CMRS); > > > > This works, but why not just use slab for this? kmalloc has 512 and 1024 > > pools already and you won't waste memory for rounding up. > > > > Something like this: > > > > sysinfo = kmalloc(TDSYSINFO_STRUCT_SIZE, GFP_KERNEL); > > if (!sysinfo) > > return -ENOMEM; > > > > cmr_array_size = sizeof(struct cmr_info) * MAX_CMRS; > > > > /* CMR array has to be 512-aligned */ > > cmr_array_size = round_up(cmr_array_size, 512); > > Should we define a macro for 512 > > +#define CMR_INFO_ARRAY_ALIGNMENT 512 > > And get rid of this comment? AFAICT Dave didn't like such comment mentioning > 512-bytes aligned if we have a macro for that. Good idea. > > cmr_array = kmalloc(cmr_array_size, GFP_KERNEL); > > if (!cmr_array) { > > kfree(sysinfo); > > return -ENOMEM; > > } > > > > ? > > > > I confess the reason I used __get_free_page() was to avoid having to allocate > twice, and in case of failure, I need to handle additional memory free. But I > can do if you think it's clearer? Less trickery is always cleaner. Especially if the trick is not justified. > I wouldn't worry about wasting memory. The buffer is freed anyway for now. > Long-termly it's just 4K. -- Kiryl Shutsemau / Kirill A. Shutemov