thebeanogamer / rpms / qemu-kvm

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