public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v3] util, pasta: Add do_clone() wrapper around __clone2() and clone()
Date: Thu, 17 Nov 2022 10:00:32 +1100	[thread overview]
Message-ID: <Y3VrkCkY12sEEj/g@yekko> (raw)
In-Reply-To: <20221116163057.2637847-1-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3890 bytes --]

On Wed, Nov 16, 2022 at 05:30:57PM +0100, Stefano Brivio wrote:
> Spotted in Debian's buildd logs: on ia64, clone(2) is not available:
> the glibc wrapper is named __clone2() and it takes, additionally,
> the size of the stack area passed by the caller.
> 
> Add a do_clone() wrapper handling the different cases, and also
> taking care of pointing the child's stack in the middle of the
> allocated area: on PA-RISC (hppa), handled by clone(), the stack
> grows up, and on ia64 the stack grows down, but the register backing
> store grows up -- and I think it might be actually used here.
> 
> Suggested-by: David Gibson <david@gibson.dropbear.id.au>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>

Heh, my only comment on v2 was going to be that it's not a good idea
to name identifiers starting with _ - that's reserved for the system
library.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
> v3: Don't call it __clone(), it conflicts with glibc on static builds
> 
> v2: Add a wrapper instead of two __ia64__ ifdefs
> 
>  pasta.c | 10 +++++-----
>  util.c  | 21 +++++++++++++++++++++
>  util.h  |  8 +++++---
>  3 files changed, 31 insertions(+), 8 deletions(-)
> 
> diff --git a/pasta.c b/pasta.c
> index db86317..528f02a 100644
> --- a/pasta.c
> +++ b/pasta.c
> @@ -226,11 +226,11 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid,
>  		arg.argv = sh_argv;
>  	}
>  
> -	pasta_child_pid = clone(pasta_spawn_cmd,
> -				ns_fn_stack + sizeof(ns_fn_stack) / 2,
> -				CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET |
> -				CLONE_NEWUTS,
> -				(void *)&arg);
> +	pasta_child_pid = do_clone(pasta_spawn_cmd, ns_fn_stack,
> +				   sizeof(ns_fn_stack),
> +				   CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET |
> +				   CLONE_NEWUTS,
> +				   (void *)&arg);
>  
>  	if (pasta_child_pid == -1) {
>  		perror("clone");
> diff --git a/util.c b/util.c
> index be102e3..2125a67 100644
> --- a/util.c
> +++ b/util.c
> @@ -482,3 +482,24 @@ int write_file(const char *path, const char *buf)
>  	close(fd);
>  	return len == 0 ? 0 : -1;
>  }
> +
> +/**
> + * do_clone() - Wrapper of __clone2() for ia64, clone() for other architectures
> + * @fn:		Entry point for child
> + * @stack_area:	Stack area for child: we'll point callees to the middle of it
> + * @stack_size:	Total size of stack area, passed to callee divided by two
> + * @flags:	clone() system call flags
> + * @arg:	Argument to @fn
> + *
> + * Return: thread ID of child, -1 on failure
> + */
> +int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
> +	     void *arg)
> +{
> +#ifdef __ia64__
> +	return __clone2(fn, stack_area + stack_size / 2, stack_size / 2,
> +			flags, arg);
> +#else
> +	return clone(fn, stack_area + stack_size / 2, flags, arg);
> +#endif
> +}
> diff --git a/util.h b/util.h
> index 2d4e1ff..02b55a9 100644
> --- a/util.h
> +++ b/util.h
> @@ -81,13 +81,15 @@
>  	(((struct in_addr *)(a))->s_addr == ((struct in_addr *)b)->s_addr)
>  
>  #define NS_FN_STACK_SIZE	(RLIMIT_STACK_VAL * 1024 / 8)
> +int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
> +	     void *arg);
>  #define NS_CALL(fn, arg)						\
>  	do {								\
>  		char ns_fn_stack[NS_FN_STACK_SIZE];			\
>  									\
> -		clone((fn), ns_fn_stack + sizeof(ns_fn_stack) / 2,	\
> -		      CLONE_VM | CLONE_VFORK | CLONE_FILES | SIGCHLD,	\
> -		      (void *)(arg));					\
> +		do_clone((fn), ns_fn_stack, sizeof(ns_fn_stack),	\
> +			 CLONE_VM | CLONE_VFORK | CLONE_FILES | SIGCHLD,\
> +			 (void *)(arg));				\
>  	} while (0)
>  
>  #if __BYTE_ORDER == __BIG_ENDIAN

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2022-11-16 23:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-16 16:30 [PATCH v3] util, pasta: Add do_clone() wrapper around __clone2() and clone() Stefano Brivio
2022-11-16 23:00 ` David Gibson [this message]
2022-11-17  7:24   ` Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y3VrkCkY12sEEj/g@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).