From: Alexey Gladkov <legion@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>,
"Eric W . Biederman" <ebiederm@xmission.com>,
Kees Cook <kees@kernel.org>,
Joel Granados <joel.granados@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>, linux-fsdevel@vger.kernel.org
Subject: [RFC PATCH v1 19/30] sysctl: net: use sysctl_field in net core per-net sysctls
Date: Wed, 26 Aug 2026 21:42:23 +0200 [thread overview]
Message-ID: <b73d88fa166b9b582f9c335133eb78dc2a6e5f1b.1787771905.git.legion@kernel.org> (raw)
In-Reply-To: <cover.1787771905.git.legion@kernel.org>
The net core per-net sysctl table is cloned for every non-init network
namespace so that the per-net entries can be patched to point at the
current struct net and the global buffer limit entries can be made
read-only.
Describe the table with sysctl_field instead. The per-net entries now
derive their storage from the registration context, and the global
buffer limit entries keep their init-net-only write permission through
a mode callback.
This keeps the table static and const while preserving the existing
permission model.
Signed-off-by: Alexey Gladkov <legion@kernel.org>
---
net/core/sysctl_net_core.c | 241 ++++++++++++++++++-------------------
1 file changed, 116 insertions(+), 125 deletions(-)
diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index b508618bfc12..39b112a302c2 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -678,100 +678,118 @@ static struct ctl_table net_core_table[] = {
},
};
-static struct ctl_table netns_core_table[] = {
+static umode_t netns_core_init_net_writable_mode(const struct sysctl_context *ctx)
+{
+ return net_eq(ctx->ns.net_ns, &init_net) ? 0644 : 0444;
+}
+
+#define NETNS_CORE_SYSCTL_DATA(type, name) \
+static type *netns_core_##name##_data(const struct sysctl_context *ctx) \
+{ \
+ return &ctx->ns.net_ns->core.sysctl_##name; \
+}
+
+#define NETNS_CORE_SYSCTL_CUSTOM_DATA(name) \
+static void *netns_core_##name##_data(const struct sysctl_context *ctx) \
+{ \
+ return &ctx->ns.net_ns->core.sysctl_##name; \
+}
+
+NETNS_CORE_SYSCTL_DATA(int, somaxconn)
+NETNS_CORE_SYSCTL_DATA(int, optmem_max)
+NETNS_CORE_SYSCTL_DATA(u8, txrehash)
+NETNS_CORE_SYSCTL_CUSTOM_DATA(txq_reselection)
+NETNS_CORE_SYSCTL_DATA(u8, bypass_prot_mem)
+
+static u8 *netns_core_tstamp_allow_data_value(const struct sysctl_context *ctx)
+{
+ return &ctx->ns.net_ns->core.sysctl_tstamp_allow_data;
+}
+
#if IS_ENABLED(CONFIG_RPS)
- {
- .procname = "rps_default_mask",
- .data = &init_net,
- .mode = 0644,
- .proc_handler = rps_default_mask_sysctl
- },
+static void *netns_core_rps_default_mask_data(const struct sysctl_context *ctx)
+{
+ return ctx->ns.net_ns;
+}
#endif
- {
- .procname = "somaxconn",
- .data = &init_net.core.sysctl_somaxconn,
- .maxlen = sizeof(int),
- .mode = 0644,
- .extra1 = SYSCTL_ZERO,
- .proc_handler = proc_dointvec_minmax
- },
- {
- .procname = "optmem_max",
- .data = &init_net.core.sysctl_optmem_max,
- .maxlen = sizeof(int),
- .mode = 0644,
- .extra1 = SYSCTL_ZERO,
- .proc_handler = proc_dointvec_minmax
- },
- {
- .procname = "txrehash",
- .data = &init_net.core.sysctl_txrehash,
- .maxlen = sizeof(u8),
- .mode = 0644,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE,
- .proc_handler = proc_dou8vec_minmax,
- },
- {
- .procname = "txq_reselection_ms",
- .data = &init_net.core.sysctl_txq_reselection,
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = proc_dointvec_ms_jiffies,
- },
- {
- .procname = "tstamp_allow_data",
- .data = &init_net.core.sysctl_tstamp_allow_data,
- .maxlen = sizeof(u8),
- .mode = 0644,
- .proc_handler = proc_dou8vec_minmax,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE
- },
- {
- .procname = "bypass_prot_mem",
- .data = &init_net.core.sysctl_bypass_prot_mem,
- .maxlen = sizeof(u8),
- .mode = 0644,
- .proc_handler = proc_dou8vec_minmax,
- .extra1 = SYSCTL_ZERO,
- .extra2 = SYSCTL_ONE
- },
- /* sysctl_core_net_init() will set the values after this
- * to readonly in network namespaces
- */
- {
- .procname = "wmem_max",
- .data = &sysctl_wmem_max,
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = proc_dointvec_minmax,
- .extra1 = &min_sndbuf,
- },
- {
- .procname = "rmem_max",
- .data = &sysctl_rmem_max,
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = proc_dointvec_minmax,
- .extra1 = &min_rcvbuf,
- },
- {
- .procname = "wmem_default",
- .data = &sysctl_wmem_default,
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = proc_dointvec_minmax,
- .extra1 = &min_sndbuf,
- },
- {
- .procname = "rmem_default",
- .data = &sysctl_rmem_default,
- .maxlen = sizeof(int),
- .mode = 0644,
- .proc_handler = proc_dointvec_minmax,
- .extra1 = &min_rcvbuf,
- },
+
+static void *netns_core_sysctl_wmem_max_data(const struct sysctl_context *ctx)
+{
+ return &sysctl_wmem_max;
+}
+
+static void *netns_core_sysctl_rmem_max_data(const struct sysctl_context *ctx)
+{
+ return &sysctl_rmem_max;
+}
+
+static void *netns_core_sysctl_wmem_default_data(const struct sysctl_context *ctx)
+{
+ return &sysctl_wmem_default;
+}
+
+static void *netns_core_sysctl_rmem_default_data(const struct sysctl_context *ctx)
+{
+ return &sysctl_rmem_default;
+}
+
+static int proc_dointvec_minmax_sndbuf(const struct ctl_table *table, int write,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct ctl_table tmp = *table;
+
+ tmp.extra1 = &min_sndbuf;
+ return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
+}
+
+static int proc_dointvec_minmax_rcvbuf(const struct ctl_table *table, int write,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ struct ctl_table tmp = *table;
+
+ tmp.extra1 = &min_rcvbuf;
+ return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
+}
+
+static const struct sysctl_field netns_core_table[] = {
+#if IS_ENABLED(CONFIG_RPS)
+ SYSCTL_FIELD_CUSTOM("rps_default_mask", 0644, 0,
+ netns_core_rps_default_mask_data,
+ rps_default_mask_sysctl),
+#endif
+ SYSCTL_FIELD_STATIC_INT_MINMAX("somaxconn", 0644,
+ netns_core_somaxconn_data,
+ SYSCTL_ZERO, NULL),
+ SYSCTL_FIELD_STATIC_INT_MINMAX("optmem_max", 0644,
+ netns_core_optmem_max_data,
+ SYSCTL_ZERO, NULL),
+ SYSCTL_FIELD_STATIC_U8_MINMAX("txrehash", 0644,
+ netns_core_txrehash_data,
+ SYSCTL_UINT_ZERO, SYSCTL_UINT_ONE),
+ SYSCTL_FIELD_CUSTOM("txq_reselection_ms", 0644, sizeof(int),
+ netns_core_txq_reselection_data, proc_dointvec_ms_jiffies),
+ SYSCTL_FIELD_STATIC_U8_MINMAX("tstamp_allow_data", 0644,
+ netns_core_tstamp_allow_data_value,
+ SYSCTL_UINT_ZERO, SYSCTL_UINT_ONE),
+ SYSCTL_FIELD_STATIC_U8_MINMAX("bypass_prot_mem", 0644,
+ netns_core_bypass_prot_mem_data,
+ SYSCTL_UINT_ZERO, SYSCTL_UINT_ONE),
+ SYSCTL_FIELD_CUSTOM_MODE("wmem_max", 0644,
+ netns_core_init_net_writable_mode, sizeof(int),
+ netns_core_sysctl_wmem_max_data,
+ proc_dointvec_minmax_sndbuf),
+ SYSCTL_FIELD_CUSTOM_MODE("rmem_max", 0644,
+ netns_core_init_net_writable_mode, sizeof(int),
+ netns_core_sysctl_rmem_max_data,
+ proc_dointvec_minmax_rcvbuf),
+ SYSCTL_FIELD_CUSTOM_MODE("wmem_default", 0644,
+ netns_core_init_net_writable_mode, sizeof(int),
+ netns_core_sysctl_wmem_default_data,
+ proc_dointvec_minmax_sndbuf),
+ SYSCTL_FIELD_CUSTOM_MODE("rmem_default", 0644,
+ netns_core_init_net_writable_mode, sizeof(int),
+ netns_core_sysctl_rmem_default_data,
+ proc_dointvec_minmax_rcvbuf),
};
static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str)
@@ -789,50 +807,23 @@ __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup);
static __net_init int sysctl_core_net_init(struct net *net)
{
- size_t table_size = ARRAY_SIZE(netns_core_table);
- struct ctl_table *tbl;
-
- tbl = netns_core_table;
- if (!net_eq(net, &init_net)) {
- int i;
- tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
- if (tbl == NULL)
- goto err_dup;
-
- for (i = 0; i < table_size; ++i) {
- if (tbl[i].data == &sysctl_wmem_max)
- break;
-
- tbl[i].data += (char *)net - (char *)&init_net;
- }
- for (; i < table_size; ++i)
- tbl[i].mode &= ~0222;
- }
-
- net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl, table_size);
+ struct sysctl_context ctx = {
+ .ns.net_ns = net,
+ };
+ net->core.sysctl_hdr = register_sysctl_fields(&net->sysctls, "net/core",
+ netns_core_table, &ctx);
if (net->core.sysctl_hdr == NULL)
- goto err_reg;
+ return -ENOMEM;
return 0;
-
-err_reg:
- if (tbl != netns_core_table)
- kfree(tbl);
-err_dup:
- return -ENOMEM;
}
static __net_exit void sysctl_core_net_exit(struct net *net)
{
- const struct ctl_table *tbl;
-
- tbl = net->core.sysctl_hdr->ctl_table_arg;
unregister_net_sysctl_table(net->core.sysctl_hdr);
- BUG_ON(tbl == netns_core_table);
#if IS_ENABLED(CONFIG_RPS)
kfree(net->core.rps_default_mask);
#endif
- kfree(tbl);
}
static __net_initdata struct pernet_operations sysctl_core_ops = {
--
2.55.0
next prev parent reply other threads:[~2026-08-26 19:43 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1787771905.git.legion@kernel.org>
2026-08-26 19:42 ` [RFC PATCH v1 01/30] proc: sysctl: address table entries by index Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 02/30] sysctl: add unsigned int limit constants Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 03/30] sysctl: add typed field descriptors Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 04/30] sysctl: use sysctl_field in ucounts Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 05/30] sysctl: ipc: use sysctl_field in mq_sysctl Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 06/30] sysctl: ipc: use sysctl_field in ipc_sysctl Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 07/30] sysctl: use sysctl_field in pid sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 08/30] sysctl: net: use sysctl_field in unix sysctl Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 09/30] sysctl: net: use sysctl_field in xfrm sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 10/30] sysctl: net: use sysctl_field for simple IPv4 per-net sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 11/30] sysctl: net: use sysctl_field in IPv4 sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 12/30] sysctl: net: use sysctl_field in IPv6 xfrm sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 13/30] sysctl: net: use sysctl_field in IPv6 fragment sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 14/30] sysctl: net: use sysctl_field in 6lowpan " Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 15/30] sysctl: net: use sysctl_field in vsock sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 16/30] sysctl: net: use sysctl_field in MPTCP sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 17/30] sysctl: net: use sysctl_field in SCTP sysctls Alexey Gladkov
2026-08-26 20:29 ` Linus Torvalds
2026-08-29 16:14 ` Alexey Gladkov
2026-08-29 16:14 ` [RFC PATCH 1/4] sysctl: add typed field descriptors Alexey Gladkov
2026-08-29 16:14 ` [RFC PATCH 2/4] sysctl: ipc: use typed fields for IPC namespace sysctls Alexey Gladkov
2026-08-29 16:14 ` [RFC PATCH 3/4] sctp: use typed fields for per-net sysctls Alexey Gladkov
2026-08-29 16:14 ` [RFC PATCH 4/4] mpls: use typed fields for per-device sysctls Alexey Gladkov
2026-08-30 15:38 ` [RFC PATCH v1 17/30] sysctl: net: use sysctl_field in SCTP sysctls Linus Torvalds
2026-08-26 19:42 ` [RFC PATCH v1 18/30] sysctl: net: use sysctl_field in core IPv6 sysctls Alexey Gladkov
2026-08-26 19:42 ` Alexey Gladkov [this message]
2026-08-26 19:42 ` [RFC PATCH v1 20/30] sysctl: net: use sysctl_field in SMC sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 21/30] sysctl: net: use sysctl_field in VRF sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 22/30] sysctl: net: use sysctl_field in RDS sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 23/30] sysctl: netfilter: use sysctl_field for per-net sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 24/30] sysctl: ipvs: " Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 25/30] sysctl: bridge: use sysctl_field for br_netfilter sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 26/30] sysctl: net: use sysctl_field for MPLS sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 27/30] sysctl: net: use sysctl_field in IPv4 devconf sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 28/30] sysctl: net: use sysctl_field in IPv6 " Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 29/30] sysctl: net: use sysctl_field in neighbour sysctls Alexey Gladkov
2026-08-26 19:42 ` [RFC PATCH v1 30/30] sysctl: parport: use sysctl_field for dynamic sysctls Alexey Gladkov
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=b73d88fa166b9b582f9c335133eb78dc2a6e5f1b.1787771905.git.legion@kernel.org \
--to=legion@kernel.org \
--cc=ebiederm@xmission.com \
--cc=joel.granados@kernel.org \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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®