mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration
@ 2026-11-26 11:47 Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type Joel Granados
                   ` (8 more replies)
  0 siblings, 9 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

This is meant to replace the sysctl array duplication mechanism used by
callers that register sysctls for individual namespaces.

Instead of duplicating a sysctl array and keeping it in memory for the
life of a namespace, use it (the array) as a template and keep only the
necessary delta in a sysctl_context struct inside ctl_table_header. The
sysctl_context can then be used to overlay the ctl_table just before
calling its proc_handler.

The last 5 commits are just examples of how the conversions at the call
sites would look. The idea is to upstream the core first and then merge
each individual calling site through its own subsystem tree.

Motivation
==========

* Reducing memory footprint for namespace registrations
  On a 64 bit ARCH, memory consumption is reduced by at least 53% (this
  is a back-of-envelope approximation): from (56 * ARRAY_SIZE *
  count(namespaces)) bytes to (26 * ARRAY_SIZE * count(namespaces))
  bytes [2]; we save more, if a subset of the context is used.

* ctl_table const qualification
  Namespace ctl_table arrays that are currently modified during
  registration can be const qualified (moved to .rodata) as they no
  longer require pre-registration changes.

What was done
=============

* struct sysctl_context:
  Embedded in struct ctl_table_header, holds flat arrays indexed by
  entry position: void **data, **extra1, **extra2; umode_t
  *mode. They point to what should be overlaid.

* overlay flags:
  struct ctl_table gets a u8 flags member in an existing hole (no size
  change) with SYSCTL_OVERLAY_{DATA,EXTRA1,EXTRA2,MODE}. They control
  what gets overlaid

* sysctl template overlay:
  Add a template registration function to initialize the sysctl
  context. Overlay data from context before calling any proc_handler.
  Template registration can only be done with a ctl_table_tmpl type.

Comment on void*
================
The sysctl_context uses 3 void** because it reflects struct ctl_table.
Yes, we should get rid of these untyped pointers. No, this is not the
series that will remove them. Yes, there are already proposals out there
that are working towards this [1]. Yes, the plan is for sysctl_context's
void pointers to change once struct ctl_table's do. I thought that
resolving the pointers had to go first, but I think these two efforts
can coexist if handled separately.

Recent approaches
=================
* Alexey Gladkov's "sysctl_field" descriptors:
  cover.1787770053.git.legion@kernel.org, cover.1790439358.git.legion@kernel.org
  Which build a temp ctl_table from typed accessors before calling the
  proc_handler

* My own header-context RFC:
  20260924-lklm-sysctl-headerctx-template-v1-0-b25e51c66ba7@kernel.org
  computed .data as base + delta in the core.

This series takes the idea of building a temp ctl_table entry before
calling proc_handler, which is a commonality in these two, and drops
everything else: no network types in sysctl core, no namespace specific
enum in sysctl core, no delta arithmetic.

To and Cc
=========
I always find it challenging to know who wants to receive these e-mails.
I added you here because you have recently commented on the issue or
are in the maintainer path. Let me know if you don't want to be included
in the future.

Any comments are greatly appreciated

Best

Based on v7.4-rc2

[1] https://lore.kernel.org/all/cover.1794844686.git.wen.yang@linux.dev
    https://lore.kernel.org/all/cover.1793204511.git.wen.yang@linux.dev
[2] sizeof(struct ctl_table) -> 56 bytes
    3*sizeof(void*) + sizeof(umode_t) -> 26 bytes

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
Joel Granados (9):
      sysctl: Add struct ctl_table_tmpl as a distinct template type
      sysctl: Add flags to struct ctl_table
      sysctl: Add overlay mechanism for flagged ctl_table members
      sysctl: Add sysctl template registration
      net: sysctl: Add sysctl registration to networking
      ipv4: Register ipv4_net_table as a template
      ipc: Register ipc_sysctls as a template
      ipc: Register mq_sysctls as a template
      ucount: Register user_table as a template

 fs/proc/proc_sysctl.c       | 362 ++++++++++++++++++++++++++++++++++++++------
 include/linux/sysctl.h      |  66 ++++++++
 include/net/net_namespace.h |  14 ++
 ipc/ipc_sysctl.c            |  24 ++-
 ipc/mq_sysctl.c             |  20 +--
 kernel/ucount.c             |  14 +-
 net/ipv4/sysctl_net_ipv4.c  | 163 +++++++++++++++++---
 net/sysctl_net.c            |  25 +++
 8 files changed, 594 insertions(+), 94 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261123-lklm-sysctl-ctx-flatarray-c4b4ea101d93

Best regards,
-- 
Joel Granados <joel.granados@kernel.org>



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 17:04   ` Linus Torvalds
  2026-11-26 11:47 ` [PATCH RFC 2/9] sysctl: Add flags to struct ctl_table Joel Granados
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Introduce a new ctl_table template type identical to ctl_table for when
the table caries several different versions/values of the same path
(think namespaces).

This is a prep commit introducing the struct, no functional change
expected. Sysctl core expects these two to be identical except for the
name; ensure this with a build assert.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 include/linux/sysctl.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index e5d7226ab6f5af34a33829883ad5d1b405e19c86..4faa3bc189e7afbc270f4ba14b04b32a3f2d1413 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -22,6 +22,7 @@
 #ifndef _LINUX_SYSCTL_H
 #define _LINUX_SYSCTL_H
 
+#include <linux/build_bug.h>
 #include <linux/list.h>
 #include <linux/rcupdate.h>
 #include <linux/wait.h>
@@ -230,6 +231,15 @@ struct ctl_table {
 	void *extra2;
 } __randomize_layout;
 
+/* Template version of struct ctl_table. Used for registering ctl_table templates */
+struct ctl_table_tmpl {
+	struct ctl_table t;
+};
+
+static_assert(sizeof(struct ctl_table_tmpl) == sizeof(struct ctl_table));
+static_assert(__alignof__(struct ctl_table_tmpl) == __alignof__(struct ctl_table));
+static_assert(offsetof(struct ctl_table_tmpl, t) == 0);
+
 struct ctl_node {
 	struct rb_node node;
 	struct ctl_table_header *header;

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 2/9] sysctl: Add flags to struct ctl_table
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 3/9] sysctl: Add overlay mechanism for flagged ctl_table members Joel Granados
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Add a u8 flags member. It takes one byte of the two-byte hole after
mode, so struct ctl_table stays the same size. Even though we are adding
it to house the overlay flags for the ctl_table_tmpl template, it can be
used as a general way to modify the registration and read/write
operation of the ctl path.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 fs/proc/proc_sysctl.c  | 1 +
 include/linux/sysctl.h | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 6c0859c6656356f39010e9d47debc0487b751a32..1c5ccb29707d83bacad06198c0dcab39c8b7b347 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -1343,6 +1343,7 @@ static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
  * data     - a pointer to data for use by proc_handler
  * maxlen   - the maximum size in bytes of the data
  * mode     - the file permissions for the /proc/sys file
+ * flags    - Used in registration and write/read ops
  * type     - Defines the target type (described in struct definition)
  * proc_handler - the text handler routine (described below)
  *
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 4faa3bc189e7afbc270f4ba14b04b32a3f2d1413..3654fd31e3cc4f9217ae61b0bb4d6f4c677ea237 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -22,6 +22,7 @@
 #ifndef _LINUX_SYSCTL_H
 #define _LINUX_SYSCTL_H
 
+#include <linux/bits.h>
 #include <linux/build_bug.h>
 #include <linux/list.h>
 #include <linux/rcupdate.h>
@@ -219,12 +220,19 @@ static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
 #define DEFINE_CTL_TABLE_POLL(name)					\
 	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
 
+/* Overlay types. Describes what is overlain when registering a template */
+#define SYSCTL_OVERLAY_DATA	BIT(0)
+#define SYSCTL_OVERLAY_EXTRA1	BIT(1)
+#define SYSCTL_OVERLAY_EXTRA2	BIT(2)
+#define SYSCTL_OVERLAY_MODE	BIT(3)
+
 /* A sysctl table is an array of struct ctl_table: */
 struct ctl_table {
 	const char *procname;		/* Text ID for /proc/sys */
 	void *data;
 	int maxlen;
 	umode_t mode;
+	u8 flags;			/* Used in registration and write/read ops */
 	proc_handler *proc_handler;	/* Callback for text formatting */
 	struct ctl_table_poll *poll;
 	void *extra1;

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 3/9] sysctl: Add overlay mechanism for flagged ctl_table members
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 2/9] sysctl: Add flags to struct ctl_table Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 4/9] sysctl: Add sysctl template registration Joel Granados
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Add the ctl_table overlay mechanism and use it whenever the overlay
flags signal the existence of an alternate value. The new
sysctl_context structure holds four arrays of overlay values each
indexed by an entry's position in its ctl_table array.

sysctl_overlay() builds the entry the core acts on. An un-flagged entry
passes through untouched. For a flagged entry it copies the entry to a
stack buffer and overlays each flagged member with data from
sysctl_context. sysctl_overlay() runs in proc_sys_call_handler(),
proc_sys_permission() and proc_sys_make_inode().

The arrays are written once at registration, before the header becomes
visible, and read under the header's use count, so they need no locking.
An error is returned when a flagged value is not found in the
sysctl_context.

This is a prep commit, there is no context registration yet, so there is
no change in functionality.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 fs/proc/proc_sysctl.c  | 97 ++++++++++++++++++++++++++++++++++++++++++++------
 include/linux/sysctl.h | 18 ++++++++++
 2 files changed, 104 insertions(+), 11 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 1c5ccb29707d83bacad06198c0dcab39c8b7b347..5b5db9fcc3c63444e79b76d94891af70ec8ecc0a 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -205,6 +205,7 @@ static void init_header(struct ctl_table_header *head,
 	head->parent = NULL;
 	head->node = node;
 	INIT_HLIST_HEAD(&head->inodes);
+	memset(&head->ctx, 0, sizeof(head->ctx));
 	if (node) {
 		const struct ctl_table *entry __maybe_unused;
 
@@ -446,13 +447,64 @@ static int sysctl_perm(struct ctl_table_header *head, const struct ctl_table *ta
 	return test_perm(mode, op);
 }
 
+/*
+ * Replace .data, .extra{1,2} or .mode when flagged. An unflagged entry is
+ * returned as it is.
+ */
+static const struct ctl_table *sysctl_overlay(struct ctl_table_header *head,
+					      const struct ctl_table *entry,
+					      struct ctl_table *buf)
+{
+	const struct sysctl_context *ctx = &head->ctx;
+	size_t idx;
+
+	if (!(entry->flags & SYSCTL_OVERLAY_MASK))
+		return entry;
+
+	/* The entry and the header arrive as a pair; anything else is a bug. */
+	idx = entry - head->ctl_table;
+	if (WARN_ON_ONCE(idx >= (size_t)head->ctl_table_size))
+		return ERR_PTR(-EINVAL);
+	*buf = *entry;
+
+	if (entry->flags & SYSCTL_OVERLAY_DATA) {
+		if (!ctx->data || !ctx->data[idx])
+			return ERR_PTR(-EINVAL);
+		buf->data = ctx->data[idx];
+	}
+	if (entry->flags & SYSCTL_OVERLAY_EXTRA1) {
+		if (!ctx->extra1 || !ctx->extra1[idx])
+			return ERR_PTR(-EINVAL);
+		buf->extra1 = ctx->extra1[idx];
+	}
+	if (entry->flags & SYSCTL_OVERLAY_EXTRA2) {
+		if (!ctx->extra2 || !ctx->extra2[idx])
+			return ERR_PTR(-EINVAL);
+		buf->extra2 = ctx->extra2[idx];
+	}
+	if (entry->flags & SYSCTL_OVERLAY_MODE) {
+		if (!ctx->mode || !ctx->mode[idx])
+			return ERR_PTR(-EINVAL);
+		buf->mode = ctx->mode[idx];
+	}
+
+	return buf;
+}
+
 static struct inode *proc_sys_make_inode(struct super_block *sb,
 		struct ctl_table_header *head, const struct ctl_table *table)
 {
 	struct ctl_table_root *root = head->root;
+	const struct ctl_table *view;
+	struct ctl_table overlaid;
 	struct inode *inode;
 	struct proc_inode *ei;
 
+	/* The mode comes from the overlaid entry; sysctl_entry keeps @table. */
+	view = sysctl_overlay(head, table, &overlaid);
+	if (IS_ERR(view))
+		return ERR_CAST(view);
+
 	inode = new_inode(sb);
 	if (!inode)
 		return ERR_PTR(-ENOMEM);
@@ -474,8 +526,8 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
 	spin_unlock(&sysctl_lock);
 
 	simple_inode_init_ts(inode);
-	inode->i_mode = table->mode;
-	if (!S_ISDIR(table->mode)) {
+	inode->i_mode = view->mode;
+	if (!S_ISDIR(view->mode)) {
 		inode->i_mode |= S_IFREG;
 		inode->i_op = &proc_sys_inode_operations;
 		inode->i_fop = &proc_sys_file_operations;
@@ -556,6 +608,7 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
 	struct inode *inode = file_inode(iocb->ki_filp);
 	struct ctl_table_header *head = grab_header(inode);
 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
+	struct ctl_table overlaid;
 	size_t count = iov_iter_count(iter);
 	char *kbuf;
 	ssize_t error;
@@ -567,6 +620,12 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
 	 * At this point we know that the sysctl was not unregistered
 	 * and won't be until we finish.
 	 */
+	table = sysctl_overlay(head, table, &overlaid);
+	if (IS_ERR(table)) {
+		error = PTR_ERR(table);
+		goto out;
+	}
+
 	error = -EPERM;
 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
 		goto out;
@@ -797,6 +856,7 @@ static int proc_sys_permission(const struct mnt_idmap *idmap,
 	 */
 	struct ctl_table_header *head;
 	const struct ctl_table *table;
+	struct ctl_table overlaid;
 	int error;
 
 	/* Executable files are not allowed under /proc/sys/ */
@@ -810,8 +870,14 @@ static int proc_sys_permission(const struct mnt_idmap *idmap,
 	table = PROC_I(inode)->sysctl_entry;
 	if (!table) /* global root - r-xr-xr-x */
 		error = mask & MAY_WRITE ? -EACCES : 0;
-	else /* Use the permissions on the sysctl table entry */
-		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
+	else {
+		/* ->permissions may inspect the entry, so overlay it first. */
+		table = sysctl_overlay(head, table, &overlaid);
+		if (IS_ERR(table))
+			error = PTR_ERR(table);
+		else
+			error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
+	}
 
 	sysctl_head_finish(head);
 	return error;
@@ -841,16 +907,23 @@ static int proc_sys_getattr(const struct mnt_idmap *idmap,
 	struct inode *inode = d_inode(path->dentry);
 	struct ctl_table_header *head = grab_header(inode);
 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
+	struct ctl_table overlaid;
+	int error = 0;
 
 	if (IS_ERR(head))
 		return PTR_ERR(head);
 
 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
-	if (table)
-		stat->mode = (stat->mode & S_IFMT) | table->mode;
+	if (table) {
+		table = sysctl_overlay(head, table, &overlaid);
+		if (IS_ERR(table))
+			error = PTR_ERR(table);
+		else
+			stat->mode = (stat->mode & S_IFMT) | table->mode;
+	}
 
 	sysctl_head_finish(head);
-	return 0;
+	return error;
 }
 
 static const struct file_operations proc_sys_file_operations = {
@@ -1118,13 +1191,13 @@ static int sysctl_check_table_array(const char *path, const struct ctl_table *ta
 		if (table->maxlen != sizeof(u8))
 			err |= sysctl_err(path, table, "array not allowed");
 
-		if (table->extra1) {
+		if (table->extra1 && !(table->flags & SYSCTL_OVERLAY_EXTRA1)) {
 			extra = *(unsigned int *) table->extra1;
 			if (extra > 255U)
 				err |= sysctl_err(path, table,
 						"range value too large for proc_dou8vec_minmax");
 		}
-		if (table->extra2) {
+		if (table->extra2 && !(table->flags & SYSCTL_OVERLAY_EXTRA2)) {
 			extra = *(unsigned int *) table->extra2;
 			if (extra > 255U)
 				err |= sysctl_err(path, table,
@@ -1159,7 +1232,8 @@ static int sysctl_check_table(const char *path, struct ctl_table_header *header)
 		    (entry->proc_handler == proc_dointvec_ms_jiffies) ||
 		    (entry->proc_handler == proc_doulongvec_minmax) ||
 		    (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
-			if (!entry->data)
+			/* Can be NULL when overlaid */
+			if (!entry->data && !(entry->flags & SYSCTL_OVERLAY_DATA))
 				err |= sysctl_err(path, entry, "No data");
 			if (!entry->maxlen)
 				err |= sysctl_err(path, entry, "No maxlen");
@@ -1169,7 +1243,8 @@ static int sysctl_check_table(const char *path, struct ctl_table_header *header)
 		if (!entry->proc_handler)
 			err |= sysctl_err(path, entry, "No proc_handler");
 
-		if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
+		if (!(entry->flags & SYSCTL_OVERLAY_MODE) &&
+		    (entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
 				entry->mode);
 	}
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 3654fd31e3cc4f9217ae61b0bb4d6f4c677ea237..11ee0cdeef0ffe8daa4cc8756a820bf2b002c9ad 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -225,6 +225,8 @@ static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
 #define SYSCTL_OVERLAY_EXTRA1	BIT(1)
 #define SYSCTL_OVERLAY_EXTRA2	BIT(2)
 #define SYSCTL_OVERLAY_MODE	BIT(3)
+#define SYSCTL_OVERLAY_MASK	(SYSCTL_OVERLAY_DATA | SYSCTL_OVERLAY_EXTRA1 | \
+				 SYSCTL_OVERLAY_EXTRA2 | SYSCTL_OVERLAY_MODE)
 
 /* A sysctl table is an array of struct ctl_table: */
 struct ctl_table {
@@ -253,6 +255,20 @@ struct ctl_node {
 	struct ctl_table_header *header;
 };
 
+/**
+ * struct sysctl_context - per-registration values for template entries
+ * @data: replaces ctl_table.data of entries flagged SYSCTL_OVERLAY_DATA
+ * @extra1: replaces ctl_table.extra1 of entries flagged SYSCTL_OVERLAY_EXTRA1
+ * @extra2: replaces ctl_table.extra2 of entries flagged SYSCTL_OVERLAY_EXTRA2
+ * @mode: replaces ctl_table.mode of entries flagged SYSCTL_OVERLAY_MODE
+ */
+struct sysctl_context {
+	void **data;
+	void **extra1;
+	void **extra2;
+	umode_t *mode;
+};
+
 /**
  * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees
  * @ctl_table: pointer to the first element in ctl_table array
@@ -262,6 +278,7 @@ struct ctl_node {
  *         something is removed from inodes
  * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered.
  * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()"
+ * @ctx: per-registration values for flagged entries, see struct sysctl_context
  *
  * @type: Enumeration to differentiate between ctl target types:
  * type.SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
@@ -286,6 +303,7 @@ struct ctl_table_header {
 	struct ctl_dir *parent;
 	struct ctl_node *node;
 	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
+	struct sysctl_context ctx;
 	enum {
 		SYSCTL_TABLE_TYPE_DEFAULT,
 		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 4/9] sysctl: Add sysctl template registration
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (2 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 3/9] sysctl: Add overlay mechanism for flagged ctl_table members Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 5/9] net: sysctl: Add sysctl registration to networking Joel Granados
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Add register_sysctl_tmpl{,_sz}() for sysctl template registrations. A
caller marks the entries that vary with the SYSCTL_OVERLAY_* flags in
the already existing const struct ctl_table. The "new" entry values are
filled and passed along in a copy of the array.

	overlay = kmemdup_array(tbl, ARRAY_SIZE(tbl), sizeof(*tbl), GFP_KERNEL);
	overlay[idx].data = VALUE;
	hdr = register_sysctl_tmpl("kernel", tbl, overlay);
	kfree(overlay);

sysctl_ctx_fill adds the overlay (flagged) entries into the header
context. Check that every flagged overlay carries a value; return an
error if this is not the case.

The new internal function register_header() contains the common logic
between the two ways of doing the sysctl registration (original and
templated)

This is a prep commit, no caller uses this yet. No functional change
intended.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 fs/proc/proc_sysctl.c  | 264 ++++++++++++++++++++++++++++++++++++++++++-------
 include/linux/sysctl.h |  30 ++++++
 2 files changed, 256 insertions(+), 38 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 5b5db9fcc3c63444e79b76d94891af70ec8ecc0a..aabcb9ef8e8e7832734360cec80796c2ee7547d4 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -1176,6 +1176,53 @@ static int sysctl_err(const char *path, const struct ctl_table *table, char *fmt
 	return -EINVAL;
 }
 
+/* Move the flagged members into the header sysctl context. */
+static int sysctl_ctx_fill(const char *path, struct ctl_table_header *head,
+			   const struct ctl_table *overlay)
+{
+	struct sysctl_context *ctx = &head->ctx;
+	const struct ctl_table *tmpl;
+
+	list_for_each_table_entry(tmpl, head) {
+		size_t idx = tmpl - head->ctl_table;
+		const struct ctl_table *ovl = &overlay[idx];
+		u8 flags = tmpl->flags;
+
+		/* An unflagged member must match the template. */
+		if (ovl->procname != tmpl->procname ||
+		    ovl->maxlen != tmpl->maxlen ||
+		    ovl->proc_handler != tmpl->proc_handler ||
+		    ovl->poll != tmpl->poll ||
+		    ovl->flags != flags ||
+		    (!(flags & SYSCTL_OVERLAY_DATA) && ovl->data != tmpl->data) ||
+		    (!(flags & SYSCTL_OVERLAY_EXTRA1) && ovl->extra1 != tmpl->extra1) ||
+		    (!(flags & SYSCTL_OVERLAY_EXTRA2) && ovl->extra2 != tmpl->extra2) ||
+		    (!(flags & SYSCTL_OVERLAY_MODE) && ovl->mode != tmpl->mode))
+			goto err;
+
+		/* A flagged one must be set: an empty slot means "not overlaid". */
+		if (((flags & SYSCTL_OVERLAY_DATA) && !ovl->data) ||
+		    ((flags & SYSCTL_OVERLAY_EXTRA1) && !ovl->extra1) ||
+		    ((flags & SYSCTL_OVERLAY_EXTRA2) && !ovl->extra2) ||
+		    ((flags & SYSCTL_OVERLAY_MODE) && !ovl->mode))
+			goto err;
+
+		if (flags & SYSCTL_OVERLAY_DATA)
+			ctx->data[idx] = ovl->data;
+		if (flags & SYSCTL_OVERLAY_EXTRA1)
+			ctx->extra1[idx] = ovl->extra1;
+		if (flags & SYSCTL_OVERLAY_EXTRA2)
+			ctx->extra2[idx] = ovl->extra2;
+		if (flags & SYSCTL_OVERLAY_MODE)
+			ctx->mode[idx] = ovl->mode;
+	}
+
+	return 0;
+
+err:
+	return sysctl_err(path, tmpl, "error while filling the sysctl context");
+}
+
 static int sysctl_check_table_array(const char *path, const struct ctl_table *table)
 {
 	unsigned int extra;
@@ -1213,6 +1260,30 @@ static int sysctl_check_table_array(const char *path, const struct ctl_table *ta
 	return err;
 }
 
+/* A flagged member must be different between the underlying entry and the overlay */
+static int sysctl_check_overlay(const char *path,
+				struct ctl_table_header *head,
+				const struct ctl_table *entry)
+{
+	const struct sysctl_context *ctx = &head->ctx;
+	size_t idx = entry - head->ctl_table;
+	u8 flags = entry->flags;
+	int err = 0;
+
+	if (flags & ~SYSCTL_OVERLAY_MASK)
+		err |= sysctl_err(path, entry, "bogus .flags 0x%x", flags);
+	if ((flags & SYSCTL_OVERLAY_DATA) && (!ctx->data || !ctx->data[idx]))
+		err |= sysctl_err(path, entry, "no data to overlay");
+	if ((flags & SYSCTL_OVERLAY_EXTRA1) && (!ctx->extra1 || !ctx->extra1[idx]))
+		err |= sysctl_err(path, entry, "no extra1 to overlay");
+	if ((flags & SYSCTL_OVERLAY_EXTRA2) && (!ctx->extra2 || !ctx->extra2[idx]))
+		err |= sysctl_err(path, entry, "no extra2 to overlay");
+	if ((flags & SYSCTL_OVERLAY_MODE) && (!ctx->mode || !ctx->mode[idx]))
+		err |= sysctl_err(path, entry, "no mode to overlay");
+
+	return err;
+}
+
 static int sysctl_check_table(const char *path, struct ctl_table_header *header)
 {
 	const struct ctl_table *entry;
@@ -1247,6 +1318,9 @@ static int sysctl_check_table(const char *path, struct ctl_table_header *header)
 		    (entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
 				entry->mode);
+
+		if (entry->flags)
+			err |= sysctl_check_overlay(path, header, entry);
 	}
 	return err;
 }
@@ -1398,6 +1472,110 @@ static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
 	return dir;
 }
 
+/* OR of every entry's flags, so the context gets an array per kind in use. */
+static u8 sysctl_table_flags(const struct ctl_table *table, size_t table_size)
+{
+	u8 flags = 0;
+	size_t i;
+
+	for (i = 0; i < table_size; i++)
+		flags |= table[i].flags;
+
+	return flags & SYSCTL_OVERLAY_MASK;
+}
+
+static size_t sysctl_header_size(const struct ctl_table *table, size_t table_size)
+{
+	u8 flags = sysctl_table_flags(table, table_size);
+	size_t size = sizeof(struct ctl_table_header) +
+		      sizeof(struct ctl_node) * table_size;
+
+	if (flags & SYSCTL_OVERLAY_DATA)
+		size += table_size * sizeof(void *);
+	if (flags & SYSCTL_OVERLAY_EXTRA1)
+		size += table_size * sizeof(void *);
+	if (flags & SYSCTL_OVERLAY_EXTRA2)
+		size += table_size * sizeof(void *);
+	if (flags & SYSCTL_OVERLAY_MODE)
+		size += table_size * sizeof(umode_t);
+
+	return size;
+}
+
+/* Init only the pointers that will be used */
+static void sysctl_ctx_init(struct ctl_table_header *head)
+{
+	size_t table_size = head->ctl_table_size;
+	u8 flags = sysctl_table_flags(head->ctl_table, table_size);
+	void **slots = (void **)(head->node + table_size);
+
+	if (flags & SYSCTL_OVERLAY_DATA) {
+		head->ctx.data = slots;
+		slots += table_size;
+	}
+	if (flags & SYSCTL_OVERLAY_EXTRA1) {
+		head->ctx.extra1 = slots;
+		slots += table_size;
+	}
+	if (flags & SYSCTL_OVERLAY_EXTRA2) {
+		head->ctx.extra2 = slots;
+		slots += table_size;
+	}
+	if (flags & SYSCTL_OVERLAY_MODE)
+		head->ctx.mode = (umode_t *)slots;
+}
+
+static struct ctl_table_header *register_header(struct ctl_table_set *set,
+	const char *path, const struct ctl_table *table,
+	const struct ctl_table *overlay, size_t table_size)
+{
+	struct ctl_table_root *root = set->dir.header.root;
+	struct ctl_table_header *header;
+	struct ctl_dir *dir;
+	struct ctl_node *node;
+	size_t size;
+
+	size = sysctl_header_size(table, table_size);
+	header = kzalloc(size, GFP_KERNEL_ACCOUNT);
+	if (!header)
+		return NULL;
+
+	node = (struct ctl_node *)(header + 1);
+	init_header(header, root, set, node, table, table_size);
+	if (overlay) {
+		sysctl_ctx_init(header);
+		if (sysctl_ctx_fill(path, header, overlay))
+			goto fail;
+	}
+	if (sysctl_check_table(path, header))
+		goto fail;
+
+	spin_lock(&sysctl_lock);
+	dir = &set->dir;
+	/* Reference moved down the directory tree get_subdir */
+	dir->header.nreg++;
+	spin_unlock(&sysctl_lock);
+
+	dir = sysctl_mkdir_p(dir, path);
+	if (IS_ERR(dir))
+		goto fail;
+	spin_lock(&sysctl_lock);
+	if (insert_header(dir, header))
+		goto fail_put_dir_locked;
+
+	drop_sysctl_table(&dir->header);
+	spin_unlock(&sysctl_lock);
+
+	return header;
+
+fail_put_dir_locked:
+	drop_sysctl_table(&dir->header);
+	spin_unlock(&sysctl_lock);
+fail:
+	kfree(header);
+	return NULL;
+}
+
 /**
  * __register_sysctl_table - register a leaf sysctl table
  * @set: Sysctl tree to register on
@@ -1446,45 +1624,31 @@ 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_root *root = set->dir.header.root;
-	struct ctl_table_header *header;
-	struct ctl_dir *dir;
-	struct ctl_node *node;
+	return register_header(set, path, table, NULL, table_size);
+}
 
-	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);
-	if (sysctl_check_table(path, header))
-		goto fail;
-
-	spin_lock(&sysctl_lock);
-	dir = &set->dir;
-	/* Reference moved down the directory tree get_subdir */
-	dir->header.nreg++;
-	spin_unlock(&sysctl_lock);
-
-	dir = sysctl_mkdir_p(dir, path);
-	if (IS_ERR(dir))
-		goto fail;
-	spin_lock(&sysctl_lock);
-	if (insert_header(dir, header))
-		goto fail_put_dir_locked;
-
-	drop_sysctl_table(&dir->header);
-	spin_unlock(&sysctl_lock);
-
-	return header;
-
-fail_put_dir_locked:
-	drop_sysctl_table(&dir->header);
-	spin_unlock(&sysctl_lock);
-fail:
-	kfree(header);
-	return NULL;
+/**
+ * __register_sysctl_table_tmpl - register a table from a template/overlay
+ * @set: Sysctl tree to register on
+ * @path: The path to the directory the sysctl table is in.
+ * @tmpl: the template, passed through CTL_TABLE_TMPL. Must be available
+ *	until unregistered. Should be const global.
+ * @overlay: caller's copy of @tmpl in which every flagged member has its
+ *	own value. The callers should free it after the registration call
+ * @table_size: The number of elements in @tmpl and @overlay.
+ *
+ * The flagged members of @overlay are stored in the header's context and
+ * overlaid on the template entry each time it is accessed.
+ *
+ * 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_tmpl(
+	struct ctl_table_set *set, const char *path,
+	const struct ctl_table_tmpl *tmpl, const struct ctl_table *overlay,
+	size_t table_size)
+{
+	return register_header(set, path, &tmpl->t, overlay, table_size);
 }
 
 /**
@@ -1513,6 +1677,30 @@ struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_t
 }
 EXPORT_SYMBOL(register_sysctl_sz);
 
+/**
+ * register_sysctl_tmpl_sz - register a sysctl table from a template
+ * @path: The path to the directory the sysctl table is in. If the path
+ *	doesn't exist we will create it for you.
+ * @tmpl: the template, see __register_sysctl_table_tmpl()
+ * @overlay: the filled-in copy of @tmpl, see __register_sysctl_table_tmpl()
+ * @table_size: The number of elements in @tmpl and @overlay.
+ *
+ * Register a sysctl table whose entries flagged SYSCTL_OVERLAY_* take their
+ * values from @overlay while @tmpl stays shared and unmodified. The
+ * register_sysctl_tmpl() macro supplies the cast and the size for an array.
+ *
+ * See __register_sysctl_table_tmpl() for more details.
+ */
+struct ctl_table_header *register_sysctl_tmpl_sz(const char *path,
+						 const struct ctl_table_tmpl *tmpl,
+						 const struct ctl_table *overlay,
+						 size_t table_size)
+{
+	return __register_sysctl_table_tmpl(&sysctl_table_root.default_set,
+					    path, tmpl, overlay, table_size);
+}
+EXPORT_SYMBOL(register_sysctl_tmpl_sz);
+
 /**
  * __register_sysctl_init() - register sysctl table to path
  * @path: path name for sysctl base. If that path doesn't exist we will create
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 11ee0cdeef0ffe8daa4cc8756a820bf2b002c9ad..1eefcbb9f02a612118bf3810f137e713dc9389cd 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -22,6 +22,7 @@
 #ifndef _LINUX_SYSCTL_H
 #define _LINUX_SYSCTL_H
 
+#include <linux/array_size.h>
 #include <linux/bits.h>
 #include <linux/build_bug.h>
 #include <linux/list.h>
@@ -250,6 +251,16 @@ static_assert(sizeof(struct ctl_table_tmpl) == sizeof(struct ctl_table));
 static_assert(__alignof__(struct ctl_table_tmpl) == __alignof__(struct ctl_table));
 static_assert(offsetof(struct ctl_table_tmpl, t) == 0);
 
+/**
+ * CTL_TABLE_TMPL - cast a struct ctl_table * as a ctl template
+ * @arr: array of struct ctl_table
+ *
+ * This macro is the only way to get a ctl_table_tmpl to register as a template
+ */
+#define CTL_TABLE_TMPL(arr)						\
+	((const struct ctl_table_tmpl *)(arr) + __must_be_array(arr) +	\
+	 BUILD_BUG_ON_ZERO(!__same_type((arr)[0], struct ctl_table)))
+
 struct ctl_node {
 	struct rb_node node;
 	struct ctl_table_header *header;
@@ -331,6 +342,9 @@ struct ctl_table_root {
 
 #define register_sysctl(path, table)	\
 	register_sysctl_sz(path, table, ARRAY_SIZE(table))
+#define register_sysctl_tmpl(path, tmpl, overlay)			\
+	register_sysctl_tmpl_sz(path, CTL_TABLE_TMPL(tmpl), overlay,		\
+				ARRAY_SIZE(tmpl))
 
 #ifdef CONFIG_SYSCTL
 
@@ -346,6 +360,14 @@ struct ctl_table_header *__register_sysctl_table(
 	const char *path, const struct ctl_table *table, size_t table_size);
 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_table_tmpl(
+	struct ctl_table_set *set, const char *path,
+	const struct ctl_table_tmpl *tmpl, const struct ctl_table *overlay,
+	size_t table_size);
+struct ctl_table_header *register_sysctl_tmpl_sz(const char *path,
+						 const struct ctl_table_tmpl *tmpl,
+						 const struct ctl_table *overlay,
+						 size_t table_size);
 void unregister_sysctl_table(struct ctl_table_header * table);
 
 extern int sysctl_init_bases(void);
@@ -379,6 +401,14 @@ static inline struct ctl_table_header *register_sysctl_sz(const char *path,
 	return NULL;
 }
 
+static inline struct ctl_table_header *register_sysctl_tmpl_sz(const char *path,
+							       const struct ctl_table_tmpl *tmpl,
+							       const struct ctl_table *overlay,
+							       size_t table_size)
+{
+	return NULL;
+}
+
 static inline void unregister_sysctl_table(struct ctl_table_header * table)
 {
 }

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 5/9] net: sysctl: Add sysctl registration to networking
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (3 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 4/9] sysctl: Add sysctl template registration Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 6/9] ipv4: Register ipv4_net_table as a template Joel Granados
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Add the per-netns entry point for template registration. It takes the
shared template and the caller's overlay and registers in the
namespace's set through __register_sysctl_table_tmpl(). Run
ensure_safe_net_sysctl() on the overlay for namespaces other than
init_net.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 include/net/net_namespace.h | 14 ++++++++++++++
 net/sysctl_net.c            | 25 +++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index c29a22ca178fa678c0468247b9f85f53eec86184..ecec120de3081d7e8f5c13b72ca98dc647704139 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -527,11 +527,19 @@ struct ctl_table;
 
 #define register_net_sysctl(net, path, table)	\
 	register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
+#define register_net_sysctl_tmpl(net, path, tmpl, overlay)		\
+	register_net_sysctl_tmpl_sz(net, path, CTL_TABLE_TMPL(tmpl),	\
+				    overlay, ARRAY_SIZE(tmpl))
 #ifdef CONFIG_SYSCTL
 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_tmpl_sz(struct net *net,
+						     const char *path,
+						     const struct ctl_table_tmpl *tmpl,
+						     const struct ctl_table *overlay,
+						     size_t table_size);
 void unregister_net_sysctl_table(struct ctl_table_header *header);
 #else
 static inline int net_sysctl_init(void) { return 0; }
@@ -540,6 +548,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_tmpl_sz(
+	struct net *net, const char *path, const struct ctl_table_tmpl *tmpl,
+	const struct ctl_table *overlay, size_t table_size)
+{
+	return NULL;
+}
 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
 {
 }
diff --git a/net/sysctl_net.c b/net/sysctl_net.c
index e190a639eef2f6929c4aed85ad625aaf8a6ebd6a..c6eae05dedaecc3bdce402741140d8690d3c15ea 100644
--- a/net/sysctl_net.c
+++ b/net/sysctl_net.c
@@ -173,6 +173,31 @@ struct ctl_table_header *register_net_sysctl_sz(struct net *net,
 }
 EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
 
+/**
+ * register_net_sysctl_tmpl_sz - register a shared template for one netns
+ * @net: the network namespace
+ * @path: path of the directory under /proc/sys
+ * @tmpl: the template every namespace shares, see register_sysctl_tmpl_sz()
+ * @overlay: copy of @tmpl holding values for @net; this ptr is not kept!
+ * @table_size: number of entries in @tmpl and @overlay
+ *
+ * The safety check runs on @overlay, since that is what @net will see
+ */
+struct ctl_table_header *register_net_sysctl_tmpl_sz(struct net *net,
+						     const char *path,
+						     const struct ctl_table_tmpl *tmpl,
+						     const struct ctl_table *overlay,
+						     size_t table_size)
+{
+	if (!net_eq(net, &init_net))
+		if (ensure_safe_net_sysctl(net, path, overlay, table_size))
+			return NULL;
+
+	return __register_sysctl_table_tmpl(&net->sysctls, path, tmpl, overlay,
+					    table_size);
+}
+EXPORT_SYMBOL_GPL(register_net_sysctl_tmpl_sz);
+
 void unregister_net_sysctl_table(struct ctl_table_header *header)
 {
 	unregister_sysctl_table(header);

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 6/9] ipv4: Register ipv4_net_table as a template
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (4 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 5/9] net: sysctl: Add sysctl registration to networking Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 7/9] ipc: Register ipc_sysctls " Joel Granados
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Flag the members of ipv4_net_table that differ per namespace and
register it through register_net_sysctl_tmpl():

  * SYSCTL_OVERLAY_DATA on the 127 entries with a .data pointer,
  * SYSCTL_OVERLAY_EXTRA2 on tcp_reordering, whose bound is the
    namespace's tcp_max_reordering,
  * SYSCTL_OVERLAY_MODE on tcp_allowed_congestion_control,
  * dataless entry that becomes read-only outside init_net

Rename ipv4_net_table_dup() to ipv4_net_table_overlay(), build the
overlay for init_net as well, free it once the call returns and stop
freeing ctl_table_arg in ipv4_sysctl_exit_net().

A namespace then keeps the context arrays in its header instead of a
copy of the whole table. init_net needs an overlay too because the core
refuses a flagged entry that has no context value.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 net/ipv4/sysctl_net_ipv4.c | 163 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 143 insertions(+), 20 deletions(-)

diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
index bf193e7ec51684a78387d30c959b5ad235c6865d..876bbec562e539520f0642e9567cd498a3678f8d 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -633,6 +633,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.tcp_death_row.sysctl_max_tw_buckets,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -640,6 +641,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_echo_ignore_all,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -649,6 +651,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_echo_enable_probe,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -658,6 +661,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_echo_ignore_broadcasts,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -667,6 +671,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -676,6 +681,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_errors_use_inbound_ifaddr,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -685,6 +691,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_errors_extension_mask,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &icmp_errors_extension_mask_all,
@@ -694,6 +701,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_ratelimit,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_ms_jiffies,
 	},
 	{
@@ -701,6 +709,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_ratemask,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -708,6 +717,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_msgs_per_sec,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 	},
@@ -716,6 +726,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_icmp_msgs_burst,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 	},
@@ -724,6 +735,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.ping_group_range.range,
 		.maxlen		= sizeof(gid_t)*2,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= ipv4_ping_group_range,
 	},
 #ifdef CONFIG_NET_L3_MASTER_DEV
@@ -732,6 +744,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_raw_l3mdev_accept,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -742,6 +755,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_ecn,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &tcp_ecn_mode_max,
@@ -751,6 +765,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_ecn_option,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_THREE,
@@ -760,6 +775,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_ecn_option_beacon,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_THREE,
@@ -769,6 +785,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_ecn_fallback,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -778,6 +795,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_dynaddr,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -785,6 +803,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_early_demux,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -792,6 +811,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_udp_early_demux,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 	},
 	{
@@ -799,6 +819,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_early_demux,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 	},
 	{
@@ -806,6 +827,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_nexthop_compat_mode,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -815,6 +837,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_default_ttl,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= &ip_ttl_min,
 		.extra2		= &ip_ttl_max,
@@ -824,6 +847,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.maxlen		= 0,
 		.data		= &init_net,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= ipv4_local_port_range,
 	},
 	{
@@ -831,6 +855,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.maxlen		= sizeof(u32),
 		.data		= &init_net.ipv4.sysctl_ip_local_port_step_width,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec,
 	},
 	{
@@ -838,6 +863,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_local_reserved_ports,
 		.maxlen		= 65536,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_do_large_bitmap,
 	},
 	{
@@ -845,6 +871,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_no_pmtu_disc,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -852,6 +879,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_fwd_use_pmtu,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -859,6 +887,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_fwd_update_priority,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler   = ipv4_fwd_update_priority,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -868,6 +897,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_nonlocal_bind,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -875,6 +905,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_ip_autobind_reuse,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1         = SYSCTL_ZERO,
 		.extra2         = SYSCTL_ONE,
@@ -884,6 +915,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_fwmark_reflect,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -891,6 +923,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_fwmark_accept,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 #ifdef CONFIG_NET_L3_MASTER_DEV
@@ -899,6 +932,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_l3mdev_accept,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -909,6 +943,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_mtu_probing,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -916,6 +951,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_base_mss,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec,
 	},
 	{
@@ -923,6 +959,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_min_snd_mss,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &tcp_min_snd_mss_min,
 		.extra2		= &tcp_min_snd_mss_max,
@@ -932,6 +969,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_mtu_probe_floor,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &tcp_min_snd_mss_min,
 		.extra2		= &tcp_min_snd_mss_max,
@@ -941,6 +979,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_probe_threshold,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec,
 	},
 	{
@@ -948,6 +987,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_probe_interval,
 		.maxlen		= sizeof(u32),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec_minmax,
 		.extra2		= &u32_max_div_HZ,
 	},
@@ -956,6 +996,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_igmp_llm_reports,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -963,6 +1004,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_igmp_max_memberships,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -970,6 +1012,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_igmp_max_msf,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 #ifdef CONFIG_IP_MULTICAST
@@ -978,6 +1021,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_igmp_qrv,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE
 	},
@@ -986,6 +1030,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.procname	= "tcp_congestion_control",
 		.data		= &init_net.ipv4.tcp_congestion_control,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.maxlen		= TCP_CA_NAME_MAX,
 		.proc_handler	= proc_tcp_congestion_control,
 	},
@@ -999,6 +1044,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.procname	= "tcp_allowed_congestion_control",
 		.maxlen		= TCP_CA_BUF_MAX,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_MODE,
 		.proc_handler   = proc_allowed_congestion_control,
 	},
 	{
@@ -1006,6 +1052,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_keepalive_time,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_jiffies,
 	},
 	{
@@ -1013,6 +1060,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_keepalive_probes,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1020,6 +1068,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_keepalive_intvl,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_jiffies,
 	},
 	{
@@ -1027,6 +1076,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_syn_retries,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= &tcp_syn_retries_min,
 		.extra2		= &tcp_syn_retries_max
@@ -1036,6 +1086,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_synack_retries,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 #ifdef CONFIG_SYN_COOKIES
@@ -1044,6 +1095,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_syncookies,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 #endif
@@ -1052,6 +1104,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_migrate_req,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE
@@ -1061,6 +1114,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_reordering,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA | SYSCTL_OVERLAY_EXTRA2,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE,
 		.extra2		= &init_net.ipv4.sysctl_tcp_max_reordering,
@@ -1070,6 +1124,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_retries1,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra2		= &tcp_retr1_max
 	},
@@ -1078,6 +1133,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_retries2,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1085,6 +1141,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_orphan_retries,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1092,6 +1149,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_fin_timeout,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_jiffies,
 	},
 	{
@@ -1099,6 +1157,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_notsent_lowat,
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec,
 	},
 	{
@@ -1106,6 +1165,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_tw_reuse,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_TWO,
@@ -1115,6 +1175,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_tw_reuse_delay,
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec_minmax,
 		.extra1		= SYSCTL_ONE,
 		.extra2		= &tcp_tw_reuse_delay_max,
@@ -1124,6 +1185,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_max_syn_backlog,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -1131,11 +1193,13 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_fastopen,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec,
 	},
 	{
 		.procname	= "tcp_fastopen_key",
 		.mode		= 0600,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.data		= &init_net.ipv4.sysctl_tcp_fastopen,
 		/* maxlen to print the list of keys in hex (*2), with dashes
 		 * separating doublewords and a comma in between keys.
@@ -1150,6 +1214,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_fastopen_blackhole_timeout,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_tfo_blackhole_detect_timeout,
 		.extra1		= SYSCTL_ZERO,
 	},
@@ -1159,6 +1224,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_fib_multipath_use_neigh,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1168,6 +1234,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_fib_multipath_hash_policy,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_fib_multipath_hash_policy,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_THREE,
@@ -1177,6 +1244,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_fib_multipath_hash_fields,
 		.maxlen		= sizeof(u32),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_fib_multipath_hash_fields,
 		.extra1		= SYSCTL_ONE,
 		.extra2		= &fib_multipath_hash_fields_all_mask,
@@ -1186,6 +1254,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net,
 		.maxlen		= sizeof(u32),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_fib_multipath_hash_seed,
 	},
 #endif
@@ -1194,6 +1263,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.maxlen		= sizeof(int),
 		.data		= &init_net.ipv4.sysctl_ip_prot_sock,
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= ipv4_privileged_ports,
 	},
 #ifdef CONFIG_NET_L3_MASTER_DEV
@@ -1202,6 +1272,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_udp_l3mdev_accept,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1212,6 +1283,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_sack,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1219,6 +1291,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_window_scaling,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1226,6 +1299,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_timestamps,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1233,6 +1307,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_early_retrans,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_FOUR,
@@ -1242,6 +1317,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_recovery,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1249,6 +1325,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_thin_linear_timeouts,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 	},
 	{
@@ -1256,6 +1333,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_slow_start_after_idle,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1263,6 +1341,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_retrans_collapse,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1270,6 +1349,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_stdurg,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1277,6 +1357,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_rfc1337,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1284,6 +1365,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_abort_on_overflow,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1291,6 +1373,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_fack,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1298,6 +1381,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_max_reordering,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE,
 	},
@@ -1306,6 +1390,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_dsack,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1313,6 +1398,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_app_win,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &tcp_app_win_max,
@@ -1322,6 +1408,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_adv_win_scale,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &tcp_adv_win_scale_min,
 		.extra2		= &tcp_adv_win_scale_max,
@@ -1331,6 +1418,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_frto,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1338,6 +1426,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_nometrics_save,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1345,6 +1434,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_no_ssthresh_metrics_save,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1354,6 +1444,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_moderate_rcvbuf,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1361,6 +1452,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_rcvbuf_low_rtt,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -1370,6 +1462,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_tso_win_divisor,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1377,6 +1470,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_workaround_signed_windows,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 	},
 	{
@@ -1384,6 +1478,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_limit_output_bytes,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -1391,6 +1486,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_challenge_ack_limit,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec
 	},
 	{
@@ -1398,6 +1494,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_min_tso_segs,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ONE,
 	},
@@ -1406,6 +1503,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_tso_rtt_log,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra2		= &tcp_tso_rtt_log_max,
 	},
@@ -1414,6 +1512,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_min_rtt_wlen,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &one_day_secs
@@ -1423,6 +1522,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_autocorking,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1432,6 +1532,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_invalid_ratelimit,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_ms_jiffies,
 	},
 	{
@@ -1439,6 +1540,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_pacing_ss_ratio,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE_THOUSAND,
@@ -1448,6 +1550,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_pacing_ca_ratio,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE_THOUSAND,
@@ -1457,6 +1560,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_wmem,
 		.maxlen		= sizeof(init_net.ipv4.sysctl_tcp_wmem),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE,
 	},
@@ -1465,6 +1569,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_rmem,
 		.maxlen		= sizeof(init_net.ipv4.sysctl_tcp_rmem),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &tcp_min_rcvbuf,
 	},
@@ -1473,6 +1578,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_comp_sack_delay_ns,
 		.maxlen		= sizeof(unsigned long),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_doulongvec_minmax,
 	},
 	{
@@ -1480,6 +1586,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_comp_sack_rtt_percent,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE,
 		.extra2		= SYSCTL_ONE_THOUSAND,
@@ -1489,6 +1596,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_comp_sack_slack_ns,
 		.maxlen		= sizeof(unsigned long),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_doulongvec_minmax,
 	},
 	{
@@ -1496,6 +1604,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_comp_sack_nr,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 	},
@@ -1504,6 +1613,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_backlog_ack_defer,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1513,6 +1623,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_reflect_tos,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra1         = SYSCTL_ZERO,
 		.extra2         = SYSCTL_ONE,
@@ -1521,6 +1632,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.procname	= "tcp_ehash_entries",
 		.data		= &init_net.ipv4.sysctl_tcp_child_ehash_entries,
 		.mode		= 0444,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_tcp_ehash_entries,
 	},
 	{
@@ -1528,6 +1640,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_child_ehash_entries,
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &tcp_child_ehash_entries_max,
@@ -1536,6 +1649,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.procname	= "udp_hash_entries",
 		.data		= &init_net.ipv4.sysctl_udp_child_hash_entries,
 		.mode		= 0444,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_udp_hash_entries,
 	},
 	{
@@ -1543,6 +1657,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_udp_child_hash_entries,
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_douintvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &udp_child_hash_entries_max,
@@ -1552,6 +1667,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_udp_rmem_min,
 		.maxlen		= sizeof(init_net.ipv4.sysctl_udp_rmem_min),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE
 	},
@@ -1560,6 +1676,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_udp_wmem_min,
 		.maxlen		= sizeof(init_net.ipv4.sysctl_udp_wmem_min),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE
 	},
@@ -1568,6 +1685,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_fib_notify_on_flag_change,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_TWO,
@@ -1577,6 +1695,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_plb_enabled,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra1         = SYSCTL_ZERO,
 		.extra2         = SYSCTL_ONE,
@@ -1586,6 +1705,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_plb_idle_rehash_rounds,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra2		= &tcp_plb_max_rounds,
 	},
@@ -1594,6 +1714,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_plb_rehash_rounds,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra2         = &tcp_plb_max_rounds,
 	},
@@ -1602,6 +1723,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_plb_suspend_rto_sec,
 		.maxlen         = sizeof(u8),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dou8vec_minmax,
 	},
 	{
@@ -1609,6 +1731,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data           = &init_net.ipv4.sysctl_tcp_plb_cong_thresh,
 		.maxlen         = sizeof(int),
 		.mode           = 0644,
+		.flags          = SYSCTL_OVERLAY_DATA,
 		.proc_handler   = proc_dointvec_minmax,
 		.extra1         = SYSCTL_ZERO,
 		.extra2         = &tcp_plb_max_cong_thresh,
@@ -1618,6 +1741,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_syn_linear_timeouts,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &tcp_syn_linear_timeouts_max,
@@ -1627,6 +1751,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_shrink_window,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -1636,6 +1761,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_pingpong_thresh,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dou8vec_minmax,
 		.extra1		= SYSCTL_ONE,
 	},
@@ -1644,6 +1770,7 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_rto_min_us,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE,
 	},
@@ -1652,13 +1779,19 @@ static const struct ctl_table ipv4_net_table[] = {
 		.data		= &init_net.ipv4.sysctl_tcp_rto_max_ms,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ONE_THOUSAND,
 		.extra2		= &tcp_rto_max_max,
 	},
 };
 
-static const struct ctl_table *ipv4_net_table_dup(struct net *net)
+/*
+ * Fill in, for @net, the members of ipv4_net_table that are flagged
+ * SYSCTL_OVERLAY_*. The copy only lives for the registration call; the
+ * table itself is shared by every namespace.
+ */
+static struct ctl_table *ipv4_net_table_overlay(struct net *net)
 {
 	size_t table_size = ARRAY_SIZE(ipv4_net_table);
 	struct ctl_table *table;
@@ -1674,7 +1807,7 @@ static const struct ctl_table *ipv4_net_table_dup(struct net *net)
 			 * the current struct net
 			 */
 			table[i].data += (void *)net - (void *)&init_net;
-		} else {
+		} else if (!net_eq(net, &init_net)) {
 			/* Entries without data pointer are global;
 			 * Make them read-only in non-init_net ns
 			 */
@@ -1689,20 +1822,17 @@ static const struct ctl_table *ipv4_net_table_dup(struct net *net)
 
 static __net_init int ipv4_sysctl_init_net(struct net *net)
 {
-	size_t table_size = ARRAY_SIZE(ipv4_net_table);
-	const struct ctl_table *table;
+	struct ctl_table *table;
 
-	table = ipv4_net_table;
-	if (!net_eq(net, &init_net)) {
-		table = ipv4_net_table_dup(net);
-		if (!table)
-			goto err_alloc;
-	}
+	table = ipv4_net_table_overlay(net);
+	if (!table)
+		goto err_alloc;
 
-	net->ipv4.ipv4_hdr = register_net_sysctl_sz(net, "net/ipv4", table,
-						    table_size);
+	net->ipv4.ipv4_hdr = register_net_sysctl_tmpl(net, "net/ipv4",
+						      ipv4_net_table, table);
+	kfree(table);
 	if (!net->ipv4.ipv4_hdr)
-		goto err_reg;
+		goto err_alloc;
 
 	net->ipv4.sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
 	if (!net->ipv4.sysctl_local_reserved_ports)
@@ -1714,20 +1844,13 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
 
 err_ports:
 	unregister_net_sysctl_table(net->ipv4.ipv4_hdr);
-err_reg:
-	if (!net_eq(net, &init_net))
-		kfree(table);
 err_alloc:
 	return -ENOMEM;
 }
 
 static __net_exit void ipv4_sysctl_exit_net(struct net *net)
 {
-	const struct ctl_table *table;
-
-	table = net->ipv4.ipv4_hdr->ctl_table_arg;
 	unregister_net_sysctl_table(net->ipv4.ipv4_hdr);
-	kfree(table);
 	kfree(net->ipv4.sysctl_local_reserved_ports);
 }
 

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 7/9] ipc: Register ipc_sysctls as a template
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (5 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 6/9] ipv4: Register ipv4_net_table as a template Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 8/9] ipc: Register mq_sysctls " Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 9/9] ucount: Register user_table " Joel Granados
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Flag SYSCTL_OVERLAY_DATA on every entry of ipc_sysctls that has a .data
pointer and register the table through __register_sysctl_table_tmpl(),
passing the rebased copy that setup_ipc_sysctls() already builds as the
overlay. Free the copy once the call returns and stop freeing
ctl_table_arg in retire_ipc_sysctls(). auto_msgmni has no .data and
stays unflagged.

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 ipc/ipc_sysctl.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c
index d038d944257f60b3a35b72492c73c41774a73da4..42a249cb58747e75fa76aa94570e5ad23cf8b003 100644
--- a/ipc/ipc_sysctl.c
+++ b/ipc/ipc_sysctl.c
@@ -79,6 +79,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.shm_ctlmax,
 		.maxlen		= sizeof(init_ipc_ns.shm_ctlmax),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_doulongvec_minmax,
 	},
 	{
@@ -86,6 +87,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.shm_ctlall,
 		.maxlen		= sizeof(init_ipc_ns.shm_ctlall),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_doulongvec_minmax,
 	},
 	{
@@ -93,6 +95,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.shm_ctlmni,
 		.maxlen		= sizeof(init_ipc_ns.shm_ctlmni),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &ipc_mni,
@@ -102,6 +105,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.shm_rmid_forced,
 		.maxlen		= sizeof(init_ipc_ns.shm_rmid_forced),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_ipc_dointvec_minmax_orphans,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
@@ -111,6 +115,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.msg_ctlmax,
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmax),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -120,6 +125,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.msg_ctlmni,
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmni),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &ipc_mni,
@@ -138,6 +144,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.msg_ctlmnb,
 		.maxlen		= sizeof(init_ipc_ns.msg_ctlmnb),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -147,6 +154,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.sem_ctls,
 		.maxlen		= 4*sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_ipc_sem_dointvec,
 	},
 #ifdef CONFIG_CHECKPOINT_RESTORE
@@ -155,6 +163,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
 		.mode		= 0444,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -164,6 +173,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
 		.mode		= 0444,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -173,6 +183,7 @@ static const struct ctl_table ipc_sysctls[] = {
 		.data		= &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
 		.maxlen		= sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
 		.mode		= 0444,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_INT_MAX,
@@ -290,11 +301,12 @@ bool setup_ipc_sysctls(struct ipc_namespace *ns)
 				tbl[i].data = NULL;
 		}
 
-		ns->ipc_sysctls = __register_sysctl_table(&ns->ipc_set, "kernel", tbl,
-							  ARRAY_SIZE(ipc_sysctls));
-	}
-	if (!ns->ipc_sysctls) {
+		ns->ipc_sysctls = __register_sysctl_table_tmpl(&ns->ipc_set, "kernel",
+							       CTL_TABLE_TMPL(ipc_sysctls),
+							       tbl, ARRAY_SIZE(ipc_sysctls));
 		kfree(tbl);
+	}
+	if (!ns->ipc_sysctls) {
 		retire_sysctl_set(&ns->ipc_set);
 		return false;
 	}
@@ -304,12 +316,8 @@ bool setup_ipc_sysctls(struct ipc_namespace *ns)
 
 void retire_ipc_sysctls(struct ipc_namespace *ns)
 {
-	const struct ctl_table *tbl;
-
-	tbl = ns->ipc_sysctls->ctl_table_arg;
 	unregister_sysctl_table(ns->ipc_sysctls);
 	retire_sysctl_set(&ns->ipc_set);
-	kfree(tbl);
 }
 
 static int __init ipc_sysctl_init(void)

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 8/9] ipc: Register mq_sysctls as a template
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (6 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 7/9] ipc: Register ipc_sysctls " Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  2026-11-26 11:47 ` [PATCH RFC 9/9] ucount: Register user_table " Joel Granados
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Flag SYSCTL_OVERLAY_DATA on all five entries of mq_sysctls and register
the table through __register_sysctl_table_tmpl(), passing the rebased
copy that setup_mq_sysctls() already builds as the overlay. Free the
copy once the call returns and stop freeing ctl_table_arg in
retire_mq_sysctls().

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 ipc/mq_sysctl.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/ipc/mq_sysctl.c b/ipc/mq_sysctl.c
index 0dd12e1c9f53ef07092ca3b638335a438d273a8c..d98c90595fabedc30fa565c8e0ca16ec3399bcf4 100644
--- a/ipc/mq_sysctl.c
+++ b/ipc/mq_sysctl.c
@@ -26,6 +26,7 @@ static const struct ctl_table mq_sysctls[] = {
 		.data		= &init_ipc_ns.mq_queues_max,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec,
 	},
 	{
@@ -33,6 +34,7 @@ static const struct ctl_table mq_sysctls[] = {
 		.data		= &init_ipc_ns.mq_msg_max,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &msg_max_limit_min,
 		.extra2		= &msg_max_limit_max,
@@ -42,6 +44,7 @@ static const struct ctl_table mq_sysctls[] = {
 		.data		= &init_ipc_ns.mq_msgsize_max,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &msg_maxsize_limit_min,
 		.extra2		= &msg_maxsize_limit_max,
@@ -51,6 +54,7 @@ static const struct ctl_table mq_sysctls[] = {
 		.data		= &init_ipc_ns.mq_msg_default,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &msg_max_limit_min,
 		.extra2		= &msg_max_limit_max,
@@ -60,6 +64,7 @@ static const struct ctl_table mq_sysctls[] = {
 		.data		= &init_ipc_ns.mq_msgsize_default,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
+		.flags		= SYSCTL_OVERLAY_DATA,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &msg_maxsize_limit_min,
 		.extra2		= &msg_maxsize_limit_max,
@@ -143,12 +148,13 @@ bool setup_mq_sysctls(struct ipc_namespace *ns)
 				tbl[i].data = NULL;
 		}
 
-		ns->mq_sysctls = __register_sysctl_table(&ns->mq_set,
-							 "fs/mqueue", tbl,
-							 ARRAY_SIZE(mq_sysctls));
-	}
-	if (!ns->mq_sysctls) {
+		ns->mq_sysctls = __register_sysctl_table_tmpl(&ns->mq_set,
+							      "fs/mqueue",
+							      CTL_TABLE_TMPL(mq_sysctls),
+							      tbl, ARRAY_SIZE(mq_sysctls));
 		kfree(tbl);
+	}
+	if (!ns->mq_sysctls) {
 		retire_sysctl_set(&ns->mq_set);
 		return false;
 	}
@@ -158,10 +164,6 @@ bool setup_mq_sysctls(struct ipc_namespace *ns)
 
 void retire_mq_sysctls(struct ipc_namespace *ns)
 {
-	const struct ctl_table *tbl;
-
-	tbl = ns->mq_sysctls->ctl_table_arg;
 	unregister_sysctl_table(ns->mq_sysctls);
 	retire_sysctl_set(&ns->mq_set);
-	kfree(tbl);
 }

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC 9/9] ucount: Register user_table as a template
  2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
                   ` (7 preceding siblings ...)
  2026-11-26 11:47 ` [PATCH RFC 8/9] ipc: Register mq_sysctls " Joel Granados
@ 2026-11-26 11:47 ` Joel Granados
  8 siblings, 0 replies; 11+ messages in thread
From: Joel Granados @ 2026-11-26 11:47 UTC (permalink / raw)
  To: Kees Cook, Alexey Gladkov, David Laight, Wen Yang
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Linus Torvalds, linux-kernel, linux-fsdevel, netdev,
	Joel Granados

Add SYSCTL_OVERLAY_DATA to UCOUNT_ENTRY() so every entry of user_table
carries it, and register the table through
__register_sysctl_table_tmpl(), passing the copy that
setup_userns_sysctls() already fills by position as the overlay. Free
the copy once the call returns and stop freeing ctl_table_arg in
retire_userns_sysctls().

Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
 kernel/ucount.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/kernel/ucount.c b/kernel/ucount.c
index 94b70502dc9ded4de4a946b73141119efc3a26fe..bf021247f5ee5999f3c298e318c25dc2164a8c9b 100644
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -69,6 +69,7 @@ static long ue_int_max = INT_MAX;
 		.procname	= name,				\
 		.maxlen		= sizeof(long),			\
 		.mode		= 0644,				\
+		.flags		= SYSCTL_OVERLAY_DATA,		\
 		.proc_handler	= proc_doulongvec_minmax,	\
 		.extra1		= &ue_zero,			\
 		.extra2		= &ue_int_max,			\
@@ -109,11 +110,12 @@ bool setup_userns_sysctls(struct user_namespace *ns)
 		for (i = 0; i < UCOUNT_COUNTS; i++) {
 			tbl[i].data = &ns->ucount_max[i];
 		}
-		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl,
-						      ARRAY_SIZE(user_table));
-	}
-	if (!ns->sysctls) {
+		ns->sysctls = __register_sysctl_table_tmpl(&ns->set, "user",
+							   CTL_TABLE_TMPL(user_table),
+							   tbl, ARRAY_SIZE(user_table));
 		kfree(tbl);
+	}
+	if (!ns->sysctls) {
 		retire_sysctl_set(&ns->set);
 		return false;
 	}
@@ -124,12 +126,8 @@ bool setup_userns_sysctls(struct user_namespace *ns)
 void retire_userns_sysctls(struct user_namespace *ns)
 {
 #ifdef CONFIG_SYSCTL
-	const struct ctl_table *tbl;
-
-	tbl = ns->sysctls->ctl_table_arg;
 	unregister_sysctl_table(ns->sysctls);
 	retire_sysctl_set(&ns->set);
-	kfree(tbl);
 #endif
 }
 

-- 
2.50.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type
  2026-11-26 11:47 ` [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type Joel Granados
@ 2026-11-26 17:04   ` Linus Torvalds
  0 siblings, 0 replies; 11+ messages in thread
From: Linus Torvalds @ 2026-11-26 17:04 UTC (permalink / raw)
  To: Joel Granados
  Cc: Kees Cook, Alexey Gladkov, David Laight, Wen Yang, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, linux-kernel,
	linux-fsdevel, netdev

On Thu, 26 Nov 2026 at 03:47, Joel Granados <joel.granados@kernel.org> wrote:
>
> +/* Template version of struct ctl_table. Used for registering ctl_table templates */
> +struct ctl_table_tmpl {
> +       struct ctl_table t;
> +};

So I really don't like members named 't'.

I see why you are doing it, but maybe you aren't aware that we already
enabled ms-extensions, and you can actually use *unnamed* structs in
templates.

So you can do

    struct ctl_table_tmpl { struct ctl_table; }

and that gives you a new type that you can use without any "t." kind
of thing, but is still distinct from 'struct ctl_table".

Note: being unnamed is very convenient in some situations, less so in
others. If you actually then want to access not the individual fields
(which is very easy), but get from the template to an actual "struct
ctl_table", you'd have to use some explicit helper wrapper, something
like

   static inline struct ctl_table *template_to_inner(struct ctl_table_tmpl *a)
   { return container_of(&a->procname, struct ctl_table, procname); }

or whatever.

Honestly, right now you seem to do something really ugly: you have
that 'template', but then you hardly ever *use* it as anything but a
top-level interface, and you turn it into a 'struct ctl_table *' very
early. And that looks like it's done exactly to avoid the whole
messiness with the 't' member - but it also measn that internally that
"template" has lost almost all meaning, and it looks like a regular
ctl_table.

Maybe that's fine. But it feels like the template is less of a real
template than just a wrapper that is only used at the top-level
wrapper and then never again. That does *not* work well if you were to
add a few extra fields to the template (that is then *not* in the
struct ctl_table. because they are only used to set it up). Hmm?

              Linus

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-26 17:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 11:47 [PATCH RFC 0/9] sysctl: Replace ARRAY kmemdup with overlay mechanism for NS registration Joel Granados
2026-11-26 11:47 ` [PATCH RFC 1/9] sysctl: Add struct ctl_table_tmpl as a distinct template type Joel Granados
2026-11-26 17:04   ` Linus Torvalds
2026-11-26 11:47 ` [PATCH RFC 2/9] sysctl: Add flags to struct ctl_table Joel Granados
2026-11-26 11:47 ` [PATCH RFC 3/9] sysctl: Add overlay mechanism for flagged ctl_table members Joel Granados
2026-11-26 11:47 ` [PATCH RFC 4/9] sysctl: Add sysctl template registration Joel Granados
2026-11-26 11:47 ` [PATCH RFC 5/9] net: sysctl: Add sysctl registration to networking Joel Granados
2026-11-26 11:47 ` [PATCH RFC 6/9] ipv4: Register ipv4_net_table as a template Joel Granados
2026-11-26 11:47 ` [PATCH RFC 7/9] ipc: Register ipc_sysctls " Joel Granados
2026-11-26 11:47 ` [PATCH RFC 8/9] ipc: Register mq_sysctls " Joel Granados
2026-11-26 11:47 ` [PATCH RFC 9/9] ucount: Register user_table " Joel Granados

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®