Skip to content

Commit

Permalink
ipc,namespace: batch free ipc_namespace structures
Browse files Browse the repository at this point in the history
Instead of waiting for an RCU grace period between each ipc_namespace
structure that is being freed, wait an RCU grace period for every batch
of ipc_namespace structures.

Thanks to Al Viro for the suggestion of the helper function.

This speeds up the run time of the test case that allocates ipc_namespaces
in a loop from 6 minutes, to a little over 1 second:

real	0m1.192s
user	0m0.038s
sys	0m1.152s

Signed-off-by: Rik van Riel <[email protected]>
Reported-by: Chris Mason <[email protected]>
Tested-by: Giuseppe Scrivano <[email protected]>
Suggested-by: Al Viro <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
rikvanriel authored and Al Viro committed Jan 28, 2023
1 parent a80c4ad commit da27f79
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
18 changes: 14 additions & 4 deletions fs/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,17 @@ struct vfsmount *mntget(struct vfsmount *mnt)
}
EXPORT_SYMBOL(mntget);

/*
* Make a mount point inaccessible to new lookups.
* Because there may still be current users, the caller MUST WAIT
* for an RCU grace period before destroying the mount point.
*/
void mnt_make_shortterm(struct vfsmount *mnt)
{
if (mnt)
real_mount(mnt)->mnt_ns = NULL;
}

/**
* path_is_mountpoint() - Check if path is a mount in the current namespace.
* @path: path to check
Expand Down Expand Up @@ -4573,8 +4584,8 @@ EXPORT_SYMBOL_GPL(kern_mount);
void kern_unmount(struct vfsmount *mnt)
{
/* release long term mount so mount point can be released */
if (!IS_ERR_OR_NULL(mnt)) {
real_mount(mnt)->mnt_ns = NULL;
if (!IS_ERR(mnt)) {
mnt_make_shortterm(mnt);
synchronize_rcu(); /* yecchhh... */
mntput(mnt);
}
Expand All @@ -4586,8 +4597,7 @@ void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
unsigned int i;

for (i = 0; i < num; i++)
if (mnt[i])
real_mount(mnt[i])->mnt_ns = NULL;
mnt_make_shortterm(mnt[i]);
synchronize_rcu_expedited();
for (i = 0; i < num; i++)
mntput(mnt[i]);
Expand Down
1 change: 1 addition & 0 deletions include/linux/mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern void mnt_drop_write(struct vfsmount *mnt);
extern void mnt_drop_write_file(struct file *file);
extern void mntput(struct vfsmount *mnt);
extern struct vfsmount *mntget(struct vfsmount *mnt);
extern void mnt_make_shortterm(struct vfsmount *mnt);
extern struct vfsmount *mnt_clone_internal(const struct path *path);
extern bool __mnt_is_readonly(struct vfsmount *mnt);
extern bool mnt_may_suid(struct vfsmount *mnt);
Expand Down
5 changes: 0 additions & 5 deletions ipc/mqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -1709,11 +1709,6 @@ void mq_clear_sbinfo(struct ipc_namespace *ns)
ns->mq_mnt->mnt_sb->s_fs_info = NULL;
}

void mq_put_mnt(struct ipc_namespace *ns)
{
kern_unmount(ns->mq_mnt);
}

static int __init init_mqueue_fs(void)
{
int error;
Expand Down
13 changes: 10 additions & 3 deletions ipc/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,

static void free_ipc_ns(struct ipc_namespace *ns)
{
/* mq_put_mnt() waits for a grace period as kern_unmount()
* uses synchronize_rcu().
/*
* Caller needs to wait for an RCU grace period to have passed
* after making the mount point inaccessible to new accesses.
*/
mq_put_mnt(ns);
mntput(ns->mq_mnt);
sem_exit_ns(ns);
msg_exit_ns(ns);
shm_exit_ns(ns);
Expand All @@ -168,6 +169,12 @@ static void free_ipc(struct work_struct *unused)
struct llist_node *node = llist_del_all(&free_ipc_list);
struct ipc_namespace *n, *t;

llist_for_each_entry_safe(n, t, node, mnt_llist)
mnt_make_shortterm(n->mq_mnt);

/* Wait for any last users to have gone away. */
synchronize_rcu();

llist_for_each_entry_safe(n, t, node, mnt_llist)
free_ipc_ns(n);
}
Expand Down
2 changes: 0 additions & 2 deletions ipc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ struct pid_namespace;

#ifdef CONFIG_POSIX_MQUEUE
extern void mq_clear_sbinfo(struct ipc_namespace *ns);
extern void mq_put_mnt(struct ipc_namespace *ns);
#else
static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
static inline void mq_put_mnt(struct ipc_namespace *ns) { }
#endif

#ifdef CONFIG_SYSVIPC
Expand Down

0 comments on commit da27f79

Please sign in to comment.