From 721d7c7fa31315fcb0483eb4400278bcc80cd9ad Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Wed, 21 Mar 2018 12:24:58 -0700 Subject: [PATCH] lmkd: only mlockall() if enabled Move process initialization, mlockall() and sched_setscheduler() to only occur if initialization succeeds and is enabled. This conserves mlock'd memory and a FIFO processing slot if (deprecated) kernel lowmemorykiller is active. Cleanup: Android coding standard compliance. Test: lmkd_unit_test Bug: 33808187 Bug: 72838192 Change-Id: I51af0235c8eca4ef958ef2d83a8071ff58b49d78 --- lmkd/Android.bp | 2 +- lmkd/lmkd.c | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lmkd/Android.bp b/lmkd/Android.bp index d172755bd..2efda86cd 100644 --- a/lmkd/Android.bp +++ b/lmkd/Android.bp @@ -3,8 +3,8 @@ cc_binary { srcs: ["lmkd.c"], shared_libs: [ - "liblog", "libcutils", + "liblog", ], local_include_dirs: ["include"], cflags: ["-Werror"], diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c index 72f9f7b24..f7c90ec0b 100644 --- a/lmkd/lmkd.c +++ b/lmkd/lmkd.c @@ -29,8 +29,8 @@ #include #include #include -#include #include +#include #include #include @@ -77,7 +77,7 @@ #define SYSTEM_ADJ (-900) /* default to old in-kernel interface if no memory pressure events */ -static int use_inkernel_interface = 1; +static bool use_inkernel_interface = true; static bool has_inkernel_module; /* memory pressure levels */ @@ -326,8 +326,9 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet) { return; } - if (use_inkernel_interface) + if (use_inkernel_interface) { return; + } if (params.oomadj >= 900) { soft_limit_mult = 0; @@ -393,8 +394,9 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet) { static void cmd_procremove(LMKD_CTRL_PACKET packet) { struct lmk_procremove params; - if (use_inkernel_interface) + if (use_inkernel_interface) { return; + } lmkd_pack_get_procremove(packet, ¶ms); pid_remove(params.pid); @@ -1205,21 +1207,28 @@ int main(int argc __unused, char **argv __unused) { kill_timeout_ms = (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0); - // MCL_ONFAULT pins pages as they fault instead of loading - // everything immediately all at once. (Which would be bad, - // because as of this writing, we have a lot of mapped pages we - // never use.) Old kernels will see MCL_ONFAULT and fail with - // EINVAL; we ignore this failure. - // - // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT - // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault - // in pages. - if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && errno != EINVAL) - ALOGW("mlockall failed: errno=%d", errno); + if (!init()) { + if (!use_inkernel_interface) { + /* + * MCL_ONFAULT pins pages as they fault instead of loading + * everything immediately all at once. (Which would be bad, + * because as of this writing, we have a lot of mapped pages we + * never use.) Old kernels will see MCL_ONFAULT and fail with + * EINVAL; we ignore this failure. + * + * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT + * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault + * in pages. + */ + if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) { + ALOGW("mlockall failed %s", strerror(errno)); + } + + sched_setscheduler(0, SCHED_FIFO, ¶m); + } - sched_setscheduler(0, SCHED_FIFO, ¶m); - if (!init()) mainloop(); + } ALOGI("exiting"); return 0;