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