f8452f
From a016ef4ab29ed62da547db008866624f75ed6407 Mon Sep 17 00:00:00 2001
f8452f
From: Chris Down <chris@chrisdown.name>
f8452f
Date: Tue, 16 Apr 2019 18:14:09 +0100
f8452f
Subject: [PATCH] cgroup: Create UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP
f8452f
f8452f
This is in preparation for creating unit_get_ancestor_memory_min.
f8452f
f8452f
(cherry picked from commit 6264b85e92aeddb74b8d8808a08c9eae8390a6a5)
f8452f
f8452f
Related: #1763435
f8452f
---
f8452f
 src/core/cgroup.c | 55 ++++++++++++++++++++++++++---------------------
f8452f
 1 file changed, 30 insertions(+), 25 deletions(-)
f8452f
f8452f
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
f8452f
index f804bf4727..46a89ff5e1 100644
f8452f
--- a/src/core/cgroup.c
f8452f
+++ b/src/core/cgroup.c
f8452f
@@ -372,31 +372,36 @@ int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode)
f8452f
         return 0;
f8452f
 }
f8452f
 
f8452f
-uint64_t unit_get_ancestor_memory_low(Unit *u) {
f8452f
-        CGroupContext *c;
f8452f
-
f8452f
-        /* 1. Is MemoryLow set in this unit? If so, use that.
f8452f
-         * 2. Is DefaultMemoryLow set in any ancestor? If so, use that.
f8452f
-         * 3. Otherwise, return CGROUP_LIMIT_MIN. */
f8452f
-
f8452f
-        assert(u);
f8452f
-
f8452f
-        c = unit_get_cgroup_context(u);
f8452f
-
f8452f
-        if (c->memory_low_set)
f8452f
-                return c->memory_low;
f8452f
-
f8452f
-        while (UNIT_ISSET(u->slice)) {
f8452f
-                u = UNIT_DEREF(u->slice);
f8452f
-                c = unit_get_cgroup_context(u);
f8452f
-
f8452f
-                if (c->default_memory_low_set)
f8452f
-                        return c->default_memory_low;
f8452f
-        }
f8452f
-
f8452f
-        /* We've reached the root, but nobody had DefaultMemoryLow set, so set it to the kernel default. */
f8452f
-        return CGROUP_LIMIT_MIN;
f8452f
-}
f8452f
+#define UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP(entry)                       \
f8452f
+        uint64_t unit_get_ancestor_##entry(Unit *u) {                   \
f8452f
+                CGroupContext *c;                                       \
f8452f
+                                                                        \
f8452f
+                /* 1. Is entry set in this unit? If so, use that.       \
f8452f
+                 * 2. Is the default for this entry set in any          \
f8452f
+                 *    ancestor? If so, use that.                        \
f8452f
+                 * 3. Otherwise, return CGROUP_LIMIT_MIN. */            \
f8452f
+                                                                        \
f8452f
+                assert(u);                                              \
f8452f
+                                                                        \
f8452f
+                c = unit_get_cgroup_context(u);                         \
f8452f
+                                                                        \
f8452f
+                if (c->entry##_set)                                     \
f8452f
+                        return c->entry;                                \
f8452f
+                                                                        \
f8452f
+                while (UNIT_ISSET(u->slice)) {                          \
f8452f
+                        u = UNIT_DEREF(u->slice);                       \
f8452f
+                        c = unit_get_cgroup_context(u);                 \
f8452f
+                                                                        \
f8452f
+                        if (c->default_##entry##_set)                   \
f8452f
+                                return c->default_##entry;              \
f8452f
+                }                                                       \
f8452f
+                                                                        \
f8452f
+                /* We've reached the root, but nobody had default for   \
f8452f
+                 * this entry set, so set it to the kernel default. */  \
f8452f
+                return CGROUP_LIMIT_MIN;                                \
f8452f
+}
f8452f
+
f8452f
+UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP(memory_low);
f8452f
 
f8452f
 static int lookup_block_device(const char *p, dev_t *ret) {
f8452f
         struct stat st;