softirq: Make it RT aware
Subsystems (3)
v2 cover softirq: Make it RT aware
RT runs softirq processing always in thread context and it requires that both the softirq execution and the BH disabled sections are preemptible.
This is achieved by serialization through per CPU local locks and substituting a few parts of the existing softirq processing code with helper functions.
The series applies on top of
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git irq/core
which already contains the irqstat cleanups and Frederic's irq time accounting changes.
Changes to V1 which can be found here:
https://lore.kernel.org/r/[email protected]
- Rebase on top of Frederic's irq time accounting changes
- Addressing the review comments
- Fixing the fallout from the irq time accounting updates
The RT variant has sucessfully been tested in the current 5.10-rt patches. For non-RT kernels there is no functional change.
Thanks,
tglx
--- include/linux/bottom_half.h | 8 + include/linux/hardirq.h | 1 include/linux/interrupt.h | 13 -- include/linux/preempt.h | 6 include/linux/rcupdate.h | 3 include/linux/sched.h | 3 kernel/sched/cputime.c | 4 kernel/softirq.c | 280 +++++++++++++++++++++++++++++++++++++++++--- kernel/time/tick-sched.c | 2 9 files changed, 291 insertions(+), 29 deletions(-)
PATCH v2 1/9 softirq: Add RT specific softirq accounting
From: Thomas Gleixner <[email protected]>
RT requires the softirq processing and local bottomhalf disabled regions to be preemptible. Using the normal preempt count based serialization is therefore not possible because this implicitely disables preemption.
RT kernels use a per CPU local lock to serialize bottomhalfs. As local_bh_disable() can nest the lock can only be acquired on the outermost invocation of local_bh_disable() and released when the nest count becomes zero. Tasks which hold the local lock can be preempted so its required to keep track of the nest count per task.
Add a RT only counter to task struct and adjust the relevant macros in preempt.h.
---
V2: Rewrote changelog.
--- include/linux/hardirq.h | 1 + include/linux/preempt.h | 6 +++++- include/linux/sched.h | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/hardirq.h +1 -0
@@ -6,6 +6,7 @@#include <linux/preempt.h> #include <linux/lockdep.h> #include <linux/ftrace_irq.h> +#include <linux/sched.h> #include <linux/vtime.h> #include <asm/hardirq.h>
include/linux/preempt.h +5 -1
@@ -79,7 +79,11 @@#define nmi_count () (preempt_count() & NMI_MASK)#define hardirq_count () (preempt_count() & HARDIRQ_MASK) -#define softirq_count () (preempt_count() & SOFTIRQ_MASK) +#ifdef CONFIG_PREEMPT_RT +# define softirq_count () (current->softirq_disable_cnt & SOFTIRQ_MASK) +#else +# define softirq_count () (preempt_count() & SOFTIRQ_MASK) +#endif #define irq_count () (nmi_count() | hardirq_count() | softirq_count())/*
include/linux/sched.h +3 -0
@@ -1004,6 +1004,9 @@ struct task_struct {int softirq_context;int irq_config;#endif +#ifdef CONFIG_PREEMPT_RT +int softirq_disable_cnt; +#endif #ifdef CONFIG_LOCKDEP # define MAX_LOCK_DEPTH 48UL
PATCH v2 2/9 irqtime: Make accounting correct on RT
In reply to Thomas Gleixnervtime_account_irq and irqtime_account_irq() base checks on preempt_count() which fails on RT because preempt_count() does not contain the softirq accounting which is seperate on RT.
These checks do not need the full preempt count as they only operate on the hard and softirq sections.
Use irq_count() instead which provides the correct value on both RT and non RT kernels. The compiler is clever enough to fold the masking for !RT:
99b: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax - 9a2: 25 ff ff ff 7f and $0x7fffffff,%eax + 9a2: 25 00 ff ff 00 and $0xffff00,%eax
---
V2: New patch
--- kernel/sched/cputime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/cputime.c +2 -2
@@ -60,7 +60,7 @@ void irqtime_account_irq(struct task_str cpu =smp_processor_id (); delta =sched_clock_cpu (cpu) - irqtime->irq_start_time; irqtime->irq_start_time += delta; - pc =preempt_count () - offset; + pc =irq_count () - offset;/* * We do not account for softirq time from ksoftirqd here.
@@ -421,7 +421,7 @@ void vtime_task_switch(struct task_strucvoid vtime_account_irq (struct task_struct *tsk,unsigned int offset) { -unsigned int pc =preempt_count () - offset; +unsigned int pc =irq_count () - offset;if (pc &HARDIRQ_OFFSET ) {vtime_account_hardirq (tsk);
PATCH v2 3/9 softirq: Move various protections into inline helpers
In reply to Thomas GleixnerTo allow reuse of the bulk of softirq processing code for RT and to avoid #ifdeffery all over the place, split protections for various code sections out into inline helpers so the RT variant can just replace them in one go.
---
V2: Adapt to Frederics rework
--- kernel/softirq.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-)
kernel/softirq.c +32 -7
@@ -204,6 +204,32 @@ void __local_bh_enable_ip(unsigned long }EXPORT_SYMBOL (__local_bh_enable_ip); +static inline void softirq_handle_begin (void ) +{ +__local_bh_disable_ip (_RET_IP_,SOFTIRQ_OFFSET ); +} + +static inline void softirq_handle_end (void ) +{ +__local_bh_enable (SOFTIRQ_OFFSET ); +WARN_ON_ONCE (in_interrupt ()); +} + +static inline void ksoftirqd_run_begin (void ) +{ +local_irq_disable (); +} + +static inline void ksoftirqd_run_end (void ) +{ +local_irq_enable (); +} + +static inline bool should_wake_ksoftirqd (void ) +{ +return true; +} +static inline void invoke_softirq (void ) {if (ksoftirqd_running (local_softirq_pending ()))
@@ -316,7 +342,7 @@ asmlinkage __visible void __softirq_entr pending =local_softirq_pending (); -__local_bh_disable_ip (_RET_IP_,SOFTIRQ_OFFSET ); +softirq_handle_begin (); in_hardirq =lockdep_softirq_start ();account_softirq_enter (current);
@@ -367,8 +393,7 @@ asmlinkage __visible void __softirq_entraccount_softirq_exit (current);lockdep_softirq_end (in_hardirq); -__local_bh_enable (SOFTIRQ_OFFSET ); -WARN_ON_ONCE (in_interrupt ()); +softirq_handle_end ();current_restore_flags (old_flags,PF_MEMALLOC ); }
@@ -463,7 +488,7 @@ inline void raise_softirq_irqoff(unsigne* Otherwise we wake up ksoftirqd to make sure we * schedule the softirq soon. */ -if (!in_interrupt ()) +if (!in_interrupt () &&should_wake_ksoftirqd ())wakeup_softirqd (); }
@@ -641,18 +666,18 @@ static int ksoftirqd_should_run(unsignedstatic void run_ksoftirqd (unsigned int cpu) { -local_irq_disable (); +ksoftirqd_run_begin ();if (local_softirq_pending ()) {/* * We can safely run softirq on inline stack, as we are not deep * in the task stack here. */ __do_softirq (); -local_irq_enable (); +ksoftirqd_run_end ();cond_resched ();return ; } -local_irq_enable (); +ksoftirqd_run_end (); }#ifdef CONFIG_HOTPLUG_CPU
PATCH v2 5/9 tick/sched: Prevent false positive softirq pending warnings on RT
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
On RT a task which has soft interrupts disabled can block on a lock and schedule out to idle while soft interrupts are pending. This triggers the warning in the NOHZ idle code which complains about going idle with pending soft interrupts. But as the task is blocked soft interrupt processing is temporarily blocked as well which means that such a warning is a false positive.
To prevent that check the per CPU state which indicates that a scheduled out task has soft interrupts disabled.
--- include/linux/bottom_half.h | 6 ++++++ kernel/softirq.c | 15 +++++++++++++++ kernel/time/tick-sched.c | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-)
include/linux/bottom_half.h +6 -0
@@ -32,4 +32,10 @@ static inline void local_bh_enable(void) __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET); } +#ifdef CONFIG_PREEMPT_RT +extern bool local_bh_blocked (void ); +#else +static inline bool local_bh_blocked (void ) {return false; } +#endif +#endif /* _LINUX_BH_H */
kernel/softirq.c +15 -0
@@ -138,6 +138,21 @@ static DEFINE_PER_CPU(struct softirq_ctr .lock = INIT_LOCAL_LOCK(softirq_ctrl.lock), }; +/** +* local_bh_blocked() - Check for idle whether BH processing is blocked +* +* Returns false if the per CPU softirq::cnt is 0 otherwise true. +* +* This is invoked from the idle task to guard against false positive +* softirq pending warnings, which would happen when the task which holds +* softirq_ctrl::lock was the only running task on the CPU and blocks on +* some other lock. +*/ +bool local_bh_blocked (void ) +{ +return this_cpu_read (softirq_ctrl.cnt) !=0 ; +} +void __local_bh_disable_ip (unsigned long ip,unsigned int cnt) {unsigned long flags;
kernel/time/tick-sched.c +1 -1
@@ -925,7 +925,7 @@ static bool can_stop_idle_tick(int cpu,if (unlikely (local_softirq_pending ())) {static int ratelimit; -if (ratelimit <10 && +if (ratelimit <10 && !local_bh_blocked () && (local_softirq_pending () &SOFTIRQ_STOP_IDLE_MASK )) {pr_warn ("NOHZ tick-stop error: Non-RCU local softirq work is pending, handler #%02x!!!\n" , (unsigned int )local_softirq_pending ());
PATCH v2 6/9 rcu: Prevent false positive softirq warning on RT
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Soft interrupt disabled sections can legitimately be preempted or schedule out when blocking on a lock on RT enabled kernels so the RCU preempt check warning has to be disabled for RT kernels.
--- include/linux/rcupdate.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/rcupdate.h +2 -1
@@ -319,7 +319,8 @@ static inline void rcu_preempt_sleep_che#define rcu_sleep_check () \ do { \ rcu_preempt_sleep_check(); \ - RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \ + RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ "Illegal context switch in RCU-bh read-side critical section"); \ RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \ "Illegal context switch in RCU-sched read-side critical section"); \
PATCH v2 8/9 tasklets: Use static inlines for stub implementations
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Inlines exist for a reason.
--- include/linux/interrupt.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/interrupt.h +3 -3
@@ -672,9 +672,9 @@ static inline void tasklet_unlock_wait(s cpu_relax(); }#else -#define tasklet_trylock (t) 1 -#define tasklet_unlock_wait (t) do { } while (0) -#define tasklet_unlock (t) do { } while (0) +static inline int tasklet_trylock (struct tasklet_struct *t) {return 1 ; } +static inline void tasklet_unlock (struct tasklet_struct *t) { } +static inline void tasklet_unlock_wait (struct tasklet_struct *t) { }#endif extern void __tasklet_schedule (struct tasklet_struct *t);
PATCH v2 9/9 tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
tasklet_kill() and tasklet_unlock_wait() spin and wait for the TASKLET_STATE_SCHED resp. TASKLET_STATE_RUN bit in the tasklet state to be cleared. This works on !RT nicely because the corresponding execution can only happen on a different CPU.
On RT softirq processing is preemptible, therefore a task preempting the softirq processing thread can spin forever.
Prevent this by invoking local_bh_disable()/enable() inside the loop. In case that the softirq processing thread was preempted by the current task, current will block on the local lock which yields the CPU to the preempted softirq processing thread. If the tasklet is processed on a different CPU then the local_bh_disable()/enable() pair is just a waste of processor cycles.
--- include/linux/interrupt.h | 8 ++------ kernel/softirq.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-)
include/linux/interrupt.h +2 -6
@@ -654,7 +654,7 @@ enumTASKLET_STATE_RUN /* Tasklet is running (SMP only) */ }; -#ifdef CONFIG_SMP +#if defined(CONFIG_SMP ) || defined(CONFIG_PREEMPT_RT )static inline int tasklet_trylock (struct tasklet_struct *t) {return !test_and_set_bit (TASKLET_STATE_RUN , &(t)->state);
@@ -666,11 +666,7 @@ static inline void tasklet_unlock(struct clear_bit(TASKLET_STATE_RUN, &(t)->state); } -static inline void tasklet_unlock_wait (struct tasklet_struct *t) -{ -while (test_bit (TASKLET_STATE_RUN , &(t)->state)) -cpu_relax (); -} +void tasklet_unlock_wait (struct tasklet_struct *t);#else static inline int tasklet_trylock (struct tasklet_struct *t) {return 1 ; }static inline void tasklet_unlock (struct tasklet_struct *t) { }
kernel/softirq.c +37 -1
@@ -818,6 +818,29 @@ void tasklet_init(struct tasklet_struct }EXPORT_SYMBOL (tasklet_init); +#if defined(CONFIG_SMP ) || defined(CONFIG_PREEMPT_RT ) + +void tasklet_unlock_wait (struct tasklet_struct *t) +{ +while (test_bit (TASKLET_STATE_RUN , &(t)->state)) { +if (IS_ENABLED (CONFIG_PREEMPT_RT )) { +/* +* Prevent a live lock when current preempted soft +* interrupt processing or prevents ksoftirqd from +* running. If the tasklet runs on a different CPU +* then this has no effect other than doing the BH +* disable/enable dance for nothing. +*/ +local_bh_disable (); +local_bh_enable (); + }else { +cpu_relax (); + } + } +} +EXPORT_SYMBOL (tasklet_unlock_wait); +#endif +void tasklet_kill (struct tasklet_struct *t) {if (in_interrupt ())
@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_structwhile (test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) {do { -yield (); +if (IS_ENABLED (CONFIG_PREEMPT_RT )) { +/* +* Prevent a live lock when current +* preempted soft interrupt processing or +* prevents ksoftirqd from running. If the +* tasklet runs on a different CPU then +* this has no effect other than doing the +* BH disable/enable dance for nothing. +*/ +local_bh_disable (); +local_bh_enable (); + }else { +yield (); + } }while (test_bit (TASKLET_STATE_SCHED , &t->state)); }tasklet_unlock_wait (t);
PATCH v2 4/9 softirq: Make softirq control and processing RT aware
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Provide a local lock based serialization for soft interrupts on RT which allows the local_bh_disabled() sections and servicing soft interrupts to be preemptible.
Provide the necessary inline helpers which allow to reuse the bulk of the softirq processing code.
---
V2: Adjusted to Frederic's changes
--- include/linux/bottom_half.h | 2 kernel/softirq.c | 188 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 182 insertions(+), 8 deletions(-)
include/linux/bottom_half.h +1 -1
@@ -4,7 +4,7 @@#include <linux/preempt.h> -#ifdef CONFIG_TRACE_IRQFLAGS +#if defined(CONFIG_PREEMPT_RT ) || defined(CONFIG_TRACE_IRQFLAGS )extern void __local_bh_disable_ip (unsigned long ip,unsigned int cnt);#else static __always_inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
kernel/softirq.c +181 -7
221 lines not on this page · raw message
PATCH v2 7/9 softirq: Replace barrier() with cpu_relax() in tasklet_unlock_wait()
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
A barrier() in a tight loop which waits for something to happen on a remote CPU is a pointless exercise. Replace it with cpu_relax() which allows HT siblings to make progress.
--- include/linux/interrupt.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/interrupt.h +2 -1
@@ -668,7 +668,8 @@ static inline void tasklet_unlock(structstatic inline void tasklet_unlock_wait (struct tasklet_struct *t) { -while (test_bit (TASKLET_STATE_RUN , &(t)->state)) {barrier (); } +while (test_bit (TASKLET_STATE_RUN , &(t)->state)) +cpu_relax (); }#else #define tasklet_trylock (t) 1
Re: [patch V2 6/9] rcu: Prevent false positive softirq warning on RT
In reply to Thomas GleixnerQuoted 7 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
Soft interrupt disabled sections can legitimately be preempted or schedule out when blocking on a lock on RT enabled kernels so the RCU preempt check warning has to be disabled for RT kernels.
Signed-off-by Thomas Gleixner <[email protected]>
Quoted 17 lines
--- include/linux/rcupdate.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/rcupdate.h +2
-1@@ -319,7 +319,8 @@ static inline void rcu_preempt_sleep_che#define rcu_sleep_check () \ do { \ rcu_preempt_sleep_check(); \ - RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \ + RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ "Illegal context switch in RCU-bh read-side critical section"); \ RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \ "Illegal context switch in RCU-sched read-side critical section"); \
Quoted 2 lines · Thomas Gleixner
The RT variant has sucessfully been tested in the current 5.10-rt patches. For non-RT kernels there is no functional change.
this series is part of v5.10-rc6-rt14.
Quoted 3 lines
Thanks,
tglx
Sebastian
Frederic Weisbecker
maintainerRe: [patch V2 2/9] irqtime: Make accounting correct on RT
In reply to Thomas GleixnerQuoted 24 lines · Thomas Gleixner
vtime_account_irqandirqtime_account_irq()base checks onpreempt_count()which fails on RT becausepreempt_count()does not contain the softirq accounting which is seperate on RT.These checks do not need the full preempt count as they only operate on the hard and softirq sections.
Use
irq_count()instead which provides the correct value on both RT and non RT kernels. The compiler is clever enough to fold the masking for !RT:99b: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax - 9a2: 25 ff ff ff 7f and $0x7fffffff,%eax + 9a2: 25 00 ff ff 00 and $0xffff00,%eaxReported-by Sebastian Andrzej Siewior <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>---
V2: New patch--- kernel/sched/cputime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/cputime.c void irqtime_account_irq(struct task_str · line 63
Quoted 5 lines
kernel/sched/cputime.c +1
-1@@ -60,7 +60,7 @@ void irqtime_account_irq(struct task_str cpu =smp_processor_id (); delta =sched_clock_cpu (cpu) - irqtime->irq_start_time; irqtime->irq_start_time += delta; - pc =preempt_count () - offset; + pc =irq_count () - offset;
There are many preempt_count() users all around waiting for similar issues. Wouldn't it be more reasonable to have current->softirq_disable_cnt just saving the softirq count on context switch?
kernel/sched/core.c +7 -0
@@ -3469,6 +3469,10 @@ static inline void prepare_task(struct task_struct *next)static inline void finish_task (struct task_struct *prev) { +#ifdef CONFIG_PREEMPT_RT + prev->softirq_disable_cnt =softirq_count (); +__preempt_count_sub (prev->softirq_disable_cnt); +#endif #ifdef CONFIG_SMP /* * This must be the very last reference to @prev from this CPU. After
@@ -3610,6 +3614,9 @@ static struct rq *finish_task_switch(struct task_struct *prev)vtime_task_switch (prev);perf_event_task_sched_in (prev, current);finish_task (prev); +#ifdef CONFIG_PREEMPT_RT +__preempt_count_add (current->softirq_disable_cnt); +#endif finish_lock_switch (rq);finish_arch_post_lock_switch ();kcov_finish_switch (current);
And I expect a few adjustments in schedule_debug() and atomic checks more generally but that should be it and it would probably be less error-prone. Although I'm probably overlooking other issues on the way.
Thanks.
Quoted 13 lines
kernel/sched/cputime.c +1
-1/* * We do not account for softirq time from ksoftirqd here. @@ -421,7 +421,7 @@ void vtime_task_switch(struct task_strucvoid vtime_account_irq (struct task_struct *tsk,unsigned int offset) { -unsigned int pc =preempt_count () - offset; +unsigned int pc =irq_count () - offset;if (pc &HARDIRQ_OFFSET ) {vtime_account_hardirq (tsk);
Re: [patch V2 2/9] irqtime: Make accounting correct on RT
Quoted 4 lines · Frederic Weisbecker
Quoted 4 lines
kernel/sched/cputime.c +0
-0@@ -60,7 +60,7 @@ void irqtime_account_irq(struct task_str cpu =smp_processor_id ();
Quoted 8 lines
kernel/sched/cputime.c void irqtime_account_irq(struct task_str · line 63
Quoted 4 lines
kernel/sched/cputime.c +1
-1delta =sched_clock_cpu (cpu) - irqtime->irq_start_time; irqtime->irq_start_time += delta; - pc =preempt_count () - offset; + pc =irq_count () - offset;There are many
preempt_count()users all around waiting for similar issues. Wouldn't it be more reasonable to havecurrent->softirq_disable_cntjust saving the softirq count on context switch?
There are not that many and all of them need to be looked at.
kernel/sched/core.c static inline void prepare_task(struct task_struct *next) · line 3475
Quoted 12 lines
kernel/sched/core.c +4
-0@@ -3469,6 +3469,10 @@ static inline void prepare_task(struct task_struct *next)static inline void finish_task (struct task_struct *prev) { +#ifdef CONFIG_PREEMPT_RT + prev->softirq_disable_cnt =softirq_count (); +__preempt_count_sub (prev->softirq_disable_cnt); +#endif
You fundamentaly break RT with that.
If local_bh_disable() fiddles with the actual preempt_count on RT then softirq disabled sections and softirq processing are not longer preemtible.
You asked me in the last round of patches to add a proper argument for pulling out the softirq count from preempt_count. Here is the revised changelog which you agreed with:
"RT requires the softirq processing and local bottomhalf disabled regions to
be preemptible. Using the normal preempt count based serialization is therefore not possible because this implicitely disables preemption.
....
"
Full text in patch 1/9.
According to the above folding of softirq count into the actual preempt count cannot work at all.
The current RT approach just works except for the places which look at the raw preempt_count and not using the wrappers. Those places are restricted to core code and a pretty small number.
Trying to do what you suggest would be a major surgery all over the place including a complete trainwreck on the highly optimized preempt_enable() --> preempt decision.
Quoted 2 lines
And I expect a few adjustments in
schedule_debug()and atomic checks more generally but that should be it and it would probably be less error-prone.
Dream on.
Quoted 1 line
Although I'm probably overlooking other issues on the way.
I think so.
Thanks,
tglx
Frederic Weisbecker
maintainerRe: [patch V2 2/9] irqtime: Make accounting correct on RT
Quoted 50 lines · Thomas Gleixner
Quoted 4 lines · Frederic Weisbecker
Quoted 4 lines
kernel/sched/cputime.c +0
-0@@ -60,7 +60,7 @@ void irqtime_account_irq(struct task_str cpu =smp_processor_id ();Quoted 8 lines
kernel/sched/cputime.c void irqtime_account_irq(struct task_str · line 63
Quoted 4 lines
kernel/sched/cputime.c +1
-1delta =sched_clock_cpu (cpu) - irqtime->irq_start_time; irqtime->irq_start_time += delta; - pc =preempt_count () - offset; + pc =irq_count () - offset;There are many
preempt_count()users all around waiting for similar issues. Wouldn't it be more reasonable to havecurrent->softirq_disable_cntjust saving the softirq count on context switch?There are not that many and all of them need to be looked at.
kernel/sched/core.c static inline void prepare_task(struct task_struct *next) · line 3475
Quoted 12 lines
kernel/sched/core.c +4
-0@@ -3469,6 +3469,10 @@ static inline void prepare_task(struct task_struct *next)static inline void finish_task (struct task_struct *prev) { +#ifdef CONFIG_PREEMPT_RT + prev->softirq_disable_cnt =softirq_count (); +__preempt_count_sub (prev->softirq_disable_cnt); +#endif You fundamentaly break RT with that.
If
local_bh_disable()fiddles with the actualpreempt_counton RT then softirq disabled sections and softirq processing are not longer preemtible.You asked me in the last round of patches to add a proper argument for pulling out the softirq count from
preempt_count. Here is the revised changelog which you agreed with:"RT requires the softirq processing and local bottomhalf disabled regions to
be preemptible. Using the normal preempt count based serialization is therefore not possible because this implicitely disables preemption.
....
"Full text in patch 1/9.
According to the above folding of softirq count into the actual preempt count cannot work at all.
Quoted 7 lines
The current RT approach just works except for the places which look at the raw
preempt_countand not using the wrappers. Those places are restricted to core code and a pretty small number.Trying to do what you suggest would be a major surgery all over the place including a complete trainwreck on the highly optimized
preempt_enable()--> preempt decision.
I suspected it was more complicated than I imagined :-) Nevermind.
Thanks.
Peter Zijlstra
maintainerRe: [patch V2 7/9] softirq: Replace barrier() with cpu_relax() in tasklet_unlock_wait()
In reply to Thomas GleixnerQuoted 14 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
A
barrier()in a tight loop which waits for something to happen on a remote CPU is a pointless exercise. Replace it withcpu_relax()which allows HT siblings to make progress.Signed-off-by Thomas Gleixner <[email protected]>--- include/linux/interrupt.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/interrupt.h static inline void tasklet_unlock(struct · line 673
Quoted 7 lines
include/linux/interrupt.h +2
-1@@ -668,7 +668,8 @@ static inline void tasklet_unlock(structstatic inline void tasklet_unlock_wait (struct tasklet_struct *t) { -while (test_bit (TASKLET_STATE_RUN , &(t)->state)) {barrier (); } +while (test_bit (TASKLET_STATE_RUN , &(t)->state)) +cpu_relax (); }
Wouldn't it be nicer to stick a completion in tasklet_struct ? Or at the very least use wait_var_event() or something?
include/linux/interrupt.h +4 -2
@@ -663,12 +663,14 @@ static inline int tasklet_trylock(struct tasklet_struct *t)static inline void tasklet_unlock (struct tasklet_struct *t) {smp_mb__before_atomic (); -clear_bit (TASKLET_STATE_RUN , &(t)->state); +clear_bit (TASKLET_STATE_RUN , &t->state); +smp_mb__after_atomic (); +wake_up_var (&t->state); }static inline void tasklet_unlock_wait (struct tasklet_struct *t) { -while (test_bit (TASKLET_STATE_RUN , &(t)->state)) {barrier (); } +wait_var_event (&t->state, !test_bit (TASKLET_STATE_RUN , &t->state)); }#else #define tasklet_trylock (t) 1
Peter Zijlstra
maintainerRe: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Thomas GleixnerReview void tasklet_kill(struct tasklet_struct · line 853
Quoted 8 lines · Thomas Gleixner
(quoted diff) +0
-1@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_struct while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { do { - yield(); } while (test_bit(TASKLET_STATE_SCHED, &t->state)); } tasklet_unlock_wait(t);
Egads... should we not start by doing something like this?
---
kernel/softirq.c +15 -8
@@ -529,6 +529,16 @@ void __tasklet_hi_schedule(struct tasklet_struct *t) }EXPORT_SYMBOL (__tasklet_hi_schedule); +static inline bool tasklet_clear_sched (struct tasklet_struct *t) +{ +if (test_and_clear_bit (TASKLET_STATE_SCHED , &t->state)) { +wake_up_var (&t->state); +return true; + } + +return false; +} +static void tasklet_action_common (struct softirq_action *a,struct tasklet_head *tl_head,unsigned int softirq_nr)
@@ -548,8 +558,7 @@ static void tasklet_action_common(struct softirq_action *a,if (tasklet_trylock (t)) {if (!atomic_read (&t->count)) { -if (!test_and_clear_bit (TASKLET_STATE_SCHED , - &t->state)) +if (!tasklet_clear_sched (t))BUG ();if (t->use_callback) t->callback (t);
@@ -609,13 +618,11 @@ void tasklet_kill(struct tasklet_struct *t)if (in_interrupt ())pr_notice ("Attempt to kill tasklet from interrupt\n" ); -while (test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) { -do { -yield (); - }while (test_bit (TASKLET_STATE_SCHED , &t->state)); - } +while (test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) +wait_var_event (&t->state, !test_bit (TASKLET_STATE_SCHED , &t->state)); +tasklet_unlock_wait (t); -clear_bit (TASKLET_STATE_SCHED , &t->state); +tasklet_clear_sched (t); }EXPORT_SYMBOL (tasklet_kill);
Re: [patch V2 1/9] softirq: Add RT specific softirq accounting
In reply to Thomas GleixnerQuoted 12 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
RT requires the softirq processing and local bottomhalf disabled regions to be preemptible. Using the normal preempt count based serialization is therefore not possible because this implicitely disables preemption.
RT kernels use a per CPU local lock to serialize bottomhalfs. As
local_bh_disable()can nest the lock can only be acquired on the outermost invocation oflocal_bh_disable()and released when the nest count becomes zero. Tasks which hold the local lock can be preempted so its required to keep track of the nest count per task.
Quoted 4 lines
Add a RT only counter to task struct and adjust the relevant macros in
preempt.h.Signed-off-by Thomas Gleixner <[email protected]>
Re: [patch V2 2/9] irqtime: Make accounting correct on RT
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
vtime_account_irqandirqtime_account_irq()base checks onpreempt_count()which fails on RT becausepreempt_count()does not contain the softirq accounting which is seperate on RT.These checks do not need the full preempt count as they only operate on the hard and softirq sections.
Use
irq_count()instead which provides the correct value on both RT and non RT kernels. The compiler is clever enough to fold the masking for !RT:
Quoted 6 lines
99b: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax - 9a2: 25 ff ff ff 7f and $0x7fffffff,%eax + 9a2: 25 00 ff ff 00 and $0xffff00,%eaxReported-by Sebastian Andrzej Siewior <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>
Also I'm seeing a few other explicit SOFTIRQ_MASK checks on top of preempt_count(), especially on RCU:
$ git grep SOFTIRQ_MASK
arch/sh/kernel/irq.c: (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
arch/sh/kernel/irq.c: (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
kernel/rcu/rcutorture.c: if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK))
kernel/rcu/tree_exp.h: if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
kernel/rcu/tree_plugin.h: !!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
kernel/rcu/tree_plugin.h: (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {I guess some RT RCU config take care of that?
Also tracing:
kernel/trace/ring_buffer.c: if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
kernel/trace/trace.c: ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0)
Thanks.
Re: [patch V2 3/9] softirq: Move various protections into inline helpers
In reply to Thomas GleixnerQuoted 5 lines · Thomas Gleixner
To allow reuse of the bulk of softirq processing code for RT and to avoid #ifdeffery all over the place, split protections for various code sections out into inline helpers so the RT variant can just replace them in one go.
Signed-off-by Thomas Gleixner <[email protected]>
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Peter ZijlstraQuoted 12 lines · Peter Zijlstra
Review void tasklet_kill(struct tasklet_struct · line 853
Quoted 8 lines · Thomas Gleixner
(quoted diff) +0
-1@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_struct while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { do { - yield(); } while (test_bit(TASKLET_STATE_SCHED, &t->state)); } tasklet_unlock_wait(t);Egads... should we not start by doing something like this?
So we keep the RT part as-is and replace the non-RT bits with this?
Sebastian
Frederic Weisbecker
maintainerRe: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Thomas GleixnerQuoted 5 lines · Thomas Gleixner
kernel/softirq.c +5
-0+void __local_bh_disable_ip (unsigned long ip,unsigned int cnt) +{ +unsigned long flags; +int newcnt; +
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 155
Quoted 10 lines
kernel/softirq.c +10
-0+WARN_ON_ONCE (in_hardirq ()); + +/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock); +/* Required to meet the RCU bottomhalf requirements. */ +rcu_read_lock (); + }else { +DEBUG_LOCKS_WARN_ON (this_cpu_read (softirq_ctrl.cnt));
So, to be clear this adds a new constraint where we can't call local_bh_disable() inside a preempt disabled section? I guess the rest of the RT code chased all the new offenders :-)
Quoted 22 lines
kernel/softirq.c +22
-0+ } + } + +/* +* Track the per CPU softirq disabled state. On RT this is per CPU +* state to allow preemption of bottom half disabled sections. +*/ + newcnt =__this_cpu_add_return (softirq_ctrl.cnt, cnt); +/* +* Reflect the result in the task state to prevent recursion on the +* local lock and to make softirq_count() & al work. +*/ + current->softirq_disable_cnt = newcnt; + +if (IS_ENABLED (CONFIG_TRACE_IRQFLAGS ) && newcnt == cnt) { +raw_local_irq_save (flags); +lockdep_softirqs_off (ip); +raw_local_irq_restore (flags); + } +} +EXPORT_SYMBOL (__local_bh_disable_ip); +
[...]
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 212
Quoted 13 lines
kernel/softirq.c +13
-0+ +void __local_bh_enable_ip (unsigned long ip,unsigned int cnt) +{ +bool preempt_on =preemptible (); +unsigned long flags; +u32 pending; +int curcnt; + +WARN_ON_ONCE (in_irq ()); +lockdep_assert_irqs_enabled (); + +local_irq_save (flags); + curcnt =this_cpu_read (softirq_ctrl.cnt);
__this_cpu_read() ?
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 219
Quoted 7 lines
kernel/softirq.c +7
-0+ +/* +* If this is not reenabling soft interrupts, no point in trying to +* run pending ones. +*/ +if (curcnt != cnt) + goto out;
I guess you could move the local_irq_save() here?
Quoted 16 lines
kernel/softirq.c +16
-0+ pending =local_softirq_pending (); +if (!pending ||ksoftirqd_running (pending)) + goto out; + +/* +* If this was called from non preemptible context, wake up the +* softirq daemon. +*/ +if (!preempt_on) { +wakeup_softirqd (); + goto out; + } + +/* +* Adjust softirq count to SOFTIRQ_OFFSET which makes +* in_serving_softirq() become true.
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 246
Quoted 10 lines
kernel/softirq.c +10
-0+*/ + cnt =SOFTIRQ_OFFSET ; +__local_bh_enable (cnt, false); +__do_softirq (); + +out: +__local_bh_enable (cnt, preempt_on); +local_irq_restore (flags); +} +EXPORT_SYMBOL (__local_bh_enable_ip);
Thanks.
Peter Zijlstra
maintainerRe: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Sebastian Andrzej SiewiorQuoted 14 lines · Sebastian Andrzej Siewior
Quoted 12 lines · Peter Zijlstra
Review void tasklet_kill(struct tasklet_struct · line 853
Quoted 8 lines · Thomas Gleixner
(quoted diff) +0
-1@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_struct while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { do { - yield(); } while (test_bit(TASKLET_STATE_SCHED, &t->state)); } tasklet_unlock_wait(t);Egads... should we not start by doing something like this?
Quoted 1 line
So we keep the RT part as-is and replace the non-RT bits with this?
For RT you probably want to wrap the wait_var_event() in that local_bh_disable()/enable() pear. I just figured those unbounded spin/yield loops suck and we should get rid of em.
Re: [patch V2 2/9] irqtime: Make accounting correct on RT
In reply to Frederic WeisbeckerQuoted 32 lines · Frederic Weisbecker
Quoted 10 lines · Thomas Gleixner
vtime_account_irqandirqtime_account_irq()base checks onpreempt_count()which fails on RT becausepreempt_count()does not contain the softirq accounting which is seperate on RT.These checks do not need the full preempt count as they only operate on the hard and softirq sections.
Use
irq_count()instead which provides the correct value on both RT and non RT kernels. The compiler is clever enough to fold the masking for !RT:Quoted 6 lines
99b: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax - 9a2: 25 ff ff ff 7f and $0x7fffffff,%eax + 9a2: 25 00 ff ff 00 and $0xffff00,%eaxReported-by Sebastian Andrzej Siewior <[email protected]>Signed-off-by Thomas Gleixner <[email protected]>Reviewed-by Frederic Weisbecker <[email protected]>Also I'm seeing a few other explicit
SOFTIRQ_MASKchecks on top ofpreempt_count(), especially on RCU:$ git grep SOFTIRQ_MASK arch/sh/kernel/irq.c: (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) | arch/sh/kernel/irq.c: (curctx->tinfo.preempt_count & SOFTIRQ_MASK); kernel/rcu/rcutorture.c: if (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK)) kernel/rcu/tree_exp.h: if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) || kernel/rcu/tree_plugin.h: !!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)); kernel/rcu/tree_plugin.h: (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
Quoted 1 line
I guess some RT RCU config take care of that?
I ignored sh so far and RCU has some quirks for RT vs. softirqs.
Quoted 4 lines
Also tracing:
kernel/trace/ring_buffer.c: if (!(pc & (NMI_MASK|HARDIRQ_MASK|SOFTIRQ_OFFSET)))
kernel/trace/trace.c: ((pc &SOFTIRQ_OFFSET) ?TRACE_FLAG_SOFTIRQ: 0)
Bah, I somehow lost the cure for that while rebasing.
Thanks,
tglx
Re: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Frederic WeisbeckerQuoted 12 lines · Frederic Weisbecker
Quoted 11 lines · Thomas Gleixner
kernel/softirq.c +11
-0+void __local_bh_disable_ip (unsigned long ip,unsigned int cnt) +{ +unsigned long flags; +int newcnt; + +WARN_ON_ONCE (in_hardirq ()); + +/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock);
Quoted 8 lines
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 155
Quoted 4 lines
kernel/softirq.c +4
-0+/* Required to meet the RCU bottomhalf requirements. */ +rcu_read_lock (); + }else { +DEBUG_LOCKS_WARN_ON (this_cpu_read (softirq_ctrl.cnt));So, to be clear this adds a new constraint where we can't call
local_bh_disable()inside a preempt disabled section? I guess the rest of the RT code chased all the new offenders :-)
There are not that many.
Quoted 23 lines
Quoted 22 lines
kernel/softirq.c +22
-0+ } + } + +/* +* Track the per CPU softirq disabled state. On RT this is per CPU +* state to allow preemption of bottom half disabled sections. +*/ + newcnt =__this_cpu_add_return (softirq_ctrl.cnt, cnt); +/* +* Reflect the result in the task state to prevent recursion on the +* local lock and to make softirq_count() & al work. +*/ + current->softirq_disable_cnt = newcnt; + +if (IS_ENABLED (CONFIG_TRACE_IRQFLAGS ) && newcnt == cnt) { +raw_local_irq_save (flags); +lockdep_softirqs_off (ip); +raw_local_irq_restore (flags); + } +} +EXPORT_SYMBOL (__local_bh_disable_ip); +[...]
Quoted 15 lines
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 212
Quoted 13 lines
kernel/softirq.c +13
-0+ +void __local_bh_enable_ip (unsigned long ip,unsigned int cnt) +{ +bool preempt_on =preemptible (); +unsigned long flags; +u32 pending; +int curcnt; + +WARN_ON_ONCE (in_irq ()); +lockdep_assert_irqs_enabled (); + +local_irq_save (flags); + curcnt =this_cpu_read (softirq_ctrl.cnt);
__this_cpu_read()?
Yes.
Quoted 9 lines
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 219
Quoted 7 lines
kernel/softirq.c +7
-0+ +/* +* If this is not reenabling soft interrupts, no point in trying to +* run pending ones. +*/ +if (curcnt != cnt) + goto out;I guess you could move the
local_irq_save()here?
Indeed.
Re: [patch V2 7/9] softirq: Replace barrier() with cpu_relax() in tasklet_unlock_wait()
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
include/linux/interrupt.h static inline int tasklet_trylock(struct tasklet_struct *t) · line 674
Quoted 6 lines · Thomas Gleixner
static inline void
tasklet_unlock_wait(struct tasklet_struct *t){include/linux/interrupt.h +2
-1-while (test_bit (TASKLET_STATE_RUN , &(t)->state)) {barrier (); } +while (test_bit (TASKLET_STATE_RUN , &(t)->state)) +cpu_relax (); }Wouldn't it be nicer to stick a completion in
tasklet_struct? Or at the very least usewait_var_event()or something?
It would be nicer. Just need to audit all possible callers. That would lose the implicit boosting of the preempted softirq thread, but these wait pathes should not be in any way relevant for user task latencies. Emphasis on should.
Lemme stare at the callers including the ones which have it wrapped into some other inline just because.
Thanks,
tglx
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Sebastian Andrzej SiewiorQuoted 14 lines · Sebastian Andrzej Siewior
Quoted 12 lines · Peter Zijlstra
Review void tasklet_kill(struct tasklet_struct · line 853
Quoted 8 lines · Thomas Gleixner
(quoted diff) +0
-1@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_struct while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { do { - yield(); } while (test_bit(TASKLET_STATE_SCHED, &t->state)); } tasklet_unlock_wait(t);Egads... should we not start by doing something like this?
Quoted 1 line
So we keep the RT part as-is and replace the non-RT bits with this?
No. It would work for both.
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
Quoted 4 lines · Thomas Gleixner
Quoted 1 line · Sebastian Andrzej Siewior
So we keep the RT part as-is and replace the non-RT bits with this?
No. It would work for both.
So instead of boosting our way through we simply wait until the tasklet completes. Given that canceling is usually done on start/stop events, it shouldn't matter if the RT priority is lost.
Sebastian
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Thomas Gleixner
Quoted 1 line · Sebastian Andrzej Siewior
So we keep the RT part as-is and replace the non-RT bits with this?
No. It would work for both.
So instead of boosting our way through we simply wait until the tasklet completes. Given that canceling is usually done on start/stop events, it shouldn't matter if the RT priority is lost.
That was my reasoning. The only thing we need to figure out whether there are callers on !RT which invoke that muck from non-sleepable context.
Thanks,
tglx
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
Quoted 7 lines · Thomas Gleixner
Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Thomas Gleixner
Quoted 1 line · Sebastian Andrzej Siewior
So we keep the RT part as-is and replace the non-RT bits with this?
No. It would work for both.
Quoted 7 lines
Quoted 3 lines
So instead of boosting our way through we simply wait until the tasklet completes. Given that canceling is usually done on start/stop events, it shouldn't matter if the RT priority is lost.
That was my reasoning. The only thing we need to figure out whether there are callers on !RT which invoke that muck from non-sleepable context.
The kill() variant is fine. It must be called from sleepable context. The otherone can be invoked everywhere except hard interrupt context IIRC.
Thanks,
tglx
Re: [patch V2 9/9] tasklets: Prevent kill/unlock_wait deadlock on RT
In reply to Peter ZijlstraQuoted 15 lines · Peter Zijlstra
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 12 lines · Peter Zijlstra
Review void tasklet_kill(struct tasklet_struct · line 853
Quoted 8 lines · Thomas Gleixner
(quoted diff) +0
-1@@ -825,7 +848,20 @@ void tasklet_kill(struct tasklet_struct while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { do { - yield(); } while (test_bit(TASKLET_STATE_SCHED, &t->state)); } tasklet_unlock_wait(t);Egads... should we not start by doing something like this?
Quoted 4 lines
Quoted 1 line
So we keep the RT part as-is and replace the non-RT bits with this?
For RT you probably want to wrap the
wait_var_event()in that local_bh_disable()/enable() pear.
Is that a new species local to the Netherlands? Never heard about bh-pears before. Are they tasty?
Quoted 2 lines
I just figured those unbounded spin/yield loops suck and we should get rid of em.
Ack.
Re: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Thomas Gleixnerkernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 184
Quoted 7 lines · Thomas Gleixner
kernel/softirq.c +7
-0+static void __local_bh_enable (unsigned int cnt,bool unlock) +{ +unsigned long flags; +int newcnt; + +DEBUG_LOCKS_WARN_ON (current->softirq_disable_cnt != +this_cpu_read (softirq_ctrl.cnt));
Less important since it's debug code, but still can be __this_cpu_read().
Quoted 9 lines
kernel/softirq.c +9
-0+ +if (IS_ENABLED (CONFIG_TRACE_IRQFLAGS ) &&softirq_count () == cnt) { +raw_local_irq_save (flags); +lockdep_softirqs_on (_RET_IP_); +raw_local_irq_restore (flags); + } + + newcnt =__this_cpu_sub_return (softirq_ctrl.cnt, cnt); + current->softirq_disable_cnt = newcnt;
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex
Quoted 10 lines
kernel/softirq.c +10
-0+ +if (!newcnt && unlock) { +rcu_read_unlock (); +local_unlock (&softirq_ctrl.lock); + } +} + +static inline bool should_wake_ksoftirqd (void ) +{ +return !this_cpu_read (softirq_ctrl.cnt);
And that too.
Other than these boring details:
Thanks.
Re: [patch V2 5/9] tick/sched: Prevent false positive softirq pending warnings on RT
In reply to Thomas GleixnerQuoted 41 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
On RT a task which has soft interrupts disabled can block on a lock and schedule out to idle while soft interrupts are pending. This triggers the warning in the NOHZ idle code which complains about going idle with pending soft interrupts. But as the task is blocked soft interrupt processing is temporarily blocked as well which means that such a warning is a false positive.
To prevent that check the per CPU state which indicates that a scheduled out task has soft interrupts disabled.
Signed-off-by Thomas Gleixner <[email protected]>--- include/linux/bottom_half.h | 6 ++++++ kernel/softirq.c | 15 +++++++++++++++ kernel/time/tick-sched.c | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-)include/linux/bottom_half.h +6
-0@@ -32,4 +32,10 @@ static inline void local_bh_enable(void) __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET); } +#ifdef CONFIG_PREEMPT_RT +extern bool local_bh_blocked (void ); +#else +static inline bool local_bh_blocked (void ) {return false; } +#endif +#endif /* _LINUX_BH_H */ kernel/softirq.c +3
-0@@ -138,6 +138,21 @@ static DEFINE_PER_CPU(struct softirq_ctr .lock = INIT_LOCAL_LOCK(softirq_ctrl.lock), }; +/** +* local_bh_blocked() - Check for idle whether BH processing is blocked +*
kernel/softirq.c static DEFINE_PER_CPU(struct softirq_ctr · line 153
Quoted 10 lines
kernel/softirq.c +10
-0+* Returns false if the per CPU softirq::cnt is 0 otherwise true. +* +* This is invoked from the idle task to guard against false positive +* softirq pending warnings, which would happen when the task which holds +* softirq_ctrl::lock was the only running task on the CPU and blocks on +* some other lock. +*/ +bool local_bh_blocked (void ) +{ +return this_cpu_read (softirq_ctrl.cnt) !=0 ;
__this_cpu_read()
Peter Zijlstra
maintainerRe: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Thomas GleixnerQuoted 19 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
Provide a local lock based serialization for soft interrupts on RT which allows the
local_bh_disabled()sections and servicing soft interrupts to be preemptible.Provide the necessary inline helpers which allow to reuse the bulk of the softirq processing code.
kernel/softirq.c +10
-0+struct softirq_ctrl { +local_lock_t lock; +int cnt; +}; + +static DEFINE_PER_CPU(struct softirq_ctrl, softirq_ctrl) = { + .lock =INIT_LOCAL_LOCK (softirq_ctrl.lock), +}; + +void __local_bh_disable_ip (unsigned long ip,unsigned int cnt)
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 151
Quoted 10 lines
kernel/softirq.c +10
-0+{ +unsigned long flags; +int newcnt; + +WARN_ON_ONCE (in_hardirq ()); + +/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock);
AFAICT this significantly changes the locking rules.
Where previously we could do:
spin_lock(&ponies) spin_lock_bh(&foo);
vs
spin_lock_bh(&bar); spin_lock(&ponies)
provided the rest of the code observed: bar -> ponies -> foo and never takes ponies from in-softirq.
This is now a genuine deadlock on RT.
Also see:
Quoted 24 lines
kernel/softirq.c +24
-0+/* Required to meet the RCU bottomhalf requirements. */ +rcu_read_lock (); + }else { +DEBUG_LOCKS_WARN_ON (this_cpu_read (softirq_ctrl.cnt)); + } + } + +/* +* Track the per CPU softirq disabled state. On RT this is per CPU +* state to allow preemption of bottom half disabled sections. +*/ + newcnt =__this_cpu_add_return (softirq_ctrl.cnt, cnt); +/* +* Reflect the result in the task state to prevent recursion on the +* local lock and to make softirq_count() & al work. +*/ + current->softirq_disable_cnt = newcnt; + +if (IS_ENABLED (CONFIG_TRACE_IRQFLAGS ) && newcnt == cnt) { +raw_local_irq_save (flags); +lockdep_softirqs_off (ip); +raw_local_irq_restore (flags); + } +}
Peter Zijlstra
maintainerRe: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Thomas Gleixnerkernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 150
Quoted 10 lines · Thomas Gleixner
kernel/softirq.c +10
-0+void __local_bh_disable_ip (unsigned long ip,unsigned int cnt) +{ +unsigned long flags; +int newcnt; + +WARN_ON_ONCE (in_hardirq ()); + +/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) {
This reminds me of that migrate_disable() thing that didn't work, and has similar problems.
preempt_disable(); local_bh_disable(); preempt_enable(); spin_lock(&foo);
is valid (albeit daft) code that will now malfunction.
Quoted 1 line
kernel/softirq.c +1
-0+local_lock (&softirq_ctrl.lock);
Re: [patch V2 4/9] softirq: Make softirq control and processing RT aware
In reply to Peter ZijlstraQuoted 16 lines · Peter Zijlstra
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 151
Quoted 5 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
kernel/softirq.c +4
-0+/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock);AFAICT this significantly changes the locking rules.
Where previously we could do:
spin_lock(&ponies) spin_lock_bh(&foo);vs
Quoted 7 lines
spin_lock_bh(&bar); spin_lock(&ponies)provided the rest of the code observed: bar -> ponies -> foo and never takes ponies from in-softirq.
This is now a genuine deadlock on RT.
I know, but making this work is trying to square the circle.
Any approach we tried before going this way had worse problems than this particular limitation.
Quoted 3 lines
Also see:
I'm aware of that and it's fortunately not that many instances of this.
Thanks,
tglx
Peter Zijlstra
maintainerRe: [patch V2 4/9] softirq: Make softirq control and processing RT aware
Quoted 20 lines · Thomas Gleixner
Quoted 19 lines · Peter Zijlstra
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 151
Quoted 5 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
kernel/softirq.c +4
-0+/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock);AFAICT this significantly changes the locking rules.
Where previously we could do:
spin_lock(&ponies) spin_lock_bh(&foo);vs
spin_lock_bh(&bar); spin_lock(&ponies)
Quoted 6 lines
Quoted 4 lines
provided the rest of the code observed: bar -> ponies -> foo and never takes ponies from in-softirq.
This is now a genuine deadlock on RT.
I know, but making this work is trying to square the circle.
:-)
Quoted 2 lines
Any approach we tried before going this way had worse problems than this particular limitation.
OK, but that would've been very good Changelog material methinks.
Also, then we should probably make sure PREEMPT_RT=n builds start suffering the same problem by adding the local_lock unconditionally, otherwise this keeps being a PREEMPT_RT special and we'll keep having to fix it up.
Re: [patch V2 4/9] softirq: Make softirq control and processing RT aware
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Thomas Gleixner
Quoted 23 lines · Peter Zijlstra
kernel/softirq.c EXPORT_PER_CPU_SYMBOL_GPL(hardirq_contex · line 151
Quoted 5 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
kernel/softirq.c +4
-0+/* First entry of a task into a BH disabled section? */ +if (!current->softirq_disable_cnt) { +if (preemptible ()) { +local_lock (&softirq_ctrl.lock);AFAICT this significantly changes the locking rules.
Where previously we could do:
spin_lock(&ponies) spin_lock_bh(&foo);vs
spin_lock_bh(&bar); spin_lock(&ponies)provided the rest of the code observed: bar -> ponies -> foo and never takes ponies from in-softirq.
This is now a genuine deadlock on RT.
Quoted 8 lines
Quoted 1 line
I know, but making this work is trying to square the circle.
:-)
Quoted 2 lines
Any approach we tried before going this way had worse problems than this particular limitation.
OK, but that would've been very good Changelog material methinks.
Let me add that.
Quoted 4 lines
Also, then we should probably make sure PREEMPT_RT=n builds start suffering the same problem by adding the
local_lockunconditionally, otherwise this keeps being aPREEMPT_RTspecial and we'll keep having to fix it up.
For lockdep builds I assume. I'd like to postpone that for a while like we postponed the rawlock nesting lockdep stuff until we have the vast majority sorted out.
Thanks,
tglx