mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: wen.yang@linux.dev
To: Joel Granados <joel.granados@kernel.org>,
	David Laight <david.laight.linux@gmail.com>,
	Kees Cook <kees@kernel.org>
Cc: Wen Yang <wen.yang@linux.dev>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds
Date: Tue, 17 Nov 2026 00:07:37 +0800	[thread overview]
Message-ID: <20272a9cb4efeb99fb710a4da398e67b230c6910.1794844686.git.wen.yang@linux.dev> (raw)
In-Reply-To: <cover.1794844686.git.wen.yang@linux.dev>

From: Wen Yang <wen.yang@linux.dev>

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@kernel.org>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
 .../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


      parent reply	other threads:[~2026-11-16 16:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 16:07 [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 1/5] sysctl: add numeric bounds and core stride infrastructure wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 2/5] sysctl: cover numeric bounds and table containers in KUnit wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 3/5] netfilter: embed logger sysctl context using stride wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 4/5] net/ipv4: embed device " wen.yang
2026-11-16 16:07 ` wen.yang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20272a9cb4efeb99fb710a4da398e67b230c6910.1794844686.git.wen.yang@linux.dev \
    --to=wen.yang@linux.dev \
    --cc=david.laight.linux@gmail.com \
    --cc=joel.granados@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®