Re: [PATCH v4 3/7] x86/virt/tdx: Mark memory cache state incoherent when making SEAMCALL

From: Huang, Kai
Date: Tue Jul 22 2025 - 22:00:08 EST


On Tue, 2025-07-22 at 22:52 +0800, Gao, Chao wrote:
> > +static __always_inline u64 do_seamcall(sc_func_t func, u64 fn,
> > + struct tdx_module_args *args)
> > +{
> > + u64 ret;
> > +
> > + lockdep_assert_preemption_disabled();
> > +
> > + /*
> > + * SEAMCALLs are made to the TDX module and can generate dirty
> > + * cachelines of TDX private memory. Mark cache state incoherent
> > + * so that the cache can be flushed during kexec.
> > + *
> > + * This needs to be done before actually making the SEAMCALL,
> > + * because kexec-ing CPU could send NMI to stop remote CPUs,
> > + * in which case even disabling IRQ won't help here.
> > + */
> > + this_cpu_write(cache_state_incoherent, true);
> > +
> > + ret = func(fn, args);
> > +
> > + return ret;
>
> @ret can be dropped here. Just
>
> return func(fn, args);
>
> should work.

Yeah thanks will do.

>
> And tracking cache incoherent state at the per-CPU level seems to add
> unnecessary complexity. It requires a new do_seamcall() wrapper, setting the
> flag on every seamcall rather than just the first one (I'm not concerned about
> performance; it just feels silly), and using preempt_disable()/enable(). In my
> view, per-CPU tracking at most saves a WBINVD on a CPU that never runs
> SEAMCALLs during KEXEC, which is quite marginal. Did I miss any other benefits?

The cache state is percpu thus a percpu boolean is a natural fit. Besides
the benefit you mentioned, it fits better if there are other cases which
could also lead to an incoherent state:

https://lore.kernel.org/lkml/eb2e3b02-cf5e-4848-8f1d-9f3af8f9c96b@xxxxxxxxx/

Setting the boolean in the SEAMCALL common code makes the logic quite
simple:

If you ever do a SEAMCALL, mark the cache in incoherent state.

Please see Dave's comment here:

https://lore.kernel.org/lkml/31e17bc8-2e9e-4e93-a912-3d54826e59d0@xxxxxxxxx/

The new code around the common SEAMCALL is pretty marginal comparing to
the SEAMCALL itself (as you said), and it's pretty straightforward, i.e.,
logically less error prone IMHO, so I am not seeing it silly.