mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: wen.yang@linux.dev
To: Joel Granados <joel.granados@kernel.org>,
	David Laight <david.laight.linux@gmail.com>,
	Kees Cook <kees@kernel.org>
Cc: Wen Yang <wen.yang@linux.dev>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 3/5] netfilter: embed logger sysctl context using stride
Date: Tue, 17 Nov 2026 00:07:35 +0800	[thread overview]
Message-ID: <796f4b6532668a124365c0fabf39af53405f3236.1794844686.git.wen.yang@linux.dev> (raw)
In-Reply-To: <cover.1794844686.git.wen.yang@linux.dev>

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

Embed struct ctl_table into struct nf_log_sysctl_entry along with
per-entry net and pf context, and register the array with
register_net_sysctl_stride().

In the proc_handler nf_log_proc_dostring(), recover the entry using
standard container_of(table, struct nf_log_sysctl_entry, ctl).  This
eliminates the need for 11 per-family wrapper functions while keeping
struct ctl_table free of a priv field.

Suggested-by: David Laight <david.laight.linux@gmail.com>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
 net/netfilter/nf_log.c | 84 +++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/net/netfilter/nf_log.c b/net/netfilter/nf_log.c
index f1d3649e3fc6..c8cbbadc50ba 100644
--- a/net/netfilter/nf_log.c
+++ b/net/netfilter/nf_log.c
@@ -420,8 +420,13 @@ static const struct seq_operations nflog_seq_ops = {
 #endif /* PROC_FS */
 
 #ifdef CONFIG_SYSCTL
+struct nf_log_sysctl_entry {
+	struct ctl_table ctl;
+	struct net *net;
+	int pf;
+};
+
 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
-static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO];
 static struct ctl_table_header *nf_log_sysctl_fhdr;
 
 static struct ctl_table nf_log_sysctl_ftable[] = {
@@ -437,11 +442,13 @@ static struct ctl_table nf_log_sysctl_ftable[] = {
 static int nf_log_proc_dostring(const struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
+	const struct nf_log_sysctl_entry *entry =
+		container_of(table, struct nf_log_sysctl_entry, ctl);
 	const struct nf_logger *logger;
 	char buf[NFLOGGER_NAME_LEN];
 	int r = 0;
-	int tindex = (unsigned long)table->extra1;
-	struct net *net = table->extra2;
+	int tindex = entry->pf;
+	struct net *net = entry->net;
 
 	if (write) {
 		struct ctl_table tmp = *table;
@@ -486,66 +493,59 @@ static int nf_log_proc_dostring(const struct ctl_table *table, int write,
 
 static int netfilter_log_sysctl_init(struct net *net)
 {
+	struct nf_log_sysctl_entry *entries;
 	int i;
-	struct ctl_table *table;
-
-	table = nf_log_sysctl_table;
-	if (!net_eq(net, &init_net)) {
-		table = kmemdup(nf_log_sysctl_table,
-				 sizeof(nf_log_sysctl_table),
-				 GFP_KERNEL_ACCOUNT);
-		if (!table)
-			goto err_alloc;
-	} else {
-		for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
-			snprintf(nf_log_sysctl_fnames[i],
-				 3, "%d", i);
-			nf_log_sysctl_table[i].procname	=
-				nf_log_sysctl_fnames[i];
-			nf_log_sysctl_table[i].maxlen = NFLOGGER_NAME_LEN;
-			nf_log_sysctl_table[i].mode = 0644;
-			nf_log_sysctl_table[i].proc_handler =
-				nf_log_proc_dostring;
-			nf_log_sysctl_table[i].extra1 =
-				(void *)(unsigned long) i;
-		}
+
+	entries = kzalloc(sizeof(*entries) * NFPROTO_NUMPROTO, GFP_KERNEL_ACCOUNT);
+	if (!entries)
+		return -ENOMEM;
+
+	for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
+		snprintf(nf_log_sysctl_fnames[i], 3, "%d", i);
+		entries[i].ctl.procname = nf_log_sysctl_fnames[i];
+		entries[i].ctl.maxlen = NFLOGGER_NAME_LEN;
+		entries[i].ctl.mode = 0644;
+		entries[i].ctl.proc_handler = nf_log_proc_dostring;
+		entries[i].net = net;
+		entries[i].pf = i;
+	}
+
+	if (net_eq(net, &init_net)) {
 		nf_log_sysctl_fhdr = register_net_sysctl(net, "net/netfilter",
 							 nf_log_sysctl_ftable);
-		if (!nf_log_sysctl_fhdr)
-			goto err_freg;
+		if (!nf_log_sysctl_fhdr) {
+			kfree(entries);
+			return -ENOMEM;
+		}
 	}
 
-	for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
-		table[i].extra2 = net;
-
-	net->nf.nf_log_dir_header = register_net_sysctl_sz(net,
-							   "net/netfilter/nf_log",
-							   table,
-							   ARRAY_SIZE(nf_log_sysctl_table));
+	net->nf.nf_log_dir_header = register_net_sysctl_stride(net,
+							       "net/netfilter/nf_log",
+							       &entries[0].ctl,
+							       NFPROTO_NUMPROTO,
+							       sizeof(*entries));
 	if (!net->nf.nf_log_dir_header)
 		goto err_reg;
 
 	return 0;
 
 err_reg:
-	if (!net_eq(net, &init_net))
-		kfree(table);
-	else
+	if (net_eq(net, &init_net))
 		unregister_net_sysctl_table(nf_log_sysctl_fhdr);
-err_freg:
-err_alloc:
+	kfree(entries);
 	return -ENOMEM;
 }
 
 static void netfilter_log_sysctl_exit(struct net *net)
 {
 	const struct ctl_table *table;
+	struct nf_log_sysctl_entry *entries;
 
 	table = net->nf.nf_log_dir_header->ctl_table_arg;
+	entries = container_of(table, struct nf_log_sysctl_entry, ctl);
 	unregister_net_sysctl_table(net->nf.nf_log_dir_header);
-	if (!net_eq(net, &init_net))
-		kfree(table);
-	else
+	kfree(entries);
+	if (net_eq(net, &init_net))
 		unregister_net_sysctl_table(nf_log_sysctl_fhdr);
 }
 #else
-- 
2.25.1


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

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

Reply instructions:

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

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

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

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

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

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

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

all inboxes | Powered by JetHome®