[v2,03/86] machine: alias -mem-path and -mem-prealloc into memory-foo backend
diff mbox series

Message ID 1579100861-73692-4-git-send-email-imammedo@redhat.com
State New
Headers show
Series
  • refactor main RAM allocation to use hostmem backend
Related show

Commit Message

Igor Mammedov Jan. 15, 2020, 3:06 p.m. UTC
Allow a machine to opt in for hostmem backend based initial
RAM even if user used old -mem-path/prealloc options by providing
  MachineClass::default_ram_id
Follow up patches will incrementally convert machines to new API,
by dropping memory_region_allocate_system_memory() and setting
default_ram_id that board used to use before conversion to keep
migration stream the same.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
CC: ehabkost@redhat.com
CC: pbonzini@redhat.com
---
 include/hw/boards.h      |  5 +++++
 include/sysemu/hostmem.h | 16 ++++++++++++++++
 backends/hostmem-file.c  |  7 -------
 backends/hostmem-ram.c   |  2 --
 vl.c                     | 25 +++++++++++++++++++++++++
 5 files changed, 46 insertions(+), 9 deletions(-)

Comments

Philippe Mathieu-Daudé Jan. 15, 2020, 6:54 p.m. UTC | #1
On 1/15/20 4:06 PM, Igor Mammedov wrote:
> Allow a machine to opt in for hostmem backend based initial
> RAM even if user used old -mem-path/prealloc options by providing
>    MachineClass::default_ram_id
> Follow up patches will incrementally convert machines to new API,
> by dropping memory_region_allocate_system_memory() and setting
> default_ram_id that board used to use before conversion to keep
> migration stream the same.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
> CC: ehabkost@redhat.com
> CC: pbonzini@redhat.com
> ---
>   include/hw/boards.h      |  5 +++++
>   include/sysemu/hostmem.h | 16 ++++++++++++++++
>   backends/hostmem-file.c  |  7 -------
>   backends/hostmem-ram.c   |  2 --
>   vl.c                     | 25 +++++++++++++++++++++++++
>   5 files changed, 46 insertions(+), 9 deletions(-)
> 
> diff --git a/include/hw/boards.h b/include/hw/boards.h
> index 6aa01b8..e3ea8b0 100644
> --- a/include/hw/boards.h
> +++ b/include/hw/boards.h
> @@ -170,6 +170,10 @@ typedef struct {
>    *    false is returned, an error must be set to show the reason of
>    *    the rejection.  If the hook is not provided, all hotplug will be
>    *    allowed.
> + * @default_ram_id:
> + *    Specifies inital RAM MemoryRegion name to be used for default backend
> + *    creation if user explicitly hasn't specified backend with "ram-memdev"
> + *    property.
>    */
>   struct MachineClass {
>       /*< private >*/
> @@ -226,6 +230,7 @@ struct MachineClass {
>       bool nvdimm_supported;
>       bool numa_mem_supported;
>       bool auto_enable_numa;
> +    const char *default_ram_id;
>   
>       HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
>                                              DeviceState *dev);
> diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
> index 4dbdadd..5db0d66 100644
> --- a/include/sysemu/hostmem.h
> +++ b/include/sysemu/hostmem.h
> @@ -27,6 +27,22 @@
>   #define MEMORY_BACKEND_CLASS(klass) \
>       OBJECT_CLASS_CHECK(HostMemoryBackendClass, (klass), TYPE_MEMORY_BACKEND)
>   
> +/* hostmem-ram.c */
> +/**
> + * @TYPE_MEMORY_BACKEND_RAM:
> + * name of backend that uses mmap on the anonymous RAM
> + */
> +
> +#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
> +
> +/* hostmem-file.c */
> +/**
> + * @TYPE_MEMORY_BACKEND_FILE:
> + * name of backend that uses mmap on a file descriptor
> + */
> +#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
> +
> +typedef struct HostMemoryBackend HostMemoryBackend;
>   typedef struct HostMemoryBackendClass HostMemoryBackendClass;
>   
>   /**
> diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
> index be64020..cb319a9 100644
> --- a/backends/hostmem-file.c
> +++ b/backends/hostmem-file.c
> @@ -18,13 +18,6 @@
>   #include "sysemu/sysemu.h"
>   #include "qom/object_interfaces.h"
>   
> -/* hostmem-file.c */
> -/**
> - * @TYPE_MEMORY_BACKEND_FILE:
> - * name of backend that uses mmap on a file descriptor
> - */
> -#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
> -
>   #define MEMORY_BACKEND_FILE(obj) \
>       OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
>   
> diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
> index 6aab8d3..5cc53e7 100644
> --- a/backends/hostmem-ram.c
> +++ b/backends/hostmem-ram.c
> @@ -16,8 +16,6 @@
>   #include "qemu/module.h"
>   #include "qom/object_interfaces.h"
>   
> -#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
> -
>   static void
>   ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
>   {
> diff --git a/vl.c b/vl.c
> index 7514012..d36a857 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -75,6 +75,7 @@ int main(int argc, char **argv)
>   #include "ui/input.h"
>   #include "sysemu/sysemu.h"
>   #include "sysemu/numa.h"
> +#include "sysemu/hostmem.h"
>   #include "exec/gdbstub.h"
>   #include "qemu/timer.h"
>   #include "chardev/char.h"
> @@ -2820,6 +2821,25 @@ static void configure_accelerators(const char *progname)
>       }
>   }
>   
> +static void create_default_memdev(MachineState *ms, const char *path,
> +                                  bool prealloc)
> +{
> +    Object *obj;
> +    MachineClass *mc = MACHINE_GET_CLASS(ms);
> +
> +    obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
> +    if (path) {
> +        object_property_set_str(obj, path, "mem-path", &error_fatal);
> +    }
> +    object_property_set_bool(obj, prealloc, "prealloc", &error_fatal);
> +    object_property_set_int(obj, ms->ram_size, "size", &error_fatal);
> +    object_property_add_child(object_get_objects_root(), mc->default_ram_id,
> +                              obj, &error_fatal);
> +    user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
> +    object_unref(obj);
> +    object_property_set_link(OBJECT(ms), obj, "ram-memdev", &error_fatal);
> +}
> +
>   int main(int argc, char **argv, char **envp)
>   {
>       int i;
> @@ -4283,6 +4303,11 @@ int main(int argc, char **argv, char **envp)
>       }
>       parse_numa_opts(current_machine);
>   
> +    if (!current_machine->ram_memdev &&
> +         machine_class->default_ram_size &&
> +         machine_class->default_ram_id) {

Do we really need for its presence?

Can we instead set it to "ram" if NULL?

> +        create_default_memdev(current_machine, mem_path, mem_prealloc);
> +    }
>       /* do monitor/qmp handling at preconfig state if requested */
>       main_loop();
>   
>
Igor Mammedov Jan. 16, 2020, 12:20 p.m. UTC | #2
On Wed, 15 Jan 2020 19:54:18 +0100
Philippe Mathieu-Daudé <philmd@redhat.com> wrote:

> On 1/15/20 4:06 PM, Igor Mammedov wrote:
> > Allow a machine to opt in for hostmem backend based initial
> > RAM even if user used old -mem-path/prealloc options by providing
> >    MachineClass::default_ram_id
> > Follow up patches will incrementally convert machines to new API,
> > by dropping memory_region_allocate_system_memory() and setting
> > default_ram_id that board used to use before conversion to keep
> > migration stream the same.
> > 
> > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > ---
> > CC: ehabkost@redhat.com
> > CC: pbonzini@redhat.com
> > ---
> >   include/hw/boards.h      |  5 +++++
> >   include/sysemu/hostmem.h | 16 ++++++++++++++++
> >   backends/hostmem-file.c  |  7 -------
> >   backends/hostmem-ram.c   |  2 --
> >   vl.c                     | 25 +++++++++++++++++++++++++
> >   5 files changed, 46 insertions(+), 9 deletions(-)
> > 
> > diff --git a/include/hw/boards.h b/include/hw/boards.h
> > index 6aa01b8..e3ea8b0 100644
> > --- a/include/hw/boards.h
> > +++ b/include/hw/boards.h
> > @@ -170,6 +170,10 @@ typedef struct {
> >    *    false is returned, an error must be set to show the reason of
> >    *    the rejection.  If the hook is not provided, all hotplug will be
> >    *    allowed.
> > + * @default_ram_id:
> > + *    Specifies inital RAM MemoryRegion name to be used for default backend
> > + *    creation if user explicitly hasn't specified backend with "ram-memdev"
> > + *    property.
> >    */
> >   struct MachineClass {
> >       /*< private >*/
> > @@ -226,6 +230,7 @@ struct MachineClass {
> >       bool nvdimm_supported;
> >       bool numa_mem_supported;
> >       bool auto_enable_numa;
> > +    const char *default_ram_id;
> >   
> >       HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
> >                                              DeviceState *dev);
> > diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
> > index 4dbdadd..5db0d66 100644
> > --- a/include/sysemu/hostmem.h
> > +++ b/include/sysemu/hostmem.h
> > @@ -27,6 +27,22 @@
> >   #define MEMORY_BACKEND_CLASS(klass) \
> >       OBJECT_CLASS_CHECK(HostMemoryBackendClass, (klass), TYPE_MEMORY_BACKEND)
> >   
> > +/* hostmem-ram.c */
> > +/**
> > + * @TYPE_MEMORY_BACKEND_RAM:
> > + * name of backend that uses mmap on the anonymous RAM
> > + */
> > +
> > +#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
> > +
> > +/* hostmem-file.c */
> > +/**
> > + * @TYPE_MEMORY_BACKEND_FILE:
> > + * name of backend that uses mmap on a file descriptor
> > + */
> > +#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
> > +
> > +typedef struct HostMemoryBackend HostMemoryBackend;
> >   typedef struct HostMemoryBackendClass HostMemoryBackendClass;
> >   
> >   /**
> > diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
> > index be64020..cb319a9 100644
> > --- a/backends/hostmem-file.c
> > +++ b/backends/hostmem-file.c
> > @@ -18,13 +18,6 @@
> >   #include "sysemu/sysemu.h"
> >   #include "qom/object_interfaces.h"
> >   
> > -/* hostmem-file.c */
> > -/**
> > - * @TYPE_MEMORY_BACKEND_FILE:
> > - * name of backend that uses mmap on a file descriptor
> > - */
> > -#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
> > -
> >   #define MEMORY_BACKEND_FILE(obj) \
> >       OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
> >   
> > diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
> > index 6aab8d3..5cc53e7 100644
> > --- a/backends/hostmem-ram.c
> > +++ b/backends/hostmem-ram.c
> > @@ -16,8 +16,6 @@
> >   #include "qemu/module.h"
> >   #include "qom/object_interfaces.h"
> >   
> > -#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
> > -
> >   static void
> >   ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
> >   {
> > diff --git a/vl.c b/vl.c
> > index 7514012..d36a857 100644
> > --- a/vl.c
> > +++ b/vl.c
> > @@ -75,6 +75,7 @@ int main(int argc, char **argv)
> >   #include "ui/input.h"
> >   #include "sysemu/sysemu.h"
> >   #include "sysemu/numa.h"
> > +#include "sysemu/hostmem.h"
> >   #include "exec/gdbstub.h"
> >   #include "qemu/timer.h"
> >   #include "chardev/char.h"
> > @@ -2820,6 +2821,25 @@ static void configure_accelerators(const char *progname)
> >       }
> >   }
> >   
> > +static void create_default_memdev(MachineState *ms, const char *path,
> > +                                  bool prealloc)
> > +{
> > +    Object *obj;
> > +    MachineClass *mc = MACHINE_GET_CLASS(ms);
> > +
> > +    obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
> > +    if (path) {
> > +        object_property_set_str(obj, path, "mem-path", &error_fatal);
> > +    }
> > +    object_property_set_bool(obj, prealloc, "prealloc", &error_fatal);
> > +    object_property_set_int(obj, ms->ram_size, "size", &error_fatal);
> > +    object_property_add_child(object_get_objects_root(), mc->default_ram_id,
> > +                              obj, &error_fatal);
> > +    user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
> > +    object_unref(obj);
> > +    object_property_set_link(OBJECT(ms), obj, "ram-memdev", &error_fatal);
> > +}
> > +
> >   int main(int argc, char **argv, char **envp)
> >   {
> >       int i;
> > @@ -4283,6 +4303,11 @@ int main(int argc, char **argv, char **envp)
> >       }
> >       parse_numa_opts(current_machine);
> >   
> > +    if (!current_machine->ram_memdev &&
> > +         machine_class->default_ram_size &&
> > +         machine_class->default_ram_id) {  
> 
> Do we really need for its presence?
> 
> Can we instead set it to "ram" if NULL?
it helps with incremental conversion,
once all boards are converted, it's cleaned up (see [76/86]).

> 
> > +        create_default_memdev(current_machine, mem_path, mem_prealloc);
> > +    }
> >       /* do monitor/qmp handling at preconfig state if requested */
> >       main_loop();
> >   
> >   
> 
>

Patch
diff mbox series

diff --git a/include/hw/boards.h b/include/hw/boards.h
index 6aa01b8..e3ea8b0 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -170,6 +170,10 @@  typedef struct {
  *    false is returned, an error must be set to show the reason of
  *    the rejection.  If the hook is not provided, all hotplug will be
  *    allowed.
+ * @default_ram_id:
+ *    Specifies inital RAM MemoryRegion name to be used for default backend
+ *    creation if user explicitly hasn't specified backend with "ram-memdev"
+ *    property.
  */
 struct MachineClass {
     /*< private >*/
@@ -226,6 +230,7 @@  struct MachineClass {
     bool nvdimm_supported;
     bool numa_mem_supported;
     bool auto_enable_numa;
+    const char *default_ram_id;
 
     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
                                            DeviceState *dev);
diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
index 4dbdadd..5db0d66 100644
--- a/include/sysemu/hostmem.h
+++ b/include/sysemu/hostmem.h
@@ -27,6 +27,22 @@ 
 #define MEMORY_BACKEND_CLASS(klass) \
     OBJECT_CLASS_CHECK(HostMemoryBackendClass, (klass), TYPE_MEMORY_BACKEND)
 
+/* hostmem-ram.c */
+/**
+ * @TYPE_MEMORY_BACKEND_RAM:
+ * name of backend that uses mmap on the anonymous RAM
+ */
+
+#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
+
+/* hostmem-file.c */
+/**
+ * @TYPE_MEMORY_BACKEND_FILE:
+ * name of backend that uses mmap on a file descriptor
+ */
+#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
+
+typedef struct HostMemoryBackend HostMemoryBackend;
 typedef struct HostMemoryBackendClass HostMemoryBackendClass;
 
 /**
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index be64020..cb319a9 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -18,13 +18,6 @@ 
 #include "sysemu/sysemu.h"
 #include "qom/object_interfaces.h"
 
-/* hostmem-file.c */
-/**
- * @TYPE_MEMORY_BACKEND_FILE:
- * name of backend that uses mmap on a file descriptor
- */
-#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
-
 #define MEMORY_BACKEND_FILE(obj) \
     OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
 
diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
index 6aab8d3..5cc53e7 100644
--- a/backends/hostmem-ram.c
+++ b/backends/hostmem-ram.c
@@ -16,8 +16,6 @@ 
 #include "qemu/module.h"
 #include "qom/object_interfaces.h"
 
-#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
-
 static void
 ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
diff --git a/vl.c b/vl.c
index 7514012..d36a857 100644
--- a/vl.c
+++ b/vl.c
@@ -75,6 +75,7 @@  int main(int argc, char **argv)
 #include "ui/input.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/numa.h"
+#include "sysemu/hostmem.h"
 #include "exec/gdbstub.h"
 #include "qemu/timer.h"
 #include "chardev/char.h"
@@ -2820,6 +2821,25 @@  static void configure_accelerators(const char *progname)
     }
 }
 
+static void create_default_memdev(MachineState *ms, const char *path,
+                                  bool prealloc)
+{
+    Object *obj;
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+
+    obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
+    if (path) {
+        object_property_set_str(obj, path, "mem-path", &error_fatal);
+    }
+    object_property_set_bool(obj, prealloc, "prealloc", &error_fatal);
+    object_property_set_int(obj, ms->ram_size, "size", &error_fatal);
+    object_property_add_child(object_get_objects_root(), mc->default_ram_id,
+                              obj, &error_fatal);
+    user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
+    object_unref(obj);
+    object_property_set_link(OBJECT(ms), obj, "ram-memdev", &error_fatal);
+}
+
 int main(int argc, char **argv, char **envp)
 {
     int i;
@@ -4283,6 +4303,11 @@  int main(int argc, char **argv, char **envp)
     }
     parse_numa_opts(current_machine);
 
+    if (!current_machine->ram_memdev &&
+         machine_class->default_ram_size &&
+         machine_class->default_ram_id) {
+        create_default_memdev(current_machine, mem_path, mem_prealloc);
+    }
     /* do monitor/qmp handling at preconfig state if requested */
     main_loop();