diff mbox series

[v8,8/9] x86/mtrr: Avoid repeated save of MTRRs on boot-time CPU bringup

Message ID 20230209154156.266385-9-usama.arif@bytedance.com (mailing list archive)
State Superseded
Headers show
Series Parallel CPU bringup for x86_64 | expand

Commit Message

Usama Arif Feb. 9, 2023, 3:41 p.m. UTC
From: David Woodhouse <dwmw@amazon.co.uk>

There's no need to repeatedly save the BSP's MTRRs for each AP being
brought up at boot time. And no need to use smp_call_function_single()
even for the one time they do need to be saved.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Usama Arif <usama.arif@bytedance.com>
---
 arch/x86/kernel/cpu/mtrr/mtrr.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Thomas Gleixner Feb. 9, 2023, 6:31 p.m. UTC | #1
On Thu, Feb 09 2023 at 15:41, Usama Arif wrote:
>  void mtrr_save_state(void)
>  {
> +	static bool mtrr_saved;
>  	int first_cpu;
>  
>  	if (!mtrr_enabled())
>  		return;
>  
> +	if (system_state < SYSTEM_RUNNING) {
> +		if (!mtrr_saved) {
> +			mtrr_save_fixed_ranges(NULL);
> +			mtrr_saved = true;
> +		}
> +		return;
> +	}
> +
>  	first_cpu = cpumask_first(cpu_online_mask);
>  	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);

So why is this relevant after the initial bringup? The BP MTRRs have
been saved already above, no?

Thanks,

        tglx
Usama Arif Feb. 9, 2023, 8:32 p.m. UTC | #2
On 09/02/2023 18:31, Thomas Gleixner wrote:
> On Thu, Feb 09 2023 at 15:41, Usama Arif wrote:
>>   void mtrr_save_state(void)
>>   {
>> +	static bool mtrr_saved;
>>   	int first_cpu;
>>   
>>   	if (!mtrr_enabled())
>>   		return;
>>   
>> +	if (system_state < SYSTEM_RUNNING) {
>> +		if (!mtrr_saved) {
>> +			mtrr_save_fixed_ranges(NULL);
>> +			mtrr_saved = true;
>> +		}
>> +		return;
>> +	}
>> +
>>   	first_cpu = cpumask_first(cpu_online_mask);
>>   	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
> 
> So why is this relevant after the initial bringup? The BP MTRRs have
> been saved already above, no?
> 
> Thanks,
> 
>          tglx

I will let David confirm if this is correct and why he did it, but this 
is what I thought while reviewing before posting v4:

- At initial boot (system_state < SYSTEM_RUNNING), when mtrr_save_state 
is called in do_cpu_up at roughly the same time so MTRR is going to be 
the same, we can just save it once and then reuse for other secondary 
cores as it wouldn't have changed for the rest of the do_cpu_up calls.

- When the system is running and you offline and then online a CPU, you 
want to make sure that hotplugged CPU gets the current MTRR (which might 
have changed since boot?), incase the MTRR has changed after the system 
has been booted, you save the MTRR of the first online CPU. When the 
hotplugged CPU runs its initialisation code, its fixed-range MTRRs will 
be updated with the newly saved fixed-range MTRRs.

So mainly for hotplug, but will let David confirm.

Thanks,
Usama
David Woodhouse Feb. 9, 2023, 8:37 p.m. UTC | #3
On Thu, 2023-02-09 at 20:32 +0000, Usama Arif wrote:
> 
> I will let David confirm if this is correct and why he did it, but this 
> is what I thought while reviewing before posting v4:
> 
> - At initial boot (system_state < SYSTEM_RUNNING), when mtrr_save_state 
> is called in do_cpu_up at roughly the same time so MTRR is going to be 
> the same, we can just save it once and then reuse for other secondary
> cores as it wouldn't have changed for the rest of the do_cpu_up calls.
> 
> - When the system is running and you offline and then online a CPU, you 
> want to make sure that hotplugged CPU gets the current MTRR (which might 
> have changed since boot?), incase the MTRR has changed after the system 
> has been booted, you save the MTRR of the first online CPU. When the 
> hotplugged CPU runs its initialisation code, its fixed-range MTRRs will 
> be updated with the newly saved fixed-range MTRRs.
> 
> So mainly for hotplug, but will let David confirm.

Sounds about right.
Thomas Gleixner Feb. 9, 2023, 11:50 p.m. UTC | #4
On Thu, Feb 09 2023 at 20:32, Usama Arif wrote:
> On 09/02/2023 18:31, Thomas Gleixner wrote:
>>>   	first_cpu = cpumask_first(cpu_online_mask);
>>>   	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
>> 
>> So why is this relevant after the initial bringup? The BP MTRRs have
>> been saved already above, no?
>> 
>
> I will let David confirm if this is correct and why he did it, but this 
> is what I thought while reviewing before posting v4:
>
> - At initial boot (system_state < SYSTEM_RUNNING), when mtrr_save_state 
> is called in do_cpu_up at roughly the same time so MTRR is going to be 
> the same, we can just save it once and then reuse for other secondary 
> cores as it wouldn't have changed for the rest of the do_cpu_up calls.
>
> - When the system is running and you offline and then online a CPU, you 
> want to make sure that hotplugged CPU gets the current MTRR (which might 
> have changed since boot?), incase the MTRR has changed after the system 
> has been booted, you save the MTRR of the first online CPU. When the 
> hotplugged CPU runs its initialisation code, its fixed-range MTRRs will 
> be updated with the newly saved fixed-range MTRRs.

I knew that already :) But seriously:

If the MTRRs are changed post boot then the cached values want to be
updated too.

We are not making these changes just to satisfy some fast boot
challenge. They have to make sense in general.

And this does not amke sense at all.

Thanks,

        tglx
David Woodhouse Feb. 10, 2023, 8:55 a.m. UTC | #5
On Fri, 2023-02-10 at 00:50 +0100, Thomas Gleixner wrote:
> On Thu, Feb 09 2023 at 20:32, Usama Arif wrote:
> > On 09/02/2023 18:31, Thomas Gleixner wrote:
> > > >         first_cpu = cpumask_first(cpu_online_mask);
> > > >         smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
> > > 
> > > So why is this relevant after the initial bringup? The BP MTRRs have
> > > been saved already above, no?
> > > 
> > 
> > I will let David confirm if this is correct and why he did it, but this 
> > is what I thought while reviewing before posting v4:
> > 
> > - At initial boot (system_state < SYSTEM_RUNNING), when mtrr_save_state 
> > is called in do_cpu_up at roughly the same time so MTRR is going to be 
> > the same, we can just save it once and then reuse for other secondary 
> > cores as it wouldn't have changed for the rest of the do_cpu_up calls.
> > 
> > - When the system is running and you offline and then online a CPU, you 
> > want to make sure that hotplugged CPU gets the current MTRR (which might 
> > have changed since boot?), incase the MTRR has changed after the system 
> > has been booted, you save the MTRR of the first online CPU. When the 
> > hotplugged CPU runs its initialisation code, its fixed-range MTRRs will 
> > be updated with the newly saved fixed-range MTRRs.
> 
> I knew that already :) But seriously:
> 
> If the MTRRs are changed post boot then the cached values want to be
> updated too.

They are, aren't they? The only way we come out of mtrr_save_state()
without calling mtrr_save_fixed_ranges() — either directly or via
smp_call_function_single() — is if they've already been saved once
*and* system_state < SYSTEM_RUNNING.

I suppose we could make that clearer by moving the definition of the
mtrr_saved flags inside the if (system_state < SYSTEM_RUNNING) block?

@@ -721,11 +721,20 @@ void __init mtrr_bp_init(void)
  */
 void mtrr_save_state(void)
 {
 	int first_cpu;
 
 	if (!mtrr_enabled())
 		return;
 
+	if (system_state < SYSTEM_RUNNING) {
+		static bool mtrr_saved;
+		if (!mtrr_saved) {
+			mtrr_save_fixed_ranges(NULL);
+			mtrr_saved = true;
+		}
+		return;
+	}
+
 	first_cpu = cpumask_first(cpu_online_mask);
 	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
 }
Usama Arif Feb. 13, 2023, 3:19 p.m. UTC | #6
On 10/02/2023 08:55, David Woodhouse wrote:
> On Fri, 2023-02-10 at 00:50 +0100, Thomas Gleixner wrote:
>> On Thu, Feb 09 2023 at 20:32, Usama Arif wrote:
>>> On 09/02/2023 18:31, Thomas Gleixner wrote:
>>>>>          first_cpu = cpumask_first(cpu_online_mask);
>>>>>          smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
>>>>
>>>> So why is this relevant after the initial bringup? The BP MTRRs have
>>>> been saved already above, no?
>>>>
>>>
>>> I will let David confirm if this is correct and why he did it, but this
>>> is what I thought while reviewing before posting v4:
>>>
>>> - At initial boot (system_state < SYSTEM_RUNNING), when mtrr_save_state
>>> is called in do_cpu_up at roughly the same time so MTRR is going to be
>>> the same, we can just save it once and then reuse for other secondary
>>> cores as it wouldn't have changed for the rest of the do_cpu_up calls.
>>>
>>> - When the system is running and you offline and then online a CPU, you
>>> want to make sure that hotplugged CPU gets the current MTRR (which might
>>> have changed since boot?), incase the MTRR has changed after the system
>>> has been booted, you save the MTRR of the first online CPU. When the
>>> hotplugged CPU runs its initialisation code, its fixed-range MTRRs will
>>> be updated with the newly saved fixed-range MTRRs.
>>
>> I knew that already :) But seriously:
>>
>> If the MTRRs are changed post boot then the cached values want to be
>> updated too.
> 

I had previously only done smpboot time measurements for the whole 
patchset, but I tested the patchset without this commit and it doesn't 
make a difference to smpboot time as its negligable work to read those 
MTRR MSRs into mtrr_state.fixed_ranges.
This commit is also independent of parallel smp bringup, similar to 
reusing timer calibration so I think it could be considered as a 
separate patchset if needed. I will post the next revision without this 
commit, but here is my view on MTRR save/restore (which shouldn't matter 
for the next revision...).

If the MTRR changes on a running system, there might be a bug during 
hotplug in the original code that handles MTRR? which is also carried 
over in this patch.
 From what I can see, MTRR is only saved+restored during initial boot, 
hotplugging CPU and __save/__restore_processor_state() (used in creating 
image for hibernation, suspend, kexec...). So if for e.g. in a running 
system (that has not hibernated, suspended, kexeced), if MTRR for CPU0 
(first_cpu) changed post-boot and CPU3 is hotplugged, only MTRR for CPU3 
is updated and CPU0 and CPU3 will hold the same value, while the rest of 
the CPUs will have the older first-boot value? This behavior will happen 
with or without this patch. I think this is what Thomas is referring to 
above when he says that the cached values want to be updated? But the 
issue is present in the original code as well.

Thanks!
Usama

> They are, aren't they? The only way we come out of mtrr_save_state()
> without calling mtrr_save_fixed_ranges() — either directly or via
> smp_call_function_single() — is if they've already been saved once
> *and* system_state < SYSTEM_RUNNING.
> 
> I suppose we could make that clearer by moving the definition of the
> mtrr_saved flags inside the if (system_state < SYSTEM_RUNNING) block?
> 
> @@ -721,11 +721,20 @@ void __init mtrr_bp_init(void)
>    */
>   void mtrr_save_state(void)
>   {
>   	int first_cpu;
>   
>   	if (!mtrr_enabled())
>   		return;
>   
> +	if (system_state < SYSTEM_RUNNING) {
> +		static bool mtrr_saved;
> +		if (!mtrr_saved) {
> +			mtrr_save_fixed_ranges(NULL);
> +			mtrr_saved = true;
> +		}
> +		return;
> +	}
> +
>   	first_cpu = cpumask_first(cpu_online_mask);
>   	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
>   }
>
Thomas Gleixner Feb. 13, 2023, 8:42 p.m. UTC | #7
On Mon, Feb 13 2023 at 15:19, Usama Arif wrote:
> On 10/02/2023 08:55, David Woodhouse wrote:
>> On Fri, 2023-02-10 at 00:50 +0100, Thomas Gleixner wrote:
>>> I knew that already :) But seriously:
>>>
>>> If the MTRRs are changed post boot then the cached values want to be
>>> updated too.
>> 
> I had previously only done smpboot time measurements for the whole 
> patchset, but I tested the patchset without this commit and it doesn't 
> make a difference to smpboot time as its negligable work to read those 
> MTRR MSRs into mtrr_state.fixed_ranges.
> This commit is also independent of parallel smp bringup, similar to 
> reusing timer calibration so I think it could be considered as a 
> separate patchset if needed. I will post the next revision without this 
> commit, but here is my view on MTRR save/restore (which shouldn't matter 
> for the next revision...).

Correct. It's an orthogonal problem and not a prerequisite for the
initial parallel bringup.

> If the MTRR changes on a running system, there might be a bug during 
> hotplug in the original code that handles MTRR? which is also carried 
> over in this patch.
>  From what I can see, MTRR is only saved+restored during initial boot, 
> hotplugging CPU and __save/__restore_processor_state() (used in creating 
> image for hibernation, suspend, kexec...). So if for e.g. in a running 
> system (that has not hibernated, suspended, kexeced), if MTRR for CPU0 
> (first_cpu) changed post-boot and CPU3 is hotplugged, only MTRR for CPU3 
> is updated and CPU0 and CPU3 will hold the same value, while the rest of 
> the CPUs will have the older first-boot value? This behavior will happen 
> with or without this patch. I think this is what Thomas is referring to 
> above when he says that the cached values want to be updated? But the 
> issue is present in the original code as well.

No. Changing a MTTR is done via stop_machine() on all online CPUs in
parallel. So there is no bug.

But its stupid that an update to a MTRR is not updating the cached
value, which results in this sillyness of reading back the real values
into the cache on CPU hotplug in order to set them up correctly on the
upcoming CPU.

So the real improvement would be to:

  1) Cache the MTRRs on the boot CPU once during early boot

  2) When MTRRs are changed then update the cache too

That ensures that the MTRR cache is always up to date and can be
utilized on CPU hotplug.

The 'if (state < RUNNING)' thing is just a sloppy hack which tries to
save a few cycles at boot.

Thanks,

        tglx
diff mbox series

Patch

diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
index 783f3210d582..b6eae3ad4414 100644
--- a/arch/x86/kernel/cpu/mtrr/mtrr.c
+++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
@@ -721,11 +721,20 @@  void __init mtrr_bp_init(void)
  */
 void mtrr_save_state(void)
 {
+	static bool mtrr_saved;
 	int first_cpu;
 
 	if (!mtrr_enabled())
 		return;
 
+	if (system_state < SYSTEM_RUNNING) {
+		if (!mtrr_saved) {
+			mtrr_save_fixed_ranges(NULL);
+			mtrr_saved = true;
+		}
+		return;
+	}
+
 	first_cpu = cpumask_first(cpu_online_mask);
 	smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
 }