Restart rt period timer when rt runtime exceeded
cover Restart rt period timer when rt runtime exceeded
When rt_runtime is modified from -1 to a valid control value, it may cause the task to be throttled all the time. Then the task cannot be killed. E.g:
The FIFO task A execution while(1):
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
struct sched_param param;
int priority = sched_get_priority_min(SCHED_FIFO);
param.sched_priority = priority;
sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
while(1) {}
return 0;
}Following the steps:
lihua@octopus ~ # echo -1 > /proc/sys/kernel/sched_rt_runtime_us lihua@octopus ~ # /bin/rt_throttled &
lihua@octopus ~ # echo 950000 > /proc/sys/kernel/sched_rt_runtime_us lihua@octopus ~ # ps aux | grep rt_throttled
root 7026 65.1 0.0 1848 180 ttyAMA0 R 01:27 0:26 /bin/rt_throttled root 7068 0.0 0.0 4072 620 ttyAMA0 S+ 01:28 0:00 grep rt_throttled
lihua@octopus ~ # killall rt_throttled
lihua@octopus ~ # date
Thu Jan 1 01:28:30 UTC 1970
lihua@octopus ~ # date
Thu Jan 1 01:28:33 UTC 1970
lihua@octopus ~ # ps aux | grep rt_throttled
root 7026 41.0 0.0 1848 180 ttyAMA0 R 01:27 0:26 /bin/rt_throttled root 7097 0.0 0.0 4072 576 ttyAMA0 S+ 01:28 0:00 grep rt_throttled
So the patch try to wake up the timer when rt runtime exceeded.
*** BLURB HERE ***
Li Hua (1):
sched/rt: Try to restart rt period timer when rt runtime exceeded
kernel/sched/rt.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
PATCH 1/1 sched/rt: Try to restart rt period timer when rt runtime exceeded
When rt_runtime is modified from -1 to a valid control value, it may cause the task to be throttled all the time. Operations like the following will trigger the bug. E.g:
1. echo -1 > /proc/sys/kernel/sched_rt_runtime_us
2. Run a FIFO task named A that executes while(1)
3. echo 950000 > /proc/sys/kernel/sched_rt_runtime_us
When rt_runtime is -1, The rt period timer will not be activated when task A enqueued. And then the task will be throttled after setting rt_runtime to 950,000. The task will always be throttled because the rt period timer is not activated.
--- kernel/sched/rt.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
kernel/sched/rt.c +25 -1
@@ -947,6 +947,23 @@ static inline int rt_se_prio(struct sched_rt_entity *rt_se)return rt_task_of (rt_se)->prio; } +static inline void try_start_rt_bandwidth (struct rt_bandwidth *rt_b) +{ +raw_spin_lock (&rt_b->rt_runtime_lock); +if (!rt_bandwidth_enabled () || rt_b->rt_runtime ==RUNTIME_INF ) { +raw_spin_unlock (&rt_b->rt_runtime_lock); +return ; + } + +if (!rt_b->rt_period_active) { + rt_b->rt_period_active =1 ; +hrtimer_forward_now (&rt_b->rt_period_timer, rt_b->rt_period); +hrtimer_start_expires (&rt_b->rt_period_timer, +HRTIMER_MODE_ABS_PINNED_HARD ); + } +raw_spin_unlock (&rt_b->rt_runtime_lock); +} +static int sched_rt_runtime_exceeded (struct rt_rq *rt_rq) {u64 runtime =sched_rt_runtime (rt_rq);
@@ -1027,11 +1044,16 @@ static void update_curr_rt(struct rq *rq)struct rt_rq *rt_rq =rt_rq_of_se (rt_se);if (sched_rt_runtime (rt_rq) !=RUNTIME_INF ) { +int exceeded; +raw_spin_lock (&rt_rq->rt_runtime_lock); rt_rq->rt_time += delta_exec; -if (sched_rt_runtime_exceeded (rt_rq)) + exceeded =sched_rt_runtime_exceeded (rt_rq); +if (exceeded)resched_curr (rq);raw_spin_unlock (&rt_rq->rt_runtime_lock); +if (exceeded) +try_start_rt_bandwidth (sched_rt_bandwidth (rt_rq)); } } }
@@ -2905,8 +2927,10 @@ static int sched_rt_global_validate(void)static void sched_rt_do_global (void ) { +raw_spin_lock (&def_rt_bandwidth.rt_runtime_lock); def_rt_bandwidth.rt_runtime =global_rt_runtime (); def_rt_bandwidth.rt_period =ns_to_ktime (global_rt_period ()); +raw_spin_unlock (&def_rt_bandwidth.rt_runtime_lock); } int sched_rt_handler(struct ctl_table *table, int write, void *buffer,
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [PATCH -next 1/1] sched/rt: Try to restart rt period timer when rt runtime exceeded
Quoted 32 lines · Li Hua
When
rt_runtimeis modified from -1 to a valid control value, it may cause the task to be throttled all the time. Operations like the following will trigger the bug. E.g:
1. echo -1 >/proc/sys/kernel/sched_rt_runtime_us
2. Run a FIFO task named A that executes while(1)
3. echo 950000 >/proc/sys/kernel/sched_rt_runtime_usWhen
rt_runtimeis -1, The rt period timer will not be activated when task A enqueued. And then the task will be throttled after settingrt_runtimeto 950,000. The task will always be throttled because the rt period timer is not activated.Reported-by Hulk Robot <[email protected]>Signed-off-by Li Hua <[email protected]>--- kernel/sched/rt.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)kernel/sched/rt.c +6
-0@@ -947,6 +947,23 @@ static inline int rt_se_prio(struct sched_rt_entity *rt_se)return rt_task_of (rt_se)->prio; } +static inline void try_start_rt_bandwidth (struct rt_bandwidth *rt_b) +{ +raw_spin_lock (&rt_b->rt_runtime_lock); +if (!rt_bandwidth_enabled () || rt_b->rt_runtime ==RUNTIME_INF ) { +raw_spin_unlock (&rt_b->rt_runtime_lock); +return ;
kernel/sched/rt.c static inline int rt_se_prio(struct sched_rt_entity *rt_se) · line 965
Quoted 10 lines
kernel/sched/rt.c +10
-0+ } + +if (!rt_b->rt_period_active) { + rt_b->rt_period_active =1 ; +hrtimer_forward_now (&rt_b->rt_period_timer, rt_b->rt_period); +hrtimer_start_expires (&rt_b->rt_period_timer, +HRTIMER_MODE_ABS_PINNED_HARD ); + } +raw_spin_unlock (&rt_b->rt_runtime_lock); +}
This is almost a verbatim copy of start_rt_bandwidth() surely we can do better.
Quoted 23 lines
kernel/sched/rt.c +7
-1+static int sched_rt_runtime_exceeded (struct rt_rq *rt_rq) {u64 runtime =sched_rt_runtime (rt_rq);@@ -1027,11 +1044,16 @@ static void update_curr_rt(struct rq *rq)struct rt_rq *rt_rq =rt_rq_of_se (rt_se);if (sched_rt_runtime (rt_rq) !=RUNTIME_INF ) { +int exceeded; +raw_spin_lock (&rt_rq->rt_runtime_lock); rt_rq->rt_time += delta_exec; -if (sched_rt_runtime_exceeded (rt_rq)) + exceeded =sched_rt_runtime_exceeded (rt_rq); +if (exceeded)resched_curr (rq);raw_spin_unlock (&rt_rq->rt_runtime_lock); +if (exceeded) +try_start_rt_bandwidth (sched_rt_bandwidth (rt_rq)); } } }
kernel/sched/rt.c static int sched_rt_global_validate(void) · line 2933
Quoted 7 lines
kernel/sched/rt.c +2
-0@@ -2905,8 +2927,10 @@ static int sched_rt_global_validate(void)static void sched_rt_do_global (void ) { +raw_spin_lock (&def_rt_bandwidth.rt_runtime_lock); def_rt_bandwidth.rt_runtime =global_rt_runtime (); def_rt_bandwidth.rt_period =ns_to_ktime (global_rt_period ()); +raw_spin_unlock (&def_rt_bandwidth.rt_runtime_lock);
And that's just wrong I think; did you test this with lockdep enabled? IIRC this lock is irq-safe, it has to be if you're using it form a timer context.