内核配置

您可以将以下配置设置用作 Android 内核配置的基础。设置会整理到 android-baseandroid-base-ARCHandroid-recommended.cfg 文件中:

  • android-base 选项可实现核心 Android 功能,并且应配置为所有设备指定的选项。
  • android-base-ARCH 选项可实现核心 Android 功能,并且应配置为采用架构 ARCH 的所有设备指定的选项。并非所有架构都具有相应的特定于架构的必需选项文件。如果您的架构没有相应文件,它没有额外特定于架构的 Android 内核配置要求。
  • android-recommended。这些选项可实现高级 Android 功能,设备可选择性启用。

这些配置文件位于 kernel/configs repo 中。使用一组对应您正在使用的内核版本的配置文件。

如需详细了解已用于加强设备内核的控件,请参阅系统和内核安全。如需详细了解必需的设置,请参阅 Android 兼容性定义文档 (CDD)

生成内核配置

对于具有极简 defconfig 格式的设备,您可以在内核树中使用 merge_config.sh 脚本来启用选项:

ARCH=ARCH scripts/kconfig/merge_config.sh <...>/device_defconfig <...>/android-base.cfg <...>/android-base-ARCH.cfg <...>/android-recommended.cfg

这会生成一个 .config 文件,您可以使用该文件保存新的 defconfig 文件或编译启用 Android 功能的新内核。

其他内核配置要求

在某些情况下,平台维护人员可以从多项内核功能中进行选择以满足 Android 依赖项的要求。这类依赖项不能在内核配置片段文件(如上所述)中表示,因为这些文件的格式不支持逻辑表达式。在 Android 9 及更高版本中,兼容性测试套件 (CTS)供应商测试套件 (VTS) 会验证是否满足以下要求:

  • CONFIG_OF=yCONFIG_ACPI=y
  • 4.4 和 4.9 内核具有 CONFIG_ANDROID_LOW_MEMORY_KILLER=y 或同时具有 CONFIG_MEMCG=yCONFIG_MEMCG_SWAP=y
  • CONFIG_DEBUG_RODATA=yCONFIG_STRICT_KERNEL_RWX=y
  • CONFIG_DEBUG_SET_MODULE_RONX=yCONFIG_STRICT_MODULE_RWX=y
  • 仅适用于 ARM64:CONFIG_ARM64_SW_TTBR0_PAN=yCONFIG_ARM64_PAN=y

此外,对于 Android 9 及更高版本中的 4.9 内核,必须将 CONFIG_INET_UDP_DIAG 选项设置为 y

启用 USB 主机模式选项

对于 USB 主机模式音频,请启用以下选项:

CONFIG_SND_USB=y
CONFIG_SND_USB_AUDIO=y
# CONFIG_USB_AUDIO is for a peripheral mode (gadget) driver

对于 USB 主机模式 MIDI,请启用以下选项:

CONFIG_SND_USB_MIDI=y

Seccomp BPF 与 TSYNC

安全计算柏克莱封包过滤器 (Seccomp BPF) 是一种内核安全技术,它支持创建沙盒来定义进程可进行系统调用的上下文。线程同步 (TSYNC) 功能可以实现从多线程程序中使用 Seccomp BPF。这种能力仅限由上游提供 Seccomp 支持的架构(ARM、ARM64、x86 和 x86_64)。

Android Live-Lock 守护程序

Android 10 包含 Android Live-Lock 守护程序 (llkd),该程序旨在发现和减少内核死锁问题。如需详细了解如何使用 llkd,请参阅 Android Live-Lock 守护程序

ARM64 上的 vDSO32

虚拟动态共享对象 (vDSO) 是系统调用的替代选项,如果正确使用和配置,可以降低周期费用。Android 10 增加了对在 64 位内核上使用 vDSO32 的支持(Android 已经支持在 64 位内核上使用 vDSO64 以及在 32 位内核上使用 vDSO32)。在 ARM64 架构上使用 vDSO32 (CONFIG_VDSO_COMPAT) 可以使电池续航时间提升 0.4%,并有助于改进其他方面的性能。

Linux 社区致力于跨架构统一 vDSO。您可以在 Linux 内核中设置 vDSO,方法是通过 arm32 编译器三元组启用具有 CONFIG_COMPATCONFIG_CROSS_COMPILE_COMPAT_VDSO 的 vDSO32。Android 内核团队已将旧版 vDSO 补丁系列向后移植至 Pixel 设备,因此您可以在 Pixel 内核版本(LINUX_FCC_CROSS_COMPILE_ARM32_PREBUILTS_BIN 路径、CROSS_COMPILE_ARM32 参考和 CONFIG_CROSS_COMPILE_ARM32 配置)中找到相关示例。

低 RAM 配置

优化内核/ActivityManager 以减少直接回收

当进程或内核尝试分配(直接分配或因新页面中存在故障而分配)内存页面并且内核已用尽所有可用内存时,就会发生直接回收。在这种情况下,内核需要释放一个页面,并在此过程中阻断分配操作。而这通常又需要磁盘 I/O 清理一个有文件支持的脏页或等待 lowmemorykiller 停止一个进程。最终可能会导致任意线程(包括界面线程)中出现额外 I/O。

为避免出现直接回收,内核已配有可触发 kswapd 或后台回收的水印。此线程会尝试释放页面,以便下次分配真实线程时,能够快速顺利启动。

用于触发后台回收的默认阈值相当低,在 2 GB 设备上约为 2 MB,在 512 MB 设备上约为 636 KB。内核通过后台回收仅能回收几兆的内存。这意味着,任何快速分配超过几 MB 内容的进程都会快速导致直接回收。

在 android-3.4 内核分支中,我们通过补丁 92189d47f66c67e5fd92eafaa287e153197a454f(“添加用于扩展可用内存空间的可调选项”)添加了对内核可调选项的支持。如果您选择将该补丁添加到设备内核中,ActivityManager 会告知内核尝试保留能容纳 3 个全屏 32 bpp 缓冲区的可用内存空间。

这些阈值可通过 config.xml 框架进行配置。

<!-- Device configuration setting the /proc/sys/vm/extra_free_kbytes tunable
in the kernel (if it exists). A high value will increase the amount of memory
that the kernel tries to keep free, reducing allocation time and causing the
lowmemorykiller to kill earlier. A low value allows more memory to be used by
processes but may cause more allocations to block waiting on disk I/O or
lowmemorykiller. Overrides the default value chosen by ActivityManager based
on screen size. 0 prevents keeping any extra memory over what the kernel keeps
by default. -1 keeps the default. -->
<integer name="config_extraFreeKbytesAbsolute">-1</integer>
<!-- Device configuration adjusting the /proc/sys/vm/extra_free_kbytes
tunable in the kernel (if it exists). 0 uses the default value chosen by
ActivityManager. A positive value will increase the amount of memory that the
kernel tries to keep free, reducing allocation time and causing the
lowmemorykiller to kill earlier. A negative value allows more memory to be
used by processes but may cause more allocations to block waiting on disk I/O
or lowmemorykiller. Directly added to the default value chosen by
ActivityManager based on screen size. -->
<integer name="config_extraFreeKbytesAdjust">0</integer>

优化 LowMemoryKiller

ActivityManager 可配置 LowMemoryKiller 的阈值,使其符合它对在每个优先级分段中运行进程时所需的文件支持页面(缓存页面)工作集的预期。如果设备对工作集有很高的要求(例如:如果供应商界面需要更多内存,或者如果添加了更多服务),则可增大阈值。

如果为文件支持页面预留了太多内存,则可减小阈值,以便系统能够在因缓存变得过小而导致磁盘超负荷之前就终止后台进程。

<!-- Device configuration setting the minfree tunable in the lowmemorykiller
in the kernel. A high value will cause the lowmemorykiller to fire earlier,
keeping more memory in the file cache and preventing I/O thrashing, but
allowing fewer processes to stay in memory. A low value will keep more
processes in memory but may cause thrashing if set too low. Overrides the
default value chosen by ActivityManager based on screen size and total memory
for the largest lowmemorykiller bucket, and scaled proportionally to the
smaller buckets. -1 keeps the default. -->
<integer name="config_lowMemoryKillerMinFreeKbytesAbsolute">-1</integer>
<!-- Device configuration adjusting the minfree tunable in the
lowmemorykiller in the kernel. A high value will cause the lowmemorykiller to
fire earlier, keeping more memory in the file cache and preventing I/O
thrashing, but allowing fewer processes to stay in memory. A low value will
keep more processes in memory but may cause thrashing if set too low. Directly
added to the default value chosen by ActivityManager based on screen
size and total memory for the largest lowmemorykiller bucket, and scaled
proportionally to the smaller buckets. 0 keeps the default. -->
<integer name="config_lowMemoryKillerMinFreeKbytesAdjust">0</integer>