Re: [PATCH v11 018/113] KVM: TDX: create/destroy VM structure

From: Sean Christopherson
Date: Thu Jan 19 2023 - 10:29:55 EST


On Thu, Jan 19, 2023, Huang, Kai wrote:
> On Thu, 2023-01-12 at 08:31 -0800, isaku.yamahata@xxxxxxxxx wrote:
> > +static void tdx_clear_page(unsigned long page_pa)
> > +{
> > + const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
> > + void *page = __va(page_pa);
> > + unsigned long i;
> > +
> > + if (!static_cpu_has(X86_FEATURE_MOVDIR64B)) {
> > + clear_page(page);
> > + return;
> > + }
>
> There might be below issues here:
>
> 1) The kernel says static_cpu_has() should only be used in fast patch where each
> cycle is counted, otherwise use boot_cpu_has(). I don't know whether here you
> should use static_cpu_has().

That documentation is stale[*], go ahead and use cpu_feature_enabled().

https://lore.kernel.org/all/20221107211505.8572-1-bp@xxxxxxxxx

> 2) IIUC a CPU feature bit can be cleared by 'clearcpuid=xxx' kernel command

As you note below, using clearcpuid taints the kernel, i.e. any breakage due to
clearcpuid is user error.

> line, so looks you should use CPUID directly otherwise the MOVDIR64B below can
> be unintentionally skipped. In practice w/o doing MOVDIR64B is fine since KeyID
> 0 doesn't have integrity enabled, but for the purpose you want to achieve
> checking real CPUID should be better.
>
> But maybe you don't want to do CPUID check here each time when reclaiming a
> page. In that case you can do CPUID during module initialization and cache
> whether MOVDIR64B is truly present. static_key is a good fit for this purpose
> too I think.
>
> But I am also seeing below in the kernel documentation:
>
> clearcpuid=X[,X...] [X86]
> ......
> Note that using this option will taint your kernel.
> Also note that user programs calling CPUID directly
> or using the feature without checking anything
> will still see it. This just prevents it from
> being used by the kernel or shown in /proc/cpuinfo.
> Also note the kernel might malfunction if you disable
> some critical bits.
>
> So the kernel is claiming using this will taint the kernel and it can even
> malfunction. So maybe it's OK to use static_cpu_has()/boot_cpu_has().