kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gleb Natapov <gleb@redhat.com>
To: Liu ping fan <kernelfans@gmail.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>,
	avi@redhat.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, aliguori@us.ibm.com
Subject: Re: [PATCH] kvm: make vcpu life cycle separated from kvm instance
Date: Tue, 6 Dec 2011 10:14:56 +0200	[thread overview]
Message-ID: <20111206081456.GL12507@redhat.com> (raw)
In-Reply-To: <CAFgQCTuTBO5WsKbMcZtwei74QiniWZ1mCiMGKivEpUJdP=v2Yg@mail.gmail.com>

On Tue, Dec 06, 2011 at 02:54:06PM +0800, Liu ping fan wrote:
> On Mon, Dec 5, 2011 at 4:41 PM, Gleb Natapov <gleb@redhat.com> wrote:
> > On Mon, Dec 05, 2011 at 01:39:37PM +0800, Liu ping fan wrote:
> >> On Sun, Dec 4, 2011 at 8:10 PM, Gleb Natapov <gleb@redhat.com> wrote:
> >> > On Sun, Dec 04, 2011 at 07:53:37PM +0800, Liu ping fan wrote:
> >> >> On Sat, Dec 3, 2011 at 2:26 AM, Jan Kiszka <jan.kiszka@siemens.com> wrote:
> >> >> > On 2011-12-02 07:26, Liu Ping Fan wrote:
> >> >> >> From: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
> >> >> >>
> >> >> >> Currently, vcpu can be destructed only when kvm instance destroyed.
> >> >> >> Change this to vcpu's destruction taken when its refcnt is zero,
> >> >> >> and then vcpu MUST and CAN be destroyed before kvm's destroy.
> >> >> >
> >> >> > I'm lacking the big picture yet (would be good to have in the change log
> >> >> > - at least I'm too lazy to read the code):
> >> >> >
> >> >> > What increments the refcnt, what decrements it again? IOW, how does user
> >> >> > space controls the life-cycle of a vcpu after your changes?
> >> >> >
> >> >> In local APIC mode, delivering IPI to target APIC, target's refcnt is
> >> >> incremented, and decremented when finished. At other times, using RCU to
> >> > Why is this needed?
> >> >
> >> Suppose the following scene:
> >>
> >> #define kvm_for_each_vcpu(idx, vcpup, kvm) \
> >>         for (idx = 0; \
> >>              idx < atomic_read(&kvm->online_vcpus) && \
> >>              (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
> >>              idx++)
> >>
> >> ------------------------------------------------------------------------------------------>
> >> Here kvm_vcpu's destruction is called
> >>               vcpup->vcpu_id ...  //oops!
> >>
> >>
> > And this is exactly how your code looks. i.e you do not increment
> > reference count in most of the loops, you only increment it twice
> > (in pic_unlock() and kvm_irq_delivery_to_apic()) because you are using
> > vcpu outside of rcu_read_lock() protected section and I do not see why
> > not just extend protected section to include kvm_vcpu_kick(). As far as
> > I can see this function does not sleep.
> >
> :-), I just want to minimize the RCU critical area, and as you say, we
> can  extend protected section to include kvm_vcpu_kick()
> 
What's the point of trying to minimize it? vcpu will not be freed quicker.

> > What should protect vcpu from disappearing in your example above is RCU
> > itself if you are using it right. But since I do not see any calls to
> > rcu_assign_pointer()/rcu_dereference() I doubt you are using it right
> > actually.
> >
> Sorry, but I thought it would not be. Please help me to check my thoughts :
> 
> struct kvm_vcpu *kvm_vcpu_get(struct kvm_vcpu *vcpu)
> {
> 	if (vcpu == NULL)
> 		return NULL;
> 	if (atomic_add_unless(&vcpu->refcount, 1, 0))
> ------------------------------increment
> 		return vcpu;
> 	return NULL;
> }
> 
> void kvm_vcpu_put(struct kvm_vcpu *vcpu)
> {
> 	struct kvm *kvm;
> 	if (atomic_dec_and_test(&vcpu->refcount)) {
> --------------------------decrement
> 		kvm = vcpu->kvm;
> 		mutex_lock(&kvm->lock);
> 		kvm->vcpus[vcpu->vcpu_id] = NULL;
> 		atomic_dec(&kvm->online_vcpus);
> 		mutex_unlock(&kvm->lock);
> 		call_rcu(&vcpu->head, kvm_vcpu_zap);
> 	}
> }
> 
> The atomic of decrement and increment are protected by cache coherent protocol.
> So once we hold a valid kvm_vcpu pointer through kvm_vcpu_get(),
> we will always keep it until we release it, then, the destruction may happen.
> 
My point is you do not need those atomics at all, not that they are
incorrect. You either protect vcpus with reference counters or RCU, but
not both. The point of RCU is that you do not need any locking
on read access to data structure, so if you add locking (by means of
reference counting) just use rwlock around access to vcpus array and be
done with it.

--
			Gleb.

  reply	other threads:[~2011-12-06  8:14 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-25  2:35 [PATCH 0] A series patches for kvm&qemu to enable vcpu destruction in kvm Liu Ping Fan
2011-11-25  2:35 ` [PATCH 1/2] kvm: make vcpu life cycle separated from kvm instance Liu Ping Fan
2011-11-27 10:36   ` Avi Kivity
2011-12-02  6:26     ` [PATCH] " Liu Ping Fan
2011-12-02 18:26       ` Jan Kiszka
2011-12-04 11:53         ` Liu ping fan
2011-12-04 12:10           ` Gleb Natapov
2011-12-05  5:39             ` Liu ping fan
2011-12-05  8:41               ` Gleb Natapov
2011-12-06  6:54                 ` Liu ping fan
2011-12-06  8:14                   ` Gleb Natapov [this message]
2011-12-04 10:23       ` Avi Kivity
2011-12-05  5:29         ` Liu ping fan
2011-12-05  9:30           ` Avi Kivity
2011-12-05  9:42             ` Gleb Natapov
2011-12-05  9:58               ` Avi Kivity
2011-12-05 10:18                 ` Gleb Natapov
2011-12-05 10:22                   ` Avi Kivity
2011-12-05 10:40                     ` Gleb Natapov
2011-12-09  5:23       ` [PATCH V2] " Liu Ping Fan
2011-12-09 14:23         ` Gleb Natapov
2011-12-12  2:41           ` [PATCH v3] " Liu Ping Fan
2011-12-12 12:54             ` Gleb Natapov
2011-12-13  9:29               ` Liu ping fan
2011-12-13  9:47                 ` Gleb Natapov
2011-12-13 11:36             ` Marcelo Tosatti
2011-12-13 11:54               ` Gleb Natapov
2011-12-15  3:21               ` Liu ping fan
2011-12-15  4:28                 ` [PATCH v4] " Liu Ping Fan
2011-12-15  5:33                   ` Xiao Guangrong
2011-12-15  6:53                     ` Liu ping fan
2011-12-15  8:25                       ` Xiao Guangrong
2011-12-15  8:57                         ` Xiao Guangrong
2011-12-15  6:48                   ` Takuya Yoshikawa
2011-12-16  9:38                     ` Marcelo Tosatti
2011-12-17  3:57                     ` Liu ping fan
2011-12-19  1:16                       ` Takuya Yoshikawa
2011-12-15  9:10                   ` Gleb Natapov
2011-12-16  7:50                     ` Liu ping fan
2011-12-15  8:33                 ` [PATCH v3] " Gleb Natapov
2011-12-15  9:06                   ` Liu ping fan
2011-12-15  9:08                     ` Gleb Natapov
2011-12-17  3:19             ` [PATCH v5] " Liu Ping Fan
2011-12-26 11:09               ` Gleb Natapov
2011-12-26 11:17                 ` Avi Kivity
2011-12-26 11:21                   ` Gleb Natapov
2011-12-27  7:53                 ` Liu ping fan
2011-12-27  8:38               ` [PATCH v6] " Liu Ping Fan
2011-12-27 11:22                 ` Takuya Yoshikawa
2011-12-28  6:54                   ` Liu ping fan
2011-12-28  9:53                     ` Avi Kivity
2011-12-29 14:03                       ` Liu ping fan
2011-12-29 14:31                         ` Avi Kivity
2012-01-05  9:35                           ` Liu ping fan
2011-12-28 10:29                     ` Takuya Yoshikawa
2011-12-28  9:53                 ` Avi Kivity
2011-12-28  9:54                   ` Avi Kivity
2011-12-28 10:19                     ` Takuya Yoshikawa
2011-12-28 10:28                       ` Avi Kivity
2012-01-07  2:55               ` [PATCH v7] " Liu Ping Fan
2012-01-12 12:37                 ` Avi Kivity
2012-01-15 13:17                   ` Liu ping fan
2012-01-15 13:37                     ` Avi Kivity
2011-11-25 17:54 ` [PATCH 0] A series patches for kvm&qemu to enable vcpu destruction in kvm Jan Kiszka
2011-11-27  3:07   ` Liu ping fan
2011-11-27  2:42 ` [PATCH 2/2] kvm: exit to userspace with reason KVM_EXIT_VCPU_DEAD Liu Ping Fan
2011-11-27 10:36   ` Avi Kivity
2011-11-27 10:50     ` Gleb Natapov
2011-11-28  7:16       ` [Qemu-devel] " Liu ping fan
2011-11-28  8:46         ` Gleb Natapov
2011-11-27  2:45 ` [PATCH 1/5] QEMU Add cpu_phyid_to_cpu() to map cpu phyid to CPUState Liu Ping Fan
2011-11-27  2:45 ` [PATCH 2/5] QEMU Add cpu_free() to support arch related CPUState release Liu Ping Fan
2011-11-27  2:45 ` [PATCH 3/5] QEMU Introduce a pci device "cpustate" to get CPU_DEAD event in guest Liu Ping Fan
2011-11-27 10:56   ` Gleb Natapov
2011-11-27  2:45 ` [PATCH 4/5] QEMU Release vcpu and finally exit vcpu thread safely Liu Ping Fan
2011-11-27  2:45 ` [PATCH 5/5] QEMU tmp patches for linux-header files Liu Ping Fan
2011-11-27  2:47 ` [PATCH] virtio: add a pci driver to notify host the CPU_DEAD event Liu Ping Fan
2011-11-27 11:10   ` Gleb Natapov

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=20111206081456.GL12507@redhat.com \
    --to=gleb@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=avi@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=kernelfans@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).