| /* |
| * kernel/sched/core.c |
| * |
| * Kernel scheduler and related syscalls |
| * |
| * Copyright (C) 1991-2002 Linus Torvalds |
| * |
| * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and |
| * make semaphores SMP safe |
| * 1998-11-19 Implemented schedule_timeout() and related stuff |
| * by Andrea Arcangeli |
| * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar: |
| * hybrid priority-list and round-robin design with |
| * an array-switch method of distributing timeslices |
| * and per-CPU runqueues. Cleanups and useful suggestions |
| * by Davide Libenzi, preemptible kernel bits by Robert Love. |
| * 2003-09-03 Interactivity tuning by Con Kolivas. |
| * 2004-04-02 Scheduler domains code by Nick Piggin |
| * 2007-04-15 Work begun on replacing all interactivity tuning with a |
| * fair scheduling design by Con Kolivas. |
| * 2007-05-05 Load balancing (smp-nice) and other improvements |
| * by Peter Williams |
| * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith |
| * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri |
| * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins, |
| * Thomas Gleixner, Mike Kravetz |
| */ |
| |
| #include <linux/mm.h> |
| #include <linux/module.h> |
| #include <linux/nmi.h> |
| #include <linux/init.h> |
| #include <linux/uaccess.h> |
| #include <linux/highmem.h> |
| #include <asm/mmu_context.h> |
| #include <linux/interrupt.h> |
| #include <linux/capability.h> |
| #include <linux/completion.h> |
| #include <linux/kernel_stat.h> |
| #include <linux/debug_locks.h> |
| #include <linux/perf_event.h> |
| #include <linux/security.h> |
| #include <linux/notifier.h> |
| #include <linux/profile.h> |
| #include <linux/freezer.h> |
| #include <linux/vmalloc.h> |
| #include <linux/blkdev.h> |
| #include <linux/delay.h> |
| #include <linux/pid_namespace.h> |
| #include <linux/smp.h> |
| #include <linux/threads.h> |
| #include <linux/timer.h> |
| #include <linux/rcupdate.h> |
| #include <linux/cpu.h> |
| #include <linux/cpuset.h> |
| #include <linux/percpu.h> |
| #include <linux/proc_fs.h> |
| #include <linux/seq_file.h> |
| #include <linux/sysctl.h> |
| #include <linux/syscalls.h> |
| #include <linux/times.h> |
| #include <linux/tsacct_kern.h> |
| #include <linux/kprobes.h> |
| #include <linux/delayacct.h> |
| #include <linux/unistd.h> |
| #include <linux/pagemap.h> |
| #include <linux/hrtimer.h> |
| #include <linux/tick.h> |
| #include <linux/debugfs.h> |
| #include <linux/ctype.h> |
| #include <linux/ftrace.h> |
| #include <linux/slab.h> |
| #include <linux/init_task.h> |
| #include <linux/binfmts.h> |
| #include <linux/context_tracking.h> |
| |
| #include <asm/switch_to.h> |
| #include <asm/tlb.h> |
| #include <asm/irq_regs.h> |
| #include <asm/mutex.h> |
| #ifdef CONFIG_PARAVIRT |
| #include <asm/paravirt.h> |
| #endif |
| |
| #include "sched.h" |
| #include "../workqueue_internal.h" |
| #include "../smpboot.h" |
| |
| #define CREATE_TRACE_POINTS |
| #include <trace/events/sched.h> |
| |
| void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period) |
| { |
| unsigned long delta; |
| ktime_t soft, hard, now; |
| |
| for (;;) { |
| if (hrtimer_active(period_timer)) |
| break; |
| |
| now = hrtimer_cb_get_time(period_timer); |
| hrtimer_forward(period_timer, now, period); |
| |
| soft = hrtimer_get_softexpires(period_timer); |
| hard = hrtimer_get_expires(period_timer); |
| delta = ktime_to_ns(ktime_sub(hard, soft)); |
| __hrtimer_start_range_ns(period_timer, soft, delta, |
| HRTIMER_MODE_ABS_PINNED, 0); |
| } |
| } |
| |
| DEFINE_MUTEX(sched_domains_mutex); |
| DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
| |
| static void update_rq_clock_task(struct rq *rq, s64 delta); |
| |
| void update_rq_clock(struct rq *rq) |
| { |
| s64 delta; |
| |
| if (rq->skip_clock_update > 0) |
| return; |
| |
| delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
| rq->clock += delta; |
| update_rq_clock_task(rq, delta); |
| } |
| |
| /* |
| * Debugging: various feature bits |
| */ |
| |
| #define SCHED_FEAT(name, enabled) \ |
| (1UL << __SCHED_FEAT_##name) * enabled | |
| |
| const_debug unsigned int sysctl_sched_features = |
| #include "features.h" |
| 0; |
| |
| #undef SCHED_FEAT |
| |
| #ifdef CONFIG_SCHED_DEBUG |
| #define SCHED_FEAT(name, enabled) \ |
| #name , |
| |
| static const char * const sched_feat_names[] = { |
| #include "features.h" |
| }; |
| |
| #undef SCHED_FEAT |
| |
| static int sched_feat_show(struct seq_file *m, void *v) |
| { |
| int i; |
| |
| for (i = 0; i < __SCHED_FEAT_NR; i++) { |
| if (!(sysctl_sched_features & (1UL << i))) |
| seq_puts(m, "NO_"); |
| seq_printf(m, "%s ", sched_feat_names[i]); |
| } |
| seq_puts(m, "\n"); |
| |
| return 0; |
| } |
| |
| #ifdef HAVE_JUMP_LABEL |
| |
| #define jump_label_key__true STATIC_KEY_INIT_TRUE |
| #define jump_label_key__false STATIC_KEY_INIT_FALSE |
| |
| #define SCHED_FEAT(name, enabled) \ |
| jump_label_key__##enabled , |
| |
| struct static_key sched_feat_keys[__SCHED_FEAT_NR] = { |
| #include "features.h" |
| }; |
| |
| #undef SCHED_FEAT |
| |
| static void sched_feat_disable(int i) |
| { |
| if (static_key_enabled(&sched_feat_keys[i])) |
| static_key_slow_dec(&sched_feat_keys[i]); |
| } |
| |
| static void sched_feat_enable(int i) |
| { |
| if (!static_key_enabled(&sched_feat_keys[i])) |
| static_key_slow_inc(&sched_feat_keys[i]); |
| } |
| #else |
| static void sched_feat_disable(int i) { }; |
| static void sched_feat_enable(int i) { }; |
| #endif /* HAVE_JUMP_LABEL */ |
| |
| static int sched_feat_set(char *cmp) |
| { |
| int i; |
| int neg = 0; |
| |
| if (strncmp(cmp, "NO_", 3) == 0) { |
| neg = 1; |
| cmp += 3; |
| } |
| |
| for (i = 0; i < __SCHED_FEAT_NR; i++) { |
| if (strcmp(cmp, sched_feat_names[i]) == 0) { |
| if (neg) { |
| sysctl_sched_features &= ~(1UL << i); |
| sched_feat_disable(i); |
| } else { |
| sysctl_sched_features |= (1UL << i); |
| sched_feat_enable(i); |
| } |
| break; |
| } |
| } |
| |
| return i; |
| } |
| |
| static ssize_t |
| sched_feat_write(struct file *filp, const char __user *ubuf, |
| size_t cnt, loff_t *ppos) |
| { |
| char buf[64]; |
| char *cmp; |
| int i; |
| |
| if (cnt > 63) |
| cnt = 63; |
| |
| if (copy_from_user(&buf, ubuf, cnt)) |
| return -EFAULT; |
| |
| buf[cnt] = 0; |
| cmp = strstrip(buf); |
| |
| i = sched_feat_set(cmp); |
| if (i == __SCHED_FEAT_NR) |
| return -EINVAL; |
| |
| *ppos += cnt; |
| |
| return cnt; |
| } |
| |
| static int sched_feat_open(struct inode *inode, struct file *filp) |
| { |
| return single_open(filp, sched_feat_show, NULL); |
| } |
| |
| static const struct file_operations sched_feat_fops = { |
| .open = sched_feat_open, |
| .write = sched_feat_write, |
| .read = seq_read, |
| .llseek = seq_lseek, |
| .release = single_release, |
| }; |
| |
| static __init int sched_init_debug(void) |
| { |
| debugfs_create_file("sched_features", 0644, NULL, NULL, |
| &sched_feat_fops); |
| |
| return 0; |
| } |
| late_initcall(sched_init_debug); |
| #endif /* CONFIG_SCHED_DEBUG */ |
| |
| /* |
| * Number of tasks to iterate in a single balance run. |
| * Limited because this is done with IRQs disabled. |
| */ |
| const_debug unsigned int sysctl_sched_nr_migrate = 32; |
| |
| /* |
| * period over which we average the RT time consumption, measured |
| * in ms. |
| * |
| * default: 1s |
| */ |
| const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC; |
| |
| /* |
| * period over which we measure -rt task cpu usage in us. |
| * default: 1s |
| */ |
| unsigned int sysctl_sched_rt_period = 1000000; |
| |
| __read_mostly int scheduler_running; |
| |
| /* |
| * part of the period that we allow rt tasks to run in us. |
| * default: 0.95s |
| */ |
| int sysctl_sched_rt_runtime = 950000; |
| |
| |
| |
| /* |
| * __task_rq_lock - lock the rq @p resides on. |
| */ |
| static inline struct rq *__task_rq_lock(struct task_struct *p) |
| __acquires(rq->lock) |
| { |
| struct rq *rq; |
| |
| lockdep_assert_held(&p->pi_lock); |
| |
| for (;;) { |
| rq = task_rq(p); |
| raw_spin_lock(&rq->lock); |
| if (likely(rq == task_rq(p))) |
| return rq; |
| raw_spin_unlock(&rq->lock); |
| } |
| } |
| |
| /* |
| * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. |
| */ |
| static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) |
| __acquires(p->pi_lock) |
| __acquires(rq->lock) |
| { |
| struct rq *rq; |
| |
| for (;;) { |
| raw_spin_lock_irqsave(&p->pi_lock, *flags); |
| rq = task_rq(p); |
| raw_spin_lock(&rq->lock); |
| if (likely(rq == task_rq(p))) |
| return rq; |
| raw_spin_unlock(&rq->lock); |
| raw_spin_unlock_irqrestore(&p->pi_lock, *flags); |
| } |
| } |
| |
| static void __task_rq_unlock(struct rq *rq) |
| __releases(rq->lock) |
| { |
| raw_spin_unlock(&rq->lock); |
| } |
| |
| static inline void |
| task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags) |
| __releases(rq->lock) |
| __releases(p->pi_lock) |
| { |
| raw_spin_unlock(&rq->lock); |
| raw_spin_unlock_irqrestore(&p->pi_lock, *flags); |
| } |
| |
| /* |
| * this_rq_lock - lock this runqueue and disable interrupts. |
| */ |
| static struct rq *this_rq_lock(void) |
| __acquires(rq->lock) |
| { |
| struct rq *rq; |
| |
| local_irq_disable(); |
| rq = this_rq(); |
| raw_spin_lock(&rq->lock); |
| |
| return rq; |
| } |
| |
| #ifdef CONFIG_SCHED_HRTICK |
| /* |
| * Use HR-timers to deliver accurate preemption points. |
| */ |
| |
| static void hrtick_clear(struct rq *rq) |
| { |
| if (hrtimer_active(&rq->hrtick_timer)) |
| hrtimer_cancel(&rq->hrtick_timer); |
| } |
| |
| /* |
| * High-resolution timer tick. |
| * Runs from hardirq context with interrupts disabled. |
| */ |
| static enum hrtimer_restart hrtick(struct hrtimer *timer) |
| { |
| struct rq *rq = container_of(timer, struct rq, hrtick_timer); |
| |
| WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); |
| |
| raw_spin_lock(&rq->lock); |
| update_rq_clock(rq); |
| rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
| raw_spin_unlock(&rq->lock); |
| |
| return HRTIMER_NORESTART; |
| } |
| |
| #ifdef CONFIG_SMP |
| |
| static int __hrtick_restart(struct rq *rq) |
| { |
| struct hrtimer *timer = &rq->hrtick_timer; |
| ktime_t time = hrtimer_get_softexpires(timer); |
| |
| return __hrtimer_start_range_ns(timer, time, 0, HRTIMER_MODE_ABS_PINNED, 0); |
| } |
| |
| /* |
| * called from hardirq (IPI) context |
| */ |
| static void __hrtick_start(void *arg) |
| { |
| struct rq *rq = arg; |
| |
| raw_spin_lock(&rq->lock); |
| __hrtick_restart(rq); |
| rq->hrtick_csd_pending = 0; |
| raw_spin_unlock(&rq->lock); |
| } |
| |
| /* |
| * Called to set the hrtick timer state. |
| * |
| * called with rq->lock held and irqs disabled |
| */ |
| void hrtick_start(struct rq *rq, u64 delay) |
| { |
| struct hrtimer *timer = &rq->hrtick_timer; |
| ktime_t time = ktime_add_ns(timer->base->get_time(), delay); |
| |
| hrtimer_set_expires(timer, time); |
| |
| if (rq == this_rq()) { |
| __hrtick_restart(rq); |
| } else if (!rq->hrtick_csd_pending) { |
| __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0); |
| rq->hrtick_csd_pending = 1; |
| } |
| } |
| |
| static int |
| hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| { |
| int cpu = (int)(long)hcpu; |
| |
| switch (action) { |
| case CPU_UP_CANCELED: |
| case CPU_UP_CANCELED_FROZEN: |
| case CPU_DOWN_PREPARE: |
| case CPU_DOWN_PREPARE_FROZEN: |
| case CPU_DEAD: |
| case CPU_DEAD_FROZEN: |
| hrtick_clear(cpu_rq(cpu)); |
| return NOTIFY_OK; |
| } |
| |
| return NOTIFY_DONE; |
| } |
| |
| static __init void init_hrtick(void) |
| { |
| hotcpu_notifier(hotplug_hrtick, 0); |
| } |
| #else |
| /* |
| * Called to set the hrtick timer state. |
| * |
| * called with rq->lock held and irqs disabled |
| */ |
| void hrtick_start(struct rq *rq, u64 delay) |
| { |
| __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0, |
| HRTIMER_MODE_REL_PINNED, 0); |
| } |
| |
| static inline void init_hrtick(void) |
| { |
| } |
| #endif /* CONFIG_SMP */ |
| |
| static void init_rq_hrtick(struct rq *rq) |
| { |
| #ifdef CONFIG_SMP |
| rq->hrtick_csd_pending = 0; |
| |
| rq->hrtick_csd.flags = 0; |
| rq->hrtick_csd.func = __hrtick_start; |
| rq->hrtick_csd.info = rq; |
| #endif |
| |
| hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| rq->hrtick_timer.function = hrtick; |
| } |
| #else /* CONFIG_SCHED_HRTICK */ |
| static inline void hrtick_clear(struct rq *rq) |
| { |
| } |
| |
| static inline void init_rq_hrtick(struct rq *rq) |
| { |
| } |
| |
| static inline void init_hrtick(void) |
| { |
| } |
| #endif /* CONFIG_SCHED_HRTICK */ |
| |
| /* |
| * resched_task - mark a task 'to be rescheduled now'. |
| * |
| * On UP this means the setting of the need_resched flag, on SMP it |
| * might also involve a cross-CPU call to trigger the scheduler on |
| * the target CPU. |
| */ |
| void resched_task(struct task_struct *p) |
| { |
| int cpu; |
| |
| lockdep_assert_held(&task_rq(p)->lock); |
| |
| if (test_tsk_need_resched(p)) |
| return; |
| |
| set_tsk_need_resched(p); |
| |
| cpu = task_cpu(p); |
| if (cpu == smp_processor_id()) { |
| set_preempt_need_resched(); |
| return; |
| } |
| |
| /* NEED_RESCHED must be visible before we test polling */ |
| smp_mb(); |
| if (!tsk_is_polling(p)) |
| smp_send_reschedule(cpu); |
| } |
| |
| void resched_cpu(int cpu) |
| { |
| struct rq *rq = cpu_rq(cpu); |
| unsigned long flags; |
| |
| if (!raw_spin_trylock_irqsave(&rq->lock, flags)) |
| return; |
| resched_task(cpu_curr(cpu)); |
| raw_spin_unlock_irqrestore(&rq->lock, flags); |
| } |
| |
| #ifdef CONFIG_SMP |
| #ifdef CONFIG_NO_HZ_COMMON |
| /* |
| * In the semi idle case, use the nearest busy cpu for migrating timers |
| * from an idle cpu. This is good for power-savings. |
| * |
| * We don't do similar optimization for completely idle system, as |
| * selecting an idle cpu will add more delays to the timers than intended |
| * (as that cpu's timer base may not be uptodate wrt jiffies etc). |
| */ |
| int get_nohz_timer_target(void) |
| { |
| int cpu = smp_processor_id(); |
| int i; |
| struct sched_domain *sd; |
| |
| rcu_read_lock(); |
| for_each_domain(cpu, sd) { |
| for_each_cpu(i, sched_domain_span(sd)) { |
| if (!idle_cpu(i)) { |
| cpu = i; |
| goto unlock; |
| } |
| } |
| } |
| unlock: |
| rcu_read_unlock(); |
| return cpu; |
| } |
| /* |
| * When add_timer_on() enqueues a timer into the timer wheel of an |
| * idle CPU then this timer might expire before the next timer event |
| * which is scheduled to wake up that CPU. In case of a completely |
| * idle system the next event might even be infinite time into the |
| * future. wake_up_idle_cpu() ensures that the CPU is woken up and |
| * leaves the inner idle loop so the newly added timer is taken into |
| * account when the CPU goes back to idle and evaluates the timer |
| * wheel for the next timer event. |
| */ |
| static void wake_up_idle_cpu(int cpu) |
| { |
| struct rq *rq = cpu_rq(cpu); |
| |
| if (cpu == smp_processor_id()) |
| return; |
| |
| /* |
| * This is safe, as this function is called with the timer |
| * wheel base lock of (cpu) held. When the CPU is on the way |
| * to idle and has not yet set rq->curr to idle then it will |
| * be serialized on the timer wheel base lock and take the new |
| * timer into account automatically. |
| */ |
| if (rq->curr != rq->idle) |
| return; |
| |
| /* |
| * We can set TIF_RESCHED on the idle task of the other CPU |
| * lockless. The worst case is that the other CPU runs the |
| * idle task through an additional NOOP schedule() |
| */ |
| set_tsk_need_resched(rq->idle); |
| |
| /* NEED_RESCHED must be visible before we test polling */ |
| smp_mb(); |
| if (!tsk_is_polling(rq->idle)) |
| smp_send_reschedule(cpu); |
| } |
| |
| static bool wake_up_full_nohz_cpu(int cpu) |
| { |
| if (tick_nohz_full_cpu(cpu)) { |
| if (cpu != smp_processor_id() || |
| tick_nohz_tick_stopped()) |
| smp_send_reschedule(cpu); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| void wake_up_nohz_cpu(int cpu) |
| { |
| if (!wake_up_full_nohz_cpu(cpu)) |
| wake_up_idle_cpu(cpu); |
| } |
| |
| static inline bool got_nohz_idle_kick(void) |
| { |
| int cpu = smp_processor_id(); |
| |
| if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu))) |
| return false; |
| |
| if (idle_cpu(cpu) && !need_resched()) |
| return true; |
| |
| /* |
| * We can't run Idle Load Balance on this CPU for this time so we |
| * cancel it and clear NOHZ_BALANCE_KICK |
| */ |
| clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)); |
| return false; |
| } |
| |
| #else /* CONFIG_NO_HZ_COMMON */ |
| |
| static inline bool got_nohz_idle_kick(void) |
| { |
| return false; |
| } |
| |
| #endif /* CONFIG_NO_HZ_COMMON */ |
| |
| #ifdef CONFIG_NO_HZ_FULL |
| bool sched_can_stop_tick(void) |
| { |
| struct rq *rq; |
| |
| rq = this_rq(); |
| |
| /* Make sure rq->nr_running update is visible after the IPI */ |
| smp_rmb(); |
| |
| /* More than one running task need preemption */ |
| if (rq->nr_running > 1) |
| return false; |
| |
| return true; |
| } |
| #endif /* CONFIG_NO_HZ_FULL */ |
| |
| void sched_avg_update(struct rq *rq) |
| { |
| s64 period = sched_avg_period(); |
| |
| while ((s64)(rq_clock(rq) - rq->age_stamp) > period) { |
| /* |
| * Inline assembly required to prevent the compiler |
| * optimising this loop into a divmod call. |
| * See __iter_div_u64_rem() for another example of this. |
| */ |
| asm("" : "+rm" (rq->age_stamp)); |
| rq->age_stamp += period; |
| rq->rt_avg /= 2; |
| } |
| } |
| |
| #endif /* CONFIG_SMP */ |
| |
| #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
| (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) |
| /* |
| * Iterate task_group tree rooted at *from, calling @down when first entering a |
| * node and @up when leaving it for the final time. |
| * |
| * Caller must hold rcu_lock or sufficient equivalent. |
| */ |
| int walk_tg_tree_from(struct task_group *from, |
| tg_visitor down, tg_visitor up, void *data) |
| { |
| struct task_group *parent, *child; |
| int ret; |
| |
| parent = from; |
| |
| down: |
| ret = (*down)(parent, data); |
| if (ret) |
| goto out; |
| list_for_each_entry_rcu(child, &parent->children, siblings) { |
| parent = child; |
| goto down; |
| |
| up: |
| continue; |
| } |
| ret = (*up)(parent, data); |
| if (ret || parent == from) |
| goto out; |
| |
| child = parent; |
| parent = parent->parent; |
| if (parent) |
| goto up; |
| out: |
| return ret; |
| } |
| |
| int tg_nop(struct task_group *tg, void *data) |
| { |
| return 0; |
| } |
| #endif |
| |
| static void set_load_weight(struct task_struct *p) |
| { |
| int prio = p->static_prio - MAX_RT_PRIO; |
| struct load_weight *load = &p->se.load; |
| |
| /* |
| * SCHED_IDLE tasks get minimal weight: |
| */ |
| if (p->policy == SCHED_IDLE) { |
| load->weight = scale_load(WEIGHT_IDLEPRIO); |
| load->inv_weight = WMULT_IDLEPRIO; |
| return; |
| } |
| |
| load->weight = scale_load(prio_to_weight[prio]); |
| load->inv_weight = prio_to_wmult[prio]; |
| } |
| |
| static void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
| { |
| update_rq_clock(rq); |
| sched_info_queued(rq, p); |
| p->sched_class->enqueue_task(rq, p, flags); |
| } |
| |
| static void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
| { |
| update_rq_clock(rq); |
| sched_info_dequeued(rq, p); |
| p->sched_class->dequeue_task(rq, p, flags); |
| } |
| |
| void activate_task(struct rq *rq, struct task_struct *p, int flags) |
| { |
| if (task_contributes_to_load(p)) |
| rq->nr_uninterruptible--; |
| |
| enqueue_task(rq, p, flags); |
| } |
| |
| void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
| { |
| if (task_contributes_to_load(p)) |
| rq->nr_uninterruptible++; |
| |
| dequeue_task(rq, p, flags); |
| } |
| |
| static void update_rq_clock_task(struct rq *rq, s64 delta) |
| { |
| /* |
| * In theory, the compile should just see 0 here, and optimize out the call |
| * to sched_rt_avg_update. But I don't trust it... |
| */ |
| #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) |
| s64 steal = 0, irq_delta = 0; |
| #endif |
| #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; |
| |
| /* |
| * Since irq_time is only updated on {soft,}irq_exit, we might run into |
| * this case when a previous update_rq_clock() happened inside a |
| * {soft,}irq region. |
| * |
| * When this happens, we stop ->clock_task and only update the |
| * prev_irq_time stamp to account for the part that fit, so that a next |
| * update will consume the rest. This ensures ->clock_task is |
| * monotonic. |
| * |
| * It does however cause some slight miss-attribution of {soft,}irq |
| * time, a more accurate solution would be to update the irq_time using |
| * the current rq->clock timestamp, except that would require using |
| * atomic ops. |
| */ |
| if (irq_delta > delta) |
| irq_delta = delta; |
| |
| rq->prev_irq_time += irq_delta; |
| delta -= irq_delta; |
| #endif |
| #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING |
| if (static_key_false((¶virt_steal_rq_enabled))) { |
| u64 st; |
| |
| steal = paravirt_steal_clock(cpu_of(rq)); |
| steal -= rq->prev_steal_time_rq; |
| |
| if (unlikely(steal > delta)) |
| steal = delta; |
| |
| st = steal_ticks(steal); |
| steal = st * TICK_NSEC; |
| |
| rq->prev_steal_time_rq += steal; |
| |
| delta -= steal; |
| } |
| #endif |
| |
| rq->clock_task += delta; |
| |
| #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) |
| if ((irq_delta + steal) && sched_feat(NONTASK_POWER)) |
| sched_rt_avg_update(rq, irq_delta + steal); |
| #endif |
| } |
| |
| void sched_set_stop_task(int cpu, struct task_struct *stop) |
| { |
| struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
| struct task_struct *old_stop = cpu_rq(cpu)->stop; |
| |
| if (stop) { |
| /* |
| * Make it appear like a SCHED_FIFO task, its something |
| * userspace knows about and won't get confused about. |
| * |
| * Also, it will make PI more or less work without too |
| * much confusion -- but then, stop work should not |
| * rely on PI working anyway. |
| */ |
| sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); |
| |
| stop->sched_class = &stop_sched_class; |
| } |
| |
| cpu_rq(cpu)->stop = stop; |
| |
| if (old_stop) { |
| /* |
| * Reset it back to a normal scheduling class so that |
| * it can die in pieces. |
| */ |
| old_stop->sched_class = &rt_sched_class; |
| } |
| } |
| |
| /* |
| * __normal_prio - return the priority that is based on the static prio |
| */ |
| static inline int __normal_prio(struct task_struct *p) |
| { |
| return p->static_prio; |
| } |
| |
| /* |
| * Calculate the expected normal priority: i.e. priority |
| * without taking RT-inheritance into account. Might be |
| * boosted by interactivity modifiers. Changes upon fork, |
| * setprio syscalls, and whenever the interactivity |
| * estimator recalculates. |
| */ |
| static inline int normal_prio(struct task_struct *p) |
| { |
| int prio; |
| |
| if (task_has_rt_policy(p)) |
| prio = MAX_RT_PRIO-1 - p->rt_priority; |
| else |
| prio = __normal_prio(p); |
| return prio; |
| } |
| |
| /* |
| * Calculate the current priority, i.e. the priority |
| * taken into account by the scheduler. This value might |
| * be boosted by RT tasks, or might be boosted by |
| * interactivity modifiers. Will be RT if the task got |
| * RT-boosted. If not then it returns p->normal_prio. |
| */ |
| static int effective_prio(struct task_struct *p) |
| { |
| p->normal_prio = normal_prio(p); |
| /* |
| * If we are RT tasks or we were boosted to RT priority, |
| * keep the priority unchanged. Otherwise, update priority |
| * to the normal priority: |
| */ |
| if (!rt_prio(p->prio)) |
| return p->normal_prio; |
| return p->prio; |
| } |
| |
| /** |
| * task_curr - is this task currently executing on a CPU? |
| * @p: the task in question. |
| * |
| * Return: 1 if the task is currently executing. 0 otherwise. |
| */ |
| inline int task_curr(const struct task_struct *p) |
| { |
| return cpu_curr(task_cpu(p)) == p; |
| } |
| |
| static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
| const struct sched_class *prev_class, |
| int oldprio) |
| { |
| if (prev_class != p->sched_class) { |
| if (prev_class->switched_from) |
| prev_class->switched_from(rq, p); |
| p->sched_class->switched_to(rq, p); |
| } else if (oldprio != p->prio) |
| p->sched_class->prio_changed(rq, p, oldprio); |
| } |
| |
| void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
| { |
| const struct sched_class *class; |
| |
| if (p->sched_class == rq->curr->sched_class) { |
| rq->curr->sched_class->check_preempt_curr(rq, p, flags); |
| } else { |
| for_each_class(class) { |
| if (class == rq->curr->sched_class) |
| break; |
| if (class == p->sched_class) { |
| resched_task(rq->curr); |
| break; |
| } |
| } |
| } |
| |
| /* |
| * A queue event has occurred, and we're going to schedule. In |
| * this case, we can save a useless back to back clock update. |
| */ |
| if (rq->curr->on_rq && test_tsk_need_resched(rq->curr)) |
| rq->skip_clock_update = 1; |
| } |
| |
| #ifdef CONFIG_SMP |
| void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
| { |
| #ifdef CONFIG_SCHED_DEBUG |
| /* |
| * We should never call set_task_cpu() on a blocked task, |
| * ttwu() will sort out the placement. |
| */ |
| WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && |
| !(task_preempt_count(p) & PREEMPT_ACTIVE)); |
| |
| #ifdef CONFIG_LOCKDEP |
| /* |
| * The caller should hold either p->pi_lock or rq->lock, when changing |
| * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. |
| * |
| * sched_move_task() holds both and thus holding either pins the cgroup, |
| * see task_group(). |
| * |
| * Furthermore, all task_rq users should acquire both locks, see |
| * task_rq_lock(). |
| */ |
| WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
| lockdep_is_held(&task_rq(p)->lock))); |
| #endif |
| #endif |
| |
| trace_sched_migrate_task(p, new_cpu); |
| |
| if (task_cpu(p) != new_cpu) { |
| if (p->sched_class->migrate_task_rq) |
| p->sched_class->migrate_task_rq(p, new_cpu); |
| p->se.nr_migrations++; |
| perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0); |
| } |
| |
| __set_task_cpu(p, new_cpu); |
| } |
| |
| static void __migrate_swap_task(struct task_struct *p, int cpu) |
| { |
| if (p->on_rq) { |
| struct rq *src_rq, *dst_rq; |
| |
| src_rq = task_rq(p); |
| dst_rq = cpu_rq(cpu); |
| |
| deactivate_task(src_rq, p, 0); |
| set_task_cpu(p, cpu); |
| activate_task(dst_rq, p, 0); |
| check_preempt_curr(dst_rq, p, 0); |
| } else { |
| /* |
| * Task isn't running anymore; make it appear like we migrated |
| * it before it went to sleep. This means on wakeup we make the |
| * previous cpu our targer instead of where it really is. |
| */ |
| p->wake_cpu = cpu; |
| } |
| } |
| |
| struct migration_swap_arg { |
| struct task_struct *src_task, *dst_task; |
| int src_cpu, dst_cpu; |
| }; |
| |
| static int migrate_swap_stop(void *data) |
| { |
| struct migration_swap_arg *arg = data; |
| struct rq *src_rq, *dst_rq; |
| int ret = -EAGAIN; |
| |
| src_rq = cpu_rq(arg->src_cpu); |
| dst_rq = cpu_rq(arg->dst_cpu); |
| |
| double_raw_lock(&arg->src_task->pi_lock, |
| &arg->dst_task->pi_lock); |
| double_rq_lock(src_rq, dst_rq); |
| if (task_cpu(arg->dst_task) != arg->dst_cpu) |
| goto unlock; |
| |
| if (task_cpu(arg->src_task) != arg->src_cpu) |
| goto unlock; |
| |
| if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task))) |
| goto unlock; |
| |
| if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task))) |
| goto unlock; |
| |
| __migrate_swap_task(arg->src_task, arg->dst_cpu); |
| __migrate_swap_task(arg->dst_task, arg->src_cpu); |
| |
| ret = 0; |
| |
| unlock: |
| double_rq_unlock(src_rq, dst_rq); |
| raw_spin_unlock(&arg->dst_task->pi_lock); |
| raw_spin_unlock(&arg->src_task->pi_lock); |
| |
| return ret; |
| } |
| |
| /* |
| * Cross migrate two tasks |
| */ |
| int migrate_swap(struct task_struct *cur, struct task_struct *p) |
| { |
| struct migration_swap_arg arg; |
| int ret = -EINVAL; |
| |
| arg = (struct migration_swap_arg){ |
| .src_task = cur, |
| .src_cpu = task_cpu(cur), |
| .dst_task = p, |
| .dst_cpu = task_cpu(p), |
| }; |
| |
| if (arg.src_cpu == arg.dst_cpu) |
| goto out; |
| |
| /* |
| * These three tests are all lockless; this is OK since all of them |
| * will be re-checked with proper locks held further down the line. |
| */ |
| if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
| goto out; |
| |
| if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task))) |
| goto out; |
| |
| if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task))) |
| goto out; |
| |
| ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
| |
| out: |
| return ret; |
| } |
| |
| struct migration_arg { |
| struct task_struct *task; |
| int dest_cpu; |
| }; |
| |
| static int migration_cpu_stop(void *data); |
| |
| /* |
| * wait_task_inactive - wait for a thread to unschedule. |
| * |
| * If @match_state is nonzero, it's the @p->state value just checked and |
| * not expected to change. If it changes, i.e. @p might have woken up, |
| * then return zero. When we succeed in waiting for @p to be off its CPU, |
| * we return a positive number (its total switch count). If a second call |
| * a short while later returns the same number, the caller can be sure that |
| * @p has remained unscheduled the whole time. |
| * |
| * The caller must ensure that the task *will* unschedule sometime soon, |
| * else this function might spin for a *long* time. This function can't |
| * be called with interrupts off, or it may introduce deadlock with |
| * smp_call_function() if an IPI is sent by the same process we are |
| * waiting to become inactive. |
| */ |
| unsigned long wait_task_inactive(struct task_struct *p, long match_state) |
| { |
| unsigned long flags; |
| int running, on_rq; |
| unsigned long ncsw; |
| struct rq *rq; |
| |
| for (;;) { |
| /* |
| * We do the initial early heuristics without holding |
| * any task-queue locks at all. We'll only try to get |
| * the runqueue lock when things look like they will |
| * work out! |
| */ |
| rq = task_rq(p); |
| |
| /* |
| * If the task is actively running on another CPU |
| * still, just relax and busy-wait without holding |
| * any locks. |
| * |
| * NOTE! Since we don't hold any locks, it's not |
| * even sure that "rq" stays as the right runqueue! |
| * But we don't care, since "task_running()" will |
| * return false if the runqueue has changed and p |
| * is actually now running somewhere else! |
| */ |
| while (task_running(rq, p)) { |
| if (match_state && unlikely(p->state != match_state)) |
| return 0; |
| cpu_relax(); |
| } |
| |
| /* |
| * Ok, time to look more closely! We need the rq |
| * lock now, to be *sure*. If we're wrong, we'll |
| * just go back and repeat. |
| */ |
| rq = task_rq_lock(p, &flags); |
| trace_sched_wait_task(p); |
| running = task_running(rq, p); |
| on_rq = p->on_rq; |
| ncsw = 0; |
| if (!match_state || p->state == match_state) |
| ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
| task_rq_unlock(rq, p, &flags); |
| |
| /* |
| * If it changed from the expected state, bail out now. |
| */ |
| if (unlikely(!ncsw)) |
| break; |
| |
| /* |
| * Was it really running after all now that we |
| * checked with the proper locks actually held? |
| * |
| * Oops. Go back and try again.. |
| */ |
| if (unlikely(running)) { |
| cpu_relax(); |
| continue; |
| } |
| |
| /* |
| * It's not enough that it's not actively running, |
| * it must be off the runqueue _entirely_, and not |
| * preempted! |
| * |
| * So if it was still runnable (but just not actively |
| * running right now), it's preempted, and we should |
| * yield - it could be a while. |
| */ |
| if (unlikely(on_rq)) { |
| ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ); |
| |
| set_current_state(TASK_UNINTERRUPTIBLE); |
| schedule_hrtimeout(&to, HRTIMER_MODE_REL); |
| continue; |
| } |
| |
| /* |
| * Ahh, all good. It wasn't running, and it wasn't |
| * runnable, which means that it will never become |
| * running in the future either. We're all done! |
| */ |
| break; |
| } |
| |
| return ncsw; |
| } |
| |
| /*** |
| * kick_process - kick a running thread to enter/exit the kernel |
| * @p: the to-be-kicked thread |
| * |
| * Cause a process which is running on another CPU to enter |
| * kernel-mode, without any delay. (to get signals handled.) |
| * |
| * NOTE: this function doesn't have to take the runqueue lock, |
| * because all it wants to ensure is that the remote task enters |
| * the kernel. If the IPI races and the task has been migrated |
| * to another CPU then no harm is done and the purpose has been |
| * achieved as well. |
| */ |
| void kick_process(struct task_struct *p) |
| { |
| int cpu; |
| |
| preempt_disable(); |
| cpu = task_cpu(p); |
| if ((cpu != smp_processor_id()) && task_curr(p)) |
| smp_send_reschedule(cpu); |
| preempt_enable(); |
| } |
| EXPORT_SYMBOL_GPL(kick_process); |
| #endif /* CONFIG_SMP */ |
| |
| #ifdef CONFIG_SMP |
| /* |
| * ->cpus_allowed is protected by both rq->lock and p->pi_lock |
| */ |
| static int select_fallback_rq(int cpu, struct task_struct *p) |
| { |
| int nid = cpu_to_node(cpu); |
| const struct cpumask *nodemask = NULL; |
| enum { cpuset, possible, fail } state = cpuset; |
| int dest_cpu; |
| |
| /* |
| * If the node that the cpu is on has been offlined, cpu_to_node() |
| * will return -1. There is no cpu on the node, and we should |
| * select the cpu on the other node. |
| */ |
| if (nid != -1) { |
| nodemask = cpumask_of_node(nid); |
| |
| /* Look for allowed, online CPU in same node. */ |
| for_each_cpu(dest_cpu, nodemask) { |
| if (!cpu_online(dest_cpu)) |
| continue; |
| if (!cpu_active(dest_cpu)) |
| continue; |
| if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p))) |
| return dest_cpu; |
| } |
| } |
| |
| for (;;) { |
| /* Any allowed, online CPU? */ |
| for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) { |
| if (!cpu_online(dest_cpu)) |
| continue; |
| if (!cpu_active(dest_cpu)) |
| continue; |
| goto out; |
| } |
| |
| switch (state) { |
| case cpuset: |
| /* No more Mr. Nice Guy. */ |
| cpuset_cpus_allowed_fallback(p); |
| state = possible; |
| break; |
| |
| case possible: |
| do_set_cpus_allowed(p, cpu_possible_mask); |
| state = fail; |
| break; |
| |
| case fail: |
| BUG(); |
| break; |
| } |
| } |
| |
| out: |
| if (state != cpuset) { |
| /* |
| * Don't tell them about moving exiting tasks or |
| * kernel threads (both mm NULL), since they never |
| * leave kernel. |
| */ |
| if (p->mm && printk_ratelimit()) { |
| printk_sched("process %d (%s) no longer affine to cpu%d\n", |
| task_pid_nr(p), p->comm, cpu); |
| } |
| } |
| |
| return dest_cpu; |
| } |
| |
| /* |
| * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable. |
| */ |
| static inline |
| int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) |
| { |
| cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags); |
| |
| /* |
| * In order not to call set_task_cpu() on a blocking task we need |
| * to rely on ttwu() to place the task on a valid ->cpus_allowed |
| * cpu. |
| * |
| * Since this is common to all placement strategies, this lives here. |
| * |
| * [ this allows ->select_task() to simply return task_cpu(p) and |
| * not worry about this generic constraint ] |
| */ |
| if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) || |
| !cpu_online(cpu))) |
| cpu = select_fallback_rq(task_cpu(p), p); |
| |
| return cpu; |
| } |
| |
| static void update_avg(u64 *avg, u64 sample) |
| { |
| s64 diff = sample - *avg; |
| *avg += diff >> 3; |
| } |
| #endif |
| |
| static void |
| ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
| { |
| #ifdef CONFIG_SCHEDSTATS |
| struct rq *rq = this_rq(); |
| |
| #ifdef CONFIG_SMP |
| int this_cpu = smp_processor_id(); |
| |
| if (cpu == this_cpu) { |
| schedstat_inc(rq, ttwu_local); |
| schedstat_inc(p, se.statistics.nr_wakeups_local); |
| } else { |
| struct sched_domain *sd; |
| |
| schedstat_inc(p, se.statistics.nr_wakeups_remote); |
| rcu_read_lock(); |
| for_each_domain(this_cpu, sd) { |
| if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
| schedstat_inc(sd, ttwu_wake_remote); |
| break; |
| } |
| } |
| rcu_read_unlock(); |
| } |
| |
| if (wake_flags & WF_MIGRATED) |
| schedstat_inc(p, se.statistics.nr_wakeups_migrate); |
| |
| #endif /* CONFIG_SMP */ |
| |
| schedstat_inc(rq, ttwu_count); |
| schedstat_inc(p, se.statistics.nr_wakeups); |
| |
| if (wake_flags & WF_SYNC) |
| schedstat_inc(p, se.statistics.nr_wakeups_sync); |
| |
| #endif /* CONFIG_SCHEDSTATS */ |
| } |
| |
| static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags) |
| { |
| activate_task(rq, p, en_flags); |
| p->on_rq = 1; |
| |
| /* if a worker is waking up, notify workqueue */ |
| if (p->flags & PF_WQ_WORKER) |
| wq_worker_waking_up(p, cpu_of(rq)); |
| } |
| |
| /* |
| * Mark the task runnable and perform wakeup-preemption. |
| */ |
| static void |
| ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
| { |
| check_preempt_curr(rq, p, wake_flags); |
| trace_sched_wakeup(p, true); |
| |
| p->state = TASK_RUNNING; |
| #ifdef CONFIG_SMP |
| if (p->sched_class->task_woken) |
| p->sched_class->task_woken(rq, p); |
| |
| if (rq->idle_stamp) { |
| u64 delta = rq_clock(rq) - rq->idle_stamp; |
| u64 max = 2*rq->max_idle_balance_cost; |
| |
| update_avg(&rq->avg_idle, delta); |
| |
| if (rq->avg_idle > max) |
| rq->avg_idle = max; |
| |
| rq->idle_stamp = 0; |
| } |
| #endif |
| } |
| |
| static void |
| ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags) |
| { |
| #ifdef CONFIG_SMP |
| if (p->sched_contributes_to_load) |
| rq->nr_uninterruptible--; |
| #endif |
| |
| ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING); |
| ttwu_do_wakeup(rq, p, wake_flags); |
| } |
| |
| /* |
| * Called in case the task @p isn't fully descheduled from its runqueue, |
| * in this case we must do a remote wakeup. Its a 'light' wakeup though, |
| * since all we need to do is flip p->state to TASK_RUNNING, since |
| * the task is still ->on_rq. |
| */ |
| static int ttwu_remote(struct task_struct *p, int wake_flags) |
| { |
| struct rq *rq; |
| int ret = 0; |
| |
| rq = __task_rq_lock(p); |
| if (p->on_rq) { |
| /* check_preempt_curr() may use rq clock */ |
| update_rq_clock(rq); |
| ttwu_do_wakeup(rq, p, wake_flags); |
| ret = 1; |
| } |
| __task_rq_unlock(rq); |
| |
| return ret; |
| } |
| |
| #ifdef CONFIG_SMP |
| static void sched_ttwu_pending(void) |
| { |
| struct rq *rq = this_rq(); |
| struct llist_node *llist = llist_del_all(&rq->wake_list); |
| struct task_struct *p; |
| |
| raw_spin_lock(&rq->lock); |
| |
| while (llist) { |
| p = llist_entry(llist, struct task_struct, wake_entry); |
| llist = llist_next(llist); |
| ttwu_do_activate(rq, p, 0); |
| } |
| |
| raw_spin_unlock(&rq->lock); |
| } |
| |
| void scheduler_ipi(void) |
| { |
| /* |
| * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting |
| * TIF_NEED_RESCHED remotely (for the first time) will also send |
| * this IPI. |
| */ |
| if (tif_need_resched()) |
| set_preempt_need_resched(); |
| |
| if (llist_empty(&this_rq()->wake_list) |
| && !tick_nohz_full_cpu(smp_processor_id()) |
| && !got_nohz_idle_kick()) |
| return; |
| |
| /* |
| * Not all reschedule IPI handlers call irq_enter/irq_exit, since |
| * traditionally all their work was done from the interrupt return |
| * path. Now that we actually do some work, we need to make sure |
| * we do call them. |
| * |
| * Some archs already do call them, luckily irq_enter/exit nest |
| * properly. |
| * |
| * Arguably we should visit all archs and update all handlers, |
| * however a fair share of IPIs are still resched only so this would |
| * somewhat pessimize the simple resched case. |
| */ |
| irq_enter(); |
| tick_nohz_full_check(); |
| sched_ttwu_pending(); |
| |
| /* |
| * Check if someone kicked us for doing the nohz idle load balance. |
| */ |
| if (unlikely(got_nohz_idle_kick())) { |
| this_rq()->idle_balance = 1; |
| raise_softirq_irqoff(SCHED_SOFTIRQ); |
| } |
| irq_exit(); |
| } |
| |
| static void ttwu_queue_remote(struct task_struct *p, int cpu) |
| { |
| if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) |
| smp_send_reschedule(cpu); |
| } |
| |
| bool cpus_share_cache(int this_cpu, int that_cpu) |
| { |
| return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); |
| } |
| #endif /* CONFIG_SMP */ |
| |
| static void ttwu_queue(struct task_struct *p, int cpu) |
| { |
| struct rq *rq = cpu_rq(cpu); |
| |
| #if defined(CONFIG_SMP) |
| if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) { |
| sched_clock_cpu(cpu); /* sync clocks x-cpu */ |
| ttwu_queue_remote(p, cpu); |
| return; |
| } |
| #endif |
| |
| raw_spin_lock(&rq->lock); |
| ttwu_do_activate(rq, p, 0); |
| raw_spin_unlock(&rq->lock); |
| } |
| |
| /** |
| * try_to_wake_up - wake up a thread |
| * @p: the thread to be awakened |
| * @state: the mask of task states that can be woken |
| * @wake_flags: wake modifier flags (WF_*) |
| * |
| * Put it on the run-queue if it's not already there. The "current" |
| * thread is always on the run-queue (except when the actual |
| * re-schedule is in progress), and as such you're allowed to do |
| * the simpler "current->state = TASK_RUNNING" to mark yourself |
| * runnable without the overhead of this. |
| * |
| * Return: %true if @p was woken up, %false if it was already running. |
| * or @state didn't match @p's state. |
| */ |
| static int |
| try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) |
| { |
| unsigned long flags; |
| int cpu, success = 0; |
| |
| /* |
| * If we are going to wake up a thread waiting for CONDITION we |
| * need to ensure that CONDITION=1 done by the caller can not be |
| * reordered with p->state check below. This pairs with mb() in |
| * set_current_state() the waiting thread does. |
| */ |
| smp_mb__before_spinlock(); |
| raw_spin_lock_irqsave(&p->pi_lock, flags); |
| if (!(p->state & state)) |
| goto out; |
| |
| success = 1; /* we're going to change ->state */ |
| cpu = task_cpu(p); |
| |
| if (p->on_rq && ttwu_remote(p, wake_flags)) |
| goto stat; |
| |
| #ifdef CONFIG_SMP |
| /* |
| * If the owning (remote) cpu is still in the middle of schedule() with |
| * this task as prev, wait until its done referencing the task. |
| */ |
| while (p->on_cpu) |
| cpu_relax(); |
| /* |
| * Pairs with the smp_wmb() in finish_lock_switch(). |
| */ |
| smp_rmb(); |
| |
| p->sched_contributes_to_load = !!task_contributes_to_load(p); |
| p->state = TASK_WAKING; |
| |
| if (p->sched_class->task_waking) |
| p->sched_class->task_waking(p); |
| |
| cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags); |
| if (task_cpu(p) != cpu) { |
| wake_flags |= WF_MIGRATED; |
| set_task_cpu(p, cpu); |
| } |
| #endif /* CONFIG_SMP */ |
| |
| ttwu_queue(p, cpu); |
| stat: |
| ttwu_stat(p, cpu, wake_flags); |
| out: |
| raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
| |
| return success; |
| } |
| |
| /** |
| * try_to_wake_up_local - try to wake up a local task with rq lock held |
| * @p: the thread to be awakened |
| * |
| * Put @p on the run-queue if it's not already there. The caller must |
| * ensure that this_rq() is locked, @p is bound to this_rq() and not |
| * the current task. |
| */ |
| static void try_to_wake_up_local(struct task_struct *p) |
| { |
| struct rq *rq = task_rq(p); |
| |
| if (WARN_ON_ONCE(rq != this_rq()) || |
| WARN_ON_ONCE(p == current)) |
| return; |
| |
| lockdep_assert_held(&rq->lock); |
| |
| if (!raw_spin_trylock(&p->pi_lock)) { |
| raw_spin_unlock(&rq->lock); |
| raw_spin_lock(&p->pi_lock); |
| raw_spin_lock(&rq->lock); |
| } |
| |
| if (!(p->state & TASK_NORMAL)) |
| goto out; |
| |
| if (!p->on_rq) |
| ttwu_activate(rq, p, ENQUEUE_WAKEUP); |
| |
| ttwu_do_wakeup(rq, p, 0); |
| ttwu_stat(p, smp_processor_id(), 0); |
| out: |
| raw_spin_unlock(&p->pi_lock); |
| } |
| |
| /** |
| * wake_up_process - Wake up a specific process |
| * @p: The process to be woken up. |
| * |
| * Attempt to wake up the nominated process and move it to the set of runnable |
| * processes. |
| * |
| * Return: 1 if the process was woken up, 0 if it was already running. |
| * |
| * It may be assumed that this function implies a write memory barrier before |
| * changing the task state if and only if any tasks are woken up. |
| */ |
| int wake_up_process(struct task_struct *p) |
| { |
| WARN_ON(task_is_stopped_or_traced(p)); |
| return try_to_wake_up(p, TASK_NORMAL, 0); |
| } |
| EXPORT_SYMBOL(wake_up_process); |
| |
| int wake_up_state(struct task_struct *p, unsigned int state) |
| { |
| return try_to_wake_up(p, state, 0); |
| } |
| |
| /* |
| * Perform scheduler related setup for a newly forked process p. |
| * p is forked by current. |
| * |
| * __sched_fork() is basic setup used by init_idle() too: |
| */ |
| static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
| { |
| p->on_rq = 0; |
| |
| p->se.on_rq = 0; |
| p->se.exec_start = 0; |
| p->se.sum_exec_runtime = 0; |
| p->se.prev_sum_exec_runtime = 0; |
| p->se.nr_migrations = 0; |
| p->se.vruntime = 0; |
| INIT_LIST_HEAD(&p->se.group_node); |
| |
| #ifdef CONFIG_SCHEDSTATS |
| memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
| #endif |
| |
| INIT_LIST_HEAD(&p->rt.run_list); |
| |
| #ifdef CONFIG_PREEMPT_NOTIFIERS |
| INIT_HLIST_HEAD(&p->preempt_notifiers); |
| #endif |
| |
| #ifdef CONFIG_NUMA_BALANCING |
| if (p->mm && atomic_read(&p->mm->mm_users) == 1) { |
| p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
| p->mm->numa_scan_seq = 0; |
| } |
| |
| if (clone_flags & CLONE_VM) |
| p->numa_preferred_nid = current->numa_preferred_nid; |
| else |
| p->numa_preferred_nid = -1; |
| |
| p->node_stamp = 0ULL; |
| p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0; |
| p->numa_scan_period = sysctl_numa_balancing_scan_delay; |
| p->numa_work.next = &p->numa_work; |
| p->numa_faults = NULL; |
| p->numa_faults_buffer = NULL; |
| |
| INIT_LIST_HEAD(&p->numa_entry); |
| p->numa_group = NULL; |
| #endif /* CONFIG_NUMA_BALANCING */ |
| } |
| |
| #ifdef CONFIG_NUMA_BALANCING |
| #ifdef CONFIG_SCHED_DEBUG |
| void set_numabalancing_state(bool enabled) |
| { |
| if (enabled) |
| sched_feat_set("NUMA"); |
| else |
| sched_feat_set("NO_NUMA"); |
| } |
| #else |
| __read_mostly bool numabalancing_enabled; |
| |
| void set_numabalancing_state(bool enabled) |
| { |
| numabalancing_enabled = enabled; |
| } |
| #endif /* CONFIG_SCHED_DEBUG */ |
| #endif /* CONFIG_NUMA_BALANCING */ |
| |
| /* |
| * fork()/clone()-time setup: |
| */ |
| void sched_fork(unsigned long clone_flags, struct task_struct *p) |
| { |
| unsigned long flags; |
| int cpu = get_cpu(); |
| |
| __sched_fork(clone_flags, p); |
| /* |
| * We mark the process as running here. This guarantees that |
| * nobody will actually run it, and a signal or other external |
| * event cannot wake it up and insert it on the runqueue either. |
| */ |
| p->state = TASK_RUNNING; |
| |
| /* |
| * Make sure we do not leak PI boosting priority to the child. |
| */ |
| p->prio = current->normal_prio; |
| |
| /* |
| * Revert to default priority/policy on fork if requested. |
| */ |
| if (unlikely(p->sched_reset_on_fork)) { |
| if (task_has_rt_policy(p)) { |
| p->policy = SCHED_NORMAL; |
| p->static_prio = NICE_TO_PRIO(0); |
| p->rt_priority = 0; |
| } else if (PRIO_TO_NICE(p->static_prio) < 0) |
| p->static_prio = NICE_TO_PRIO(0); |
| |
| p->prio = p->normal_prio = __normal_prio(p); |
| set_load_weight(p); |
| |
| /* |
| * We don't need the reset flag anymore after the fork. It has |
| * fulfilled its duty: |
| */ |
| p->sched_reset_on_fork = 0; |
| } |
| |
| if (!rt_prio(p->prio)) |
| p->sched_class = &fair_sched_class; |
| |
| if (p->sched_class->task_fork) |
| p->sched_class->task_fork(p); |
| |
| /* |
| * The child is not yet in the pid-hash so no cgroup attach races, |
| * and the cgroup is pinned to this child due to cgroup_fork() |
| * is ran before sched_fork(). |
| * |
| * Silence PROVE_RCU. |
| */ |
| raw_spin_lock_irqsave(&p->pi_lock, flags); |
| set_task_cpu(p, cpu); |
| raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
| |
| #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
| if (likely(sched_info_on())) |
| memset(&p->sched_info, 0, sizeof(p->sched_info)); |
| #endif |
| #if defined(CONFIG_SMP) |
| p->on_cpu = 0; |
| #endif |
| init_task_preempt_count(p); |
| #ifdef CONFIG_SMP |
| plist_node_init(&p->pushable_tasks, MAX_PRIO); |
| #endif |
| |
| put_cpu(); |
| } |
| |
| /* |
| * wake_up_new_task - wake up a newly created task for the first time. |
| * |
| * This function will do some initial scheduler statistics housekeeping |
| * that must be done for every newly created context, then puts the task |
| * on the runqueue and wakes it. |
| */ |
| void wake_up_new_task(struct task_struct *p) |
| { |
| unsigned long flags; |
| struct rq *rq; |
| |
| raw_spin_lock_irqsave(&p->pi_lock, flags); |
| #ifdef CONFIG_SMP |
| /* |
| * Fork balancing, do it here and not earlier because: |
| * - cpus_allowed can change in the fork path |
| * - any previously selected cpu might disappear through hotplug |
| */ |
| set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0)); |
| #endif |
| |
| /* Initialize new task's runnable average */ |
| init_task_runnable_average(p); |
| rq = __task_rq_lock(p); |
| activate_task(rq, p, 0); |
| p->on_rq = 1; |
| trace_sched_wakeup_new(p, true); |
| check_preempt_curr(rq, p, WF_FORK); |
| #ifdef CONFIG_SMP |
| if (p->sched_class->task_woken) |
| p->sched_class->task_woken(rq, p); |
| #endif |
| task_rq_unlock(rq, p, &flags); |
| } |
| |
| #ifdef CONFIG_PREEMPT_NOTIFIERS |
| |
| /** |
| * preempt_notifier_register - tell me when current is being preempted & rescheduled |
| * @notifier: notifier struct to register |
| */ |
| void preempt_notifier_register(struct preempt_notifier *notifier) |
| { |
| hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
| } |
| EXPORT_SYMBOL_GPL(preempt_notifier_register); |
| |
| /** |
| * preempt_notifier_unregister - no longer interested in preemption notifications |
| * @notifier: notifier struct to unregister |
| * |
| * This is safe to call from within a preemption notifier. |
| */ |
| void preempt_notifier_unregister(struct preempt_notifier *notifier) |
| { |
| hlist_del(¬ifier->link); |
| } |
| EXPORT_SYMBOL_GPL(preempt_notifier_unregister); |
| |
| static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| { |
| struct preempt_notifier *notifier; |
| |
| hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
| notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
| } |
| |
| static void |
| fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| struct task_struct *next) |
| { |
| struct preempt_notifier *notifier; |
| |
| hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
| notifier->ops->sched_out(notifier, next); |
| } |
| |
| #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
| |
| static void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
| { |
| } |
| |
| static void |
| fire_sched_out_preempt_notifiers(struct task_struct *curr, |
| struct task_struct *next) |
| { |
| } |
| |
| #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
| |
| /** |
| * prepare_task_switch - prepare to switch tasks |
| * @rq: the runqueue preparing to switch |
| * @prev: the current task that is being switched out |
| * @next: the task we are going to switch to. |
| * |
| * This is called with the rq lock held and interrupts off. It must |
| * be paired with a subsequent finish_task_switch after the context |
| * switch. |
| * |
| * prepare_task_switch sets up locking and calls architecture specific |
| * hooks. |
| */ |
| static inline void |
| prepare_task_switch(struct rq *rq, struct task_struct *prev, |
| struct task_struct *next) |
| { |
| trace_sched_switch(prev, next); |
| sched_info_switch(rq, prev, next); |
| perf_event_task_sched_out(prev, next); |
| fire_sched_out_preempt_notifiers(prev, next); |
| prepare_lock_switch(rq, next); |
| prepare_arch_switch(next); |
| } |
| |
| /** |
| * finish_task_switch - clean up after a task-switch |
| * @rq: runqueue associated with task-switch |
| * @prev: the thread we just switched away from. |
| * |
| * finish_task_switch must be called after the context switch, paired |
| * with a prepare_task_switch call before the context switch. |
| * finish_task_switch will reconcile locking set up by prepare_task_switch, |
| * and do any other architecture-specific cleanup actions. |
| * |
| * Note that we may have delayed dropping an mm in context_switch(). If |
| * so, we finish that here outside of the runqueue lock. (Doing it |
| * with the lock held can cause deadlocks; see schedule() for |
| * details.) |
| */ |
| static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
| __releases(rq->lock) |
| { |
| struct mm_struct *mm = rq->prev_mm; |
| long prev_state; |
| |
| rq->prev_mm = NULL; |
| |
| /* |
| * A task struct has one reference for the use as "current". |
| * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
| * schedule one last time. The schedule call will never return, and |
| * the scheduled task must drop that reference. |
| * The test for TASK_DEAD must occur while the runqueue locks are |
| * still held, otherwise prev could be scheduled on another cpu, die |
| * there before we look at prev->state, and then the reference would |
| * be dropped twice. |
| * Manfred Spraul <manfred@colorfullife.com> |
| */ |
| prev_state = prev->state; |
| vtime_task_switch(prev); |
| finish_arch_switch(prev); |
| perf_event_task_sched_in(prev, current); |
| finish_lock_switch(rq, prev); |
| finish_arch_post_lock_switch(); |
| |
| fire_sched_in_preempt_notifiers(current); |
| if (mm) |
| mmdrop(mm); |
| if (unlikely(prev_state == TASK_DEAD)) { |
| task_numa_free(prev); |
| |
| /* |
| * Remove function-return probe instances associated with this |
| * task and put them back on the free list. |
| */ |
| kprobe_flush_task(prev); |
| put_task_struct(prev); |
| } |
| |
| tick_nohz_task_switch(current); |
| } |
| |
| #ifdef CONFIG_SMP |
| |
| /* assumes rq->lock is held */ |
| static inline void pre_schedule(struct rq *rq, struct task_struct *prev) |
| { |
| if (prev->sched_class->pre_schedule) |
| prev->sched_class->pre_schedule(rq, prev); |
| } |
| |
| /* rq->lock is NOT held, but preemption is disabled */ |
| static inline void post_schedule(struct rq *rq) |
| { |
| if (rq->post_schedule) { |
| unsigned long flags; |
| |
| raw_spin_lock_irqsave(&rq->lock, flags); |
| if (rq->curr->sched_class->post_schedule) |
| rq->curr->sched_class->post_schedule(rq); |
| raw_spin_unlock_irqrestore(&rq->lock, flags); |
| |
| rq->post_schedule = 0; |
| } |
| } |
| |
| #else |
| |
| static inline void pre_schedule(struct rq *rq, struct task_struct *p) |
| { |
| } |
| |
| static inline void post_schedule(struct rq *rq) |
| { |
| } |
| |
| #endif |
| |
| /** |
| * schedule_tail - first thing a freshly forked thread must call. |
| * @prev: the thread we just switched away from. |
| */ |
| asmlinkage void schedule_tail(struct task_struct *prev) |
| __releases(rq->lock) |
| { |
| struct rq *rq = this_rq(); |
| |
| finish_task_switch(rq, prev); |
| |
| /* |
| * FIXME: do we need to worry about rq being invalidated by the |
| * task_switch? |
| */ |
| post_schedule(rq); |
| |
| #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
| /* In this case, finish_task_switch does not reenable preemption */ |
| preempt_enable(); |
| #endif |
| if (current->set_child_tid) |
| put_user(task_pid_vnr(current), current->set_child_tid); |
| } |
| |
| /* |
| * context_switch - switch to the new MM and the new |
| * thread's register state. |
| */ |
| static inline void |
| context_switch(struct rq *rq, struct task_struct *prev, |
| struct task_struct *next) |
| { |
| struct mm_struct *mm, *oldmm; |
| |
| prepare_task_switch(rq, prev, next); |
| |
| mm = next->mm; |
| oldmm = prev->active_mm; |
| /* |
| * For paravirt, this is coupled with an exit in switch_to to |
| * combine the page table reload and the switch backend into |
| * one hypercall. |
| */ |
| arch_start_context_switch(prev); |
| |
| if (!mm) { |
| next->active_mm = oldmm; |
| atomic_inc(&oldmm->mm_count); |
| enter_lazy_tlb(oldmm, next); |
| } else |
| switch_mm(oldmm, mm, next); |
| |
| if (!prev->mm) { |
| prev->active_mm = NULL; |
| rq->prev_mm = oldmm; |
| } |
| /* |
| * Since the runqueue lock will be released by the next |
| * task (which is an invalid locking op but in the case |
| * of the scheduler it's an obvious special-case), so we |
| * do an early lockdep release here: |
| */ |
| #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
| spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
| #endif |
| |
| context_tracking_task_switch(prev, next); |
| /* Here we just switch the register state and the stack. */ |
| switch_to(prev, next, prev); |
| |
| barrier(); |
| /* |
| * this_rq must be evaluated again because prev may have moved |
| * CPUs since it called schedule(), thus the 'rq' on its stack |
| * frame will be invalid. |
| */ |
| finish_task_switch(this_rq(), prev); |
| } |
| |
| /* |
| * nr_running and nr_context_switches: |
| * |
| * externally visible scheduler statistics: current number of runnable |
| * threads, total number of context switches performed since bootup. |
| */ |
| unsigned long nr_running(void) |
| { |
| unsigned long i, sum = 0; |
| |
| for_each_online_cpu(i) |
| sum += cpu_rq(i)->nr_running; |
| |
| return sum; |
| } |
| |
| unsigned long long nr_context_switches(void) |
| { |
| int i; |
| unsigned long long sum = 0; |
| |
| for_each_possible_cpu(i) |
| sum += cpu_rq(i)->nr_switches; |
| |
| return sum; |
| } |
| |
| unsigned long nr_iowait(void) |
| { |
| unsigned long i, sum = 0; |
| |
| for_each_possible_cpu(i) |
| sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| |
| return sum; |
| } |
| |
| unsigned long nr_iowait_cpu(int cpu) |
| { |
| struct rq *this = cpu_rq(cpu); |
| return atomic_read(&this->nr_iowait); |
| } |
| |
| #ifdef CONFIG_SMP |
| |
| /* |
| * sched_exec - execve() is a valuable balancing opportunity, because at |
| * this point the task has the smallest effective memory and cache footprint. |
| */ |
| void sched_exec(void) |
| { |
| struct task_struct *p = current; |
| unsigned long flags; |
| int dest_cpu; |
| |
| raw_spin_lock_irqsave(&p->pi_lock, flags); |
| dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0); |
| if (dest_cpu == smp_processor_id()) |
| goto unlock; |
| |
| if (likely(cpu_active(dest_cpu))) { |
| struct migration_arg arg = { p, dest_cpu }; |
| |
| raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
| stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); |
| return; |
| } |
| unlock: |
| raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
| } |
| |
| #endif |
| |
| DEFINE_PER_CPU(struct kernel_stat, kstat); |
| DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
| |
| EXPORT_PER_CPU_SYMBOL(kstat); |
| EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
| |
| /* |
| * Return any ns on the sched_clock that have not yet been accounted in |
| * @p in case that task is currently running. |
| * |
| * Called with task_rq_lock() held on @rq. |
| */ |
| static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq) |
| { |
| u64 ns = 0; |
| |
| if (task_current(rq, p)) { |
| update_rq_clock(rq); |
| ns = rq_clock_task(rq) - p->se.exec_start; |
| if ((s64)ns < 0) |
| ns = 0; |
| } |
| |
| return ns; |
| } |
| |
| unsigned long long task_delta_exec(struct task_struct *p) |
| { |
| unsigned long flags; |
| struct rq *rq; |
| u64 ns = 0; |
| |
| rq = task_rq_lock(p, &flags); |
| ns = do_task_delta_exec(p, rq); |
| task_rq_unlock(rq, p, &flags); |
| |
| return ns; |
| } |
| |
| /* |
| * Return accounted runtime for the task. |
| * In case the task is currently running, return the runtime plus current's |
| * pending runtime that have not been accounted yet. |
| */ |
| unsigned long long task_sched_runtime(struct task_struct *p) |
| { |
| unsigned long flags; |
| struct rq *rq; |
| u64 ns = 0; |
| |
| #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
| /* |
| * 64-bit doesn't need locks to atomically read a 64bit value. |
| * So we have a optimization chance when the task's delta_exec is 0. |
| * Reading ->on_cpu is racy, but this is ok. |
| * |
| * If we race with it leaving cpu, we'll take a lock. So we're correct. |
| * If we race with it entering cpu, unaccounted time is 0. This is |
| * indistinguishable from the read occurring a few cycles earlier. |
| */ |
| if (!p->on_cpu) |
| return p->se.sum_exec_runtime; |
| #endif |
| |
| rq = task_rq_lock(p, &flags); |
| ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq); |
| task_rq_unlock(rq, p, &flags); |
| |
| return ns; |
| } |
| |
| /* |
| * This function gets called by the timer code, with HZ frequency. |
| * We call it with interrupts disabled. |
| */ |
| void scheduler_tick(void) |
| { |
| int cpu = smp_processor_id(); |
| struct rq *rq = cpu_rq(cpu); |
| struct task_struct *curr = rq->curr; |
| |
| sched_clock_tick(); |
| |
| raw_spin_lock(&rq->lock); |
| update_rq_clock(rq); |
| curr->sched_class->task_tick(rq, curr, 0); |
| update_cpu_load_active(rq); |
| raw_spin_unlock(&rq->lock); |
| |
| perf_event_task_tick(); |
| |
| #ifdef CONFIG_SMP |
| rq->idle_balance = idle_cpu(cpu); |
| trigger_load_balance(rq, cpu); |
| #endif |
| rq_last_tick_reset(rq); |
| } |
| |
| #ifdef CONFIG_NO_HZ_FULL |
| /** |
| * scheduler_tick_max_deferment |
| * |
| * Keep at least one tick per second when a single |
| * active task is running because the scheduler doesn't |
| * yet completely support full dynticks environment. |
| * |
| * This makes sure that uptime, CFS vruntime, load |
| * balancing, etc... continue to move forward, even |
| * with a very low granularity. |
| * |
| * Return: Maximum deferment in nanoseconds. |
| */ |
| u64 scheduler_tick_max_deferment(void) |
| { |
| struct rq *rq = this_rq(); |
| unsigned long next, now = ACCESS_ONCE(jiffies); |
| |
| next = rq->last_sched_tick + HZ; |
| |
| if (time_before_eq(next, now)) |
| return 0; |
| |
| return jiffies_to_usecs(next - now) * NSEC_PER_USEC; |
| } |
| #endif |
| |
| notrace unsigned long get_parent_ip(unsigned long addr) |
| { |
| if (in_lock_functions(addr)) { |
| addr = CALLER_ADDR2; |
| if (in_lock_functions(addr)) |
| addr = CALLER_ADDR3; |
| } |
| return addr; |
| } |
| |
| #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
| defined(CONFIG_PREEMPT_TRACER)) |
| |
| void __kprobes preempt_count_add(int val) |
| { |
| #ifdef CONFIG_DEBUG_PREEMPT |
| /* |
| * Underflow? |
| */ |
| if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
| return; |
| #endif |
| __preempt_count_add(val); |
| #ifdef CONFIG_DEBUG_PREEMPT |
| /* |
| * Spinlock count overflowing soon? |
| */ |
| DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
| PREEMPT_MASK - 10); |
| #endif |
| if (preempt_count() == val) |
| trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); |
| } |
| EXPORT_SYMBOL(preempt_count_add); |
| |
| void __kprobes preempt_count_sub(int val) |
| { |
| #ifdef CONFIG_DEBUG_PREEMPT |
| /* |
| * Underflow? |
| */ |
| if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
| return; |
| /* |
| * Is the spinlock portion underflowing? |
| */ |
| if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
| !(preempt_count() & PREEMPT_MASK))) |
| return; |
| #endif |
| |
| if (preempt_count() == val) |
| trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); |
| __preempt_count_sub(val); |
| } |
| EXPORT_SYMBOL(preempt_count_sub); |
| |
| #endif |
| |
| /* |
| * Print scheduling while atomic bug: |
| */ |
| static noinline void __schedule_bug(struct task_struct *prev) |
| { |
| if (oops_in_progress) |
| return; |
| |
| printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
| prev->comm, prev->pid, preempt_count()); |
| |
| debug_show_held_locks(prev); |
| print_modules(); |
| if (irqs_disabled()) |
| print_irqtrace_events(prev); |
| dump_stack(); |
| add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
| } |
| |
| /* |
| * Various schedule()-time debugging checks and statistics: |
| */ |
| static inline void schedule_debug(struct task_struct *prev) |
| { |
| /* |
| * Test if we are atomic. Since do_exit() needs to call into |
| * schedule() atomically, we ignore that path for now. |
| * Otherwise, whine if we are scheduling when we should not be. |
| */ |
| if (unlikely(in_atomic_preempt_off() && !prev->exit_state)) |
| __schedule_bug(prev); |
| rcu_sleep_check(); |
| |
| profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| |
| schedstat_inc(this_rq(), sched_count); |
| } |
| |
| static void put_prev_task(struct rq *rq, struct task_struct *prev) |
| { |
| if (prev->on_rq || rq->skip_clock_update < 0) |
| update_rq_clock(rq); |
| prev->sched_class->put_prev_task(rq, prev); |
| } |
| |
| /* |
| * Pick up the highest-prio task: |
| */ |
| static inline struct task_struct * |
| pick_next_task(struct rq *rq) |
| { |
| const struct sched_class *class; |
| struct task_struct *p; |
| |
| /* |
| * Optimization: we know that if all tasks are in |
| * the fair class we can call that function directly: |
| */ |
| if (likely(rq->nr_running == rq->cfs.h_nr_running)) { |
| p = fair_sched_class.pick_next_task(rq); |
| if (likely(p)) |
| return p; |
| } |
| |
| for_each_class(class) { |
| p = class->pick_next_task(rq); |
| if (p) |
| return p; |
| } |
| |
| BUG(); /* the idle class will always have a runnable task */ |
| } |
| |
| /* |
| * __schedule() is the main scheduler function. |
| * |
| * The main means of driving the scheduler and thus entering this function are: |
| * |
| * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. |
| * |
| * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return |
| * paths. For example, see arch/x86/entry_64.S. |
| * |
| * To drive preemption between tasks, the scheduler sets the flag in timer |
| * interrupt handler scheduler_tick(). |
| * |
| * 3. Wakeups don't really cause entry into schedule(). They add a |
| * task to the run-queue and that's it. |
| * |
| * Now, if the new task added to the run-queue preempts the current |
| * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets |
| * called on the nearest possible occasion: |
| * |
| * - If the kernel is preemptible (CONFIG_PREEMPT=y): |
| * |
| * - in syscall or exception context, at the next outmost |
| * preempt_enable(). (this might be as soon as the wake_up()'s |
| * spin_unlock()!) |
| * |
| * - in IRQ context, return from interrupt-handler to |
| * preemptible context |
| * |
| * - If the kernel is not preemptible (CONFIG_PREEMPT is not set) |
| * then at the next: |
| * |
| * - cond_resched() call |
| * - explicit schedule() call |
| * - return from syscall or exception to user-space |
| * - return from interrupt-handler to user-space |
| */ |
| static void __sched __schedule(void) |
| { |
| struct task_struct *prev, *next; |
| unsigned long *switch_count; |
| struct rq *rq; |
| int cpu; |
| |
| need_resched: |
| preempt_disable(); |
| cpu = smp_processor_id(); |
| rq = cpu_rq(cpu); |
| rcu_note_context_switch(cpu); |
| prev = rq->curr; |
| |
| schedule_debug(prev); |
| |
| if (sched_feat(HRTICK)) |
| hrtick_clear(rq); |
| |
| /* |
| * Make sure that signal_pending_state()->signal_pending() below |
| * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) |
| * done by the caller to avoid the race with signal_wake_up(). |
| */ |
| smp_mb__before_spinlock(); |
| raw_spin_lock_irq(&rq->lock); |
| |
| switch_count = &prev->nivcsw; |
| if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| if (unlikely(signal_pending_state(prev->state, prev))) { |
| prev->state = TASK_RUNNING; |
| } else { |
| deactivate_task(rq, prev, DEQUEUE_SLEEP); |
| prev->on_rq = 0; |
| |
| /* |
| * If a worker went to sleep, notify and ask workqueue |
| * whether it wants to wake up a task to maintain |
| * concurrency. |
| */ |
| if (prev->flags & PF_WQ_WORKER) { |
| struct task_struct *to_wakeup; |
| |
| to_wakeup = wq_worker_sleeping(prev, cpu); |
| if (to_wakeup) |
| try_to_wake_up_local(to_wakeup); |
| } |
| } |
| switch_count = &prev->nvcsw; |
| } |
| |
| pre_schedule(rq, prev); |
| |
| if (unlikely(!rq->nr_running)) |
| idle_balance(cpu, rq); |
| |
| put_prev_task(rq, prev); |
| next = pick_next_task(rq); |
| clear_tsk_need_resched(prev); |
| clear_preempt_need_resched(); |
| rq->skip_clock_update = 0; |
| |
| if (likely(prev != next)) { |
| rq->nr_switches++; |
| rq->curr = next; |
| ++*switch_count; |
| |
| context_switch(rq, prev, next); /* unlocks the rq */ |
| /* |
| * The context switch have flipped the stack from under us |
| * and restored the local variables which were saved when |
| * this task called schedule() in the past. prev == current |
| * is still correct, but it can be moved to another cpu/rq. |
| */ |
| cpu = smp_processor_id(); |
| rq = cpu_rq(cpu); |
| } else |
| raw_spin_unlock_irq(&rq->lock); |
| |
| post_schedule(rq); |
| |
| sched_preempt_enable_no_resched(); |
| if (need_resched()) |
| goto need_resched; |
| } |
| |
| static inline void sched_submit_work(struct task_struct *tsk) |
| { |
| if (!tsk->state || tsk_is_pi_blocked(tsk)) |
| return; |
| /* |
| * If we are going to sleep and we have plugged IO queued, |
| * make sure to submit it to avoid deadlocks. |
| */ |
| if (blk_needs_flush_plug(tsk)) |
| blk_schedule_flush_plug(tsk); |
| } |
| |
| asmlinkage void __sched schedule(void) |
| { |
| struct task_struct *tsk = current; |
| |
| sched_submit_work(tsk); |
| __schedule(); |
| } |
| EXPORT_SYMBOL(schedule); |
| |
| #ifdef CONFIG_CONTEXT_TRACKING |
| asmlinkage void __sched schedule_user(void) |
| { |
| /* |
| * If we come here after a random call to set_need_resched(), |
| * or we have been woken up remotely but the IPI has not yet arrived, |
| * we haven't yet exited the RCU idle mode. Do it here manually until |
| * we find a better solution. |
| */ |
| user_exit(); |
| schedule(); |
| user_enter(); |
| } |
| #endif |
| |
| /** |
| * schedule_preempt_disabled - called with preemption disabled |
| * |
| * Returns with preemption disabled. Note: preempt_count must be 1 |
| */ |
| void __sched schedule_preempt_disabled(void) |
| { |
| sched_preempt_enable_no_resched(); |
| schedule(); |
| preempt_disable(); |
| } |
| |
| #ifdef CONFIG_PREEMPT |
| /* |
| * this is the entry point to schedule() from in-kernel preemption |
| * off of preempt_enable. Kernel preemptions off return from interrupt |
| * occur there and call schedule directly. |
| */ |
| asmlinkage void __sched notrace preempt_schedule(void) |
| { |
| /* |
| * If there is a non-zero preempt_count or interrupts are disabled, |
| * we do not want to preempt the current task. Just return.. |
| */ |
| if (likely(!preemptible())) |
| return; |
| |
| do { |
| __preempt_count_add(PREEMPT_ACTIVE); |
| __schedule(); |
| __preempt_count_sub(PREEMPT_ACTIVE); |
| |
| /* |
| * Check again in case we missed a preemption opportunity |
| * between schedule and now. |
| */ |
| barrier(); |
| } while (need_resched()); |
| } |
| EXPORT_SYMBOL(preempt_schedule); |
| |
| /* |
| * this is the entry point to schedule() from kernel preemption |
| * off of irq context. |
| * Note, that this is called and return with irqs disabled. This will |
| * protect us against recursive calling from irq. |
| */ |
| asmlinkage void __sched preempt_schedule_irq(void) |
| { |
| enum ctx_state prev_state; |
| |
| /* Catch callers which need to be fixed */ |
| BUG_ON(preempt_count() || !irqs_disabled()); |
| |
| prev_state = exception_enter(); |
| |
| do { |
| __preempt_count_add(PREEMPT_ACTIVE); |
| local_irq_enable(); |
| __schedule(); |
| local_irq_disable(); |
| __preempt_count_sub(PREEMPT_ACTIVE); |
| |
| /* |
| * Check again in case we missed a preemption opportunity |
| * between schedule and now. |
| */ |
| barrier(); |
| } while (need_resched()); |
| |
| exception_exit(prev_state); |
| } |
| |
| #endif /* CONFIG_PREEMPT */ |
| |
| int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags, |
| void *key) |
| { |
| return try_to_wake_up(curr->private, mode, wake_flags); |
| } |
| EXPORT_SYMBOL(default_wake_function); |
| |
| static long __sched |
| sleep_on_common(wait_queue_head_t *q, int state, long timeout) |
| { |
| unsigned long flags; |
| wait_queue_t wait; |
| |
| init_waitqueue_entry(&wait, current); |
| |
| __set_current_state(state); |
| |
| spin_lock_irqsave(&q->lock, flags); |
| __add_wait_queue(q, &wait); |
| spin_unlock(&q->lock); |
| timeout = schedule_timeout(timeout); |
| spin_lock_irq(&q->lock); |
| __remove_wait_queue(q, &wait); |
| spin_unlock_irqrestore(&q->lock, flags); |
| |
| return timeout; |
| } |
| |
| void __sched interruptible_sleep_on(wait_queue_head_t *q) |
| { |
| sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
| } |
| EXPORT_SYMBOL(interruptible_sleep_on); |
| |
| long __sched |
| interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| { |
| return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
| } |
| EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| |
| void __sched sleep_on(wait_queue_head_t *q) |
| { |
| sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
| } |
| EXPORT_SYMBOL(sleep_on); |
| |
| long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| { |
| return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
| } |
| EXPORT_SYMBOL(sleep_on_timeout); |
| |
| #ifdef CONFIG_RT_MUTEXES |
| |
| /* |
| * rt_mutex_setprio - set the current priority of a task |
| * @p: task |
| * @prio: prio value (kernel-internal form) |
| * |
| * This function changes the 'effective' priority of a task. It does |
| * not touch ->normal_prio like __setscheduler(). |
| * |
| * Used by the rt_mutex code to implement priority inheritance logic. |
| */ |
| void rt_mutex_setprio(struct task_struct *p, int prio) |
| { |
| int oldprio, on_rq, running; |
| struct rq *rq; |
| const struct sched_class *prev_class; |
| |
| BUG_ON(prio < 0 || prio > MAX_PRIO); |
| |
| rq = __task_rq_lock(p); |
| |
| /* |
| * Idle task boosting is a nono in general. There is one |
| * exception, when PREEMPT_RT and NOHZ is active: |
| * |
| * The idle task calls get_next_timer_interrupt() and holds |
| * the timer wheel base->lock on the CPU and another CPU wants |
| * to access the timer (probably to cancel it). We can safely |
| * ignore the boosting request, as the idle CPU runs this code |
| * with interrupts disabled and will complete the lock |
| * protected section without being interrupted. So there is no |
| * real need to boost. |
| */ |
| if (unlikely(p == rq->idle)) { |
| WARN_ON(p != rq->curr); |
| WARN_ON(p->pi_blocked_on); |
| goto out_unlock; |
| } |
| |
| trace_sched_pi_setprio(p, prio); |
| oldprio = p->prio; |
| prev_class = p->sched_class; |
| on_rq = p->on_rq; |
| running = task_current(rq, p); |
| if (on_rq) |
| dequeue_task(rq, p, 0); |
| if (running) |
| p->sched_class->put_prev_task(rq, p); |
| |
| if (rt_prio(prio)) |
| p->sched_class = &rt_sched_class; |
| else |
| p->sched_class = &fair_sched_class; |
| |
| p->prio = prio; |
| |
| if (running) |
| p->sched_class->set_curr_task(rq); |
| if (on_rq) |
| enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0); |
| |
| check_class_changed(rq, p, prev_class, oldprio); |
| out_unlock: |
| __task_rq_unlock(rq); |
| } |
| #endif |
| void set_user_nice(struct task_struct *p, long nice) |
| { |
| int old_prio, delta, on_rq; |
| unsigned long flags; |
| struct rq *rq; |
| |
| if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| return; |
| /* |
| * We have to be careful, if called from sys_setpriority(), |
| * the task might be in the middle of scheduling on another CPU. |
| */ |
| rq = task_rq_lock(p, &flags); |
| /* |
| * The RT priorities are set via sched_setscheduler(), but we still |
| * allow the 'normal' nice value to be set - but as expected |
| * it wont have any effect on scheduling until the task is |
| * SCHED_FIFO/SCHED_RR: |
| */ |
| if (task_has_rt_policy(p)) { |
| p->static_prio = NICE_TO_PRIO(nice); |
| goto out_unlock; |
| } |
| on_rq = p->on_rq; |
| if (on_rq) |
| dequeue_task(rq, p, 0); |
| |
| p->static_prio = NICE_TO_PRIO(nice); |
| set_load_weight(p); |
| old_prio = p->prio; |
| p->prio = effective_prio(p); |
| delta = p->prio - old_prio; |
| |
| if (on_rq) { |
| enqueue_task(rq, p, 0); |
| /* |
| * If the task increased its priority or is running and |
| * lowered its priority, then reschedule its CPU: |
| */ |
| if (delta < 0 || (delta > 0 && task_running(rq, p))) |
| resched_task(rq->curr); |
| } |
| out_unlock: |
| task_rq_unlock(rq, p, &flags); |
| } |
| EXPORT_SYMBOL(set_user_nice); |
| |
| /* |
| * can_nice - check if a task can reduce its nice value |
| * @p: task |
| * @nice: nice value |
| */ |
| int can_nice(const struct task_struct *p, const int nice) |
| { |
| /* convert nice value [19,-20] to rlimit style value [1,40] */ |
| int nice_rlim = 20 - nice; |
| |
| return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
| capable(CAP_SYS_NICE)); |
| } |
| |
| #ifdef __ARCH_WANT_SYS_NICE |
| |
| /* |
| * sys_nice - change the priority of the current process. |
| * @increment: priority increment |
| * |
| * sys_setpriority is a more generic, but much slower function that |
| * does similar things. |
| */ |
| SYSCALL_DEFINE1(nice, int, increment) |
| { |
| long nice, retval; |
| |
| /* |
| * Setpriority might change our priority at the same moment. |
| * We don't have to worry. Conceptually one call occurs first |
| * and we have a single winner. |
| */ |
| if (increment < -40) |
| increment = -40; |
| if (increment > 40) |
| increment = 40; |
| |
| nice = TASK_NICE(current) + increment; |
| if (nice < -20) |
| nice = -20; |
| if (nice > 19) |
| nice = 19; |
| |
| if (increment < 0 && !can_nice(current, nice)) |
| return -EPERM; |
| |
| retval = security_task_setnice(current, nice); |
| if (retval) |
| return retval; |
| |
| set_user_nice(current, nice); |
| return 0; |
| } |
| |
| #endif |
| |
| /** |
| * task_prio - return the priority value of a given task. |
| * @p: the task in question. |
| * |
| * Return: The priority value as seen by users in /proc. |
| * RT tasks are offset by -200. Normal tasks are centered |
| * around 0, value goes from -16 to +15. |
| */ |
| int task_prio(const struct task_struct *p) |
| { |
| return p->prio - MAX_RT_PRIO; |
| } |
| |
| /** |
| * task_nice - return the nice value of a given task. |
| * @p: the task in question. |
| * |
| * Return: The nice value [ -20 ... 0 ... 19 ]. |
| */ |
| int task_nice(const struct task_struct *p) |
| { |
| return TASK_NICE(p); |
| } |
| EXPORT_SYMBOL(task_nice); |
| |
| /** |
| * idle_cpu - is a given cpu idle currently? |
| * @cpu: the processor in question. |
| * |
| * Return: 1 if the CPU is currently idle. 0 otherwise. |
| */ |
| int idle_cpu(int cpu) |
| { |
| struct rq *rq = cpu_rq(cpu); |
| |
| if (rq->curr != rq->idle) |
| return 0; |
| |
| if (rq->nr_running) |
| return 0; |
| |
| #ifdef CONFIG_SMP |
| if (!llist_empty(&rq->wake_list)) |
| return 0; |
| #endif |
| |
| return 1; |
| } |
| |
| /** |
| * idle_task - return the idle task for a given cpu. |
| * @cpu: the processor in question. |
| * |
| * Return: The idle task for the cpu @cpu. |
| */ |
| struct task_struct *idle_task(int cpu) |
| { |
| return cpu_rq(cpu)->idle; |
| } |
| |
| /** |
| * find_process_by_pid - find a process with a matching PID value. |
| * @pid: the pid in question. |
| * |
| * The task of @pid, if found. %NULL otherwise. |
| */ |
| static struct task_struct *find_process_by_pid(pid_t pid) |
| { |
| return pid ? find_task_by_vpid(pid) : current; |
| } |
| |
| /* Actually do priority change: must hold rq lock. */ |
| static void |
| __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) |
| { |
| p->policy = policy; |
| p->rt_priority = prio; |
| p->normal_prio = normal_prio(p); |
| /* we are holding p->pi_lock already */ |
| p->prio = rt_mutex_getprio(p); |
| if (rt_prio(p->prio)) |
| p->sched_class = &rt_sched_class; |
| else |
| p->sched_class = &fair_sched_class; |
| set_load_weight(p); |
| } |
| |
| /* |
| * check the target process has a UID that matches the current process's |
| */ |
| static bool check_same_owner(struct task_struct *p) |
| { |
| const struct cred *cred = current_cred(), *pcred; |
| bool match; |
| |
| rcu_read_lock(); |
| pcred = __task_cred(p); |
| match = (uid_eq(cred->euid, pcred->euid) || |
| uid_eq(cred->euid, pcred->uid)); |
| rcu_read_unlock(); |
| return match; |
| } |
| |
| static int __sched_setscheduler(struct task_struct *p, int policy, |
| const struct sched_param *param, bool user) |
| { |
| int retval, oldprio, oldpolicy = -1, on_rq, running; |
| unsigned long flags; |
| const struct sched_class *prev_class; |
| struct rq *rq; |
| int reset_on_fork; |
| |
| /* may grab non-irq protected spin_locks */ |
| BUG_ON(in_interrupt()); |
| recheck: |
| /* double check policy once rq lock held */ |
| if (policy < 0) { |
| reset_on_fork = p->sched_reset_on_fork; |
| policy = oldpolicy = p->policy; |
| } else { |
| reset_on_fork = !!(policy & SCHED_RESET_ON_FORK); |
| policy &= ~SCHED_RESET_ON_FORK; |
| |
| if (policy != SCHED_FIFO && policy != SCHED_RR && |
| policy != SCHED_NORMAL && policy != SCHED_BATCH && |
| policy != SCHED_IDLE) |
| return -EINVAL; |
| } |
| |
| /* |
| * Valid priorities for SCHED_FIFO and SCHED_RR are |
| * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
| * SCHED_BATCH and SCHED_IDLE is 0. |
| */ |
| if (param->sched_priority < 0 || |
| (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
| (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
| return -EINVAL; |
| if (rt_policy(policy) != (param->sched_priority != 0)) |
| return -EINVAL; |
| |
| /* |
| * Allow unprivileged RT tasks to decrease priority: |
| */ |
| if (user && !capable(CAP_SYS_NICE)) { |
| if (rt_policy(policy)) { |
| unsigned long rlim_rtprio = |
| task_rlimit(p, RLIMIT_RTPRIO); |
| |
| /* can't set/change the rt policy */ |
| if (policy != p->policy && !rlim_rtprio) |
| return -EPERM; |
| |
| /* can't increase priority */ |
| if (param->sched_priority > p->rt_priority && |
| param->sched_priority > rlim_rtprio) |
| return -EPERM; |
| } |
| |
| /* |
| * Treat SCHED_IDLE as nice 20. Only allow a switch to |
| * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. |
| */ |
| if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) { |
| if (!can_nice(p, TASK_NICE(p))) |
| return -EPERM; |
| } |
| |
| /* can't change other user's priorities */ |
| if (!check_same_owner(p)) |
| return -EPERM; |
| |
| /* Normal users shall not reset the sched_reset_on_fork flag */ |
| if (p->sched_reset_on_fork && !reset_on_fork) |
| return -EPERM; |
| } |
| |
| if (user) { |
| retval = security_task_setscheduler(p); |
| if (retval) |
| return retval; |
| } |
| |
| /* |
| * make sure no PI-waiters arrive (or leave) while we are |
| * changing the priority of the task: |
| * |
| * To be able to change p->policy safely, the appropriate |
| * runqueue lock must be held. |
| */ |
| rq = task_rq_lock(p, &flags); |
| |
| /* |
| * Changing the policy of the stop threads its a very bad idea |
| */ |
| if (p == rq->stop) { |
| task_rq_unlock(rq, p, &flags); |
| return -EINVAL; |
| } |
| |
| /* |
| * If not changing anything there's no need to proceed further: |
| */ |
| if (unlikely(policy == p->policy && (!rt_policy(policy) || |
| param->sched_priority == p->rt_priority))) { |
| task_rq_unlock(rq, p, &flags); |
| return 0; |
| } |
| |
| #ifdef CONFIG_RT_GROUP_SCHED |
| if (user) { |
| /* |
| * Do not allow realtime tasks into groups that have no runtime |
| * assigned. |
| */ |
| if (rt_bandwidth_enabled() && rt_policy(policy) && |
| task_group(p)->rt_bandwidth.rt_runtime == 0 && |
| !task_group_is_autogroup(task_group(p))) { |
| task_rq_unlock(rq, p, &flags); |
| return -EPERM; |
| } |
| } |
| #endif |
| |
| /* recheck policy now with rq lock held */ |
| if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| policy = oldpolicy = -1; |
| task_rq_unlock(rq, p, &flags); |
| goto recheck; |
| } |
| on_rq = p->on_rq; |
| running = task_current(rq, p); |
| if (on_rq) |
| dequeue_task(rq, p, 0); |
| if (running) |
| p->sched_class->put_prev_task(rq, p); |
| |
| p->sched_reset_on_fork = reset_on_fork; |
| |
| oldprio = p->prio; |
| prev_class = p->sched_class; |
| __setscheduler(rq, p, policy, param->sched_priority); |
| |
| if (running) |
| p->sched_class->set_curr_task(rq); |
| if (on_rq) |
| enqueue_task(rq, p, 0); |
| |
| check_class_changed(rq, p, prev_class, oldprio); |
| task_rq_unlock(rq, p, &flags); |
| |
| rt_mutex_adjust_pi(p); |
| |
| return 0; |
| } |
| |
| /** |
| * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. |
| * @p: the task in question. |
| * @policy: new policy. |
| * @param: structure containing the new RT priority. |
| * |
| * Return: 0 on success. An error code otherwise. |
| * |
| * NOTE that the task may be already dead. |
| */ |
| int sched_setscheduler(struct task_struct *p, int policy, |
| const struct sched_param *param) |
| { |
| return __sched_setscheduler(p, policy, param, true); |
| } |
| EXPORT_SYMBOL_GPL(sched_setscheduler); |
| |
| /** |
| * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. |
| * @p: the task in question. |
| * @policy: new policy. |
| * @param: structure containing the new RT priority. |
| * |
| * Just like sched_setscheduler, only don't bother checking if the |
| * current context has permission. For example, this is needed in |
| * stop_machine(): we create temporary high priority worker threads, |
| * but our caller might not have that capability. |
| * |
| * Return: 0 on success. An error code otherwise. |
| */ |
| int sched_setscheduler_nocheck(struct task_struct *p, int policy, |
| const struct sched_param *param) |
| { |
| return __sched_setscheduler(p, policy, param, false); |
| } |
| |
| static int |
| do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) |
| { |
| struct sched_param lparam; |
| struct task_struct *p; |
| int retval; |
| |
| if (!param || pid < 0) |
| return -EINVAL; |
| if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| return -EFAULT; |
| |
| rcu_read_lock(); |
| retval = -ESRCH; |
| p = find_process_by_pid(pid); |
| if (p != NULL) |
| retval = sched_setscheduler(p, policy, &lparam); |
| rcu_read_unlock(); |
| |
| return retval; |
| } |
| |
| /** |
| * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| * @pid: the pid in question. |
| * @policy: new policy. |
| * @param: structure containing the new RT priority. |
| * |
| * Return: 0 on success. An error code otherwise. |
| */ |
| SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, |
| struct sched_param __user *, param) |
| { |
| /* negative values for policy are not valid */ |
| if (policy < 0) |
| return -EINVAL; |
| |
| return do_sched_setscheduler(pid, policy, param); |
| } |
| |
| /** |
| * sys_sched_setparam - set/change the RT priority of a thread |
| * @pid: the pid in question. |
| * @param: structure containing the new RT priority. |
| * |
| * Return: 0 on success. An error code otherwise. |
| */ |
| SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
| { |
| return do_sched_setscheduler(pid, -1, param); |
| } |
| |
| /** |
| * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| * @pid: the pid in question. |
| * |
| * Return: On success, the policy of the thread. Otherwise, a negative error |
| * code. |
| */ |
| SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
| { |
| struct task_struct *p; |
| int retval; |
| |
| if (pid < 0) |
| return -EINVAL; |
| |
| retval = -ESRCH; |
| rcu_read_lock(); |
| p = find_process_by_pid(pid); |
| if (p) { |
| retval = security_task_getscheduler(p); |
| if (!retval) |
| retval = p->policy |
| | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); |
| } |
| rcu_read_unlock(); |
| return retval; |
| } |
| |
| /** |
| * sys_sched_getparam - get the RT priority of a thread |
| * @pid: the pid in question. |
| * @param: structure containing the RT priority. |
| * |
| * Return: On success, 0 and the RT priority is in @param. Otherwise, an error |
| * code. |
| */ |
| SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
| { |
| struct sched_param lp; |
| struct task_struct *p; |
| int retval; |
| |
| if (!param || pid < 0) |
| return -EINVAL; |
| |
| rcu_read_lock(); |
| p = find_process_by_pid(pid); |
| retval = -ESRCH; |
| if (!p) |
| goto out_unlock; |
| |
| retval = security_task_getscheduler(p); |
| if (retval) |
| goto out_unlock; |
| |
| lp.sched_priority = p->rt_priority; |
| rcu_read_unlock(); |
| |
| /* |
| * This one might sleep, we cannot do it with a spinlock held ... |
| */ |
| retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| |
| return retval; |
| |
| out_unlock: |
| rcu_read_unlock(); |
| return retval; |
| } |
| |
| long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
| { |
| cpumask_var_t cpus_allowed, new_mask; |
| struct task_struct *p; |
| int retval; |
| |
| rcu_read_lock(); |
| |
| p = find_process_by_pid(pid); |
| if (!p) { |
| rcu_read_unlock(); |
| return -ESRCH; |
| } |
| |
| /* Prevent p going away */ |
| get_task_struct(p); |
| rcu_read_unlock(); |
| |
| if (p->flags & PF_NO_SETAFFINITY) { |
| retval = -EINVAL; |
| goto out_put_task; |
| } |
| if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
| retval = -ENOMEM; |
| goto out_put_task; |
| } |
| if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { |
| retval = -ENOMEM; |
| goto out_free_cpus_allowed; |
| } |
| retval = -EPERM; |
| if (!check_same_owner(p)) { |
| rcu_read_lock(); |
| if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { |
| rcu_read_unlock(); |
| goto out_unlock; |
| } |
| rcu_read_unlock(); |
| } |
| |
| retval = security_task_setscheduler(p); |
| if (retval) |
| goto out_unlock; |
| |
| cpuset_cpus_allowed(p, cpus_allowed); |
| cpumask_and(new_mask, in_mask, cpus_allowed); |
| again: |
| retval = set_cpus_allowed_ptr(p, new_mask); |
| |
| if (!retval) { |
| cpuset_cpus_allowed(p, cpus_allowed); |
| if (!cpumask_subset(new_mask, cpus_allowed)) { |
| /* |
| * We must have raced with a concurrent cpuset |
| * update. Just reset the cpus_allowed to the |
| * cpuset's cpus_allowed |
| */ |
| cpumask_copy(new_mask, cpus_allowed); |
| goto again; |
| } |
| } |
| out_unlock: |
| free_cpumask_var(new_mask); |
| out_free_cpus_allowed: |
| free_cpumask_var(cpus_allowed); |
| out_put_task: |
| put_task_struct(p); |
| return retval; |
| } |
| |
| static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| struct cpumask *new_mask) |
| { |
| if (len < cpumask_size()) |
| cpumask_clear(new_mask); |
| else if (len > cpumask_size()) |
| len = cpumask_size(); |
| |
| return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| } |
| |
| /** |
| * sys_sched_setaffinity - set the cpu affinity of a process |
| * @pid: pid of the process |
| * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| * @user_mask_ptr: user-space pointer to the new cpu mask |
|