* [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride
@ 2026-11-16 16:07 wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 1/5] sysctl: add numeric bounds and core stride infrastructure wen.yang
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
From: Wen Yang <wen.yang@linux.dev>
struct ctl_table has long relied on extra1 and extra2 pointers to serve
multiple unrelated duties, including range limits and caller-specific
context. This series addresses both:
1. Replace bound pointers with plain unsigned long min and max fields
directly in struct ctl_table.
2. Replace per-entry context pointers with a core stride mechanism,
allowing callers to register arrays of structures embedding struct
ctl_table and recover enclosing context via standard container_of().
This RFC focuses on the core stride infrastructure and two representative
complex users (netfilter nf_log and IPv4 devinet), as suggested by Joel
Granados. The remaining treewide callers are converted in a single
squashed commit to ensure the tree builds and tests pass cleanly.
Changes from v1:
=================
v1: https://lore.kernel.org/all/cover.1793204511.git.wen.yang@linux.dev/
1. Abandoned per-entry 'void *priv' (David Laight):
- v1 added 'priv' to struct ctl_table, incurring 8 bytes on 64-bit and
4 bytes on 32-bit for all ~1,000 entries across thousands of dynamic
instances, even though <7% of entries need context.
- Refactor: context lives outside struct ctl_table via core stride;
callers embed ctl_table at offset 0 of their own record and recover
context with standard container_of().
- Cost: zero growth in struct ctl_table (56/32 bytes) and zero growth
in struct ctl_table_header (88/52 bytes, packing u16 stride and type).
2. Eliminated slot wrappers & index coupling (Joel Granados, David Laight):
- An intermediate prototype discussed on the list used slot-indexed
container recovery, requiring 32 wrapper functions in devinet and 11
in nf_log.
- Refactor: with core stride, the core passes the exact registered
entry pointer to the handler, so handlers use container_of() directly
and all 43 wrappers disappear without touching proc_handler
signatures.
3. KUnit purity & dependency fix (0-day robot):
- Removed register_sysctl() calls from KUnit; tests invoke
proc_handler directly so no live /proc/sys nodes are modified.
- Added 'depends on SYSCTL' to SYSCTL_KUNIT_TEST in lib/Kconfig.debug,
fixing undefined symbols when built as a module with SYSCTL=n.
4. Baseline rebase & conflict resolution:
- Rebased onto next-20261111 (aaa2f954c21b).
- Added proc_dointvec_jiffies_minmax and proc_dointvec_ms_jiffies_minmax
to sysctl_check_table().
5. Added Coccinelle verification script (Joel Granados):
- A 17-rule script (patch and context modes) maps all 15 SYSCTL_*
constants through one lookup table instead of repeating two rules
per constant.
- Bounds that point at a variable (&foo_min, &foo_max) are left
untouched and reported: in several callers (power/main.c, i915_perf,
ipc_sysctl, pid, sched/rt, rds, xprtsock) that variable is updated
at boot or at run time, so inlining its initial value would silently
freeze a live bound. The script also flags .min/.max fields holding
a pointer or a legacy macro.
6. Streamlined RFC series layout (Joel Granados):
- Reduced from 39 patches in v1 to 6 focused patches (core
infrastructure, unit test, two complex stride consumers, coccinelle
script.
Key Design Points:
==================
1. Zero Memory Growth:
- struct ctl_table keeps its exact size (56 bytes on 64-bit, 32 bytes on
32-bit). No 'priv' field is added.
- u16 ctl_table_stride is introduced into struct ctl_table_header.
Together with u16 type, it occupies the existing alignment padding
hole on 64-bit (88 bytes unchanged) and keeps 52 bytes on 32-bit.
Registration rejects stride values that exceed U16_MAX.
- Headers are allocated once per table registration (~240 sites
in-tree), not per entry (thousands in-tree).
2. Eliminates some Wrapper Functions:
- net/ipv4 devinet: registers strided struct devinet_sysctl_entry,
eliminating 32 per-attribute wrapper functions.
- Handlers recover their context using standard container_of() without
changing any proc_handler signatures.
3. Standalone Stride API vs. Existing _sz:
register_sysctl_stride() and register_net_sysctl_stride() are added
alongside register_*_sz() so existing callers remain untouched. If
preferred, stride can instead be folded into register_*_sz() as an
element-size parameter (similar to bsearch()).
4. Fixes & Cleanups:
- Fixes net.core sysctl write permissions in init_net (mode 0222 is
now cleared only for non-init network namespaces).
- Cleans up redundant '.min = 0' initializers treewide.
- Provides a Coccinelle script (scripts/coccinelle/api/sysctl/) for
automated conversion and proof of no leftover extra1/extra2 uses.
Series Structure:
=================
- Patch 1: Core stride infrastructure and numeric bounds.
- Patch 2: KUnit unit test coverage (21 tests, pure handler execution).
- Patch 3: netfilter nf_log stride migration (11 wrappers removed).
- Patch 4: net/ipv4 devinet stride migration (32 wrappers removed).
- Patch 5: Coccinelle script for detection and conversion.
Testing:
========
- KUnit sysctl_test: pass:21 fail:0 (UML).
- x86_64 defconfig build of kernel/, fs/proc/, net/ passed.
- Coccinelle: full-tree MODE=context finds 0 leftovers after conversion
and 822 before it; MODE=patch converts the legacy net/unix table.
Wen Yang (5):
sysctl: add numeric bounds and core stride infrastructure
sysctl: cover numeric bounds and table containers in KUnit
netfilter: embed logger sysctl context using stride
net/ipv4: embed device sysctl context using stride
scripts/coccinelle: add script to detect and convert sysctl bounds
fs/proc/proc_sysctl.c | 82 +-
include/linux/sysctl.h | 43 +-
include/net/net_namespace.h | 10 +
kernel/sysctl-test.c | 776 +++++++++++++++++-
kernel/sysctl.c | 38 +-
lib/Kconfig.debug | 1 +
net/ipv4/devinet.c | 190 +++--
net/netfilter/nf_log.c | 84 +-
net/sysctl_net.c | 31 +-
.../api/sysctl/sysctl_numeric_bounds.cocci | 276 +++++++
10 files changed, 1339 insertions(+), 192 deletions(-)
create mode 100644 scripts/coccinelle/api/sysctl/sysctl_numeric_bounds.cocci
base-commit: aaa2f954c21b205a0bcd0961e5b2be4e82e58455
--
2.25.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v2 1/5] sysctl: add numeric bounds and core stride infrastructure
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 ` wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 2/5] sysctl: cover numeric bounds and table containers in KUnit wen.yang
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
From: Wen Yang <wen.yang@linux.dev>
Add min and max numeric fields to struct ctl_table alongside the
existing extra1/extra2 pointers to enable progressive migration to
numeric bounds.
To support caller-specific context without adding per-entry overhead
to thousands of sysctl entries, introduce 'u16 ctl_table_stride' into
struct ctl_table_header and implement register_sysctl_stride() and
register_net_sysctl_stride().
In contrast to individual ctl_table entries, headers are allocated
once per table registration (~240 call sites total in-tree). Packing
'ctl_table_stride' with 'type' as u16 fields ensures zero memory
growth for struct ctl_table_header:
- On 64-bit systems, the size remains unchanged at 88 bytes by filling
the alignment padding hole before 'type'.
- On 32-bit systems, the size remains unchanged at 52 bytes.
Core array iteration and node mapping in fs/proc/proc_sysctl.c use
sysctl_entry_at() and sysctl_entry_index() helpers. This allows
callers to register arrays of structures embedding struct ctl_table
and recover outer context with container_of(), eliminating 43 per-slot
wrapper functions in devinet and nf_log without changing proc_handler
signatures.
Existing registration interfaces continue to default to
sizeof(struct ctl_table).
Suggested-by: Joel Granados <joel.granados@kernel.org>
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
fs/proc/proc_sysctl.c | 82 +++++++++++++++++++++++++++++--------
include/linux/sysctl.h | 43 +++++++++++++++++--
include/net/net_namespace.h | 10 +++++
kernel/sysctl.c | 38 +++++++++++------
net/sysctl_net.c | 31 +++++++++-----
5 files changed, 160 insertions(+), 44 deletions(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index eeebf813668e..b8e438529300 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -20,9 +20,23 @@
#include <linux/lockdep.h>
#include "internal.h"
+static inline const struct ctl_table *
+sysctl_entry_at(const struct ctl_table_header *head, size_t i)
+{
+ return (const struct ctl_table *)
+ ((const char *)head->ctl_table + i * head->ctl_table_stride);
+}
+
+static inline size_t
+sysctl_entry_index(const struct ctl_table_header *head, const struct ctl_table *entry)
+{
+ return ((const char *)entry - (const char *)head->ctl_table) / head->ctl_table_stride;
+}
+
#define list_for_each_table_entry(entry, header) \
- entry = header->ctl_table; \
- for (size_t i = 0 ; i < header->ctl_table_size; ++i, entry++)
+ for (size_t _i = 0; \
+ _i < (header)->ctl_table_size && ((entry) = sysctl_entry_at((header), _i), 1); \
+ ++_i)
static const struct dentry_operations proc_sys_dentry_operations;
static const struct file_operations proc_sys_file_operations;
@@ -80,6 +94,7 @@ static struct ctl_table_root sysctl_table_root = {
.nreg = 1,
.ctl_table = root_table }},
.ctl_table_arg = root_table,
+ .ctl_table_stride = sizeof(struct ctl_table),
.root = &sysctl_table_root,
.set = &sysctl_table_root.default_set,
},
@@ -127,7 +142,7 @@ static const struct ctl_table *find_entry(struct ctl_table_header **phead,
ctl_node = rb_entry(node, struct ctl_node, node);
head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
+ entry = sysctl_entry_at(head, ctl_node - head->node);
procname = entry->procname;
cmp = namecmp(name, namelen, procname, strlen(procname));
@@ -145,7 +160,7 @@ static const struct ctl_table *find_entry(struct ctl_table_header **phead,
static int insert_entry(struct ctl_table_header *head, const struct ctl_table *entry)
{
- struct rb_node *node = &head->node[entry - head->ctl_table].node;
+ struct rb_node *node = &head->node[sysctl_entry_index(head, entry)].node;
struct rb_node **p = &head->parent->root.rb_node;
struct rb_node *parent = NULL;
const char *name = entry->procname;
@@ -161,7 +176,7 @@ static int insert_entry(struct ctl_table_header *head, const struct ctl_table *e
parent = *p;
parent_node = rb_entry(parent, struct ctl_node, node);
parent_head = parent_node->header;
- parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
+ parent_entry = sysctl_entry_at(parent_head, parent_node - parent_head->node);
parent_name = parent_entry->procname;
cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
@@ -184,17 +199,19 @@ static int insert_entry(struct ctl_table_header *head, const struct ctl_table *e
static void erase_entry(struct ctl_table_header *head, const struct ctl_table *entry)
{
- struct rb_node *node = &head->node[entry - head->ctl_table].node;
+ struct rb_node *node = &head->node[sysctl_entry_index(head, entry)].node;
rb_erase(node, &head->parent->root);
}
static void init_header(struct ctl_table_header *head,
struct ctl_table_root *root, struct ctl_table_set *set,
- struct ctl_node *node, const struct ctl_table *table, size_t table_size)
+ struct ctl_node *node, const struct ctl_table *table, size_t table_size,
+ size_t stride)
{
head->ctl_table = table;
head->ctl_table_size = table_size;
+ head->ctl_table_stride = stride;
head->ctl_table_arg = table;
head->used = 0;
head->count = 1;
@@ -391,7 +408,7 @@ static void first_entry(struct ctl_dir *dir,
spin_unlock(&sysctl_lock);
if (ctl_node) {
head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
+ entry = sysctl_entry_at(head, ctl_node - head->node);
}
*phead = head;
*pentry = entry;
@@ -401,7 +418,7 @@ static void next_entry(struct ctl_table_header **phead, const struct ctl_table *
{
struct ctl_table_header *head = *phead;
const struct ctl_table *entry = *pentry;
- struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
+ struct ctl_node *ctl_node = &head->node[sysctl_entry_index(head, entry)];
spin_lock(&sysctl_lock);
unuse_table(head);
@@ -411,7 +428,7 @@ static void next_entry(struct ctl_table_header **phead, const struct ctl_table *
head = NULL;
if (ctl_node) {
head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
+ entry = sysctl_entry_at(head, ctl_node - head->node);
}
*phead = head;
*pentry = entry;
@@ -977,7 +994,8 @@ static struct ctl_dir *new_dir(struct ctl_table_set *set,
memcpy(new_name, name, namelen);
table[0].procname = new_name;
table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
- init_header(&new->header, set->dir.header.root, set, node, table, 1);
+ init_header(&new->header, set->dir.header.root, set, node, table, 1,
+ sizeof(struct ctl_table));
return new;
}
@@ -1133,6 +1151,10 @@ static int sysctl_check_table_array(const char *path, const struct ctl_table *ta
err |= sysctl_err(path, table,
"range value too large for proc_dou8vec_minmax");
}
+
+ if (table->min > 255U || table->max > 255U)
+ err |= sysctl_err(path, table,
+ "range value too large for proc_dou8vec_minmax");
}
if (table->proc_handler == proc_dobool) {
@@ -1217,7 +1239,7 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_
link++;
}
init_header(links, dir->header.root, dir->header.set, node, link_table,
- head->ctl_table_size);
+ head->ctl_table_size, sizeof(struct ctl_table));
links->nreg = head->ctl_table_size;
return links;
@@ -1350,8 +1372,7 @@ static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
* proc_handler - the text handler routine (described below)
*
* extra1, extra2 - extra pointers usable by the proc handler routines
- * XXX: we should eventually modify these to use long min / max [0]
- * [0] https://lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org
+ * min, max - numeric bounds used by range-checking handlers
*
* Leaf nodes in the sysctl tree will be represented by a single file
* under /proc; non-leaf nodes are not allowed.
@@ -1369,22 +1390,28 @@ static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
* This routine returns %NULL on a failure to register, and a pointer
* to the table header on success.
*/
-struct ctl_table_header *__register_sysctl_table(
+struct ctl_table_header *__register_sysctl_table_stride(
struct ctl_table_set *set,
- const char *path, const struct ctl_table *table, size_t table_size)
+ const char *path, const struct ctl_table *table, size_t table_size,
+ size_t stride)
{
struct ctl_table_root *root = set->dir.header.root;
struct ctl_table_header *header;
struct ctl_dir *dir;
struct ctl_node *node;
+ if (stride < sizeof(struct ctl_table) ||
+ stride > U16_MAX ||
+ stride % __alignof__(struct ctl_table))
+ return NULL;
+
header = kzalloc(sizeof(struct ctl_table_header) +
sizeof(struct ctl_node)*table_size, GFP_KERNEL_ACCOUNT);
if (!header)
return NULL;
node = (struct ctl_node *)(header + 1);
- init_header(header, root, set, node, table, table_size);
+ init_header(header, root, set, node, table, table_size, stride);
if (sysctl_check_table(path, header))
goto fail;
@@ -1414,6 +1441,24 @@ struct ctl_table_header *__register_sysctl_table(
return NULL;
}
+struct ctl_table_header *__register_sysctl_table(
+ struct ctl_table_set *set,
+ const char *path, const struct ctl_table *table, size_t table_size)
+{
+ return __register_sysctl_table_stride(set, path, table, table_size,
+ sizeof(struct ctl_table));
+}
+
+struct ctl_table_header *register_sysctl_stride(const char *path,
+ const struct ctl_table *table,
+ size_t table_size,
+ size_t stride)
+{
+ return __register_sysctl_table_stride(&sysctl_table_root.default_set,
+ path, table, table_size, stride);
+}
+EXPORT_SYMBOL(register_sysctl_stride);
+
/**
* register_sysctl_sz - register a sysctl table
* @path: The path to the directory the sysctl table is in. If the path
@@ -1552,7 +1597,8 @@ void setup_sysctl_set(struct ctl_table_set *set,
{
memset(set, 0, sizeof(*set));
set->is_seen = is_seen;
- init_header(&set->dir.header, root, set, NULL, root_table, 1);
+ init_header(&set->dir.header, root, set, NULL, root_table, 1,
+ sizeof(struct ctl_table));
}
void retire_sysctl_set(struct ctl_table_set *set)
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index e5d7226ab6f5..bc2ac811d792 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -228,8 +228,26 @@ struct ctl_table {
struct ctl_table_poll *poll;
void *extra1;
void *extra2;
+ unsigned long min;
+ unsigned long max;
} __randomize_layout;
+/**
+ * sysctl_table_container - recover the container of a sysctl table entry
+ * @entry: pointer to an array element.
+ * @type: type of the containing structure.
+ * @member: the ctl_table array member in @type.
+ * @index: index of @entry within @member.
+ *
+ * The caller must pass the original registered entry, not a copy.
+ */
+#define sysctl_table_container(entry, type, member, index) ({ \
+ static_assert(__same_type(*(entry), typeof_member(type, member[0])), \
+ "pointer type mismatch in sysctl_table_container()"); \
+ (type *)((void *)(entry) - offsetof(type, member) - \
+ (size_t)(index) * sizeof(*(entry))); \
+})
+
struct ctl_node {
struct rb_node node;
struct ctl_table_header *header;
@@ -250,6 +268,11 @@ struct ctl_node {
* type.SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Identifies a permanently empty dir
* target to serve as a mount point
*/
+enum sysctl_table_type {
+ SYSCTL_TABLE_TYPE_DEFAULT = 0,
+ SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY = 1,
+};
+
struct ctl_table_header {
union {
struct {
@@ -268,10 +291,8 @@ struct ctl_table_header {
struct ctl_dir *parent;
struct ctl_node *node;
struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
- enum {
- SYSCTL_TABLE_TYPE_DEFAULT,
- SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,
- } type;
+ u16 ctl_table_stride;
+ u16 type;
};
struct ctl_dir {
@@ -308,8 +329,14 @@ extern void retire_sysctl_set(struct ctl_table_set *set);
struct ctl_table_header *__register_sysctl_table(
struct ctl_table_set *set,
const char *path, const struct ctl_table *table, size_t table_size);
+struct ctl_table_header *__register_sysctl_table_stride(
+ struct ctl_table_set *set,
+ const char *path, const struct ctl_table *table, size_t table_size,
+ size_t stride);
struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_table *table,
size_t table_size);
+struct ctl_table_header *register_sysctl_stride(const char *path, const struct ctl_table *table,
+ size_t table_size, size_t stride);
void unregister_sysctl_table(struct ctl_table_header * table);
extern int sysctl_init_bases(void);
@@ -343,6 +370,14 @@ static inline struct ctl_table_header *register_sysctl_sz(const char *path,
return NULL;
}
+static inline struct ctl_table_header *register_sysctl_stride(const char *path,
+ const struct ctl_table *table,
+ size_t table_size,
+ size_t stride)
+{
+ return NULL;
+}
+
static inline void unregister_sysctl_table(struct ctl_table_header * table)
{
}
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 4a4d72080acd..b5091cacdbb9 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -533,6 +533,10 @@ int net_sysctl_init(void);
struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
const struct ctl_table *table,
size_t table_size);
+struct ctl_table_header *register_net_sysctl_stride(struct net *net, const char *path,
+ const struct ctl_table *table,
+ size_t table_size,
+ size_t stride);
void unregister_net_sysctl_table(struct ctl_table_header *header);
#else
static inline int net_sysctl_init(void) { return 0; }
@@ -541,6 +545,12 @@ static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
{
return NULL;
}
+static inline struct ctl_table_header *register_net_sysctl_stride(struct net *net,
+ const char *path, const struct ctl_table *table, size_t table_size,
+ size_t stride)
+{
+ return NULL;
+}
static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
{
}
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index f5145b32f6be..86db3affd0b2 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -423,11 +423,15 @@ int proc_uint_conv(ulong *u_ptr, uint *k_ptr, int dir,
ret = user_to_kern(u_ptr, &tmp_k);
if (ret)
return ret;
- if ((tbl->extra1 &&
- *(uint *)tbl->extra1 > tmp_k) ||
- (tbl->extra2 &&
- *(uint *)tbl->extra2 < tmp_k))
+ if (tbl->extra1 || tbl->extra2) {
+ if ((tbl->extra1 &&
+ *(uint *)tbl->extra1 > tmp_k) ||
+ (tbl->extra2 &&
+ *(uint *)tbl->extra2 < tmp_k))
+ return -ERANGE;
+ } else if (tbl->min > tmp_k || tbl->max < tmp_k) {
return -ERANGE;
+ }
WRITE_ONCE(*k_ptr, tmp_k);
} else
return user_to_kern(u_ptr, k_ptr);
@@ -543,9 +547,13 @@ int proc_int_conv(bool *negp, ulong *u_ptr, int *k_ptr, int dir,
ret = user_to_kern(negp, u_ptr, &tmp_k);
if (ret)
return ret;
- if ((tbl->extra1 && *(int *)tbl->extra1 > tmp_k) ||
- (tbl->extra2 && *(int *)tbl->extra2 < tmp_k))
+ if (tbl->extra1 || tbl->extra2) {
+ if ((tbl->extra1 && *(int *)tbl->extra1 > tmp_k) ||
+ (tbl->extra2 && *(int *)tbl->extra2 < tmp_k))
+ return -EINVAL;
+ } else if ((long)tbl->min > tmp_k || (long)tbl->max < tmp_k) {
return -EINVAL;
+ }
WRITE_ONCE(*k_ptr, tmp_k);
} else
return user_to_kern(negp, u_ptr, k_ptr);
@@ -997,10 +1005,12 @@ int proc_dou8vec_minmax(const struct ctl_table *table, int dir,
tmp.maxlen = sizeof(val);
tmp.data = &val;
- if (!tmp.extra1)
- tmp.extra1 = (unsigned int *) &min;
- if (!tmp.extra2)
- tmp.extra2 = (unsigned int *) &max;
+ if (tmp.extra1 || tmp.extra2) {
+ if (!tmp.extra1)
+ tmp.extra1 = (unsigned int *) &min;
+ if (!tmp.extra2)
+ tmp.extra2 = (unsigned int *) &max;
+ }
val = READ_ONCE(*data);
res = proc_douintvec_minmax(&tmp, dir, buffer, lenp, ppos);
@@ -1045,9 +1055,13 @@ int proc_ulong_conv(ulong *u_ptr, ulong *k_ptr, int dir,
ret = user_to_kern(u_ptr, &tmp_k);
if (ret)
return ret;
- if ((tbl->extra1 && *(ulong *)tbl->extra1 > tmp_k) ||
- (tbl->extra2 && *(ulong *)tbl->extra2 < tmp_k))
+ if (tbl->extra1 || tbl->extra2) {
+ if ((tbl->extra1 && *(ulong *)tbl->extra1 > tmp_k) ||
+ (tbl->extra2 && *(ulong *)tbl->extra2 < tmp_k))
+ return -ERANGE;
+ } else if (tbl->min > tmp_k || tbl->max < tmp_k) {
return -ERANGE;
+ }
WRITE_ONCE(*k_ptr, tmp_k);
} else
return user_to_kern(u_ptr, k_ptr);
diff --git a/net/sysctl_net.c b/net/sysctl_net.c
index e190a639eef2..d335573012cf 100644
--- a/net/sysctl_net.c
+++ b/net/sysctl_net.c
@@ -122,13 +122,12 @@ __init int net_sysctl_init(void)
*/
static int ensure_safe_net_sysctl(struct net *net, const char *path,
const struct ctl_table *table,
- size_t table_size)
+ size_t table_size, size_t stride)
{
- const struct ctl_table *ent;
-
pr_debug("Registering net sysctl (net %p): %s\n", net, path);
- ent = table;
- for (size_t i = 0; i < table_size; ent++, i++) {
+ for (size_t i = 0; i < table_size; i++) {
+ const struct ctl_table *ent = (const struct ctl_table *)
+ ((const char *)table + i * stride);
unsigned long addr;
const char *where;
@@ -160,16 +159,28 @@ static int ensure_safe_net_sysctl(struct net *net, const char *path,
return 0;
}
+struct ctl_table_header *register_net_sysctl_stride(struct net *net,
+ const char *path,
+ const struct ctl_table *table,
+ size_t table_size,
+ size_t stride)
+{
+ if (!net_eq(net, &init_net))
+ if (ensure_safe_net_sysctl(net, path, table, table_size, stride))
+ return NULL;
+
+ return __register_sysctl_table_stride(&net->sysctls, path, table,
+ table_size, stride);
+}
+EXPORT_SYMBOL_GPL(register_net_sysctl_stride);
+
struct ctl_table_header *register_net_sysctl_sz(struct net *net,
const char *path,
const struct ctl_table *table,
size_t table_size)
{
- if (!net_eq(net, &init_net))
- if (ensure_safe_net_sysctl(net, path, table, table_size))
- return NULL;
-
- return __register_sysctl_table(&net->sysctls, path, table, table_size);
+ return register_net_sysctl_stride(net, path, table, table_size,
+ sizeof(struct ctl_table));
}
EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v2 2/5] sysctl: cover numeric bounds and table containers in KUnit
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 ` wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 3/5] netfilter: embed logger sysctl context using stride wen.yang
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
From: Wen Yang <wen.yang@linux.dev>
Add comprehensive KUnit test coverage for numeric bounds and table
containers without mutating live /proc/sys entries:
- Validate signed, unsigned and u8 numeric bounds across positive,
negative, zero and boundary ranges.
- Test atomic array commits rejecting partial updates.
- Test dynamic range updates and sibling bounds.
- Test container recovery with sysctl_table_container() across
multi-entry, per-entry, and copied table structures.
- Ensure SYSCTL_KUNIT_TEST depends on SYSCTL to prevent undefined
symbol errors when building as a module without CONFIG_SYSCTL.
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
kernel/sysctl-test.c | 776 +++++++++++++++++++++++++++++++++++++++++--
lib/Kconfig.debug | 1 +
2 files changed, 757 insertions(+), 20 deletions(-)
diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c
index 92f94ea28957..a48c2871e203 100644
--- a/kernel/sysctl-test.c
+++ b/kernel/sysctl-test.c
@@ -4,11 +4,27 @@
*/
#include <kunit/test.h>
+#include <linux/jiffies.h>
+#include <linux/limits.h>
#include <linux/sysctl.h>
+#if IS_ENABLED(CONFIG_NET)
+#endif
#define KUNIT_PROC_READ 0
#define KUNIT_PROC_WRITE 1
+/* data is the value; handlers recover the containing object from it. */
+#define SYSCTL_TEST_CTX(VALUE, NAME, MODE, HANDLER, CTX) \
+ { \
+ .procname = (NAME), \
+ .data = &(VALUE), \
+ .maxlen = sizeof(VALUE), \
+ .mode = (MODE), \
+ .proc_handler = (HANDLER), \
+ .min = 0, \
+ .max = ULONG_MAX, \
+ }
+
/*
* Test that proc_dointvec will not try to use a NULL .data field even when the
* length is non-zero.
@@ -26,8 +42,8 @@ static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
/*
* proc_dointvec expects a buffer in user space, so we allocate one. We
@@ -76,8 +92,8 @@ static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit *test)
.maxlen = 0,
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
GFP_USER);
@@ -119,8 +135,8 @@ static void sysctl_test_api_dointvec_table_len_is_zero(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
GFP_USER);
@@ -153,8 +169,8 @@ static void sysctl_test_api_dointvec_table_read_but_position_set(
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
GFP_USER);
@@ -188,8 +204,8 @@ static void sysctl_test_dointvec_read_happy_single_positive(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
size_t len = 4;
loff_t pos = 0;
@@ -219,8 +235,8 @@ static void sysctl_test_dointvec_read_happy_single_negative(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
size_t len = 5;
loff_t pos = 0;
@@ -248,8 +264,8 @@ static void sysctl_test_dointvec_write_happy_single_positive(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
char input[] = "9";
size_t len = sizeof(input) - 1;
@@ -278,8 +294,8 @@ static void sysctl_test_dointvec_write_happy_single_negative(struct kunit *test)
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
char input[] = "-9";
size_t len = sizeof(input) - 1;
@@ -310,8 +326,8 @@ static void sysctl_test_api_dointvec_write_single_less_int_min(
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
size_t max_len = 32, len = max_len;
loff_t pos = 0;
@@ -348,8 +364,8 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE_HUNDRED,
+ .min = 0,
+ .max = 100,
};
size_t max_len = 32, len = max_len;
loff_t pos = 0;
@@ -367,6 +383,715 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
}
+static void sysctl_test_entry_roundtrip(struct kunit *test,
+ const struct ctl_table *table, const char *input)
+{
+ char expected[32];
+ char *buffer = kunit_kzalloc(test, sizeof(expected), GFP_KERNEL);
+ size_t len = strlen(input);
+ loff_t pos = 0;
+
+ KUNIT_ASSERT_NOT_NULL(test, buffer);
+ KUNIT_ASSERT_LT(test, len, sizeof(expected) - 1);
+ memcpy(buffer, input, len);
+ KUNIT_ASSERT_EQ(test, table->proc_handler(table, KUNIT_PROC_WRITE,
+ buffer, &len, &pos), 0);
+ KUNIT_EXPECT_EQ(test, len, strlen(input));
+ KUNIT_EXPECT_EQ(test, pos, strlen(input));
+
+ snprintf(expected, sizeof(expected), "%s\n", input);
+ memset(buffer, 0, sizeof(expected));
+ len = sizeof(expected) - 1;
+ pos = 0;
+ KUNIT_ASSERT_EQ(test, table->proc_handler(table, KUNIT_PROC_READ,
+ buffer, &len, &pos), 0);
+ KUNIT_ASSERT_LT(test, len, sizeof(expected));
+ buffer[len] = '\0';
+ KUNIT_EXPECT_EQ(test, len, strlen(expected));
+ KUNIT_EXPECT_STREQ(test, buffer, expected);
+}
+
+/* A rejected write must not change any byte of the backing storage. */
+static void sysctl_test_entry_reject(struct kunit *test,
+ const struct ctl_table *table, const char *input)
+{
+ void *saved = kunit_kmalloc(test, table->maxlen, GFP_KERNEL);
+ char *buffer = kunit_kstrdup(test, input, GFP_KERNEL);
+ size_t len = strlen(input);
+ loff_t pos = 0;
+
+ KUNIT_ASSERT_NOT_NULL(test, saved);
+ KUNIT_ASSERT_NOT_NULL(test, buffer);
+ memcpy(saved, table->data, table->maxlen);
+ KUNIT_EXPECT_EQ(test, table->proc_handler(table, KUNIT_PROC_WRITE,
+ buffer, &len, &pos), -EINVAL);
+ KUNIT_EXPECT_MEMEQ(test, table->data, saved, table->maxlen);
+}
+
+/* Entries are full-range, lower-only, upper-only and explicitly zero-only. */
+enum sysctl_test_value_kind {
+ SYSCTL_TEST_VALUE_INT,
+ SYSCTL_TEST_VALUE_ULONG,
+ SYSCTL_TEST_VALUE_U8,
+};
+
+union sysctl_test_value {
+ int i;
+ unsigned long ul;
+ u8 b;
+};
+
+struct sysctl_test_range {
+ unsigned long min;
+ unsigned long max;
+};
+
+struct sysctl_test_limits {
+ proc_handler *handler;
+ size_t size;
+ enum sysctl_test_value_kind kind;
+ const struct sysctl_test_range *ranges;
+ size_t nranges;
+};
+
+static const struct sysctl_test_range int_limit_ranges[] = {
+ { (unsigned long)INT_MIN, (unsigned long)INT_MAX },
+ { 2, (unsigned long)INT_MAX },
+ { (unsigned long)INT_MIN, 4 },
+ { 0, 0 },
+};
+
+static const struct sysctl_test_range ulong_limit_ranges[] = {
+ { 0, ULONG_MAX },
+ { 2, ULONG_MAX },
+ { 0, 4 },
+ { 0, 0 },
+};
+
+static const struct sysctl_test_range u8_limit_ranges[] = {
+ { 0, U8_MAX },
+ { 2, U8_MAX },
+ { 0, 4 },
+ { 0, 0 },
+ { 2, 4 },
+};
+
+static const struct sysctl_test_limits limit_tests[] = {
+ {
+ .handler = proc_dointvec_minmax,
+ .size = sizeof(int),
+ .kind = SYSCTL_TEST_VALUE_INT,
+ .ranges = int_limit_ranges,
+ .nranges = ARRAY_SIZE(int_limit_ranges),
+ },
+ {
+ .handler = proc_doulongvec_minmax,
+ .size = sizeof(unsigned long),
+ .kind = SYSCTL_TEST_VALUE_ULONG,
+ .ranges = ulong_limit_ranges,
+ .nranges = ARRAY_SIZE(ulong_limit_ranges),
+ },
+ {
+ .handler = proc_dou8vec_minmax,
+ .size = sizeof(u8),
+ .kind = SYSCTL_TEST_VALUE_U8,
+ .ranges = u8_limit_ranges,
+ .nranges = ARRAY_SIZE(u8_limit_ranges),
+ },
+};
+
+static unsigned long sysctl_test_value_get(const union sysctl_test_value *value,
+ enum sysctl_test_value_kind kind)
+{
+ switch (kind) {
+ case SYSCTL_TEST_VALUE_INT:
+ return (unsigned long)(long)value->i;
+ case SYSCTL_TEST_VALUE_ULONG:
+ return value->ul;
+ case SYSCTL_TEST_VALUE_U8:
+ return value->b;
+ }
+ return 0;
+}
+
+static void sysctl_test_entry_range_limits(struct kunit *test,
+ const struct ctl_table *entries,
+ size_t nranges,
+ const char *minimum,
+ const char *maximum,
+ const char *underflow,
+ const char *overflow)
+{
+ sysctl_test_entry_roundtrip(test, &entries[0], minimum);
+ sysctl_test_entry_roundtrip(test, &entries[0], maximum);
+ sysctl_test_entry_reject(test, &entries[0], underflow);
+ sysctl_test_entry_reject(test, &entries[0], overflow);
+
+ sysctl_test_entry_roundtrip(test, &entries[1], "2");
+ sysctl_test_entry_roundtrip(test, &entries[1], maximum);
+ sysctl_test_entry_reject(test, &entries[1], "1");
+ sysctl_test_entry_reject(test, &entries[1], overflow);
+
+ sysctl_test_entry_roundtrip(test, &entries[2], minimum);
+ sysctl_test_entry_roundtrip(test, &entries[2], "4");
+ sysctl_test_entry_reject(test, &entries[2], "5");
+ sysctl_test_entry_reject(test, &entries[2], underflow);
+
+ sysctl_test_entry_roundtrip(test, &entries[3], "0");
+ sysctl_test_entry_reject(test, &entries[3], "1");
+ sysctl_test_entry_reject(test, &entries[3], "-1");
+
+ if (nranges > 4) {
+ sysctl_test_entry_roundtrip(test, &entries[4], "2");
+ sysctl_test_entry_roundtrip(test, &entries[4], "4");
+ sysctl_test_entry_reject(test, &entries[4], "1");
+ sysctl_test_entry_reject(test, &entries[4], "5");
+ }
+}
+
+static void sysctl_test_entry_limits(struct kunit *test)
+{
+ static const char *const names[] = {
+ "full", "lower", "upper", "zero", "both",
+ };
+ size_t t, i;
+
+ for (t = 0; t < ARRAY_SIZE(limit_tests); t++) {
+ const struct sysctl_test_limits *limits = &limit_tests[t];
+ union sysctl_test_value value = {};
+ struct ctl_table entries[5] = {};
+ char minimum[32], maximum[32], underflow[32], overflow[32];
+ unsigned long max_value;
+
+ for (i = 0; i < limits->nranges; i++) {
+ entries[i].procname = names[i];
+ entries[i].data = &value;
+ entries[i].maxlen = limits->size;
+ entries[i].mode = 0644;
+ entries[i].proc_handler = limits->handler;
+ entries[i].min = limits->ranges[i].min;
+ entries[i].max = limits->ranges[i].max;
+ }
+
+ switch (limits->kind) {
+ case SYSCTL_TEST_VALUE_INT:
+ snprintf(minimum, sizeof(minimum), "%d", INT_MIN);
+ snprintf(maximum, sizeof(maximum), "%d", INT_MAX);
+ snprintf(underflow, sizeof(underflow), "-%lu",
+ (unsigned long)INT_MAX + 2);
+ snprintf(overflow, sizeof(overflow), "%lu",
+ (unsigned long)INT_MAX + 1);
+ max_value = (unsigned long)INT_MAX;
+ break;
+ case SYSCTL_TEST_VALUE_ULONG:
+ snprintf(minimum, sizeof(minimum), "0");
+ snprintf(maximum, sizeof(maximum), "%lu", ULONG_MAX);
+ snprintf(underflow, sizeof(underflow), "-1");
+ snprintf(overflow, sizeof(overflow), "%lu0", ULONG_MAX);
+ max_value = ULONG_MAX;
+ break;
+ case SYSCTL_TEST_VALUE_U8:
+ snprintf(minimum, sizeof(minimum), "0");
+ snprintf(maximum, sizeof(maximum), "%u", U8_MAX);
+ snprintf(underflow, sizeof(underflow), "-1");
+ snprintf(overflow, sizeof(overflow), "%u", U8_MAX + 1);
+ max_value = U8_MAX;
+ break;
+ }
+
+ sysctl_test_entry_range_limits(test, entries, limits->nranges,
+ minimum, maximum, underflow, overflow);
+ sysctl_test_entry_roundtrip(test, &entries[0], maximum);
+ KUNIT_EXPECT_EQ(test, sysctl_test_value_get(&value, limits->kind),
+ max_value);
+ }
+}
+
+static void sysctl_test_entry_array_no_partial_commit(struct kunit *test)
+{
+ int signed_data[] = { 2, 3 };
+ unsigned long long_data[] = { 2, 3 };
+ unsigned int unsigned_data[] = { 2, 3 };
+ u8 byte_data[] = { 2, 3 };
+ const struct ctl_table entries[] = {
+ {
+ .procname = "signed",
+ .data = &(signed_data),
+ .maxlen = sizeof(signed_data),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .min = 2,
+ .max = 4,
+ },
+ {
+ .procname = "long",
+ .data = &(long_data),
+ .maxlen = sizeof(long_data),
+ .mode = 0644,
+ .proc_handler = proc_doulongvec_minmax,
+ .min = 2,
+ .max = 4,
+ },
+ {
+ .procname = "unsigned",
+ .data = &(unsigned_data),
+ .maxlen = sizeof(unsigned_data),
+ .mode = 0644,
+ .proc_handler = proc_douintvec_minmax,
+ .min = 0,
+ .max = UINT_MAX,
+ },
+ {
+ .procname = "byte",
+ .data = &(byte_data),
+ .maxlen = sizeof(byte_data),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .min = 0,
+ .max = U8_MAX,
+ },
+ };
+ int i;
+
+ for (i = 0; i < 2; i++) {
+ sysctl_test_entry_reject(test, &entries[i], "4\t5");
+ sysctl_test_entry_reject(test, &entries[i], "4\t1");
+ sysctl_test_entry_reject(test, &entries[i], "4\tbad");
+ sysctl_test_entry_reject(test, &entries[i], "4\t-1");
+ sysctl_test_entry_roundtrip(test, &entries[i], "4\t2");
+ }
+ for (; i < ARRAY_SIZE(entries); i++) {
+ char buffer[32] = {};
+ size_t len = sizeof(buffer);
+ loff_t pos = 0;
+
+ sysctl_test_entry_reject(test, &entries[i], "4\t2");
+ KUNIT_EXPECT_EQ(test, entries[i].proc_handler(&entries[i],
+ KUNIT_PROC_READ,
+ buffer, &len, &pos),
+ -EINVAL);
+ }
+ KUNIT_EXPECT_EQ(test, signed_data[0], 4);
+ KUNIT_EXPECT_EQ(test, signed_data[1], 2);
+ KUNIT_EXPECT_EQ(test, long_data[0], 4UL);
+ KUNIT_EXPECT_EQ(test, long_data[1], 2UL);
+}
+
+/* Each wrapper reads the related bound anew and resolves data in a local copy. */
+static int sysctl_test_entry_write(struct kunit *test, const struct ctl_table *table,
+ const char *input)
+{
+ char *buffer = kunit_kstrdup(test, input, GFP_KERNEL);
+ size_t len = strlen(input);
+ loff_t pos = 0;
+
+ if (!buffer)
+ return -ENOMEM;
+ return table->proc_handler(table, KUNIT_PROC_WRITE, buffer, &len, &pos);
+}
+
+/* Dynamic ranges refresh their limit on every call. */
+struct sysctl_test_dynamic {
+ union {
+ int i;
+ unsigned int u;
+ unsigned long l;
+ u8 b;
+ } value;
+ unsigned long min, max;
+ unsigned int calls;
+ proc_handler *handler;
+};
+
+static int sysctl_test_dynamic_handler(const struct ctl_table *table, int dir,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_dynamic *ctx =
+ container_of(table->data, struct sysctl_test_dynamic, value);
+ struct ctl_table resolved = *table;
+
+ ctx->calls++;
+ resolved.data = &ctx->value;
+ resolved.maxlen = ctx->handler == proc_doulongvec_minmax ?
+ sizeof(ctx->value.l) :
+ ctx->handler == proc_dou8vec_minmax ? sizeof(ctx->value.b) :
+ sizeof(ctx->value.i);
+ resolved.min = ctx->min;
+ resolved.max = ctx->max;
+ return ctx->handler(&resolved, dir, buffer, lenp, ppos);
+}
+
+static unsigned long sysctl_test_dynamic_read(const struct sysctl_test_dynamic *ctx)
+{
+ if (ctx->handler == proc_doulongvec_minmax)
+ return ctx->value.l;
+ if (ctx->handler == proc_douintvec_minmax)
+ return ctx->value.u;
+ if (ctx->handler == proc_dou8vec_minmax)
+ return ctx->value.b;
+ return (unsigned long)(long)ctx->value.i;
+}
+
+static void sysctl_test_dynamic_range(struct kunit *test)
+{
+ static proc_handler *const handlers[] = {
+ proc_dointvec_minmax, proc_douintvec_minmax,
+ proc_doulongvec_minmax, proc_dou8vec_minmax,
+ };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(handlers); i++) {
+ struct sysctl_test_dynamic ctx = { .min = 2, .max = 4,
+ .handler = handlers[i] };
+ const struct ctl_table table = SYSCTL_TEST_CTX(ctx.value, "dynamic",
+ 0644,
+ sysctl_test_dynamic_handler,
+ ctx);
+ sysctl_test_entry_roundtrip(test, &table, "4");
+ KUNIT_EXPECT_EQ(test, sysctl_test_dynamic_read(&ctx), 4UL);
+ KUNIT_EXPECT_GT(test, ctx.calls, 0U);
+
+ ctx.max = 2;
+ KUNIT_EXPECT_EQ(test, -EINVAL, sysctl_test_entry_write(test, &table, "3"));
+ KUNIT_EXPECT_EQ(test, sysctl_test_dynamic_read(&ctx), 4UL);
+
+ ctx.min = 3;
+ ctx.max = 4;
+ sysctl_test_entry_roundtrip(test, &table, "3");
+ KUNIT_EXPECT_EQ(test, sysctl_test_dynamic_read(&ctx), 3UL);
+ KUNIT_EXPECT_EQ(test, -EINVAL, sysctl_test_entry_write(test, &table, "2"));
+
+ ctx.min = 2;
+ ctx.max = 8;
+ sysctl_test_entry_roundtrip(test, &table, "8");
+ KUNIT_EXPECT_EQ(test, sysctl_test_dynamic_read(&ctx), 8UL);
+ KUNIT_EXPECT_GT(test, ctx.calls, 0U);
+ KUNIT_EXPECT_EQ(test, table.min, 0UL);
+ KUNIT_EXPECT_EQ(test, table.max, ULONG_MAX);
+ }
+}
+
+/* Sibling thresholds bound each other. */
+struct sysctl_test_sibling_long {
+ unsigned long high;
+ unsigned long low;
+};
+
+static int sysctl_test_high_long_handler(const struct ctl_table *table, int dir,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_sibling_long *ctx =
+ container_of(table->data, struct sysctl_test_sibling_long, high);
+ struct ctl_table resolved = *table;
+
+ resolved.data = &ctx->high;
+ resolved.maxlen = sizeof(ctx->high);
+ resolved.min = ctx->low;
+ resolved.max = ULONG_MAX;
+ return proc_doulongvec_minmax(&resolved, dir, buffer, lenp, ppos);
+}
+
+static int sysctl_test_low_long_handler(const struct ctl_table *table, int dir,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_sibling_long *ctx =
+ container_of(table->data, struct sysctl_test_sibling_long, low);
+ struct ctl_table resolved = *table;
+
+ resolved.data = &ctx->low;
+ resolved.maxlen = sizeof(ctx->low);
+ resolved.min = 0;
+ resolved.max = ctx->high;
+ return proc_doulongvec_minmax(&resolved, dir, buffer, lenp, ppos);
+}
+
+static void sysctl_test_runtime_sibling_bounds(struct kunit *test)
+{
+ struct sysctl_test_sibling_long ctx = { .high = 4, .low = 2 };
+ const struct ctl_table entries[] = {
+ SYSCTL_TEST_CTX(ctx.high, "high", 0644,
+ sysctl_test_high_long_handler, ctx),
+ SYSCTL_TEST_CTX(ctx.low, "low", 0644,
+ sysctl_test_low_long_handler, ctx),
+ };
+
+ sysctl_test_entry_roundtrip(test, &entries[0], "4");
+ KUNIT_EXPECT_EQ(test, -EINVAL, sysctl_test_entry_write(test, &entries[0], "1"));
+ KUNIT_EXPECT_EQ(test, ctx.high, 4UL);
+ sysctl_test_entry_roundtrip(test, &entries[1], "3");
+ KUNIT_EXPECT_EQ(test, ctx.low, 3UL);
+ KUNIT_EXPECT_EQ(test, -EINVAL, sysctl_test_entry_write(test, &entries[1], "5"));
+ KUNIT_EXPECT_EQ(test, ctx.low, 3UL);
+
+ ctx.low = 1;
+ sysctl_test_entry_roundtrip(test, &entries[0], "2");
+ KUNIT_EXPECT_EQ(test, ctx.high, 2UL);
+ KUNIT_EXPECT_EQ(test, -EINVAL, sysctl_test_entry_write(test, &entries[1], "3"));
+ KUNIT_EXPECT_EQ(test, ctx.low, 1UL);
+ sysctl_test_entry_roundtrip(test, &entries[1], "2");
+ KUNIT_EXPECT_EQ(test, ctx.low, 2UL);
+}
+
+/* Two object pointers supply value and limit without becoming bounds. */
+/* An owner embedded around the bound storage can be recovered with container_of(). */
+struct sysctl_test_owner {
+ int value;
+ int limit;
+};
+
+struct sysctl_test_holder {
+ struct sysctl_test_owner owner;
+};
+
+static int sysctl_test_owner_handler(const struct ctl_table *table, int dir,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_holder *holder =
+ container_of(table->data, struct sysctl_test_holder,
+ owner.value);
+ struct sysctl_test_owner *owner = &holder->owner;
+ struct ctl_table tmp = *table;
+
+ tmp.data = &owner->value;
+ tmp.maxlen = sizeof(owner->value);
+ tmp.min = 0;
+ tmp.max = READ_ONCE(owner->limit);
+ return proc_dointvec_minmax(&tmp, dir, buffer, lenp, ppos);
+}
+
+static void sysctl_test_runtime_owner_from_data(struct kunit *test)
+{
+ struct sysctl_test_holder holder = { .owner.limit = 4 };
+ const struct ctl_table table = {
+ .procname = "owner",
+ .data = &(holder.owner.value),
+ .maxlen = sizeof(holder.owner.value),
+ .mode = 0644,
+ .proc_handler = sysctl_test_owner_handler,
+ .min = 0,
+ .max = ULONG_MAX,
+ };
+
+ sysctl_test_entry_roundtrip(test, &table, "4");
+ KUNIT_EXPECT_EQ(test, holder.owner.value, 4);
+ holder.owner.limit = 2;
+ KUNIT_EXPECT_EQ(test, -EINVAL,
+ sysctl_test_entry_write(test, &table, "3"));
+ KUNIT_EXPECT_EQ(test, holder.owner.value, 4);
+ holder.owner.limit = 8;
+ sysctl_test_entry_roundtrip(test, &table, "8");
+ KUNIT_EXPECT_EQ(test, holder.owner.value, 8);
+ KUNIT_EXPECT_PTR_EQ(test, table.data, &holder.owner.value);
+}
+
+struct sysctl_test_dynamic_jiffies {
+ int int_value;
+ unsigned long ulong_value;
+ int int_min;
+ int int_max;
+ unsigned long ulong_min;
+ unsigned long ulong_max;
+ unsigned int calls;
+};
+
+static int sysctl_test_dynamic_jiffies_int_handler(const struct ctl_table *table,
+ int dir, void *buffer,
+ size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_dynamic_jiffies *ctx =
+ container_of(table->data, struct sysctl_test_dynamic_jiffies,
+ int_value);
+ struct ctl_table resolved = *table;
+
+ ctx->calls++;
+ resolved.data = &ctx->int_value;
+ resolved.maxlen = sizeof(ctx->int_value);
+ resolved.min = (unsigned long)(long)ctx->int_min;
+ resolved.max = (unsigned long)(long)ctx->int_max;
+ return proc_dointvec_ms_jiffies_minmax(&resolved, dir, buffer, lenp, ppos);
+}
+
+static int sysctl_test_dynamic_jiffies_ulong_handler(const struct ctl_table *table,
+ int dir, void *buffer,
+ size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_dynamic_jiffies *ctx =
+ container_of(table->data, struct sysctl_test_dynamic_jiffies,
+ ulong_value);
+ struct ctl_table resolved = *table;
+
+ ctx->calls++;
+ resolved.data = &ctx->ulong_value;
+ resolved.maxlen = sizeof(ctx->ulong_value);
+ resolved.min = ctx->ulong_min;
+ resolved.max = ctx->ulong_max;
+ return proc_doulongvec_ms_jiffies_minmax(&resolved, dir, buffer, lenp, ppos);
+}
+
+static void sysctl_test_dynamic_jiffies_ranges(struct kunit *test)
+{
+ struct sysctl_test_dynamic_jiffies ctx = {
+ .int_min = HZ,
+ .int_max = 2 * HZ,
+ .ulong_min = HZ,
+ .ulong_max = 2 * HZ,
+ };
+ const struct ctl_table entries[] = {
+ SYSCTL_TEST_CTX(ctx.int_value, "jiffies_int", 0644,
+ sysctl_test_dynamic_jiffies_int_handler, ctx),
+ SYSCTL_TEST_CTX(ctx.ulong_value, "jiffies_ulong", 0644,
+ sysctl_test_dynamic_jiffies_ulong_handler, ctx),
+ };
+
+ sysctl_test_entry_roundtrip(test, &entries[0], "1000");
+ KUNIT_EXPECT_EQ(test, ctx.int_value, HZ);
+ ctx.int_max = HZ;
+ KUNIT_EXPECT_EQ(test, -EINVAL,
+ sysctl_test_entry_write(test, &entries[0], "1500"));
+ KUNIT_EXPECT_EQ(test, ctx.int_value, HZ);
+ ctx.int_min = 2 * HZ;
+ ctx.int_max = 2 * HZ;
+ sysctl_test_entry_roundtrip(test, &entries[0], "2000");
+ KUNIT_EXPECT_EQ(test, ctx.int_value, 2 * HZ);
+
+ sysctl_test_entry_roundtrip(test, &entries[1], "1000");
+ KUNIT_EXPECT_EQ(test, ctx.ulong_value, (unsigned long)HZ);
+ ctx.ulong_max = HZ;
+ KUNIT_EXPECT_EQ(test, -EINVAL,
+ sysctl_test_entry_write(test, &entries[1], "1500"));
+ KUNIT_EXPECT_EQ(test, ctx.ulong_value, (unsigned long)HZ);
+ ctx.ulong_min = 2UL * HZ;
+ ctx.ulong_max = 2UL * HZ;
+ sysctl_test_entry_roundtrip(test, &entries[1], "2000");
+ KUNIT_EXPECT_EQ(test, ctx.ulong_value, 2UL * HZ);
+ KUNIT_EXPECT_GT(test, ctx.calls, 0U);
+}
+
+/* sysctl_table_container() coverage for embedded, shared and per-entry tables. */
+
+#define TEST_ENTRIES 8
+
+struct sysctl_test_shared_context {
+ const struct ctl_table table[TEST_ENTRIES];
+ int shared;
+};
+
+struct sysctl_test_entry_context {
+ int value;
+ int limit;
+};
+
+struct sysctl_test_per_entry_context {
+ const struct ctl_table table[TEST_ENTRIES];
+ struct sysctl_test_entry_context entries[TEST_ENTRIES];
+};
+
+struct sysctl_test_two_tables {
+ const struct ctl_table device_dir[2];
+ int shared;
+ const struct ctl_table vars[4];
+};
+
+struct sysctl_test_handler_state {
+ const struct ctl_table table[4];
+ int calls;
+};
+
+static int sysctl_test_counting_handler(const struct ctl_table *table, int dir,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct sysctl_test_handler_state *outer =
+ sysctl_table_container(table, struct sysctl_test_handler_state, table, 2);
+
+ (void)dir;
+ (void)buffer;
+ (void)lenp;
+ (void)ppos;
+ outer->calls++;
+ return 0;
+}
+
+static void sysctl_test_table_container_multi_entry(struct kunit *test)
+{
+ struct sysctl_test_shared_context outer = { .shared = 7 };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(outer.table); i++) {
+ struct sysctl_test_shared_context *p;
+
+ p = sysctl_table_container(&outer.table[i],
+ struct sysctl_test_shared_context, table, i);
+ KUNIT_EXPECT_PTR_EQ(test, p, &outer);
+ KUNIT_EXPECT_EQ(test, p->shared, 7);
+ }
+
+ {
+ const struct ctl_table *entry = &outer.table[3];
+ struct sysctl_test_shared_context *p;
+
+ p = sysctl_table_container(entry, struct sysctl_test_shared_context, table, 3);
+ KUNIT_EXPECT_PTR_EQ(test, p, &outer);
+ }
+}
+
+static void sysctl_test_table_container_per_entry_context(struct kunit *test)
+{
+ struct sysctl_test_per_entry_context outer;
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(outer.entries); i++) {
+ struct sysctl_test_per_entry_context *p;
+
+ outer.entries[i].value = i * 10;
+ outer.entries[i].limit = i * 10 + 5;
+ p = sysctl_table_container(&outer.table[i],
+ struct sysctl_test_per_entry_context, table, i);
+ KUNIT_EXPECT_PTR_EQ(test, p, &outer);
+ KUNIT_EXPECT_PTR_EQ(test, &p->entries[i], &outer.entries[i]);
+ KUNIT_EXPECT_EQ(test, p->entries[i].value, i * 10);
+ KUNIT_EXPECT_EQ(test, p->entries[i].limit, i * 10 + 5);
+ }
+}
+
+static void sysctl_test_table_container_two_arrays(struct kunit *test)
+{
+ struct sysctl_test_two_tables outer = { .shared = 3 };
+ struct sysctl_test_two_tables *p;
+
+ p = sysctl_table_container(&outer.device_dir[0],
+ struct sysctl_test_two_tables, device_dir, 0);
+ KUNIT_EXPECT_PTR_EQ(test, p, &outer);
+
+ p = sysctl_table_container(&outer.vars[3], struct sysctl_test_two_tables, vars, 3);
+ KUNIT_EXPECT_PTR_EQ(test, p, &outer);
+ KUNIT_EXPECT_EQ(test, p->shared, 3);
+}
+
+static void sysctl_test_table_container_handler(struct kunit *test)
+{
+ struct sysctl_test_handler_state outer = { .calls = 5 };
+ int ret;
+
+ ret = sysctl_test_counting_handler(&outer.table[2], 0, NULL, NULL, NULL);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, outer.calls, 6);
+}
+
+static void sysctl_test_table_container_copied_outer(struct kunit *test)
+{
+ struct sysctl_test_shared_context outer = { .shared = 123 };
+ struct sysctl_test_shared_context *copy;
+ struct sysctl_test_shared_context *p;
+
+ copy = kunit_kzalloc(test, sizeof(*copy), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, copy);
+ memcpy(copy, &outer, sizeof(outer));
+
+ p = sysctl_table_container(©->table[6], struct sysctl_test_shared_context, table, 6);
+ KUNIT_EXPECT_PTR_EQ(test, p, copy);
+ KUNIT_EXPECT_EQ(test, p->shared, 123);
+}
+
static struct kunit_case sysctl_test_cases[] = {
KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
@@ -378,6 +1103,17 @@ static struct kunit_case sysctl_test_cases[] = {
KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative),
KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
+ KUNIT_CASE(sysctl_test_entry_limits),
+ KUNIT_CASE(sysctl_test_entry_array_no_partial_commit),
+ KUNIT_CASE(sysctl_test_dynamic_range),
+ KUNIT_CASE(sysctl_test_runtime_sibling_bounds),
+ KUNIT_CASE(sysctl_test_runtime_owner_from_data),
+ KUNIT_CASE(sysctl_test_dynamic_jiffies_ranges),
+ KUNIT_CASE(sysctl_test_table_container_multi_entry),
+ KUNIT_CASE(sysctl_test_table_container_per_entry_context),
+ KUNIT_CASE(sysctl_test_table_container_two_arrays),
+ KUNIT_CASE(sysctl_test_table_container_handler),
+ KUNIT_CASE(sysctl_test_table_container_copied_outer),
{}
};
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1dac2674a983..bda6ebb25471 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2847,6 +2847,7 @@ config RESOURCE_KUNIT_TEST
config SYSCTL_KUNIT_TEST
tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
depends on KUNIT
+ depends on SYSCTL
default KUNIT_ALL_TESTS
help
This builds the proc sysctl unit test, which runs on boot.
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v2 3/5] netfilter: embed logger sysctl context using stride
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 ` wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 4/5] net/ipv4: embed device " wen.yang
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
From: Wen Yang <wen.yang@linux.dev>
Embed struct ctl_table into struct nf_log_sysctl_entry along with
per-entry net and pf context, and register the array with
register_net_sysctl_stride().
In the proc_handler nf_log_proc_dostring(), recover the entry using
standard container_of(table, struct nf_log_sysctl_entry, ctl). This
eliminates the need for 11 per-family wrapper functions while keeping
struct ctl_table free of a priv field.
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
net/netfilter/nf_log.c | 84 +++++++++++++++++++++---------------------
1 file changed, 42 insertions(+), 42 deletions(-)
diff --git a/net/netfilter/nf_log.c b/net/netfilter/nf_log.c
index f1d3649e3fc6..c8cbbadc50ba 100644
--- a/net/netfilter/nf_log.c
+++ b/net/netfilter/nf_log.c
@@ -420,8 +420,13 @@ static const struct seq_operations nflog_seq_ops = {
#endif /* PROC_FS */
#ifdef CONFIG_SYSCTL
+struct nf_log_sysctl_entry {
+ struct ctl_table ctl;
+ struct net *net;
+ int pf;
+};
+
static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
-static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO];
static struct ctl_table_header *nf_log_sysctl_fhdr;
static struct ctl_table nf_log_sysctl_ftable[] = {
@@ -437,11 +442,13 @@ static struct ctl_table nf_log_sysctl_ftable[] = {
static int nf_log_proc_dostring(const struct ctl_table *table, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
+ const struct nf_log_sysctl_entry *entry =
+ container_of(table, struct nf_log_sysctl_entry, ctl);
const struct nf_logger *logger;
char buf[NFLOGGER_NAME_LEN];
int r = 0;
- int tindex = (unsigned long)table->extra1;
- struct net *net = table->extra2;
+ int tindex = entry->pf;
+ struct net *net = entry->net;
if (write) {
struct ctl_table tmp = *table;
@@ -486,66 +493,59 @@ static int nf_log_proc_dostring(const struct ctl_table *table, int write,
static int netfilter_log_sysctl_init(struct net *net)
{
+ struct nf_log_sysctl_entry *entries;
int i;
- struct ctl_table *table;
-
- table = nf_log_sysctl_table;
- if (!net_eq(net, &init_net)) {
- table = kmemdup(nf_log_sysctl_table,
- sizeof(nf_log_sysctl_table),
- GFP_KERNEL_ACCOUNT);
- if (!table)
- goto err_alloc;
- } else {
- for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
- snprintf(nf_log_sysctl_fnames[i],
- 3, "%d", i);
- nf_log_sysctl_table[i].procname =
- nf_log_sysctl_fnames[i];
- nf_log_sysctl_table[i].maxlen = NFLOGGER_NAME_LEN;
- nf_log_sysctl_table[i].mode = 0644;
- nf_log_sysctl_table[i].proc_handler =
- nf_log_proc_dostring;
- nf_log_sysctl_table[i].extra1 =
- (void *)(unsigned long) i;
- }
+
+ entries = kzalloc(sizeof(*entries) * NFPROTO_NUMPROTO, GFP_KERNEL_ACCOUNT);
+ if (!entries)
+ return -ENOMEM;
+
+ for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
+ snprintf(nf_log_sysctl_fnames[i], 3, "%d", i);
+ entries[i].ctl.procname = nf_log_sysctl_fnames[i];
+ entries[i].ctl.maxlen = NFLOGGER_NAME_LEN;
+ entries[i].ctl.mode = 0644;
+ entries[i].ctl.proc_handler = nf_log_proc_dostring;
+ entries[i].net = net;
+ entries[i].pf = i;
+ }
+
+ if (net_eq(net, &init_net)) {
nf_log_sysctl_fhdr = register_net_sysctl(net, "net/netfilter",
nf_log_sysctl_ftable);
- if (!nf_log_sysctl_fhdr)
- goto err_freg;
+ if (!nf_log_sysctl_fhdr) {
+ kfree(entries);
+ return -ENOMEM;
+ }
}
- for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
- table[i].extra2 = net;
-
- net->nf.nf_log_dir_header = register_net_sysctl_sz(net,
- "net/netfilter/nf_log",
- table,
- ARRAY_SIZE(nf_log_sysctl_table));
+ net->nf.nf_log_dir_header = register_net_sysctl_stride(net,
+ "net/netfilter/nf_log",
+ &entries[0].ctl,
+ NFPROTO_NUMPROTO,
+ sizeof(*entries));
if (!net->nf.nf_log_dir_header)
goto err_reg;
return 0;
err_reg:
- if (!net_eq(net, &init_net))
- kfree(table);
- else
+ if (net_eq(net, &init_net))
unregister_net_sysctl_table(nf_log_sysctl_fhdr);
-err_freg:
-err_alloc:
+ kfree(entries);
return -ENOMEM;
}
static void netfilter_log_sysctl_exit(struct net *net)
{
const struct ctl_table *table;
+ struct nf_log_sysctl_entry *entries;
table = net->nf.nf_log_dir_header->ctl_table_arg;
+ entries = container_of(table, struct nf_log_sysctl_entry, ctl);
unregister_net_sysctl_table(net->nf.nf_log_dir_header);
- if (!net_eq(net, &init_net))
- kfree(table);
- else
+ kfree(entries);
+ if (net_eq(net, &init_net))
unregister_net_sysctl_table(nf_log_sysctl_fhdr);
}
#else
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v2 4/5] net/ipv4: embed device sysctl context using stride
2026-11-16 16:07 [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride wen.yang
` (2 preceding siblings ...)
2026-11-16 16:07 ` [RFC PATCH v2 3/5] netfilter: embed logger sysctl context using stride wen.yang
@ 2026-11-16 16:07 ` wen.yang
2026-11-16 16:07 ` [RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds wen.yang
2026-11-16 21:40 ` [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride David Laight
5 siblings, 0 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
From: Wen Yang <wen.yang@linux.dev>
Embed struct ctl_table into struct devinet_sysctl_entry with devconf
and net pointers, and register devinet arrays using
register_net_sysctl_stride().
Both devinet_conf_proc() and devinet_sysctl_forward() recover their
context via container_of(ctl, struct devinet_sysctl_entry, ctl). This
completely eliminates the need for 32 per-attribute wrapper functions
while avoiding any priv field in struct ctl_table.
Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
net/ipv4/devinet.c | 190 +++++++++++++++++++++++++--------------------
1 file changed, 104 insertions(+), 86 deletions(-)
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 1e9d988849c9..1692a597be18 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -2555,17 +2555,25 @@ static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
}
}
+struct devinet_sysctl_entry {
+ struct ctl_table ctl;
+ struct ipv4_devconf *cnf;
+ struct net *net;
+};
+
static int devinet_conf_proc(const struct ctl_table *ctl, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
+ const struct devinet_sysctl_entry *entry =
+ container_of(ctl, struct devinet_sysctl_entry, ctl);
int old_value = *(int *)ctl->data;
int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
int new_value = *(int *)ctl->data;
if (write) {
- struct ipv4_devconf *cnf = ctl->extra1;
+ struct ipv4_devconf *cnf = entry->cnf;
int i = (int *)ctl->data - cnf->data;
- struct net *net = ctl->extra2;
+ struct net *net = entry->net;
int ifindex;
/* These attributes are bypassing the tracking state,
@@ -2597,7 +2605,9 @@ static int devinet_conf_proc(const struct ctl_table *ctl, int write,
static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
- struct net *net = ctl->extra2;
+ const struct devinet_sysctl_entry *entry =
+ container_of(ctl, struct devinet_sysctl_entry, ctl);
+ struct net *net = entry->net;
int *valp = ctl->data;
int val = *valp;
int ret;
@@ -2617,7 +2627,7 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
inet_forward_change(net);
} else {
- struct ipv4_devconf *cnf = ctl->extra1;
+ struct ipv4_devconf *cnf = entry->cnf;
struct in_device *idev =
container_of(cnf, struct in_device, cnf);
if (*valp)
@@ -2641,13 +2651,14 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
{ \
- .procname = name, \
- .data = ipv4_devconf.data + \
- IPV4_DEVCONF_ ## attr - 1, \
- .maxlen = sizeof(int), \
- .mode = mval, \
- .proc_handler = proc, \
- .extra1 = &ipv4_devconf, \
+ .ctl = { \
+ .procname = name, \
+ .data = ipv4_devconf.data + \
+ IPV4_DEVCONF_ ## attr - 1, \
+ .maxlen = sizeof(int), \
+ .mode = mval, \
+ .proc_handler = proc, \
+ }, \
}
#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
@@ -2659,57 +2670,57 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
-static struct devinet_sysctl_table {
+static const struct devinet_sysctl_entry devinet_sysctl[IPV4_DEVCONF_MAX] = {
+ DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
+ devinet_sysctl_forward),
+ DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
+ DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
+
+ DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
+ DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
+ DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
+ DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
+ DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
+ DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
+ "accept_source_route"),
+ DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
+ DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
+ DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
+ DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
+ DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
+ DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
+ DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
+ DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
+ DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
+ DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
+ DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
+ DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
+ DEVINET_SYSCTL_RW_ENTRY(ARP_EVICT_NOCARRIER,
+ "arp_evict_nocarrier"),
+ DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
+ DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
+ "force_igmp_version"),
+ DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
+ "igmpv2_unsolicited_report_interval"),
+ DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
+ "igmpv3_unsolicited_report_interval"),
+ DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
+ "ignore_routes_with_linkdown"),
+ DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
+ "drop_gratuitous_arp"),
+ DEVINET_SYSCTL_RW_ENTRY(NOXFRM, "disable_xfrm"),
+ DEVINET_SYSCTL_RW_ENTRY(NOPOLICY, "disable_policy"),
+ DEVINET_SYSCTL_RW_ENTRY(PROMOTE_SECONDARIES,
+ "promote_secondaries"),
+ DEVINET_SYSCTL_RW_ENTRY(ROUTE_LOCALNET,
+ "route_localnet"),
+ DEVINET_SYSCTL_RW_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
+ "drop_unicast_in_l2_multicast"),
+};
+
+struct devinet_sysctl_table {
struct ctl_table_header *sysctl_header;
- struct ctl_table devinet_vars[IPV4_DEVCONF_MAX];
-} devinet_sysctl = {
- .devinet_vars = {
- DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
- devinet_sysctl_forward),
- DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
- DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
-
- DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
- DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
- DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
- DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
- DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
- DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
- "accept_source_route"),
- DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
- DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
- DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
- DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
- DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
- DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
- DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
- DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
- DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
- DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
- DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
- DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
- DEVINET_SYSCTL_RW_ENTRY(ARP_EVICT_NOCARRIER,
- "arp_evict_nocarrier"),
- DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
- DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
- "force_igmp_version"),
- DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
- "igmpv2_unsolicited_report_interval"),
- DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
- "igmpv3_unsolicited_report_interval"),
- DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
- "ignore_routes_with_linkdown"),
- DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
- "drop_gratuitous_arp"),
- DEVINET_SYSCTL_RW_ENTRY(NOXFRM, "disable_xfrm"),
- DEVINET_SYSCTL_RW_ENTRY(NOPOLICY, "disable_policy"),
- DEVINET_SYSCTL_RW_ENTRY(PROMOTE_SECONDARIES,
- "promote_secondaries"),
- DEVINET_SYSCTL_RW_ENTRY(ROUTE_LOCALNET,
- "route_localnet"),
- DEVINET_SYSCTL_RW_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
- "drop_unicast_in_l2_multicast"),
- },
+ struct devinet_sysctl_entry entries[IPV4_DEVCONF_MAX];
};
static int __devinet_sysctl_register(struct net *net, char *dev_name,
@@ -2719,19 +2730,22 @@ static int __devinet_sysctl_register(struct net *net, char *dev_name,
struct devinet_sysctl_table *t;
char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
- t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL_ACCOUNT);
+ t = kmemdup(&devinet_sysctl, sizeof(devinet_sysctl), GFP_KERNEL_ACCOUNT);
if (!t)
goto out;
- for (i = 0; i < ARRAY_SIZE(t->devinet_vars); i++) {
- t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
- t->devinet_vars[i].extra1 = p;
- t->devinet_vars[i].extra2 = net;
+ for (i = 0; i < ARRAY_SIZE(t->entries); i++) {
+ t->entries[i].ctl.data += (char *)p - (char *)&ipv4_devconf;
+ t->entries[i].cnf = p;
+ t->entries[i].net = net;
}
snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
- t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
+ t->sysctl_header = register_net_sysctl_stride(net, path,
+ &t->entries[0].ctl,
+ ARRAY_SIZE(t->entries),
+ sizeof(t->entries[0]));
if (!t->sysctl_header)
goto free;
@@ -2786,16 +2800,16 @@ static void devinet_sysctl_unregister(struct in_device *idev)
neigh_sysctl_unregister(idev->arp_parms);
}
-static const struct ctl_table ctl_forward_entry[] = {
+static const struct devinet_sysctl_entry ctl_forward_entry[] = {
{
- .procname = "ip_forward",
- .data = &ipv4_devconf.data[
- IPV4_DEVCONF_FORWARDING - 1],
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = devinet_sysctl_forward,
- .extra1 = &ipv4_devconf,
- .extra2 = &init_net,
+ .ctl = {
+ .procname = "ip_forward",
+ .data = &ipv4_devconf.data[
+ IPV4_DEVCONF_FORWARDING - 1],
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = devinet_sysctl_forward,
+ },
},
};
#endif
@@ -2804,7 +2818,7 @@ static __net_init int devinet_init_net(struct net *net)
{
#ifdef CONFIG_SYSCTL
struct ctl_table_header *forw_hdr;
- struct ctl_table *tbl;
+ struct devinet_sysctl_entry *fwd_entry;
#endif
struct ipv4_devconf *all, *dflt;
int err;
@@ -2825,13 +2839,13 @@ static __net_init int devinet_init_net(struct net *net)
goto err_alloc_dflt;
#ifdef CONFIG_SYSCTL
- tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
- if (!tbl)
+ fwd_entry = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
+ if (!fwd_entry)
goto err_alloc_ctl;
- tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
- tbl[0].extra1 = all;
- tbl[0].extra2 = net;
+ fwd_entry[0].ctl.data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
+ fwd_entry[0].cnf = all;
+ fwd_entry[0].net = net;
#endif
if (!net_eq(net, &init_net)) {
@@ -2869,8 +2883,10 @@ static __net_init int devinet_init_net(struct net *net)
goto err_reg_dflt;
err = -ENOMEM;
- forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl,
- ARRAY_SIZE(ctl_forward_entry));
+ forw_hdr = register_net_sysctl_stride(net, "net/ipv4",
+ &fwd_entry[0].ctl,
+ ARRAY_SIZE(ctl_forward_entry),
+ sizeof(fwd_entry[0]));
if (!forw_hdr)
goto err_reg_ctl;
net->ipv4.forw_hdr = forw_hdr;
@@ -2891,7 +2907,7 @@ static __net_init int devinet_init_net(struct net *net)
err_reg_dflt:
__devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
err_reg_all:
- kfree(tbl);
+ kfree(fwd_entry);
err_alloc_ctl:
#endif
kfree(dflt);
@@ -2907,18 +2923,20 @@ static __net_exit void devinet_exit_net(struct net *net)
{
#ifdef CONFIG_SYSCTL
const struct ctl_table *tbl;
+ struct devinet_sysctl_entry *fwd;
#endif
cancel_delayed_work_sync(&net->ipv4.addr_chk_work);
#ifdef CONFIG_SYSCTL
tbl = net->ipv4.forw_hdr->ctl_table_arg;
+ fwd = container_of(tbl, struct devinet_sysctl_entry, ctl);
unregister_net_sysctl_table(net->ipv4.forw_hdr);
__devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
NETCONFA_IFINDEX_DEFAULT);
__devinet_sysctl_unregister(net, net->ipv4.devconf_all,
NETCONFA_IFINDEX_ALL);
- kfree(tbl);
+ kfree(fwd);
#endif
kfree(net->ipv4.devconf_dflt);
kfree(net->ipv4.devconf_all);
--
2.25.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds
2026-11-16 16:07 [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride wen.yang
` (3 preceding siblings ...)
2026-11-16 16:07 ` [RFC PATCH v2 4/5] net/ipv4: embed device " wen.yang
@ 2026-11-16 16:07 ` wen.yang
2026-11-16 21:40 ` [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride David Laight
5 siblings, 0 replies; 7+ messages in thread
From: wen.yang @ 2026-11-16 16:07 UTC (permalink / raw)
To: Joel Granados, David Laight, Kees Cook
Cc: Wen Yang, linux-fsdevel, linux-kernel
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
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride
2026-11-16 16:07 [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride wen.yang
` (4 preceding siblings ...)
2026-11-16 16:07 ` [RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds wen.yang
@ 2026-11-16 21:40 ` David Laight
5 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2026-11-16 21:40 UTC (permalink / raw)
To: wen.yang; +Cc: Joel Granados, Kees Cook, linux-fsdevel, linux-kernel
On Tue, 17 Nov 2026 00:07:32 +0800
wen.yang@linux.dev wrote:
> From: Wen Yang <wen.yang@linux.dev>
>
> struct ctl_table has long relied on extra1 and extra2 pointers to serve
> multiple unrelated duties, including range limits and caller-specific
> context. This series addresses both:
>
> 1. Replace bound pointers with plain unsigned long min and max fields
> directly in struct ctl_table.
>
> 2. Replace per-entry context pointers with a core stride mechanism,
> allowing callers to register arrays of structures embedding struct
> ctl_table and recover enclosing context via standard container_of().
>
> This RFC focuses on the core stride infrastructure and two representative
> complex users (netfilter nf_log and IPv4 devinet), as suggested by Joel
> Granados. The remaining treewide callers are converted in a single
> squashed commit to ensure the tree builds and tests pass cleanly.
I think I would have tried to avoid the indentation changes - just to
make the actual changes easier to see.
...
> 4. Baseline rebase & conflict resolution:
> - Rebased onto next-20261111 (aaa2f954c21b).
> - Added proc_dointvec_jiffies_minmax and proc_dointvec_ms_jiffies_minmax
> to sysctl_check_table().
I didn't spot those in here, but are they even needed?
Without the bounds don't the minmax variants behave like the original functions?
So it was only necessary to add support for bounds to the existing functions.
(The only think that half makes sense is if a caller uses extra1/2 for
something else.)
Related, I think you can reduce the size of the final patch and the likely hood
of some silly errors by making the 'dointvec' functions treat min == 0, max == 0
(ie no initialisers) as 'no limit' rather than 'only zero allowed'.
David
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-16 21:40 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH v2 5/5] scripts/coccinelle: add script to detect and convert sysctl bounds wen.yang
2026-11-16 21:40 ` [RFC PATCH v2 0/6] sysctl: replace overloaded extra pointers with typed bounds and core stride David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox