Skip to contentSkip to conversation

task_work: simplify the task_work_add() interface

PATCH New Christoph Hellwig 1 participant 1 message Jan 4, 2022

Subsystems (16)
x86 VFS ACPI perf events +12 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 task_work: simplify the task_work_add() interface

Provide a low-level task_work_add_nonotify interface that just adds the work to the list and open code the TWA_SIGNAL and TWA_NONE callers using it. task_work_add() itself now only handles the common TWA_RESUME case and can drop the notify argument.

---
 arch/x86/kernel/cpu/mce/core.c |  2 +-
 arch/x86/mm/tlb.c              |  2 +-
 drivers/acpi/apei/ghes.c       |  3 +-
 drivers/android/binder.c       |  2 +-
 fs/file_table.c                |  2 +-
 fs/io-wq.c                     |  3 +-
 fs/io_uring.c                  | 16 +++++-----
 fs/namespace.c                 |  2 +-
 include/linux/task_work.h      | 11 ++-----
 include/linux/tracehook.h      |  2 +-
 kernel/events/uprobes.c        |  2 +-
 kernel/irq/manage.c            |  2 +-
 kernel/sched/fair.c            |  2 +-
 kernel/task_work.c             | 56 ++++++++++++++++++----------------
 kernel/time/posix-cpu-timers.c |  2 +-
 security/keys/keyctl.c         |  2 +-
 security/yama/yama_lsm.c       |  2 +-
 17 files changed, 55 insertions(+), 58 deletions(-)
arch/x86/kernel/cpu/mce/core.c +1 -1
index 5818b837fd4d4..a9da9a968163c 100644
@@ -1349,7 +1349,7 @@ static void queue_task_work(struct mce *m, char *msg, void (*func)(struct callba
 	if (count > 1)
 		return;
 
-	task_work_add(current, &current->mce_kill_me, TWA_RESUME);
+	task_work_add(current, &current->mce_kill_me);
 }
 
 /* Handle unconfigured int18 (should never happen) */
arch/x86/mm/tlb.c +1 -1
index a6cf56a149393..8db8f4f7001f8 100644
@@ -355,7 +355,7 @@ static void l1d_flush_evaluate(unsigned long prev_mm, unsigned long next_mm,
 	if (this_cpu_read(cpu_info.smt_active)) {
 		clear_ti_thread_flag(&next->thread_info, TIF_SPEC_L1D_FLUSH);
 		next->l1d_flush_kill.func = l1d_flush_force_sigbus;
-		task_work_add(next, &next->l1d_flush_kill, TWA_RESUME);
+		task_work_add(next, &next->l1d_flush_kill);
 	}
 }
 
drivers/acpi/apei/ghes.c +1 -2
index 0c5c9acc6254e..12bd63b9af308 100644
@@ -988,8 +988,7 @@ static void ghes_proc_in_irq(struct irq_work *irq_work)
 		if (task_work_pending && current->mm != &init_mm) {
 			estatus_node->task_work.func = ghes_kick_task_work;
 			estatus_node->task_work_cpu = smp_processor_id();
-			ret = task_work_add(current, &estatus_node->task_work,
-					    TWA_RESUME);
+			ret = task_work_add(current, &estatus_node->task_work);
 			if (ret)
 				estatus_node->task_work.func = NULL;
 		}
drivers/android/binder.c +1 -1
index 8351c5638880b..eeef8dfde606b 100644
@@ -1858,7 +1858,7 @@ static void binder_deferred_fd_close(int fd)
 	close_fd_get_file(fd, &twcb->file);
 	if (twcb->file) {
 		filp_close(twcb->file, current->files);
-		task_work_add(current, &twcb->twork, TWA_RESUME);
+		task_work_add(current, &twcb->twork);
 	} else {
 		kfree(twcb);
 	}
fs/file_table.c +1 -1
index 57edef16dce46..2fd8c17fd2708 100644
@@ -369,7 +369,7 @@ void fput_many(struct file *file, unsigned int refs)
 
 		if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
 			init_task_work(&file->f_u.fu_rcuhead, ____fput);
-			if (!task_work_add(task, &file->f_u.fu_rcuhead, TWA_RESUME))
+			if (!task_work_add(task, &file->f_u.fu_rcuhead))
 				return;
 			/*
 			 * After this task has run exit_task_work(),
fs/io-wq.c +2 -1
index b9e447a956c0e..5136891e5bdc4 100644
@@ -364,7 +364,8 @@ static bool io_queue_worker_create(struct io_worker *worker,
 	atomic_inc(&wq->worker_refs);
 	init_task_work(&worker->create_work, func);
 	worker->create_index = acct->index;
-	if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
+	if (!task_work_add_nonotify(wq->task, &worker->create_work)) {
+		set_notify_signal(wq->task);
 		/*
 		 * EXIT may have been set after checking it above, check after
 		 * adding the task_work and remove any creation item if it is
fs/io_uring.c +8 -8
index 6a4e6b029f04f..2b81d1591537c 100644
@@ -2376,7 +2376,6 @@ static void io_req_task_work_add(struct io_kiocb *req, bool priority)
 {
 	struct task_struct *tsk = req->task;
 	struct io_uring_task *tctx = tsk->io_uring;
-	enum task_work_notify_mode notify;
 	struct io_wq_work_node *node;
 	unsigned long flags;
 	bool running;
@@ -2399,14 +2398,15 @@ static void io_req_task_work_add(struct io_kiocb *req, bool priority)
 
 	/*
 	 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
-	 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
-	 * processing task_work. There's no reliable way to tell if TWA_RESUME
-	 * will do the job.
+	 * all other cases, use set_notify_signal unconditionally to ensure
+	 * we're processing the task_work. There's no reliable way to tell if
+	 * task_work_add will do the job.
 	 */
-	notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
-	if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
-		if (notify == TWA_NONE)
+	if (likely(!task_work_add_nonotify(tsk, &tctx->task_work))) {
+		if (req->ctx->flags & IORING_SETUP_SQPOLL)
 			wake_up_process(tsk);
+		else
+			set_notify_signal(tsk);
 		return;
 	}
 
@@ -9668,7 +9668,7 @@ static __cold void io_ring_exit_work(struct work_struct *work)
 					ctx_node);
 		/* don't spin on a single task if cancellation failed */
 		list_rotate_left(&ctx->tctx_list);
-		ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
+		ret = task_work_add_nonotify(node->task, &exit.task_work);
 		if (WARN_ON_ONCE(ret))
 			continue;
 
fs/namespace.c +1 -1
index 4e026e366d734..8cbd03d67aa33 100644
@@ -1235,7 +1235,7 @@ static void mntput_no_expire(struct mount *mnt)
 		struct task_struct *task = current;
 		if (likely(!(task->flags & PF_KTHREAD))) {
 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
-			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
+			if (!task_work_add(task, &mnt->mnt_rcu))
 				return;
 		}
 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
include/linux/task_work.h +3 -8
index 5b8a93f288bb4..af27deee1a559 100644
@@ -13,14 +13,9 @@ init_task_work(struct callback_head *twork, task_work_func_t func)
 	twork->func = func;
 }
 
-enum task_work_notify_mode {
-	TWA_NONE,
-	TWA_RESUME,
-	TWA_SIGNAL,
-};
-
-int task_work_add(struct task_struct *task, struct callback_head *twork,
-			enum task_work_notify_mode mode);
+int task_work_add(struct task_struct *task, struct callback_head *twork);
+int task_work_add_nonotify(struct task_struct *task,
+		struct callback_head *twork);
 
 struct callback_head *task_work_cancel_match(struct task_struct *task,
 	bool (*match)(struct callback_head *, void *data), void *data);
include/linux/tracehook.h +1 -1
index 2564b7434b4d7..a2f916e9eca35 100644
@@ -203,7 +203,7 @@ static inline void tracehook_notify_resume(struct pt_regs *regs)
 
 /*
  * called by exit_to_user_mode_loop() if ti_work & _TIF_NOTIFY_SIGNAL. This
- * is currently used by TWA_SIGNAL based task_work, which requires breaking
+ * is currently used by signal based task_work, which requires breaking
  * wait loops to ensure that task_work is noticed and run.
  */
 static inline void tracehook_notify_signal(void)
kernel/events/uprobes.c +1 -1
index 6357c3580d07b..6c12d9f0dc647 100644
@@ -1823,7 +1823,7 @@ void uprobe_copy_process(struct task_struct *t, unsigned long flags)
 
 	t->utask->dup_xol_addr = area->vaddr;
 	init_task_work(&t->utask->dup_xol_work, dup_xol_work);
-	task_work_add(t, &t->utask->dup_xol_work, TWA_RESUME);
+	task_work_add(t, &t->utask->dup_xol_work);
 }
 
 /*
kernel/irq/manage.c +1 -1
index f23ffd30385b1..4557319303374 100644
@@ -1268,7 +1268,7 @@ static int irq_thread(void *data)
 		handler_fn = irq_thread_fn;
 
 	init_task_work(&on_exit_work, irq_thread_dtor);
-	task_work_add(current, &on_exit_work, TWA_NONE);
+	task_work_add_nonotify(current, &on_exit_work);
 
 	irq_thread_check_affinity(desc, action);
 
kernel/sched/fair.c +1 -1
index 095b0aa378df0..9aaa900beb55b 100644
@@ -2880,7 +2880,7 @@ static void task_tick_numa(struct rq *rq, struct task_struct *curr)
 		curr->node_stamp += period;
 
 		if (!time_before(jiffies, curr->mm->numa_next_scan))
-			task_work_add(curr, work, TWA_RESUME);
+			task_work_add(curr, work);
 	}
 }
 
kernel/task_work.c +29 -27

82 lines not on this page · raw message

kernel/time/posix-cpu-timers.c +1 -1
index 96b4e78104266..c8808b32dc144 100644
@@ -1201,7 +1201,7 @@ static inline void __run_posix_cpu_timers(struct task_struct *tsk)
 
 	/* Schedule task work to actually expire the timers */
 	tsk->posix_cputimers_work.scheduled = true;
-	task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
+	task_work_add(tsk, &tsk->posix_cputimers_work.work);
 }
 
 static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
security/keys/keyctl.c +1 -1
index 96a92a645216d..331a825ff6616 100644
@@ -1693,7 +1693,7 @@ long keyctl_session_to_parent(void)
 
 	/* the replacement session keyring is applied just prior to userspace
 	 * restarting */
-	ret = task_work_add(parent, newwork, TWA_RESUME);
+	ret = task_work_add(parent, newwork);
 	if (!ret)
 		newwork = NULL;
 unlock:
security/yama/yama_lsm.c +1 -1
index 06e226166aab3..2af248939dd46 100644
@@ -99,7 +99,7 @@ static void report_access(const char *access, struct task_struct *target,
 	info->access = access;
 	info->target = target;
 	info->agent = agent;
-	if (task_work_add(current, &info->work, TWA_RESUME) == 0)
+	if (task_work_add(current, &info->work) == 0)
 		return; /* success */
 
 	WARN(1, "report_access called from exiting task");
Signature · 1 line
2.30.2