commit 79692c387497a0b67f65b0293291d1137461985f
Author: Jim Fehlig <jfehlig@suse.com>
Date:   Tue Jan 31 17:10:34 2017 -0700

    libxl: fix dom0 maximum memory setting
    
    When the libxl driver is initialized, it creates a virDomainDef
    object for dom0 and adds it to the list of domains. Total memory
    for dom0 was being set from the max_memkb field of libxl_dominfo
    struct retrieved from libxl, but this field can be set to
    LIBXL_MEMKB_DEFAULT (~0ULL) if dom0 maximum memory has not been
    explicitly set by the user.
    
    This patch adds some simple parsing of the Xen commandline,
    looking for a dom0_mem parameter that also specifies a 'max' value.
    If not specified, dom0 maximum memory is effectively all physical
    host memory.
    
    Signed-off-by: Jim Fehlig <jfehlig@suse.com>

Index: libvirt-1.2.18.4/src/libxl/libxl_conf.c
===================================================================
--- libvirt-1.2.18.4.orig/src/libxl/libxl_conf.c
+++ libvirt-1.2.18.4/src/libxl/libxl_conf.c
@@ -34,6 +34,7 @@
 #include "internal.h"
 #include "virlog.h"
 #include "virerror.h"
+#include "c-ctype.h"
 #include "datatypes.h"
 #include "virconf.h"
 #include "virfile.h"
@@ -1735,6 +1736,87 @@ int libxlDriverConfigLoadFile(libxlDrive
 
 }
 
+/*
+ * dom0's maximum memory can be controled by the user with the 'dom0_mem' Xen
+ * command line parameter. E.g. to set dom0's initial memory to 4G and max
+ * memory to 8G: dom0_mem=4G,max:8G
+ * Supported unit suffixes are [bBkKmMgGtT]. If not specified the default
+ * unit is kilobytes.
+ *
+ * If not constrained by the user, dom0 can effectively use all host memory.
+ * This function returns the configured maximum memory for dom0 in kilobytes,
+ * either the user-specified value or total physical memory as a default.
+ */
+int
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
+                             unsigned long long *maxmem)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    libxl_physinfo physinfo;
+    int ret = -1;
+
+    if (cfg->verInfo->commandline == NULL ||
+        !(cmd_tokens = virStringSplit(cfg->verInfo->commandline, " ", 0)))
+        goto physmem;
+
+    for (i = 0; cmd_tokens[i] != NULL; i++) {
+        if (!STRPREFIX(cmd_tokens[i], "dom0_mem="))
+            continue;
+
+        if (!(mem_tokens = virStringSplit(cmd_tokens[i], ",", 0)))
+            break;
+        for (j = 0; mem_tokens[j] != NULL; j++) {
+            if (STRPREFIX(mem_tokens[j], "max:")) {
+                char *p = mem_tokens[j] + 4;
+                unsigned long long multiplier = 1;
+
+                while (c_isdigit(*p))
+                    p++;
+                if (virStrToLong_ull(mem_tokens[j] + 4, &p, 10, maxmem) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 't':
+                    case 'T':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }
+                *maxmem = *maxmem * multiplier;
+                ret = 0;
+                goto cleanup;
+            }
+        }
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    if (libxl_get_physinfo(cfg->ctx, &physinfo)) {
+        VIR_WARN("libxl_get_physinfo failed");
+        goto cleanup;
+    }
+    *maxmem = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+    ret = 0;
+
+ cleanup:
+    virStringFreeList(cmd_tokens);
+    virStringFreeList(mem_tokens);
+    return ret;
+}
+
 int
 libxlMakePCI(virDomainHostdevDefPtr hostdev, libxl_device_pci *pcidev)
 {
Index: libvirt-1.2.18.4/src/libxl/libxl_conf.h
===================================================================
--- libvirt-1.2.18.4.orig/src/libxl/libxl_conf.h
+++ libvirt-1.2.18.4/src/libxl/libxl_conf.h
@@ -178,6 +178,10 @@ libxlDriverNodeGetInfo(libxlDriverPrivat
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+int
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
+                             unsigned long long *maxmem);
+
 virCapsPtr
 libxlMakeCapabilities(libxl_ctx *ctx);
 
Index: libvirt-1.2.18.4/src/libxl/libxl_driver.c
===================================================================
--- libvirt-1.2.18.4.orig/src/libxl/libxl_driver.c
+++ libvirt-1.2.18.4/src/libxl/libxl_driver.c
@@ -517,6 +517,7 @@ libxlAddDom0(libxlDriverPrivatePtr drive
     virDomainObjPtr vm = NULL;
     virDomainDefPtr oldDef = NULL;
     libxl_dominfo d_info;
+    unsigned long long maxmem;
     int ret = -1;
 
     libxl_dominfo_init(&d_info);
@@ -553,7 +554,9 @@ libxlAddDom0(libxlDriverPrivatePtr drive
     vm->def->vcpus = d_info.vcpu_online;
     vm->def->maxvcpus = d_info.vcpu_max_id + 1;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    vm->def->mem.max_balloon = d_info.max_memkb;
+    if (libxlDriverGetDom0MaxmemConf(cfg, &maxmem) < 0)
+        maxmem = d_info.current_memkb;
+    vm->def->mem.max_balloon = maxmem;
 
     ret = 0;
 
