From 1447120f78bd653ed7d3501f4c1588a1aec15d99 Mon Sep 17 00:00:00 2001 From: Carlos Galo Date: Wed, 7 Dec 2022 23:39:05 +0000 Subject: [PATCH] Set memlock rlimit to 64KB Defaulting Android to limit memlock to 64KB. This will help preventing pages from being swapped until the app is killed it's memory will stay resident. CTS test is enforced only in U+ devies. Bug: 201797650 Test: Added new test to verify we are memlock at or under 64KB Change-Id: I5a9e9da12f6df5a056ee47d0593c13e9c779e054 --- init/init_test.cpp | 16 ++++++++++++++++ rootdir/init.rc | 3 +++ 2 files changed, 19 insertions(+) diff --git a/init/init_test.cpp b/init/init_test.cpp index 18a08c748..1ab69acb3 100644 --- a/init/init_test.cpp +++ b/init/init_test.cpp @@ -21,8 +21,10 @@ #include #include #include +#include #include #include +#include #include "action.h" #include "action_manager.h" @@ -626,6 +628,20 @@ service A something ASSERT_EQ(1u, parser.parse_error_count()); } +TEST(init, MemLockLimit) { + // Test is enforced only for U+ devices + if (android::base::GetIntProperty("ro.vendor.api_level", 0) < __ANDROID_API_U__) { + GTEST_SKIP(); + } + + // Verify we are running memlock at, or under, 64KB + const unsigned long max_limit = 65536; + struct rlimit curr_limit; + ASSERT_EQ(getrlimit(RLIMIT_MEMLOCK, &curr_limit), 0); + ASSERT_LE(curr_limit.rlim_cur, max_limit); + ASSERT_LE(curr_limit.rlim_max, max_limit); +} + class TestCaseLogger : public ::testing::EmptyTestEventListener { void OnTestStart(const ::testing::TestInfo& test_info) override { #ifdef __ANDROID__ diff --git a/rootdir/init.rc b/rootdir/init.rc index 323d9cb3c..49841ed47 100644 --- a/rootdir/init.rc +++ b/rootdir/init.rc @@ -47,6 +47,9 @@ on early-init # Allow up to 32K FDs per process setrlimit nofile 32768 32768 + # set RLIMIT_MEMLOCK to 64KB + setrlimit memlock 65536 65536 + # Set up linker config subdirectories based on mount namespaces mkdir /linkerconfig/bootstrap 0755 mkdir /linkerconfig/default 0755