From patchwork Sat Oct 22 15:48:22 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: 1693687 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=W8VxXgbX; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MwlBJ1p3Cz23js for ; Mon, 24 Oct 2022 17:13:28 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1omi5r-000674-Ei for incoming@patchwork.ozlabs.org; Sun, 23 Oct 2022 17:01:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omGjh-0002Sx-2M for qemu-devel@nongnu.org; Sat, 22 Oct 2022 11:48:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omGjf-00084y-CX for qemu-devel@nongnu.org; Sat, 22 Oct 2022 11:48:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453709; 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=uNjRaph9W6dgO7kgHkY9avmuf/bCsD1foX0HoT0P6sA=; b=W8VxXgbXmx8D2Ek9OMiV4dFwGS8Dnlqnb5r3BecVfiS3uD28qZIKSUO8fiV5K0grcoTEb4 fljepOdX3Qd6PwR7DoDkUY0HMvdEHJM4I+2WEu4GUFmY/A37aKsx8f9JHR+jESlGLFwFy9 WO2PxAlZXEVf//bxgWD23GQVL3PaU24= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-147-FWE20gQhOru02AGSyFIP0Q-1; Sat, 22 Oct 2022 11:48:27 -0400 X-MC-Unique: FWE20gQhOru02AGSyFIP0Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C69DF38060EC; Sat, 22 Oct 2022 15:48:26 +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 8737D40C6E15; Sat, 22 Oct 2022 15:48:26 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Maxim Levitsky , "Michael S. Tsirkin" , Cornelia Huck , David Hildenbrand , kvm@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 1/2] linux-headers/linux/kvm.h: introduce kvm_userspace_memory_region_list ioctl Date: Sat, 22 Oct 2022 11:48:22 -0400 Message-Id: <20221022154823.1823193-2-eesposit@redhat.com> In-Reply-To: <20221022154823.1823193-1-eesposit@redhat.com> References: <20221022154823.1823193-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Received-SPF: pass client-ip=170.10.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.251, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- linux-headers/linux/kvm.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index f089349149..1fcf69f903 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -2067,4 +2067,7 @@ struct kvm_stats_desc { /* Available with KVM_CAP_XSAVE2 */ #define KVM_GET_XSAVE2 _IOR(KVMIO, 0xcf, struct kvm_xsave) +#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:23 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: 1693573 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZCmUJx7v; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MwXgY0JFzz23kR for ; Mon, 24 Oct 2022 09:19:36 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1omi98-0006lZ-3T for incoming@patchwork.ozlabs.org; Sun, 23 Oct 2022 17:04:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omGji-0002TC-Ge for qemu-devel@nongnu.org; Sat, 22 Oct 2022 11:48:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omGjg-0008LN-Vl for qemu-devel@nongnu.org; Sat, 22 Oct 2022 11:48:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453712; 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=6dbzyM+9g9fjSLtaxfcxeYNDxW4iMiSOGFthjgPIStQ=; b=ZCmUJx7v/yKmnw7jPuEawHltPsjwKyjkyfMVxz4HnvMwm7WMKCxPPE/5LvwTCERw8DbMSu R+eeQLSaxD6Wd9MtGZqXsmmiMB8FOk1PBOvl22z+ozkt3shcmfamGrO0kCgI/Lo6t19BTv xZGh6pANAP0fWVeqH2anLHJX7hJfuiU= 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-177-UIIhHk5sP4-sgGnYe7LgDA-1; Sat, 22 Oct 2022 11:48:28 -0400 X-MC-Unique: UIIhHk5sP4-sgGnYe7LgDA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2EAA5811E75; Sat, 22 Oct 2022 15:48:28 +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 CEC8140C6E14; Sat, 22 Oct 2022 15:48:26 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Maxim Levitsky , "Michael S. Tsirkin" , Cornelia Huck , David Hildenbrand , kvm@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 2/2] accel/kvm: introduce begin/commit listener callbacks Date: Sat, 22 Oct 2022 11:48:23 -0400 Message-Id: <20221022154823.1823193-3-eesposit@redhat.com> In-Reply-To: <20221022154823.1823193-1-eesposit@redhat.com> References: <20221022154823.1823193-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Received-SPF: pass client-ip=170.10.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.251, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" These callback make sure that all vcpus are blocked before performing memslot updates, and resumed once we are finished. They rely on kvm support for KVM_KICK_ALL_RUNNING_VCPUS and KVM_RESUME_ALL_KICKED_VCPUS ioctls to respectively pause and resume all vcpus that are in KVM_RUN state. Signed-off-by: Emanuele Giuseppe Esposito --- accel/kvm/kvm-all.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 645f0a249a..bd0dfa8613 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -178,6 +178,8 @@ bool kvm_has_guest_debug; int kvm_sstep_flags; static bool kvm_immediate_exit; static hwaddr kvm_max_slot_size = ~0; +static QemuEvent mem_transaction_proceed; + static const KVMCapabilityInfo kvm_required_capabilites[] = { KVM_CAP_INFO(USER_MEMORY), @@ -1523,6 +1525,38 @@ static void kvm_region_del(MemoryListener *listener, memory_region_unref(section->mr); } +static void kvm_begin(MemoryListener *listener) +{ + KVMState *s = kvm_state; + + /* + * Make sure BQL is taken so cpus in kvm_cpu_exec that just exited from + * KVM_RUN do not continue, since many run->exit_reason take it anyways. + */ + assert(qemu_mutex_iothread_locked()); + + /* + * Stop incoming cpus that want to execute KVM_RUN from running. + * Makes cpus calling qemu_event_wait() in kvm_cpu_exec() block. + */ + qemu_event_reset(&mem_transaction_proceed); + + /* Ask KVM to stop all vcpus that are currently running KVM_RUN */ + kvm_vm_ioctl(s, KVM_KICK_ALL_RUNNING_VCPUS); +} + +static void kvm_commit(MemoryListener *listener) +{ + KVMState *s = kvm_state; + assert(qemu_mutex_iothread_locked()); + + /* Ask KVM to resume all vcpus that are currently blocked in KVM_RUN */ + kvm_vm_ioctl(s, KVM_RESUME_ALL_KICKED_VCPUS); + + /* Resume cpus waiting in qemu_event_wait() in kvm_cpu_exec() */ + qemu_event_set(&mem_transaction_proceed); +} + static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) { @@ -1668,6 +1702,8 @@ void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml, kml->listener.region_del = kvm_region_del; kml->listener.log_start = kvm_log_start; kml->listener.log_stop = kvm_log_stop; + kml->listener.begin = kvm_begin; + kml->listener.commit = kvm_commit; kml->listener.priority = 10; kml->listener.name = name; @@ -2611,6 +2647,7 @@ static int kvm_init(MachineState *ms) } kvm_state = s; + qemu_event_init(&mem_transaction_proceed, false); ret = kvm_arch_init(ms, s); if (ret < 0) { @@ -2875,6 +2912,19 @@ int kvm_cpu_exec(CPUState *cpu) } qemu_mutex_unlock_iothread(); + + /* + * Wait that a running memory transaction (memslot update) is concluded. + * + * If the event state is EV_SET, it means kvm_commit() has already finished + * and called qemu_event_set(), therefore cpu can execute. + * + * If it's EV_FREE, it means kvm_begin() has already called + * qemu_event_reset(), therefore a memory transaction is happening and the + * cpu must wait. + */ + qemu_event_wait(&mem_transaction_proceed); + cpu_exec_start(cpu); do {