Skip to content

Commit fed95ba

Browse files
lizf-osgregkh
authored andcommitted
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>
1 parent 6d0abec commit fed95ba

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

fs/kernfs/mount.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const void *kernfs_super_ns(struct super_block *sb)
9494
* @fs_type: file_system_type of the fs being mounted
9595
* @flags: mount flags specified for the mount
9696
* @root: kernfs_root of the hierarchy being mounted
97+
* @new_sb_created: tell the caller if we allocated a new superblock
9798
* @ns: optional namespace tag of the mount
9899
*
99100
* 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)
104105
* The return value can be passed to the vfs layer verbatim.
105106
*/
106107
struct 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)
108110
{
109111
struct super_block *sb;
110112
struct kernfs_super_info *info;
@@ -122,6 +124,10 @@ struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
122124
kfree(info);
123125
if (IS_ERR(sb))
124126
return ERR_CAST(sb);
127+
128+
if (new_sb_created)
129+
*new_sb_created = !sb->s_root;
130+
125131
if (!sb->s_root) {
126132
error = kernfs_fill_super(sb);
127133
if (error) {

fs/sysfs/mount.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
2727
{
2828
struct dentry *root;
2929
void *ns;
30+
bool new_sb;
3031

3132
if (!(flags & MS_KERNMOUNT)) {
3233
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,
3738
}
3839

3940
ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
40-
root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
41-
if (IS_ERR(root))
41+
root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
42+
if (IS_ERR(root) || !new_sb)
4243
kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
4344
return root;
4445
}

include/linux/kernfs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ void kernfs_notify(struct kernfs_node *kn);
249249

250250
const void *kernfs_super_ns(struct super_block *sb);
251251
struct 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);
253254
void kernfs_kill_sb(struct super_block *sb);
254255

255256
void kernfs_init(void);
@@ -317,7 +318,7 @@ static inline const void *kernfs_super_ns(struct super_block *sb)
317318

318319
static inline struct dentry *
319320
kernfs_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)
321322
{ return ERR_PTR(-ENOSYS); }
322323

323324
static inline void kernfs_kill_sb(struct super_block *sb) { }
@@ -368,9 +369,9 @@ static inline int kernfs_remove_by_name(struct kernfs_node *parent,
368369

369370
static inline struct dentry *
370371
kernfs_mount(struct file_system_type *fs_type, int flags,
371-
struct kernfs_root *root)
372+
struct kernfs_root *root, bool *new_sb_created)
372373
{
373-
return kernfs_mount_ns(fs_type, flags, root, NULL);
374+
return kernfs_mount_ns(fs_type, flags, root, new_sb_created, NULL);
374375
}
375376

376377
#endif /* __LINUX_KERNFS_H */

0 commit comments

Comments
 (0)