Re: [PATCH 3/4] x86/kexec(): Reset TDX private memory on platforms with TDX erratum

From: Dave Hansen
Date: Thu Feb 01 2024 - 11:57:41 EST


On 2/1/24 06:39, Kirill A. Shutemov wrote:
>> On 1/02/2024 6:03 am, Kirill A. Shutemov wrote:
>>> On Wed, Jan 31, 2024 at 01:21:39PM -0800, Dave Hansen wrote:
>>>>> #ifdef CONFIG_KEXEC_JUMP
>>>>> if (image->preserve_context)
>>>>> save_processor_state();
>>>>> + else
>>>>> + tdx_reset_memory();
>>>>> +#else
>>>>> + tdx_reset_memory();
>>>>> #endif
..
> +void save_processor_state(void);
> +void restore_processor_state(void);
> +
> #ifdef CONFIG_SUSPEND
> extern suspend_state_t pm_suspend_target_state;
> extern suspend_state_t mem_sleep_current;
> @@ -491,8 +494,6 @@ static inline int is_hibernate_resume_dev(dev_t dev) { return 0; }
> extern struct mutex system_transition_mutex;
>
> #ifdef CONFIG_PM_SLEEP
> -void save_processor_state(void);
> -void restore_processor_state(void);

It's a little funky that we've got a #ifdef CONFIG_KEXEC_JUMP in the .c
file and then we're dodging around an #ifdef CONFIG_PM_SLEEP in the
header. This is one of the reasons we shouldn't be putting #ifdefs in
c files in the first place. But I digress...

Either way, if you focus on getting the dang #ifdef out of the main code
flow, the rest will fall in place easily. Heck, if you even do this in
the x86 kexec code:

static void kexec_save_processor_start(image)
{
#ifdef CONFIG_KEXEC_JUMP
if (image->preserve_context)
save_processor_state();
#endif
}

it'll leave you with:

kexec_save_processor_start(image);

/* Give a good reason here */
if (!image->preserve_context)
tdx_reset_memory();

which is *FINE*. No funky #ifdefs, indentation or else's dangling about.