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