923a60
From cbeadf0e57c0d240977d574a8b5726b441f519a9 Mon Sep 17 00:00:00 2001
923a60
From: Jan Rybar <jrybar@redhat.com>
923a60
Date: Fri, 22 Sep 2017 11:53:50 +0200
923a60
Subject: [PATCH] cgroup resource property setting ignored if einval
923a60
923a60
Resolves: rhbz#1302305
923a60
Cherry-picked from: d53d94743c5e5e3a4a6, 3fdf9ad
923a60
---
923a60
 man/systemd.resource-control.xml |   9 ++-
923a60
 src/core/cgroup.c                |  54 ++++++-------
923a60
 src/core/cgroup.h                |  14 ++--
923a60
 src/core/dbus-cgroup.c           | 125 ++++++++++++-------------------
923a60
 src/core/load-fragment.c         |  54 +++++--------
923a60
 src/core/unit.c                  |   4 +-
923a60
 src/libsystemd/sd-bus/bus-util.c |  35 ++++++---
923a60
 src/shared/cgroup-util.c         |  41 ++++++++++
923a60
 src/shared/cgroup-util.h         |  27 +++++++
923a60
 9 files changed, 205 insertions(+), 158 deletions(-)
923a60
923a60
diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
923a60
index f507c67487..6ab17e8cb9 100644
923a60
--- a/man/systemd.resource-control.xml
923a60
+++ b/man/systemd.resource-control.xml
923a60
@@ -118,10 +118,11 @@
923a60
 
923a60
         <listitem>
923a60
           <para>Assign the specified CPU time share weight to the
923a60
-          processes executed. Those options take an integer value and
923a60
+          processes executed. These options take an integer value and
923a60
           control the <literal>cpu.shares</literal> control group
923a60
-          attribute, which defaults to 1024. For details about this
923a60
-          control group attribute, see 
923a60
+          attribute. The allowed range is 2 to 262144. Defaults to
923a60
+          1024. For details about this control group attribute, see
923a60
+          
923a60
           url="https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt">sched-design-CFS.txt</ulink>.
923a60
           The available CPU time is split up among all units within
923a60
           one slice relative to their CPU time share weight.</para>
923a60
@@ -258,7 +259,7 @@
923a60
         the executed processes. Takes a single weight value (between
923a60
         10 and 1000) to set the default block IO weight. This controls
923a60
         the <literal>blkio.weight</literal> control group attribute,
923a60
-        which defaults to 1000. For details about this control group
923a60
+        which defaults to 500. For details about this control group
923a60
         attribute, see 
923a60
         url="https://www.kernel.org/doc/Documentation/cgroups/blkio-controller.txt">blkio-controller.txt</ulink>.
923a60
         The available IO bandwidth is split up among all units within
923a60
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
923a60
index d4a8f9cbe3..0779fa5552 100644
923a60
--- a/src/core/cgroup.c
923a60
+++ b/src/core/cgroup.c
923a60
@@ -35,14 +35,16 @@ void cgroup_context_init(CGroupContext *c) {
923a60
         /* Initialize everything to the kernel defaults, assuming the
923a60
          * structure is preinitialized to 0 */
923a60
 
923a60
-        c->cpu_shares = (unsigned long) -1;
923a60
-        c->startup_cpu_shares = (unsigned long) -1;
923a60
+        c->cpu_shares = CGROUP_CPU_SHARES_INVALID;
923a60
+        c->startup_cpu_shares = CGROUP_CPU_SHARES_INVALID;
923a60
+        c->cpu_quota_per_sec_usec = USEC_INFINITY;
923a60
+
923a60
         c->memory_limit = (uint64_t) -1;
923a60
-        c->blockio_weight = (unsigned long) -1;
923a60
-        c->startup_blockio_weight = (unsigned long) -1;
923a60
-        c->tasks_max = (uint64_t) -1;
923a60
 
923a60
-        c->cpu_quota_per_sec_usec = USEC_INFINITY;
923a60
+        c->blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID;
923a60
+        c->startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID;
923a60
+
923a60
+        c->tasks_max = (uint64_t) -1;
923a60
 }
923a60
 
923a60
 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
923a60
@@ -100,11 +102,12 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
923a60
                 "%sCPUAccounting=%s\n"
923a60
                 "%sBlockIOAccounting=%s\n"
923a60
                 "%sMemoryAccounting=%s\n"
923a60
-                "%sCPUShares=%lu\n"
923a60
-                "%sStartupCPUShares=%lu\n"
923a60
+                "%sTasksAccounting=%s\n"
923a60
+                "%sCPUShares=%" PRIu64 "\n"
923a60
+                "%sStartupCPUShares=%" PRIu64 "\n"
923a60
                 "%sCPUQuotaPerSecSec=%s\n"
923a60
-                "%sBlockIOWeight=%lu\n"
923a60
-                "%sStartupBlockIOWeight=%lu\n"
923a60
+                "%sBlockIOWeight=%" PRIu64 "\n"
923a60
+                "%sStartupBlockIOWeight=%" PRIu64 "\n"
923a60
                 "%sMemoryLimit=%" PRIu64 "\n"
923a60
                 "%sTasksMax=%" PRIu64 "\n"
923a60
                 "%sDevicePolicy=%s\n"
923a60
@@ -112,6 +115,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
923a60
                 prefix, yes_no(c->cpu_accounting),
923a60
                 prefix, yes_no(c->blockio_accounting),
923a60
                 prefix, yes_no(c->memory_accounting),
923a60
+                prefix, yes_no(c->tasks_accounting),
923a60
                 prefix, c->cpu_shares,
923a60
                 prefix, c->startup_cpu_shares,
923a60
                 prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
923a60
@@ -131,7 +135,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
923a60
 
923a60
         LIST_FOREACH(device_weights, w, c->blockio_device_weights)
923a60
                 fprintf(f,
923a60
-                        "%sBlockIODeviceWeight=%s %lu",
923a60
+                        "%sBlockIODeviceWeight=%s %" PRIu64,
923a60
                         prefix,
923a60
                         w->path,
923a60
                         w->weight);
923a60
@@ -307,11 +311,11 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
923a60
          * and missing cgroups, i.e. EROFS and ENOENT. */
923a60
 
923a60
         if ((mask & CGROUP_CPU) && !is_root) {
923a60
-                char buf[MAX(DECIMAL_STR_MAX(unsigned long), DECIMAL_STR_MAX(usec_t)) + 1];
923a60
+                char buf[MAX(DECIMAL_STR_MAX(uint64_t), DECIMAL_STR_MAX(usec_t)) + 1];
923a60
 
923a60
-                sprintf(buf, "%lu\n",
923a60
-                        IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_cpu_shares != (unsigned long) -1 ? c->startup_cpu_shares :
923a60
-                        c->cpu_shares != (unsigned long) -1 ? c->cpu_shares : 1024);
923a60
+                sprintf(buf, "%" PRIu64 "\n",
923a60
+                        IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->startup_cpu_shares :
923a60
+                        c->cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->cpu_shares : CGROUP_CPU_SHARES_DEFAULT);
923a60
                 r = cg_set_attribute("cpu", path, "cpu.shares", buf);
923a60
                 if (r < 0)
923a60
                         log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
923a60
@@ -334,15 +338,15 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
923a60
         }
923a60
 
923a60
         if (mask & CGROUP_BLKIO) {
923a60
-                char buf[MAX3(DECIMAL_STR_MAX(unsigned long)+1,
923a60
-                              DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(unsigned long)*1,
923a60
-                              DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1)];
923a60
+                char buf[MAX(DECIMAL_STR_MAX(uint64_t)+1,
923a60
+                             DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1)];
923a60
                 CGroupBlockIODeviceWeight *w;
923a60
                 CGroupBlockIODeviceBandwidth *b;
923a60
 
923a60
                 if (!is_root) {
923a60
-                        sprintf(buf, "%lu\n", IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_blockio_weight != (unsigned long) -1 ? c->startup_blockio_weight :
923a60
-                                c->blockio_weight != (unsigned long) -1 ? c->blockio_weight : 1000);
923a60
+                        sprintf(buf, "%" PRIu64 "\n",
923a60
+                                IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ? c->startup_blockio_weight :
923a60
+                                c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ? c->blockio_weight : CGROUP_BLKIO_WEIGHT_DEFAULT);
923a60
                         r = cg_set_attribute("blkio", path, "blkio.weight", buf);
923a60
                         if (r < 0)
923a60
                                 log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
923a60
@@ -356,7 +360,7 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
923a60
                                 if (r < 0)
923a60
                                         continue;
923a60
 
923a60
-                                sprintf(buf, "%u:%u %lu", major(dev), minor(dev), w->weight);
923a60
+                                sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), w->weight);
923a60
                                 r = cg_set_attribute("blkio", path, "blkio.weight_device", buf);
923a60
                                 if (r < 0)
923a60
                                         log_full_errno(IN_SET(r, -ENOENT, -EROFS) ? LOG_DEBUG : LOG_WARNING, r,
923a60
@@ -482,14 +486,14 @@ CGroupControllerMask cgroup_context_get_mask(CGroupContext *c) {
923a60
         /* Figure out which controllers we need */
923a60
 
923a60
         if (c->cpu_accounting ||
923a60
-            c->cpu_shares != (unsigned long) -1 ||
923a60
-            c->startup_cpu_shares != (unsigned long) -1 ||
923a60
+            c->cpu_shares != CGROUP_CPU_SHARES_INVALID ||
923a60
+            c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID ||
923a60
             c->cpu_quota_per_sec_usec != USEC_INFINITY)
923a60
                 mask |= CGROUP_CPUACCT | CGROUP_CPU;
923a60
 
923a60
         if (c->blockio_accounting ||
923a60
-            c->blockio_weight != (unsigned long) -1 ||
923a60
-            c->startup_blockio_weight != (unsigned long) -1 ||
923a60
+            c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
923a60
+            c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
923a60
             c->blockio_device_weights ||
923a60
             c->blockio_device_bandwidths)
923a60
                 mask |= CGROUP_BLKIO;
923a60
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
923a60
index 8af3eaa3ae..870f39c520 100644
923a60
--- a/src/core/cgroup.h
923a60
+++ b/src/core/cgroup.h
923a60
@@ -58,7 +58,7 @@ struct CGroupDeviceAllow {
923a60
 struct CGroupBlockIODeviceWeight {
923a60
         LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
923a60
         char *path;
923a60
-        unsigned long weight;
923a60
+        uint64_t weight;
923a60
 };
923a60
 
923a60
 struct CGroupBlockIODeviceBandwidth {
923a60
@@ -74,12 +74,12 @@ struct CGroupContext {
923a60
         bool memory_accounting;
923a60
         bool tasks_accounting;
923a60
 
923a60
-        unsigned long cpu_shares;
923a60
-        unsigned long startup_cpu_shares;
923a60
+        uint64_t cpu_shares;
923a60
+        uint64_t startup_cpu_shares;
923a60
         usec_t cpu_quota_per_sec_usec;
923a60
 
923a60
-        unsigned long blockio_weight;
923a60
-        unsigned long startup_blockio_weight;
923a60
+        uint64_t blockio_weight;
923a60
+        uint64_t startup_blockio_weight;
923a60
         LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
923a60
         LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
923a60
 
923a60
@@ -88,9 +88,9 @@ struct CGroupContext {
923a60
         CGroupDevicePolicy device_policy;
923a60
         LIST_HEAD(CGroupDeviceAllow, device_allow);
923a60
 
923a60
-        bool delegate;
923a60
-
923a60
         uint64_t tasks_max;
923a60
+
923a60
+        bool delegate;
923a60
 };
923a60
 
923a60
 #include "unit.h"
923a60
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
923a60
index fa76c60c1f..ffeeb5aa9a 100644
923a60
--- a/src/core/dbus-cgroup.c
923a60
+++ b/src/core/dbus-cgroup.c
923a60
@@ -133,34 +133,16 @@ static int property_get_device_allow(
923a60
         return sd_bus_message_close_container(reply);
923a60
 }
923a60
 
923a60
-static int property_get_ulong_as_u64(
923a60
-                sd_bus *bus,
923a60
-                const char *path,
923a60
-                const char *interface,
923a60
-                const char *property,
923a60
-                sd_bus_message *reply,
923a60
-                void *userdata,
923a60
-                sd_bus_error *error) {
923a60
-
923a60
-        unsigned long *ul = userdata;
923a60
-
923a60
-        assert(bus);
923a60
-        assert(reply);
923a60
-        assert(ul);
923a60
-
923a60
-        return sd_bus_message_append(reply, "t", *ul == (unsigned long) -1 ? (uint64_t) -1 : (uint64_t) *ul);
923a60
-}
923a60
-
923a60
 const sd_bus_vtable bus_cgroup_vtable[] = {
923a60
         SD_BUS_VTABLE_START(0),
923a60
         SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool, offsetof(CGroupContext, delegate), 0),
923a60
         SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, cpu_accounting), 0),
923a60
-        SD_BUS_PROPERTY("CPUShares", "t", property_get_ulong_as_u64, offsetof(CGroupContext, cpu_shares), 0),
923a60
-        SD_BUS_PROPERTY("StartupCPUShares", "t", property_get_ulong_as_u64, offsetof(CGroupContext, startup_cpu_shares), 0),
923a60
+        SD_BUS_PROPERTY("CPUShares", "t", NULL, offsetof(CGroupContext, cpu_shares), 0),
923a60
+        SD_BUS_PROPERTY("StartupCPUShares", "t", NULL, offsetof(CGroupContext, startup_cpu_shares), 0),
923a60
         SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_per_sec_usec), 0),
923a60
         SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, blockio_accounting), 0),
923a60
-        SD_BUS_PROPERTY("BlockIOWeight", "t", property_get_ulong_as_u64, offsetof(CGroupContext, blockio_weight), 0),
923a60
-        SD_BUS_PROPERTY("StartupBlockIOWeight", "t", property_get_ulong_as_u64, offsetof(CGroupContext, startup_blockio_weight), 0),
923a60
+        SD_BUS_PROPERTY("BlockIOWeight", "t", NULL, offsetof(CGroupContext, blockio_weight), 0),
923a60
+        SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL, offsetof(CGroupContext, startup_blockio_weight), 0),
923a60
         SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight, 0, 0),
923a60
         SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
923a60
         SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
923a60
@@ -237,49 +219,45 @@ int bus_cgroup_set_property(
923a60
                 return 1;
923a60
 
923a60
         } else if (streq(name, "CPUShares")) {
923a60
-                uint64_t u64;
923a60
-                unsigned long ul;
923a60
+                uint64_t shares;
923a60
 
923a60
-                r = sd_bus_message_read(message, "t", &u64);
923a60
+                r = sd_bus_message_read(message, "t", &shares);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
-                if (u64 == (uint64_t) -1)
923a60
-                        ul = (unsigned long) -1;
923a60
-                else {
923a60
-                        ul = (unsigned long) u64;
923a60
-                        if (ul <= 0 || (uint64_t) ul != u64)
923a60
-                                return sd_bus_error_set_errnof(error, EINVAL, "CPUShares value out of range");
923a60
-                }
923a60
+                if (!CGROUP_CPU_SHARES_IS_OK(shares))
923a60
+                        return sd_bus_error_set_errnof(error, EINVAL, "CPUShares value out of range");
923a60
 
923a60
                 if (mode != UNIT_CHECK) {
923a60
-                        c->cpu_shares = ul;
923a60
+                        c->cpu_shares = shares;
923a60
                         u->cgroup_realized_mask &= ~CGROUP_CPU;
923a60
-                        unit_write_drop_in_private_format(u, mode, name, "CPUShares=%lu", ul);
923a60
+
923a60
+                        if (shares == CGROUP_CPU_SHARES_INVALID)
923a60
+                                unit_write_drop_in_private(u, mode, name, "CPUShares=");
923a60
+                        else
923a60
+                                unit_write_drop_in_private_format(u, mode, name, "CPUShares=%" PRIu64, shares);
923a60
                 }
923a60
 
923a60
                 return 1;
923a60
 
923a60
         } else if (streq(name, "StartupCPUShares")) {
923a60
-                uint64_t u64;
923a60
-                unsigned long ul;
923a60
+                uint64_t shares;
923a60
 
923a60
-                r = sd_bus_message_read(message, "t", &u64);
923a60
+                r = sd_bus_message_read(message, "t", &shares);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
-                if (u64 == (uint64_t) -1)
923a60
-                        ul = (unsigned long) -1;
923a60
-                else {
923a60
-                        ul = (unsigned long) u64;
923a60
-                        if (ul <= 0 || (uint64_t) ul != u64)
923a60
-                                return sd_bus_error_set_errnof(error, EINVAL, "StartupCPUShares value out of range");
923a60
-                }
923a60
+                if (!CGROUP_CPU_SHARES_IS_OK(shares))
923a60
+                        return sd_bus_error_set_errnof(error, EINVAL, "StartupCPUShares value out of range");
923a60
 
923a60
                 if (mode != UNIT_CHECK) {
923a60
-                        c->startup_cpu_shares = ul;
923a60
+                        c->startup_cpu_shares = shares;
923a60
                         u->cgroup_realized_mask &= ~CGROUP_CPU;
923a60
-                        unit_write_drop_in_private_format(u, mode, name, "StartupCPUShares=%lu", ul);
923a60
+
923a60
+                        if (shares == CGROUP_CPU_SHARES_INVALID)
923a60
+                                unit_write_drop_in_private(u, mode, name, "StartupCPUShares=");
923a60
+                        else
923a60
+                                unit_write_drop_in_private_format(u, mode, name, "StartupCPUShares=%" PRIu64, shares);
923a60
                 }
923a60
 
923a60
                 return 1;
923a60
@@ -318,49 +296,45 @@ int bus_cgroup_set_property(
923a60
                 return 1;
923a60
 
923a60
         } else if (streq(name, "BlockIOWeight")) {
923a60
-                uint64_t u64;
923a60
-                unsigned long ul;
923a60
+                uint64_t weight;
923a60
 
923a60
-                r = sd_bus_message_read(message, "t", &u64);
923a60
+                r = sd_bus_message_read(message, "t", &weight);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
-                if (u64 == (uint64_t) -1)
923a60
-                        ul = (unsigned long) -1;
923a60
-                else  {
923a60
-                        ul = (unsigned long) u64;
923a60
-                        if (ul < 10 || ul > 1000)
923a60
-                                return sd_bus_error_set_errnof(error, EINVAL, "BlockIOWeight value out of range");
923a60
-                }
923a60
+                if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight))
923a60
+                        return sd_bus_error_set_errnof(error, EINVAL, "BlockIOWeight value out of range");
923a60
 
923a60
                 if (mode != UNIT_CHECK) {
923a60
-                        c->blockio_weight = ul;
923a60
+                        c->blockio_weight = weight;
923a60
                         u->cgroup_realized_mask &= ~CGROUP_BLKIO;
923a60
-                        unit_write_drop_in_private_format(u, mode, name, "BlockIOWeight=%lu", ul);
923a60
+
923a60
+                        if (weight == CGROUP_BLKIO_WEIGHT_INVALID)
923a60
+                                unit_write_drop_in_private(u, mode, name, "BlockIOWeight=");
923a60
+                        else
923a60
+                                unit_write_drop_in_private_format(u, mode, name, "BlockIOWeight=%" PRIu64, weight);
923a60
                 }
923a60
 
923a60
                 return 1;
923a60
 
923a60
         } else if (streq(name, "StartupBlockIOWeight")) {
923a60
-                uint64_t u64;
923a60
-                unsigned long ul;
923a60
+                uint64_t weight;
923a60
 
923a60
-                r = sd_bus_message_read(message, "t", &u64);
923a60
+                r = sd_bus_message_read(message, "t", &weight);
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
-                if (u64 == (uint64_t) -1)
923a60
-                        ul = (unsigned long) -1;
923a60
-                else  {
923a60
-                        ul = (unsigned long) u64;
923a60
-                        if (ul < 10 || ul > 1000)
923a60
-                                return sd_bus_error_set_errnof(error, EINVAL, "StartupBlockIOWeight value out of range");
923a60
-                }
923a60
+                if (CGROUP_BLKIO_WEIGHT_IS_OK(weight))
923a60
+                        return sd_bus_error_set_errnof(error, EINVAL, "StartupBlockIOWeight value out of range");
923a60
 
923a60
                 if (mode != UNIT_CHECK) {
923a60
-                        c->startup_blockio_weight = ul;
923a60
+                        c->startup_blockio_weight = weight;
923a60
                         u->cgroup_realized_mask &= ~CGROUP_BLKIO;
923a60
-                        unit_write_drop_in_private_format(u, mode, name, "StartupBlockIOWeight=%lu", ul);
923a60
+
923a60
+                        if (weight == CGROUP_BLKIO_WEIGHT_INVALID)
923a60
+                                unit_write_drop_in_private(u, mode, name, "StartupBlockIOWeight=");
923a60
+                        else
923a60
+                                unit_write_drop_in_private_format(u, mode, name, "StartupBlockIOWeight=%" PRIu64, weight);
923a60
                 }
923a60
 
923a60
                 return 1;
923a60
@@ -455,17 +429,16 @@ int bus_cgroup_set_property(
923a60
 
923a60
         } else if (streq(name, "BlockIODeviceWeight")) {
923a60
                 const char *path;
923a60
-                uint64_t u64;
923a60
+                uint64_t weight;
923a60
                 unsigned n = 0;
923a60
 
923a60
                 r = sd_bus_message_enter_container(message, 'a', "(st)");
923a60
                 if (r < 0)
923a60
                         return r;
923a60
 
923a60
-                while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
923a60
-                        unsigned long ul = u64;
923a60
+                while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
923a60
 
923a60
-                        if (ul < 10 || ul > 1000)
923a60
+                        if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight) || weight == CGROUP_BLKIO_WEIGHT_INVALID)
923a60
                                 return sd_bus_error_set_errnof(error, EINVAL, "BlockIODeviceWeight out of range");
923a60
 
923a60
                         if (mode != UNIT_CHECK) {
923a60
@@ -491,7 +464,7 @@ int bus_cgroup_set_property(
923a60
                                         LIST_PREPEND(device_weights,c->blockio_device_weights, a);
923a60
                                 }
923a60
 
923a60
-                                a->weight = ul;
923a60
+                                a->weight = weight;
923a60
                         }
923a60
 
923a60
                         n++;
923a60
@@ -520,7 +493,7 @@ int bus_cgroup_set_property(
923a60
 
923a60
                         fputs("BlockIODeviceWeight=\n", f);
923a60
                         LIST_FOREACH(device_weights, a, c->blockio_device_weights)
923a60
-                                fprintf(f, "BlockIODeviceWeight=%s %lu\n", a->path, a->weight);
923a60
+                                fprintf(f, "BlockIODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
923a60
 
923a60
                         fflush(f);
923a60
                         unit_write_drop_in_private(u, mode, name, buf);
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index a10e1903a4..da58bcc5c9 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -2951,26 +2951,19 @@ int config_parse_cpu_shares(
923a60
                 void *data,
923a60
                 void *userdata) {
923a60
 
923a60
-        unsigned long *shares = data, lu;
923a60
+        uint64_t *shares = data;
923a60
         int r;
923a60
 
923a60
         assert(filename);
923a60
         assert(lvalue);
923a60
         assert(rvalue);
923a60
 
923a60
-        if (isempty(rvalue)) {
923a60
-                *shares = (unsigned long) -1;
923a60
-                return 0;
923a60
-        }
923a60
-
923a60
-        r = safe_atolu(rvalue, &lu);
923a60
-        if (r < 0 || lu <= 0) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
923a60
-                           "CPU shares '%s' invalid. Ignoring.", rvalue);
923a60
+        r = cg_cpu_shares_parse(rvalue, shares);
923a60
+        if (r < 0) {
923a60
+                log_syntax(unit, LOG_ERR, filename, line, r, "CPU shares '%s' invalid. Ignoring.", rvalue);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        *shares = lu;
923a60
         return 0;
923a60
 }
923a60
 
923a60
@@ -3163,26 +3156,19 @@ int config_parse_blockio_weight(
923a60
                 void *data,
923a60
                 void *userdata) {
923a60
 
923a60
-        unsigned long *weight = data, lu;
923a60
+        uint64_t *weight = data;
923a60
         int r;
923a60
 
923a60
         assert(filename);
923a60
         assert(lvalue);
923a60
         assert(rvalue);
923a60
 
923a60
-        if (isempty(rvalue)) {
923a60
-                *weight = (unsigned long) -1;
923a60
-                return 0;
923a60
-        }
923a60
-
923a60
-        r = safe_atolu(rvalue, &lu);
923a60
-        if (r < 0 || lu < 10 || lu > 1000) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
923a60
-                           "Block IO weight '%s' invalid. Ignoring.", rvalue);
923a60
+        r = cg_blkio_weight_parse(rvalue, weight);
923a60
+        if (r < 0) {
923a60
+                log_syntax(unit, LOG_ERR, filename, line, r, "Block IO weight '%s' invalid. Ignoring.", rvalue);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        *weight = lu;
923a60
         return 0;
923a60
 }
923a60
 
923a60
@@ -3201,8 +3187,8 @@ int config_parse_blockio_device_weight(
923a60
         _cleanup_free_ char *path = NULL;
923a60
         CGroupBlockIODeviceWeight *w;
923a60
         CGroupContext *c = data;
923a60
-        unsigned long lu;
923a60
         const char *weight;
923a60
+        uint64_t u;
923a60
         size_t n;
923a60
         int r;
923a60
 
923a60
@@ -3219,9 +3205,10 @@ int config_parse_blockio_device_weight(
923a60
 
923a60
         n = strcspn(rvalue, WHITESPACE);
923a60
         weight = rvalue + n;
923a60
-        if (!*weight) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
923a60
-                           "Expected block device and device weight. Ignoring.");
923a60
+        weight += strspn(weight, WHITESPACE);
923a60
+
923a60
+        if (isempty(weight)) {
923a60
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Expected block device and device weight. Ignoring.");
923a60
                 return 0;
923a60
         }
923a60
 
923a60
@@ -3230,19 +3217,18 @@ int config_parse_blockio_device_weight(
923a60
                 return log_oom();
923a60
 
923a60
         if (!path_startswith(path, "/dev")) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
923a60
-                           "Invalid device node path '%s'. Ignoring.", path);
923a60
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Invalid device node path '%s'. Ignoring.", path);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        weight += strspn(weight, WHITESPACE);
923a60
-        r = safe_atolu(weight, &lu);
923a60
-        if (r < 0 || lu < 10 || lu > 1000) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
923a60
-                           "Block IO weight '%s' invalid. Ignoring.", rvalue);
923a60
+        r = cg_blkio_weight_parse(weight, &u);
923a60
+        if (r < 0) {
923a60
+                log_syntax(unit, LOG_ERR, filename, line, r, "Block IO weight '%s' invalid. Ignoring.", weight);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
+        assert(u != CGROUP_BLKIO_WEIGHT_INVALID);
923a60
+
923a60
         w = new0(CGroupBlockIODeviceWeight, 1);
923a60
         if (!w)
923a60
                 return log_oom();
923a60
@@ -3250,7 +3236,7 @@ int config_parse_blockio_device_weight(
923a60
         w->path = path;
923a60
         path = NULL;
923a60
 
923a60
-        w->weight = lu;
923a60
+        w->weight = u;
923a60
 
923a60
         LIST_PREPEND(device_weights, c->blockio_device_weights, w);
923a60
         return 0;
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index 6a2ad6ed38..8c0fde8784 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -1178,8 +1178,8 @@ static int unit_add_startup_units(Unit *u) {
923a60
         if (!c)
923a60
                 return 0;
923a60
 
923a60
-        if (c->startup_cpu_shares == (unsigned long) -1 &&
923a60
-            c->startup_blockio_weight == (unsigned long) -1)
923a60
+        if (c->startup_cpu_shares == CGROUP_CPU_SHARES_INVALID &&
923a60
+            c->startup_blockio_weight == CGROUP_BLKIO_WEIGHT_INVALID)
923a60
                 return 0;
923a60
 
923a60
         r = set_put(u->manager->startup_units, u);
923a60
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
923a60
index 2634574279..cbf1eccf77 100644
923a60
--- a/src/libsystemd/sd-bus/bus-util.c
923a60
+++ b/src/libsystemd/sd-bus/bus-util.c
923a60
@@ -23,20 +23,23 @@
923a60
 
923a60
 #include "sd-daemon.h"
923a60
 #include "sd-event.h"
923a60
-#include "util.h"
923a60
-#include "strv.h"
923a60
-#include "macro.h"
923a60
+#include "sd-bus.h"
923a60
+
923a60
+#include "bus-error.h"
923a60
+#include "bus-internal.h"
923a60
+#include "bus-label.h"
923a60
+#include "bus-message.h"
923a60
+#include "cgroup-util.h"
923a60
 #include "def.h"
923a60
-#include "path-util.h"
923a60
+#include "macro.h"
923a60
 #include "missing.h"
923a60
+#include "path-util.h"
923a60
 #include "set.h"
923a60
+#include "strv.h"
923a60
 #include "unit-name.h"
923a60
+#include "util.h"
923a60
 
923a60
-#include "sd-bus.h"
923a60
-#include "bus-error.h"
923a60
-#include "bus-message.h"
923a60
 #include "bus-util.h"
923a60
-#include "bus-internal.h"
923a60
 
923a60
 static int name_owner_change_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
923a60
         sd_event *e = userdata;
923a60
@@ -1429,10 +1432,22 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
923a60
                 }
923a60
 
923a60
                 r = sd_bus_message_append(m, "sv", "TasksMax", "t", t);
923a60
-        } else if (STR_IN_SET(field, "CPUShares", "BlockIOWeight")) {
923a60
+
923a60
+        } else if (STR_IN_SET(field, "CPUShares", "StartupCPUShares")) {
923a60
+                uint64_t u;
923a60
+
923a60
+                r = cg_cpu_shares_parse(eq, &u);
923a60
+                if (r < 0) {
923a60
+                        log_error("Failed to parse %s value %s.", field, eq);
923a60
+                        return -EINVAL;
923a60
+                }
923a60
+
923a60
+                r = sd_bus_message_append(m, "v", "t", u);
923a60
+
923a60
+        } else if (STR_IN_SET(field, "BlockIOWeight", "StartupBlockIOWeight")) {
923a60
                 uint64_t u;
923a60
 
923a60
-                r = safe_atou64(eq, &u);
923a60
+                r = cg_blkio_weight_parse(eq, &u);
923a60
                 if (r < 0) {
923a60
                         log_error("Failed to parse %s value %s.", field, eq);
923a60
                         return -EINVAL;
923a60
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
923a60
index c5d9e4bb58..f67b53b4de 100644
923a60
--- a/src/shared/cgroup-util.c
923a60
+++ b/src/shared/cgroup-util.c
923a60
@@ -1795,3 +1795,44 @@ int cg_kernel_controllers(Set *controllers) {
923a60
 
923a60
         return 0;
923a60
 }
923a60
+
923a60
+
923a60
+int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
923a60
+        uint64_t u;
923a60
+        int r;
923a60
+
923a60
+        if (isempty(s)) {
923a60
+                *ret = CGROUP_CPU_SHARES_INVALID;
923a60
+                return 0;
923a60
+        }
923a60
+
923a60
+        r = safe_atou64(s, &u);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
923a60
+                return -ERANGE;
923a60
+
923a60
+        *ret = u;
923a60
+        return 0;
923a60
+}
923a60
+
923a60
+int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
923a60
+        uint64_t u;
923a60
+        int r;
923a60
+
923a60
+        if (isempty(s)) {
923a60
+                *ret = CGROUP_BLKIO_WEIGHT_INVALID;
923a60
+                return 0;
923a60
+        }
923a60
+
923a60
+        r = safe_atou64(s, &u);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
923a60
+                return -ERANGE;
923a60
+
923a60
+        *ret = u;
923a60
+        return 0;
923a60
+}
923a60
diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h
923a60
index 31bd8d311f..615c1f03e3 100644
923a60
--- a/src/shared/cgroup-util.h
923a60
+++ b/src/shared/cgroup-util.h
923a60
@@ -39,6 +39,30 @@ typedef enum CGroupControllerMask {
923a60
         _CGROUP_CONTROLLER_MASK_ALL = 31
923a60
 } CGroupControllerMask;
923a60
 
923a60
+/* Special values for the cpu.shares attribute */
923a60
+#define CGROUP_CPU_SHARES_INVALID ((uint64_t) -1)
923a60
+#define CGROUP_CPU_SHARES_MIN UINT64_C(2)
923a60
+#define CGROUP_CPU_SHARES_MAX UINT64_C(262144)
923a60
+#define CGROUP_CPU_SHARES_DEFAULT UINT64_C(1024)
923a60
+
923a60
+static inline bool CGROUP_CPU_SHARES_IS_OK(uint64_t x) {
923a60
+        return
923a60
+            x == CGROUP_CPU_SHARES_INVALID ||
923a60
+            (x >= CGROUP_CPU_SHARES_MIN && x <= CGROUP_CPU_SHARES_MAX);
923a60
+}
923a60
+
923a60
+/* Special values for the blkio.weight attribute */
923a60
+#define CGROUP_BLKIO_WEIGHT_INVALID ((uint64_t) -1)
923a60
+#define CGROUP_BLKIO_WEIGHT_MIN UINT64_C(10)
923a60
+#define CGROUP_BLKIO_WEIGHT_MAX UINT64_C(1000)
923a60
+#define CGROUP_BLKIO_WEIGHT_DEFAULT UINT64_C(500)
923a60
+
923a60
+static inline bool CGROUP_BLKIO_WEIGHT_IS_OK(uint64_t x) {
923a60
+        return
923a60
+            x == CGROUP_BLKIO_WEIGHT_INVALID ||
923a60
+            (x >= CGROUP_BLKIO_WEIGHT_MIN && x <= CGROUP_BLKIO_WEIGHT_MAX);
923a60
+}
923a60
+
923a60
 /*
923a60
  * General rules:
923a60
  *
923a60
@@ -136,3 +160,6 @@ int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool de
923a60
 CGroupControllerMask cg_mask_supported(void);
923a60
 
923a60
 int cg_kernel_controllers(Set *controllers);
923a60
+
923a60
+int cg_cpu_shares_parse(const char *s, uint64_t *ret);
923a60
+int cg_blkio_weight_parse(const char *s, uint64_t *ret);