Commit Graph

445 Commits

Author SHA1 Message Date
Tom Cherry 43f3d2b190 Build lmkd as C++
Bug: 145669697
Test: build
Change-Id: I4fb2a9a900c8a6915ee84cc3d82434596301b24b
2019-12-13 08:40:30 -08:00
Jing Ji 5c48096d8d lmkd: Support to send unsolicited message to clients
For now the only unsolicited message from lmkd is the process
kills on memory pressure.

Bug: 136036078
Test: atest ApplicationExitInfoTest

Change-Id: I503fd6a45ebab5276460b0ab978ebb2b8431dc0d
Signed-off-by: Jing Ji <jji@google.com>
2019-12-12 08:53:04 -08:00
George Burgess IV a5845e7b60 lmkd: fix an overallocation
pidhash is defined as an array of pointers:

static struct proc** pidhash = NULL;

...So we should be allocating `LINE_MAX * sizeof(struct proc *)` elems
here. Given the current constants here, this saves ~130KB, so not a big
deal, but still convenient.

Caught by clang's static analyzer:
system/memory/lmkd/statslog.c:354:19: warning: Result of 'calloc' is
converted to a pointer of type 'struct proc *', which is incompatible
with sizeof operand type 'struct proc'
[clang-analyzer-unix.MallocSizeof]

Bug: None
Test: TreeHugger
Change-Id: Iee9ca00a3a2a0ecababe9810d2ffcfc42169dd25
2019-12-09 11:02:08 -08:00
Suren Baghdasaryan a93503e3d7 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 <surenb@google.com>
2019-11-07 18:19:15 +00:00
Suren Baghdasaryan 7d3c5c6ea2 lmkd: Add library function to unregister a process
Introduce lmkd_unregister_proc helper function. Fix a bug where
lmkd_pack_set_procremove used a wrong structure as a parameter.

Bug: 129011369
Test: verify process record removal when it is manually killed
Change-Id: I7ab5a499f6b1c6eecfdba4d0a5ec916053e2726a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-11-07 18:18:54 +00:00
Suren Baghdasaryan f2cbefd298 lmkd: Prepare lmkd to support connection from init process
In order to register native services init needs ability to communicate with
lmkd. Make liblmkd_utils library available in recovery mode so that init
can link to it and add a data socket in lmkd to support additional
connection from init. Ensure SOCK_CLOEXEC type for lmkd socket to prevent
init children from inheriting it.

Bug: 129011369
Test: boot and verify native service registration
Change-Id: Iaa4f59282fb10f838f6811571e97d55754b1bd41
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-11-07 18:18:46 +00:00
Suren Baghdasaryan 945658abc4 lmkd: Restrict process record modifications to the client that created it
Associate each registered process with the PID of the lmkd client that
registered it to prevent one client from updating records of another
client.

Bug: 129011369
Test: boot and verify native service registration
Change-Id: Id8ca7bb6314df225d04da6469b523d2cdc237eaa
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-11-07 18:15:04 +00:00
Greg Kaiser 352e1d9575 Merge "lmkd: Remove unnecessary conditional"
am: 6b0e789a21

Change-Id: Iaa24b3f79aa09fa3957c91b55a28c170196afd50
2019-10-11 07:46:38 -07:00
Greg Kaiser 5e80ed5ff6 lmkd: Remove unnecessary conditional
We want poll_handler to be handler_info, so it's more efficient
to just unconditionally assign it.

Test: TreeHugger
Change-Id: I55b5164da1817ef77b5d455eb618f9a2471afc5c
2019-10-10 06:52:23 -07:00
Suren Baghdasaryan f9c3a891c5 Merge "lmkd: Track processes using pidfds"
am: d11c6f7fd8

Change-Id: I7cf396167954f51d3c7a092b5eebc54826f69f88
2019-10-09 11:13:15 -07:00
Suren Baghdasaryan ddcd0f67fc Merge "lmkd: Add support for process death notifications"
am: 530098cebc

Change-Id: I9316ebe6e9b8497f2a34dfb5a6423e216a73811f
2019-10-09 09:47:08 -07:00
Suren Baghdasaryan a10157c0d3 lmkd: Track processes using pidfds
lmkd uses PIDs to track processes, however occasionally a PID of a process
might be reused without lmkd detecting that. This can happen if originally
registered process crashes, PID numbers wrap around and the same PID gets
reused for a different process. In this situation lmkd might kill a wrong
process. To prevent this issue from occurring lmkd will track processes
using their pidfd. During process registration lmkd calls sys_pidfd_open
and stores returned pidfd with the process record. Returned pidfd will not
be reused until lmkd closes it which happens only after the process is
unregistered. This way lmkd ensures that process identification is unique
and can't be reused.

Bug: 135608568
Test: lmkd_unit_test with and without pidfd kernel support
Change-Id: Ida10ea13905c250e47f792cdd6bd2e65aeaa3709
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-08 18:15:34 -07:00
Suren Baghdasaryan bc99e1b702 lmkd: Add support for process death notifications
With pidfd polling support lmkd can detect process death without periodic
polling. Implement mechanism to detect kernel pidfd support using
pidfd_open syscall existence as an indicator. Implement the logic to use
pidfd to wait for process death.

Bug: 135608568
Test: lmkd_unit_test with and without pidfd kernel support
Change-Id: Ic6db7e50893534467f5130a7f998b66fb4451272
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-08 18:15:12 -07:00
Suren Baghdasaryan 770e14e1d6 Merge "lmkd: Fix string null termination in proc_get_size and proc_get_name"
am: d72d846754

Change-Id: I8f94dffd1583861289df8fd68ea5d18ace4238dc
2019-10-07 16:09:35 -07:00
Suren Baghdasaryan 632f1c5365 lmkd: Fix string null termination in proc_get_size and proc_get_name
Fix string null termination and cleanup proc_get_size and proc_get_name
functions.

Bug: 74119935
Test: lmkd_unit_test
Change-Id: Ied176b8d347b672b91866ac59a813d4ff349930a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-07 12:50:38 -07:00
George Burgess IV b384307d44 Merge "lmkd: fix an uninit value bug"
am: 50816f0c21

Change-Id: Ia5d1982c11006897758d203d30e3fc7b62993616
2019-10-02 23:09:48 -07:00
Greg Kaiser b09ca44774 Merge "lmkd: Avoid potential out of buffer write"
am: 3d8fbfada6

Change-Id: Id7063aea8c8d5fb380e123af49cb3bb61c7fd449
2019-10-02 14:06:25 -07:00
George Burgess IV 3b36b904d6 lmkd: fix an uninit value bug
Caught by clang's static analyzer:
system/core/lmkd/lmkd.c:930:9: warning: 1st function call argument is an
uninitialized value [clang-analyzer-core.CallAndMessage]

Bug: None
Test: TreeHugger
Change-Id: I9dc8e97d6aa22ea977fa06553d957a31a9df8819
2019-10-02 19:55:41 +00:00
Treehugger Robot 3f5cb4c0d8 Merge "lmkd: Avoid potential out of buffer write" 2019-10-02 16:29:27 +00:00
Suren Baghdasaryan df37c06db4 Merge "lmkd: Change meminfo_log into killinfo_log and log additional fields"
am: c763c399a9

Change-Id: I9a4d5ee618f5eb3819718c151173f732aa45eeef
2019-10-02 07:35:45 -07:00
Greg Kaiser 259984f730 lmkd: Avoid potential out of buffer write
Our buffer needs enough space for the '\0' terminator character
as well.

Test: TreeHugger
Change-Id: Ib85d08dacf87b93d5f8b35b033ca0ec478868e66
2019-10-02 07:07:32 -07:00
Suren Baghdasaryan 12cacaedc0 lmkd: Change meminfo_log into killinfo_log and log additional fields
meminfo_log is used to log the state of the memory at the time of a kill.
Instead of reporting kill information and meminfo separately let's combine
them into one killinfo_log report. While normal logs can be trimmed by
chatty, meminfo_log uses a separate log context which gives it a better
chance of survival. As a result we will have all the information relevant
to a kill in one report which has higher chance of surviving chatty.

Bug: 74119935
Test: lmkd_unit_test
Change-Id: I83a9c12d538e1fb107721b04fdafc3c6c0d83b60
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-01 23:26:56 +00:00
Suren Baghdasaryan d7213e3008 Merge "lmkd: Isolate statslog related code from lmkd code"
am: 7718c75bc2

Change-Id: Ic514445460bc5c6929d53944bb585d070a26b5ae
2019-10-01 13:55:35 -07:00
Suren Baghdasaryan 613fa51aaf Merge "lmkd: Fix kill failure handling"
am: 46bc39ba23

Change-Id: Ia581ad84feaf56ebff1320e8f69e757590f7a864
2019-10-01 13:55:01 -07:00
Suren Baghdasaryan d4e3b4df6e Merge changes from topic "default_psi_kill_mode"
am: c67350bc68

Change-Id: I1b3f86193a7c2a47829e9fd8403e3b130b79c5fb
2019-10-01 09:46:55 -07:00
Suren Baghdasaryan da4267b006 lmkd: Add optional kill reason description into kill reports
am: fd7518f8c1

Change-Id: Id638e3307138f92a85d985fb1177bc67109f2cc5
2019-10-01 09:46:50 -07:00
Suren Baghdasaryan 73b38b256f lmkd: Enable new kill strategy, add and adjust required system properties
am: 844f26bc24

Change-Id: If215f12f703835f54698ecbb21d1a5ecbbe49f11
2019-10-01 09:46:43 -07:00
Suren Baghdasaryan c74f08fec5 lmkd: Use aggregate zone watermarks as low memory threshold
am: 4787ab452f

Change-Id: Id1f68f09604831055a6d1458c3e97b8f0e1d1868
2019-10-01 09:46:35 -07:00
Suren Baghdasaryan 54cafad7a6 lmkd: Introduce kill strategy based on zone watermarks, swap and thrashing
am: 561cfd9478

Change-Id: Ief53fac72c50bef2735382757c53466cf9222e62
2019-10-01 09:46:26 -07:00
Suren Baghdasaryan e14093b038 lmkd: Support variable polling intervals set by event handlers
am: ef3650f8a0

Change-Id: Ib259965c00ee8497dbc17313ab39a9b281418613
2019-10-01 09:46:15 -07:00
Suren Baghdasaryan 15e5ded118 lmkd: Change zoneinfo parsing to retrieve zone watermarks
am: 94ce3dd573

Change-Id: Ia7957957dc1af77ba9dd7febf95fc37b247c4f3e
2019-10-01 09:45:59 -07:00
Suren Baghdasaryan 521602b550 lmkd: Change procfs read routine to handle files larger than 1 page in size
am: a77b327bb9

Change-Id: I89db84a6360c6479e5649f6bf632babbbb6ca94a
2019-10-01 09:45:46 -07:00
Suren Baghdasaryan 8b016be930 lmkd: Isolate statslog related code from lmkd code
Move statsd related code out of lmkd.c to minimize ifdefs sprinkled around
the code and make it more maintainable.

Bug: 74119935
Test: lmkd_unit_test
Change-Id: Ib22f90fd380b9a31e09ab18ef16787bc07415ddf
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-01 16:36:08 +00:00
Suren Baghdasaryan c2e05b6ffa lmkd: Fix kill failure handling
When lmkd fails to kill it should log the error, remove the process record
and exit immediately.

Bug: 74119935
Test: lmkd_unit_test
Change-Id: I26b0fd873eeed325f7dd56097ec31abc0d63f3a1
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-10-01 16:35:49 +00:00
Suren Baghdasaryan a280a6071c Merge "lmkd: Fix killed process name reporting"
am: 7c2f94bdf0

Change-Id: I82d986b3ef6f19878df8b536ac047d01c0be2ce0
2019-09-30 10:07:29 -07:00
Suren Baghdasaryan 970a26aeb7 lmkd: Prevent killing foreground processes due to thrashing
Page cache thrashing affects device performance and by killing a process
we try to stop it. However if the thrashing application is the one which
user is interacting with then lmkd should not kill it even though it might
affect device performance.

Bug: 141286980
Test: SequentialRWTest CTS test
Change-Id: If86c0e7e8ad9adf1816659562151ca083eaa65c4
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:54:55 +00:00
Suren Baghdasaryan 89454e620c lmkd: Add optional kill reason description into kill reports
Allow kill report to be appended with the explanation of the reasons
killing has been done. This would help identify kill reasons while
troubleshooting lmkd kills.

Change-Id: Ie5dd7a44e51d04c43c2492be8c1bc964d1b03555
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:54:39 +00:00
Suren Baghdasaryan 2f88e15c3a lmkd: Enable new kill strategy, add and adjust required system properties
Enable new kill strategy when PSI mode is used in combination with
ro.lmk.use_minfree_levels=false. Adjust ro.lmk.swap_free_low_percentage,
introduce ro.lmk.psi_partial_stall_ms and ro.lmk.psi_complete_stall_ms
system properties to support two levels of PSI events measuring partial
and complete stalls. Add ro.lmk.use_new_strategy system property to switch
to the old strategy if necessary.

Bug: 132642304
Test: lmkd_unit_test, ACT memory pressure tests
Change-Id: I6f1b65e19dbe9b58c862e5e4255270c82f0afb9a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:54:27 +00:00
Suren Baghdasaryan 81c75b2a33 lmkd: Use aggregate zone watermarks as low memory threshold
Parsing /proc/zoneinfo is expensive and zone watermarks normally do no
change often. Instead of checking free memory per each zone we aggregate
zone watermarks and compare them with MemFree from meminfo as an
approximation of memory being under a given watermark.
zoneinfo parsing is rate limited to once per minute to detect a possible
change of the memory margins from userspace.

Bug: 132642304
Test: lmkd_unit_test, ACT memory pressure tests
Change-Id: If4a8154c004e24324e6de44359de416766139df6
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:54:16 +00:00
Suren Baghdasaryan af2be4c55d lmkd: Introduce kill strategy based on zone watermarks, swap and thrashing
Add new kill strategy which makes kill decisions based on which zone
watermark is breached, how much free swap space is still available and
what percentage of the file-backed page cache has been refaulted. This mode
is designed to be used only with PSI signals. It kills unconditionally when
a critical pressure event is received, therefore PSI stall for that event
should be set to a value representing a truly non-responding system
(currently set to 700ms out of 1sec spent in complete stall). New event
handler also controls polling interval based on current memory conditions.

Bug: 132642304
Test: lmkd_unit_test, ACT memory pressure tests
Change-Id: Ia213ef2bb06b245d651ebf2d813e944b4ae7565f
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:54:05 +00:00
Suren Baghdasaryan e12a067ee0 lmkd: Support variable polling intervals set by event handlers
After a memory event happens event handler can assess current memory
condition and decide if and when lmkd should re-check memory metrics in
order to respond to changing memory conditions. Change the event handler
interface to allow control over polling period and ability to start/extend
polling session.

Bug: 132642304
Test: lmkd_unit_test
Change-Id: Ia74011e943140b6cffbf452ff8e1744b7336eacf
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:53:46 +00:00
Suren Baghdasaryan 92d0eec2d2 lmkd: Change zoneinfo parsing to retrieve zone watermarks
/proc/zoneinfo contains per-node data and each node contains per-zone
section for each zone. Current parser does not recognize this hierarchy
and useful per-zone information like zone watermarks cannot be retrieved.
Change the parser to parse zoneinfo into a hierarchical structure. New
parser also handles up to 2 nodes and can be easily extended to handle
more if needed by changing MAX_NR_NODES.

Bug: 132642304
Test: lmkd_unit_test
Change-Id: I9306289ea6d30d78a261c5d5c29f4f6ea167807d
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:53:34 +00:00
Suren Baghdasaryan 03cb836735 lmkd: Change procfs read routine to handle files larger than 1 page in size
Files like /proc/zoneinfo or /proc/<pid>/status can be larger than 4KB
page size. Change reread_file routine to resize read buffer whenever
it is not big enough to read the entire file. Start with 1-page buffer
and double its size until it's big enough to read the entire file.
Read /proc/zoneinfo during initialization to initialize the buffer
to a big enough size and avoid re-allocations when under memory pressure.

Bug: 137010962
Test: lmkd_unit_test
Change-Id: If9a5b0d27c2f4de9063f0fd0f36f908ece87dcce
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:53:18 +00:00
Suren Baghdasaryan 6dce58d561 lmkd: Fix killed process name reporting
Fix termination of killed process name in proc_get_name function. While at
it also fix the coding style in the function.

Test: lmkd_unit_test
Bug: 141780598
Change-Id: I3f99b3e37b9a9d0750ece94f08f0b50ac839dacb
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-09-30 16:02:35 +00:00
Suren Baghdasaryan ab9cad3d05 Merge "lmkd: Maintain pid to taskname mapping to amend kill reports."
am: e36ae49985

Change-Id: Ibbaed78d9fc26d746801205458b23d3ffc76c0ab
2019-09-27 09:23:51 -07:00
Jim Blackler 90853b6203 lmkd: Maintain pid to taskname mapping to amend kill reports.
Required because the kernel cannot always get the taskname safely at
the time the process is killed (due to competition for mm->mmap_sem).

Test: manually
Bug: 130017100
Signed-off-by: Jim Blackler <jimblackler@google.com>
Change-Id: I27a2c3340da321570f0832d58fe9e79ca031620b
2019-09-26 16:27:03 -07:00
Suren Baghdasaryan 80a2b74405 Merge "lmkd: Prevent non-main threads being registered or killed by lmkd" am: e0b729d214 am: 771882633e
am: 719f2002a1

Change-Id: I873c37af288058df6b4ff9848cce1179e8e8734f
2019-07-12 13:59:51 -07:00
Suren Baghdasaryan a7212f43ae Merge "lmkd: Prevent non-main threads being registered or killed by lmkd" into qt-r1-dev
am: e818cccd58

Change-Id: Ia248d3b149bbaa9852b1bdec6c456bdd2419272d
2019-07-12 13:40:07 -07:00
Suren Baghdasaryan fe8419215a Merge "lmkd: Prevent non-main threads being registered or killed by lmkd"
am: e0b729d214

Change-Id: I11738b8c2c7acfa4947976e8b12ac9b94e7fc8ac
2019-07-12 13:39:47 -07:00
Suren Baghdasaryan 513edaf0d6 lmkd: Prevent non-main threads being registered or killed by lmkd
Only thread group leaders should be registered with lmkd. Add a check to
ignore any non-leader TIDs and generate an error if such condition is
detected. Run the same check before killing a process to detect cases of
non-leader TIDs being used to kill a process. This might happen if PIDs
overflow and previously registered PID gets reused for a non-leader
thread in the following scenario:

1. pid X is a thread group leader and is registered with lmkd
2. pid X dies without lmkd knowing it and pid gets recycled
3. process Y creates a thread with tid X
4. lmkd kills pid X which results in process Y being killed

Bug: 136408020
Test: lmkd_unit_test
Change-Id: I46c5a0b273f2b72cefc20ec59b80b4393f2a1a37
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2019-07-12 15:12:16 +00:00