task_work: Consume only item at a time while invoking the callbacks.
PATCH 1/1 task_work: Consume only item at a time while invoking the callbacks.
Yi and syzbot managed to hang the task within task_run().
The problem is
task_work_run() -> __fput() -> perf_release() -> perf_event_release_kernel() -> _free_event() -> perf_pending_task_sync() -> task_work_cancel() failed -> rcuwait_wait_event().
Once task_work_run() is running, the list of callbacks removed from the task_struct and from this point on task_work_cancel() can't remove any pending and not yet started work items.
The proposed solution is to remove one item at a time.
Oleg pointed out that this might be problematic if one closes 2.000.000 files at once. While testing this scenario by opening that many files following by exit() to ensure that all files are closed at once, I did not observe anything outside of noise.
Consume only one work item at a time. Forward the next work item under the lock to ensure it is not canceled during the replacement.
--- kernel/task_work.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
kernel/task_work.c +18 -17
@@ -194,7 +194,7 @@ bool task_work_cancel(struct task_struct *task, struct callback_head *cb)void task_work_run (void ) {struct task_struct *task = current; -struct callback_head *work, *head, *next; +struct callback_head *work, *head;for (;;) {/*
@@ -202,17 +202,7 @@ void task_work_run(void)* work_exited unless the list is empty. */ work =READ_ONCE (task->task_works); -do { - head =NULL ; -if (!work) { -if (task->flags &PF_EXITING ) - head = &work_exited; -else -break ; - } - }while (!try_cmpxchg (&task->task_works, &work, head)); - -if (!work) +if (!work && !(task->flags &PF_EXITING ))break ;/* * Synchronize with task_work_cancel_match(). It can not remove
@@ -220,13 +210,24 @@ void task_work_run(void)* But it can remove another entry from the ->next list. */ raw_spin_lock_irq (&task->pi_lock); +do { + head =NULL ; +if (work) { + head =READ_ONCE (work->next); + }else { +if (task->flags &PF_EXITING ) + head = &work_exited; +else +break ; + } + }while (!try_cmpxchg (&task->task_works, &work, head));raw_spin_unlock_irq (&task->pi_lock); -do { - next = work->next; - work->func (work); - work = next; +if (!work) +break ; + work->func (work); + +if (head)cond_resched (); - }while (work); } }
Signature · 1 line
2.47.2
Well... I won't really argue because I can't suggest a better fix at least right now. Most probably never.
However, let me say that this patch doesn't make me happy ;) See below.
Quoted 5 lines · Sebastian Andrzej Siewior
Oleg pointed out that this might be problematic if one closes 2.000.000 files at once. While testing this scenario by opening that many files following by
exit()to ensure that all files are closed at once, I did not observe anything outside of noise.
and this probably means that we can revert c82199061009 ("task_work: remove fifo ordering guarantee") and restore the fifo ordering which IMO makes much more sense.
But:
Quoted 1 line
Fixes c5d93d23a2601 ("perf: Enqueue SIGTRAP always via task_work.")
Yes. So, to fix this specific problem in perf this patch changes task_work.c
And after this change we can never enforce a "clear" ordering, fifo or even lifo. The ordering is simply "unpredictable/random".
I'll try to find and read the previous discussions tomorrow, but iirc Frederic had another solution?
Oleg.
Quoted 63 lines
kernel/task_work.c +18
-18@@ -194,7 +194,7 @@ bool task_work_cancel(struct task_struct *task, struct callback_head *cb)void task_work_run (void ) {struct task_struct *task = current; -struct callback_head *work, *head, *next; +struct callback_head *work, *head;for (;;) {/* @@ -202,17 +202,7 @@ void task_work_run(void)* work_exited unless the list is empty. */ work =READ_ONCE (task->task_works); -do { - head =NULL ; -if (!work) { -if (task->flags &PF_EXITING ) - head = &work_exited; -else -break ; - } - }while (!try_cmpxchg (&task->task_works, &work, head)); - -if (!work) +if (!work && !(task->flags &PF_EXITING ))break ;/* * Synchronize with task_work_cancel_match(). It can not remove @@ -220,13 +210,24 @@ void task_work_run(void)* But it can remove another entry from the ->next list. */ raw_spin_lock_irq (&task->pi_lock); +do { + head =NULL ; +if (work) { + head =READ_ONCE (work->next); + }else { +if (task->flags &PF_EXITING ) + head = &work_exited; +else +break ; + } + }while (!try_cmpxchg (&task->task_works, &work, head));raw_spin_unlock_irq (&task->pi_lock); -do { - next = work->next; - work->func (work); - work = next; +if (!work) +break ; + work->func (work); + +if (head)cond_resched (); - }while (work); } } --2.47.2
Quoted 20 lines · Oleg Nesterov
Well... I won't really argue because I can't suggest a better fix at least right now. Most probably never.
However, let me say that this patch doesn't make me happy ;) See below.
Quoted 5 lines · Sebastian Andrzej Siewior
Oleg pointed out that this might be problematic if one closes 2.000.000 files at once. While testing this scenario by opening that many files following by
exit()to ensure that all files are closed at once, I did not observe anything outside of noise.and this probably means that we can revert
c82199061009("task_work: remove fifo ordering guarantee") and restore the fifo ordering which IMO makes much more sense.But:
Quoted 1 line
Fixes c5d93d23a2601 ("perf: Enqueue SIGTRAP always via task_work.")
Quoted 7 lines
Yes. So, to fix this specific problem in perf this patch changes
task_work.cAnd after this change we can never enforce a "clear" ordering, fifo or even lifo. The ordering is simply "unpredictable/random".
I'll try to find and read the previous discussions tomorrow, but iirc Frederic had another solution?
This:
https://lore.kernel.org/all/[email protected]/
Though I'm not entirely happy with it either.
Thanks.
Quoted 11 lines · Frederic Weisbecker
Quoted 3 lines · Oleg Nesterov
I'll try to find and read the previous discussions tomorrow, but iirc Frederic had another solution?
This:
https://lore.kernel.org/all/[email protected]/
Though I'm not entirely happy with it either.
Yes, thanks...
Can we do something else and avoid this rcuwait_wait_event() altogether?
To simplify the discussion, suppose we add a global XXX_LOCK. Just in case, of course we shouldn't do this ;) But lets suppose we do.
Now, can _something_ like the (incomplete, ugly as hell) patch below work?
Oleg.
---
kernel/events/core.c +28 -7
@@ -5304,12 +5304,12 @@ static void perf_pending_task_sync(struct perf_event *event)return ; } -/* -* All accesses related to the event are within the same RCU section in -* perf_pending_task(). The RCU grace period before the event is freed -* will make sure all those accesses are complete by then. -*/ -rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); +spin_lock (XXX_LOCK ); +if (event->pending_work) { +local_dec (&event->ctx->nr_no_switch_fast); + event->pending_work =-1 ; + } +spin_unlock (XXX_LOCK ); }static void _free_event (struct perf_event *event)
@@ -5369,7 +5369,15 @@ static void _free_event(struct perf_event *event)exclusive_event_destroy (event);module_put (event->pmu->module); -call_rcu (&event->rcu_head, free_event_rcu); +bool free = true; +spin_lock (XXX_LOCK) + if (event->pending_work == -1) { + event->pending_work =-2 ; + free = false; + } +spin_unlock (XXX_LOCK ); +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }/*
@@ -6981,7 +6989,14 @@ static void perf_pending_task(struct callback_head *head) { struct perf_event *event = container_of(head, struct perf_event, pending_task);int rctx; +bool free = false; +spin_lock (XXX_LOCK ); +if ((int )event->pending_work <0 ) { + free = event->pending_work ==-2u ; + event->pending_work =0 ; + goto unlock; + }/* * All accesses to the event must belong to the same implicit RCU read-side * critical section as the ->pending_work reset. See comment in
@@ -7004,6 +7019,12 @@ static void perf_pending_task(struct callback_head *head)if (rctx >=0 )perf_swevent_put_recursion_context (rctx); + +unlock: +spin_unlock (XXX_LOCK ); + +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }#ifdef CONFIG_GUEST_PERF_EVENTS
Quoted 82 lines · Oleg Nesterov
Quoted 11 lines · Frederic Weisbecker
Quoted 3 lines · Oleg Nesterov
I'll try to find and read the previous discussions tomorrow, but iirc Frederic had another solution?
This:
https://lore.kernel.org/all/[email protected]/
Though I'm not entirely happy with it either.
Yes, thanks...
Can we do something else and avoid this
rcuwait_wait_event()altogether?To simplify the discussion, suppose we add a global
XXX_LOCK. Just in case, of course we shouldn't do this ;) But lets suppose we do.Now, can _something_ like the (incomplete, ugly as hell) patch below work?
Oleg.
---kernel/events/core.c +22
-7@@ -5304,12 +5304,12 @@ static void perf_pending_task_sync(struct perf_event *event)return ; } -/* -* All accesses related to the event are within the same RCU section in -* perf_pending_task(). The RCU grace period before the event is freed -* will make sure all those accesses are complete by then. -*/ -rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); +spin_lock (XXX_LOCK ); +if (event->pending_work) { +local_dec (&event->ctx->nr_no_switch_fast); + event->pending_work =-1 ; + } +spin_unlock (XXX_LOCK ); }static void _free_event (struct perf_event *event)@@ -5369,7 +5369,15 @@ static void _free_event(struct perf_event *event)exclusive_event_destroy (event);module_put (event->pmu->module); -call_rcu (&event->rcu_head, free_event_rcu); +bool free = true; +spin_lock (XXX_LOCK) + if (event->pending_work == -1) { + event->pending_work =-2 ; + free = false; + } +spin_unlock (XXX_LOCK ); +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }/* @@ -6981,7 +6989,14 @@ static void perf_pending_task(struct callback_head *head) { struct perf_event *event = container_of(head, struct perf_event, pending_task);int rctx; +bool free = false; +spin_lock (XXX_LOCK ); +if ((int )event->pending_work <0 ) { + free = event->pending_work ==-2u ; + event->pending_work =0 ; + goto unlock; + }/* * All accesses to the event must belong to the same implicit RCU read-side * critical section as the ->pending_work reset. See comment in @@ -7004,6 +7019,12 @@ static void perf_pending_task(struct callback_head *head)if (rctx >=0 )
kernel/events/core.c static void perf_pending_task(struct callback_head *head) · line 7030
Quoted 10 lines
kernel/events/core.c +6
-0perf_swevent_put_recursion_context (rctx); + +unlock: +spin_unlock (XXX_LOCK ); + +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }#ifdef CONFIG_GUEST_PERF_EVENTS
Heh, I suggested something similar also:
https://lore.kernel.org/lkml/[email protected]/
The main point being that the event has its refcount inc when the task work is queued and put from the task work handler. This restore the initial behaviour but without the bug where the event was freed before the task work was reached.
My remaining worry was that exec'ing tasks with suid might get an irrelevant signal from the unprivileged past but surely we can work around that. An untested, complete version would look like:
include/linux/perf_event.h +0 -1
@@ -831,7 +831,6 @@ struct perf_event {struct irq_work pending_disable_irq;struct callback_head pending_task;unsigned int pending_work; -struct rcuwait pending_work_wait;atomic_t event_limit;
kernel/events/core.c +12 -45
130 lines not on this page · raw message
Quoted 1 line · Oleg Nesterov
Hmm. empty email? Let me resend.
Quoted 67 lines · Frederic Weisbecker
Quoted 65 lines · Oleg Nesterov
kernel/events/core.c +28
-7@@ -5304,12 +5304,12 @@ static void perf_pending_task_sync(struct perf_event *event)return ; } -/* -* All accesses related to the event are within the same RCU section in -* perf_pending_task(). The RCU grace period before the event is freed -* will make sure all those accesses are complete by then. -*/ -rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); +spin_lock (XXX_LOCK ); +if (event->pending_work) { +local_dec (&event->ctx->nr_no_switch_fast); + event->pending_work =-1 ; + } +spin_unlock (XXX_LOCK ); }static void _free_event (struct perf_event *event)@@ -5369,7 +5369,15 @@ static void _free_event(struct perf_event *event)exclusive_event_destroy (event);module_put (event->pmu->module); -call_rcu (&event->rcu_head, free_event_rcu); +bool free = true; +spin_lock (XXX_LOCK) + if (event->pending_work == -1) { + event->pending_work =-2 ; + free = false; + } +spin_unlock (XXX_LOCK ); +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }/* @@ -6981,7 +6989,14 @@ static void perf_pending_task(struct callback_head *head) { struct perf_event *event = container_of(head, struct perf_event, pending_task);int rctx; +bool free = false; +spin_lock (XXX_LOCK ); +if ((int )event->pending_work <0 ) { + free = event->pending_work ==-2u ; + event->pending_work =0 ; + goto unlock; + }/* * All accesses to the event must belong to the same implicit RCU read-side * critical section as the ->pending_work reset. See comment in @@ -7004,6 +7019,12 @@ static void perf_pending_task(struct callback_head *head)if (rctx >=0 )perf_swevent_put_recursion_context (rctx); + +unlock: +spin_unlock (XXX_LOCK ); + +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }
Quoted 5 lines
kernel/events/core.c static void perf_pending_task(struct callback_head *head) · line 7030
Quoted 2 lines
kernel/events/core.c +0
-0#ifdef CONFIG_GUEST_PERF_EVENTS Heh, I suggested something similar also:
https://lore.kernel.org/lkml/[email protected]/
;)
I can't comment your patch because I don't understand this code enough.
My patch is more simple, it doesn't play with refcount.
perf_pending_task_sync() sets ->pending_work = -1, after that perf_pending_task() (which can run in parallel on another CPU) will only clear ->pending_work and do nothing else.
Then _free_event() rechecks ->pending_work before return, if it is still nonzero then perf_pending_task() is still pending. In this case _free_event() sets ->pending_work = -2 to offload call_rcu(free_event_rcu) to the pending perf_pending_task().
But it is certainly more ugly, and perhaps the very idea is wrong. So I will be happy if we go with your patch.
Either way, IMO we should try to kill this rcuwait_wait_event() logic. See another email I sent a minute ago in this thread. Quite possibly I missed something, but the very idea to wait for another task doesn't look safe to me.
Thanks!
Oleg.
Quoted 80 lines · Oleg Nesterov
Quoted 67 lines · Frederic Weisbecker
Quoted 65 lines · Oleg Nesterov
kernel/events/core.c +28
-7@@ -5304,12 +5304,12 @@ static void perf_pending_task_sync(struct perf_event *event)return ; } -/* -* All accesses related to the event are within the same RCU section in -* perf_pending_task(). The RCU grace period before the event is freed -* will make sure all those accesses are complete by then. -*/ -rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); +spin_lock (XXX_LOCK ); +if (event->pending_work) { +local_dec (&event->ctx->nr_no_switch_fast); + event->pending_work =-1 ; + } +spin_unlock (XXX_LOCK ); }static void _free_event (struct perf_event *event)@@ -5369,7 +5369,15 @@ static void _free_event(struct perf_event *event)exclusive_event_destroy (event);module_put (event->pmu->module); -call_rcu (&event->rcu_head, free_event_rcu); +bool free = true; +spin_lock (XXX_LOCK) + if (event->pending_work == -1) { + event->pending_work =-2 ; + free = false; + } +spin_unlock (XXX_LOCK ); +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }/* @@ -6981,7 +6989,14 @@ static void perf_pending_task(struct callback_head *head) { struct perf_event *event = container_of(head, struct perf_event, pending_task);int rctx; +bool free = false; +spin_lock (XXX_LOCK ); +if ((int )event->pending_work <0 ) { + free = event->pending_work ==-2u ; + event->pending_work =0 ; + goto unlock; + }/* * All accesses to the event must belong to the same implicit RCU read-side * critical section as the ->pending_work reset. See comment in @@ -7004,6 +7019,12 @@ static void perf_pending_task(struct callback_head *head)if (rctx >=0 )perf_swevent_put_recursion_context (rctx); + +unlock: +spin_unlock (XXX_LOCK ); + +if (free) +call_rcu (&event->rcu_head, free_event_rcu); }Quoted 5 lines
kernel/events/core.c static void perf_pending_task(struct callback_head *head) · line 7030
Quoted 2 lines
kernel/events/core.c +0
-0#ifdef CONFIG_GUEST_PERF_EVENTS Heh, I suggested something similar also:
https://lore.kernel.org/lkml/[email protected]/;)
I can't comment your patch because I don't understand this code enough.
My patch is more simple, it doesn't play with refcount.
Quoted 8 lines
perf_pending_task_sync()sets ->pending_work = -1, after thatperf_pending_task()(which can run in parallel on another CPU) will only clear ->pending_work and do nothing else.Then
_free_event()rechecks ->pending_work before return, if it is still nonzero thenperf_pending_task()is still pending. In this case_free_event()sets ->pending_work = -2 to offloadcall_rcu(free_event_rcu)to the pendingperf_pending_task().
Right it works but it does a parallel implementation of events refcounting.
Quoted 3 lines
But it is certainly more ugly, and perhaps the very idea is wrong. So I will be happy if we go with your patch.
Ok I'll prepare a changelog and see where it goes.
Quoted 4 lines
Either way, IMO we should try to kill this
rcuwait_wait_event()logic. See another email I sent a minute ago in this thread. Quite possibly I missed something, but the very idea to wait for another task doesn't look safe to me.
You're right it's very fragile...
Thanks.
Quoted 5 lines
Thanks!
Oleg.
Quoted 2 lines · Frederic Weisbecker
Right it works but it does a parallel implementation of events refcounting.
Yes, yes, and this is the main reason why I think it is ugly.
Quoted 4 lines
Quoted 2 lines
But it is certainly more ugly, and perhaps the very idea is wrong. So I will be happy if we go with your patch.
Ok I'll prepare a changelog and see where it goes.
Great! thanks.
Oleg.
Quoted 7 lines · Oleg Nesterov
Well... I won't really argue because I can't suggest a better fix at least right now. Most probably never.
However, let me say that this patch doesn't make me happy ;) See below.
Quoted 1 line · Sebastian Andrzej Siewior
Quoted 8 lines
Quoted 4 lines
Oleg pointed out that this might be problematic if one closes 2.000.000 files at once. While testing this scenario by opening that many files following by
exit()to ensure that all files are closed at once, I did not observe anything outside of noise.and this probably means that we can revert
c82199061009("task_work: remove fifo ordering guarantee") and restore the fifo ordering which IMO makes much more sense.
So assume that turning around will fix the problem because the cancel callback is run first followed by the clean up.
Quoted 11 lines
But:
Quoted 1 line
Fixes c5d93d23a2601 ("perf: Enqueue SIGTRAP always via task_work.")Yes. So, to fix this specific problem in perf this patch changes
task_work.cAnd after this change we can never enforce a "clear" ordering, fifo or even lifo. The ordering is simply "unpredictable/random".
I'll try to find and read the previous discussions tomorrow, but iirc Frederic had another solution?
Two at least:
- having a pointer to the next item
- avoiding the wait in the task_work callback. I think this is the unfortunate part. I think he had something but was very unhappy with it.
Quoted 1 line
Oleg.
Sebastian
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 12 lines · Oleg Nesterov
Well... I won't really argue because I can't suggest a better fix at least right now. Most probably never.
However, let me say that this patch doesn't make me happy ;) See below.
Quoted 5 lines · Sebastian Andrzej Siewior
Oleg pointed out that this might be problematic if one closes 2.000.000 files at once. While testing this scenario by opening that many files following by
exit()to ensure that all files are closed at once, I did not observe anything outside of noise.
Quoted 6 lines
Quoted 3 lines
and this probably means that we can revert
c82199061009("task_work: remove fifo ordering guarantee") and restore the fifo ordering which IMO makes much more sense.So assume that turning around will fix the problem because the cancel callback is run first followed by the clean up.
Not really, they can run in any order, so fifo can't really help.
But this doesn't matter, please see another email:
https://lore.kernel.org/all/[email protected]/
Oleg.
Quoted 5 lines · Oleg Nesterov
Not really, they can run in any order, so fifo can't really help.
But this doesn't matter, please see another email:
https://lore.kernel.org/all/[email protected]/
Yes, I just got to it. So it looks like that wait part from task_work should go… Let me check Frederic's patch in this thread…
Quoted 1 line
Oleg.
Sebastian
Quoted 9 lines · Sebastian Andrzej Siewior
Yi and syzbot managed to hang the task within
task_run().The problem is
task_work_run()->__fput()->perf_release()->perf_event_release_kernel()->_free_event()->perf_pending_task_sync()->task_work_cancel()failed ->rcuwait_wait_event().
Quoted 3 lines
Once
task_work_run()is running, the list of callbacks removed from thetask_structand from this point ontask_work_cancel()can't remove any pending and not yet started work items.
But can this patch really solve the problem?
Suppose we have two tasks, T1 and T2.
T1 does fd = perf_event_open(pid => T2->pid);
T2 does fd = perf_event_open(pid => T1->pid);
Now, both T1 and T2 do close(fd), call task_work_run(), dequeue the ____fput work, and finally call __fput(). Now suppose that both perf events fire before T1/T2 call perf_event_release_kernel/_free_event.
Now, T1 and T2 will hang forever in perf_pending_task_sync() waiting for each other. task_work_cancel(current) can't succeed with or without this patch.
No?
Oleg.
Quoted 25 lines · Oleg Nesterov
Quoted 9 lines · Sebastian Andrzej Siewior
Yi and syzbot managed to hang the task within
task_run().The problem is
task_work_run()->__fput()->perf_release()->perf_event_release_kernel()->_free_event()->perf_pending_task_sync()->task_work_cancel()failed ->rcuwait_wait_event().Quoted 3 lines
Once
task_work_run()is running, the list of callbacks removed from thetask_structand from this point ontask_work_cancel()can't remove any pending and not yet started work items.But can this patch really solve the problem?
Suppose we have two tasks, T1 and T2.
T1 does fd =
perf_event_open(pid => T2->pid);
T2 does fd =perf_event_open(pid => T1->pid);Now, both T1 and T2 do close(fd), call
task_work_run(), dequeue the____fputwork, and finally call__fput(). Now suppose that both perf events fire before T1/T2 call perf_event_release_kernel/_free_event.
Quoted 5 lines
Now, T1 and T2 will hang forever in
perf_pending_task_sync()waiting for each other.task_work_cancel(current)can't succeed with or without this patch.No?
Duh!
So indeed, the wait/wake based solution is too fragile. Are we back to the old serialized workqueue days flavour of deadlocks with task work?
Anyway the perf_pending_task()'s put_event() based solution thing should fix that scenario too.
Thanks.