Linux HAM/Amateur Radio development
 help / color / mirror / Atom feed
From: Joel Granados <joel.granados@gmail.com>
To: mcgrof@kernel.org
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	Iurii Zaikin <yzaikin@google.com>,
	Jozsef Kadlecsik <kadlec@netfilter.org>,
	Sven Schnelle <svens@linux.ibm.com>,
	Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Steffen Klassert <steffen.klassert@secunet.com>,
	Kees Cook <keescook@chromium.org>,
	"D. Wythe" <alibuda@linux.alibaba.com>,
	mptcp@lists.linux.dev, Jakub Kicinski <kuba@kernel.org>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Paolo Abeni <pabeni@redhat.com>,
	coreteam@netfilter.org, Jan Karcher <jaka@linux.ibm.com>,
	Alexander Aring <alex.aring@gmail.com>,
	Will Deacon <will@kernel.org>,
	Stefan Schmidt <stefan@datenfreihafen.org>,
	Matthieu Baerts <matthieu.baerts@tessares.net>,
	bridge@lists.linux-foundation.org,
	linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 08/14] sysctl: Add size to register_net_sysctl function
Date: Mon, 31 Jul 2023 09:17:22 +0200	[thread overview]
Message-ID: <20230731071728.3493794-9-j.granados@samsung.com> (raw)
In-Reply-To: <20230731071728.3493794-1-j.granados@samsung.com>

This commit adds size to the register_net_sysctl indirection function to
facilitate the removal of the sentinel elements (last empty markers)
from the ctl_table arrays. Though we don't actually remove any sentinels
in this commit, register_net_sysctl* now has the capability of
forwarding table_size for when that happens.

We create a new function register_net_sysctl_sz with an extra size
argument. A macro replaces the existing register_net_sysctl. The size in
the macro is SIZE_MAX instead of ARRAY_SIZE to avoid compilation errors
while we systematically migrate to register_net_sysctl_sz. Will change
to ARRAY_SIZE in subsequent commits.

Care is taken to add table_size to the stopping criteria in such a way
that when we remove the empty sentinel element, it will continue
stopping in the last element of the ctl_table array.

Signed-off-by: Joel Granados <j.granados@samsung.com>
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 include/net/net_namespace.h | 10 ++++++----
 net/sysctl_net.c            | 22 +++++++++++++---------
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 78beaa765c73..e4e5fe75a281 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -469,15 +469,17 @@ void unregister_pernet_device(struct pernet_operations *);
 
 struct ctl_table;
 
+#define register_net_sysctl(net, path, table)	\
+	register_net_sysctl_sz(net, path, table, SIZE_MAX)
 #ifdef CONFIG_SYSCTL
 int net_sysctl_init(void);
-struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
-					     struct ctl_table *table);
+struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
+					     struct ctl_table *table, size_t table_size);
 void unregister_net_sysctl_table(struct ctl_table_header *header);
 #else
 static inline int net_sysctl_init(void) { return 0; }
-static inline struct ctl_table_header *register_net_sysctl(struct net *net,
-	const char *path, struct ctl_table *table)
+static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
+	const char *path, struct ctl_table *table, size_t table_size)
 {
 	return NULL;
 }
diff --git a/net/sysctl_net.c b/net/sysctl_net.c
index d9cbbb51b143..051ed5f6fc93 100644
--- a/net/sysctl_net.c
+++ b/net/sysctl_net.c
@@ -122,12 +122,13 @@ __init int net_sysctl_init(void)
  *    allocated.
  */
 static void ensure_safe_net_sysctl(struct net *net, const char *path,
-				   struct ctl_table *table)
+				   struct ctl_table *table, size_t table_size)
 {
 	struct ctl_table *ent;
 
 	pr_debug("Registering net sysctl (net %p): %s\n", net, path);
-	for (ent = table; ent->procname; ent++) {
+	ent = table;
+	for (size_t i = 0; i < table_size && ent->procname; ent++, i++) {
 		unsigned long addr;
 		const char *where;
 
@@ -160,21 +161,24 @@ static void ensure_safe_net_sysctl(struct net *net, const char *path,
 	}
 }
 
-struct ctl_table_header *register_net_sysctl(struct net *net,
-	const char *path, struct ctl_table *table)
+struct ctl_table_header *register_net_sysctl_sz(struct net *net,
+						const char *path,
+						struct ctl_table *table,
+						size_t table_size)
 {
-	int count = 0;
+	int count;
 	struct ctl_table *entry;
 
 	if (!net_eq(net, &init_net))
-		ensure_safe_net_sysctl(net, path, table);
+		ensure_safe_net_sysctl(net, path, table, table_size);
 
-	for (entry = table; entry->procname; entry++)
-		count++;
+	entry = table;
+	for (count = 0 ; count < table_size && entry->procname; entry++, count++)
+		;
 
 	return __register_sysctl_table(&net->sysctls, path, table, count);
 }
-EXPORT_SYMBOL_GPL(register_net_sysctl);
+EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
 
 void unregister_net_sysctl_table(struct ctl_table_header *header)
 {
-- 
2.30.2


  parent reply	other threads:[~2023-07-31  7:17 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-31  7:17 [PATCH v2 00/14] sysctl: Add a size argument to register functions in sysctl Joel Granados
2023-07-31  7:17 ` [PATCH v2 01/14] sysctl: Prefer ctl_table_header in proc_sysctl Joel Granados
2023-07-31  7:17 ` [PATCH v2 02/14] sysctl: Use ctl_table_header in list_for_each_table_entry Joel Granados
2023-07-31  7:17 ` [PATCH v2 03/14] sysctl: Add ctl_table_size to ctl_table_header Joel Granados
     [not found]   ` <20230731071728.3493794-4-j.granados-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2023-07-31 18:30     ` Simon Horman
2023-07-31 19:07       ` Simon Horman
2023-08-01 10:03         ` Joel Granados
2023-07-31  7:17 ` [PATCH v2 04/14] sysctl: Add size argument to init_header Joel Granados
2023-07-31  7:17 ` [PATCH v2 05/14] sysctl: Add a size arg to __register_sysctl_table Joel Granados
2023-07-31  7:17 ` [PATCH v2 06/14] sysctl: Add size to register_sysctl Joel Granados
2023-07-31  7:17 ` [PATCH v2 07/14] sysctl: Add size arg to __register_sysctl_init Joel Granados
2023-07-31  7:17 ` Joel Granados [this message]
2023-07-31  7:17 ` [PATCH v2 09/14] ax.25: Update to register_net_sysctl_sz Joel Granados
     [not found] ` <20230731071728.3493794-1-j.granados-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2023-07-31  7:17   ` [PATCH v2 10/14] netfilter: " Joel Granados
2023-08-01  6:11     ` Julian Anastasov
2023-07-31  7:17   ` [PATCH v2 11/14] networking: " Joel Granados
2023-08-08 11:20     ` Przemek Kitszel
2023-08-08 14:24       ` Joel Granados
2023-08-08 15:23       ` Joel Granados
2023-07-31  7:17 ` [PATCH v2 12/14] vrf: " Joel Granados
2023-07-31  7:17 ` [PATCH v2 13/14] sysctl: SIZE_MAX->ARRAY_SIZE in register_net_sysctl Joel Granados
2023-07-31  7:17 ` [PATCH v2 14/14] sysctl: Use ctl_table_size as stopping criteria for list macro Joel Granados
2023-07-31 20:50 ` [PATCH v2 00/14] sysctl: Add a size argument to register functions in sysctl Luis Chamberlain
2023-08-01 10:01   ` Joel Granados
2023-07-31 21:36 ` Luis Chamberlain
2023-08-01  9:35   ` Joel Granados
2023-08-07 21:44   ` Luis Chamberlain
2023-08-07 23:00     ` Chris Maness
2023-08-07 23:43       ` Luis Chamberlain
2023-08-08  2:50         ` Chris Maness
2023-08-08  3:00           ` Luis Chamberlain
2023-08-08  3:07             ` Chris Maness
2023-08-08 13:59               ` Joel Granados
2023-08-08 13:58           ` Joel Granados
2023-08-08  2:09     ` Jakub Kicinski
2023-08-08  2:34       ` Luis Chamberlain

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=20230731071728.3493794-9-j.granados@samsung.com \
    --to=joel.granados@gmail.com \
    --cc=alex.aring@gmail.com \
    --cc=alibuda@linux.alibaba.com \
    --cc=bridge@lists.linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=coreteam@netfilter.org \
    --cc=gor@linux.ibm.com \
    --cc=jaka@linux.ibm.com \
    --cc=kadlec@netfilter.org \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=marcelo.leitner@gmail.com \
    --cc=matthieu.baerts@tessares.net \
    --cc=mcgrof@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=pabeni@redhat.com \
    --cc=stefan@datenfreihafen.org \
    --cc=steffen.klassert@secunet.com \
    --cc=svens@linux.ibm.com \
    --cc=will@kernel.org \
    --cc=yzaikin@google.com \
    /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