內核配置

使用以下配置設定作為 Android 核心配置的基礎。 android-baseandroid-base- ARCHandroid-recommended的設定被組織到.cfg檔案中:

  • android-base選項啟用核心 Android 功能,並應依照所有裝置的指定進行設定。
  • android-base- ARCH選項啟用核心 Android 功能,並且應依照ARCH架構的所有裝置的指定進行配置。並非所有體系結構都有特定於體系結構所需選項的對應檔案。如果您的架構沒有文件,則它沒有針對 Android 的其他特定於架構的核心配置要求。
  • android-recommended 。這些選項可啟用進階 Android 功能,並且對於裝置來說是可選的。

這些設定檔位於kernel/configs儲存庫中。使用與您正在使用的核心版本相對應的設定檔集。

有關為增強設備內核而採取的控制措施的詳細信息,請參閱系統和內核安全性。有關所需設定的詳細信息,請參閱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

帶有 TSYNC 的 Seccomp BPF

安全計算伯克利資料包過濾器 (Seccomp BPF) 是一種核心安全技術,可以建立沙箱來定義進程可以在其中進行系統呼叫的上下文。線程同步 (TSYNC) 功能允許在多線程程式中使用 Seccomp BPF。此功能僅限於具有 Seccomp 支援上游的架構(ARM、ARM64、x86 和 x86_64)。

Android 活鎖守護程式

Android 10 包含 Android Live-Lock Daemon ( llkd ),旨在捕捉和緩解核心死鎖。有關使用llkd的詳細信息,請參閱Android Live-Lock Daemon

ARM64 上的 vDSO32

虛擬動態共享物件 (vDSO) 是系統呼叫的替代方案,如果正確使用和配置,可以降低週期成本。 Android 10 新增了對 64 位元核心上的 vDSO32 的支援(Android 已支援 64 位元核心上的 vDSO64 和 32 位元核心上的 vDSO32)。在 ARM64 架構上使用 vDSO32 ( CONFIG_VDSO_COMPAT ) 可將電池壽命延長 0.4%,並提高其他效能。

Linux 社群正積極致力於統一跨架構的 vDSO 。您可以透過使用CONFIG_COMPAT啟用 vDSO32 以及使用 arm32 編譯器三元組的CONFIG_CROSS_COMPILE_COMPAT_VDSO在 Linux 核心中設定 vDSO。 Android 核心團隊已將舊版的 vDSO 修補程式系列向後移植到 Pixel 裝置中,因此您可以在 Pixel 核心版本中找到範例( LINUX_FCC_CROSS_COMPILE_ARM32_PREBUILTS_BIN路徑、 CROSS_COMPILE_ARM32參考和CONFIG_PREBUILTS_BIN路徑、 CROSS_COMPILE_ARM32參考和CONFIG_CROSS_COMPILE_ARM32 )。

低記憶體配置

調整內核/ActivityManager以減少直接回收

當進程或核心嘗試分配記憶體頁(直接分配或由於新頁出錯)並且核心已使用所有可用的空閒記憶體時,會發生直接回收。這要求內核在釋放頁面時阻止分配。這反過來通常需要磁碟 I/O 來刷新髒檔案支援的頁面或等待lowmemorykiller停止進程。這可能會導致任何執行緒(包括 UI 執行緒)中出現額外的 I/O。

為了避免直接回收,核心具有觸發kswapd或後台回收的水印。這是一個嘗試釋放頁面的線程,因此下次真正的線程分配時,它可以快速成功。

觸發後台回收的預設閾值相當低,在 2 GB 裝置上約為 2 MB,在 512 MB 裝置上約為 636 KB。核心在後台回收中僅回收幾兆位元組的記憶體。這意味著任何快速分配超過幾兆位元組的進程都將很快實現直接回收。

Android-3.4 核心分支中新增了對核心可調參數的支持,作為補丁 92189d47f66c67e5fd92eafaa287e153197a454f(「新增額外可用的千位元組可調參數」)。將這個補丁精挑細選到裝置核心中,可以讓ActivityManager告訴核心嘗試保持三個全螢幕 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>

調低記憶體殺手

ActivityManager配置LowMemoryKiller的閾值,以符合其在每個優先權儲存桶中執行進程所需的檔案支援頁面(快取頁面)工作集的期望。如果設備對工作集有很高的要求,例如,如果供應商 UI 需要更多記憶體或添加了更多服務,則可以提高閾值。

如果為檔案支援的頁面保留太多內存,則可以降低閾值,這樣後台進程就會在由於快取變得太小而發生磁碟抖動之前被終止。

<!-- 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>