b38b0f
From e7dc49f6bf6fd242b5b7a83bc270664a1ca45879 Mon Sep 17 00:00:00 2001
b38b0f
From: Paolo Bonzini <pbonzini@redhat.com>
b38b0f
Date: Mon, 22 Jul 2019 18:22:08 +0100
b38b0f
Subject: [PATCH 27/39] KVM: Introduce kvm_arch_destroy_vcpu()
b38b0f
b38b0f
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
b38b0f
Message-id: <20190722182220.19374-7-pbonzini@redhat.com>
b38b0f
Patchwork-id: 89623
b38b0f
O-Subject: [RHEL-8.1.0 PATCH qemu-kvm v3 06/18] KVM: Introduce kvm_arch_destroy_vcpu()
b38b0f
Bugzilla: 1689269
b38b0f
RH-Acked-by: Peter Xu <zhexu@redhat.com>
b38b0f
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
b38b0f
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
b38b0f
b38b0f
From: Liran Alon <liran.alon@oracle.com>
b38b0f
b38b0f
Simiar to how kvm_init_vcpu() calls kvm_arch_init_vcpu() to perform
b38b0f
arch-dependent initialisation, introduce kvm_arch_destroy_vcpu()
b38b0f
to be called from kvm_destroy_vcpu() to perform arch-dependent
b38b0f
destruction.
b38b0f
b38b0f
This was added because some architectures (Such as i386)
b38b0f
currently do not free memory that it have allocated in
b38b0f
kvm_arch_init_vcpu().
b38b0f
b38b0f
Suggested-by: Maran Wilson <maran.wilson@oracle.com>
b38b0f
Reviewed-by: Maran Wilson <maran.wilson@oracle.com>
b38b0f
Signed-off-by: Liran Alon <liran.alon@oracle.com>
b38b0f
Message-Id: <20190619162140.133674-3-liran.alon@oracle.com>
b38b0f
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
b38b0f
(cherry picked from commit b1115c99919cf158bb859865f14c3198a0e6f679)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 accel/kvm/kvm-all.c  |  5 +++++
b38b0f
 include/sysemu/kvm.h |  1 +
b38b0f
 target/arm/kvm32.c   |  5 +++++
b38b0f
 target/arm/kvm64.c   |  5 +++++
b38b0f
 target/i386/kvm.c    | 12 ++++++++++++
b38b0f
 target/mips/kvm.c    |  5 +++++
b38b0f
 target/ppc/kvm.c     |  5 +++++
b38b0f
 target/s390x/kvm.c   | 10 ++++++++++
b38b0f
 8 files changed, 48 insertions(+)
b38b0f
b38b0f
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
b38b0f
index 3a7c8a3..a939b26 100644
b38b0f
--- a/accel/kvm/kvm-all.c
b38b0f
+++ b/accel/kvm/kvm-all.c
b38b0f
@@ -290,6 +290,11 @@ int kvm_destroy_vcpu(CPUState *cpu)
b38b0f
 
b38b0f
     DPRINTF("kvm_destroy_vcpu\n");
b38b0f
 
b38b0f
+    ret = kvm_arch_destroy_vcpu(cpu);
b38b0f
+    if (ret < 0) {
b38b0f
+        goto err;
b38b0f
+    }
b38b0f
+
b38b0f
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
b38b0f
     if (mmap_size < 0) {
b38b0f
         ret = mmap_size;
b38b0f
diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
b38b0f
index 3d8f294..a5a6dff 100644
b38b0f
--- a/include/sysemu/kvm.h
b38b0f
+++ b/include/sysemu/kvm.h
b38b0f
@@ -372,6 +372,7 @@ int kvm_arch_put_registers(CPUState *cpu, int level);
b38b0f
 int kvm_arch_init(MachineState *ms, KVMState *s);
b38b0f
 
b38b0f
 int kvm_arch_init_vcpu(CPUState *cpu);
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cpu);
b38b0f
 
b38b0f
 bool kvm_vcpu_id_is_valid(int vcpu_id);
b38b0f
 
b38b0f
diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c
b38b0f
index 1740cda..056f016 100644
b38b0f
--- a/target/arm/kvm32.c
b38b0f
+++ b/target/arm/kvm32.c
b38b0f
@@ -237,6 +237,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return kvm_arm_init_cpreg_list(cpu);
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+	return 0;
b38b0f
+}
b38b0f
+
b38b0f
 typedef struct Reg {
b38b0f
     uint64_t id;
b38b0f
     int offset;
b38b0f
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
b38b0f
index e0b8246..c9c761e 100644
b38b0f
--- a/target/arm/kvm64.c
b38b0f
+++ b/target/arm/kvm64.c
b38b0f
@@ -549,6 +549,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return kvm_arm_init_cpreg_list(cpu);
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+    return 0;
b38b0f
+}
b38b0f
+
b38b0f
 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
b38b0f
 {
b38b0f
     /* Return true if the regidx is a register we should synchronize
b38b0f
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
b38b0f
index 8e861a1..305809f 100644
b38b0f
--- a/target/i386/kvm.c
b38b0f
+++ b/target/i386/kvm.c
b38b0f
@@ -1196,6 +1196,18 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return r;
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+    X86CPU *cpu = X86_CPU(cs);
b38b0f
+
b38b0f
+    if (cpu->kvm_msr_buf) {
b38b0f
+        g_free(cpu->kvm_msr_buf);
b38b0f
+        cpu->kvm_msr_buf = NULL;
b38b0f
+    }
b38b0f
+
b38b0f
+    return 0;
b38b0f
+}
b38b0f
+
b38b0f
 void kvm_arch_reset_vcpu(X86CPU *cpu)
b38b0f
 {
b38b0f
     CPUX86State *env = &cpu->env;
b38b0f
diff --git a/target/mips/kvm.c b/target/mips/kvm.c
b38b0f
index 8e72850..938f8f1 100644
b38b0f
--- a/target/mips/kvm.c
b38b0f
+++ b/target/mips/kvm.c
b38b0f
@@ -91,6 +91,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return ret;
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+    return 0;
b38b0f
+}
b38b0f
+
b38b0f
 void kvm_mips_reset_vcpu(MIPSCPU *cpu)
b38b0f
 {
b38b0f
     CPUMIPSState *env = &cpu->env;
b38b0f
diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
b38b0f
index 058dcbe..b9858fa 100644
b38b0f
--- a/target/ppc/kvm.c
b38b0f
+++ b/target/ppc/kvm.c
b38b0f
@@ -607,6 +607,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return ret;
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+    return 0;
b38b0f
+}
b38b0f
+
b38b0f
 static void kvm_sw_tlb_put(PowerPCCPU *cpu)
b38b0f
 {
b38b0f
     CPUPPCState *env = &cpu->env;
b38b0f
diff --git a/target/s390x/kvm.c b/target/s390x/kvm.c
b38b0f
index 1d6cc33..0814333 100644
b38b0f
--- a/target/s390x/kvm.c
b38b0f
+++ b/target/s390x/kvm.c
b38b0f
@@ -370,6 +370,16 @@ int kvm_arch_init_vcpu(CPUState *cs)
b38b0f
     return 0;
b38b0f
 }
b38b0f
 
b38b0f
+int kvm_arch_destroy_vcpu(CPUState *cs)
b38b0f
+{
b38b0f
+    S390CPU *cpu = S390_CPU(cs);
b38b0f
+
b38b0f
+    g_free(cpu->irqstate);
b38b0f
+    cpu->irqstate = NULL;
b38b0f
+
b38b0f
+    return 0;
b38b0f
+}
b38b0f
+
b38b0f
 void kvm_s390_reset_vcpu(S390CPU *cpu)
b38b0f
 {
b38b0f
     CPUState *cs = CPU(cpu);
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f