Fix bugs reported at SVACE 29/274529/5 accepted/tizen/unified/20220503.065026 submit/tizen/20220502.092616
authorUnsung Lee <unsung.lee@samsung.com>
Mon, 2 May 2022 02:15:13 +0000 (11:15 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Mon, 2 May 2022 08:28:25 +0000 (17:28 +0900)
Change-Id: I588c3329a85139967e28224bcb346adec7a76ce6
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
12 files changed:
src/common/cgroup/cgroup.c
src/common/config-parser.c
src/common/procfs.c
src/common/util.c
src/common/util.h
src/process/proc-main.c
src/process/watchdog/proc-watchdog-handler.c
src/process/watchdog/proc-watchdog.c
src/resource-limiter/memory/lowmem-limit.c
src/resource-limiter/memory/vmpressure-lowmem-handler.c
src/resource-optimizer/cpu/cpu-sched.c
src/resource-optimizer/memory/swap/swap.c

index 9b9f735..f107c2f 100644 (file)
 #include <sys/mount.h>
 
 #define MAKE_NAME(name)    CGROUP_##name##_NAME
-#define cgroup_name_cpy(dst, src, length)      \
-               do { \
-                       bool success = sizeof(dst) >= length ? true : false;    \
-                       if(success) { \
-                               strncpy(dst, src, sizeof(dst) - 1);             \
-                       }       \
-                       else {  \
-                               return RESOURCED_ERROR_OUT_OF_MEMORY;   \
-                       }       \
-               } while(0)
-
-#define cgroup_name_cat(dst, src, length)      \
-               do { \
-                       bool success = sizeof(dst) >= strlen(dst) + length ? true : false;      \
-                       if(success) { \
-                               strncat(dst, src, sizeof(dst) - strlen(dst) - 1);               \
-                       }       \
-                       else {  \
-                               return RESOURCED_ERROR_OUT_OF_MEMORY;   \
-                       }       \
-               } while(0)
+
 
 /*
  * This structure has full hierarchy of cgroups on running system.
@@ -346,29 +326,29 @@ int cgroup_make_full_subdir(const char* parentdir)
        int result;
        char path[MAX_PATH_LENGTH] = {0, };
 
-       cgroup_name_cpy(path, parentdir, strlen(parentdir) + 1);
+       str_name_cpy(path, parentdir, strlen(parentdir) + 1);
 
        for(int i = CGROUP_VIP; i < CGROUP_END; i++) {
                char name[MAX_NAME_LENGTH] = {0, };
 
                if(i == CGROUP_VIP) {
-                       cgroup_name_cpy(name, MAKE_NAME(VIP), strlen(MAKE_NAME(VIP))+ 1);
+                       str_name_cpy(name, MAKE_NAME(VIP), strlen(MAKE_NAME(VIP))+ 1);
                }
                else if(i == CGROUP_HIGH) {
-                       cgroup_name_cpy(name, MAKE_NAME(HIGH), strlen(MAKE_NAME(HIGH))+ 1);
+                       str_name_cpy(name, MAKE_NAME(HIGH), strlen(MAKE_NAME(HIGH))+ 1);
                }
                else if(i == CGROUP_MEDIUM) {
-                       cgroup_name_cpy(name, MAKE_NAME(MEDIUM), strlen(MAKE_NAME(MEDIUM))+ 1);
+                       str_name_cpy(name, MAKE_NAME(MEDIUM), strlen(MAKE_NAME(MEDIUM))+ 1);
                }
                else if(i == CGROUP_LOW) {
-                       cgroup_name_cpy(name, MAKE_NAME(LOW), strlen(MAKE_NAME(LOW))+ 1);
+                       str_name_cpy(name, MAKE_NAME(LOW), strlen(MAKE_NAME(LOW))+ 1);
                }
 
                result = cgroup_make_subdir(path, name, NULL);
                ret_value_msg_if(result < 0, result, "%s/%s init failed\n", path, name);
 
-               cgroup_name_cat(path, "/", 2);
-               cgroup_name_cat(path, name, strlen(name) + 1);
+               str_name_cat(path, "/", 2);
+               str_name_cat(path, name, strlen(name) + 1);
 
                // ../../perprocess
                result = cgroup_make_subdir(path, MAKE_NAME(PER_PROCESS), NULL);
index d4d6555..bdea3ac 100644 (file)
@@ -872,6 +872,8 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                        }
                }
        }
+       _D("Success to load %s", file_name);
+       fclose(f);
        return 0;
 
 error:
index 0f615af..367734d 100644 (file)
@@ -339,6 +339,11 @@ int proc_get_approx_mem_usage(pid_t pid, unsigned int *usage_kb)
        if (ret < 0)
                return RESOURCED_ERROR_FAIL;
 
+       if (resident_pages < shared_pages) {
+               _E("# resident page should be larger than or equal to # shared page");
+               return RESOURCED_ERROR_FAIL;
+       }
+
        /*
         * The value resident - shared is mostly similar to Uss.
         */
index 14963d0..0c63c42 100644 (file)
@@ -133,7 +133,7 @@ int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int flags)
        if (!error)
                attr->sched_priority = sp.sched_priority;
 
-       if (attr->sched_policy >= 0 && !error)
+       if (attr->sched_policy > 0 && !error)
                return RESOURCED_ERROR_NONE;
        else
                return RESOURCED_ERROR_FAIL;
index 0d73e0c..be25e27 100644 (file)
@@ -141,9 +141,32 @@ static inline bool strstart_with(const char *str, const char *with)
                         continue;                                       \
                 else
 
-#define FOREACH_STRV(s, l)                      \
+#define FOREACH_STRV(s, l)                                              \
        for ((s) = (l); (s) && *(s); (s)++)
 
+#define str_name_cpy(dst, src, length)                                  \
+               do {                                                            \
+                       bool success = sizeof(dst) >= length ? true : false;        \
+                       if(success) {                                               \
+                               strncpy(dst, src, sizeof(dst) - 1);                     \
+                       }                                                           \
+                       else {                                                      \
+                               return RESOURCED_ERROR_OUT_OF_MEMORY;                   \
+                       }                                                           \
+               } while(0)
+
+#define str_name_cat(dst, src, length)                                          \
+               do {                                                                    \
+                       bool success = sizeof(dst) >= strlen(dst) + length ? true : false;  \
+                       if(success) {                                                       \
+                               strncat(dst, src, sizeof(dst) - strlen(dst) - 1);               \
+                       }                                                                   \
+                       else {                                                              \
+                               return RESOURCED_ERROR_OUT_OF_MEMORY;                           \
+                       }                                                                   \
+               } while(0)
+
+
 /**
  * @desc executes given command and dumps output to a file
  * @param argv - command to be executed with parameters
index ffac350..2590efd 100644 (file)
@@ -950,7 +950,6 @@ __attribute__((weak)) struct proc_app_info *proc_create_app_info(const char *app
                                if (pci->mem_action.memory_bytes && pci->mem_action.action) {
                                        struct proc_limit_status pls = {0, };
 
-                                       pai->memory.memlimit_update_exclude = true;
                                        pls.limit_bytes = pci->mem_action.memory_bytes;
                                        pls.ps.pai = pai;
                                        pls.action = pci->mem_action.action;
@@ -1539,6 +1538,9 @@ enum proc_state proc_check_suspend_state(struct proc_app_info *pai)
                        return PROC_STATE_SUSPEND;
 
                ui = (struct proc_app_info *)g_slist_nth_data(ppi->app_list, 0);
+               if (!ui)
+                       return PROC_STATE_DEFAULT;
+
                if (ui->state == PROC_STATE_SUSPEND)
                        return PROC_STATE_SUSPEND;
                pai->state = PROC_STATE_SUSPEND_READY;
index 8c8954d..4d91ae4 100644 (file)
@@ -66,10 +66,10 @@ static int run_exec(char **argv)
 
 int main(int argc, char *argv[])
 {
-       int checkfd;
        char *dumpargv[3] = {DUMP_PATH, "urgent", NULL};
        char *rebootargv[4] = {REBOOT_PATH, "silent", NULL, NULL};
        DIR *dir = 0;
+       int fd;
 
        _I("[WATCHDOG] Starting process watchdog handler: [%d:%s]", argc, argv[1]);
 
@@ -80,17 +80,20 @@ int main(int argc, char *argv[])
        }
        closedir(dir);
 
-       /* check previous process */
-       if (access(CHECK_RELEASE_PROGRESS, F_OK) == 0)
+       fd = open(CHECK_RELEASE_PROGRESS, O_RDWR);
+       if (fd > 0) {
+               close(fd);
                return 0;
-
-       _D("[WATCHDOG] No other release_agent is running.");
-
-       /* make tmp file */
-       checkfd = creat(CHECK_RELEASE_PROGRESS, 0640);
-       if (checkfd < 0) {
-               _E("[WATCHDOG] fail to make %s file\n", CHECK_RELEASE_PROGRESS);
-               checkfd = 0;
+       }
+       else {
+               _D("[WATCHDOG] No other release_agent is running.");
+
+               /* make tmp file */
+               fd = open(CHECK_RELEASE_PROGRESS, O_CREAT|O_WRONLY|O_TRUNC, 0640);
+               if (fd < 0) {
+                       _E("[WATCHDOG] fail to make %s file\n", CHECK_RELEASE_PROGRESS);
+                       fd = 0;
+               }
        }
 
        /* unmount cgroup for preventing launching another release_agent */
@@ -102,10 +105,10 @@ int main(int argc, char *argv[])
                run_exec(dumpargv);
 
        /* clear tmp file */
-       if (checkfd) {
-               if (unlink(CHECK_RELEASE_PROGRESS) < 0)
+       if (fd) {
+               if (unlinkat(fd, CHECK_RELEASE_PROGRESS, 0) < 0)
                        _E("[WATCHDOG] fail to remove %s file\n", CHECK_RELEASE_PROGRESS);
-               close(checkfd);
+               close(fd);
        }
 
        sync();
index 3642436..c6e2341 100644 (file)
@@ -162,17 +162,19 @@ static int resourced_proc_watchdog_process_init(void *data)
 {
        _cleanup_close_ int checkfd = -1;
        int r;
+       int fd;
 
        if (proc_watchdog_boot_param_reboot_disabled()) {
                proc_watchdog_print_console("Reboot has been disabled, boot param: %s", BOOT_PARAM_PROC_WATCHDOG_REBOOT_DISABLED);
                return RESOURCED_ERROR_NONE;
        }
 
-       r = access(CHECK_RELEASE_PROGRESS, F_OK);
-       if (r == 0) {
-               r = unlink(CHECK_RELEASE_PROGRESS);
+       fd = open(CHECK_RELEASE_PROGRESS, O_RDWR);
+       if (fd > 0) {
+               r = unlinkat(fd, CHECK_RELEASE_PROGRESS, 0);
                if (r < 0)
                        _E("[WATCHDOG] failed to remove %s: %m", CHECK_RELEASE_PROGRESS);
+               close(fd);
        }
 
        if (!is_mounted(PROC_WATCHDOGCG_PATH)) {
index e36903b..7e470c8 100644 (file)
@@ -488,7 +488,7 @@ static int lowmem_limit_app(void *data)
        struct proc_limit_status *pls = (struct proc_limit_status *)data;
 
        error = lowmem_limit_set_app(pls->limit_bytes, pls->ps.pai, pls->action);
-       if (!error)
+       if (!error && pls->ps.pai)
                pls->ps.pai->memory.memlimit_update_exclude = true;
 
        return error;
@@ -514,7 +514,10 @@ static int lowmem_limit_service(void *data)
 
        struct proc_status *ps = (struct proc_status *)data;
 
-       if (ps->pai && ps->pai->memory.memlimit_update_exclude)
+       if (ps->pai == NULL)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
+       if (ps->pai->memory.memlimit_update_exclude)
                return RESOURCED_ERROR_NONE;
 
        if (mem_service_limit_bytes && mem_service_action != PROC_ACTION_IGNORE) {
@@ -529,7 +532,10 @@ static int lowmem_limit_appwidget(void *data)
 
        struct proc_status *ps = (struct proc_status *)data;
 
-       if (ps->pai && ps->pai->memory.memlimit_update_exclude)
+       if (ps->pai == NULL)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
+       if (ps->pai->memory.memlimit_update_exclude)
                return RESOURCED_ERROR_NONE;
 
        if (mem_guiapp_limit_bytes && mem_guiapp_action != PROC_ACTION_IGNORE &&
@@ -551,7 +557,10 @@ static int lowmem_limit_bgapp(void *data)
 
        struct proc_status *ps = (struct proc_status *)data;
 
-       if (ps->pai && ps->pai->memory.memlimit_update_exclude)
+       if (ps->pai == NULL)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
+       if (ps->pai->memory.memlimit_update_exclude)
                return RESOURCED_ERROR_NONE;
 
        lowmem_limit_set_app(mem_bgapp_limit_bytes, ps->pai, mem_bgapp_action);
@@ -565,6 +574,9 @@ static int lowmem_limit_fgapp(void *data)
 
        struct proc_status *ps = (struct proc_status *)data;
 
+       if (ps->pai == NULL)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
        if ((mem_guiapp_limit_bytes && ps->pai->type == PROC_TYPE_GUI) ||
            (mem_widget_limit_bytes && ps->pai->type == PROC_TYPE_WIDGET))
                return lowmem_limit_appwidget(data);
index 10fba10..dfc6950 100644 (file)
@@ -948,11 +948,15 @@ static void *lowmem_reclaim_worker(void *arg)
                /* Wait on any wake-up call */
                ctl = g_async_queue_pop(lmw->queue);
 
-               if (ctl->flags & OOM_DROP)
-                       LOWMEM_DESTROY_REQUEST(ctl);
+               if (!ctl) {
+                       _W("[LMK] ctl structure is NULL");
+                       continue;
+               }
 
-               if (!LOWMEM_WORKER_IS_ACTIVE(lmw) || !ctl)
+               if ((ctl->flags & OOM_DROP) || !LOWMEM_WORKER_IS_ACTIVE(lmw)) {
+                       LOWMEM_DESTROY_REQUEST(ctl);
                        break;
+               }
 
                LOWMEM_WORKER_RUN(lmw);
 process_again:
@@ -1875,9 +1879,6 @@ static int lowmem_init(void)
 
 static int lowmem_exit(void)
 {
-       if (strncmp(event_level, MEMCG_DEFAULT_EVENT_LEVEL, sizeof(MEMCG_DEFAULT_EVENT_LEVEL)))
-               free(event_level);
-
        lowmem_deactivate_worker();
        lowmem_limit_exit();
        lowmem_system_exit();
index a226d17..ff06fa9 100644 (file)
@@ -257,7 +257,6 @@ static int load_cpu_affinity_config(struct cpu_sched *data)
        c->name = name;
 
        if (cpu_sched_parse_cpuset(c, get_cpucg_conf_value()) < 0) {
-               _E("[CPU-SCHED] cpu-sched parse %s coreset: could not parse", name);
                return RESOURCED_ERROR_FAIL;
        }
 
index 7fb84cf..6d1a38f 100644 (file)
@@ -684,10 +684,11 @@ static int swap_communicate_thread(struct swap_thread_bundle *bundle)
 static int swap_start_handler(void *data)
 {
        int ret;
+       const char *path = data;
        struct swap_thread_bundle *bundle;
 
-       if (!data) {
-               _E("data is NULL");
+       if (!path) {
+               _E("path name is NULL");
                return RESOURCED_ERROR_NO_DATA;
        }
 
@@ -695,8 +696,8 @@ static int swap_start_handler(void *data)
        if (!bundle)
                return RESOURCED_ERROR_OUT_OF_MEMORY;
 
+       str_name_cpy(bundle->msg.path, path, strlen(path) + 1);
        bundle->op = SWAP_OP_RECLAIM;
-       memcpy(bundle->msg.path, data, sizeof(struct swap_status_msg));
        ret = swap_communicate_thread(bundle);
        return ret;
 }
@@ -1032,7 +1033,12 @@ static void resourced_swap_change_memcg_settings(enum cgroup_type type)
        if (!cgroup_swap)
                return;
 
-       cgroup_write_node_uint32(cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE, 1);
+       ret = cgroup_write_node_uint32(cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE, 1);
+       if (ret != RESOURCED_ERROR_NONE)
+       {
+               _E("Failed to write at %s/%s", cgroup_swap->memcg_info->name, MEMCG_MOVE_CHARGE);
+       }
+
        snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, MEMCG_FORCE_RECLAIM);
        ret = swap_check_node(buf);
        if (ret == RESOURCED_ERROR_NONE) {