sched: Core Scheduling
Subsystems (5)
Peter Zijlstra
maintainerPATCH 1/19 sched/fair: Add a few assertions
In reply to Peter Zijlstra--- kernel/sched/fair.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/fair.c +9 -2
@@ -6246,6 +6246,11 @@ static int select_idle_sibling(struct ta task_util =uclamp_task_util (p); } +/* +* per-cpu select_idle_mask usage +*/ +lockdep_assert_irqs_disabled (); +if ((available_idle_cpu (target) ||sched_idle_cpu (target)) &&asym_fits_capacity (task_util, target))return target;
@@ -6711,8 +6716,6 @@ static int find_energy_efficient_cpu(str* certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set. * * Returns the target CPU number. -* -* preempt must be disabled. */ static int select_task_rq_fair (struct task_struct *p,int prev_cpu,int wake_flags)
@@ -6725,6 +6728,10 @@ select_task_rq_fair(struct task_struct */* SD_flags and WF_flags share the first nibble */ int sd_flag = wake_flags &0xF ; +/* +* required for stable ->cpus_allowed +*/ +lockdep_assert_held (&p->pi_lock);if (wake_flags &WF_TTWU ) {record_wakee (p);
Peter Zijlstra
maintainerPATCH 2/19 sched: Provide raw_spin_rq_*lock*() helpers
In reply to Peter ZijlstraIn prepration for playing games with rq->lock, add some rq_lock wrappers.
--- kernel/sched/core.c | 15 +++++++++++++++ kernel/sched/sched.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
kernel/sched/core.c +15 -0
@@ -184,6 +184,21 @@ int sysctl_sched_rt_runtime = 950000;* */ +void raw_spin_rq_lock_nested (struct rq *rq,int subclass) +{ +raw_spin_lock_nested (rq_lockp (rq), subclass); +} + +bool raw_spin_rq_trylock (struct rq *rq) +{ +return raw_spin_trylock (rq_lockp (rq)); +} + +void raw_spin_rq_unlock (struct rq *rq) +{ +raw_spin_unlock (rq_lockp (rq)); +} +/* * __task_rq_lock - lock the rq @p resides on. */
kernel/sched/sched.h +50 -0
56 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 4/19 sched: Prepare for Core-wide rq->lock
In reply to Peter ZijlstraWhen switching on core-sched, CPUs need to agree which lock to use for their RQ.
The new rule will be that rq->core_enabled will be toggled while holding all rq->__locks that belong to a core. This means we need to double check the rq->core_enabled value after each lock acquire and retry if it changed.
This also has implications for those sites that take multiple RQ locks, they need to be careful that the second lock doesn't end up being the first lock.
Verify the lock pointer after acquiring the first lock, because if they're on the same core, holding any of the rq->__lock instances will pin the core state.
While there, change the rq->__lock order to CPU number, instead of rq address, this greatly simplifies the next patch.
--- kernel/sched/core.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- kernel/sched/sched.h | 41 +++++++++++------------------------------ 2 files changed, 57 insertions(+), 32 deletions(-)
kernel/sched/core.c +46 -2
@@ -186,12 +186,37 @@ int sysctl_sched_rt_runtime = 950000;void raw_spin_rq_lock_nested (struct rq *rq,int subclass) { -raw_spin_lock_nested (rq_lockp (rq), subclass); +raw_spinlock_t *lock; + +if (sched_core_disabled ()) { +raw_spin_lock_nested (&rq->__lock, subclass); +return ; + } + +for (;;) { + lock =rq_lockp (rq); +raw_spin_lock_nested (lock, subclass); +if (likely (lock ==rq_lockp (rq))) +return ; +raw_spin_unlock (lock); + } }bool raw_spin_rq_trylock (struct rq *rq) { -return raw_spin_trylock (rq_lockp (rq)); +raw_spinlock_t *lock; +bool ret; + +if (sched_core_disabled ()) +return raw_spin_trylock (&rq->__lock); + +for (;;) { + lock =rq_lockp (rq); + ret =raw_spin_trylock (lock); +if (!ret || (likely (lock ==rq_lockp (rq)))) +return ret; +raw_spin_unlock (lock); + } }void raw_spin_rq_unlock (struct rq *rq)
@@ -199,6 +224,25 @@ void raw_spin_rq_unlock(struct rq *rq)raw_spin_unlock (rq_lockp (rq)); } +#ifdef CONFIG_SMP +/* +* double_rq_lock - safely lock two runqueues +*/ +void double_rq_lock (struct rq *rq1,struct rq *rq2) +{ +lockdep_assert_irqs_disabled (); + +if (rq1->cpu > rq2->cpu) +swap (rq1, rq2); + +raw_spin_rq_lock (rq1); +if (rq_lockp (rq1) ==rq_lockp (rq2)) +return ; + +raw_spin_rq_lock_nested (rq2,SINGLE_DEPTH_NESTING ); +} +#endif +/* * __task_rq_lock - lock the rq @p resides on. */
kernel/sched/sched.h +11 -30
72 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 3/19 sched: Wrap rq::lock access
In reply to Peter ZijlstraIn preparation of playing games with rq->lock, abstract the thing using an accessor.
--- kernel/sched/core.c | 70 ++++++++++++++++---------------- kernel/sched/cpuacct.c | 12 ++--- kernel/sched/deadline.c | 22 +++++----- kernel/sched/debug.c | 4 - kernel/sched/fair.c | 35 +++++++--------- kernel/sched/idle.c | 4 - kernel/sched/pelt.h | 2 kernel/sched/rt.c | 16 +++---- kernel/sched/sched.h | 103 +++++++++++++++++++++++------------------------- kernel/sched/topology.c | 4 - 10 files changed, 135 insertions(+), 137 deletions(-)
kernel/sched/core.c +35 -35
@@ -211,12 +211,12 @@ struct rq *__task_rq_lock(struct task_stfor (;;) { rq =task_rq (p); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);if (likely (rq ==task_rq (p) && !task_on_rq_migrating (p))) {rq_pin_lock (rq, rf);return rq; } -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);while (unlikely (task_on_rq_migrating (p)))cpu_relax ();
@@ -235,7 +235,7 @@ struct rq *task_rq_lock(struct task_strufor (;;) {raw_spin_lock_irqsave (&p->pi_lock, rf->flags); rq =task_rq (p); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);/* * move_queued_task() task_rq_lock() *
@@ -257,7 +257,7 @@ struct rq *task_rq_lock(struct task_strurq_pin_lock (rq, rf);return rq; } -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);raw_spin_unlock_irqrestore (&p->pi_lock, rf->flags);while (unlikely (task_on_rq_migrating (p)))
@@ -327,7 +327,7 @@ void update_rq_clock(struct rq *rq) {s64 delta; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);if (rq->clock_update_flags &RQCF_ACT_SKIP )return ;
@@ -626,7 +626,7 @@ void resched_curr(struct rq *rq)struct task_struct *curr = rq->curr;int cpu; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);if (test_tsk_need_resched (curr))return ;
@@ -650,10 +650,10 @@ void resched_cpu(int cpu)struct rq *rq =cpu_rq (cpu);unsigned long flags; -raw_spin_lock_irqsave (&rq->lock, flags); +raw_spin_rq_lock_irqsave (rq, flags);if (cpu_online (cpu) || cpu ==smp_processor_id ())resched_curr (rq); -raw_spin_unlock_irqrestore (&rq->lock, flags); +raw_spin_rq_unlock_irqrestore (rq, flags); }#ifdef CONFIG_SMP
@@ -1152,7 +1152,7 @@ static inline void uclamp_rq_inc_id(strustruct uclamp_se *uc_se = &p->uclamp[clamp_id];struct uclamp_bucket *bucket; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);/* Update task effective clamp */ p->uclamp[clamp_id] =uclamp_eff_get (p, clamp_id);
@@ -1192,7 +1192,7 @@ static inline void uclamp_rq_dec_id(struunsigned int bkt_clamp;unsigned int rq_clamp; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);/* * If sched_uclamp_used was enabled after task @p was enqueued,
@@ -1865,7 +1865,7 @@ static inline bool is_cpu_allowed(structstatic struct rq *move_queued_task (struct rq *rq,struct rq_flags *rf,struct task_struct *p,int new_cpu) { -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);deactivate_task (rq, p,DEQUEUE_NOCLOCK );set_task_cpu (p, new_cpu);
@@ -2039,7 +2039,7 @@ int push_cpu_stop(void *arg)struct task_struct *p = arg;raw_spin_lock_irq (&p->pi_lock); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);if (task_rq (p) != rq) goto out_unlock;
@@ -2069,7 +2069,7 @@ int push_cpu_stop(void *arg) out_unlock: rq->push_busy = false; -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);raw_spin_unlock_irq (&p->pi_lock);put_task_struct (p);
@@ -2122,7 +2122,7 @@ __do_set_cpus_allowed(struct task_struct* Because __kthread_bind() calls this on blocked tasks without * holding rq->lock. */ -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);dequeue_task (rq, p,DEQUEUE_SAVE |DEQUEUE_NOCLOCK ); }if (running)
@@ -2463,7 +2463,7 @@ void set_task_cpu(struct task_struct *p,* task_rq_lock(). */ WARN_ON_ONCE (debug_locks && !(lockdep_is_held (&p->pi_lock) || -lockdep_is_held (&task_rq (p)->lock))); +lockdep_is_held (rq_lockp (task_rq (p)))));#endif /* * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
@@ -3005,7 +3005,7 @@ ttwu_do_activate(struct rq *rq, struct t {int en_flags =ENQUEUE_WAKEUP |ENQUEUE_NOCLOCK ; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);if (p->sched_contributes_to_load) rq->nr_uninterruptible--;
@@ -4016,7 +4016,7 @@ static void do_balance_callbacks(structvoid (*func)(struct rq *rq);struct callback_head *next; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);while (head) { func = (void (*)(struct rq *))head->func;
@@ -4039,7 +4039,7 @@ static inline struct callback_head *spli {struct callback_head *head = rq->balance_callback; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);if (head) rq->balance_callback =NULL ;
@@ -4056,9 +4056,9 @@ static inline void balance_callbacks(strunsigned long flags;if (unlikely (head)) { -raw_spin_lock_irqsave (&rq->lock, flags); +raw_spin_rq_lock_irqsave (rq, flags);do_balance_callbacks (rq, head); -raw_spin_unlock_irqrestore (&rq->lock, flags); +raw_spin_rq_unlock_irqrestore (rq, flags); } }
@@ -4089,10 +4089,10 @@ prepare_lock_switch(struct rq *rq, struc* do an early lockdep release here: */ rq_unpin_lock (rq, rf); -spin_release (&rq->lock.dep_map, _THIS_IP_); +spin_release (&rq_lockp (rq)->dep_map, _THIS_IP_);#ifdef CONFIG_DEBUG_SPINLOCK /* this is a valid case when another task releases the spinlock */ - rq->lock.owner = next; +rq_lockp (rq)->owner = next;#endif }
@@ -4103,9 +4103,9 @@ static inline void finish_lock_switch(st* fix up the runqueue lock - which gets 'carried over' from * prev into current: */ -spin_acquire (&rq->lock.dep_map,0 ,0 , _THIS_IP_); +spin_acquire (&rq_lockp (rq)->dep_map,0 ,0 , _THIS_IP_);__balance_callbacks (rq); -raw_spin_unlock_irq (&rq->lock); +raw_spin_rq_unlock_irq (rq); }/*
@@ -5165,7 +5165,7 @@ static void __sched notrace __schedule(brq_unpin_lock (rq, &rf);__balance_callbacks (rq); -raw_spin_unlock_irq (&rq->lock); +raw_spin_rq_unlock_irq (rq); } }
@@ -5707,7 +5707,7 @@ void rt_mutex_setprio(struct task_structrq_unpin_lock (rq, &rf);__balance_callbacks (rq); -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);preempt_enable (); }
@@ -7456,7 +7456,7 @@ void init_idle(struct task_struct *idle,__sched_fork (0 , idle);raw_spin_lock_irqsave (&idle->pi_lock, flags); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq); idle->state =TASK_RUNNING ; idle->se.exec_start =sched_clock ();
@@ -7494,7 +7494,7 @@ void init_idle(struct task_struct *idle,#ifdef CONFIG_SMP idle->on_cpu =1 ;#endif -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);raw_spin_unlock_irqrestore (&idle->pi_lock, flags);/* Set the preempt count _outside_ the spinlocks! */
@@ -7660,7 +7660,7 @@ static void balance_push(struct rq *rq) {struct task_struct *push_task = rq->curr; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);SCHED_WARN_ON (rq->cpu !=smp_processor_id ());/*
@@ -7698,9 +7698,9 @@ static void balance_push(struct rq *rq)*/ if (!rq->nr_running && !rq_has_pinned_tasks (rq) &&rcuwait_active (&rq->hotplug_wait)) { -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);rcuwait_wake_up (&rq->hotplug_wait); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq); }return ; }
@@ -7710,7 +7710,7 @@ static void balance_push(struct rq *rq)* Temporarily drop rq->lock such that we can wake-up the stop task. * Both preemption and IRQs are still disabled. */ -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);stop_one_cpu_nowait (rq->cpu, __balance_push_cpu_stop, push_task,this_cpu_ptr (&push_work));/*
@@ -7718,7 +7718,7 @@ static void balance_push(struct rq *rq)* schedule(). The next pick is obviously going to be the stop task * which kthread_is_per_cpu() and will push this task away. */ -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq); }static void balance_push_set (int cpu,bool on)
@@ -8008,7 +8008,7 @@ static void dump_rq_tasks(struct rq *rq,struct task_struct *g, *p;int cpu =cpu_of (rq); -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);printk ("%sCPU%d enqueued tasks (%u total):\n" , loglvl, cpu, rq->nr_running);for_each_process_thread (g, p) {
@@ -8181,7 +8181,7 @@ void __init sched_init(void)struct rq *rq; rq =cpu_rq (i); -raw_spin_lock_init (&rq->lock); +raw_spin_lock_init (&rq->__lock); rq->nr_running =0 ; rq->calc_load_active =0 ; rq->calc_load_update = jiffies +LOAD_FREQ ;
kernel/sched/cpuacct.c +6 -6
@@ -112,7 +112,7 @@ static u64 cpuacct_cpuusage_read(struct/* * Take rq->lock to make 64-bit read safe on 32-bit platforms. */ -raw_spin_lock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_lock_irq (cpu_rq (cpu));#endif if (index ==CPUACCT_STAT_NSTATS ) {
@@ -126,7 +126,7 @@ static u64 cpuacct_cpuusage_read(struct }#ifndef CONFIG_64BIT -raw_spin_unlock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_unlock_irq (cpu_rq (cpu));#endif return data;
@@ -141,14 +141,14 @@ static void cpuacct_cpuusage_write(struc/* * Take rq->lock to make 64-bit write safe on 32-bit platforms. */ -raw_spin_lock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_lock_irq (cpu_rq (cpu));#endif for (i =0 ; i <CPUACCT_STAT_NSTATS ; i++) cpuusage->usages[i] = val;#ifndef CONFIG_64BIT -raw_spin_unlock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_unlock_irq (cpu_rq (cpu));#endif }
@@ -253,13 +253,13 @@ static int cpuacct_all_seq_show(struct s* Take rq->lock to make 64-bit read safe on 32-bit * platforms. */ -raw_spin_lock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_lock_irq (cpu_rq (cpu));#endif seq_printf (m," %llu" , cpuusage->usages[index]);#ifndef CONFIG_64BIT -raw_spin_unlock_irq (&cpu_rq (cpu)->lock); +raw_spin_rq_unlock_irq (cpu_rq (cpu));#endif }seq_puts (m,"\n" );
kernel/sched/deadline.c +11 -11
@@ -157,7 +157,7 @@ void __add_running_bw(u64 dl_bw, struct {u64 old = dl_rq->running_bw; -lockdep_assert_held (&(rq_of_dl_rq (dl_rq))->lock); +lockdep_assert_rq_held (rq_of_dl_rq (dl_rq)); dl_rq->running_bw += dl_bw;SCHED_WARN_ON (dl_rq->running_bw < old);/* overflow */ SCHED_WARN_ON (dl_rq->running_bw > dl_rq->this_bw);
@@ -170,7 +170,7 @@ void __sub_running_bw(u64 dl_bw, struct {u64 old = dl_rq->running_bw; -lockdep_assert_held (&(rq_of_dl_rq (dl_rq))->lock); +lockdep_assert_rq_held (rq_of_dl_rq (dl_rq)); dl_rq->running_bw -= dl_bw;SCHED_WARN_ON (dl_rq->running_bw > old);/* underflow */ if (dl_rq->running_bw > old)
@@ -184,7 +184,7 @@ void __add_rq_bw(u64 dl_bw, struct dl_rq {u64 old = dl_rq->this_bw; -lockdep_assert_held (&(rq_of_dl_rq (dl_rq))->lock); +lockdep_assert_rq_held (rq_of_dl_rq (dl_rq)); dl_rq->this_bw += dl_bw;SCHED_WARN_ON (dl_rq->this_bw < old);/* overflow */ }
@@ -194,7 +194,7 @@ void __sub_rq_bw(u64 dl_bw, struct dl_rq {u64 old = dl_rq->this_bw; -lockdep_assert_held (&(rq_of_dl_rq (dl_rq))->lock); +lockdep_assert_rq_held (rq_of_dl_rq (dl_rq)); dl_rq->this_bw -= dl_bw;SCHED_WARN_ON (dl_rq->this_bw > old);/* underflow */ if (dl_rq->this_bw > old)
@@ -987,7 +987,7 @@ static int start_dl_timer(struct task_stktime_t now, act;s64 delta; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);/* * We want the timer to fire at the deadline, but considering
@@ -1097,9 +1097,9 @@ static enum hrtimer_restart dl_task_time* If the runqueue is no longer available, migrate the * task elsewhere. This necessarily changes rq. */ -lockdep_unpin_lock (&rq->lock, rf.cookie); +lockdep_unpin_lock (rq_lockp (rq), rf.cookie); rq =dl_task_offline_migration (rq, p); - rf.cookie =lockdep_pin_lock (&rq->lock); + rf.cookie =lockdep_pin_lock (rq_lockp (rq));update_rq_clock (rq);/*
@@ -1731,7 +1731,7 @@ static void migrate_task_rq_dl(struct ta* from try_to_wake_up(). Hence, p->pi_lock is locked, but * rq->lock is not... So, lock it */ -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);if (p->dl.dl_non_contending) {sub_running_bw (&p->dl, &rq->dl); p->dl.dl_non_contending =0 ;
@@ -1746,7 +1746,7 @@ static void migrate_task_rq_dl(struct taput_task_struct (p); }sub_rq_bw (&p->dl, &rq->dl); -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq); }static void check_preempt_equal_dl (struct rq *rq,struct task_struct *p)
@@ -2291,10 +2291,10 @@ static void pull_dl_task(struct rq *thisdouble_unlock_balance (this_rq, src_rq);if (push_task) { -raw_spin_unlock (&this_rq->lock); +raw_spin_rq_unlock (this_rq);stop_one_cpu_nowait (src_rq->cpu, push_cpu_stop, push_task, &src_rq->push_work); -raw_spin_lock (&this_rq->lock); +raw_spin_rq_lock (this_rq); } }
kernel/sched/debug.c +2 -2
@@ -576,7 +576,7 @@ void print_cfs_rq(struct seq_file *m, inSEQ_printf (m," .%-30s: %Ld.%06ld\n" ,"exec_clock" ,SPLIT_NS (cfs_rq->exec_clock)); -raw_spin_lock_irqsave (&rq->lock, flags); +raw_spin_rq_lock_irqsave (rq, flags);if (rb_first_cached (&cfs_rq->tasks_timeline)) MIN_vruntime = (__pick_first_entity (cfs_rq))->vruntime; last =__pick_last_entity (cfs_rq);
@@ -584,7 +584,7 @@ void print_cfs_rq(struct seq_file *m, in max_vruntime = last->vruntime; min_vruntime = cfs_rq->min_vruntime; rq0_min_vruntime =cpu_rq (0 )->cfs.min_vruntime; -raw_spin_unlock_irqrestore (&rq->lock, flags); +raw_spin_rq_unlock_irqrestore (rq, flags);SEQ_printf (m," .%-30s: %Ld.%06ld\n" ,"MIN_vruntime" ,SPLIT_NS (MIN_vruntime)); SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
kernel/sched/fair.c +17 -18
@@ -1107,7 +1107,7 @@ struct numa_group {static struct numa_group *deref_task_numa_group (struct task_struct *p) {return rcu_dereference_check (p->numa_group, p == current || - (lockdep_is_held (&task_rq (p)->lock) && !READ_ONCE (p->on_cpu))); + (lockdep_is_held (rq_lockp (task_rq (p))) && !READ_ONCE (p->on_cpu))); }static struct numa_group *deref_curr_numa_group (struct task_struct *p)
@@ -5328,7 +5328,7 @@ static void __maybe_unused update_runtim {struct task_group *tg; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);rcu_read_lock ();list_for_each_entry_rcu (tg, &task_groups, list) {
@@ -5347,7 +5347,7 @@ static void __maybe_unused unthrottle_of {struct task_group *tg; -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);rcu_read_lock ();list_for_each_entry_rcu (tg, &task_groups, list) {
@@ -6874,7 +6874,7 @@ static void migrate_task_rq_fair(struct* In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old' * rq->lock and can modify state directly. */ -lockdep_assert_held (&task_rq (p)->lock); +lockdep_assert_rq_held (task_rq (p));detach_entity_cfs_rq (&p->se); }else {
@@ -7501,7 +7501,7 @@ static int task_hot(struct task_struct * {s64 delta; -lockdep_assert_held (&env->src_rq->lock); +lockdep_assert_rq_held (env->src_rq);if (p->sched_class != &fair_sched_class)return 0 ;
@@ -7599,7 +7599,7 @@ int can_migrate_task(struct task_struct {int tsk_cache_hot; -lockdep_assert_held (&env->src_rq->lock); +lockdep_assert_rq_held (env->src_rq);/* * We do not migrate tasks that are:
@@ -7688,7 +7688,7 @@ int can_migrate_task(struct task_struct*/ static void detach_task (struct task_struct *p,struct lb_env *env) { -lockdep_assert_held (&env->src_rq->lock); +lockdep_assert_rq_held (env->src_rq);deactivate_task (env->src_rq, p,DEQUEUE_NOCLOCK );set_task_cpu (p, env->dst_cpu);
@@ -7704,7 +7704,7 @@ static struct task_struct *detach_one_ta {struct task_struct *p; -lockdep_assert_held (&env->src_rq->lock); +lockdep_assert_rq_held (env->src_rq);list_for_each_entry_reverse (p, &env->src_rq->cfs_tasks, se.group_node) {
@@ -7740,7 +7740,7 @@ static int detach_tasks(struct lb_env *estruct task_struct *p;int detached =0 ; -lockdep_assert_held (&env->src_rq->lock); +lockdep_assert_rq_held (env->src_rq);/* * Source run queue has been emptied by another CPU, clear
@@ -7870,7 +7870,7 @@ static int detach_tasks(struct lb_env *e*/ static void attach_task (struct rq *rq,struct task_struct *p) { -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);BUG_ON (task_rq (p) != rq);activate_task (rq, p,ENQUEUE_NOCLOCK );
@@ -9781,7 +9781,7 @@ static int load_balance(int this_cpu, stif (need_active_balance (&env)) {unsigned long flags; -raw_spin_lock_irqsave (&busiest->lock, flags); +raw_spin_rq_lock_irqsave (busiest, flags);/* * Don't kick the active_load_balance_cpu_stop,
@@ -9789,8 +9789,7 @@ static int load_balance(int this_cpu, st* moved to this_cpu: */ if (!cpumask_test_cpu (this_cpu, busiest->curr->cpus_ptr)) { -raw_spin_unlock_irqrestore (&busiest->lock, - flags); +raw_spin_rq_unlock_irqrestore (busiest, flags); goto out_one_pinned; }
@@ -9807,7 +9806,7 @@ static int load_balance(int this_cpu, st busiest->push_cpu = this_cpu; active_balance =1 ; } -raw_spin_unlock_irqrestore (&busiest->lock, flags); +raw_spin_rq_unlock_irqrestore (busiest, flags);if (active_balance) { stop_one_cpu_nowait(cpu_of(busiest),
@@ -10624,7 +10623,7 @@ static int newidle_balance(struct rq *th goto out; } -raw_spin_unlock (&this_rq->lock); +raw_spin_rq_unlock (this_rq);update_blocked_averages (this_cpu);rcu_read_lock ();
@@ -10662,7 +10661,7 @@ static int newidle_balance(struct rq *th }rcu_read_unlock (); -raw_spin_lock (&this_rq->lock); +raw_spin_rq_lock (this_rq);if (curr_cost > this_rq->max_idle_balance_cost) this_rq->max_idle_balance_cost = curr_cost;
@@ -11143,9 +11142,9 @@ void unregister_fair_sched_group(struct rq =cpu_rq (cpu); -raw_spin_lock_irqsave (&rq->lock, flags); +raw_spin_rq_lock_irqsave (rq, flags);list_del_leaf_cfs_rq (tg->cfs_rq[cpu]); -raw_spin_unlock_irqrestore (&rq->lock, flags); +raw_spin_rq_unlock_irqrestore (rq, flags); } }
kernel/sched/idle.c +2 -2
@@ -455,10 +455,10 @@ struct task_struct *pick_next_task_idle(static void dequeue_task_idle (struct rq *rq,struct task_struct *p,int flags) { -raw_spin_unlock_irq (&rq->lock); +raw_spin_rq_unlock_irq (rq);printk (KERN_ERR "bad: scheduling from the idle thread!\n" );dump_stack (); -raw_spin_lock_irq (&rq->lock); +raw_spin_rq_lock_irq (rq); }/*
kernel/sched/pelt.h +1 -1
@@ -141,7 +141,7 @@ static inline void update_idle_rq_clock_static inline u64 rq_clock_pelt (struct rq *rq) { -lockdep_assert_held (&rq->lock); +lockdep_assert_rq_held (rq);assert_clock_updated (rq);return rq->clock_pelt - rq->lost_idle_time;
kernel/sched/rt.c +8 -8
@@ -888,7 +888,7 @@ static int do_sched_rt_period_timer(struif (skip)continue ; -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);update_rq_clock (rq);if (rt_rq->rt_time) {
@@ -926,7 +926,7 @@ static int do_sched_rt_period_timer(struif (enqueue)sched_rt_rq_enqueue (rt_rq); -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq); }if (!throttled && (!rt_bandwidth_enabled () || rt_b->rt_runtime ==RUNTIME_INF ))
@@ -1894,10 +1894,10 @@ static int push_rt_task(struct rq *rq, b*/ push_task =get_push_task (rq);if (push_task) { -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq);stop_one_cpu_nowait (rq->cpu, push_cpu_stop, push_task, &rq->push_work); -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq); }return 0 ;
@@ -2122,10 +2122,10 @@ void rto_push_irq_work_func(struct irq_w* When it gets updated, a check is made if a push is possible. */ if (has_pushable_tasks (rq)) { -raw_spin_lock (&rq->lock); +raw_spin_rq_lock (rq);while (push_rt_task (rq, true)) ; -raw_spin_unlock (&rq->lock); +raw_spin_rq_unlock (rq); }raw_spin_lock (&rd->rto_lock);
@@ -2243,10 +2243,10 @@ static void pull_rt_task(struct rq *thisdouble_unlock_balance (this_rq, src_rq);if (push_task) { -raw_spin_unlock (&this_rq->lock); +raw_spin_rq_unlock (this_rq);stop_one_cpu_nowait (src_rq->cpu, push_cpu_stop, push_task, &src_rq->push_work); -raw_spin_lock (&this_rq->lock); +raw_spin_rq_lock (this_rq); } }
kernel/sched/sched.h +51 -52
274 lines not on this page · raw message
kernel/sched/topology.c +2 -2
@@ -467,7 +467,7 @@ void rq_attach_root(struct rq *rq, strucstruct root_domain *old_rd =NULL ;unsigned long flags; -raw_spin_lock_irqsave (&rq->lock, flags); +raw_spin_rq_lock_irqsave (rq, flags);if (rq->rd) { old_rd = rq->rd;
@@ -493,7 +493,7 @@ void rq_attach_root(struct rq *rq, strucif (cpumask_test_cpu (rq->cpu, cpu_active_mask))set_rq_online (rq); -raw_spin_unlock_irqrestore (&rq->lock, flags); +raw_spin_rq_unlock_irqrestore (rq, flags);if (old_rd)call_rcu (&old_rd->rcu, free_rootdomain);
Peter Zijlstra
maintainercover sched: Core Scheduling
Hai,
This is an agressive fold of all the core-scheduling work so far. I've stripped a whole bunch of tags along the way (hopefully not too many, please yell if you feel I made a mistake), including tested-by. Please retest.
Changes since the last partial post is dropping all the cgroup stuff and PR_SCHED_CORE_CLEAR as well as that exec() behaviour in order to later resolve the cgroup issue.
Since we're really rather late for the coming merge window, my plan was to merge the lot right after the merge window.
Again, please test.
These patches should shortly be available in my queue.git.
--- b/kernel/sched/core_sched.c | 229 ++++++ b/tools/testing/selftests/sched/.gitignore | 1 b/tools/testing/selftests/sched/Makefile | 14 b/tools/testing/selftests/sched/config | 1 b/tools/testing/selftests/sched/cs_prctl_test.c | 338 +++++++++ include/linux/sched.h | 19 include/uapi/linux/prctl.h | 8 kernel/Kconfig.preempt | 6 kernel/fork.c | 4 kernel/sched/Makefile | 1 kernel/sched/core.c | 858 ++++++++++++++++++++++-- kernel/sched/cpuacct.c | 12 kernel/sched/deadline.c | 38 - kernel/sched/debug.c | 4 kernel/sched/fair.c | 276 +++++-- kernel/sched/idle.c | 13 kernel/sched/pelt.h | 2 kernel/sched/rt.c | 31 kernel/sched/sched.h | 393 ++++++++-- kernel/sched/stop_task.c | 14 kernel/sched/topology.c | 4 kernel/sys.c | 5 tools/include/uapi/linux/prctl.h | 8 23 files changed, 2057 insertions(+), 222 deletions(-)
Peter Zijlstra
maintainerPATCH 12/19 sched: Fix priority inversion of cookied task with sibling
From: "Joel Fernandes (Google)" <[email protected]>
The rationale is as follows. In the core-wide pick logic, even if need_sync == false, we need to go look at other CPUs (non-local CPUs) to see if they could be running RT.
Say the RQs in a particular core look like this:
Let CFS1 and CFS2 be 2 tagged CFS tags.
Let RT1 be an untagged RT task.
rq0 rq1 CFS1 (tagged) RT1 (no tag) CFS2 (tagged)
Say schedule() runs on rq0. Now, it will enter the above loop and pick_task(RT) will return NULL for 'p'. It will enter the above if() block and see that need_sync == false and will skip RT entirely.
The end result of the selection will be (say prio(CFS1) > prio(CFS2)):
rq0 rq1
CFS1 IDLE
When it should have selected:
rq0 rq1
IDLE RT
--- kernel/sched/core.c | 65 ++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-)
kernel/sched/core.c +26 -39
@@ -5425,6 +5425,15 @@ pick_next_task(struct rq *rq, struct tasput_prev_task_balance (rq, prev, rf); smt_mask =cpu_smt_mask (cpu); + need_sync = !!rq->core->core_cookie; + +/* reset state */ + rq->core->core_cookie =0UL ; +if (rq->core->core_forceidle) { + need_sync = true; + fi_before = true; + rq->core->core_forceidle = false; + }/* * core->core_task_seq, core->core_pick_seq, rq->core_sched_seq
@@ -5437,14 +5446,25 @@ pick_next_task(struct rq *rq, struct tas* 'Fix' this by also increasing @task_seq for every pick. */ rq->core->core_task_seq++; - need_sync = !!rq->core->core_cookie; -/* reset state */ - rq->core->core_cookie =0UL ; -if (rq->core->core_forceidle) { +/* +* Optimize for common case where this CPU has no cookies +* and there are no cookied tasks running on siblings. +*/ +if (!need_sync) { +for_each_class (class) { + next = class->pick_task (rq); +if (next) +break ; + } + +if (!next->core_cookie) { + rq->core_pick =NULL ; + goto done; + } need_sync = true; - rq->core->core_forceidle = false; } +for_each_cpu (i, smt_mask) {struct rq *rq_i =cpu_rq (i);
@@ -5474,31 +5494,8 @@ pick_next_task(struct rq *rq, struct tas* core. */ p =pick_task (rq_i, class, max); -if (!p) { -/* -* If there weren't no cookies; we don't need to -* bother with the other siblings. -* If the rest of the core is not running a tagged -* task, i.e. need_sync == 0, and the current CPU -* which called into the schedule() loop does not -* have any tasks for this class, skip selecting for -* other siblings since there's no point. We don't skip -* for RT/DL because that could make CFS force-idle RT. -*/ -if (i == cpu && !need_sync && class == &fair_sched_class) - goto next_class; - +if (!p)continue ; - } - -/* -* Optimize the 'normal' case where there aren't any -* cookies and we don't need to sync up. -*/ -if (i == cpu && !need_sync && !p->core_cookie) { - next = p; - goto done; - } rq_i->core_pick = p;
@@ -5526,19 +5523,9 @@ pick_next_task(struct rq *rq, struct tascpu_rq (j)->core_pick =NULL ; } goto again; - }else { -/* -* Once we select a task for a cpu, we -* should not be doing an unconstrained -* pick because it might starve a task -* on a forced idle cpu. -*/ - need_sync = true; } - } } -next_class:; } rq->core->core_pick_seq = rq->core->core_task_seq;
Peter Zijlstra
maintainerPATCH 5/19 sched: Core-wide rq->lock
In reply to Peter ZijlstraIntroduce the basic infrastructure to have a core wide rq->lock.
This relies on the rq->__lock order being in increasing CPU number. It is also constrained to SMT8 per lockdep (and SMT256 per preempt_count).
Luckily SMT8 is the max supported SMT count for Linux (Mips, Sparc and Power are known to have this).
--- kernel/Kconfig.preempt | 6 ++ kernel/sched/core.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 37 +++++++++++++ 3 files changed, 182 insertions(+)
kernel/Kconfig.preempt +6 -0
@@ -99,3 +99,9 @@ config PREEMPT_DYNAMIC Interesting if you want the same pre-built kernel should be used for both Server and Desktop workloads. + +config SCHED_CORE + bool "Core Scheduling for SMT" + default y + depends on SCHED_SMT +
kernel/sched/core.c +137 -0
161 lines not on this page · raw message
kernel/sched/sched.h +37 -0
@@ -1075,6 +1075,12 @@ struct rq {#endif unsigned int push_busy;struct cpu_stop_work push_work; + +#ifdef CONFIG_SCHED_CORE +/* per rq */ +struct rq *core; +unsigned int core_enabled; +#endif };#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -1113,6 +1119,35 @@ static inline bool is_migration_disabled#endif } +#ifdef CONFIG_SCHED_CORE + +DECLARE_STATIC_KEY_FALSE (__sched_core_enabled); + +static inline bool sched_core_enabled (struct rq *rq) +{ +return static_branch_unlikely (&__sched_core_enabled) && rq->core_enabled; +} + +static inline bool sched_core_disabled (void ) +{ +return !static_branch_unlikely (&__sched_core_enabled); +} + +static inline raw_spinlock_t *rq_lockp (struct rq *rq) +{ +if (sched_core_enabled (rq)) +return &rq->core->__lock; + +return &rq->__lock; +} + +#else /* !CONFIG_SCHED_CORE */ + +static inline bool sched_core_enabled (struct rq *rq) +{ +return false; +} +static inline bool sched_core_disabled (void ) {return true;
@@ -1123,6 +1158,8 @@ static inline raw_spinlock_t *rq_lockp(s return &rq->__lock; } +#endif /* CONFIG_SCHED_CORE */ +static inline void lockdep_assert_rq_held (struct rq *rq) {lockdep_assert_held (rq_lockp (rq));
Peter Zijlstra
maintainerPATCH 6/19 sched: Optimize rq_lockp() usage
In reply to Peter Zijlstrarq_lockp() includes a static_branch(), which is asm-goto, which is asm volatile which defeats regular CSE. This means that:
if (!static_branch(&foo)) return simple; if (static_branch(&foo) && cond) return complex;
Doesn't fold and we get horrible code. Introduce __rq_lockp() without the static_branch() on.
--- kernel/sched/core.c | 16 ++++++++-------- kernel/sched/deadline.c | 4 ++-- kernel/sched/fair.c | 2 +- kernel/sched/sched.h | 33 +++++++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 19 deletions(-)
kernel/sched/core.c +8 -8
@@ -281,9 +281,9 @@ void raw_spin_rq_lock_nested(struct rq * }for (;;) { - lock =rq_lockp (rq); + lock =__rq_lockp (rq);raw_spin_lock_nested (lock, subclass); -if (likely (lock ==rq_lockp (rq))) +if (likely (lock ==__rq_lockp (rq)))return ;raw_spin_unlock (lock); }
@@ -298,9 +298,9 @@ bool raw_spin_rq_trylock(struct rq *rq)return raw_spin_trylock (&rq->__lock);for (;;) { - lock =rq_lockp (rq); + lock =__rq_lockp (rq); ret =raw_spin_trylock (lock); -if (!ret || (likely (lock ==rq_lockp (rq)))) +if (!ret || (likely (lock ==__rq_lockp (rq))))return ret;raw_spin_unlock (lock); }
@@ -323,7 +323,7 @@ void double_rq_lock(struct rq *rq1, struswap (rq1, rq2);raw_spin_rq_lock (rq1); -if (rq_lockp (rq1) ==rq_lockp (rq2)) +if (__rq_lockp (rq1) ==__rq_lockp (rq2))return ;raw_spin_rq_lock_nested (rq2,SINGLE_DEPTH_NESTING );
@@ -2594,7 +2594,7 @@ void set_task_cpu(struct task_struct *p,* task_rq_lock(). */ WARN_ON_ONCE (debug_locks && !(lockdep_is_held (&p->pi_lock) || -lockdep_is_held (rq_lockp (task_rq (p))))); +lockdep_is_held (__rq_lockp (task_rq (p)))));#endif /* * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
@@ -4220,7 +4220,7 @@ prepare_lock_switch(struct rq *rq, struc* do an early lockdep release here: */ rq_unpin_lock (rq, rf); -spin_release (&rq_lockp (rq)->dep_map, _THIS_IP_); +spin_release (&__rq_lockp (rq)->dep_map, _THIS_IP_);#ifdef CONFIG_DEBUG_SPINLOCK /* this is a valid case when another task releases the spinlock */ rq_lockp (rq)->owner = next;
@@ -4234,7 +4234,7 @@ static inline void finish_lock_switch(st* fix up the runqueue lock - which gets 'carried over' from * prev into current: */ -spin_acquire (&rq_lockp (rq)->dep_map,0 ,0 , _THIS_IP_); +spin_acquire (&__rq_lockp (rq)->dep_map,0 ,0 , _THIS_IP_);__balance_callbacks (rq);raw_spin_rq_unlock_irq (rq); }
kernel/sched/deadline.c +2 -2
@@ -1097,9 +1097,9 @@ static enum hrtimer_restart dl_task_time* If the runqueue is no longer available, migrate the * task elsewhere. This necessarily changes rq. */ -lockdep_unpin_lock (rq_lockp (rq), rf.cookie); +lockdep_unpin_lock (__rq_lockp (rq), rf.cookie); rq =dl_task_offline_migration (rq, p); - rf.cookie =lockdep_pin_lock (rq_lockp (rq)); + rf.cookie =lockdep_pin_lock (__rq_lockp (rq));update_rq_clock (rq);/*
kernel/sched/fair.c +1 -1
@@ -1107,7 +1107,7 @@ struct numa_group {static struct numa_group *deref_task_numa_group (struct task_struct *p) {return rcu_dereference_check (p->numa_group, p == current || - (lockdep_is_held (rq_lockp (task_rq (p))) && !READ_ONCE (p->on_cpu))); + (lockdep_is_held (__rq_lockp (task_rq (p))) && !READ_ONCE (p->on_cpu))); }static struct numa_group *deref_curr_numa_group (struct task_struct *p)
kernel/sched/sched.h +25 -8
@@ -1127,6 +1127,10 @@ static inline bool sched_core_disabled(vreturn !static_branch_unlikely (&__sched_core_enabled); } +/* +* Be careful with this function; not for general use. The return value isn't +* stable unless you actually hold a relevant rq->__lock. +*/ static inline raw_spinlock_t *rq_lockp (struct rq *rq) {if (sched_core_enabled (rq))
@@ -1135,6 +1139,14 @@ static inline raw_spinlock_t *rq_lockp(s return &rq->__lock; } +static inline raw_spinlock_t *__rq_lockp (struct rq *rq) +{ +if (rq->core_enabled) +return &rq->core->__lock; + +return &rq->__lock; +} +#else /* !CONFIG_SCHED_CORE */ static inline bool sched_core_enabled (struct rq *rq)
@@ -1152,11 +1164,16 @@ static inline raw_spinlock_t *rq_lockp(s return &rq->__lock; } +static inline raw_spinlock_t *__rq_lockp (struct rq *rq) +{ +return &rq->__lock; +} +#endif /* CONFIG_SCHED_CORE */ static inline void lockdep_assert_rq_held (struct rq *rq) { -lockdep_assert_held (rq_lockp (rq)); +lockdep_assert_held (__rq_lockp (rq)); }extern void raw_spin_rq_lock_nested (struct rq *rq,int subclass);
@@ -1340,7 +1357,7 @@ extern struct callback_head balance_push*/ static inline void rq_pin_lock (struct rq *rq,struct rq_flags *rf) { - rf->cookie =lockdep_pin_lock (rq_lockp (rq)); + rf->cookie =lockdep_pin_lock (__rq_lockp (rq));#ifdef CONFIG_SCHED_DEBUG rq->clock_update_flags &= (RQCF_REQ_SKIP |RQCF_ACT_SKIP );
@@ -1358,12 +1375,12 @@ static inline void rq_unpin_lock(struct rf->clock_update_flags =RQCF_UPDATED ;#endif -lockdep_unpin_lock (rq_lockp (rq), rf->cookie); +lockdep_unpin_lock (__rq_lockp (rq), rf->cookie); }static inline void rq_repin_lock (struct rq *rq,struct rq_flags *rf) { -lockdep_repin_lock (rq_lockp (rq), rf->cookie); +lockdep_repin_lock (__rq_lockp (rq), rf->cookie);#ifdef CONFIG_SCHED_DEBUG /*
@@ -2306,7 +2323,7 @@ static inline int _double_lock_balance(s __acquires(busiest->lock) __acquires(this_rq->lock) { -if (rq_lockp (this_rq) ==rq_lockp (busiest)) +if (__rq_lockp (this_rq) ==__rq_lockp (busiest))return 0 ;if (likely (raw_spin_rq_trylock (busiest)))
@@ -2338,9 +2355,9 @@ static inline int double_lock_balance(ststatic inline void double_unlock_balance (struct rq *this_rq,struct rq *busiest) __releases(busiest->lock) { -if (rq_lockp (this_rq) !=rq_lockp (busiest)) +if (__rq_lockp (this_rq) !=__rq_lockp (busiest))raw_spin_rq_unlock (busiest); -lock_set_subclass (&rq_lockp (this_rq)->dep_map,0 , _RET_IP_); +lock_set_subclass (&__rq_lockp (this_rq)->dep_map,0 , _RET_IP_); }static inline void double_lock (spinlock_t *l1,spinlock_t *l2)
@@ -2381,7 +2398,7 @@ static inline void double_rq_unlock(stru __releases(rq2->lock) {raw_spin_rq_unlock (rq1); -if (rq_lockp (rq1) !=rq_lockp (rq2)) +if (__rq_lockp (rq1) !=__rq_lockp (rq2))raw_spin_rq_unlock (rq2);else __release (rq2->lock);
Peter Zijlstra
maintainerPATCH 9/19 sched: Basic tracking of matching tasks
In reply to Peter ZijlstraIntroduce task_struct::core_cookie as an opaque identifier for core scheduling. When enabled; core scheduling will only allow matching task to be on the core; where idle matches everything.
When task_struct::core_cookie is set (and core scheduling is enabled) these tasks are indexed in a second RB-tree, first on cookie value then on scheduling function, such that matching task selection always finds the most elegible match.
NOTE: *shudder* at the overhead...
NOTE: *sigh*, a 3rd copy of the scheduling function; the alternative is per class tracking of cookies and that just duplicates a lot of stuff for no raisin (the 2nd copy lives in the rt-mutex PI code).
--- include/linux/sched.h | 8 ++ kernel/sched/core.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++-- kernel/sched/fair.c | 46 --------------- kernel/sched/sched.h | 55 ++++++++++++++++++ 4 files changed, 210 insertions(+), 51 deletions(-)
include/linux/sched.h +7 -1
@@ -699,10 +699,16 @@ struct task_struct {const struct sched_class *sched_class;struct sched_entity se;struct sched_rt_entity rt; +struct sched_dl_entity dl; + +#ifdef CONFIG_SCHED_CORE +struct rb_node core_node; +unsigned long core_cookie; +#endif +#ifdef CONFIG_CGROUP_SCHED struct task_group *sched_task_group;#endif -struct sched_dl_entity dl;#ifdef CONFIG_UCLAMP_TASK /*
kernel/sched/core.c +148 -4
188 lines not on this page · raw message
kernel/sched/fair.c +0 -46
66 lines not on this page · raw message
kernel/sched/sched.h +55 -0
67 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 14/19 sched: Trivial forced-newidle balancer
In reply to Peter ZijlstraWhen a sibling is forced-idle to match the core-cookie; search for matching tasks to fill the core.
rcu_read_unlock() can incur an infrequent deadlock in sched_core_balance(). Fix this by using the RCU-sched flavor instead.
--- include/linux/sched.h | 1 kernel/sched/core.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++- kernel/sched/idle.c | 1 kernel/sched/sched.h | 6 ++ 4 files changed, 137 insertions(+), 1 deletion(-)
include/linux/sched.h +1 -0
@@ -704,6 +704,7 @@ struct task_struct {#ifdef CONFIG_SCHED_CORE struct rb_node core_node;unsigned long core_cookie; +unsigned int core_occupation;#endif #ifdef CONFIG_CGROUP_SCHED
kernel/sched/core.c +129 -1
167 lines not on this page · raw message
kernel/sched/idle.c +1 -0
@@ -437,6 +437,7 @@ static void set_next_task_idle(struct rq {update_idle_core (rq);schedstat_inc (rq->sched_goidle); +queue_core_balance (rq); }#ifdef CONFIG_SMP
kernel/sched/sched.h +6 -0
@@ -1170,6 +1170,8 @@ static inline raw_spinlock_t *__rq_lockpbool cfs_prio_less (struct task_struct *a,struct task_struct *b,bool fi); +extern void queue_core_balance (struct rq *rq); +#else /* !CONFIG_SCHED_CORE */ static inline bool sched_core_enabled (struct rq *rq)
@@ -1187,6 +1189,10 @@ static inline raw_spinlock_t *rq_lockp(s return &rq->__lock; } +static inline void queue_core_balance (struct rq *rq) +{ +} +#endif /* CONFIG_SCHED_CORE */ static inline void lockdep_assert_rq_held (struct rq *rq)
Peter Zijlstra
maintainerPATCH 8/19 sched: Introduce sched_class::pick_task()
In reply to Peter ZijlstraBecause sched_class::pick_next_task() also implies sched_class::set_next_task() (and possibly put_prev_task() and newidle_balance) it is not state invariant. This makes it unsuitable for remote task selection.
--- kernel/sched/deadline.c | 16 ++++++++++++++-- kernel/sched/fair.c | 40 +++++++++++++++++++++++++++++++++++++--- kernel/sched/idle.c | 8 ++++++++ kernel/sched/rt.c | 15 +++++++++++++-- kernel/sched/sched.h | 3 +++ kernel/sched/stop_task.c | 14 ++++++++++++-- 6 files changed, 87 insertions(+), 9 deletions(-)
kernel/sched/deadline.c +14 -2
@@ -1873,7 +1873,7 @@ static struct sched_dl_entity *pick_next return rb_entry(left, struct sched_dl_entity, rb_node); } -static struct task_struct *pick_next_task_dl (struct rq *rq) +static struct task_struct *pick_task_dl (struct rq *rq) {struct sched_dl_entity *dl_se;struct dl_rq *dl_rq = &rq->dl;
@@ -1885,7 +1885,18 @@ static struct task_struct *pick_next_tas dl_se =pick_next_dl_entity (rq, dl_rq);BUG_ON (!dl_se); p =dl_task_of (dl_se); -set_next_task_dl (rq, p, true); + +return p; +} + +static struct task_struct *pick_next_task_dl (struct rq *rq) +{ +struct task_struct *p; + + p =pick_task_dl (rq); +if (p) +set_next_task_dl (rq, p, true); +return p; }
@@ -2557,6 +2568,7 @@ DEFINE_SCHED_CLASS(dl) = {#ifdef CONFIG_SMP .balance = balance_dl, + .pick_task = pick_task_dl, .select_task_rq = select_task_rq_dl, .migrate_task_rq = migrate_task_rq_dl, .set_cpus_allowed = set_cpus_allowed_dl,
kernel/sched/fair.c +37 -3
@@ -4400,6 +4400,8 @@ check_preempt_tick(struct cfs_rq *cfs_rqstatic void set_next_entity (struct cfs_rq *cfs_rq,struct sched_entity *se) { +clear_buddies (cfs_rq, se); +/* 'current' is not kept within the tree. */ if (se->on_rq) {/*
@@ -4459,7 +4461,7 @@ pick_next_entity(struct cfs_rq *cfs_rq,* Avoid running the skip buddy, if running something else can * be done without getting too unfair. */ -if (cfs_rq->skip == se) { +if (cfs_rq->skip && cfs_rq->skip == se) {struct sched_entity *second;if (se == curr) {
@@ -4486,8 +4488,6 @@ pick_next_entity(struct cfs_rq *cfs_rq, se = cfs_rq->last; } -clear_buddies (cfs_rq, se); -return se; }
@@ -7018,6 +7018,39 @@ static void check_preempt_wakeup(structset_last_buddy (se); } +#ifdef CONFIG_SMP +static struct task_struct *pick_task_fair (struct rq *rq) +{ +struct sched_entity *se; +struct cfs_rq *cfs_rq; + +again: + cfs_rq = &rq->cfs; +if (!cfs_rq->nr_running) +return NULL ; + +do { +struct sched_entity *curr = cfs_rq->curr; + +/* When we pick for a remote RQ, we'll not have done put_prev_entity() */ +if (curr) { +if (curr->on_rq) +update_curr (cfs_rq); +else + curr =NULL ; + +if (unlikely (check_cfs_rq_runtime (cfs_rq))) + goto again; + } + + se =pick_next_entity (cfs_rq, curr); + cfs_rq =group_cfs_rq (se); + }while (cfs_rq); + +return task_of (se); +} +#endif +struct task_struct *pick_next_task_fair (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) {
@@ -11220,6 +11253,7 @@ DEFINE_SCHED_CLASS(fair) = {#ifdef CONFIG_SMP .balance = balance_fair, + .pick_task = pick_task_fair, .select_task_rq = select_task_rq_fair, .migrate_task_rq = migrate_task_rq_fair,
kernel/sched/idle.c +8 -0
@@ -406,6 +406,13 @@ static void set_next_task_idle(struct rqschedstat_inc (rq->sched_goidle); } +#ifdef CONFIG_SMP +static struct task_struct *pick_task_idle (struct rq *rq) +{ +return rq->idle; +} +#endif +struct task_struct *pick_next_task_idle (struct rq *rq) {struct task_struct *next = rq->idle;
@@ -473,6 +480,7 @@ DEFINE_SCHED_CLASS(idle) = {#ifdef CONFIG_SMP .balance = balance_idle, + .pick_task = pick_task_idle, .select_task_rq = select_task_rq_idle, .set_cpus_allowed = set_cpus_allowed_common,#endif
kernel/sched/rt.c +13 -2
@@ -1626,7 +1626,7 @@ static struct task_struct *_pick_next_tareturn rt_task_of (rt_se); } -static struct task_struct *pick_next_task_rt (struct rq *rq) +static struct task_struct *pick_task_rt (struct rq *rq) {struct task_struct *p;
@@ -1634,7 +1634,17 @@ static struct task_struct *pick_next_tasreturn NULL ; p =_pick_next_task_rt (rq); -set_next_task_rt (rq, p, true); + +return p; +} + +static struct task_struct *pick_next_task_rt (struct rq *rq) +{ +struct task_struct *p =pick_task_rt (rq); + +if (p) +set_next_task_rt (rq, p, true); +return p; }
@@ -2483,6 +2493,7 @@ DEFINE_SCHED_CLASS(rt) = {#ifdef CONFIG_SMP .balance = balance_rt, + .pick_task = pick_task_rt, .select_task_rq = select_task_rq_rt, .set_cpus_allowed = set_cpus_allowed_common, .rq_online = rq_online_rt,
kernel/sched/sched.h +3 -0
@@ -1839,6 +1839,9 @@ struct sched_class {#ifdef CONFIG_SMP int (*balance)(struct rq *rq,struct task_struct *prev,struct rq_flags *rf);int (*select_task_rq)(struct task_struct *p,int task_cpu,int flags); + +struct task_struct * (*pick_task)(struct rq *rq); +void (*migrate_task_rq)(struct task_struct *p,int new_cpu);void (*task_woken)(struct rq *this_rq,struct task_struct *task);
kernel/sched/stop_task.c +12 -2
@@ -34,15 +34,24 @@ static void set_next_task_stop(struct rq stop->se.exec_start =rq_clock_task (rq); } -static struct task_struct *pick_next_task_stop (struct rq *rq) +static struct task_struct *pick_task_stop (struct rq *rq) {if (!sched_stop_runnable (rq))return NULL ; -set_next_task_stop (rq, rq->stop, true);return rq->stop; } +static struct task_struct *pick_next_task_stop (struct rq *rq) +{ +struct task_struct *p =pick_task_stop (rq); + +if (p) +set_next_task_stop (rq, p, true); + +return p; +} +static void enqueue_task_stop (struct rq *rq,struct task_struct *p,int flags) {
@@ -123,6 +132,7 @@ DEFINE_SCHED_CLASS(stop) = {#ifdef CONFIG_SMP .balance = balance_stop, + .pick_task = pick_task_stop, .select_task_rq = select_task_rq_stop, .set_cpus_allowed = set_cpus_allowed_common,#endif
Peter Zijlstra
maintainerPATCH 16/19 sched: Trivial core scheduling cookie management
In reply to Peter ZijlstraIn order to not have to use pid_struct, create a new, smaller, structure to manage task cookies for core scheduling.
--- include/linux/sched.h | 6 ++ kernel/fork.c | 1 kernel/sched/Makefile | 1 kernel/sched/core.c | 7 +- kernel/sched/core_sched.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 16 ++++++ 6 files changed, 137 insertions(+), 3 deletions(-)
include/linux/sched.h +6 -0
@@ -2170,4 +2170,10 @@ int sched_trace_rq_nr_running(struct rqconst struct cpumask *sched_trace_rd_span (struct root_domain *rd); +#ifdef CONFIG_SCHED_CORE +extern void sched_core_free (struct task_struct *tsk); +#else +static inline void sched_core_free (struct task_struct *tsk) { } +#endif +#endif
kernel/fork.c +1 -0
@@ -737,6 +737,7 @@ void __put_task_struct(struct task_strucexit_creds (tsk);delayacct_tsk_free (tsk);put_signal_struct (tsk->signal); +sched_core_free (tsk);if (!profile_handoff_task (tsk))free_task (tsk);
kernel/sched/Makefile +1 -0
@@ -36,3 +36,4 @@ obj-$(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) += obj-$(CONFIG_MEMBARRIER) += membarrier.o obj-$(CONFIG_CPU_ISOLATION) += isolation.o obj-$(CONFIG_PSI) += psi.o +obj-$(CONFIG_SCHED_CORE) += core_sched.o
kernel/sched/core.c +4 -3
@@ -157,7 +157,7 @@ static inline int rb_sched_core_cmp(consreturn 0 ; } -static void sched_core_enqueue (struct rq *rq,struct task_struct *p) +void sched_core_enqueue (struct rq *rq,struct task_struct *p) { rq->core->core_task_seq++;
@@ -167,14 +167,15 @@ static void sched_core_enqueue(struct rqrb_add (&p->core_node, &rq->core_tree, rb_sched_core_less); } -static void sched_core_dequeue (struct rq *rq,struct task_struct *p) +void sched_core_dequeue (struct rq *rq,struct task_struct *p) { rq->core->core_task_seq++; -if (!p->core_cookie) +if (!sched_core_enqueued (p))return ;rb_erase (&p->core_node, &rq->core_tree); +RB_CLEAR_NODE (&p->core_node); }/*
kernel/sched/core_sched.c new file +109 -0
109 lines not on this page · raw message
kernel/sched/sched.h +16 -0
@@ -1222,6 +1222,22 @@ static inline bool sched_group_cookie_maextern void queue_core_balance (struct rq *rq); +static inline bool sched_core_enqueued (struct task_struct *p) +{ +return !RB_EMPTY_NODE (&p->core_node); +} + +extern void sched_core_enqueue (struct rq *rq,struct task_struct *p); +extern void sched_core_dequeue (struct rq *rq,struct task_struct *p); + +extern void sched_core_get (void ); +extern void sched_core_put (void ); + +extern unsigned long sched_core_alloc_cookie (void ); +extern void sched_core_put_cookie (unsigned long cookie); +extern unsigned long sched_core_get_cookie (unsigned long cookie); +extern unsigned long sched_core_update_cookie (struct task_struct *p,unsigned long cookie); +#else /* !CONFIG_SCHED_CORE */ static inline bool sched_core_enabled (struct rq *rq)
Peter Zijlstra
maintainerPATCH 17/19 sched: Inherit task cookie on fork()
In reply to Peter ZijlstraNote that sched_core_fork() is called from under tasklist_lock, and not from sched_fork() earlier. This avoids a few races later.
--- include/linux/sched.h | 2 ++ kernel/fork.c | 3 +++ kernel/sched/core_sched.c | 6 ++++++ 3 files changed, 11 insertions(+)
include/linux/sched.h +2 -0
@@ -2172,8 +2172,10 @@ const struct cpumask *sched_trace_rd_spa#ifdef CONFIG_SCHED_CORE extern void sched_core_free (struct task_struct *tsk); +extern void sched_core_fork (struct task_struct *p);#else static inline void sched_core_free (struct task_struct *tsk) { } +static inline void sched_core_fork (struct task_struct *p) { }#endif #endif
kernel/fork.c +3 -0
@@ -2249,6 +2249,8 @@ static __latent_entropy struct task_struklp_copy_process (p); +sched_core_fork (p); +spin_lock (¤t->sighand->siglock);/*
@@ -2336,6 +2338,7 @@ static __latent_entropy struct task_strureturn p; bad_fork_cancel_cgroup: +sched_core_free (p);spin_unlock (¤t->sighand->siglock);write_unlock_irq (&tasklist_lock);cgroup_cancel_fork (p, args);
kernel/sched/core_sched.c +6 -0
@@ -100,6 +100,12 @@ static unsigned long sched_core_clone_coreturn cookie; } +void sched_core_fork (struct task_struct *p) +{ +RB_CLEAR_NODE (&p->core_node); + p->core_cookie =sched_core_clone_cookie (current); +} +void sched_core_free (struct task_struct *p) {sched_core_put_cookie (p->core_cookie);
Peter Zijlstra
maintainerPATCH 18/19 sched: prctl() core-scheduling interface
In reply to Peter ZijlstraFrom: Chris Hyser <[email protected]>
This patch provides support for setting and copying core scheduling 'task cookies' between threads (PID), processes (TGID), and process groups (PGID).
The value of core scheduling isn't that tasks don't share a core, 'nosmt' can do that. The value lies in exploiting all the sharing opportunities that exist to recover possible lost performance and that requires a degree of flexibility in the API.
Quoted 1 line
From a security perspective (and there are others), the thread,
process and process group distinction is an existent hierarchal categorization of tasks that reflects many of the security concerns about 'data sharing'. For example, protecting against cache-snooping by a thread that can just read the memory directly isn't all that useful.
With this in mind, subcommands to CREATE/SHARE (TO/FROM) provide a mechanism to create and share cookies. CREATE/SHARE_TO specify a target pid with enum pidtype used to specify the scope of the targeted tasks. For example, PIDTYPE_TGID will share the cookie with the process and all of it's threads as typically desired in a security scenario.
API:
prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, tgtpid, pidtype, &cookie) prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, tgtpid, pidtype, NULL) prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, tgtpid, pidtype, NULL) prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, srcpid, pidtype, NULL)
where 'tgtpid/srcpid == 0' implies the current process and pidtype is kernel enum pid_type {PIDTYPE_PID, PIDTYPE_TGID, PIDTYPE_PGID, ...}.
For return values, EINVAL, ENOMEM are what they say. ESRCH means the tgtpid/srcpid was not found. EPERM indicates lack of PTRACE permission access to tgtpid/srcpid. ENODEV indicates your machines lacks SMT.
--- include/linux/sched.h | 2 include/uapi/linux/prctl.h | 8 ++ kernel/sched/core_sched.c | 114 +++++++++++++++++++++++++++++++++++++++ kernel/sys.c | 5 + tools/include/uapi/linux/prctl.h | 8 ++ 5 files changed, 137 insertions(+)
include/linux/sched.h +2 -0
@@ -2173,6 +2173,8 @@ const struct cpumask *sched_trace_rd_spa#ifdef CONFIG_SCHED_CORE extern void sched_core_free (struct task_struct *tsk);extern void sched_core_fork (struct task_struct *p); +extern int sched_core_share_pid (unsigned int cmd,pid_t pid,enum pid_type type, +unsigned long uaddr);#else static inline void sched_core_free (struct task_struct *tsk) { }static inline void sched_core_fork (struct task_struct *p) { }
include/uapi/linux/prctl.h +8 -0
@@ -255,4 +255,12 @@ struct prctl_mm_map {# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE 60 +# define PR_SCHED_CORE_GET 0 +# define PR_SCHED_CORE_CREATE 1/* create unique core_sched cookie */ +# define PR_SCHED_CORE_SHARE_TO 2/* push core_sched cookie to pid */ +# define PR_SCHED_CORE_SHARE_FROM 3/* pull core_sched cookie to pid */ +# define PR_SCHED_CORE_MAX 4 +#endif /* _LINUX_PRCTL_H */
kernel/sched/core_sched.c +114 -0
122 lines not on this page · raw message
kernel/sys.c +5 -0
@@ -2534,6 +2534,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsi error =set_syscall_user_dispatch (arg2, arg3, arg4, (char __user *) arg5);break ; +#ifdef CONFIG_SCHED_CORE +case PR_SCHED_CORE : + error =sched_core_share_pid (arg2, arg3, arg4, arg5); +break ; +#endif default : error = -EINVAL ;break ;
tools/include/uapi/linux/prctl.h +8 -0
@@ -255,4 +255,12 @@ struct prctl_mm_map {# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1 +/* Request the scheduler to share a core */ +#define PR_SCHED_CORE 60 +# define PR_SCHED_CORE_GET 0 +# define PR_SCHED_CORE_CREATE 1/* create unique core_sched cookie */ +# define PR_SCHED_CORE_SHARE_TO 2/* push core_sched cookie to pid */ +# define PR_SCHED_CORE_SHARE_FROM 3/* pull core_sched cookie to pid */ +# define PR_SCHED_CORE_MAX 4 +#endif /* _LINUX_PRCTL_H */
Peter Zijlstra
maintainerPATCH 15/19 sched: Migration changes for core scheduling
In reply to Peter ZijlstraFrom: Aubrey Li <[email protected]>
- Don't migrate if there is a cookie mismatch
Load balance tries to move task from busiest CPU to the destination CPU. When core scheduling is enabled, if the task's cookie does not match with the destination CPU's core cookie, this task may be skipped by this CPU. This mitigates the forced idle time on the destination CPU.
- Select cookie matched idle CPU
In the fast path of task wakeup, select the first cookie matched idle CPU instead of the first idle CPU.
- Find cookie matched idlest CPU
In the slow path of task wakeup, find the idlest CPU whose core cookie matches with task's cookie
--- kernel/sched/fair.c | 29 ++++++++++++++++---- kernel/sched/sched.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-)
kernel/sched/fair.c +23 -6
@@ -5889,11 +5889,15 @@ find_idlest_group_cpu(struct sched_group/* Traverse only the allowed CPUs */ for_each_cpu_and (i,sched_group_span (group), p->cpus_ptr) { +struct rq *rq =cpu_rq (i); + +if (!sched_core_cookie_match (rq, p)) +continue ; +if (sched_idle_cpu (i))return i;if (available_idle_cpu (i)) { -struct rq *rq =cpu_rq (i);struct cpuidle_state *idle =idle_get_state (rq);if (idle && idle->exit_latency < min_exit_latency) {/*
@@ -5979,9 +5983,10 @@ static inline int find_idlest_cpu(structreturn new_cpu; } -static inline int __select_idle_cpu (int cpu) +static inline int __select_idle_cpu (int cpu,struct task_struct *p) { -if (available_idle_cpu (cpu) ||sched_idle_cpu (cpu)) +if ((available_idle_cpu (cpu) ||sched_idle_cpu (cpu)) && +sched_cpu_cookie_match (cpu_rq (cpu), p))return cpu;return -1 ;
@@ -6051,7 +6056,7 @@ static int select_idle_core(struct task_int cpu;if (!static_branch_likely (&sched_smt_present)) -return __select_idle_cpu (core); +return __select_idle_cpu (core, p);for_each_cpu (cpu,cpu_smt_mask (core)) {if (!available_idle_cpu (cpu)) {
@@ -6107,7 +6112,7 @@ static inline bool test_idle_cores(int cstatic inline int select_idle_core (struct task_struct *p,int core,struct cpumask *cpus,int *idle_cpu) { -return __select_idle_cpu (core); +return __select_idle_cpu (core, p); }static inline int select_idle_smt (struct task_struct *p,struct sched_domain *sd,int target)
@@ -6164,7 +6169,7 @@ static int select_idle_cpu(struct task_s }else {if (!--nr)return -1 ; - idle_cpu =__select_idle_cpu (cpu); + idle_cpu =__select_idle_cpu (cpu, p);if ((unsigned int )idle_cpu < nr_cpumask_bits)break ; }
@@ -7517,6 +7522,14 @@ static int task_hot(struct task_struct *if (sysctl_sched_migration_cost ==-1 )return 1 ; + +/* +* Don't migrate task if the task's cookie does not match +* with the destination CPU's core cookie. +*/ +if (!sched_core_cookie_match (cpu_rq (env->dst_cpu), p)) +return 1 ; +if (sysctl_sched_migration_cost ==0 )return 0 ;
@@ -8847,6 +8860,10 @@ find_idlest_group(struct sched_domain *s p->cpus_ptr))continue ; +/* Skip over this group if no cookie matched */ +if (!sched_group_cookie_match (cpu_rq (this_cpu), p, group)) +continue ; + local_group =cpumask_test_cpu (this_cpu,sched_group_span (group));
kernel/sched/sched.h +73 -0
92 lines not on this page · raw message
Peter Zijlstra
maintainerPATCH 7/19 sched: Allow sched_core_put() from atomic context
In reply to Peter ZijlstraStuff the meat of sched_core_put() into a work such that we can use sched_core_put() from atomic context.
--- kernel/sched/core.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)
kernel/sched/core.c +27 -6
@@ -102,7 +102,7 @@ DEFINE_STATIC_KEY_FALSE(__sched_core_ena*/ static DEFINE_MUTEX (sched_core_mutex); -static int sched_core_count; +static atomic_t sched_core_count;static struct cpumask sched_core_mask;static void __sched_core_flip (bool enabled)
@@ -165,18 +165,39 @@ static void __sched_core_disable(void)void sched_core_get (void ) { +if (atomic_inc_not_zero (&sched_core_count)) +return ; +mutex_lock (&sched_core_mutex); -if (!sched_core_count++) +if (!atomic_read (&sched_core_count))__sched_core_enable (); + +smp_mb__before_atomic (); +atomic_inc (&sched_core_count);mutex_unlock (&sched_core_mutex); } -void sched_core_put (void ) +static void __sched_core_put (struct work_struct *work) { -mutex_lock (&sched_core_mutex); -if (!--sched_core_count) +if (atomic_dec_and_mutex_lock (&sched_core_count, &sched_core_mutex)) {__sched_core_disable (); -mutex_unlock (&sched_core_mutex); +mutex_unlock (&sched_core_mutex); + } +} + +void sched_core_put (void ) +{ + static DECLARE_WORK(_work, __sched_core_put); + +/* +* "There can be only one" +* +* Either this is the last one, or we don't actually need to do any +* 'work'. If it is the last *again*, we rely on +* WORK_STRUCT_PENDING_BIT. +*/ +if (!atomic_add_unless (&sched_core_count,-1 ,1 )) +schedule_work (&_work); }#endif /* CONFIG_SCHED_CORE */
Peter Zijlstra
maintainerPATCH 10/19 sched: Add core wide task selection and scheduling
In reply to Peter ZijlstraInstead of only selecting a local task, select a task for all SMT siblings for every reschedule on the core (irrespective which logical CPU does the reschedule).
--- kernel/sched/core.c | 301 ++++++++++++++++++++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 6 - 2 files changed, 305 insertions(+), 2 deletions(-)
kernel/sched/core.c +300 -1
@@ -5077,7 +5077,7 @@ static void put_prev_task_balance(struct* Pick up the highest-prio task: */ static inline struct task_struct * -pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +__pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) {const struct sched_class *class;struct task_struct *p;
@@ -5118,6 +5118,294 @@ pick_next_task(struct rq *rq, struct tas }#ifdef CONFIG_SCHED_CORE +static inline bool is_task_rq_idle (struct task_struct *t) +{ +return (task_rq (t)->idle == t); +} + +static inline bool cookie_equals (struct task_struct *a,unsigned long cookie) +{ +return is_task_rq_idle (a) || (a->core_cookie == cookie); +} + +static inline bool cookie_match (struct task_struct *a,struct task_struct *b) +{ +if (is_task_rq_idle (a) ||is_task_rq_idle (b)) +return true; + +return a->core_cookie == b->core_cookie; +} + +// XXX fairness/fwd progress conditions +/* +* Returns +* - NULL if there is no runnable task for this class. +* - the highest priority task for this runqueue if it matches +* rq->core->core_cookie or its priority is greater than max. +* - Else returns idle_task. +*/ +static struct task_struct * +pick_task (struct rq *rq,const struct sched_class *class,struct task_struct *max) +{ +struct task_struct *class_pick, *cookie_pick; +unsigned long cookie = rq->core->core_cookie; + + class_pick = class->pick_task (rq); +if (!class_pick) +return NULL ; + +if (!cookie) { +/* +* If class_pick is tagged, return it only if it has +* higher priority than max. +*/ +if (max && class_pick->core_cookie && +prio_less (class_pick, max)) +return idle_sched_class.pick_task (rq); + +return class_pick; + } + +/* +* If class_pick is idle or matches cookie, return early. +*/ +if (cookie_equals (class_pick, cookie)) +return class_pick; + + cookie_pick =sched_core_find (rq, cookie); + +/* +* If class > max && class > cookie, it is the highest priority task on +* the core (so far) and it must be selected, otherwise we must go with +* the cookie pick in order to satisfy the constraint. +*/ +if (prio_less (cookie_pick, class_pick) && + (!max ||prio_less (max, class_pick))) +return class_pick; + +return cookie_pick; +} + +static struct task_struct * +pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +{ +struct task_struct *next, *max =NULL ; +const struct sched_class *class; +const struct cpumask *smt_mask; +bool need_sync; +int i, j, cpu; + +if (!sched_core_enabled (rq)) +return __pick_next_task (rq, prev, rf); + + cpu =cpu_of (rq); + +/* Stopper task is switching into idle, no need core-wide selection. */ +if (cpu_is_offline (cpu)) { +/* +* Reset core_pick so that we don't enter the fastpath when +* coming online. core_pick would already be migrated to +* another cpu during offline. +*/ + rq->core_pick =NULL ; +return __pick_next_task (rq, prev, rf); + } + +/* +* If there were no {en,de}queues since we picked (IOW, the task +* pointers are all still valid), and we haven't scheduled the last +* pick yet, do so now. +* +* rq->core_pick can be NULL if no selection was made for a CPU because +* it was either offline or went offline during a sibling's core-wide +* selection. In this case, do a core-wide selection. +*/ +if (rq->core->core_pick_seq == rq->core->core_task_seq && + rq->core->core_pick_seq != rq->core_sched_seq && + rq->core_pick) { +WRITE_ONCE (rq->core_sched_seq, rq->core->core_pick_seq); + + next = rq->core_pick; +if (next != prev) { +put_prev_task (rq, prev); +set_next_task (rq, next); + } + + rq->core_pick =NULL ; +return next; + } + +put_prev_task_balance (rq, prev, rf); + + smt_mask =cpu_smt_mask (cpu); + +/* +* core->core_task_seq, core->core_pick_seq, rq->core_sched_seq +* +* @task_seq guards the task state ({en,de}queues) +* @pick_seq is the @task_seq we did a selection on +* @sched_seq is the @pick_seq we scheduled +* +* However, preemptions can cause multiple picks on the same task set. +* 'Fix' this by also increasing @task_seq for every pick. +*/ + rq->core->core_task_seq++; + need_sync = !!rq->core->core_cookie; + +/* reset state */ + rq->core->core_cookie =0UL ; +for_each_cpu (i, smt_mask) { +struct rq *rq_i =cpu_rq (i); + + rq_i->core_pick =NULL ; + +if (rq_i->core_forceidle) { + need_sync = true; + rq_i->core_forceidle = false; + } + +if (i != cpu) +update_rq_clock (rq_i); + } + +/* +* Try and select tasks for each sibling in decending sched_class +* order. +*/ +for_each_class (class) { +again: +for_each_cpu_wrap (i, smt_mask, cpu) { +struct rq *rq_i =cpu_rq (i); +struct task_struct *p; + +if (rq_i->core_pick) +continue ; + +/* +* If this sibling doesn't yet have a suitable task to +* run; ask for the most elegible task, given the +* highest priority task already selected for this +* core. +*/ + p =pick_task (rq_i, class, max); +if (!p) { +/* +* If there weren't no cookies; we don't need to +* bother with the other siblings. +* If the rest of the core is not running a tagged +* task, i.e. need_sync == 0, and the current CPU +* which called into the schedule() loop does not +* have any tasks for this class, skip selecting for +* other siblings since there's no point. We don't skip +* for RT/DL because that could make CFS force-idle RT. +*/ +if (i == cpu && !need_sync && class == &fair_sched_class) + goto next_class; + +continue ; + } + +/* +* Optimize the 'normal' case where there aren't any +* cookies and we don't need to sync up. +*/ +if (i == cpu && !need_sync && !p->core_cookie) { + next = p; + goto done; + } + + rq_i->core_pick = p; + +/* +* If this new candidate is of higher priority than the +* previous; and they're incompatible; we need to wipe +* the slate and start over. pick_task makes sure that +* p's priority is more than max if it doesn't match +* max's cookie. +* +* NOTE: this is a linear max-filter and is thus bounded +* in execution time. +*/ +if (!max || !cookie_match (max, p)) { +struct task_struct *old_max = max; + + rq->core->core_cookie = p->core_cookie; + max = p; + +if (old_max) { +for_each_cpu (j, smt_mask) { +if (j == i) +continue ; + +cpu_rq (j)->core_pick =NULL ; + } + goto again; + }else { +/* +* Once we select a task for a cpu, we +* should not be doing an unconstrained +* pick because it might starve a task +* on a forced idle cpu. +*/ + need_sync = true; + } + + } + } +next_class:; + } + + rq->core->core_pick_seq = rq->core->core_task_seq; + next = rq->core_pick; + rq->core_sched_seq = rq->core->core_pick_seq; + +/* Something should have been selected for current CPU */ +WARN_ON_ONCE (!next); + +/* +* Reschedule siblings +* +* NOTE: L1TF -- at this point we're no longer running the old task and +* sending an IPI (below) ensures the sibling will no longer be running +* their task. This ensures there is no inter-sibling overlap between +* non-matching user state. +*/ +for_each_cpu (i, smt_mask) { +struct rq *rq_i =cpu_rq (i); + +/* +* An online sibling might have gone offline before a task +* could be picked for it, or it might be offline but later +* happen to come online, but its too late and nothing was +* picked for it. That's Ok - it will pick tasks for itself, +* so ignore it. +*/ +if (!rq_i->core_pick) +continue ; + +if (is_task_rq_idle (rq_i->core_pick) && rq_i->nr_running) + rq_i->core_forceidle = true; + +if (i == cpu) { + rq_i->core_pick =NULL ; +continue ; + } + +/* Did we break L1TF mitigation requirements? */ +WARN_ON_ONCE (!cookie_match (next, rq_i->core_pick)); + +if (rq_i->curr == rq_i->core_pick) { + rq_i->core_pick =NULL ; +continue ; + } + +resched_curr (rq_i); + } + +done: +set_next_task (rq, next); +return next; +}static inline void sched_core_cpu_starting (unsigned int cpu) {
@@ -5151,6 +5439,12 @@ static inline void sched_core_cpu_startistatic inline void sched_core_cpu_starting (unsigned int cpu) {} +static struct task_struct * +pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +{ +return __pick_next_task (rq, prev, rf); +} +#endif /* CONFIG_SCHED_CORE */ /*
@@ -8048,7 +8342,12 @@ void __init sched_init(void)#ifdef CONFIG_SCHED_CORE rq->core =NULL ; + rq->core_pick =NULL ; rq->core_enabled =0 ; + rq->core_tree =RB_ROOT ; + rq->core_forceidle = false; + + rq->core_cookie =0UL ;#endif }
kernel/sched/sched.h +5 -1
@@ -1065,11 +1065,16 @@ struct rq {#ifdef CONFIG_SCHED_CORE /* per rq */ struct rq *core; +struct task_struct *core_pick;unsigned int core_enabled; +unsigned int core_sched_seq;struct rb_root core_tree; +unsigned char core_forceidle;/* shared state */ unsigned int core_task_seq; +unsigned int core_pick_seq; +unsigned long core_cookie;#endif };
@@ -1977,7 +1982,6 @@ static inline void put_prev_task(structstatic inline void set_next_task (struct rq *rq,struct task_struct *next) { -WARN_ON_ONCE (rq->curr != next); next->sched_class->set_next_task (rq, next, false); }
Peter Zijlstra
maintainerPATCH 13/19 sched/fair: Snapshot the min_vruntime of CPUs on force idle
In reply to Peter ZijlstraFrom: "Joel Fernandes (Google)" <[email protected]>
During force-idle, we end up doing cross-cpu comparison of vruntimes during pick_next_task. If we simply compare (vruntime-min_vruntime) across CPUs, and if the CPUs only have 1 task each, we will always end up comparing 0 with 0 and pick just one of the tasks all the time. This starves the task that was not picked. To fix this, take a snapshot of the min_vruntime when entering force idle and use it for comparison. This min_vruntime snapshot will only be used for cross-CPU vruntime comparison, and nothing else.
A note about the min_vruntime snapshot and force idling:
During selection:
When we're not fi, we need to update snapshot.
when we're fi and we were not fi, we must update snapshot. When we're fi and we were already fi, we must not update snapshot.
Which gives:
fib fi update 0 0 1 0 1 1 1 0 1 1 1 0
Where:
fi: force-idled now
fib: force-idled before
So the min_vruntime snapshot needs to be updated when: !(fib && fi).
Also, the cfs_prio_less() function needs to be aware of whether the core is in force idle or not, since it will be use this information to know whether to advance a cfs_rq's min_vruntime_fi in the hierarchy. So pass this information along via pick_task() -> prio_less().
--- kernel/sched/core.c | 59 +++++++++++++++++++++++----------------- kernel/sched/fair.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 8 +++++ 3 files changed, 117 insertions(+), 25 deletions(-)
kernel/sched/core.c +34 -25
@@ -101,7 +101,7 @@ static inline int __task_prio(struct tas*/ /* real prio, less is less */ -static inline bool prio_less (struct task_struct *a,struct task_struct *b) +static inline bool prio_less (struct task_struct *a,struct task_struct *b,bool in_fi) {int pa =__task_prio (a), pb =__task_prio (b);
@@ -115,19 +115,8 @@ static inline bool prio_less(struct taskif (pa ==-1 )/* dl_prio() doesn't work because of stop_class above */ return !dl_time_before (a->dl.deadline, b->dl.deadline); -if (pa ==MAX_RT_PRIO +MAX_NICE ) {/* fair */ -u64 vruntime = b->se.vruntime; - -/* -* Normalize the vruntime if tasks are in different cpus. -*/ -if (task_cpu (a) !=task_cpu (b)) { - vruntime -=task_cfs_rq (b)->min_vruntime; - vruntime +=task_cfs_rq (a)->min_vruntime; - } - -return !((s64)(a->se.vruntime - vruntime) <=0 ); - } +if (pa ==MAX_RT_PRIO +MAX_NICE )/* fair */ +return cfs_prio_less (a, b, in_fi);return false; }
@@ -141,7 +130,7 @@ static inline bool __sched_core_less(strreturn false;/* flip prio, so high prio is leftmost */ -if (prio_less (b, a)) +if (prio_less (b, a,task_rq (a)->core->core_forceidle))return true;return false;
@@ -5145,7 +5134,7 @@ static inline bool cookie_match(struct t* - Else returns idle_task. */ static struct task_struct * -pick_task (struct rq *rq,const struct sched_class *class,struct task_struct *max) +pick_task (struct rq *rq,const struct sched_class *class,struct task_struct *max,bool in_fi) {struct task_struct *class_pick, *cookie_pick;unsigned long cookie = rq->core->core_cookie;
@@ -5160,7 +5149,7 @@ pick_task(struct rq *rq, const struct sc* higher priority than max. */ if (max && class_pick->core_cookie && -prio_less (class_pick, max)) +prio_less (class_pick, max, in_fi))return idle_sched_class.pick_task (rq);return class_pick;
@@ -5179,19 +5168,22 @@ pick_task(struct rq *rq, const struct sc* the core (so far) and it must be selected, otherwise we must go with * the cookie pick in order to satisfy the constraint. */ -if (prio_less (cookie_pick, class_pick) && - (!max ||prio_less (max, class_pick))) +if (prio_less (cookie_pick, class_pick, in_fi) && + (!max ||prio_less (max, class_pick, in_fi)))return class_pick;return cookie_pick; } +extern void task_vruntime_update (struct rq *rq,struct task_struct *p,bool in_fi); +static struct task_struct *pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) {struct task_struct *next, *max =NULL ;const struct sched_class *class;const struct cpumask *smt_mask; +bool fi_before = false;bool need_sync;int i, j, cpu;
@@ -5273,9 +5265,14 @@ pick_next_task(struct rq *rq, struct tasif (!next->core_cookie) { rq->core_pick =NULL ; +/* +* For robustness, update the min_vruntime_fi for +* unconstrained picks as well. +*/ +WARN_ON_ONCE (fi_before); +task_vruntime_update (rq, next, false); goto done; } - need_sync = true; }for_each_cpu (i, smt_mask) {
@@ -5306,11 +5303,16 @@ pick_next_task(struct rq *rq, struct tas* highest priority task already selected for this * core. */ - p =pick_task (rq_i, class, max); + p =pick_task (rq_i, class, max, fi_before);if (!p)continue ; rq_i->core_pick = p; +if (rq_i->idle == p && rq_i->nr_running) { + rq->core->core_forceidle = true; +if (!fi_before) + rq->core->core_forceidle_seq++; + }/* * If this new candidate is of higher priority than the
@@ -5329,6 +5331,7 @@ pick_next_task(struct rq *rq, struct tas max = p;if (old_max) { + rq->core->core_forceidle = false;for_each_cpu (j, smt_mask) {if (j == i)continue ;
@@ -5369,10 +5372,16 @@ pick_next_task(struct rq *rq, struct tasif (!rq_i->core_pick)continue ; -if (is_task_rq_idle (rq_i->core_pick) && rq_i->nr_running && - !rq_i->core->core_forceidle) { - rq_i->core->core_forceidle = true; - } +/* +* Update for new !FI->FI transitions, or if continuing to be in !FI: +* fi_before fi update? +* 0 0 1 +* 0 1 1 +* 1 0 1 +* 1 1 0 +*/ +if (!(fi_before && rq->core->core_forceidle)) +task_vruntime_update (rq_i, rq_i->core_pick, rq->core->core_forceidle);if (i == cpu) { rq_i->core_pick =NULL ;
kernel/sched/fair.c +75 -0
81 lines not on this page · raw message
kernel/sched/sched.h +8 -0
@@ -517,6 +517,11 @@ struct cfs_rq {u64 exec_clock;u64 min_vruntime; +#ifdef CONFIG_SCHED_CORE +unsigned int forceidle_seq; +u64 min_vruntime_fi; +#endif +#ifndef CONFIG_64BIT u64 min_vruntime_copy;#endif
@@ -1075,6 +1080,7 @@ struct rq {unsigned int core_pick_seq;unsigned long core_cookie;unsigned char core_forceidle; +unsigned int core_forceidle_seq;#endif };
@@ -1130,6 +1136,8 @@ static inline raw_spinlock_t *rq_lockp(s return &rq->__lock; } +bool cfs_prio_less (struct task_struct *a,struct task_struct *b,bool fi); +#else /* !CONFIG_SCHED_CORE */ static inline bool sched_core_enabled (struct rq *rq)
Peter Zijlstra
maintainerPATCH 11/19 sched/fair: Fix forced idle sibling starvation corner case
In reply to Peter ZijlstraFrom: Vineeth Pillai <[email protected]>
If there is only one long running local task and the sibling is forced idle, it might not get a chance to run until a schedule event happens on any cpu in the core.
So we check for this condition during a tick to see if a sibling is starved and then give it a chance to schedule.
--- kernel/sched/core.c | 15 ++++++++------- kernel/sched/fair.c | 40 ++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 2 +- 3 files changed, 49 insertions(+), 8 deletions(-)
kernel/sched/core.c +8 -7
@@ -5206,16 +5206,15 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)/* reset state */ rq->core->core_cookie =0UL ; +if (rq->core->core_forceidle) { + need_sync = true; + rq->core->core_forceidle = false; + }for_each_cpu (i, smt_mask) {struct rq *rq_i =cpu_rq (i); rq_i->core_pick =NULL ; -if (rq_i->core_forceidle) { - need_sync = true; - rq_i->core_forceidle = false; - } -if (i != cpu)update_rq_clock (rq_i); }
@@ -5335,8 +5334,10 @@ next_class:;if (!rq_i->core_pick)continue ; -if (is_task_rq_idle (rq_i->core_pick) && rq_i->nr_running) - rq_i->core_forceidle = true; +if (is_task_rq_idle (rq_i->core_pick) && rq_i->nr_running && + !rq_i->core->core_forceidle) { + rq_i->core->core_forceidle = true; + }if (i == cpu) { rq_i->core_pick =NULL ;
kernel/sched/fair.c +40 -0
@@ -10692,6 +10692,44 @@ static void rq_offline_fair(struct rq *rq)#endif /* CONFIG_SMP */ +#ifdef CONFIG_SCHED_CORE +static inline bool +__entity_slice_used (struct sched_entity *se,int min_nr_tasks) +{ +u64 slice =sched_slice (cfs_rq_of (se), se); +u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; + +return (rtime * min_nr_tasks > slice); +} + +#define MIN_NR_TASKS_DURING_FORCEIDLE 2 +static inline void task_tick_core (struct rq *rq,struct task_struct *curr) +{ +if (!sched_core_enabled (rq)) +return ; + +/* +* If runqueue has only one task which used up its slice and +* if the sibling is forced idle, then trigger schedule to +* give forced idle task a chance. +* +* sched_slice() considers only this active rq and it gets the +* whole slice. But during force idle, we have siblings acting +* like a single runqueue and hence we need to consider runnable +* tasks on this cpu and the forced idle cpu. Ideally, we should +* go through the forced idle rq, but that would be a perf hit. +* We can assume that the forced idle cpu has atleast +* MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check +* if we need to give up the cpu. +*/ +if (rq->core->core_forceidle && rq->cfs.nr_running ==1 && +__entity_slice_used (&curr->se,MIN_NR_TASKS_DURING_FORCEIDLE )) +resched_curr (rq); +} +#else +static inline void task_tick_core (struct rq *rq,struct task_struct *curr) {} +#endif +/* * scheduler tick hitting a task of our scheduling class. *
@@ -10715,6 +10753,8 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)update_misfit_status (curr, rq);update_overutilized_status (task_rq (curr)); + +task_tick_core (rq, curr); }/*
kernel/sched/sched.h +1 -1
@@ -1069,12 +1069,12 @@ struct rq {unsigned int core_enabled;unsigned int core_sched_seq;struct rb_root core_tree; -unsigned char core_forceidle;/* shared state */ unsigned int core_task_seq;unsigned int core_pick_seq;unsigned long core_cookie; +unsigned char core_forceidle;#endif };
Signature · 1 line
2.29.2.299.gdc1121823c-goog
Peter Zijlstra
maintainerPATCH 19/19 kselftest: Add test for core sched prctl interface
In reply to Peter ZijlstraFrom: Chris Hyser <[email protected]>
Provides a selftest and examples of using the interface.
--- tools/testing/selftests/sched/.gitignore | 1 tools/testing/selftests/sched/Makefile | 14 + tools/testing/selftests/sched/config | 1 tools/testing/selftests/sched/cs_prctl_test.c | 338 ++++++++++++++++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 tools/testing/selftests/sched/.gitignore create mode 100644 tools/testing/selftests/sched/Makefile create mode 100644 tools/testing/selftests/sched/config create mode 100644 tools/testing/selftests/sched/cs_prctl_test.c
tools/testing/selftests/sched/.gitignore new file +1 -0
@@ -0,0 +1 @@ +cs_prctl_test
tools/testing/selftests/sched/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0+ + +ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),) +CLANG_FLAGS += -no-integrated-as +endif + +CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/ -Wl,-rpath=./ \ + $(CLANG_FLAGS) +LDLIBS += -lpthread + +TEST_GEN_FILES := cs_prctl_test +TEST_PROGS := cs_prctl_test + +include ../lib.mk
tools/testing/selftests/sched/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_SCHED_DEBUG=y
tools/testing/selftests/sched/cs_prctl_test.c new file +338 -0
338 lines not on this page · raw message
Quoted 11 lines · Peter Zijlstra
Hai,
This is an agressive fold of all the core-scheduling work so far. I've stripped a whole bunch of tags along the way (hopefully not too many, please yell if you feel I made a mistake), including tested-by. Please retest.
Changes since the last partial post is dropping all the cgroup stuff and
PR_SCHED_CORE_CLEARas well as thatexec()behaviour in order to later resolve the cgroup issue.
Hi Peter,
Is there a reason that PR_SCHED_CORE_CLEAR got removed? It's handy to be able to clear
cookies.
Thanks,
Don
Quoted 33 lines
Since we're really rather late for the coming merge window, my plan was to merge the lot right after the merge window.
Again, please test.
These patches should shortly be available in my queue.git.
--- b/kernel/sched/core_sched.c | 229 ++++++ b/tools/testing/selftests/sched/.gitignore | 1 b/tools/testing/selftests/sched/Makefile | 14 b/tools/testing/selftests/sched/config | 1 b/tools/testing/selftests/sched/cs_prctl_test.c | 338 +++++++++ include/linux/sched.h | 19 include/uapi/linux/prctl.h | 8 kernel/Kconfig.preempt | 6 kernel/fork.c | 4 kernel/sched/Makefile | 1 kernel/sched/core.c | 858 ++++++++++++++++++++++-- kernel/sched/cpuacct.c | 12 kernel/sched/deadline.c | 38 - kernel/sched/debug.c | 4 kernel/sched/fair.c | 276 +++++-- kernel/sched/idle.c | 13 kernel/sched/pelt.h | 2 kernel/sched/rt.c | 31 kernel/sched/sched.h | 393 ++++++++-- kernel/sched/stop_task.c | 14 kernel/sched/topology.c | 4 kernel/sys.c | 5 tools/include/uapi/linux/prctl.h | 8 23 files changed, 2057 insertions(+), 222 deletions(-)
Peter Zijlstra
maintainerQuoted 8 lines · Don Hiatt
Quoted 7 lines · Peter Zijlstra
Hai,
This is an agressive fold of all the core-scheduling work so far. I've stripped a whole bunch of tags along the way (hopefully not too many, please yell if you feel I made a mistake), including tested-by. Please retest.
Quoted 8 lines
Quoted 4 lines
Changes since the last partial post is dropping all the cgroup stuff and
PR_SCHED_CORE_CLEARas well as thatexec()behaviour in order to later resolve the cgroup issue.Hi Peter,
Is there a reason that
PR_SCHED_CORE_CLEARgot removed? It's handy to be able to clear cookies.
I agree, but if we're forced to do cgroup with cookie inherit, when CLEAR allows a trivial escape.
Until that whole cgroup thing is sorted, it is easiest to not have CLEAR, lest we have to change/augment CLEAR semantics in unfortunate ways, which is always harder once an API is out there.
Also, see the entire previous discussion here:
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
In reply to Peter ZijlstraHi Peter,
kernel/sched/core.c int sysctl_sched_rt_runtime = 950000; · line 191
Quoted 10 lines
kernel/sched/core.c +3
-1@@ -186,12 +186,37 @@ int sysctl_sched_rt_runtime = 950000;void raw_spin_rq_lock_nested (struct rq *rq,int subclass) { -raw_spin_lock_nested (rq_lockp (rq), subclass); +raw_spinlock_t *lock; + +if (sched_core_disabled ()) {
Nothing to stop sched_core from being enabled right here? Leading to us potentially taking the wrong lock.
Quoted 12 lines
kernel/sched/core.c +11
-0+raw_spin_lock_nested (&rq->__lock, subclass); +return ; + } + +for (;;) { + lock =rq_lockp (rq); +raw_spin_lock_nested (lock, subclass); +if (likely (lock ==rq_lockp (rq))) +return ; +raw_spin_unlock (lock); + } }
Peter Zijlstra
maintainerRe: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
Quoted 6 lines · Josh Don
Hi Peter,
Quoted 4 lines
kernel/sched/core.c +0
-0@@ -186,12 +186,37 @@ int sysctl_sched_rt_runtime = 950000;
Quoted 22 lines
kernel/sched/core.c int sysctl_sched_rt_runtime = 950000; · line 191
Quoted 6 lines
kernel/sched/core.c +3
-1void raw_spin_rq_lock_nested (struct rq *rq,int subclass) { -raw_spin_lock_nested (rq_lockp (rq), subclass); +raw_spinlock_t *lock; + +if (sched_core_disabled ()) {Nothing to stop
sched_corefrom being enabled right here? Leading to us potentially taking the wrong lock.Quoted 12 lines
kernel/sched/core.c +11
-0+raw_spin_lock_nested (&rq->__lock, subclass); +return ; + } + +for (;;) { + lock =rq_lockp (rq); +raw_spin_lock_nested (lock, subclass); +if (likely (lock ==rq_lockp (rq))) +return ; +raw_spin_unlock (lock); + } }
Very good; something like the below seems to be the best I can make of it..
kernel/sched/core.c +20 -4
@@ -290,6 +290,10 @@ static void sched_core_assert_empty(void)static void __sched_core_enable (void ) {static_branch_enable (&__sched_core_enabled); +/* +* Ensure raw_spin_rq_*lock*() have completed before flipping. +*/ +synchronize_sched ();__sched_core_flip (true);sched_core_assert_empty (); }
@@ -449,16 +453,22 @@ void raw_spin_rq_lock_nested(struct rq *rq, int subclass) {raw_spinlock_t *lock; +preempt_disable ();if (sched_core_disabled ()) {raw_spin_lock_nested (&rq->__lock, subclass); +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; }for (;;) { lock =__rq_lockp (rq);raw_spin_lock_nested (lock, subclass); -if (likely (lock ==__rq_lockp (rq))) +if (likely (lock ==__rq_lockp (rq))) { +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; + }raw_spin_unlock (lock); } }
@@ -468,14 +478,20 @@ bool raw_spin_rq_trylock(struct rq *rq)raw_spinlock_t *lock;bool ret; -if (sched_core_disabled ()) -return raw_spin_trylock (&rq->__lock); +preempt_disable (); +if (sched_core_disabled ()) { + ret =raw_spin_trylock (&rq->__lock); +preempt_enable (); +return ret; + }for (;;) { lock =__rq_lockp (rq); ret =raw_spin_trylock (lock); -if (!ret || (likely (lock ==__rq_lockp (rq)))) +if (!ret || (likely (lock ==__rq_lockp (rq)))) { +preempt_enable ();return ret; + }raw_spin_unlock (lock); } }
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
kernel/sched/core.c static void sched_core_assert_empty(void) · line 296
Quoted 12 lines
kernel/sched/core.c +4
-0@@ -290,6 +290,10 @@ static void sched_core_assert_empty(void)static void __sched_core_enable (void ) {static_branch_enable (&__sched_core_enabled); +/* +* Ensure raw_spin_rq_*lock*() have completed before flipping. +*/ +synchronize_sched ();
synchronize_rcu()
Quoted 4 lines
kernel/sched/core.c +0
-0__sched_core_flip (true);sched_core_assert_empty (); }
kernel/sched/core.c void raw_spin_rq_lock_nested(struct rq *rq, int subclass) · line 462
Quoted 10 lines
kernel/sched/core.c +3
-0@@ -449,16 +453,22 @@ void raw_spin_rq_lock_nested(struct rq *rq, int subclass) {raw_spinlock_t *lock; +preempt_disable ();if (sched_core_disabled ()) {raw_spin_lock_nested (&rq->__lock, subclass); +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; }
This approach looks good to me. I'm guessing you went this route instead of doing the re-check after locking in order to optimize the disabled case?
Recommend a comment that the preempt_disable() here pairs with the synchronize_rcu() in __sched_core_enable().
Quoted 13 lines
kernel/sched/core.c +4
-1for (;;) { lock =__rq_lockp (rq);raw_spin_lock_nested (lock, subclass); -if (likely (lock ==__rq_lockp (rq))) +if (likely (lock ==__rq_lockp (rq))) { +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; + }raw_spin_unlock (lock); } }
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
Quoted 23 lines · Josh Don
kernel/sched/core.c static void sched_core_assert_empty(void) · line 296
Quoted 13 lines
kernel/sched/core.c +4
-0@@ -290,6 +290,10 @@ static void sched_core_assert_empty(void)static void __sched_core_enable (void ) {static_branch_enable (&__sched_core_enabled); +/* +* Ensure raw_spin_rq_*lock*() have completed before flipping. +*/ +synchronize_sched ();
synchronize_rcu()Quoted 7 lines
kernel/sched/core.c +0
-0__sched_core_flip (true);sched_core_assert_empty (); }@@ -449,16 +453,22 @@ void raw_spin_rq_lock_nested(struct rq *rq, int subclass) {raw_spinlock_t *lock;
Quoted 12 lines
kernel/sched/core.c void raw_spin_rq_lock_nested(struct rq *rq, int subclass) · line 462
Quoted 7 lines
kernel/sched/core.c +3
-0+preempt_disable ();if (sched_core_disabled ()) {raw_spin_lock_nested (&rq->__lock, subclass); +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; }This approach looks good to me. I'm guessing you went this route instead of doing the re-check after locking in order to optimize the disabled case?
Hi Josh and Peter,
I've been running into soft lookups and hard lockups when running a script that just cycles setting the cookie of a group of processes over and over again.
Unfortunately the only way I can reproduce this is by setting the cookies on qemu. I've tried sysbench, stress-ng but those seem to work just fine.
I'm running Peter's branch and even tried the suggested changes here but still see the same behavior. I enabled panic on hard lockup and here below is a snippet of the log.
Is there anything you'd like me to try or have any debugging you'd like me to do? I'd certainly like to get to the bottom of this.
Thanks and have a great day,
Don
[ 1164.129706] NMI backtrace for cpu 10 [ 1164.129707] CPU: 10 PID: 6994 Comm: CPU 2/KVM Kdump: loaded Tainted: G I L 5.12.0-rc8+ #4 [ 1164.129707] Hardware name: Dell Inc. PowerEdge R640/0W23H8, BIOS 2.9.4 11/06/2020 [ 1164.129708] RIP: 0010:native_queued_spin_lock_slowpath+0x69/0x1e0 [ 1164.129708] Code: 0f 92 c0 0f b6 c0 c1 e0 08 89 c2 8b 07 30 e4 09 d0 a9 00 01 ff ff 75 20 85 c0 75 0c b8 01 00 00 00 66 89 07 5d c3 f3 90 8b 07 <84> c0 75 f8 b8 01 00 00 00 66 89 07 eb ec f6 c4 01 75 04 c6 47 01 [ 1164.129709] RSP: 0018:ffffb35d591e0e20 EFLAGS: 00000002 [ 1164.129710] RAX: 0000000000200101 RBX: ffff9ecb0096c180 RCX: 0000000000000001 [ 1164.129710] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff9ecb0096c180 [ 1164.129711] RBP: ffffb35d591e0e20 R08: 0000000000000000 R09: 000000010002e2a6 [ 1164.129711] R10: 0000000000000000 R11: ffffb35d591e0ff8 R12: ffff9ecb0096c180 [ 1164.129712] R13: 000000000000000a R14: 000000000000000a R15: ffff9e6e8dc88000 [ 1164.129712] FS: 00007f91bd79f700(0000) GS:ffff9ecb00940000(0000) knlGS:0000000000000000 [ 1164.129713] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1164.129713] CR2: 000000000255d05c CR3: 000000022eb8c005 CR4: 00000000007726e0 [ 1164.129714] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 1164.129715] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 1164.129715] PKRU: 55555554 [ 1164.129715] Call Trace: [ 1164.129716] <IRQ> [ 1164.129716] _raw_spin_lock+0x1f/0x30 [ 1164.129717] raw_spin_rq_lock_nested.part.93+0x2b/0x60 [ 1164.129717] raw_spin_rq_lock_nested.constprop.126+0x1a/0x20 [ 1164.129718] scheduler_tick+0x4c/0x240 [ 1164.129718] ? tick_sched_handle.isra.17+0x60/0x60 [ 1164.129719] update_process_times+0xab/0xc0 [ 1164.129719] tick_sched_handle.isra.17+0x21/0x60 [ 1164.129720] tick_sched_timer+0x6b/0x80 [ 1164.129720] __hrtimer_run_queues+0x10d/0x230 [ 1164.129721] hrtimer_interrupt+0xe7/0x230 [ 1164.129721] __sysvec_apic_timer_interrupt+0x62/0x100 [ 1164.129721] sysvec_apic_timer_interrupt+0x51/0xa0 [ 1164.129722] </IRQ> [ 1164.129722] asm_sysvec_apic_timer_interrupt+0x12/0x20 [ 1164.129722] RIP: 0010:smp_call_function_single+0xf6/0x130 [ 1164.129723] Code: 00 75 4d 48 83 c4 48 41 5a 5d 49 8d 62 f8 c3 48 89 75 c0 48 8d 75 b0 48 89 55 c8 e8 84 fe ff ff 8b 55 b8 83 e2 01 74 0a f3 90 <8b> 55 b8 83 e2 01 75 f6 eb c0 8b 05 4a d8 b0 01 85 c0 0f 85 67 ff [ 1164.129724] RSP: 0018:ffffb35d5e20fb20 EFLAGS: 00000202 [ 1164.129725] RAX: 0000000000000000 RBX: ffff9e6e8df12600 RCX: ffffb35d5f2ffcc0 [ 1164.129725] RDX: 0000000000000001 RSI: ffffb35d5e20fb20 RDI: ffffb35d5e20fb20 [ 1164.129725] RBP: ffffb35d5e20fb70 R08: 0000000000001000 R09: 0000000000001907 [ 1164.129726] R10: ffffb35d5e20fb98 R11: 00000000003d0900 R12: 000000000000000a [ 1164.129726] R13: 000000000000000c R14: 0000000000000000 R15: 0000000000000000 [ 1164.129727] ? vmclear_error+0x150/0x390 [kvm_intel] [ 1164.129727] vmx_vcpu_load_vmcs+0x1b2/0x330 [kvm_intel] [ 1164.129728] ? update_load_avg+0x1b8/0x640 [ 1164.129728] ? vmx_vcpu_load_vmcs+0x1b2/0x330 [kvm_intel] [ 1164.129728] ? load_fixmap_gdt+0x23/0x30 [ 1164.129729] vmx_vcpu_load_vmcs+0x309/0x330 [kvm_intel] [ 1164.129729] kvm_arch_vcpu_load+0x41/0x260 [kvm] [ 1164.129729] ? __switch_to_xtra+0xe3/0x4e0 [ 1164.129730] kvm_io_bus_write+0xbc/0xf0 [kvm] [ 1164.129730] ? kvm_io_bus_write+0xbc/0xf0 [kvm] [ 1164.129730] finish_task_switch+0x184/0x290 [ 1164.129731] __schedule+0x30a/0x12f0 [ 1164.129731] schedule+0x40/0xb0 [ 1164.129732] kvm_vcpu_block+0x57/0x4b0 [kvm] [ 1164.129732] kvm_arch_vcpu_ioctl_run+0x3fe/0x1b60 [kvm] [ 1164.129732] kvm_vcpu_kick+0x59c/0xa10 [kvm] [ 1164.129733] ? kvm_vcpu_kick+0x59c/0xa10 [kvm] [ 1164.129733] __x64_sys_ioctl+0x96/0xd0 [ 1164.129733] do_syscall_64+0x37/0x80 [ 1164.129734] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 1164.129734] RIP: 0033:0x7fb1c8a4c317 [ 1164.129735] Code: b3 66 90 48 8b 05 71 4b 2d 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 41 4b 2d 00 f7 d8 64 89 01 48 [ 1164.129735] RSP: 002b:00007f91bd79e7e8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 [ 1164.129736] RAX: ffffffffffffffda RBX: 000000000000ae80 RCX: 00007fb1c8a4c317 [ 1164.129737] RDX: 0000000000000000 RSI: 000000000000ae80 RDI: 000000000000001a [ 1164.129737] RBP: 0000000000000000 R08: 000055f3e6d027c0 R09: 00007f91a80040b8 [ 1164.129738] R10: 00007f91a80050a0 R11: 0000000000000246 R12: 00007fb1c4023010 [ 1164.129739] R13: 000055f3e569d880 R14: 00007fb1cb8e7000 R15: 00007ffd86806f70
Quoted 16 lines
Recommend a comment that the
preempt_disable()here pairs with thesynchronize_rcu()in__sched_core_enable().Quoted 13 lines
kernel/sched/core.c +4
-1for (;;) { lock =__rq_lockp (rq);raw_spin_lock_nested (lock, subclass); -if (likely (lock ==__rq_lockp (rq))) +if (likely (lock ==__rq_lockp (rq))) { +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; + }raw_spin_unlock (lock); } }
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
In reply to Josh DonQuoted 35 lines · Josh Don
kernel/sched/core.c static void sched_core_assert_empty(void) · line 296
Quoted 13 lines
kernel/sched/core.c +4
-0@@ -290,6 +290,10 @@ static void sched_core_assert_empty(void)static void __sched_core_enable (void ) {static_branch_enable (&__sched_core_enabled); +/* +* Ensure raw_spin_rq_*lock*() have completed before flipping. +*/ +synchronize_sched ();
synchronize_rcu()Quoted 4 lines
kernel/sched/core.c +0
-0__sched_core_flip (true);sched_core_assert_empty (); }kernel/sched/core.c void raw_spin_rq_lock_nested(struct rq *rq, int subclass) · line 462
Quoted 10 lines
kernel/sched/core.c +3
-0@@ -449,16 +453,22 @@ void raw_spin_rq_lock_nested(struct rq *rq, int subclass) {raw_spinlock_t *lock; +preempt_disable ();if (sched_core_disabled ()) {raw_spin_lock_nested (&rq->__lock, subclass); +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; }This approach looks good to me. I'm guessing you went this route instead of doing the re-check after locking in order to optimize the disabled case?
Quoted 16 lines
Recommend a comment that the
preempt_disable()here pairs with thesynchronize_rcu()in__sched_core_enable().Quoted 13 lines
kernel/sched/core.c +4
-1for (;;) { lock =__rq_lockp (rq);raw_spin_lock_nested (lock, subclass); -if (likely (lock ==__rq_lockp (rq))) +if (likely (lock ==__rq_lockp (rq))) { +/* preempt *MUST* still be disabled here */ +preempt_enable_no_resched ();return ; + }raw_spin_unlock (lock); } }
Also, did you mean to have a preempt_enable_no_resched() rather than prempt_enable() in raw_spin_rq_trylock?
I went over the rq_lockp stuff again after Don's reported lockup. Most uses are safe due to already holding an rq lock. However, double_rq_unlock() is prone to race:
double_rq_unlock(rq1, rq2):
/* Initial state: core sched enabled, and rq1 and rq2 are smt siblings. So, double_rq_lock(rq1, rq2) only took a single rq lock */ raw_spin_rq_unlock(rq1);
/* now not holding any rq lock */
/* sched core disabled. Now __rq_lockp(rq1) != __rq_lockp(rq2), so we falsely unlock rq2 */
if (__rq_lockp(rq1) != __rq_lockp(rq2))
raw_spin_rq_unlock(rq2);
else
__release(rq2->lock);Instead we can cache __rq_lockp(rq1) and __rq_lockp(rq2) before releasing the lock, in order to prevent this. FWIW I think it is likely that Don is seeing a different issue.
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
In reply to Don HiattQuoted 8 lines · Don Hiatt
Hi Josh and Peter,
I've been running into soft lookups and hard lockups when running a script that just cycles setting the cookie of a group of processes over and over again.
Unfortunately the only way I can reproduce this is by setting the cookies on qemu. I've tried sysbench, stress-ng but those seem to work just fine.
Quoted 6 lines
I'm running Peter's branch and even tried the suggested changes here but still see the same behavior. I enabled panic on hard lockup and here below is a snippet of the log.
Is there anything you'd like me to try or have any debugging you'd like me to do? I'd certainly like to get to the bottom of this.
Hi Don,
I tried to repro using qemu, but did not generate a lockup. Could you provide more details on what your script is doing (or better yet, share the script directly)? I would have expected you to potentially hit a lockup if you were cycling sched_core being enabled and disabled, but it sounds like you are just recreating the cookie for a process group over and over?
Best,
Josh
Re: [PATCH 04/19] sched: Prepare for Core-wide rq->lock
Quoted 17 lines · Josh Don
Quoted 8 lines · Don Hiatt
Hi Josh and Peter,
I've been running into soft lookups and hard lockups when running a script that just cycles setting the cookie of a group of processes over and over again.
Unfortunately the only way I can reproduce this is by setting the cookies on qemu. I've tried sysbench, stress-ng but those seem to work just fine.
Quoted 6 lines
I'm running Peter's branch and even tried the suggested changes here but still see the same behavior. I enabled panic on hard lockup and here below is a snippet of the log.
Is there anything you'd like me to try or have any debugging you'd like me to do? I'd certainly like to get to the bottom of this.
Quoted 9 lines
Hi Don,
I tried to repro using qemu, but did not generate a lockup. Could you provide more details on what your script is doing (or better yet, share the script directly)? I would have expected you to potentially hit a lockup if you were cycling
sched_corebeing enabled and disabled, but it sounds like you are just recreating the cookie for a process group over and over?
I saw something similar on a bare metal hardware. Also tried the suggested patch here and no luck. Panic stack attached with softlockup_all_cpu_backtrace=1.
(sorry, my system has 192 cpus and somehow putting 184 cpus offline causes system hang without any message...)
My script created the core cookie for two different process groups. The one is for sysbench cpu, the other is for sysbench mysql, mysqld(cookie=0) is
also on the same machine. The number of tasks in each category is the same as the number of CPUs on the system. And cookie is created just during task startup.
Please let me know if the script is needed, I'll push it to github with some cleanup.
Thanks,
-Aubrey
��[ B E G I N ] 2 0 2 1 / 4 / 2 8 1 : 0 0 : 1 1