Skip to contentSkip to conversation

libxl: a few domain maximum memory fixes

PATCH In review Jim Fehlig 2 participants 7 messages Feb 2, 2017 – Feb 8, 2017

lore
Share & tools
lore

Jim Fehlig

author
#0 lore raw

cover libxl: a few domain maximum memory fixes

Patch 1 fixes reporting of domain maximum memory for running domains. When creating a virDomainDef object to represent dom0, max memory was not set correctly, which is fixed by patch2.

Jim Fehlig (2):
libxl: fix reporting of maximum memory
libxl: fix dom0 maximum memory setting

 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  7 ++---
 3 files changed, 81 insertions(+), 4 deletions(-)
Signature · 7 lines
2.9.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH 1/2 libxl: fix reporting of maximum memory

The libxl driver reports different values of maximum memory depending on state of a domain. If inactive, maximum memory value is reported correctly. When active, maximum memory is derived from max_pages value returned by the XEN_SYSCTL_getdomaininfolist sysctl operation. But max_pages can be changed by toolstacks and does not necessarily represent the maximum memory a domain can use during its active lifetime.

A better location for determining a domain's maximum memory is the /local/domain/<id>/memory/static-max node in xenstore. This value is set from the libxl_domain_build_info.max_memkb field when creating the domain. Currently it cannot be changed nor can its value be exceeded by a balloon operation. From libvirt's perspective, always reporting maximum memory with virDomainDefGetMemoryTotal() will produce the same results as reading the static-max node in xenstore.

---

As an alternative, libvirt could call libxl_retrieve_domain_configuration and use the max_memkb field embedded in the libxl_domain_config object.

 src/libxl/libxl_driver.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/libxl/libxl_driver.c +2 -3
index 3a69720..921cc93 100644
@@ -1640,10 +1640,10 @@ libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
     if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
 
+    info->maxMem = virDomainDefGetMemoryTotal(vm->def);
     if (!virDomainObjIsActive(vm)) {
         info->cpuTime = 0;
         info->memory = vm->def->mem.cur_balloon;
-        info->maxMem = virDomainDefGetMemoryTotal(vm->def);
     } else {
         libxl_dominfo_init(&d_info);
 
@@ -1655,7 +1655,6 @@ libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
         }
         info->cpuTime = d_info.cpu_time;
         info->memory = d_info.current_memkb;
-        info->maxMem = d_info.max_memkb;
 
         libxl_dominfo_dispose(&d_info);
     }
@@ -5172,7 +5171,7 @@ libxlDomainMemoryStats(virDomainPtr dom,
         goto endjob;
     }
     mem = d_info.current_memkb;
-    maxmem = d_info.max_memkb;
+    maxmem = virDomainDefGetMemoryTotal(vm->def);
 
     LIBXL_SET_MEMSTAT(VIR_DOMAIN_MEMORY_STAT_ACTUAL_BALLOON, mem);
     LIBXL_SET_MEMSTAT(VIR_DOMAIN_MEMORY_STAT_AVAILABLE, maxmem);
Signature · 7 lines
2.9.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#2 lore raw

PATCH 2/2 libxl: fix dom0 maximum memory setting

In reply to Jim Fehlig

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.

---
 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  2 +-
 3 files changed, 79 insertions(+), 1 deletion(-)
src/libxl/libxl_conf.c +75 -0
index b5186f2..bfe0e92 100644
@@ -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"
@@ -1530,6 +1531,80 @@ int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
 
 }
 
+/*
+ * 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
+ *
+ * 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.
+ */
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    unsigned long long ret;
+    libxl_physinfo physinfo;
+
+    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, &ret) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'k':
+                    case 'K':
+                        multiplier = 1024;
+                        break;
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }
+                ret = (ret * multiplier) / 1024;
+                goto cleanup;
+            }
+        }
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    libxl_get_physinfo(cfg->ctx, &physinfo);
+    ret = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+
+ cleanup:
+    virStringListFree(cmd_tokens);
+    virStringListFree(mem_tokens);
+    return ret;
+}
+
+
 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
 static int
 libxlPrepareChannel(virDomainChrDefPtr channel,
src/libxl/libxl_conf.h +3 -0
index 69d7885..c4ddbfe 100644
@@ -173,6 +173,9 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver,
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg);
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_dev, libxl_device_disk *x_dev);
 int
src/libxl/libxl_driver.c +1 -1
index 921cc93..e54b3b7 100644
@@ -615,7 +615,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
     if (virDomainDefSetVcpus(vm->def, d_info.vcpu_online) < 0)
         goto cleanup;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    virDomainDefSetMemoryTotal(vm->def, d_info.max_memkb);
+    virDomainDefSetMemoryTotal(vm->def, libxlDriverGetDom0MaxmemConf(cfg));
 
     ret = 0;
 
Signature · 7 lines
2.9.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw

Re: [libvirt] [PATCH 2/2] libxl: fix dom0 maximum memory setting

Quoted 89 lines · Jim Fehlig

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.

---
 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  2 +-
 3 files changed, 79 insertions(+), 1 deletion(-)
src/libxl/libxl_conf.c +55 -0
index b5186f2..bfe0e92 100644
@@ -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"
@@ -1530,6 +1531,80 @@ int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
 
 }
 
+/*
+ * 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
+ *
+ * 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.
+ */
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    unsigned long long ret;
+    libxl_physinfo physinfo;
+
+    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, &ret) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'k':
+                    case 'K':
+                        multiplier = 1024;
+                        break;
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }

src/libxl/libxl_conf.c int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg, · line 1597

Quoted 10 lines
src/libxl/libxl_conf.c +10 -0
+                ret = (ret * multiplier) / 1024;
+                goto cleanup;
+            }
+        }
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    libxl_get_physinfo(cfg->ctx, &physinfo);

Despite being an unlikely event, libxl_get_physinfo can fail here - I think you need to check the return value here.

Quoted 32 lines
src/libxl/libxl_conf.c +10 -0
+    ret = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+
+ cleanup:
+    virStringListFree(cmd_tokens);
+    virStringListFree(mem_tokens);
+    return ret;
+}
+
+
 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
 static int
 libxlPrepareChannel(virDomainChrDefPtr channel,
src/libxl/libxl_conf.h +3 -0
index 69d7885..c4ddbfe 100644
@@ -173,6 +173,9 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver,
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg);
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_dev, libxl_device_disk *x_dev);
 int
src/libxl/libxl_driver.c +0 -0
index 921cc93..e54b3b7 100644

src/libxl/libxl_driver.c libxlAddDom0(libxlDriverPrivatePtr driver) · line 618

Quoted 5 lines
src/libxl/libxl_driver.c +1 -1
@@ -615,7 +615,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
     if (virDomainDefSetVcpus(vm->def, d_info.vcpu_online) < 0)
         goto cleanup;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    virDomainDefSetMemoryTotal(vm->def, d_info.max_memkb);
+    virDomainDefSetMemoryTotal(vm->def, libxlDriverGetDom0MaxmemConf(cfg));

And perhaps checking here too on whether "libxlDriverGetDom0MaxmemConf(cfg) < 0" ?

src/libxl/libxl_driver.c libxlAddDom0(libxlDriverPrivatePtr driver)

Quoted 4 lines
src/libxl/libxl_driver.c +0 -0
     ret = 0;
 
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#4 lore raw

Re: [libvirt] [PATCH 2/2] libxl: fix dom0 maximum memory setting

Quoted 94 lines · Joao Martins
Quoted 93 lines · Jim Fehlig

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.

---
 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  2 +-
 3 files changed, 79 insertions(+), 1 deletion(-)
src/libxl/libxl_conf.c +59 -0
index b5186f2..bfe0e92 100644
@@ -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"
@@ -1530,6 +1531,80 @@ int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
 
 }
 
+/*
+ * 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
+ *
+ * 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.
+ */
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    unsigned long long ret;
+    libxl_physinfo physinfo;
+
+    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, &ret) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'k':
+                    case 'K':
+                        multiplier = 1024;
+                        break;
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }
+                ret = (ret * multiplier) / 1024;
+                goto cleanup;
+            }
+        }
Quoted 8 lines

src/libxl/libxl_conf.c int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg, · line 1597

Quoted 6 lines
src/libxl/libxl_conf.c +6 -0
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    libxl_get_physinfo(cfg->ctx, &physinfo);

Despite being an unlikely event, libxl_get_physinfo can fail here - I think you need to check the return value here.

Bummer. I was hoping this function could always return a sane value for dom0 max memory. But I'm not really sure what that would be if libxl_get_physinfo failed.

Quoted 32 lines
Quoted 32 lines
src/libxl/libxl_conf.c +10 -0
+    ret = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+
+ cleanup:
+    virStringListFree(cmd_tokens);
+    virStringListFree(mem_tokens);
+    return ret;
+}
+
+
 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
 static int
 libxlPrepareChannel(virDomainChrDefPtr channel,
src/libxl/libxl_conf.h +3 -0
index 69d7885..c4ddbfe 100644
@@ -173,6 +173,9 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver,
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg);
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_dev, libxl_device_disk *x_dev);
 int
src/libxl/libxl_driver.c +0 -0
index 921cc93..e54b3b7 100644
Quoted 8 lines

src/libxl/libxl_driver.c libxlAddDom0(libxlDriverPrivatePtr driver) · line 618

Quoted 6 lines
src/libxl/libxl_driver.c +1 -1
@@ -615,7 +615,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
     if (virDomainDefSetVcpus(vm->def, d_info.vcpu_online) < 0)
         goto cleanup;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    virDomainDefSetMemoryTotal(vm->def, d_info.max_memkb);
+    virDomainDefSetMemoryTotal(vm->def, libxlDriverGetDom0MaxmemConf(cfg));

And perhaps checking here too on whether "libxlDriverGetDom0MaxmemConf(cfg) < 0" ?

ATM, libxlDriverGetDom0MaxmemConf returns an unsigned long long. I could change the signature to

int libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
unsigned long long *maxmem)

but again I'm not really sure what to do with maxmem in case of failure. Perhaps just set it to current_memkb?

Regards,
Jim

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Re: [libvirt] [PATCH 2/2] libxl: fix dom0 maximum memory setting

Quoted 104 lines · Jim Fehlig
Quoted 94 lines · Joao Martins
Quoted 93 lines · Jim Fehlig

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.

---
 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  2 +-
 3 files changed, 79 insertions(+), 1 deletion(-)
src/libxl/libxl_conf.c +59 -0
index b5186f2..bfe0e92 100644
@@ -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"
@@ -1530,6 +1531,80 @@ int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
 
 }
 
+/*
+ * 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
+ *
+ * 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.
+ */
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    unsigned long long ret;
+    libxl_physinfo physinfo;
+
+    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, &ret) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'k':
+                    case 'K':
+                        multiplier = 1024;
+                        break;
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }
+                ret = (ret * multiplier) / 1024;
+                goto cleanup;
+            }
+        }
Quoted 8 lines

src/libxl/libxl_conf.c int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg, · line 1597

Quoted 6 lines
src/libxl/libxl_conf.c +6 -0
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    libxl_get_physinfo(cfg->ctx, &physinfo);

Despite being an unlikely event, libxl_get_physinfo can fail here - I think you need to check the return value here.

Quoted 3 lines

Bummer. I was hoping this function could always return a sane value for dom0 max memory. But I'm not really sure what that would be if libxl_get_physinfo failed.

Yeah, We probably have some serious concerns if this fails. And given that libxlAddDom0 happens early while bootstrapping the libxl module, it might be good to warn the user (following similar pattern throughout the rest of the code):

virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

               _("libxl_get_physinfo_info failed"));

or perhaps simply VIR_WARN?

Quoted 43 lines
Quoted 31 lines
Quoted 31 lines
src/libxl/libxl_conf.c +10 -0
+    ret = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+
+ cleanup:
+    virStringListFree(cmd_tokens);
+    virStringListFree(mem_tokens);
+    return ret;
+}
+
+
 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
 static int
 libxlPrepareChannel(virDomainChrDefPtr channel,
src/libxl/libxl_conf.h +3 -0
index 69d7885..c4ddbfe 100644
@@ -173,6 +173,9 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver,
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg);
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_dev, libxl_device_disk *x_dev);
 int
src/libxl/libxl_driver.c +0 -0
index 921cc93..e54b3b7 100644
Quoted 8 lines

src/libxl/libxl_driver.c libxlAddDom0(libxlDriverPrivatePtr driver) · line 618

Quoted 6 lines
src/libxl/libxl_driver.c +1 -1
@@ -615,7 +615,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
     if (virDomainDefSetVcpus(vm->def, d_info.vcpu_online) < 0)
         goto cleanup;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    virDomainDefSetMemoryTotal(vm->def, d_info.max_memkb);
+    virDomainDefSetMemoryTotal(vm->def, libxlDriverGetDom0MaxmemConf(cfg));

And perhaps checking here too on whether "libxlDriverGetDom0MaxmemConf(cfg) < 0" ?

ATM, libxlDriverGetDom0MaxmemConf returns an unsigned long long. I could change the signature to

Quoted 6 lines

int libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
unsigned long long *maxmem)

but again I'm not really sure what to do with maxmem in case of failure. Perhaps just set it to current_memkb?

Yeap, that's what I was thinking too.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#6 lore raw

Re: [libvirt] [PATCH 2/2] libxl: fix dom0 maximum memory setting

Quoted 157 lines · Joao Martins
Quoted 103 lines · Jim Fehlig
Quoted 94 lines · Joao Martins
Quoted 93 lines · Jim Fehlig

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.

---
 src/libxl/libxl_conf.c   | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/libxl/libxl_conf.h   |  3 ++
 src/libxl/libxl_driver.c |  2 +-
 3 files changed, 79 insertions(+), 1 deletion(-)
src/libxl/libxl_conf.c +59 -0
index b5186f2..bfe0e92 100644
@@ -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"
@@ -1530,6 +1531,80 @@ int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
 
 }
 
+/*
+ * 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
+ *
+ * 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.
+ */
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg)
+{
+    char **cmd_tokens = NULL;
+    char **mem_tokens = NULL;
+    size_t i;
+    size_t j;
+    unsigned long long ret;
+    libxl_physinfo physinfo;
+
+    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, &ret) < 0)
+                    break;
+                if (*p) {
+                    switch (*p) {
+                    case 'k':
+                    case 'K':
+                        multiplier = 1024;
+                        break;
+                    case 'm':
+                    case 'M':
+                        multiplier = 1024 * 1024;
+                        break;
+                    case 'g':
+                    case 'G':
+                        multiplier = 1024 * 1024 * 1024;
+                        break;
+                    }
+                }
+                ret = (ret * multiplier) / 1024;
+                goto cleanup;
+            }
+        }
Quoted 8 lines

src/libxl/libxl_conf.c int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg, · line 1597

Quoted 6 lines
src/libxl/libxl_conf.c +6 -0
+    }
+
+ physmem:
+    /* No 'max' specified in dom0_mem, so dom0 can use all physical memory */
+    libxl_physinfo_init(&physinfo);
+    libxl_get_physinfo(cfg->ctx, &physinfo);

Despite being an unlikely event, libxl_get_physinfo can fail here - I think you need to check the return value here.

Quoted 3 lines

Bummer. I was hoping this function could always return a sane value for dom0 max memory. But I'm not really sure what that would be if libxl_get_physinfo failed.

Yeah, We probably have some serious concerns if this fails. And given that libxlAddDom0 happens early while bootstrapping the libxl module, it might be good to warn the user (following similar pattern throughout the rest of the code):

virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

               _("libxl_get_physinfo_info failed"));

or perhaps simply VIR_WARN?

Quoted 41 lines
Quoted 30 lines
Quoted 30 lines
src/libxl/libxl_conf.c +10 -0
+    ret = (physinfo.total_pages * cfg->verInfo->pagesize) / 1024;
+    libxl_physinfo_dispose(&physinfo);
+
+ cleanup:
+    virStringListFree(cmd_tokens);
+    virStringListFree(mem_tokens);
+    return ret;
+}
+
+
 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
 static int
 libxlPrepareChannel(virDomainChrDefPtr channel,
src/libxl/libxl_conf.h +3 -0
index 69d7885..c4ddbfe 100644
@@ -173,6 +173,9 @@ libxlDriverNodeGetInfo(libxlDriverPrivatePtr driver,
 int libxlDriverConfigLoadFile(libxlDriverConfigPtr cfg,
                               const char *filename);
 
+unsigned long long
+libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg);
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_dev, libxl_device_disk *x_dev);
 int
src/libxl/libxl_driver.c +0 -1
index 921cc93..e54b3b7 100644
--- a/src/libxl/libxl_driver.c
Quoted 8 lines

src/libxl/libxl_driver.c libxlAddDom0(libxlDriverPrivatePtr driver) · line 618

Quoted 7 lines

+++ b/src/libxl/libxl_driver.c

src/libxl/libxl_driver.c +1 -1
@@ -615,7 +615,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
     if (virDomainDefSetVcpus(vm->def, d_info.vcpu_online) < 0)
         goto cleanup;
     vm->def->mem.cur_balloon = d_info.current_memkb;
-    virDomainDefSetMemoryTotal(vm->def, d_info.max_memkb);
+    virDomainDefSetMemoryTotal(vm->def, libxlDriverGetDom0MaxmemConf(cfg));

And perhaps checking here too on whether "libxlDriverGetDom0MaxmemConf(cfg) < 0" ?

ATM, libxlDriverGetDom0MaxmemConf returns an unsigned long long. I could change the signature to

Quoted 7 lines
Quoted 6 lines

int libxlDriverGetDom0MaxmemConf(libxlDriverConfigPtr cfg,
unsigned long long *maxmem)

but again I'm not really sure what to do with maxmem in case of failure. Perhaps just set it to current_memkb?

Yeap, that's what I was thinking too.

Thanks for the comments. I've sent a V2 incorporating them

https://www.redhat.com/archives/libvir-list/2017-February/msg00349.html

Regards,
Jim

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel