From nobody Mon Apr 29 01:56:09 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B52A3C7EE2C for ; Sat, 3 Jun 2023 01:53:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237128AbjFCBxr (ORCPT ); Fri, 2 Jun 2023 21:53:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236585AbjFCBxq (ORCPT ); Fri, 2 Jun 2023 21:53:46 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC1AFE48 for ; Fri, 2 Jun 2023 18:53:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4F2A761956 for ; Sat, 3 Jun 2023 01:53:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC3D5C433EF; Sat, 3 Jun 2023 01:53:40 +0000 (UTC) From: Huacai Chen To: Luis Chamberlain , Andrew Morton , "Eric W . Biederman" Cc: Kees Cook , chenhuacai@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huacai Chen Subject: [PATCH V1] kthread: Unify kernel_thread() and user_mode_thread() Date: Sat, 3 Jun 2023 09:53:02 +0800 Message-Id: <20230603015302.1768127-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 343f4c49f2438d8 ("kthread: Don't allocate kthread_struct for init and umh") introduces a new function user_mode_thread() for init and umh. init and umh are different from typical kernel threads since the don't need a "kthread" struct and they will finally become user processes by calling kernel_execve(), but on the other hand, they are also different from typical user mode threads (they have no "mm" structs at creation time, which is traditionally used to distinguish a user thread and a kernel thread). So I think it is reasonable to treat init and umh as "special kernel threads". Then let's unify the kernel_thread() and user_mode_thread() to kernel_thread() again, and add a new 'user' parameter for init and umh. This also makes code simpler.=20 Signed-off-by: Huacai Chen --- RFC -> V1: Update commit message and change "user" from int to bool. include/linux/sched/task.h | 3 +-- init/main.c | 4 ++-- kernel/fork.c | 20 ++------------------ kernel/kthread.c | 2 +- kernel/umh.c | 6 +++--- 5 files changed, 9 insertions(+), 26 deletions(-) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 537cbf9a2ade..02eb953bc809 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -98,8 +98,7 @@ struct task_struct *copy_process(struct pid *pid, int tra= ce, int node, struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int nod= e); struct task_struct *fork_idle(int); extern pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name, - unsigned long flags); -extern pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long = flags); + unsigned long flags, bool user); extern long kernel_wait4(pid_t, int __user *, int, struct rusage *); int kernel_wait(pid_t pid, int *stat); =20 diff --git a/init/main.c b/init/main.c index af50044deed5..469cebbd35e0 100644 --- a/init/main.c +++ b/init/main.c @@ -697,7 +697,7 @@ noinline void __ref __noreturn rest_init(void) * the init task will end up wanting to create kthreads, which, if * we schedule it before we create kthreadd, will OOPS. */ - pid =3D user_mode_thread(kernel_init, NULL, CLONE_FS); + pid =3D kernel_thread(kernel_init, NULL, NULL, CLONE_FS, true); /* * Pin init on the boot CPU. Task migration is not properly working * until sched_init_smp() has been run. It will set the allowed @@ -710,7 +710,7 @@ noinline void __ref __noreturn rest_init(void) rcu_read_unlock(); =20 numa_default_policy(); - pid =3D kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES); + pid =3D kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES, false= ); rcu_read_lock(); kthreadd_task =3D find_task_by_pid_ns(pid, &init_pid_ns); rcu_read_unlock(); diff --git a/kernel/fork.c b/kernel/fork.c index ed4e01daccaa..f91696904252 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2965,7 +2965,7 @@ pid_t kernel_clone(struct kernel_clone_args *args) * Create a kernel thread. */ pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name, - unsigned long flags) + unsigned long flags, bool user) { struct kernel_clone_args args =3D { .flags =3D ((lower_32_bits(flags) | CLONE_VM | @@ -2974,23 +2974,7 @@ pid_t kernel_thread(int (*fn)(void *), void *arg, co= nst char *name, .fn =3D fn, .fn_arg =3D arg, .name =3D name, - .kthread =3D 1, - }; - - return kernel_clone(&args); -} - -/* - * Create a user mode thread. - */ -pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags) -{ - struct kernel_clone_args args =3D { - .flags =3D ((lower_32_bits(flags) | CLONE_VM | - CLONE_UNTRACED) & ~CSIGNAL), - .exit_signal =3D (lower_32_bits(flags) & CSIGNAL), - .fn =3D fn, - .fn_arg =3D arg, + .kthread =3D !user, }; =20 return kernel_clone(&args); diff --git a/kernel/kthread.c b/kernel/kthread.c index 490792b1066e..5f025569eb38 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -400,7 +400,7 @@ static void create_kthread(struct kthread_create_info *= create) #endif /* We want our own signal handler (we take no signals by default). */ pid =3D kernel_thread(kthread, create, create->full_name, - CLONE_FS | CLONE_FILES | SIGCHLD); + CLONE_FS | CLONE_FILES | SIGCHLD, false); if (pid < 0) { /* Release the structure when caller killed by a fatal signal. */ struct completion *done =3D xchg(&create->done, NULL); diff --git a/kernel/umh.c b/kernel/umh.c index 60aa9e764a38..b0ead7cce761 100644 --- a/kernel/umh.c +++ b/kernel/umh.c @@ -130,7 +130,7 @@ static void call_usermodehelper_exec_sync(struct subpro= cess_info *sub_info) =20 /* If SIGCLD is ignored do_wait won't populate the status. */ kernel_sigaction(SIGCHLD, SIG_DFL); - pid =3D user_mode_thread(call_usermodehelper_exec_async, sub_info, SIGCHL= D); + pid =3D kernel_thread(call_usermodehelper_exec_async, sub_info, NULL, SIG= CHLD, true); if (pid < 0) sub_info->retval =3D pid; else @@ -169,8 +169,8 @@ static void call_usermodehelper_exec_work(struct work_s= truct *work) * want to pollute current->children, and we need a parent * that always ignores SIGCHLD to ensure auto-reaping. */ - pid =3D user_mode_thread(call_usermodehelper_exec_async, sub_info, - CLONE_PARENT | SIGCHLD); + pid =3D kernel_thread(call_usermodehelper_exec_async, sub_info, + NULL, CLONE_PARENT | SIGCHLD, true); if (pid < 0) { sub_info->retval =3D pid; umh_complete(sub_info); --=20 2.39.1