[libvirt] [PATCH 0/2] add support for --config in setmaxmem command

Chen Hanxiao (2): LXC: add support for --config in setmaxmem command LXC: use lxcDomainSetMemoryFlags to do lxcDomainSetMaxMemory's work src/lxc/lxc_driver.c | 105 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 54 deletions(-) -- 1.9.0

In lxc, we could not use setmaxmem command with --config options. This patch will add support for this. Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 69 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index b7b4b02..be6ee19 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -721,10 +721,10 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, virLXCDomainObjPrivatePtr priv; virLXCDriverPtr driver = dom->conn->privateData; virLXCDriverConfigPtr cfg = NULL; - unsigned long oldmax = 0; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | - VIR_DOMAIN_AFFECT_CONFIG, -1); + VIR_DOMAIN_AFFECT_CONFIG | + VIR_DOMAIN_MEM_MAXIMUM, -1); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -743,32 +743,55 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, &persistentDef) < 0) goto cleanup; - if (flags & VIR_DOMAIN_AFFECT_LIVE) - oldmax = vm->def->mem.max_balloon; - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - if (!oldmax || oldmax > persistentDef->mem.max_balloon) - oldmax = persistentDef->mem.max_balloon; - } + if (flags & VIR_DOMAIN_MEM_MAXIMUM) { + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (newmem < vm->def->mem.cur_balloon) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("Cannot resize the max memory less than current" + " memory for an active domain")); + goto cleanup; + } + vm->def->mem.max_balloon = newmem; + } - if (newmem > oldmax) { - virReportError(VIR_ERR_INVALID_ARG, - "%s", _("Cannot set memory higher than max memory")); - goto cleanup; - } + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef); + persistentDef->mem.max_balloon = newmem; + if (persistentDef->mem.cur_balloon > newmem) + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } + } else { + unsigned long oldmax = 0; - if (flags & VIR_DOMAIN_AFFECT_LIVE) { - if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - "%s", _("Failed to set memory for domain")); - goto cleanup; + if (flags & VIR_DOMAIN_AFFECT_LIVE) + oldmax = vm->def->mem.max_balloon; + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + if (!oldmax || oldmax > persistentDef->mem.max_balloon) + oldmax = persistentDef->mem.max_balloon; } - } - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - sa_assert(persistentDef); - persistentDef->mem.cur_balloon = newmem; - if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + if (newmem > oldmax) { + virReportError(VIR_ERR_INVALID_ARG, + "%s", _("Cannot set memory higher than max memory")); goto cleanup; + } + + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + "%s", _("Failed to set memory for domain")); + goto cleanup; + } + } + + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef); + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } } ret = 0; -- 1.9.0

On 16.07.2014 11:51, Chen Hanxiao wrote:
In lxc, we could not use setmaxmem command with --config options. This patch will add support for this.
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 69 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 23 deletions(-)
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index b7b4b02..be6ee19 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -721,10 +721,10 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, virLXCDomainObjPrivatePtr priv; virLXCDriverPtr driver = dom->conn->privateData; virLXCDriverConfigPtr cfg = NULL; - unsigned long oldmax = 0;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | - VIR_DOMAIN_AFFECT_CONFIG, -1); + VIR_DOMAIN_AFFECT_CONFIG | + VIR_DOMAIN_MEM_MAXIMUM, -1);
if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -743,32 +743,55 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, &persistentDef) < 0) goto cleanup;
- if (flags & VIR_DOMAIN_AFFECT_LIVE) - oldmax = vm->def->mem.max_balloon; - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - if (!oldmax || oldmax > persistentDef->mem.max_balloon) - oldmax = persistentDef->mem.max_balloon; - } + if (flags & VIR_DOMAIN_MEM_MAXIMUM) { + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (newmem < vm->def->mem.cur_balloon) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("Cannot resize the max memory less than current" + " memory for an active domain")); + goto cleanup; + } + vm->def->mem.max_balloon = newmem;
Are things that easy? Don't we need to communicate this with the lxc_controler somehow? Even though you allow only extending, I think unless we are 100% sure guest will see the resize, we shouldn't allow this.
+ }
- if (newmem > oldmax) { - virReportError(VIR_ERR_INVALID_ARG, - "%s", _("Cannot set memory higher than max memory")); - goto cleanup; - } + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef);
Is this assert needed? Did clang complain or is this just a pure lefover from copying from qemu_driver.c?
+ persistentDef->mem.max_balloon = newmem; + if (persistentDef->mem.cur_balloon > newmem) + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } + } else { + unsigned long oldmax = 0;
- if (flags & VIR_DOMAIN_AFFECT_LIVE) { - if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - "%s", _("Failed to set memory for domain")); - goto cleanup; + if (flags & VIR_DOMAIN_AFFECT_LIVE) + oldmax = vm->def->mem.max_balloon; + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + if (!oldmax || oldmax > persistentDef->mem.max_balloon) + oldmax = persistentDef->mem.max_balloon; } - }
- if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - sa_assert(persistentDef);
Well, since it has been here already, I think we can leave it in your patch too.
- persistentDef->mem.cur_balloon = newmem; - if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + if (newmem > oldmax) { + virReportError(VIR_ERR_INVALID_ARG, + "%s", _("Cannot set memory higher than max memory")); goto cleanup; + } + + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + "%s", _("Failed to set memory for domain")); + goto cleanup; + } + } + + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef); + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } }
ret = 0;
But what I miss in this patch is, what would: virDomainSetMaxMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_CURRENT | VIR_DOMAIN_MEM_MAXIMUM) do? I mean, the _CURRENT is not translated to _LIVE or _CONFIG anywhere in the function. Michal

-----Original Message----- From: Michal Privoznik [mailto:mprivozn@redhat.com] Sent: Thursday, July 24, 2014 5:00 PM To: Chen, Hanxiao/陈 晗霄; libvir-list@redhat.com Subject: Re: [libvirt] [PATCH 1/2] LXC: add support for --config in setmaxmem command
On 16.07.2014 11:51, Chen Hanxiao wrote:
In lxc, we could not use setmaxmem command with --config options. This patch will add support for this.
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 69 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 23 deletions(-)
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index b7b4b02..be6ee19 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -721,10 +721,10 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, virLXCDomainObjPrivatePtr priv; virLXCDriverPtr driver = dom->conn->privateData; virLXCDriverConfigPtr cfg = NULL; - unsigned long oldmax = 0;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | - VIR_DOMAIN_AFFECT_CONFIG, -1); + VIR_DOMAIN_AFFECT_CONFIG | + VIR_DOMAIN_MEM_MAXIMUM, -1);
if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -743,32 +743,55 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, &persistentDef) < 0) goto cleanup;
- if (flags & VIR_DOMAIN_AFFECT_LIVE) - oldmax = vm->def->mem.max_balloon; - if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - if (!oldmax || oldmax > persistentDef->mem.max_balloon) - oldmax = persistentDef->mem.max_balloon; - } + if (flags & VIR_DOMAIN_MEM_MAXIMUM) { + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (newmem < vm->def->mem.cur_balloon) { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("Cannot resize the max memory less than current" + " memory for an active domain")); + goto cleanup; + } + vm->def->mem.max_balloon = newmem;
Are things that easy? Don't we need to communicate this with the lxc_controler somehow? Even though you allow only extending, I think unless we are 100% sure guest will see the resize, we shouldn't allow this.
I focused on '--config', so I kept what the original lxcDomainSetMaxMemory did. It looks that guest could not see the resize, and we shouldn't allow this.
+ }
- if (newmem > oldmax) { - virReportError(VIR_ERR_INVALID_ARG, - "%s", _("Cannot set memory higher than max memory")); - goto cleanup; - } + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef);
Is this assert needed? Did clang complain or is this just a pure lefover from copying from qemu_driver.c?
+ persistentDef->mem.max_balloon = newmem; + if (persistentDef->mem.cur_balloon > newmem) + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } + } else { + unsigned long oldmax = 0;
- if (flags & VIR_DOMAIN_AFFECT_LIVE) { - if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - "%s", _("Failed to set memory for domain")); - goto cleanup; + if (flags & VIR_DOMAIN_AFFECT_LIVE) + oldmax = vm->def->mem.max_balloon; + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + if (!oldmax || oldmax > persistentDef->mem.max_balloon) + oldmax = persistentDef->mem.max_balloon; } - }
- if (flags & VIR_DOMAIN_AFFECT_CONFIG) { - sa_assert(persistentDef);
Well, since it has been here already, I think we can leave it in your patch too.
- persistentDef->mem.cur_balloon = newmem; - if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + if (newmem > oldmax) { + virReportError(VIR_ERR_INVALID_ARG, + "%s", _("Cannot set memory higher than max memory")); goto cleanup; + } + + if (flags & VIR_DOMAIN_AFFECT_LIVE) { + if (virCgroupSetMemory(priv->cgroup, newmem) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + "%s", _("Failed to set memory for domain")); + goto cleanup; + } + } + + if (flags & VIR_DOMAIN_AFFECT_CONFIG) { + sa_assert(persistentDef); + persistentDef->mem.cur_balloon = newmem; + if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0) + goto cleanup; + } }
ret = 0;
But what I miss in this patch is, what would:
virDomainSetMaxMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_CURRENT | VIR_DOMAIN_MEM_MAXIMUM)
do? I mean, the _CURRENT is not translated to _LIVE or _CONFIG anywhere in the function.
I put that section in 2/2 patch. I'll add a description in v2. Thanks, - Chen

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index be6ee19..9f974eb 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -680,37 +680,6 @@ lxcDomainGetMaxMemory(virDomainPtr dom) return ret; } -static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) -{ - virDomainObjPtr vm; - int ret = -1; - - if (!(vm = lxcDomObjFromDomain(dom))) - goto cleanup; - - if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0) - goto cleanup; - - if (newmax < vm->def->mem.cur_balloon) { - if (!virDomainObjIsActive(vm)) { - vm->def->mem.cur_balloon = newmax; - } else { - virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("Cannot set max memory lower than current" - " memory for an active domain")); - goto cleanup; - } - } - - vm->def->mem.max_balloon = newmax; - ret = 0; - - cleanup: - if (vm) - virObjectUnlock(vm); - return ret; -} - static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, unsigned int flags) { @@ -809,6 +778,11 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) return lxcDomainSetMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_LIVE); } +static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long memory) +{ + return lxcDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM); +} + static int lxcDomainSetMemoryParameters(virDomainPtr dom, virTypedParameterPtr params, -- 1.9.0

On 16.07.2014 11:51, Chen Hanxiao wrote:
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-)
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index be6ee19..9f974eb 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -680,37 +680,6 @@ lxcDomainGetMaxMemory(virDomainPtr dom) return ret; }
-static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) -{ - virDomainObjPtr vm; - int ret = -1; - - if (!(vm = lxcDomObjFromDomain(dom))) - goto cleanup; - - if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0) - goto cleanup; - - if (newmax < vm->def->mem.cur_balloon) { - if (!virDomainObjIsActive(vm)) { - vm->def->mem.cur_balloon = newmax; - } else { - virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("Cannot set max memory lower than current" - " memory for an active domain")); - goto cleanup; - } - } - - vm->def->mem.max_balloon = newmax; - ret = 0; - - cleanup: - if (vm) - virObjectUnlock(vm); - return ret; -} -
A-ha! This is what I was looking for in 1/2. Okay, but I'd rather note this fact in 1/2 commit message to make it more obvious.
static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, unsigned int flags) { @@ -809,6 +778,11 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) return lxcDomainSetMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_LIVE); }
+static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long memory) +{ + return lxcDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM); +} +
So previously, calling virDomainSetMaxMemory() on an inactive LXC domain would succeed. Now, after the change, due to problem with _CURRENT, _LIVE and _CONFIG this will basically return success, but without any effect on the domain config. And that's wrong.
static int lxcDomainSetMemoryParameters(virDomainPtr dom, virTypedParameterPtr params,
Moreover, I think these two patches can be joined into one. Michal

-----Original Message----- From: Michal Privoznik [mailto:mprivozn@redhat.com] Sent: Thursday, July 24, 2014 5:00 PM To: Chen, Hanxiao/陈 晗霄; libvir-list@redhat.com Subject: Re: [libvirt] [PATCH 2/2] LXC: use lxcDomainSetMemoryFlags to do lxcDomainSetMaxMemory's work
On 16.07.2014 11:51, Chen Hanxiao wrote:
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- src/lxc/lxc_driver.c | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-)
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index be6ee19..9f974eb 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -680,37 +680,6 @@ lxcDomainGetMaxMemory(virDomainPtr dom) return ret; }
-static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) -{ - virDomainObjPtr vm; - int ret = -1; - - if (!(vm = lxcDomObjFromDomain(dom))) - goto cleanup; - - if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0) - goto cleanup; - - if (newmax < vm->def->mem.cur_balloon) { - if (!virDomainObjIsActive(vm)) { - vm->def->mem.cur_balloon = newmax; - } else { - virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("Cannot set max memory lower than current" - " memory for an active domain")); - goto cleanup; - } - } - - vm->def->mem.max_balloon = newmax; - ret = 0; - - cleanup: - if (vm) - virObjectUnlock(vm); - return ret; -} -
A-ha! This is what I was looking for in 1/2. Okay, but I'd rather note this fact in 1/2 commit message to make it more obvious.
Will update commit message in v2.
static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, unsigned int flags) { @@ -809,6 +778,11 @@ static int lxcDomainSetMemory(virDomainPtr dom, unsigned long newmem) return lxcDomainSetMemoryFlags(dom, newmem, VIR_DOMAIN_AFFECT_LIVE); }
+static int lxcDomainSetMaxMemory(virDomainPtr dom, unsigned long memory) +{ + return lxcDomainSetMemoryFlags(dom, memory, VIR_DOMAIN_MEM_MAXIMUM); +} +
So previously, calling virDomainSetMaxMemory() on an inactive LXC domain would succeed. Now, after the change, due to problem with _CURRENT, _LIVE and _CONFIG this will basically return success, but without any effect on the domain config. And that's wrong.
Current lxcDomainSetMaxMemory did: inactive: affect domain config active: success but no change So we should fix that and will do in v2. Thanks, - Chen
static int lxcDomainSetMemoryParameters(virDomainPtr dom, virTypedParameterPtr params,
Moreover, I think these two patches can be joined into one.
Michal

ping
-----Original Message----- From: libvir-list-bounces@redhat.com [mailto:libvir-list-bounces@redhat.com] On Behalf Of Chen Hanxiao Sent: Wednesday, July 16, 2014 5:51 PM To: libvir-list@redhat.com Subject: [libvirt] [PATCH 0/2] add support for --config in setmaxmem command
Chen Hanxiao (2): LXC: add support for --config in setmaxmem command LXC: use lxcDomainSetMemoryFlags to do lxcDomainSetMaxMemory's work
src/lxc/lxc_driver.c | 105 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 54 deletions(-)
-- 1.9.0
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (3)
-
Chen Hanxiao
-
chenhanxiao@cn.fujitsu.com
-
Michal Privoznik