All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ns: move free_nsproxy() out of do_exit() path
@ 2012-07-13 11:48 ` Kirill A. Shutemov
  0 siblings, 0 replies; 22+ messages in thread
From: Kirill A. Shutemov @ 2012-07-13 11:48 UTC (permalink / raw)
  To: Andrew Morton, Serge Hallyn
  Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Dmitry V. Levin,
	Doug Ledford, Al Viro, KOSAKI Motohiro, Kirill A. Shutemov,
	Pavel Emelyanov

From: "Kirill A. Shutemov" <kirill.shutemov-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>

free_nsproxy() is too heavy to be on exit path. Let's free namespaces
asynchronously to not block exit_group() syscall.

Microbenchmark:

: #define _GNU_SOURCE
: #include <unistd.h>
: #include <sched.h>
: #include <stdlib.h>
: #include <sys/wait.h>
:
: int
: main(void)
: {
:       int i;
:       for (i = 0; i < 1024; i++) {
:               if (fork()) {
:                       wait(NULL);
:                       continue;
:               }
:               unshare(CLONE_NEWIPC);
:               exit(0);
:       }
:       return 0;
: }

Before the patch:

real    0m8.335s
user    0m0.000s
sys     0m0.265s

After:

real    0m0.569s
user    0m0.001s
sys     0m0.154s

The patch also fixes bug with free namespace without synchronize_rcu() through
put_nsproxy().

Signed-off-by: Kirill A. Shutemov <kirill.shutemov-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
Acked-by: Serge E. Hallyn <serge.hallyn-GeWIH/nMZzLQT0dZR+AlfA@public.gmane.org>
---

Serge, I've changed the patch a bit, but I hope I can reuse your Ack.

---
 include/linux/nsproxy.h |    1 +
 kernel/nsproxy.c        |   34 +++++++++++++++++++++++-----------
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/include/linux/nsproxy.h b/include/linux/nsproxy.h
index cc37a55..1d26be7 100644
--- a/include/linux/nsproxy.h
+++ b/include/linux/nsproxy.h
@@ -24,6 +24,7 @@ struct fs_struct;
  */
 struct nsproxy {
 	atomic_t count;
+	struct work_struct free_nsproxy_work;
 	struct uts_namespace *uts_ns;
 	struct ipc_namespace *ipc_ns;
 	struct mnt_namespace *mnt_ns;
diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
index b576f7f..ebc7d40 100644
--- a/kernel/nsproxy.c
+++ b/kernel/nsproxy.c
@@ -41,13 +41,17 @@ struct nsproxy init_nsproxy = {
 #endif
 };
 
+static void free_nsproxy_work(struct work_struct *work);
+
 static inline struct nsproxy *create_nsproxy(void)
 {
 	struct nsproxy *nsproxy;
 
 	nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
-	if (nsproxy)
+	if (nsproxy) {
 		atomic_set(&nsproxy->count, 1);
+		INIT_WORK(&nsproxy->free_nsproxy_work, free_nsproxy_work);
+	}
 	return nsproxy;
 }
 
@@ -166,6 +170,14 @@ out:
 
 void free_nsproxy(struct nsproxy *ns)
 {
+	/*
+	 * wait for others to get what they want from this nsproxy.
+	 *
+	 * cannot release this nsproxy via the call_rcu() since
+	 * put_mnt_ns() will want to sleep
+	 */
+	synchronize_rcu();
+
 	if (ns->mnt_ns)
 		put_mnt_ns(ns->mnt_ns);
 	if (ns->uts_ns)
@@ -178,6 +190,14 @@ void free_nsproxy(struct nsproxy *ns)
 	kmem_cache_free(nsproxy_cachep, ns);
 }
 
+static void free_nsproxy_work(struct work_struct *work)
+{
+	struct nsproxy *ns = container_of(work, struct nsproxy,
+			free_nsproxy_work);
+
+	free_nsproxy(ns);
+}
+
 /*
  * Called from unshare. Unshare all the namespaces part of nsproxy.
  * On success, returns the new nsproxy.
@@ -215,16 +235,8 @@ void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
 
 	rcu_assign_pointer(p->nsproxy, new);
 
-	if (ns && atomic_dec_and_test(&ns->count)) {
-		/*
-		 * wait for others to get what they want from this nsproxy.
-		 *
-		 * cannot release this nsproxy via the call_rcu() since
-		 * put_mnt_ns() will want to sleep
-		 */
-		synchronize_rcu();
-		free_nsproxy(ns);
-	}
+	if (ns && atomic_dec_and_test(&ns->count))
+		schedule_work(&ns->free_nsproxy_work);
 }
 
 void exit_task_namespaces(struct task_struct *p)
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2012-07-16 21:05 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-13 11:48 [PATCH] ns: move free_nsproxy() out of do_exit() path Kirill A. Shutemov
2012-07-13 11:48 ` Kirill A. Shutemov
     [not found] ` <1342180088-22647-1-git-send-email-kirill.shutemov-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2012-07-13 21:08   ` Andrew Morton
2012-07-13 21:08     ` Andrew Morton
     [not found]     ` <20120713140806.b3d0fda8.akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
2012-07-13 22:23       ` Kirill A. Shutemov
2012-07-13 22:23         ` Kirill A. Shutemov
2012-07-16 15:09       ` [PATCH v2] ns: do not block exit_task_namespaces() for a long time Kirill A. Shutemov
2012-07-16 15:09         ` Kirill A. Shutemov
2012-07-16 15:39         ` Myklebust, Trond
     [not found]           ` <1342453174.4648.6.camel-SyLVLa/KEI9HwK5hSS5vWB2eb7JE58TQ@public.gmane.org>
2012-07-16 16:39             ` Kirill A. Shutemov
2012-07-16 16:39               ` Kirill A. Shutemov
2012-07-16 16:53               ` Myklebust, Trond
2012-07-16 16:53                 ` Myklebust, Trond
2012-07-16 21:05         ` Kirill A. Shutemov
     [not found]         ` <1342451364-14787-1-git-send-email-kirill.shutemov-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2012-07-16 15:39           ` Myklebust, Trond
2012-07-16 16:53           ` Al Viro
2012-07-16 16:53             ` Al Viro
     [not found]             ` <20120716165301.GN31729-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
2012-07-16 17:16               ` Kirill A. Shutemov
2012-07-16 17:16                 ` Kirill A. Shutemov
     [not found]                 ` <20120716171634.GA21620-oKw7cIdHH8eLwutG50LtGA@public.gmane.org>
2012-07-16 17:22                   ` Al Viro
2012-07-16 17:22                     ` Al Viro
2012-07-16 21:05           ` Kirill A. Shutemov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.