[CRIU] [PATCH v3 1/6] ns: Alloc child stack dynamically in create_user_ns_hierarhy_fn()

Kirill Tkhai ktkhai at virtuozzo.com
Sat Apr 1 04:37:19 PDT 2017


This will be used in next patch.

Also, check for MAP_FAILED istead of NULL before munmap().

v3: New

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/namespaces.c |   19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/criu/namespaces.c b/criu/namespaces.c
index 6637abcb..6278267f 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2175,9 +2175,10 @@ struct ns_arg {
 
 static int create_user_ns_hierarhy_fn(void *in_arg)
 {
-	char stack[128] __stack_aligned__;
-	struct ns_arg *arg = NULL, *p_arg = in_arg;
+	struct ns_arg *arg, *p_arg = in_arg;
 	futex_t *p_futex = NULL, *futex = NULL;
+	size_t map_size = 2 * 1024 * 1024;
+	void *map = MAP_FAILED, *stack;
 	int status, fd, ret = -1;
 	struct ns_id *me, *child;
 	pid_t pid = -1;
@@ -2213,18 +2214,20 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
 		}
 	}
 
-	arg = mmap(NULL, sizeof(*arg), PROT_WRITE | PROT_READ, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-	if (arg == MAP_FAILED) {
-		pr_perror("Failed to mmap arg");
+	map = mmap(NULL, map_size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	if (map == MAP_FAILED) {
+		pr_perror("Failed to mmap");
 		goto out;
 	}
+	arg = map + map_size - sizeof(*arg);
+	stack = ((void *)arg) - sizeof(unsigned long);
 	futex = &arg->futex;
 
 	list_for_each_entry(child, &me->children, siblings) {
 		arg->me = child;
 		futex_init(futex);
 
-		pid = clone(create_user_ns_hierarhy_fn, stack + 128, CLONE_NEWUSER | CLONE_FILES | SIGCHLD, arg);
+		pid = clone(create_user_ns_hierarhy_fn, stack, CLONE_NEWUSER | CLONE_FILES | SIGCHLD, arg);
 		if (pid < 0) {
 			pr_perror("Can't clone");
 			goto out;
@@ -2249,8 +2252,8 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
 out:
 	if (p_futex)
 		futex_set_and_wake(p_futex, ret ? NS__ERROR : NS__RESTORED);
-	if (arg)
-		munmap(arg, sizeof(*arg));
+	if (map != MAP_FAILED)
+		munmap(map, map_size);
 	return ret ? 1 : 0;
 }
 



More information about the CRIU mailing list