Skip to contentSkip to conversation

hostmem-file: reject invalid pmem file sizes

PATCH In review Stefan Hajnoczi 7 participants 14 messages Feb 14, 2019 – Mar 8, 2019 Arm STM32

lore
Share & tools
lore
#0 lore raw

v3 cover hostmem-file: reject invalid pmem file sizes

v3:
* Added Patch 1 by Igor [Wei]
* Moved pmem size check after mem_path check [Wei]

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

Igor Mammedov (1):
hostmem: simplify ifdef-s in file_backend_memory_alloc()

Stefan Hajnoczi (1):
hostmem-file: reject invalid pmem file sizes

 include/qemu/osdep.h    | 13 ++++++++++
 backends/hostmem-file.c | 32 +++++++++++++++++++++----
 util/oslib-posix.c      | 53 +++++++++++++++++++++++++++++++++++++++++
 util/oslib-win32.c      |  5 ++++
 4 files changed, 98 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/2 hostmem: simplify ifdef-s in file_backend_memory_alloc()

From: Igor Mammedov <[email protected]>

cleanup file_backend_memory_alloc() by using one CONFIG_POSIX ifdef instead of several ones within the function to make it simpler to follow.

Message-Id: <[email protected]>

---
 backends/hostmem-file.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
backends/hostmem-file.c +4 -5
index ba601ce940..ce54788048 100644
@@ -41,10 +41,12 @@ struct HostMemoryBackendFile {
 static void
 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
+#ifndef CONFIG_POSIX
+    error_setg(errp, "backend '%s' not supported on this host",
+               object_get_typename(OBJECT(backend)));
+#else
     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
-#ifdef CONFIG_POSIX
     gchar *name;
-#endif
 
     if (!backend->size) {
         error_setg(errp, "can't create backend with size 0");
@@ -54,9 +56,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         error_setg(errp, "mem-path property not set");
         return;
     }
-#ifndef CONFIG_POSIX
-    error_setg(errp, "-mem-path not supported on this host");
-#else
     backend->force_prealloc = mem_prealloc;
     name = host_memory_backend_get_name(backend);
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
Signature · 1 line
2.20.1
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] hostmem: simplify ifdef-s in file_backend_memory_alloc()

Maybe change the title

s/hotmem/hostmem-file/

would be more proper.

I missed this in previous review.

Quoted 45 lines · Stefan Hajnoczi

From: Igor Mammedov <[email protected]>

cleanup file_backend_memory_alloc() by using one CONFIG_POSIX ifdef instead of several ones within the function to make it simpler to follow.

Message-Id: <[email protected]>

---
backends/hostmem-file.c | 9 ++++-----

1 file changed, 4 insertions(+), 5 deletions(-)
backends/hostmem-file.c +0 -0
index ba601ce940..ce54788048 100644
@@ -41,10 +41,12 @@ struct HostMemoryBackendFile {

static void
file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) {

backends/hostmem-file.c +4 -5
+#ifndef CONFIG_POSIX
+    error_setg(errp, "backend '%s' not supported on this host",
+               object_get_typename(OBJECT(backend)));
+#else
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
-#ifdef CONFIG_POSIX
    gchar *name;
-#endif
 
    if (!backend->size) {
        error_setg(errp, "can't create backend with size 0");
@@ -54,9 +56,6 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
        error_setg(errp, "mem-path property not set");
        return;
    }
-#ifndef CONFIG_POSIX
-    error_setg(errp, "-mem-path not supported on this host");
-#else
    backend->force_prealloc = mem_prealloc;
    name = host_memory_backend_get_name(backend);
    memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
Signature · 1 line
2.20.1
Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] hostmem: simplify ifdef-s in file_backend_memory_alloc()

Quoted 7 lines · Wei Yang

Maybe change the title

s/hotmem/hostmem-file/

would be more proper.

I missed this in previous review.

You're right. This can be fixed when merging the patch.

Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] hostmem: simplify ifdef-s in file_backend_memory_alloc()

Quoted 5 lines · On Thu, 14 Feb 2019 16:16:19 +0800 Stefan Hajnoczi <[email protected]> wrote:
Quoted 4 lines · Wei Yang

Maybe change the title

s/hotmem/hostmem-file/

Quoted 7 lines
Quoted 3 lines

would be more proper.

I missed this in previous review.

You're right. This can be fixed when merging the patch.

Stefan

Did this patch fall through cracks?

#5 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] hostmem: simplify ifdef-s in file_backend_memory_alloc()

Quoted 10 lines · Igor Mammedov
Quoted 7 lines · On Thu, 14 Feb 2019 16:16:19 +0800 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines · Wei Yang

Maybe change the title

s/hotmem/hostmem-file/

would be more proper.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I missed this in previous review.

You're right. This can be fixed when merging the patch.

Stefan

Did this patch fall through cracks?

Eduardo has been away. It wasn't urgent but I'd be happy if either you or Eduardo picks up these patches.

Thanks,
Stefan

#6 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] hostmem: simplify ifdef-s in file_backend_memory_alloc()

Quoted 17 lines · Stefan Hajnoczi
Quoted 16 lines · Igor Mammedov
Quoted 12 lines · On Thu, 14 Feb 2019 16:16:19 +0800 Stefan Hajnoczi <[email protected]> wrote:
Quoted 7 lines · Wei Yang

Maybe change the title

s/hotmem/hostmem-file/

would be more proper.

I missed this in previous review.

You're right. This can be fixed when merging the patch.

Stefan

Quoted 8 lines
Quoted 1 line

Did this patch fall through cracks?

Eduardo has been away. It wasn't urgent but I'd be happy if either you or Eduardo picks up these patches.

Thanks,
Stefan

On Igor request, applied to my trivial-patches branch.

Thanks,
Laurent

#7 lore raw

PATCH v3 2/2 hostmem-file: reject invalid pmem file sizes

In reply to Stefan Hajnoczi

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

---
 include/qemu/osdep.h    | 13 ++++++++++
 backends/hostmem-file.c | 23 ++++++++++++++++++
 util/oslib-posix.c      | 53 +++++++++++++++++++++++++++++++++++++++++
 util/oslib-win32.c      |  5 ++++
 4 files changed, 94 insertions(+)
include/qemu/osdep.h +13 -0
index 840af09cb0..303d315c5d 100644
@@ -570,6 +570,19 @@ void qemu_set_tty_echo(int fd, bool echo);
 void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
                      Error **errp);
 
+/**
+ * qemu_get_pmem_size:
+ * @filename: path to a pmem file
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Determine the size of a persistent memory file.  Besides supporting files on
+ * DAX file systems, this function also supports Linux devdax character
+ * devices.
+ *
+ * Returns: the size or 0 on failure
+ */
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp);
+
 /**
  * qemu_get_pid_name:
  * @pid: pid of a process
backends/hostmem-file.c +23 -0
index ce54788048..37ac6445d2 100644
@@ -56,6 +56,29 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         error_setg(errp, "mem-path property not set");
         return;
     }
+
+    /*
+     * Verify pmem file size since starting a guest with an incorrect size
+     * leads to confusing failures inside the guest.
+     */
+    if (fb->is_pmem) {
+        Error *local_err = NULL;
+        uint64_t size;
+
+        size = qemu_get_pmem_size(fb->mem_path, &local_err);
+        if (!size) {
+            error_propagate(errp, local_err);
+            return;
+        }
+
+        if (backend->size > size) {
+            error_setg(errp, "size property %" PRIu64 " is larger than "
+                       "pmem file \"%s\" size %" PRIu64, backend->size,
+                       fb->mem_path, size);
+            return;
+        }
+    }
+
     backend->force_prealloc = mem_prealloc;
     name = host_memory_backend_get_name(backend);
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
util/oslib-posix.c +53 -0

59 lines not on this page · raw message

util/oslib-win32.c +5 -0
index b4c17f5dfa..bd633afab6 100644
@@ -560,6 +560,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
     }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    error_setg(errp, "pmem support not available");
+    return 0;
+}
 
 char *qemu_get_pid_name(pid_t pid)
 {
Signature · 1 line
2.20.1
#8 lore raw
Quoted 147 lines · Stefan Hajnoczi

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

---

include/qemu/osdep.h    | 13 ++++++++++
backends/hostmem-file.c | 23 ++++++++++++++++++
util/oslib-posix.c      | 53 +++++++++++++++++++++++++++++++++++++++++
util/oslib-win32.c      |  5 ++++
4 files changed, 94 insertions(+)
include/qemu/osdep.h +13 -0
index 840af09cb0..303d315c5d 100644
@@ -570,6 +570,19 @@ void qemu_set_tty_echo(int fd, bool echo);
 void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
                     Error **errp);
 
+/**
+ * qemu_get_pmem_size:
+ * @filename: path to a pmem file
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Determine the size of a persistent memory file.  Besides supporting files on
+ * DAX file systems, this function also supports Linux devdax character
+ * devices.
+ *
+ * Returns: the size or 0 on failure
+ */
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp);
+
 /**
 * qemu_get_pid_name:
 * @pid: pid of a process
backends/hostmem-file.c +23 -0
index ce54788048..37ac6445d2 100644
@@ -56,6 +56,29 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
        error_setg(errp, "mem-path property not set");
        return;
    }
+
+    /*
+     * Verify pmem file size since starting a guest with an incorrect size
+     * leads to confusing failures inside the guest.
+     */
+    if (fb->is_pmem) {
+        Error *local_err = NULL;
+        uint64_t size;
+
+        size = qemu_get_pmem_size(fb->mem_path, &local_err);
+        if (!size) {
+            error_propagate(errp, local_err);
+            return;
+        }
+
+        if (backend->size > size) {
+            error_setg(errp, "size property %" PRIu64 " is larger than "
+                       "pmem file \"%s\" size %" PRIu64, backend->size,
+                       fb->mem_path, size);
+            return;
+        }
+    }
+
    backend->force_prealloc = mem_prealloc;
    name = host_memory_backend_get_name(backend);
    memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
util/oslib-posix.c +53 -0
index 37c5854b9c..10d90d1783 100644
@@ -500,6 +500,59 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
    }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    struct stat st;
+
+    if (stat(filename, &st) < 0) {
+        error_setg(errp, "unable to stat pmem file \"%s\"", filename);
+        return 0;
+    }
+
+#if defined(__linux__)
+    /* Special handling for devdax character devices */
+    if (S_ISCHR(st.st_mode)) {
+        char *subsystem_path = NULL;
+        char *subsystem = NULL;
+        char *size_path = NULL;
+        char *size_str = NULL;
+        uint64_t ret = 0;
+
+        subsystem_path = g_strdup_printf("/sys/dev/char/%d:%d/subsystem",
+                                         major(st.st_rdev), minor(st.st_rdev));
+        subsystem = g_file_read_link(subsystem_path, NULL);
+        if (!subsystem) {
+            error_setg(errp, "unable to read subsystem for pmem file \"%s\"",
+                       filename);
+            goto devdax_err;
+        }
+
+        if (!g_str_has_suffix(subsystem, "/dax")) {
+            error_setg(errp, "pmem file \"%s\" is not a dax device", filename);
+            goto devdax_err;
+        }
+
+        size_path = g_strdup_printf("/sys/dev/char/%d:%d/size",
+                                    major(st.st_rdev), minor(st.st_rdev));
+        if (!g_file_get_contents(size_path, &size_str, NULL, NULL)) {
+            error_setg(errp, "unable to read size for pmem file \"%s\"",
+                       size_path);
+            goto devdax_err;
+        }
+
+        ret = g_ascii_strtoull(size_str, NULL, 0);
+
+devdax_err:
+        g_free(size_str);
+        g_free(size_path);
+        g_free(subsystem);
+        g_free(subsystem_path);
+        return ret;
+    }
+#endif /* defined(__linux__) */
+
+    return st.st_size;
+}
 
 char *qemu_get_pid_name(pid_t pid)
 {
util/oslib-win32.c +0 -0
index b4c17f5dfa..bd633afab6 100644

util/oslib-win32.c void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, · line 567

Quoted 8 lines
util/oslib-win32.c +5 -0
@@ -560,6 +560,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
    }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    error_setg(errp, "pmem support not available");
+    return 0;
+}

After the cleanup from Igor, qemu_get_pmem_size() would be invoked only on POSIX system.

Is it still necessary to define this on win32?

Quoted 5 lines
util/oslib-win32.c +0 -0
 char *qemu_get_pid_name(pid_t pid)
 {
Signature · 1 line
2.20.1
Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw
Quoted 18 lines · Wei Yang

util/oslib-win32.c void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, · line 567

Quoted 13 lines · Stefan Hajnoczi
util/oslib-win32.c +5 -0
index b4c17f5dfa..bd633afab6 100644
@@ -560,6 +560,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
    }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    error_setg(errp, "pmem support not available");
+    return 0;
+}

After the cleanup from Igor, qemu_get_pmem_size() would be invoked only on POSIX system.

Quoted 1 line

Is it still necessary to define this on win32?

Either way is possible. I'd like to leave it like this unless there are strong feelings against this.

Stefan

Reviewed-by
#10 lore raw
Quoted 19 lines · Stefan Hajnoczi
Quoted 18 lines · Wei Yang

util/oslib-win32.c void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, · line 567

Quoted 13 lines · Stefan Hajnoczi
util/oslib-win32.c +5 -0
index b4c17f5dfa..bd633afab6 100644
@@ -560,6 +560,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
    }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    error_setg(errp, "pmem support not available");
+    return 0;
+}

After the cleanup from Igor, qemu_get_pmem_size() would be invoked only on POSIX system.

Quoted 5 lines
Quoted 1 line

Is it still necessary to define this on win32?

Either way is possible. I'd like to leave it like this unless there are strong feelings against this.

Well, I am fine with this.

Quoted 1 line

Stefan

Signature · 2 lines
Wei Yang
Help you, Help me
Reviewed-by on v3 2/2
#11 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · On Thu, 14 Feb 2019 11:10:04 +0800 Stefan Hajnoczi <[email protected]> wrote:

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

Quoted 6 lines
Quoted 145 lines
---
 include/qemu/osdep.h    | 13 ++++++++++
 backends/hostmem-file.c | 23 ++++++++++++++++++
 util/oslib-posix.c      | 53 +++++++++++++++++++++++++++++++++++++++++
 util/oslib-win32.c      |  5 ++++
 4 files changed, 94 insertions(+)
include/qemu/osdep.h +13 -0
index 840af09cb0..303d315c5d 100644
@@ -570,6 +570,19 @@ void qemu_set_tty_echo(int fd, bool echo);
 void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
                      Error **errp);
 
+/**
+ * qemu_get_pmem_size:
+ * @filename: path to a pmem file
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Determine the size of a persistent memory file.  Besides supporting files on
+ * DAX file systems, this function also supports Linux devdax character
+ * devices.
+ *
+ * Returns: the size or 0 on failure
+ */
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp);
+
 /**
  * qemu_get_pid_name:
  * @pid: pid of a process
backends/hostmem-file.c +23 -0
index ce54788048..37ac6445d2 100644
@@ -56,6 +56,29 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
         error_setg(errp, "mem-path property not set");
         return;
     }
+
+    /*
+     * Verify pmem file size since starting a guest with an incorrect size
+     * leads to confusing failures inside the guest.
+     */
+    if (fb->is_pmem) {
+        Error *local_err = NULL;
+        uint64_t size;
+
+        size = qemu_get_pmem_size(fb->mem_path, &local_err);
+        if (!size) {
+            error_propagate(errp, local_err);
+            return;
+        }
+
+        if (backend->size > size) {
+            error_setg(errp, "size property %" PRIu64 " is larger than "
+                       "pmem file \"%s\" size %" PRIu64, backend->size,
+                       fb->mem_path, size);
+            return;
+        }
+    }
+
     backend->force_prealloc = mem_prealloc;
     name = host_memory_backend_get_name(backend);
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
util/oslib-posix.c +53 -0
index 37c5854b9c..10d90d1783 100644
@@ -500,6 +500,59 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
     }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    struct stat st;
+
+    if (stat(filename, &st) < 0) {
+        error_setg(errp, "unable to stat pmem file \"%s\"", filename);
+        return 0;
+    }
+
+#if defined(__linux__)
+    /* Special handling for devdax character devices */
+    if (S_ISCHR(st.st_mode)) {
+        char *subsystem_path = NULL;
+        char *subsystem = NULL;
+        char *size_path = NULL;
+        char *size_str = NULL;
+        uint64_t ret = 0;
+
+        subsystem_path = g_strdup_printf("/sys/dev/char/%d:%d/subsystem",
+                                         major(st.st_rdev), minor(st.st_rdev));
+        subsystem = g_file_read_link(subsystem_path, NULL);
+        if (!subsystem) {
+            error_setg(errp, "unable to read subsystem for pmem file \"%s\"",
+                       filename);
+            goto devdax_err;
+        }
+
+        if (!g_str_has_suffix(subsystem, "/dax")) {
+            error_setg(errp, "pmem file \"%s\" is not a dax device", filename);
+            goto devdax_err;
+        }
+
+        size_path = g_strdup_printf("/sys/dev/char/%d:%d/size",
+                                    major(st.st_rdev), minor(st.st_rdev));
+        if (!g_file_get_contents(size_path, &size_str, NULL, NULL)) {
+            error_setg(errp, "unable to read size for pmem file \"%s\"",
+                       size_path);
+            goto devdax_err;
+        }
+
+        ret = g_ascii_strtoull(size_str, NULL, 0);
+
+devdax_err:
+        g_free(size_str);
+        g_free(size_path);
+        g_free(subsystem);
+        g_free(subsystem_path);
+        return ret;
+    }
+#endif /* defined(__linux__) */
+
+    return st.st_size;
+}
 
 char *qemu_get_pid_name(pid_t pid)
 {
util/oslib-win32.c +5 -0
index b4c17f5dfa..bd633afab6 100644
@@ -560,6 +560,11 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
     }
 }
 
+uint64_t qemu_get_pmem_size(const char *filename, Error **errp)
+{
+    error_setg(errp, "pmem support not available");
+    return 0;
+}
 
 char *qemu_get_pid_name(pid_t pid)
 {
#12 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

Quoted 6 lines

I've just noticed this wasn't picked up on my previous pull request. I'm queueing this on machine-next, now.

Signature · 1 line
Eduardo
Reviewed-by on the v3 series
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 20 lines

v3:
* Added Patch 1 by Igor [Wei]
* Moved pmem size check after mem_path check [Wei]

Guests started with NVDIMMs larger than the underlying host file produce confusing errors inside the guest. This happens because the guest accesses pages beyond the end of the file.

Check the pmem file size on startup and print a clear error message if the size is invalid.

Igor Mammedov (1):
hostmem: simplify ifdef-s in file_backend_memory_alloc()

Stefan Hajnoczi (1):
hostmem-file: reject invalid pmem file sizes

Quoted 8 lines
 include/qemu/osdep.h    | 13 ++++++++++
 backends/hostmem-file.c | 32 +++++++++++++++++++++----
 util/oslib-posix.c      | 53 +++++++++++++++++++++++++++++++++++++++++
 util/oslib-win32.c      |  5 ++++
 4 files changed, 98 insertions(+), 5 deletions(-)

--
2.20.1

Both the patches look good to me.