218e99
From 1666ea1986c2fdce3bc27aa96ae6bf3a632dbc99 Mon Sep 17 00:00:00 2001
218e99
From: Nigel Croxon <ncroxon@redhat.com>
218e99
Date: Wed, 31 Jul 2013 15:12:17 +0200
218e99
Subject: Introduce async_run_on_cpu
218e99
218e99
RH-Author: Nigel Croxon <ncroxon@redhat.com>
218e99
Message-id: <1375283539-18714-2-git-send-email-ncroxon@redhat.com>
218e99
Patchwork-id: 52874
218e99
O-Subject: [RHEL7 PATCH 1/3] Introduce async_run_on_cpu
218e99
Bugzilla: 985958
218e99
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>
218e99
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
218e99
Bugzilla: 985958 - Throttle-down guest to help with live migration convergence (backport to RHEL7.0)
218e99
https://bugzilla.redhat.com/show_bug.cgi?id=985958
218e99
218e99
Backported from the following upstream commit:
218e99
218e99
commit 3c02270db980007424d797506301826310ce2db4
218e99
Author: Chegu Vinod <chegu_vinod@hp.com>
218e99
Date:   Mon Jun 24 03:49:41 2013 -0600
218e99
218e99
    Introduce async_run_on_cpu()
218e99
218e99
    Introduce an asynchronous version of run_on_cpu() i.e. the caller
218e99
    doesn't have to block till the call back routine finishes execution
218e99
    on the target vcpu.
218e99
218e99
    Signed-off-by: Chegu Vinod <chegu_vinod@hp.com>
218e99
    Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
    Signed-off-by: Juan Quintela <quintela@redhat.com>
218e99
218e99
diff --git a/cpus.c b/cpus.c
218e99
index c232265..8cd4eab 100644
218e99
--- a/cpus.c
218e99
+++ b/cpus.c
218e99
@@ -653,6 +653,7 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
218e99
 
218e99
     wi.func = func;
218e99
     wi.data = data;
218e99
+    wi.free = false;
218e99
     if (cpu->queued_work_first == NULL) {
218e99
         cpu->queued_work_first = &wi;
218e99
     } else {
218e99
@@ -671,6 +672,31 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
218e99
     }
218e99
 }
218e99
 
218e99
+void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
218e99
+{
218e99
+    struct qemu_work_item *wi;
218e99
+
218e99
+    if (qemu_cpu_is_self(cpu)) {
218e99
+        func(data);
218e99
+        return;
218e99
+    }
218e99
+
218e99
+    wi = g_malloc0(sizeof(struct qemu_work_item));
218e99
+    wi->func = func;
218e99
+    wi->data = data;
218e99
+    wi->free = true;
218e99
+    if (cpu->queued_work_first == NULL) {
218e99
+        cpu->queued_work_first = wi;
218e99
+    } else {
218e99
+        cpu->queued_work_last->next = wi;
218e99
+    }
218e99
+    cpu->queued_work_last = wi;
218e99
+    wi->next = NULL;
218e99
+    wi->done = false;
218e99
+
218e99
+    qemu_cpu_kick(cpu);
218e99
+}
218e99
+
218e99
 static void flush_queued_work(CPUState *cpu)
218e99
 {
218e99
     struct qemu_work_item *wi;
218e99
@@ -683,6 +709,9 @@ static void flush_queued_work(CPUState *cpu)
218e99
         cpu->queued_work_first = wi->next;
218e99
         wi->func(wi->data);
218e99
         wi->done = true;
218e99
+        if (wi->free) {
218e99
+            g_free(wi);
218e99
+        }
218e99
     }
218e99
     cpu->queued_work_last = NULL;
218e99
     qemu_cond_broadcast(&qemu_work_cond);
218e99
diff --git a/include/qemu-common.h b/include/qemu-common.h
218e99
index 3b1ca8e..73c6419 100644
218e99
--- a/include/qemu-common.h
218e99
+++ b/include/qemu-common.h
218e99
@@ -288,6 +288,7 @@ struct qemu_work_item {
218e99
     void (*func)(void *data);
218e99
     void *data;
218e99
     int done;
218e99
+    bool free;
218e99
 };
218e99
 
218e99
 #ifdef CONFIG_USER_ONLY
218e99
diff --git a/include/qom/cpu.h b/include/qom/cpu.h
218e99
index 7cd9442..46465e9 100644
218e99
--- a/include/qom/cpu.h
218e99
+++ b/include/qom/cpu.h
218e99
@@ -265,6 +265,16 @@ bool cpu_is_stopped(CPUState *cpu);
218e99
 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
218e99
 
218e99
 /**
218e99
+ * async_run_on_cpu:
218e99
+ * @cpu: The vCPU to run on.
218e99
+ * @func: The function to be executed.
218e99
+ * @data: Data to pass to the function.
218e99
+ *
218e99
+ * Schedules the function @func for execution on the vCPU @cpu asynchronously.
218e99
+ */
218e99
+void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
218e99
+
218e99
+/**
218e99
  * qemu_for_each_cpu:
218e99
  * @func: The function to be executed.
218e99
  * @data: Data to pass to the function.