I submitted bug https://bugzilla.redhat.com/show_bug.cgi?id=1038363 for being unable to raise the persistent mem/vcpu values above a live domain’s maxmem/maxvcpu values (rather than the persistent maxmem/maxvcpu values). I was asked to submit my patch here for a wider review.

For memory, check the newmem value against mem.max_baloon of the live def for non-persistent changes, or mem.max_baloon of the persistent def for persistent changes. Same theory for setting vcpus/maxvcpus.

--- a/libvirt0/libvirt-1.1.1/src/qemu/qemu_driver.c
+++ b/libvirt0/libvirt-1.1.1/src/qemu/qemu_driver.c
@@ -2236,13 +2236,13 @@ static int qemuDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
     } else {
         /* resize the current memory */
 
-        if (newmem > vm->def->mem.max_balloon) {
-            virReportError(VIR_ERR_INVALID_ARG, "%s",
-                           _("cannot set memory higher than max memory"));
-            goto endjob;
-        }
-
         if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+            if (newmem > vm->def->mem.max_balloon) {
+                virReportError(VIR_ERR_INVALID_ARG, "%s",
+                    _("cannot set memory higher than max memory"));
+                goto endjob;
+            }
+
             priv = vm->privateData;
             qemuDomainObjEnterMonitor(driver, vm);
             r = qemuMonitorSetBalloon(priv->mon, newmem);
@@ -2262,6 +2262,12 @@ static int qemuDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem,
         }
 
         if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+            if (newmem > persistentDef->mem.max_balloon) {
+                virReportError(VIR_ERR_INVALID_ARG, "%s",
+                    _("cannot set memory higher than max memory"));
+                goto endjob;
+            }
+
             sa_assert(persistentDef);
             persistentDef->mem.cur_balloon = newmem;
             ret = virDomainSaveConfig(cfg->configDir, persistentDef);
@@ -4160,6 +4166,7 @@ qemuDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
     virDomainDefPtr persistentDef;
     int ret = -1;
     bool maximum;
+    unsigned int maxvcpus;
     virQEMUDriverConfigPtr cfg = NULL;
     virCapsPtr caps = NULL;
     qemuAgentCPUInfoPtr cpuinfo = NULL;
@@ -4207,11 +4214,12 @@ qemuDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
         goto endjob;
     }
 
-    if (!maximum && nvcpus > vm->def->maxvcpus) {
+    maxvcpus = (flags & VIR_DOMAIN_AFFECT_LIVE) ? vm->def->maxvcpus : persistentDef->maxvcpus;
+    if (!maximum && nvcpus > maxvcpus) {
         virReportError(VIR_ERR_INVALID_ARG,
                        _("requested vcpus is greater than max allowable"
                          " vcpus for the domain: %d > %d"),
-                       nvcpus, vm->def->maxvcpus);
+                       nvcpus, maxvcpus);
         goto endjob;
     }