[PATCH v2 04/11] Documentation: Describe kconfig-audit usage and design

From: Ahmed S. Darwish

Date: Thu Dec 03 2026 - 17:49:59 EST


Document kconfig-audit usage, policy format, design, and dependencies.

For PREEMPT_RT users, mention that kconfig-audit can now be used for
validating a kernel's .config against PREEMPT_RT requirements.

For the kconfig-audit external dependencies error handling code, reference
the newly added documentation as well.

Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
---
.../real-time/kernel-configuration.rst | 3 +
Documentation/tools/index.rst | 1 +
Documentation/tools/kconfig-audit.rst | 517 ++++++++++++++++++
tools/kconfig-audit/audit.py | 7 +-
4 files changed, 527 insertions(+), 1 deletion(-)
create mode 100644 Documentation/tools/kconfig-audit.rst

diff --git a/Documentation/core-api/real-time/kernel-configuration.rst b/Documentation/core-api/real-time/kernel-configuration.rst
index d7f08e7b8760..7ae61d171a75 100644
--- a/Documentation/core-api/real-time/kernel-configuration.rst
+++ b/Documentation/core-api/real-time/kernel-configuration.rst
@@ -14,6 +14,9 @@ Introduction
This document lists the kernel configuration options that might affect a
real-time kernel's worst-case latency. It is intended for system integrators.

+You can validate your kernel's ``.config`` against the guidelines below using
+the ``kconfig-audit`` tool. See Documentation/tools/kconfig-audit.rst
+
Configuration options
=====================

diff --git a/Documentation/tools/index.rst b/Documentation/tools/index.rst
index 1adf4a6f909b..ee87e3599931 100644
--- a/Documentation/tools/index.rst
+++ b/Documentation/tools/index.rst
@@ -10,6 +10,7 @@ more additions are needed here:
.. toctree::
:maxdepth: 1

+ kconfig-audit
rtla/index
rv/index
python
diff --git a/Documentation/tools/kconfig-audit.rst b/Documentation/tools/kconfig-audit.rst
new file mode 100644
index 000000000000..5b8e2fa842b1
--- /dev/null
+++ b/Documentation/tools/kconfig-audit.rst
@@ -0,0 +1,517 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============================
+Kernel configuration checker
+============================
+
+``kconfig-audit`` is a Python tool for checking a kernel ``.config`` file
+against a set of policy rules; e.g. PREEMPT_RT kernel configuration
+requirements. It generates a sanity report showing whether the supplied
+kernel configuration satisfies a known profile.
+
+.. contents:: Table of Contents
+ :depth: 2
+ :local:
+
+Invocation
+==========
+
+The checker can be invoked from anywhere in the file system::
+
+ audit.py list-policies
+
+ audit.py list-api-versions
+
+Check a ``.config`` against a policy known to the checker::
+
+ audit.py check -c path/to/.config -p preempt_rt
+
+Example invocation from within the kernel tree:
+
+.. code-block:: shell
+
+ ~/linux$ make defconfig
+ ~/linux$ tools/kconfig-audit/audit.py check -c .config -p preempt_rt
+
+External dependencies
+=====================
+
+The checker requires the ``PyYAML`` and ``jsonschema`` Python modules.
+Install them using your distribution's package manager, for example:
+
+.. table::
+ :align: left
+
+ ============ ================== ======================
+ Distribution PyYAML jsonschema
+ ============ ================== ======================
+ Debian ``python3-yaml`` ``python3-jsonschema``
+ Red Hat ``python3-pyyaml`` ``python3-jsonschema``
+ Arch Linux ``python-yaml`` ``python-jsonschema``
+ ============ ================== ======================
+
+Report format
+=============
+
+When the configuration does not match the policy, the checker prints a
+report grouped by severity.
+
+A typical report looks like::
+
+ Configuration Issues with severity = FATAL
+ ==========================================
+
+ * CONFIG_PREEMPT_RT is not in .config
+
+ Rationale:
+ https://docs.kernel.org/core-api/real-time/kernel-configuration.html#config-preempt-rt
+
+ Configuration Issues with severity = MEDIUM
+ ===========================================
+
+ * CONFIG_EFI_DISABLE_RUNTIME is disabled
+
+ Rationale:
+ https://docs.kernel.org/core-api/real-time/kernel-configuration.html#config-efi-disable-runtime
+
+After printing the report, the checker exits with an error code.
+
+Exit codes
+==========
+
+When checking a ``.config`` file against a policy, kconfig-audit uses the
+following exit codes:
+
+.. table::
+ :align: left
+
+ =================== ====== =====================================================
+ Exit code Value Meaning
+ =================== ====== =====================================================
+ Success 0 The ``.config`` file matches selected policy.
+ ``EX_CONFIG`` 78 The ``.config`` file does not match selected policy.
+ ``EX_NOINPUT`` 66 One or more input files could not be opened.
+ ``EX_DATAERR`` 65 The ``.config``, the selected policy, or the tool's
+ internal schema is malformed or invalid.
+ ``EX_UNAVAILABLE`` 69 Python 3.9 or later, or a required Python module, is
+ unavailable.
+ Usage error 2 The command-line arguments are invalid.
+ =================== ====== =====================================================
+
+Policy file format
+==================
+
+Configuration policy files are YAML documents. A minimal policy looks like
+this:
+
+.. code-block:: yaml
+
+ api_version: kconfig-audit.kernel.org/v1
+ kind: Policy
+
+ rules:
+ - name: CONFIG_PREEMPT_RT
+ expected: enabled
+ severity: fatal
+
+The supplied PREEMPT_RT policy resides in the kernel source tree at::
+
+ tools/kconfig-audit/policy/preempt_rt.yml
+
+For field-by-field descriptions of the policy's top-level and rule fields,
+see :ref:`Policy file reference <policy-file-reference>`.
+
+Rule matching
+=============
+
+Exact symbol rules
+------------------
+
+An exact symbol rule checks only the named symbol. For example:
+
+.. code-block:: yaml
+
+ rules:
+ - name: CONFIG_EXT4_FS
+ expected: enabled
+ severity: high
+
+passes the checked ``.config`` if it contains either::
+
+ CONFIG_EXT4_FS=y
+
+or::
+
+ CONFIG_EXT4_FS=m
+
+It fails the checked ``.config`` if it has::
+
+ # CONFIG_EXT4_FS is not set
+
+or if such a symbol is absent from ``.config``.
+
+Pattern rules
+-------------
+
+A pattern rule ensures that all symbols in the checked ``.config`` matching
+its regular expression pattern satisfies expectations.
+
+For example, for below policy:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_SLUB_DEBUG.*
+ expected: enabled
+ severity: high
+
+if the checked ``.config`` has below symbols::
+
+ CONFIG_SLUB_DEBUG
+ CONFIG_SLUB_DEBUG_ON
+
+then, they're expected to be enabled.
+
+Rule precedence
+===============
+
+Rule order in the YAML file **does not** define precedence. Moving a rule
+earlier or later in the file does not change the meaning of the policy.
+
+Exact rules take precedence over pattern rules
+----------------------------------------------
+
+For example, for below policy:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_USB
+ expected: disabled
+ severity: high
+
+ - name: CONFIG_USB_GADGET
+ expected: disabled
+ severity: low
+
+if the checked ``.config`` has ``CONFIG_USB_GADGET`` enabled, the only the
+policy's exact rule will trigger an issue, *at low severity*.
+
+Similarly, for the policy:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_CPU_FREQ_GOV.*
+ expected: disabled
+ severity: medium
+
+ - name: CONFIG_CPU_FREQ_GOV_USERSPACE
+ expected: enabled
+ severity: low
+
+if the checked ``.config`` has ``CONFIG_CPU_FREQ_GOV_USERSPACE``, then it
+is expected to be enabled, regardless of the pattern's rule disabled
+expectation.
+
+Pattern priorities
+------------------
+
+When a symbol matches more than one pattern rule, the pattern rule with the
+highest priority (``pattern_prio``) is used. That field is optional and
+defaults to ``0``. A bigger integer implies a higher priority.
+
+For example, for the policy below:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_DEBUG
+ expected: disabled
+ severity: medium
+
+ - name_pattern: CONFIG_DEBUG_INFO
+ pattern_prio: 1
+ expected: enabled
+ severity: low
+
+if the checked ``.config`` has ``CONFIG_DEBUG_INFO_DWARF4``, it is expected
+to be enabled. This is because the ``CONFIG_DEBUG_INFO`` pattern has a
+higher priority of ``1``, while the broader ``CONFIG_DEBUG`` pattern has
+the default priority ``0``.
+
+Pattern priority conflicts are rejected
+---------------------------------------
+
+If multiple matching pattern rules share the highest priority for a symbol,
+the policy is ambiguous and the checker reports it as malformed. A unique
+higher-priority match takes precedence over any lower-priority matches.
+
+For example, for the policy below:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_NETFILTER_XT_MATCH_.*
+ expected: disabled
+ severity: medium
+
+ - name_pattern: CONFIG_NETFILTER_XT_MATCH_BPF.*
+ expected: enabled
+ severity: low
+
+if the checked ``.config`` has a ``CONFIG_NETFILTER_XT_MATCH_BPF`` symbol,
+then ``kconfig-audit`` will exit with a "malformed policy" error.
+
+Note that pattern priority conflicts are detected only for symbols present
+in the checked ``.config``. An exact ``name`` rule continues to take
+precedence over all pattern rules for such symbols.
+
+Duplicate rules are rejected
+----------------------------
+
+Duplicate exact symbol rules are rejected. The policy below:
+
+.. code-block:: yaml
+
+ rules:
+ - name: CONFIG_SECURITY_SELINUX
+ expected: enabled
+ severity: fatal
+
+ - name: CONFIG_SECURITY_SELINUX
+ expected: neutral
+ severity: low
+
+is considered malformed.
+
+.. _policy-file-reference:
+
+Policy file reference
+=====================
+
+Top-level fields
+----------------
+
+``api_version``
++++++++++++++++
+
+*Required*. Sets the kconfig-audit policy file version. Only one version
+tag is supported so far::
+
+ kconfig-audit.kernel.org/v1
+
+``kind``
+++++++++
+
+*Required*. Identifies the YAML document's type. For a configuration
+policy file, the value must be::
+
+ Policy
+
+``docs_url``
+++++++++++++
+
+*Optional*. Base URL used for rule documentation links. For example:
+
+.. code-block:: yaml
+
+ docs_url: https://docs.kernel.org/core-api/real-time/kernel-configuration.html
+
+A rule may then provide ``docs_hint`` to form a full rationale URL.
+
+``rules``
++++++++++
+
+*Required*. A non-empty list of rule entries.
+
+Each rule describes either one exact configuration symbol or a pattern that
+matches several symbols. See :ref:`Rule fields <rule-fields>`.
+
+.. _rule-fields:
+
+Rule fields
+-----------
+
+Each rule must contain one configuration symbol selector, one expected
+value, and one severity.
+
+A rule matching one exact symbol uses ``name``:
+
+.. code-block:: yaml
+
+ rules:
+ - name: CONFIG_PREEMPT_RT
+ expected: enabled
+ severity: fatal
+
+A rule matching symbols by regular expression uses ``name_pattern``:
+
+.. code-block:: yaml
+
+ rules:
+ - name_pattern: CONFIG_USB
+ expected: disabled
+ severity: medium
+
+A rule must not contain both ``name`` and ``name_pattern``.
+
+``name``
+++++++++
+
+Matches one exact kernel configuration symbol.
+
+The name must have the usual ``CONFIG_`` form, for example::
+
+ CONFIG_EXT4_FS
+
+Exact symbol rules are best when one symbol has a distinct meaning, a
+distinct severity, or its own rationale.
+
+``name_pattern``
+++++++++++++++++
+
+Matches configuration symbols by Python regular expression.
+
+The checker applies the regular expression to each symbol name using search
+semantics. Expressions are not implicitly anchored.
+
+For example, this pattern::
+
+ CONFIG_NETFILTER
+
+matches names such as::
+
+ CONFIG_NETFILTER
+ CONFIG_NETFILTER_ADVANCED
+ CONFIG_NETFILTER_XT_MATCH_BPF
+
+Use anchors when the rule should match only a narrow set of symbols. For
+example::
+
+ ^CONFIG_NO_HZ(_FULL|_IDLE)?$
+
+The schema validates that ``name_pattern`` is a syntactically valid regular
+expression.
+
+``pattern_prio``
+++++++++++++++++
+
+*Optional*. Sets the priority of a ``name_pattern`` rule. The value must
+be an integer from 1 to 100. If omitted, the priority is ``0``.
+
+For a symbol matching multiple pattern rules, the matching rule with the
+highest priority is used. If multiple matching rules share the highest
+priority, the policy is rejected as ambiguous. This field is not valid on
+an exact ``name`` rule.
+
+``expected``
+++++++++++++
+
+*Required*. Describes the expected state of the matching symbol or
+symbols.
+
+Allowed values are:
+
+``enabled``
+ The symbol must be set to ``y`` or ``m``.
+
+ For an exact ``name`` rule, a missing symbol fails an ``enabled`` rule.
+ An explicitly unset symbol also fails. For a ``name_pattern`` rule, the
+ rule applies only to symbols matched in the checked ``.config``. A
+ non-boolean value, such as an integer, hexadecimal value, or string,
+ fails because the rule expects a tristate kconfig symbol.
+
+``disabled``
+ For an exact ``name`` rule, the symbol must be absent or explicitly
+ unset. For a ``name_pattern`` rule, any matched symbol must be
+ explicitly unset.
+
+ A symbol set to ``y`` or ``m`` fails. A non-boolean value also fails,
+ because the rule has a tristate kconfig expectation.
+
+``neutral``
+ The symbol is deliberately not checked.
+
+ A neutral pattern rule can exempt symbols from broader pattern rules when
+ its ``pattern_prio`` is higher. Neutral rules are also useful when a
+ policy wants to document that a symbol was considered but intentionally
+ left unconstrained.
+
+``severity``
+++++++++++++
+
+*Required*. Describes how serious a rule violation is.
+
+Allowed values are, from highest to lowest::
+
+ fatal
+ high
+ medium
+ low
+ info
+
+The checker reports issues grouped by severity. Any issue, including an
+``info`` issue, causes the check command to exit unsuccessfully. Severity
+is therefore for reporting and triage; it is not a pass/fail threshold.
+
+``docs_hint``
++++++++++++++
+
+*Optional*. Identifies documentation for the rule.
+
+If the policy has ``docs_url`` and a rule has ``docs_hint``, the checker
+prints a rationale URL by appending the hint as a URI fragment.
+
+For an exact ``name`` rule, this shortcut may be used:
+
+.. code-block:: yaml
+
+ docs_hint: config
+
+With an exact symbol rule, ``config`` is converted into a fragment derived
+from the configuration symbol name.
+
+For example:
+
+.. code-block:: yaml
+
+ docs_url: https://docs.kernel.org/core-api/real-time/kernel-configuration.html
+
+ rules:
+ - name: CONFIG_DRM
+ expected: disabled
+ severity: info
+ docs_hint: config
+
+produces the URL::
+
+ https://docs.kernel.org/core-api/real-time/kernel-configuration.html#config-drm
+
+For any other ``docs_hint`` value, the value is used directly as the
+fragment identifier. For example:
+
+.. code-block:: yaml
+
+ docs_hint: non-performance-cpu-frequency-governors
+
+produces::
+
+ #non-performance-cpu-frequency-governors
+
+Since a pattern does not name one specific symbol, pattern rules *should*
+use an explicit fragment name rather than the ``config`` shortcut.
+
+``rationale``
++++++++++++++
+
+*Optional*. Human-readable explanation printed with a reported issue.
+
+A rationale *should* explain why the rule exists, not merely repeat the
+rule. It may be written on one line, or as a YAML block for longer text:
+
+.. code-block:: yaml
+
+ rationale:
+ Dynamic CPU frequency changes can introduce latency variation.
+ Real-time deployments must select a fixed performance governor.
diff --git a/tools/kconfig-audit/audit.py b/tools/kconfig-audit/audit.py
index 6ff4a56281ad..eac25740beca 100755
--- a/tools/kconfig-audit/audit.py
+++ b/tools/kconfig-audit/audit.py
@@ -98,7 +98,12 @@ try:
import jsonschema # type: ignore
import yaml as yaml_m # type: ignore
except ModuleNotFoundError as e:
- error(f'Missing Python module: {e.name}', os.EX_UNAVAILABLE)
+ docs = 'Documentation/tools/kconfig-audit.rst'
+ error(
+ f'Missing Python module: {e.name}\n'
+ f'Check {docs} for how to install required dependencies',
+ os.EX_UNAVAILABLE
+ )

#
# Kernel .config model:
--
2.56.0