thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From ae2077fd5d351a68c313c64f07fb225dff694a8f Mon Sep 17 00:00:00 2001
ed5979
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
Date: Mon, 16 Jan 2023 07:16:41 -0500
ed5979
Subject: [PATCH 29/31] accel: introduce accelerator blocker API
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
RH-MergeRequest: 138: accel: introduce accelerator blocker API
ed5979
RH-Bugzilla: 1979276
ed5979
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
RH-Acked-by: David Hildenbrand <david@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [1/3] 56b07cd7db516c5066e6d66b4695064fdf73abbf (eesposit/qemu-kvm)
ed5979
ed5979
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1979276
ed5979
ed5979
commit bd688fc93120fb3e28aa70e3dfdf567ccc1e0bc1
ed5979
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
Date:   Fri Nov 11 10:47:56 2022 -0500
ed5979
ed5979
    accel: introduce accelerator blocker API
ed5979
ed5979
    This API allows the accelerators to prevent vcpus from issuing
ed5979
    new ioctls while execting a critical section marked with the
ed5979
    accel_ioctl_inhibit_begin/end functions.
ed5979
ed5979
    Note that all functions submitting ioctls must mark where the
ed5979
    ioctl is being called with accel_{cpu_}ioctl_begin/end().
ed5979
ed5979
    This API requires the caller to always hold the BQL.
ed5979
    API documentation is in sysemu/accel-blocker.h
ed5979
ed5979
    Internally, it uses a QemuLockCnt together with a per-CPU QemuLockCnt
ed5979
    (to minimize cache line bouncing) to keep avoid that new ioctls
ed5979
    run when the critical section starts, and a QemuEvent to wait
ed5979
    that all running ioctls finish.
ed5979
ed5979
    Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
    Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
ed5979
    Message-Id: <20221111154758.1372674-2-eesposit@redhat.com>
ed5979
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
ed5979
Conflicts:
ed5979
	util/meson.build: "interval-tree.c" does not exist
ed5979
ed5979
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
---
ed5979
 accel/accel-blocker.c          | 154 +++++++++++++++++++++++++++++++++
ed5979
 accel/meson.build              |   2 +-
ed5979
 hw/core/cpu-common.c           |   2 +
ed5979
 include/hw/core/cpu.h          |   3 +
ed5979
 include/sysemu/accel-blocker.h |  56 ++++++++++++
ed5979
 util/meson.build               |   2 +-
ed5979
 6 files changed, 217 insertions(+), 2 deletions(-)
ed5979
 create mode 100644 accel/accel-blocker.c
ed5979
 create mode 100644 include/sysemu/accel-blocker.h
ed5979
ed5979
diff --git a/accel/accel-blocker.c b/accel/accel-blocker.c
ed5979
new file mode 100644
ed5979
index 0000000000..1e7f423462
ed5979
--- /dev/null
ed5979
+++ b/accel/accel-blocker.c
ed5979
@@ -0,0 +1,154 @@
ed5979
+/*
ed5979
+ * Lock to inhibit accelerator ioctls
ed5979
+ *
ed5979
+ * Copyright (c) 2022 Red Hat Inc.
ed5979
+ *
ed5979
+ * Author: Emanuele Giuseppe Esposito       <eesposit@redhat.com>
ed5979
+ *
ed5979
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
ed5979
+ * of this software and associated documentation files (the "Software"), to deal
ed5979
+ * in the Software without restriction, including without limitation the rights
ed5979
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ed5979
+ * copies of the Software, and to permit persons to whom the Software is
ed5979
+ * furnished to do so, subject to the following conditions:
ed5979
+ *
ed5979
+ * The above copyright notice and this permission notice shall be included in
ed5979
+ * all copies or substantial portions of the Software.
ed5979
+ *
ed5979
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ed5979
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ed5979
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ed5979
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ed5979
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ed5979
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ed5979
+ * THE SOFTWARE.
ed5979
+ */
ed5979
+
ed5979
+#include "qemu/osdep.h"
ed5979
+#include "qemu/thread.h"
ed5979
+#include "qemu/main-loop.h"
ed5979
+#include "hw/core/cpu.h"
ed5979
+#include "sysemu/accel-blocker.h"
ed5979
+
ed5979
+static QemuLockCnt accel_in_ioctl_lock;
ed5979
+static QemuEvent accel_in_ioctl_event;
ed5979
+
ed5979
+void accel_blocker_init(void)
ed5979
+{
ed5979
+    qemu_lockcnt_init(&accel_in_ioctl_lock);
ed5979
+    qemu_event_init(&accel_in_ioctl_event, false);
ed5979
+}
ed5979
+
ed5979
+void accel_ioctl_begin(void)
ed5979
+{
ed5979
+    if (likely(qemu_mutex_iothread_locked())) {
ed5979
+        return;
ed5979
+    }
ed5979
+
ed5979
+    /* block if lock is taken in kvm_ioctl_inhibit_begin() */
ed5979
+    qemu_lockcnt_inc(&accel_in_ioctl_lock);
ed5979
+}
ed5979
+
ed5979
+void accel_ioctl_end(void)
ed5979
+{
ed5979
+    if (likely(qemu_mutex_iothread_locked())) {
ed5979
+        return;
ed5979
+    }
ed5979
+
ed5979
+    qemu_lockcnt_dec(&accel_in_ioctl_lock);
ed5979
+    /* change event to SET. If event was BUSY, wake up all waiters */
ed5979
+    qemu_event_set(&accel_in_ioctl_event);
ed5979
+}
ed5979
+
ed5979
+void accel_cpu_ioctl_begin(CPUState *cpu)
ed5979
+{
ed5979
+    if (unlikely(qemu_mutex_iothread_locked())) {
ed5979
+        return;
ed5979
+    }
ed5979
+
ed5979
+    /* block if lock is taken in kvm_ioctl_inhibit_begin() */
ed5979
+    qemu_lockcnt_inc(&cpu->in_ioctl_lock);
ed5979
+}
ed5979
+
ed5979
+void accel_cpu_ioctl_end(CPUState *cpu)
ed5979
+{
ed5979
+    if (unlikely(qemu_mutex_iothread_locked())) {
ed5979
+        return;
ed5979
+    }
ed5979
+
ed5979
+    qemu_lockcnt_dec(&cpu->in_ioctl_lock);
ed5979
+    /* change event to SET. If event was BUSY, wake up all waiters */
ed5979
+    qemu_event_set(&accel_in_ioctl_event);
ed5979
+}
ed5979
+
ed5979
+static bool accel_has_to_wait(void)
ed5979
+{
ed5979
+    CPUState *cpu;
ed5979
+    bool needs_to_wait = false;
ed5979
+
ed5979
+    CPU_FOREACH(cpu) {
ed5979
+        if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
ed5979
+            /* exit the ioctl, if vcpu is running it */
ed5979
+            qemu_cpu_kick(cpu);
ed5979
+            needs_to_wait = true;
ed5979
+        }
ed5979
+    }
ed5979
+
ed5979
+    return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
ed5979
+}
ed5979
+
ed5979
+void accel_ioctl_inhibit_begin(void)
ed5979
+{
ed5979
+    CPUState *cpu;
ed5979
+
ed5979
+    /*
ed5979
+     * We allow to inhibit only when holding the BQL, so we can identify
ed5979
+     * when an inhibitor wants to issue an ioctl easily.
ed5979
+     */
ed5979
+    g_assert(qemu_mutex_iothread_locked());
ed5979
+
ed5979
+    /* Block further invocations of the ioctls outside the BQL.  */
ed5979
+    CPU_FOREACH(cpu) {
ed5979
+        qemu_lockcnt_lock(&cpu->in_ioctl_lock);
ed5979
+    }
ed5979
+    qemu_lockcnt_lock(&accel_in_ioctl_lock);
ed5979
+
ed5979
+    /* Keep waiting until there are running ioctls */
ed5979
+    while (true) {
ed5979
+
ed5979
+        /* Reset event to FREE. */
ed5979
+        qemu_event_reset(&accel_in_ioctl_event);
ed5979
+
ed5979
+        if (accel_has_to_wait()) {
ed5979
+            /*
ed5979
+             * If event is still FREE, and there are ioctls still in progress,
ed5979
+             * wait.
ed5979
+             *
ed5979
+             *  If an ioctl finishes before qemu_event_wait(), it will change
ed5979
+             * the event state to SET. This will prevent qemu_event_wait() from
ed5979
+             * blocking, but it's not a problem because if other ioctls are
ed5979
+             * still running the loop will iterate once more and reset the event
ed5979
+             * status to FREE so that it can wait properly.
ed5979
+             *
ed5979
+             * If an ioctls finishes while qemu_event_wait() is blocking, then
ed5979
+             * it will be waken up, but also here the while loop makes sure
ed5979
+             * to re-enter the wait if there are other running ioctls.
ed5979
+             */
ed5979
+            qemu_event_wait(&accel_in_ioctl_event);
ed5979
+        } else {
ed5979
+            /* No ioctl is running */
ed5979
+            return;
ed5979
+        }
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
+void accel_ioctl_inhibit_end(void)
ed5979
+{
ed5979
+    CPUState *cpu;
ed5979
+
ed5979
+    qemu_lockcnt_unlock(&accel_in_ioctl_lock);
ed5979
+    CPU_FOREACH(cpu) {
ed5979
+        qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
diff --git a/accel/meson.build b/accel/meson.build
ed5979
index 259c35c4c8..061332610f 100644
ed5979
--- a/accel/meson.build
ed5979
+++ b/accel/meson.build
ed5979
@@ -1,4 +1,4 @@
ed5979
-specific_ss.add(files('accel-common.c'))
ed5979
+specific_ss.add(files('accel-common.c', 'accel-blocker.c'))
ed5979
 softmmu_ss.add(files('accel-softmmu.c'))
ed5979
 user_ss.add(files('accel-user.c'))
ed5979
 
ed5979
diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
ed5979
index f9fdd46b9d..8d6a4b1b65 100644
ed5979
--- a/hw/core/cpu-common.c
ed5979
+++ b/hw/core/cpu-common.c
ed5979
@@ -237,6 +237,7 @@ static void cpu_common_initfn(Object *obj)
ed5979
     cpu->nr_threads = 1;
ed5979
 
ed5979
     qemu_mutex_init(&cpu->work_mutex);
ed5979
+    qemu_lockcnt_init(&cpu->in_ioctl_lock);
ed5979
     QSIMPLEQ_INIT(&cpu->work_list);
ed5979
     QTAILQ_INIT(&cpu->breakpoints);
ed5979
     QTAILQ_INIT(&cpu->watchpoints);
ed5979
@@ -248,6 +249,7 @@ static void cpu_common_finalize(Object *obj)
ed5979
 {
ed5979
     CPUState *cpu = CPU(obj);
ed5979
 
ed5979
+    qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
ed5979
     qemu_mutex_destroy(&cpu->work_mutex);
ed5979
 }
ed5979
 
ed5979
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
ed5979
index 8830546121..2417597236 100644
ed5979
--- a/include/hw/core/cpu.h
ed5979
+++ b/include/hw/core/cpu.h
ed5979
@@ -398,6 +398,9 @@ struct CPUState {
ed5979
     uint32_t kvm_fetch_index;
ed5979
     uint64_t dirty_pages;
ed5979
 
ed5979
+    /* Use by accel-block: CPU is executing an ioctl() */
ed5979
+    QemuLockCnt in_ioctl_lock;
ed5979
+
ed5979
     /* Used for events with 'vcpu' and *without* the 'disabled' properties */
ed5979
     DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
ed5979
     DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
ed5979
diff --git a/include/sysemu/accel-blocker.h b/include/sysemu/accel-blocker.h
ed5979
new file mode 100644
ed5979
index 0000000000..72020529ef
ed5979
--- /dev/null
ed5979
+++ b/include/sysemu/accel-blocker.h
ed5979
@@ -0,0 +1,56 @@
ed5979
+/*
ed5979
+ * Accelerator blocking API, to prevent new ioctls from starting and wait the
ed5979
+ * running ones finish.
ed5979
+ * This mechanism differs from pause/resume_all_vcpus() in that it does not
ed5979
+ * release the BQL.
ed5979
+ *
ed5979
+ *  Copyright (c) 2022 Red Hat Inc.
ed5979
+ *
ed5979
+ * Author: Emanuele Giuseppe Esposito       <eesposit@redhat.com>
ed5979
+ *
ed5979
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
ed5979
+ * See the COPYING file in the top-level directory.
ed5979
+ */
ed5979
+#ifndef ACCEL_BLOCKER_H
ed5979
+#define ACCEL_BLOCKER_H
ed5979
+
ed5979
+#include "qemu/osdep.h"
ed5979
+#include "sysemu/cpus.h"
ed5979
+
ed5979
+extern void accel_blocker_init(void);
ed5979
+
ed5979
+/*
ed5979
+ * accel_{cpu_}ioctl_begin/end:
ed5979
+ * Mark when ioctl is about to run or just finished.
ed5979
+ *
ed5979
+ * accel_{cpu_}ioctl_begin will block after accel_ioctl_inhibit_begin() is
ed5979
+ * called, preventing new ioctls to run. They will continue only after
ed5979
+ * accel_ioctl_inibith_end().
ed5979
+ */
ed5979
+extern void accel_ioctl_begin(void);
ed5979
+extern void accel_ioctl_end(void);
ed5979
+extern void accel_cpu_ioctl_begin(CPUState *cpu);
ed5979
+extern void accel_cpu_ioctl_end(CPUState *cpu);
ed5979
+
ed5979
+/*
ed5979
+ * accel_ioctl_inhibit_begin: start critical section
ed5979
+ *
ed5979
+ * This function makes sure that:
ed5979
+ * 1) incoming accel_{cpu_}ioctl_begin() calls block
ed5979
+ * 2) wait that all ioctls that were already running reach
ed5979
+ *    accel_{cpu_}ioctl_end(), kicking vcpus if necessary.
ed5979
+ *
ed5979
+ * This allows the caller to access shared data or perform operations without
ed5979
+ * worrying of concurrent vcpus accesses.
ed5979
+ */
ed5979
+extern void accel_ioctl_inhibit_begin(void);
ed5979
+
ed5979
+/*
ed5979
+ * accel_ioctl_inhibit_end: end critical section started by
ed5979
+ * accel_ioctl_inhibit_begin()
ed5979
+ *
ed5979
+ * This function allows blocked accel_{cpu_}ioctl_begin() to continue.
ed5979
+ */
ed5979
+extern void accel_ioctl_inhibit_end(void);
ed5979
+
ed5979
+#endif /* ACCEL_BLOCKER_H */
ed5979
diff --git a/util/meson.build b/util/meson.build
ed5979
index 25b9b61f98..85a5504c4d 100644
ed5979
--- a/util/meson.build
ed5979
+++ b/util/meson.build
ed5979
@@ -57,6 +57,7 @@ util_ss.add(files('guest-random.c'))
ed5979
 util_ss.add(files('yank.c'))
ed5979
 util_ss.add(files('int128.c'))
ed5979
 util_ss.add(files('memalign.c'))
ed5979
+util_ss.add(files('lockcnt.c'))
ed5979
 
ed5979
 if have_user
ed5979
   util_ss.add(files('selfmap.c'))
ed5979
@@ -71,7 +72,6 @@ endif
ed5979
 if have_block or have_ga
ed5979
   util_ss.add(files('aiocb.c', 'async.c'))
ed5979
   util_ss.add(files('base64.c'))
ed5979
-  util_ss.add(files('lockcnt.c'))
ed5979
   util_ss.add(files('main-loop.c'))
ed5979
   util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c', 'qemu-coroutine-io.c'))
ed5979
   util_ss.add(files('coroutine-@0@.c'.format(config_host['CONFIG_COROUTINE_BACKEND'])))
ed5979
-- 
ed5979
2.31.1
ed5979