* [patch 1/8] signal: Prevent exec() race
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:35 ` Oleg Nesterov
2026-09-04 11:22 ` [patch 2/8] exec: Cleanup POSIX timers right after de_thread() Thomas Gleixner
` (6 subsequent siblings)
7 siblings, 1 reply; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman, stable
Hyunwoo debugged the following KASAN UAF splat:
BUG: KASAN: slab-use-after-free in __send_signal_locked+0xb27/0xba0
Write of size 8 at addr ffff888007ed80c8 by task poc/79
...
Call Trace:
__send_signal_locked+0xb27/0xba0
do_send_sig_info+0xa7/0x160
do_send_specific+0x76/0xa0
__x64_sys_tgkill+0x193/0x270
...
Allocated by task 80:
do_timer_create+0x1a4/0x1030
__x64_sys_timer_create+0x145/0x190
...
Freed by task 12:
kmem_cache_free_bulk+0x1f8/0x4a0
kvfree_rcu_bulk+0x14f/0x1c0
kfree_rcu_work+0x128/0x1a0
...
Last potentially related work creation:
kvfree_call_rcu+0x39/0x390
__flush_itimer_signals+0x211/0x320
flush_itimer_signals+0x47/0x90
begin_new_exec+0xa6b/0x28c0
It turned out that this happens with a non-leader exec() as Hyunwoo
explained:
de_thread() calls exchange_tids() before release_task(leader), so the
struct pid held by a SIGEV_THREAD_ID timer created against the leader's tid
now points to the thread which called execve(). pid_task() returns that
thread and lock_task_sighand() on it succeeds.
If the timer signal is blocked, its sigqueue stays queued on the leader's
task::pending. The next expiry of that timer can then run while
release_task() flushes the queue.
posixtimer_send_sigqueue() checks whether the sigqueue is already queued
with a plain list_empty(), which only reads list_head::next.
list_del_init() is not atomic and INIT_LIST_HEAD() stores list_head::next
before list_head::prev, so the check can pass in between. list_add_tail()
queues the entry on the task::pending of the live thread, and the
list_head::prev store from the flush then overwrites the list_head::prev
link that list_add_tail() has just set.
__flush_itimer_signals() does not undo that either. With list_head::prev
pointing at the entry itself, its list_del_init() only stores the same
values again, so the entry is not removed from the list. It is still there
after the last reference is dropped and the timer is freed by RCU, and the
list_add_tail() of a later tgkill() follows that list_head::prev into the
freed timer.
This problem surfaced with the recent commit which moved the sigqueue flush
out of the sighand lock held region.
Hyonwoo proposed to fix this by using list_del_init_careful(), but that
just papers over the problem. After some disucssions and various attempts
to solve it, Eric pointed out that there is no reason to flush
task::pending late in release_task() and it should be done in
exit_signals() already.
As nothing can collect and deliver signals which are queued in a dying
task's pending queue, there is no reason to delay it further.
But it has to be ensured that no signals can be queued into it after that
point. exit_signals() sets PF_EXITING in task::flags, which can be used as
an indicator for this.
Cure it by:
- Preventing signal queueing for task private signals (PIDTYPE_PID) when
the task has PF_EXITING set in __send_signal_locked() and in
posixtimer_send_sigqueue().
- Protecting the unlocked setting of PF_EXITING in exit_signals() for the
task group empty and the group exit case with sighand lock
- Flushing task::pending signals right there.
Optimize that by moving the whole pending list to an on-stack list head
under sighand lock and free the signals without the lock held.
Fixes: fb3bbcfe344e ("exit: change the release_task() paths to call flush_sigqueue() lockless")
Reported-by: Hyunwoo Kim <imv4bel@gmail.com>
Debugged-by: Hyunwoo Kim <imv4bel@gmail.com>
Suggested-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Cc: stable@vger.kernel.org
Closes: https://patch.msgid.link/aok1rdkBgZsynHZB@v4bel
---
V3: Restructure code and fix the missing unlock - Oleg
V2: Don't flush w/o sighand lock held - Oleg
Move the while pending list under the lock and free it lockless
---
kernel/exit.c | 11 ++++----
kernel/signal.c | 70 ++++++++++++++++++++++++++++++++------------------------
2 files changed, 47 insertions(+), 34 deletions(-)
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -299,12 +299,13 @@ void release_task(struct task_struct *p)
free_pids(post.pids);
release_thread(p);
/*
- * This task was already removed from the process/thread/pid lists
- * and lock_task_sighand(p) can't succeed. Nobody else can touch
- * ->pending or, if group dead, signal->shared_pending. We can call
- * flush_sigqueue() lockless.
+ * This task was already removed from the process/thread/pid lists and
+ * lock_task_sighand(p) can't succeed. If it's the group leader then
+ * flush tsk->signal->shared_pending. tsk->pending has been flushed
+ * already in exit_signals(). Nothing else can touch
+ * signal->shared_pending anymore, so flush_sigqueue() can be invoked
+ * lockless.
*/
- flush_sigqueue(&p->pending);
if (thread_group_leader(p))
flush_sigqueue(&p->signal->shared_pending);
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -457,18 +457,28 @@ static void __sigqueue_free(struct sigqu
kmem_cache_free(sigqueue_cachep, q);
}
-void flush_sigqueue(struct sigpending *queue)
+static void flush_sigqueue_list(struct list_head *head)
{
- struct sigqueue *q;
+ struct sigqueue *q, *tmp;
- sigemptyset(&queue->signal);
- while (!list_empty(&queue->list)) {
- q = list_entry(queue->list.next, struct sigqueue , list);
+ list_for_each_entry_safe(q, tmp, head, list) {
list_del_init(&q->list);
__sigqueue_free(q);
}
}
+void flush_sigqueue(struct sigpending *queue)
+{
+ sigemptyset(&queue->signal);
+ flush_sigqueue_list(&queue->list);
+}
+
+static void sigqueue_dequeue_pending(struct sigpending *queue, struct list_head *head)
+{
+ sigemptyset(&queue->signal);
+ list_splice_init(&queue->list, head);
+}
+
/*
* Flush all pending signals for this kthread.
*/
@@ -1030,6 +1040,10 @@ static int __send_signal_locked(int sig,
lockdep_assert_held(&t->sighand->siglock);
result = TRACE_SIGNAL_IGNORED;
+
+ if (unlikely(type == PIDTYPE_PID && (t->flags & PF_EXITING)))
+ goto ret;
+
if (!prepare_signal(sig, t, force))
goto ret;
@@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_i
if (!likely(lock_task_sighand(t, &flags)))
return;
+ if (unlikely(tmr->it_pid_type == PIDTYPE_PID && (t->flags & PF_EXITING)))
+ goto unlock;
+
/*
* Update @tmr::sigqueue_seq for posix timer signals with sighand
* locked to prevent a race against dequeue_signal().
@@ -2081,6 +2098,7 @@ void posixtimer_send_sigqueue(struct k_i
result = TRACE_SIGNAL_DELIVERED;
out:
trace_signal_generate(sig, &q->info, t, tmr->it_pid_type != PIDTYPE_PID, result);
+unlock:
unlock_task_sighand(t, &flags);
}
@@ -3120,42 +3138,36 @@ static void retarget_shared_pending(stru
void exit_signals(struct task_struct *tsk)
{
+ LIST_HEAD(sigq_list);
int group_stop = 0;
- sigset_t unblocked;
/*
* @tsk is about to have PF_EXITING set - lock out users which
- * expect stable threadgroup.
+ * expect a stable threadgroup.
*/
cgroup_threadgroup_change_begin(tsk);
- if (thread_group_empty(tsk) || (tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
+ scoped_guard(spinlock_irq, &tsk->sighand->siglock) {
tsk->flags |= PF_EXITING;
- cgroup_threadgroup_change_end(tsk);
- return;
- }
- spin_lock_irq(&tsk->sighand->siglock);
- /*
- * From now this task is not visible for group-wide signals,
- * see wants_signal(), do_signal_stop().
- */
- tsk->flags |= PF_EXITING;
+ sigqueue_dequeue_pending(&tsk->pending, &sigq_list);
- cgroup_threadgroup_change_end(tsk);
+ if (task_sigpending(tsk) && !thread_group_empty(tsk) &&
+ !(tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
+ sigset_t unblocked = tsk->blocked;
+
+ signotset(&unblocked);
+ retarget_shared_pending(tsk, &unblocked);
+
+ if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
+ task_participate_group_stop(tsk))
+ group_stop = CLD_STOPPED;
+ }
+ }
- if (!task_sigpending(tsk))
- goto out;
+ cgroup_threadgroup_change_end(tsk);
- unblocked = tsk->blocked;
- signotset(&unblocked);
- retarget_shared_pending(tsk, &unblocked);
-
- if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
- task_participate_group_stop(tsk))
- group_stop = CLD_STOPPED;
-out:
- spin_unlock_irq(&tsk->sighand->siglock);
+ flush_sigqueue_list(&sigq_list);
/*
* If group stop has completed, deliver the notification. This
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [patch 1/8] signal: Prevent exec() race
2026-09-04 11:22 ` [patch 1/8] signal: Prevent exec() race Thomas Gleixner
@ 2026-09-04 11:35 ` Oleg Nesterov
2026-09-05 7:34 ` Thomas Gleixner
0 siblings, 1 reply; 16+ messages in thread
From: Oleg Nesterov @ 2026-09-04 11:35 UTC (permalink / raw)
To: Thomas Gleixner
Cc: LKML, Hyunwoo Kim, Frederic Weisbecker, Christian Brauner,
Peter Zijlstra, John Stultz, Ingo Molnar, Alexander Viro,
Eric W. Biederman, stable
Thomas, I can't read this series today, will try on Weekend.
But you didn't reply to
https://lore.kernel.org/all/apgHJj0qH7HlAXKO@redhat.com/
Let me quote that email here:
OK... lets suppose the exiting task T passes exit_signals().
Suppose we have an "ignored" timer tmr. Another sub-thread calls
do_sigaction() -> posixtimer_sig_unignore() and finds that tmr
in ->ignored_posix_timers list.
But posixtimer_queue_sigqueue() doesn't check PF_EXITING, I guess
it should check it too?
Or perhaps it makes more sense to check PF_EXITING in
posixtimer_get_target() ?
and at first glance it seems that this version doesn't differ in this respect?
Sorry if I missed something (again ;) but I'd better ask just in case.
Oleg.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [patch 1/8] signal: Prevent exec() race
2026-09-04 11:35 ` Oleg Nesterov
@ 2026-09-05 7:34 ` Thomas Gleixner
2026-09-05 7:50 ` Frederic Weisbecker
0 siblings, 1 reply; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-05 7:34 UTC (permalink / raw)
To: Oleg Nesterov
Cc: LKML, Hyunwoo Kim, Frederic Weisbecker, Christian Brauner,
Peter Zijlstra, John Stultz, Ingo Molnar, Alexander Viro,
Eric W. Biederman, stable
On Fri, Sep 04 2026 at 13:35, Oleg Nesterov wrote:
> Thomas, I can't read this series today, will try on Weekend.
>
> But you didn't reply to
> https://lore.kernel.org/all/apgHJj0qH7HlAXKO@redhat.com/
> Let me quote that email here:
>
> OK... lets suppose the exiting task T passes exit_signals().
>
> Suppose we have an "ignored" timer tmr. Another sub-thread calls
> do_sigaction() -> posixtimer_sig_unignore() and finds that tmr
> in ->ignored_posix_timers list.
>
> But posixtimer_queue_sigqueue() doesn't check PF_EXITING, I guess
> it should check it too?
From the patch:
@@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_i
if (!likely(lock_task_sighand(t, &flags)))
return;
+ if (unlikely(tmr->it_pid_type == PIDTYPE_PID && (t->flags & PF_EXITING)))
+ goto unlock;
+
> Or perhaps it makes more sense to check PF_EXITING in
> posixtimer_get_target() ?
No. Because that's outside of the sighand locked region and therefore
can't provide a reliable answer.
Thanks,
tglx
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [patch 1/8] signal: Prevent exec() race
2026-09-05 7:34 ` Thomas Gleixner
@ 2026-09-05 7:50 ` Frederic Weisbecker
2026-09-05 11:22 ` Thomas Gleixner
0 siblings, 1 reply; 16+ messages in thread
From: Frederic Weisbecker @ 2026-09-05 7:50 UTC (permalink / raw)
To: Thomas Gleixner
Cc: Oleg Nesterov, LKML, Hyunwoo Kim, Christian Brauner,
Peter Zijlstra, John Stultz, Ingo Molnar, Alexander Viro,
Eric W. Biederman, stable
Le Sat, Sep 05, 2026 at 09:34:52AM +0200, Thomas Gleixner a écrit :
> On Fri, Sep 04 2026 at 13:35, Oleg Nesterov wrote:
> > Thomas, I can't read this series today, will try on Weekend.
> >
> > But you didn't reply to
> > https://lore.kernel.org/all/apgHJj0qH7HlAXKO@redhat.com/
> > Let me quote that email here:
> >
> > OK... lets suppose the exiting task T passes exit_signals().
> >
> > Suppose we have an "ignored" timer tmr. Another sub-thread calls
> > do_sigaction() -> posixtimer_sig_unignore() and finds that tmr
> > in ->ignored_posix_timers list.
> >
> > But posixtimer_queue_sigqueue() doesn't check PF_EXITING, I guess
> > it should check it too?
>
> From the patch:
>
> @@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_i
> if (!likely(lock_task_sighand(t, &flags)))
> return;
>
> + if (unlikely(tmr->it_pid_type == PIDTYPE_PID && (t->flags & PF_EXITING)))
> + goto unlock;
> +
But posixtimer_sig_unignore() calls directly posixtimer_queue_sigqueue(), not
posixtimer_send_sigqueue().
However posixtimer_queue_sigqueue() -> complete_signal() -> wants_signal()
checks PF_EXITING and complete_signal() then just drops it.
Ah but doesn't that leak a refcount drop? Looks like the PF_EXITING check should
indeed move to posixtimer_get_target().
Thanks.
--
Frederic Weisbecker
SUSE Labs
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [patch 1/8] signal: Prevent exec() race
2026-09-05 7:50 ` Frederic Weisbecker
@ 2026-09-05 11:22 ` Thomas Gleixner
0 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-05 11:22 UTC (permalink / raw)
To: Frederic Weisbecker
Cc: Oleg Nesterov, LKML, Hyunwoo Kim, Christian Brauner,
Peter Zijlstra, John Stultz, Ingo Molnar, Alexander Viro,
Eric W. Biederman, stable
On Sat, Sep 05 2026 at 09:50, Frederic Weisbecker wrote:
> Le Sat, Sep 05, 2026 at 09:34:52AM +0200, Thomas Gleixner a écrit :
>> On Fri, Sep 04 2026 at 13:35, Oleg Nesterov wrote:
>> > Thomas, I can't read this series today, will try on Weekend.
>> >
>> > But you didn't reply to
>> > https://lore.kernel.org/all/apgHJj0qH7HlAXKO@redhat.com/
>> > Let me quote that email here:
>> >
>> > OK... lets suppose the exiting task T passes exit_signals().
>> >
>> > Suppose we have an "ignored" timer tmr. Another sub-thread calls
>> > do_sigaction() -> posixtimer_sig_unignore() and finds that tmr
>> > in ->ignored_posix_timers list.
>> >
>> > But posixtimer_queue_sigqueue() doesn't check PF_EXITING, I guess
>> > it should check it too?
>>
>> From the patch:
>>
>> @@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_i
>> if (!likely(lock_task_sighand(t, &flags)))
>> return;
>>
>> + if (unlikely(tmr->it_pid_type == PIDTYPE_PID && (t->flags & PF_EXITING)))
>> + goto unlock;
>> +
>
> But posixtimer_sig_unignore() calls directly posixtimer_queue_sigqueue(), not
> posixtimer_send_sigqueue().
I obviously can't read.
> However posixtimer_queue_sigqueue() -> complete_signal() -> wants_signal()
> checks PF_EXITING and complete_signal() then just drops it.
>
> Ah but doesn't that leak a refcount drop? Looks like the PF_EXITING check should
> indeed move to posixtimer_get_target().
Again. get_target() is not protected by sighand lock when called in
posxtimer_send_sigqueue().
Let me stare at it some more.
^ permalink raw reply [flat|nested] 16+ messages in thread
* [patch 2/8] exec: Cleanup POSIX timers right after de_thread()
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
2026-09-04 11:22 ` [patch 1/8] signal: Prevent exec() race Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:22 ` [patch 3/8] posix-timers: Move posixtimer_exec_cleanup() out of exec.c Thomas Gleixner
` (5 subsequent siblings)
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman, stable
From: Hyunwoo Kim <imv4bel@gmail.com>
A per-thread CPU timer holds a reference to the PID of the thread it is
attached to and, while it is armed, its node is queued in that thread's
posix_cputimers. The task is looked up by that PID.
When a non-leader thread exec()s, de_thread() changes which task owns
that PID. pid_task(timer->it.cpu.pid, PIDTYPE_PID) then returns NULL,
but the node is still queued on tsk, which is alive. timer_lock_sighand()
takes a failed lookup to mean that the node is already dequeued, so it
has nothing to undo.
begin_new_exec() calls posix_cpu_timers_exit(me) right after
exec_task_namespaces() and that removes the leftover node, so the state
normally stays invisible. But bprm->point_of_no_return is set before
de_thread(), so if unshare_files(), set_mm_exe_file(), exec_mmap() or
exec_task_namespaces() fails, the task dies before it gets there.
exit_itimers() then frees the k_itimer while its node is still queued,
and reaping tsk later erases that freed node from the rbtree.
In short:
the non-leader thread B the parent
timer_create(CLOCK_THREAD_CPUTIME_ID)
timer_settime()
arm_timer() // the node is queued on B
execve()
de_thread(B)
exchange_tids(B, leader) // B's PID now belongs to the leader
release_task(leader)
__exit_signal(leader)
posix_cpu_timers_exit(leader) // cleans leader's queue, not B's
__unhash_process(leader) // that PID has no task anymore
exec_mmap()
mmap_read_lock_killable(old_mm)
kill(B, SIGKILL)
// -EINTR
get_signal()
do_exit()
exit_itimers()
posix_timer_delete()
posix_cpu_timer_del()
posix_timer_unhash_and_free() // freed while still queued
wait4()
release_task(B)
posix_cpu_timers_exit(B)
cleanup_timerqueue()
timerqueue_del() // use-after-free
Move the POSIX timer cleanup right after de_thread() before any of the
later failure conditions brings the task into do_exit().
[ tglx: Move the cleanup right after de_thread() ]
Fixes: 55e8c8eb2c7b ("posix-cpu-timers: Store a reference to a pid not a task")
Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/ao7Q8miiuLAPVnWv@v4bel
---
Changes in v3:
Move the cleanup right after de_thread() - Oleg
Rework change log
Changes in v2:
- Add the trigger sequence and the KASAN log to the commit message.
- v1: https://lore.kernel.org/all/anfgrsPlUdwBhdrp@v4bel/
---
fs/exec.c | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1115,6 +1115,17 @@ static struct file *bprm_identity_file(c
return bprm->file;
}
+static void posixtimer_exec(struct task_struct *me)
+{
+#ifdef CONFIG_POSIX_TIMERS
+ spin_lock_irq(&me->sighand->siglock);
+ posix_cpu_timers_exit(me);
+ spin_unlock_irq(&me->sighand->siglock);
+ exit_itimers(me);
+ flush_itimer_signals();
+#endif
+}
+
/*
* Calling this is the point of no return. None of the failures will be
* seen by userspace since either the process is already taking a fatal
@@ -1152,6 +1163,16 @@ int begin_new_exec(struct linux_binprm *
retval = de_thread(me);
if (retval)
goto out;
+
+ /*
+ * This must be done here to ensure that POSIX CPU timers which were
+ * armed on the current task are dequeued from me::posix_cputimers.
+ * That ensures that in case of a TID switch the deletion of the related
+ * POSIX timer will not free an enqueued timer because the TID lookup
+ * failed as the original target TID was the old leader.
+ */
+ posixtimer_exec(me);
+
/* see the comment in check_unsafe_exec() */
current->fs->in_exec = 0;
/*
@@ -1192,14 +1213,6 @@ int begin_new_exec(struct linux_binprm *
if (retval)
goto out_unlock;
-#ifdef CONFIG_POSIX_TIMERS
- spin_lock_irq(&me->sighand->siglock);
- posix_cpu_timers_exit(me);
- spin_unlock_irq(&me->sighand->siglock);
- exit_itimers(me);
- flush_itimer_signals();
-#endif
-
/*
* Make the signal table private.
*/
^ permalink raw reply [flat|nested] 16+ messages in thread* [patch 3/8] posix-timers: Move posixtimer_exec_cleanup() out of exec.c
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
2026-09-04 11:22 ` [patch 1/8] signal: Prevent exec() race Thomas Gleixner
2026-09-04 11:22 ` [patch 2/8] exec: Cleanup POSIX timers right after de_thread() Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:22 ` [patch 4/8] posix-timers: Move POSIX timer group exit related code out of do_exit() Thomas Gleixner
` (4 subsequent siblings)
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
Move it to the POSIX timer code and provide a proper stub when POSIX timers
are disabled in Kconfig.
No functional change.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
fs/exec.c | 13 +------------
include/linux/posix-timers.h | 3 +++
kernel/time/posix-timers.c | 9 +++++++++
3 files changed, 13 insertions(+), 12 deletions(-)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1115,17 +1115,6 @@ static struct file *bprm_identity_file(c
return bprm->file;
}
-static void posixtimer_exec(struct task_struct *me)
-{
-#ifdef CONFIG_POSIX_TIMERS
- spin_lock_irq(&me->sighand->siglock);
- posix_cpu_timers_exit(me);
- spin_unlock_irq(&me->sighand->siglock);
- exit_itimers(me);
- flush_itimer_signals();
-#endif
-}
-
/*
* Calling this is the point of no return. None of the failures will be
* seen by userspace since either the process is already taking a fatal
@@ -1171,7 +1160,7 @@ int begin_new_exec(struct linux_binprm *
* POSIX timer will not free an enqueued timer because the TID lookup
* failed as the original target TID was the old leader.
*/
- posixtimer_exec(me);
+ posixtimer_exec();
/* see the comment in check_unsafe_exec() */
current->fs->in_exec = 0;
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -232,6 +232,8 @@ void set_process_cpu_timer(struct task_s
int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
#ifdef CONFIG_POSIX_TIMERS
+void posixtimer_exec(void);
+
static inline void posixtimer_putref(struct k_itimer *tmr)
{
if (rcuref_put(&tmr->rcuref))
@@ -259,6 +261,7 @@ static inline bool posixtimer_valid(cons
return !(val & 0x1UL);
}
#else /* CONFIG_POSIX_TIMERS */
+static inline void posixtimer_exec(void) { }
static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { }
static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { }
#endif /* !CONFIG_POSIX_TIMERS */
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -1120,6 +1120,15 @@ void exit_itimers(struct task_struct *ts
}
}
+void posixtimer_exec(void)
+{
+ scoped_guard(spinlock_irq, ¤t->sighand->siglock)
+ posix_cpu_timers_exit(current);
+
+ exit_itimers(current);
+ flush_itimer_signals();
+}
+
SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
const struct __kernel_timespec __user *, tp)
{
^ permalink raw reply [flat|nested] 16+ messages in thread* [patch 4/8] posix-timers: Move POSIX timer group exit related code out of do_exit()
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
` (2 preceding siblings ...)
2026-09-04 11:22 ` [patch 3/8] posix-timers: Move posixtimer_exec_cleanup() out of exec.c Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:22 ` [patch 5/8] posix-cpu-timers: Move inlines out of public header Thomas Gleixner
` (3 subsequent siblings)
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
Move the POSIX timer group exit handling into the posix timer code and
provide a proper stub when POSIX timers are disabled in Kconfig.
No functional change.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
include/linux/posix-timers.h | 2 ++
include/linux/sched/task.h | 1 -
kernel/exit.c | 7 +++----
kernel/time/posix-timers.c | 16 +++++++++-------
4 files changed, 14 insertions(+), 12 deletions(-)
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -233,6 +233,7 @@ int update_rlimit_cpu(struct task_struct
#ifdef CONFIG_POSIX_TIMERS
void posixtimer_exec(void);
+void posixtimer_exit(void);
static inline void posixtimer_putref(struct k_itimer *tmr)
{
@@ -262,6 +263,7 @@ static inline bool posixtimer_valid(cons
}
#else /* CONFIG_POSIX_TIMERS */
static inline void posixtimer_exec(void) { }
+static inline void posixtimer_exit(void) { }
static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { }
static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { }
#endif /* !CONFIG_POSIX_TIMERS */
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -94,7 +94,6 @@ static inline void exit_thread(struct ta
extern __noreturn void do_group_exit(int);
extern void exit_files(struct task_struct *);
-extern void exit_itimers(struct task_struct *);
extern pid_t kernel_clone(struct kernel_clone_args *kargs);
struct task_struct *copy_process(struct pid *pid, int trace, int node,
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -963,13 +963,12 @@ void __noreturn do_exit(long code)
panic("Attempted to kill init! exitcode=0x%08x\n",
tsk->signal->group_exit_code ?: (int)code);
-#ifdef CONFIG_POSIX_TIMERS
- hrtimer_cancel(&tsk->signal->real_timer);
- exit_itimers(tsk);
-#endif
+ posixtimer_exit();
+
if (tsk->mm)
setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
}
+
acct_collect(code, group_dead);
if (group_dead)
tty_audit_exit();
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -1077,13 +1077,9 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t
return 0;
}
-/*
- * Invoked from do_exit() when the last thread of a thread group exits.
- * At that point no other task can access the timers of the dying
- * task anymore.
- */
-void exit_itimers(struct task_struct *tsk)
+static void posixtimer_delete_timers(void)
{
+ struct task_struct *tsk = current;
struct hlist_head timers;
struct hlist_node *next;
struct k_itimer *timer;
@@ -1120,12 +1116,18 @@ void exit_itimers(struct task_struct *ts
}
}
+void posixtimer_exit(void)
+{
+ hrtimer_cancel(¤t->signal->real_timer);
+ posixtimer_delete_timers();
+}
+
void posixtimer_exec(void)
{
scoped_guard(spinlock_irq, ¤t->sighand->siglock)
posix_cpu_timers_exit(current);
- exit_itimers(current);
+ posixtimer_delete_timers();
flush_itimer_signals();
}
^ permalink raw reply [flat|nested] 16+ messages in thread* [patch 5/8] posix-cpu-timers: Move inlines out of public header
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
` (3 preceding siblings ...)
2026-09-04 11:22 ` [patch 4/8] posix-timers: Move POSIX timer group exit related code out of do_exit() Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:22 ` [patch 6/8] posix-cpu-timers: Use PF_EXITING to indicate exit Thomas Gleixner
` (2 subsequent siblings)
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
They are only used in the POSIX CPU timer code. No point in exposing them
globally and parsing them for nothing.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
include/linux/posix-timers.h | 32 --------------------------------
kernel/time/posix-cpu-timers.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 32 deletions(-)
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -66,38 +66,6 @@ struct cpu_timer {
struct task_struct __rcu *handling;
};
-static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
- struct cpu_timer *ctmr)
-{
- ctmr->head = head;
- return timerqueue_add(head, &ctmr->node);
-}
-
-static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
-{
- return !!ctmr->head;
-}
-
-static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
-{
- if (cpu_timer_queued(ctmr)) {
- timerqueue_del(ctmr->head, &ctmr->node);
- ctmr->head = NULL;
- return true;
- }
- return false;
-}
-
-static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
-{
- return ctmr->node.expires;
-}
-
-static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
-{
- ctmr->node.expires = exp;
-}
-
static inline void posix_cputimers_init(struct posix_cputimers *pct)
{
memset(pct, 0, sizeof(*pct));
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -438,6 +438,38 @@ static void trigger_base_recalc_expires(
base->nextevt = 0;
}
+static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
+ struct cpu_timer *ctmr)
+{
+ ctmr->head = head;
+ return timerqueue_add(head, &ctmr->node);
+}
+
+static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
+{
+ return !!ctmr->head;
+}
+
+static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
+{
+ if (cpu_timer_queued(ctmr)) {
+ timerqueue_del(ctmr->head, &ctmr->node);
+ ctmr->head = NULL;
+ return true;
+ }
+ return false;
+}
+
+static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr)
+{
+ return ctmr->node.expires;
+}
+
+static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp)
+{
+ ctmr->node.expires = exp;
+}
+
/*
* Dequeue the timer and reset the base if it was its earliest expiration.
* It makes sure the next tick recalculates the base next expiration so we
^ permalink raw reply [flat|nested] 16+ messages in thread* [patch 6/8] posix-cpu-timers: Use PF_EXITING to indicate exit
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
` (4 preceding siblings ...)
2026-09-04 11:22 ` [patch 5/8] posix-cpu-timers: Move inlines out of public header Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 11:22 ` [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set Thomas Gleixner
2026-09-04 11:22 ` [patch 8/8] posix-timers: Handle exit in do_exit() completely Thomas Gleixner
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
Right now POSIX CPU timers use task::exit_state to check whether a task is
exiting. That works correctly, but exit_state is set later in do_exit() and
too late for allowing to cleanup POSIX timers earlier.
It does not matter in case of exit whether the cutoff is a bit earlier.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
kernel/time/posix-cpu-timers.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -1505,7 +1505,7 @@ void run_posix_cpu_timers(void)
* posix_cpu_timer_del() may fail to lock_task_sighand(tsk) and
* miss timer->it.cpu.firing != 0.
*/
- if (tsk->exit_state)
+ if (tsk->flags & PF_EXITING)
return;
/*
^ permalink raw reply [flat|nested] 16+ messages in thread* [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
` (5 preceding siblings ...)
2026-09-04 11:22 ` [patch 6/8] posix-cpu-timers: Use PF_EXITING to indicate exit Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
2026-09-04 12:06 ` Eric W. Biederman
2026-09-04 11:22 ` [patch 8/8] posix-timers: Handle exit in do_exit() completely Thomas Gleixner
7 siblings, 1 reply; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
To prepare for cleaning up POSIX CPU timers in do_exit(), prevent
enqueueing POSIX CPU timers on a task which has PF_EXITING set.
Queueing a timer on such a task is pointless because the task won't expire
the timer anymore.
Pretending that the timer is armed allows to keep the POSIX timer mechanism
"working" so that the timer stays accessible up to the point where a task
is unhashed.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -628,6 +628,7 @@ static int posix_cpu_timer_del(struct k_
}
if (!ret) {
+ WARN_ON_ONCE(cpu_timer_queued(&timer->it.cpu));
put_pid(timer->it.cpu.pid);
timer->it_status = POSIX_TIMER_DISARMED;
}
@@ -674,6 +675,15 @@ void posix_cpu_timers_exit_group(struct
cleanup_timers(&tsk->signal->posix_cputimers);
}
+static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
+{
+ if (likely(!(p->flags & PF_EXITING)))
+ return true;
+
+ /* Allow TGID type unless the last thread is on the way out. */
+ return clock_pid_type(timer->it_clock) == PIDTYPE_TGID && atomic_read(&p->signal->live);
+}
+
/*
* Insert the timer on the appropriate list before any timers that
* expire later. This must be called with the sighand lock held.
@@ -684,7 +694,24 @@ static void arm_timer(struct k_itimer *t
struct cpu_timer *ctmr = &timer->it.cpu;
u64 newexp = cpu_timer_getexpires(ctmr);
+ lockdep_assert_held(&p->sighand->siglock);
+
timer->it_status = POSIX_TIMER_ARMED;
+
+ /*
+ * Don't enqueue timers when the task or the group is exiting. That
+ * ensures that timer operations are still succeeding as long as the
+ * tasks are visible, but won't enqueue the timers on the task or
+ * process. They won't expire anyway because run_posix_cpu_timers()
+ * exits early when PF_EXITING is set.
+ *
+ * Enqueue is skipped if PF_EXITING is set when the timer is per task
+ * and when the last thread decremented p::signal::live to zero also for
+ * per process timers.
+ */
+ if (unlikely(!task_can_enqueue(timer, p)))
+ return;
+
if (!cpu_timer_enqueue(&base->tqhead, ctmr))
return;
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
2026-09-04 11:22 ` [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set Thomas Gleixner
@ 2026-09-04 12:06 ` Eric W. Biederman
2026-09-04 15:47 ` Eric W. Biederman
0 siblings, 1 reply; 16+ messages in thread
From: Eric W. Biederman @ 2026-09-04 12:06 UTC (permalink / raw)
To: Thomas Gleixner
Cc: LKML, Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro
Thomas Gleixner <tglx@kernel.org> writes:
> To prepare for cleaning up POSIX CPU timers in do_exit(), prevent
> enqueueing POSIX CPU timers on a task which has PF_EXITING set.
>
> Queueing a timer on such a task is pointless because the task won't expire
> the timer anymore.
>
> Pretending that the timer is armed allows to keep the POSIX timer mechanism
> "working" so that the timer stays accessible up to the point where a task
> is unhashed.
>
> Signed-off-by: Thomas Gleixner <tglx@kernel.org>
> ---
> kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++
> 1 file changed, 27 insertions(+)
>
> --- a/kernel/time/posix-cpu-timers.c
> +++ b/kernel/time/posix-cpu-timers.c
> @@ -628,6 +628,7 @@ static int posix_cpu_timer_del(struct k_
> }
>
> if (!ret) {
> + WARN_ON_ONCE(cpu_timer_queued(&timer->it.cpu));
> put_pid(timer->it.cpu.pid);
> timer->it_status = POSIX_TIMER_DISARMED;
> }
> @@ -674,6 +675,15 @@ void posix_cpu_timers_exit_group(struct
> cleanup_timers(&tsk->signal->posix_cputimers);
> }
>
> +static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
> +{
> + if (likely(!(p->flags & PF_EXITING)))
> + return true;
> +
> + /* Allow TGID type unless the last thread is on the way out. */
> + return clock_pid_type(timer->it_clock) == PIDTYPE_TGID && atomic_read(&p->signal->live);
Couldn't this be?
/* Allow TGID type unless the group is on the way out. */
return (clock_pid_type(timer->it_clock) == PIDTYPE_TGID) &&
!(p->signal->flags & SIGNAL_GROUP_EXIT);
> +}
I don't understand why we would want to re-arm a timer after it has
been decided the group is dying.
Plush I really don't like the idea of p->signal->live spreading to more
places. In the future that has the potential to complicate any changes
to the group_dead calculation.
> /*
> * Insert the timer on the appropriate list before any timers that
> * expire later. This must be called with the sighand lock held.
> @@ -684,7 +694,24 @@ static void arm_timer(struct k_itimer *t
> struct cpu_timer *ctmr = &timer->it.cpu;
> u64 newexp = cpu_timer_getexpires(ctmr);
>
> + lockdep_assert_held(&p->sighand->siglock);
> +
> timer->it_status = POSIX_TIMER_ARMED;
> +
> + /*
> + * Don't enqueue timers when the task or the group is exiting. That
> + * ensures that timer operations are still succeeding as long as the
> + * tasks are visible, but won't enqueue the timers on the task or
> + * process. They won't expire anyway because run_posix_cpu_timers()
> + * exits early when PF_EXITING is set.
> + *
> + * Enqueue is skipped if PF_EXITING is set when the timer is per task
> + * and when the last thread decremented p::signal::live to zero also for
> + * per process timers.
> + */
> + if (unlikely(!task_can_enqueue(timer, p)))
> + return;
> +
> if (!cpu_timer_enqueue(&base->tqhead, ctmr))
> return;
>
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
2026-09-04 12:06 ` Eric W. Biederman
@ 2026-09-04 15:47 ` Eric W. Biederman
2026-09-05 7:50 ` Thomas Gleixner
0 siblings, 1 reply; 16+ messages in thread
From: Eric W. Biederman @ 2026-09-04 15:47 UTC (permalink / raw)
To: Thomas Gleixner
Cc: LKML, Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro
"Eric W. Biederman" <ebiederm@xmission.com> writes:
2> Thomas Gleixner <tglx@kernel.org> writes:
>
>> To prepare for cleaning up POSIX CPU timers in do_exit(), prevent
>> enqueueing POSIX CPU timers on a task which has PF_EXITING set.
>>
>> Queueing a timer on such a task is pointless because the task won't expire
>> the timer anymore.
>>
>> Pretending that the timer is armed allows to keep the POSIX timer mechanism
>> "working" so that the timer stays accessible up to the point where a task
>> is unhashed.
>>
>> Signed-off-by: Thomas Gleixner <tglx@kernel.org>
>> ---
>> kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++
>> 1 file changed, 27 insertions(+)
>>
>> --- a/kernel/time/posix-cpu-timers.c
>> +++ b/kernel/time/posix-cpu-timers.c
>> @@ -628,6 +628,7 @@ static int posix_cpu_timer_del(struct k_
>> }
>>
>> if (!ret) {
>> + WARN_ON_ONCE(cpu_timer_queued(&timer->it.cpu));
>> put_pid(timer->it.cpu.pid);
>> timer->it_status = POSIX_TIMER_DISARMED;
>> }
>> @@ -674,6 +675,15 @@ void posix_cpu_timers_exit_group(struct
>> cleanup_timers(&tsk->signal->posix_cputimers);
>> }
>>
>> +static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
>> +{
>> + if (likely(!(p->flags & PF_EXITING)))
>> + return true;
>> +
>> + /* Allow TGID type unless the last thread is on the way out. */
>> + return clock_pid_type(timer->it_clock) == PIDTYPE_TGID && atomic_read(&p->signal->live);
>
> Couldn't this be?
>
> /* Allow TGID type unless the group is on the way out. */
> return (clock_pid_type(timer->it_clock) == PIDTYPE_TGID) &&
> !(p->signal->flags & SIGNAL_GROUP_EXIT);
>> +}
>
> I don't understand why we would want to re-arm a timer after it has
> been decided the group is dying.
>
> Plush I really don't like the idea of p->signal->live spreading to more
> places. In the future that has the potential to complicate any changes
> to the group_dead calculation.
Hmm.
Now that I think about it this could be:
static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
{
/* Is the process exiting? */
if (signal->flags & SIGNAL_GROUP_EXIT)
return false;
/* Is the thread exiting? */
if ((clock_pid_type(timer->it_clock) == PIDTYPE_PID) &&
(p->flags & PF_EXITING))
return false;
return true;
}
Unless I am missing something subtle with the parts of shutdown. As
there is a difference between starting the shutdown, and having reached
do_exit.
But unless there is some subtle reason to start a timer after some
thread has called exit() or after a fatal signal has been received
I suspect stopping as soon as SIGNAL_GROUP_EXIT is set is a good idea.
>> /*
>> * Insert the timer on the appropriate list before any timers that
>> * expire later. This must be called with the sighand lock held.
>> @@ -684,7 +694,24 @@ static void arm_timer(struct k_itimer *t
>> struct cpu_timer *ctmr = &timer->it.cpu;
>> u64 newexp = cpu_timer_getexpires(ctmr);
>>
>> + lockdep_assert_held(&p->sighand->siglock);
>> +
>> timer->it_status = POSIX_TIMER_ARMED;
>> +
>> + /*
>> + * Don't enqueue timers when the task or the group is exiting. That
>> + * ensures that timer operations are still succeeding as long as the
>> + * tasks are visible, but won't enqueue the timers on the task or
>> + * process. They won't expire anyway because run_posix_cpu_timers()
>> + * exits early when PF_EXITING is set.
>> + *
>> + * Enqueue is skipped if PF_EXITING is set when the timer is per task
>> + * and when the last thread decremented p::signal::live to zero also for
>> + * per process timers.
>> + */
>> + if (unlikely(!task_can_enqueue(timer, p)))
>> + return;
>> +
>> if (!cpu_timer_enqueue(&base->tqhead, ctmr))
>> return;
>>
Eric
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
2026-09-04 15:47 ` Eric W. Biederman
@ 2026-09-05 7:50 ` Thomas Gleixner
0 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-05 7:50 UTC (permalink / raw)
To: Eric W. Biederman
Cc: LKML, Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro
On Fri, Sep 04 2026 at 10:47, Eric W. Biederman wrote:
> "Eric W. Biederman" <ebiederm@xmission.com> writes:
>> Thomas Gleixner <tglx@kernel.org> writes:
>>
>>>
>>> +static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
>>> +{
>>> + if (likely(!(p->flags & PF_EXITING)))
>>> + return true;
>>> +
>>> + /* Allow TGID type unless the last thread is on the way out. */
>>> + return clock_pid_type(timer->it_clock) == PIDTYPE_TGID && atomic_read(&p->signal->live);
>>
>> Couldn't this be?
>>
>> /* Allow TGID type unless the group is on the way out. */
>> return (clock_pid_type(timer->it_clock) == PIDTYPE_TGID) &&
>> !(p->signal->flags & SIGNAL_GROUP_EXIT);
>>> +}
>>
>> I don't understand why we would want to re-arm a timer after it has
>> been decided the group is dying.
>>
>> Plush I really don't like the idea of p->signal->live spreading to more
>> places. In the future that has the potential to complicate any changes
>> to the group_dead calculation.
>
> Hmm.
>
> Now that I think about it this could be:
>
> static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
> {
> /* Is the process exiting? */
> if (signal->flags & SIGNAL_GROUP_EXIT)
> return false;
>
> /* Is the thread exiting? */
> if ((clock_pid_type(timer->it_clock) == PIDTYPE_PID) &&
> (p->flags & PF_EXITING))
> return false;
>
> return true;
> }
>
> Unless I am missing something subtle with the parts of shutdown. As
> there is a difference between starting the shutdown, and having reached
> do_exit.
>
> But unless there is some subtle reason to start a timer after some
> thread has called exit() or after a fatal signal has been received
> I suspect stopping as soon as SIGNAL_GROUP_EXIT is set is a good idea.
I don't see a reason why that would be a problem. It puts the cutoff a
little bit earlier, but who cares. The task/process is going to exit no
matter what.
So the "observable" difference would be that a POSIX CPU timer which is
attached to the process by a different entity will stop firing a little
bit earlier. The resulting "fallout" is a purely academic problem IMO.
Thanks,
tglx
^ permalink raw reply [flat|nested] 16+ messages in thread
* [patch 8/8] posix-timers: Handle exit in do_exit() completely
2026-09-04 11:22 [patch 0/8] exec/exit: POSIX timer related bugfixes and related cleanups Thomas Gleixner
` (6 preceding siblings ...)
2026-09-04 11:22 ` [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set Thomas Gleixner
@ 2026-09-04 11:22 ` Thomas Gleixner
7 siblings, 0 replies; 16+ messages in thread
From: Thomas Gleixner @ 2026-09-04 11:22 UTC (permalink / raw)
To: LKML
Cc: Hyunwoo Kim, Oleg Nesterov, Frederic Weisbecker,
Christian Brauner, Peter Zijlstra, John Stultz, Ingo Molnar,
Alexander Viro, Eric W. Biederman
Now that POSIX CPU timers cannot be enqueued on a task after PF_EXITING is
set and process wide timers cannot be enqueued when PF_EXITING is set and
the last thread in the group is exiting, it is possible to mop up POSIX
timers in do_exit() completely.
This requires to cancel an eventually pending POSIX CPU timer task work
right there because do_exit() invokes exit_task_work() later, which would
be acting on torn down data.
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
---
include/linux/posix-timers.h | 6 ++----
kernel/exit.c | 10 ++--------
kernel/time/posix-cpu-timers.c | 38 +++++++++++++++++++++++++++++++-------
kernel/time/posix-timers.c | 15 +++++++++------
kernel/time/posix-timers.h | 3 +++
5 files changed, 47 insertions(+), 25 deletions(-)
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -192,8 +192,6 @@ struct k_itimer {
} ____cacheline_aligned_in_smp;
void run_posix_cpu_timers(void);
-void posix_cpu_timers_exit(struct task_struct *task);
-void posix_cpu_timers_exit_group(struct task_struct *task);
void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
u64 *newval, u64 *oldval);
@@ -201,7 +199,7 @@ int update_rlimit_cpu(struct task_struct
#ifdef CONFIG_POSIX_TIMERS
void posixtimer_exec(void);
-void posixtimer_exit(void);
+void posixtimer_exit(bool group_dead);
static inline void posixtimer_putref(struct k_itimer *tmr)
{
@@ -231,7 +229,7 @@ static inline bool posixtimer_valid(cons
}
#else /* CONFIG_POSIX_TIMERS */
static inline void posixtimer_exec(void) { }
-static inline void posixtimer_exit(void) { }
+static inline void posixtimer_exit(bool group_dead) { }
static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { }
static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { }
#endif /* !CONFIG_POSIX_TIMERS */
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -167,12 +167,6 @@ static void __exit_signal(struct release
lockdep_tasklist_lock_is_held());
spin_lock(&sighand->siglock);
-#ifdef CONFIG_POSIX_TIMERS
- posix_cpu_timers_exit(tsk);
- if (group_dead)
- posix_cpu_timers_exit_group(tsk);
-#endif
-
if (group_dead) {
tty = sig->tty;
sig->tty = NULL;
@@ -963,12 +957,12 @@ void __noreturn do_exit(long code)
panic("Attempted to kill init! exitcode=0x%08x\n",
tsk->signal->group_exit_code ?: (int)code);
- posixtimer_exit();
-
if (tsk->mm)
setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
}
+ posixtimer_exit(group_dead);
+
acct_collect(code, group_dead);
if (group_dead)
tty_audit_exit();
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -661,18 +661,29 @@ static void cleanup_timers(struct posix_
cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead);
}
+static inline void posix_cpu_timers_exit_work(void);
+
/*
- * These are both called with the siglock held, when the current thread
- * is being reaped. When the final (leader) thread in the group is reaped,
- * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
+ * Invoked from posixtimer_exit_task() after PF_EXITING was set in tsk::flags or
+ * from posixtimer_exec_cleanup().
*/
-void posix_cpu_timers_exit(struct task_struct *tsk)
+void posix_cpu_timers_exit_task(void)
{
- cleanup_timers(&tsk->posix_cputimers);
+ posix_cpu_timers_exit_work();
+
+ guard(spinlock_irq)(¤t->sighand->siglock);
+ cleanup_timers(¤t->posix_cputimers);
}
-void posix_cpu_timers_exit_group(struct task_struct *tsk)
+
+/*
+ * Invoked from posixtimer_exit_group() after PF_EXITING was set in tsk::flags.
+ */
+void posix_cpu_timers_exit_group(void)
{
- cleanup_timers(&tsk->signal->posix_cputimers);
+ posix_cpu_timers_exit_task();
+
+ guard(spinlock_irq)(¤t->sighand->siglock);
+ cleanup_timers(¤t->signal->posix_cputimers);
}
static inline bool task_can_enqueue(struct k_itimer *timer, struct task_struct *p)
@@ -1256,6 +1267,17 @@ static void posix_cpu_timers_work(struct
mutex_unlock(&cw->mutex);
}
+static inline void posix_cpu_timers_exit_work(void)
+{
+ /*
+ * current->flags has PF_EXITING set so this can be done lockless and
+ * with interrupts enabled as PF_EXITING prevents the interrupt from
+ * scheduling the work.
+ */
+ if (current->posix_cputimers_work.scheduled)
+ task_work_cancel(current, ¤t->posix_cputimers_work.work);
+}
+
/*
* Invoked from the posix-timer core when a cancel operation failed because
* the timer is marked firing. The caller holds rcu_read_lock(), which
@@ -1386,6 +1408,8 @@ static inline void __run_posix_cpu_timer
lockdep_posixtimer_exit();
}
+static inline void posix_cpu_timers_exit_work(void) { }
+
static void posix_cpu_timer_wait_running(struct k_itimer *timr)
{
cpu_relax();
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -1116,17 +1116,20 @@ static void posixtimer_delete_timers(voi
}
}
-void posixtimer_exit(void)
+void posixtimer_exit(bool group_dead)
{
- hrtimer_cancel(¤t->signal->real_timer);
- posixtimer_delete_timers();
+ if (group_dead) {
+ hrtimer_cancel(¤t->signal->real_timer);
+ posix_cpu_timers_exit_group();
+ posixtimer_delete_timers();
+ } else {
+ posix_cpu_timers_exit_task();
+ }
}
void posixtimer_exec(void)
{
- scoped_guard(spinlock_irq, ¤t->sighand->siglock)
- posix_cpu_timers_exit(current);
-
+ posix_cpu_timers_exit_task();
posixtimer_delete_timers();
flush_itimer_signals();
}
--- a/kernel/time/posix-timers.h
+++ b/kernel/time/posix-timers.h
@@ -51,3 +51,6 @@ int common_timer_set(struct k_itimer *ti
struct itimerspec64 *old_setting);
void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting);
int common_timer_del(struct k_itimer *timer);
+
+void posix_cpu_timers_exit_task(void);
+void posix_cpu_timers_exit_group(void);
^ permalink raw reply [flat|nested] 16+ messages in thread