From patchwork Tue May 9 10:41:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 13235610 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FF09C77B75 for ; Tue, 9 May 2023 10:42:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B74E6B00A7; Tue, 9 May 2023 06:42:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 667CF6B00A8; Tue, 9 May 2023 06:42:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55655280001; Tue, 9 May 2023 06:42:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 438056B00A7 for ; Tue, 9 May 2023 06:42:22 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 57FAB12080B for ; Tue, 9 May 2023 10:42:21 +0000 (UTC) X-FDA: 80770377282.21.BBC2D5F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 7DEEF80014 for ; Tue, 9 May 2023 10:42:19 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of "SRS0=/ane=A6=loongson.cn=chenhuacai@kernel.org" designates 139.178.84.217 as permitted sender) smtp.mailfrom="SRS0=/ane=A6=loongson.cn=chenhuacai@kernel.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1683628939; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=sqJ4HHQeKA/Ogex9msCTbWEUlI7g2PRqtRt28duDMEA=; b=zePOOHhsA8faAy8uBU/o/rqDBp266tVjSXRGSYvKCm1SaFzDitXtZ2kfAcIKi3Kb5JRam7 xqi9NuKDUzbU9iIEoLps984S54smxI2NKUny9Gct5Fu3WUovUKcBDBa3IUmBTBLdL9VKAl f5KPWS0dfR2SvupeMhvxR4+I7B4BqkU= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of "SRS0=/ane=A6=loongson.cn=chenhuacai@kernel.org" designates 139.178.84.217 as permitted sender) smtp.mailfrom="SRS0=/ane=A6=loongson.cn=chenhuacai@kernel.org"; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683628939; a=rsa-sha256; cv=none; b=e1zrANj7ks+ar3SCDtgwPkROwHQ49+//rInE3sJvniI8XMkmOU+20y+N7ZWV3pG7jcAyMj 9Ee7hm4cq43mmw2o4gmhaQ7t13iQgSRtMJr5I2wW/kkJ+gW4nIKhJVrv5Ni0bcS7awWkAS FSgaoNhAVFgGRg1ry4+qVeyFzL6Tb9g= 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 6CB5264599; Tue, 9 May 2023 10:42:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 388D1C4339C; Tue, 9 May 2023 10:42:14 +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 RFC] kthread: Unify kernel_thread() and user_mode_thread() Date: Tue, 9 May 2023 18:41:27 +0800 Message-Id: <20230509104127.1997562-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Stat-Signature: 68dgfkmqphxxi1ywztdk9wkj96qo33ro X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7DEEF80014 X-Rspam-User: X-HE-Tag: 1683628939-630685 X-HE-Meta: U2FsdGVkX1/XOKZB7Jx3S3fncibHZm/fRyjfH8MVtBIVGtyo+vMTs5IvgDx4gY+vKxJgkMTt0rSo9l9x9t19WeURBfM3O/AF4x/7btKziO3JbNgYXhUdZswus7xDYJBO6UAgUEMg6Oa76AaIvEiG5O+0IiXD95y9Nt6OUL0UDejLlaOOM52NXiq8XJkCobAeRpaJmICqOvAXDtmaKiJVIsnqgrqK0ymPgZ7loM0D6gHV2nVLGTFKUIWmpYX5ms1KmHxS+L4XgewXK3U82+Hpa2BW8fD5SklXx5y5pNT7mO2fJAM2RUokFOoOYjS410thKr6ytTWRkbnb1vqn0gR3tzS8laoiTEdnaGl1Q3VaVouuQcpNNy7Z1GdAMzwYf1Sr95GtELi4SPgvaK0hkEeZWe1hWidbGkx/CPugs9TC92ZmiMVzb1I/Js2gU4hYKjisLnVmR45ZhsxqmurbZZcTug2Z6pR3rVkMeR93MS0vxm34kjH6Ln7ad0mmFqOf7sGBDcawa1BdS7gmy0VIw1BFh+gC7Z5iB6hlrbgSQWTH24isoYdi9u2TTXkbXVjAKNBBgiSdmwpT6yj7gHVoKpLNfimdKRBwjbLyqT4W20W6wsbTVCDrOykAwRte+biZTC/qDkuxVyaNf/pL2IP0wVpfyYI7CU9Ow5dHHdKxp/sr9gvePQFP95LWvAtejs3JfLAkx+aqQMKhhC/wDz+Fp520UGMkiOU4gD0Xyc7IjD6NWvgdP9sV++M8tNMJ8T7xBViS8wFx81VSz+lPOyEpvKxqZwgI2wrXT7RRV1tobvc7KzapovF1ebqwpGxOt8tGBsl2PTrunMXn0A1Z83jxkcrX3SAvlBkKFCNUikISnOVlqisIMeXOKl37CE8bgG7X3SJNXRejwJVOsBCerUC+r13Ixyi5NQ991UHvYEzSXdBbn+A8aer+/u5S7LWOlo005HngLWCYkyVgOnswrxzwoin MnJ777Nt JkZauMsml7apIgZ6oR7PXA5NmvQ80h3EUJZDHNA7ci3xMbAB2ypC8p/MKicOjTXe7tcacoymOy4F24syZN4UzHnooXZTKMrCNwXwnN/0p+tsTfNHZqA7MDonRL38QKWUdsq3P0fKnbzsDPXVtlMMte51lDgb37r56+XjXVrsN5X5nqNaOrVLRuXk9La5yW1g5rEs1HSvQDoXtUSs2T3vXIFSedudLIjFpghaZZIkpm96bhDEyImLN9M7Mh3oIpVgtVhwma5AGgQV15bWTiwzt09HZkyr7+T3fWcw3o5Q0f7BxTGRVl7XizslnJyKOJRweGk4By35KMjfgc6m4bHP+pPXcj68DanZEVTZs3OjA+w6VyrrpZUXbLnquQ8dCX269rbvJro/UGJaGF+4OiZGLG7+bz8vNQXo0x+tztOVRH5tntcdr7BsE4b+9xiqK+coIzU5B X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Commit 343f4c49f2438d8 ("kthread: Don't allocate kthread_struct for init and umh") introduces a new function user_mode_thread() for init and umh. But the name is a bit confusing because init and umh are indeed kernel threads at creation time, the real difference is "they will become user processes". So let's unify the kernel_thread() and user_mode_thread() to kernel_thread() again, and add a new 'user' parameter for init and umh. Signed-off-by: Huacai Chen --- 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..1fc09768257c 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 trace, int node, struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node); 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, int user); extern long kernel_wait4(pid_t, int __user *, int, struct rusage *); int kernel_wait(pid_t pid, int *stat); diff --git a/init/main.c b/init/main.c index af50044deed5..487d93da5eea 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 = user_mode_thread(kernel_init, NULL, CLONE_FS); + pid = kernel_thread(kernel_init, NULL, NULL, CLONE_FS, 1); /* * 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(); numa_default_policy(); - pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES); + pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES, 0); rcu_read_lock(); kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); rcu_read_unlock(); diff --git a/kernel/fork.c b/kernel/fork.c index ed4e01daccaa..eeaf50944a0b 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, int user) { struct kernel_clone_args args = { .flags = ((lower_32_bits(flags) | CLONE_VM | @@ -2974,23 +2974,7 @@ pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name, .fn = fn, .fn_arg = arg, .name = name, - .kthread = 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 = { - .flags = ((lower_32_bits(flags) | CLONE_VM | - CLONE_UNTRACED) & ~CSIGNAL), - .exit_signal = (lower_32_bits(flags) & CSIGNAL), - .fn = fn, - .fn_arg = arg, + .kthread = !user, }; return kernel_clone(&args); diff --git a/kernel/kthread.c b/kernel/kthread.c index 490792b1066e..f4ac241c8d94 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 = kernel_thread(kthread, create, create->full_name, - CLONE_FS | CLONE_FILES | SIGCHLD); + CLONE_FS | CLONE_FILES | SIGCHLD, 0); if (pid < 0) { /* Release the structure when caller killed by a fatal signal. */ struct completion *done = xchg(&create->done, NULL); diff --git a/kernel/umh.c b/kernel/umh.c index 60aa9e764a38..ec4ec12094b5 100644 --- a/kernel/umh.c +++ b/kernel/umh.c @@ -130,7 +130,7 @@ static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info) /* If SIGCLD is ignored do_wait won't populate the status. */ kernel_sigaction(SIGCHLD, SIG_DFL); - pid = user_mode_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD); + pid = kernel_thread(call_usermodehelper_exec_async, sub_info, NULL, SIGCHLD, 1); if (pid < 0) sub_info->retval = pid; else @@ -169,8 +169,8 @@ static void call_usermodehelper_exec_work(struct work_struct *work) * want to pollute current->children, and we need a parent * that always ignores SIGCHLD to ensure auto-reaping. */ - pid = user_mode_thread(call_usermodehelper_exec_async, sub_info, - CLONE_PARENT | SIGCHLD); + pid = kernel_thread(call_usermodehelper_exec_async, sub_info, + NULL, CLONE_PARENT | SIGCHLD, 1); if (pid < 0) { sub_info->retval = pid; umh_complete(sub_info);