From patchwork Tue Nov 25 16:04:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 5380421 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 21B3B9F39D for ; Tue, 25 Nov 2014 16:04:45 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6E2DB20179 for ; Tue, 25 Nov 2014 16:04:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A5E8F200F3 for ; Tue, 25 Nov 2014 16:04:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751345AbaKYQEW (ORCPT ); Tue, 25 Nov 2014 11:04:22 -0500 Received: from e06smtp12.uk.ibm.com ([195.75.94.108]:52236 "EHLO e06smtp12.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750852AbaKYQET (ORCPT ); Tue, 25 Nov 2014 11:04:19 -0500 Received: from /spool/local by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 25 Nov 2014 16:04:17 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 25 Nov 2014 16:04:15 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 290D01B08049 for ; Tue, 25 Nov 2014 16:04:30 +0000 (GMT) Received: from d06av12.portsmouth.uk.ibm.com (d06av12.portsmouth.uk.ibm.com [9.149.37.247]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id sAPG4FtD18350502 for ; Tue, 25 Nov 2014 16:04:15 GMT Received: from d06av12.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id sAPG4CMk028631 for ; Tue, 25 Nov 2014 09:04:15 -0700 Received: from tuxmaker.boeblingen.de.ibm.com (tuxmaker.boeblingen.de.ibm.com [9.152.85.9]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id sAPG4Bif028543; Tue, 25 Nov 2014 09:04:12 -0700 From: David Hildenbrand To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, gleb@kernel.org, jfrei@linux.vnet.ibm.com, borntraeger@de.ibm.com, cornelia.huck@de.ibm.com, dahi@linux.vnet.ibm.com Subject: [PATCH RFC 2/2] KVM: thread creating a vcpu is the owner of that vcpu Date: Tue, 25 Nov 2014 17:04:09 +0100 Message-Id: <1416931449-24585-3-git-send-email-dahi@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.5.5 In-Reply-To: <1416931449-24585-1-git-send-email-dahi@linux.vnet.ibm.com> References: <1416931449-24585-1-git-send-email-dahi@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14112516-0009-0000-0000-000002222C36 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, we allow changing the PID of a VCPU. This PID is used to identify the thread to yield to if we want to yield to this specific VCPU. In practice (e.g. QEMU), the thread creating and executing the VCPU remains always the same. Temporarily exchanging the PID (e.g. because an ioctl is triggered from a wrong thread) doesn't really make sense. The PID is exchanged and a synchronize_rcu() is called. When the executing thread tries to run the VCPU again, another synchronize_rcu() happens. If a yield to that VCPU is triggered while the PID of the wrong thread is active, the wrong thread might receive a yield, but this will most likely not help the executing thread at all. The executing thread won't have a higher priority after the wrong thread has finished with the ioctl. The wrong thread will even receive yields afterwards that were targeted to the executing vcpu, until the executing VCPU has replaced the PID on the next ioctl - doesn't feel correct to me. This patch makes the creating thread the owning thread, and therefore the only valid yield candidate (especially because VCPU ioctls are - in theory - only valid when triggered from the owning thread - old user space versions may not stick to this rule). This should also speed up the initial start of all VCPUs, when the PID is assigned for the first time. Should be backwards compatible - if there is any old user space version out there that doesn't stick to the creating == executing thread rule, yields will not work as intended. Signed-off-by: David Hildenbrand Acked-by: Christian Borntraeger --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 18 ++---------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index aa56894..f1fe655 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -245,6 +245,7 @@ struct kvm_vcpu { int fpu_active; int guest_fpu_loaded, guest_xcr0_loaded; wait_queue_head_t wq; + /* the pid owning this vcpu - target for vcpu yields */ struct pid *pid; int sigset_active; sigset_t sigset; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 184f52e..4ba7810 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -124,15 +124,6 @@ int vcpu_load(struct kvm_vcpu *vcpu) if (mutex_lock_killable(&vcpu->mutex)) return -EINTR; - if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) { - /* The thread running this VCPU changed. */ - struct pid *oldpid = vcpu->pid; - struct pid *newpid = get_task_pid(current, PIDTYPE_PID); - rcu_assign_pointer(vcpu->pid, newpid); - if (oldpid) - synchronize_rcu(); - put_pid(oldpid); - } cpu = get_cpu(); preempt_notifier_register(&vcpu->preempt_notifier); kvm_arch_vcpu_load(vcpu, cpu); @@ -220,7 +211,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) vcpu->cpu = -1; vcpu->kvm = kvm; vcpu->vcpu_id = id; - vcpu->pid = NULL; + vcpu->pid = get_task_pid(current, PIDTYPE_PID); init_waitqueue_head(&vcpu->wq); kvm_async_pf_vcpu_init(vcpu); @@ -1771,15 +1762,10 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_kick); int kvm_vcpu_yield_to(struct kvm_vcpu *target) { - struct pid *pid; struct task_struct *task = NULL; int ret = 0; - rcu_read_lock(); - pid = rcu_dereference(target->pid); - if (pid) - task = get_pid_task(pid, PIDTYPE_PID); - rcu_read_unlock(); + task = get_pid_task(target->pid, PIDTYPE_PID); if (!task) return ret; ret = yield_to(task, 1);