Skip to content

Commit e353d86

Browse files
lmkd: Support process types when registering a process
Add an optional process type field into lmkd registration protocol so that applications can be distinguished from services. Bug: 129011369 Test: boot and verify native service registration Change-Id: Ie610b5d07cbe247a55ab31bc079ee5c5923bea11 Signed-off-by: Suren Baghdasaryan <[email protected]>
1 parent 4c6d3d7 commit e353d86

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lmkd/include/lmkd.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,33 @@ static inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet, struct lmk_ta
8787
return idx * sizeof(int);
8888
}
8989

90+
/* Process types for lmk_procprio.ptype */
91+
enum proc_type {
92+
PROC_TYPE_FIRST,
93+
PROC_TYPE_APP = PROC_TYPE_FIRST,
94+
PROC_TYPE_SERVICE,
95+
PROC_TYPE_COUNT,
96+
};
97+
9098
/* LMK_PROCPRIO packet payload */
9199
struct lmk_procprio {
92100
pid_t pid;
93101
uid_t uid;
94102
int oomadj;
103+
enum proc_type ptype;
95104
};
96105

97106
/*
98107
* For LMK_PROCPRIO packet get its payload.
99108
* Warning: no checks performed, caller should ensure valid parameters.
100109
*/
101-
static inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet, struct lmk_procprio* params) {
110+
static inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet, int field_count,
111+
struct lmk_procprio* params) {
102112
params->pid = (pid_t)ntohl(packet[1]);
103113
params->uid = (uid_t)ntohl(packet[2]);
104114
params->oomadj = ntohl(packet[3]);
115+
/* if field is missing assume PROC_TYPE_APP for backward compatibility */
116+
params->ptype = field_count > 3 ? (enum proc_type)ntohl(packet[4]) : PROC_TYPE_APP;
105117
}
106118

107119
/*
@@ -113,7 +125,8 @@ static inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet, struct lmk_
113125
packet[1] = htonl(params->pid);
114126
packet[2] = htonl(params->uid);
115127
packet[3] = htonl(params->oomadj);
116-
return 4 * sizeof(int);
128+
packet[4] = htonl((int)params->ptype);
129+
return 5 * sizeof(int);
117130
}
118131

119132
/* LMK_PROCREMOVE packet payload */

lmkd/lmkd.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ static void remove_claims(pid_t pid) {
876876
}
877877
}
878878

879-
static void cmd_procprio(LMKD_CTRL_PACKET packet, struct ucred *cred) {
879+
static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred *cred) {
880880
struct proc *procp;
881881
char path[LINE_MAX];
882882
char val[20];
@@ -886,14 +886,19 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet, struct ucred *cred) {
886886
struct passwd *pwdrec;
887887
int tgid;
888888

889-
lmkd_pack_get_procprio(packet, &params);
889+
lmkd_pack_get_procprio(packet, field_count, &params);
890890

891891
if (params.oomadj < OOM_SCORE_ADJ_MIN ||
892892
params.oomadj > OOM_SCORE_ADJ_MAX) {
893893
ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
894894
return;
895895
}
896896

897+
if (params.ptype < PROC_TYPE_FIRST || params.ptype >= PROC_TYPE_COUNT) {
898+
ALOGE("Invalid PROCPRIO process type argument %d", params.ptype);
899+
return;
900+
}
901+
897902
/* Check if registered process is a thread group leader */
898903
tgid = proc_get_tgid(params.pid);
899904
if (tgid >= 0 && tgid != params.pid) {
@@ -920,7 +925,8 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet, struct ucred *cred) {
920925
return;
921926
}
922927

923-
if (per_app_memcg) {
928+
/* lmkd should not change soft limits for services */
929+
if (params.ptype == PROC_TYPE_APP && per_app_memcg) {
924930
if (params.oomadj >= 900) {
925931
soft_limit_mult = 0;
926932
} else if (params.oomadj >= 800) {
@@ -1298,9 +1304,10 @@ static void ctrl_command_handler(int dsock_idx) {
12981304
cmd_target(targets, packet);
12991305
break;
13001306
case LMK_PROCPRIO:
1301-
if (nargs != 3)
1307+
/* process type field is optional for backward compatibility */
1308+
if (nargs < 3 || nargs > 4)
13021309
goto wronglen;
1303-
cmd_procprio(packet, &cred);
1310+
cmd_procprio(packet, nargs, &cred);
13041311
break;
13051312
case LMK_PROCREMOVE:
13061313
if (nargs != 1)

0 commit comments

Comments
 (0)