aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLi Zefan <lizefan@huawei.com>2014-02-25 06:28:44 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-02-25 10:37:52 -0500
commitfed95bab8d29b928fcf6225be72d37ded452e8a2 (patch)
tree45acbc72b0e58cc47d261b6346f99a5896dea85e
parent6d0abeca3242a88cab8232e4acd7e2bf088f3bc2 (diff)
sysfs: fix namespace refcnt leak
As mount() and kill_sb() is not a one-to-one match, we shoudn't get ns refcnt unconditionally in sysfs_mount(), and instead we should get the refcnt only when kernfs_mount() allocated a new superblock. v2: - Changed the name of the new argument, suggested by Tejun. - Made the argument optional, suggested by Tejun. v3: - Make the new argument as second-to-last arg, suggested by Tejun. Signed-off-by: Li Zefan <lizefan@huawei.com> Acked-by: Tejun Heo <tj@kernel.org> --- fs/kernfs/mount.c | 8 +++++++- fs/sysfs/mount.c | 5 +++-- include/linux/kernfs.h | 9 +++++---- 3 files changed, 15 insertions(+), 7 deletions(-) Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--fs/kernfs/mount.c8
-rw-r--r--fs/sysfs/mount.c5
-rw-r--r--include/linux/kernfs.h9
3 files changed, 15 insertions, 7 deletions
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index 0d6ce895a9ee..0f4152defe7b 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -94,6 +94,7 @@ const void *kernfs_super_ns(struct super_block *sb)
94 * @fs_type: file_system_type of the fs being mounted 94 * @fs_type: file_system_type of the fs being mounted
95 * @flags: mount flags specified for the mount 95 * @flags: mount flags specified for the mount
96 * @root: kernfs_root of the hierarchy being mounted 96 * @root: kernfs_root of the hierarchy being mounted
97 * @new_sb_created: tell the caller if we allocated a new superblock
97 * @ns: optional namespace tag of the mount 98 * @ns: optional namespace tag of the mount
98 * 99 *
99 * This is to be called from each kernfs user's file_system_type->mount() 100 * This is to be called from each kernfs user's file_system_type->mount()
@@ -104,7 +105,8 @@ const void *kernfs_super_ns(struct super_block *sb)
104 * The return value can be passed to the vfs layer verbatim. 105 * The return value can be passed to the vfs layer verbatim.
105 */ 106 */
106struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, 107struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
107 struct kernfs_root *root, const void *ns) 108 struct kernfs_root *root, bool *new_sb_created,
109 const void *ns)
108{ 110{
109 struct super_block *sb; 111 struct super_block *sb;
110 struct kernfs_super_info *info; 112 struct kernfs_super_info *info;
@@ -122,6 +124,10 @@ struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
122 kfree(info); 124 kfree(info);
123 if (IS_ERR(sb)) 125 if (IS_ERR(sb))
124 return ERR_CAST(sb); 126 return ERR_CAST(sb);
127
128 if (new_sb_created)
129 *new_sb_created = !sb->s_root;
130
125 if (!sb->s_root) { 131 if (!sb->s_root) {
126 error = kernfs_fill_super(sb); 132 error = kernfs_fill_super(sb);
127 if (error) { 133 if (error) {
diff --git a/fs/sysfs/mount.c b/fs/sysfs/mount.c
index 6211230814fd..3eaf5c6622eb 100644
--- a/fs/sysfs/mount.c
+++ b/fs/sysfs/mount.c
@@ -27,6 +27,7 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
27{ 27{
28 struct dentry *root; 28 struct dentry *root;
29 void *ns; 29 void *ns;
30 bool new_sb;
30 31
31 if (!(flags & MS_KERNMOUNT)) { 32 if (!(flags & MS_KERNMOUNT)) {
32 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type)) 33 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
@@ -37,8 +38,8 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
37 } 38 }
38 39
39 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); 40 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
40 root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns); 41 root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
41 if (IS_ERR(root)) 42 if (IS_ERR(root) || !new_sb)
42 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); 43 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
43 return root; 44 return root;
44} 45}
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 5be9f0228a3b..d267623c28cf 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -249,7 +249,8 @@ void kernfs_notify(struct kernfs_node *kn);
249 249
250const void *kernfs_super_ns(struct super_block *sb); 250const void *kernfs_super_ns(struct super_block *sb);
251struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, 251struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
252 struct kernfs_root *root, const void *ns); 252 struct kernfs_root *root, bool *new_sb_created,
253 const void *ns);
253void kernfs_kill_sb(struct super_block *sb); 254void kernfs_kill_sb(struct super_block *sb);
254 255
255void kernfs_init(void); 256void kernfs_init(void);
@@ -317,7 +318,7 @@ static inline const void *kernfs_super_ns(struct super_block *sb)
317 318
318static inline struct dentry * 319static inline struct dentry *
319kernfs_mount_ns(struct file_system_type *fs_type, int flags, 320kernfs_mount_ns(struct file_system_type *fs_type, int flags,
320 struct kernfs_root *root, const void *ns) 321 struct kernfs_root *root, bool *new_sb_created, const void *ns)
321{ return ERR_PTR(-ENOSYS); } 322{ return ERR_PTR(-ENOSYS); }
322 323
323static inline void kernfs_kill_sb(struct super_block *sb) { } 324static inline void kernfs_kill_sb(struct super_block *sb) { }
@@ -368,9 +369,9 @@ static inline int kernfs_remove_by_name(struct kernfs_node *parent,
368 369
369static inline struct dentry * 370static inline struct dentry *
370kernfs_mount(struct file_system_type *fs_type, int flags, 371kernfs_mount(struct file_system_type *fs_type, int flags,
371 struct kernfs_root *root) 372 struct kernfs_root *root, bool *new_sb_created)
372{ 373{
373 return kernfs_mount_ns(fs_type, flags, root, NULL); 374 return kernfs_mount_ns(fs_type, flags, root, new_sb_created, NULL);
374} 375}
375 376
376#endif /* __LINUX_KERNFS_H */ 377#endif /* __LINUX_KERNFS_H */