Skip to content

Commit 95d4977

Browse files
Joelgranadosmcgrof
authored andcommitted
sysctl: Add size to register_net_sysctl function
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 <[email protected]> Suggested-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 3bc269c commit 95d4977

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

include/net/net_namespace.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,17 @@ void unregister_pernet_device(struct pernet_operations *);
469469

470470
struct ctl_table;
471471

472+
#define register_net_sysctl(net, path, table) \
473+
register_net_sysctl_sz(net, path, table, SIZE_MAX)
472474
#ifdef CONFIG_SYSCTL
473475
int net_sysctl_init(void);
474-
struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
475-
struct ctl_table *table);
476+
struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
477+
struct ctl_table *table, size_t table_size);
476478
void unregister_net_sysctl_table(struct ctl_table_header *header);
477479
#else
478480
static inline int net_sysctl_init(void) { return 0; }
479-
static inline struct ctl_table_header *register_net_sysctl(struct net *net,
480-
const char *path, struct ctl_table *table)
481+
static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
482+
const char *path, struct ctl_table *table, size_t table_size)
481483
{
482484
return NULL;
483485
}

net/sysctl_net.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ __init int net_sysctl_init(void)
122122
* allocated.
123123
*/
124124
static void ensure_safe_net_sysctl(struct net *net, const char *path,
125-
struct ctl_table *table)
125+
struct ctl_table *table, size_t table_size)
126126
{
127127
struct ctl_table *ent;
128128

129129
pr_debug("Registering net sysctl (net %p): %s\n", net, path);
130-
for (ent = table; ent->procname; ent++) {
130+
ent = table;
131+
for (size_t i = 0; i < table_size && ent->procname; ent++, i++) {
131132
unsigned long addr;
132133
const char *where;
133134

@@ -160,21 +161,24 @@ static void ensure_safe_net_sysctl(struct net *net, const char *path,
160161
}
161162
}
162163

163-
struct ctl_table_header *register_net_sysctl(struct net *net,
164-
const char *path, struct ctl_table *table)
164+
struct ctl_table_header *register_net_sysctl_sz(struct net *net,
165+
const char *path,
166+
struct ctl_table *table,
167+
size_t table_size)
165168
{
166-
int count = 0;
169+
int count;
167170
struct ctl_table *entry;
168171

169172
if (!net_eq(net, &init_net))
170-
ensure_safe_net_sysctl(net, path, table);
173+
ensure_safe_net_sysctl(net, path, table, table_size);
171174

172-
for (entry = table; entry->procname; entry++)
173-
count++;
175+
entry = table;
176+
for (count = 0 ; count < table_size && entry->procname; entry++, count++)
177+
;
174178

175179
return __register_sysctl_table(&net->sysctls, path, table, count);
176180
}
177-
EXPORT_SYMBOL_GPL(register_net_sysctl);
181+
EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
178182

179183
void unregister_net_sysctl_table(struct ctl_table_header *header)
180184
{

0 commit comments

Comments
 (0)