[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230627113752.djyxgt4io6aiixwy@box.shutemov.name>
Date: Tue, 27 Jun 2023 14:37:52 +0300
From: "kirill.shutemov@...ux.intel.com" <kirill.shutemov@...ux.intel.com>
To: "Huang, Kai" <kai.huang@...el.com>
Cc: "kvm@...r.kernel.org" <kvm@...r.kernel.org>,
"Raj, Ashok" <ashok.raj@...el.com>,
"Hansen, Dave" <dave.hansen@...el.com>,
"david@...hat.com" <david@...hat.com>,
"bagasdotme@...il.com" <bagasdotme@...il.com>,
"Luck, Tony" <tony.luck@...el.com>,
"ak@...ux.intel.com" <ak@...ux.intel.com>,
"Wysocki, Rafael J" <rafael.j.wysocki@...el.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"Christopherson,, Sean" <seanjc@...gle.com>,
"mingo@...hat.com" <mingo@...hat.com>,
"pbonzini@...hat.com" <pbonzini@...hat.com>,
"linux-mm@...ck.org" <linux-mm@...ck.org>,
"tglx@...utronix.de" <tglx@...utronix.de>,
"Yamahata, Isaku" <isaku.yamahata@...el.com>,
"Chatre, Reinette" <reinette.chatre@...el.com>,
"nik.borisov@...e.com" <nik.borisov@...e.com>,
"hpa@...or.com" <hpa@...or.com>,
"peterz@...radead.org" <peterz@...radead.org>,
"Shahar, Sagi" <sagis@...gle.com>,
"imammedo@...hat.com" <imammedo@...hat.com>,
"bp@...en8.de" <bp@...en8.de>, "Gao, Chao" <chao.gao@...el.com>,
"Brown, Len" <len.brown@...el.com>,
"sathyanarayanan.kuppuswamy@...ux.intel.com"
<sathyanarayanan.kuppuswamy@...ux.intel.com>,
"Huang, Ying" <ying.huang@...el.com>,
"x86@...nel.org" <x86@...nel.org>,
"Williams, Dan J" <dan.j.williams@...el.com>
Subject: Re: [PATCH v12 08/22] x86/virt/tdx: Get information about TDX module
and TDX-capable memory
On Tue, Jun 27, 2023 at 10:45:33AM +0000, Huang, Kai wrote:
> On Tue, 2023-06-27 at 12:51 +0300, kirill.shutemov@...ux.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
Powered by blists - more mailing lists