Skip to content

Commit 90b5363

Browse files
Peter Zijlstra (Intel)KAGA-KOKO
authored andcommitted
sched: Clean up scheduler_ipi()
The scheduler IPI has grown weird and wonderful over the years, time for spring cleaning. Move all the non-trivial stuff out of it and into a regular smp function call IPI. This then reduces the schedule_ipi() to most of it's former NOP glory and ensures to keep the interrupt vector lean and mean. Aside of that avoiding the full irq_enter() in the x86 IPI implementation is incorrect as scheduler_ipi() can be instrumented. To work around that scheduler_ipi() had an irq_enter/exit() hack when heavy work was pending. This is gone now. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Link: https://lkml.kernel.org/r/20200505134058.361859938@linutronix.de
1 parent b1d1779 commit 90b5363

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

kernel/sched/core.c

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ void update_rq_clock(struct rq *rq)
219219
update_rq_clock_task(rq, delta);
220220
}
221221

222+
static inline void
223+
rq_csd_init(struct rq *rq, call_single_data_t *csd, smp_call_func_t func)
224+
{
225+
csd->flags = 0;
226+
csd->func = func;
227+
csd->info = rq;
228+
}
222229

223230
#ifdef CONFIG_SCHED_HRTICK
224231
/*
@@ -314,16 +321,14 @@ void hrtick_start(struct rq *rq, u64 delay)
314321
hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
315322
HRTIMER_MODE_REL_PINNED_HARD);
316323
}
324+
317325
#endif /* CONFIG_SMP */
318326

319327
static void hrtick_rq_init(struct rq *rq)
320328
{
321329
#ifdef CONFIG_SMP
322-
rq->hrtick_csd.flags = 0;
323-
rq->hrtick_csd.func = __hrtick_start;
324-
rq->hrtick_csd.info = rq;
330+
rq_csd_init(rq, &rq->hrtick_csd, __hrtick_start);
325331
#endif
326-
327332
hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
328333
rq->hrtick_timer.function = hrtick;
329334
}
@@ -650,6 +655,16 @@ static inline bool got_nohz_idle_kick(void)
650655
return false;
651656
}
652657

658+
static void nohz_csd_func(void *info)
659+
{
660+
struct rq *rq = info;
661+
662+
if (got_nohz_idle_kick()) {
663+
rq->idle_balance = 1;
664+
raise_softirq_irqoff(SCHED_SOFTIRQ);
665+
}
666+
}
667+
653668
#else /* CONFIG_NO_HZ_COMMON */
654669

655670
static inline bool got_nohz_idle_kick(void)
@@ -2292,6 +2307,11 @@ void sched_ttwu_pending(void)
22922307
rq_unlock_irqrestore(rq, &rf);
22932308
}
22942309

2310+
static void wake_csd_func(void *info)
2311+
{
2312+
sched_ttwu_pending();
2313+
}
2314+
22952315
void scheduler_ipi(void)
22962316
{
22972317
/*
@@ -2300,34 +2320,6 @@ void scheduler_ipi(void)
23002320
* this IPI.
23012321
*/
23022322
preempt_fold_need_resched();
2303-
2304-
if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
2305-
return;
2306-
2307-
/*
2308-
* Not all reschedule IPI handlers call irq_enter/irq_exit, since
2309-
* traditionally all their work was done from the interrupt return
2310-
* path. Now that we actually do some work, we need to make sure
2311-
* we do call them.
2312-
*
2313-
* Some archs already do call them, luckily irq_enter/exit nest
2314-
* properly.
2315-
*
2316-
* Arguably we should visit all archs and update all handlers,
2317-
* however a fair share of IPIs are still resched only so this would
2318-
* somewhat pessimize the simple resched case.
2319-
*/
2320-
irq_enter();
2321-
sched_ttwu_pending();
2322-
2323-
/*
2324-
* Check if someone kicked us for doing the nohz idle load balance.
2325-
*/
2326-
if (unlikely(got_nohz_idle_kick())) {
2327-
this_rq()->idle_balance = 1;
2328-
raise_softirq_irqoff(SCHED_SOFTIRQ);
2329-
}
2330-
irq_exit();
23312323
}
23322324

23332325
static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags)
@@ -2336,9 +2328,9 @@ static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags)
23362328

23372329
p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
23382330

2339-
if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
2331+
if (llist_add(&p->wake_entry, &rq->wake_list)) {
23402332
if (!set_nr_if_polling(rq->idle))
2341-
smp_send_reschedule(cpu);
2333+
smp_call_function_single_async(cpu, &rq->wake_csd);
23422334
else
23432335
trace_sched_wake_idle_without_ipi(cpu);
23442336
}
@@ -6693,12 +6685,16 @@ void __init sched_init(void)
66936685
rq->avg_idle = 2*sysctl_sched_migration_cost;
66946686
rq->max_idle_balance_cost = sysctl_sched_migration_cost;
66956687

6688+
rq_csd_init(rq, &rq->wake_csd, wake_csd_func);
6689+
66966690
INIT_LIST_HEAD(&rq->cfs_tasks);
66976691

66986692
rq_attach_root(rq, &def_root_domain);
66996693
#ifdef CONFIG_NO_HZ_COMMON
67006694
rq->last_blocked_load_update_tick = jiffies;
67016695
atomic_set(&rq->nohz_flags, 0);
6696+
6697+
rq_csd_init(rq, &rq->nohz_csd, nohz_csd_func);
67026698
#endif
67036699
#endif /* CONFIG_SMP */
67046700
hrtick_rq_init(rq);

kernel/sched/fair.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10000,12 +10000,11 @@ static void kick_ilb(unsigned int flags)
1000010000
return;
1000110001

1000210002
/*
10003-
* Use smp_send_reschedule() instead of resched_cpu().
10004-
* This way we generate a sched IPI on the target CPU which
10003+
* This way we generate an IPI on the target CPU which
1000510004
* is idle. And the softirq performing nohz idle load balance
1000610005
* will be run before returning from the IPI.
1000710006
*/
10008-
smp_send_reschedule(ilb_cpu);
10007+
smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd);
1000910008
}
1001010009

1001110010
/*

kernel/sched/sched.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,10 @@ struct rq {
889889
#ifdef CONFIG_SMP
890890
unsigned long last_blocked_load_update_tick;
891891
unsigned int has_blocked_load;
892+
call_single_data_t nohz_csd;
892893
#endif /* CONFIG_SMP */
893894
unsigned int nohz_tick_stopped;
894-
atomic_t nohz_flags;
895+
atomic_t nohz_flags;
895896
#endif /* CONFIG_NO_HZ_COMMON */
896897

897898
unsigned long nr_load_updates;
@@ -978,7 +979,7 @@ struct rq {
978979

979980
/* This is used to determine avg_idle's max value */
980981
u64 max_idle_balance_cost;
981-
#endif
982+
#endif /* CONFIG_SMP */
982983

983984
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
984985
u64 prev_irq_time;
@@ -1020,6 +1021,7 @@ struct rq {
10201021
#endif
10211022

10221023
#ifdef CONFIG_SMP
1024+
call_single_data_t wake_csd;
10231025
struct llist_head wake_list;
10241026
#endif
10251027

0 commit comments

Comments
 (0)