This is a complete low memory killer solution for Android that is small
and simple. Processes are killed according to the priorities that
Android gives them, so that the least important processes are always
killed first. Processes are killed until memory deficits are satisfied,
as observed from kswapd struggling to free up pages. Simple LMK stops
killing processes when kswapd finally goes back to sleep.

The only tunables are the desired amount of memory to be freed per
reclaim event and desired frequency of reclaim events. Simple LMK tries
to free at least the desired amount of memory per reclaim and waits
until all of its victims' memory is freed before proceeding to kill more
processes.

翻译:

这是一个安卓LMK的完整解决方案,并且它很小很简单。它根据系统给出的进程优先级进行,因此,最不重要的任务最先被杀死。SLMK会持续杀任务,直到内存需求被满足。SLMK通过检测kswapd的活跃状态来检测内存压力,kswapd进入休眠状态时SLMK停止回收内存。(新版已经使用vmpressure了)
唯一能被调整的内容是每次内存回收至少回收的内存与内存回收的频率。SLMK将尝试在每次回收内存时回收至少达到预设值的内存量,并等待所有被杀的进程的内存已经被真正回收后,再去杀别的进程。

准备

  • 一套可以开机并且正常使用的内核的源码
  • git环境
  • 脑子

开始

  • 进入你的内核所在目录
  • 打开终端

    获取源码

    git fetch https://github.com/kerneltoast/simple_lmk <分支名>
    分支名你可以自己点进去看,选择一个与当前内核版本匹配的分支
    比如

    $ git fetch https://github.com/kerneltoast/simple_lmk linux-4.4
    来自 https://github.com/kerneltoast/simple_lmk
     * branch                      linux-4.4  -> FETCH_HEAD
    

Pick提交

  • 你可以直接在github网页上查看提交
  • 你也可以使用git log查看对应的提交
  • 比如
$ git log FETCH_HEAD --oneline 
1dc32ed871bf mm: vmpressure: Don't cache the window size
d6034d1b8698 mm: vmpressure: Interpret zero scanned pages as 100% pressure
d90c64b30570 mm: vmpressure: Don't exclude any allocation types
077d0ef7467d mm: vmpressure: Make local functions and global variables static
89e5c733a088 simple_lmk: Update adj targeting for Android 10
1f891e3f1fc4 simple_lmk: Use vmpressure notifier to trigger kills
950ff59d912c mm: vmpressure: make vmpressure window variable
06fffb15fa56 mm: vmpressure: account allocstalls only on higher pressures
29e2aa448fca mm: vmpressure: scale pressure based on reclaim context
12a1795554df mm: vmpressure: allow in-kernel clients to subscribe for events
18563c988d96 mm: Stop kswapd early when nothing's waiting for it to free pages
e312917fc305 simple_lmk: Include swap memory usage in the size of victims
bed87448c799 simple_lmk: Relax memory barriers and clean up some styling
78053381afe9 simple_lmk: Place victims onto SCHED_RR
e7ff187d49da simple_lmk: Add a timeout to stop waiting for victims to die
ea56ff259b35 simple_lmk: Ignore tasks that won't free memory
b53e124b7be1 simple_lmk: Simplify tricks used to speed up the death process
541f7caef81b simple_lmk: Report mm as freed as soon as exit_mmap() finishes
de6ef2660c82 cpuset, mm: fix TIF_MEMDIE check in cpuset_change_task_nodemask
a0c0d2dcfe1d freezer, oom: check TIF_MEMDIE on the correct task
f8aa439e974b simple_lmk: Mark victim thread group with TIF_MEMDIE
44d42ab5c2cd simple_lmk: Disable OOM killer when Simple LMK is enabled
f1e197aa8577 simple_lmk: Print a message when there are no processes to kill
c8c12e67b7a2 simple_lmk: Remove compat cruft not specific to 4.4
62870986f772 simple_lmk: Update copyright to 2020
490f3e24a758 simple_lmk: Don't queue up new reclaim requests during reclaim
3432b76e0ea9 simple_lmk: Increase default minfree value
0b98c18f0aab simple_lmk: Clean up some code style nitpicks
77c531f66919 simple_lmk: Make reclaim deterministic
7938d5a4377d simple_lmk: Fix broken multicopy atomicity for victims_to_kill
1a1e373d0809 simple_lmk: Use proper atomic_* operations where needed
dadd79701363 simple_lmk: Remove kthread_should_stop() exit condition
29429d8167a2 simple_lmk: Fix pages_found calculation
16f8ac04a316 simple_lmk: Introduce Simple Low Memory Killer for Android

正常情况下,我们可以在确定首尾提交后直接执行
git cherry-pick 16f8ac04a316^..1dc32ed871bf 快速pick
但是
这里不行
这个仓库似乎被施法了 连续pick不仅会触发各种奇怪的bug 还会顺序乱套
甚至连手动连续pick也不行 (如下)
$ git cherry-pick 16f8ac04a316 29429d8167a2 dadd79701363 1a1e373d0809 7938d5a4377d 77c531f66919 0b98c18f0aab 3432b76e0ea9 490f3e24a758 62870986f772 c8c12e67b7a2 f1e197aa8577 44d42ab5c2cd f8aa439e974b a0c0d2dcfe1d de6ef2660c82 541f7caef81b b53e124b7be1 ea56ff259b35 e7ff187d49da 78053381afe9 bed87448c799 e312917fc305 18563c988d96 12a1795554df 29e2aa448fca 06fffb15fa56 950ff59d912c 1f891e3f1fc4 89e5c733a088 077d0ef7467d d90c64b30570 d6034d1b8698 1dc32ed871bf

我们只能手动单个pick

如下所示

$ git cherry-pick 16f8ac04a316
error: 不能应用 16f8ac04a316... simple_lmk: Introduce Simple Low Memory Killer for Android
提示:冲突解决完毕后,用 'git add <路径>' 或 'git rm <路径>'
提示:对修正后的文件做标记,然后用 'git commit' 提交

第一个提交就出现了冲突 我们查看冲突内容

$ git status 
位于分支 demo
您在执行拣选提交 16f8ac04a316 的操作。
  (解决冲突并运行 "git cherry-pick --continue")
  (使用 "git cherry-pick --abort" 以取消拣选操作)

要提交的变更:

    修改:     drivers/android/Kconfig
    修改:     drivers/android/Makefile
    新文件:   drivers/android/simple_lmk.c
    新文件:   include/linux/simple_lmk.h
    修改:     kernel/fork.c

未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)

    双方修改:   mm/vmscan.c

可知冲突的文件是mm/vmscan.c,我们打开它,找到冲突的部分

static void kswapd_try_to_sleep(pg_data_t *pgdat, int order,
                int classzone_idx, int balanced_classzone_idx)
{
    long remaining = 0;
    DEFINE_WAIT(wait);

    if (freezing(current) || kthread_should_stop())
        return;

    prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);

    /* Try to sleep for a short interval */
<<<<<<< HEAD
    if (prepare_kswapd_sleep(pgdat, order, remaining,
                        balanced_classzone_idx)) {
        /*
         * Compaction records what page blocks it recently failed to
         * isolate pages from and skips them in the future scanning.
         * When kswapd is going to sleep, it is reasonable to assume
         * that pages and compaction may succeed so reset the cache.
         */
        reset_isolation_suitable(pgdat);

        /*
         * We have freed the memory, now we should compact it to make
         * allocation of the requested order possible.
         */
        wakeup_kcompactd(pgdat, order, classzone_idx);

=======
    if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
        simple_lmk_stop_reclaim();
>>>>>>> 16f8ac04a316... simple_lmk: Introduce Simple Low Memory Killer for Android
        remaining = schedule_timeout(HZ/10);
        finish_wait(&pgdat->kswapd_wait, &wait);
        prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
    }

    /*
     * After a short sleep, check if it was a premature sleep. If not, then
     * go fully to sleep until explicitly woken up.
     */
<<<<<<< HEAD
    if (prepare_kswapd_sleep(pgdat, order, remaining,
                        balanced_classzone_idx)) {
=======
    if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
        simple_lmk_stop_reclaim();
>>>>>>> 16f8ac04a316... simple_lmk: Introduce Simple Low Memory Killer for Android
        trace_mm_vmscan_kswapd_sleep(pgdat->node_id);

        /*
         * vmstat counters are not perfectly accurate and the estimated
         * value for counters such as NR_FREE_PAGES can deviate from the

冲突有点大 我太菜了 无从下手
于是找到github上的源提交 看看它原来是怎么改的

原来是在里面加了接口 那就简单

我们先把被git搞砸的东西还原

$ git checkout --ours mm/vmscan.c

然后手动编辑

我们通过遍历文件上下文 找到要加入东西的地方 手动加入
以下是我的手动编辑内容

diff --git a/mm/vmscan.c b/mm/vmscan.c
index aa1074d3031b..fcecc2da716a 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -46,6 +46,7 @@
 #include <linux/oom.h>
 #include <linux/prefetch.h>
 #include <linux/printk.h>
+#include <linux/simple_lmk.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -3325,7 +3326,8 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
        do {
                bool raise_priority = true;
                unsigned long lru_pages = 0;
-
+               
+               simple_lmk_decide_reclaim(sc.priority);
                sc.nr_reclaimed = 0;
 
                /*
@@ -3475,6 +3477,7 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order,
        /* Try to sleep for a short interval */
        if (prepare_kswapd_sleep(pgdat, order, remaining,
                                                balanced_classzone_idx)) {
+               simple_lmk_stop_reclaim();
                /*
                 * Compaction records what page blocks it recently failed to
                 * isolate pages from and skips them in the future scanning.
@@ -3500,6 +3503,7 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order,
         */
        if (prepare_kswapd_sleep(pgdat, order, remaining,
                                                balanced_classzone_idx)) {
+               simple_lmk_stop_reclaim();
                trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
 
                /*

修改完成后git commit -a

simple_lmk: Introduce Simple Low Memory Killer for Android

This is a complete low memory killer solution for Android that is small
and simple. Processes are killed according to the priorities that
Android gives them, so that the least important processes are always
killed first. Processes are killed until memory deficits are satisfied,
as observed from kswapd struggling to free up pages. Simple LMK stops
killing processes when kswapd finally goes back to sleep.

The only tunables are the desired amount of memory to be freed per
reclaim event and desired frequency of reclaim events. Simple LMK tries
to free at least the desired amount of memory per reclaim and waits
until all of its victims' memory is freed before proceeding to kill more
processes.

Signed-off-by: Sultan Alsawaf <[email protected]>

# Conflicts:
#       mm/vmscan.c
#
# 似乎您正在做一个拣选提交。如果不对,请删除文件
#       .git/CHERRY_PICK_HEAD
# 然后重试。


# 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交
# 说明将会终止提交。
#
# 作者:  Sultan Alsawaf <[email protected]>
# 日期:  Mon Jun 24 07:22:55 2019 -0700
#
# 位于分支 demo
# 您在执行拣选提交 16f8ac04a316 的操作。
#
# 要提交的变更:
#       修改:     drivers/android/Kconfig
#       修改:     drivers/android/Makefile
#       新文件:   drivers/android/simple_lmk.c
#       新文件:   include/linux/simple_lmk.h
#       修改:     kernel/fork.c
#       修改:     mm/vmscan.c

在编辑框中我们可以看到原作者的信息得以保留

保存并离开即可

然后我们继续Pick

$ git cherry-pick 29429d8167a2a2fd87f23f5da94dafa87cc76677
[demo 4cc127635423] simple_lmk: Fix pages_found calculation
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Jul 20 09:54:58 2019 -0700
 1 file changed, 1 insertion(+), 2 deletions(-)
$ git cherry-pick dadd79701363cf4d9c78f89527ee0a7d99812469
[demo e16a3926a41c] simple_lmk: Remove kthread_should_stop() exit condition
 Author: Sultan Alsawaf <[email protected]>
 Date: Wed Aug 21 08:30:55 2019 -0700
 1 file changed, 1 insertion(+), 7 deletions(-)
$ git cherry-pick 1a1e373d080959c7fe40e116a42b3f508c0c5510
[demo 8a4e791f0e30] simple_lmk: Use proper atomic_* operations where needed
 Author: Sultan Alsawaf <[email protected]>
 Date: Wed Aug 21 08:37:04 2019 -0700
 1 file changed, 7 insertions(+), 7 deletions(-)
$ git cherry-pick 7938d5a4377dc3d7f48b817ab1cff76152dcc16e
[demo 69609412804e] simple_lmk: Fix broken multicopy atomicity for victims_to_kill
 Author: Sultan Alsawaf <[email protected]>
 Date: Mon Nov 4 10:56:15 2019 -0800
 1 file changed, 4 insertions(+), 4 deletions(-)
 $ git cherry-pick 77c531f66919c5df2bcc04437dc2518a7b320e70
error: 不能应用 77c531f66919... simple_lmk: Make reclaim deterministic
提示:冲突解决完毕后,用 'git add <路径>' 或 'git rm <路径>'
提示:对修正后的文件做标记,然后用 'git commit' 提交

不错又炸了一个
我们再按照上面的套路来看一看

$ git status 
位于分支 demo
您在执行拣选提交 77c531f66919 的操作。
  (解决冲突并运行 "git cherry-pick --continue")
  (使用 "git cherry-pick --abort" 以取消拣选操作)

要提交的变更:

    修改:     drivers/android/simple_lmk.c
    修改:     include/linux/simple_lmk.h

未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)

    双方修改:   mm/vmscan.c

怎么又是你个vmscan (原因是caf版本和kernel/common差异较大 因此次次冲突)

照样,我们先看看冲突内容
打开文件 找到位置


    /* Try to sleep for a short interval */
<<<<<<< HEAD
    if (prepare_kswapd_sleep(pgdat, order, remaining,
                        balanced_classzone_idx)) {
        simple_lmk_stop_reclaim();
        /*
         * Compaction records what page blocks it recently failed to
         * isolate pages from and skips them in the future scanning.
         * When kswapd is going to sleep, it is reasonable to assume
         * that pages and compaction may succeed so reset the cache.
         */
        reset_isolation_suitable(pgdat);

        /*
         * We have freed the memory, now we should compact it to make
         * allocation of the requested order possible.
         */
        wakeup_kcompactd(pgdat, order, classzone_idx);

=======
    if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
>>>>>>> 77c531f66919... simple_lmk: Make reclaim deterministic
        remaining = schedule_timeout(HZ/10);
        finish_wait(&pgdat->kswapd_wait, &wait);
        prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
    }

    /*
     * After a short sleep, check if it was a premature sleep. If not, then
     * go fully to sleep until explicitly woken up.
     */
<<<<<<< HEAD
    if (prepare_kswapd_sleep(pgdat, order, remaining,
                        balanced_classzone_idx)) {
        simple_lmk_stop_reclaim();
=======
    if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
>>>>>>> 77c531f66919... simple_lmk: Make reclaim deterministic
        trace_mm_vmscan_kswapd_sleep(pgdat->node_id);

        /*

不错,真是令人头大
我们和上面一样 把这个文件切换回我们原本的状态 手动修改

$ git checkout --ours mm/vmscan.c

去看看源提交
这也太简单了吧

diff --git a/mm/vmscan.c b/mm/vmscan.c
index fcecc2da716a..e5f20645a1b2 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3477,7 +3477,6 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order,
        /* Try to sleep for a short interval */
        if (prepare_kswapd_sleep(pgdat, order, remaining,
                                                balanced_classzone_idx)) {
-               simple_lmk_stop_reclaim();
                /*
                 * Compaction records what page blocks it recently failed to
                 * isolate pages from and skips them in the future scanning.
@@ -3503,7 +3502,6 @@ static void kswapd_try_to_sleep(pg_data_t *pgdat, int order,
         */
        if (prepare_kswapd_sleep(pgdat, order, remaining,
                                                balanced_classzone_idx)) {
-               simple_lmk_stop_reclaim();
                trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
 
                /*

我们照样修改后
完成

$ git commit -a
[demo ff7ec78e6f81] simple_lmk: Make reclaim deterministic
 Author: Sultan Alsawaf <[email protected]>
 Date: Mon Nov 4 11:06:13 2019 -0800
 3 files changed, 14 insertions(+), 30 deletions(-)

然后继续

$ git cherry-pick 0b98c18f0aab85765252ac39c92c012901930bae
[demo 2157943f8b87] simple_lmk: Clean up some code style nitpicks
 Author: Sultan Alsawaf <[email protected]>
 Date: Mon Nov 4 11:27:29 2019 -0800
 1 file changed, 20 insertions(+), 25 deletions(-)
$ git cherry-pick 3432b76e0ea917091327053ebe0243407924b20e
[demo 9131c3dd1a69] simple_lmk: Increase default minfree value
 Author: Sultan Alsawaf <[email protected]>
 Date: Wed Nov 6 10:02:57 2019 -0800
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git cherry-pick 490f3e24a758ca7a787f293ba6ad0503fffe6603
[demo 732dc935e775] simple_lmk: Don't queue up new reclaim requests during reclaim
 Author: Sultan Alsawaf <[email protected]>
 Date: Mon Jan 20 16:03:44 2020 -0800
 1 file changed, 5 insertions(+), 13 deletions(-)
$ git cherry-pick 62870986f772abc81f690516349a7bec12f8c995
[demo 62bf517e9336] simple_lmk: Update copyright to 2020
 Author: Sultan Alsawaf <[email protected]>
 Date: Thu Feb 6 20:57:53 2020 -0800
 2 files changed, 2 insertions(+), 2 deletions(-)
$ git cherry-pick c8c12e67b7a2cae1994117f87781220fb372916a
[demo 36d9bcb1b607] simple_lmk: Remove compat cruft not specific to 4.4
 Author: Sultan Alsawaf <[email protected]>
 Date: Thu Feb 6 20:59:22 2020 -0800
 1 file changed, 1 insertion(+), 21 deletions(-)
$ git cherry-pick f1e197aa85772249f2ebe942784da7f6a076d2e9
[demo 8fec83731ed3] simple_lmk: Print a message when there are no processes to kill
 Author: Sultan Alsawaf <[email protected]>
 Date: Thu Feb 6 21:03:24 2020 -0800
 1 file changed, 3 insertions(+), 1 deletion(-)
$ git cherry-pick 44d42ab5c2cdc1f5c1abbb8d45c91aa4e42f9211
[demo 0c66742a7687] simple_lmk: Disable OOM killer when Simple LMK is enabled
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 7 23:36:58 2020 -0800
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git cherry-pick f8aa439e974baa07d7125eda454356c812661e09
error: 不能应用 f8aa439e974b... simple_lmk: Mark victim thread group with TIF_MEMDIE
提示:冲突解决完毕后,用 'git add <路径>' 或 'git rm <路径>'
提示:对修正后的文件做标记,然后用 'git commit' 提交

又来一个
我们看看

$ git status 
位于分支 demo
您在执行拣选提交 f8aa439e974b 的操作。
  (解决冲突并运行 "git cherry-pick --continue")
  (使用 "git cherry-pick --abort" 以取消拣选操作)

要提交的变更:

    修改:     drivers/android/simple_lmk.c

未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)

    双方修改:   kernel/exit.c

直接去文件看

    up_read(&mm->mmap_sem);
    enter_lazy_tlb(mm, current);
    task_unlock(tsk);
    mm_update_next_owner(mm);
<<<<<<< HEAD

    mm_released = mmput(mm);
    if (test_thread_flag(TIF_MEMDIE))
        exit_oom_victim();
    if (mm_released)
        set_tsk_thread_flag(tsk, TIF_MM_RELEASED);
=======
    mmput(mm);
#ifdef CONFIG_ANDROID_SIMPLE_LMK
    clear_thread_flag(TIF_MEMDIE);
#else
    if (test_thread_flag(TIF_MEMDIE))
        exit_oom_victim();
#endif
>>>>>>> f8aa439e974b... simple_lmk: Mark victim thread group with TIF_MEMDIE
}

static struct task_struct *find_alive_thread(struct task_struct *p)
{
    struct task_struct *t;

不错,规模比上个冲突小多了,不过解决方案依然不是一眼能看出来的(我太菜了
那就按照上面的老套路 手动导入

$ git checkout kernel/exit.c --ours
diff --git a/kernel/exit.c b/kernel/exit.c
index 8e288e8e9ca3..ea1cefb533f6 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -440,8 +440,12 @@ static void exit_mm(struct task_struct *tsk)
        mm_update_next_owner(mm);
 
        mm_released = mmput(mm);
+#ifdef CONFIG_ANDROID_SIMPLE_LMK
+       clear_thread_flag(TIF_MEMDIE);
+#else
        if (test_thread_flag(TIF_MEMDIE))
                exit_oom_victim();
+#endif
        if (mm_released)
                set_tsk_thread_flag(tsk, TIF_MM_RELEASED);
 }

解决后提交

$ git commit -a
[demo aec3880aa55d] simple_lmk: Mark victim thread group with TIF_MEMDIE
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Feb 8 17:03:35 2020 -0800
 2 files changed, 11 insertions(+), 1 deletion(-)

然后继续

$ git cherry-pick a0c0d2dcfe1d5eb24228087d68917dc841603fc2
[demo 2cf1688f1e5f] freezer, oom: check TIF_MEMDIE on the correct task
 Author: Michal Hocko <[email protected]>
 Date: Thu Jul 28 15:45:16 2016 -0700
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git cherry-pick de6ef2660c8270286efc3cba4d18655253ef141d
[demo c7acdc8466b0] cpuset, mm: fix TIF_MEMDIE check in cpuset_change_task_nodemask
 Author: Michal Hocko <[email protected]>
 Date: Thu Jul 28 15:45:19 2016 -0700
 1 file changed, 9 deletions(-)
$ git cherry-pick 541f7caef81b027b103e5569eb127abf27e514ac
[demo 5a6d5a9b6fb7] simple_lmk: Report mm as freed as soon as exit_mmap() finishes
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 7 23:51:57 2020 -0800
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git cherry-pick b53e124b7be12f2df288a7a7420b3ac48893c7b9
[demo ee5758ea80fb] simple_lmk: Simplify tricks used to speed up the death process
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Feb 8 00:00:48 2020 -0800
 1 file changed, 3 insertions(+), 12 deletions(-)
$ git cherry-pick ea56ff259b35993f8fbbfc77d7895962323ab414
[demo 809367323cc9] simple_lmk: Ignore tasks that won't free memory
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Feb 8 03:21:01 2020 -0800
 1 file changed, 7 insertions(+), 3 deletions(-)
$ git cherry-pick e7ff187d49da339afbff39c2919921059baed8d9
[demo 8d8ebf4a8d74] simple_lmk: Add a timeout to stop waiting for victims to die
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Feb 8 03:22:44 2020 -0800
 2 files changed, 40 insertions(+), 13 deletions(-)
$ git cherry-pick 78053381afe94d0b330ff77bc6f1c99d94ce1828
[demo 18474dcde65b] simple_lmk: Place victims onto SCHED_RR
 Author: Sultan Alsawaf <[email protected]>
 Date: Sat Feb 8 14:26:31 2020 -0800
 1 file changed, 3 insertions(+), 2 deletions(-)
$ git cherry-pick bed87448c7997788dfae75d7471d7ae29b8b1b36
[demo edc30fbda6d3] simple_lmk: Relax memory barriers and clean up some styling
 Author: Sultan Alsawaf <[email protected]>
 Date: Tue Feb 18 22:37:48 2020 -0800
 1 file changed, 12 insertions(+), 11 deletions(-)
$ git cherry-pick e312917fc30579c02c00f71b46ad774a0c351a0b
[demo c62bb9136940] simple_lmk: Include swap memory usage in the size of victims
 Author: Sultan Alsawaf <[email protected]>
 Date: Tue Feb 18 22:39:41 2020 -0800
 1 file changed, 12 insertions(+), 1 deletion(-)
$ git cherry-pick 18563c988d96e5893f36b24382e19f3019d5f5cc
[demo 3486ae8a0408] mm: Stop kswapd early when nothing's waiting for it to free pages
 Author: Sultan Alsawaf <[email protected]>
 Date: Tue Feb 18 22:52:50 2020 -0800
 3 files changed, 16 insertions(+), 3 deletions(-)
$ git cherry-pick 12a1795554df58b7527e8c1e2d06816db797dd76
error: 不能应用 12a1795554df... mm: vmpressure: allow in-kernel clients to subscribe for events
提示:冲突解决完毕后,用 'git add <路径>' 或 'git rm <路径>'
提示:对修正后的文件做标记,然后用 'git commit' 提交

又出问题了
这次我们再来按照老套路

$ git status 
位于分支 demo
您在执行拣选提交 12a1795554df 的操作。
  (解决冲突并运行 "git cherry-pick --continue")
  (使用 "git cherry-pick --abort" 以取消拣选操作)

未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)

    双方修改:   mm/Makefile
    双方修改:   mm/vmpressure.c

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

查看冲突的文件内容 发现并不简单 冲突规模之大令人发指 这里就不演示了

那么这是怎么回事呢?

我们去浏览CAF内核的默认提交历史 发现,这个提交以及接下来几个提交竟然已经包含在内(也就是说 caf内核已经自带)

因此 我们不再需要pick它

$ git cherry-pick --abort

我们继续

 $ git cherry-pick 1f891e3f1fc46f24665d167560e45c7d45a37fab
error: 不能应用 1f891e3f1fc4... simple_lmk: Use vmpressure notifier to trigger kills
提示:冲突解决完毕后,用 'git add <路径>' 或 'git rm <路径>'
提示:对修正后的文件做标记,然后用 'git commit' 提交

又是一个开门红啊

$ git status 
位于分支 demo
您在执行拣选提交 1f891e3f1fc4 的操作。
  (解决冲突并运行 "git cherry-pick --continue")
  (使用 "git cherry-pick --abort" 以取消拣选操作)

要提交的变更:

    修改:     drivers/android/Kconfig
    修改:     drivers/android/simple_lmk.c
    修改:     include/linux/simple_lmk.h

未合并的路径:
  (使用 "git add <文件>..." 标记解决方案)

    双方修改:   mm/vmscan.c

还是熟悉的你

我们打开文件

        bool raise_priority = true;
<<<<<<< HEAD
        unsigned long lru_pages = 0;
        
        simple_lmk_decide_reclaim(sc.priority);
=======
        bool pgdat_needs_compaction = (order > 0);

>>>>>>> 1f891e3f1fc4... simple_lmk: Use vmpressure notifier to trigger kills
        sc.nr_reclaimed = 0;

不错 这次的冲突内容更简单了

在参照源提交的情况下,我们可以直接解决(因为比较简单),无需checkout重新导入修改

    count_vm_event(PAGEOUTRUN);

    do {
        bool raise_priority = true;
        unsigned long lru_pages = 0;
        
        sc.nr_reclaimed = 0;

        /*
         * Scan in the highmem->dma direction for the highest
         * zone which needs scanning
         */

继续 就快成功了

$ git commit -a 
[demo c5d7565e7211] simple_lmk: Use vmpressure notifier to trigger kills
 Author: Sultan Alsawaf <[email protected]>
 Date: Mon Feb 24 18:19:26 2020 -0800
 4 files changed, 17 insertions(+), 35 deletions(-)
$ git cherry-pick 89e5c733a0888d442d0c04778f1ff550a1ae1ecd
[demo a36c2257e165] simple_lmk: Update adj targeting for Android 10
 Author: Sultan Alsawaf <[email protected]>
 Date: Wed Feb 26 10:14:18 2020 -0800
 1 file changed, 22 insertions(+), 21 deletions(-)
$ git cherry-pick 077d0ef7467d385936b6c2785af52685df6eb929
[demo 79ca977764c6] mm: vmpressure: Make local functions and global variables static
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 28 12:22:08 2020 -0800
 1 file changed, 8 insertions(+), 8 deletions(-)
$ git cherry-pick d90c64b30570678fb4ec6479c45c8da1996c68a9
[demo 3ac6d0dd94e4] mm: vmpressure: Don't exclude any allocation types
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 28 12:28:54 2020 -0800
 1 file changed, 17 deletions(-)
$ git cherry-pick d6034d1b86986dba4244c41c8934967d27467dbf
[demo b16975ae6bfe] mm: vmpressure: Interpret zero scanned pages as 100% pressure
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 28 12:38:10 2020 -0800
 1 file changed, 21 insertions(+), 18 deletions(-)
$ git cherry-pick 1dc32ed871bf5e8f06d61b71d2d238f34cd62917
[demo b7e7d7d4ef27] mm: vmpressure: Don't cache the window size
 Author: Sultan Alsawaf <[email protected]>
 Date: Fri Feb 28 12:40:06 2020 -0800
 1 file changed, 44 insertions(+), 53 deletions(-)

成了
接下来就是改改config的事了

总结

  • Simple lmk 具有别致的仓库,连续pick不可用,会触发bug
  • Simple lmk中冲突的原因其实主要是kernel/common和caf内核之间的差异
  • 其实4.4内核可以直接pick sultan kernel的仓库,无需费周折在这里解决冲突,这里仅供演示基本的pick和冲突处理解决步骤
  • 处理冲突需要完全理解代码的上下文含义,以及双方的差异,才能正确进行
  • 上面的提交中似乎有好几个是属于那种加了又删的东西。有人可能会问,那我为什么要跟着他加了又删?不是浪费?我只能说,你必须尊重作者,你也只能跟着他这么干(当然你squash一下,打上他的名字似乎也无所谓),或者你去写个更简洁更好用的。