kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Nadav Amit <nadav.amit@gmail.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>,
	"Nadav Amit" <namit@cs.technion.ac.il>,
	joro@8bytes.org, kvm@vger.kernel.org
Subject: Re: [PATCH 5/5] KVM: x86: Using TSC deadline may cause multiple interrupts by user writes
Date: Fri, 10 Oct 2014 15:55:07 +0200	[thread overview]
Message-ID: <5437E53B.40000@redhat.com> (raw)
In-Reply-To: <42A4DE6D-496C-4D2B-960D-5CABE1C6934F@gmail.com>

Il 10/10/2014 14:51, Nadav Amit ha scritto:
>>> Second, I think that the solution I proposed would perform better.
>>> Currently, there are many unnecessary cancellations and setups of the
>>> timer. This solution does not resolve this problem.
>>
>> I think it does.  You do not get an hrtimer_start if tscdeadline <=
>> guest_tsc.  To avoid useless cancels, either check hrtimer_is_enqueued
>> before calling hrtimer_cancel, or go straight to the source and avoid
>> taking the lock in the easy cases:
>>
>> diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
>> index 1c2fe7de2842..6ce725007424 100644
>> --- a/kernel/time/hrtimer.c
>> +++ b/kernel/time/hrtimer.c
>> @@ -1043,10 +1043,17 @@ int hrtimer_try_to_cancel(struct hrtimer *timer)
>> {
>> 	struct hrtimer_clock_base *base;
>> 	unsigned long flags;
>> -	int ret = -1;
>> +	unsigned long state = timer->state;
>> +	int ret;
>> +
>> +	if (state & HRTIMER_STATE_ENQUEUED)
>> +		return 0;
>> +	if (state & HRTIMER_STATE_CALLBACK)
>> +		return -1;
>>
>> 	base = lock_hrtimer_base(timer, &flags);
>>
>> +	ret = -1;
>> 	if (!hrtimer_callback_running(timer))
>> 		ret = remove_hrtimer(timer, base);
> Wouldn’t this change would cause cancellations never to succeed (the first check would always be true if the timer is active)?

Ehm, there is a missing ! in that first "if".

>>> Last, I think that having less interrupts on deadline changes is not
>>> completely according to the SDM which says: "If software disarms the
>>> timer or postpones the deadline, race conditions may result in the
>>> delivery of a spurious timer interrupt.” It never says interrupts may
>>> be lost if you reprogram the deadline before you check it expired.
>>
>> But the case when you rewrite the same value to the MSR is neither
>> disarming nor postponing.  You would be getting two interrupts for the
>> same event.  That is why I agree with Radim that checking host_initiated
>> is wrong.
> 
> I understand, and Radim's solution seems functionally fine, now that I am fully awake and understand it.
> I still think that if tscdeadline > guest_tsc, then reprogramming the deadline with the same value, as QEMU does, would result in unwarranted overhead.

The overhead is about two atomic operations (70 clock cycles?).  Still,
there are plenty of other micro-optimizations possible:

1) instead of incrementing timer->pending, set it to 1

2) change it to test_and_set_bit and only set PENDING_TIMER if the
result was zero

3) non-atomically test PENDING_TIMER before (atomically) clearing it

4) return bool from kvm_inject_apic_timer_irqs and only clear
PENDING_TIMER if a timer interrupt was actually injected.

(1) or (2) would remove one atomic operation when reprogramming a passed
deadline with the same value.  (3) or (4) would remove one atomic
operation in the case where the cause of the exit is not an expired
timer.  Any takers?

> Perhaps it would be enough not to reprogram the timer if tscdeadline value does not change (by either guest or host).

Yes, and that would just be your patch without host_initiated.

Paolo

  reply	other threads:[~2014-10-10 13:55 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-02 22:10 [PATCH 0/5] KVM: x86: Various bug fixes Nadav Amit
2014-10-02 22:10 ` [PATCH 1/5] KVM: x86: Clear DR7.LE during task-switch Nadav Amit
2014-10-06 19:45   ` Radim Krčmář
2014-10-02 22:10 ` [PATCH 2/5] KVM: x86: Emulator performs code segment checks on read access Nadav Amit
2014-10-06 20:32   ` Radim Krčmář
2014-10-10  2:07     ` [PATCH v2 " Nadav Amit
2014-10-10 15:54       ` Radim Krčmář
2014-10-11  9:39         ` Paolo Bonzini
2014-10-12  6:57           ` Nadav Amit
2014-10-12 12:12             ` Paolo Bonzini
2014-10-12 23:15               ` Nadav Amit
2014-10-13  4:29                 ` Paolo Bonzini
2014-10-13 11:31                 ` Gleb Natapov
2014-10-19 16:07                   ` Nadav Amit
2014-10-02 22:10 ` [PATCH 3/5] KVM: x86: Decoding guest instructions which cross page boundary may fail Nadav Amit
2014-10-06 20:50   ` Radim Krčmář
2014-10-07  9:15     ` Nadav Amit
2014-10-08  9:02       ` Paolo Bonzini
2014-10-02 22:10 ` [PATCH 4/5] KVM: vmx: Unavailable DR4/5 is checked before CPL Nadav Amit
2014-10-06 19:33   ` Radim Krčmář
2014-10-02 22:10 ` [PATCH 5/5] KVM: x86: Using TSC deadline may cause multiple interrupts by user writes Nadav Amit
2014-10-06 20:57   ` Radim Krčmář
2014-10-07  9:35     ` Nadav Amit
2014-10-08 10:06       ` Radim Krčmář
2014-10-08 10:07         ` Paolo Bonzini
2014-10-10  1:55         ` Nadav Amit
2014-10-10  9:45           ` Paolo Bonzini
2014-10-10 12:50             ` Radim Krčmář
2014-10-10 12:51             ` Nadav Amit
2014-10-10 13:55               ` Paolo Bonzini [this message]
2014-10-10 14:02         ` Paolo Bonzini
2014-10-08  9:29   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5437E53B.40000@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=nadav.amit@gmail.com \
    --cc=namit@cs.technion.ac.il \
    --cc=rkrcmar@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).