Patchwork add -machine mem_merge=on|off option

login
register
mail settings
Submitter Luiz Capitulino
Date June 27, 2012, 4:49 p.m.
Message ID <20120627134924.02c4f5b2@doriath.home>
Download mbox | patch
Permalink /patch/167730/
State New
Headers show

Comments

Luiz Capitulino - June 27, 2012, 4:49 p.m.
Allows to disable memory merge support (KSM on Linux), which is enabled
by default otherwise.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 exec.c          | 19 ++++++++++++++++---
 qemu-config.c   |  4 ++++
 qemu-options.hx |  5 ++++-
 3 files changed, 24 insertions(+), 4 deletions(-)
Jan Kiszka - June 28, 2012, 9:25 a.m.
On 2012-06-27 18:49, Luiz Capitulino wrote:
> Allows to disable memory merge support (KSM on Linux), which is enabled
> by default otherwise.
> 
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  exec.c          | 19 ++++++++++++++++---
>  qemu-config.c   |  4 ++++
>  qemu-options.hx |  5 ++++-
>  3 files changed, 24 insertions(+), 4 deletions(-)
> 
> diff --git a/exec.c b/exec.c
> index 8244d54..24dd154 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -2499,6 +2499,19 @@ void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
>      }
>  }
>  
> +static int madvise_mergeable(void *addr, size_t len)
> +{
> +    QemuOpts *opts;
> +
> +    opts = qemu_opts_find(qemu_find_opts("machine"), 0);
> +    if (opts && !qemu_opt_get_bool(opts, "mem_merge", true)) {
> +        /* disabled by the user */
> +        return 0;
> +    }
> +
> +    return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
> +}
> +
>  ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
>                                     MemoryRegion *mr)
>  {
> @@ -2518,7 +2531,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
>              new_block->host = file_ram_alloc(new_block, size, mem_path);
>              if (!new_block->host) {
>                  new_block->host = qemu_vmalloc(size);
> -                qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
> +                madvise_mergeable(new_block->host, size);
>              }
>  #else
>              fprintf(stderr, "-mem-path option unsupported\n");
> @@ -2545,7 +2558,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
>                  new_block->host = qemu_vmalloc(size);
>              }
>  #endif
> -            qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
> +            madvise_mergeable(new_block->host, size);
>          }
>      }
>      new_block->length = size;
> @@ -2672,7 +2685,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
>                              length, addr);
>                      exit(1);
>                  }
> -                qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
> +                madvise_mergeable(vaddr, length);
>              }
>              return;
>          }
> diff --git a/qemu-config.c b/qemu-config.c
> index 5c3296b..e47612e 100644
> --- a/qemu-config.c
> +++ b/qemu-config.c
> @@ -595,6 +595,10 @@ static QemuOptsList qemu_machine_opts = {
>              .name = "dt_compatible",
>              .type = QEMU_OPT_STRING,
>              .help = "Overrides the \"compatible\" property of the dt root node",
> +        }, {
> +            .name = "mem_merge",
> +            .type = QEMU_OPT_BOOL,
> +            .help = "enable/disable memory merge support",
>          },
>          { /* End of list */ }
>      },
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 8b66264..e0e7686 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -33,7 +33,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
>      "                property accel=accel1[:accel2[:...]] selects accelerator\n"
>      "                supported accelerators are kvm, xen, tcg (default: tcg)\n"
>      "                kernel_irqchip=on|off controls accelerated irqchip support\n"
> -    "                kvm_shadow_mem=size of KVM shadow MMU\n",
> +    "                kvm_shadow_mem=size of KVM shadow MMU\n"
> +    "                mem_merge=on|off controls memory merge support (default: on)\n",
>      QEMU_ARCH_ALL)
>  STEXI
>  @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
> @@ -50,6 +51,8 @@ to initialize.
>  Enables in-kernel irqchip support for the chosen accelerator when available.
>  @item kvm_shadow_mem=size
>  Defines the size of the KVM shadow MMU.
> +@item mem_merge=on|off
> +Enables or disables memory merge support (enabled by default).

Describe it as memory deduplication? Just a suggestion. Interface looks
good to me now.

Jan
Luiz Capitulino - June 28, 2012, 1:38 p.m.
On Thu, 28 Jun 2012 11:25:10 +0200
Jan Kiszka <jan.kiszka@siemens.com> wrote:

> On 2012-06-27 18:49, Luiz Capitulino wrote:
> > Allows to disable memory merge support (KSM on Linux), which is enabled
> > by default otherwise.
> > 
> > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > ---
> >  exec.c          | 19 ++++++++++++++++---
> >  qemu-config.c   |  4 ++++
> >  qemu-options.hx |  5 ++++-
> >  3 files changed, 24 insertions(+), 4 deletions(-)
> > 
> > diff --git a/exec.c b/exec.c
> > index 8244d54..24dd154 100644
> > --- a/exec.c
> > +++ b/exec.c
> > @@ -2499,6 +2499,19 @@ void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
> >      }
> >  }
> >  
> > +static int madvise_mergeable(void *addr, size_t len)
> > +{
> > +    QemuOpts *opts;
> > +
> > +    opts = qemu_opts_find(qemu_find_opts("machine"), 0);
> > +    if (opts && !qemu_opt_get_bool(opts, "mem_merge", true)) {
> > +        /* disabled by the user */
> > +        return 0;
> > +    }
> > +
> > +    return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
> > +}
> > +
> >  ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
> >                                     MemoryRegion *mr)
> >  {
> > @@ -2518,7 +2531,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
> >              new_block->host = file_ram_alloc(new_block, size, mem_path);
> >              if (!new_block->host) {
> >                  new_block->host = qemu_vmalloc(size);
> > -                qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
> > +                madvise_mergeable(new_block->host, size);
> >              }
> >  #else
> >              fprintf(stderr, "-mem-path option unsupported\n");
> > @@ -2545,7 +2558,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
> >                  new_block->host = qemu_vmalloc(size);
> >              }
> >  #endif
> > -            qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
> > +            madvise_mergeable(new_block->host, size);
> >          }
> >      }
> >      new_block->length = size;
> > @@ -2672,7 +2685,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
> >                              length, addr);
> >                      exit(1);
> >                  }
> > -                qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
> > +                madvise_mergeable(vaddr, length);
> >              }
> >              return;
> >          }
> > diff --git a/qemu-config.c b/qemu-config.c
> > index 5c3296b..e47612e 100644
> > --- a/qemu-config.c
> > +++ b/qemu-config.c
> > @@ -595,6 +595,10 @@ static QemuOptsList qemu_machine_opts = {
> >              .name = "dt_compatible",
> >              .type = QEMU_OPT_STRING,
> >              .help = "Overrides the \"compatible\" property of the dt root node",
> > +        }, {
> > +            .name = "mem_merge",
> > +            .type = QEMU_OPT_BOOL,
> > +            .help = "enable/disable memory merge support",
> >          },
> >          { /* End of list */ }
> >      },
> > diff --git a/qemu-options.hx b/qemu-options.hx
> > index 8b66264..e0e7686 100644
> > --- a/qemu-options.hx
> > +++ b/qemu-options.hx
> > @@ -33,7 +33,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
> >      "                property accel=accel1[:accel2[:...]] selects accelerator\n"
> >      "                supported accelerators are kvm, xen, tcg (default: tcg)\n"
> >      "                kernel_irqchip=on|off controls accelerated irqchip support\n"
> > -    "                kvm_shadow_mem=size of KVM shadow MMU\n",
> > +    "                kvm_shadow_mem=size of KVM shadow MMU\n"
> > +    "                mem_merge=on|off controls memory merge support (default: on)\n",
> >      QEMU_ARCH_ALL)
> >  STEXI
> >  @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
> > @@ -50,6 +51,8 @@ to initialize.
> >  Enables in-kernel irqchip support for the chosen accelerator when available.
> >  @item kvm_shadow_mem=size
> >  Defines the size of the KVM shadow MMU.
> > +@item mem_merge=on|off
> > +Enables or disables memory merge support (enabled by default).
> 
> Describe it as memory deduplication? Just a suggestion. Interface looks
> good to me now.

I can extend the manpage description to read:

 Enables or disables memory merge support. This feature, when supported by
 the host, de-duplicates identical memory pages among VMs instances.
 (enabled by default).

Patch

diff --git a/exec.c b/exec.c
index 8244d54..24dd154 100644
--- a/exec.c
+++ b/exec.c
@@ -2499,6 +2499,19 @@  void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
     }
 }
 
+static int madvise_mergeable(void *addr, size_t len)
+{
+    QemuOpts *opts;
+
+    opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (opts && !qemu_opt_get_bool(opts, "mem_merge", true)) {
+        /* disabled by the user */
+        return 0;
+    }
+
+    return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
+}
+
 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                    MemoryRegion *mr)
 {
@@ -2518,7 +2531,7 @@  ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
             new_block->host = file_ram_alloc(new_block, size, mem_path);
             if (!new_block->host) {
                 new_block->host = qemu_vmalloc(size);
-                qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
+                madvise_mergeable(new_block->host, size);
             }
 #else
             fprintf(stderr, "-mem-path option unsupported\n");
@@ -2545,7 +2558,7 @@  ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                 new_block->host = qemu_vmalloc(size);
             }
 #endif
-            qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
+            madvise_mergeable(new_block->host, size);
         }
     }
     new_block->length = size;
@@ -2672,7 +2685,7 @@  void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
                             length, addr);
                     exit(1);
                 }
-                qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
+                madvise_mergeable(vaddr, length);
             }
             return;
         }
diff --git a/qemu-config.c b/qemu-config.c
index 5c3296b..e47612e 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -595,6 +595,10 @@  static QemuOptsList qemu_machine_opts = {
             .name = "dt_compatible",
             .type = QEMU_OPT_STRING,
             .help = "Overrides the \"compatible\" property of the dt root node",
+        }, {
+            .name = "mem_merge",
+            .type = QEMU_OPT_BOOL,
+            .help = "enable/disable memory merge support",
         },
         { /* End of list */ }
     },
diff --git a/qemu-options.hx b/qemu-options.hx
index 8b66264..e0e7686 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -33,7 +33,8 @@  DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                property accel=accel1[:accel2[:...]] selects accelerator\n"
     "                supported accelerators are kvm, xen, tcg (default: tcg)\n"
     "                kernel_irqchip=on|off controls accelerated irqchip support\n"
-    "                kvm_shadow_mem=size of KVM shadow MMU\n",
+    "                kvm_shadow_mem=size of KVM shadow MMU\n"
+    "                mem_merge=on|off controls memory merge support (default: on)\n",
     QEMU_ARCH_ALL)
 STEXI
 @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
@@ -50,6 +51,8 @@  to initialize.
 Enables in-kernel irqchip support for the chosen accelerator when available.
 @item kvm_shadow_mem=size
 Defines the size of the KVM shadow MMU.
+@item mem_merge=on|off
+Enables or disables memory merge support (enabled by default).
 @end table
 ETEXI