修改Android系统lowmemorykiller.c保证某个应用不被kill掉

项目中遇到recovery升级系统zip包的时候因为内存不足被kill掉的情况,可通过修改内核的

drivers/staging/android/lowmemorykiller.c 

#ifdef CONFIG_MP_CMA_PATCH_DELAY_FREE
extern void set_delay_free_min_mem(int min_mem);
#endif
static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
{
#ifdef CONFIG_MP_DEBUG_TOOL_MEMORY_USAGE_MONITOR
        unsigned long time_start = jiffies;
#endif
#ifdef CONFIG_MP_CMA_PATCH_OOM_KILLER
        int large_order_free = 0;       /* for memory order larger than 2 */
#endif

        struct task_struct *tsk;
        struct task_struct *selected = NULL;
        int rem = 0;
        int tasksize;
        int i;
        short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
        int minfree = 0;
        int selected_tasksize = 0;
        short selected_oom_score_adj;
        int array_size = ARRAY_SIZE(lowmem_adj);
        int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
        int other_file = global_page_state(NR_FILE_PAGES) -
                                                global_page_state(NR_SHMEM);
        int free_cma = 0;
#ifdef CONFIG_MP_Android_MSTAR_ADJUST_LOW_MEM_KILLER_POLICY
        int total_free = 0;
#endif

#ifdef CONFIG_CMA
        if (allocflags_to_migratetype(sc->gfp_mask) != MIGRATE_MOVABLE)
                free_cma = global_page_state(NR_FREE_CMA_PAGES);
#ifdef CONFIG_MP_CMA_PATCH_AGRESSIVE_KILL_PROCESS_TO_FREE_CMA_PAGE
    if(lowmem_adj[4])
        set_early_kill_oom_adj_threshold(lowmem_adj[4]);
#endif
#ifdef CONFIG_MP_CMA_PATCH_DELAY_FREE
    if(lowmem_minfree[5])
        set_delay_free_min_mem(lowmem_minfree[5]);
#endif
#endif

#ifdef CONFIG_MP_Android_MSTAR_ADJUST_LOW_MEM_KILLER_POLICY
        total_free = other_free - free_cma + other_file - global_page_state(NR_FILE_MAPPED);
#endif

        if (lowmem_adj_size < array_size)
                array_size = lowmem_adj_size;
        if (lowmem_minfree_size < array_size)
                array_size = lowmem_minfree_size;
        for (i = 0; i < array_size; i++) {
                minfree = lowmem_minfree[i];
#ifdef CONFIG_MP_Android_MSTAR_ADJUST_LOW_MEM_KILLER_POLICY
                if (total_free < minfree) {
#else
                if ((other_free - free_cma) < minfree && other_file < minfree) {
#endif
                        min_score_adj = lowmem_adj[i];
                        break;
                }
        }
        if (sc->nr_to_scan > 0)
                lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %hd\n",
                                sc->nr_to_scan, sc->gfp_mask, other_free,
                                other_file, min_score_adj);
        rem = global_page_state(NR_ACTIVE_ANON) +
                global_page_state(NR_ACTIVE_FILE) +
                global_page_state(NR_INACTIVE_ANON) +
                global_page_state(NR_INACTIVE_FILE);

        /* Remove this due to the return value rem will affect
         * whether next lowmem_shrink() will be done @ shrink_slab().
         * The rem value is sum of active and inactive anonymous pages, active and inactive file pages,
         * which means reclaimable page count, and this rem has no relationship with free_cma.
         */
        //rem -= free_cma;

        /* enhanced oom killer, for browser */
#ifdef CONFIG_MP_CMA_PATCH_OOM_KILLER
        if(show_debug_info > 0)
        {
                large_order_free = count_order_free_mem(3);     // only consider if order >= 3

                for (i = 0; i < array_size; i++) {
                        minfree = lowmem_minfree[i];
                        if (large_order_free < minfree && other_file < minfree) {
                                min_score_adj = lowmem_adj[i];
                                break;
                        }
                }
        }
#endif

        if (sc->nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
                lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
                             sc->nr_to_scan, sc->gfp_mask, rem);

#ifdef CONFIG_MP_DEBUG_TOOL_MEMORY_USAGE_MONITOR
                atomic_add((jiffies-time_start), &time_cnt_table[7].lone_time);
                atomic_inc(&time_cnt_table[7].do_cnt);
#endif
                return rem;
        }
        selected_oom_score_adj = min_score_adj;

        rcu_read_lock();
        for_each_process(tsk) {
                struct task_struct *p;
                short oom_score_adj;

                if (tsk->flags & PF_KTHREAD)
                        continue;

                p = find_lock_task_mm(tsk);
                if (!p)
                        continue;

                if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
                    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
                        task_unlock(p);
                        rcu_read_unlock();
#ifdef CONFIG_MP_DEBUG_TOOL_MEMORY_USAGE_MONITOR
                        atomic_add((jiffies-time_start), &time_cnt_table[7].lone_time);
                        atomic_inc(&time_cnt_table[7].do_cnt);
#endif
                        return 0;
                }
                if ((strstr(p->comm, "recovery") != NULL) && p->signal->oom_score_adj == 0)
                {
                        task_unlock(p);
                        lowmem_print(1, "lowmem_shrink: do not kill recovery process;\n");
                        continue;
                }
                oom_score_adj = p->signal->oom_score_adj;
                if (oom_score_adj < min_score_adj) {
                        task_unlock(p);
                        continue;
                }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值