From patchwork Sat Oct 22 15:48:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 13015996 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21F0AFA373F for ; Sat, 22 Oct 2022 15:48:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229895AbiJVPsd (ORCPT ); Sat, 22 Oct 2022 11:48:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229845AbiJVPsa (ORCPT ); Sat, 22 Oct 2022 11:48:30 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BFEB24FEFE for ; Sat, 22 Oct 2022 08:48:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SFuETpa6kYA2kfLTdzptBf2PIFoZB2DGXGMPa7uX8z4=; b=b35ztFSC9dYLT/WP2/WaH5ALXm36d15Jw+pHKo4rdR92IJZ79pVEIbg0op/L+kQMfiHa3P 9RLTX3lAVzPF39cjAMfTKSLyWrHvko4uCbQs7whwnJLotyaMw8RuFTKhdyrzBFT+qu3ZJZ pO2nt9hLkHXKxGFm4CJfXmGcCpN9/qQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-465-Pe_Wdq8lNOudrwKkAqkw_g-1; Sat, 22 Oct 2022 11:48:23 -0400 X-MC-Unique: Pe_Wdq8lNOudrwKkAqkw_g-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 56FBE862FEC; Sat, 22 Oct 2022 15:48:22 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECC3C4A9268; Sat, 22 Oct 2022 15:48:21 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Maxim Levitsky , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 1/4] linux-headers/linux/kvm.h: introduce kvm_userspace_memory_region_list ioctl Date: Sat, 22 Oct 2022 11:48:16 -0400 Message-Id: <20221022154819.1823133-2-eesposit@redhat.com> In-Reply-To: <20221022154819.1823133-1-eesposit@redhat.com> References: <20221022154819.1823133-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Introduce new KVM_KICK_ALL_RUNNING_VCPUS and KVM_RESUME_ALL_KICKED_VCPUS ioctl that will be used respectively to pause and then resume all vcpus currently executing KVM_RUN in kvm. Signed-off-by: Emanuele Giuseppe Esposito --- include/uapi/linux/kvm.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index eed0315a77a6..d3cba8d4ca91 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2227,4 +2227,7 @@ struct kvm_s390_zpci_op { /* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +#define KVM_KICK_ALL_RUNNING_VCPUS _IO(KVMIO, 0xd2) +#define KVM_RESUME_ALL_KICKED_VCPUS _IO(KVMIO, 0xd3) + #endif /* __LINUX_KVM_H */ From patchwork Sat Oct 22 15:48:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 13015998 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5215ECDFA1 for ; Sat, 22 Oct 2022 15:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229925AbiJVPsj (ORCPT ); Sat, 22 Oct 2022 11:48:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbiJVPsb (ORCPT ); Sat, 22 Oct 2022 11:48:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9D40250282 for ; Sat, 22 Oct 2022 08:48:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EDSXo8hsV8NzzkWo+KouABAzM9HOiQi26MuXJl4LsjA=; b=QK0AZ7vbjsk0ALDQ3FQRy3VLy9OODV+wj+t1iEy3us8gjbPgtPp4Qv5M/s1JTddqTM0Grv dRDM5E7VrFqyyfhTG4xTyTuVgHNsRN8UIEXRuWG44NDkjBqMN+HId5oTFrzlKIycM2ocQj qF4iBD96uUo21xF9udDbJMG6CUqJi+0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-6wXycPoUPqGmjo96iTbJBQ-1; Sat, 22 Oct 2022 11:48:23 -0400 X-MC-Unique: 6wXycPoUPqGmjo96iTbJBQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD53585A583; Sat, 22 Oct 2022 15:48:22 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F96D4A9268; Sat, 22 Oct 2022 15:48:22 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Maxim Levitsky , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 2/4] KVM: introduce kvm_clear_all_cpus_request Date: Sat, 22 Oct 2022 11:48:17 -0400 Message-Id: <20221022154819.1823133-3-eesposit@redhat.com> In-Reply-To: <20221022154819.1823133-1-eesposit@redhat.com> References: <20221022154819.1823133-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Clear the given request in all vcpus of the VM with struct kvm. Signed-off-by: Emanuele Giuseppe Esposito --- Documentation/virt/kvm/vcpu-requests.rst | 3 +++ virt/kvm/kvm_main.c | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/Documentation/virt/kvm/vcpu-requests.rst b/Documentation/virt/kvm/vcpu-requests.rst index 31f62b64e07b..468410dfe84d 100644 --- a/Documentation/virt/kvm/vcpu-requests.rst +++ b/Documentation/virt/kvm/vcpu-requests.rst @@ -36,6 +36,9 @@ its TLB with a VCPU request. The API consists of the following functions:: /* Make request @req of all VCPUs of the VM with struct kvm @kvm. */ bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req); + /* Clear request @req of all VCPUs of the VM with struct kvm @kvm. */ + void kvm_clear_all_cpus_request(struct kvm *kvm, unsigned int req); + Typically a requester wants the VCPU to perform the activity as soon as possible after making the request. This means most requests (kvm_make_request() calls) are followed by a call to kvm_vcpu_kick(), diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 584a5bab3af3..c080b93edc0d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -355,6 +355,16 @@ bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req) } EXPORT_SYMBOL_GPL(kvm_make_all_cpus_request); +void kvm_clear_all_cpus_request(struct kvm *kvm, unsigned int req) +{ + unsigned long i; + struct kvm_vcpu *vcpu; + + kvm_for_each_vcpu(i, vcpu, kvm) + kvm_clear_request(req, vcpu); +} +EXPORT_SYMBOL_GPL(kvm_clear_all_cpus_request); + #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL void kvm_flush_remote_tlbs(struct kvm *kvm) { From patchwork Sat Oct 22 15:48:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 13015997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11FA4C04A95 for ; Sat, 22 Oct 2022 15:48:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229893AbiJVPse (ORCPT ); Sat, 22 Oct 2022 11:48:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbiJVPsa (ORCPT ); Sat, 22 Oct 2022 11:48:30 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49DD424F7AA for ; Sat, 22 Oct 2022 08:48:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i+xYh4ED8NY+eSwwcDjW7bMv+eHis2lS261cJxu7qzg=; b=WlgAi6m0eZv6v4inzbAUpTsYY2Ls6oedU3zW6RkTciAYrj7twGDKVR9NIgX7F8X6HfuX28 e8YdTwgbHeGCWWeGoKpatNbaw2UwlYaEuERFfgT/MMPYrcV+jh2wbn4uS+4MVjBlvW5L5d 0SntYgIOZdQHsUJtTysrcXsjt48ABvk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-63-cSLsb5qPObusKCwipu1UWA-1; Sat, 22 Oct 2022 11:48:23 -0400 X-MC-Unique: cSLsb5qPObusKCwipu1UWA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3122F85A59D; Sat, 22 Oct 2022 15:48:23 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C65FD4A9279; Sat, 22 Oct 2022 15:48:22 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Maxim Levitsky , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 3/4] KVM: introduce memory transaction semaphore Date: Sat, 22 Oct 2022 11:48:18 -0400 Message-Id: <20221022154819.1823133-4-eesposit@redhat.com> In-Reply-To: <20221022154819.1823133-1-eesposit@redhat.com> References: <20221022154819.1823133-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Right now the semaphore is only used to signal that a vcpu entered KVM_RUN (not necessarly in guest mode, could be also blocked/halted). Later it will be used by specific ioctls (writers) to wait that all vcpus (readers) exit from KVM_RUN. Signed-off-by: Emanuele Giuseppe Esposito --- virt/kvm/kvm_main.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c080b93edc0d..ae0240928a4a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -119,6 +119,8 @@ static const struct file_operations stat_fops_per_vm; static struct file_operations kvm_chardev_ops; +static DECLARE_RWSEM(memory_transaction); + static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, unsigned long arg); #ifdef CONFIG_KVM_COMPAT @@ -4074,7 +4076,19 @@ static long kvm_vcpu_ioctl(struct file *filp, synchronize_rcu(); put_pid(oldpid); } + /* + * Notify that a vcpu wants to run, and thus could be reading + * memslots. + * If KVM_KICK_ALL_RUNNING_VCPUS runs afterwards, it will have + * to wait that KVM_RUN exited and up_read() is called. + * If KVM_KICK_ALL_RUNNING_VCPUS already returned but + * KVM_RESUME_ALL_KICKED_VCPUS didn't start yet, then there + * is a request pending for the vcpu that will cause it to + * exit KVM_RUN. + */ + down_read(&memory_transaction); r = kvm_arch_vcpu_ioctl_run(vcpu); + up_read(&memory_transaction); trace_kvm_userspace_exit(vcpu->run->exit_reason, r); break; } From patchwork Sat Oct 22 15:48:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 13015994 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1A9DC433FE for ; Sat, 22 Oct 2022 15:48:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229868AbiJVPsa (ORCPT ); Sat, 22 Oct 2022 11:48:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbiJVPs2 (ORCPT ); Sat, 22 Oct 2022 11:48:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C122D24F0F for ; Sat, 22 Oct 2022 08:48:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453705; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G1V0vMZpfXGyU72nQJGgurcWjMWd4+DUDDvGesYkw+w=; b=C9iw/DLWjETkVxiVskUR7Ulb7jZS0nHZPoMUewURbMicxjWda64fyHnbE3Fk4HGTgEcfH/ BWyzbjG0v74zbfLkbE4MZDdJtfARFY9Io8rQ37r/n8kxlUw/vnBU0/y1VbdGCI7XfNIVR+ UNuM74jZAsS3aZdZmYCNKzKCvVNu3NE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-103-_KyNeSWwMFydFnCfRiW-Lg-1; Sat, 22 Oct 2022 11:48:24 -0400 X-MC-Unique: _KyNeSWwMFydFnCfRiW-Lg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 980EC800B30; Sat, 22 Oct 2022 15:48:23 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A31F4A9268; Sat, 22 Oct 2022 15:48:23 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Maxim Levitsky , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 4/4] KVM: use signals to abort enter_guest/blocking and retry Date: Sat, 22 Oct 2022 11:48:19 -0400 Message-Id: <20221022154819.1823133-5-eesposit@redhat.com> In-Reply-To: <20221022154819.1823133-1-eesposit@redhat.com> References: <20221022154819.1823133-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Once a vcpu exectues KVM_RUN, it could enter two states: enter guest mode, or block/halt. Use a signal to allow a vcpu to exit the guest state or unblock, so that it can exit KVM_RUN and release the read semaphore, allowing a pending KVM_KICK_ALL_RUNNING_VCPUS to continue. Note that the signal is not deleted and used to propagate the exit reason till vcpu_run(). It will be clearead only by KVM_RESUME_ALL_KICKED_VCPUS. This allows the vcpu to keep try entering KVM_RUN and perform again all checks done in kvm_arch_vcpu_ioctl_run() before entering the guest state, where it will return again if the request is still set. However, the userspace hypervisor should also try to avoid continuously calling KVM_RUN after invoking KVM_KICK_ALL_RUNNING_VCPUS, because such call will just translate in a back-to-back down_read() and up_read() (thanks to the signal). Signed-off-by: Emanuele Giuseppe Esposito --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 8 ++++++++ virt/kvm/kvm_main.c | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index aa381ab69a19..d5c37f344d65 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -108,6 +108,8 @@ KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_USERSPACE_KICK \ + KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b0c47b41c264..2af5f427b4e9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10270,6 +10270,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) } if (kvm_request_pending(vcpu)) { + if (kvm_test_request(KVM_REQ_USERSPACE_KICK, vcpu)) { + r = -EINTR; + goto out; + } if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) { r = -EIO; goto out; @@ -10701,6 +10705,10 @@ static int vcpu_run(struct kvm_vcpu *vcpu) r = vcpu_block(vcpu); } + /* vcpu exited guest/unblocked because of this request */ + if (kvm_test_request(KVM_REQ_USERSPACE_KICK, vcpu)) + return -EINTR; + if (r <= 0) break; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ae0240928a4a..13fa7229b85d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3431,6 +3431,8 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) goto out; if (kvm_check_request(KVM_REQ_UNBLOCK, vcpu)) goto out; + if (kvm_test_request(KVM_REQ_USERSPACE_KICK, vcpu)) + goto out; ret = 0; out: @@ -4668,6 +4670,25 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap); break; } + case KVM_KICK_ALL_RUNNING_VCPUS: { + /* + * Notify all running vcpus that they have to stop. + * Caught in kvm_arch_vcpu_ioctl_run() + */ + kvm_make_all_cpus_request(kvm, KVM_REQ_USERSPACE_KICK); + + /* + * Use wr semaphore to wait for all vcpus to exit from KVM_RUN. + */ + down_write(&memory_transaction); + up_write(&memory_transaction); + break; + } + case KVM_RESUME_ALL_KICKED_VCPUS: { + /* Remove all requests sent with KVM_KICK_ALL_RUNNING_VCPUS */ + kvm_clear_all_cpus_request(kvm, KVM_REQ_USERSPACE_KICK); + break; + } case KVM_SET_USER_MEMORY_REGION: { struct kvm_userspace_memory_region kvm_userspace_mem;