* [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
@ 2020-03-14 11:31 Paolo Bonzini
2020-03-16 8:33 ` Vitaly Kuznetsov
0 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2020-03-14 11:31 UTC (permalink / raw)
To: linux-kernel, kvm; +Cc: linmiaohe, vkuznets
After test_and_set_bit() for kvm->arch.apicv_inhibit_reasons, we will
always get false when calling kvm_apicv_activated() because it's sure
apicv_inhibit_reasons do not equal to 0.
What the code wants to do, is check whether APICv was *already* active
and if so skip the costly request; we can do this using cmpxchg.
Reported-by: Miaohe Lin <linmiaohe@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/x86.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a7cb85231330..49efa4529662 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8049,19 +8049,26 @@ void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
*/
void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
{
+ unsigned long old, new, expected;
+
if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
!kvm_x86_ops->check_apicv_inhibit_reasons(bit))
return;
- if (activate) {
- if (!test_and_clear_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
- !kvm_apicv_activated(kvm))
- return;
- } else {
- if (test_and_set_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
- kvm_apicv_activated(kvm))
- return;
- }
+ old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
+ do {
+ expected = new = old;
+ if (activate)
+ __clear_bit(bit, &new);
+ else
+ __set_bit(bit, &new);
+ if (new == old)
+ break;
+ old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
+ } while (old != expected);
+
+ if ((old == 0) == (new == 0))
+ return;
trace_kvm_apicv_update_request(activate, bit);
if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
--
1.8.3.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-14 11:31 [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check Paolo Bonzini
@ 2020-03-16 8:33 ` Vitaly Kuznetsov
2020-03-16 15:26 ` Sean Christopherson
0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-16 8:33 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: linmiaohe, linux-kernel, kvm
Paolo Bonzini <pbonzini@redhat.com> writes:
> After test_and_set_bit() for kvm->arch.apicv_inhibit_reasons, we will
> always get false when calling kvm_apicv_activated() because it's sure
> apicv_inhibit_reasons do not equal to 0.
>
> What the code wants to do, is check whether APICv was *already* active
> and if so skip the costly request; we can do this using cmpxchg.
>
> Reported-by: Miaohe Lin <linmiaohe@huawei.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> arch/x86/kvm/x86.c | 25 ++++++++++++++++---------
> 1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index a7cb85231330..49efa4529662 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -8049,19 +8049,26 @@ void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
> */
> void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit)
> {
> + unsigned long old, new, expected;
> +
> if (!kvm_x86_ops->check_apicv_inhibit_reasons ||
> !kvm_x86_ops->check_apicv_inhibit_reasons(bit))
> return;
>
> - if (activate) {
> - if (!test_and_clear_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
> - !kvm_apicv_activated(kvm))
> - return;
> - } else {
> - if (test_and_set_bit(bit, &kvm->arch.apicv_inhibit_reasons) ||
> - kvm_apicv_activated(kvm))
> - return;
> - }
> + old = READ_ONCE(kvm->arch.apicv_inhibit_reasons);
> + do {
> + expected = new = old;
> + if (activate)
> + __clear_bit(bit, &new);
> + else
> + __set_bit(bit, &new);
> + if (new == old)
> + break;
> + old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new);
> + } while (old != expected);
'expected' here is a bit confusing as it's not what we expect to get as
the result but rather what we expect to see pre-change. I don't have a
better suggestion though.
> +
> + if ((old == 0) == (new == 0))
> + return;
This is a very laconic expression I personally find hard to read :-)
/* Check if WE actually changed APICv state */
if ((!old && !new) || (old && new))
return;
would be my preference (not strong though, I read yours several times
and now I feel like I understand it just fine :-)
>
> trace_kvm_apicv_update_request(activate, bit);
> if (kvm_x86_ops->pre_update_apicv_exec_ctrl)
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
--
Vitaly
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-16 8:33 ` Vitaly Kuznetsov
@ 2020-03-16 15:26 ` Sean Christopherson
2020-03-16 15:44 ` Vitaly Kuznetsov
0 siblings, 1 reply; 7+ messages in thread
From: Sean Christopherson @ 2020-03-16 15:26 UTC (permalink / raw)
To: Vitaly Kuznetsov; +Cc: Paolo Bonzini, linmiaohe, linux-kernel, kvm
On Mon, Mar 16, 2020 at 09:33:50AM +0100, Vitaly Kuznetsov wrote:
> Paolo Bonzini <pbonzini@redhat.com> writes:
> > + if ((old == 0) == (new == 0))
> > + return;
>
> This is a very laconic expression I personally find hard to read :-)
>
> /* Check if WE actually changed APICv state */
> if ((!old && !new) || (old && new))
> return;
>
> would be my preference (not strong though, I read yours several times
> and now I feel like I understand it just fine :-)
Or maybe this to avoid so many equals signs?
if (!old == !new)
return;
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-16 15:26 ` Sean Christopherson
@ 2020-03-16 15:44 ` Vitaly Kuznetsov
2020-03-16 15:59 ` Sean Christopherson
0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-16 15:44 UTC (permalink / raw)
To: Sean Christopherson; +Cc: Paolo Bonzini, linmiaohe, linux-kernel, kvm
Sean Christopherson <sean.j.christopherson@intel.com> writes:
> On Mon, Mar 16, 2020 at 09:33:50AM +0100, Vitaly Kuznetsov wrote:
>> Paolo Bonzini <pbonzini@redhat.com> writes:
>> > + if ((old == 0) == (new == 0))
>> > + return;
>>
>> This is a very laconic expression I personally find hard to read :-)
>>
>> /* Check if WE actually changed APICv state */
>> if ((!old && !new) || (old && new))
>> return;
>>
>> would be my preference (not strong though, I read yours several times
>> and now I feel like I understand it just fine :-)
>
> Or maybe this to avoid so many equals signs?
>
> if (!old == !new)
> return;
>
if (!!old == !!new)
return;
to make it clear we're converting them to 1/0 :-)
--
Vitaly
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-16 15:44 ` Vitaly Kuznetsov
@ 2020-03-16 15:59 ` Sean Christopherson
2020-03-16 16:39 ` Paolo Bonzini
0 siblings, 1 reply; 7+ messages in thread
From: Sean Christopherson @ 2020-03-16 15:59 UTC (permalink / raw)
To: Vitaly Kuznetsov; +Cc: Paolo Bonzini, linmiaohe, linux-kernel, kvm
On Mon, Mar 16, 2020 at 04:44:47PM +0100, Vitaly Kuznetsov wrote:
> Sean Christopherson <sean.j.christopherson@intel.com> writes:
>
> > On Mon, Mar 16, 2020 at 09:33:50AM +0100, Vitaly Kuznetsov wrote:
> >> Paolo Bonzini <pbonzini@redhat.com> writes:
> >> > + if ((old == 0) == (new == 0))
> >> > + return;
> >>
> >> This is a very laconic expression I personally find hard to read :-)
> >>
> >> /* Check if WE actually changed APICv state */
> >> if ((!old && !new) || (old && new))
> >> return;
> >>
> >> would be my preference (not strong though, I read yours several times
> >> and now I feel like I understand it just fine :-)
> >
> > Or maybe this to avoid so many equals signs?
> >
> > if (!old == !new)
> > return;
> >
>
> if (!!old == !!new)
> return;
>
> to make it clear we're converting them to 1/0 :-)
All I can think of now is the Onion article regarding razor blades...
if (!!!!old == !!!!new)
return;
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-16 15:59 ` Sean Christopherson
@ 2020-03-16 16:39 ` Paolo Bonzini
2020-03-17 11:24 ` Xiaoyao Li
0 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2020-03-16 16:39 UTC (permalink / raw)
To: Sean Christopherson, Vitaly Kuznetsov; +Cc: linmiaohe, linux-kernel, kvm
On 16/03/20 16:59, Sean Christopherson wrote:
>>>
>> if (!!old == !!new)
>> return;
>>
>> to make it clear we're converting them to 1/0 :-)
>
> All I can think of now is the Onion article regarding razor blades...
>
> if (!!!!old == !!!!new)
> return;
>
That would be !!!!!, but seriously I'll go with two.
(Thanks for giving me a chuckle, it's sorely needed these days).
Paolo
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check
2020-03-16 16:39 ` Paolo Bonzini
@ 2020-03-17 11:24 ` Xiaoyao Li
0 siblings, 0 replies; 7+ messages in thread
From: Xiaoyao Li @ 2020-03-17 11:24 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov
Cc: linmiaohe, linux-kernel, kvm
On 3/17/2020 12:39 AM, Paolo Bonzini wrote:
> On 16/03/20 16:59, Sean Christopherson wrote:
>>>>
>>> if (!!old == !!new)
>>> return;
>>>
>>> to make it clear we're converting them to 1/0 :-)
>>
>> All I can think of now is the Onion article regarding razor blades...
>>
>> if (!!!!old == !!!!new)
>> return;
>>
>
> That would be !!!!!, but seriously I'll go with two.
>
> (Thanks for giving me a chuckle, it's sorely needed these days).
Take care, Paolo.
I have been staying at home for two months in Wuhan, China, and things
are going better now. I believe all the world can defeat Coronavirus
eventually.
> Paolo
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2020-03-17 11:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-14 11:31 [PATCH] KVM: X86: correct meaningless kvm_apicv_activated() check Paolo Bonzini
2020-03-16 8:33 ` Vitaly Kuznetsov
2020-03-16 15:26 ` Sean Christopherson
2020-03-16 15:44 ` Vitaly Kuznetsov
2020-03-16 15:59 ` Sean Christopherson
2020-03-16 16:39 ` Paolo Bonzini
2020-03-17 11:24 ` Xiaoyao Li
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.