Date: Fri, 17 Jun 2022 19:38:01 GMT From: Dmitry Chagin <dchagin@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 8fb3f959dcaa - stable/13 - linux(4): Add struct clone_args for future clone3 system call. Message-ID: <202206171938.25HJc18r014115@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=8fb3f959dcaab8b254eadaadbacc3fa416ce69e4 commit 8fb3f959dcaab8b254eadaadbacc3fa416ce69e4 Author: Dmitry Chagin <dchagin@FreeBSD.org> AuthorDate: 2021-08-12 08:49:01 +0000 Commit: Dmitry Chagin <dchagin@FreeBSD.org> CommitDate: 2022-06-17 19:33:30 +0000 linux(4): Add struct clone_args for future clone3 system call. In preparation for clone3 system call add struct clone_args and use it in clone implementation. Move all of clone related bits to the newly created linux_fork.h header. Differential revision: https://reviews.freebsd.org/D31474 MFC after: 2 weeks (cherry picked from commit 0a4b664ae8582b7596858317b53d84eb4f8bbf9a) --- sys/amd64/linux/linux_machdep.c | 1 + sys/amd64/linux/linux_sysvec.c | 1 + sys/amd64/linux32/linux32_machdep.c | 1 + sys/amd64/linux32/linux32_sysvec.c | 1 + sys/arm64/linux/linux_machdep.c | 1 + sys/arm64/linux/linux_sysvec.c | 1 + sys/compat/linux/linux.h | 2 + sys/compat/linux/linux_emul.c | 10 ++-- sys/compat/linux/linux_emul.h | 3 +- sys/compat/linux/linux_fork.c | 53 ++++++++++++--------- sys/compat/linux/linux_fork.h | 92 +++++++++++++++++++++++++++++++++++++ sys/compat/linux/linux_misc.h | 27 ----------- sys/i386/linux/linux_machdep.c | 1 + sys/i386/linux/linux_sysvec.c | 1 + 14 files changed, 140 insertions(+), 55 deletions(-) diff --git a/sys/amd64/linux/linux_machdep.c b/sys/amd64/linux/linux_machdep.c index 5de27a7ee7d3..dc6b6140f60c 100644 --- a/sys/amd64/linux/linux_machdep.c +++ b/sys/amd64/linux/linux_machdep.c @@ -88,6 +88,7 @@ __FBSDID("$FreeBSD$"); #include <amd64/linux/linux_proto.h> #include <compat/linux/linux_emul.h> #include <compat/linux/linux_file.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ipc.h> #include <compat/linux/linux_misc.h> #include <compat/linux/linux_mmap.h> diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c index f9ab19a40ef2..b008b4b49c1c 100644 --- a/sys/amd64/linux/linux_sysvec.c +++ b/sys/amd64/linux/linux_sysvec.c @@ -77,6 +77,7 @@ __FBSDID("$FreeBSD$"); #include <amd64/linux/linux.h> #include <amd64/linux/linux_proto.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ioctl.h> #include <compat/linux/linux_mib.h> #include <compat/linux/linux_misc.h> diff --git a/sys/amd64/linux32/linux32_machdep.c b/sys/amd64/linux32/linux32_machdep.c index 926c69c9ebac..5801fac7f126 100644 --- a/sys/amd64/linux32/linux32_machdep.c +++ b/sys/amd64/linux32/linux32_machdep.c @@ -76,6 +76,7 @@ __FBSDID("$FreeBSD$"); #include <amd64/linux32/linux.h> #include <amd64/linux32/linux32_proto.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ipc.h> #include <compat/linux/linux_misc.h> #include <compat/linux/linux_mmap.h> diff --git a/sys/amd64/linux32/linux32_sysvec.c b/sys/amd64/linux32/linux32_sysvec.c index 42b40ee3094a..7905b8587192 100644 --- a/sys/amd64/linux32/linux32_sysvec.c +++ b/sys/amd64/linux32/linux32_sysvec.c @@ -83,6 +83,7 @@ __FBSDID("$FreeBSD$"); #include <amd64/linux32/linux.h> #include <amd64/linux32/linux32_proto.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ioctl.h> #include <compat/linux/linux_mib.h> #include <compat/linux/linux_misc.h> diff --git a/sys/arm64/linux/linux_machdep.c b/sys/arm64/linux/linux_machdep.c index eff084eb9f96..055e9374424c 100644 --- a/sys/arm64/linux/linux_machdep.c +++ b/sys/arm64/linux/linux_machdep.c @@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$"); #include <arm64/linux/linux_proto.h> #include <compat/linux/linux_dtrace.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_misc.h> #include <compat/linux/linux_mmap.h> #include <compat/linux/linux_util.h> diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c index 63438cb62ec4..dfbc12ee4b42 100644 --- a/sys/arm64/linux/linux_sysvec.c +++ b/sys/arm64/linux/linux_sysvec.c @@ -60,6 +60,7 @@ __FBSDID("$FreeBSD$"); #include <arm64/linux/linux_proto.h> #include <compat/linux/linux_dtrace.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ioctl.h> #include <compat/linux/linux_mib.h> #include <compat/linux/linux_misc.h> diff --git a/sys/compat/linux/linux.h b/sys/compat/linux/linux.h index 0b405b692796..f73a0484cf55 100644 --- a/sys/compat/linux/linux.h +++ b/sys/compat/linux/linux.h @@ -259,4 +259,6 @@ struct l_statx { uint64_t __spare2[13]; }; +#define lower_32_bits(n) ((uint32_t)((n) & 0xffffffff)) + #endif /* _LINUX_MI_H_ */ diff --git a/sys/compat/linux/linux_emul.c b/sys/compat/linux/linux_emul.c index fbfe9080cac7..86688d50c8a8 100644 --- a/sys/compat/linux/linux_emul.c +++ b/sys/compat/linux/linux_emul.c @@ -139,7 +139,7 @@ linux_set_default_stacksize(struct thread *td, struct proc *p) } void -linux_proc_init(struct thread *td, struct thread *newtd, int flags) +linux_proc_init(struct thread *td, struct thread *newtd, bool init_thread) { struct linux_emuldata *em; struct linux_pemuldata *pem; @@ -150,7 +150,7 @@ linux_proc_init(struct thread *td, struct thread *newtd, int flags) /* non-exec call */ em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO); - if (flags & LINUX_CLONE_THREAD) { + if (init_thread) { LINUX_CTR1(proc_init, "thread newtd(%d)", newtd->td_tid); @@ -312,12 +312,12 @@ linux_on_exec(struct proc *p, struct image_params *imgp) * before exec. Update emuldata to reflect * single-threaded cleaned state after exec. */ - linux_proc_init(td, NULL, 0); + linux_proc_init(td, NULL, false); } else { /* * We are switching the process to Linux emulator. */ - linux_proc_init(td, td, 0); + linux_proc_init(td, td, false); /* * Create a transient td_emuldata for all suspended @@ -328,7 +328,7 @@ linux_on_exec(struct proc *p, struct image_params *imgp) FOREACH_THREAD_IN_PROC(td->td_proc, othertd) { if (othertd == td) continue; - linux_proc_init(td, othertd, LINUX_CLONE_THREAD); + linux_proc_init(td, othertd, true); } } #if defined(__amd64__) diff --git a/sys/compat/linux/linux_emul.h b/sys/compat/linux/linux_emul.h index ed5332b2c96c..70646cc93847 100644 --- a/sys/compat/linux/linux_emul.h +++ b/sys/compat/linux/linux_emul.h @@ -55,12 +55,11 @@ struct linux_emuldata { struct linux_emuldata *em_find(struct thread *); int linux_exec_imgact_try(struct image_params *); -void linux_proc_init(struct thread *, struct thread *, int); +void linux_proc_init(struct thread *, struct thread *, bool); void linux_on_exit(struct proc *); void linux_schedtail(struct thread *); void linux_on_exec(struct proc *, struct image_params *); void linux_thread_dtor(struct thread *); -void linux_thread_detach(struct thread *); int linux_common_execve(struct thread *, struct image_args *); void linux32_prepare_notes(struct thread *, struct note_info_list *, size_t *); void linux64_prepare_notes(struct thread *, struct note_info_list *, size_t *); diff --git a/sys/compat/linux/linux_fork.c b/sys/compat/linux/linux_fork.c index 955e9e512bd1..97f5b7d89de4 100644 --- a/sys/compat/linux/linux_fork.c +++ b/sys/compat/linux/linux_fork.c @@ -60,7 +60,9 @@ __FBSDID("$FreeBSD$"); #include <machine/../linux/linux.h> #include <machine/../linux/linux_proto.h> #endif +#include <compat/linux/linux.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_futex.h> #include <compat/linux/linux_misc.h> #include <compat/linux/linux_util.h> @@ -82,7 +84,7 @@ linux_fork(struct thread *td, struct linux_fork_args *args) td2 = FIRST_THREAD_IN_PROC(p2); - linux_proc_init(td, td2, 0); + linux_proc_init(td, td2, false); td->td_retval[0] = p2->p_pid; @@ -112,7 +114,7 @@ linux_vfork(struct thread *td, struct linux_vfork_args *args) td2 = FIRST_THREAD_IN_PROC(p2); - linux_proc_init(td, td2, 0); + linux_proc_init(td, td2, false); td->td_retval[0] = p2->p_pid; @@ -128,7 +130,7 @@ linux_vfork(struct thread *td, struct linux_vfork_args *args) #endif static int -linux_clone_proc(struct thread *td, struct linux_clone_args *args) +linux_clone_proc(struct thread *td, struct l_clone_args *args) { struct fork_req fr; int error, ff = RFPROC | RFSTOPPED, f2; @@ -138,11 +140,12 @@ linux_clone_proc(struct thread *td, struct linux_clone_args *args) struct linux_emuldata *em; f2 = 0; - exit_signal = args->flags & 0x000000ff; - if (LINUX_SIG_VALID(exit_signal)) { - exit_signal = linux_to_bsd_signal(exit_signal); - } else if (exit_signal != 0) + if (LINUX_SIG_VALID(args->exit_signal)) { + exit_signal = linux_to_bsd_signal(args->exit_signal); + } else if (args->exit_signal != 0) return (EINVAL); + else + exit_signal = 0; if (args->flags & LINUX_CLONE_VM) ff |= RFMEM; @@ -158,7 +161,7 @@ linux_clone_proc(struct thread *td, struct linux_clone_args *args) } if (args->flags & LINUX_CLONE_PARENT_SETTID) - if (args->parent_tidptr == NULL) + if (args->parent_tid == NULL) return (EINVAL); if (args->flags & LINUX_CLONE_VFORK) @@ -175,23 +178,23 @@ linux_clone_proc(struct thread *td, struct linux_clone_args *args) td2 = FIRST_THREAD_IN_PROC(p2); /* create the emuldata */ - linux_proc_init(td, td2, args->flags); + linux_proc_init(td, td2, false); em = em_find(td2); KASSERT(em != NULL, ("clone_proc: emuldata not found.\n")); if (args->flags & LINUX_CLONE_CHILD_SETTID) - em->child_set_tid = args->child_tidptr; + em->child_set_tid = args->child_tid; else em->child_set_tid = NULL; if (args->flags & LINUX_CLONE_CHILD_CLEARTID) - em->child_clear_tid = args->child_tidptr; + em->child_clear_tid = args->child_tid; else em->child_clear_tid = NULL; if (args->flags & LINUX_CLONE_PARENT_SETTID) { - error = copyout(&p2->p_pid, args->parent_tidptr, + error = copyout(&p2->p_pid, args->parent_tid, sizeof(p2->p_pid)); if (error) linux_msg(td, "copyout p_pid failed!"); @@ -235,7 +238,7 @@ linux_clone_proc(struct thread *td, struct linux_clone_args *args) } static int -linux_clone_thread(struct thread *td, struct linux_clone_args *args) +linux_clone_thread(struct thread *td, struct l_clone_args *args) { struct linux_emuldata *em; struct thread *newtd; @@ -244,12 +247,12 @@ linux_clone_thread(struct thread *td, struct linux_clone_args *args) LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p", td->td_tid, (unsigned)args->flags, - args->parent_tidptr, args->child_tidptr); + args->parent_tid, args->child_tid); if ((args->flags & LINUX_CLONE_PARENT) != 0) return (EINVAL); if (args->flags & LINUX_CLONE_PARENT_SETTID) - if (args->parent_tidptr == NULL) + if (args->parent_tid == NULL) return (EINVAL); /* Threads should be created with own stack */ @@ -284,7 +287,7 @@ linux_clone_thread(struct thread *td, struct linux_clone_args *args) thread_cow_get(newtd, td); /* create the emuldata */ - linux_proc_init(td, newtd, args->flags); + linux_proc_init(td, newtd, true); em = em_find(newtd); KASSERT(em != NULL, ("clone_thread: emuldata not found.\n")); @@ -293,12 +296,12 @@ linux_clone_thread(struct thread *td, struct linux_clone_args *args) linux_set_cloned_tls(newtd, PTRIN(args->tls)); if (args->flags & LINUX_CLONE_CHILD_SETTID) - em->child_set_tid = args->child_tidptr; + em->child_set_tid = args->child_tid; else em->child_set_tid = NULL; if (args->flags & LINUX_CLONE_CHILD_CLEARTID) - em->child_clear_tid = args->child_tidptr; + em->child_clear_tid = args->child_tid; else em->child_clear_tid = NULL; @@ -328,7 +331,7 @@ linux_clone_thread(struct thread *td, struct linux_clone_args *args) td->td_tid, newtd->td_tid); if (args->flags & LINUX_CLONE_PARENT_SETTID) { - error = copyout(&newtd->td_tid, args->parent_tidptr, + error = copyout(&newtd->td_tid, args->parent_tid, sizeof(newtd->td_tid)); if (error) linux_msg(td, "clone_thread: copyout td_tid failed!"); @@ -359,11 +362,19 @@ fail: int linux_clone(struct thread *td, struct linux_clone_args *args) { + struct l_clone_args ca = { + .flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL), + .child_tid = args->child_tidptr, + .parent_tid = args->parent_tidptr, + .exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL), + .stack = args->stack, + .tls = args->tls, + }; if (args->flags & LINUX_CLONE_THREAD) - return (linux_clone_thread(td, args)); + return (linux_clone_thread(td, &ca)); else - return (linux_clone_proc(td, args)); + return (linux_clone_proc(td, &ca)); } int diff --git a/sys/compat/linux/linux_fork.h b/sys/compat/linux/linux_fork.h new file mode 100644 index 000000000000..04dfb8ac8a70 --- /dev/null +++ b/sys/compat/linux/linux_fork.h @@ -0,0 +1,92 @@ +/*- + * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _LINUX_FORK_H_ +#define _LINUX_FORK_H_ + +#define LINUX_CLONE_VM 0x00000100 +#define LINUX_CLONE_FS 0x00000200 +#define LINUX_CLONE_FILES 0x00000400 +#define LINUX_CLONE_SIGHAND 0x00000800 +#define LINUX_CLONE_PID 0x00001000 /* No longer exist in Linux */ +#define LINUX_CLONE_PTRACE 0x00002000 +#define LINUX_CLONE_VFORK 0x00004000 +#define LINUX_CLONE_PARENT 0x00008000 +#define LINUX_CLONE_THREAD 0x00010000 +#define LINUX_CLONE_NEWNS 0x00020000 /* New mount NS */ +#define LINUX_CLONE_SYSVSEM 0x00040000 +#define LINUX_CLONE_SETTLS 0x00080000 +#define LINUX_CLONE_PARENT_SETTID 0x00100000 +#define LINUX_CLONE_CHILD_CLEARTID 0x00200000 +#define LINUX_CLONE_DETACHED 0x00400000 /* Unused */ +#define LINUX_CLONE_UNTRACED 0x00800000 +#define LINUX_CLONE_CHILD_SETTID 0x01000000 +#define LINUX_CLONE_NEWCGROUP 0x02000000 /* New cgroup NS */ +#define LINUX_CLONE_NEWUTS 0x04000000 +#define LINUX_CLONE_NEWIPC 0x08000000 +#define LINUX_CLONE_NEWUSER 0x10000000 +#define LINUX_CLONE_NEWPID 0x20000000 +#define LINUX_CLONE_NEWNET 0x40000000 +#define LINUX_CLONE_IO 0x80000000 + +#define LINUX_CSIGNAL 0x000000ff + +/* + * User-space clone3 args layout. + */ +struct l_user_clone_args { + uint64_t flags; + uint64_t pidfd; + uint64_t child_tid; + uint64_t parent_tid; + uint64_t exit_signal; + uint64_t stack; + uint64_t stack_size; + uint64_t tls; + uint64_t set_tid; + uint64_t set_tid_size; + uint64_t cgroup; +}; + +/* + * Kernel clone3 args layout. + */ +struct l_clone_args { + uint64_t flags; + l_int *child_tid; + l_int *parent_tid; + l_int exit_signal; + l_ulong stack; + l_ulong stack_size; + l_ulong tls; +}; + +int linux_set_upcall(struct thread *, register_t); +int linux_set_cloned_tls(struct thread *, void *); +void linux_thread_detach(struct thread *); + +#endif /* _LINUX_FORK_H_ */ diff --git a/sys/compat/linux/linux_misc.h b/sys/compat/linux/linux_misc.h index dc17ed430014..80f6b8a58e81 100644 --- a/sys/compat/linux/linux_misc.h +++ b/sys/compat/linux/linux_misc.h @@ -98,31 +98,6 @@ #define __LINUX_NPXCW__ 0x37f #endif -#define LINUX_CLONE_VM 0x00000100 -#define LINUX_CLONE_FS 0x00000200 -#define LINUX_CLONE_FILES 0x00000400 -#define LINUX_CLONE_SIGHAND 0x00000800 -#define LINUX_CLONE_PID 0x00001000 /* No longer exist in Linux */ -#define LINUX_CLONE_PTRACE 0x00002000 -#define LINUX_CLONE_VFORK 0x00004000 -#define LINUX_CLONE_PARENT 0x00008000 -#define LINUX_CLONE_THREAD 0x00010000 -#define LINUX_CLONE_NEWNS 0x00020000 /* New mount NS */ -#define LINUX_CLONE_SYSVSEM 0x00040000 -#define LINUX_CLONE_SETTLS 0x00080000 -#define LINUX_CLONE_PARENT_SETTID 0x00100000 -#define LINUX_CLONE_CHILD_CLEARTID 0x00200000 -#define LINUX_CLONE_DETACHED 0x00400000 /* Unused */ -#define LINUX_CLONE_UNTRACED 0x00800000 -#define LINUX_CLONE_CHILD_SETTID 0x01000000 -#define LINUX_CLONE_NEWCGROUP 0x02000000 /* New cgroup NS */ -#define LINUX_CLONE_NEWUTS 0x04000000 -#define LINUX_CLONE_NEWIPC 0x08000000 -#define LINUX_CLONE_NEWUSER 0x10000000 -#define LINUX_CLONE_NEWPID 0x20000000 -#define LINUX_CLONE_NEWNET 0x40000000 -#define LINUX_CLONE_IO 0x80000000 - /* Scheduling policies */ #define LINUX_SCHED_OTHER 0 #define LINUX_SCHED_FIFO 1 @@ -180,8 +155,6 @@ extern int stclohz; int linux_ptrace_status(struct thread *td, int pid, int status); #endif void linux_to_bsd_waitopts(int options, int *bsdopts); -int linux_set_upcall(struct thread *td, register_t stack); -int linux_set_cloned_tls(struct thread *td, void *desc); struct thread *linux_tdfind(struct thread *, lwpid_t, pid_t); #endif /* _LINUX_MISC_H_ */ diff --git a/sys/i386/linux/linux_machdep.c b/sys/i386/linux/linux_machdep.c index 1a51b7954063..c81bf1b4e2e5 100644 --- a/sys/i386/linux/linux_machdep.c +++ b/sys/i386/linux/linux_machdep.c @@ -66,6 +66,7 @@ __FBSDID("$FreeBSD$"); #include <i386/linux/linux.h> #include <i386/linux/linux_proto.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ipc.h> #include <compat/linux/linux_misc.h> #include <compat/linux/linux_mmap.h> diff --git a/sys/i386/linux/linux_sysvec.c b/sys/i386/linux/linux_sysvec.c index 6805a29204c5..8968a491ce95 100644 --- a/sys/i386/linux/linux_sysvec.c +++ b/sys/i386/linux/linux_sysvec.c @@ -66,6 +66,7 @@ __FBSDID("$FreeBSD$"); #include <i386/linux/linux.h> #include <i386/linux/linux_proto.h> #include <compat/linux/linux_emul.h> +#include <compat/linux/linux_fork.h> #include <compat/linux/linux_ioctl.h> #include <compat/linux/linux_mib.h> #include <compat/linux/linux_misc.h>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202206171938.25HJc18r014115>