[RFC PATCH 34/39] ipv4: move device sysctl context to priv
From: wen . yang
Date: Wed Oct 28 2026 - 12:43:13 EST
From: Wen Yang <wen.yang@xxxxxxxxx>
Like addrconf, devinet needs the ipv4_devconf and the netns in each
handler, and the per-net ip_forward entry needs the all-devconf. Those
pointers currently share extra1/extra2 with the numeric bounds.
Put them in priv, keep data as the devconf field, and let registration
set only priv. The forwarding entry points data straight at the
all-devconf forwarding slot.
Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
---
net/ipv4/devinet.c | 53 ++++++++++++++++++++++++++++++++--------------
1 file changed, 37 insertions(+), 16 deletions(-)
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 1e9d988849c9..e1899b268a34 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -2555,17 +2555,26 @@ static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
}
}
+/* Owned by the registered table; devconf and net outlive its handlers. */
+struct devinet_sysctl_ctx {
+ struct ipv4_devconf *devconf;
+ struct net *net;
+};
+
static int devinet_conf_proc(const struct ctl_table *ctl, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
- int old_value = *(int *)ctl->data;
- int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
- int new_value = *(int *)ctl->data;
+ const struct devinet_sysctl_ctx *ctx = ctl->priv;
+ int old_value, ret, new_value;
+
+ old_value = *(int *)ctl->data;
+ ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
+ new_value = *(int *)ctl->data;
if (write) {
- struct ipv4_devconf *cnf = ctl->extra1;
+ struct ipv4_devconf *cnf = ctx->devconf;
int i = (int *)ctl->data - cnf->data;
- struct net *net = ctl->extra2;
+ struct net *net = ctx->net;
int ifindex;
/* These attributes are bypassing the tracking state,
@@ -2597,7 +2606,8 @@ 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_ctx *ctx = ctl->priv;
+ struct net *net = ctx->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 = ctx->devconf;
struct in_device *idev =
container_of(cnf, struct in_device, cnf);
if (*valp)
@@ -2647,7 +2657,6 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
.maxlen = sizeof(int), \
.mode = mval, \
.proc_handler = proc, \
- .extra1 = &ipv4_devconf, \
}
#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
@@ -2662,6 +2671,7 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
static struct devinet_sysctl_table {
struct ctl_table_header *sysctl_header;
struct ctl_table devinet_vars[IPV4_DEVCONF_MAX];
+ struct devinet_sysctl_ctx contexts[IPV4_DEVCONF_MAX];
} devinet_sysctl = {
.devinet_vars = {
DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
@@ -2725,8 +2735,12 @@ static int __devinet_sysctl_register(struct net *net, char *dev_name,
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;
+ if (t->devinet_vars[i].proc_handler == devinet_conf_proc ||
+ t->devinet_vars[i].proc_handler == devinet_sysctl_forward) {
+ t->contexts[i].devconf = p;
+ t->contexts[i].net = net;
+ t->devinet_vars[i].priv = &t->contexts[i];
+ }
}
snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
@@ -2786,6 +2800,11 @@ static void devinet_sysctl_unregister(struct in_device *idev)
neigh_sysctl_unregister(idev->arp_parms);
}
+struct devinet_forward_table {
+ struct ctl_table table[1];
+ struct devinet_sysctl_ctx context;
+};
+
static const struct ctl_table ctl_forward_entry[] = {
{
.procname = "ip_forward",
@@ -2794,8 +2813,6 @@ static const struct ctl_table ctl_forward_entry[] = {
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = devinet_sysctl_forward,
- .extra1 = &ipv4_devconf,
- .extra2 = &init_net,
},
};
#endif
@@ -2805,6 +2822,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_forward_table *forward;
#endif
struct ipv4_devconf *all, *dflt;
int err;
@@ -2825,13 +2843,16 @@ 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)
+ forward = kmalloc(sizeof(*forward), GFP_KERNEL);
+ if (!forward)
goto err_alloc_ctl;
+ tbl = forward->table;
+ memcpy(tbl, ctl_forward_entry, sizeof(ctl_forward_entry));
+ forward->context.devconf = all;
+ forward->context.net = net;
tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
- tbl[0].extra1 = all;
- tbl[0].extra2 = net;
+ tbl[0].priv = &forward->context;
#endif
if (!net_eq(net, &init_net)) {
--
2.25.1