diff mbox

mlock: fix bug when mlockall called before mbind

Message ID 1407928917-16220-1-git-send-email-zhang.zhanghailiang@huawei.com
State New
Headers show

Commit Message

Zhanghailiang Aug. 13, 2014, 11:21 a.m. UTC
If we configure qemu with realtime-mlock-on and memory-node-bind at the same time,
Qemu will fail to start, and mbind() fails with message "Input/output error".

From man page:
int mbind(void *addr, unsigned long len, int mode,
                 unsigned long *nodemask, unsigned long maxnode,
                 unsigned flags);
The *MPOL_BIND* mode specifies a strict policy that restricts memory allocation
to the nodes specified in nodemask.
If *MPOL_MF_STRICT* is passed in flags and policy is not MPOL_DEFAULT(In qemu
here is MPOL_BIND), then the call will fail with the error EIO if the existing
pages in  the memory range don't follow the policy.

The memory locked ahead by mlockall can not guarantee to follow the policy above,
And if that happens, it will result in an EIO error.

So we should call mlock after mbind, here we adjust the place where called mlock,
Move it to function pc_memory_init.

Signed-off-by: xiexiangyou <xiexiangyou@huawei.com>
Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
---
 hw/i386/pc.c            |  8 ++++++++
 include/sysemu/sysemu.h |  1 +
 vl.c                    | 10 +---------
 3 files changed, 10 insertions(+), 9 deletions(-)
diff mbox

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 9e58982..08a03c2 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -1321,6 +1321,14 @@  FWCfgState *pc_memory_init(MachineState *machine,
     for (i = 0; i < nb_option_roms; i++) {
         rom_add_option(option_rom[i].name, option_rom[i].bootindex);
     }
+
+    if (enable_mlock) {
+        if (os_mlock() < 0) {
+            error_report("qemu: locking memory failed\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+
     guest_info->fw_cfg = fw_cfg;
     return fw_cfg;
 }
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index d8539fd..b61e78f 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -136,6 +136,7 @@  extern uint8_t qemu_extra_params_fw[2];
 extern QEMUClockType rtc_clock;
 extern const char *mem_path;
 extern int mem_prealloc;
+extern bool enable_mlock;
 
 #define MAX_NODES 128
 
diff --git a/vl.c b/vl.c
index a8029d5..9a19d97 100644
--- a/vl.c
+++ b/vl.c
@@ -134,6 +134,7 @@  const char* keyboard_layout = NULL;
 ram_addr_t ram_size;
 const char *mem_path = NULL;
 int mem_prealloc = 0; /* force preallocation of physical target memory */
+bool enable_mlock = false;
 int nb_nics;
 NICInfo nd_table[MAX_NICS];
 int autostart;
@@ -1419,16 +1420,7 @@  static void smp_parse(QemuOpts *opts)
 
 static void configure_realtime(QemuOpts *opts)
 {
-    bool enable_mlock;
-
     enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
-
-    if (enable_mlock) {
-        if (os_mlock() < 0) {
-            fprintf(stderr, "qemu: locking memory failed\n");
-            exit(1);
-        }
-    }
 }