core/cgroup: for non-cached attrs, don't return ENODATA blindly
authorMike Yuan <me@yhndnzj.com>
Fri, 1 Dec 2023 19:06:16 +0000 (03:06 +0800)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sat, 2 Dec 2023 00:13:46 +0000 (00:13 +0000)
Follow-up for f17b07f4d72238da95312920dcc2ad076568cba3

Hope I won't break this thing again...

src/core/cgroup.c

index e1bf90d..61ac4df 100644 (file)
@@ -4065,7 +4065,7 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin
 
         if (!u->cgroup_path)
                 /* If the cgroup is already gone, we try to find the last cached value. */
-                goto cache;
+                goto finish;
 
         /* The root cgroup doesn't expose this information. */
         if (unit_has_host_root_cgroup(u))
@@ -4085,17 +4085,18 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin
                 return r;
         updated = r >= 0;
 
-cache:
-        if (metric > _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST)
-                return -ENODATA;
+finish:
+        if (metric <= _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST) {
+                uint64_t *last = &u->memory_accounting_last[metric];
 
-        uint64_t *last = &u->memory_accounting_last[metric];
+                if (updated)
+                        *last = bytes;
+                else if (*last != UINT64_MAX)
+                        bytes = *last;
+                else
+                        return -ENODATA;
 
-        if (updated)
-                *last = bytes;
-        else if (*last != UINT64_MAX)
-                bytes = *last;
-        else
+        } else if (!updated)
                 return -ENODATA;
 
         if (ret)