virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
@ 2024-05-16 13:02 Chen Yu
  2024-05-23 10:04 ` Juergen Gross
  2024-05-23 16:30 ` Dave Hansen
  0 siblings, 2 replies; 6+ messages in thread
From: Chen Yu @ 2024-05-16 13:02 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen
  Cc: Juergen Gross, Ajay Kaher, Alexey Makhalov, Arnd Bergmann,
	H. Peter Anvin, x86, virtualization, linux-kernel, Chen Yu,
	Qiuxu Zhuo, Prem Nath Dey, Xiaoping Zhou

Performance drop is reported when running encode/decode workload and
BenchSEE cache sub-workload.
Bisect points to commit ce0a1b608bfc ("x86/paravirt: Silence unused
native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS
is disabled the virt_spin_lock_key is set to true on bare-metal.
The qspinlock degenerates to test-and-set spinlock, which decrease the
performance on bare-metal.

Fix this by disabling virt_spin_lock_key if CONFIG_PARAVIRT_SPINLOCKS
is not set, or it is on bare-metal.

Fixes: ce0a1b608bfc ("x86/paravirt: Silence unused native_pv_lock_init() function warning")
Suggested-by: Qiuxu Zhuo <qiuxu.zhuo@intel.com>
Reported-by: Prem Nath Dey <prem.nath.dey@intel.com>
Reported-by: Xiaoping Zhou <xiaoping.zhou@intel.com>
Signed-off-by: Chen Yu <yu.c.chen@intel.com>
---
 arch/x86/kernel/paravirt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
index 5358d43886ad..ee51c0949ed8 100644
--- a/arch/x86/kernel/paravirt.c
+++ b/arch/x86/kernel/paravirt.c
@@ -55,7 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
 void __init native_pv_lock_init(void)
 {
-	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
+	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||
 	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		static_branch_disable(&virt_spin_lock_key);
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
  2024-05-16 13:02 [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled Chen Yu
@ 2024-05-23 10:04 ` Juergen Gross
  2024-05-23 16:30 ` Dave Hansen
  1 sibling, 0 replies; 6+ messages in thread
From: Juergen Gross @ 2024-05-23 10:04 UTC (permalink / raw)
  To: Chen Yu, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen
  Cc: Ajay Kaher, Alexey Makhalov, Arnd Bergmann, H. Peter Anvin, x86,
	virtualization, linux-kernel, Qiuxu Zhuo, Prem Nath Dey,
	Xiaoping Zhou


[-- Attachment #1.1.1: Type: text/plain, Size: 1566 bytes --]

On 16.05.24 15:02, Chen Yu wrote:
> Performance drop is reported when running encode/decode workload and
> BenchSEE cache sub-workload.
> Bisect points to commit ce0a1b608bfc ("x86/paravirt: Silence unused
> native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS
> is disabled the virt_spin_lock_key is set to true on bare-metal.
> The qspinlock degenerates to test-and-set spinlock, which decrease the
> performance on bare-metal.
> 
> Fix this by disabling virt_spin_lock_key if CONFIG_PARAVIRT_SPINLOCKS
> is not set, or it is on bare-metal.
> 
> Fixes: ce0a1b608bfc ("x86/paravirt: Silence unused native_pv_lock_init() function warning")
> Suggested-by: Qiuxu Zhuo <qiuxu.zhuo@intel.com>
> Reported-by: Prem Nath Dey <prem.nath.dey@intel.com>
> Reported-by: Xiaoping Zhou <xiaoping.zhou@intel.com>
> Signed-off-by: Chen Yu <yu.c.chen@intel.com>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen

> ---
>   arch/x86/kernel/paravirt.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
> index 5358d43886ad..ee51c0949ed8 100644
> --- a/arch/x86/kernel/paravirt.c
> +++ b/arch/x86/kernel/paravirt.c
> @@ -55,7 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
>   
>   void __init native_pv_lock_init(void)
>   {
> -	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
> +	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||
>   	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
>   		static_branch_disable(&virt_spin_lock_key);
>   }


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
  2024-05-16 13:02 [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled Chen Yu
  2024-05-23 10:04 ` Juergen Gross
@ 2024-05-23 16:30 ` Dave Hansen
  2024-05-23 18:39   ` Jürgen Groß
  2024-05-25  6:03   ` Chen Yu
  1 sibling, 2 replies; 6+ messages in thread
From: Dave Hansen @ 2024-05-23 16:30 UTC (permalink / raw)
  To: Chen Yu, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen
  Cc: Juergen Gross, Ajay Kaher, Alexey Makhalov, Arnd Bergmann,
	H. Peter Anvin, x86, virtualization, linux-kernel, Qiuxu Zhuo,
	Prem Nath Dey, Xiaoping Zhou

[-- Attachment #1: Type: text/plain, Size: 2349 bytes --]

On 5/16/24 06:02, Chen Yu wrote:
> Performance drop is reported when running encode/decode workload and
> BenchSEE cache sub-workload.
> Bisect points to commit ce0a1b608bfc ("x86/paravirt: Silence unused
> native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS
> is disabled the virt_spin_lock_key is set to true on bare-metal.
> The qspinlock degenerates to test-and-set spinlock, which decrease the
> performance on bare-metal.
> 
> Fix this by disabling virt_spin_lock_key if CONFIG_PARAVIRT_SPINLOCKS
> is not set, or it is on bare-metal.

This is missing some background:

The kernel can change spinlock behavior when running as a guest.  But
this guest-friendly behavior causes performance problems on bare metal.
So there's a 'virt_spin_lock_key' static key to switch between the two
modes.

The static key is always enabled by default (run in guest mode) and
should be disabled for bare metal (and in some guests that want native
behavior).

... then describe the regression and the fix

> diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
> index 5358d43886ad..ee51c0949ed8 100644
> --- a/arch/x86/kernel/paravirt.c
> +++ b/arch/x86/kernel/paravirt.c
> @@ -55,7 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
>  
>  void __init native_pv_lock_init(void)
>  {
> -	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
> +	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||
>  	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
>  		static_branch_disable(&virt_spin_lock_key);
>  }
This gets used at a single site:

        if (pv_enabled())
                goto pv_queue;

        if (virt_spin_lock(lock))
                return;

which is logically:

	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS))
		goto ...; // don't look at virt_spin_lock_key

	if (virt_spin_lock_key)
		return; // On virt, but non-paravirt.  Did Test-and-Set
			// spinlock.

So I _think_ Arnd was trying to optimize native_pv_lock_init() away when
it's going to get skipped over anyway by the 'goto'.

But this took me at least 30 minutes of scratching my head and trying to
untangle the whole thing.  It's all far too subtle for my taste, and all
of that to save a few bytes of init text in a configuration that's
probably not even used very often (PARAVIRT=y, but PARAVIRT_SPINLOCKS=n).

Let's just keep it simple.  How about the attached patch?

[-- Attachment #2: pv.patch --]
[-- Type: text/x-patch, Size: 0 bytes --]



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
  2024-05-23 16:30 ` Dave Hansen
@ 2024-05-23 18:39   ` Jürgen Groß
  2024-05-23 18:44     ` Dave Hansen
  2024-05-25  6:03   ` Chen Yu
  1 sibling, 1 reply; 6+ messages in thread
From: Jürgen Groß @ 2024-05-23 18:39 UTC (permalink / raw)
  To: Dave Hansen, Chen Yu, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen
  Cc: Ajay Kaher, Alexey Makhalov, Arnd Bergmann, H. Peter Anvin, x86,
	virtualization, linux-kernel, Qiuxu Zhuo, Prem Nath Dey,
	Xiaoping Zhou

On 23.05.24 18:30, Dave Hansen wrote:
> On 5/16/24 06:02, Chen Yu wrote:
>> Performance drop is reported when running encode/decode workload and
>> BenchSEE cache sub-workload.
>> Bisect points to commit ce0a1b608bfc ("x86/paravirt: Silence unused
>> native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS
>> is disabled the virt_spin_lock_key is set to true on bare-metal.
>> The qspinlock degenerates to test-and-set spinlock, which decrease the
>> performance on bare-metal.
>>
>> Fix this by disabling virt_spin_lock_key if CONFIG_PARAVIRT_SPINLOCKS
>> is not set, or it is on bare-metal.
> 
> This is missing some background:
> 
> The kernel can change spinlock behavior when running as a guest.  But
> this guest-friendly behavior causes performance problems on bare metal.
> So there's a 'virt_spin_lock_key' static key to switch between the two
> modes.
> 
> The static key is always enabled by default (run in guest mode) and
> should be disabled for bare metal (and in some guests that want native
> behavior).
> 
> ... then describe the regression and the fix
> 
>> diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
>> index 5358d43886ad..ee51c0949ed8 100644
>> --- a/arch/x86/kernel/paravirt.c
>> +++ b/arch/x86/kernel/paravirt.c
>> @@ -55,7 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
>>   
>>   void __init native_pv_lock_init(void)
>>   {
>> -	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
>> +	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||
>>   	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
>>   		static_branch_disable(&virt_spin_lock_key);
>>   }
> This gets used at a single site:
> 
>          if (pv_enabled())
>                  goto pv_queue;
> 
>          if (virt_spin_lock(lock))
>                  return;
> 
> which is logically:
> 
> 	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS))
> 		goto ...; // don't look at virt_spin_lock_key
> 
> 	if (virt_spin_lock_key)
> 		return; // On virt, but non-paravirt.  Did Test-and-Set
> 			// spinlock.
> 
> So I _think_ Arnd was trying to optimize native_pv_lock_init() away when
> it's going to get skipped over anyway by the 'goto'.
> 
> But this took me at least 30 minutes of scratching my head and trying to
> untangle the whole thing.  It's all far too subtle for my taste, and all
> of that to save a few bytes of init text in a configuration that's
> probably not even used very often (PARAVIRT=y, but PARAVIRT_SPINLOCKS=n).
> 
> Let's just keep it simple.  How about the attached patch?

Simple indeed. The attachment is empty. :-p


Juergen

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
  2024-05-23 18:39   ` Jürgen Groß
@ 2024-05-23 18:44     ` Dave Hansen
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Hansen @ 2024-05-23 18:44 UTC (permalink / raw)
  To: Jürgen Groß, Chen Yu, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen
  Cc: Ajay Kaher, Alexey Makhalov, Arnd Bergmann, H. Peter Anvin, x86,
	virtualization, linux-kernel, Qiuxu Zhuo, Prem Nath Dey,
	Xiaoping Zhou

[-- Attachment #1: Type: text/plain, Size: 176 bytes --]

On 5/23/24 11:39, Jürgen Groß wrote:
>>
>> Let's just keep it simple.  How about the attached patch?
> 
> Simple indeed. The attachment is empty. 😛

Let's try this again.

[-- Attachment #2: pv.patch --]
[-- Type: text/x-patch, Size: 470 bytes --]

diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
index 5358d43886ad..c193c9e60a1b 100644
--- a/arch/x86/kernel/paravirt.c
+++ b/arch/x86/kernel/paravirt.c
@@ -55,8 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
 void __init native_pv_lock_init(void)
 {
-	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
-	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
+	if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		static_branch_disable(&virt_spin_lock_key);
 }
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled
  2024-05-23 16:30 ` Dave Hansen
  2024-05-23 18:39   ` Jürgen Groß
@ 2024-05-25  6:03   ` Chen Yu
  1 sibling, 0 replies; 6+ messages in thread
From: Chen Yu @ 2024-05-25  6:03 UTC (permalink / raw)
  To: Dave Hansen, Juergen Gross
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	Juergen Gross, Ajay Kaher, Alexey Makhalov, Arnd Bergmann,
	H. Peter Anvin, x86, virtualization, linux-kernel, Qiuxu Zhuo,
	Prem Nath Dey, Xiaoping Zhou

On 2024-05-23 at 09:30:59 -0700, Dave Hansen wrote:
> On 5/16/24 06:02, Chen Yu wrote:
> > Performance drop is reported when running encode/decode workload and
> > BenchSEE cache sub-workload.
> > Bisect points to commit ce0a1b608bfc ("x86/paravirt: Silence unused
> > native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS
> > is disabled the virt_spin_lock_key is set to true on bare-metal.
> > The qspinlock degenerates to test-and-set spinlock, which decrease the
> > performance on bare-metal.
> > 
> > Fix this by disabling virt_spin_lock_key if CONFIG_PARAVIRT_SPINLOCKS
> > is not set, or it is on bare-metal.
> 
> This is missing some background:
> 
> The kernel can change spinlock behavior when running as a guest.  But
> this guest-friendly behavior causes performance problems on bare metal.
> So there's a 'virt_spin_lock_key' static key to switch between the two
> modes.
> 
> The static key is always enabled by default (run in guest mode) and
> should be disabled for bare metal (and in some guests that want native
> behavior).
> 
> ... then describe the regression and the fix
>
Thanks Juergen for your review.

And thanks Dave for the write up, I'll refine the log according to your suggestion. 

> > diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
> > index 5358d43886ad..ee51c0949ed8 100644
> > --- a/arch/x86/kernel/paravirt.c
> > +++ b/arch/x86/kernel/paravirt.c
> > @@ -55,7 +55,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
> >  
> >  void __init native_pv_lock_init(void)
> >  {
> > -	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
> > +	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||
> >  	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
> >  		static_branch_disable(&virt_spin_lock_key);
> >  }
> This gets used at a single site:
> 
>         if (pv_enabled())
>                 goto pv_queue;
> 
>         if (virt_spin_lock(lock))
>                 return;
> 
> which is logically:
> 
> 	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS))
> 		goto ...; // don't look at virt_spin_lock_key
> 
> 	if (virt_spin_lock_key)
> 		return; // On virt, but non-paravirt.  Did Test-and-Set
> 			// spinlock.
>

Thanks for the description in detail, my original change might break the
"X86_FEATURE_HYPERVISOR + NO_CONFIG_PARAVIRT_SPINLOCKS " case that, the guest
can not fall into test-and-set.
 
> So I _think_ Arnd was trying to optimize native_pv_lock_init() away when
> it's going to get skipped over anyway by the 'goto'.
> 
> But this took me at least 30 minutes of scratching my head and trying to
> untangle the whole thing.  It's all far too subtle for my taste, and all
> of that to save a few bytes of init text in a configuration that's
> probably not even used very often (PARAVIRT=y, but PARAVIRT_SPINLOCKS=n).
> 
> Let's just keep it simple.  How about the attached patch?

Yes, this one works, I'll refine it.

thanks,
Chenyu 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-05-25  6:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-16 13:02 [PATCH] x86/paravirt: Disable virt spinlock when CONFIG_PARAVIRT_SPINLOCKS disabled Chen Yu
2024-05-23 10:04 ` Juergen Gross
2024-05-23 16:30 ` Dave Hansen
2024-05-23 18:39   ` Jürgen Groß
2024-05-23 18:44     ` Dave Hansen
2024-05-25  6:03   ` Chen Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).