qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 4/4] target/i386: replace strerror() function to the thread sa


From: Yohei Kojima
Subject: [PATCH v3 4/4] target/i386: replace strerror() function to the thread safe qemu_strerror()
Date: Fri, 31 Mar 2023 02:13:22 +0900

strerror() is not guaranteed to be thread-safe as described in
(https://gitlab.com/qemu-project/qemu/-/issues/416).

This commit changes files under /target/i386 that call strerror() to
call the safer qemu_strerror().

Signed-off-by: Yohei Kojima <y-koj@outlook.jp>
---
 target/i386/kvm/kvm.c             | 49 ++++++++++++++++---------------
 target/i386/kvm/xen-emu.c         |  7 +++--
 target/i386/nvmm/nvmm-accel-ops.c |  2 +-
 target/i386/sev.c                 |  5 ++--
 target/i386/whpx/whpx-accel-ops.c |  2 +-
 5 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index de531842f6..b31810f108 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -41,6 +41,7 @@
 #include "qemu/main-loop.h"
 #include "qemu/ratelimit.h"
 #include "qemu/config-file.h"
+#include "qemu/cutils.h"
 #include "qemu/error-report.h"
 #include "qemu/memalign.h"
 #include "hw/i386/x86.h"
@@ -275,7 +276,7 @@ static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int 
max)
             return NULL;
         } else {
             fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
-                    strerror(-r));
+                    qemu_strerror(-r));
             exit(1);
         }
     }
@@ -519,7 +520,7 @@ uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, 
uint32_t index)
     ret = kvm_ioctl(s, KVM_GET_MSRS, &msr_data);
     if (ret != 1) {
         error_report("KVM get MSR (index=0x%x) feature failed, %s",
-            index, strerror(-ret));
+            index, qemu_strerror(-ret));
         exit(1);
     }
 
@@ -1055,7 +1056,7 @@ static struct kvm_cpuid2 *try_get_hv_cpuid(CPUState *cs, 
int max,
             return NULL;
         } else {
             fprintf(stderr, "KVM_GET_SUPPORTED_HV_CPUID failed: %s\n",
-                    strerror(-r));
+                    qemu_strerror(-r));
             exit(1);
         }
     }
@@ -1642,7 +1643,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
         ret = kvm_vcpu_enable_cap(cs, synic_cap, 0);
         if (ret < 0) {
             error_report("failed to turn on HyperV SynIC in KVM: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             return ret;
         }
 
@@ -1650,7 +1651,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
             ret = hyperv_x86_synic_add(cpu);
             if (ret < 0) {
                 error_report("failed to create HyperV SynIC: %s",
-                             strerror(-ret));
+                             qemu_strerror(-ret));
                 return ret;
             }
         }
@@ -1689,7 +1690,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
         ret = kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_ENFORCE_CPUID, 0, 1);
         if (ret < 0) {
             error_report("failed to enable KVM_CAP_HYPERV_ENFORCE_CPUID: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             return ret;
         }
     }
@@ -1918,7 +1919,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (r < 0) {
             fprintf(stderr,
                     "failed to enable KVM_CAP_ENFORCE_PV_FEATURE_CPUID: %s",
-                    strerror(-r));
+                    qemu_strerror(-r));
             abort();
         }
     }
@@ -2156,7 +2157,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
 
         ret = kvm_get_mce_cap_supported(cs->kvm_state, &mcg_cap, &banks);
         if (ret < 0) {
-            fprintf(stderr, "kvm_get_mce_cap_supported: %s", strerror(-ret));
+            fprintf(stderr, "kvm_get_mce_cap_supported: %s",
+                    qemu_strerror(-ret));
             return ret;
         }
 
@@ -2179,7 +2181,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         env->mcg_cap &= mcg_cap | MCG_CAP_BANKS_MASK;
         ret = kvm_vcpu_ioctl(cs, KVM_X86_SETUP_MCE, &env->mcg_cap);
         if (ret < 0) {
-            fprintf(stderr, "KVM_X86_SETUP_MCE: %s", strerror(-ret));
+            fprintf(stderr, "KVM_X86_SETUP_MCE: %s", qemu_strerror(-ret));
             return ret;
         }
     }
@@ -2354,7 +2356,7 @@ static int kvm_get_supported_feature_msrs(KVMState *s)
     ret = kvm_ioctl(s, KVM_GET_MSR_FEATURE_INDEX_LIST, &msr_list);
     if (ret < 0 && ret != -E2BIG) {
         error_report("Fetch KVM feature MSR list failed: %s",
-            strerror(-ret));
+            qemu_strerror(-ret));
         return ret;
     }
 
@@ -2367,7 +2369,7 @@ static int kvm_get_supported_feature_msrs(KVMState *s)
 
     if (ret < 0) {
         error_report("Fetch KVM feature MSR list failed: %s",
-            strerror(-ret));
+            qemu_strerror(-ret));
         g_free(kvm_feature_msrs);
         kvm_feature_msrs = NULL;
         return ret;
@@ -2595,7 +2597,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         ret = kvm_vm_enable_cap(s, KVM_CAP_EXCEPTION_PAYLOAD, 0, true);
         if (ret < 0) {
             error_report("kvm: Failed to enable exception payload cap: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             return ret;
         }
     }
@@ -2605,7 +2607,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         ret = kvm_vm_enable_cap(s, KVM_CAP_X86_TRIPLE_FAULT_EVENT, 0, true);
         if (ret < 0) {
             error_report("kvm: Failed to enable triple fault event cap: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             return ret;
         }
     }
@@ -2707,7 +2709,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
                                 disable_exits);
         if (ret < 0) {
             error_report("kvm: guest stopping CPU not supported: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
         }
     }
 
@@ -2724,7 +2726,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
                                     KVM_BUS_LOCK_DETECTION_EXIT);
             if (ret < 0) {
                 error_report("kvm: Failed to enable bus lock detection cap: 
%s",
-                             strerror(-ret));
+                             qemu_strerror(-ret));
                 return ret;
             }
             ratelimit_init(&bus_lock_ratelimit_ctrl);
@@ -2743,7 +2745,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
                                     notify_window_flags);
             if (ret < 0) {
                 error_report("kvm: Failed to enable notify vmexit cap: %s",
-                             strerror(-ret));
+                             qemu_strerror(-ret));
                 return ret;
             }
     }
@@ -2754,7 +2756,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
                                 KVM_MSR_EXIT_REASON_FILTER);
         if (ret) {
             error_report("Could not enable user space MSRs: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             exit(1);
         }
 
@@ -2762,7 +2764,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
                            kvm_rdmsr_core_thread_count, NULL);
         if (!r) {
             error_report("Could not install MSR_CORE_THREAD_COUNT handler: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             exit(1);
         }
     }
@@ -4889,7 +4891,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
             ret = kvm_vcpu_ioctl(cpu, KVM_NMI);
             if (ret < 0) {
                 fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n",
-                        strerror(-ret));
+                        qemu_strerror(-ret));
             }
         }
         if (cpu->interrupt_request & CPU_INTERRUPT_SMI) {
@@ -4900,7 +4902,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
             ret = kvm_vcpu_ioctl(cpu, KVM_SMI);
             if (ret < 0) {
                 fprintf(stderr, "KVM: injection failed, SMI lost (%s)\n",
-                        strerror(-ret));
+                        qemu_strerror(-ret));
             }
         }
     }
@@ -4941,7 +4943,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
                 if (ret < 0) {
                     fprintf(stderr,
                             "KVM: injection failed, interrupt lost (%s)\n",
-                            strerror(-ret));
+                            qemu_strerror(-ret));
                 }
             }
         }
@@ -5414,7 +5416,8 @@ static bool __kvm_enable_sgx_provisioning(KVMState *s)
 
     ret = kvm_vm_enable_cap(s, KVM_CAP_SGX_ATTRIBUTE, 0, fd);
     if (ret) {
-        error_report("Could not enable SGX PROVISIONKEY: %s", strerror(-ret));
+        error_report("Could not enable SGX PROVISIONKEY: %s",
+                     qemu_strerror(-ret));
         exit(1);
     }
     close(fd);
@@ -5580,7 +5583,7 @@ int kvm_arch_irqchip_create(KVMState *s)
         ret = kvm_vm_enable_cap(s, KVM_CAP_SPLIT_IRQCHIP, 0, 24);
         if (ret) {
             error_report("Could not enable split irqchip mode: %s",
-                         strerror(-ret));
+                         qemu_strerror(-ret));
             exit(1);
         } else {
             DPRINTF("Enabled KVM_CAP_SPLIT_IRQCHIP\n");
diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c
index d7c7eb8d9c..f7837ff95a 100644
--- a/target/i386/kvm/xen-emu.c
+++ b/target/i386/kvm/xen-emu.c
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "qemu/log.h"
 #include "qemu/main-loop.h"
+#include "qemu/cutils.h"
 #include "qemu/error-report.h"
 #include "hw/xen/xen.h"
 #include "sysemu/kvm_int.h"
@@ -135,7 +136,7 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
     ret = kvm_vm_ioctl(s, KVM_XEN_HVM_CONFIG, &cfg);
     if (ret < 0) {
         error_report("kvm: Failed to enable Xen HVM support: %s",
-                     strerror(-ret));
+                     qemu_strerror(-ret));
         return ret;
     }
 
@@ -209,7 +210,7 @@ int kvm_xen_init_vcpu(CPUState *cs)
         err = kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &va);
         if (err) {
             error_report("kvm: Failed to set Xen vCPU ID attribute: %s",
-                         strerror(-err));
+                         qemu_strerror(-err));
             return err;
         }
     }
@@ -964,7 +965,7 @@ static uint64_t kvm_get_current_ns(void)
 
     ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
     if (ret < 0) {
-        fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
+        fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", qemu_strerror(ret));
                 abort();
     }
 
diff --git a/target/i386/nvmm/nvmm-accel-ops.c 
b/target/i386/nvmm/nvmm-accel-ops.c
index 6c46101ac1..97d9daacea 100644
--- a/target/i386/nvmm/nvmm-accel-ops.c
+++ b/target/i386/nvmm/nvmm-accel-ops.c
@@ -32,7 +32,7 @@ static void *qemu_nvmm_cpu_thread_fn(void *arg)
 
     r = nvmm_init_vcpu(cpu);
     if (r < 0) {
-        fprintf(stderr, "nvmm_init_vcpu failed: %s\n", strerror(-r));
+        fprintf(stderr, "nvmm_init_vcpu failed: %s\n", qemu_strerror(-r));
         exit(1);
     }
 
diff --git a/target/i386/sev.c b/target/i386/sev.c
index 859e06f6ad..9f19fc5469 100644
--- a/target/i386/sev.c
+++ b/target/i386/sev.c
@@ -38,6 +38,7 @@
 #include "exec/confidential-guest-support.h"
 #include "hw/i386/pc.h"
 #include "exec/address-spaces.h"
+#include "qemu/cutils.h"
 
 #define TYPE_SEV_GUEST "sev-guest"
 OBJECT_DECLARE_SIMPLE_TYPE(SevGuestState, SEV_GUEST)
@@ -247,7 +248,7 @@ sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t 
size,
     r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
     if (r) {
         error_report("%s: failed to register region (%p+%#zx) error '%s'",
-                     __func__, host, max_size, strerror(errno));
+                     __func__, host, max_size, qemu_strerror(errno));
         exit(1);
     }
 }
@@ -948,7 +949,7 @@ int sev_kvm_init(ConfidentialGuestSupport *cgs, Error 
**errp)
     sev->sev_fd = open(devname, O_RDWR);
     if (sev->sev_fd < 0) {
         error_setg(errp, "%s: Failed to open %s '%s'", __func__,
-                   devname, strerror(errno));
+                   devname, qemu_strerror(errno));
         g_free(devname);
         goto err;
     }
diff --git a/target/i386/whpx/whpx-accel-ops.c 
b/target/i386/whpx/whpx-accel-ops.c
index e8dc4b3a47..bf16c8e643 100644
--- a/target/i386/whpx/whpx-accel-ops.c
+++ b/target/i386/whpx/whpx-accel-ops.c
@@ -32,7 +32,7 @@ static void *whpx_cpu_thread_fn(void *arg)
 
     r = whpx_init_vcpu(cpu);
     if (r < 0) {
-        fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
+        fprintf(stderr, "whpx_init_vcpu failed: %s\n", qemu_strerror(-r));
         exit(1);
     }
 
-- 
2.39.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]