From patchwork Sat Nov 25 20:57:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10075155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1C65E60383 for ; Sat, 25 Nov 2017 20:58:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D75D286EC for ; Sat, 25 Nov 2017 20:58:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0235B28B53; Sat, 25 Nov 2017 20:58:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A5183286EC for ; Sat, 25 Nov 2017 20:58:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=q4hsi63hsBBlW7gO6spV79RFK0EFf2RxS7to93xvaPI=; b=O72YflTTZSrOcU00hFspQCKyup lTiqmM5uU2OKGQ02mschvISqq+ezSNsiv5Wc8vIAqvRVpYbLFj3YwaB+3SQQlbYBMlkGLZVEO3kgL 0yo/V0dS0Vp+gOi+p8Pj4GctBBNiZLTI1gny73xYQsAQIXn4y1Do2VEWD4bwiH3/5Dsm5N3IfBRVQ LONSrs8x/qGs5aduPVg7lBy1ygMJjnDmyI1BbWxFl+PcR0j0tLsZ2lrhqQ7onh4qYYTqHNhCY4Fd/ cYNpiy8+d7u0Lg6MIdHodPRQejqYVR2yczAfbIHa8lhLto3DDdBK2XYUx7KFyiJpYBXtzebHQbN0c 2iWRwazA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eIhWy-0005ra-6l; Sat, 25 Nov 2017 20:58:32 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eIhW5-00054T-4m for linux-arm-kernel@lists.infradead.org; Sat, 25 Nov 2017 20:57:39 +0000 Received: by mail-wm0-x241.google.com with SMTP id 9so27611021wme.4 for ; Sat, 25 Nov 2017 12:57:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=91VGEXMnBQngPYpeTzmCKazrW0+iJB8LM1lvpe7RajA=; b=PDyqZGJ/lIUNU+LsYwJGNsA/YKaajJxmUoLkwTP0aONjiVpFA9D8OeSbinxMxvwWaa PWKHK5thEIfL+/GEd9f2gcNx7Ny6HOf0xabyhf7zFOxrg8d3YHMBW0CLD1a5rl//Hyza IGJiHwyn8Bthv2KdK0vfjxSsodc0LBoLkokQE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=91VGEXMnBQngPYpeTzmCKazrW0+iJB8LM1lvpe7RajA=; b=FDCsn/irE7OFKuQgeWq/sLl+gQDoD4ytwtUhqECfjq2EY24uXUYgtUyNlwwLQRaQaU h0rrc15LUJMnB//6+zHSPN27PQ8VEBeRKA0Eh8Wn0Lb3qsG+/bowqcP8xqtDXIIXYmT7 r5NO/42bFiVLO7sn6Jac+DURPJuxOy99Em3ZMhmCV+LSCYxNCczioQkmEdWaLlCDpe+S CCFAhjbEnpYDsXbO+PnU01v/A/FgVb3cuGQLNkMQR6MMZVdwcI6hZw9B9Ovm7UTMlcqG Uv63uVcWmiPflkT0MN6hTkJWhSvQc4JJhaN/qhb/jBO+ZAI86eh0TKr3yZCOD+Hlvdf8 crVQ== X-Gm-Message-State: AJaThX7StR85xqWIehu1AIw18ISa8IzSi391gB5xVnXlpYwyNPT9B8PA N+8QlLtGzdPMRO+q2sZ5aLMINw== X-Google-Smtp-Source: AGs4zMYCVEDBDrErrDrAgLYJ3ifjfaKubR2+zQQHO1qV4vWyu8tcJm+pYSghvfjVBdsRJmddmKyr7g== X-Received: by 10.28.6.2 with SMTP id 2mr12557157wmg.37.1511643434971; Sat, 25 Nov 2017 12:57:14 -0800 (PST) Received: from localhost.localdomain (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id z37sm15157577wrc.31.2017.11.25.12.57.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 25 Nov 2017 12:57:14 -0800 (PST) From: Christoffer Dall To: kvm@vger.kernel.org Subject: [PATCH 02/15] KVM: Factor out vcpu->pid adjustment for KVM_RUN Date: Sat, 25 Nov 2017 21:57:05 +0100 Message-Id: <20171125205718.7731-3-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171125205718.7731-1-christoffer.dall@linaro.org> References: <20171125205718.7731-1-christoffer.dall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171125_125737_352297_A6F0CB06 X-CRM114-Status: GOOD ( 15.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-mips@linux-mips.org, Andrew Jones , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Marc Zyngier , James Hogan , Cornelia Huck , Christian Borntraeger , kvm-ppc@vger.kernel.org, Alexander Graf , linux-arm-kernel@lists.infradead.org, Paolo Bonzini , linux-s390@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Christoffer Dall MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Every time userspace calls KVM_RUM, we check if another thread started running the VCPU, and in that case, we adjust the vcpu->pid field to the new thread. We obviously only want to perform this logic once we hold the vcpu->mutex and are actually going to run the VCPU. As we are about to move the vcpu_load() call into the architecture-specific implementation of the ioctl, we first factor the pid adjustment logic out in its own function which each architecture can call later on. Signed-off-by: Christoffer Dall --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6882538eda32..739a2f8e74c5 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -536,6 +536,8 @@ void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); int __must_check vcpu_load(struct kvm_vcpu *vcpu); void vcpu_put(struct kvm_vcpu *vcpu); +void kvm_vcpu_run_adjust_pid(struct kvm_vcpu *vcpu); + #ifdef __KVM_HAVE_IOAPIC void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm); void kvm_arch_post_irq_routing_update(struct kvm *kvm); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fafafcc38b5a..c9549d44c489 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2504,6 +2504,22 @@ static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) return 0; } +void kvm_vcpu_run_adjust_pid(struct kvm_vcpu *vcpu) +{ + struct pid *oldpid; + + oldpid = rcu_access_pointer(vcpu->pid); + if (unlikely(oldpid != current->pids[PIDTYPE_PID].pid)) { + /* The thread running this VCPU changed. */ + struct pid *newpid = get_task_pid(current, PIDTYPE_PID); + + rcu_assign_pointer(vcpu->pid, newpid); + if (oldpid) + synchronize_rcu(); + put_pid(oldpid); + } +} + static long kvm_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -2530,23 +2546,13 @@ static long kvm_vcpu_ioctl(struct file *filp, switch (ioctl) { case KVM_RUN: { - struct pid *oldpid; r = -EINVAL; if (arg) goto out; r = vcpu_load(vcpu); if (r) goto out; - oldpid = rcu_access_pointer(vcpu->pid); - if (unlikely(oldpid != current->pids[PIDTYPE_PID].pid)) { - /* The thread running this VCPU changed. */ - struct pid *newpid = get_task_pid(current, PIDTYPE_PID); - - rcu_assign_pointer(vcpu->pid, newpid); - if (oldpid) - synchronize_rcu(); - put_pid(oldpid); - } + kvm_vcpu_run_adjust_pid(vcpu); r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); vcpu_put(vcpu); trace_kvm_userspace_exit(vcpu->run->exit_reason, r);