Some callers may need to make signaling decisions based on the state of the targeted task, and that can only safely be done post adding the task_work to the task. Split task_work_add() into:
__task_work_add() - adds the work item __task_work_notify() - sends the notification
No functional changes in this patch.
Cc: Peter Zijlstra peterz@infradead.org Cc: stable@vger.kernel.org # v5.7+ Signed-off-by: Jens Axboe axboe@kernel.dk --- include/linux/task_work.h | 19 ++++++++++++++++ kernel/task_work.c | 48 +++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 22 deletions(-)
diff --git a/include/linux/task_work.h b/include/linux/task_work.h index 0fb93aafa478..7abbd8df5e13 100644 --- a/include/linux/task_work.h +++ b/include/linux/task_work.h @@ -5,6 +5,8 @@ #include <linux/list.h> #include <linux/sched.h>
+extern struct callback_head work_exited; + typedef void (*task_work_func_t)(struct callback_head *);
static inline void @@ -13,6 +15,21 @@ init_task_work(struct callback_head *twork, task_work_func_t func) twork->func = func; }
+static inline int __task_work_add(struct task_struct *task, + struct callback_head *work) +{ + struct callback_head *head; + + do { + head = READ_ONCE(task->task_works); + if (unlikely(head == &work_exited)) + return -ESRCH; + work->next = head; + } while (cmpxchg(&task->task_works, head, work) != head); + + return 0; +} + #define TWA_RESUME 1 #define TWA_SIGNAL 2 int task_work_add(struct task_struct *task, struct callback_head *twork, int); @@ -20,6 +37,8 @@ int task_work_add(struct task_struct *task, struct callback_head *twork, int); struct callback_head *task_work_cancel(struct task_struct *, task_work_func_t); void task_work_run(void);
+void __task_work_notify(struct task_struct *task, int notify); + static inline void exit_task_work(struct task_struct *task) { task_work_run(); diff --git a/kernel/task_work.c b/kernel/task_work.c index 5c0848ca1287..9bde81481984 100644 --- a/kernel/task_work.c +++ b/kernel/task_work.c @@ -3,7 +3,27 @@ #include <linux/task_work.h> #include <linux/tracehook.h>
-static struct callback_head work_exited; /* all we need is ->next == NULL */ +struct callback_head work_exited = { + .next = NULL /* all we need is ->next == NULL */ +}; + +void __task_work_notify(struct task_struct *task, int notify) +{ + unsigned long flags; + + switch (notify) { + case TWA_RESUME: + set_notify_resume(task); + break; + case TWA_SIGNAL: + if (lock_task_sighand(task, &flags)) { + task->jobctl |= JOBCTL_TASK_WORK; + signal_wake_up(task, 0); + unlock_task_sighand(task, &flags); + } + break; + } +}
/** * task_work_add - ask the @task to execute @work->func() @@ -27,29 +47,13 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */ int task_work_add(struct task_struct *task, struct callback_head *work, int notify) { - struct callback_head *head; - unsigned long flags; + int ret;
- do { - head = READ_ONCE(task->task_works); - if (unlikely(head == &work_exited)) - return -ESRCH; - work->next = head; - } while (cmpxchg(&task->task_works, head, work) != head); - - switch (notify) { - case TWA_RESUME: - set_notify_resume(task); - break; - case TWA_SIGNAL: - if (lock_task_sighand(task, &flags)) { - task->jobctl |= JOBCTL_TASK_WORK; - signal_wake_up(task, 0); - unlock_task_sighand(task, &flags); - } - break; - } + ret = __task_work_add(task, work); + if (unlikely(ret)) + return ret;
+ __task_work_notify(task, notify); return 0; }
On Sat, Aug 08, 2020 at 12:34:38PM -0600, Jens Axboe wrote:
Some callers may need to make signaling decisions based on the state of the targeted task, and that can only safely be done post adding the task_work to the task. Split task_work_add() into:
__task_work_add() - adds the work item __task_work_notify() - sends the notification
No functional changes in this patch.
Might be nice to mention __task_work_add() is now inline.
Cc: Peter Zijlstra peterz@infradead.org Cc: stable@vger.kernel.org # v5.7+ Signed-off-by: Jens Axboe axboe@kernel.dk
include/linux/task_work.h | 19 ++++++++++++++++ kernel/task_work.c | 48 +++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 22 deletions(-)
+struct callback_head work_exited = {
- .next = NULL /* all we need is ->next == NULL */
+};
Would it make sense to make this const ? Esp. with the thing exposed, sticking it in R/O memory might avoid a mistake somewhere.
On 8/10/20 5:37 AM, peterz@infradead.org wrote:
On Sat, Aug 08, 2020 at 12:34:38PM -0600, Jens Axboe wrote:
Some callers may need to make signaling decisions based on the state of the targeted task, and that can only safely be done post adding the task_work to the task. Split task_work_add() into:
__task_work_add() - adds the work item __task_work_notify() - sends the notification
No functional changes in this patch.
Might be nice to mention __task_work_add() is now inline.
OK, will mention that.
Cc: Peter Zijlstra peterz@infradead.org Cc: stable@vger.kernel.org # v5.7+ Signed-off-by: Jens Axboe axboe@kernel.dk
include/linux/task_work.h | 19 ++++++++++++++++ kernel/task_work.c | 48 +++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 22 deletions(-)
+struct callback_head work_exited = {
- .next = NULL /* all we need is ->next == NULL */
+};
Would it make sense to make this const ? Esp. with the thing exposed, sticking it in R/O memory might avoid a mistake somewhere.
That was my original intent, but that makes 'head' in task_work_run() const as well, and cmpxchg() doesn't like that:
kernel/task_work.c: In function ‘task_work_run’: ./arch/x86/include/asm/cmpxchg.h:89:29: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] 89 | __typeof__(*(ptr)) __new = (new); \ | ^ ./arch/x86/include/asm/cmpxchg.h:134:2: note: in expansion of macro ‘__raw_cmpxchg’ 134 | __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX) | ^~~~~~~~~~~~~ ./arch/x86/include/asm/cmpxchg.h:149:2: note: in expansion of macro ‘__cmpxchg’ 149 | __cmpxchg(ptr, old, new, sizeof(*(ptr))) | ^~~~~~~~~ ./include/asm-generic/atomic-instrumented.h:1685:2: note: in expansion of macro ‘arch_cmpxchg’ 1685 | arch_cmpxchg(__ai_ptr, __VA_ARGS__); \ | ^~~~~~~~~~~~ kernel/task_work.c:126:12: note: in expansion of macro ‘cmpxchg’ 126 | } while (cmpxchg(&task->task_works, work, head) != work); | ^~~~~~~
which is somewhat annoying. Because there's really no good reason why it can't be const, it'll just require the changes to dig a bit deeper.
On Mon, Aug 10, 2020 at 09:01:02AM -0600, Jens Axboe wrote:
+struct callback_head work_exited = {
- .next = NULL /* all we need is ->next == NULL */
+};
Would it make sense to make this const ? Esp. with the thing exposed, sticking it in R/O memory might avoid a mistake somewhere.
That was my original intent, but that makes 'head' in task_work_run() const as well, and cmpxchg() doesn't like that:
kernel/task_work.c: In function ‘task_work_run’: ./arch/x86/include/asm/cmpxchg.h:89:29: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] 89 | __typeof__(*(ptr)) __new = (new); \ | ^ ./arch/x86/include/asm/cmpxchg.h:134:2: note: in expansion of macro ‘__raw_cmpxchg’ 134 | __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX) | ^~~~~~~~~~~~~ ./arch/x86/include/asm/cmpxchg.h:149:2: note: in expansion of macro ‘__cmpxchg’ 149 | __cmpxchg(ptr, old, new, sizeof(*(ptr))) | ^~~~~~~~~ ./include/asm-generic/atomic-instrumented.h:1685:2: note: in expansion of macro ‘arch_cmpxchg’ 1685 | arch_cmpxchg(__ai_ptr, __VA_ARGS__); \ | ^~~~~~~~~~~~ kernel/task_work.c:126:12: note: in expansion of macro ‘cmpxchg’ 126 | } while (cmpxchg(&task->task_works, work, head) != work); | ^~~~~~~
which is somewhat annoying. Because there's really no good reason why it can't be const, it'll just require the changes to dig a bit deeper.
Bah! Best I can come up with is casting the const away there, what a mess :-(
On 8/10/20 9:01 AM, Jens Axboe wrote:
On 8/10/20 5:37 AM, peterz@infradead.org wrote:
On Sat, Aug 08, 2020 at 12:34:38PM -0600, Jens Axboe wrote:
Some callers may need to make signaling decisions based on the state of the targeted task, and that can only safely be done post adding the task_work to the task. Split task_work_add() into:
__task_work_add() - adds the work item __task_work_notify() - sends the notification
No functional changes in this patch.
Might be nice to mention __task_work_add() is now inline.
OK, will mention that.
Added a note of that in the commit message, otherwise the patch is unchanged:
https://git.kernel.dk/cgit/linux-block/commit/?h=io_uring-5.9&id=67e5aca...
Are you happy with this one now, given that we cannot easily make the exit_work const?
On Mon, Aug 10, 2020 at 11:51:33AM -0600, Jens Axboe wrote:
Added a note of that in the commit message, otherwise the patch is unchanged:
https://git.kernel.dk/cgit/linux-block/commit/?h=io_uring-5.9&id=67e5aca...
Acked-by: Peter Zijlstra (Intel) peterz@infradead.org
linux-stable-mirror@lists.linaro.org