Skip to contentSkip to conversation

fork: Export create_io_thread()

PATCH New Kent Overstreet 1 participant 2 messages May 30, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fork: Export create_io_thread()

create_io_thread() creates a thread for doing work on behalf of some user process - export it so it can be used in thread_with_file, which spins up a kthread connected to a file descriptor.

---
 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
index 99076dbe27d8..fadf08532327 100644
@@ -2744,6 +2744,7 @@ struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node)
 
 	return copy_process(NULL, 0, node, &args);
 }
+EXPORT_SYMBOL_GPL(create_io_thread);
 
 /*
  *  Ok, this is the main fork-routine.
Signature · 1 line
2.45.1
#1 lore raw

PATCH 2/2 bcachefs: twf: Switch to create_io_thread()

create_io_thread() is more appropriate than kthread_create() when spinning up a thread to do work asynchronously on behalf of some specific userspace task.

---
 fs/bcachefs/chardev.c          |  3 +--
 fs/bcachefs/thread_with_file.c | 29 +++++++++++++++++++----------
 fs/bcachefs/thread_with_file.h |  3 ++-
 3 files changed, 22 insertions(+), 13 deletions(-)
fs/bcachefs/chardev.c +1 -2
index fbb2b95e0f5c..9be0744cb19a 100644
@@ -441,13 +441,12 @@ struct bch_data_ctx {
 	struct bch_move_stats		stats;
 };
 
-static int bch2_data_thread(void *arg)
+static void bch2_data_thread(void *arg)
 {
 	struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
 
 	ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
 	ctx->stats.data_type = U8_MAX;
-	return 0;
 }
 
 static int bch2_data_job_release(struct inode *inode, struct file *file)
fs/bcachefs/thread_with_file.c +19 -10
index 075567016db5..a37d45a69328 100644
@@ -13,15 +13,21 @@
 
 void bch2_thread_with_file_exit(struct thread_with_file *thr)
 {
-	if (thr->task) {
-		kthread_stop(thr->task);
+	if (thr->task)
 		put_task_struct(thr->task);
-	}
+}
+
+static int thr_with_file_fn(void *p)
+{
+	struct thread_with_file *thr = p;
+
+	thr->fn(thr);
+	do_exit(0);
 }
 
 int bch2_run_thread_with_file(struct thread_with_file *thr,
 			      const struct file_operations *fops,
-			      int (*fn)(void *))
+			      void (*fn)(void *))
 {
 	struct file *file = NULL;
 	int ret, fd = -1;
@@ -38,11 +44,15 @@ int bch2_run_thread_with_file(struct thread_with_file *thr,
 	get_task_comm(name, current);
 
 	thr->ret = 0;
-	thr->task = kthread_create(fn, thr, "%s", name);
+	thr->fn = fn;
+	thr->task = create_io_thread(thr_with_file_fn, thr, NUMA_NO_NODE);
 	ret = PTR_ERR_OR_ZERO(thr->task);
 	if (ret)
 		return ret;
 
+	set_task_comm(thr->task, name);
+	set_cpus_allowed_ptr(thr->task, cpu_possible_mask);
+
 	ret = get_unused_fd_flags(fd_flags);
 	if (ret < 0)
 		goto err;
@@ -54,14 +64,15 @@ int bch2_run_thread_with_file(struct thread_with_file *thr,
 		goto err;
 
 	get_task_struct(thr->task);
-	wake_up_process(thr->task);
+	wake_up_new_task(thr->task);
 	fd_install(fd, file);
+
 	return fd;
 err:
 	if (fd >= 0)
 		put_unused_fd(fd);
 	if (thr->task)
-		kthread_stop(thr->task);
+		put_task_struct(thr->task);
 	return ret;
 }
 
@@ -293,14 +304,12 @@ static const struct file_operations thread_with_stdout_fops = {
 	.unlocked_ioctl	= thread_with_stdio_ioctl,
 };
 
-static int thread_with_stdio_fn(void *arg)
+static void thread_with_stdio_fn(void *arg)
 {
 	struct thread_with_stdio *thr = arg;
 
 	thr->thr.ret = thr->ops->fn(thr);
-
 	thread_with_stdio_done(thr);
-	return 0;
 }
 
 void bch2_thread_with_stdio_init(struct thread_with_stdio *thr,
fs/bcachefs/thread_with_file.h +2 -1
index 72497b921911..d1aacc894ef9 100644
@@ -40,6 +40,7 @@ struct task_struct;
 
 struct thread_with_file {
 	struct task_struct	*task;
+	void			(*fn)(void *);
 	int			ret;
 	bool			done;
 };
@@ -47,7 +48,7 @@ struct thread_with_file {
 void bch2_thread_with_file_exit(struct thread_with_file *);
 int bch2_run_thread_with_file(struct thread_with_file *,
 			      const struct file_operations *,
-			      int (*fn)(void *));
+			      void (*fn)(void *));
 
 struct thread_with_stdio;
 
Signature · 1 line
2.45.1