[PATCH 2/3] kthread: Replace kernel_thread with new_kthread

Eric W. Biederman
2022-06-26T19:16:24Z

It is desriable to be able to perform all of the kthread setup before
the kernel thread is awaked for the first time.

To make that possible replace kernel_thread with new_kthread that does
all of the same work except it does not call wake_up_new_task.

Replace the two uses of kernel_threadd with new_kthread and a call
to wake_up_new_task.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
---
include/linux/sched/task.h | 2 +-
init/main.c | 6 ++----
kernel/fork.c | 4 ++--
kernel/kthread.c | 10 ++++++----
4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index d95930e220da..c4c7a0118553 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -91,7 +91,7 @@ extern pid_t kernel_clone(struct kernel_clone_args *kargs);
struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node);
struct task_struct *fork_idle(int);
struct mm_struct *copy_init_mm(void);
-extern pid_t kernel_thread(int (*fn)(void *), void *arg);
+extern struct task_struct *new_kthread(int (*fn)(void *), void *arg, int node);
extern pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags);
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 211d38db0d16..b437581f8001 100644
--- a/init/main.c
+++ b/init/main.c
@@ -701,10 +701,8 @@ noinline void __ref rest_init(void)
rcu_read_unlock();

numa_default_policy();
- pid = kernel_thread(kthreadd, NULL);
- rcu_read_lock();
- kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
- rcu_read_unlock();
+ kthreadd_task = new_kthread(kthreadd, NULL, NUMA_NO_NODE);
+ wake_up_new_task(kthreadd_task);

/*
* Enable might_sleep() and smp_processor_id() checks.
diff --git a/kernel/fork.c b/kernel/fork.c
index 65909ded0ea7..794d9f9c78bc 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2694,7 +2694,7 @@ pid_t kernel_clone(struct kernel_clone_args *args)
/*
* Create a kernel thread.
*/
-pid_t kernel_thread(int (*fn)(void *), void *arg)
+struct task_struct *new_kthread(int (*fn)(void *), void *arg, int node)
{
unsigned long flags = CLONE_FS | CLONE_FILES | SIGCHLD;
struct kernel_clone_args args = {
@@ -2706,7 +2706,7 @@ pid_t kernel_thread(int (*fn)(void *), void *arg)
.kthread = 1,
};

- return kernel_clone(&args);
+ return copy_process(NULL, 0, node, &args);
}

/*
diff --git a/kernel/kthread.c b/kernel/kthread.c
index c0505e6b7142..8529f6b1582b 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -390,14 +390,14 @@ int tsk_fork_get_node(struct task_struct *tsk)

static void create_kthread(struct kthread_create_info *create)
{
- int pid;
+ struct task_struct *new;

#ifdef CONFIG_NUMA
current->pref_node_fork = create->node;
#endif
/* We want our own signal handler (we take no signals by default). */
- pid = kernel_thread(kthread, create);
- if (pid < 0) {
+ new = new_kthread(kthread, create, NUMA_NO_NODE);
+ if (IS_ERR(new)) {
/* If user was SIGKILLed, I release the structure. */
struct completion *done = xchg(&create->done, NULL);

@@ -405,8 +405,10 @@ static void create_kthread(struct kthread_create_info *create)
kfree(create);
return;
}
- create->result = ERR_PTR(pid);
+ create->result = ERR_CAST(new);
complete(done);
+ } else {
+ wake_up_new_task(new);
}
}

--
2.35.3