Skip to content

Commit a685e08

Browse files
author
Al Viro
committed
Delay struct net freeing while there's a sysfs instance refering to it
* new refcount in struct net, controlling actual freeing of the memory * new method in kobj_ns_type_operations (->drop_ns()) * ->current_ns() semantics change - it's supposed to be followed by corresponding ->drop_ns(). For struct net in case of CONFIG_NET_NS it bumps the new refcount; net_drop_ns() decrements it and calls net_free() if the last reference has been dropped. Method renamed to ->grab_current_ns(). * old net_free() callers call net_drop_ns() instead. * sysfs_exit_ns() is gone, along with a large part of callchain leading to it; now that the references stored in ->ns[...] stay valid we do not need to hunt them down and replace them with NULL. That fixes problems in sysfs_lookup() and sysfs_readdir(), along with getting rid of sb->s_instances abuse. Note that struct net *shutdown* logics has not changed - net_cleanup() is called exactly when it used to be called. The only thing postponed by having a sysfs instance refering to that struct net is actual freeing of memory occupied by struct net. Signed-off-by: Al Viro <[email protected]>
1 parent dde194a commit a685e08

File tree

8 files changed

+55
-72
lines changed

8 files changed

+55
-72
lines changed

fs/sysfs/mount.c

+11-26
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ static int sysfs_set_super(struct super_block *sb, void *data)
9595
return error;
9696
}
9797

98+
static void free_sysfs_super_info(struct sysfs_super_info *info)
99+
{
100+
int type;
101+
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++)
102+
kobj_ns_drop(type, info->ns[type]);
103+
kfree(info);
104+
}
105+
98106
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
99107
int flags, const char *dev_name, void *data)
100108
{
@@ -108,11 +116,11 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
108116
return ERR_PTR(-ENOMEM);
109117

110118
for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++)
111-
info->ns[type] = kobj_ns_current(type);
119+
info->ns[type] = kobj_ns_grab_current(type);
112120

113121
sb = sget(fs_type, sysfs_test_super, sysfs_set_super, info);
114122
if (IS_ERR(sb) || sb->s_fs_info != info)
115-
kfree(info);
123+
free_sysfs_super_info(info);
116124
if (IS_ERR(sb))
117125
return ERR_CAST(sb);
118126
if (!sb->s_root) {
@@ -131,12 +139,11 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
131139
static void sysfs_kill_sb(struct super_block *sb)
132140
{
133141
struct sysfs_super_info *info = sysfs_info(sb);
134-
135142
/* Remove the superblock from fs_supers/s_instances
136143
* so we can't find it, before freeing sysfs_super_info.
137144
*/
138145
kill_anon_super(sb);
139-
kfree(info);
146+
free_sysfs_super_info(info);
140147
}
141148

142149
static struct file_system_type sysfs_fs_type = {
@@ -145,28 +152,6 @@ static struct file_system_type sysfs_fs_type = {
145152
.kill_sb = sysfs_kill_sb,
146153
};
147154

148-
void sysfs_exit_ns(enum kobj_ns_type type, const void *ns)
149-
{
150-
struct super_block *sb;
151-
152-
mutex_lock(&sysfs_mutex);
153-
spin_lock(&sb_lock);
154-
list_for_each_entry(sb, &sysfs_fs_type.fs_supers, s_instances) {
155-
struct sysfs_super_info *info = sysfs_info(sb);
156-
/*
157-
* If we see a superblock on the fs_supers/s_instances
158-
* list the unmount has not completed and sb->s_fs_info
159-
* points to a valid struct sysfs_super_info.
160-
*/
161-
/* Ignore superblocks with the wrong ns */
162-
if (info->ns[type] != ns)
163-
continue;
164-
info->ns[type] = NULL;
165-
}
166-
spin_unlock(&sb_lock);
167-
mutex_unlock(&sysfs_mutex);
168-
}
169-
170155
int __init sysfs_init(void)
171156
{
172157
int err = -ENOMEM;

fs/sysfs/sysfs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct sysfs_addrm_cxt {
136136
* instance).
137137
*/
138138
struct sysfs_super_info {
139-
const void *ns[KOBJ_NS_TYPES];
139+
void *ns[KOBJ_NS_TYPES];
140140
};
141141
#define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info))
142142
extern struct sysfs_dirent sysfs_root;

include/linux/kobject_ns.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,27 @@ enum kobj_ns_type {
3232

3333
/*
3434
* Callbacks so sysfs can determine namespaces
35-
* @current_ns: return calling task's namespace
35+
* @grab_current_ns: return a new reference to calling task's namespace
3636
* @netlink_ns: return namespace to which a sock belongs (right?)
3737
* @initial_ns: return the initial namespace (i.e. init_net_ns)
38+
* @drop_ns: drops a reference to namespace
3839
*/
3940
struct kobj_ns_type_operations {
4041
enum kobj_ns_type type;
41-
const void *(*current_ns)(void);
42+
void *(*grab_current_ns)(void);
4243
const void *(*netlink_ns)(struct sock *sk);
4344
const void *(*initial_ns)(void);
45+
void (*drop_ns)(void *);
4446
};
4547

4648
int kobj_ns_type_register(const struct kobj_ns_type_operations *ops);
4749
int kobj_ns_type_registered(enum kobj_ns_type type);
4850
const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent);
4951
const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj);
5052

51-
const void *kobj_ns_current(enum kobj_ns_type type);
53+
void *kobj_ns_grab_current(enum kobj_ns_type type);
5254
const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk);
5355
const void *kobj_ns_initial(enum kobj_ns_type type);
54-
void kobj_ns_exit(enum kobj_ns_type type, const void *ns);
56+
void kobj_ns_drop(enum kobj_ns_type type, void *ns);
5557

5658
#endif /* _LINUX_KOBJECT_NS_H */

include/linux/sysfs.h

-7
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
177177
struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
178178
void sysfs_put(struct sysfs_dirent *sd);
179179

180-
/* Called to clear a ns tag when it is no longer valid */
181-
void sysfs_exit_ns(enum kobj_ns_type type, const void *tag);
182-
183180
int __must_check sysfs_init(void);
184181

185182
#else /* CONFIG_SYSFS */
@@ -338,10 +335,6 @@ static inline void sysfs_put(struct sysfs_dirent *sd)
338335
{
339336
}
340337

341-
static inline void sysfs_exit_ns(int type, const void *tag)
342-
{
343-
}
344-
345338
static inline int __must_check sysfs_init(void)
346339
{
347340
return 0;

include/net/net_namespace.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ struct netns_ipvs;
3535
#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
3636

3737
struct net {
38+
atomic_t passive; /* To decided when the network
39+
* namespace should be freed.
40+
*/
3841
atomic_t count; /* To decided when the network
39-
* namespace should be freed.
42+
* namespace should be shut down.
4043
*/
4144
#ifdef NETNS_REFCNT_DEBUG
4245
atomic_t use_count; /* To track references we
@@ -154,6 +157,9 @@ int net_eq(const struct net *net1, const struct net *net2)
154157
{
155158
return net1 == net2;
156159
}
160+
161+
extern void net_drop_ns(void *);
162+
157163
#else
158164

159165
static inline struct net *get_net(struct net *net)
@@ -175,6 +181,8 @@ int net_eq(const struct net *net1, const struct net *net2)
175181
{
176182
return 1;
177183
}
184+
185+
#define net_drop_ns NULL
178186
#endif
179187

180188

lib/kobject.c

+9-17
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,14 @@ const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
948948
}
949949

950950

951-
const void *kobj_ns_current(enum kobj_ns_type type)
951+
void *kobj_ns_grab_current(enum kobj_ns_type type)
952952
{
953-
const void *ns = NULL;
953+
void *ns = NULL;
954954

955955
spin_lock(&kobj_ns_type_lock);
956956
if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
957957
kobj_ns_ops_tbl[type])
958-
ns = kobj_ns_ops_tbl[type]->current_ns();
958+
ns = kobj_ns_ops_tbl[type]->grab_current_ns();
959959
spin_unlock(&kobj_ns_type_lock);
960960

961961
return ns;
@@ -987,23 +987,15 @@ const void *kobj_ns_initial(enum kobj_ns_type type)
987987
return ns;
988988
}
989989

990-
/*
991-
* kobj_ns_exit - invalidate a namespace tag
992-
*
993-
* @type: the namespace type (i.e. KOBJ_NS_TYPE_NET)
994-
* @ns: the actual namespace being invalidated
995-
*
996-
* This is called when a tag is no longer valid. For instance,
997-
* when a network namespace exits, it uses this helper to
998-
* make sure no sb's sysfs_info points to the now-invalidated
999-
* netns.
1000-
*/
1001-
void kobj_ns_exit(enum kobj_ns_type type, const void *ns)
990+
void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1002991
{
1003-
sysfs_exit_ns(type, ns);
992+
spin_lock(&kobj_ns_type_lock);
993+
if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
994+
kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
995+
kobj_ns_ops_tbl[type]->drop_ns(ns);
996+
spin_unlock(&kobj_ns_type_lock);
1004997
}
1005998

1006-
1007999
EXPORT_SYMBOL(kobject_get);
10081000
EXPORT_SYMBOL(kobject_put);
10091001
EXPORT_SYMBOL(kobject_del);

net/core/net-sysfs.c

+9-14
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,14 @@ static void remove_queue_kobjects(struct net_device *net)
11791179
#endif
11801180
}
11811181

1182-
static const void *net_current_ns(void)
1182+
static void *net_grab_current_ns(void)
11831183
{
1184-
return current->nsproxy->net_ns;
1184+
struct net *ns = current->nsproxy->net_ns;
1185+
#ifdef CONFIG_NET_NS
1186+
if (ns)
1187+
atomic_inc(&ns->passive);
1188+
#endif
1189+
return ns;
11851190
}
11861191

11871192
static const void *net_initial_ns(void)
@@ -1196,22 +1201,13 @@ static const void *net_netlink_ns(struct sock *sk)
11961201

11971202
struct kobj_ns_type_operations net_ns_type_operations = {
11981203
.type = KOBJ_NS_TYPE_NET,
1199-
.current_ns = net_current_ns,
1204+
.grab_current_ns = net_grab_current_ns,
12001205
.netlink_ns = net_netlink_ns,
12011206
.initial_ns = net_initial_ns,
1207+
.drop_ns = net_drop_ns,
12021208
};
12031209
EXPORT_SYMBOL_GPL(net_ns_type_operations);
12041210

1205-
static void net_kobj_ns_exit(struct net *net)
1206-
{
1207-
kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
1208-
}
1209-
1210-
static struct pernet_operations kobj_net_ops = {
1211-
.exit = net_kobj_ns_exit,
1212-
};
1213-
1214-
12151211
#ifdef CONFIG_HOTPLUG
12161212
static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
12171213
{
@@ -1339,6 +1335,5 @@ EXPORT_SYMBOL(netdev_class_remove_file);
13391335
int netdev_kobject_init(void)
13401336
{
13411337
kobj_ns_type_register(&net_ns_type_operations);
1342-
register_pernet_subsys(&kobj_net_ops);
13431338
return class_register(&net_class);
13441339
}

net/core/net_namespace.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static __net_init int setup_net(struct net *net)
128128
LIST_HEAD(net_exit_list);
129129

130130
atomic_set(&net->count, 1);
131+
atomic_set(&net->passive, 1);
131132

132133
#ifdef NETNS_REFCNT_DEBUG
133134
atomic_set(&net->use_count, 0);
@@ -210,6 +211,13 @@ static void net_free(struct net *net)
210211
kmem_cache_free(net_cachep, net);
211212
}
212213

214+
void net_drop_ns(void *p)
215+
{
216+
struct net *ns = p;
217+
if (ns && atomic_dec_and_test(&ns->passive))
218+
net_free(ns);
219+
}
220+
213221
struct net *copy_net_ns(unsigned long flags, struct net *old_net)
214222
{
215223
struct net *net;
@@ -230,7 +238,7 @@ struct net *copy_net_ns(unsigned long flags, struct net *old_net)
230238
}
231239
mutex_unlock(&net_mutex);
232240
if (rv < 0) {
233-
net_free(net);
241+
net_drop_ns(net);
234242
return ERR_PTR(rv);
235243
}
236244
return net;
@@ -286,7 +294,7 @@ static void cleanup_net(struct work_struct *work)
286294
/* Finally it is safe to free my network namespace structure */
287295
list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
288296
list_del_init(&net->exit_list);
289-
net_free(net);
297+
net_drop_ns(net);
290298
}
291299
}
292300
static DECLARE_WORK(net_cleanup_work, cleanup_net);

0 commit comments

Comments
 (0)