Blame SOURCES/kvm-vl-add-qemu_add_vm_change_state_handler_prio.patch

8b1478
From f32ee7f16206334c90d2c92517617c08f436ca97 Mon Sep 17 00:00:00 2001
8b1478
From: Stefan Hajnoczi <stefanha@redhat.com>
8b1478
Date: Tue, 16 Jul 2019 13:22:13 +0200
8b1478
Subject: [PATCH 20/23] vl: add qemu_add_vm_change_state_handler_prio()
8b1478
8b1478
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
8b1478
Message-id: <20190716132215.18503-2-stefanha@redhat.com>
8b1478
Patchwork-id: 89536
8b1478
O-Subject: [RHEL-7.8 RHEL-7.7.z qemu-kvm-rhev PATCH 1/3] vl: add qemu_add_vm_change_state_handler_prio()
8b1478
Bugzilla: 1673546
8b1478
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
8b1478
RH-Acked-by: John Snow <jsnow@redhat.com>
8b1478
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
8b1478
8b1478
Add an API for registering vm change state handlers with a well-defined
8b1478
ordering.  This is necessary when handlers depend on each other.
8b1478
8b1478
Small coding style fixes are included to make checkpatch.pl happy.
8b1478
8b1478
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
8b1478
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
8b1478
(cherry picked from commit 60dbc5a1c5176269669ffc26c081ab2cfb7f12f7)
8b1478
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
8b1478
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
8b1478
8b1478
Conflicts:
8b1478
  vl.c
8b1478
  The QTAILQ macros require an explicit name for the head type.
8b1478
---
8b1478
 include/sysemu/sysemu.h |  2 ++
8b1478
 vl.c                    | 61 +++++++++++++++++++++++++++++++++++++++----------
8b1478
 2 files changed, 51 insertions(+), 12 deletions(-)
8b1478
8b1478
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
8b1478
index 2a6f4a5..723c7a9 100644
8b1478
--- a/include/sysemu/sysemu.h
8b1478
+++ b/include/sysemu/sysemu.h
8b1478
@@ -29,6 +29,8 @@ typedef void VMChangeStateHandler(void *opaque, int running, RunState state);
8b1478
 
8b1478
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
8b1478
                                                      void *opaque);
8b1478
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
8b1478
+        VMChangeStateHandler *cb, void *opaque, int priority);
8b1478
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
8b1478
 void vm_state_notify(int running, RunState state);
8b1478
 
8b1478
diff --git a/vl.c b/vl.c
8b1478
index 5b337e1..6529d68 100644
8b1478
--- a/vl.c
8b1478
+++ b/vl.c
8b1478
@@ -1658,28 +1658,58 @@ static int machine_help_func(QemuOpts *opts, MachineState *machine)
8b1478
 struct vm_change_state_entry {
8b1478
     VMChangeStateHandler *cb;
8b1478
     void *opaque;
8b1478
-    QLIST_ENTRY (vm_change_state_entry) entries;
8b1478
+    QTAILQ_ENTRY(vm_change_state_entry) entries;
8b1478
+    int priority;
8b1478
 };
8b1478
 
8b1478
-static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
8b1478
+static QTAILQ_HEAD(VMChangeStateHead,
8b1478
+                   vm_change_state_entry) vm_change_state_head;
8b1478
 
8b1478
-VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
8b1478
-                                                     void *opaque)
8b1478
+/**
8b1478
+ * qemu_add_vm_change_state_handler_prio:
8b1478
+ * @cb: the callback to invoke
8b1478
+ * @opaque: user data passed to the callback
8b1478
+ * @priority: low priorities execute first when the vm runs and the reverse is
8b1478
+ *            true when the vm stops
8b1478
+ *
8b1478
+ * Register a callback function that is invoked when the vm starts or stops
8b1478
+ * running.
8b1478
+ *
8b1478
+ * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
8b1478
+ */
8b1478
+VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
8b1478
+        VMChangeStateHandler *cb, void *opaque, int priority)
8b1478
 {
8b1478
     VMChangeStateEntry *e;
8b1478
+    VMChangeStateEntry *other;
8b1478
 
8b1478
-    e = g_malloc0(sizeof (*e));
8b1478
-
8b1478
+    e = g_malloc0(sizeof(*e));
8b1478
     e->cb = cb;
8b1478
     e->opaque = opaque;
8b1478
-    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
8b1478
+    e->priority = priority;
8b1478
+
8b1478
+    /* Keep list sorted in ascending priority order */
8b1478
+    QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
8b1478
+        if (priority < other->priority) {
8b1478
+            QTAILQ_INSERT_BEFORE(other, e, entries);
8b1478
+            return e;
8b1478
+        }
8b1478
+    }
8b1478
+
8b1478
+    QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
8b1478
     return e;
8b1478
 }
8b1478
 
8b1478
+VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
8b1478
+                                                     void *opaque)
8b1478
+{
8b1478
+    return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
8b1478
+}
8b1478
+
8b1478
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
8b1478
 {
8b1478
-    QLIST_REMOVE (e, entries);
8b1478
-    g_free (e);
8b1478
+    QTAILQ_REMOVE(&vm_change_state_head, e, entries);
8b1478
+    g_free(e);
8b1478
 }
8b1478
 
8b1478
 void vm_state_notify(int running, RunState state)
8b1478
@@ -1688,8 +1718,15 @@ void vm_state_notify(int running, RunState state)
8b1478
 
8b1478
     trace_vm_state_notify(running, state);
8b1478
 
8b1478
-    QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
8b1478
-        e->cb(e->opaque, running, state);
8b1478
+    if (running) {
8b1478
+        QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
8b1478
+            e->cb(e->opaque, running, state);
8b1478
+        }
8b1478
+    } else {
8b1478
+        QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head,
8b1478
+                                    VMChangeStateHead, entries, next) {
8b1478
+            e->cb(e->opaque, running, state);
8b1478
+        }
8b1478
     }
8b1478
 }
8b1478
 
8b1478
@@ -3194,7 +3231,7 @@ int main(int argc, char **argv, char **envp)
8b1478
     }
8b1478
     rtc_clock = QEMU_CLOCK_HOST;
8b1478
 
8b1478
-    QLIST_INIT (&vm_change_state_head);
8b1478
+    QTAILQ_INIT(&vm_change_state_head);
8b1478
     os_setup_early_signal_handling();
8b1478
 
8b1478
     cpu_model = NULL;
8b1478
-- 
8b1478
1.8.3.1
8b1478