[PATCH 2/5] kernel: Add kernel_clone_fns function to setup task

From: Mike Christie
Date: Sun Feb 12 2023 - 20:00:50 EST


The io_uring code will set it's worker threads's name with a
iou-sqd/iou-wrk prefix and then either the task's pid or the task's
parent's pid. The kthread code will set the name to whatever the caller
decides. To set these names they have come up with different approaches.

This patch adds a callout to setup a kthread or io_uring worker's
internal struct and/or set the task's coomm.

Signed-off-by: Mike Christie <michael.christie@xxxxxxxxxx>
---
include/linux/sched.h | 2 ++
include/linux/sched/task.h | 1 +
kernel/fork.c | 6 ++++++
3 files changed, 9 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 853d08f7562b..09bdc71c0523 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1066,6 +1066,8 @@ struct task_struct {
* executable name, excluding path.
*
* - normally initialized setup_new_exec()
+ * - can be initialized in copy_process if
+ * kernel_clone_args.fns.setup_thread_fn() is set.
* - access it with [gs]et_task_comm()
* - lock it with task_lock()
*/
diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index 7a61ebbcdfe0..844824e0023c 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -20,6 +20,7 @@ struct css_set;

struct kernel_clone_fns {
int (*thread_fn)(void *fn_arg);
+ int (*setup_thread_fn)(struct task_struct *tsk, void *fn_arg);
};

struct kernel_clone_args {
diff --git a/kernel/fork.c b/kernel/fork.c
index f308a5ae0ed3..057274da64fb 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2345,6 +2345,12 @@ static __latent_entropy struct task_struct *copy_process(
p->tgid = p->pid;
}

+ if (args->fns && args->fns->setup_thread_fn) {
+ retval = args->fns->setup_thread_fn(p, args->fn_arg);
+ if (retval)
+ goto bad_fork_put_pidfd;
+ }
+
p->nr_dirtied = 0;
p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
p->dirty_paused_when = 0;
--
2.25.1