Android 13 添加进程白名单方法

1 简介

本文主要介绍,在Android 13中通过设置进程oomadj,来防止进程容易被lmkd给杀掉。
该问题起始点在于:我在进行CTS测试时,发现测试进程总是由于lowmemory给杀死。因此想设法给该进程保活,在研究方法时,发现android中当出现了lowmemory时,会根据进程的adj来判断优先将哪些进程杀死。因此就想通过修改对应进程的adj来实现进程保活。

2 修改方式

From 659b5d396d67e7a664905d31b98e7d324c6bbc09 Mon Sep 17 00:00:00 2001
From: zhaohaohao <tony.zhao@faiot.com>
Date: Fri, 08 Sep 2023 16:09:50 +0800
Subject: [PATCH] [FT01GMS-26] CtsMediaMiscTestCases : testReclaimResourceSecureVsNonsecure

Change-Id: I00c79bcb9482f88bb00002df6861c6cffc6c174f
---

diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index ab47fce..16d28c4 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -5180,7 +5180,10 @@
     <bool name="config_pdp_reject_enable_retry">false</bool>
     <!-- pdp data reject retry delay in ms -->
     <integer name="config_pdp_reject_retry_delay_ms">-1</integer>
-
+    <string-array translatable="false" name="low_memory_killer_tracker_whitelist" >
+	    <item>"android.media.misc.cts"</item>
+	    <item>"android.process.media"</item>
+     </string-array>
     <!-- Duration in milliseconds for device to vibrate on mash press on power
          button. -->
     <integer name="config_mashPressVibrateTimeOnPowerButton">0</integer>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 1ae5fed..d25ff4b 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -4465,7 +4465,7 @@
   <java-symbol type="string" name="config_pdp_reject_user_authentication_failed" />
   <java-symbol type="string" name="config_pdp_reject_service_not_subscribed" />
   <java-symbol type="string" name="config_pdp_reject_multi_conn_to_same_pdn_not_allowed" />
-
+  <java-symbol type="array" name="low_memory_killer_tracker_whitelist" />
   <java-symbol type="array" name="config_notificationMsgPkgsAllowedAsConvos" />
 
   <!-- Binder heavy hitter watcher configs -->
diff --git a/services/core/java/com/android/server/am/OomAdjuster.java b/services/core/java/com/android/server/am/OomAdjuster.java
index 3f62867..6ef2e6b 100644
--- a/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/services/core/java/com/android/server/am/OomAdjuster.java
@@ -105,7 +105,7 @@
 import android.util.BoostFramework;
 import android.util.Slog;
 import android.util.proto.ProtoOutputStream;
-
+import android.util.Log;
 import com.android.internal.annotations.CompositeRWLock;
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
@@ -119,7 +119,8 @@
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
-
+import java.util.List;
+import android.content.res.Resources;
 /**
  * All of the code required to compute proc states and oom_adj values.
  */
@@ -139,7 +140,7 @@
     static final String OOM_ADJ_REASON_ALLOWLIST = OOM_ADJ_REASON_METHOD + "_allowlistChange";
     static final String OOM_ADJ_REASON_PROCESS_BEGIN = OOM_ADJ_REASON_METHOD + "_processBegin";
     static final String OOM_ADJ_REASON_PROCESS_END = OOM_ADJ_REASON_METHOD + "_processEnd";
-
+   
     /**
      * Flag {@link android.content.Context#BIND_INCLUDE_CAPABILITIES} is used
      * to pass while-in-use capabilities from client process to bound service. In targetSdkVersion
@@ -260,7 +261,7 @@
     private final ArrayDeque<ProcessRecord> mTmpQueue;
     private final ArraySet<ProcessRecord> mPendingProcessSet = new ArraySet<>();
     private final ArraySet<ProcessRecord> mProcessesInCycle = new ArraySet<>();
-
+    private List<String> mLmKillerBypassPackages = new ArrayList<String>();
     /**
      * Flag to mark if there is an ongoing oomAdjUpdate: potentially the oomAdjUpdate
      * could be called recursively because of the indirect calls during the update;
@@ -309,6 +310,9 @@
         mConstants = mService.mConstants;
         mCachedAppOptimizer = new CachedAppOptimizer(mService);
         mCacheOomRanker = new CacheOomRanker(service);
+        String[] lmKillerTrackerWhitelist = Resources.getSystem().getStringArray(
+                com.android.internal.R.array.low_memory_killer_tracker_whitelist);
+        mLmKillerBypassPackages = Arrays.asList(lmKillerTrackerWhitelist);
 
         if(mPerf != null) {
             mMinBServiceAgingTime = Integer.valueOf(mPerf.perfGetProp("ro.vendor.qti.sys.fw.bservice_age", "5000"));
@@ -353,6 +357,23 @@
                 / ProcessList.CACHED_APP_IMPORTANCE_LEVELS;
     }
 
+
+
+    private boolean isInWhitelist(ProcessRecord pr) {
+         String pkgName = pr.info.packageName;
+ 
+         for (String token : mLmKillerBypassPackages) {
+             if (pkgName.startsWith(token)) {
+                 return true;
+             }
+         }
+         return false;
+     }
+
+
+
+
+
     void initSettings() {
         mCachedAppOptimizer.init();
         mCacheOomRanker.init(ActivityThread.currentApplication().getMainExecutor());
@@ -2655,6 +2676,7 @@
     @GuardedBy({"mService", "mProcLock"})
     private boolean applyOomAdjLSP(ProcessRecord app, boolean doingAll, long now,
             long nowElapsed, String oomAdjReson) {
+	   Log.e("zhh", "applyOomAdjLSP----------------");
         boolean success = true;
         final ProcessStateRecord state = app.mState;
         final UidRecord uidRec = app.getUidRecord();
@@ -2715,7 +2737,7 @@
                     }
                 }
             }
-            ProcessList.setOomAdj(app.getPid(), app.uid, app.mState.getCurAdj());
+           /* ProcessList.setOomAdj(app.getPid(), app.uid, app.mState.getCurAdj());
             if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
                 String msg = "Set " + app.getPid() + " " + app.processName + " adj "
                         + state.getCurAdj() + ": " + state.getAdjType();
@@ -2725,8 +2747,39 @@
             if (uidRec != null) {
                 uidRec.noteProcAdjChanged();
             }
-            state.setVerifiedAdj(ProcessList.INVALID_ADJ);
-        }
+            state.setVerifiedAdj(ProcessList.INVALID_ADJ);*/
+
+
+            boolean isAppWhiteProcess = false;
+            if (isInWhitelist(app) && (state.getCurAdj() > ProcessList.PERSISTENT_SERVICE_ADJ))
+                isAppWhiteProcess = true;
+            if (isAppWhiteProcess) {
+                Slog.d("zhh", "isAppWhiteProcess so not kill!");
+                Slog.d(TAG, "isAppWhiteProcess so not kill!");
+                ProcessList.setOomAdj(app.getPid(), app.uid, ProcessList.PERSISTENT_SERVICE_ADJ);
+                if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
+                    String msg = "Set " + app.getPid() + " " + app.processName + " adj "
+                            + state.getCurAdj() + ": " + state.getAdjType();
+                    reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
+                }
+                state.setSetAdj(ProcessList.PERSISTENT_SERVICE_ADJ);
+                state.setVerifiedAdj(ProcessList.INVALID_ADJ);
+            } else {
+                ProcessList.setOomAdj(app.getPid(), app.uid, state.getCurAdj());
+                if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
+                    String msg = "Set " + app.getPid() + " " + app.processName + " adj "
+                            + state.getCurAdj() + ": " + state.getAdjType();
+                    reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
+                }
+                state.setSetAdj(state.getCurAdj());
+                if (uidRec != null) {
+                     uidRec.noteProcAdjChanged();
+                }
+                state.setVerifiedAdj(ProcessList.INVALID_ADJ);
+
+
+           }
+	}
 
         final int curSchedGroup = state.getCurrentSchedulingGroup();
         if (state.getSetSchedGroup() != curSchedGroup) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值