memory-cgroup: Add memory_cgroup_is_valid_cgroup_type() to check cgroup validity 84/320484/1
authorUnsung <unsung.lee@samsung.com>
Thu, 10 Oct 2024 07:53:33 +0000 (16:53 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Sat, 16 Nov 2024 04:12:32 +0000 (13:12 +0900)
Add memory_cgroup_is_valid_cgroup_type() function to check the range of cgroup enum value.
In addition, the range check code is changed to calling the corresponding function.

Change-Id: I1c18af7e83ee8c03ef515007a1940fe69f49c9a0
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/cgroup/memory-cgroup.c

index fbabde5f800c9c6b08b2d47a4dbecc6c929f352a..93fc69b972bf56e3f5886df9405fe175dca92c27 100644 (file)
@@ -126,65 +126,61 @@ int cgroup_get_type(int oom_score_adj)
                return MEMCG_ROOT;
 }
 
+static bool is_valid_cgroup_type(enum cgroup_type cgroup_type)
+{
+       switch(cgroup_type) {
+       case MEMCG_ROOT:
+       case MEMCG_BACKGROUND_MRU:
+       case MEMCG_BACKGROUND_LRU:
+               return true;
+       default:
+               _E("[CGROUP] Failed to check validity: cgroup type(%d)", cgroup_type);
+               return false;
+       }
+}
+
 struct cgroup *get_cgroup_tree(int idx)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) cgroup tree is NULL", idx);
+       if (!is_valid_cgroup_type(idx))
                return NULL;
-       }
-       else
-               return &cgroup_tree[idx];
+
+       return &cgroup_tree[idx];
 }
 
 static void set_memcg_info(int idx, struct memcg_info *mi)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) index is out of scope", idx);
-       }
-       else
+       if (is_valid_cgroup_type(idx))
                cgroup_tree[idx].memcg_info = mi;
 }
 
 struct memcg_info *get_memcg_info(int idx)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) cgroup tree's memcg info is NULL", idx);
+       if (!is_valid_cgroup_type(idx))
                return NULL;
-       }
-       else
-               return cgroup_tree[idx].memcg_info;
+
+       return cgroup_tree[idx].memcg_info;
 }
 
 static int get_parent_cgroup(int idx)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) cgroup range is out of scope", idx);
+       if (!is_valid_cgroup_type(idx))
                return MEMCG_TOP;
-       }
-       else {
-               return cgroup_tree[idx].parent_cgroup;
-       }
+
+       return cgroup_tree[idx].parent_cgroup;
 }
 
 static void set_use_hierarchy(int idx, bool use_hierarchy)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) cgroup range is out of scope", idx);
-       }
-       else {
+       if (is_valid_cgroup_type(idx))
                cgroup_tree[idx].use_hierarchy = use_hierarchy;
-       }
 }
 
 bool get_use_hierarchy(int idx)
 {
-       if(idx < MEMCG_ROOT || idx >= MEMCG_END) {
-               _E("[CGROUP] (%d) cgroup range is out of scope", idx);
+       if (!is_valid_cgroup_type(idx))
                return CGROUP_DEFAULT_USE_HIERARCHY;
-       }
-       else {
-               return cgroup_tree[idx].use_hierarchy;
-       }
+
+       return cgroup_tree[idx].use_hierarchy;
 }
 
 void register_totalram_bytes(unsigned long long ram_bytes)
@@ -454,12 +450,18 @@ int memcg_write_limiter_params(void)
 
 int memcg_write_optimizer_params(enum cgroup_type cgroup_type)
 {
-       struct memcg_info *mi = get_memcg_info(cgroup_type);
-       if (mi == NULL) {
+       struct memcg_info *memcg_info = NULL;
+
+       if (!is_valid_cgroup_type(cgroup_type))
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
+       memcg_info = get_memcg_info(cgroup_type);
+       if (memcg_info == NULL) {
                _E("Failed to get memcg info");
                return RESOURCED_ERROR_FAIL;
        }
-       memcg_write_optimizer_info(mi);
+
+       memcg_write_optimizer_info(memcg_info);
 
        return RESOURCED_ERROR_NONE;
 }