Skip to content

Commit 5829614

Browse files
committed
Merge branch 'net-sysctl-sentinel'
Joel Granados says: ==================== sysctl: Remove sentinel elements from networking What? These commits remove the sentinel element (last empty element) from the sysctl arrays of all the files under the "net/" directory that register a sysctl array. The merging of the preparation patches [4] to mainline allows us to just remove sentinel elements without changing behavior. This is safe because the sysctl registration code (register_sysctl() and friends) use the array size in addition to checking for a sentinel [1]. Why? By removing the sysctl sentinel elements we avoid kernel bloat as ctl_table arrays get moved out of kernel/sysctl.c into their own respective subsystems. This move was started long ago to avoid merge conflicts; the sentinel removal bit came after Mathew Wilcox suggested it to avoid bloating the kernel by one element as arrays moved out. This patchset will reduce the overall build time size of the kernel and run time memory bloat by about ~64 bytes per declared ctl_table array (more info here [5]). When are we done? There are 4 patchest (25 commits [2]) that are still outstanding to completely remove the sentinels: files under "net/" (this patchset), files under "kernel/" dir, misc dirs (files under mm/ security/ and others) and the final set that removes the unneeded check for ->procname == NULL. Testing: * Ran sysctl selftests (./tools/testing/selftests/sysctl/sysctl.sh) * Ran this through 0-day with no errors or warnings Savings in vmlinux: A total of 64 bytes per sentinel is saved after removal; I measured in x86_64 to give an idea of the aggregated savings. The actual savings will depend on individual kernel configuration. * bloat-o-meter - The "yesall" config saves 3976 bytes (bloat-o-meter output [6]) - A reduced config [3] saves 1263 bytes (bloat-o-meter output [7]) Savings in allocated memory: None in this set but will occur when the superfluous allocations are removed from proc_sysctl.c. I include it here for context. The estimated savings during boot for config [3] are 6272 bytes. See [8] for how to measure it. Comments/feedback greatly appreciated Changes in v6: - Rebased onto net-next/main. - Besides re-running my cocci scripts, I ran a new find script [9]. Found 0 hits in net/ - Moved "i" variable declaraction out of for() in sysctl_core_net_init - Removed forgotten sentinel in mpls_table - Removed CONFIG_AX25_DAMA_SLAVE guard from net/ax25/ax25_ds_timer.c. It is not needed because that file is compiled only when CONFIG_AX25_DAMA_SLAVE is set. - When traversing smc_table, stop on ARRAY_SIZE instead of ARRAY_SIZE-1. - Link to v5: https://lore.kernel.org/r/20240426-jag-sysctl_remset_net-v5-0-e3b12f6111a6@samsung.com Changes in v5: - Added net files with additional variable to my test .config so the typo can be caught next time. - Fixed typo tabel_size -> table_size - Link to v4: https://lore.kernel.org/r/20240425-jag-sysctl_remset_net-v4-0-9e82f985777d@samsung.com Changes in v4: - Keep reverse xmas tree order when introducing new variables - Use a table_size variable to keep the value of ARRAY_SIZE - Separated the original "networking: Remove the now superfluous sentinel elements from ctl_table arra" into smaller commits to ease review - Merged x.25 and ax.25 commits together. - Removed any SOB from the commits that were changed - Link to v3: https://lore.kernel.org/r/20240412-jag-sysctl_remset_net-v3-0-11187d13c211@samsung.com Changes in v3: - Reworkded ax.25 - Added a BUILD_BUG_ON for the ax.25 commit - Added a CONFIG_AX25_DAMA_SLAVE guard where needed - Link to v2: https://lore.kernel.org/r/20240328-jag-sysctl_remset_net-v2-0-52c9fad9a1af@samsung.com Changes in v2: - Rebased to v6.9-rc1 - Removed unneeded comment from sysctl_net_ax25.c - Link to v1: https://lore.kernel.org/r/20240314-jag-sysctl_remset_net-v1-0-aa26b44d29d9@samsung.com ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents a17ef9e + 78a7b5d commit 5829614

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+48
-119
lines changed

include/net/ax25.h

+2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ enum {
139139
AX25_VALUES_N2, /* Default N2 value */
140140
AX25_VALUES_PACLEN, /* AX.25 MTU */
141141
AX25_VALUES_PROTOCOL, /* Std AX.25, DAMA Slave, DAMA Master */
142+
#ifdef CONFIG_AX25_DAMA_SLAVE
142143
AX25_VALUES_DS_TIMEOUT, /* DAMA Slave timeout */
144+
#endif
143145
AX25_MAX_VALUES /* THIS MUST REMAIN THE LAST ENTRY OF THIS LIST */
144146
};
145147

net/appletalk/sysctl_net_atalk.c

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ static struct ctl_table atalk_table[] = {
4040
.mode = 0644,
4141
.proc_handler = proc_dointvec_jiffies,
4242
},
43-
{ },
4443
};
4544

4645
static struct ctl_table_header *atalk_table_header;

net/ax25/ax25_dev.c

+3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ void ax25_dev_device_up(struct net_device *dev)
7878
ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
7979
ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
8080
ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
81+
82+
#ifdef CONFIG_AX25_DAMA_SLAVE
8183
ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
84+
#endif
8285

8386
#if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
8487
ax25_ds_setup_timer(ax25_dev);

net/ax25/ax25_ds_timer.c

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void ax25_ds_set_timer(ax25_dev *ax25_dev)
5555
ax25_dev->dama.slave_timeout =
5656
msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
5757
mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
58+
return;
5859
}
5960

6061
/*

net/ax25/sysctl_net_ax25.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ static const struct ctl_table ax25_param_table[] = {
141141
.extra2 = &max_ds_timeout
142142
},
143143
#endif
144-
145-
{ } /* that's all, folks! */
146144
};
147145

148146
int ax25_register_dev_sysctl(ax25_dev *ax25_dev)
@@ -155,6 +153,7 @@ int ax25_register_dev_sysctl(ax25_dev *ax25_dev)
155153
if (!table)
156154
return -ENOMEM;
157155

156+
BUILD_BUG_ON(ARRAY_SIZE(ax25_param_table) != AX25_MAX_VALUES);
158157
for (k = 0; k < AX25_MAX_VALUES; k++)
159158
table[k].data = &ax25_dev->values[k];
160159

net/bridge/br_netfilter_hooks.c

-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,6 @@ static struct ctl_table brnf_table[] = {
12261226
.mode = 0644,
12271227
.proc_handler = brnf_sysctl_call_tables,
12281228
},
1229-
{ }
12301229
};
12311230

12321231
static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)

net/core/neighbour.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
37333733

37343734
static struct neigh_sysctl_table {
37353735
struct ctl_table_header *sysctl_header;
3736-
struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3736+
struct ctl_table neigh_vars[NEIGH_VAR_MAX];
37373737
} neigh_sysctl_template __read_mostly = {
37383738
.neigh_vars = {
37393739
NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
@@ -3784,7 +3784,6 @@ static struct neigh_sysctl_table {
37843784
.extra2 = SYSCTL_INT_MAX,
37853785
.proc_handler = proc_dointvec_minmax,
37863786
},
3787-
{},
37883787
},
37893788
};
37903789

@@ -3812,8 +3811,6 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
38123811
if (dev) {
38133812
dev_name_source = dev->name;
38143813
/* Terminate the table early */
3815-
memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3816-
sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
38173814
neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
38183815
} else {
38193816
struct neigh_table *tbl = p->tbl;

net/core/sysctl_net_core.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@ static struct ctl_table net_core_table[] = {
661661
.proc_handler = proc_dointvec_minmax,
662662
.extra1 = SYSCTL_ZERO,
663663
},
664-
{ }
665664
};
666665

667666
static struct ctl_table netns_core_table[] = {
@@ -698,7 +697,6 @@ static struct ctl_table netns_core_table[] = {
698697
.extra2 = SYSCTL_ONE,
699698
.proc_handler = proc_dou8vec_minmax,
700699
},
701-
{ }
702700
};
703701

704702
static int __init fb_tunnels_only_for_init_net_sysctl_setup(char *str)
@@ -716,20 +714,21 @@ __setup("fb_tunnels=", fb_tunnels_only_for_init_net_sysctl_setup);
716714

717715
static __net_init int sysctl_core_net_init(struct net *net)
718716
{
719-
struct ctl_table *tbl, *tmp;
717+
size_t table_size = ARRAY_SIZE(netns_core_table);
718+
struct ctl_table *tbl;
720719

721720
tbl = netns_core_table;
722721
if (!net_eq(net, &init_net)) {
722+
int i;
723723
tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
724724
if (tbl == NULL)
725725
goto err_dup;
726726

727-
for (tmp = tbl; tmp->procname; tmp++)
728-
tmp->data += (char *)net - (char *)&init_net;
727+
for (i = 0; i < table_size; ++i)
728+
tbl[i].data += (char *)net - (char *)&init_net;
729729
}
730730

731-
net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl,
732-
ARRAY_SIZE(netns_core_table));
731+
net->core.sysctl_hdr = register_net_sysctl_sz(net, "net/core", tbl, table_size);
733732
if (net->core.sysctl_hdr == NULL)
734733
goto err_reg;
735734

net/dccp/sysctl.c

-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ static struct ctl_table dccp_default_table[] = {
9090
.mode = 0644,
9191
.proc_handler = proc_dointvec_ms_jiffies,
9292
},
93-
94-
{ }
9593
};
9694

9795
static struct ctl_table_header *dccp_table_header;

net/ieee802154/6lowpan/reassembly.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ static struct ctl_table lowpan_frags_ns_ctl_table[] = {
338338
.mode = 0644,
339339
.proc_handler = proc_dointvec_jiffies,
340340
},
341-
{ }
342341
};
343342

344343
/* secret interval has been deprecated */
@@ -351,7 +350,6 @@ static struct ctl_table lowpan_frags_ctl_table[] = {
351350
.mode = 0644,
352351
.proc_handler = proc_dointvec_jiffies,
353352
},
354-
{ }
355353
};
356354

357355
static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
@@ -370,10 +368,8 @@ static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
370368
goto err_alloc;
371369

372370
/* Don't export sysctls to unprivileged users */
373-
if (net->user_ns != &init_user_ns) {
374-
table[0].procname = NULL;
371+
if (net->user_ns != &init_user_ns)
375372
table_size = 0;
376-
}
377373
}
378374

379375
table[0].data = &ieee802154_lowpan->fqdir->high_thresh;

net/ipv4/devinet.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
25202520

25212521
static struct devinet_sysctl_table {
25222522
struct ctl_table_header *sysctl_header;
2523-
struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
2523+
struct ctl_table devinet_vars[IPV4_DEVCONF_MAX];
25242524
} devinet_sysctl = {
25252525
.devinet_vars = {
25262526
DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
@@ -2583,7 +2583,7 @@ static int __devinet_sysctl_register(struct net *net, char *dev_name,
25832583
if (!t)
25842584
goto out;
25852585

2586-
for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2586+
for (i = 0; i < ARRAY_SIZE(t->devinet_vars); i++) {
25872587
t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
25882588
t->devinet_vars[i].extra1 = p;
25892589
t->devinet_vars[i].extra2 = net;
@@ -2657,7 +2657,6 @@ static struct ctl_table ctl_forward_entry[] = {
26572657
.extra1 = &ipv4_devconf,
26582658
.extra2 = &init_net,
26592659
},
2660-
{ },
26612660
};
26622661
#endif
26632662

net/ipv4/ip_fragment.c

-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ static struct ctl_table ip4_frags_ns_ctl_table[] = {
580580
.proc_handler = proc_dointvec_minmax,
581581
.extra1 = &dist_min,
582582
},
583-
{ }
584583
};
585584

586585
/* secret interval has been deprecated */
@@ -593,7 +592,6 @@ static struct ctl_table ip4_frags_ctl_table[] = {
593592
.mode = 0644,
594593
.proc_handler = proc_dointvec_jiffies,
595594
},
596-
{ }
597595
};
598596

599597
static int __net_init ip4_frags_ns_ctl_register(struct net *net)

net/ipv4/route.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -3496,7 +3496,6 @@ static struct ctl_table ipv4_route_table[] = {
34963496
.mode = 0644,
34973497
.proc_handler = proc_dointvec,
34983498
},
3499-
{ }
35003499
};
35013500

35023501
static const char ipv4_route_flush_procname[] = "flush";
@@ -3530,7 +3529,6 @@ static struct ctl_table ipv4_route_netns_table[] = {
35303529
.mode = 0644,
35313530
.proc_handler = proc_dointvec,
35323531
},
3533-
{ },
35343532
};
35353533

35363534
static __net_init int sysctl_route_net_init(struct net *net)
@@ -3548,16 +3546,14 @@ static __net_init int sysctl_route_net_init(struct net *net)
35483546

35493547
/* Don't export non-whitelisted sysctls to unprivileged users */
35503548
if (net->user_ns != &init_user_ns) {
3551-
if (tbl[0].procname != ipv4_route_flush_procname) {
3552-
tbl[0].procname = NULL;
3549+
if (tbl[0].procname != ipv4_route_flush_procname)
35533550
table_size = 0;
3554-
}
35553551
}
35563552

35573553
/* Update the variables to point into the current struct net
35583554
* except for the first element flush
35593555
*/
3560-
for (i = 1; i < ARRAY_SIZE(ipv4_route_netns_table) - 1; i++)
3556+
for (i = 1; i < table_size; i++)
35613557
tbl[i].data += (void *)net - (void *)&init_net;
35623558
}
35633559
tbl[0].extra1 = net;

net/ipv4/sysctl_net_ipv4.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@ static struct ctl_table ipv4_table[] = {
575575
.extra1 = &sysctl_fib_sync_mem_min,
576576
.extra2 = &sysctl_fib_sync_mem_max,
577577
},
578-
{ }
579578
};
580579

581580
static struct ctl_table ipv4_net_table[] = {
@@ -1502,11 +1501,11 @@ static struct ctl_table ipv4_net_table[] = {
15021501
.proc_handler = proc_dou8vec_minmax,
15031502
.extra1 = SYSCTL_ONE,
15041503
},
1505-
{ }
15061504
};
15071505

15081506
static __net_init int ipv4_sysctl_init_net(struct net *net)
15091507
{
1508+
size_t table_size = ARRAY_SIZE(ipv4_net_table);
15101509
struct ctl_table *table;
15111510

15121511
table = ipv4_net_table;
@@ -1517,7 +1516,7 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
15171516
if (!table)
15181517
goto err_alloc;
15191518

1520-
for (i = 0; i < ARRAY_SIZE(ipv4_net_table) - 1; i++) {
1519+
for (i = 0; i < table_size; i++) {
15211520
if (table[i].data) {
15221521
/* Update the variables to point into
15231522
* the current struct net
@@ -1533,7 +1532,7 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
15331532
}
15341533

15351534
net->ipv4.ipv4_hdr = register_net_sysctl_sz(net, "net/ipv4", table,
1536-
ARRAY_SIZE(ipv4_net_table));
1535+
table_size);
15371536
if (!net->ipv4.ipv4_hdr)
15381537
goto err_reg;
15391538

net/ipv4/xfrm4_policy.c

-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ static struct ctl_table xfrm4_policy_table[] = {
152152
.mode = 0644,
153153
.proc_handler = proc_dointvec,
154154
},
155-
{ }
156155
};
157156

158157
static __net_init int xfrm4_net_sysctl_init(struct net *net)

net/ipv6/addrconf.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -7184,14 +7184,12 @@ static const struct ctl_table addrconf_sysctl[] = {
71847184
.extra1 = SYSCTL_ZERO,
71857185
.extra2 = SYSCTL_TWO,
71867186
},
7187-
{
7188-
/* sentinel */
7189-
}
71907187
};
71917188

71927189
static int __addrconf_sysctl_register(struct net *net, char *dev_name,
71937190
struct inet6_dev *idev, struct ipv6_devconf *p)
71947191
{
7192+
size_t table_size = ARRAY_SIZE(addrconf_sysctl);
71957193
int i, ifindex;
71967194
struct ctl_table *table;
71977195
char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
@@ -7200,7 +7198,7 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
72007198
if (!table)
72017199
goto out;
72027200

7203-
for (i = 0; table[i].data; i++) {
7201+
for (i = 0; i < table_size; i++) {
72047202
table[i].data += (char *)p - (char *)&ipv6_devconf;
72057203
/* If one of these is already set, then it is not safe to
72067204
* overwrite either of them: this makes proc_dointvec_minmax
@@ -7215,7 +7213,7 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
72157213
snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
72167214

72177215
p->sysctl_header = register_net_sysctl_sz(net, path, table,
7218-
ARRAY_SIZE(addrconf_sysctl));
7216+
table_size);
72197217
if (!p->sysctl_header)
72207218
goto free;
72217219

net/ipv6/icmp.c

-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@ static struct ctl_table ipv6_icmp_table_template[] = {
12061206
.extra1 = SYSCTL_ZERO,
12071207
.extra2 = SYSCTL_ONE,
12081208
},
1209-
{ },
12101209
};
12111210

12121211
struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)

net/ipv6/netfilter/nf_conntrack_reasm.c

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ static struct ctl_table nf_ct_frag6_sysctl_table[] = {
6262
.mode = 0644,
6363
.proc_handler = proc_doulongvec_minmax,
6464
},
65-
{ }
6665
};
6766

6867
static int nf_ct_frag6_sysctl_register(struct net *net)

net/ipv6/reassembly.c

-2
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ static struct ctl_table ip6_frags_ns_ctl_table[] = {
436436
.mode = 0644,
437437
.proc_handler = proc_dointvec_jiffies,
438438
},
439-
{ }
440439
};
441440

442441
/* secret interval has been deprecated */
@@ -449,7 +448,6 @@ static struct ctl_table ip6_frags_ctl_table[] = {
449448
.mode = 0644,
450449
.proc_handler = proc_dointvec_jiffies,
451450
},
452-
{ }
453451
};
454452

455453
static int __net_init ip6_frags_ns_sysctl_register(struct net *net)

net/ipv6/route.c

-5
Original file line numberDiff line numberDiff line change
@@ -6428,7 +6428,6 @@ static struct ctl_table ipv6_route_table_template[] = {
64286428
.extra1 = SYSCTL_ZERO,
64296429
.extra2 = SYSCTL_ONE,
64306430
},
6431-
{ }
64326431
};
64336432

64346433
struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
@@ -6452,10 +6451,6 @@ struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
64526451
table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
64536452
table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
64546453
table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6455-
6456-
/* Don't export sysctls to unprivileged users */
6457-
if (net->user_ns != &init_user_ns)
6458-
table[1].procname = NULL;
64596454
}
64606455

64616456
return table;

0 commit comments

Comments
 (0)