README.md
1<!--
2Project: /_project.yaml
3Book: /_book.yaml
4
5{% include "_versions.html" %}
6-->
7
8<!--
9 Copyright 2020 The Android Open Source Project
10
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14
15 http://www.apache.org/licenses/LICENSE-2.0
16
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22-->
23
24# Android Live-LocK Daemon (llkd)
25
26Android 10 <!-- {{ androidQVersionNumber }} --> includes the Android Live-LocK Daemon
27(`llkd`), which is designed to catch and mitigate kernel deadlocks. The `llkd`
28component provides a default standalone implementation, but you can
29alternatively integrate the `llkd` code into another service, either as part of
30the main loop or as a separate thread.
31
32## Detection scenarios <!-- {:#detection-scenarios} -->
33
34The `llkd` has two detection scenarios: Persistent D or Z state, and persistent
35stack signature.
36
37### Persistent D or Z state <!-- {:#persistent-d-or-z-state} -->
38
39If a thread is in D (uninterruptible sleep) or Z (zombie) state with no forward
40progress for longer than `ro.llk.timeout_ms or ro.llk.[D|Z].timeout_ms`, the
41`llkd` kills the process (or parent process). If a subsequent scan shows the
42same process continues to exist, the `llkd` confirms a live-lock condition and
43panics the kernel in a manner that provides the most detailed bug report for the
44condition.
45
46The `llkd` includes a self watchdog that alarms if `llkd` locks up; watchdog is
47double the expected time to flow through the mainloop and sampling is every
48`ro.llk_sample_ms`.
49
50### Persistent stack signature <!-- {:#persistent-stack-signature} -->
51
52For userdebug releases, the `llkd` can detect kernel live-locks using persistent
53stack signature checking. If a thread in any state except Z has a persistent
54listed `ro.llk.stack` kernel symbol that is reported for longer than
55`ro.llk.timeout_ms` or `ro.llk.stack.timeout_ms`, the `llkd` kills the process
56(even if there is forward scheduling progress). If a subsequent scan shows the
57same process continues to exist, the `llkd` confirms a live-lock condition and
58panics the kernel in a manner that provides the most detailed bug report for the
59condition.
60
61Note: Because forward scheduling progress is allowed, the `llkd` does not
62perform [ABA detection](https://en.wikipedia.org/wiki/ABA_problem){:.external}.
63
64The `lldk` check persists continuously when the live lock condition exists and
65looks for the composed strings `" symbol+0x"` or `" symbol.cfi+0x"` in the
66`/proc/pid/stack` file on Linux. The list of symbols is in `ro.llk.stack` and
67defaults to the comma-separated list of
68"`cma_alloc,__get_user_pages,bit_wait_io,wait_on_page_bit_killable`".
69
70Symbols should be rare and short-lived enough that on a typical system the
71function is seen only once in a sample over the timeout period of
72`ro.llk.stack.timeout_ms` (samples occur every `ro.llk.check_ms`). Due to lack
73of ABA protection, this is the only way to prevent a false trigger. The symbol
74function must appear below the function calling the lock that could contend. If
75the lock is below or in the symbol function, the symbol appears in all affected
76processes, not just the one that caused the lockup.
77
78## Coverage <!-- {:#coverage} -->
79
80The default implementation of `llkd` does not monitor `init`, `[kthreadd]`, or
81`[kthreadd]` spawns. For the `llkd` to cover `[kthreadd]`-spawned threads:
82
83* Drivers must not remain in a persistent D state,
84
85OR
86
87* Drivers must have mechanisms to recover the thread should it be killed
88 externally. For example, use `wait_event_interruptible()` instead of
89 `wait_event()`.
90
91If one of the above conditions is met, the `llkd` ignorelist can be adjusted to
92cover kernel components. Stack symbol checking involves an additional process
93ignore list to prevent sepolicy violations on services that block `ptrace`
94operations.
95
96## Android properties <!-- {:#android-properties} -->
97
98The `llkd` responds to several Android properties (listed below).
99
100* Properties named `prop_ms` are in milliseconds.
101* Properties that use comma (,) separator for lists use a leading separator to
102 preserve the default entry, then add or subtract entries with optional plus
103 (+) and minus (-) prefixes respectively. For these lists, the string "false"
104 is synonymous with an empty list, and blank or missing entries resort to the
105 specified default value.
106
107### ro.config.low_ram <!-- {:#ro-config-low-ram} -->
108
109Device is configured with limited memory.
110
111### ro.debuggable <!-- {:#ro-debuggable} -->
112
113Device is configured for userdebug or eng build.
114
115### ro.llk.sysrq_t <!-- {:#ro-llk-sysrq-t} -->
116
117If property is "eng", the default is not `ro.config.low_ram` or `ro.debuggable`.
118If true, dump all threads (`sysrq t`).
119
120### ro.llk.enable <!-- {:#ro-llk-enable} -->
121
122Allow live-lock daemon to be enabled. Default is false.
123
124### llk.enable <!-- {:#llk-enable} -->
125
126Evaluated for eng builds. Default is `ro.llk.enable`.
127
128### ro.khungtask.enable <!-- {:#ro-khungtask-enable} -->
129
130Allow `[khungtask]` daemon to be enabled. Default is false.
131
132### khungtask.enable <!-- {:#khungtask-enable} -->
133
134Evaluated for eng builds. Default is `ro.khungtask.enable`.
135
136### ro.llk.mlockall <!-- {:#ro-llk-mlockall} -->
137
138Enable call to `mlockall()`. Default is false.
139
140### ro.khungtask.timeout <!-- {:#ro-khungtask-timeout} -->
141
142`[khungtask]` maximum time limit. Default is 12 minutes.
143
144### ro.llk.timeout_ms <!-- {:#ro-llk-timeout-ms} -->
145
146D or Z maximum time limit. Default is 10 minutes. Double this value to set the
147alarm watchdog for `llkd`.
148
149### ro.llk.D.timeout_ms <!-- {:#ro-llk-D-timeout-ms} -->
150
151D maximum time limit. Default is `ro.llk.timeout_ms`.
152
153### ro.llk.Z.timeout_ms <!-- {:#ro-llk-Z-timeout-ms} -->
154
155Z maximum time limit. Default is `ro.llk.timeout_ms`.
156
157### ro.llk.stack.timeout_ms <!-- {:#ro-llk-stack-timeout-ms} -->
158
159Checks for persistent stack symbols maximum time limit. Default is
160`ro.llk.timeout_ms`. **Active only on userdebug or eng builds**.
161
162### ro.llk.check_ms <!-- {:#ro-llk-check-ms} -->
163
164Samples of threads for D or Z. Default is two minutes.
165
166### ro.llk.stack <!-- {:#ro-llk-stack} -->
167
168Checks for kernel stack symbols that if persistently present can indicate a
169subsystem is locked up. Default is
170`cma_alloc,__get_user_pages,bit_wait_io,wait_on_page_bit_killable`
171comma-separated list of kernel symbols. The check doesn't do forward scheduling
172ABA except by polling every `ro.llk_check_ms` over the period
173`ro.llk.stack.timeout_ms`, so stack symbols should be exceptionally rare and
174fleeting (it is highly unlikely for a symbol to show up persistently in all
175samples of the stack). Checks for a match for `" symbol+0x"` or
176`" symbol.cfi+0x"` in stack expansion. **Available only on userdebug or eng
177builds**; security concerns on user builds result in limited privileges that
178prevent this check.
179
180### ro.llk.ignorelist.process <!-- {:#ro-llk-ignorelist-process} -->
181
182The `llkd` does not watch the specified processes. Default is `0,1,2` (`kernel`,
183`init`, and `[kthreadd]`) plus process names
184`init,[kthreadd],[khungtaskd],lmkd,llkd,watchdogd, [watchdogd],[watchdogd/0],...,[watchdogd/get_nprocs-1]`.
185A process can be a `comm`, `cmdline`, or `pid` reference. An automated default
186can be larger than the current maximum property size of 92.
187
188Note: `false` is an extremely unlikely process to want to ignore.
189
190### ro.llk.ignorelist.parent <!-- {:#ro-llk-ignorelist-parent} -->
191
192The `llkd` does not watch processes that have the specified parent(s). Default
193is `0,2,adbd&[setsid]` (`kernel`, `[kthreadd]`, and `adbd` only for zombie
194`setsid`). An ampersand (&) separator specifies that the parent is ignored only
195in combination with the target child process. Ampersand was selected because it
196is never part of a process name; however, a `setprop` in the shell requires the
197ampersand to be escaped or quoted, although the `init rc` file where this is
198normally specified does not have this issue. A parent or target process can be a
199`comm`, `cmdline`, or `pid` reference.
200
201### ro.llk.ignorelist.uid <!-- {:#ro-llk-ignorelist-uid} -->
202
203The `llkd` does not watch processes that match the specified uid(s).
204Comma-separated list of uid numbers or names. Default is empty or false.
205
206### ro.llk.ignorelist.process.stack <!-- {:#ro-llk-ignorelist-process-stack} -->
207
208The `llkd` does not monitor the specified subset of processes for live lock stack
209signatures. Default is process names
210`init,lmkd.llkd,llkd,keystore,ueventd,apexd,logd`. Prevents the sepolicy
211violation associated with processes that block `ptrace` (as these can't be
212checked). **Active only on userdebug and eng builds**. For details on build
213types, refer to [Building Android](/setup/build/building#choose-a-target).
214
215## Architectural concerns <!-- {:#architectural-concerns} -->
216
217* Properties are limited to 92 characters. However, this is not limited for
218 defaults defined in the `include/llkd.h` file in the sources.
219* The built-in `[khungtask]` daemon is too generic and trips on driver code that
220 sits around in D state too much. Switching drivers to sleep, or S state,
221 would make task(s) killable, and need to be resurrectable by drivers on an
222 as-need basis.
223
224## Library interface (optional) <!-- {:#library-interface-optional} -->
225
226You can optionally incorporate the `llkd` into another privileged daemon using
227the following C interface from the `libllkd` component:
228
229```
230#include "llkd.h"
231bool llkInit(const char* threadname) /* return true if enabled */
232unsigned llkCheckMillseconds(void) /* ms to sleep for next check */
233```
234
235If a threadname is provided, a thread automatically spawns, otherwise the caller
236must call `llkCheckMilliseconds` in its main loop. The function returns the
237period of time before the next expected call to this handler.
238