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