Skip to content

Commit 4665079

Browse files
xemulDavid S. Miller
authored andcommitted
[NETNS]: Move some code into __init section when CONFIG_NET_NS=n
With the net namespaces many code leaved the __init section, thus making the kernel occupy more memory than it did before. Since we have a config option that prohibits the namespace creation, the functions that initialize/finalize some netns stuff are simply not needed and can be freed after the boot. Currently, this is almost not noticeable, since few calls are no longer in __init, but when the namespaces will be merged it will be possible to free more code. I propose to use the __net_init, __net_exit and __net_initdata "attributes" for functions/variables that are not used if the CONFIG_NET_NS is not set to save more space in memory. The exiting functions cannot just reside in the __exit section, as noticed by David, since the init section will have references on it and the compilation will fail due to modpost checks. These references can exist, since the init namespace never dies and the exit callbacks are never called. So I introduce the __exit_refok attribute just like it is already done with the __init_refok. Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d62a38d commit 4665079

8 files changed

Lines changed: 32 additions & 21 deletions

File tree

‎drivers/net/loopback.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static void loopback_setup(struct net_device *dev)
250250
}
251251

252252
/* Setup and register the loopback device. */
253-
static int loopback_net_init(struct net *net)
253+
static __net_init int loopback_net_init(struct net *net)
254254
{
255255
struct net_device *dev;
256256
int err;
@@ -278,14 +278,14 @@ static int loopback_net_init(struct net *net)
278278
goto out;
279279
}
280280

281-
static void loopback_net_exit(struct net *net)
281+
static __net_exit void loopback_net_exit(struct net *net)
282282
{
283283
struct net_device *dev = net->loopback_dev;
284284

285285
unregister_netdev(dev);
286286
}
287287

288-
static struct pernet_operations loopback_net_ops = {
288+
static struct pernet_operations __net_initdata loopback_net_ops = {
289289
.init = loopback_net_init,
290290
.exit = loopback_net_exit,
291291
};

‎fs/proc/proc_net.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static struct inode_operations proc_net_dir_inode_operations = {
140140
.setattr = proc_net_setattr,
141141
};
142142

143-
static int proc_net_ns_init(struct net *net)
143+
static __net_init int proc_net_ns_init(struct net *net)
144144
{
145145
struct proc_dir_entry *root, *netd, *net_statd;
146146
int err;
@@ -178,19 +178,19 @@ static int proc_net_ns_init(struct net *net)
178178
goto out;
179179
}
180180

181-
static void proc_net_ns_exit(struct net *net)
181+
static __net_exit void proc_net_ns_exit(struct net *net)
182182
{
183183
remove_proc_entry("stat", net->proc_net);
184184
remove_proc_entry("net", net->proc_net_root);
185185
kfree(net->proc_net_root);
186186
}
187187

188-
struct pernet_operations proc_net_ns_ops = {
188+
struct pernet_operations __net_initdata proc_net_ns_ops = {
189189
.init = proc_net_ns_init,
190190
.exit = proc_net_ns_exit,
191191
};
192192

193-
int proc_net_init(void)
193+
int __init proc_net_init(void)
194194
{
195195
proc_net_shadow = proc_mkdir("net", NULL);
196196
proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;

‎include/linux/init.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* The markers follow same syntax rules as __init / __initdata. */
5858
#define __init_refok noinline __attribute__ ((__section__ (".text.init.refok")))
5959
#define __initdata_refok __attribute__ ((__section__ (".data.init.refok")))
60+
#define __exit_refok noinline __attribute__ ((__section__ (".exit.text.refok")))
6061

6162
#ifdef MODULE
6263
#define __exit __attribute__ ((__section__(".exit.text"))) __cold

‎include/net/net_namespace.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ static inline void release_net(struct net *net)
9999
#define for_each_net(VAR) \
100100
list_for_each_entry(VAR, &net_namespace_list, list)
101101

102+
#ifdef CONFIG_NET_NS
103+
#define __net_init
104+
#define __net_exit
105+
#define __net_initdata
106+
#else
107+
#define __net_init __init
108+
#define __net_exit __exit_refok
109+
#define __net_initdata __initdata
110+
#endif
102111

103112
struct pernet_operations {
104113
struct list_head list;

‎net/core/dev.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,7 @@ static const struct file_operations ptype_seq_fops = {
26112611
};
26122612

26132613

2614-
static int dev_proc_net_init(struct net *net)
2614+
static int __net_init dev_proc_net_init(struct net *net)
26152615
{
26162616
int rc = -ENOMEM;
26172617

@@ -2636,7 +2636,7 @@ static int dev_proc_net_init(struct net *net)
26362636
goto out;
26372637
}
26382638

2639-
static void dev_proc_net_exit(struct net *net)
2639+
static void __net_exit dev_proc_net_exit(struct net *net)
26402640
{
26412641
wext_proc_exit(net);
26422642

@@ -2645,7 +2645,7 @@ static void dev_proc_net_exit(struct net *net)
26452645
proc_net_remove(net, "dev");
26462646
}
26472647

2648-
static struct pernet_operations dev_proc_ops = {
2648+
static struct pernet_operations __net_initdata dev_proc_ops = {
26492649
.init = dev_proc_net_init,
26502650
.exit = dev_proc_net_exit,
26512651
};
@@ -4278,7 +4278,7 @@ static struct hlist_head *netdev_create_hash(void)
42784278
}
42794279

42804280
/* Initialize per network namespace state */
4281-
static int netdev_init(struct net *net)
4281+
static int __net_init netdev_init(struct net *net)
42824282
{
42834283
INIT_LIST_HEAD(&net->dev_base_head);
42844284
rwlock_init(&dev_base_lock);
@@ -4299,18 +4299,18 @@ static int netdev_init(struct net *net)
42994299
return -ENOMEM;
43004300
}
43014301

4302-
static void netdev_exit(struct net *net)
4302+
static void __net_exit netdev_exit(struct net *net)
43034303
{
43044304
kfree(net->dev_name_head);
43054305
kfree(net->dev_index_head);
43064306
}
43074307

4308-
static struct pernet_operations netdev_net_ops = {
4308+
static struct pernet_operations __net_initdata netdev_net_ops = {
43094309
.init = netdev_init,
43104310
.exit = netdev_exit,
43114311
};
43124312

4313-
static void default_device_exit(struct net *net)
4313+
static void __net_exit default_device_exit(struct net *net)
43144314
{
43154315
struct net_device *dev, *next;
43164316
/*
@@ -4336,7 +4336,7 @@ static void default_device_exit(struct net *net)
43364336
rtnl_unlock();
43374337
}
43384338

4339-
static struct pernet_operations default_device_ops = {
4339+
static struct pernet_operations __net_initdata default_device_ops = {
43404340
.exit = default_device_exit,
43414341
};
43424342

‎net/core/dev_mcast.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,19 @@ static const struct file_operations dev_mc_seq_fops = {
273273

274274
#endif
275275

276-
static int dev_mc_net_init(struct net *net)
276+
static int __net_init dev_mc_net_init(struct net *net)
277277
{
278278
if (!proc_net_fops_create(net, "dev_mcast", 0, &dev_mc_seq_fops))
279279
return -ENOMEM;
280280
return 0;
281281
}
282282

283-
static void dev_mc_net_exit(struct net *net)
283+
static void __net_exit dev_mc_net_exit(struct net *net)
284284
{
285285
proc_net_remove(net, "dev_mcast");
286286
}
287287

288-
static struct pernet_operations dev_mc_net_ops = {
288+
static struct pernet_operations __net_initdata dev_mc_net_ops = {
289289
.init = dev_mc_net_init,
290290
.exit = dev_mc_net_exit,
291291
};

‎net/netlink/af_netlink.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ static struct net_proto_family netlink_family_ops = {
19241924
.owner = THIS_MODULE, /* for consistency 8) */
19251925
};
19261926

1927-
static int netlink_net_init(struct net *net)
1927+
static int __net_init netlink_net_init(struct net *net)
19281928
{
19291929
#ifdef CONFIG_PROC_FS
19301930
if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
@@ -1933,14 +1933,14 @@ static int netlink_net_init(struct net *net)
19331933
return 0;
19341934
}
19351935

1936-
static void netlink_net_exit(struct net *net)
1936+
static void __net_exit netlink_net_exit(struct net *net)
19371937
{
19381938
#ifdef CONFIG_PROC_FS
19391939
proc_net_remove(net, "netlink");
19401940
#endif
19411941
}
19421942

1943-
static struct pernet_operations netlink_net_ops = {
1943+
static struct pernet_operations __net_initdata netlink_net_ops = {
19441944
.init = netlink_net_init,
19451945
.exit = netlink_net_exit,
19461946
};

‎scripts/mod/modpost.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ static int secref_whitelist(const char *modname, const char *tosec,
709709

710710
/* Check for pattern 0 */
711711
if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) ||
712+
(strncmp(fromsec, ".exit.text.refok", strlen(".exit.text.refok")) == 0) ||
712713
(strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0))
713714
return 1;
714715

0 commit comments

Comments
 (0)