From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=193.142.43.55 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796338179; b=KDjRbECtfaUn1qPAmMwlFpLJcPtbOGfeDuMFxMtGu71BZ7Lb9EHnPEJCpjoj6MRbXNQI Tu0FxYNrOKXnklWVR9R9DJhUc9PmB3BXODvpgf8hwg9/8/g9BiBYpkQvOf533ePd7I4FA UB6ZMOrPPbvEuijjA7TorTbsQMtiv1nEpxptoF0+JO8wA2toV3sNZoax8i1J94iDR0jx1 10pO6nwyeCw2bygicmcCCAfumhjiHYuJ7PXaZxtgOtEm+rATgKgf+IfN2QnnwuxKsXLye w+VKxJ4lqudi2/CBI3im3BGcRbe5N8D5sWUFzUhsHT7a/3dYuRC5Q5P9XPkl5cm3oeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796338179; h=DMARC-Filter:From:DKIM-Signature:DKIM-Signature:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=u/6kdW7SebulYf9saRBLfQObB72wDW/p3WS1F6OlZUQ=; b=ww6eOlGPe7VoGiu5S+xt0G3hSnDumeznslo1cjXTW0K+tFCVEPV3ZOoSpKRaG5S7you0 v2QptPux73f3ckUJ6E3f+FjUNPP8GbLSoP50wWeTk/e4P1aS661weZI7Cj0a7+gj1uDc0 RSaWcpI7+yhAvokP1xXDzJ5mEAY/PK/pwTR6AcJv+Baph31RCsQKE4C9JoGKd1ukmIYLd uY+CA+E7HoOI288ZvPFBM2d1P5/GzVATfMynfaeAXcfDk8ighc5QAcOuosthLPeAoIk9K piG3Aa0BsJa06tKZz/1HxH2Vb3da3sVWMroqpJyaEzhVp1ssSaklJH3ddaHRYDPBHzA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass header.d=linutronix.de header.i=@linutronix.de header.a=rsa-sha256 header.s=2020 header.b=epX3M5SL; dkim=pass header.d=linutronix.de header.i=@linutronix.de header.a=ed25519-sha256 header.s=2020e header.b=SM0SMOzc; arc=none smtp.remote-ip=193.142.43.55 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7A22606A337; Thu, 3 Dec 2026 22:49:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A7A22606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.a=rsa-sha256 header.s=2020 header.b=epX3M5SL; dkim=pass header.d=linutronix.de header.i=@linutronix.de header.a=ed25519-sha256 header.s=2020e header.b=SM0SMOzc From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1796338177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=u/6kdW7SebulYf9saRBLfQObB72wDW/p3WS1F6OlZUQ=; b=epX3M5SL4jRf2gC9JLVRgFXco/M1k+DOCpEEKNmIy/tIvTPmrUyNs09a9EP+0Tu+a7YXnq DcH6V8Sx1mZ2tSx6G2NWubnY9ufI6//Qfe8+SOXuuXSJv8qxgouN0pSOqK0r880AEpCzC8 0ArVEx1Xd4o+qfJAoLEHaCTRyR6r6Y8MSzaN+pzDWNcvPUgEYLhSF1RdODOt/iMmXzqVDj C8nnJvNrjgy7tj80E+71m/BqDWj7M512PfThP1ebvs1t3KKYVLNnvoj2G0jLQwXbIvJYbs qeQ5eiwOHji8G9lGUbC+vMCFpwUWNlSsWYlE2mQdXgiXVVjKBHN38/igjyhBKg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1796338177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=u/6kdW7SebulYf9saRBLfQObB72wDW/p3WS1F6OlZUQ=; b=SM0SMOzce8H8RWtEfIoBVVVUlpSglFQOK+Hsi+NvSbdI9JI4kd6rqueCsUU1LoIxgtNE+o HQ0PNqHftp7pyeBQ== To: Nathan Chancellor , Nicolas Schier , Julian Braha , Emanuele Rocca , Clark Williams , Steven Rostedt , Ingo Molnar Cc: Sebastian Andrzej Siewior , Frederic Weisbecker , John Ogness , Derek Barbosa , =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= , Daan De Meyer , Luca Boccassi , linux-rt-devel@lists.linux.dev, debian-kernel@lists.debian.org, linux-kbuild@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, "Ahmed S. Darwish" Subject: [PATCH v2 04/11] Documentation: Describe kconfig-audit usage and design Date: Thu, 3 Dec 2026 23:49:05 +0100 Message-ID: <20261203224916.118064-5-darwi@linutronix.de> In-Reply-To: <20261203224916.118064-1-darwi@linutronix.de> References: <20261203224916.118064-1-darwi@linutronix.de> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- .../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 `. + +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 +----------- + +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