render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
397dc2
From c82c32f60579d148f37064e5156e857fa3c84c2f Mon Sep 17 00:00:00 2001
397dc2
Message-Id: <c82c32f60579d148f37064e5156e857fa3c84c2f@dist-git>
397dc2
From: Pavel Hrdina <phrdina@redhat.com>
397dc2
Date: Thu, 4 Mar 2021 12:57:57 +0100
397dc2
Subject: [PATCH] vircgroup: enforce range limit for cpu.shares
397dc2
MIME-Version: 1.0
397dc2
Content-Type: text/plain; charset=UTF-8
397dc2
Content-Transfer-Encoding: 8bit
397dc2
397dc2
Before the conversion to using systemd DBus API to set the cpu.shares
397dc2
there was some magic conversion done by kernel which was documented in
397dc2
virsh manpage as well. Now systemd errors out if the value is out of
397dc2
range.
397dc2
397dc2
Since we enforce the range for other cpu cgroup attributes 'quota' and
397dc2
'period' it makes sense to do the same for 'shares' as well.
397dc2
397dc2
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
397dc2
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
(cherry picked from commit 1d9d9961ada6c2d0b9facae0ef8be4f459cf7fc9)
397dc2
397dc2
Conflicts:
397dc2
    docs/formatdomain.rst
397dc2
    src/conf/domain_validate.c
397dc2
        - both are not present in downstream
397dc2
397dc2
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
397dc2
397dc2
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
397dc2
Message-Id: <79b9ef9f98b3ab35061f8c4e4acf7b6861d28055.1614858616.git.phrdina@redhat.com>
397dc2
Reviewed-by: Ján Tomko <jtomko@redhat.com>
397dc2
---
397dc2
 docs/formatdomain.html.in |  1 +
397dc2
 docs/manpages/virsh.rst   |  5 +----
397dc2
 src/conf/domain_conf.c    | 10 ++++++++++
397dc2
 src/util/vircgroup.h      |  2 ++
397dc2
 src/util/vircgroupv1.c    | 10 ++++++++++
397dc2
 src/util/vircgroupv2.c    | 10 ++++++++++
397dc2
 6 files changed, 34 insertions(+), 4 deletions(-)
397dc2
397dc2
diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
397dc2
index 4341e256a8..7ac9523684 100644
397dc2
--- a/docs/formatdomain.html.in
397dc2
+++ b/docs/formatdomain.html.in
397dc2
@@ -854,6 +854,7 @@
397dc2
         it's a relative measure based on the setting of other VM,
397dc2
         e.g. A VM configured with value
397dc2
         2048 will get twice as much CPU time as a VM configured with value 1024.
397dc2
+        The value should be in range [2, 262144].
397dc2
         Since 0.9.0
397dc2
       
397dc2
       
period
397dc2
diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
397dc2
index a5b95c1123..01e1c01912 100644
397dc2
--- a/docs/manpages/virsh.rst
397dc2
+++ b/docs/manpages/virsh.rst
397dc2
@@ -3704,10 +3704,7 @@ If *--live* is specified, set scheduler information of a running guest.
397dc2
 If *--config* is specified, affect the next boot of a persistent guest.
397dc2
 If *--current* is specified, affect the current guest state.
397dc2
 
397dc2
-``Note``: The cpu_shares parameter has a valid value range of 0-262144; Negative
397dc2
-values are wrapped to positive, and larger values are capped at the maximum.
397dc2
-Therefore, -1 is a useful shorthand for 262144. On the Linux kernel, the
397dc2
-values 0 and 1 are automatically converted to a minimal value of 2.
397dc2
+``Note``: The cpu_shares parameter has a valid value range of 2-262144.
397dc2
 
397dc2
 ``Note``: The weight and cap parameters are defined only for the
397dc2
 XEN_CREDIT scheduler.
397dc2
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
397dc2
index 9f6cdb0de8..444657c9a1 100644
397dc2
--- a/src/conf/domain_conf.c
397dc2
+++ b/src/conf/domain_conf.c
397dc2
@@ -7026,6 +7026,16 @@ virDomainDefLifecycleActionValidate(const virDomainDef *def)
397dc2
 static int
397dc2
 virDomainDefCputuneValidate(const virDomainDef *def)
397dc2
 {
397dc2
+    if (def->cputune.shares > 0 &&
397dc2
+        (def->cputune.shares < VIR_CGROUP_CPU_SHARES_MIN ||
397dc2
+         def->cputune.shares > VIR_CGROUP_CPU_SHARES_MAX)) {
397dc2
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
397dc2
+                       _("Value of cputune 'shares' must be in range [%llu, %llu]"),
397dc2
+                         VIR_CGROUP_CPU_SHARES_MIN,
397dc2
+                         VIR_CGROUP_CPU_SHARES_MAX);
397dc2
+        return -1;
397dc2
+    }
397dc2
+
397dc2
     CPUTUNE_VALIDATE_PERIOD(period);
397dc2
     CPUTUNE_VALIDATE_PERIOD(global_period);
397dc2
     CPUTUNE_VALIDATE_PERIOD(emulator_period);
397dc2
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
397dc2
index 1c6edea0be..938cfdfbe3 100644
397dc2
--- a/src/util/vircgroup.h
397dc2
+++ b/src/util/vircgroup.h
397dc2
@@ -243,6 +243,8 @@ virCgroupGetDomainTotalCpuStats(virCgroupPtr group,
397dc2
 int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares);
397dc2
 int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares);
397dc2
 
397dc2
+#define VIR_CGROUP_CPU_SHARES_MIN 2LL
397dc2
+#define VIR_CGROUP_CPU_SHARES_MAX 262144LL
397dc2
 #define VIR_CGROUP_CPU_PERIOD_MIN 1000LL
397dc2
 #define VIR_CGROUP_CPU_PERIOD_MAX 1000000LL
397dc2
 #define VIR_CGROUP_CPU_QUOTA_MIN 1000LL
397dc2
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
397dc2
index 49a2cb023e..d417446447 100644
397dc2
--- a/src/util/vircgroupv1.c
397dc2
+++ b/src/util/vircgroupv1.c
397dc2
@@ -1901,6 +1901,16 @@ static int
397dc2
 virCgroupV1SetCpuShares(virCgroupPtr group,
397dc2
                         unsigned long long shares)
397dc2
 {
397dc2
+    if (shares < VIR_CGROUP_CPU_SHARES_MIN ||
397dc2
+        shares > VIR_CGROUP_CPU_SHARES_MAX) {
397dc2
+        virReportError(VIR_ERR_INVALID_ARG,
397dc2
+                       _("shares '%llu' must be in range [%llu, %llu]"),
397dc2
+                       shares,
397dc2
+                       VIR_CGROUP_CPU_SHARES_MIN,
397dc2
+                       VIR_CGROUP_CPU_SHARES_MAX);
397dc2
+        return -1;
397dc2
+    }
397dc2
+
397dc2
     if (group->unitName) {
397dc2
         return virCgroupSetValueDBus(group->unitName, "CPUShares",
397dc2
                                      "t", shares);
397dc2
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
397dc2
index a14fc669fb..079fe6a8ec 100644
397dc2
--- a/src/util/vircgroupv2.c
397dc2
+++ b/src/util/vircgroupv2.c
397dc2
@@ -1499,6 +1499,16 @@ static int
397dc2
 virCgroupV2SetCpuShares(virCgroupPtr group,
397dc2
                         unsigned long long shares)
397dc2
 {
397dc2
+    if (shares < VIR_CGROUP_CPU_SHARES_MIN ||
397dc2
+        shares > VIR_CGROUP_CPU_SHARES_MAX) {
397dc2
+        virReportError(VIR_ERR_INVALID_ARG,
397dc2
+                       _("shares '%llu' must be in range [%llu, %llu]"),
397dc2
+                       shares,
397dc2
+                       VIR_CGROUP_CPU_SHARES_MIN,
397dc2
+                       VIR_CGROUP_CPU_SHARES_MAX);
397dc2
+        return -1;
397dc2
+    }
397dc2
+
397dc2
     if (group->unitName) {
397dc2
         return virCgroupSetValueDBus(group->unitName, "CPUWeight",
397dc2
                                      "t", shares);
397dc2
-- 
397dc2
2.30.0
397dc2