[RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds
From: wen . yang
Date: Mon Nov 16 2026 - 11:08:30 EST
From: Wen Yang <wen.yang@xxxxxxxxx>
Add a Coccinelle script supporting:
- 'patch' mode: automatically convert struct ctl_table definitions using
legacy extra1/extra2 pointers or constants to numeric .min and .max
fields, omitting redundant .min = 0.
- 'context' modes: detect any residual usages or
direct member dereferences of extra1/extra2 across the tree.
Suggested-by: Joel Granados <joel.granados@xxxxxxxxxx>
Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
---
.../api/sysctl/sysctl_numeric_bounds.cocci | 276 ++++++++++++++++++
1 file changed, 276 insertions(+)
create mode 100644 scripts/coccinelle/api/sysctl/sysctl_numeric_bounds.cocci
diff --git a/scripts/coccinelle/api/sysctl/sysctl_numeric_bounds.cocci b/scripts/coccinelle/api/sysctl/sysctl_numeric_bounds.cocci
new file mode 100644
index 000000000000..8f8f5e48e136
--- /dev/null
+++ b/scripts/coccinelle/api/sysctl/sysctl_numeric_bounds.cocci
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Convert sysctl tables that still use the legacy extra1/extra2 bound
+/// pointers to numeric .min/.max fields, and report anything that still
+/// needs a manual look.
+///
+/// Only well-known constant bounds (SYSCTL_ZERO, SYSCTL_ONE, ...) are
+/// rewritten automatically. Bounds that point at a variable (&foo_min,
+/// &foo_max) cannot be rewritten safely: in several callers (power/main.c,
+/// i915_perf.c, ipc_sysctl.c, pid.c, sched/rt.c, rds/sysctl.c,
+/// xprtsock.c) that variable is updated at boot or at run time, so
+/// inlining its initial value would silently freeze a live bound. Those
+/// entries are left untouched and reported for manual conversion,
+/// together with any .min/.max field that ends up holding a pointer or a
+/// legacy SYSCTL_ constant.
+///
+/// Use MODE=patch to convert and MODE=context to list what is left.
+///
+// Confidence: Medium
+// Options: --no-includes --include-headers
+//
+
+virtual context
+virtual patch
+
+// ----------------------------------------------------------------------------
+// 1. Patch rules: constant bounds in static table initializers
+// ----------------------------------------------------------------------------
+
+// min 0 is the zero-initialised default; drop the line instead of
+// emitting a redundant .min = 0.
+@p_min_zero depends on patch@
+identifier T;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+- .extra1 = SYSCTL_ZERO,
+ ...
+ },
+ ...
+};
+
+@m_min depends on patch@
+identifier T;
+identifier M =~ "^SYSCTL_";
+@@
+struct ctl_table T[] = { ..., { ..., .extra1 = M, ... }, ... };
+
+@script:python tx_min depends on patch@
+m << m_min.M;
+v;
+@@
+MAP = {
+ "SYSCTL_ONE": "1",
+ "SYSCTL_TWO": "2",
+ "SYSCTL_THREE": "3",
+ "SYSCTL_FOUR": "4",
+ "SYSCTL_ONE_HUNDRED": "100",
+ "SYSCTL_TWO_HUNDRED": "200",
+ "SYSCTL_ONE_THOUSAND": "1000",
+ "SYSCTL_THREE_THOUSAND": "3000",
+ "SYSCTL_INT_MAX": "INT_MAX",
+ "SYSCTL_MAXOLDUID": "65535",
+ "SYSCTL_NEG_ONE": "-1",
+ "SYSCTL_LONG_ZERO": "0",
+ "SYSCTL_LONG_ONE": "1",
+ "SYSCTL_LONG_MAX": "LONG_MAX",
+}
+if m in MAP:
+ coccinelle.v = cocci.make_ident(MAP[m])
+else:
+ cocci.include_match(False)
+
+@p_min depends on patch@
+identifier m_min.T, m_min.M;
+identifier tx_min.v;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+- .extra1 = M,
++ .min = v,
+ ...
+ },
+ ...
+};
+
+@m_max depends on patch@
+identifier T;
+identifier M =~ "^SYSCTL_";
+@@
+struct ctl_table T[] = { ..., { ..., .extra2 = M, ... }, ... };
+
+@script:python tx_max depends on patch@
+m << m_max.M;
+v;
+@@
+MAP = {
+ "SYSCTL_ZERO": "0",
+ "SYSCTL_ONE": "1",
+ "SYSCTL_TWO": "2",
+ "SYSCTL_THREE": "3",
+ "SYSCTL_FOUR": "4",
+ "SYSCTL_ONE_HUNDRED": "100",
+ "SYSCTL_TWO_HUNDRED": "200",
+ "SYSCTL_ONE_THOUSAND": "1000",
+ "SYSCTL_THREE_THOUSAND": "3000",
+ "SYSCTL_INT_MAX": "INT_MAX",
+ "SYSCTL_MAXOLDUID": "65535",
+ "SYSCTL_NEG_ONE": "-1",
+ "SYSCTL_LONG_ZERO": "0",
+ "SYSCTL_LONG_ONE": "1",
+ "SYSCTL_LONG_MAX": "LONG_MAX",
+}
+if m in MAP:
+ coccinelle.v = cocci.make_ident(MAP[m])
+else:
+ cocci.include_match(False)
+
+@p_max depends on patch@
+identifier m_max.T, m_max.M;
+identifier tx_max.v;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+- .extra2 = M,
++ .max = v,
+ ...
+ },
+ ...
+};
+
+@p_min_literal depends on patch@
+identifier T;
+constant C;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+- .extra1 = C,
++ .min = C,
+ ...
+ },
+ ...
+};
+
+@p_max_literal depends on patch@
+identifier T;
+constant C;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+- .extra2 = C,
++ .max = C,
+ ...
+ },
+ ...
+};
+
+// ----------------------------------------------------------------------------
+// 2. Detection rules: any remaining extra1 / extra2 use
+// ----------------------------------------------------------------------------
+
+@r_extra1 depends on context@
+identifier T;
+expression E;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .extra1 = E,
+ ...
+ },
+ ...
+};
+
+@r_extra2 depends on context@
+identifier T;
+expression E;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .extra2 = E,
+ ...
+ },
+ ...
+};
+
+@r_member_ptr depends on context@
+struct ctl_table *C;
+@@
+(
+* C->extra1
+|
+* C->extra2
+)
+
+@r_member_value depends on context@
+struct ctl_table T;
+@@
+(
+* T.extra1
+|
+* T.extra2
+)
+
+// ----------------------------------------------------------------------------
+// 3. Detection rules: bound fields holding a pointer or a legacy macro
+// ----------------------------------------------------------------------------
+
+@r_init_min_ptr depends on context@
+identifier T;
+expression E;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .min = &E,
+ ...
+ },
+ ...
+};
+
+@r_init_max_ptr depends on context@
+identifier T;
+expression E;
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .max = &E,
+ ...
+ },
+ ...
+};
+
+@r_init_min_macro depends on context@
+identifier T;
+identifier M =~ "^SYSCTL_";
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .min = M,
+ ...
+ },
+ ...
+};
+
+@r_init_max_macro depends on context@
+identifier T;
+identifier M =~ "^SYSCTL_";
+@@
+struct ctl_table T[] = {
+ ...,
+ {
+ ...,
+* .max = M,
+ ...
+ },
+ ...
+};
--
2.25.1