thebeanogamer / rpms / qemu-kvm

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