exec/exit: POSIX timer related bugfixes and related cleanups
Subsystems (4)
Thomas Gleixner
maintainercover exec/exit: POSIX timer related bugfixes and related cleanups
Recent findings from Hyunwoo unearthed two bugs in handling POSIX timers on exec().
The relevant patches, reports and discussions can be found here:
https://patch.msgid.link/aok1rdkBgZsynHZB@v4bel
https://patch.msgid.link/ao7Q8miiuLAPVnWv@v4bel
TLDR:
Both problems are related to non-leader exec(). POSIX CPU timers which are targeted at tasks hold a pid reference of the target task, which is used to look up the task in the related POSIX timer operations.
The non-leader exec() switches the TID of the old and the new leader, which obviously invalidates these references for pid_task(PIDTYPE_PID) lookups.
This causes UAFs due to the resulting list corruptions or premature freeing without removing the underlying POSIX CPU timers from the involved tasks.
The first issue which corrupts the signal pending list is solved by:
- Preventing the queueing of per task signals on a task which has PF_EXITING set.
- Protecting the unlocked setting of PF_EXITING in exit_signals() with sighand lock.
- Flushing all per task signals right in exit_signals()
The second issue which keeps the POSIX CPU timers queued on the new leader is solved by:
- Moving the exec related POSIX timer cleanup right after de_thread() which ensures that the timers queued in new_leader::posix_cputimers are removed before the underlying POSIX timers are deleted.
After looking deeper at the exit() handling it turned out that the POSIX timer cleanups can be done early in do_exit() instead of delaying them until release_task().
The reason for this late cleanup is that POSIX CPU timers can be created, rearmed and deleted as long as a task is visible, i.e. the pid is hashed and sighand is not NULL. This allows to retrieve information from the timer up to the point where the task is gone for real and that can't be changed easily as that'd be a user visible change.
But once PF_EXITING is set on a task the task does not longer expire POSIX CPU timers. So it makes no sense that the timers stay queued in task::posix_cputimers after that point.
The only thing which needs to be prevented is that timers are requeued on task::posix_cputimers once PF_EXITING is set or requeued on signal::posix_cputimers when PF_EXITING is set and signal::live is zero, which indicates that the thread group is dead.
With that solved the timers can be dequeued from task::posix_cputimer pending when a task exits and from signal::posix_cputimer pending once the threadgroup reaches the dead state, i.e. signal::live goes to zero.
The series applies on 7.3-rc1 and is avalaible from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git posix-timers
Thanks,
tglx
--- fs/exec.c | 18 ++++--- include/linux/posix-timers.h | 39 ++-------------- include/linux/sched/task.h | 1 kernel/exit.c | 24 +++------ kernel/signal.c | 70 ++++++++++++++++------------ kernel/time/posix-cpu-timers.c | 99 +++++++++++++++++++++++++++++++++++++---- kernel/time/posix-timers.c | 26 ++++++++-- kernel/time/posix-timers.h | 3 + 8 files changed, 179 insertions(+), 101 deletions(-)
Thomas Gleixner
maintainerPATCH 1/8 signal: Prevent exec() race
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.
---
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(-)
kernel/exit.c +6 -5
@@ -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);
kernel/signal.c +41 -29
119 lines not on this page · raw message
Re: [patch 1/8] signal: Prevent exec() race
Thomas, I can't read this series today, will try on Weekend.
But you didn't reply to
https://lore.kernel.org/all/[email protected]/
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.
Thomas Gleixner
maintainerRe: [patch 1/8] signal: Prevent exec() race
Quoted 6 lines · Oleg Nesterov
Thomas, I can't read this series today, will try on Weekend.
But you didn't reply to
https://lore.kernel.org/all/[email protected]/
Let me quote that email here:
Quoted 8 lines
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 checkPF_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;
+
Quoted 2 lines
Or perhaps it makes more sense to check
PF_EXITINGinposixtimer_get_target()?
No. Because that's outside of the sighand locked region and therefore can't provide a reliable answer.
Thanks,
tglx
Frederic Weisbecker
maintainerRe: [patch 1/8] signal: Prevent exec() race
Quoted 19 lines · Thomas Gleixner
Quoted 6 lines · Oleg Nesterov
Thomas, I can't read this series today, will try on Weekend.
But you didn't reply to
https://lore.kernel.org/all/[email protected]/
Let me quote that email here:Quoted 8 lines
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 checkPF_EXITING, I guess it should check it too?From the patch:
kernel/signal.c void posixtimer_send_sigqueue(struct k_i · line 2009
Quoted 6 lines
kernel/signal.c +3
-0@@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_iif (!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.
Signature · 2 lines
Frederic Weisbecker SUSE Labs
Thomas Gleixner
maintainerRe: [patch 1/8] signal: Prevent exec() race
Quoted 23 lines · Frederic Weisbecker
Quoted 22 lines · Thomas Gleixner
Quoted 6 lines · Oleg Nesterov
Thomas, I can't read this series today, will try on Weekend.
But you didn't reply to
https://lore.kernel.org/all/[email protected]/
Let me quote that email here:Quoted 8 lines
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 checkPF_EXITING, I guess it should check it too?From the patch:
kernel/signal.c +0
-0@@ -1990,6 +2004,9 @@ void posixtimer_send_sigqueue(struct k_iif (!likely (lock_task_sighand (t, &flags)))return ;
Quoted 6 lines
kernel/signal.c void posixtimer_send_sigqueue(struct k_i · line 2009
Quoted 3 lines
kernel/signal.c +3
-0+if (unlikely (tmr->it_pid_type ==PIDTYPE_PID && (t->flags &PF_EXITING ))) + goto unlock; +But
posixtimer_sig_unignore()calls directlyposixtimer_queue_sigqueue(), notposixtimer_send_sigqueue().
I obviously can't read.
Quoted 5 lines
However
posixtimer_queue_sigqueue()->complete_signal()->wants_signal()checksPF_EXITINGandcomplete_signal()then just drops it.Ah but doesn't that leak a refcount drop? Looks like the
PF_EXITINGcheck should indeed move toposixtimer_get_target().
Again. get_target() is not protected by sighand lock when called in posxtimer_send_sigqueue().
Let me stare at it some more.
Thomas Gleixner
maintainerPATCH 2/8 exec: Cleanup POSIX timers right after de_thread()
In reply to Thomas GleixnerFrom: Hyunwoo Kim <[email protected]>
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() ]
---
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(-)
fs/exec.c +21 -8
@@ -1115,6 +1115,17 @@ static struct file *bprm_identity_file(creturn 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. */
Thomas Gleixner
maintainerPATCH 3/8 posix-timers: Move posixtimer_exec_cleanup() out of exec.c
In reply to Thomas GleixnerMove it to the POSIX timer code and provide a proper stub when POSIX timers are disabled in Kconfig.
No functional change.
--- fs/exec.c | 13 +------------ include/linux/posix-timers.h | 3 +++ kernel/time/posix-timers.c | 9 +++++++++ 3 files changed, 13 insertions(+), 12 deletions(-)
fs/exec.c +1 -12
@@ -1115,17 +1115,6 @@ static struct file *bprm_identity_file(creturn 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 ;
include/linux/posix-timers.h +3 -0
@@ -232,6 +232,8 @@ void set_process_cpu_timer(struct task_sint 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 */
kernel/time/posix-timers.c +9 -0
@@ -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) {
Thomas Gleixner
maintainerPATCH 4/8 posix-timers: Move POSIX timer group exit related code out of do_exit()
In reply to Thomas GleixnerMove 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.
--- 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(-)
include/linux/posix-timers.h +2 -0
@@ -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 */
include/linux/sched/task.h +0 -1
@@ -94,7 +94,6 @@ static inline void exit_thread(struct taextern __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,
kernel/exit.c +3 -4
@@ -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 ();
kernel/time/posix-timers.c +9 -7
@@ -1077,13 +1077,9 @@ SYSCALL_DEFINE1(timer_delete, timer_t, treturn 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 (); }
Thomas Gleixner
maintainerPATCH 5/8 posix-cpu-timers: Move inlines out of public header
In reply to Thomas GleixnerThey are only used in the POSIX CPU timer code. No point in exposing them globally and parsing them for nothing.
--- include/linux/posix-timers.h | 32 -------------------------------- kernel/time/posix-cpu-timers.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 32 deletions(-)
include/linux/posix-timers.h +0 -32
@@ -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));
kernel/time/posix-cpu-timers.c +32 -0
@@ -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
Thomas Gleixner
maintainerPATCH 6/8 posix-cpu-timers: Use PF_EXITING to indicate exit
In reply to Thomas GleixnerRight 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.
--- kernel/time/posix-cpu-timers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +1 -1
@@ -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 ;/*
Thomas Gleixner
maintainerPATCH 7/8 posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
In reply to Thomas GleixnerTo 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.
--- kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
kernel/time/posix-cpu-timers.c +27 -0
@@ -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(structcleanup_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 *tstruct 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 ;
Re: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
Quoted 26 lines · Thomas Gleixner
To prepare for cleaning up POSIX CPU timers in
do_exit(), prevent enqueueing POSIX CPU timers on a task which hasPF_EXITINGset.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 <[email protected]>--- kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)kernel/time/posix-cpu-timers.c +1
-0@@ -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 ; }
kernel/time/posix-cpu-timers.c void posix_cpu_timers_exit_group(struct · line 684
Quoted 10 lines
kernel/time/posix-cpu-timers.c +7
-0@@ -674,6 +675,15 @@ void posix_cpu_timers_exit_group(structcleanup_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);kernel/time/posix-cpu-timers.c void posix_cpu_timers_exit_group(struct · line 685
Quoted 1 line
kernel/time/posix-cpu-timers.c +1
-0+}
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.
Quoted 28 lines
kernel/time/posix-cpu-timers.c +17
-0/* * 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 *tstruct 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 ;
Re: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
"Eric W. Biederman" <[email protected]> writes:
Quoted 43 lines · 2> Thomas Gleixner <[email protected]> writes:
Quoted 27 lines
To prepare for cleaning up POSIX CPU timers in
do_exit(), prevent enqueueing POSIX CPU timers on a task which hasPF_EXITINGset.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 <[email protected]>--- kernel/time/posix-cpu-timers.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)kernel/time/posix-cpu-timers.c +1
-0@@ -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 ; }kernel/time/posix-cpu-timers.c void posix_cpu_timers_exit_group(struct · line 684
Quoted 10 lines
kernel/time/posix-cpu-timers.c +7
-0@@ -674,6 +675,15 @@ void posix_cpu_timers_exit_group(structcleanup_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);
Quoted 8 lines
kernel/time/posix-cpu-timers.c void posix_cpu_timers_exit_group(struct · line 685
Quoted 1 line
kernel/time/posix-cpu-timers.c +1
-0+}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->livespreading to more places. In the future that has the potential to complicate any changes to thegroup_deadcalculation.
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.
Quoted 20 lines
Quoted 20 lines
kernel/time/posix-cpu-timers.c +12
-0/* * 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 *tstruct 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
Quoted 8 lines
kernel/time/posix-cpu-timers.c static void arm_timer(struct k_itimer *t · line 717
Quoted 8 lines
kernel/time/posix-cpu-timers.c +5
-0+* per process timers. +*/ +if (unlikely (!task_can_enqueue (timer, p))) +return ; +if (!cpu_timer_enqueue (&base->tqhead, ctmr))return ;
Eric
Thomas Gleixner
maintainerRe: [patch 7/8] posix-cpu-timers: Prevent enqueueing when PF_EXITING is set
Quoted 44 lines · Eric W. Biederman
Quoted 16 lines · Eric W. Biederman
kernel/time/posix-cpu-timers.c void posix_cpu_timers_exit_group(struct · line 684
Quoted 8 lines · Thomas Gleixner
kernel/time/posix-cpu-timers.c +7
-0+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);Quoted 8 lines
Quoted 1 line
+}
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->livespreading to more places. In the future that has the potential to complicate any changes to thegroup_deadcalculation.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; }
Quoted 7 lines
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 asSIGNAL_GROUP_EXITis 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
Thomas Gleixner
maintainerPATCH 8/8 posix-timers: Handle exit in do_exit() completely
In reply to Thomas GleixnerNow 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.
--- 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(-)
include/linux/posix-timers.h +2 -4
@@ -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 */
kernel/exit.c +2 -8
@@ -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 ();
kernel/time/posix-cpu-timers.c +31 -7
@@ -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(structmutex_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_timerlockdep_posixtimer_exit (); } +static inline void posix_cpu_timers_exit_work (void ) { } +static void posix_cpu_timer_wait_running (struct k_itimer *timr) {cpu_relax ();
kernel/time/posix-timers.c +9 -6
@@ -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 (); }
kernel/time/posix-timers.h +3 -0
@@ -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 );