Skip to content

Commit e1eb26f

Browse files
giuseppetorvalds
authored andcommitted
ipc/namespace.c: use a work queue to free_ipc
the reason is to avoid a delay caused by the synchronize_rcu() call in kern_umount() when the mqueue mount is freed. the code: #define _GNU_SOURCE #include <sched.h> #include <error.h> #include <errno.h> #include <stdlib.h> int main() { int i; for (i = 0; i < 1000; i++) if (unshare(CLONE_NEWIPC) < 0) error(EXIT_FAILURE, errno, "unshare"); } goes from Command being timed: "./ipc-namespace" User time (seconds): 0.00 System time (seconds): 0.06 Percent of CPU this job got: 0% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.05 to Command being timed: "./ipc-namespace" User time (seconds): 0.00 System time (seconds): 0.02 Percent of CPU this job got: 96% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.03 Signed-off-by: Giuseppe Scrivano <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Paul E. McKenney <[email protected]> Reviewed-by: Waiman Long <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: Manfred Spraul <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4b78e20 commit e1eb26f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

include/linux/ipc_namespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ struct ipc_namespace {
6868
struct user_namespace *user_ns;
6969
struct ucounts *ucounts;
7070

71+
struct llist_node mnt_llist;
72+
7173
struct ns_common ns;
7274
} __randomize_layout;
7375

ipc/namespace.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
117117

118118
static void free_ipc_ns(struct ipc_namespace *ns)
119119
{
120+
/* mq_put_mnt() waits for a grace period as kern_unmount()
121+
* uses synchronize_rcu().
122+
*/
123+
mq_put_mnt(ns);
120124
sem_exit_ns(ns);
121125
msg_exit_ns(ns);
122126
shm_exit_ns(ns);
@@ -127,6 +131,21 @@ static void free_ipc_ns(struct ipc_namespace *ns)
127131
kfree(ns);
128132
}
129133

134+
static LLIST_HEAD(free_ipc_list);
135+
static void free_ipc(struct work_struct *unused)
136+
{
137+
struct llist_node *node = llist_del_all(&free_ipc_list);
138+
struct ipc_namespace *n, *t;
139+
140+
llist_for_each_entry_safe(n, t, node, mnt_llist)
141+
free_ipc_ns(n);
142+
}
143+
144+
/*
145+
* The work queue is used to avoid the cost of synchronize_rcu in kern_unmount.
146+
*/
147+
static DECLARE_WORK(free_ipc_work, free_ipc);
148+
130149
/*
131150
* put_ipc_ns - drop a reference to an ipc namespace.
132151
* @ns: the namespace to put
@@ -148,8 +167,9 @@ void put_ipc_ns(struct ipc_namespace *ns)
148167
if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
149168
mq_clear_sbinfo(ns);
150169
spin_unlock(&mq_lock);
151-
mq_put_mnt(ns);
152-
free_ipc_ns(ns);
170+
171+
if (llist_add(&ns->mnt_llist, &free_ipc_list))
172+
schedule_work(&free_ipc_work);
153173
}
154174
}
155175

0 commit comments

Comments
 (0)