context_tracking: A few improvements
Subsystems (2)
cover context_tracking: A few improvements
Hi,
A few updates that I plan to push in a few days:
_ Fix recursion issues (rare crashes reported)
_ Optimize TIF_NOHZ propagation
_ Some __init tags
git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
nohz/core
HEAD: bf7d3e11e99120f6c435cc709842e9b82431c003
Thanks,
Frederic
---
Frederic Weisbecker (3):
context_tracking: Protect against recursion
context_tracking: Inherit TIF_NOHZ through forks instead of context switches
context_tracking: Tag init code
include/linux/context_tracking.h | 10 ----- include/linux/context_tracking_state.h | 1 + kernel/context_tracking.c | 76 ++++++++++++++++++++++++---------- kernel/sched/core.c | 1 - 4 files changed, 55 insertions(+), 33 deletions(-)
PATCH 1/3 context_tracking: Protect against recursion
Context tracking recursion can happen when an exception triggers in the middle of a call to a context tracking probe.
This special case can be caused by vmalloc faults. If an access to a memory area allocated by vmalloc happens in the middle of context_tracking_enter(), we may run into an endless fault loop because the exception in turn calls context_tracking_enter() which faults on the same vmalloc'ed memory, triggering an exception again, etc...
Some rare crashes have been reported so lets protect against this with a recursion counter.
--- include/linux/context_tracking_state.h | 1 + kernel/context_tracking.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/linux/context_tracking_state.h +1 -0
@@ -12,6 +12,7 @@ struct context_tracking {* may be further optimized using static keys. */ bool active; +int recursion;enum ctx_state {CONTEXT_KERNEL =0 ,CONTEXT_USER ,
kernel/context_tracking.c +30 -0
@@ -38,6 +38,25 @@ void context_tracking_cpu_set(int cpu) } } +static bool context_tracking_recursion_enter (void ) +{ +int recursion; + + recursion =__this_cpu_inc_return (context_tracking.recursion); +if (recursion ==1 ) +return true; + +WARN_ONCE ((recursion <1 ),"Invalid context tracking recursion value %d\n" , recursion); +__this_cpu_dec (context_tracking.recursion); + +return false; +} + +static void context_tracking_recursion_exit (void ) +{ +__this_cpu_dec (context_tracking.recursion); +} +/** * context_tracking_enter - Inform the context tracking that the CPU is going * enter user or guest space mode.
@@ -75,6 +94,11 @@ void context_tracking_enter(enum ctx_state state)WARN_ON_ONCE (!current->mm);local_irq_save (flags); +if (!context_tracking_recursion_enter ()) { +local_irq_restore (flags); +return ; + } +if (__this_cpu_read (context_tracking.state) != state) {if (__this_cpu_read (context_tracking.active)) {/*
@@ -105,6 +129,7 @@ void context_tracking_enter(enum ctx_state state)*/ __this_cpu_write (context_tracking.state, state); } +context_tracking_recursion_exit ();local_irq_restore (flags); }NOKPROBE_SYMBOL (context_tracking_enter);
@@ -139,6 +164,10 @@ void context_tracking_exit(enum ctx_state state)return ;local_irq_save (flags); +if (!context_tracking_recursion_enter ()) { +local_irq_restore (flags); +return ; + }if (__this_cpu_read (context_tracking.state) == state) {if (__this_cpu_read (context_tracking.active)) {/*
@@ -153,6 +182,7 @@ void context_tracking_exit(enum ctx_state state) }__this_cpu_write (context_tracking.state,CONTEXT_KERNEL ); } +context_tracking_recursion_exit ();local_irq_restore (flags); }NOKPROBE_SYMBOL (context_tracking_exit);
Signature · 1 line
2.1.4
Re: [PATCH 1/3] context_tracking: Protect against recursion
Quoted 12 lines · Frederic Weisbecker
Context tracking recursion can happen when an exception triggers in the middle of a call to a context tracking probe.
This special case can be caused by vmalloc faults. If an access to a memory area allocated by vmalloc happens in the middle of
context_tracking_enter(), we may run into an endless fault loop because the exception in turn callscontext_tracking_enter()which faults on the same vmalloc'ed memory, triggering an exception again, etc...Some rare crashes have been reported so lets protect against this with a recursion counter.
Quoted 9 lines
Reported-by Dave Jones <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rik van Riel <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Ingo Molnar <[email protected]>Cc Dave Jones <[email protected]>Cc Oleg Nesterov <[email protected]>
PATCH 2/3 context_tracking: Inherit TIF_NOHZ through forks instead of context switches
In reply to Frederic WeisbeckerTIF_NOHZ is used by context_tracking to force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.
It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
--- include/linux/context_tracking.h | 10 -------- kernel/context_tracking.c | 52 +++++++++++++++++++++------------------- kernel/sched/core.c | 1 - 3 files changed, 27 insertions(+), 36 deletions(-)
include/linux/context_tracking.h +0 -10
@@ -14,8 +14,6 @@ extern void context_tracking_enter(enum ctx_state state);extern void context_tracking_exit (enum ctx_state state);extern void context_tracking_user_enter (void );extern void context_tracking_user_exit (void ); -extern void __context_tracking_task_switch (struct task_struct *prev, -struct task_struct *next);static inline void user_enter (void ) {
@@ -51,19 +49,11 @@ static inline void exception_exit(enum ctx_state prev_ctx) } } -static inline void context_tracking_task_switch (struct task_struct *prev, -struct task_struct *next) -{ -if (context_tracking_is_enabled ()) -__context_tracking_task_switch (prev, next); -}#else static inline void user_enter (void ) { }static inline void user_exit (void ) { }static inline enum ctx_state exception_enter (void ) {return 0 ; }static inline void exception_exit (enum ctx_state prev_ctx) { } -static inline void context_tracking_task_switch (struct task_struct *prev, -struct task_struct *next) { }#endif /* !CONFIG_CONTEXT_TRACKING */
kernel/context_tracking.c +27 -25
65 lines not on this page · raw message
kernel/sched/core.c +0 -1
@@ -2312,7 +2312,6 @@ context_switch(struct rq *rq, struct task_struct *prev,*/ spin_release (&rq->lock.dep_map,1 , _THIS_IP_); -context_tracking_task_switch (prev, next);/* Here we just switch the register state and the stack. */ switch_to (prev, next, prev);barrier ();
Signature · 1 line
2.1.4
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
Quoted 11 lines · Frederic Weisbecker
TIF_NOHZis used bycontext_trackingto force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
Quoted 9 lines
Suggested-by Oleg Nesterov <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rik van Riel <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Ingo Molnar <[email protected]>Cc Dave Jones <[email protected]>Cc Oleg Nesterov <[email protected]>
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
In reply to Frederic WeisbeckerQuoted 22 lines · Frederic Weisbecker
kernel/context_tracking.c +18
-2+void context_tracking_cpu_set (int cpu) { -clear_tsk_thread_flag (prev,TIF_NOHZ ); -set_tsk_thread_flag (next,TIF_NOHZ ); +static bool initialized = false; +struct task_struct *p, *t; +unsigned long flags; + +if (!per_cpu (context_tracking.active, cpu)) { +per_cpu (context_tracking.active, cpu) = true; +static_key_slow_inc (&context_tracking_enabled); + } + +if (initialized) +return ; + +set_tsk_thread_flag (&init_task,TIF_NOHZ ); + +/* +* There shouldn't be any thread at this early boot stage +* but the scheduler is ready to host any. So lets walk
kernel/context_tracking.c void context_tracking_user_exit(void) · line 217
Quoted 10 lines
kernel/context_tracking.c +9
-0+* the tasklist just in case. tasklist_lock isn't necessary +* either that early but take it for correctness checkers. +*/ +read_lock_irqsave (&tasklist_lock, flags); +for_each_process_thread (p, t) +set_tsk_thread_flag (t,TIF_NOHZ ); +read_unlock_irqrestore (&tasklist_lock, flags); + + initialized = true; }
Agreed, but _irqsave is not needed. read_lock(tasklist) should work just fine.
Any reason 3/3 comes as a separate change? I won't argue, just curious.
Oleg.
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
Quoted 28 lines · Oleg Nesterov
Quoted 27 lines · Frederic Weisbecker
kernel/context_tracking.c +23
-2+void context_tracking_cpu_set (int cpu) { -clear_tsk_thread_flag (prev,TIF_NOHZ ); -set_tsk_thread_flag (next,TIF_NOHZ ); +static bool initialized = false; +struct task_struct *p, *t; +unsigned long flags; + +if (!per_cpu (context_tracking.active, cpu)) { +per_cpu (context_tracking.active, cpu) = true; +static_key_slow_inc (&context_tracking_enabled); + } + +if (initialized) +return ; + +set_tsk_thread_flag (&init_task,TIF_NOHZ ); + +/* +* There shouldn't be any thread at this early boot stage +* but the scheduler is ready to host any. So lets walk +* the tasklist just in case. tasklist_lock isn't necessary +* either that early but take it for correctness checkers. +*/ +read_lock_irqsave (&tasklist_lock, flags); +for_each_process_thread (p, t)
Quoted 8 lines
kernel/context_tracking.c void context_tracking_user_exit(void) · line 217
Quoted 5 lines
kernel/context_tracking.c +4
-0+set_tsk_thread_flag (t,TIF_NOHZ ); +read_unlock_irqrestore (&tasklist_lock, flags); + + initialized = true; }Agreed, but
_irqsaveis not needed.read_lock(tasklist)should work just fine.
Indeed, that was just in case we get more callers but then if it happens, it's their responsibility to fix the callee. So I'll just change that.
Quoted 2 lines
Any reason 3/3 comes as a separate change? I won't argue, just curious.
Nope, I should fold them indeed.
Thanks.
Quoted 3 lines
Oleg.
Peter Zijlstra
maintainerRe: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
In reply to Frederic WeisbeckerQuoted 11 lines · Frederic Weisbecker
TIF_NOHZis used bycontext_trackingto force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
One must ask, what's the point of the flag if everybody must always have it set?
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
TIF_NOHZis used bycontext_trackingto force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Quoted 6 lines
Quoted 3 lines
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
One must ask, what's the point of the flag if everybody must always have it set?
We already test this word full of flags in the syscall entry and exit path.
Testing this same word for an additional flag is cheaper than testing a different variable.
See the places in entry_{32,64}.S where do_notify_resume, syscall_trace_enter, syscall_trace_leave, etc get called. All are called as a result of testing flags in the same word.
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
Quoted 20 lines · Rik van Riel
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
TIF_NOHZis used bycontext_trackingto force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Quoted 6 lines
Quoted 3 lines
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
One must ask, what's the point of the flag if everybody must always have it set?
We already test this word full of flags in the syscall entry and exit path.
Quoted 7 lines
Testing this same word for an additional flag is cheaper than testing a different variable.
See the places in entry_{32,64}.S where
do_notify_resume,syscall_trace_enter,syscall_trace_leave, etc get called. All are called as a result of testing flags in the same word.
Indeed, we could add another check for a global flag but that's going to bloat the syscall fastpath.
Re: [PATCH 2/3] context_tracking: Inherit TIF_NOHZ through forks instead of context switches
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
TIF_NOHZis used bycontext_trackingto force syscall slow-path on every task in order to track userspace roundtrips. As such, it must be set on all running tasks.It's currently explicitly inherited through context switches. There is no need to do it on this fast-path though. The flag could be simply set once for all on all tasks, whether they are running or not.
Quoted 6 lines
Quoted 3 lines
Lets do this by setting the flag to init task on early boot and let it propagate through fork inheritance.
One must ask, what's the point of the flag if everybody must always have it set?
To force the slow path in the syscall paths. Unfortunately it is not easy to change this ...
Oleg.
PATCH 3/3 context_tracking: Tag init code
In reply to Frederic WeisbeckerMark context_tracking_cpu_set() as init code, we only need it at early boot time.
--- kernel/context_tracking.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/context_tracking.c +2 -2
@@ -186,9 +186,9 @@ void context_tracking_user_exit(void) }NOKPROBE_SYMBOL (context_tracking_user_exit); -void context_tracking_cpu_set (int cpu) +void __init context_tracking_cpu_set (int cpu) { -static bool initialized = false; +static __initdata bool initialized = false;struct task_struct *p, *t;unsigned long flags;
Signature · 1 line
2.1.4
Re: [PATCH 3/3] context_tracking: Tag init code
Quoted 11 lines · Frederic Weisbecker
Mark
context_tracking_cpu_set()as init code, we only need it at early boot time.Signed-off-by Frederic Weisbecker <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Rik van Riel <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Ingo Molnar <[email protected]>Cc Dave Jones <[email protected]>Cc Oleg Nesterov <[email protected]>