Blame SOURCES/0578-cgroup-resource-property-setting-ignored-if-einval.patch

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