OSDN Git Service

simple_lmk: Clean up some code style nitpicks
authorSultan Alsawaf <sultan@kerneltoast.com>
Mon, 4 Nov 2019 19:17:16 +0000 (11:17 -0800)
committer0ranko0P <ranko0p@outlook.com>
Tue, 24 Dec 2019 20:42:46 +0000 (04:42 +0800)
Using a parameter to pass around a unmodified pointer to a global
variable is crufty; just use the `victims` variable directly instead.
Also, compress the code in simple_lmk_init_set() a bit to make it look
cleaner.

Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com>
Signed-off-by: celtare21 <celtare21@gmail.com>
drivers/android/simple_lmk.c

index fffe688..06322d2 100644 (file)
@@ -63,21 +63,19 @@ static int victim_size_cmp(const void *lhs_ptr, const void *rhs_ptr)
        return rhs->size - lhs->size;
 }
 
-static bool vtsk_is_duplicate(struct victim_info *varr, int vlen,
-                             struct task_struct *vtsk)
+static bool vtsk_is_duplicate(int vlen, struct task_struct *vtsk)
 {
        int i;
 
        for (i = 0; i < vlen; i++) {
-               if (same_thread_group(varr[i].tsk, vtsk))
+               if (same_thread_group(victims[i].tsk, vtsk))
                        return true;
        }
 
        return false;
 }
 
-static unsigned long find_victims(struct victim_info *varr, int *vindex,
-                                 int vmaxlen, short target_adj)
+static unsigned long find_victims(int *vindex, short target_adj)
 {
        unsigned long pages_found = 0;
        int old_vindex = *vindex;
@@ -96,7 +94,7 @@ static unsigned long find_victims(struct victim_info *varr, int *vindex,
                 * trying to lock a task that we locked earlier.
                 */
                if (READ_ONCE(tsk->signal->oom_score_adj) != target_adj ||
-                   vtsk_is_duplicate(varr, *vindex, tsk))
+                   vtsk_is_duplicate(*vindex, tsk))
                        continue;
 
                vtsk = find_lock_task_mm(tsk);
@@ -104,15 +102,15 @@ static unsigned long find_victims(struct victim_info *varr, int *vindex,
                        continue;
 
                /* Store this potential victim away for later */
-               varr[*vindex].tsk = vtsk;
-               varr[*vindex].mm = vtsk->mm;
-               varr[*vindex].size = get_mm_rss(vtsk->mm);
+               victims[*vindex].tsk = vtsk;
+               victims[*vindex].mm = vtsk->mm;
+               victims[*vindex].size = get_mm_rss(vtsk->mm);
 
                /* Keep track of the number of pages that have been found */
-               pages_found += varr[*vindex].size;
+               pages_found += victims[*vindex].size;
 
                /* Make sure there's space left in the victim array */
-               if (++*vindex == vmaxlen)
+               if (++*vindex == MAX_VICTIMS)
                        break;
        }
 
@@ -121,14 +119,13 @@ static unsigned long find_victims(struct victim_info *varr, int *vindex,
         * the larger ones first.
         */
        if (pages_found)
-               sort(&varr[old_vindex], *vindex - old_vindex, sizeof(*varr),
-                    victim_size_cmp, NULL);
+               sort(&victims[old_vindex], *vindex - old_vindex,
+                    sizeof(*victims), victim_size_cmp, NULL);
 
        return pages_found;
 }
 
-static int process_victims(struct victim_info *varr, int vlen,
-                          unsigned long pages_needed)
+static int process_victims(int vlen, unsigned long pages_needed)
 {
        unsigned long pages_found = 0;
        int i, nr_to_kill = 0;
@@ -166,8 +163,7 @@ static void scan_and_kill(unsigned long pages_needed)
         */
        read_lock(&tasklist_lock);
        for (i = 0; i < ARRAY_SIZE(adj_prio); i++) {
-               pages_found += find_victims(victims, &nr_victims, MAX_VICTIMS,
-                                           adj_prio[i]);
+               pages_found += find_victims(&nr_victims, adj_prio[i]);
                if (pages_found >= pages_needed || nr_victims == MAX_VICTIMS)
                        break;
        }
@@ -178,7 +174,7 @@ static void scan_and_kill(unsigned long pages_needed)
                return;
 
        /* First round of victim processing to weed out unneeded victims */
-       nr_to_kill = process_victims(victims, nr_victims, pages_needed);
+       nr_to_kill = process_victims(nr_victims, pages_needed);
 
        /*
         * Try to kill as few of the chosen victims as possible by sorting the
@@ -188,7 +184,7 @@ static void scan_and_kill(unsigned long pages_needed)
        sort(victims, nr_to_kill, sizeof(*victims), victim_size_cmp, NULL);
 
        /* Second round of victim processing to finally select the victims */
-       nr_to_kill = process_victims(victims, nr_to_kill, pages_needed);
+       nr_to_kill = process_victims(nr_to_kill, pages_needed);
 
        /* Kill the victims */
        atomic_set_release(&victims_to_kill, nr_to_kill);
@@ -266,12 +262,11 @@ static int simple_lmk_init_set(const char *val, const struct kernel_param *kp)
        static atomic_t init_done = ATOMIC_INIT(0);
        struct task_struct *thread;
 
-       if (atomic_cmpxchg(&init_done, 0, 1))
-               return 0;
-
-       thread = kthread_run_perf_critical(simple_lmk_reclaim_thread, NULL,
-                                          "simple_lmkd");
-       BUG_ON(IS_ERR(thread));
+       if (!atomic_cmpxchg(&init_done, 0, 1)) {
+               thread = kthread_run_perf_critical(simple_lmk_reclaim_thread,
+                                                  NULL, "simple_lmkd");
+               BUG_ON(IS_ERR(thread));
+       }
 
        return 0;
 }