public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
To: Gregory Haskins <ghaskins-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
Cc: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Subject: Re: irqdevice INTR example
Date: Sat, 14 Apr 2007 17:30:02 +0300	[thread overview]
Message-ID: <4620E56A.7040207@qumranet.com> (raw)
In-Reply-To: <461E1F73.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>

Gregory Haskins wrote:
>> It's really subtle.
>>
>> With respect to interrupts, VT^H^Hthe hardware provides an override over 
>> IF.  If an interrupt happens while this override is enabled, we exit 
>> guest mode regardless of the state of IF.  In effect interrupts are 
>> enabled but _delivery_ is disabled.  Once we exit guest mode, interrupts 
>> become disabled again until we set IF explicitly.
>>     
>
> Heres a version that implements that idea.  Note that its still a POC, as the kick_process still needs to be exported and handled in extern-module-compat (or whatever its called)
>   

Hopefully Ingo will ack such a patch, as he suggested kick_process()
originally.


> @@ -1866,6 +1874,19 @@ static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
>  		kvm_arch_ops->decache_regs(vcpu);
>  	}
>  
> +	/*
> +	 * Make sure the current task is accurately recorded.  Most of the 
> +	 * time, it will be so we first check if its necessary without taking
> +	 * the lock.  If there is a mismatch, we must aquire the lock and 
> +	 * check again to eliminate races
> +	 */
> +	if (unlikely(vcpu->irq.task != current)) {
> +		spin_lock(&vcpu->irq.lock);
> +		if (vcpu->irq.task != current)
> +			vcpu->irq.task = current;
> +		spin_unlock(&vcpu->irq.lock);
> +	}
> +
>   

Isn't this equivalent to

    vcpu->irq.task = current

?

btw, these double-check things are broken, see
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html. 
It will usually work on x86, and can be made to work elsewhere with
memory barriers, but it's less simple than it appears.


> +	 * FIXME: Do we need to do anything additional to mask IPI/NMIs? 
>   

I'm fairly certain you don't.  I keep hearing about the deadlockability
of sending ipis with interrupts disabled.

> +	 */
> +	local_irq_save(irq_flags);
> +
> +	spin_lock(&vcpu->irq.lock);
> +
> +	/*
> +	 * There are optimizations we can make when signaling interrupts
> +	 * if we know the VCPU is in GUEST mode, so mark that here
> +	 */
> +	vcpu->irq.guest_mode = 1;
> +
> +	/*
> +	 * We also must inject interrupts (if any) while the irq_lock
> +	 * is held
> +	 */
>  	if (!vcpu->mmio_read_completed)
>  		do_interrupt_requests(vcpu, kvm_run);
>  
> +	spin_unlock(&vcpu->irq.lock);
> +
>  	clgi();
>   

stgi() / clgi() is what implements interrupt handling under svm.  I
think you got this right, but please tell me you read the relevant
sections in the manual a few times.


> @@ -1617,6 +1650,16 @@ again:
>  	reload_tss(vcpu);
>  
>  	/*
> +	 * Signal that we have transitioned back to host mode 
> +	 */
> +	spin_lock(&vcpu->irq.lock);
> +
> +	vcpu->irq.guest_mode = 0;
> +	vcpu->irq.pending = 0;
> +	
> +	spin_unlock(&vcpu->irq.lock);
> +
>   

If an interrupt happened just before here, then we need to check it,
otherwise it's lost.  Also an opportunity to see ->read_vector in
action, which was the purpose of the exercise IIRC.

>  
>  	asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
>  
> +	/*
> +	 * Signal that we have transitioned back to host mode 
> +	 */
> +	spin_lock(&vcpu->irq.lock);
> +
> +	vcpu->irq.guest_mode = 0;
> +	vcpu->irq.pending = 0;
> +
> +	spin_unlock(&vcpu->irq.lock);
> +
>  	if (fail) {
>  		kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
>  		kvm_run->fail_entry.hardware_entry_failure_reason
>   

Ditto.

You also want spin_lock_irq() when interrupts are enabled, since some
callers will lock it in interrupt context.


-- 
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

  parent reply	other threads:[~2007-04-14 14:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-12  4:02 irqdevice INTR example Gregory Haskins
     [not found] ` <461D7702.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
2007-04-12  8:02   ` Avi Kivity
     [not found]     ` <461DE791.1040707-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-04-12  8:18       ` Christoph Hellwig
2007-04-12 11:55       ` Gregory Haskins
     [not found]         ` <461DE5C9.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
2007-04-12 12:49           ` Avi Kivity
     [not found]             ` <461E2AD5.7070905-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-04-12 13:43               ` Gregory Haskins
     [not found]                 ` <461DFF1C.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
2007-04-12 14:14                   ` Avi Kivity
     [not found]                     ` <461E3EDB.3080002-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-04-12 16:01                       ` Gregory Haskins
2007-04-13 13:05                         ` Fwd: " Gregory Haskins
     [not found]                         ` <461E1F73.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
2007-04-14 14:30                           ` Avi Kivity [this message]
     [not found]                             ` <4620E56A.7040207-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-04-15 22:32                               ` Gregory Haskins
2007-04-15 23:32                                 ` Gregory Haskins
     [not found]                                 ` <46226FBC.BA47.005A.0-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org>
2007-04-16  5:46                                   ` Avi Kivity

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=4620E56A.7040207@qumranet.com \
    --to=avi-atkuwr5tajbwk0htik3j/w@public.gmane.org \
    --cc=ghaskins-Et1tbQHTxzrQT0dZR+AlfA@public.gmane.org \
    --cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    /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