LCOV - code coverage report
Current view: top level - kernel/sched - core.c (source / functions) Hit Total Coverage
Test: Real Lines: 988 1647 60.0 %
Date: 2020-10-17 15:46:16 Functions: 0 201 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  *  kernel/sched/core.c
       4                 :            :  *
       5                 :            :  *  Core kernel scheduler code and related syscalls
       6                 :            :  *
       7                 :            :  *  Copyright (C) 1991-2002  Linus Torvalds
       8                 :            :  */
       9                 :            : #include "sched.h"
      10                 :            : 
      11                 :            : #include <linux/nospec.h>
      12                 :            : 
      13                 :            : #include <linux/kcov.h>
      14                 :            : 
      15                 :            : #include <asm/switch_to.h>
      16                 :            : #include <asm/tlb.h>
      17                 :            : 
      18                 :            : #include "../workqueue_internal.h"
      19                 :            : #include "../smpboot.h"
      20                 :            : 
      21                 :            : #include "pelt.h"
      22                 :            : 
      23                 :            : #define CREATE_TRACE_POINTS
      24                 :            : #include <trace/events/sched.h>
      25                 :            : 
      26                 :            : /*
      27                 :            :  * Export tracepoints that act as a bare tracehook (ie: have no trace event
      28                 :            :  * associated with them) to allow external modules to probe them.
      29                 :            :  */
      30                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp);
      31                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp);
      32                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp);
      33                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp);
      34                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp);
      35                 :            : EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp);
      36                 :            : 
      37                 :            : DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
      38                 :            : 
      39                 :            : #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_JUMP_LABEL)
      40                 :            : /*
      41                 :            :  * Debugging: various feature bits
      42                 :            :  *
      43                 :            :  * If SCHED_DEBUG is disabled, each compilation unit has its own copy of
      44                 :            :  * sysctl_sched_features, defined in sched.h, to allow constants propagation
      45                 :            :  * at compile time and compiler optimization based on features default.
      46                 :            :  */
      47                 :            : #define SCHED_FEAT(name, enabled)       \
      48                 :            :         (1UL << __SCHED_FEAT_##name) * enabled |
      49                 :            : const_debug unsigned int sysctl_sched_features =
      50                 :            : #include "features.h"
      51                 :            :         0;
      52                 :            : #undef SCHED_FEAT
      53                 :            : #endif
      54                 :            : 
      55                 :            : /*
      56                 :            :  * Number of tasks to iterate in a single balance run.
      57                 :            :  * Limited because this is done with IRQs disabled.
      58                 :            :  */
      59                 :            : const_debug unsigned int sysctl_sched_nr_migrate = 32;
      60                 :            : 
      61                 :            : /*
      62                 :            :  * period over which we measure -rt task CPU usage in us.
      63                 :            :  * default: 1s
      64                 :            :  */
      65                 :            : unsigned int sysctl_sched_rt_period = 1000000;
      66                 :            : 
      67                 :            : __read_mostly int scheduler_running;
      68                 :            : 
      69                 :            : /*
      70                 :            :  * part of the period that we allow rt tasks to run in us.
      71                 :            :  * default: 0.95s
      72                 :            :  */
      73                 :            : int sysctl_sched_rt_runtime = 950000;
      74                 :            : 
      75                 :            : /*
      76                 :            :  * __task_rq_lock - lock the rq @p resides on.
      77                 :            :  */
      78                 :          3 : struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
      79                 :            :         __acquires(rq->lock)
      80                 :            : {
      81                 :            :         struct rq *rq;
      82                 :            : 
      83                 :            :         lockdep_assert_held(&p->pi_lock);
      84                 :            : 
      85                 :            :         for (;;) {
      86                 :          3 :                 rq = task_rq(p);
      87                 :          3 :                 raw_spin_lock(&rq->lock);
      88                 :          3 :                 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
      89                 :            :                         rq_pin_lock(rq, rf);
      90                 :          3 :                         return rq;
      91                 :            :                 }
      92                 :            :                 raw_spin_unlock(&rq->lock);
      93                 :            : 
      94                 :          0 :                 while (unlikely(task_on_rq_migrating(p)))
      95                 :          0 :                         cpu_relax();
      96                 :            :         }
      97                 :            : }
      98                 :            : 
      99                 :            : /*
     100                 :            :  * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
     101                 :            :  */
     102                 :          3 : struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
     103                 :            :         __acquires(p->pi_lock)
     104                 :            :         __acquires(rq->lock)
     105                 :            : {
     106                 :            :         struct rq *rq;
     107                 :            : 
     108                 :            :         for (;;) {
     109                 :          3 :                 raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
     110                 :          3 :                 rq = task_rq(p);
     111                 :          3 :                 raw_spin_lock(&rq->lock);
     112                 :            :                 /*
     113                 :            :                  *      move_queued_task()              task_rq_lock()
     114                 :            :                  *
     115                 :            :                  *      ACQUIRE (rq->lock)
     116                 :            :                  *      [S] ->on_rq = MIGRATING              [L] rq = task_rq()
     117                 :            :                  *      WMB (__set_task_cpu())          ACQUIRE (rq->lock);
     118                 :            :                  *      [S] ->cpu = new_cpu          [L] task_rq()
     119                 :            :                  *                                      [L] ->on_rq
     120                 :            :                  *      RELEASE (rq->lock)
     121                 :            :                  *
     122                 :            :                  * If we observe the old CPU in task_rq_lock(), the acquire of
     123                 :            :                  * the old rq->lock will fully serialize against the stores.
     124                 :            :                  *
     125                 :            :                  * If we observe the new CPU in task_rq_lock(), the address
     126                 :            :                  * dependency headed by '[L] rq = task_rq()' and the acquire
     127                 :            :                  * will pair with the WMB to ensure we then also see migrating.
     128                 :            :                  */
     129                 :          3 :                 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
     130                 :            :                         rq_pin_lock(rq, rf);
     131                 :          3 :                         return rq;
     132                 :            :                 }
     133                 :            :                 raw_spin_unlock(&rq->lock);
     134                 :          3 :                 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
     135                 :            : 
     136                 :          3 :                 while (unlikely(task_on_rq_migrating(p)))
     137                 :          3 :                         cpu_relax();
     138                 :            :         }
     139                 :            : }
     140                 :            : 
     141                 :            : /*
     142                 :            :  * RQ-clock updating methods:
     143                 :            :  */
     144                 :            : 
     145                 :            : static void update_rq_clock_task(struct rq *rq, s64 delta)
     146                 :            : {
     147                 :            : /*
     148                 :            :  * In theory, the compile should just see 0 here, and optimize out the call
     149                 :            :  * to sched_rt_avg_update. But I don't trust it...
     150                 :            :  */
     151                 :            :         s64 __maybe_unused steal = 0, irq_delta = 0;
     152                 :            : 
     153                 :            : #ifdef CONFIG_IRQ_TIME_ACCOUNTING
     154                 :            :         irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
     155                 :            : 
     156                 :            :         /*
     157                 :            :          * Since irq_time is only updated on {soft,}irq_exit, we might run into
     158                 :            :          * this case when a previous update_rq_clock() happened inside a
     159                 :            :          * {soft,}irq region.
     160                 :            :          *
     161                 :            :          * When this happens, we stop ->clock_task and only update the
     162                 :            :          * prev_irq_time stamp to account for the part that fit, so that a next
     163                 :            :          * update will consume the rest. This ensures ->clock_task is
     164                 :            :          * monotonic.
     165                 :            :          *
     166                 :            :          * It does however cause some slight miss-attribution of {soft,}irq
     167                 :            :          * time, a more accurate solution would be to update the irq_time using
     168                 :            :          * the current rq->clock timestamp, except that would require using
     169                 :            :          * atomic ops.
     170                 :            :          */
     171                 :            :         if (irq_delta > delta)
     172                 :            :                 irq_delta = delta;
     173                 :            : 
     174                 :            :         rq->prev_irq_time += irq_delta;
     175                 :            :         delta -= irq_delta;
     176                 :            : #endif
     177                 :            : #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
     178                 :            :         if (static_key_false((&paravirt_steal_rq_enabled))) {
     179                 :            :                 steal = paravirt_steal_clock(cpu_of(rq));
     180                 :            :                 steal -= rq->prev_steal_time_rq;
     181                 :            : 
     182                 :            :                 if (unlikely(steal > delta))
     183                 :            :                         steal = delta;
     184                 :            : 
     185                 :            :                 rq->prev_steal_time_rq += steal;
     186                 :            :                 delta -= steal;
     187                 :            :         }
     188                 :            : #endif
     189                 :            : 
     190                 :          3 :         rq->clock_task += delta;
     191                 :            : 
     192                 :            : #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
     193                 :            :         if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
     194                 :            :                 update_irq_load_avg(rq, irq_delta + steal);
     195                 :            : #endif
     196                 :          3 :         update_rq_clock_pelt(rq, delta);
     197                 :            : }
     198                 :            : 
     199                 :          3 : void update_rq_clock(struct rq *rq)
     200                 :            : {
     201                 :            :         s64 delta;
     202                 :            : 
     203                 :            :         lockdep_assert_held(&rq->lock);
     204                 :            : 
     205                 :          3 :         if (rq->clock_update_flags & RQCF_ACT_SKIP)
     206                 :            :                 return;
     207                 :            : 
     208                 :            : #ifdef CONFIG_SCHED_DEBUG
     209                 :          3 :         if (sched_feat(WARN_DOUBLE_CLOCK))
     210                 :          0 :                 SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED);
     211                 :          3 :         rq->clock_update_flags |= RQCF_UPDATED;
     212                 :            : #endif
     213                 :            : 
     214                 :          3 :         delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
     215                 :          3 :         if (delta < 0)
     216                 :            :                 return;
     217                 :          3 :         rq->clock += delta;
     218                 :            :         update_rq_clock_task(rq, delta);
     219                 :            : }
     220                 :            : 
     221                 :            : 
     222                 :            : #ifdef CONFIG_SCHED_HRTICK
     223                 :            : /*
     224                 :            :  * Use HR-timers to deliver accurate preemption points.
     225                 :            :  */
     226                 :            : 
     227                 :          0 : static void hrtick_clear(struct rq *rq)
     228                 :            : {
     229                 :          0 :         if (hrtimer_active(&rq->hrtick_timer))
     230                 :          0 :                 hrtimer_cancel(&rq->hrtick_timer);
     231                 :          0 : }
     232                 :            : 
     233                 :            : /*
     234                 :            :  * High-resolution timer tick.
     235                 :            :  * Runs from hardirq context with interrupts disabled.
     236                 :            :  */
     237                 :          0 : static enum hrtimer_restart hrtick(struct hrtimer *timer)
     238                 :            : {
     239                 :          0 :         struct rq *rq = container_of(timer, struct rq, hrtick_timer);
     240                 :            :         struct rq_flags rf;
     241                 :            : 
     242                 :          0 :         WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
     243                 :            : 
     244                 :            :         rq_lock(rq, &rf);
     245                 :          0 :         update_rq_clock(rq);
     246                 :          0 :         rq->curr->sched_class->task_tick(rq, rq->curr, 1);
     247                 :            :         rq_unlock(rq, &rf);
     248                 :            : 
     249                 :          0 :         return HRTIMER_NORESTART;
     250                 :            : }
     251                 :            : 
     252                 :            : #ifdef CONFIG_SMP
     253                 :            : 
     254                 :            : static void __hrtick_restart(struct rq *rq)
     255                 :            : {
     256                 :          0 :         struct hrtimer *timer = &rq->hrtick_timer;
     257                 :            : 
     258                 :          0 :         hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
     259                 :            : }
     260                 :            : 
     261                 :            : /*
     262                 :            :  * called from hardirq (IPI) context
     263                 :            :  */
     264                 :          0 : static void __hrtick_start(void *arg)
     265                 :            : {
     266                 :            :         struct rq *rq = arg;
     267                 :            :         struct rq_flags rf;
     268                 :            : 
     269                 :            :         rq_lock(rq, &rf);
     270                 :            :         __hrtick_restart(rq);
     271                 :          0 :         rq->hrtick_csd_pending = 0;
     272                 :            :         rq_unlock(rq, &rf);
     273                 :          0 : }
     274                 :            : 
     275                 :            : /*
     276                 :            :  * Called to set the hrtick timer state.
     277                 :            :  *
     278                 :            :  * called with rq->lock held and irqs disabled
     279                 :            :  */
     280                 :          0 : void hrtick_start(struct rq *rq, u64 delay)
     281                 :            : {
     282                 :            :         struct hrtimer *timer = &rq->hrtick_timer;
     283                 :            :         ktime_t time;
     284                 :            :         s64 delta;
     285                 :            : 
     286                 :            :         /*
     287                 :            :          * Don't schedule slices shorter than 10000ns, that just
     288                 :            :          * doesn't make sense and can cause timer DoS.
     289                 :            :          */
     290                 :          0 :         delta = max_t(s64, delay, 10000LL);
     291                 :          0 :         time = ktime_add_ns(timer->base->get_time(), delta);
     292                 :            : 
     293                 :            :         hrtimer_set_expires(timer, time);
     294                 :            : 
     295                 :          0 :         if (rq == this_rq()) {
     296                 :            :                 __hrtick_restart(rq);
     297                 :          0 :         } else if (!rq->hrtick_csd_pending) {
     298                 :          0 :                 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
     299                 :          0 :                 rq->hrtick_csd_pending = 1;
     300                 :            :         }
     301                 :          0 : }
     302                 :            : 
     303                 :            : #else
     304                 :            : /*
     305                 :            :  * Called to set the hrtick timer state.
     306                 :            :  *
     307                 :            :  * called with rq->lock held and irqs disabled
     308                 :            :  */
     309                 :            : void hrtick_start(struct rq *rq, u64 delay)
     310                 :            : {
     311                 :            :         /*
     312                 :            :          * Don't schedule slices shorter than 10000ns, that just
     313                 :            :          * doesn't make sense. Rely on vruntime for fairness.
     314                 :            :          */
     315                 :            :         delay = max_t(u64, delay, 10000LL);
     316                 :            :         hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
     317                 :            :                       HRTIMER_MODE_REL_PINNED_HARD);
     318                 :            : }
     319                 :            : #endif /* CONFIG_SMP */
     320                 :            : 
     321                 :            : static void hrtick_rq_init(struct rq *rq)
     322                 :            : {
     323                 :            : #ifdef CONFIG_SMP
     324                 :          3 :         rq->hrtick_csd_pending = 0;
     325                 :            : 
     326                 :          3 :         rq->hrtick_csd.flags = 0;
     327                 :          3 :         rq->hrtick_csd.func = __hrtick_start;
     328                 :          3 :         rq->hrtick_csd.info = rq;
     329                 :            : #endif
     330                 :            : 
     331                 :          3 :         hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
     332                 :          3 :         rq->hrtick_timer.function = hrtick;
     333                 :            : }
     334                 :            : #else   /* CONFIG_SCHED_HRTICK */
     335                 :            : static inline void hrtick_clear(struct rq *rq)
     336                 :            : {
     337                 :            : }
     338                 :            : 
     339                 :            : static inline void hrtick_rq_init(struct rq *rq)
     340                 :            : {
     341                 :            : }
     342                 :            : #endif  /* CONFIG_SCHED_HRTICK */
     343                 :            : 
     344                 :            : /*
     345                 :            :  * cmpxchg based fetch_or, macro so it works for different integer types
     346                 :            :  */
     347                 :            : #define fetch_or(ptr, mask)                                             \
     348                 :            :         ({                                                              \
     349                 :            :                 typeof(ptr) _ptr = (ptr);                               \
     350                 :            :                 typeof(mask) _mask = (mask);                            \
     351                 :            :                 typeof(*_ptr) _old, _val = *_ptr;                       \
     352                 :            :                                                                         \
     353                 :            :                 for (;;) {                                              \
     354                 :            :                         _old = cmpxchg(_ptr, _val, _val | _mask);       \
     355                 :            :                         if (_old == _val)                               \
     356                 :            :                                 break;                                  \
     357                 :            :                         _val = _old;                                    \
     358                 :            :                 }                                                       \
     359                 :            :         _old;                                                           \
     360                 :            : })
     361                 :            : 
     362                 :            : #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
     363                 :            : /*
     364                 :            :  * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
     365                 :            :  * this avoids any races wrt polling state changes and thereby avoids
     366                 :            :  * spurious IPIs.
     367                 :            :  */
     368                 :            : static bool set_nr_and_not_polling(struct task_struct *p)
     369                 :            : {
     370                 :            :         struct thread_info *ti = task_thread_info(p);
     371                 :            :         return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
     372                 :            : }
     373                 :            : 
     374                 :            : /*
     375                 :            :  * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
     376                 :            :  *
     377                 :            :  * If this returns true, then the idle task promises to call
     378                 :            :  * sched_ttwu_pending() and reschedule soon.
     379                 :            :  */
     380                 :            : static bool set_nr_if_polling(struct task_struct *p)
     381                 :            : {
     382                 :            :         struct thread_info *ti = task_thread_info(p);
     383                 :            :         typeof(ti->flags) old, val = READ_ONCE(ti->flags);
     384                 :            : 
     385                 :            :         for (;;) {
     386                 :            :                 if (!(val & _TIF_POLLING_NRFLAG))
     387                 :            :                         return false;
     388                 :            :                 if (val & _TIF_NEED_RESCHED)
     389                 :            :                         return true;
     390                 :            :                 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
     391                 :            :                 if (old == val)
     392                 :            :                         break;
     393                 :            :                 val = old;
     394                 :            :         }
     395                 :            :         return true;
     396                 :            : }
     397                 :            : 
     398                 :            : #else
     399                 :            : static bool set_nr_and_not_polling(struct task_struct *p)
     400                 :            : {
     401                 :            :         set_tsk_need_resched(p);
     402                 :            :         return true;
     403                 :            : }
     404                 :            : 
     405                 :            : #ifdef CONFIG_SMP
     406                 :            : static bool set_nr_if_polling(struct task_struct *p)
     407                 :            : {
     408                 :            :         return false;
     409                 :            : }
     410                 :            : #endif
     411                 :            : #endif
     412                 :            : 
     413                 :          3 : static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task)
     414                 :            : {
     415                 :          3 :         struct wake_q_node *node = &task->wake_q;
     416                 :            : 
     417                 :            :         /*
     418                 :            :          * Atomically grab the task, if ->wake_q is !nil already it means
     419                 :            :          * its already queued (either by us or someone else) and will get the
     420                 :            :          * wakeup due to that.
     421                 :            :          *
     422                 :            :          * In order to ensure that a pending wakeup will observe our pending
     423                 :            :          * state, even in the failed case, an explicit smp_mb() must be used.
     424                 :            :          */
     425                 :          3 :         smp_mb__before_atomic();
     426                 :          3 :         if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
     427                 :            :                 return false;
     428                 :            : 
     429                 :            :         /*
     430                 :            :          * The head is context local, there can be no concurrency.
     431                 :            :          */
     432                 :          3 :         *head->lastp = node;
     433                 :          3 :         head->lastp = &node->next;
     434                 :          3 :         return true;
     435                 :            : }
     436                 :            : 
     437                 :            : /**
     438                 :            :  * wake_q_add() - queue a wakeup for 'later' waking.
     439                 :            :  * @head: the wake_q_head to add @task to
     440                 :            :  * @task: the task to queue for 'later' wakeup
     441                 :            :  *
     442                 :            :  * Queue a task for later wakeup, most likely by the wake_up_q() call in the
     443                 :            :  * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
     444                 :            :  * instantly.
     445                 :            :  *
     446                 :            :  * This function must be used as-if it were wake_up_process(); IOW the task
     447                 :            :  * must be ready to be woken at this location.
     448                 :            :  */
     449                 :          3 : void wake_q_add(struct wake_q_head *head, struct task_struct *task)
     450                 :            : {
     451                 :          3 :         if (__wake_q_add(head, task))
     452                 :            :                 get_task_struct(task);
     453                 :          3 : }
     454                 :            : 
     455                 :            : /**
     456                 :            :  * wake_q_add_safe() - safely queue a wakeup for 'later' waking.
     457                 :            :  * @head: the wake_q_head to add @task to
     458                 :            :  * @task: the task to queue for 'later' wakeup
     459                 :            :  *
     460                 :            :  * Queue a task for later wakeup, most likely by the wake_up_q() call in the
     461                 :            :  * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
     462                 :            :  * instantly.
     463                 :            :  *
     464                 :            :  * This function must be used as-if it were wake_up_process(); IOW the task
     465                 :            :  * must be ready to be woken at this location.
     466                 :            :  *
     467                 :            :  * This function is essentially a task-safe equivalent to wake_q_add(). Callers
     468                 :            :  * that already hold reference to @task can call the 'safe' version and trust
     469                 :            :  * wake_q to do the right thing depending whether or not the @task is already
     470                 :            :  * queued for wakeup.
     471                 :            :  */
     472                 :          3 : void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)
     473                 :            : {
     474                 :          3 :         if (!__wake_q_add(head, task))
     475                 :          0 :                 put_task_struct(task);
     476                 :          3 : }
     477                 :            : 
     478                 :          3 : void wake_up_q(struct wake_q_head *head)
     479                 :            : {
     480                 :          3 :         struct wake_q_node *node = head->first;
     481                 :            : 
     482                 :          3 :         while (node != WAKE_Q_TAIL) {
     483                 :            :                 struct task_struct *task;
     484                 :            : 
     485                 :          3 :                 task = container_of(node, struct task_struct, wake_q);
     486                 :          3 :                 BUG_ON(!task);
     487                 :            :                 /* Task can safely be re-inserted now: */
     488                 :          3 :                 node = node->next;
     489                 :          3 :                 task->wake_q.next = NULL;
     490                 :            : 
     491                 :            :                 /*
     492                 :            :                  * wake_up_process() executes a full barrier, which pairs with
     493                 :            :                  * the queueing in wake_q_add() so as not to miss wakeups.
     494                 :            :                  */
     495                 :            :                 wake_up_process(task);
     496                 :          3 :                 put_task_struct(task);
     497                 :            :         }
     498                 :          3 : }
     499                 :            : 
     500                 :            : /*
     501                 :            :  * resched_curr - mark rq's current task 'to be rescheduled now'.
     502                 :            :  *
     503                 :            :  * On UP this means the setting of the need_resched flag, on SMP it
     504                 :            :  * might also involve a cross-CPU call to trigger the scheduler on
     505                 :            :  * the target CPU.
     506                 :            :  */
     507                 :          3 : void resched_curr(struct rq *rq)
     508                 :            : {
     509                 :          3 :         struct task_struct *curr = rq->curr;
     510                 :            :         int cpu;
     511                 :            : 
     512                 :            :         lockdep_assert_held(&rq->lock);
     513                 :            : 
     514                 :          3 :         if (test_tsk_need_resched(curr))
     515                 :            :                 return;
     516                 :            : 
     517                 :            :         cpu = cpu_of(rq);
     518                 :            : 
     519                 :          3 :         if (cpu == smp_processor_id()) {
     520                 :            :                 set_tsk_need_resched(curr);
     521                 :            :                 set_preempt_need_resched();
     522                 :            :                 return;
     523                 :            :         }
     524                 :            : 
     525                 :            :         if (set_nr_and_not_polling(curr))
     526                 :          3 :                 smp_send_reschedule(cpu);
     527                 :            :         else
     528                 :            :                 trace_sched_wake_idle_without_ipi(cpu);
     529                 :            : }
     530                 :            : 
     531                 :          0 : void resched_cpu(int cpu)
     532                 :            : {
     533                 :          0 :         struct rq *rq = cpu_rq(cpu);
     534                 :            :         unsigned long flags;
     535                 :            : 
     536                 :          0 :         raw_spin_lock_irqsave(&rq->lock, flags);
     537                 :          0 :         if (cpu_online(cpu) || cpu == smp_processor_id())
     538                 :          0 :                 resched_curr(rq);
     539                 :          0 :         raw_spin_unlock_irqrestore(&rq->lock, flags);
     540                 :          0 : }
     541                 :            : 
     542                 :            : #ifdef CONFIG_SMP
     543                 :            : #ifdef CONFIG_NO_HZ_COMMON
     544                 :            : /*
     545                 :            :  * In the semi idle case, use the nearest busy CPU for migrating timers
     546                 :            :  * from an idle CPU.  This is good for power-savings.
     547                 :            :  *
     548                 :            :  * We don't do similar optimization for completely idle system, as
     549                 :            :  * selecting an idle CPU will add more delays to the timers than intended
     550                 :            :  * (as that CPU's timer base may not be uptodate wrt jiffies etc).
     551                 :            :  */
     552                 :          3 : int get_nohz_timer_target(void)
     553                 :            : {
     554                 :          3 :         int i, cpu = smp_processor_id();
     555                 :            :         struct sched_domain *sd;
     556                 :            : 
     557                 :          3 :         if (!idle_cpu(cpu) && housekeeping_cpu(cpu, HK_FLAG_TIMER))
     558                 :            :                 return cpu;
     559                 :            : 
     560                 :            :         rcu_read_lock();
     561                 :          3 :         for_each_domain(cpu, sd) {
     562                 :          3 :                 for_each_cpu(i, sched_domain_span(sd)) {
     563                 :          3 :                         if (cpu == i)
     564                 :          3 :                                 continue;
     565                 :            : 
     566                 :          3 :                         if (!idle_cpu(i) && housekeeping_cpu(i, HK_FLAG_TIMER)) {
     567                 :          3 :                                 cpu = i;
     568                 :          3 :                                 goto unlock;
     569                 :            :                         }
     570                 :            :                 }
     571                 :            :         }
     572                 :            : 
     573                 :          3 :         if (!housekeeping_cpu(cpu, HK_FLAG_TIMER))
     574                 :          0 :                 cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
     575                 :            : unlock:
     576                 :            :         rcu_read_unlock();
     577                 :          3 :         return cpu;
     578                 :            : }
     579                 :            : 
     580                 :            : /*
     581                 :            :  * When add_timer_on() enqueues a timer into the timer wheel of an
     582                 :            :  * idle CPU then this timer might expire before the next timer event
     583                 :            :  * which is scheduled to wake up that CPU. In case of a completely
     584                 :            :  * idle system the next event might even be infinite time into the
     585                 :            :  * future. wake_up_idle_cpu() ensures that the CPU is woken up and
     586                 :            :  * leaves the inner idle loop so the newly added timer is taken into
     587                 :            :  * account when the CPU goes back to idle and evaluates the timer
     588                 :            :  * wheel for the next timer event.
     589                 :            :  */
     590                 :          3 : static void wake_up_idle_cpu(int cpu)
     591                 :            : {
     592                 :          3 :         struct rq *rq = cpu_rq(cpu);
     593                 :            : 
     594                 :          3 :         if (cpu == smp_processor_id())
     595                 :          3 :                 return;
     596                 :            : 
     597                 :          1 :         if (set_nr_and_not_polling(rq->idle))
     598                 :          1 :                 smp_send_reschedule(cpu);
     599                 :            :         else
     600                 :            :                 trace_sched_wake_idle_without_ipi(cpu);
     601                 :            : }
     602                 :            : 
     603                 :            : static bool wake_up_full_nohz_cpu(int cpu)
     604                 :            : {
     605                 :            :         /*
     606                 :            :          * We just need the target to call irq_exit() and re-evaluate
     607                 :            :          * the next tick. The nohz full kick at least implies that.
     608                 :            :          * If needed we can still optimize that later with an
     609                 :            :          * empty IRQ.
     610                 :            :          */
     611                 :          3 :         if (cpu_is_offline(cpu))
     612                 :            :                 return true;  /* Don't try to wake offline CPUs. */
     613                 :            :         if (tick_nohz_full_cpu(cpu)) {
     614                 :            :                 if (cpu != smp_processor_id() ||
     615                 :            :                     tick_nohz_tick_stopped())
     616                 :            :                         tick_nohz_full_kick_cpu(cpu);
     617                 :            :                 return true;
     618                 :            :         }
     619                 :            : 
     620                 :            :         return false;
     621                 :            : }
     622                 :            : 
     623                 :            : /*
     624                 :            :  * Wake up the specified CPU.  If the CPU is going offline, it is the
     625                 :            :  * caller's responsibility to deal with the lost wakeup, for example,
     626                 :            :  * by hooking into the CPU_DEAD notifier like timers and hrtimers do.
     627                 :            :  */
     628                 :          3 : void wake_up_nohz_cpu(int cpu)
     629                 :            : {
     630                 :          3 :         if (!wake_up_full_nohz_cpu(cpu))
     631                 :          3 :                 wake_up_idle_cpu(cpu);
     632                 :          3 : }
     633                 :            : 
     634                 :          3 : static inline bool got_nohz_idle_kick(void)
     635                 :            : {
     636                 :          3 :         int cpu = smp_processor_id();
     637                 :            : 
     638                 :          3 :         if (!(atomic_read(nohz_flags(cpu)) & NOHZ_KICK_MASK))
     639                 :            :                 return false;
     640                 :            : 
     641                 :          3 :         if (idle_cpu(cpu) && !need_resched())
     642                 :            :                 return true;
     643                 :            : 
     644                 :            :         /*
     645                 :            :          * We can't run Idle Load Balance on this CPU for this time so we
     646                 :            :          * cancel it and clear NOHZ_BALANCE_KICK
     647                 :            :          */
     648                 :          3 :         atomic_andnot(NOHZ_KICK_MASK, nohz_flags(cpu));
     649                 :          3 :         return false;
     650                 :            : }
     651                 :            : 
     652                 :            : #else /* CONFIG_NO_HZ_COMMON */
     653                 :            : 
     654                 :            : static inline bool got_nohz_idle_kick(void)
     655                 :            : {
     656                 :            :         return false;
     657                 :            : }
     658                 :            : 
     659                 :            : #endif /* CONFIG_NO_HZ_COMMON */
     660                 :            : 
     661                 :            : #ifdef CONFIG_NO_HZ_FULL
     662                 :            : bool sched_can_stop_tick(struct rq *rq)
     663                 :            : {
     664                 :            :         int fifo_nr_running;
     665                 :            : 
     666                 :            :         /* Deadline tasks, even if single, need the tick */
     667                 :            :         if (rq->dl.dl_nr_running)
     668                 :            :                 return false;
     669                 :            : 
     670                 :            :         /*
     671                 :            :          * If there are more than one RR tasks, we need the tick to effect the
     672                 :            :          * actual RR behaviour.
     673                 :            :          */
     674                 :            :         if (rq->rt.rr_nr_running) {
     675                 :            :                 if (rq->rt.rr_nr_running == 1)
     676                 :            :                         return true;
     677                 :            :                 else
     678                 :            :                         return false;
     679                 :            :         }
     680                 :            : 
     681                 :            :         /*
     682                 :            :          * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
     683                 :            :          * forced preemption between FIFO tasks.
     684                 :            :          */
     685                 :            :         fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
     686                 :            :         if (fifo_nr_running)
     687                 :            :                 return true;
     688                 :            : 
     689                 :            :         /*
     690                 :            :          * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left;
     691                 :            :          * if there's more than one we need the tick for involuntary
     692                 :            :          * preemption.
     693                 :            :          */
     694                 :            :         if (rq->nr_running > 1)
     695                 :            :                 return false;
     696                 :            : 
     697                 :            :         return true;
     698                 :            : }
     699                 :            : #endif /* CONFIG_NO_HZ_FULL */
     700                 :            : #endif /* CONFIG_SMP */
     701                 :            : 
     702                 :            : #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
     703                 :            :                         (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
     704                 :            : /*
     705                 :            :  * Iterate task_group tree rooted at *from, calling @down when first entering a
     706                 :            :  * node and @up when leaving it for the final time.
     707                 :            :  *
     708                 :            :  * Caller must hold rcu_lock or sufficient equivalent.
     709                 :            :  */
     710                 :          0 : int walk_tg_tree_from(struct task_group *from,
     711                 :            :                              tg_visitor down, tg_visitor up, void *data)
     712                 :            : {
     713                 :            :         struct task_group *parent, *child;
     714                 :            :         int ret;
     715                 :            : 
     716                 :            :         parent = from;
     717                 :            : 
     718                 :            : down:
     719                 :          0 :         ret = (*down)(parent, data);
     720                 :          0 :         if (ret)
     721                 :            :                 goto out;
     722                 :          0 :         list_for_each_entry_rcu(child, &parent->children, siblings) {
     723                 :          0 :                 parent = child;
     724                 :          0 :                 goto down;
     725                 :            : 
     726                 :            : up:
     727                 :          0 :                 continue;
     728                 :            :         }
     729                 :          0 :         ret = (*up)(parent, data);
     730                 :          0 :         if (ret || parent == from)
     731                 :            :                 goto out;
     732                 :            : 
     733                 :            :         child = parent;
     734                 :          0 :         parent = parent->parent;
     735                 :          0 :         if (parent)
     736                 :            :                 goto up;
     737                 :            : out:
     738                 :          0 :         return ret;
     739                 :            : }
     740                 :            : 
     741                 :          0 : int tg_nop(struct task_group *tg, void *data)
     742                 :            : {
     743                 :          0 :         return 0;
     744                 :            : }
     745                 :            : #endif
     746                 :            : 
     747                 :          3 : static void set_load_weight(struct task_struct *p, bool update_load)
     748                 :            : {
     749                 :          3 :         int prio = p->static_prio - MAX_RT_PRIO;
     750                 :            :         struct load_weight *load = &p->se.load;
     751                 :            : 
     752                 :            :         /*
     753                 :            :          * SCHED_IDLE tasks get minimal weight:
     754                 :            :          */
     755                 :          3 :         if (task_has_idle_policy(p)) {
     756                 :          0 :                 load->weight = scale_load(WEIGHT_IDLEPRIO);
     757                 :          0 :                 load->inv_weight = WMULT_IDLEPRIO;
     758                 :          0 :                 p->se.runnable_weight = load->weight;
     759                 :          3 :                 return;
     760                 :            :         }
     761                 :            : 
     762                 :            :         /*
     763                 :            :          * SCHED_OTHER tasks have to update their load when changing their
     764                 :            :          * weight
     765                 :            :          */
     766                 :          3 :         if (update_load && p->sched_class == &fair_sched_class) {
     767                 :          3 :                 reweight_task(p, prio);
     768                 :            :         } else {
     769                 :          3 :                 load->weight = scale_load(sched_prio_to_weight[prio]);
     770                 :          3 :                 load->inv_weight = sched_prio_to_wmult[prio];
     771                 :          3 :                 p->se.runnable_weight = load->weight;
     772                 :            :         }
     773                 :            : }
     774                 :            : 
     775                 :            : #ifdef CONFIG_UCLAMP_TASK
     776                 :            : /*
     777                 :            :  * Serializes updates of utilization clamp values
     778                 :            :  *
     779                 :            :  * The (slow-path) user-space triggers utilization clamp value updates which
     780                 :            :  * can require updates on (fast-path) scheduler's data structures used to
     781                 :            :  * support enqueue/dequeue operations.
     782                 :            :  * While the per-CPU rq lock protects fast-path update operations, user-space
     783                 :            :  * requests are serialized using a mutex to reduce the risk of conflicting
     784                 :            :  * updates or API abuses.
     785                 :            :  */
     786                 :            : static DEFINE_MUTEX(uclamp_mutex);
     787                 :            : 
     788                 :            : /* Max allowed minimum utilization */
     789                 :            : unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE;
     790                 :            : 
     791                 :            : /* Max allowed maximum utilization */
     792                 :            : unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE;
     793                 :            : 
     794                 :            : /* All clamps are required to be less or equal than these values */
     795                 :            : static struct uclamp_se uclamp_default[UCLAMP_CNT];
     796                 :            : 
     797                 :            : /* Integer rounded range for each bucket */
     798                 :            : #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
     799                 :            : 
     800                 :            : #define for_each_clamp_id(clamp_id) \
     801                 :            :         for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
     802                 :            : 
     803                 :            : static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
     804                 :            : {
     805                 :            :         return clamp_value / UCLAMP_BUCKET_DELTA;
     806                 :            : }
     807                 :            : 
     808                 :            : static inline unsigned int uclamp_bucket_base_value(unsigned int clamp_value)
     809                 :            : {
     810                 :            :         return UCLAMP_BUCKET_DELTA * uclamp_bucket_id(clamp_value);
     811                 :            : }
     812                 :            : 
     813                 :            : static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
     814                 :            : {
     815                 :            :         if (clamp_id == UCLAMP_MIN)
     816                 :            :                 return 0;
     817                 :            :         return SCHED_CAPACITY_SCALE;
     818                 :            : }
     819                 :            : 
     820                 :            : static inline void uclamp_se_set(struct uclamp_se *uc_se,
     821                 :            :                                  unsigned int value, bool user_defined)
     822                 :            : {
     823                 :            :         uc_se->value = value;
     824                 :            :         uc_se->bucket_id = uclamp_bucket_id(value);
     825                 :            :         uc_se->user_defined = user_defined;
     826                 :            : }
     827                 :            : 
     828                 :            : static inline unsigned int
     829                 :            : uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id,
     830                 :            :                   unsigned int clamp_value)
     831                 :            : {
     832                 :            :         /*
     833                 :            :          * Avoid blocked utilization pushing up the frequency when we go
     834                 :            :          * idle (which drops the max-clamp) by retaining the last known
     835                 :            :          * max-clamp.
     836                 :            :          */
     837                 :            :         if (clamp_id == UCLAMP_MAX) {
     838                 :            :                 rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
     839                 :            :                 return clamp_value;
     840                 :            :         }
     841                 :            : 
     842                 :            :         return uclamp_none(UCLAMP_MIN);
     843                 :            : }
     844                 :            : 
     845                 :            : static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
     846                 :            :                                      unsigned int clamp_value)
     847                 :            : {
     848                 :            :         /* Reset max-clamp retention only on idle exit */
     849                 :            :         if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
     850                 :            :                 return;
     851                 :            : 
     852                 :            :         WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value);
     853                 :            : }
     854                 :            : 
     855                 :            : static inline
     856                 :            : unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id,
     857                 :            :                                    unsigned int clamp_value)
     858                 :            : {
     859                 :            :         struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket;
     860                 :            :         int bucket_id = UCLAMP_BUCKETS - 1;
     861                 :            : 
     862                 :            :         /*
     863                 :            :          * Since both min and max clamps are max aggregated, find the
     864                 :            :          * top most bucket with tasks in.
     865                 :            :          */
     866                 :            :         for ( ; bucket_id >= 0; bucket_id--) {
     867                 :            :                 if (!bucket[bucket_id].tasks)
     868                 :            :                         continue;
     869                 :            :                 return bucket[bucket_id].value;
     870                 :            :         }
     871                 :            : 
     872                 :            :         /* No tasks -- default clamp values */
     873                 :            :         return uclamp_idle_value(rq, clamp_id, clamp_value);
     874                 :            : }
     875                 :            : 
     876                 :            : static inline struct uclamp_se
     877                 :            : uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
     878                 :            : {
     879                 :            :         struct uclamp_se uc_req = p->uclamp_req[clamp_id];
     880                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
     881                 :            :         struct uclamp_se uc_max;
     882                 :            : 
     883                 :            :         /*
     884                 :            :          * Tasks in autogroups or root task group will be
     885                 :            :          * restricted by system defaults.
     886                 :            :          */
     887                 :            :         if (task_group_is_autogroup(task_group(p)))
     888                 :            :                 return uc_req;
     889                 :            :         if (task_group(p) == &root_task_group)
     890                 :            :                 return uc_req;
     891                 :            : 
     892                 :            :         uc_max = task_group(p)->uclamp[clamp_id];
     893                 :            :         if (uc_req.value > uc_max.value || !uc_req.user_defined)
     894                 :            :                 return uc_max;
     895                 :            : #endif
     896                 :            : 
     897                 :            :         return uc_req;
     898                 :            : }
     899                 :            : 
     900                 :            : /*
     901                 :            :  * The effective clamp bucket index of a task depends on, by increasing
     902                 :            :  * priority:
     903                 :            :  * - the task specific clamp value, when explicitly requested from userspace
     904                 :            :  * - the task group effective clamp value, for tasks not either in the root
     905                 :            :  *   group or in an autogroup
     906                 :            :  * - the system default clamp value, defined by the sysadmin
     907                 :            :  */
     908                 :            : static inline struct uclamp_se
     909                 :            : uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
     910                 :            : {
     911                 :            :         struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id);
     912                 :            :         struct uclamp_se uc_max = uclamp_default[clamp_id];
     913                 :            : 
     914                 :            :         /* System default restrictions always apply */
     915                 :            :         if (unlikely(uc_req.value > uc_max.value))
     916                 :            :                 return uc_max;
     917                 :            : 
     918                 :            :         return uc_req;
     919                 :            : }
     920                 :            : 
     921                 :            : unsigned int uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
     922                 :            : {
     923                 :            :         struct uclamp_se uc_eff;
     924                 :            : 
     925                 :            :         /* Task currently refcounted: use back-annotated (effective) value */
     926                 :            :         if (p->uclamp[clamp_id].active)
     927                 :            :                 return p->uclamp[clamp_id].value;
     928                 :            : 
     929                 :            :         uc_eff = uclamp_eff_get(p, clamp_id);
     930                 :            : 
     931                 :            :         return uc_eff.value;
     932                 :            : }
     933                 :            : 
     934                 :            : /*
     935                 :            :  * When a task is enqueued on a rq, the clamp bucket currently defined by the
     936                 :            :  * task's uclamp::bucket_id is refcounted on that rq. This also immediately
     937                 :            :  * updates the rq's clamp value if required.
     938                 :            :  *
     939                 :            :  * Tasks can have a task-specific value requested from user-space, track
     940                 :            :  * within each bucket the maximum value for tasks refcounted in it.
     941                 :            :  * This "local max aggregation" allows to track the exact "requested" value
     942                 :            :  * for each bucket when all its RUNNABLE tasks require the same clamp.
     943                 :            :  */
     944                 :            : static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p,
     945                 :            :                                     enum uclamp_id clamp_id)
     946                 :            : {
     947                 :            :         struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
     948                 :            :         struct uclamp_se *uc_se = &p->uclamp[clamp_id];
     949                 :            :         struct uclamp_bucket *bucket;
     950                 :            : 
     951                 :            :         lockdep_assert_held(&rq->lock);
     952                 :            : 
     953                 :            :         /* Update task effective clamp */
     954                 :            :         p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id);
     955                 :            : 
     956                 :            :         bucket = &uc_rq->bucket[uc_se->bucket_id];
     957                 :            :         bucket->tasks++;
     958                 :            :         uc_se->active = true;
     959                 :            : 
     960                 :            :         uclamp_idle_reset(rq, clamp_id, uc_se->value);
     961                 :            : 
     962                 :            :         /*
     963                 :            :          * Local max aggregation: rq buckets always track the max
     964                 :            :          * "requested" clamp value of its RUNNABLE tasks.
     965                 :            :          */
     966                 :            :         if (bucket->tasks == 1 || uc_se->value > bucket->value)
     967                 :            :                 bucket->value = uc_se->value;
     968                 :            : 
     969                 :            :         if (uc_se->value > READ_ONCE(uc_rq->value))
     970                 :            :                 WRITE_ONCE(uc_rq->value, uc_se->value);
     971                 :            : }
     972                 :            : 
     973                 :            : /*
     974                 :            :  * When a task is dequeued from a rq, the clamp bucket refcounted by the task
     975                 :            :  * is released. If this is the last task reference counting the rq's max
     976                 :            :  * active clamp value, then the rq's clamp value is updated.
     977                 :            :  *
     978                 :            :  * Both refcounted tasks and rq's cached clamp values are expected to be
     979                 :            :  * always valid. If it's detected they are not, as defensive programming,
     980                 :            :  * enforce the expected state and warn.
     981                 :            :  */
     982                 :            : static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
     983                 :            :                                     enum uclamp_id clamp_id)
     984                 :            : {
     985                 :            :         struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
     986                 :            :         struct uclamp_se *uc_se = &p->uclamp[clamp_id];
     987                 :            :         struct uclamp_bucket *bucket;
     988                 :            :         unsigned int bkt_clamp;
     989                 :            :         unsigned int rq_clamp;
     990                 :            : 
     991                 :            :         lockdep_assert_held(&rq->lock);
     992                 :            : 
     993                 :            :         bucket = &uc_rq->bucket[uc_se->bucket_id];
     994                 :            :         SCHED_WARN_ON(!bucket->tasks);
     995                 :            :         if (likely(bucket->tasks))
     996                 :            :                 bucket->tasks--;
     997                 :            :         uc_se->active = false;
     998                 :            : 
     999                 :            :         /*
    1000                 :            :          * Keep "local max aggregation" simple and accept to (possibly)
    1001                 :            :          * overboost some RUNNABLE tasks in the same bucket.
    1002                 :            :          * The rq clamp bucket value is reset to its base value whenever
    1003                 :            :          * there are no more RUNNABLE tasks refcounting it.
    1004                 :            :          */
    1005                 :            :         if (likely(bucket->tasks))
    1006                 :            :                 return;
    1007                 :            : 
    1008                 :            :         rq_clamp = READ_ONCE(uc_rq->value);
    1009                 :            :         /*
    1010                 :            :          * Defensive programming: this should never happen. If it happens,
    1011                 :            :          * e.g. due to future modification, warn and fixup the expected value.
    1012                 :            :          */
    1013                 :            :         SCHED_WARN_ON(bucket->value > rq_clamp);
    1014                 :            :         if (bucket->value >= rq_clamp) {
    1015                 :            :                 bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value);
    1016                 :            :                 WRITE_ONCE(uc_rq->value, bkt_clamp);
    1017                 :            :         }
    1018                 :            : }
    1019                 :            : 
    1020                 :            : static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
    1021                 :            : {
    1022                 :            :         enum uclamp_id clamp_id;
    1023                 :            : 
    1024                 :            :         if (unlikely(!p->sched_class->uclamp_enabled))
    1025                 :            :                 return;
    1026                 :            : 
    1027                 :            :         for_each_clamp_id(clamp_id)
    1028                 :            :                 uclamp_rq_inc_id(rq, p, clamp_id);
    1029                 :            : 
    1030                 :            :         /* Reset clamp idle holding when there is one RUNNABLE task */
    1031                 :            :         if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
    1032                 :            :                 rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
    1033                 :            : }
    1034                 :            : 
    1035                 :            : static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
    1036                 :            : {
    1037                 :            :         enum uclamp_id clamp_id;
    1038                 :            : 
    1039                 :            :         if (unlikely(!p->sched_class->uclamp_enabled))
    1040                 :            :                 return;
    1041                 :            : 
    1042                 :            :         for_each_clamp_id(clamp_id)
    1043                 :            :                 uclamp_rq_dec_id(rq, p, clamp_id);
    1044                 :            : }
    1045                 :            : 
    1046                 :            : static inline void
    1047                 :            : uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id)
    1048                 :            : {
    1049                 :            :         struct rq_flags rf;
    1050                 :            :         struct rq *rq;
    1051                 :            : 
    1052                 :            :         /*
    1053                 :            :          * Lock the task and the rq where the task is (or was) queued.
    1054                 :            :          *
    1055                 :            :          * We might lock the (previous) rq of a !RUNNABLE task, but that's the
    1056                 :            :          * price to pay to safely serialize util_{min,max} updates with
    1057                 :            :          * enqueues, dequeues and migration operations.
    1058                 :            :          * This is the same locking schema used by __set_cpus_allowed_ptr().
    1059                 :            :          */
    1060                 :            :         rq = task_rq_lock(p, &rf);
    1061                 :            : 
    1062                 :            :         /*
    1063                 :            :          * Setting the clamp bucket is serialized by task_rq_lock().
    1064                 :            :          * If the task is not yet RUNNABLE and its task_struct is not
    1065                 :            :          * affecting a valid clamp bucket, the next time it's enqueued,
    1066                 :            :          * it will already see the updated clamp bucket value.
    1067                 :            :          */
    1068                 :            :         if (p->uclamp[clamp_id].active) {
    1069                 :            :                 uclamp_rq_dec_id(rq, p, clamp_id);
    1070                 :            :                 uclamp_rq_inc_id(rq, p, clamp_id);
    1071                 :            :         }
    1072                 :            : 
    1073                 :            :         task_rq_unlock(rq, p, &rf);
    1074                 :            : }
    1075                 :            : 
    1076                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    1077                 :            : static inline void
    1078                 :            : uclamp_update_active_tasks(struct cgroup_subsys_state *css,
    1079                 :            :                            unsigned int clamps)
    1080                 :            : {
    1081                 :            :         enum uclamp_id clamp_id;
    1082                 :            :         struct css_task_iter it;
    1083                 :            :         struct task_struct *p;
    1084                 :            : 
    1085                 :            :         css_task_iter_start(css, 0, &it);
    1086                 :            :         while ((p = css_task_iter_next(&it))) {
    1087                 :            :                 for_each_clamp_id(clamp_id) {
    1088                 :            :                         if ((0x1 << clamp_id) & clamps)
    1089                 :            :                                 uclamp_update_active(p, clamp_id);
    1090                 :            :                 }
    1091                 :            :         }
    1092                 :            :         css_task_iter_end(&it);
    1093                 :            : }
    1094                 :            : 
    1095                 :            : static void cpu_util_update_eff(struct cgroup_subsys_state *css);
    1096                 :            : static void uclamp_update_root_tg(void)
    1097                 :            : {
    1098                 :            :         struct task_group *tg = &root_task_group;
    1099                 :            : 
    1100                 :            :         uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN],
    1101                 :            :                       sysctl_sched_uclamp_util_min, false);
    1102                 :            :         uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX],
    1103                 :            :                       sysctl_sched_uclamp_util_max, false);
    1104                 :            : 
    1105                 :            :         rcu_read_lock();
    1106                 :            :         cpu_util_update_eff(&root_task_group.css);
    1107                 :            :         rcu_read_unlock();
    1108                 :            : }
    1109                 :            : #else
    1110                 :            : static void uclamp_update_root_tg(void) { }
    1111                 :            : #endif
    1112                 :            : 
    1113                 :            : int sysctl_sched_uclamp_handler(struct ctl_table *table, int write,
    1114                 :            :                                 void __user *buffer, size_t *lenp,
    1115                 :            :                                 loff_t *ppos)
    1116                 :            : {
    1117                 :            :         bool update_root_tg = false;
    1118                 :            :         int old_min, old_max;
    1119                 :            :         int result;
    1120                 :            : 
    1121                 :            :         mutex_lock(&uclamp_mutex);
    1122                 :            :         old_min = sysctl_sched_uclamp_util_min;
    1123                 :            :         old_max = sysctl_sched_uclamp_util_max;
    1124                 :            : 
    1125                 :            :         result = proc_dointvec(table, write, buffer, lenp, ppos);
    1126                 :            :         if (result)
    1127                 :            :                 goto undo;
    1128                 :            :         if (!write)
    1129                 :            :                 goto done;
    1130                 :            : 
    1131                 :            :         if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max ||
    1132                 :            :             sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE) {
    1133                 :            :                 result = -EINVAL;
    1134                 :            :                 goto undo;
    1135                 :            :         }
    1136                 :            : 
    1137                 :            :         if (old_min != sysctl_sched_uclamp_util_min) {
    1138                 :            :                 uclamp_se_set(&uclamp_default[UCLAMP_MIN],
    1139                 :            :                               sysctl_sched_uclamp_util_min, false);
    1140                 :            :                 update_root_tg = true;
    1141                 :            :         }
    1142                 :            :         if (old_max != sysctl_sched_uclamp_util_max) {
    1143                 :            :                 uclamp_se_set(&uclamp_default[UCLAMP_MAX],
    1144                 :            :                               sysctl_sched_uclamp_util_max, false);
    1145                 :            :                 update_root_tg = true;
    1146                 :            :         }
    1147                 :            : 
    1148                 :            :         if (update_root_tg)
    1149                 :            :                 uclamp_update_root_tg();
    1150                 :            : 
    1151                 :            :         /*
    1152                 :            :          * We update all RUNNABLE tasks only when task groups are in use.
    1153                 :            :          * Otherwise, keep it simple and do just a lazy update at each next
    1154                 :            :          * task enqueue time.
    1155                 :            :          */
    1156                 :            : 
    1157                 :            :         goto done;
    1158                 :            : 
    1159                 :            : undo:
    1160                 :            :         sysctl_sched_uclamp_util_min = old_min;
    1161                 :            :         sysctl_sched_uclamp_util_max = old_max;
    1162                 :            : done:
    1163                 :            :         mutex_unlock(&uclamp_mutex);
    1164                 :            : 
    1165                 :            :         return result;
    1166                 :            : }
    1167                 :            : 
    1168                 :            : static int uclamp_validate(struct task_struct *p,
    1169                 :            :                            const struct sched_attr *attr)
    1170                 :            : {
    1171                 :            :         unsigned int lower_bound = p->uclamp_req[UCLAMP_MIN].value;
    1172                 :            :         unsigned int upper_bound = p->uclamp_req[UCLAMP_MAX].value;
    1173                 :            : 
    1174                 :            :         if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
    1175                 :            :                 lower_bound = attr->sched_util_min;
    1176                 :            :         if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX)
    1177                 :            :                 upper_bound = attr->sched_util_max;
    1178                 :            : 
    1179                 :            :         if (lower_bound > upper_bound)
    1180                 :            :                 return -EINVAL;
    1181                 :            :         if (upper_bound > SCHED_CAPACITY_SCALE)
    1182                 :            :                 return -EINVAL;
    1183                 :            : 
    1184                 :            :         return 0;
    1185                 :            : }
    1186                 :            : 
    1187                 :            : static void __setscheduler_uclamp(struct task_struct *p,
    1188                 :            :                                   const struct sched_attr *attr)
    1189                 :            : {
    1190                 :            :         enum uclamp_id clamp_id;
    1191                 :            : 
    1192                 :            :         /*
    1193                 :            :          * On scheduling class change, reset to default clamps for tasks
    1194                 :            :          * without a task-specific value.
    1195                 :            :          */
    1196                 :            :         for_each_clamp_id(clamp_id) {
    1197                 :            :                 struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
    1198                 :            :                 unsigned int clamp_value = uclamp_none(clamp_id);
    1199                 :            : 
    1200                 :            :                 /* Keep using defined clamps across class changes */
    1201                 :            :                 if (uc_se->user_defined)
    1202                 :            :                         continue;
    1203                 :            : 
    1204                 :            :                 /* By default, RT tasks always get 100% boost */
    1205                 :            :                 if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
    1206                 :            :                         clamp_value = uclamp_none(UCLAMP_MAX);
    1207                 :            : 
    1208                 :            :                 uclamp_se_set(uc_se, clamp_value, false);
    1209                 :            :         }
    1210                 :            : 
    1211                 :            :         if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
    1212                 :            :                 return;
    1213                 :            : 
    1214                 :            :         if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
    1215                 :            :                 uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
    1216                 :            :                               attr->sched_util_min, true);
    1217                 :            :         }
    1218                 :            : 
    1219                 :            :         if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
    1220                 :            :                 uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
    1221                 :            :                               attr->sched_util_max, true);
    1222                 :            :         }
    1223                 :            : }
    1224                 :            : 
    1225                 :            : static void uclamp_fork(struct task_struct *p)
    1226                 :            : {
    1227                 :            :         enum uclamp_id clamp_id;
    1228                 :            : 
    1229                 :            :         for_each_clamp_id(clamp_id)
    1230                 :            :                 p->uclamp[clamp_id].active = false;
    1231                 :            : 
    1232                 :            :         if (likely(!p->sched_reset_on_fork))
    1233                 :            :                 return;
    1234                 :            : 
    1235                 :            :         for_each_clamp_id(clamp_id) {
    1236                 :            :                 uclamp_se_set(&p->uclamp_req[clamp_id],
    1237                 :            :                               uclamp_none(clamp_id), false);
    1238                 :            :         }
    1239                 :            : }
    1240                 :            : 
    1241                 :            : static void __init init_uclamp_rq(struct rq *rq)
    1242                 :            : {
    1243                 :            :         enum uclamp_id clamp_id;
    1244                 :            :         struct uclamp_rq *uc_rq = rq->uclamp;
    1245                 :            : 
    1246                 :            :         for_each_clamp_id(clamp_id) {
    1247                 :            :                 uc_rq[clamp_id] = (struct uclamp_rq) {
    1248                 :            :                         .value = uclamp_none(clamp_id)
    1249                 :            :                 };
    1250                 :            :         }
    1251                 :            : 
    1252                 :            :         rq->uclamp_flags = 0;
    1253                 :            : }
    1254                 :            : 
    1255                 :            : static void __init init_uclamp(void)
    1256                 :            : {
    1257                 :            :         struct uclamp_se uc_max = {};
    1258                 :            :         enum uclamp_id clamp_id;
    1259                 :            :         int cpu;
    1260                 :            : 
    1261                 :            :         mutex_init(&uclamp_mutex);
    1262                 :            : 
    1263                 :            :         for_each_possible_cpu(cpu)
    1264                 :            :                 init_uclamp_rq(cpu_rq(cpu));
    1265                 :            : 
    1266                 :            :         for_each_clamp_id(clamp_id) {
    1267                 :            :                 uclamp_se_set(&init_task.uclamp_req[clamp_id],
    1268                 :            :                               uclamp_none(clamp_id), false);
    1269                 :            :         }
    1270                 :            : 
    1271                 :            :         /* System defaults allow max clamp values for both indexes */
    1272                 :            :         uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false);
    1273                 :            :         for_each_clamp_id(clamp_id) {
    1274                 :            :                 uclamp_default[clamp_id] = uc_max;
    1275                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    1276                 :            :                 root_task_group.uclamp_req[clamp_id] = uc_max;
    1277                 :            :                 root_task_group.uclamp[clamp_id] = uc_max;
    1278                 :            : #endif
    1279                 :            :         }
    1280                 :            : }
    1281                 :            : 
    1282                 :            : #else /* CONFIG_UCLAMP_TASK */
    1283                 :            : static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { }
    1284                 :            : static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
    1285                 :            : static inline int uclamp_validate(struct task_struct *p,
    1286                 :            :                                   const struct sched_attr *attr)
    1287                 :            : {
    1288                 :            :         return -EOPNOTSUPP;
    1289                 :            : }
    1290                 :            : static void __setscheduler_uclamp(struct task_struct *p,
    1291                 :            :                                   const struct sched_attr *attr) { }
    1292                 :            : static inline void uclamp_fork(struct task_struct *p) { }
    1293                 :            : static inline void init_uclamp(void) { }
    1294                 :            : #endif /* CONFIG_UCLAMP_TASK */
    1295                 :            : 
    1296                 :          3 : static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
    1297                 :            : {
    1298                 :          3 :         if (!(flags & ENQUEUE_NOCLOCK))
    1299                 :          3 :                 update_rq_clock(rq);
    1300                 :            : 
    1301                 :          3 :         if (!(flags & ENQUEUE_RESTORE)) {
    1302                 :            :                 sched_info_queued(rq, p);
    1303                 :            :                 psi_enqueue(p, flags & ENQUEUE_WAKEUP);
    1304                 :            :         }
    1305                 :            : 
    1306                 :            :         uclamp_rq_inc(rq, p);
    1307                 :          3 :         p->sched_class->enqueue_task(rq, p, flags);
    1308                 :          3 : }
    1309                 :            : 
    1310                 :          3 : static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
    1311                 :            : {
    1312                 :          3 :         if (!(flags & DEQUEUE_NOCLOCK))
    1313                 :          0 :                 update_rq_clock(rq);
    1314                 :            : 
    1315                 :          3 :         if (!(flags & DEQUEUE_SAVE)) {
    1316                 :          3 :                 sched_info_dequeued(rq, p);
    1317                 :            :                 psi_dequeue(p, flags & DEQUEUE_SLEEP);
    1318                 :            :         }
    1319                 :            : 
    1320                 :            :         uclamp_rq_dec(rq, p);
    1321                 :          3 :         p->sched_class->dequeue_task(rq, p, flags);
    1322                 :          3 : }
    1323                 :            : 
    1324                 :          3 : void activate_task(struct rq *rq, struct task_struct *p, int flags)
    1325                 :            : {
    1326                 :          3 :         if (task_contributes_to_load(p))
    1327                 :          0 :                 rq->nr_uninterruptible--;
    1328                 :            : 
    1329                 :          3 :         enqueue_task(rq, p, flags);
    1330                 :            : 
    1331                 :          3 :         p->on_rq = TASK_ON_RQ_QUEUED;
    1332                 :          3 : }
    1333                 :            : 
    1334                 :          3 : void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
    1335                 :            : {
    1336                 :          3 :         p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING;
    1337                 :            : 
    1338                 :          3 :         if (task_contributes_to_load(p))
    1339                 :          3 :                 rq->nr_uninterruptible++;
    1340                 :            : 
    1341                 :          3 :         dequeue_task(rq, p, flags);
    1342                 :          3 : }
    1343                 :            : 
    1344                 :            : /*
    1345                 :            :  * __normal_prio - return the priority that is based on the static prio
    1346                 :            :  */
    1347                 :            : static inline int __normal_prio(struct task_struct *p)
    1348                 :            : {
    1349                 :          3 :         return p->static_prio;
    1350                 :            : }
    1351                 :            : 
    1352                 :            : /*
    1353                 :            :  * Calculate the expected normal priority: i.e. priority
    1354                 :            :  * without taking RT-inheritance into account. Might be
    1355                 :            :  * boosted by interactivity modifiers. Changes upon fork,
    1356                 :            :  * setprio syscalls, and whenever the interactivity
    1357                 :            :  * estimator recalculates.
    1358                 :            :  */
    1359                 :            : static inline int normal_prio(struct task_struct *p)
    1360                 :            : {
    1361                 :            :         int prio;
    1362                 :            : 
    1363                 :          3 :         if (task_has_dl_policy(p))
    1364                 :            :                 prio = MAX_DL_PRIO-1;
    1365                 :          3 :         else if (task_has_rt_policy(p))
    1366                 :          3 :                 prio = MAX_RT_PRIO-1 - p->rt_priority;
    1367                 :            :         else
    1368                 :            :                 prio = __normal_prio(p);
    1369                 :            :         return prio;
    1370                 :            : }
    1371                 :            : 
    1372                 :            : /*
    1373                 :            :  * Calculate the current priority, i.e. the priority
    1374                 :            :  * taken into account by the scheduler. This value might
    1375                 :            :  * be boosted by RT tasks, or might be boosted by
    1376                 :            :  * interactivity modifiers. Will be RT if the task got
    1377                 :            :  * RT-boosted. If not then it returns p->normal_prio.
    1378                 :            :  */
    1379                 :            : static int effective_prio(struct task_struct *p)
    1380                 :            : {
    1381                 :          3 :         p->normal_prio = normal_prio(p);
    1382                 :            :         /*
    1383                 :            :          * If we are RT tasks or we were boosted to RT priority,
    1384                 :            :          * keep the priority unchanged. Otherwise, update priority
    1385                 :            :          * to the normal priority:
    1386                 :            :          */
    1387                 :          3 :         if (!rt_prio(p->prio))
    1388                 :            :                 return p->normal_prio;
    1389                 :            :         return p->prio;
    1390                 :            : }
    1391                 :            : 
    1392                 :            : /**
    1393                 :            :  * task_curr - is this task currently executing on a CPU?
    1394                 :            :  * @p: the task in question.
    1395                 :            :  *
    1396                 :            :  * Return: 1 if the task is currently executing. 0 otherwise.
    1397                 :            :  */
    1398                 :          3 : inline int task_curr(const struct task_struct *p)
    1399                 :            : {
    1400                 :          3 :         return cpu_curr(task_cpu(p)) == p;
    1401                 :            : }
    1402                 :            : 
    1403                 :            : /*
    1404                 :            :  * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
    1405                 :            :  * use the balance_callback list if you want balancing.
    1406                 :            :  *
    1407                 :            :  * this means any call to check_class_changed() must be followed by a call to
    1408                 :            :  * balance_callback().
    1409                 :            :  */
    1410                 :          3 : static inline void check_class_changed(struct rq *rq, struct task_struct *p,
    1411                 :            :                                        const struct sched_class *prev_class,
    1412                 :            :                                        int oldprio)
    1413                 :            : {
    1414                 :          3 :         if (prev_class != p->sched_class) {
    1415                 :          3 :                 if (prev_class->switched_from)
    1416                 :          3 :                         prev_class->switched_from(rq, p);
    1417                 :            : 
    1418                 :          3 :                 p->sched_class->switched_to(rq, p);
    1419                 :          0 :         } else if (oldprio != p->prio || dl_task(p))
    1420                 :          0 :                 p->sched_class->prio_changed(rq, p, oldprio);
    1421                 :          3 : }
    1422                 :            : 
    1423                 :          3 : void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
    1424                 :            : {
    1425                 :            :         const struct sched_class *class;
    1426                 :            : 
    1427                 :          3 :         if (p->sched_class == rq->curr->sched_class) {
    1428                 :          3 :                 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
    1429                 :            :         } else {
    1430                 :          3 :                 for_each_class(class) {
    1431                 :          3 :                         if (class == rq->curr->sched_class)
    1432                 :            :                                 break;
    1433                 :          3 :                         if (class == p->sched_class) {
    1434                 :          3 :                                 resched_curr(rq);
    1435                 :          3 :                                 break;
    1436                 :            :                         }
    1437                 :            :                 }
    1438                 :            :         }
    1439                 :            : 
    1440                 :            :         /*
    1441                 :            :          * A queue event has occurred, and we're going to schedule.  In
    1442                 :            :          * this case, we can save a useless back to back clock update.
    1443                 :            :          */
    1444                 :          3 :         if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
    1445                 :            :                 rq_clock_skip_update(rq);
    1446                 :          3 : }
    1447                 :            : 
    1448                 :            : #ifdef CONFIG_SMP
    1449                 :            : 
    1450                 :            : static inline bool is_per_cpu_kthread(struct task_struct *p)
    1451                 :            : {
    1452                 :          3 :         if (!(p->flags & PF_KTHREAD))
    1453                 :            :                 return false;
    1454                 :            : 
    1455                 :          3 :         if (p->nr_cpus_allowed != 1)
    1456                 :            :                 return false;
    1457                 :            : 
    1458                 :            :         return true;
    1459                 :            : }
    1460                 :            : 
    1461                 :            : /*
    1462                 :            :  * Per-CPU kthreads are allowed to run on !active && online CPUs, see
    1463                 :            :  * __set_cpus_allowed_ptr() and select_fallback_rq().
    1464                 :            :  */
    1465                 :          3 : static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
    1466                 :            : {
    1467                 :          3 :         if (!cpumask_test_cpu(cpu, p->cpus_ptr))
    1468                 :            :                 return false;
    1469                 :            : 
    1470                 :          3 :         if (is_per_cpu_kthread(p))
    1471                 :          3 :                 return cpu_online(cpu);
    1472                 :            : 
    1473                 :          3 :         return cpu_active(cpu);
    1474                 :            : }
    1475                 :            : 
    1476                 :            : /*
    1477                 :            :  * This is how migration works:
    1478                 :            :  *
    1479                 :            :  * 1) we invoke migration_cpu_stop() on the target CPU using
    1480                 :            :  *    stop_one_cpu().
    1481                 :            :  * 2) stopper starts to run (implicitly forcing the migrated thread
    1482                 :            :  *    off the CPU)
    1483                 :            :  * 3) it checks whether the migrated task is still in the wrong runqueue.
    1484                 :            :  * 4) if it's in the wrong runqueue then the migration thread removes
    1485                 :            :  *    it and puts it into the right queue.
    1486                 :            :  * 5) stopper completes and stop_one_cpu() returns and the migration
    1487                 :            :  *    is done.
    1488                 :            :  */
    1489                 :            : 
    1490                 :            : /*
    1491                 :            :  * move_queued_task - move a queued task to new rq.
    1492                 :            :  *
    1493                 :            :  * Returns (locked) new rq. Old rq's lock is released.
    1494                 :            :  */
    1495                 :          3 : static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
    1496                 :            :                                    struct task_struct *p, int new_cpu)
    1497                 :            : {
    1498                 :            :         lockdep_assert_held(&rq->lock);
    1499                 :            : 
    1500                 :            :         WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
    1501                 :          3 :         dequeue_task(rq, p, DEQUEUE_NOCLOCK);
    1502                 :          3 :         set_task_cpu(p, new_cpu);
    1503                 :            :         rq_unlock(rq, rf);
    1504                 :            : 
    1505                 :          3 :         rq = cpu_rq(new_cpu);
    1506                 :            : 
    1507                 :            :         rq_lock(rq, rf);
    1508                 :          3 :         BUG_ON(task_cpu(p) != new_cpu);
    1509                 :          3 :         enqueue_task(rq, p, 0);
    1510                 :          3 :         p->on_rq = TASK_ON_RQ_QUEUED;
    1511                 :          3 :         check_preempt_curr(rq, p, 0);
    1512                 :            : 
    1513                 :          3 :         return rq;
    1514                 :            : }
    1515                 :            : 
    1516                 :            : struct migration_arg {
    1517                 :            :         struct task_struct *task;
    1518                 :            :         int dest_cpu;
    1519                 :            : };
    1520                 :            : 
    1521                 :            : /*
    1522                 :            :  * Move (not current) task off this CPU, onto the destination CPU. We're doing
    1523                 :            :  * this because either it can't run here any more (set_cpus_allowed()
    1524                 :            :  * away from this CPU, or CPU going down), or because we're
    1525                 :            :  * attempting to rebalance this task on exec (sched_exec).
    1526                 :            :  *
    1527                 :            :  * So we race with normal scheduler movements, but that's OK, as long
    1528                 :            :  * as the task is no longer on this CPU.
    1529                 :            :  */
    1530                 :          3 : static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
    1531                 :            :                                  struct task_struct *p, int dest_cpu)
    1532                 :            : {
    1533                 :            :         /* Affinity changed (again). */
    1534                 :          3 :         if (!is_cpu_allowed(p, dest_cpu))
    1535                 :            :                 return rq;
    1536                 :            : 
    1537                 :          3 :         update_rq_clock(rq);
    1538                 :          3 :         rq = move_queued_task(rq, rf, p, dest_cpu);
    1539                 :            : 
    1540                 :          3 :         return rq;
    1541                 :            : }
    1542                 :            : 
    1543                 :            : /*
    1544                 :            :  * migration_cpu_stop - this will be executed by a highprio stopper thread
    1545                 :            :  * and performs thread migration by bumping thread off CPU then
    1546                 :            :  * 'pushing' onto another runqueue.
    1547                 :            :  */
    1548                 :          3 : static int migration_cpu_stop(void *data)
    1549                 :            : {
    1550                 :            :         struct migration_arg *arg = data;
    1551                 :          3 :         struct task_struct *p = arg->task;
    1552                 :          3 :         struct rq *rq = this_rq();
    1553                 :            :         struct rq_flags rf;
    1554                 :            : 
    1555                 :            :         /*
    1556                 :            :          * The original target CPU might have gone down and we might
    1557                 :            :          * be on another CPU but it doesn't matter.
    1558                 :            :          */
    1559                 :          3 :         local_irq_disable();
    1560                 :            :         /*
    1561                 :            :          * We need to explicitly wake pending tasks before running
    1562                 :            :          * __migrate_task() such that we will not miss enforcing cpus_ptr
    1563                 :            :          * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
    1564                 :            :          */
    1565                 :          3 :         sched_ttwu_pending();
    1566                 :            : 
    1567                 :          3 :         raw_spin_lock(&p->pi_lock);
    1568                 :            :         rq_lock(rq, &rf);
    1569                 :            :         /*
    1570                 :            :          * If task_rq(p) != rq, it cannot be migrated here, because we're
    1571                 :            :          * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
    1572                 :            :          * we're holding p->pi_lock.
    1573                 :            :          */
    1574                 :          3 :         if (task_rq(p) == rq) {
    1575                 :          3 :                 if (task_on_rq_queued(p))
    1576                 :          3 :                         rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
    1577                 :            :                 else
    1578                 :          0 :                         p->wake_cpu = arg->dest_cpu;
    1579                 :            :         }
    1580                 :            :         rq_unlock(rq, &rf);
    1581                 :            :         raw_spin_unlock(&p->pi_lock);
    1582                 :            : 
    1583                 :          3 :         local_irq_enable();
    1584                 :          3 :         return 0;
    1585                 :            : }
    1586                 :            : 
    1587                 :            : /*
    1588                 :            :  * sched_class::set_cpus_allowed must do the below, but is not required to
    1589                 :            :  * actually call this function.
    1590                 :            :  */
    1591                 :          3 : void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
    1592                 :            : {
    1593                 :            :         cpumask_copy(&p->cpus_mask, new_mask);
    1594                 :          3 :         p->nr_cpus_allowed = cpumask_weight(new_mask);
    1595                 :          3 : }
    1596                 :            : 
    1597                 :          3 : void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
    1598                 :            : {
    1599                 :          3 :         struct rq *rq = task_rq(p);
    1600                 :            :         bool queued, running;
    1601                 :            : 
    1602                 :            :         lockdep_assert_held(&p->pi_lock);
    1603                 :            : 
    1604                 :            :         queued = task_on_rq_queued(p);
    1605                 :            :         running = task_current(rq, p);
    1606                 :            : 
    1607                 :          3 :         if (queued) {
    1608                 :            :                 /*
    1609                 :            :                  * Because __kthread_bind() calls this on blocked tasks without
    1610                 :            :                  * holding rq->lock.
    1611                 :            :                  */
    1612                 :            :                 lockdep_assert_held(&rq->lock);
    1613                 :          3 :                 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
    1614                 :            :         }
    1615                 :          3 :         if (running)
    1616                 :          3 :                 put_prev_task(rq, p);
    1617                 :            : 
    1618                 :          3 :         p->sched_class->set_cpus_allowed(p, new_mask);
    1619                 :            : 
    1620                 :          3 :         if (queued)
    1621                 :          3 :                 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
    1622                 :          3 :         if (running)
    1623                 :          3 :                 set_next_task(rq, p);
    1624                 :          3 : }
    1625                 :            : 
    1626                 :            : /*
    1627                 :            :  * Change a given task's CPU affinity. Migrate the thread to a
    1628                 :            :  * proper CPU and schedule it away if the CPU it's executing on
    1629                 :            :  * is removed from the allowed bitmask.
    1630                 :            :  *
    1631                 :            :  * NOTE: the caller must have a valid reference to the task, the
    1632                 :            :  * task must not exit() & deallocate itself prematurely. The
    1633                 :            :  * call is not atomic; no spinlocks may be held.
    1634                 :            :  */
    1635                 :          3 : static int __set_cpus_allowed_ptr(struct task_struct *p,
    1636                 :            :                                   const struct cpumask *new_mask, bool check)
    1637                 :            : {
    1638                 :            :         const struct cpumask *cpu_valid_mask = cpu_active_mask;
    1639                 :            :         unsigned int dest_cpu;
    1640                 :            :         struct rq_flags rf;
    1641                 :            :         struct rq *rq;
    1642                 :            :         int ret = 0;
    1643                 :            : 
    1644                 :          3 :         rq = task_rq_lock(p, &rf);
    1645                 :          3 :         update_rq_clock(rq);
    1646                 :            : 
    1647                 :          3 :         if (p->flags & PF_KTHREAD) {
    1648                 :            :                 /*
    1649                 :            :                  * Kernel threads are allowed on online && !active CPUs
    1650                 :            :                  */
    1651                 :            :                 cpu_valid_mask = cpu_online_mask;
    1652                 :            :         }
    1653                 :            : 
    1654                 :            :         /*
    1655                 :            :          * Must re-check here, to close a race against __kthread_bind(),
    1656                 :            :          * sched_setaffinity() is not guaranteed to observe the flag.
    1657                 :            :          */
    1658                 :          3 :         if (check && (p->flags & PF_NO_SETAFFINITY)) {
    1659                 :            :                 ret = -EINVAL;
    1660                 :            :                 goto out;
    1661                 :            :         }
    1662                 :            : 
    1663                 :          3 :         if (cpumask_equal(&p->cpus_mask, new_mask))
    1664                 :            :                 goto out;
    1665                 :            : 
    1666                 :          3 :         dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
    1667                 :          3 :         if (dest_cpu >= nr_cpu_ids) {
    1668                 :            :                 ret = -EINVAL;
    1669                 :            :                 goto out;
    1670                 :            :         }
    1671                 :            : 
    1672                 :          3 :         do_set_cpus_allowed(p, new_mask);
    1673                 :            : 
    1674                 :          3 :         if (p->flags & PF_KTHREAD) {
    1675                 :            :                 /*
    1676                 :            :                  * For kernel threads that do indeed end up on online &&
    1677                 :            :                  * !active we want to ensure they are strict per-CPU threads.
    1678                 :            :                  */
    1679                 :          3 :                 WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
    1680                 :            :                         !cpumask_intersects(new_mask, cpu_active_mask) &&
    1681                 :            :                         p->nr_cpus_allowed != 1);
    1682                 :            :         }
    1683                 :            : 
    1684                 :            :         /* Can the task run on the task's current CPU? If so, we're done */
    1685                 :          3 :         if (cpumask_test_cpu(task_cpu(p), new_mask))
    1686                 :            :                 goto out;
    1687                 :            : 
    1688                 :          3 :         if (task_running(rq, p) || p->state == TASK_WAKING) {
    1689                 :          1 :                 struct migration_arg arg = { p, dest_cpu };
    1690                 :            :                 /* Need help from migration thread: drop lock and wait. */
    1691                 :          1 :                 task_rq_unlock(rq, p, &rf);
    1692                 :          1 :                 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
    1693                 :            :                 return 0;
    1694                 :          3 :         } else if (task_on_rq_queued(p)) {
    1695                 :            :                 /*
    1696                 :            :                  * OK, since we're going to drop the lock immediately
    1697                 :            :                  * afterwards anyway.
    1698                 :            :                  */
    1699                 :          0 :                 rq = move_queued_task(rq, &rf, p, dest_cpu);
    1700                 :            :         }
    1701                 :            : out:
    1702                 :          3 :         task_rq_unlock(rq, p, &rf);
    1703                 :            : 
    1704                 :          3 :         return ret;
    1705                 :            : }
    1706                 :            : 
    1707                 :          3 : int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
    1708                 :            : {
    1709                 :          3 :         return __set_cpus_allowed_ptr(p, new_mask, false);
    1710                 :            : }
    1711                 :            : EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
    1712                 :            : 
    1713                 :          3 : void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
    1714                 :            : {
    1715                 :            : #ifdef CONFIG_SCHED_DEBUG
    1716                 :            :         /*
    1717                 :            :          * We should never call set_task_cpu() on a blocked task,
    1718                 :            :          * ttwu() will sort out the placement.
    1719                 :            :          */
    1720                 :          3 :         WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
    1721                 :            :                         !p->on_rq);
    1722                 :            : 
    1723                 :            :         /*
    1724                 :            :          * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
    1725                 :            :          * because schedstat_wait_{start,end} rebase migrating task's wait_start
    1726                 :            :          * time relying on p->on_rq.
    1727                 :            :          */
    1728                 :          3 :         WARN_ON_ONCE(p->state == TASK_RUNNING &&
    1729                 :            :                      p->sched_class == &fair_sched_class &&
    1730                 :            :                      (p->on_rq && !task_on_rq_migrating(p)));
    1731                 :            : 
    1732                 :            : #ifdef CONFIG_LOCKDEP
    1733                 :            :         /*
    1734                 :            :          * The caller should hold either p->pi_lock or rq->lock, when changing
    1735                 :            :          * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
    1736                 :            :          *
    1737                 :            :          * sched_move_task() holds both and thus holding either pins the cgroup,
    1738                 :            :          * see task_group().
    1739                 :            :          *
    1740                 :            :          * Furthermore, all task_rq users should acquire both locks, see
    1741                 :            :          * task_rq_lock().
    1742                 :            :          */
    1743                 :            :         WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
    1744                 :            :                                       lockdep_is_held(&task_rq(p)->lock)));
    1745                 :            : #endif
    1746                 :            :         /*
    1747                 :            :          * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
    1748                 :            :          */
    1749                 :          3 :         WARN_ON_ONCE(!cpu_online(new_cpu));
    1750                 :            : #endif
    1751                 :            : 
    1752                 :          3 :         trace_sched_migrate_task(p, new_cpu);
    1753                 :            : 
    1754                 :          3 :         if (task_cpu(p) != new_cpu) {
    1755                 :          3 :                 if (p->sched_class->migrate_task_rq)
    1756                 :          3 :                         p->sched_class->migrate_task_rq(p, new_cpu);
    1757                 :          3 :                 p->se.nr_migrations++;
    1758                 :          3 :                 rseq_migrate(p);
    1759                 :            :                 perf_event_task_migrate(p);
    1760                 :            :         }
    1761                 :            : 
    1762                 :            :         __set_task_cpu(p, new_cpu);
    1763                 :          3 : }
    1764                 :            : 
    1765                 :            : #ifdef CONFIG_NUMA_BALANCING
    1766                 :            : static void __migrate_swap_task(struct task_struct *p, int cpu)
    1767                 :            : {
    1768                 :            :         if (task_on_rq_queued(p)) {
    1769                 :            :                 struct rq *src_rq, *dst_rq;
    1770                 :            :                 struct rq_flags srf, drf;
    1771                 :            : 
    1772                 :            :                 src_rq = task_rq(p);
    1773                 :            :                 dst_rq = cpu_rq(cpu);
    1774                 :            : 
    1775                 :            :                 rq_pin_lock(src_rq, &srf);
    1776                 :            :                 rq_pin_lock(dst_rq, &drf);
    1777                 :            : 
    1778                 :            :                 deactivate_task(src_rq, p, 0);
    1779                 :            :                 set_task_cpu(p, cpu);
    1780                 :            :                 activate_task(dst_rq, p, 0);
    1781                 :            :                 check_preempt_curr(dst_rq, p, 0);
    1782                 :            : 
    1783                 :            :                 rq_unpin_lock(dst_rq, &drf);
    1784                 :            :                 rq_unpin_lock(src_rq, &srf);
    1785                 :            : 
    1786                 :            :         } else {
    1787                 :            :                 /*
    1788                 :            :                  * Task isn't running anymore; make it appear like we migrated
    1789                 :            :                  * it before it went to sleep. This means on wakeup we make the
    1790                 :            :                  * previous CPU our target instead of where it really is.
    1791                 :            :                  */
    1792                 :            :                 p->wake_cpu = cpu;
    1793                 :            :         }
    1794                 :            : }
    1795                 :            : 
    1796                 :            : struct migration_swap_arg {
    1797                 :            :         struct task_struct *src_task, *dst_task;
    1798                 :            :         int src_cpu, dst_cpu;
    1799                 :            : };
    1800                 :            : 
    1801                 :            : static int migrate_swap_stop(void *data)
    1802                 :            : {
    1803                 :            :         struct migration_swap_arg *arg = data;
    1804                 :            :         struct rq *src_rq, *dst_rq;
    1805                 :            :         int ret = -EAGAIN;
    1806                 :            : 
    1807                 :            :         if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
    1808                 :            :                 return -EAGAIN;
    1809                 :            : 
    1810                 :            :         src_rq = cpu_rq(arg->src_cpu);
    1811                 :            :         dst_rq = cpu_rq(arg->dst_cpu);
    1812                 :            : 
    1813                 :            :         double_raw_lock(&arg->src_task->pi_lock,
    1814                 :            :                         &arg->dst_task->pi_lock);
    1815                 :            :         double_rq_lock(src_rq, dst_rq);
    1816                 :            : 
    1817                 :            :         if (task_cpu(arg->dst_task) != arg->dst_cpu)
    1818                 :            :                 goto unlock;
    1819                 :            : 
    1820                 :            :         if (task_cpu(arg->src_task) != arg->src_cpu)
    1821                 :            :                 goto unlock;
    1822                 :            : 
    1823                 :            :         if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
    1824                 :            :                 goto unlock;
    1825                 :            : 
    1826                 :            :         if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
    1827                 :            :                 goto unlock;
    1828                 :            : 
    1829                 :            :         __migrate_swap_task(arg->src_task, arg->dst_cpu);
    1830                 :            :         __migrate_swap_task(arg->dst_task, arg->src_cpu);
    1831                 :            : 
    1832                 :            :         ret = 0;
    1833                 :            : 
    1834                 :            : unlock:
    1835                 :            :         double_rq_unlock(src_rq, dst_rq);
    1836                 :            :         raw_spin_unlock(&arg->dst_task->pi_lock);
    1837                 :            :         raw_spin_unlock(&arg->src_task->pi_lock);
    1838                 :            : 
    1839                 :            :         return ret;
    1840                 :            : }
    1841                 :            : 
    1842                 :            : /*
    1843                 :            :  * Cross migrate two tasks
    1844                 :            :  */
    1845                 :            : int migrate_swap(struct task_struct *cur, struct task_struct *p,
    1846                 :            :                 int target_cpu, int curr_cpu)
    1847                 :            : {
    1848                 :            :         struct migration_swap_arg arg;
    1849                 :            :         int ret = -EINVAL;
    1850                 :            : 
    1851                 :            :         arg = (struct migration_swap_arg){
    1852                 :            :                 .src_task = cur,
    1853                 :            :                 .src_cpu = curr_cpu,
    1854                 :            :                 .dst_task = p,
    1855                 :            :                 .dst_cpu = target_cpu,
    1856                 :            :         };
    1857                 :            : 
    1858                 :            :         if (arg.src_cpu == arg.dst_cpu)
    1859                 :            :                 goto out;
    1860                 :            : 
    1861                 :            :         /*
    1862                 :            :          * These three tests are all lockless; this is OK since all of them
    1863                 :            :          * will be re-checked with proper locks held further down the line.
    1864                 :            :          */
    1865                 :            :         if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
    1866                 :            :                 goto out;
    1867                 :            : 
    1868                 :            :         if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr))
    1869                 :            :                 goto out;
    1870                 :            : 
    1871                 :            :         if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr))
    1872                 :            :                 goto out;
    1873                 :            : 
    1874                 :            :         trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
    1875                 :            :         ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
    1876                 :            : 
    1877                 :            : out:
    1878                 :            :         return ret;
    1879                 :            : }
    1880                 :            : #endif /* CONFIG_NUMA_BALANCING */
    1881                 :            : 
    1882                 :            : /*
    1883                 :            :  * wait_task_inactive - wait for a thread to unschedule.
    1884                 :            :  *
    1885                 :            :  * If @match_state is nonzero, it's the @p->state value just checked and
    1886                 :            :  * not expected to change.  If it changes, i.e. @p might have woken up,
    1887                 :            :  * then return zero.  When we succeed in waiting for @p to be off its CPU,
    1888                 :            :  * we return a positive number (its total switch count).  If a second call
    1889                 :            :  * a short while later returns the same number, the caller can be sure that
    1890                 :            :  * @p has remained unscheduled the whole time.
    1891                 :            :  *
    1892                 :            :  * The caller must ensure that the task *will* unschedule sometime soon,
    1893                 :            :  * else this function might spin for a *long* time. This function can't
    1894                 :            :  * be called with interrupts off, or it may introduce deadlock with
    1895                 :            :  * smp_call_function() if an IPI is sent by the same process we are
    1896                 :            :  * waiting to become inactive.
    1897                 :            :  */
    1898                 :          3 : unsigned long wait_task_inactive(struct task_struct *p, long match_state)
    1899                 :            : {
    1900                 :            :         int running, queued;
    1901                 :            :         struct rq_flags rf;
    1902                 :            :         unsigned long ncsw;
    1903                 :            :         struct rq *rq;
    1904                 :            : 
    1905                 :            :         for (;;) {
    1906                 :            :                 /*
    1907                 :            :                  * We do the initial early heuristics without holding
    1908                 :            :                  * any task-queue locks at all. We'll only try to get
    1909                 :            :                  * the runqueue lock when things look like they will
    1910                 :            :                  * work out!
    1911                 :            :                  */
    1912                 :          3 :                 rq = task_rq(p);
    1913                 :            : 
    1914                 :            :                 /*
    1915                 :            :                  * If the task is actively running on another CPU
    1916                 :            :                  * still, just relax and busy-wait without holding
    1917                 :            :                  * any locks.
    1918                 :            :                  *
    1919                 :            :                  * NOTE! Since we don't hold any locks, it's not
    1920                 :            :                  * even sure that "rq" stays as the right runqueue!
    1921                 :            :                  * But we don't care, since "task_running()" will
    1922                 :            :                  * return false if the runqueue has changed and p
    1923                 :            :                  * is actually now running somewhere else!
    1924                 :            :                  */
    1925                 :          3 :                 while (task_running(rq, p)) {
    1926                 :          3 :                         if (match_state && unlikely(p->state != match_state))
    1927                 :            :                                 return 0;
    1928                 :          3 :                         cpu_relax();
    1929                 :            :                 }
    1930                 :            : 
    1931                 :            :                 /*
    1932                 :            :                  * Ok, time to look more closely! We need the rq
    1933                 :            :                  * lock now, to be *sure*. If we're wrong, we'll
    1934                 :            :                  * just go back and repeat.
    1935                 :            :                  */
    1936                 :          3 :                 rq = task_rq_lock(p, &rf);
    1937                 :          3 :                 trace_sched_wait_task(p);
    1938                 :            :                 running = task_running(rq, p);
    1939                 :            :                 queued = task_on_rq_queued(p);
    1940                 :            :                 ncsw = 0;
    1941                 :          3 :                 if (!match_state || p->state == match_state)
    1942                 :          3 :                         ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
    1943                 :          3 :                 task_rq_unlock(rq, p, &rf);
    1944                 :            : 
    1945                 :            :                 /*
    1946                 :            :                  * If it changed from the expected state, bail out now.
    1947                 :            :                  */
    1948                 :          3 :                 if (unlikely(!ncsw))
    1949                 :            :                         break;
    1950                 :            : 
    1951                 :            :                 /*
    1952                 :            :                  * Was it really running after all now that we
    1953                 :            :                  * checked with the proper locks actually held?
    1954                 :            :                  *
    1955                 :            :                  * Oops. Go back and try again..
    1956                 :            :                  */
    1957                 :          3 :                 if (unlikely(running)) {
    1958                 :          0 :                         cpu_relax();
    1959                 :          0 :                         continue;
    1960                 :            :                 }
    1961                 :            : 
    1962                 :            :                 /*
    1963                 :            :                  * It's not enough that it's not actively running,
    1964                 :            :                  * it must be off the runqueue _entirely_, and not
    1965                 :            :                  * preempted!
    1966                 :            :                  *
    1967                 :            :                  * So if it was still runnable (but just not actively
    1968                 :            :                  * running right now), it's preempted, and we should
    1969                 :            :                  * yield - it could be a while.
    1970                 :            :                  */
    1971                 :          3 :                 if (unlikely(queued)) {
    1972                 :          0 :                         ktime_t to = NSEC_PER_SEC / HZ;
    1973                 :            : 
    1974                 :          0 :                         set_current_state(TASK_UNINTERRUPTIBLE);
    1975                 :          0 :                         schedule_hrtimeout(&to, HRTIMER_MODE_REL);
    1976                 :          0 :                         continue;
    1977                 :            :                 }
    1978                 :            : 
    1979                 :            :                 /*
    1980                 :            :                  * Ahh, all good. It wasn't running, and it wasn't
    1981                 :            :                  * runnable, which means that it will never become
    1982                 :            :                  * running in the future either. We're all done!
    1983                 :            :                  */
    1984                 :            :                 break;
    1985                 :            :         }
    1986                 :            : 
    1987                 :          3 :         return ncsw;
    1988                 :            : }
    1989                 :            : 
    1990                 :            : /***
    1991                 :            :  * kick_process - kick a running thread to enter/exit the kernel
    1992                 :            :  * @p: the to-be-kicked thread
    1993                 :            :  *
    1994                 :            :  * Cause a process which is running on another CPU to enter
    1995                 :            :  * kernel-mode, without any delay. (to get signals handled.)
    1996                 :            :  *
    1997                 :            :  * NOTE: this function doesn't have to take the runqueue lock,
    1998                 :            :  * because all it wants to ensure is that the remote task enters
    1999                 :            :  * the kernel. If the IPI races and the task has been migrated
    2000                 :            :  * to another CPU then no harm is done and the purpose has been
    2001                 :            :  * achieved as well.
    2002                 :            :  */
    2003                 :          3 : void kick_process(struct task_struct *p)
    2004                 :            : {
    2005                 :            :         int cpu;
    2006                 :            : 
    2007                 :          3 :         preempt_disable();
    2008                 :          3 :         cpu = task_cpu(p);
    2009                 :          3 :         if ((cpu != smp_processor_id()) && task_curr(p))
    2010                 :          3 :                 smp_send_reschedule(cpu);
    2011                 :          3 :         preempt_enable();
    2012                 :          3 : }
    2013                 :            : EXPORT_SYMBOL_GPL(kick_process);
    2014                 :            : 
    2015                 :            : /*
    2016                 :            :  * ->cpus_ptr is protected by both rq->lock and p->pi_lock
    2017                 :            :  *
    2018                 :            :  * A few notes on cpu_active vs cpu_online:
    2019                 :            :  *
    2020                 :            :  *  - cpu_active must be a subset of cpu_online
    2021                 :            :  *
    2022                 :            :  *  - on CPU-up we allow per-CPU kthreads on the online && !active CPU,
    2023                 :            :  *    see __set_cpus_allowed_ptr(). At this point the newly online
    2024                 :            :  *    CPU isn't yet part of the sched domains, and balancing will not
    2025                 :            :  *    see it.
    2026                 :            :  *
    2027                 :            :  *  - on CPU-down we clear cpu_active() to mask the sched domains and
    2028                 :            :  *    avoid the load balancer to place new tasks on the to be removed
    2029                 :            :  *    CPU. Existing tasks will remain running there and will be taken
    2030                 :            :  *    off.
    2031                 :            :  *
    2032                 :            :  * This means that fallback selection must not select !active CPUs.
    2033                 :            :  * And can assume that any active CPU must be online. Conversely
    2034                 :            :  * select_task_rq() below may allow selection of !active CPUs in order
    2035                 :            :  * to satisfy the above rules.
    2036                 :            :  */
    2037                 :          3 : static int select_fallback_rq(int cpu, struct task_struct *p)
    2038                 :            : {
    2039                 :            :         int nid = cpu_to_node(cpu);
    2040                 :            :         const struct cpumask *nodemask = NULL;
    2041                 :            :         enum { cpuset, possible, fail } state = cpuset;
    2042                 :            :         int dest_cpu;
    2043                 :            : 
    2044                 :            :         /*
    2045                 :            :          * If the node that the CPU is on has been offlined, cpu_to_node()
    2046                 :            :          * will return -1. There is no CPU on the node, and we should
    2047                 :            :          * select the CPU on the other node.
    2048                 :            :          */
    2049                 :            :         if (nid != -1) {
    2050                 :            :                 nodemask = cpumask_of_node(nid);
    2051                 :            : 
    2052                 :            :                 /* Look for allowed, online CPU in same node. */
    2053                 :          3 :                 for_each_cpu(dest_cpu, nodemask) {
    2054                 :          3 :                         if (!cpu_active(dest_cpu))
    2055                 :          0 :                                 continue;
    2056                 :          3 :                         if (cpumask_test_cpu(dest_cpu, p->cpus_ptr))
    2057                 :          0 :                                 return dest_cpu;
    2058                 :            :                 }
    2059                 :            :         }
    2060                 :            : 
    2061                 :            :         for (;;) {
    2062                 :            :                 /* Any allowed, online CPU? */
    2063                 :          3 :                 for_each_cpu(dest_cpu, p->cpus_ptr) {
    2064                 :          3 :                         if (!is_cpu_allowed(p, dest_cpu))
    2065                 :          3 :                                 continue;
    2066                 :            : 
    2067                 :            :                         goto out;
    2068                 :            :                 }
    2069                 :            : 
    2070                 :            :                 /* No more Mr. Nice Guy. */
    2071                 :          3 :                 switch (state) {
    2072                 :            :                 case cpuset:
    2073                 :            :                         if (IS_ENABLED(CONFIG_CPUSETS)) {
    2074                 :          3 :                                 cpuset_cpus_allowed_fallback(p);
    2075                 :            :                                 state = possible;
    2076                 :          3 :                                 break;
    2077                 :            :                         }
    2078                 :            :                         /* Fall-through */
    2079                 :            :                 case possible:
    2080                 :          0 :                         do_set_cpus_allowed(p, cpu_possible_mask);
    2081                 :            :                         state = fail;
    2082                 :          0 :                         break;
    2083                 :            : 
    2084                 :            :                 case fail:
    2085                 :          0 :                         BUG();
    2086                 :            :                         break;
    2087                 :            :                 }
    2088                 :            :         }
    2089                 :            : 
    2090                 :            : out:
    2091                 :          3 :         if (state != cpuset) {
    2092                 :            :                 /*
    2093                 :            :                  * Don't tell them about moving exiting tasks or
    2094                 :            :                  * kernel threads (both mm NULL), since they never
    2095                 :            :                  * leave kernel.
    2096                 :            :                  */
    2097                 :          3 :                 if (p->mm && printk_ratelimit()) {
    2098                 :          0 :                         printk_deferred("process %d (%s) no longer affine to cpu%d\n",
    2099                 :          0 :                                         task_pid_nr(p), p->comm, cpu);
    2100                 :            :                 }
    2101                 :            :         }
    2102                 :            : 
    2103                 :          3 :         return dest_cpu;
    2104                 :            : }
    2105                 :            : 
    2106                 :            : /*
    2107                 :            :  * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
    2108                 :            :  */
    2109                 :            : static inline
    2110                 :          3 : int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
    2111                 :            : {
    2112                 :            :         lockdep_assert_held(&p->pi_lock);
    2113                 :            : 
    2114                 :          3 :         if (p->nr_cpus_allowed > 1)
    2115                 :          3 :                 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
    2116                 :            :         else
    2117                 :          3 :                 cpu = cpumask_any(p->cpus_ptr);
    2118                 :            : 
    2119                 :            :         /*
    2120                 :            :          * In order not to call set_task_cpu() on a blocking task we need
    2121                 :            :          * to rely on ttwu() to place the task on a valid ->cpus_ptr
    2122                 :            :          * CPU.
    2123                 :            :          *
    2124                 :            :          * Since this is common to all placement strategies, this lives here.
    2125                 :            :          *
    2126                 :            :          * [ this allows ->select_task() to simply return task_cpu(p) and
    2127                 :            :          *   not worry about this generic constraint ]
    2128                 :            :          */
    2129                 :          3 :         if (unlikely(!is_cpu_allowed(p, cpu)))
    2130                 :          3 :                 cpu = select_fallback_rq(task_cpu(p), p);
    2131                 :            : 
    2132                 :          3 :         return cpu;
    2133                 :            : }
    2134                 :            : 
    2135                 :            : static void update_avg(u64 *avg, u64 sample)
    2136                 :            : {
    2137                 :          3 :         s64 diff = sample - *avg;
    2138                 :          3 :         *avg += diff >> 3;
    2139                 :            : }
    2140                 :            : 
    2141                 :          3 : void sched_set_stop_task(int cpu, struct task_struct *stop)
    2142                 :            : {
    2143                 :          3 :         struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
    2144                 :          3 :         struct task_struct *old_stop = cpu_rq(cpu)->stop;
    2145                 :            : 
    2146                 :          3 :         if (stop) {
    2147                 :            :                 /*
    2148                 :            :                  * Make it appear like a SCHED_FIFO task, its something
    2149                 :            :                  * userspace knows about and won't get confused about.
    2150                 :            :                  *
    2151                 :            :                  * Also, it will make PI more or less work without too
    2152                 :            :                  * much confusion -- but then, stop work should not
    2153                 :            :                  * rely on PI working anyway.
    2154                 :            :                  */
    2155                 :            :                 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
    2156                 :            : 
    2157                 :          3 :                 stop->sched_class = &stop_sched_class;
    2158                 :            :         }
    2159                 :            : 
    2160                 :          3 :         cpu_rq(cpu)->stop = stop;
    2161                 :            : 
    2162                 :          3 :         if (old_stop) {
    2163                 :            :                 /*
    2164                 :            :                  * Reset it back to a normal scheduling class so that
    2165                 :            :                  * it can die in pieces.
    2166                 :            :                  */
    2167                 :          0 :                 old_stop->sched_class = &rt_sched_class;
    2168                 :            :         }
    2169                 :          3 : }
    2170                 :            : 
    2171                 :            : #else
    2172                 :            : 
    2173                 :            : static inline int __set_cpus_allowed_ptr(struct task_struct *p,
    2174                 :            :                                          const struct cpumask *new_mask, bool check)
    2175                 :            : {
    2176                 :            :         return set_cpus_allowed_ptr(p, new_mask);
    2177                 :            : }
    2178                 :            : 
    2179                 :            : #endif /* CONFIG_SMP */
    2180                 :            : 
    2181                 :            : static void
    2182                 :          3 : ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
    2183                 :            : {
    2184                 :            :         struct rq *rq;
    2185                 :            : 
    2186                 :          3 :         if (!schedstat_enabled())
    2187                 :          3 :                 return;
    2188                 :            : 
    2189                 :          0 :         rq = this_rq();
    2190                 :            : 
    2191                 :            : #ifdef CONFIG_SMP
    2192                 :          0 :         if (cpu == rq->cpu) {
    2193                 :          0 :                 __schedstat_inc(rq->ttwu_local);
    2194                 :          0 :                 __schedstat_inc(p->se.statistics.nr_wakeups_local);
    2195                 :            :         } else {
    2196                 :            :                 struct sched_domain *sd;
    2197                 :            : 
    2198                 :          0 :                 __schedstat_inc(p->se.statistics.nr_wakeups_remote);
    2199                 :            :                 rcu_read_lock();
    2200                 :          0 :                 for_each_domain(rq->cpu, sd) {
    2201                 :          0 :                         if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
    2202                 :          0 :                                 __schedstat_inc(sd->ttwu_wake_remote);
    2203                 :          0 :                                 break;
    2204                 :            :                         }
    2205                 :            :                 }
    2206                 :            :                 rcu_read_unlock();
    2207                 :            :         }
    2208                 :            : 
    2209                 :          0 :         if (wake_flags & WF_MIGRATED)
    2210                 :          0 :                 __schedstat_inc(p->se.statistics.nr_wakeups_migrate);
    2211                 :            : #endif /* CONFIG_SMP */
    2212                 :            : 
    2213                 :          0 :         __schedstat_inc(rq->ttwu_count);
    2214                 :          0 :         __schedstat_inc(p->se.statistics.nr_wakeups);
    2215                 :            : 
    2216                 :          0 :         if (wake_flags & WF_SYNC)
    2217                 :          0 :                 __schedstat_inc(p->se.statistics.nr_wakeups_sync);
    2218                 :            : }
    2219                 :            : 
    2220                 :            : /*
    2221                 :            :  * Mark the task runnable and perform wakeup-preemption.
    2222                 :            :  */
    2223                 :          3 : static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags,
    2224                 :            :                            struct rq_flags *rf)
    2225                 :            : {
    2226                 :          3 :         check_preempt_curr(rq, p, wake_flags);
    2227                 :          3 :         p->state = TASK_RUNNING;
    2228                 :          3 :         trace_sched_wakeup(p);
    2229                 :            : 
    2230                 :            : #ifdef CONFIG_SMP
    2231                 :          3 :         if (p->sched_class->task_woken) {
    2232                 :            :                 /*
    2233                 :            :                  * Our task @p is fully woken up and running; so its safe to
    2234                 :            :                  * drop the rq->lock, hereafter rq is only used for statistics.
    2235                 :            :                  */
    2236                 :            :                 rq_unpin_lock(rq, rf);
    2237                 :          2 :                 p->sched_class->task_woken(rq, p);
    2238                 :            :                 rq_repin_lock(rq, rf);
    2239                 :            :         }
    2240                 :            : 
    2241                 :          3 :         if (rq->idle_stamp) {
    2242                 :          3 :                 u64 delta = rq_clock(rq) - rq->idle_stamp;
    2243                 :          3 :                 u64 max = 2*rq->max_idle_balance_cost;
    2244                 :            : 
    2245                 :            :                 update_avg(&rq->avg_idle, delta);
    2246                 :            : 
    2247                 :          3 :                 if (rq->avg_idle > max)
    2248                 :          3 :                         rq->avg_idle = max;
    2249                 :            : 
    2250                 :          3 :                 rq->idle_stamp = 0;
    2251                 :            :         }
    2252                 :            : #endif
    2253                 :          3 : }
    2254                 :            : 
    2255                 :            : static void
    2256                 :          3 : ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
    2257                 :            :                  struct rq_flags *rf)
    2258                 :            : {
    2259                 :            :         int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
    2260                 :            : 
    2261                 :            :         lockdep_assert_held(&rq->lock);
    2262                 :            : 
    2263                 :            : #ifdef CONFIG_SMP
    2264                 :          3 :         if (p->sched_contributes_to_load)
    2265                 :          3 :                 rq->nr_uninterruptible--;
    2266                 :            : 
    2267                 :          3 :         if (wake_flags & WF_MIGRATED)
    2268                 :            :                 en_flags |= ENQUEUE_MIGRATED;
    2269                 :            : #endif
    2270                 :            : 
    2271                 :          3 :         activate_task(rq, p, en_flags);
    2272                 :          3 :         ttwu_do_wakeup(rq, p, wake_flags, rf);
    2273                 :          3 : }
    2274                 :            : 
    2275                 :            : /*
    2276                 :            :  * Called in case the task @p isn't fully descheduled from its runqueue,
    2277                 :            :  * in this case we must do a remote wakeup. Its a 'light' wakeup though,
    2278                 :            :  * since all we need to do is flip p->state to TASK_RUNNING, since
    2279                 :            :  * the task is still ->on_rq.
    2280                 :            :  */
    2281                 :          3 : static int ttwu_remote(struct task_struct *p, int wake_flags)
    2282                 :            : {
    2283                 :            :         struct rq_flags rf;
    2284                 :            :         struct rq *rq;
    2285                 :            :         int ret = 0;
    2286                 :            : 
    2287                 :          3 :         rq = __task_rq_lock(p, &rf);
    2288                 :          3 :         if (task_on_rq_queued(p)) {
    2289                 :            :                 /* check_preempt_curr() may use rq clock */
    2290                 :          3 :                 update_rq_clock(rq);
    2291                 :          3 :                 ttwu_do_wakeup(rq, p, wake_flags, &rf);
    2292                 :            :                 ret = 1;
    2293                 :            :         }
    2294                 :            :         __task_rq_unlock(rq, &rf);
    2295                 :            : 
    2296                 :          3 :         return ret;
    2297                 :            : }
    2298                 :            : 
    2299                 :            : #ifdef CONFIG_SMP
    2300                 :          3 : void sched_ttwu_pending(void)
    2301                 :            : {
    2302                 :          3 :         struct rq *rq = this_rq();
    2303                 :            :         struct llist_node *llist = llist_del_all(&rq->wake_list);
    2304                 :            :         struct task_struct *p, *t;
    2305                 :            :         struct rq_flags rf;
    2306                 :            : 
    2307                 :          3 :         if (!llist)
    2308                 :          3 :                 return;
    2309                 :            : 
    2310                 :            :         rq_lock_irqsave(rq, &rf);
    2311                 :          3 :         update_rq_clock(rq);
    2312                 :            : 
    2313                 :          3 :         llist_for_each_entry_safe(p, t, llist, wake_entry)
    2314                 :          3 :                 ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf);
    2315                 :            : 
    2316                 :            :         rq_unlock_irqrestore(rq, &rf);
    2317                 :            : }
    2318                 :            : 
    2319                 :          3 : void scheduler_ipi(void)
    2320                 :            : {
    2321                 :            :         /*
    2322                 :            :          * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
    2323                 :            :          * TIF_NEED_RESCHED remotely (for the first time) will also send
    2324                 :            :          * this IPI.
    2325                 :            :          */
    2326                 :            :         preempt_fold_need_resched();
    2327                 :            : 
    2328                 :          3 :         if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
    2329                 :          3 :                 return;
    2330                 :            : 
    2331                 :            :         /*
    2332                 :            :          * Not all reschedule IPI handlers call irq_enter/irq_exit, since
    2333                 :            :          * traditionally all their work was done from the interrupt return
    2334                 :            :          * path. Now that we actually do some work, we need to make sure
    2335                 :            :          * we do call them.
    2336                 :            :          *
    2337                 :            :          * Some archs already do call them, luckily irq_enter/exit nest
    2338                 :            :          * properly.
    2339                 :            :          *
    2340                 :            :          * Arguably we should visit all archs and update all handlers,
    2341                 :            :          * however a fair share of IPIs are still resched only so this would
    2342                 :            :          * somewhat pessimize the simple resched case.
    2343                 :            :          */
    2344                 :          3 :         irq_enter();
    2345                 :          3 :         sched_ttwu_pending();
    2346                 :            : 
    2347                 :            :         /*
    2348                 :            :          * Check if someone kicked us for doing the nohz idle load balance.
    2349                 :            :          */
    2350                 :          3 :         if (unlikely(got_nohz_idle_kick())) {
    2351                 :          3 :                 this_rq()->idle_balance = 1;
    2352                 :          3 :                 raise_softirq_irqoff(SCHED_SOFTIRQ);
    2353                 :            :         }
    2354                 :          3 :         irq_exit();
    2355                 :            : }
    2356                 :            : 
    2357                 :          3 : static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags)
    2358                 :            : {
    2359                 :          3 :         struct rq *rq = cpu_rq(cpu);
    2360                 :            : 
    2361                 :          3 :         p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
    2362                 :            : 
    2363                 :          3 :         if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
    2364                 :            :                 if (!set_nr_if_polling(rq->idle))
    2365                 :          3 :                         smp_send_reschedule(cpu);
    2366                 :            :                 else
    2367                 :            :                         trace_sched_wake_idle_without_ipi(cpu);
    2368                 :            :         }
    2369                 :          3 : }
    2370                 :            : 
    2371                 :          0 : void wake_up_if_idle(int cpu)
    2372                 :            : {
    2373                 :          0 :         struct rq *rq = cpu_rq(cpu);
    2374                 :            :         struct rq_flags rf;
    2375                 :            : 
    2376                 :            :         rcu_read_lock();
    2377                 :            : 
    2378                 :          0 :         if (!is_idle_task(rcu_dereference(rq->curr)))
    2379                 :            :                 goto out;
    2380                 :            : 
    2381                 :            :         if (set_nr_if_polling(rq->idle)) {
    2382                 :            :                 trace_sched_wake_idle_without_ipi(cpu);
    2383                 :            :         } else {
    2384                 :            :                 rq_lock_irqsave(rq, &rf);
    2385                 :          0 :                 if (is_idle_task(rq->curr))
    2386                 :          0 :                         smp_send_reschedule(cpu);
    2387                 :            :                 /* Else CPU is not idle, do nothing here: */
    2388                 :            :                 rq_unlock_irqrestore(rq, &rf);
    2389                 :            :         }
    2390                 :            : 
    2391                 :            : out:
    2392                 :            :         rcu_read_unlock();
    2393                 :          0 : }
    2394                 :            : 
    2395                 :          3 : bool cpus_share_cache(int this_cpu, int that_cpu)
    2396                 :            : {
    2397                 :          3 :         return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
    2398                 :            : }
    2399                 :            : #endif /* CONFIG_SMP */
    2400                 :            : 
    2401                 :          3 : static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
    2402                 :            : {
    2403                 :          3 :         struct rq *rq = cpu_rq(cpu);
    2404                 :            :         struct rq_flags rf;
    2405                 :            : 
    2406                 :            : #if defined(CONFIG_SMP)
    2407                 :          3 :         if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
    2408                 :          3 :                 sched_clock_cpu(cpu); /* Sync clocks across CPUs */
    2409                 :          3 :                 ttwu_queue_remote(p, cpu, wake_flags);
    2410                 :          3 :                 return;
    2411                 :            :         }
    2412                 :            : #endif
    2413                 :            : 
    2414                 :            :         rq_lock(rq, &rf);
    2415                 :          3 :         update_rq_clock(rq);
    2416                 :          3 :         ttwu_do_activate(rq, p, wake_flags, &rf);
    2417                 :            :         rq_unlock(rq, &rf);
    2418                 :            : }
    2419                 :            : 
    2420                 :            : /*
    2421                 :            :  * Notes on Program-Order guarantees on SMP systems.
    2422                 :            :  *
    2423                 :            :  *  MIGRATION
    2424                 :            :  *
    2425                 :            :  * The basic program-order guarantee on SMP systems is that when a task [t]
    2426                 :            :  * migrates, all its activity on its old CPU [c0] happens-before any subsequent
    2427                 :            :  * execution on its new CPU [c1].
    2428                 :            :  *
    2429                 :            :  * For migration (of runnable tasks) this is provided by the following means:
    2430                 :            :  *
    2431                 :            :  *  A) UNLOCK of the rq(c0)->lock scheduling out task t
    2432                 :            :  *  B) migration for t is required to synchronize *both* rq(c0)->lock and
    2433                 :            :  *     rq(c1)->lock (if not at the same time, then in that order).
    2434                 :            :  *  C) LOCK of the rq(c1)->lock scheduling in task
    2435                 :            :  *
    2436                 :            :  * Release/acquire chaining guarantees that B happens after A and C after B.
    2437                 :            :  * Note: the CPU doing B need not be c0 or c1
    2438                 :            :  *
    2439                 :            :  * Example:
    2440                 :            :  *
    2441                 :            :  *   CPU0            CPU1            CPU2
    2442                 :            :  *
    2443                 :            :  *   LOCK rq(0)->lock
    2444                 :            :  *   sched-out X
    2445                 :            :  *   sched-in Y
    2446                 :            :  *   UNLOCK rq(0)->lock
    2447                 :            :  *
    2448                 :            :  *                                   LOCK rq(0)->lock // orders against CPU0
    2449                 :            :  *                                   dequeue X
    2450                 :            :  *                                   UNLOCK rq(0)->lock
    2451                 :            :  *
    2452                 :            :  *                                   LOCK rq(1)->lock
    2453                 :            :  *                                   enqueue X
    2454                 :            :  *                                   UNLOCK rq(1)->lock
    2455                 :            :  *
    2456                 :            :  *                   LOCK rq(1)->lock // orders against CPU2
    2457                 :            :  *                   sched-out Z
    2458                 :            :  *                   sched-in X
    2459                 :            :  *                   UNLOCK rq(1)->lock
    2460                 :            :  *
    2461                 :            :  *
    2462                 :            :  *  BLOCKING -- aka. SLEEP + WAKEUP
    2463                 :            :  *
    2464                 :            :  * For blocking we (obviously) need to provide the same guarantee as for
    2465                 :            :  * migration. However the means are completely different as there is no lock
    2466                 :            :  * chain to provide order. Instead we do:
    2467                 :            :  *
    2468                 :            :  *   1) smp_store_release(X->on_cpu, 0)
    2469                 :            :  *   2) smp_cond_load_acquire(!X->on_cpu)
    2470                 :            :  *
    2471                 :            :  * Example:
    2472                 :            :  *
    2473                 :            :  *   CPU0 (schedule)  CPU1 (try_to_wake_up) CPU2 (schedule)
    2474                 :            :  *
    2475                 :            :  *   LOCK rq(0)->lock LOCK X->pi_lock
    2476                 :            :  *   dequeue X
    2477                 :            :  *   sched-out X
    2478                 :            :  *   smp_store_release(X->on_cpu, 0);
    2479                 :            :  *
    2480                 :            :  *                    smp_cond_load_acquire(&X->on_cpu, !VAL);
    2481                 :            :  *                    X->state = WAKING
    2482                 :            :  *                    set_task_cpu(X,2)
    2483                 :            :  *
    2484                 :            :  *                    LOCK rq(2)->lock
    2485                 :            :  *                    enqueue X
    2486                 :            :  *                    X->state = RUNNING
    2487                 :            :  *                    UNLOCK rq(2)->lock
    2488                 :            :  *
    2489                 :            :  *                                          LOCK rq(2)->lock // orders against CPU1
    2490                 :            :  *                                          sched-out Z
    2491                 :            :  *                                          sched-in X
    2492                 :            :  *                                          UNLOCK rq(2)->lock
    2493                 :            :  *
    2494                 :            :  *                    UNLOCK X->pi_lock
    2495                 :            :  *   UNLOCK rq(0)->lock
    2496                 :            :  *
    2497                 :            :  *
    2498                 :            :  * However, for wakeups there is a second guarantee we must provide, namely we
    2499                 :            :  * must ensure that CONDITION=1 done by the caller can not be reordered with
    2500                 :            :  * accesses to the task state; see try_to_wake_up() and set_current_state().
    2501                 :            :  */
    2502                 :            : 
    2503                 :            : /**
    2504                 :            :  * try_to_wake_up - wake up a thread
    2505                 :            :  * @p: the thread to be awakened
    2506                 :            :  * @state: the mask of task states that can be woken
    2507                 :            :  * @wake_flags: wake modifier flags (WF_*)
    2508                 :            :  *
    2509                 :            :  * If (@state & @p->state) @p->state = TASK_RUNNING.
    2510                 :            :  *
    2511                 :            :  * If the task was not queued/runnable, also place it back on a runqueue.
    2512                 :            :  *
    2513                 :            :  * Atomic against schedule() which would dequeue a task, also see
    2514                 :            :  * set_current_state().
    2515                 :            :  *
    2516                 :            :  * This function executes a full memory barrier before accessing the task
    2517                 :            :  * state; see set_current_state().
    2518                 :            :  *
    2519                 :            :  * Return: %true if @p->state changes (an actual wakeup was done),
    2520                 :            :  *         %false otherwise.
    2521                 :            :  */
    2522                 :            : static int
    2523                 :          3 : try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
    2524                 :            : {
    2525                 :            :         unsigned long flags;
    2526                 :            :         int cpu, success = 0;
    2527                 :            : 
    2528                 :          3 :         preempt_disable();
    2529                 :          3 :         if (p == current) {
    2530                 :            :                 /*
    2531                 :            :                  * We're waking current, this means 'p->on_rq' and 'task_cpu(p)
    2532                 :            :                  * == smp_processor_id()'. Together this means we can special
    2533                 :            :                  * case the whole 'p->on_rq && ttwu_remote()' case below
    2534                 :            :                  * without taking any locks.
    2535                 :            :                  *
    2536                 :            :                  * In particular:
    2537                 :            :                  *  - we rely on Program-Order guarantees for all the ordering,
    2538                 :            :                  *  - we're serialized against set_special_state() by virtue of
    2539                 :            :                  *    it disabling IRQs (this allows not taking ->pi_lock).
    2540                 :            :                  */
    2541                 :          3 :                 if (!(p->state & state))
    2542                 :            :                         goto out;
    2543                 :            : 
    2544                 :            :                 success = 1;
    2545                 :          3 :                 cpu = task_cpu(p);
    2546                 :          3 :                 trace_sched_waking(p);
    2547                 :          3 :                 p->state = TASK_RUNNING;
    2548                 :          3 :                 trace_sched_wakeup(p);
    2549                 :          3 :                 goto out;
    2550                 :            :         }
    2551                 :            : 
    2552                 :            :         /*
    2553                 :            :          * If we are going to wake up a thread waiting for CONDITION we
    2554                 :            :          * need to ensure that CONDITION=1 done by the caller can not be
    2555                 :            :          * reordered with p->state check below. This pairs with mb() in
    2556                 :            :          * set_current_state() the waiting thread does.
    2557                 :            :          */
    2558                 :          3 :         raw_spin_lock_irqsave(&p->pi_lock, flags);
    2559                 :            :         smp_mb__after_spinlock();
    2560                 :          3 :         if (!(p->state & state))
    2561                 :            :                 goto unlock;
    2562                 :            : 
    2563                 :          3 :         trace_sched_waking(p);
    2564                 :            : 
    2565                 :            :         /* We're going to change ->state: */
    2566                 :            :         success = 1;
    2567                 :          3 :         cpu = task_cpu(p);
    2568                 :            : 
    2569                 :            :         /*
    2570                 :            :          * Ensure we load p->on_rq _after_ p->state, otherwise it would
    2571                 :            :          * be possible to, falsely, observe p->on_rq == 0 and get stuck
    2572                 :            :          * in smp_cond_load_acquire() below.
    2573                 :            :          *
    2574                 :            :          * sched_ttwu_pending()                 try_to_wake_up()
    2575                 :            :          *   STORE p->on_rq = 1                        LOAD p->state
    2576                 :            :          *   UNLOCK rq->lock
    2577                 :            :          *
    2578                 :            :          * __schedule() (switch to task 'p')
    2579                 :            :          *   LOCK rq->lock                     smp_rmb();
    2580                 :            :          *   smp_mb__after_spinlock();
    2581                 :            :          *   UNLOCK rq->lock
    2582                 :            :          *
    2583                 :            :          * [task p]
    2584                 :            :          *   STORE p->state = UNINTERRUPTIBLE          LOAD p->on_rq
    2585                 :            :          *
    2586                 :            :          * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
    2587                 :            :          * __schedule().  See the comment for smp_mb__after_spinlock().
    2588                 :            :          */
    2589                 :          3 :         smp_rmb();
    2590                 :          3 :         if (p->on_rq && ttwu_remote(p, wake_flags))
    2591                 :            :                 goto unlock;
    2592                 :            : 
    2593                 :            : #ifdef CONFIG_SMP
    2594                 :            :         /*
    2595                 :            :          * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
    2596                 :            :          * possible to, falsely, observe p->on_cpu == 0.
    2597                 :            :          *
    2598                 :            :          * One must be running (->on_cpu == 1) in order to remove oneself
    2599                 :            :          * from the runqueue.
    2600                 :            :          *
    2601                 :            :          * __schedule() (switch to task 'p')    try_to_wake_up()
    2602                 :            :          *   STORE p->on_cpu = 1               LOAD p->on_rq
    2603                 :            :          *   UNLOCK rq->lock
    2604                 :            :          *
    2605                 :            :          * __schedule() (put 'p' to sleep)
    2606                 :            :          *   LOCK rq->lock                     smp_rmb();
    2607                 :            :          *   smp_mb__after_spinlock();
    2608                 :            :          *   STORE p->on_rq = 0                        LOAD p->on_cpu
    2609                 :            :          *
    2610                 :            :          * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
    2611                 :            :          * __schedule().  See the comment for smp_mb__after_spinlock().
    2612                 :            :          */
    2613                 :          3 :         smp_rmb();
    2614                 :            : 
    2615                 :            :         /*
    2616                 :            :          * If the owning (remote) CPU is still in the middle of schedule() with
    2617                 :            :          * this task as prev, wait until its done referencing the task.
    2618                 :            :          *
    2619                 :            :          * Pairs with the smp_store_release() in finish_task().
    2620                 :            :          *
    2621                 :            :          * This ensures that tasks getting woken will be fully ordered against
    2622                 :            :          * their previous state and preserve Program Order.
    2623                 :            :          */
    2624                 :          3 :         smp_cond_load_acquire(&p->on_cpu, !VAL);
    2625                 :            : 
    2626                 :          3 :         p->sched_contributes_to_load = !!task_contributes_to_load(p);
    2627                 :          3 :         p->state = TASK_WAKING;
    2628                 :            : 
    2629                 :          3 :         if (p->in_iowait) {
    2630                 :          3 :                 delayacct_blkio_end(p);
    2631                 :          3 :                 atomic_dec(&task_rq(p)->nr_iowait);
    2632                 :            :         }
    2633                 :            : 
    2634                 :          3 :         cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
    2635                 :          3 :         if (task_cpu(p) != cpu) {
    2636                 :          3 :                 wake_flags |= WF_MIGRATED;
    2637                 :            :                 psi_ttwu_dequeue(p);
    2638                 :          3 :                 set_task_cpu(p, cpu);
    2639                 :            :         }
    2640                 :            : 
    2641                 :            : #else /* CONFIG_SMP */
    2642                 :            : 
    2643                 :            :         if (p->in_iowait) {
    2644                 :            :                 delayacct_blkio_end(p);
    2645                 :            :                 atomic_dec(&task_rq(p)->nr_iowait);
    2646                 :            :         }
    2647                 :            : 
    2648                 :            : #endif /* CONFIG_SMP */
    2649                 :            : 
    2650                 :          3 :         ttwu_queue(p, cpu, wake_flags);
    2651                 :            : unlock:
    2652                 :          3 :         raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    2653                 :            : out:
    2654                 :          3 :         if (success)
    2655                 :          3 :                 ttwu_stat(p, cpu, wake_flags);
    2656                 :          3 :         preempt_enable();
    2657                 :            : 
    2658                 :          3 :         return success;
    2659                 :            : }
    2660                 :            : 
    2661                 :            : /**
    2662                 :            :  * wake_up_process - Wake up a specific process
    2663                 :            :  * @p: The process to be woken up.
    2664                 :            :  *
    2665                 :            :  * Attempt to wake up the nominated process and move it to the set of runnable
    2666                 :            :  * processes.
    2667                 :            :  *
    2668                 :            :  * Return: 1 if the process was woken up, 0 if it was already running.
    2669                 :            :  *
    2670                 :            :  * This function executes a full memory barrier before accessing the task state.
    2671                 :            :  */
    2672                 :          3 : int wake_up_process(struct task_struct *p)
    2673                 :            : {
    2674                 :          3 :         return try_to_wake_up(p, TASK_NORMAL, 0);
    2675                 :            : }
    2676                 :            : EXPORT_SYMBOL(wake_up_process);
    2677                 :            : 
    2678                 :          3 : int wake_up_state(struct task_struct *p, unsigned int state)
    2679                 :            : {
    2680                 :          3 :         return try_to_wake_up(p, state, 0);
    2681                 :            : }
    2682                 :            : 
    2683                 :            : /*
    2684                 :            :  * Perform scheduler related setup for a newly forked process p.
    2685                 :            :  * p is forked by current.
    2686                 :            :  *
    2687                 :            :  * __sched_fork() is basic setup used by init_idle() too:
    2688                 :            :  */
    2689                 :          3 : static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
    2690                 :            : {
    2691                 :          3 :         p->on_rq                     = 0;
    2692                 :            : 
    2693                 :          3 :         p->se.on_rq                  = 0;
    2694                 :          3 :         p->se.exec_start             = 0;
    2695                 :          3 :         p->se.sum_exec_runtime               = 0;
    2696                 :          3 :         p->se.prev_sum_exec_runtime  = 0;
    2697                 :          3 :         p->se.nr_migrations          = 0;
    2698                 :          3 :         p->se.vruntime                       = 0;
    2699                 :          3 :         INIT_LIST_HEAD(&p->se.group_node);
    2700                 :            : 
    2701                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    2702                 :          3 :         p->se.cfs_rq                 = NULL;
    2703                 :            : #endif
    2704                 :            : 
    2705                 :            : #ifdef CONFIG_SCHEDSTATS
    2706                 :            :         /* Even if schedstat is disabled, there should not be garbage */
    2707                 :          3 :         memset(&p->se.statistics, 0, sizeof(p->se.statistics));
    2708                 :            : #endif
    2709                 :            : 
    2710                 :          3 :         RB_CLEAR_NODE(&p->dl.rb_node);
    2711                 :          3 :         init_dl_task_timer(&p->dl);
    2712                 :          3 :         init_dl_inactive_task_timer(&p->dl);
    2713                 :          3 :         __dl_clear_params(p);
    2714                 :            : 
    2715                 :          3 :         INIT_LIST_HEAD(&p->rt.run_list);
    2716                 :          3 :         p->rt.timeout                = 0;
    2717                 :          3 :         p->rt.time_slice     = sched_rr_timeslice;
    2718                 :          3 :         p->rt.on_rq          = 0;
    2719                 :          3 :         p->rt.on_list                = 0;
    2720                 :            : 
    2721                 :            : #ifdef CONFIG_PREEMPT_NOTIFIERS
    2722                 :            :         INIT_HLIST_HEAD(&p->preempt_notifiers);
    2723                 :            : #endif
    2724                 :            : 
    2725                 :            : #ifdef CONFIG_COMPACTION
    2726                 :          3 :         p->capture_control = NULL;
    2727                 :            : #endif
    2728                 :            :         init_numa_balancing(clone_flags, p);
    2729                 :          3 : }
    2730                 :            : 
    2731                 :            : DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
    2732                 :            : 
    2733                 :            : #ifdef CONFIG_NUMA_BALANCING
    2734                 :            : 
    2735                 :            : void set_numabalancing_state(bool enabled)
    2736                 :            : {
    2737                 :            :         if (enabled)
    2738                 :            :                 static_branch_enable(&sched_numa_balancing);
    2739                 :            :         else
    2740                 :            :                 static_branch_disable(&sched_numa_balancing);
    2741                 :            : }
    2742                 :            : 
    2743                 :            : #ifdef CONFIG_PROC_SYSCTL
    2744                 :            : int sysctl_numa_balancing(struct ctl_table *table, int write,
    2745                 :            :                          void __user *buffer, size_t *lenp, loff_t *ppos)
    2746                 :            : {
    2747                 :            :         struct ctl_table t;
    2748                 :            :         int err;
    2749                 :            :         int state = static_branch_likely(&sched_numa_balancing);
    2750                 :            : 
    2751                 :            :         if (write && !capable(CAP_SYS_ADMIN))
    2752                 :            :                 return -EPERM;
    2753                 :            : 
    2754                 :            :         t = *table;
    2755                 :            :         t.data = &state;
    2756                 :            :         err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
    2757                 :            :         if (err < 0)
    2758                 :            :                 return err;
    2759                 :            :         if (write)
    2760                 :            :                 set_numabalancing_state(state);
    2761                 :            :         return err;
    2762                 :            : }
    2763                 :            : #endif
    2764                 :            : #endif
    2765                 :            : 
    2766                 :            : #ifdef CONFIG_SCHEDSTATS
    2767                 :            : 
    2768                 :            : DEFINE_STATIC_KEY_FALSE(sched_schedstats);
    2769                 :            : static bool __initdata __sched_schedstats = false;
    2770                 :            : 
    2771                 :          3 : static void set_schedstats(bool enabled)
    2772                 :            : {
    2773                 :          3 :         if (enabled)
    2774                 :          0 :                 static_branch_enable(&sched_schedstats);
    2775                 :            :         else
    2776                 :          3 :                 static_branch_disable(&sched_schedstats);
    2777                 :          3 : }
    2778                 :            : 
    2779                 :          0 : void force_schedstat_enabled(void)
    2780                 :            : {
    2781                 :          0 :         if (!schedstat_enabled()) {
    2782                 :          0 :                 pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
    2783                 :          0 :                 static_branch_enable(&sched_schedstats);
    2784                 :            :         }
    2785                 :          0 : }
    2786                 :            : 
    2787                 :          0 : static int __init setup_schedstats(char *str)
    2788                 :            : {
    2789                 :            :         int ret = 0;
    2790                 :          0 :         if (!str)
    2791                 :            :                 goto out;
    2792                 :            : 
    2793                 :            :         /*
    2794                 :            :          * This code is called before jump labels have been set up, so we can't
    2795                 :            :          * change the static branch directly just yet.  Instead set a temporary
    2796                 :            :          * variable so init_schedstats() can do it later.
    2797                 :            :          */
    2798                 :          0 :         if (!strcmp(str, "enable")) {
    2799                 :          0 :                 __sched_schedstats = true;
    2800                 :            :                 ret = 1;
    2801                 :          0 :         } else if (!strcmp(str, "disable")) {
    2802                 :          0 :                 __sched_schedstats = false;
    2803                 :            :                 ret = 1;
    2804                 :            :         }
    2805                 :            : out:
    2806                 :          0 :         if (!ret)
    2807                 :          0 :                 pr_warn("Unable to parse schedstats=\n");
    2808                 :            : 
    2809                 :          0 :         return ret;
    2810                 :            : }
    2811                 :            : __setup("schedstats=", setup_schedstats);
    2812                 :            : 
    2813                 :          3 : static void __init init_schedstats(void)
    2814                 :            : {
    2815                 :          3 :         set_schedstats(__sched_schedstats);
    2816                 :          3 : }
    2817                 :            : 
    2818                 :            : #ifdef CONFIG_PROC_SYSCTL
    2819                 :          0 : int sysctl_schedstats(struct ctl_table *table, int write,
    2820                 :            :                          void __user *buffer, size_t *lenp, loff_t *ppos)
    2821                 :            : {
    2822                 :            :         struct ctl_table t;
    2823                 :            :         int err;
    2824                 :          0 :         int state = static_branch_likely(&sched_schedstats);
    2825                 :            : 
    2826                 :          0 :         if (write && !capable(CAP_SYS_ADMIN))
    2827                 :            :                 return -EPERM;
    2828                 :            : 
    2829                 :          0 :         t = *table;
    2830                 :          0 :         t.data = &state;
    2831                 :          0 :         err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
    2832                 :          0 :         if (err < 0)
    2833                 :            :                 return err;
    2834                 :          0 :         if (write)
    2835                 :          0 :                 set_schedstats(state);
    2836                 :          0 :         return err;
    2837                 :            : }
    2838                 :            : #endif /* CONFIG_PROC_SYSCTL */
    2839                 :            : #else  /* !CONFIG_SCHEDSTATS */
    2840                 :            : static inline void init_schedstats(void) {}
    2841                 :            : #endif /* CONFIG_SCHEDSTATS */
    2842                 :            : 
    2843                 :            : /*
    2844                 :            :  * fork()/clone()-time setup:
    2845                 :            :  */
    2846                 :          3 : int sched_fork(unsigned long clone_flags, struct task_struct *p)
    2847                 :            : {
    2848                 :            :         unsigned long flags;
    2849                 :            : 
    2850                 :          3 :         __sched_fork(clone_flags, p);
    2851                 :            :         /*
    2852                 :            :          * We mark the process as NEW here. This guarantees that
    2853                 :            :          * nobody will actually run it, and a signal or other external
    2854                 :            :          * event cannot wake it up and insert it on the runqueue either.
    2855                 :            :          */
    2856                 :          3 :         p->state = TASK_NEW;
    2857                 :            : 
    2858                 :            :         /*
    2859                 :            :          * Make sure we do not leak PI boosting priority to the child.
    2860                 :            :          */
    2861                 :          3 :         p->prio = current->normal_prio;
    2862                 :            : 
    2863                 :            :         uclamp_fork(p);
    2864                 :            : 
    2865                 :            :         /*
    2866                 :            :          * Revert to default priority/policy on fork if requested.
    2867                 :            :          */
    2868                 :          3 :         if (unlikely(p->sched_reset_on_fork)) {
    2869                 :          0 :                 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
    2870                 :          0 :                         p->policy = SCHED_NORMAL;
    2871                 :          0 :                         p->static_prio = NICE_TO_PRIO(0);
    2872                 :          0 :                         p->rt_priority = 0;
    2873                 :          0 :                 } else if (PRIO_TO_NICE(p->static_prio) < 0)
    2874                 :          0 :                         p->static_prio = NICE_TO_PRIO(0);
    2875                 :            : 
    2876                 :          0 :                 p->prio = p->normal_prio = __normal_prio(p);
    2877                 :          0 :                 set_load_weight(p, false);
    2878                 :            : 
    2879                 :            :                 /*
    2880                 :            :                  * We don't need the reset flag anymore after the fork. It has
    2881                 :            :                  * fulfilled its duty:
    2882                 :            :                  */
    2883                 :          0 :                 p->sched_reset_on_fork = 0;
    2884                 :            :         }
    2885                 :            : 
    2886                 :          3 :         if (dl_prio(p->prio))
    2887                 :            :                 return -EAGAIN;
    2888                 :          3 :         else if (rt_prio(p->prio))
    2889                 :          0 :                 p->sched_class = &rt_sched_class;
    2890                 :            :         else
    2891                 :          3 :                 p->sched_class = &fair_sched_class;
    2892                 :            : 
    2893                 :          3 :         init_entity_runnable_average(&p->se);
    2894                 :            : 
    2895                 :            :         /*
    2896                 :            :          * The child is not yet in the pid-hash so no cgroup attach races,
    2897                 :            :          * and the cgroup is pinned to this child due to cgroup_fork()
    2898                 :            :          * is ran before sched_fork().
    2899                 :            :          *
    2900                 :            :          * Silence PROVE_RCU.
    2901                 :            :          */
    2902                 :          3 :         raw_spin_lock_irqsave(&p->pi_lock, flags);
    2903                 :          3 :         rseq_migrate(p);
    2904                 :            :         /*
    2905                 :            :          * We're setting the CPU for the first time, we don't migrate,
    2906                 :            :          * so use __set_task_cpu().
    2907                 :            :          */
    2908                 :          3 :         __set_task_cpu(p, smp_processor_id());
    2909                 :          3 :         if (p->sched_class->task_fork)
    2910                 :          3 :                 p->sched_class->task_fork(p);
    2911                 :          3 :         raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    2912                 :            : 
    2913                 :            : #ifdef CONFIG_SCHED_INFO
    2914                 :            :         if (likely(sched_info_on()))
    2915                 :          3 :                 memset(&p->sched_info, 0, sizeof(p->sched_info));
    2916                 :            : #endif
    2917                 :            : #if defined(CONFIG_SMP)
    2918                 :          3 :         p->on_cpu = 0;
    2919                 :            : #endif
    2920                 :          3 :         init_task_preempt_count(p);
    2921                 :            : #ifdef CONFIG_SMP
    2922                 :            :         plist_node_init(&p->pushable_tasks, MAX_PRIO);
    2923                 :          3 :         RB_CLEAR_NODE(&p->pushable_dl_tasks);
    2924                 :            : #endif
    2925                 :          3 :         return 0;
    2926                 :            : }
    2927                 :            : 
    2928                 :          3 : unsigned long to_ratio(u64 period, u64 runtime)
    2929                 :            : {
    2930                 :          3 :         if (runtime == RUNTIME_INF)
    2931                 :            :                 return BW_UNIT;
    2932                 :            : 
    2933                 :            :         /*
    2934                 :            :          * Doing this here saves a lot of checks in all
    2935                 :            :          * the calling paths, and returning zero seems
    2936                 :            :          * safe for them anyway.
    2937                 :            :          */
    2938                 :          3 :         if (period == 0)
    2939                 :            :                 return 0;
    2940                 :            : 
    2941                 :          3 :         return div64_u64(runtime << BW_SHIFT, period);
    2942                 :            : }
    2943                 :            : 
    2944                 :            : /*
    2945                 :            :  * wake_up_new_task - wake up a newly created task for the first time.
    2946                 :            :  *
    2947                 :            :  * This function will do some initial scheduler statistics housekeeping
    2948                 :            :  * that must be done for every newly created context, then puts the task
    2949                 :            :  * on the runqueue and wakes it.
    2950                 :            :  */
    2951                 :          3 : void wake_up_new_task(struct task_struct *p)
    2952                 :            : {
    2953                 :            :         struct rq_flags rf;
    2954                 :            :         struct rq *rq;
    2955                 :            : 
    2956                 :          3 :         raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
    2957                 :          3 :         p->state = TASK_RUNNING;
    2958                 :            : #ifdef CONFIG_SMP
    2959                 :            :         /*
    2960                 :            :          * Fork balancing, do it here and not earlier because:
    2961                 :            :          *  - cpus_ptr can change in the fork path
    2962                 :            :          *  - any previously selected CPU might disappear through hotplug
    2963                 :            :          *
    2964                 :            :          * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
    2965                 :            :          * as we're not fully set-up yet.
    2966                 :            :          */
    2967                 :          3 :         p->recent_used_cpu = task_cpu(p);
    2968                 :          3 :         rseq_migrate(p);
    2969                 :          3 :         __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
    2970                 :            : #endif
    2971                 :          3 :         rq = __task_rq_lock(p, &rf);
    2972                 :          3 :         update_rq_clock(rq);
    2973                 :          3 :         post_init_entity_util_avg(p);
    2974                 :            : 
    2975                 :          3 :         activate_task(rq, p, ENQUEUE_NOCLOCK);
    2976                 :          3 :         trace_sched_wakeup_new(p);
    2977                 :          3 :         check_preempt_curr(rq, p, WF_FORK);
    2978                 :            : #ifdef CONFIG_SMP
    2979                 :          3 :         if (p->sched_class->task_woken) {
    2980                 :            :                 /*
    2981                 :            :                  * Nothing relies on rq->lock after this, so its fine to
    2982                 :            :                  * drop it.
    2983                 :            :                  */
    2984                 :            :                 rq_unpin_lock(rq, &rf);
    2985                 :          0 :                 p->sched_class->task_woken(rq, p);
    2986                 :            :                 rq_repin_lock(rq, &rf);
    2987                 :            :         }
    2988                 :            : #endif
    2989                 :          3 :         task_rq_unlock(rq, p, &rf);
    2990                 :          3 : }
    2991                 :            : 
    2992                 :            : #ifdef CONFIG_PREEMPT_NOTIFIERS
    2993                 :            : 
    2994                 :            : static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key);
    2995                 :            : 
    2996                 :            : void preempt_notifier_inc(void)
    2997                 :            : {
    2998                 :            :         static_branch_inc(&preempt_notifier_key);
    2999                 :            : }
    3000                 :            : EXPORT_SYMBOL_GPL(preempt_notifier_inc);
    3001                 :            : 
    3002                 :            : void preempt_notifier_dec(void)
    3003                 :            : {
    3004                 :            :         static_branch_dec(&preempt_notifier_key);
    3005                 :            : }
    3006                 :            : EXPORT_SYMBOL_GPL(preempt_notifier_dec);
    3007                 :            : 
    3008                 :            : /**
    3009                 :            :  * preempt_notifier_register - tell me when current is being preempted & rescheduled
    3010                 :            :  * @notifier: notifier struct to register
    3011                 :            :  */
    3012                 :            : void preempt_notifier_register(struct preempt_notifier *notifier)
    3013                 :            : {
    3014                 :            :         if (!static_branch_unlikely(&preempt_notifier_key))
    3015                 :            :                 WARN(1, "registering preempt_notifier while notifiers disabled\n");
    3016                 :            : 
    3017                 :            :         hlist_add_head(&notifier->link, &current->preempt_notifiers);
    3018                 :            : }
    3019                 :            : EXPORT_SYMBOL_GPL(preempt_notifier_register);
    3020                 :            : 
    3021                 :            : /**
    3022                 :            :  * preempt_notifier_unregister - no longer interested in preemption notifications
    3023                 :            :  * @notifier: notifier struct to unregister
    3024                 :            :  *
    3025                 :            :  * This is *not* safe to call from within a preemption notifier.
    3026                 :            :  */
    3027                 :            : void preempt_notifier_unregister(struct preempt_notifier *notifier)
    3028                 :            : {
    3029                 :            :         hlist_del(&notifier->link);
    3030                 :            : }
    3031                 :            : EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
    3032                 :            : 
    3033                 :            : static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
    3034                 :            : {
    3035                 :            :         struct preempt_notifier *notifier;
    3036                 :            : 
    3037                 :            :         hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
    3038                 :            :                 notifier->ops->sched_in(notifier, raw_smp_processor_id());
    3039                 :            : }
    3040                 :            : 
    3041                 :            : static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
    3042                 :            : {
    3043                 :            :         if (static_branch_unlikely(&preempt_notifier_key))
    3044                 :            :                 __fire_sched_in_preempt_notifiers(curr);
    3045                 :            : }
    3046                 :            : 
    3047                 :            : static void
    3048                 :            : __fire_sched_out_preempt_notifiers(struct task_struct *curr,
    3049                 :            :                                    struct task_struct *next)
    3050                 :            : {
    3051                 :            :         struct preempt_notifier *notifier;
    3052                 :            : 
    3053                 :            :         hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
    3054                 :            :                 notifier->ops->sched_out(notifier, next);
    3055                 :            : }
    3056                 :            : 
    3057                 :            : static __always_inline void
    3058                 :            : fire_sched_out_preempt_notifiers(struct task_struct *curr,
    3059                 :            :                                  struct task_struct *next)
    3060                 :            : {
    3061                 :            :         if (static_branch_unlikely(&preempt_notifier_key))
    3062                 :            :                 __fire_sched_out_preempt_notifiers(curr, next);
    3063                 :            : }
    3064                 :            : 
    3065                 :            : #else /* !CONFIG_PREEMPT_NOTIFIERS */
    3066                 :            : 
    3067                 :            : static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
    3068                 :            : {
    3069                 :            : }
    3070                 :            : 
    3071                 :            : static inline void
    3072                 :            : fire_sched_out_preempt_notifiers(struct task_struct *curr,
    3073                 :            :                                  struct task_struct *next)
    3074                 :            : {
    3075                 :            : }
    3076                 :            : 
    3077                 :            : #endif /* CONFIG_PREEMPT_NOTIFIERS */
    3078                 :            : 
    3079                 :            : static inline void prepare_task(struct task_struct *next)
    3080                 :            : {
    3081                 :            : #ifdef CONFIG_SMP
    3082                 :            :         /*
    3083                 :            :          * Claim the task as running, we do this before switching to it
    3084                 :            :          * such that any running task will have this set.
    3085                 :            :          */
    3086                 :          3 :         next->on_cpu = 1;
    3087                 :            : #endif
    3088                 :            : }
    3089                 :            : 
    3090                 :            : static inline void finish_task(struct task_struct *prev)
    3091                 :            : {
    3092                 :            : #ifdef CONFIG_SMP
    3093                 :            :         /*
    3094                 :            :          * After ->on_cpu is cleared, the task can be moved to a different CPU.
    3095                 :            :          * We must ensure this doesn't happen until the switch is completely
    3096                 :            :          * finished.
    3097                 :            :          *
    3098                 :            :          * In particular, the load of prev->state in finish_task_switch() must
    3099                 :            :          * happen before this.
    3100                 :            :          *
    3101                 :            :          * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
    3102                 :            :          */
    3103                 :          3 :         smp_store_release(&prev->on_cpu, 0);
    3104                 :            : #endif
    3105                 :            : }
    3106                 :            : 
    3107                 :            : static inline void
    3108                 :          3 : prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf)
    3109                 :            : {
    3110                 :            :         /*
    3111                 :            :          * Since the runqueue lock will be released by the next
    3112                 :            :          * task (which is an invalid locking op but in the case
    3113                 :            :          * of the scheduler it's an obvious special-case), so we
    3114                 :            :          * do an early lockdep release here:
    3115                 :            :          */
    3116                 :            :         rq_unpin_lock(rq, rf);
    3117                 :            :         spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
    3118                 :            : #ifdef CONFIG_DEBUG_SPINLOCK
    3119                 :            :         /* this is a valid case when another task releases the spinlock */
    3120                 :            :         rq->lock.owner = next;
    3121                 :            : #endif
    3122                 :          3 : }
    3123                 :            : 
    3124                 :            : static inline void finish_lock_switch(struct rq *rq)
    3125                 :            : {
    3126                 :            :         /*
    3127                 :            :          * If we are tracking spinlock dependencies then we have to
    3128                 :            :          * fix up the runqueue lock - which gets 'carried over' from
    3129                 :            :          * prev into current:
    3130                 :            :          */
    3131                 :            :         spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
    3132                 :          3 :         raw_spin_unlock_irq(&rq->lock);
    3133                 :            : }
    3134                 :            : 
    3135                 :            : /*
    3136                 :            :  * NOP if the arch has not defined these:
    3137                 :            :  */
    3138                 :            : 
    3139                 :            : #ifndef prepare_arch_switch
    3140                 :            : # define prepare_arch_switch(next)      do { } while (0)
    3141                 :            : #endif
    3142                 :            : 
    3143                 :            : #ifndef finish_arch_post_lock_switch
    3144                 :            : # define finish_arch_post_lock_switch() do { } while (0)
    3145                 :            : #endif
    3146                 :            : 
    3147                 :            : /**
    3148                 :            :  * prepare_task_switch - prepare to switch tasks
    3149                 :            :  * @rq: the runqueue preparing to switch
    3150                 :            :  * @prev: the current task that is being switched out
    3151                 :            :  * @next: the task we are going to switch to.
    3152                 :            :  *
    3153                 :            :  * This is called with the rq lock held and interrupts off. It must
    3154                 :            :  * be paired with a subsequent finish_task_switch after the context
    3155                 :            :  * switch.
    3156                 :            :  *
    3157                 :            :  * prepare_task_switch sets up locking and calls architecture specific
    3158                 :            :  * hooks.
    3159                 :            :  */
    3160                 :            : static inline void
    3161                 :          3 : prepare_task_switch(struct rq *rq, struct task_struct *prev,
    3162                 :            :                     struct task_struct *next)
    3163                 :            : {
    3164                 :            :         kcov_prepare_switch(prev);
    3165                 :            :         sched_info_switch(rq, prev, next);
    3166                 :          3 :         perf_event_task_sched_out(prev, next);
    3167                 :          3 :         rseq_preempt(prev);
    3168                 :            :         fire_sched_out_preempt_notifiers(prev, next);
    3169                 :            :         prepare_task(next);
    3170                 :            :         prepare_arch_switch(next);
    3171                 :          3 : }
    3172                 :            : 
    3173                 :            : /**
    3174                 :            :  * finish_task_switch - clean up after a task-switch
    3175                 :            :  * @prev: the thread we just switched away from.
    3176                 :            :  *
    3177                 :            :  * finish_task_switch must be called after the context switch, paired
    3178                 :            :  * with a prepare_task_switch call before the context switch.
    3179                 :            :  * finish_task_switch will reconcile locking set up by prepare_task_switch,
    3180                 :            :  * and do any other architecture-specific cleanup actions.
    3181                 :            :  *
    3182                 :            :  * Note that we may have delayed dropping an mm in context_switch(). If
    3183                 :            :  * so, we finish that here outside of the runqueue lock. (Doing it
    3184                 :            :  * with the lock held can cause deadlocks; see schedule() for
    3185                 :            :  * details.)
    3186                 :            :  *
    3187                 :            :  * The context switch have flipped the stack from under us and restored the
    3188                 :            :  * local variables which were saved when this task called schedule() in the
    3189                 :            :  * past. prev == current is still correct but we need to recalculate this_rq
    3190                 :            :  * because prev may have moved to another CPU.
    3191                 :            :  */
    3192                 :          3 : static struct rq *finish_task_switch(struct task_struct *prev)
    3193                 :            :         __releases(rq->lock)
    3194                 :            : {
    3195                 :          3 :         struct rq *rq = this_rq();
    3196                 :          3 :         struct mm_struct *mm = rq->prev_mm;
    3197                 :            :         long prev_state;
    3198                 :            : 
    3199                 :            :         /*
    3200                 :            :          * The previous task will have left us with a preempt_count of 2
    3201                 :            :          * because it left us after:
    3202                 :            :          *
    3203                 :            :          *      schedule()
    3204                 :            :          *        preempt_disable();                    // 1
    3205                 :            :          *        __schedule()
    3206                 :            :          *          raw_spin_lock_irq(&rq->lock) // 2
    3207                 :            :          *
    3208                 :            :          * Also, see FORK_PREEMPT_COUNT.
    3209                 :            :          */
    3210                 :          3 :         if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
    3211                 :            :                       "corrupted preempt_count: %s/%d/0x%x\n",
    3212                 :            :                       current->comm, current->pid, preempt_count()))
    3213                 :            :                 preempt_count_set(FORK_PREEMPT_COUNT);
    3214                 :            : 
    3215                 :          3 :         rq->prev_mm = NULL;
    3216                 :            : 
    3217                 :            :         /*
    3218                 :            :          * A task struct has one reference for the use as "current".
    3219                 :            :          * If a task dies, then it sets TASK_DEAD in tsk->state and calls
    3220                 :            :          * schedule one last time. The schedule call will never return, and
    3221                 :            :          * the scheduled task must drop that reference.
    3222                 :            :          *
    3223                 :            :          * We must observe prev->state before clearing prev->on_cpu (in
    3224                 :            :          * finish_task), otherwise a concurrent wakeup can get prev
    3225                 :            :          * running on another CPU and we could rave with its RUNNING -> DEAD
    3226                 :            :          * transition, resulting in a double drop.
    3227                 :            :          */
    3228                 :          3 :         prev_state = prev->state;
    3229                 :            :         vtime_task_switch(prev);
    3230                 :          3 :         perf_event_task_sched_in(prev, current);
    3231                 :            :         finish_task(prev);
    3232                 :            :         finish_lock_switch(rq);
    3233                 :            :         finish_arch_post_lock_switch();
    3234                 :            :         kcov_finish_switch(current);
    3235                 :            : 
    3236                 :            :         fire_sched_in_preempt_notifiers(current);
    3237                 :            :         /*
    3238                 :            :          * When switching through a kernel thread, the loop in
    3239                 :            :          * membarrier_{private,global}_expedited() may have observed that
    3240                 :            :          * kernel thread and not issued an IPI. It is therefore possible to
    3241                 :            :          * schedule between user->kernel->user threads without passing though
    3242                 :            :          * switch_mm(). Membarrier requires a barrier after storing to
    3243                 :            :          * rq->curr, before returning to userspace, so provide them here:
    3244                 :            :          *
    3245                 :            :          * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly
    3246                 :            :          *   provided by mmdrop(),
    3247                 :            :          * - a sync_core for SYNC_CORE.
    3248                 :            :          */
    3249                 :          3 :         if (mm) {
    3250                 :            :                 membarrier_mm_sync_core_before_usermode(mm);
    3251                 :          3 :                 mmdrop(mm);
    3252                 :            :         }
    3253                 :          3 :         if (unlikely(prev_state == TASK_DEAD)) {
    3254                 :          3 :                 if (prev->sched_class->task_dead)
    3255                 :          3 :                         prev->sched_class->task_dead(prev);
    3256                 :            : 
    3257                 :            :                 /*
    3258                 :            :                  * Remove function-return probe instances associated with this
    3259                 :            :                  * task and put them back on the free list.
    3260                 :            :                  */
    3261                 :          3 :                 kprobe_flush_task(prev);
    3262                 :            : 
    3263                 :            :                 /* Task is done with its stack. */
    3264                 :            :                 put_task_stack(prev);
    3265                 :            : 
    3266                 :          3 :                 put_task_struct_rcu_user(prev);
    3267                 :            :         }
    3268                 :            : 
    3269                 :            :         tick_nohz_task_switch();
    3270                 :          3 :         return rq;
    3271                 :            : }
    3272                 :            : 
    3273                 :            : #ifdef CONFIG_SMP
    3274                 :            : 
    3275                 :            : /* rq->lock is NOT held, but preemption is disabled */
    3276                 :          0 : static void __balance_callback(struct rq *rq)
    3277                 :            : {
    3278                 :            :         struct callback_head *head, *next;
    3279                 :            :         void (*func)(struct rq *rq);
    3280                 :            :         unsigned long flags;
    3281                 :            : 
    3282                 :          0 :         raw_spin_lock_irqsave(&rq->lock, flags);
    3283                 :          0 :         head = rq->balance_callback;
    3284                 :          0 :         rq->balance_callback = NULL;
    3285                 :          0 :         while (head) {
    3286                 :          0 :                 func = (void (*)(struct rq *))head->func;
    3287                 :          0 :                 next = head->next;
    3288                 :          0 :                 head->next = NULL;
    3289                 :            :                 head = next;
    3290                 :            : 
    3291                 :          0 :                 func(rq);
    3292                 :            :         }
    3293                 :          0 :         raw_spin_unlock_irqrestore(&rq->lock, flags);
    3294                 :          0 : }
    3295                 :            : 
    3296                 :            : static inline void balance_callback(struct rq *rq)
    3297                 :            : {
    3298                 :          3 :         if (unlikely(rq->balance_callback))
    3299                 :          0 :                 __balance_callback(rq);
    3300                 :            : }
    3301                 :            : 
    3302                 :            : #else
    3303                 :            : 
    3304                 :            : static inline void balance_callback(struct rq *rq)
    3305                 :            : {
    3306                 :            : }
    3307                 :            : 
    3308                 :            : #endif
    3309                 :            : 
    3310                 :            : /**
    3311                 :            :  * schedule_tail - first thing a freshly forked thread must call.
    3312                 :            :  * @prev: the thread we just switched away from.
    3313                 :            :  */
    3314                 :          3 : asmlinkage __visible void schedule_tail(struct task_struct *prev)
    3315                 :            :         __releases(rq->lock)
    3316                 :            : {
    3317                 :            :         struct rq *rq;
    3318                 :            : 
    3319                 :            :         /*
    3320                 :            :          * New tasks start with FORK_PREEMPT_COUNT, see there and
    3321                 :            :          * finish_task_switch() for details.
    3322                 :            :          *
    3323                 :            :          * finish_task_switch() will drop rq->lock() and lower preempt_count
    3324                 :            :          * and the preempt_enable() will end up enabling preemption (on
    3325                 :            :          * PREEMPT_COUNT kernels).
    3326                 :            :          */
    3327                 :            : 
    3328                 :          3 :         rq = finish_task_switch(prev);
    3329                 :            :         balance_callback(rq);
    3330                 :          3 :         preempt_enable();
    3331                 :            : 
    3332                 :          3 :         if (current->set_child_tid)
    3333                 :          3 :                 put_user(task_pid_vnr(current), current->set_child_tid);
    3334                 :            : 
    3335                 :          3 :         calculate_sigpending();
    3336                 :          3 : }
    3337                 :            : 
    3338                 :            : /*
    3339                 :            :  * context_switch - switch to the new MM and the new thread's register state.
    3340                 :            :  */
    3341                 :            : static __always_inline struct rq *
    3342                 :            : context_switch(struct rq *rq, struct task_struct *prev,
    3343                 :            :                struct task_struct *next, struct rq_flags *rf)
    3344                 :            : {
    3345                 :          3 :         prepare_task_switch(rq, prev, next);
    3346                 :            : 
    3347                 :            :         /*
    3348                 :            :          * For paravirt, this is coupled with an exit in switch_to to
    3349                 :            :          * combine the page table reload and the switch backend into
    3350                 :            :          * one hypercall.
    3351                 :            :          */
    3352                 :            :         arch_start_context_switch(prev);
    3353                 :            : 
    3354                 :            :         /*
    3355                 :            :          * kernel -> kernel   lazy + transfer active
    3356                 :            :          *   user -> kernel   lazy + mmgrab() active
    3357                 :            :          *
    3358                 :            :          * kernel ->   user   switch + mmdrop() active
    3359                 :            :          *   user ->   user   switch
    3360                 :            :          */
    3361                 :          3 :         if (!next->mm) {                                // to kernel
    3362                 :          3 :                 enter_lazy_tlb(prev->active_mm, next);
    3363                 :            : 
    3364                 :          3 :                 next->active_mm = prev->active_mm;
    3365                 :          3 :                 if (prev->mm)                           // from user
    3366                 :          3 :                         mmgrab(prev->active_mm);
    3367                 :            :                 else
    3368                 :          3 :                         prev->active_mm = NULL;
    3369                 :            :         } else {                                        // to user
    3370                 :          3 :                 membarrier_switch_mm(rq, prev->active_mm, next->mm);
    3371                 :            :                 /*
    3372                 :            :                  * sys_membarrier() requires an smp_mb() between setting
    3373                 :            :                  * rq->curr / membarrier_switch_mm() and returning to userspace.
    3374                 :            :                  *
    3375                 :            :                  * The below provides this either through switch_mm(), or in
    3376                 :            :                  * case 'prev->active_mm == next->mm' through
    3377                 :            :                  * finish_task_switch()'s mmdrop().
    3378                 :            :                  */
    3379                 :          3 :                 switch_mm_irqs_off(prev->active_mm, next->mm, next);
    3380                 :            : 
    3381                 :          3 :                 if (!prev->mm) {                        // from kernel
    3382                 :            :                         /* will mmdrop() in finish_task_switch(). */
    3383                 :          3 :                         rq->prev_mm = prev->active_mm;
    3384                 :          3 :                         prev->active_mm = NULL;
    3385                 :            :                 }
    3386                 :            :         }
    3387                 :            : 
    3388                 :          3 :         rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
    3389                 :            : 
    3390                 :          3 :         prepare_lock_switch(rq, next, rf);
    3391                 :            : 
    3392                 :            :         /* Here we just switch the register state and the stack. */
    3393                 :          3 :         switch_to(prev, next, prev);
    3394                 :          3 :         barrier();
    3395                 :            : 
    3396                 :          3 :         return finish_task_switch(prev);
    3397                 :            : }
    3398                 :            : 
    3399                 :            : /*
    3400                 :            :  * nr_running and nr_context_switches:
    3401                 :            :  *
    3402                 :            :  * externally visible scheduler statistics: current number of runnable
    3403                 :            :  * threads, total number of context switches performed since bootup.
    3404                 :            :  */
    3405                 :          0 : unsigned long nr_running(void)
    3406                 :            : {
    3407                 :            :         unsigned long i, sum = 0;
    3408                 :            : 
    3409                 :          0 :         for_each_online_cpu(i)
    3410                 :          0 :                 sum += cpu_rq(i)->nr_running;
    3411                 :            : 
    3412                 :          0 :         return sum;
    3413                 :            : }
    3414                 :            : 
    3415                 :            : /*
    3416                 :            :  * Check if only the current task is running on the CPU.
    3417                 :            :  *
    3418                 :            :  * Caution: this function does not check that the caller has disabled
    3419                 :            :  * preemption, thus the result might have a time-of-check-to-time-of-use
    3420                 :            :  * race.  The caller is responsible to use it correctly, for example:
    3421                 :            :  *
    3422                 :            :  * - from a non-preemptible section (of course)
    3423                 :            :  *
    3424                 :            :  * - from a thread that is bound to a single CPU
    3425                 :            :  *
    3426                 :            :  * - in a loop with very short iterations (e.g. a polling loop)
    3427                 :            :  */
    3428                 :          0 : bool single_task_running(void)
    3429                 :            : {
    3430                 :          0 :         return raw_rq()->nr_running == 1;
    3431                 :            : }
    3432                 :            : EXPORT_SYMBOL(single_task_running);
    3433                 :            : 
    3434                 :          3 : unsigned long long nr_context_switches(void)
    3435                 :            : {
    3436                 :            :         int i;
    3437                 :            :         unsigned long long sum = 0;
    3438                 :            : 
    3439                 :          3 :         for_each_possible_cpu(i)
    3440                 :          3 :                 sum += cpu_rq(i)->nr_switches;
    3441                 :            : 
    3442                 :          3 :         return sum;
    3443                 :            : }
    3444                 :            : 
    3445                 :            : /*
    3446                 :            :  * Consumers of these two interfaces, like for example the cpuidle menu
    3447                 :            :  * governor, are using nonsensical data. Preferring shallow idle state selection
    3448                 :            :  * for a CPU that has IO-wait which might not even end up running the task when
    3449                 :            :  * it does become runnable.
    3450                 :            :  */
    3451                 :            : 
    3452                 :          3 : unsigned long nr_iowait_cpu(int cpu)
    3453                 :            : {
    3454                 :          3 :         return atomic_read(&cpu_rq(cpu)->nr_iowait);
    3455                 :            : }
    3456                 :            : 
    3457                 :            : /*
    3458                 :            :  * IO-wait accounting, and how its mostly bollocks (on SMP).
    3459                 :            :  *
    3460                 :            :  * The idea behind IO-wait account is to account the idle time that we could
    3461                 :            :  * have spend running if it were not for IO. That is, if we were to improve the
    3462                 :            :  * storage performance, we'd have a proportional reduction in IO-wait time.
    3463                 :            :  *
    3464                 :            :  * This all works nicely on UP, where, when a task blocks on IO, we account
    3465                 :            :  * idle time as IO-wait, because if the storage were faster, it could've been
    3466                 :            :  * running and we'd not be idle.
    3467                 :            :  *
    3468                 :            :  * This has been extended to SMP, by doing the same for each CPU. This however
    3469                 :            :  * is broken.
    3470                 :            :  *
    3471                 :            :  * Imagine for instance the case where two tasks block on one CPU, only the one
    3472                 :            :  * CPU will have IO-wait accounted, while the other has regular idle. Even
    3473                 :            :  * though, if the storage were faster, both could've ran at the same time,
    3474                 :            :  * utilising both CPUs.
    3475                 :            :  *
    3476                 :            :  * This means, that when looking globally, the current IO-wait accounting on
    3477                 :            :  * SMP is a lower bound, by reason of under accounting.
    3478                 :            :  *
    3479                 :            :  * Worse, since the numbers are provided per CPU, they are sometimes
    3480                 :            :  * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly
    3481                 :            :  * associated with any one particular CPU, it can wake to another CPU than it
    3482                 :            :  * blocked on. This means the per CPU IO-wait number is meaningless.
    3483                 :            :  *
    3484                 :            :  * Task CPU affinities can make all that even more 'interesting'.
    3485                 :            :  */
    3486                 :            : 
    3487                 :          0 : unsigned long nr_iowait(void)
    3488                 :            : {
    3489                 :            :         unsigned long i, sum = 0;
    3490                 :            : 
    3491                 :          0 :         for_each_possible_cpu(i)
    3492                 :          0 :                 sum += nr_iowait_cpu(i);
    3493                 :            : 
    3494                 :          0 :         return sum;
    3495                 :            : }
    3496                 :            : 
    3497                 :            : #ifdef CONFIG_SMP
    3498                 :            : 
    3499                 :            : /*
    3500                 :            :  * sched_exec - execve() is a valuable balancing opportunity, because at
    3501                 :            :  * this point the task has the smallest effective memory and cache footprint.
    3502                 :            :  */
    3503                 :          3 : void sched_exec(void)
    3504                 :            : {
    3505                 :          3 :         struct task_struct *p = current;
    3506                 :            :         unsigned long flags;
    3507                 :            :         int dest_cpu;
    3508                 :            : 
    3509                 :          3 :         raw_spin_lock_irqsave(&p->pi_lock, flags);
    3510                 :          3 :         dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
    3511                 :          3 :         if (dest_cpu == smp_processor_id())
    3512                 :            :                 goto unlock;
    3513                 :            : 
    3514                 :          3 :         if (likely(cpu_active(dest_cpu))) {
    3515                 :          3 :                 struct migration_arg arg = { p, dest_cpu };
    3516                 :            : 
    3517                 :          3 :                 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    3518                 :          3 :                 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
    3519                 :          3 :                 return;
    3520                 :            :         }
    3521                 :            : unlock:
    3522                 :          3 :         raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    3523                 :            : }
    3524                 :            : 
    3525                 :            : #endif
    3526                 :            : 
    3527                 :            : DEFINE_PER_CPU(struct kernel_stat, kstat);
    3528                 :            : DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
    3529                 :            : 
    3530                 :            : EXPORT_PER_CPU_SYMBOL(kstat);
    3531                 :            : EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
    3532                 :            : 
    3533                 :            : /*
    3534                 :            :  * The function fair_sched_class.update_curr accesses the struct curr
    3535                 :            :  * and its field curr->exec_start; when called from task_sched_runtime(),
    3536                 :            :  * we observe a high rate of cache misses in practice.
    3537                 :            :  * Prefetching this data results in improved performance.
    3538                 :            :  */
    3539                 :            : static inline void prefetch_curr_exec_start(struct task_struct *p)
    3540                 :            : {
    3541                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    3542                 :          3 :         struct sched_entity *curr = (&p->se)->cfs_rq->curr;
    3543                 :            : #else
    3544                 :            :         struct sched_entity *curr = (&task_rq(p)->cfs)->curr;
    3545                 :            : #endif
    3546                 :            :         prefetch(curr);
    3547                 :          3 :         prefetch(&curr->exec_start);
    3548                 :            : }
    3549                 :            : 
    3550                 :            : /*
    3551                 :            :  * Return accounted runtime for the task.
    3552                 :            :  * In case the task is currently running, return the runtime plus current's
    3553                 :            :  * pending runtime that have not been accounted yet.
    3554                 :            :  */
    3555                 :          3 : unsigned long long task_sched_runtime(struct task_struct *p)
    3556                 :            : {
    3557                 :            :         struct rq_flags rf;
    3558                 :            :         struct rq *rq;
    3559                 :            :         u64 ns;
    3560                 :            : 
    3561                 :            : #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
    3562                 :            :         /*
    3563                 :            :          * 64-bit doesn't need locks to atomically read a 64-bit value.
    3564                 :            :          * So we have a optimization chance when the task's delta_exec is 0.
    3565                 :            :          * Reading ->on_cpu is racy, but this is ok.
    3566                 :            :          *
    3567                 :            :          * If we race with it leaving CPU, we'll take a lock. So we're correct.
    3568                 :            :          * If we race with it entering CPU, unaccounted time is 0. This is
    3569                 :            :          * indistinguishable from the read occurring a few cycles earlier.
    3570                 :            :          * If we see ->on_cpu without ->on_rq, the task is leaving, and has
    3571                 :            :          * been accounted, so we're correct here as well.
    3572                 :            :          */
    3573                 :            :         if (!p->on_cpu || !task_on_rq_queued(p))
    3574                 :            :                 return p->se.sum_exec_runtime;
    3575                 :            : #endif
    3576                 :            : 
    3577                 :          3 :         rq = task_rq_lock(p, &rf);
    3578                 :            :         /*
    3579                 :            :          * Must be ->curr _and_ ->on_rq.  If dequeued, we would
    3580                 :            :          * project cycles that may never be accounted to this
    3581                 :            :          * thread, breaking clock_gettime().
    3582                 :            :          */
    3583                 :          3 :         if (task_current(rq, p) && task_on_rq_queued(p)) {
    3584                 :            :                 prefetch_curr_exec_start(p);
    3585                 :          3 :                 update_rq_clock(rq);
    3586                 :          3 :                 p->sched_class->update_curr(rq);
    3587                 :            :         }
    3588                 :          3 :         ns = p->se.sum_exec_runtime;
    3589                 :          3 :         task_rq_unlock(rq, p, &rf);
    3590                 :            : 
    3591                 :          3 :         return ns;
    3592                 :            : }
    3593                 :            : 
    3594                 :            : /*
    3595                 :            :  * This function gets called by the timer code, with HZ frequency.
    3596                 :            :  * We call it with interrupts disabled.
    3597                 :            :  */
    3598                 :          3 : void scheduler_tick(void)
    3599                 :            : {
    3600                 :          3 :         int cpu = smp_processor_id();
    3601                 :          3 :         struct rq *rq = cpu_rq(cpu);
    3602                 :          3 :         struct task_struct *curr = rq->curr;
    3603                 :            :         struct rq_flags rf;
    3604                 :            : 
    3605                 :            :         sched_clock_tick();
    3606                 :            : 
    3607                 :            :         rq_lock(rq, &rf);
    3608                 :            : 
    3609                 :          3 :         update_rq_clock(rq);
    3610                 :          3 :         curr->sched_class->task_tick(rq, curr, 0);
    3611                 :          3 :         calc_global_load_tick(rq);
    3612                 :            :         psi_task_tick(rq);
    3613                 :            : 
    3614                 :            :         rq_unlock(rq, &rf);
    3615                 :            : 
    3616                 :          3 :         perf_event_task_tick();
    3617                 :            : 
    3618                 :            : #ifdef CONFIG_SMP
    3619                 :          3 :         rq->idle_balance = idle_cpu(cpu);
    3620                 :          3 :         trigger_load_balance(rq);
    3621                 :            : #endif
    3622                 :          3 : }
    3623                 :            : 
    3624                 :            : #ifdef CONFIG_NO_HZ_FULL
    3625                 :            : 
    3626                 :            : struct tick_work {
    3627                 :            :         int                     cpu;
    3628                 :            :         atomic_t                state;
    3629                 :            :         struct delayed_work     work;
    3630                 :            : };
    3631                 :            : /* Values for ->state, see diagram below. */
    3632                 :            : #define TICK_SCHED_REMOTE_OFFLINE       0
    3633                 :            : #define TICK_SCHED_REMOTE_OFFLINING     1
    3634                 :            : #define TICK_SCHED_REMOTE_RUNNING       2
    3635                 :            : 
    3636                 :            : /*
    3637                 :            :  * State diagram for ->state:
    3638                 :            :  *
    3639                 :            :  *
    3640                 :            :  *          TICK_SCHED_REMOTE_OFFLINE
    3641                 :            :  *                    |   ^
    3642                 :            :  *                    |   |
    3643                 :            :  *                    |   | sched_tick_remote()
    3644                 :            :  *                    |   |
    3645                 :            :  *                    |   |
    3646                 :            :  *                    +--TICK_SCHED_REMOTE_OFFLINING
    3647                 :            :  *                    |   ^
    3648                 :            :  *                    |   |
    3649                 :            :  * sched_tick_start() |   | sched_tick_stop()
    3650                 :            :  *                    |   |
    3651                 :            :  *                    V   |
    3652                 :            :  *          TICK_SCHED_REMOTE_RUNNING
    3653                 :            :  *
    3654                 :            :  *
    3655                 :            :  * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote()
    3656                 :            :  * and sched_tick_start() are happy to leave the state in RUNNING.
    3657                 :            :  */
    3658                 :            : 
    3659                 :            : static struct tick_work __percpu *tick_work_cpu;
    3660                 :            : 
    3661                 :            : static void sched_tick_remote(struct work_struct *work)
    3662                 :            : {
    3663                 :            :         struct delayed_work *dwork = to_delayed_work(work);
    3664                 :            :         struct tick_work *twork = container_of(dwork, struct tick_work, work);
    3665                 :            :         int cpu = twork->cpu;
    3666                 :            :         struct rq *rq = cpu_rq(cpu);
    3667                 :            :         struct task_struct *curr;
    3668                 :            :         struct rq_flags rf;
    3669                 :            :         u64 delta;
    3670                 :            :         int os;
    3671                 :            : 
    3672                 :            :         /*
    3673                 :            :          * Handle the tick only if it appears the remote CPU is running in full
    3674                 :            :          * dynticks mode. The check is racy by nature, but missing a tick or
    3675                 :            :          * having one too much is no big deal because the scheduler tick updates
    3676                 :            :          * statistics and checks timeslices in a time-independent way, regardless
    3677                 :            :          * of when exactly it is running.
    3678                 :            :          */
    3679                 :            :         if (!tick_nohz_tick_stopped_cpu(cpu))
    3680                 :            :                 goto out_requeue;
    3681                 :            : 
    3682                 :            :         rq_lock_irq(rq, &rf);
    3683                 :            :         curr = rq->curr;
    3684                 :            :         if (cpu_is_offline(cpu))
    3685                 :            :                 goto out_unlock;
    3686                 :            : 
    3687                 :            :         update_rq_clock(rq);
    3688                 :            : 
    3689                 :            :         if (!is_idle_task(curr)) {
    3690                 :            :                 /*
    3691                 :            :                  * Make sure the next tick runs within a reasonable
    3692                 :            :                  * amount of time.
    3693                 :            :                  */
    3694                 :            :                 delta = rq_clock_task(rq) - curr->se.exec_start;
    3695                 :            :                 WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3);
    3696                 :            :         }
    3697                 :            :         curr->sched_class->task_tick(rq, curr, 0);
    3698                 :            : 
    3699                 :            :         calc_load_nohz_remote(rq);
    3700                 :            : out_unlock:
    3701                 :            :         rq_unlock_irq(rq, &rf);
    3702                 :            : out_requeue:
    3703                 :            : 
    3704                 :            :         /*
    3705                 :            :          * Run the remote tick once per second (1Hz). This arbitrary
    3706                 :            :          * frequency is large enough to avoid overload but short enough
    3707                 :            :          * to keep scheduler internal stats reasonably up to date.  But
    3708                 :            :          * first update state to reflect hotplug activity if required.
    3709                 :            :          */
    3710                 :            :         os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING);
    3711                 :            :         WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE);
    3712                 :            :         if (os == TICK_SCHED_REMOTE_RUNNING)
    3713                 :            :                 queue_delayed_work(system_unbound_wq, dwork, HZ);
    3714                 :            : }
    3715                 :            : 
    3716                 :            : static void sched_tick_start(int cpu)
    3717                 :            : {
    3718                 :            :         int os;
    3719                 :            :         struct tick_work *twork;
    3720                 :            : 
    3721                 :            :         if (housekeeping_cpu(cpu, HK_FLAG_TICK))
    3722                 :            :                 return;
    3723                 :            : 
    3724                 :            :         WARN_ON_ONCE(!tick_work_cpu);
    3725                 :            : 
    3726                 :            :         twork = per_cpu_ptr(tick_work_cpu, cpu);
    3727                 :            :         os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING);
    3728                 :            :         WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING);
    3729                 :            :         if (os == TICK_SCHED_REMOTE_OFFLINE) {
    3730                 :            :                 twork->cpu = cpu;
    3731                 :            :                 INIT_DELAYED_WORK(&twork->work, sched_tick_remote);
    3732                 :            :                 queue_delayed_work(system_unbound_wq, &twork->work, HZ);
    3733                 :            :         }
    3734                 :            : }
    3735                 :            : 
    3736                 :            : #ifdef CONFIG_HOTPLUG_CPU
    3737                 :            : static void sched_tick_stop(int cpu)
    3738                 :            : {
    3739                 :            :         struct tick_work *twork;
    3740                 :            :         int os;
    3741                 :            : 
    3742                 :            :         if (housekeeping_cpu(cpu, HK_FLAG_TICK))
    3743                 :            :                 return;
    3744                 :            : 
    3745                 :            :         WARN_ON_ONCE(!tick_work_cpu);
    3746                 :            : 
    3747                 :            :         twork = per_cpu_ptr(tick_work_cpu, cpu);
    3748                 :            :         /* There cannot be competing actions, but don't rely on stop-machine. */
    3749                 :            :         os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING);
    3750                 :            :         WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING);
    3751                 :            :         /* Don't cancel, as this would mess up the state machine. */
    3752                 :            : }
    3753                 :            : #endif /* CONFIG_HOTPLUG_CPU */
    3754                 :            : 
    3755                 :            : int __init sched_tick_offload_init(void)
    3756                 :            : {
    3757                 :            :         tick_work_cpu = alloc_percpu(struct tick_work);
    3758                 :            :         BUG_ON(!tick_work_cpu);
    3759                 :            :         return 0;
    3760                 :            : }
    3761                 :            : 
    3762                 :            : #else /* !CONFIG_NO_HZ_FULL */
    3763                 :            : static inline void sched_tick_start(int cpu) { }
    3764                 :            : static inline void sched_tick_stop(int cpu) { }
    3765                 :            : #endif
    3766                 :            : 
    3767                 :            : #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \
    3768                 :            :                                 defined(CONFIG_TRACE_PREEMPT_TOGGLE))
    3769                 :            : /*
    3770                 :            :  * If the value passed in is equal to the current preempt count
    3771                 :            :  * then we just disabled preemption. Start timing the latency.
    3772                 :            :  */
    3773                 :            : static inline void preempt_latency_start(int val)
    3774                 :            : {
    3775                 :            :         if (preempt_count() == val) {
    3776                 :            :                 unsigned long ip = get_lock_parent_ip();
    3777                 :            : #ifdef CONFIG_DEBUG_PREEMPT
    3778                 :            :                 current->preempt_disable_ip = ip;
    3779                 :            : #endif
    3780                 :            :                 trace_preempt_off(CALLER_ADDR0, ip);
    3781                 :            :         }
    3782                 :            : }
    3783                 :            : 
    3784                 :            : void preempt_count_add(int val)
    3785                 :            : {
    3786                 :            : #ifdef CONFIG_DEBUG_PREEMPT
    3787                 :            :         /*
    3788                 :            :          * Underflow?
    3789                 :            :          */
    3790                 :            :         if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
    3791                 :            :                 return;
    3792                 :            : #endif
    3793                 :            :         __preempt_count_add(val);
    3794                 :            : #ifdef CONFIG_DEBUG_PREEMPT
    3795                 :            :         /*
    3796                 :            :          * Spinlock count overflowing soon?
    3797                 :            :          */
    3798                 :            :         DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
    3799                 :            :                                 PREEMPT_MASK - 10);
    3800                 :            : #endif
    3801                 :            :         preempt_latency_start(val);
    3802                 :            : }
    3803                 :            : EXPORT_SYMBOL(preempt_count_add);
    3804                 :            : NOKPROBE_SYMBOL(preempt_count_add);
    3805                 :            : 
    3806                 :            : /*
    3807                 :            :  * If the value passed in equals to the current preempt count
    3808                 :            :  * then we just enabled preemption. Stop timing the latency.
    3809                 :            :  */
    3810                 :            : static inline void preempt_latency_stop(int val)
    3811                 :            : {
    3812                 :            :         if (preempt_count() == val)
    3813                 :            :                 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
    3814                 :            : }
    3815                 :            : 
    3816                 :            : void preempt_count_sub(int val)
    3817                 :            : {
    3818                 :            : #ifdef CONFIG_DEBUG_PREEMPT
    3819                 :            :         /*
    3820                 :            :          * Underflow?
    3821                 :            :          */
    3822                 :            :         if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
    3823                 :            :                 return;
    3824                 :            :         /*
    3825                 :            :          * Is the spinlock portion underflowing?
    3826                 :            :          */
    3827                 :            :         if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
    3828                 :            :                         !(preempt_count() & PREEMPT_MASK)))
    3829                 :            :                 return;
    3830                 :            : #endif
    3831                 :            : 
    3832                 :            :         preempt_latency_stop(val);
    3833                 :            :         __preempt_count_sub(val);
    3834                 :            : }
    3835                 :            : EXPORT_SYMBOL(preempt_count_sub);
    3836                 :            : NOKPROBE_SYMBOL(preempt_count_sub);
    3837                 :            : 
    3838                 :            : #else
    3839                 :            : static inline void preempt_latency_start(int val) { }
    3840                 :            : static inline void preempt_latency_stop(int val) { }
    3841                 :            : #endif
    3842                 :            : 
    3843                 :            : static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
    3844                 :            : {
    3845                 :            : #ifdef CONFIG_DEBUG_PREEMPT
    3846                 :            :         return p->preempt_disable_ip;
    3847                 :            : #else
    3848                 :            :         return 0;
    3849                 :            : #endif
    3850                 :            : }
    3851                 :            : 
    3852                 :            : /*
    3853                 :            :  * Print scheduling while atomic bug:
    3854                 :            :  */
    3855                 :          0 : static noinline void __schedule_bug(struct task_struct *prev)
    3856                 :            : {
    3857                 :            :         /* Save this before calling printk(), since that will clobber it */
    3858                 :            :         unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
    3859                 :            : 
    3860                 :          0 :         if (oops_in_progress)
    3861                 :          0 :                 return;
    3862                 :            : 
    3863                 :          0 :         printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
    3864                 :          0 :                 prev->comm, prev->pid, preempt_count());
    3865                 :            : 
    3866                 :            :         debug_show_held_locks(prev);
    3867                 :          0 :         print_modules();
    3868                 :            :         if (irqs_disabled())
    3869                 :            :                 print_irqtrace_events(prev);
    3870                 :            :         if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
    3871                 :            :             && in_atomic_preempt_off()) {
    3872                 :            :                 pr_err("Preemption disabled at:");
    3873                 :            :                 print_ip_sym(preempt_disable_ip);
    3874                 :            :                 pr_cont("\n");
    3875                 :            :         }
    3876                 :          0 :         if (panic_on_warn)
    3877                 :          0 :                 panic("scheduling while atomic\n");
    3878                 :            : 
    3879                 :          0 :         dump_stack();
    3880                 :          0 :         add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
    3881                 :            : }
    3882                 :            : 
    3883                 :            : /*
    3884                 :            :  * Various schedule()-time debugging checks and statistics:
    3885                 :            :  */
    3886                 :          3 : static inline void schedule_debug(struct task_struct *prev, bool preempt)
    3887                 :            : {
    3888                 :            : #ifdef CONFIG_SCHED_STACK_END_CHECK
    3889                 :            :         if (task_stack_end_corrupted(prev))
    3890                 :            :                 panic("corrupted stack end detected inside scheduler\n");
    3891                 :            : #endif
    3892                 :            : 
    3893                 :            : #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
    3894                 :            :         if (!preempt && prev->state && prev->non_block_count) {
    3895                 :            :                 printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n",
    3896                 :            :                         prev->comm, prev->pid, prev->non_block_count);
    3897                 :            :                 dump_stack();
    3898                 :            :                 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
    3899                 :            :         }
    3900                 :            : #endif
    3901                 :            : 
    3902                 :          3 :         if (unlikely(in_atomic_preempt_off())) {
    3903                 :          0 :                 __schedule_bug(prev);
    3904                 :            :                 preempt_count_set(PREEMPT_DISABLED);
    3905                 :            :         }
    3906                 :            :         rcu_sleep_check();
    3907                 :            : 
    3908                 :          3 :         profile_hit(SCHED_PROFILING, __builtin_return_address(0));
    3909                 :            : 
    3910                 :          3 :         schedstat_inc(this_rq()->sched_count);
    3911                 :          3 : }
    3912                 :            : 
    3913                 :            : /*
    3914                 :            :  * Pick up the highest-prio task:
    3915                 :            :  */
    3916                 :            : static inline struct task_struct *
    3917                 :          3 : pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
    3918                 :            : {
    3919                 :            :         const struct sched_class *class;
    3920                 :            :         struct task_struct *p;
    3921                 :            : 
    3922                 :            :         /*
    3923                 :            :          * Optimization: we know that if all tasks are in the fair class we can
    3924                 :            :          * call that function directly, but only if the @prev task wasn't of a
    3925                 :            :          * higher scheduling class, because otherwise those loose the
    3926                 :            :          * opportunity to pull in more work from other CPUs.
    3927                 :            :          */
    3928                 :          3 :         if (likely((prev->sched_class == &idle_sched_class ||
    3929                 :            :                     prev->sched_class == &fair_sched_class) &&
    3930                 :            :                    rq->nr_running == rq->cfs.h_nr_running)) {
    3931                 :            : 
    3932                 :          3 :                 p = fair_sched_class.pick_next_task(rq, prev, rf);
    3933                 :          3 :                 if (unlikely(p == RETRY_TASK))
    3934                 :            :                         goto restart;
    3935                 :            : 
    3936                 :            :                 /* Assumes fair_sched_class->next == idle_sched_class */
    3937                 :          3 :                 if (unlikely(!p))
    3938                 :          3 :                         p = idle_sched_class.pick_next_task(rq, prev, rf);
    3939                 :            : 
    3940                 :          3 :                 return p;
    3941                 :            :         }
    3942                 :            : 
    3943                 :            : restart:
    3944                 :            : #ifdef CONFIG_SMP
    3945                 :            :         /*
    3946                 :            :          * We must do the balancing pass before put_next_task(), such
    3947                 :            :          * that when we release the rq->lock the task is in the same
    3948                 :            :          * state as before we took rq->lock.
    3949                 :            :          *
    3950                 :            :          * We can terminate the balance pass as soon as we know there is
    3951                 :            :          * a runnable task of @class priority or higher.
    3952                 :            :          */
    3953                 :          3 :         for_class_range(class, prev->sched_class, &idle_sched_class) {
    3954                 :          3 :                 if (class->balance(rq, prev, rf))
    3955                 :            :                         break;
    3956                 :            :         }
    3957                 :            : #endif
    3958                 :            : 
    3959                 :          3 :         put_prev_task(rq, prev);
    3960                 :            : 
    3961                 :          3 :         for_each_class(class) {
    3962                 :          3 :                 p = class->pick_next_task(rq, NULL, NULL);
    3963                 :          3 :                 if (p)
    3964                 :          3 :                         return p;
    3965                 :            :         }
    3966                 :            : 
    3967                 :            :         /* The idle class should always have a runnable task: */
    3968                 :          0 :         BUG();
    3969                 :            : }
    3970                 :            : 
    3971                 :            : /*
    3972                 :            :  * __schedule() is the main scheduler function.
    3973                 :            :  *
    3974                 :            :  * The main means of driving the scheduler and thus entering this function are:
    3975                 :            :  *
    3976                 :            :  *   1. Explicit blocking: mutex, semaphore, waitqueue, etc.
    3977                 :            :  *
    3978                 :            :  *   2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
    3979                 :            :  *      paths. For example, see arch/x86/entry_64.S.
    3980                 :            :  *
    3981                 :            :  *      To drive preemption between tasks, the scheduler sets the flag in timer
    3982                 :            :  *      interrupt handler scheduler_tick().
    3983                 :            :  *
    3984                 :            :  *   3. Wakeups don't really cause entry into schedule(). They add a
    3985                 :            :  *      task to the run-queue and that's it.
    3986                 :            :  *
    3987                 :            :  *      Now, if the new task added to the run-queue preempts the current
    3988                 :            :  *      task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
    3989                 :            :  *      called on the nearest possible occasion:
    3990                 :            :  *
    3991                 :            :  *       - If the kernel is preemptible (CONFIG_PREEMPTION=y):
    3992                 :            :  *
    3993                 :            :  *         - in syscall or exception context, at the next outmost
    3994                 :            :  *           preempt_enable(). (this might be as soon as the wake_up()'s
    3995                 :            :  *           spin_unlock()!)
    3996                 :            :  *
    3997                 :            :  *         - in IRQ context, return from interrupt-handler to
    3998                 :            :  *           preemptible context
    3999                 :            :  *
    4000                 :            :  *       - If the kernel is not preemptible (CONFIG_PREEMPTION is not set)
    4001                 :            :  *         then at the next:
    4002                 :            :  *
    4003                 :            :  *          - cond_resched() call
    4004                 :            :  *          - explicit schedule() call
    4005                 :            :  *          - return from syscall or exception to user-space
    4006                 :            :  *          - return from interrupt-handler to user-space
    4007                 :            :  *
    4008                 :            :  * WARNING: must be called with preemption disabled!
    4009                 :            :  */
    4010                 :          3 : static void __sched notrace __schedule(bool preempt)
    4011                 :            : {
    4012                 :            :         struct task_struct *prev, *next;
    4013                 :            :         unsigned long *switch_count;
    4014                 :            :         struct rq_flags rf;
    4015                 :            :         struct rq *rq;
    4016                 :            :         int cpu;
    4017                 :            : 
    4018                 :          3 :         cpu = smp_processor_id();
    4019                 :          3 :         rq = cpu_rq(cpu);
    4020                 :          3 :         prev = rq->curr;
    4021                 :            : 
    4022                 :          3 :         schedule_debug(prev, preempt);
    4023                 :            : 
    4024                 :          3 :         if (sched_feat(HRTICK))
    4025                 :          0 :                 hrtick_clear(rq);
    4026                 :            : 
    4027                 :          3 :         local_irq_disable();
    4028                 :          3 :         rcu_note_context_switch(preempt);
    4029                 :            : 
    4030                 :            :         /*
    4031                 :            :          * Make sure that signal_pending_state()->signal_pending() below
    4032                 :            :          * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
    4033                 :            :          * done by the caller to avoid the race with signal_wake_up().
    4034                 :            :          *
    4035                 :            :          * The membarrier system call requires a full memory barrier
    4036                 :            :          * after coming from user-space, before storing to rq->curr.
    4037                 :            :          */
    4038                 :            :         rq_lock(rq, &rf);
    4039                 :            :         smp_mb__after_spinlock();
    4040                 :            : 
    4041                 :            :         /* Promote REQ to ACT */
    4042                 :          3 :         rq->clock_update_flags <<= 1;
    4043                 :          3 :         update_rq_clock(rq);
    4044                 :            : 
    4045                 :          3 :         switch_count = &prev->nivcsw;
    4046                 :          3 :         if (!preempt && prev->state) {
    4047                 :          3 :                 if (signal_pending_state(prev->state, prev)) {
    4048                 :          3 :                         prev->state = TASK_RUNNING;
    4049                 :            :                 } else {
    4050                 :          3 :                         deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);
    4051                 :            : 
    4052                 :          3 :                         if (prev->in_iowait) {
    4053                 :          3 :                                 atomic_inc(&rq->nr_iowait);
    4054                 :          3 :                                 delayacct_blkio_start();
    4055                 :            :                         }
    4056                 :            :                 }
    4057                 :          3 :                 switch_count = &prev->nvcsw;
    4058                 :            :         }
    4059                 :            : 
    4060                 :          3 :         next = pick_next_task(rq, prev, &rf);
    4061                 :            :         clear_tsk_need_resched(prev);
    4062                 :            :         clear_preempt_need_resched();
    4063                 :            : 
    4064                 :          3 :         if (likely(prev != next)) {
    4065                 :          3 :                 rq->nr_switches++;
    4066                 :            :                 /*
    4067                 :            :                  * RCU users of rcu_dereference(rq->curr) may not see
    4068                 :            :                  * changes to task_struct made by pick_next_task().
    4069                 :            :                  */
    4070                 :          3 :                 RCU_INIT_POINTER(rq->curr, next);
    4071                 :            :                 /*
    4072                 :            :                  * The membarrier system call requires each architecture
    4073                 :            :                  * to have a full memory barrier after updating
    4074                 :            :                  * rq->curr, before returning to user-space.
    4075                 :            :                  *
    4076                 :            :                  * Here are the schemes providing that barrier on the
    4077                 :            :                  * various architectures:
    4078                 :            :                  * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC.
    4079                 :            :                  *   switch_mm() rely on membarrier_arch_switch_mm() on PowerPC.
    4080                 :            :                  * - finish_lock_switch() for weakly-ordered
    4081                 :            :                  *   architectures where spin_unlock is a full barrier,
    4082                 :            :                  * - switch_to() for arm64 (weakly-ordered, spin_unlock
    4083                 :            :                  *   is a RELEASE barrier),
    4084                 :            :                  */
    4085                 :          3 :                 ++*switch_count;
    4086                 :            : 
    4087                 :          3 :                 trace_sched_switch(preempt, prev, next);
    4088                 :            : 
    4089                 :            :                 /* Also unlocks the rq: */
    4090                 :            :                 rq = context_switch(rq, prev, next, &rf);
    4091                 :            :         } else {
    4092                 :          3 :                 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
    4093                 :            :                 rq_unlock_irq(rq, &rf);
    4094                 :            :         }
    4095                 :            : 
    4096                 :            :         balance_callback(rq);
    4097                 :          3 : }
    4098                 :            : 
    4099                 :          3 : void __noreturn do_task_dead(void)
    4100                 :            : {
    4101                 :            :         /* Causes final put_task_struct in finish_task_switch(): */
    4102                 :          3 :         set_special_state(TASK_DEAD);
    4103                 :            : 
    4104                 :            :         /* Tell freezer to ignore us: */
    4105                 :          3 :         current->flags |= PF_NOFREEZE;
    4106                 :            : 
    4107                 :          3 :         __schedule(false);
    4108                 :          0 :         BUG();
    4109                 :            : 
    4110                 :            :         /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */
    4111                 :            :         for (;;)
    4112                 :            :                 cpu_relax();
    4113                 :            : }
    4114                 :            : 
    4115                 :          3 : static inline void sched_submit_work(struct task_struct *tsk)
    4116                 :            : {
    4117                 :          3 :         if (!tsk->state)
    4118                 :            :                 return;
    4119                 :            : 
    4120                 :            :         /*
    4121                 :            :          * If a worker went to sleep, notify and ask workqueue whether
    4122                 :            :          * it wants to wake up a task to maintain concurrency.
    4123                 :            :          * As this function is called inside the schedule() context,
    4124                 :            :          * we disable preemption to avoid it calling schedule() again
    4125                 :            :          * in the possible wakeup of a kworker.
    4126                 :            :          */
    4127                 :          3 :         if (tsk->flags & PF_WQ_WORKER) {
    4128                 :          3 :                 preempt_disable();
    4129                 :          3 :                 wq_worker_sleeping(tsk);
    4130                 :          3 :                 preempt_enable_no_resched();
    4131                 :            :         }
    4132                 :            : 
    4133                 :          3 :         if (tsk_is_pi_blocked(tsk))
    4134                 :            :                 return;
    4135                 :            : 
    4136                 :            :         /*
    4137                 :            :          * If we are going to sleep and we have plugged IO queued,
    4138                 :            :          * make sure to submit it to avoid deadlocks.
    4139                 :            :          */
    4140                 :          3 :         if (blk_needs_flush_plug(tsk))
    4141                 :            :                 blk_schedule_flush_plug(tsk);
    4142                 :            : }
    4143                 :            : 
    4144                 :            : static void sched_update_worker(struct task_struct *tsk)
    4145                 :            : {
    4146                 :          3 :         if (tsk->flags & PF_WQ_WORKER)
    4147                 :          3 :                 wq_worker_running(tsk);
    4148                 :            : }
    4149                 :            : 
    4150                 :          3 : asmlinkage __visible void __sched schedule(void)
    4151                 :            : {
    4152                 :          3 :         struct task_struct *tsk = current;
    4153                 :            : 
    4154                 :          3 :         sched_submit_work(tsk);
    4155                 :            :         do {
    4156                 :          3 :                 preempt_disable();
    4157                 :          3 :                 __schedule(false);
    4158                 :          3 :                 sched_preempt_enable_no_resched();
    4159                 :          3 :         } while (need_resched());
    4160                 :            :         sched_update_worker(tsk);
    4161                 :          3 : }
    4162                 :            : EXPORT_SYMBOL(schedule);
    4163                 :            : 
    4164                 :            : /*
    4165                 :            :  * synchronize_rcu_tasks() makes sure that no task is stuck in preempted
    4166                 :            :  * state (have scheduled out non-voluntarily) by making sure that all
    4167                 :            :  * tasks have either left the run queue or have gone into user space.
    4168                 :            :  * As idle tasks do not do either, they must not ever be preempted
    4169                 :            :  * (schedule out non-voluntarily).
    4170                 :            :  *
    4171                 :            :  * schedule_idle() is similar to schedule_preempt_disable() except that it
    4172                 :            :  * never enables preemption because it does not call sched_submit_work().
    4173                 :            :  */
    4174                 :          3 : void __sched schedule_idle(void)
    4175                 :            : {
    4176                 :            :         /*
    4177                 :            :          * As this skips calling sched_submit_work(), which the idle task does
    4178                 :            :          * regardless because that function is a nop when the task is in a
    4179                 :            :          * TASK_RUNNING state, make sure this isn't used someplace that the
    4180                 :            :          * current task can be in any other state. Note, idle is always in the
    4181                 :            :          * TASK_RUNNING state.
    4182                 :            :          */
    4183                 :          3 :         WARN_ON_ONCE(current->state);
    4184                 :            :         do {
    4185                 :          3 :                 __schedule(false);
    4186                 :          3 :         } while (need_resched());
    4187                 :          3 : }
    4188                 :            : 
    4189                 :            : #ifdef CONFIG_CONTEXT_TRACKING
    4190                 :            : asmlinkage __visible void __sched schedule_user(void)
    4191                 :            : {
    4192                 :            :         /*
    4193                 :            :          * If we come here after a random call to set_need_resched(),
    4194                 :            :          * or we have been woken up remotely but the IPI has not yet arrived,
    4195                 :            :          * we haven't yet exited the RCU idle mode. Do it here manually until
    4196                 :            :          * we find a better solution.
    4197                 :            :          *
    4198                 :            :          * NB: There are buggy callers of this function.  Ideally we
    4199                 :            :          * should warn if prev_state != CONTEXT_USER, but that will trigger
    4200                 :            :          * too frequently to make sense yet.
    4201                 :            :          */
    4202                 :            :         enum ctx_state prev_state = exception_enter();
    4203                 :            :         schedule();
    4204                 :            :         exception_exit(prev_state);
    4205                 :            : }
    4206                 :            : #endif
    4207                 :            : 
    4208                 :            : /**
    4209                 :            :  * schedule_preempt_disabled - called with preemption disabled
    4210                 :            :  *
    4211                 :            :  * Returns with preemption disabled. Note: preempt_count must be 1
    4212                 :            :  */
    4213                 :          3 : void __sched schedule_preempt_disabled(void)
    4214                 :            : {
    4215                 :          3 :         sched_preempt_enable_no_resched();
    4216                 :          3 :         schedule();
    4217                 :          3 :         preempt_disable();
    4218                 :          3 : }
    4219                 :            : 
    4220                 :          3 : static void __sched notrace preempt_schedule_common(void)
    4221                 :            : {
    4222                 :            :         do {
    4223                 :            :                 /*
    4224                 :            :                  * Because the function tracer can trace preempt_count_sub()
    4225                 :            :                  * and it also uses preempt_enable/disable_notrace(), if
    4226                 :            :                  * NEED_RESCHED is set, the preempt_enable_notrace() called
    4227                 :            :                  * by the function tracer will call this function again and
    4228                 :            :                  * cause infinite recursion.
    4229                 :            :                  *
    4230                 :            :                  * Preemption must be disabled here before the function
    4231                 :            :                  * tracer can trace. Break up preempt_disable() into two
    4232                 :            :                  * calls. One to disable preemption without fear of being
    4233                 :            :                  * traced. The other to still record the preemption latency,
    4234                 :            :                  * which can also be traced by the function tracer.
    4235                 :            :                  */
    4236                 :          3 :                 preempt_disable_notrace();
    4237                 :            :                 preempt_latency_start(1);
    4238                 :          3 :                 __schedule(true);
    4239                 :            :                 preempt_latency_stop(1);
    4240                 :          3 :                 preempt_enable_no_resched_notrace();
    4241                 :            : 
    4242                 :            :                 /*
    4243                 :            :                  * Check again in case we missed a preemption opportunity
    4244                 :            :                  * between schedule and now.
    4245                 :            :                  */
    4246                 :          3 :         } while (need_resched());
    4247                 :          3 : }
    4248                 :            : 
    4249                 :            : #ifdef CONFIG_PREEMPTION
    4250                 :            : /*
    4251                 :            :  * This is the entry point to schedule() from in-kernel preemption
    4252                 :            :  * off of preempt_enable.
    4253                 :            :  */
    4254                 :            : asmlinkage __visible void __sched notrace preempt_schedule(void)
    4255                 :            : {
    4256                 :            :         /*
    4257                 :            :          * If there is a non-zero preempt_count or interrupts are disabled,
    4258                 :            :          * we do not want to preempt the current task. Just return..
    4259                 :            :          */
    4260                 :            :         if (likely(!preemptible()))
    4261                 :            :                 return;
    4262                 :            : 
    4263                 :            :         preempt_schedule_common();
    4264                 :            : }
    4265                 :            : NOKPROBE_SYMBOL(preempt_schedule);
    4266                 :            : EXPORT_SYMBOL(preempt_schedule);
    4267                 :            : 
    4268                 :            : /**
    4269                 :            :  * preempt_schedule_notrace - preempt_schedule called by tracing
    4270                 :            :  *
    4271                 :            :  * The tracing infrastructure uses preempt_enable_notrace to prevent
    4272                 :            :  * recursion and tracing preempt enabling caused by the tracing
    4273                 :            :  * infrastructure itself. But as tracing can happen in areas coming
    4274                 :            :  * from userspace or just about to enter userspace, a preempt enable
    4275                 :            :  * can occur before user_exit() is called. This will cause the scheduler
    4276                 :            :  * to be called when the system is still in usermode.
    4277                 :            :  *
    4278                 :            :  * To prevent this, the preempt_enable_notrace will use this function
    4279                 :            :  * instead of preempt_schedule() to exit user context if needed before
    4280                 :            :  * calling the scheduler.
    4281                 :            :  */
    4282                 :            : asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
    4283                 :            : {
    4284                 :            :         enum ctx_state prev_ctx;
    4285                 :            : 
    4286                 :            :         if (likely(!preemptible()))
    4287                 :            :                 return;
    4288                 :            : 
    4289                 :            :         do {
    4290                 :            :                 /*
    4291                 :            :                  * Because the function tracer can trace preempt_count_sub()
    4292                 :            :                  * and it also uses preempt_enable/disable_notrace(), if
    4293                 :            :                  * NEED_RESCHED is set, the preempt_enable_notrace() called
    4294                 :            :                  * by the function tracer will call this function again and
    4295                 :            :                  * cause infinite recursion.
    4296                 :            :                  *
    4297                 :            :                  * Preemption must be disabled here before the function
    4298                 :            :                  * tracer can trace. Break up preempt_disable() into two
    4299                 :            :                  * calls. One to disable preemption without fear of being
    4300                 :            :                  * traced. The other to still record the preemption latency,
    4301                 :            :                  * which can also be traced by the function tracer.
    4302                 :            :                  */
    4303                 :            :                 preempt_disable_notrace();
    4304                 :            :                 preempt_latency_start(1);
    4305                 :            :                 /*
    4306                 :            :                  * Needs preempt disabled in case user_exit() is traced
    4307                 :            :                  * and the tracer calls preempt_enable_notrace() causing
    4308                 :            :                  * an infinite recursion.
    4309                 :            :                  */
    4310                 :            :                 prev_ctx = exception_enter();
    4311                 :            :                 __schedule(true);
    4312                 :            :                 exception_exit(prev_ctx);
    4313                 :            : 
    4314                 :            :                 preempt_latency_stop(1);
    4315                 :            :                 preempt_enable_no_resched_notrace();
    4316                 :            :         } while (need_resched());
    4317                 :            : }
    4318                 :            : EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
    4319                 :            : 
    4320                 :            : #endif /* CONFIG_PREEMPTION */
    4321                 :            : 
    4322                 :            : /*
    4323                 :            :  * This is the entry point to schedule() from kernel preemption
    4324                 :            :  * off of irq context.
    4325                 :            :  * Note, that this is called and return with irqs disabled. This will
    4326                 :            :  * protect us against recursive calling from irq.
    4327                 :            :  */
    4328                 :          0 : asmlinkage __visible void __sched preempt_schedule_irq(void)
    4329                 :            : {
    4330                 :            :         enum ctx_state prev_state;
    4331                 :            : 
    4332                 :            :         /* Catch callers which need to be fixed */
    4333                 :          0 :         BUG_ON(preempt_count() || !irqs_disabled());
    4334                 :            : 
    4335                 :            :         prev_state = exception_enter();
    4336                 :            : 
    4337                 :            :         do {
    4338                 :          0 :                 preempt_disable();
    4339                 :          0 :                 local_irq_enable();
    4340                 :          0 :                 __schedule(true);
    4341                 :          0 :                 local_irq_disable();
    4342                 :          0 :                 sched_preempt_enable_no_resched();
    4343                 :          0 :         } while (need_resched());
    4344                 :            : 
    4345                 :            :         exception_exit(prev_state);
    4346                 :          0 : }
    4347                 :            : 
    4348                 :          3 : int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
    4349                 :            :                           void *key)
    4350                 :            : {
    4351                 :          3 :         return try_to_wake_up(curr->private, mode, wake_flags);
    4352                 :            : }
    4353                 :            : EXPORT_SYMBOL(default_wake_function);
    4354                 :            : 
    4355                 :            : #ifdef CONFIG_RT_MUTEXES
    4356                 :            : 
    4357                 :            : static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
    4358                 :            : {
    4359                 :          3 :         if (pi_task)
    4360                 :          0 :                 prio = min(prio, pi_task->prio);
    4361                 :            : 
    4362                 :            :         return prio;
    4363                 :            : }
    4364                 :            : 
    4365                 :            : static inline int rt_effective_prio(struct task_struct *p, int prio)
    4366                 :            : {
    4367                 :            :         struct task_struct *pi_task = rt_mutex_get_top_task(p);
    4368                 :            : 
    4369                 :            :         return __rt_effective_prio(pi_task, prio);
    4370                 :            : }
    4371                 :            : 
    4372                 :            : /*
    4373                 :            :  * rt_mutex_setprio - set the current priority of a task
    4374                 :            :  * @p: task to boost
    4375                 :            :  * @pi_task: donor task
    4376                 :            :  *
    4377                 :            :  * This function changes the 'effective' priority of a task. It does
    4378                 :            :  * not touch ->normal_prio like __setscheduler().
    4379                 :            :  *
    4380                 :            :  * Used by the rt_mutex code to implement priority inheritance
    4381                 :            :  * logic. Call site only calls if the priority of the task changed.
    4382                 :            :  */
    4383                 :          0 : void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
    4384                 :            : {
    4385                 :            :         int prio, oldprio, queued, running, queue_flag =
    4386                 :            :                 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
    4387                 :            :         const struct sched_class *prev_class;
    4388                 :            :         struct rq_flags rf;
    4389                 :            :         struct rq *rq;
    4390                 :            : 
    4391                 :            :         /* XXX used to be waiter->prio, not waiter->task->prio */
    4392                 :          0 :         prio = __rt_effective_prio(pi_task, p->normal_prio);
    4393                 :            : 
    4394                 :            :         /*
    4395                 :            :          * If nothing changed; bail early.
    4396                 :            :          */
    4397                 :          0 :         if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
    4398                 :          0 :                 return;
    4399                 :            : 
    4400                 :          0 :         rq = __task_rq_lock(p, &rf);
    4401                 :          0 :         update_rq_clock(rq);
    4402                 :            :         /*
    4403                 :            :          * Set under pi_lock && rq->lock, such that the value can be used under
    4404                 :            :          * either lock.
    4405                 :            :          *
    4406                 :            :          * Note that there is loads of tricky to make this pointer cache work
    4407                 :            :          * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
    4408                 :            :          * ensure a task is de-boosted (pi_task is set to NULL) before the
    4409                 :            :          * task is allowed to run again (and can exit). This ensures the pointer
    4410                 :            :          * points to a blocked task -- which guaratees the task is present.
    4411                 :            :          */
    4412                 :          0 :         p->pi_top_task = pi_task;
    4413                 :            : 
    4414                 :            :         /*
    4415                 :            :          * For FIFO/RR we only need to set prio, if that matches we're done.
    4416                 :            :          */
    4417                 :          0 :         if (prio == p->prio && !dl_prio(prio))
    4418                 :            :                 goto out_unlock;
    4419                 :            : 
    4420                 :            :         /*
    4421                 :            :          * Idle task boosting is a nono in general. There is one
    4422                 :            :          * exception, when PREEMPT_RT and NOHZ is active:
    4423                 :            :          *
    4424                 :            :          * The idle task calls get_next_timer_interrupt() and holds
    4425                 :            :          * the timer wheel base->lock on the CPU and another CPU wants
    4426                 :            :          * to access the timer (probably to cancel it). We can safely
    4427                 :            :          * ignore the boosting request, as the idle CPU runs this code
    4428                 :            :          * with interrupts disabled and will complete the lock
    4429                 :            :          * protected section without being interrupted. So there is no
    4430                 :            :          * real need to boost.
    4431                 :            :          */
    4432                 :          0 :         if (unlikely(p == rq->idle)) {
    4433                 :          0 :                 WARN_ON(p != rq->curr);
    4434                 :          0 :                 WARN_ON(p->pi_blocked_on);
    4435                 :            :                 goto out_unlock;
    4436                 :            :         }
    4437                 :            : 
    4438                 :          0 :         trace_sched_pi_setprio(p, pi_task);
    4439                 :          0 :         oldprio = p->prio;
    4440                 :            : 
    4441                 :          0 :         if (oldprio == prio)
    4442                 :            :                 queue_flag &= ~DEQUEUE_MOVE;
    4443                 :            : 
    4444                 :          0 :         prev_class = p->sched_class;
    4445                 :            :         queued = task_on_rq_queued(p);
    4446                 :            :         running = task_current(rq, p);
    4447                 :          0 :         if (queued)
    4448                 :          0 :                 dequeue_task(rq, p, queue_flag);
    4449                 :          0 :         if (running)
    4450                 :          0 :                 put_prev_task(rq, p);
    4451                 :            : 
    4452                 :            :         /*
    4453                 :            :          * Boosting condition are:
    4454                 :            :          * 1. -rt task is running and holds mutex A
    4455                 :            :          *      --> -dl task blocks on mutex A
    4456                 :            :          *
    4457                 :            :          * 2. -dl task is running and holds mutex A
    4458                 :            :          *      --> -dl task blocks on mutex A and could preempt the
    4459                 :            :          *          running task
    4460                 :            :          */
    4461                 :          0 :         if (dl_prio(prio)) {
    4462                 :          0 :                 if (!dl_prio(p->normal_prio) ||
    4463                 :          0 :                     (pi_task && dl_prio(pi_task->prio) &&
    4464                 :            :                      dl_entity_preempt(&pi_task->dl, &p->dl))) {
    4465                 :          0 :                         p->dl.dl_boosted = 1;
    4466                 :          0 :                         queue_flag |= ENQUEUE_REPLENISH;
    4467                 :            :                 } else
    4468                 :          0 :                         p->dl.dl_boosted = 0;
    4469                 :          0 :                 p->sched_class = &dl_sched_class;
    4470                 :          0 :         } else if (rt_prio(prio)) {
    4471                 :          0 :                 if (dl_prio(oldprio))
    4472                 :          0 :                         p->dl.dl_boosted = 0;
    4473                 :          0 :                 if (oldprio < prio)
    4474                 :          0 :                         queue_flag |= ENQUEUE_HEAD;
    4475                 :          0 :                 p->sched_class = &rt_sched_class;
    4476                 :            :         } else {
    4477                 :          0 :                 if (dl_prio(oldprio))
    4478                 :          0 :                         p->dl.dl_boosted = 0;
    4479                 :          0 :                 if (rt_prio(oldprio))
    4480                 :          0 :                         p->rt.timeout = 0;
    4481                 :          0 :                 p->sched_class = &fair_sched_class;
    4482                 :            :         }
    4483                 :            : 
    4484                 :          0 :         p->prio = prio;
    4485                 :            : 
    4486                 :          0 :         if (queued)
    4487                 :          0 :                 enqueue_task(rq, p, queue_flag);
    4488                 :          0 :         if (running)
    4489                 :          0 :                 set_next_task(rq, p);
    4490                 :            : 
    4491                 :          0 :         check_class_changed(rq, p, prev_class, oldprio);
    4492                 :            : out_unlock:
    4493                 :            :         /* Avoid rq from going away on us: */
    4494                 :          0 :         preempt_disable();
    4495                 :            :         __task_rq_unlock(rq, &rf);
    4496                 :            : 
    4497                 :            :         balance_callback(rq);
    4498                 :          0 :         preempt_enable();
    4499                 :            : }
    4500                 :            : #else
    4501                 :            : static inline int rt_effective_prio(struct task_struct *p, int prio)
    4502                 :            : {
    4503                 :            :         return prio;
    4504                 :            : }
    4505                 :            : #endif
    4506                 :            : 
    4507                 :          3 : void set_user_nice(struct task_struct *p, long nice)
    4508                 :            : {
    4509                 :            :         bool queued, running;
    4510                 :            :         int old_prio, delta;
    4511                 :            :         struct rq_flags rf;
    4512                 :            :         struct rq *rq;
    4513                 :            : 
    4514                 :          3 :         if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
    4515                 :          3 :                 return;
    4516                 :            :         /*
    4517                 :            :          * We have to be careful, if called from sys_setpriority(),
    4518                 :            :          * the task might be in the middle of scheduling on another CPU.
    4519                 :            :          */
    4520                 :          3 :         rq = task_rq_lock(p, &rf);
    4521                 :          3 :         update_rq_clock(rq);
    4522                 :            : 
    4523                 :            :         /*
    4524                 :            :          * The RT priorities are set via sched_setscheduler(), but we still
    4525                 :            :          * allow the 'normal' nice value to be set - but as expected
    4526                 :            :          * it wont have any effect on scheduling until the task is
    4527                 :            :          * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
    4528                 :            :          */
    4529                 :          3 :         if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
    4530                 :          0 :                 p->static_prio = NICE_TO_PRIO(nice);
    4531                 :          0 :                 goto out_unlock;
    4532                 :            :         }
    4533                 :            :         queued = task_on_rq_queued(p);
    4534                 :            :         running = task_current(rq, p);
    4535                 :          3 :         if (queued)
    4536                 :          3 :                 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
    4537                 :          3 :         if (running)
    4538                 :          3 :                 put_prev_task(rq, p);
    4539                 :            : 
    4540                 :          3 :         p->static_prio = NICE_TO_PRIO(nice);
    4541                 :          3 :         set_load_weight(p, true);
    4542                 :          3 :         old_prio = p->prio;
    4543                 :          3 :         p->prio = effective_prio(p);
    4544                 :          3 :         delta = p->prio - old_prio;
    4545                 :            : 
    4546                 :          3 :         if (queued) {
    4547                 :          3 :                 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
    4548                 :            :                 /*
    4549                 :            :                  * If the task increased its priority or is running and
    4550                 :            :                  * lowered its priority, then reschedule its CPU:
    4551                 :            :                  */
    4552                 :          3 :                 if (delta < 0 || (delta > 0 && task_running(rq, p)))
    4553                 :          3 :                         resched_curr(rq);
    4554                 :            :         }
    4555                 :          3 :         if (running)
    4556                 :          3 :                 set_next_task(rq, p);
    4557                 :            : out_unlock:
    4558                 :          3 :         task_rq_unlock(rq, p, &rf);
    4559                 :            : }
    4560                 :            : EXPORT_SYMBOL(set_user_nice);
    4561                 :            : 
    4562                 :            : /*
    4563                 :            :  * can_nice - check if a task can reduce its nice value
    4564                 :            :  * @p: task
    4565                 :            :  * @nice: nice value
    4566                 :            :  */
    4567                 :          0 : int can_nice(const struct task_struct *p, const int nice)
    4568                 :            : {
    4569                 :            :         /* Convert nice value [19,-20] to rlimit style value [1,40]: */
    4570                 :            :         int nice_rlim = nice_to_rlimit(nice);
    4571                 :            : 
    4572                 :          0 :         return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
    4573                 :          0 :                 capable(CAP_SYS_NICE));
    4574                 :            : }
    4575                 :            : 
    4576                 :            : #ifdef __ARCH_WANT_SYS_NICE
    4577                 :            : 
    4578                 :            : /*
    4579                 :            :  * sys_nice - change the priority of the current process.
    4580                 :            :  * @increment: priority increment
    4581                 :            :  *
    4582                 :            :  * sys_setpriority is a more generic, but much slower function that
    4583                 :            :  * does similar things.
    4584                 :            :  */
    4585                 :          0 : SYSCALL_DEFINE1(nice, int, increment)
    4586                 :            : {
    4587                 :            :         long nice, retval;
    4588                 :            : 
    4589                 :            :         /*
    4590                 :            :          * Setpriority might change our priority at the same moment.
    4591                 :            :          * We don't have to worry. Conceptually one call occurs first
    4592                 :            :          * and we have a single winner.
    4593                 :            :          */
    4594                 :          0 :         increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
    4595                 :          0 :         nice = task_nice(current) + increment;
    4596                 :            : 
    4597                 :          0 :         nice = clamp_val(nice, MIN_NICE, MAX_NICE);
    4598                 :          0 :         if (increment < 0 && !can_nice(current, nice))
    4599                 :            :                 return -EPERM;
    4600                 :            : 
    4601                 :          0 :         retval = security_task_setnice(current, nice);
    4602                 :          0 :         if (retval)
    4603                 :            :                 return retval;
    4604                 :            : 
    4605                 :          0 :         set_user_nice(current, nice);
    4606                 :          0 :         return 0;
    4607                 :            : }
    4608                 :            : 
    4609                 :            : #endif
    4610                 :            : 
    4611                 :            : /**
    4612                 :            :  * task_prio - return the priority value of a given task.
    4613                 :            :  * @p: the task in question.
    4614                 :            :  *
    4615                 :            :  * Return: The priority value as seen by users in /proc.
    4616                 :            :  * RT tasks are offset by -200. Normal tasks are centered
    4617                 :            :  * around 0, value goes from -16 to +15.
    4618                 :            :  */
    4619                 :          3 : int task_prio(const struct task_struct *p)
    4620                 :            : {
    4621                 :          3 :         return p->prio - MAX_RT_PRIO;
    4622                 :            : }
    4623                 :            : 
    4624                 :            : /**
    4625                 :            :  * idle_cpu - is a given CPU idle currently?
    4626                 :            :  * @cpu: the processor in question.
    4627                 :            :  *
    4628                 :            :  * Return: 1 if the CPU is currently idle. 0 otherwise.
    4629                 :            :  */
    4630                 :          3 : int idle_cpu(int cpu)
    4631                 :            : {
    4632                 :          3 :         struct rq *rq = cpu_rq(cpu);
    4633                 :            : 
    4634                 :          3 :         if (rq->curr != rq->idle)
    4635                 :            :                 return 0;
    4636                 :            : 
    4637                 :          3 :         if (rq->nr_running)
    4638                 :            :                 return 0;
    4639                 :            : 
    4640                 :            : #ifdef CONFIG_SMP
    4641                 :          3 :         if (!llist_empty(&rq->wake_list))
    4642                 :            :                 return 0;
    4643                 :            : #endif
    4644                 :            : 
    4645                 :          3 :         return 1;
    4646                 :            : }
    4647                 :            : 
    4648                 :            : /**
    4649                 :            :  * available_idle_cpu - is a given CPU idle for enqueuing work.
    4650                 :            :  * @cpu: the CPU in question.
    4651                 :            :  *
    4652                 :            :  * Return: 1 if the CPU is currently idle. 0 otherwise.
    4653                 :            :  */
    4654                 :          3 : int available_idle_cpu(int cpu)
    4655                 :            : {
    4656                 :          3 :         if (!idle_cpu(cpu))
    4657                 :            :                 return 0;
    4658                 :            : 
    4659                 :            :         if (vcpu_is_preempted(cpu))
    4660                 :            :                 return 0;
    4661                 :            : 
    4662                 :          3 :         return 1;
    4663                 :            : }
    4664                 :            : 
    4665                 :            : /**
    4666                 :            :  * idle_task - return the idle task for a given CPU.
    4667                 :            :  * @cpu: the processor in question.
    4668                 :            :  *
    4669                 :            :  * Return: The idle task for the CPU @cpu.
    4670                 :            :  */
    4671                 :          0 : struct task_struct *idle_task(int cpu)
    4672                 :            : {
    4673                 :          0 :         return cpu_rq(cpu)->idle;
    4674                 :            : }
    4675                 :            : 
    4676                 :            : /**
    4677                 :            :  * find_process_by_pid - find a process with a matching PID value.
    4678                 :            :  * @pid: the pid in question.
    4679                 :            :  *
    4680                 :            :  * The task of @pid, if found. %NULL otherwise.
    4681                 :            :  */
    4682                 :            : static struct task_struct *find_process_by_pid(pid_t pid)
    4683                 :            : {
    4684                 :          3 :         return pid ? find_task_by_vpid(pid) : current;
    4685                 :            : }
    4686                 :            : 
    4687                 :            : /*
    4688                 :            :  * sched_setparam() passes in -1 for its policy, to let the functions
    4689                 :            :  * it calls know not to change it.
    4690                 :            :  */
    4691                 :            : #define SETPARAM_POLICY -1
    4692                 :            : 
    4693                 :          3 : static void __setscheduler_params(struct task_struct *p,
    4694                 :            :                 const struct sched_attr *attr)
    4695                 :            : {
    4696                 :          3 :         int policy = attr->sched_policy;
    4697                 :            : 
    4698                 :          3 :         if (policy == SETPARAM_POLICY)
    4699                 :          0 :                 policy = p->policy;
    4700                 :            : 
    4701                 :          3 :         p->policy = policy;
    4702                 :            : 
    4703                 :          3 :         if (dl_policy(policy))
    4704                 :          0 :                 __setparam_dl(p, attr);
    4705                 :          3 :         else if (fair_policy(policy))
    4706                 :          0 :                 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
    4707                 :            : 
    4708                 :            :         /*
    4709                 :            :          * __sched_setscheduler() ensures attr->sched_priority == 0 when
    4710                 :            :          * !rt_policy. Always setting this ensures that things like
    4711                 :            :          * getparam()/getattr() don't report silly values for !rt tasks.
    4712                 :            :          */
    4713                 :          3 :         p->rt_priority = attr->sched_priority;
    4714                 :          3 :         p->normal_prio = normal_prio(p);
    4715                 :          3 :         set_load_weight(p, true);
    4716                 :          3 : }
    4717                 :            : 
    4718                 :            : /* Actually do priority change: must hold pi & rq lock. */
    4719                 :          3 : static void __setscheduler(struct rq *rq, struct task_struct *p,
    4720                 :            :                            const struct sched_attr *attr, bool keep_boost)
    4721                 :            : {
    4722                 :            :         /*
    4723                 :            :          * If params can't change scheduling class changes aren't allowed
    4724                 :            :          * either.
    4725                 :            :          */
    4726                 :          3 :         if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)
    4727                 :          3 :                 return;
    4728                 :            : 
    4729                 :          3 :         __setscheduler_params(p, attr);
    4730                 :            : 
    4731                 :            :         /*
    4732                 :            :          * Keep a potential priority boosting if called from
    4733                 :            :          * sched_setscheduler().
    4734                 :            :          */
    4735                 :          3 :         p->prio = normal_prio(p);
    4736                 :          3 :         if (keep_boost)
    4737                 :          3 :                 p->prio = rt_effective_prio(p, p->prio);
    4738                 :            : 
    4739                 :          3 :         if (dl_prio(p->prio))
    4740                 :          0 :                 p->sched_class = &dl_sched_class;
    4741                 :          3 :         else if (rt_prio(p->prio))
    4742                 :          3 :                 p->sched_class = &rt_sched_class;
    4743                 :            :         else
    4744                 :          0 :                 p->sched_class = &fair_sched_class;
    4745                 :            : }
    4746                 :            : 
    4747                 :            : /*
    4748                 :            :  * Check the target process has a UID that matches the current process's:
    4749                 :            :  */
    4750                 :          0 : static bool check_same_owner(struct task_struct *p)
    4751                 :            : {
    4752                 :          0 :         const struct cred *cred = current_cred(), *pcred;
    4753                 :            :         bool match;
    4754                 :            : 
    4755                 :            :         rcu_read_lock();
    4756                 :          0 :         pcred = __task_cred(p);
    4757                 :          0 :         match = (uid_eq(cred->euid, pcred->euid) ||
    4758                 :            :                  uid_eq(cred->euid, pcred->uid));
    4759                 :            :         rcu_read_unlock();
    4760                 :          0 :         return match;
    4761                 :            : }
    4762                 :            : 
    4763                 :          3 : static int __sched_setscheduler(struct task_struct *p,
    4764                 :            :                                 const struct sched_attr *attr,
    4765                 :            :                                 bool user, bool pi)
    4766                 :            : {
    4767                 :          3 :         int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
    4768                 :          3 :                       MAX_RT_PRIO - 1 - attr->sched_priority;
    4769                 :            :         int retval, oldprio, oldpolicy = -1, queued, running;
    4770                 :          3 :         int new_effective_prio, policy = attr->sched_policy;
    4771                 :            :         const struct sched_class *prev_class;
    4772                 :            :         struct rq_flags rf;
    4773                 :            :         int reset_on_fork;
    4774                 :            :         int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
    4775                 :            :         struct rq *rq;
    4776                 :            : 
    4777                 :            :         /* The pi code expects interrupts enabled */
    4778                 :          3 :         BUG_ON(pi && in_interrupt());
    4779                 :            : recheck:
    4780                 :            :         /* Double check policy once rq lock held: */
    4781                 :          3 :         if (policy < 0) {
    4782                 :          0 :                 reset_on_fork = p->sched_reset_on_fork;
    4783                 :          0 :                 policy = oldpolicy = p->policy;
    4784                 :            :         } else {
    4785                 :          3 :                 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
    4786                 :            : 
    4787                 :          3 :                 if (!valid_policy(policy))
    4788                 :            :                         return -EINVAL;
    4789                 :            :         }
    4790                 :            : 
    4791                 :          3 :         if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV))
    4792                 :            :                 return -EINVAL;
    4793                 :            : 
    4794                 :            :         /*
    4795                 :            :          * Valid priorities for SCHED_FIFO and SCHED_RR are
    4796                 :            :          * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
    4797                 :            :          * SCHED_BATCH and SCHED_IDLE is 0.
    4798                 :            :          */
    4799                 :          3 :         if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
    4800                 :            :             (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
    4801                 :            :                 return -EINVAL;
    4802                 :          3 :         if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
    4803                 :          3 :             (rt_policy(policy) != (attr->sched_priority != 0)))
    4804                 :            :                 return -EINVAL;
    4805                 :            : 
    4806                 :            :         /*
    4807                 :            :          * Allow unprivileged RT tasks to decrease priority:
    4808                 :            :          */
    4809                 :          3 :         if (user && !capable(CAP_SYS_NICE)) {
    4810                 :          0 :                 if (fair_policy(policy)) {
    4811                 :          0 :                         if (attr->sched_nice < task_nice(p) &&
    4812                 :          0 :                             !can_nice(p, attr->sched_nice))
    4813                 :            :                                 return -EPERM;
    4814                 :            :                 }
    4815                 :            : 
    4816                 :          0 :                 if (rt_policy(policy)) {
    4817                 :            :                         unsigned long rlim_rtprio =
    4818                 :            :                                         task_rlimit(p, RLIMIT_RTPRIO);
    4819                 :            : 
    4820                 :            :                         /* Can't set/change the rt policy: */
    4821                 :          0 :                         if (policy != p->policy && !rlim_rtprio)
    4822                 :            :                                 return -EPERM;
    4823                 :            : 
    4824                 :            :                         /* Can't increase priority: */
    4825                 :          0 :                         if (attr->sched_priority > p->rt_priority &&
    4826                 :            :                             attr->sched_priority > rlim_rtprio)
    4827                 :            :                                 return -EPERM;
    4828                 :            :                 }
    4829                 :            : 
    4830                 :            :                  /*
    4831                 :            :                   * Can't set/change SCHED_DEADLINE policy at all for now
    4832                 :            :                   * (safest behavior); in the future we would like to allow
    4833                 :            :                   * unprivileged DL tasks to increase their relative deadline
    4834                 :            :                   * or reduce their runtime (both ways reducing utilization)
    4835                 :            :                   */
    4836                 :          0 :                 if (dl_policy(policy))
    4837                 :            :                         return -EPERM;
    4838                 :            : 
    4839                 :            :                 /*
    4840                 :            :                  * Treat SCHED_IDLE as nice 20. Only allow a switch to
    4841                 :            :                  * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
    4842                 :            :                  */
    4843                 :          0 :                 if (task_has_idle_policy(p) && !idle_policy(policy)) {
    4844                 :          0 :                         if (!can_nice(p, task_nice(p)))
    4845                 :            :                                 return -EPERM;
    4846                 :            :                 }
    4847                 :            : 
    4848                 :            :                 /* Can't change other user's priorities: */
    4849                 :          0 :                 if (!check_same_owner(p))
    4850                 :            :                         return -EPERM;
    4851                 :            : 
    4852                 :            :                 /* Normal users shall not reset the sched_reset_on_fork flag: */
    4853                 :          0 :                 if (p->sched_reset_on_fork && !reset_on_fork)
    4854                 :            :                         return -EPERM;
    4855                 :            :         }
    4856                 :            : 
    4857                 :          3 :         if (user) {
    4858                 :          3 :                 if (attr->sched_flags & SCHED_FLAG_SUGOV)
    4859                 :            :                         return -EINVAL;
    4860                 :            : 
    4861                 :          3 :                 retval = security_task_setscheduler(p);
    4862                 :          3 :                 if (retval)
    4863                 :          0 :                         return retval;
    4864                 :            :         }
    4865                 :            : 
    4866                 :            :         /* Update task specific "requested" clamps */
    4867                 :          3 :         if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) {
    4868                 :            :                 retval = uclamp_validate(p, attr);
    4869                 :            :                 if (retval)
    4870                 :            :                         return retval;
    4871                 :            :         }
    4872                 :            : 
    4873                 :          3 :         if (pi)
    4874                 :          3 :                 cpuset_read_lock();
    4875                 :            : 
    4876                 :            :         /*
    4877                 :            :          * Make sure no PI-waiters arrive (or leave) while we are
    4878                 :            :          * changing the priority of the task:
    4879                 :            :          *
    4880                 :            :          * To be able to change p->policy safely, the appropriate
    4881                 :            :          * runqueue lock must be held.
    4882                 :            :          */
    4883                 :          3 :         rq = task_rq_lock(p, &rf);
    4884                 :          3 :         update_rq_clock(rq);
    4885                 :            : 
    4886                 :            :         /*
    4887                 :            :          * Changing the policy of the stop threads its a very bad idea:
    4888                 :            :          */
    4889                 :          3 :         if (p == rq->stop) {
    4890                 :            :                 retval = -EINVAL;
    4891                 :            :                 goto unlock;
    4892                 :            :         }
    4893                 :            : 
    4894                 :            :         /*
    4895                 :            :          * If not changing anything there's no need to proceed further,
    4896                 :            :          * but store a possible modification of reset_on_fork.
    4897                 :            :          */
    4898                 :          3 :         if (unlikely(policy == p->policy)) {
    4899                 :          3 :                 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
    4900                 :            :                         goto change;
    4901                 :          3 :                 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
    4902                 :            :                         goto change;
    4903                 :          3 :                 if (dl_policy(policy) && dl_param_changed(p, attr))
    4904                 :            :                         goto change;
    4905                 :          3 :                 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
    4906                 :            :                         goto change;
    4907                 :            : 
    4908                 :          3 :                 p->sched_reset_on_fork = reset_on_fork;
    4909                 :            :                 retval = 0;
    4910                 :          3 :                 goto unlock;
    4911                 :            :         }
    4912                 :            : change:
    4913                 :            : 
    4914                 :          3 :         if (user) {
    4915                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    4916                 :            :                 /*
    4917                 :            :                  * Do not allow realtime tasks into groups that have no runtime
    4918                 :            :                  * assigned.
    4919                 :            :                  */
    4920                 :            :                 if (rt_bandwidth_enabled() && rt_policy(policy) &&
    4921                 :            :                                 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
    4922                 :            :                                 !task_group_is_autogroup(task_group(p))) {
    4923                 :            :                         retval = -EPERM;
    4924                 :            :                         goto unlock;
    4925                 :            :                 }
    4926                 :            : #endif
    4927                 :            : #ifdef CONFIG_SMP
    4928                 :          3 :                 if (dl_bandwidth_enabled() && dl_policy(policy) &&
    4929                 :          0 :                                 !(attr->sched_flags & SCHED_FLAG_SUGOV)) {
    4930                 :          0 :                         cpumask_t *span = rq->rd->span;
    4931                 :            : 
    4932                 :            :                         /*
    4933                 :            :                          * Don't allow tasks with an affinity mask smaller than
    4934                 :            :                          * the entire root_domain to become SCHED_DEADLINE. We
    4935                 :            :                          * will also fail if there's no bandwidth available.
    4936                 :            :                          */
    4937                 :          0 :                         if (!cpumask_subset(span, p->cpus_ptr) ||
    4938                 :          0 :                             rq->rd->dl_bw.bw == 0) {
    4939                 :            :                                 retval = -EPERM;
    4940                 :            :                                 goto unlock;
    4941                 :            :                         }
    4942                 :            :                 }
    4943                 :            : #endif
    4944                 :            :         }
    4945                 :            : 
    4946                 :            :         /* Re-check policy now with rq lock held: */
    4947                 :          3 :         if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
    4948                 :            :                 policy = oldpolicy = -1;
    4949                 :          0 :                 task_rq_unlock(rq, p, &rf);
    4950                 :          0 :                 if (pi)
    4951                 :          0 :                         cpuset_read_unlock();
    4952                 :            :                 goto recheck;
    4953                 :            :         }
    4954                 :            : 
    4955                 :            :         /*
    4956                 :            :          * If setscheduling to SCHED_DEADLINE (or changing the parameters
    4957                 :            :          * of a SCHED_DEADLINE task) we need to check if enough bandwidth
    4958                 :            :          * is available.
    4959                 :            :          */
    4960                 :          3 :         if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
    4961                 :            :                 retval = -EBUSY;
    4962                 :            :                 goto unlock;
    4963                 :            :         }
    4964                 :            : 
    4965                 :          3 :         p->sched_reset_on_fork = reset_on_fork;
    4966                 :          3 :         oldprio = p->prio;
    4967                 :            : 
    4968                 :          3 :         if (pi) {
    4969                 :            :                 /*
    4970                 :            :                  * Take priority boosted tasks into account. If the new
    4971                 :            :                  * effective priority is unchanged, we just store the new
    4972                 :            :                  * normal parameters and do not touch the scheduler class and
    4973                 :            :                  * the runqueue. This will be done when the task deboost
    4974                 :            :                  * itself.
    4975                 :            :                  */
    4976                 :            :                 new_effective_prio = rt_effective_prio(p, newprio);
    4977                 :          3 :                 if (new_effective_prio == oldprio)
    4978                 :            :                         queue_flags &= ~DEQUEUE_MOVE;
    4979                 :            :         }
    4980                 :            : 
    4981                 :            :         queued = task_on_rq_queued(p);
    4982                 :            :         running = task_current(rq, p);
    4983                 :          3 :         if (queued)
    4984                 :          3 :                 dequeue_task(rq, p, queue_flags);
    4985                 :          3 :         if (running)
    4986                 :          1 :                 put_prev_task(rq, p);
    4987                 :            : 
    4988                 :          3 :         prev_class = p->sched_class;
    4989                 :            : 
    4990                 :          3 :         __setscheduler(rq, p, attr, pi);
    4991                 :            :         __setscheduler_uclamp(p, attr);
    4992                 :            : 
    4993                 :          3 :         if (queued) {
    4994                 :            :                 /*
    4995                 :            :                  * We enqueue to tail when the priority of a task is
    4996                 :            :                  * increased (user space view).
    4997                 :            :                  */
    4998                 :          3 :                 if (oldprio < p->prio)
    4999                 :          0 :                         queue_flags |= ENQUEUE_HEAD;
    5000                 :            : 
    5001                 :          3 :                 enqueue_task(rq, p, queue_flags);
    5002                 :            :         }
    5003                 :          3 :         if (running)
    5004                 :          1 :                 set_next_task(rq, p);
    5005                 :            : 
    5006                 :          3 :         check_class_changed(rq, p, prev_class, oldprio);
    5007                 :            : 
    5008                 :            :         /* Avoid rq from going away on us: */
    5009                 :          3 :         preempt_disable();
    5010                 :          3 :         task_rq_unlock(rq, p, &rf);
    5011                 :            : 
    5012                 :          3 :         if (pi) {
    5013                 :          3 :                 cpuset_read_unlock();
    5014                 :          3 :                 rt_mutex_adjust_pi(p);
    5015                 :            :         }
    5016                 :            : 
    5017                 :            :         /* Run balance callbacks after we've adjusted the PI chain: */
    5018                 :            :         balance_callback(rq);
    5019                 :          3 :         preempt_enable();
    5020                 :            : 
    5021                 :          3 :         return 0;
    5022                 :            : 
    5023                 :            : unlock:
    5024                 :          3 :         task_rq_unlock(rq, p, &rf);
    5025                 :          3 :         if (pi)
    5026                 :          3 :                 cpuset_read_unlock();
    5027                 :          3 :         return retval;
    5028                 :            : }
    5029                 :            : 
    5030                 :          3 : static int _sched_setscheduler(struct task_struct *p, int policy,
    5031                 :            :                                const struct sched_param *param, bool check)
    5032                 :            : {
    5033                 :          3 :         struct sched_attr attr = {
    5034                 :            :                 .sched_policy   = policy,
    5035                 :          3 :                 .sched_priority = param->sched_priority,
    5036                 :          3 :                 .sched_nice     = PRIO_TO_NICE(p->static_prio),
    5037                 :            :         };
    5038                 :            : 
    5039                 :            :         /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
    5040                 :          3 :         if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
    5041                 :          0 :                 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
    5042                 :          0 :                 policy &= ~SCHED_RESET_ON_FORK;
    5043                 :          0 :                 attr.sched_policy = policy;
    5044                 :            :         }
    5045                 :            : 
    5046                 :          3 :         return __sched_setscheduler(p, &attr, check, true);
    5047                 :            : }
    5048                 :            : /**
    5049                 :            :  * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
    5050                 :            :  * @p: the task in question.
    5051                 :            :  * @policy: new policy.
    5052                 :            :  * @param: structure containing the new RT priority.
    5053                 :            :  *
    5054                 :            :  * Return: 0 on success. An error code otherwise.
    5055                 :            :  *
    5056                 :            :  * NOTE that the task may be already dead.
    5057                 :            :  */
    5058                 :          3 : int sched_setscheduler(struct task_struct *p, int policy,
    5059                 :            :                        const struct sched_param *param)
    5060                 :            : {
    5061                 :          3 :         return _sched_setscheduler(p, policy, param, true);
    5062                 :            : }
    5063                 :            : EXPORT_SYMBOL_GPL(sched_setscheduler);
    5064                 :            : 
    5065                 :          0 : int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
    5066                 :            : {
    5067                 :          0 :         return __sched_setscheduler(p, attr, true, true);
    5068                 :            : }
    5069                 :            : EXPORT_SYMBOL_GPL(sched_setattr);
    5070                 :            : 
    5071                 :          0 : int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
    5072                 :            : {
    5073                 :          0 :         return __sched_setscheduler(p, attr, false, true);
    5074                 :            : }
    5075                 :            : 
    5076                 :            : /**
    5077                 :            :  * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
    5078                 :            :  * @p: the task in question.
    5079                 :            :  * @policy: new policy.
    5080                 :            :  * @param: structure containing the new RT priority.
    5081                 :            :  *
    5082                 :            :  * Just like sched_setscheduler, only don't bother checking if the
    5083                 :            :  * current context has permission.  For example, this is needed in
    5084                 :            :  * stop_machine(): we create temporary high priority worker threads,
    5085                 :            :  * but our caller might not have that capability.
    5086                 :            :  *
    5087                 :            :  * Return: 0 on success. An error code otherwise.
    5088                 :            :  */
    5089                 :          3 : int sched_setscheduler_nocheck(struct task_struct *p, int policy,
    5090                 :            :                                const struct sched_param *param)
    5091                 :            : {
    5092                 :          3 :         return _sched_setscheduler(p, policy, param, false);
    5093                 :            : }
    5094                 :            : EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
    5095                 :            : 
    5096                 :            : static int
    5097                 :          3 : do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
    5098                 :            : {
    5099                 :            :         struct sched_param lparam;
    5100                 :            :         struct task_struct *p;
    5101                 :            :         int retval;
    5102                 :            : 
    5103                 :          3 :         if (!param || pid < 0)
    5104                 :            :                 return -EINVAL;
    5105                 :          3 :         if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
    5106                 :            :                 return -EFAULT;
    5107                 :            : 
    5108                 :            :         rcu_read_lock();
    5109                 :            :         retval = -ESRCH;
    5110                 :            :         p = find_process_by_pid(pid);
    5111                 :          3 :         if (likely(p))
    5112                 :            :                 get_task_struct(p);
    5113                 :            :         rcu_read_unlock();
    5114                 :            : 
    5115                 :          3 :         if (likely(p)) {
    5116                 :            :                 retval = sched_setscheduler(p, policy, &lparam);
    5117                 :          3 :                 put_task_struct(p);
    5118                 :            :         }
    5119                 :            : 
    5120                 :          3 :         return retval;
    5121                 :            : }
    5122                 :            : 
    5123                 :            : /*
    5124                 :            :  * Mimics kernel/events/core.c perf_copy_attr().
    5125                 :            :  */
    5126                 :          0 : static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
    5127                 :            : {
    5128                 :            :         u32 size;
    5129                 :            :         int ret;
    5130                 :            : 
    5131                 :            :         /* Zero the full structure, so that a short copy will be nice: */
    5132                 :          0 :         memset(attr, 0, sizeof(*attr));
    5133                 :            : 
    5134                 :          0 :         ret = get_user(size, &uattr->size);
    5135                 :          0 :         if (ret)
    5136                 :            :                 return ret;
    5137                 :            : 
    5138                 :            :         /* ABI compatibility quirk: */
    5139                 :          0 :         if (!size)
    5140                 :            :                 size = SCHED_ATTR_SIZE_VER0;
    5141                 :          0 :         if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
    5142                 :            :                 goto err_size;
    5143                 :            : 
    5144                 :            :         ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
    5145                 :          0 :         if (ret) {
    5146                 :          0 :                 if (ret == -E2BIG)
    5147                 :            :                         goto err_size;
    5148                 :            :                 return ret;
    5149                 :            :         }
    5150                 :            : 
    5151                 :          0 :         if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
    5152                 :            :             size < SCHED_ATTR_SIZE_VER1)
    5153                 :            :                 return -EINVAL;
    5154                 :            : 
    5155                 :            :         /*
    5156                 :            :          * XXX: Do we want to be lenient like existing syscalls; or do we want
    5157                 :            :          * to be strict and return an error on out-of-bounds values?
    5158                 :            :          */
    5159                 :          0 :         attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
    5160                 :            : 
    5161                 :          0 :         return 0;
    5162                 :            : 
    5163                 :            : err_size:
    5164                 :          0 :         put_user(sizeof(*attr), &uattr->size);
    5165                 :          0 :         return -E2BIG;
    5166                 :            : }
    5167                 :            : 
    5168                 :            : /**
    5169                 :            :  * sys_sched_setscheduler - set/change the scheduler policy and RT priority
    5170                 :            :  * @pid: the pid in question.
    5171                 :            :  * @policy: new policy.
    5172                 :            :  * @param: structure containing the new RT priority.
    5173                 :            :  *
    5174                 :            :  * Return: 0 on success. An error code otherwise.
    5175                 :            :  */
    5176                 :          3 : SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
    5177                 :            : {
    5178                 :          3 :         if (policy < 0)
    5179                 :            :                 return -EINVAL;
    5180                 :            : 
    5181                 :          3 :         return do_sched_setscheduler(pid, policy, param);
    5182                 :            : }
    5183                 :            : 
    5184                 :            : /**
    5185                 :            :  * sys_sched_setparam - set/change the RT priority of a thread
    5186                 :            :  * @pid: the pid in question.
    5187                 :            :  * @param: structure containing the new RT priority.
    5188                 :            :  *
    5189                 :            :  * Return: 0 on success. An error code otherwise.
    5190                 :            :  */
    5191                 :          0 : SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
    5192                 :            : {
    5193                 :          0 :         return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
    5194                 :            : }
    5195                 :            : 
    5196                 :            : /**
    5197                 :            :  * sys_sched_setattr - same as above, but with extended sched_attr
    5198                 :            :  * @pid: the pid in question.
    5199                 :            :  * @uattr: structure containing the extended parameters.
    5200                 :            :  * @flags: for future extension.
    5201                 :            :  */
    5202                 :          0 : SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
    5203                 :            :                                unsigned int, flags)
    5204                 :            : {
    5205                 :            :         struct sched_attr attr;
    5206                 :            :         struct task_struct *p;
    5207                 :            :         int retval;
    5208                 :            : 
    5209                 :          0 :         if (!uattr || pid < 0 || flags)
    5210                 :            :                 return -EINVAL;
    5211                 :            : 
    5212                 :          0 :         retval = sched_copy_attr(uattr, &attr);
    5213                 :          0 :         if (retval)
    5214                 :            :                 return retval;
    5215                 :            : 
    5216                 :          0 :         if ((int)attr.sched_policy < 0)
    5217                 :            :                 return -EINVAL;
    5218                 :          0 :         if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY)
    5219                 :          0 :                 attr.sched_policy = SETPARAM_POLICY;
    5220                 :            : 
    5221                 :            :         rcu_read_lock();
    5222                 :            :         retval = -ESRCH;
    5223                 :            :         p = find_process_by_pid(pid);
    5224                 :          0 :         if (likely(p))
    5225                 :            :                 get_task_struct(p);
    5226                 :            :         rcu_read_unlock();
    5227                 :            : 
    5228                 :          0 :         if (likely(p)) {
    5229                 :            :                 retval = sched_setattr(p, &attr);
    5230                 :          0 :                 put_task_struct(p);
    5231                 :            :         }
    5232                 :            : 
    5233                 :          0 :         return retval;
    5234                 :            : }
    5235                 :            : 
    5236                 :            : /**
    5237                 :            :  * sys_sched_getscheduler - get the policy (scheduling class) of a thread
    5238                 :            :  * @pid: the pid in question.
    5239                 :            :  *
    5240                 :            :  * Return: On success, the policy of the thread. Otherwise, a negative error
    5241                 :            :  * code.
    5242                 :            :  */
    5243                 :          3 : SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
    5244                 :            : {
    5245                 :            :         struct task_struct *p;
    5246                 :            :         int retval;
    5247                 :            : 
    5248                 :          3 :         if (pid < 0)
    5249                 :            :                 return -EINVAL;
    5250                 :            : 
    5251                 :            :         retval = -ESRCH;
    5252                 :            :         rcu_read_lock();
    5253                 :            :         p = find_process_by_pid(pid);
    5254                 :          3 :         if (p) {
    5255                 :          3 :                 retval = security_task_getscheduler(p);
    5256                 :          3 :                 if (!retval)
    5257                 :          3 :                         retval = p->policy
    5258                 :          3 :                                 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
    5259                 :            :         }
    5260                 :            :         rcu_read_unlock();
    5261                 :          3 :         return retval;
    5262                 :            : }
    5263                 :            : 
    5264                 :            : /**
    5265                 :            :  * sys_sched_getparam - get the RT priority of a thread
    5266                 :            :  * @pid: the pid in question.
    5267                 :            :  * @param: structure containing the RT priority.
    5268                 :            :  *
    5269                 :            :  * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
    5270                 :            :  * code.
    5271                 :            :  */
    5272                 :          3 : SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
    5273                 :            : {
    5274                 :          3 :         struct sched_param lp = { .sched_priority = 0 };
    5275                 :            :         struct task_struct *p;
    5276                 :            :         int retval;
    5277                 :            : 
    5278                 :          3 :         if (!param || pid < 0)
    5279                 :            :                 return -EINVAL;
    5280                 :            : 
    5281                 :            :         rcu_read_lock();
    5282                 :            :         p = find_process_by_pid(pid);
    5283                 :            :         retval = -ESRCH;
    5284                 :          3 :         if (!p)
    5285                 :            :                 goto out_unlock;
    5286                 :            : 
    5287                 :          3 :         retval = security_task_getscheduler(p);
    5288                 :          3 :         if (retval)
    5289                 :            :                 goto out_unlock;
    5290                 :            : 
    5291                 :          3 :         if (task_has_rt_policy(p))
    5292                 :          0 :                 lp.sched_priority = p->rt_priority;
    5293                 :            :         rcu_read_unlock();
    5294                 :            : 
    5295                 :            :         /*
    5296                 :            :          * This one might sleep, we cannot do it with a spinlock held ...
    5297                 :            :          */
    5298                 :          3 :         retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
    5299                 :            : 
    5300                 :          3 :         return retval;
    5301                 :            : 
    5302                 :            : out_unlock:
    5303                 :            :         rcu_read_unlock();
    5304                 :          0 :         return retval;
    5305                 :            : }
    5306                 :            : 
    5307                 :            : /*
    5308                 :            :  * Copy the kernel size attribute structure (which might be larger
    5309                 :            :  * than what user-space knows about) to user-space.
    5310                 :            :  *
    5311                 :            :  * Note that all cases are valid: user-space buffer can be larger or
    5312                 :            :  * smaller than the kernel-space buffer. The usual case is that both
    5313                 :            :  * have the same size.
    5314                 :            :  */
    5315                 :            : static int
    5316                 :          0 : sched_attr_copy_to_user(struct sched_attr __user *uattr,
    5317                 :            :                         struct sched_attr *kattr,
    5318                 :            :                         unsigned int usize)
    5319                 :            : {
    5320                 :            :         unsigned int ksize = sizeof(*kattr);
    5321                 :            : 
    5322                 :          0 :         if (!access_ok(uattr, usize))
    5323                 :            :                 return -EFAULT;
    5324                 :            : 
    5325                 :            :         /*
    5326                 :            :          * sched_getattr() ABI forwards and backwards compatibility:
    5327                 :            :          *
    5328                 :            :          * If usize == ksize then we just copy everything to user-space and all is good.
    5329                 :            :          *
    5330                 :            :          * If usize < ksize then we only copy as much as user-space has space for,
    5331                 :            :          * this keeps ABI compatibility as well. We skip the rest.
    5332                 :            :          *
    5333                 :            :          * If usize > ksize then user-space is using a newer version of the ABI,
    5334                 :            :          * which part the kernel doesn't know about. Just ignore it - tooling can
    5335                 :            :          * detect the kernel's knowledge of attributes from the attr->size value
    5336                 :            :          * which is set to ksize in this case.
    5337                 :            :          */
    5338                 :          0 :         kattr->size = min(usize, ksize);
    5339                 :            : 
    5340                 :          0 :         if (copy_to_user(uattr, kattr, kattr->size))
    5341                 :            :                 return -EFAULT;
    5342                 :            : 
    5343                 :          0 :         return 0;
    5344                 :            : }
    5345                 :            : 
    5346                 :            : /**
    5347                 :            :  * sys_sched_getattr - similar to sched_getparam, but with sched_attr
    5348                 :            :  * @pid: the pid in question.
    5349                 :            :  * @uattr: structure containing the extended parameters.
    5350                 :            :  * @usize: sizeof(attr) for fwd/bwd comp.
    5351                 :            :  * @flags: for future extension.
    5352                 :            :  */
    5353                 :          0 : SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
    5354                 :            :                 unsigned int, usize, unsigned int, flags)
    5355                 :            : {
    5356                 :          0 :         struct sched_attr kattr = { };
    5357                 :            :         struct task_struct *p;
    5358                 :            :         int retval;
    5359                 :            : 
    5360                 :          0 :         if (!uattr || pid < 0 || usize > PAGE_SIZE ||
    5361                 :          0 :             usize < SCHED_ATTR_SIZE_VER0 || flags)
    5362                 :            :                 return -EINVAL;
    5363                 :            : 
    5364                 :            :         rcu_read_lock();
    5365                 :            :         p = find_process_by_pid(pid);
    5366                 :            :         retval = -ESRCH;
    5367                 :          0 :         if (!p)
    5368                 :            :                 goto out_unlock;
    5369                 :            : 
    5370                 :          0 :         retval = security_task_getscheduler(p);
    5371                 :          0 :         if (retval)
    5372                 :            :                 goto out_unlock;
    5373                 :            : 
    5374                 :          0 :         kattr.sched_policy = p->policy;
    5375                 :          0 :         if (p->sched_reset_on_fork)
    5376                 :          0 :                 kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
    5377                 :          0 :         if (task_has_dl_policy(p))
    5378                 :          0 :                 __getparam_dl(p, &kattr);
    5379                 :          0 :         else if (task_has_rt_policy(p))
    5380                 :          0 :                 kattr.sched_priority = p->rt_priority;
    5381                 :            :         else
    5382                 :          0 :                 kattr.sched_nice = task_nice(p);
    5383                 :            : 
    5384                 :            : #ifdef CONFIG_UCLAMP_TASK
    5385                 :            :         kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value;
    5386                 :            :         kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value;
    5387                 :            : #endif
    5388                 :            : 
    5389                 :            :         rcu_read_unlock();
    5390                 :            : 
    5391                 :          0 :         return sched_attr_copy_to_user(uattr, &kattr, usize);
    5392                 :            : 
    5393                 :            : out_unlock:
    5394                 :            :         rcu_read_unlock();
    5395                 :          0 :         return retval;
    5396                 :            : }
    5397                 :            : 
    5398                 :          0 : long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
    5399                 :            : {
    5400                 :            :         cpumask_var_t cpus_allowed, new_mask;
    5401                 :            :         struct task_struct *p;
    5402                 :            :         int retval;
    5403                 :            : 
    5404                 :            :         rcu_read_lock();
    5405                 :            : 
    5406                 :            :         p = find_process_by_pid(pid);
    5407                 :          0 :         if (!p) {
    5408                 :            :                 rcu_read_unlock();
    5409                 :          0 :                 return -ESRCH;
    5410                 :            :         }
    5411                 :            : 
    5412                 :            :         /* Prevent p going away */
    5413                 :            :         get_task_struct(p);
    5414                 :            :         rcu_read_unlock();
    5415                 :            : 
    5416                 :          0 :         if (p->flags & PF_NO_SETAFFINITY) {
    5417                 :            :                 retval = -EINVAL;
    5418                 :            :                 goto out_put_task;
    5419                 :            :         }
    5420                 :            :         if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
    5421                 :            :                 retval = -ENOMEM;
    5422                 :            :                 goto out_put_task;
    5423                 :            :         }
    5424                 :            :         if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
    5425                 :            :                 retval = -ENOMEM;
    5426                 :            :                 goto out_free_cpus_allowed;
    5427                 :            :         }
    5428                 :            :         retval = -EPERM;
    5429                 :          0 :         if (!check_same_owner(p)) {
    5430                 :            :                 rcu_read_lock();
    5431                 :          0 :                 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
    5432                 :            :                         rcu_read_unlock();
    5433                 :            :                         goto out_free_new_mask;
    5434                 :            :                 }
    5435                 :            :                 rcu_read_unlock();
    5436                 :            :         }
    5437                 :            : 
    5438                 :          0 :         retval = security_task_setscheduler(p);
    5439                 :          0 :         if (retval)
    5440                 :            :                 goto out_free_new_mask;
    5441                 :            : 
    5442                 :            : 
    5443                 :          0 :         cpuset_cpus_allowed(p, cpus_allowed);
    5444                 :            :         cpumask_and(new_mask, in_mask, cpus_allowed);
    5445                 :            : 
    5446                 :            :         /*
    5447                 :            :          * Since bandwidth control happens on root_domain basis,
    5448                 :            :          * if admission test is enabled, we only admit -deadline
    5449                 :            :          * tasks allowed to run on all the CPUs in the task's
    5450                 :            :          * root_domain.
    5451                 :            :          */
    5452                 :            : #ifdef CONFIG_SMP
    5453                 :          0 :         if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
    5454                 :            :                 rcu_read_lock();
    5455                 :          0 :                 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
    5456                 :            :                         retval = -EBUSY;
    5457                 :            :                         rcu_read_unlock();
    5458                 :            :                         goto out_free_new_mask;
    5459                 :            :                 }
    5460                 :            :                 rcu_read_unlock();
    5461                 :            :         }
    5462                 :            : #endif
    5463                 :            : again:
    5464                 :          0 :         retval = __set_cpus_allowed_ptr(p, new_mask, true);
    5465                 :            : 
    5466                 :          0 :         if (!retval) {
    5467                 :          0 :                 cpuset_cpus_allowed(p, cpus_allowed);
    5468                 :          0 :                 if (!cpumask_subset(new_mask, cpus_allowed)) {
    5469                 :            :                         /*
    5470                 :            :                          * We must have raced with a concurrent cpuset
    5471                 :            :                          * update. Just reset the cpus_allowed to the
    5472                 :            :                          * cpuset's cpus_allowed
    5473                 :            :                          */
    5474                 :            :                         cpumask_copy(new_mask, cpus_allowed);
    5475                 :            :                         goto again;
    5476                 :            :                 }
    5477                 :            :         }
    5478                 :            : out_free_new_mask:
    5479                 :            :         free_cpumask_var(new_mask);
    5480                 :            : out_free_cpus_allowed:
    5481                 :            :         free_cpumask_var(cpus_allowed);
    5482                 :            : out_put_task:
    5483                 :          0 :         put_task_struct(p);
    5484                 :          0 :         return retval;
    5485                 :            : }
    5486                 :            : 
    5487                 :          0 : static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
    5488                 :            :                              struct cpumask *new_mask)
    5489                 :            : {
    5490                 :          0 :         if (len < cpumask_size())
    5491                 :            :                 cpumask_clear(new_mask);
    5492                 :          0 :         else if (len > cpumask_size())
    5493                 :            :                 len = cpumask_size();
    5494                 :            : 
    5495                 :          0 :         return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
    5496                 :            : }
    5497                 :            : 
    5498                 :            : /**
    5499                 :            :  * sys_sched_setaffinity - set the CPU affinity of a process
    5500                 :            :  * @pid: pid of the process
    5501                 :            :  * @len: length in bytes of the bitmask pointed to by user_mask_ptr
    5502                 :            :  * @user_mask_ptr: user-space pointer to the new CPU mask
    5503                 :            :  *
    5504                 :            :  * Return: 0 on success. An error code otherwise.
    5505                 :            :  */
    5506                 :          0 : SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
    5507                 :            :                 unsigned long __user *, user_mask_ptr)
    5508                 :            : {
    5509                 :            :         cpumask_var_t new_mask;
    5510                 :            :         int retval;
    5511                 :            : 
    5512                 :            :         if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
    5513                 :            :                 return -ENOMEM;
    5514                 :            : 
    5515                 :          0 :         retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
    5516                 :          0 :         if (retval == 0)
    5517                 :          0 :                 retval = sched_setaffinity(pid, new_mask);
    5518                 :            :         free_cpumask_var(new_mask);
    5519                 :            :         return retval;
    5520                 :            : }
    5521                 :            : 
    5522                 :          3 : long sched_getaffinity(pid_t pid, struct cpumask *mask)
    5523                 :            : {
    5524                 :            :         struct task_struct *p;
    5525                 :            :         unsigned long flags;
    5526                 :            :         int retval;
    5527                 :            : 
    5528                 :            :         rcu_read_lock();
    5529                 :            : 
    5530                 :            :         retval = -ESRCH;
    5531                 :            :         p = find_process_by_pid(pid);
    5532                 :          3 :         if (!p)
    5533                 :            :                 goto out_unlock;
    5534                 :            : 
    5535                 :          3 :         retval = security_task_getscheduler(p);
    5536                 :          3 :         if (retval)
    5537                 :            :                 goto out_unlock;
    5538                 :            : 
    5539                 :          3 :         raw_spin_lock_irqsave(&p->pi_lock, flags);
    5540                 :            :         cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
    5541                 :          3 :         raw_spin_unlock_irqrestore(&p->pi_lock, flags);
    5542                 :            : 
    5543                 :            : out_unlock:
    5544                 :            :         rcu_read_unlock();
    5545                 :            : 
    5546                 :          3 :         return retval;
    5547                 :            : }
    5548                 :            : 
    5549                 :            : /**
    5550                 :            :  * sys_sched_getaffinity - get the CPU affinity of a process
    5551                 :            :  * @pid: pid of the process
    5552                 :            :  * @len: length in bytes of the bitmask pointed to by user_mask_ptr
    5553                 :            :  * @user_mask_ptr: user-space pointer to hold the current CPU mask
    5554                 :            :  *
    5555                 :            :  * Return: size of CPU mask copied to user_mask_ptr on success. An
    5556                 :            :  * error code otherwise.
    5557                 :            :  */
    5558                 :          3 : SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
    5559                 :            :                 unsigned long __user *, user_mask_ptr)
    5560                 :            : {
    5561                 :            :         int ret;
    5562                 :            :         cpumask_var_t mask;
    5563                 :            : 
    5564                 :          3 :         if ((len * BITS_PER_BYTE) < nr_cpu_ids)
    5565                 :            :                 return -EINVAL;
    5566                 :          3 :         if (len & (sizeof(unsigned long)-1))
    5567                 :            :                 return -EINVAL;
    5568                 :            : 
    5569                 :            :         if (!alloc_cpumask_var(&mask, GFP_KERNEL))
    5570                 :            :                 return -ENOMEM;
    5571                 :            : 
    5572                 :          3 :         ret = sched_getaffinity(pid, mask);
    5573                 :          3 :         if (ret == 0) {
    5574                 :          3 :                 unsigned int retlen = min(len, cpumask_size());
    5575                 :            : 
    5576                 :          3 :                 if (copy_to_user(user_mask_ptr, mask, retlen))
    5577                 :            :                         ret = -EFAULT;
    5578                 :            :                 else
    5579                 :          3 :                         ret = retlen;
    5580                 :            :         }
    5581                 :            :         free_cpumask_var(mask);
    5582                 :            : 
    5583                 :          3 :         return ret;
    5584                 :            : }
    5585                 :            : 
    5586                 :            : /**
    5587                 :            :  * sys_sched_yield - yield the current processor to other threads.
    5588                 :            :  *
    5589                 :            :  * This function yields the current CPU to other tasks. If there are no
    5590                 :            :  * other threads running on this CPU then this function will return.
    5591                 :            :  *
    5592                 :            :  * Return: 0.
    5593                 :            :  */
    5594                 :          0 : static void do_sched_yield(void)
    5595                 :            : {
    5596                 :            :         struct rq_flags rf;
    5597                 :            :         struct rq *rq;
    5598                 :            : 
    5599                 :          0 :         rq = this_rq_lock_irq(&rf);
    5600                 :            : 
    5601                 :          0 :         schedstat_inc(rq->yld_count);
    5602                 :          0 :         current->sched_class->yield_task(rq);
    5603                 :            : 
    5604                 :            :         /*
    5605                 :            :          * Since we are going to call schedule() anyway, there's
    5606                 :            :          * no need to preempt or enable interrupts:
    5607                 :            :          */
    5608                 :          0 :         preempt_disable();
    5609                 :            :         rq_unlock(rq, &rf);
    5610                 :          0 :         sched_preempt_enable_no_resched();
    5611                 :            : 
    5612                 :          0 :         schedule();
    5613                 :          0 : }
    5614                 :            : 
    5615                 :          0 : SYSCALL_DEFINE0(sched_yield)
    5616                 :            : {
    5617                 :          0 :         do_sched_yield();
    5618                 :          0 :         return 0;
    5619                 :            : }
    5620                 :            : 
    5621                 :            : #ifndef CONFIG_PREEMPTION
    5622                 :          3 : int __sched _cond_resched(void)
    5623                 :            : {
    5624                 :          3 :         if (should_resched(0)) {
    5625                 :          3 :                 preempt_schedule_common();
    5626                 :          3 :                 return 1;
    5627                 :            :         }
    5628                 :          3 :         rcu_all_qs();
    5629                 :          3 :         return 0;
    5630                 :            : }
    5631                 :            : EXPORT_SYMBOL(_cond_resched);
    5632                 :            : #endif
    5633                 :            : 
    5634                 :            : /*
    5635                 :            :  * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
    5636                 :            :  * call schedule, and on return reacquire the lock.
    5637                 :            :  *
    5638                 :            :  * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level
    5639                 :            :  * operations here to prevent schedule() from being called twice (once via
    5640                 :            :  * spin_unlock(), once by hand).
    5641                 :            :  */
    5642                 :          3 : int __cond_resched_lock(spinlock_t *lock)
    5643                 :            : {
    5644                 :          3 :         int resched = should_resched(PREEMPT_LOCK_OFFSET);
    5645                 :            :         int ret = 0;
    5646                 :            : 
    5647                 :            :         lockdep_assert_held(lock);
    5648                 :            : 
    5649                 :          3 :         if (spin_needbreak(lock) || resched) {
    5650                 :            :                 spin_unlock(lock);
    5651                 :          3 :                 if (resched)
    5652                 :          3 :                         preempt_schedule_common();
    5653                 :            :                 else
    5654                 :          0 :                         cpu_relax();
    5655                 :            :                 ret = 1;
    5656                 :            :                 spin_lock(lock);
    5657                 :            :         }
    5658                 :          3 :         return ret;
    5659                 :            : }
    5660                 :            : EXPORT_SYMBOL(__cond_resched_lock);
    5661                 :            : 
    5662                 :            : /**
    5663                 :            :  * yield - yield the current processor to other threads.
    5664                 :            :  *
    5665                 :            :  * Do not ever use this function, there's a 99% chance you're doing it wrong.
    5666                 :            :  *
    5667                 :            :  * The scheduler is at all times free to pick the calling task as the most
    5668                 :            :  * eligible task to run, if removing the yield() call from your code breaks
    5669                 :            :  * it, its already broken.
    5670                 :            :  *
    5671                 :            :  * Typical broken usage is:
    5672                 :            :  *
    5673                 :            :  * while (!event)
    5674                 :            :  *      yield();
    5675                 :            :  *
    5676                 :            :  * where one assumes that yield() will let 'the other' process run that will
    5677                 :            :  * make event true. If the current task is a SCHED_FIFO task that will never
    5678                 :            :  * happen. Never use yield() as a progress guarantee!!
    5679                 :            :  *
    5680                 :            :  * If you want to use yield() to wait for something, use wait_event().
    5681                 :            :  * If you want to use yield() to be 'nice' for others, use cond_resched().
    5682                 :            :  * If you still want to use yield(), do not!
    5683                 :            :  */
    5684                 :          0 : void __sched yield(void)
    5685                 :            : {
    5686                 :          0 :         set_current_state(TASK_RUNNING);
    5687                 :          0 :         do_sched_yield();
    5688                 :          0 : }
    5689                 :            : EXPORT_SYMBOL(yield);
    5690                 :            : 
    5691                 :            : /**
    5692                 :            :  * yield_to - yield the current processor to another thread in
    5693                 :            :  * your thread group, or accelerate that thread toward the
    5694                 :            :  * processor it's on.
    5695                 :            :  * @p: target task
    5696                 :            :  * @preempt: whether task preemption is allowed or not
    5697                 :            :  *
    5698                 :            :  * It's the caller's job to ensure that the target task struct
    5699                 :            :  * can't go away on us before we can do any checks.
    5700                 :            :  *
    5701                 :            :  * Return:
    5702                 :            :  *      true (>0) if we indeed boosted the target task.
    5703                 :            :  *      false (0) if we failed to boost the target.
    5704                 :            :  *      -ESRCH if there's no task to yield to.
    5705                 :            :  */
    5706                 :          0 : int __sched yield_to(struct task_struct *p, bool preempt)
    5707                 :            : {
    5708                 :          0 :         struct task_struct *curr = current;
    5709                 :            :         struct rq *rq, *p_rq;
    5710                 :            :         unsigned long flags;
    5711                 :            :         int yielded = 0;
    5712                 :            : 
    5713                 :          0 :         local_irq_save(flags);
    5714                 :          0 :         rq = this_rq();
    5715                 :            : 
    5716                 :            : again:
    5717                 :          0 :         p_rq = task_rq(p);
    5718                 :            :         /*
    5719                 :            :          * If we're the only runnable task on the rq and target rq also
    5720                 :            :          * has only one task, there's absolutely no point in yielding.
    5721                 :            :          */
    5722                 :          0 :         if (rq->nr_running == 1 && p_rq->nr_running == 1) {
    5723                 :            :                 yielded = -ESRCH;
    5724                 :            :                 goto out_irq;
    5725                 :            :         }
    5726                 :            : 
    5727                 :          0 :         double_rq_lock(rq, p_rq);
    5728                 :          0 :         if (task_rq(p) != p_rq) {
    5729                 :          0 :                 double_rq_unlock(rq, p_rq);
    5730                 :          0 :                 goto again;
    5731                 :            :         }
    5732                 :            : 
    5733                 :          0 :         if (!curr->sched_class->yield_to_task)
    5734                 :            :                 goto out_unlock;
    5735                 :            : 
    5736                 :          0 :         if (curr->sched_class != p->sched_class)
    5737                 :            :                 goto out_unlock;
    5738                 :            : 
    5739                 :          0 :         if (task_running(p_rq, p) || p->state)
    5740                 :            :                 goto out_unlock;
    5741                 :            : 
    5742                 :          0 :         yielded = curr->sched_class->yield_to_task(rq, p, preempt);
    5743                 :          0 :         if (yielded) {
    5744                 :          0 :                 schedstat_inc(rq->yld_count);
    5745                 :            :                 /*
    5746                 :            :                  * Make p's CPU reschedule; pick_next_entity takes care of
    5747                 :            :                  * fairness.
    5748                 :            :                  */
    5749                 :          0 :                 if (preempt && rq != p_rq)
    5750                 :          0 :                         resched_curr(p_rq);
    5751                 :            :         }
    5752                 :            : 
    5753                 :            : out_unlock:
    5754                 :          0 :         double_rq_unlock(rq, p_rq);
    5755                 :            : out_irq:
    5756                 :          0 :         local_irq_restore(flags);
    5757                 :            : 
    5758                 :          0 :         if (yielded > 0)
    5759                 :          0 :                 schedule();
    5760                 :            : 
    5761                 :          0 :         return yielded;
    5762                 :            : }
    5763                 :            : EXPORT_SYMBOL_GPL(yield_to);
    5764                 :            : 
    5765                 :          3 : int io_schedule_prepare(void)
    5766                 :            : {
    5767                 :          3 :         int old_iowait = current->in_iowait;
    5768                 :            : 
    5769                 :          3 :         current->in_iowait = 1;
    5770                 :          3 :         blk_schedule_flush_plug(current);
    5771                 :            : 
    5772                 :          3 :         return old_iowait;
    5773                 :            : }
    5774                 :            : 
    5775                 :          3 : void io_schedule_finish(int token)
    5776                 :            : {
    5777                 :          3 :         current->in_iowait = token;
    5778                 :          3 : }
    5779                 :            : 
    5780                 :            : /*
    5781                 :            :  * This task is about to go to sleep on IO. Increment rq->nr_iowait so
    5782                 :            :  * that process accounting knows that this is a task in IO wait state.
    5783                 :            :  */
    5784                 :          0 : long __sched io_schedule_timeout(long timeout)
    5785                 :            : {
    5786                 :            :         int token;
    5787                 :            :         long ret;
    5788                 :            : 
    5789                 :          0 :         token = io_schedule_prepare();
    5790                 :          0 :         ret = schedule_timeout(timeout);
    5791                 :            :         io_schedule_finish(token);
    5792                 :            : 
    5793                 :          0 :         return ret;
    5794                 :            : }
    5795                 :            : EXPORT_SYMBOL(io_schedule_timeout);
    5796                 :            : 
    5797                 :          3 : void __sched io_schedule(void)
    5798                 :            : {
    5799                 :            :         int token;
    5800                 :            : 
    5801                 :          3 :         token = io_schedule_prepare();
    5802                 :          3 :         schedule();
    5803                 :            :         io_schedule_finish(token);
    5804                 :          3 : }
    5805                 :            : EXPORT_SYMBOL(io_schedule);
    5806                 :            : 
    5807                 :            : /**
    5808                 :            :  * sys_sched_get_priority_max - return maximum RT priority.
    5809                 :            :  * @policy: scheduling class.
    5810                 :            :  *
    5811                 :            :  * Return: On success, this syscall returns the maximum
    5812                 :            :  * rt_priority that can be used by a given scheduling class.
    5813                 :            :  * On failure, a negative error code is returned.
    5814                 :            :  */
    5815                 :          3 : SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
    5816                 :            : {
    5817                 :            :         int ret = -EINVAL;
    5818                 :            : 
    5819                 :          3 :         switch (policy) {
    5820                 :            :         case SCHED_FIFO:
    5821                 :            :         case SCHED_RR:
    5822                 :            :                 ret = MAX_USER_RT_PRIO-1;
    5823                 :            :                 break;
    5824                 :            :         case SCHED_DEADLINE:
    5825                 :            :         case SCHED_NORMAL:
    5826                 :            :         case SCHED_BATCH:
    5827                 :            :         case SCHED_IDLE:
    5828                 :            :                 ret = 0;
    5829                 :            :                 break;
    5830                 :            :         }
    5831                 :            :         return ret;
    5832                 :            : }
    5833                 :            : 
    5834                 :            : /**
    5835                 :            :  * sys_sched_get_priority_min - return minimum RT priority.
    5836                 :            :  * @policy: scheduling class.
    5837                 :            :  *
    5838                 :            :  * Return: On success, this syscall returns the minimum
    5839                 :            :  * rt_priority that can be used by a given scheduling class.
    5840                 :            :  * On failure, a negative error code is returned.
    5841                 :            :  */
    5842                 :          3 : SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
    5843                 :            : {
    5844                 :            :         int ret = -EINVAL;
    5845                 :            : 
    5846                 :          3 :         switch (policy) {
    5847                 :            :         case SCHED_FIFO:
    5848                 :            :         case SCHED_RR:
    5849                 :            :                 ret = 1;
    5850                 :            :                 break;
    5851                 :            :         case SCHED_DEADLINE:
    5852                 :            :         case SCHED_NORMAL:
    5853                 :            :         case SCHED_BATCH:
    5854                 :            :         case SCHED_IDLE:
    5855                 :            :                 ret = 0;
    5856                 :            :         }
    5857                 :            :         return ret;
    5858                 :            : }
    5859                 :            : 
    5860                 :          0 : static int sched_rr_get_interval(pid_t pid, struct timespec64 *t)
    5861                 :            : {
    5862                 :            :         struct task_struct *p;
    5863                 :            :         unsigned int time_slice;
    5864                 :            :         struct rq_flags rf;
    5865                 :            :         struct rq *rq;
    5866                 :            :         int retval;
    5867                 :            : 
    5868                 :          0 :         if (pid < 0)
    5869                 :            :                 return -EINVAL;
    5870                 :            : 
    5871                 :            :         retval = -ESRCH;
    5872                 :            :         rcu_read_lock();
    5873                 :            :         p = find_process_by_pid(pid);
    5874                 :          0 :         if (!p)
    5875                 :            :                 goto out_unlock;
    5876                 :            : 
    5877                 :          0 :         retval = security_task_getscheduler(p);
    5878                 :          0 :         if (retval)
    5879                 :            :                 goto out_unlock;
    5880                 :            : 
    5881                 :          0 :         rq = task_rq_lock(p, &rf);
    5882                 :            :         time_slice = 0;
    5883                 :          0 :         if (p->sched_class->get_rr_interval)
    5884                 :          0 :                 time_slice = p->sched_class->get_rr_interval(rq, p);
    5885                 :          0 :         task_rq_unlock(rq, p, &rf);
    5886                 :            : 
    5887                 :            :         rcu_read_unlock();
    5888                 :          0 :         jiffies_to_timespec64(time_slice, t);
    5889                 :          0 :         return 0;
    5890                 :            : 
    5891                 :            : out_unlock:
    5892                 :            :         rcu_read_unlock();
    5893                 :          0 :         return retval;
    5894                 :            : }
    5895                 :            : 
    5896                 :            : /**
    5897                 :            :  * sys_sched_rr_get_interval - return the default timeslice of a process.
    5898                 :            :  * @pid: pid of the process.
    5899                 :            :  * @interval: userspace pointer to the timeslice value.
    5900                 :            :  *
    5901                 :            :  * this syscall writes the default timeslice value of a given process
    5902                 :            :  * into the user-space timespec buffer. A value of '0' means infinity.
    5903                 :            :  *
    5904                 :            :  * Return: On success, 0 and the timeslice is in @interval. Otherwise,
    5905                 :            :  * an error code.
    5906                 :            :  */
    5907                 :          0 : SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
    5908                 :            :                 struct __kernel_timespec __user *, interval)
    5909                 :            : {
    5910                 :            :         struct timespec64 t;
    5911                 :          0 :         int retval = sched_rr_get_interval(pid, &t);
    5912                 :            : 
    5913                 :          0 :         if (retval == 0)
    5914                 :          0 :                 retval = put_timespec64(&t, interval);
    5915                 :            : 
    5916                 :          0 :         return retval;
    5917                 :            : }
    5918                 :            : 
    5919                 :            : #ifdef CONFIG_COMPAT_32BIT_TIME
    5920                 :          0 : SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid,
    5921                 :            :                 struct old_timespec32 __user *, interval)
    5922                 :            : {
    5923                 :            :         struct timespec64 t;
    5924                 :          0 :         int retval = sched_rr_get_interval(pid, &t);
    5925                 :            : 
    5926                 :          0 :         if (retval == 0)
    5927                 :          0 :                 retval = put_old_timespec32(&t, interval);
    5928                 :          0 :         return retval;
    5929                 :            : }
    5930                 :            : #endif
    5931                 :            : 
    5932                 :          0 : void sched_show_task(struct task_struct *p)
    5933                 :            : {
    5934                 :            :         unsigned long free = 0;
    5935                 :            :         int ppid;
    5936                 :            : 
    5937                 :          0 :         if (!try_get_task_stack(p))
    5938                 :          0 :                 return;
    5939                 :            : 
    5940                 :          0 :         printk(KERN_INFO "%-15.15s %c", p->comm, task_state_to_char(p));
    5941                 :            : 
    5942                 :          0 :         if (p->state == TASK_RUNNING)
    5943                 :          0 :                 printk(KERN_CONT "  running task    ");
    5944                 :            : #ifdef CONFIG_DEBUG_STACK_USAGE
    5945                 :            :         free = stack_not_used(p);
    5946                 :            : #endif
    5947                 :            :         ppid = 0;
    5948                 :            :         rcu_read_lock();
    5949                 :          0 :         if (pid_alive(p))
    5950                 :          0 :                 ppid = task_pid_nr(rcu_dereference(p->real_parent));
    5951                 :            :         rcu_read_unlock();
    5952                 :          0 :         printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
    5953                 :            :                 task_pid_nr(p), ppid,
    5954                 :          0 :                 (unsigned long)task_thread_info(p)->flags);
    5955                 :            : 
    5956                 :          0 :         print_worker_info(KERN_INFO, p);
    5957                 :          0 :         show_stack(p, NULL);
    5958                 :            :         put_task_stack(p);
    5959                 :            : }
    5960                 :            : EXPORT_SYMBOL_GPL(sched_show_task);
    5961                 :            : 
    5962                 :            : static inline bool
    5963                 :            : state_filter_match(unsigned long state_filter, struct task_struct *p)
    5964                 :            : {
    5965                 :            :         /* no filter, everything matches */
    5966                 :          0 :         if (!state_filter)
    5967                 :            :                 return true;
    5968                 :            : 
    5969                 :            :         /* filter, but doesn't match */
    5970                 :          0 :         if (!(p->state & state_filter))
    5971                 :            :                 return false;
    5972                 :            : 
    5973                 :            :         /*
    5974                 :            :          * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
    5975                 :            :          * TASK_KILLABLE).
    5976                 :            :          */
    5977                 :          0 :         if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE)
    5978                 :            :                 return false;
    5979                 :            : 
    5980                 :            :         return true;
    5981                 :            : }
    5982                 :            : 
    5983                 :            : 
    5984                 :          0 : void show_state_filter(unsigned long state_filter)
    5985                 :            : {
    5986                 :            :         struct task_struct *g, *p;
    5987                 :            : 
    5988                 :            : #if BITS_PER_LONG == 32
    5989                 :          0 :         printk(KERN_INFO
    5990                 :            :                 "  task                PC stack   pid father\n");
    5991                 :            : #else
    5992                 :            :         printk(KERN_INFO
    5993                 :            :                 "  task                        PC stack   pid father\n");
    5994                 :            : #endif
    5995                 :            :         rcu_read_lock();
    5996                 :          0 :         for_each_process_thread(g, p) {
    5997                 :            :                 /*
    5998                 :            :                  * reset the NMI-timeout, listing all files on a slow
    5999                 :            :                  * console might take a lot of time:
    6000                 :            :                  * Also, reset softlockup watchdogs on all CPUs, because
    6001                 :            :                  * another CPU might be blocked waiting for us to process
    6002                 :            :                  * an IPI.
    6003                 :            :                  */
    6004                 :            :                 touch_nmi_watchdog();
    6005                 :            :                 touch_all_softlockup_watchdogs();
    6006                 :          0 :                 if (state_filter_match(state_filter, p))
    6007                 :          0 :                         sched_show_task(p);
    6008                 :            :         }
    6009                 :            : 
    6010                 :            : #ifdef CONFIG_SCHED_DEBUG
    6011                 :          0 :         if (!state_filter)
    6012                 :          0 :                 sysrq_sched_debug_show();
    6013                 :            : #endif
    6014                 :            :         rcu_read_unlock();
    6015                 :            :         /*
    6016                 :            :          * Only show locks if all tasks are dumped:
    6017                 :            :          */
    6018                 :            :         if (!state_filter)
    6019                 :            :                 debug_show_all_locks();
    6020                 :          0 : }
    6021                 :            : 
    6022                 :            : /**
    6023                 :            :  * init_idle - set up an idle thread for a given CPU
    6024                 :            :  * @idle: task in question
    6025                 :            :  * @cpu: CPU the idle task belongs to
    6026                 :            :  *
    6027                 :            :  * NOTE: this function does not set the idle thread's NEED_RESCHED
    6028                 :            :  * flag, to make booting more robust.
    6029                 :            :  */
    6030                 :          3 : void init_idle(struct task_struct *idle, int cpu)
    6031                 :            : {
    6032                 :          3 :         struct rq *rq = cpu_rq(cpu);
    6033                 :            :         unsigned long flags;
    6034                 :            : 
    6035                 :          3 :         __sched_fork(0, idle);
    6036                 :            : 
    6037                 :          3 :         raw_spin_lock_irqsave(&idle->pi_lock, flags);
    6038                 :          3 :         raw_spin_lock(&rq->lock);
    6039                 :            : 
    6040                 :          3 :         idle->state = TASK_RUNNING;
    6041                 :          3 :         idle->se.exec_start = sched_clock();
    6042                 :          3 :         idle->flags |= PF_IDLE;
    6043                 :            : 
    6044                 :            :         kasan_unpoison_task_stack(idle);
    6045                 :            : 
    6046                 :            : #ifdef CONFIG_SMP
    6047                 :            :         /*
    6048                 :            :          * Its possible that init_idle() gets called multiple times on a task,
    6049                 :            :          * in that case do_set_cpus_allowed() will not do the right thing.
    6050                 :            :          *
    6051                 :            :          * And since this is boot we can forgo the serialization.
    6052                 :            :          */
    6053                 :          3 :         set_cpus_allowed_common(idle, cpumask_of(cpu));
    6054                 :            : #endif
    6055                 :            :         /*
    6056                 :            :          * We're having a chicken and egg problem, even though we are
    6057                 :            :          * holding rq->lock, the CPU isn't yet set to this CPU so the
    6058                 :            :          * lockdep check in task_group() will fail.
    6059                 :            :          *
    6060                 :            :          * Similar case to sched_fork(). / Alternatively we could
    6061                 :            :          * use task_rq_lock() here and obtain the other rq->lock.
    6062                 :            :          *
    6063                 :            :          * Silence PROVE_RCU
    6064                 :            :          */
    6065                 :            :         rcu_read_lock();
    6066                 :            :         __set_task_cpu(idle, cpu);
    6067                 :            :         rcu_read_unlock();
    6068                 :            : 
    6069                 :          3 :         rq->idle = idle;
    6070                 :          3 :         rcu_assign_pointer(rq->curr, idle);
    6071                 :          3 :         idle->on_rq = TASK_ON_RQ_QUEUED;
    6072                 :            : #ifdef CONFIG_SMP
    6073                 :          3 :         idle->on_cpu = 1;
    6074                 :            : #endif
    6075                 :            :         raw_spin_unlock(&rq->lock);
    6076                 :          3 :         raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
    6077                 :            : 
    6078                 :            :         /* Set the preempt count _outside_ the spinlocks! */
    6079                 :          3 :         init_idle_preempt_count(idle, cpu);
    6080                 :            : 
    6081                 :            :         /*
    6082                 :            :          * The idle tasks have their own, simple scheduling class:
    6083                 :            :          */
    6084                 :          3 :         idle->sched_class = &idle_sched_class;
    6085                 :          3 :         ftrace_graph_init_idle_task(idle, cpu);
    6086                 :            :         vtime_init_idle(idle, cpu);
    6087                 :            : #ifdef CONFIG_SMP
    6088                 :          3 :         sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
    6089                 :            : #endif
    6090                 :          3 : }
    6091                 :            : 
    6092                 :            : #ifdef CONFIG_SMP
    6093                 :            : 
    6094                 :          0 : int cpuset_cpumask_can_shrink(const struct cpumask *cur,
    6095                 :            :                               const struct cpumask *trial)
    6096                 :            : {
    6097                 :            :         int ret = 1;
    6098                 :            : 
    6099                 :          0 :         if (!cpumask_weight(cur))
    6100                 :            :                 return ret;
    6101                 :            : 
    6102                 :          0 :         ret = dl_cpuset_cpumask_can_shrink(cur, trial);
    6103                 :            : 
    6104                 :          0 :         return ret;
    6105                 :            : }
    6106                 :            : 
    6107                 :          0 : int task_can_attach(struct task_struct *p,
    6108                 :            :                     const struct cpumask *cs_cpus_allowed)
    6109                 :            : {
    6110                 :            :         int ret = 0;
    6111                 :            : 
    6112                 :            :         /*
    6113                 :            :          * Kthreads which disallow setaffinity shouldn't be moved
    6114                 :            :          * to a new cpuset; we don't want to change their CPU
    6115                 :            :          * affinity and isolating such threads by their set of
    6116                 :            :          * allowed nodes is unnecessary.  Thus, cpusets are not
    6117                 :            :          * applicable for such threads.  This prevents checking for
    6118                 :            :          * success of set_cpus_allowed_ptr() on all attached tasks
    6119                 :            :          * before cpus_mask may be changed.
    6120                 :            :          */
    6121                 :          0 :         if (p->flags & PF_NO_SETAFFINITY) {
    6122                 :            :                 ret = -EINVAL;
    6123                 :            :                 goto out;
    6124                 :            :         }
    6125                 :            : 
    6126                 :          0 :         if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
    6127                 :            :                                               cs_cpus_allowed))
    6128                 :          0 :                 ret = dl_task_can_attach(p, cs_cpus_allowed);
    6129                 :            : 
    6130                 :            : out:
    6131                 :          0 :         return ret;
    6132                 :            : }
    6133                 :            : 
    6134                 :            : bool sched_smp_initialized __read_mostly;
    6135                 :            : 
    6136                 :            : #ifdef CONFIG_NUMA_BALANCING
    6137                 :            : /* Migrate current task p to target_cpu */
    6138                 :            : int migrate_task_to(struct task_struct *p, int target_cpu)
    6139                 :            : {
    6140                 :            :         struct migration_arg arg = { p, target_cpu };
    6141                 :            :         int curr_cpu = task_cpu(p);
    6142                 :            : 
    6143                 :            :         if (curr_cpu == target_cpu)
    6144                 :            :                 return 0;
    6145                 :            : 
    6146                 :            :         if (!cpumask_test_cpu(target_cpu, p->cpus_ptr))
    6147                 :            :                 return -EINVAL;
    6148                 :            : 
    6149                 :            :         /* TODO: This is not properly updating schedstats */
    6150                 :            : 
    6151                 :            :         trace_sched_move_numa(p, curr_cpu, target_cpu);
    6152                 :            :         return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
    6153                 :            : }
    6154                 :            : 
    6155                 :            : /*
    6156                 :            :  * Requeue a task on a given node and accurately track the number of NUMA
    6157                 :            :  * tasks on the runqueues
    6158                 :            :  */
    6159                 :            : void sched_setnuma(struct task_struct *p, int nid)
    6160                 :            : {
    6161                 :            :         bool queued, running;
    6162                 :            :         struct rq_flags rf;
    6163                 :            :         struct rq *rq;
    6164                 :            : 
    6165                 :            :         rq = task_rq_lock(p, &rf);
    6166                 :            :         queued = task_on_rq_queued(p);
    6167                 :            :         running = task_current(rq, p);
    6168                 :            : 
    6169                 :            :         if (queued)
    6170                 :            :                 dequeue_task(rq, p, DEQUEUE_SAVE);
    6171                 :            :         if (running)
    6172                 :            :                 put_prev_task(rq, p);
    6173                 :            : 
    6174                 :            :         p->numa_preferred_nid = nid;
    6175                 :            : 
    6176                 :            :         if (queued)
    6177                 :            :                 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
    6178                 :            :         if (running)
    6179                 :            :                 set_next_task(rq, p);
    6180                 :            :         task_rq_unlock(rq, p, &rf);
    6181                 :            : }
    6182                 :            : #endif /* CONFIG_NUMA_BALANCING */
    6183                 :            : 
    6184                 :            : #ifdef CONFIG_HOTPLUG_CPU
    6185                 :            : /*
    6186                 :            :  * Ensure that the idle task is using init_mm right before its CPU goes
    6187                 :            :  * offline.
    6188                 :            :  */
    6189                 :            : void idle_task_exit(void)
    6190                 :            : {
    6191                 :            :         struct mm_struct *mm = current->active_mm;
    6192                 :            : 
    6193                 :            :         BUG_ON(cpu_online(smp_processor_id()));
    6194                 :            :         BUG_ON(current != this_rq()->idle);
    6195                 :            : 
    6196                 :            :         if (mm != &init_mm) {
    6197                 :            :                 switch_mm(mm, &init_mm, current);
    6198                 :            :                 finish_arch_post_lock_switch();
    6199                 :            :         }
    6200                 :            : 
    6201                 :            :         /* finish_cpu(), as ran on the BP, will clean up the active_mm state */
    6202                 :            : }
    6203                 :            : 
    6204                 :            : /*
    6205                 :            :  * Since this CPU is going 'away' for a while, fold any nr_active delta
    6206                 :            :  * we might have. Assumes we're called after migrate_tasks() so that the
    6207                 :            :  * nr_active count is stable. We need to take the teardown thread which
    6208                 :            :  * is calling this into account, so we hand in adjust = 1 to the load
    6209                 :            :  * calculation.
    6210                 :            :  *
    6211                 :            :  * Also see the comment "Global load-average calculations".
    6212                 :            :  */
    6213                 :            : static void calc_load_migrate(struct rq *rq)
    6214                 :            : {
    6215                 :            :         long delta = calc_load_fold_active(rq, 1);
    6216                 :            :         if (delta)
    6217                 :            :                 atomic_long_add(delta, &calc_load_tasks);
    6218                 :            : }
    6219                 :            : 
    6220                 :            : static struct task_struct *__pick_migrate_task(struct rq *rq)
    6221                 :            : {
    6222                 :            :         const struct sched_class *class;
    6223                 :            :         struct task_struct *next;
    6224                 :            : 
    6225                 :            :         for_each_class(class) {
    6226                 :            :                 next = class->pick_next_task(rq, NULL, NULL);
    6227                 :            :                 if (next) {
    6228                 :            :                         next->sched_class->put_prev_task(rq, next);
    6229                 :            :                         return next;
    6230                 :            :                 }
    6231                 :            :         }
    6232                 :            : 
    6233                 :            :         /* The idle class should always have a runnable task */
    6234                 :            :         BUG();
    6235                 :            : }
    6236                 :            : 
    6237                 :            : /*
    6238                 :            :  * Migrate all tasks from the rq, sleeping tasks will be migrated by
    6239                 :            :  * try_to_wake_up()->select_task_rq().
    6240                 :            :  *
    6241                 :            :  * Called with rq->lock held even though we'er in stop_machine() and
    6242                 :            :  * there's no concurrency possible, we hold the required locks anyway
    6243                 :            :  * because of lock validation efforts.
    6244                 :            :  */
    6245                 :            : static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf)
    6246                 :            : {
    6247                 :            :         struct rq *rq = dead_rq;
    6248                 :            :         struct task_struct *next, *stop = rq->stop;
    6249                 :            :         struct rq_flags orf = *rf;
    6250                 :            :         int dest_cpu;
    6251                 :            : 
    6252                 :            :         /*
    6253                 :            :          * Fudge the rq selection such that the below task selection loop
    6254                 :            :          * doesn't get stuck on the currently eligible stop task.
    6255                 :            :          *
    6256                 :            :          * We're currently inside stop_machine() and the rq is either stuck
    6257                 :            :          * in the stop_machine_cpu_stop() loop, or we're executing this code,
    6258                 :            :          * either way we should never end up calling schedule() until we're
    6259                 :            :          * done here.
    6260                 :            :          */
    6261                 :            :         rq->stop = NULL;
    6262                 :            : 
    6263                 :            :         /*
    6264                 :            :          * put_prev_task() and pick_next_task() sched
    6265                 :            :          * class method both need to have an up-to-date
    6266                 :            :          * value of rq->clock[_task]
    6267                 :            :          */
    6268                 :            :         update_rq_clock(rq);
    6269                 :            : 
    6270                 :            :         for (;;) {
    6271                 :            :                 /*
    6272                 :            :                  * There's this thread running, bail when that's the only
    6273                 :            :                  * remaining thread:
    6274                 :            :                  */
    6275                 :            :                 if (rq->nr_running == 1)
    6276                 :            :                         break;
    6277                 :            : 
    6278                 :            :                 next = __pick_migrate_task(rq);
    6279                 :            : 
    6280                 :            :                 /*
    6281                 :            :                  * Rules for changing task_struct::cpus_mask are holding
    6282                 :            :                  * both pi_lock and rq->lock, such that holding either
    6283                 :            :                  * stabilizes the mask.
    6284                 :            :                  *
    6285                 :            :                  * Drop rq->lock is not quite as disastrous as it usually is
    6286                 :            :                  * because !cpu_active at this point, which means load-balance
    6287                 :            :                  * will not interfere. Also, stop-machine.
    6288                 :            :                  */
    6289                 :            :                 rq_unlock(rq, rf);
    6290                 :            :                 raw_spin_lock(&next->pi_lock);
    6291                 :            :                 rq_relock(rq, rf);
    6292                 :            : 
    6293                 :            :                 /*
    6294                 :            :                  * Since we're inside stop-machine, _nothing_ should have
    6295                 :            :                  * changed the task, WARN if weird stuff happened, because in
    6296                 :            :                  * that case the above rq->lock drop is a fail too.
    6297                 :            :                  */
    6298                 :            :                 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
    6299                 :            :                         raw_spin_unlock(&next->pi_lock);
    6300                 :            :                         continue;
    6301                 :            :                 }
    6302                 :            : 
    6303                 :            :                 /* Find suitable destination for @next, with force if needed. */
    6304                 :            :                 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
    6305                 :            :                 rq = __migrate_task(rq, rf, next, dest_cpu);
    6306                 :            :                 if (rq != dead_rq) {
    6307                 :            :                         rq_unlock(rq, rf);
    6308                 :            :                         rq = dead_rq;
    6309                 :            :                         *rf = orf;
    6310                 :            :                         rq_relock(rq, rf);
    6311                 :            :                 }
    6312                 :            :                 raw_spin_unlock(&next->pi_lock);
    6313                 :            :         }
    6314                 :            : 
    6315                 :            :         rq->stop = stop;
    6316                 :            : }
    6317                 :            : #endif /* CONFIG_HOTPLUG_CPU */
    6318                 :            : 
    6319                 :          3 : void set_rq_online(struct rq *rq)
    6320                 :            : {
    6321                 :          3 :         if (!rq->online) {
    6322                 :            :                 const struct sched_class *class;
    6323                 :            : 
    6324                 :          3 :                 cpumask_set_cpu(rq->cpu, rq->rd->online);
    6325                 :          3 :                 rq->online = 1;
    6326                 :            : 
    6327                 :          3 :                 for_each_class(class) {
    6328                 :          3 :                         if (class->rq_online)
    6329                 :          3 :                                 class->rq_online(rq);
    6330                 :            :                 }
    6331                 :            :         }
    6332                 :          3 : }
    6333                 :            : 
    6334                 :          3 : void set_rq_offline(struct rq *rq)
    6335                 :            : {
    6336                 :          3 :         if (rq->online) {
    6337                 :            :                 const struct sched_class *class;
    6338                 :            : 
    6339                 :          3 :                 for_each_class(class) {
    6340                 :          3 :                         if (class->rq_offline)
    6341                 :          3 :                                 class->rq_offline(rq);
    6342                 :            :                 }
    6343                 :            : 
    6344                 :          3 :                 cpumask_clear_cpu(rq->cpu, rq->rd->online);
    6345                 :          3 :                 rq->online = 0;
    6346                 :            :         }
    6347                 :          3 : }
    6348                 :            : 
    6349                 :            : /*
    6350                 :            :  * used to mark begin/end of suspend/resume:
    6351                 :            :  */
    6352                 :            : static int num_cpus_frozen;
    6353                 :            : 
    6354                 :            : /*
    6355                 :            :  * Update cpusets according to cpu_active mask.  If cpusets are
    6356                 :            :  * disabled, cpuset_update_active_cpus() becomes a simple wrapper
    6357                 :            :  * around partition_sched_domains().
    6358                 :            :  *
    6359                 :            :  * If we come here as part of a suspend/resume, don't touch cpusets because we
    6360                 :            :  * want to restore it back to its original state upon resume anyway.
    6361                 :            :  */
    6362                 :          0 : static void cpuset_cpu_active(void)
    6363                 :            : {
    6364                 :          0 :         if (cpuhp_tasks_frozen) {
    6365                 :            :                 /*
    6366                 :            :                  * num_cpus_frozen tracks how many CPUs are involved in suspend
    6367                 :            :                  * resume sequence. As long as this is not the last online
    6368                 :            :                  * operation in the resume sequence, just build a single sched
    6369                 :            :                  * domain, ignoring cpusets.
    6370                 :            :                  */
    6371                 :          0 :                 partition_sched_domains(1, NULL, NULL);
    6372                 :          0 :                 if (--num_cpus_frozen)
    6373                 :          0 :                         return;
    6374                 :            :                 /*
    6375                 :            :                  * This is the last CPU online operation. So fall through and
    6376                 :            :                  * restore the original sched domains by considering the
    6377                 :            :                  * cpuset configurations.
    6378                 :            :                  */
    6379                 :          0 :                 cpuset_force_rebuild();
    6380                 :            :         }
    6381                 :          0 :         cpuset_update_active_cpus();
    6382                 :            : }
    6383                 :            : 
    6384                 :          0 : static int cpuset_cpu_inactive(unsigned int cpu)
    6385                 :            : {
    6386                 :          0 :         if (!cpuhp_tasks_frozen) {
    6387                 :          0 :                 if (dl_cpu_busy(cpu))
    6388                 :            :                         return -EBUSY;
    6389                 :          0 :                 cpuset_update_active_cpus();
    6390                 :            :         } else {
    6391                 :          0 :                 num_cpus_frozen++;
    6392                 :          0 :                 partition_sched_domains(1, NULL, NULL);
    6393                 :            :         }
    6394                 :            :         return 0;
    6395                 :            : }
    6396                 :            : 
    6397                 :          3 : int sched_cpu_activate(unsigned int cpu)
    6398                 :            : {
    6399                 :          3 :         struct rq *rq = cpu_rq(cpu);
    6400                 :            :         struct rq_flags rf;
    6401                 :            : 
    6402                 :            : #ifdef CONFIG_SCHED_SMT
    6403                 :            :         /*
    6404                 :            :          * When going up, increment the number of cores with SMT present.
    6405                 :            :          */
    6406                 :            :         if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
    6407                 :            :                 static_branch_inc_cpuslocked(&sched_smt_present);
    6408                 :            : #endif
    6409                 :          3 :         set_cpu_active(cpu, true);
    6410                 :            : 
    6411                 :          3 :         if (sched_smp_initialized) {
    6412                 :            :                 sched_domains_numa_masks_set(cpu);
    6413                 :          0 :                 cpuset_cpu_active();
    6414                 :            :         }
    6415                 :            : 
    6416                 :            :         /*
    6417                 :            :          * Put the rq online, if not already. This happens:
    6418                 :            :          *
    6419                 :            :          * 1) In the early boot process, because we build the real domains
    6420                 :            :          *    after all CPUs have been brought up.
    6421                 :            :          *
    6422                 :            :          * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
    6423                 :            :          *    domains.
    6424                 :            :          */
    6425                 :            :         rq_lock_irqsave(rq, &rf);
    6426                 :          3 :         if (rq->rd) {
    6427                 :          3 :                 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
    6428                 :          3 :                 set_rq_online(rq);
    6429                 :            :         }
    6430                 :            :         rq_unlock_irqrestore(rq, &rf);
    6431                 :            : 
    6432                 :          3 :         return 0;
    6433                 :            : }
    6434                 :            : 
    6435                 :          0 : int sched_cpu_deactivate(unsigned int cpu)
    6436                 :            : {
    6437                 :            :         int ret;
    6438                 :            : 
    6439                 :          0 :         set_cpu_active(cpu, false);
    6440                 :            :         /*
    6441                 :            :          * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
    6442                 :            :          * users of this state to go away such that all new such users will
    6443                 :            :          * observe it.
    6444                 :            :          *
    6445                 :            :          * Do sync before park smpboot threads to take care the rcu boost case.
    6446                 :            :          */
    6447                 :          0 :         synchronize_rcu();
    6448                 :            : 
    6449                 :            : #ifdef CONFIG_SCHED_SMT
    6450                 :            :         /*
    6451                 :            :          * When going down, decrement the number of cores with SMT present.
    6452                 :            :          */
    6453                 :            :         if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
    6454                 :            :                 static_branch_dec_cpuslocked(&sched_smt_present);
    6455                 :            : #endif
    6456                 :            : 
    6457                 :          0 :         if (!sched_smp_initialized)
    6458                 :            :                 return 0;
    6459                 :            : 
    6460                 :          0 :         ret = cpuset_cpu_inactive(cpu);
    6461                 :          0 :         if (ret) {
    6462                 :          0 :                 set_cpu_active(cpu, true);
    6463                 :          0 :                 return ret;
    6464                 :            :         }
    6465                 :            :         sched_domains_numa_masks_clear(cpu);
    6466                 :            :         return 0;
    6467                 :            : }
    6468                 :            : 
    6469                 :            : static void sched_rq_cpu_starting(unsigned int cpu)
    6470                 :            : {
    6471                 :          3 :         struct rq *rq = cpu_rq(cpu);
    6472                 :            : 
    6473                 :          3 :         rq->calc_load_update = calc_load_update;
    6474                 :          3 :         update_max_interval();
    6475                 :            : }
    6476                 :            : 
    6477                 :          3 : int sched_cpu_starting(unsigned int cpu)
    6478                 :            : {
    6479                 :            :         sched_rq_cpu_starting(cpu);
    6480                 :            :         sched_tick_start(cpu);
    6481                 :          3 :         return 0;
    6482                 :            : }
    6483                 :            : 
    6484                 :            : #ifdef CONFIG_HOTPLUG_CPU
    6485                 :            : int sched_cpu_dying(unsigned int cpu)
    6486                 :            : {
    6487                 :            :         struct rq *rq = cpu_rq(cpu);
    6488                 :            :         struct rq_flags rf;
    6489                 :            : 
    6490                 :            :         /* Handle pending wakeups and then migrate everything off */
    6491                 :            :         sched_ttwu_pending();
    6492                 :            :         sched_tick_stop(cpu);
    6493                 :            : 
    6494                 :            :         rq_lock_irqsave(rq, &rf);
    6495                 :            :         if (rq->rd) {
    6496                 :            :                 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
    6497                 :            :                 set_rq_offline(rq);
    6498                 :            :         }
    6499                 :            :         migrate_tasks(rq, &rf);
    6500                 :            :         BUG_ON(rq->nr_running != 1);
    6501                 :            :         rq_unlock_irqrestore(rq, &rf);
    6502                 :            : 
    6503                 :            :         calc_load_migrate(rq);
    6504                 :            :         update_max_interval();
    6505                 :            :         nohz_balance_exit_idle(rq);
    6506                 :            :         hrtick_clear(rq);
    6507                 :            :         return 0;
    6508                 :            : }
    6509                 :            : #endif
    6510                 :            : 
    6511                 :          3 : void __init sched_init_smp(void)
    6512                 :            : {
    6513                 :            :         sched_init_numa();
    6514                 :            : 
    6515                 :            :         /*
    6516                 :            :          * There's no userspace yet to cause hotplug operations; hence all the
    6517                 :            :          * CPU masks are stable and all blatant races in the below code cannot
    6518                 :            :          * happen.
    6519                 :            :          */
    6520                 :          3 :         mutex_lock(&sched_domains_mutex);
    6521                 :          3 :         sched_init_domains(cpu_active_mask);
    6522                 :          3 :         mutex_unlock(&sched_domains_mutex);
    6523                 :            : 
    6524                 :            :         /* Move init over to a non-isolated CPU */
    6525                 :          3 :         if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
    6526                 :          0 :                 BUG();
    6527                 :          3 :         sched_init_granularity();
    6528                 :            : 
    6529                 :          3 :         init_sched_rt_class();
    6530                 :          3 :         init_sched_dl_class();
    6531                 :            : 
    6532                 :          3 :         sched_smp_initialized = true;
    6533                 :          3 : }
    6534                 :            : 
    6535                 :          3 : static int __init migration_init(void)
    6536                 :            : {
    6537                 :          3 :         sched_cpu_starting(smp_processor_id());
    6538                 :          3 :         return 0;
    6539                 :            : }
    6540                 :            : early_initcall(migration_init);
    6541                 :            : 
    6542                 :            : #else
    6543                 :            : void __init sched_init_smp(void)
    6544                 :            : {
    6545                 :            :         sched_init_granularity();
    6546                 :            : }
    6547                 :            : #endif /* CONFIG_SMP */
    6548                 :            : 
    6549                 :          3 : int in_sched_functions(unsigned long addr)
    6550                 :            : {
    6551                 :          3 :         return in_lock_functions(addr) ||
    6552                 :          3 :                 (addr >= (unsigned long)__sched_text_start
    6553                 :          3 :                 && addr < (unsigned long)__sched_text_end);
    6554                 :            : }
    6555                 :            : 
    6556                 :            : #ifdef CONFIG_CGROUP_SCHED
    6557                 :            : /*
    6558                 :            :  * Default task group.
    6559                 :            :  * Every task in system belongs to this group at bootup.
    6560                 :            :  */
    6561                 :            : struct task_group root_task_group;
    6562                 :            : LIST_HEAD(task_groups);
    6563                 :            : 
    6564                 :            : /* Cacheline aligned slab cache for task_group */
    6565                 :            : static struct kmem_cache *task_group_cache __read_mostly;
    6566                 :            : #endif
    6567                 :            : 
    6568                 :            : DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
    6569                 :            : DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);
    6570                 :            : 
    6571                 :          3 : void __init sched_init(void)
    6572                 :            : {
    6573                 :            :         unsigned long ptr = 0;
    6574                 :            :         int i;
    6575                 :            : 
    6576                 :          3 :         wait_bit_init();
    6577                 :            : 
    6578                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    6579                 :          3 :         ptr += 2 * nr_cpu_ids * sizeof(void **);
    6580                 :            : #endif
    6581                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    6582                 :            :         ptr += 2 * nr_cpu_ids * sizeof(void **);
    6583                 :            : #endif
    6584                 :          3 :         if (ptr) {
    6585                 :          3 :                 ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT);
    6586                 :            : 
    6587                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    6588                 :          3 :                 root_task_group.se = (struct sched_entity **)ptr;
    6589                 :          3 :                 ptr += nr_cpu_ids * sizeof(void **);
    6590                 :            : 
    6591                 :          3 :                 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
    6592                 :            :                 ptr += nr_cpu_ids * sizeof(void **);
    6593                 :            : 
    6594                 :            : #endif /* CONFIG_FAIR_GROUP_SCHED */
    6595                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    6596                 :            :                 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
    6597                 :            :                 ptr += nr_cpu_ids * sizeof(void **);
    6598                 :            : 
    6599                 :            :                 root_task_group.rt_rq = (struct rt_rq **)ptr;
    6600                 :            :                 ptr += nr_cpu_ids * sizeof(void **);
    6601                 :            : 
    6602                 :            : #endif /* CONFIG_RT_GROUP_SCHED */
    6603                 :            :         }
    6604                 :            : #ifdef CONFIG_CPUMASK_OFFSTACK
    6605                 :            :         for_each_possible_cpu(i) {
    6606                 :            :                 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
    6607                 :            :                         cpumask_size(), GFP_KERNEL, cpu_to_node(i));
    6608                 :            :                 per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node(
    6609                 :            :                         cpumask_size(), GFP_KERNEL, cpu_to_node(i));
    6610                 :            :         }
    6611                 :            : #endif /* CONFIG_CPUMASK_OFFSTACK */
    6612                 :            : 
    6613                 :          3 :         init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime());
    6614                 :          3 :         init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime());
    6615                 :            : 
    6616                 :            : #ifdef CONFIG_SMP
    6617                 :          3 :         init_defrootdomain();
    6618                 :            : #endif
    6619                 :            : 
    6620                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    6621                 :            :         init_rt_bandwidth(&root_task_group.rt_bandwidth,
    6622                 :            :                         global_rt_period(), global_rt_runtime());
    6623                 :            : #endif /* CONFIG_RT_GROUP_SCHED */
    6624                 :            : 
    6625                 :            : #ifdef CONFIG_CGROUP_SCHED
    6626                 :          3 :         task_group_cache = KMEM_CACHE(task_group, 0);
    6627                 :            : 
    6628                 :            :         list_add(&root_task_group.list, &task_groups);
    6629                 :            :         INIT_LIST_HEAD(&root_task_group.children);
    6630                 :            :         INIT_LIST_HEAD(&root_task_group.siblings);
    6631                 :          3 :         autogroup_init(&init_task);
    6632                 :            : #endif /* CONFIG_CGROUP_SCHED */
    6633                 :            : 
    6634                 :          3 :         for_each_possible_cpu(i) {
    6635                 :            :                 struct rq *rq;
    6636                 :            : 
    6637                 :          3 :                 rq = cpu_rq(i);
    6638                 :          3 :                 raw_spin_lock_init(&rq->lock);
    6639                 :          3 :                 rq->nr_running = 0;
    6640                 :          3 :                 rq->calc_load_active = 0;
    6641                 :          3 :                 rq->calc_load_update = jiffies + LOAD_FREQ;
    6642                 :          3 :                 init_cfs_rq(&rq->cfs);
    6643                 :          3 :                 init_rt_rq(&rq->rt);
    6644                 :          3 :                 init_dl_rq(&rq->dl);
    6645                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    6646                 :          3 :                 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
    6647                 :          3 :                 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
    6648                 :          3 :                 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
    6649                 :            :                 /*
    6650                 :            :                  * How much CPU bandwidth does root_task_group get?
    6651                 :            :                  *
    6652                 :            :                  * In case of task-groups formed thr' the cgroup filesystem, it
    6653                 :            :                  * gets 100% of the CPU resources in the system. This overall
    6654                 :            :                  * system CPU resource is divided among the tasks of
    6655                 :            :                  * root_task_group and its child task-groups in a fair manner,
    6656                 :            :                  * based on each entity's (task or task-group's) weight
    6657                 :            :                  * (se->load.weight).
    6658                 :            :                  *
    6659                 :            :                  * In other words, if root_task_group has 10 tasks of weight
    6660                 :            :                  * 1024) and two child groups A0 and A1 (of weight 1024 each),
    6661                 :            :                  * then A0's share of the CPU resource is:
    6662                 :            :                  *
    6663                 :            :                  *      A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
    6664                 :            :                  *
    6665                 :            :                  * We achieve this by letting root_task_group's tasks sit
    6666                 :            :                  * directly in rq->cfs (i.e root_task_group->se[] = NULL).
    6667                 :            :                  */
    6668                 :          3 :                 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
    6669                 :          3 :                 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
    6670                 :            : #endif /* CONFIG_FAIR_GROUP_SCHED */
    6671                 :            : 
    6672                 :          3 :                 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
    6673                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    6674                 :            :                 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
    6675                 :            : #endif
    6676                 :            : #ifdef CONFIG_SMP
    6677                 :          3 :                 rq->sd = NULL;
    6678                 :          3 :                 rq->rd = NULL;
    6679                 :          3 :                 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
    6680                 :          3 :                 rq->balance_callback = NULL;
    6681                 :          3 :                 rq->active_balance = 0;
    6682                 :          3 :                 rq->next_balance = jiffies;
    6683                 :          3 :                 rq->push_cpu = 0;
    6684                 :          3 :                 rq->cpu = i;
    6685                 :          3 :                 rq->online = 0;
    6686                 :          3 :                 rq->idle_stamp = 0;
    6687                 :          3 :                 rq->avg_idle = 2*sysctl_sched_migration_cost;
    6688                 :          3 :                 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
    6689                 :            : 
    6690                 :          3 :                 INIT_LIST_HEAD(&rq->cfs_tasks);
    6691                 :            : 
    6692                 :          3 :                 rq_attach_root(rq, &def_root_domain);
    6693                 :            : #ifdef CONFIG_NO_HZ_COMMON
    6694                 :          3 :                 rq->last_load_update_tick = jiffies;
    6695                 :          3 :                 rq->last_blocked_load_update_tick = jiffies;
    6696                 :            :                 atomic_set(&rq->nohz_flags, 0);
    6697                 :            : #endif
    6698                 :            : #endif /* CONFIG_SMP */
    6699                 :            :                 hrtick_rq_init(rq);
    6700                 :            :                 atomic_set(&rq->nr_iowait, 0);
    6701                 :            :         }
    6702                 :            : 
    6703                 :          3 :         set_load_weight(&init_task, false);
    6704                 :            : 
    6705                 :            :         /*
    6706                 :            :          * The boot idle thread does lazy MMU switching as well:
    6707                 :            :          */
    6708                 :            :         mmgrab(&init_mm);
    6709                 :            :         enter_lazy_tlb(&init_mm, current);
    6710                 :            : 
    6711                 :            :         /*
    6712                 :            :          * Make us the idle thread. Technically, schedule() should not be
    6713                 :            :          * called from this thread, however somewhere below it might be,
    6714                 :            :          * but because we are the idle thread, we just pick up running again
    6715                 :            :          * when this runqueue becomes "idle".
    6716                 :            :          */
    6717                 :          3 :         init_idle(current, smp_processor_id());
    6718                 :            : 
    6719                 :          3 :         calc_load_update = jiffies + LOAD_FREQ;
    6720                 :            : 
    6721                 :            : #ifdef CONFIG_SMP
    6722                 :          3 :         idle_thread_set_boot_cpu();
    6723                 :            : #endif
    6724                 :          3 :         init_sched_fair_class();
    6725                 :            : 
    6726                 :          3 :         init_schedstats();
    6727                 :            : 
    6728                 :            :         psi_init();
    6729                 :            : 
    6730                 :            :         init_uclamp();
    6731                 :            : 
    6732                 :          3 :         scheduler_running = 1;
    6733                 :          3 : }
    6734                 :            : 
    6735                 :            : #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
    6736                 :            : static inline int preempt_count_equals(int preempt_offset)
    6737                 :            : {
    6738                 :            :         int nested = preempt_count() + rcu_preempt_depth();
    6739                 :            : 
    6740                 :            :         return (nested == preempt_offset);
    6741                 :            : }
    6742                 :            : 
    6743                 :            : void __might_sleep(const char *file, int line, int preempt_offset)
    6744                 :            : {
    6745                 :            :         /*
    6746                 :            :          * Blocking primitives will set (and therefore destroy) current->state,
    6747                 :            :          * since we will exit with TASK_RUNNING make sure we enter with it,
    6748                 :            :          * otherwise we will destroy state.
    6749                 :            :          */
    6750                 :            :         WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
    6751                 :            :                         "do not call blocking ops when !TASK_RUNNING; "
    6752                 :            :                         "state=%lx set at [<%p>] %pS\n",
    6753                 :            :                         current->state,
    6754                 :            :                         (void *)current->task_state_change,
    6755                 :            :                         (void *)current->task_state_change);
    6756                 :            : 
    6757                 :            :         ___might_sleep(file, line, preempt_offset);
    6758                 :            : }
    6759                 :            : EXPORT_SYMBOL(__might_sleep);
    6760                 :            : 
    6761                 :            : void ___might_sleep(const char *file, int line, int preempt_offset)
    6762                 :            : {
    6763                 :            :         /* Ratelimiting timestamp: */
    6764                 :            :         static unsigned long prev_jiffy;
    6765                 :            : 
    6766                 :            :         unsigned long preempt_disable_ip;
    6767                 :            : 
    6768                 :            :         /* WARN_ON_ONCE() by default, no rate limit required: */
    6769                 :            :         rcu_sleep_check();
    6770                 :            : 
    6771                 :            :         if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
    6772                 :            :              !is_idle_task(current) && !current->non_block_count) ||
    6773                 :            :             system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
    6774                 :            :             oops_in_progress)
    6775                 :            :                 return;
    6776                 :            : 
    6777                 :            :         if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
    6778                 :            :                 return;
    6779                 :            :         prev_jiffy = jiffies;
    6780                 :            : 
    6781                 :            :         /* Save this before calling printk(), since that will clobber it: */
    6782                 :            :         preempt_disable_ip = get_preempt_disable_ip(current);
    6783                 :            : 
    6784                 :            :         printk(KERN_ERR
    6785                 :            :                 "BUG: sleeping function called from invalid context at %s:%d\n",
    6786                 :            :                         file, line);
    6787                 :            :         printk(KERN_ERR
    6788                 :            :                 "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n",
    6789                 :            :                         in_atomic(), irqs_disabled(), current->non_block_count,
    6790                 :            :                         current->pid, current->comm);
    6791                 :            : 
    6792                 :            :         if (task_stack_end_corrupted(current))
    6793                 :            :                 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
    6794                 :            : 
    6795                 :            :         debug_show_held_locks(current);
    6796                 :            :         if (irqs_disabled())
    6797                 :            :                 print_irqtrace_events(current);
    6798                 :            :         if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
    6799                 :            :             && !preempt_count_equals(preempt_offset)) {
    6800                 :            :                 pr_err("Preemption disabled at:");
    6801                 :            :                 print_ip_sym(preempt_disable_ip);
    6802                 :            :                 pr_cont("\n");
    6803                 :            :         }
    6804                 :            :         dump_stack();
    6805                 :            :         add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
    6806                 :            : }
    6807                 :            : EXPORT_SYMBOL(___might_sleep);
    6808                 :            : 
    6809                 :            : void __cant_sleep(const char *file, int line, int preempt_offset)
    6810                 :            : {
    6811                 :            :         static unsigned long prev_jiffy;
    6812                 :            : 
    6813                 :            :         if (irqs_disabled())
    6814                 :            :                 return;
    6815                 :            : 
    6816                 :            :         if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
    6817                 :            :                 return;
    6818                 :            : 
    6819                 :            :         if (preempt_count() > preempt_offset)
    6820                 :            :                 return;
    6821                 :            : 
    6822                 :            :         if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
    6823                 :            :                 return;
    6824                 :            :         prev_jiffy = jiffies;
    6825                 :            : 
    6826                 :            :         printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line);
    6827                 :            :         printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
    6828                 :            :                         in_atomic(), irqs_disabled(),
    6829                 :            :                         current->pid, current->comm);
    6830                 :            : 
    6831                 :            :         debug_show_held_locks(current);
    6832                 :            :         dump_stack();
    6833                 :            :         add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
    6834                 :            : }
    6835                 :            : EXPORT_SYMBOL_GPL(__cant_sleep);
    6836                 :            : #endif
    6837                 :            : 
    6838                 :            : #ifdef CONFIG_MAGIC_SYSRQ
    6839                 :          0 : void normalize_rt_tasks(void)
    6840                 :            : {
    6841                 :            :         struct task_struct *g, *p;
    6842                 :          0 :         struct sched_attr attr = {
    6843                 :            :                 .sched_policy = SCHED_NORMAL,
    6844                 :            :         };
    6845                 :            : 
    6846                 :          0 :         read_lock(&tasklist_lock);
    6847                 :          0 :         for_each_process_thread(g, p) {
    6848                 :            :                 /*
    6849                 :            :                  * Only normalize user tasks:
    6850                 :            :                  */
    6851                 :          0 :                 if (p->flags & PF_KTHREAD)
    6852                 :          0 :                         continue;
    6853                 :            : 
    6854                 :          0 :                 p->se.exec_start = 0;
    6855                 :          0 :                 schedstat_set(p->se.statistics.wait_start,  0);
    6856                 :          0 :                 schedstat_set(p->se.statistics.sleep_start, 0);
    6857                 :          0 :                 schedstat_set(p->se.statistics.block_start, 0);
    6858                 :            : 
    6859                 :          0 :                 if (!dl_task(p) && !rt_task(p)) {
    6860                 :            :                         /*
    6861                 :            :                          * Renice negative nice level userspace
    6862                 :            :                          * tasks back to 0:
    6863                 :            :                          */
    6864                 :          0 :                         if (task_nice(p) < 0)
    6865                 :          0 :                                 set_user_nice(p, 0);
    6866                 :          0 :                         continue;
    6867                 :            :                 }
    6868                 :            : 
    6869                 :          0 :                 __sched_setscheduler(p, &attr, false, false);
    6870                 :            :         }
    6871                 :            :         read_unlock(&tasklist_lock);
    6872                 :          0 : }
    6873                 :            : 
    6874                 :            : #endif /* CONFIG_MAGIC_SYSRQ */
    6875                 :            : 
    6876                 :            : #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
    6877                 :            : /*
    6878                 :            :  * These functions are only useful for the IA64 MCA handling, or kdb.
    6879                 :            :  *
    6880                 :            :  * They can only be called when the whole system has been
    6881                 :            :  * stopped - every CPU needs to be quiescent, and no scheduling
    6882                 :            :  * activity can take place. Using them for anything else would
    6883                 :            :  * be a serious bug, and as a result, they aren't even visible
    6884                 :            :  * under any other configuration.
    6885                 :            :  */
    6886                 :            : 
    6887                 :            : /**
    6888                 :            :  * curr_task - return the current task for a given CPU.
    6889                 :            :  * @cpu: the processor in question.
    6890                 :            :  *
    6891                 :            :  * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
    6892                 :            :  *
    6893                 :            :  * Return: The current task for @cpu.
    6894                 :            :  */
    6895                 :          0 : struct task_struct *curr_task(int cpu)
    6896                 :            : {
    6897                 :          0 :         return cpu_curr(cpu);
    6898                 :            : }
    6899                 :            : 
    6900                 :            : #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
    6901                 :            : 
    6902                 :            : #ifdef CONFIG_IA64
    6903                 :            : /**
    6904                 :            :  * ia64_set_curr_task - set the current task for a given CPU.
    6905                 :            :  * @cpu: the processor in question.
    6906                 :            :  * @p: the task pointer to set.
    6907                 :            :  *
    6908                 :            :  * Description: This function must only be used when non-maskable interrupts
    6909                 :            :  * are serviced on a separate stack. It allows the architecture to switch the
    6910                 :            :  * notion of the current task on a CPU in a non-blocking manner. This function
    6911                 :            :  * must be called with all CPU's synchronized, and interrupts disabled, the
    6912                 :            :  * and caller must save the original value of the current task (see
    6913                 :            :  * curr_task() above) and restore that value before reenabling interrupts and
    6914                 :            :  * re-starting the system.
    6915                 :            :  *
    6916                 :            :  * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
    6917                 :            :  */
    6918                 :            : void ia64_set_curr_task(int cpu, struct task_struct *p)
    6919                 :            : {
    6920                 :            :         cpu_curr(cpu) = p;
    6921                 :            : }
    6922                 :            : 
    6923                 :            : #endif
    6924                 :            : 
    6925                 :            : #ifdef CONFIG_CGROUP_SCHED
    6926                 :            : /* task_group_lock serializes the addition/removal of task groups */
    6927                 :            : static DEFINE_SPINLOCK(task_group_lock);
    6928                 :            : 
    6929                 :            : static inline void alloc_uclamp_sched_group(struct task_group *tg,
    6930                 :            :                                             struct task_group *parent)
    6931                 :            : {
    6932                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    6933                 :            :         enum uclamp_id clamp_id;
    6934                 :            : 
    6935                 :            :         for_each_clamp_id(clamp_id) {
    6936                 :            :                 uclamp_se_set(&tg->uclamp_req[clamp_id],
    6937                 :            :                               uclamp_none(clamp_id), false);
    6938                 :            :                 tg->uclamp[clamp_id] = parent->uclamp[clamp_id];
    6939                 :            :         }
    6940                 :            : #endif
    6941                 :            : }
    6942                 :            : 
    6943                 :          3 : static void sched_free_group(struct task_group *tg)
    6944                 :            : {
    6945                 :          3 :         free_fair_sched_group(tg);
    6946                 :          3 :         free_rt_sched_group(tg);
    6947                 :          3 :         autogroup_free(tg);
    6948                 :          3 :         kmem_cache_free(task_group_cache, tg);
    6949                 :          3 : }
    6950                 :            : 
    6951                 :            : /* allocate runqueue etc for a new task group */
    6952                 :          3 : struct task_group *sched_create_group(struct task_group *parent)
    6953                 :            : {
    6954                 :            :         struct task_group *tg;
    6955                 :            : 
    6956                 :          3 :         tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
    6957                 :          3 :         if (!tg)
    6958                 :            :                 return ERR_PTR(-ENOMEM);
    6959                 :            : 
    6960                 :          3 :         if (!alloc_fair_sched_group(tg, parent))
    6961                 :            :                 goto err;
    6962                 :            : 
    6963                 :          3 :         if (!alloc_rt_sched_group(tg, parent))
    6964                 :            :                 goto err;
    6965                 :            : 
    6966                 :            :         alloc_uclamp_sched_group(tg, parent);
    6967                 :            : 
    6968                 :            :         return tg;
    6969                 :            : 
    6970                 :            : err:
    6971                 :          0 :         sched_free_group(tg);
    6972                 :          0 :         return ERR_PTR(-ENOMEM);
    6973                 :            : }
    6974                 :            : 
    6975                 :          3 : void sched_online_group(struct task_group *tg, struct task_group *parent)
    6976                 :            : {
    6977                 :            :         unsigned long flags;
    6978                 :            : 
    6979                 :          3 :         spin_lock_irqsave(&task_group_lock, flags);
    6980                 :          3 :         list_add_rcu(&tg->list, &task_groups);
    6981                 :            : 
    6982                 :            :         /* Root should already exist: */
    6983                 :          3 :         WARN_ON(!parent);
    6984                 :            : 
    6985                 :          3 :         tg->parent = parent;
    6986                 :          3 :         INIT_LIST_HEAD(&tg->children);
    6987                 :          3 :         list_add_rcu(&tg->siblings, &parent->children);
    6988                 :            :         spin_unlock_irqrestore(&task_group_lock, flags);
    6989                 :            : 
    6990                 :          3 :         online_fair_sched_group(tg);
    6991                 :          3 : }
    6992                 :            : 
    6993                 :            : /* rcu callback to free various structures associated with a task group */
    6994                 :          3 : static void sched_free_group_rcu(struct rcu_head *rhp)
    6995                 :            : {
    6996                 :            :         /* Now it should be safe to free those cfs_rqs: */
    6997                 :          3 :         sched_free_group(container_of(rhp, struct task_group, rcu));
    6998                 :          3 : }
    6999                 :            : 
    7000                 :          3 : void sched_destroy_group(struct task_group *tg)
    7001                 :            : {
    7002                 :            :         /* Wait for possible concurrent references to cfs_rqs complete: */
    7003                 :          3 :         call_rcu(&tg->rcu, sched_free_group_rcu);
    7004                 :          3 : }
    7005                 :            : 
    7006                 :          3 : void sched_offline_group(struct task_group *tg)
    7007                 :            : {
    7008                 :            :         unsigned long flags;
    7009                 :            : 
    7010                 :            :         /* End participation in shares distribution: */
    7011                 :          3 :         unregister_fair_sched_group(tg);
    7012                 :            : 
    7013                 :          3 :         spin_lock_irqsave(&task_group_lock, flags);
    7014                 :            :         list_del_rcu(&tg->list);
    7015                 :            :         list_del_rcu(&tg->siblings);
    7016                 :            :         spin_unlock_irqrestore(&task_group_lock, flags);
    7017                 :          3 : }
    7018                 :            : 
    7019                 :          3 : static void sched_change_group(struct task_struct *tsk, int type)
    7020                 :            : {
    7021                 :            :         struct task_group *tg;
    7022                 :            : 
    7023                 :            :         /*
    7024                 :            :          * All callers are synchronized by task_rq_lock(); we do not use RCU
    7025                 :            :          * which is pointless here. Thus, we pass "true" to task_css_check()
    7026                 :            :          * to prevent lockdep warnings.
    7027                 :            :          */
    7028                 :          3 :         tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
    7029                 :            :                           struct task_group, css);
    7030                 :          3 :         tg = autogroup_task_group(tsk, tg);
    7031                 :          3 :         tsk->sched_task_group = tg;
    7032                 :            : 
    7033                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    7034                 :          3 :         if (tsk->sched_class->task_change_group)
    7035                 :          3 :                 tsk->sched_class->task_change_group(tsk, type);
    7036                 :            :         else
    7037                 :            : #endif
    7038                 :          0 :                 set_task_rq(tsk, task_cpu(tsk));
    7039                 :          3 : }
    7040                 :            : 
    7041                 :            : /*
    7042                 :            :  * Change task's runqueue when it moves between groups.
    7043                 :            :  *
    7044                 :            :  * The caller of this function should have put the task in its new group by
    7045                 :            :  * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
    7046                 :            :  * its new group.
    7047                 :            :  */
    7048                 :          3 : void sched_move_task(struct task_struct *tsk)
    7049                 :            : {
    7050                 :            :         int queued, running, queue_flags =
    7051                 :            :                 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
    7052                 :            :         struct rq_flags rf;
    7053                 :            :         struct rq *rq;
    7054                 :            : 
    7055                 :          3 :         rq = task_rq_lock(tsk, &rf);
    7056                 :          3 :         update_rq_clock(rq);
    7057                 :            : 
    7058                 :            :         running = task_current(rq, tsk);
    7059                 :            :         queued = task_on_rq_queued(tsk);
    7060                 :            : 
    7061                 :          3 :         if (queued)
    7062                 :          3 :                 dequeue_task(rq, tsk, queue_flags);
    7063                 :          3 :         if (running)
    7064                 :          3 :                 put_prev_task(rq, tsk);
    7065                 :            : 
    7066                 :          3 :         sched_change_group(tsk, TASK_MOVE_GROUP);
    7067                 :            : 
    7068                 :          3 :         if (queued)
    7069                 :          3 :                 enqueue_task(rq, tsk, queue_flags);
    7070                 :          3 :         if (running) {
    7071                 :          3 :                 set_next_task(rq, tsk);
    7072                 :            :                 /*
    7073                 :            :                  * After changing group, the running task may have joined a
    7074                 :            :                  * throttled one but it's still the running task. Trigger a
    7075                 :            :                  * resched to make sure that task can still run.
    7076                 :            :                  */
    7077                 :          3 :                 resched_curr(rq);
    7078                 :            :         }
    7079                 :            : 
    7080                 :          3 :         task_rq_unlock(rq, tsk, &rf);
    7081                 :          3 : }
    7082                 :            : 
    7083                 :            : static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
    7084                 :            : {
    7085                 :          3 :         return css ? container_of(css, struct task_group, css) : NULL;
    7086                 :            : }
    7087                 :            : 
    7088                 :            : static struct cgroup_subsys_state *
    7089                 :          3 : cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
    7090                 :            : {
    7091                 :            :         struct task_group *parent = css_tg(parent_css);
    7092                 :            :         struct task_group *tg;
    7093                 :            : 
    7094                 :          3 :         if (!parent) {
    7095                 :            :                 /* This is early initialization for the top cgroup */
    7096                 :            :                 return &root_task_group.css;
    7097                 :            :         }
    7098                 :            : 
    7099                 :          0 :         tg = sched_create_group(parent);
    7100                 :          0 :         if (IS_ERR(tg))
    7101                 :            :                 return ERR_PTR(-ENOMEM);
    7102                 :            : 
    7103                 :          0 :         return &tg->css;
    7104                 :            : }
    7105                 :            : 
    7106                 :            : /* Expose task group only after completing cgroup initialization */
    7107                 :          3 : static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
    7108                 :            : {
    7109                 :            :         struct task_group *tg = css_tg(css);
    7110                 :          3 :         struct task_group *parent = css_tg(css->parent);
    7111                 :            : 
    7112                 :          3 :         if (parent)
    7113                 :          0 :                 sched_online_group(tg, parent);
    7114                 :            : 
    7115                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    7116                 :            :         /* Propagate the effective uclamp value for the new group */
    7117                 :            :         cpu_util_update_eff(css);
    7118                 :            : #endif
    7119                 :            : 
    7120                 :          3 :         return 0;
    7121                 :            : }
    7122                 :            : 
    7123                 :          0 : static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
    7124                 :            : {
    7125                 :            :         struct task_group *tg = css_tg(css);
    7126                 :            : 
    7127                 :          0 :         sched_offline_group(tg);
    7128                 :          0 : }
    7129                 :            : 
    7130                 :          0 : static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
    7131                 :            : {
    7132                 :            :         struct task_group *tg = css_tg(css);
    7133                 :            : 
    7134                 :            :         /*
    7135                 :            :          * Relies on the RCU grace period between css_released() and this.
    7136                 :            :          */
    7137                 :          0 :         sched_free_group(tg);
    7138                 :          0 : }
    7139                 :            : 
    7140                 :            : /*
    7141                 :            :  * This is called before wake_up_new_task(), therefore we really only
    7142                 :            :  * have to set its group bits, all the other stuff does not apply.
    7143                 :            :  */
    7144                 :          3 : static void cpu_cgroup_fork(struct task_struct *task)
    7145                 :            : {
    7146                 :            :         struct rq_flags rf;
    7147                 :            :         struct rq *rq;
    7148                 :            : 
    7149                 :          3 :         rq = task_rq_lock(task, &rf);
    7150                 :            : 
    7151                 :          3 :         update_rq_clock(rq);
    7152                 :          3 :         sched_change_group(task, TASK_SET_GROUP);
    7153                 :            : 
    7154                 :          3 :         task_rq_unlock(rq, task, &rf);
    7155                 :          3 : }
    7156                 :            : 
    7157                 :          0 : static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
    7158                 :            : {
    7159                 :            :         struct task_struct *task;
    7160                 :            :         struct cgroup_subsys_state *css;
    7161                 :            :         int ret = 0;
    7162                 :            : 
    7163                 :          0 :         cgroup_taskset_for_each(task, css, tset) {
    7164                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    7165                 :            :                 if (!sched_rt_can_attach(css_tg(css), task))
    7166                 :            :                         return -EINVAL;
    7167                 :            : #endif
    7168                 :            :                 /*
    7169                 :            :                  * Serialize against wake_up_new_task() such that if its
    7170                 :            :                  * running, we're sure to observe its full state.
    7171                 :            :                  */
    7172                 :          0 :                 raw_spin_lock_irq(&task->pi_lock);
    7173                 :            :                 /*
    7174                 :            :                  * Avoid calling sched_move_task() before wake_up_new_task()
    7175                 :            :                  * has happened. This would lead to problems with PELT, due to
    7176                 :            :                  * move wanting to detach+attach while we're not attached yet.
    7177                 :            :                  */
    7178                 :          0 :                 if (task->state == TASK_NEW)
    7179                 :            :                         ret = -EINVAL;
    7180                 :          0 :                 raw_spin_unlock_irq(&task->pi_lock);
    7181                 :            : 
    7182                 :          0 :                 if (ret)
    7183                 :            :                         break;
    7184                 :            :         }
    7185                 :          0 :         return ret;
    7186                 :            : }
    7187                 :            : 
    7188                 :          0 : static void cpu_cgroup_attach(struct cgroup_taskset *tset)
    7189                 :            : {
    7190                 :            :         struct task_struct *task;
    7191                 :            :         struct cgroup_subsys_state *css;
    7192                 :            : 
    7193                 :          0 :         cgroup_taskset_for_each(task, css, tset)
    7194                 :          0 :                 sched_move_task(task);
    7195                 :          0 : }
    7196                 :            : 
    7197                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    7198                 :            : static void cpu_util_update_eff(struct cgroup_subsys_state *css)
    7199                 :            : {
    7200                 :            :         struct cgroup_subsys_state *top_css = css;
    7201                 :            :         struct uclamp_se *uc_parent = NULL;
    7202                 :            :         struct uclamp_se *uc_se = NULL;
    7203                 :            :         unsigned int eff[UCLAMP_CNT];
    7204                 :            :         enum uclamp_id clamp_id;
    7205                 :            :         unsigned int clamps;
    7206                 :            : 
    7207                 :            :         css_for_each_descendant_pre(css, top_css) {
    7208                 :            :                 uc_parent = css_tg(css)->parent
    7209                 :            :                         ? css_tg(css)->parent->uclamp : NULL;
    7210                 :            : 
    7211                 :            :                 for_each_clamp_id(clamp_id) {
    7212                 :            :                         /* Assume effective clamps matches requested clamps */
    7213                 :            :                         eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value;
    7214                 :            :                         /* Cap effective clamps with parent's effective clamps */
    7215                 :            :                         if (uc_parent &&
    7216                 :            :                             eff[clamp_id] > uc_parent[clamp_id].value) {
    7217                 :            :                                 eff[clamp_id] = uc_parent[clamp_id].value;
    7218                 :            :                         }
    7219                 :            :                 }
    7220                 :            :                 /* Ensure protection is always capped by limit */
    7221                 :            :                 eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]);
    7222                 :            : 
    7223                 :            :                 /* Propagate most restrictive effective clamps */
    7224                 :            :                 clamps = 0x0;
    7225                 :            :                 uc_se = css_tg(css)->uclamp;
    7226                 :            :                 for_each_clamp_id(clamp_id) {
    7227                 :            :                         if (eff[clamp_id] == uc_se[clamp_id].value)
    7228                 :            :                                 continue;
    7229                 :            :                         uc_se[clamp_id].value = eff[clamp_id];
    7230                 :            :                         uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]);
    7231                 :            :                         clamps |= (0x1 << clamp_id);
    7232                 :            :                 }
    7233                 :            :                 if (!clamps) {
    7234                 :            :                         css = css_rightmost_descendant(css);
    7235                 :            :                         continue;
    7236                 :            :                 }
    7237                 :            : 
    7238                 :            :                 /* Immediately update descendants RUNNABLE tasks */
    7239                 :            :                 uclamp_update_active_tasks(css, clamps);
    7240                 :            :         }
    7241                 :            : }
    7242                 :            : 
    7243                 :            : /*
    7244                 :            :  * Integer 10^N with a given N exponent by casting to integer the literal "1eN"
    7245                 :            :  * C expression. Since there is no way to convert a macro argument (N) into a
    7246                 :            :  * character constant, use two levels of macros.
    7247                 :            :  */
    7248                 :            : #define _POW10(exp) ((unsigned int)1e##exp)
    7249                 :            : #define POW10(exp) _POW10(exp)
    7250                 :            : 
    7251                 :            : struct uclamp_request {
    7252                 :            : #define UCLAMP_PERCENT_SHIFT    2
    7253                 :            : #define UCLAMP_PERCENT_SCALE    (100 * POW10(UCLAMP_PERCENT_SHIFT))
    7254                 :            :         s64 percent;
    7255                 :            :         u64 util;
    7256                 :            :         int ret;
    7257                 :            : };
    7258                 :            : 
    7259                 :            : static inline struct uclamp_request
    7260                 :            : capacity_from_percent(char *buf)
    7261                 :            : {
    7262                 :            :         struct uclamp_request req = {
    7263                 :            :                 .percent = UCLAMP_PERCENT_SCALE,
    7264                 :            :                 .util = SCHED_CAPACITY_SCALE,
    7265                 :            :                 .ret = 0,
    7266                 :            :         };
    7267                 :            : 
    7268                 :            :         buf = strim(buf);
    7269                 :            :         if (strcmp(buf, "max")) {
    7270                 :            :                 req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT,
    7271                 :            :                                              &req.percent);
    7272                 :            :                 if (req.ret)
    7273                 :            :                         return req;
    7274                 :            :                 if ((u64)req.percent > UCLAMP_PERCENT_SCALE) {
    7275                 :            :                         req.ret = -ERANGE;
    7276                 :            :                         return req;
    7277                 :            :                 }
    7278                 :            : 
    7279                 :            :                 req.util = req.percent << SCHED_CAPACITY_SHIFT;
    7280                 :            :                 req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE);
    7281                 :            :         }
    7282                 :            : 
    7283                 :            :         return req;
    7284                 :            : }
    7285                 :            : 
    7286                 :            : static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
    7287                 :            :                                 size_t nbytes, loff_t off,
    7288                 :            :                                 enum uclamp_id clamp_id)
    7289                 :            : {
    7290                 :            :         struct uclamp_request req;
    7291                 :            :         struct task_group *tg;
    7292                 :            : 
    7293                 :            :         req = capacity_from_percent(buf);
    7294                 :            :         if (req.ret)
    7295                 :            :                 return req.ret;
    7296                 :            : 
    7297                 :            :         mutex_lock(&uclamp_mutex);
    7298                 :            :         rcu_read_lock();
    7299                 :            : 
    7300                 :            :         tg = css_tg(of_css(of));
    7301                 :            :         if (tg->uclamp_req[clamp_id].value != req.util)
    7302                 :            :                 uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false);
    7303                 :            : 
    7304                 :            :         /*
    7305                 :            :          * Because of not recoverable conversion rounding we keep track of the
    7306                 :            :          * exact requested value
    7307                 :            :          */
    7308                 :            :         tg->uclamp_pct[clamp_id] = req.percent;
    7309                 :            : 
    7310                 :            :         /* Update effective clamps to track the most restrictive value */
    7311                 :            :         cpu_util_update_eff(of_css(of));
    7312                 :            : 
    7313                 :            :         rcu_read_unlock();
    7314                 :            :         mutex_unlock(&uclamp_mutex);
    7315                 :            : 
    7316                 :            :         return nbytes;
    7317                 :            : }
    7318                 :            : 
    7319                 :            : static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of,
    7320                 :            :                                     char *buf, size_t nbytes,
    7321                 :            :                                     loff_t off)
    7322                 :            : {
    7323                 :            :         return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN);
    7324                 :            : }
    7325                 :            : 
    7326                 :            : static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of,
    7327                 :            :                                     char *buf, size_t nbytes,
    7328                 :            :                                     loff_t off)
    7329                 :            : {
    7330                 :            :         return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX);
    7331                 :            : }
    7332                 :            : 
    7333                 :            : static inline void cpu_uclamp_print(struct seq_file *sf,
    7334                 :            :                                     enum uclamp_id clamp_id)
    7335                 :            : {
    7336                 :            :         struct task_group *tg;
    7337                 :            :         u64 util_clamp;
    7338                 :            :         u64 percent;
    7339                 :            :         u32 rem;
    7340                 :            : 
    7341                 :            :         rcu_read_lock();
    7342                 :            :         tg = css_tg(seq_css(sf));
    7343                 :            :         util_clamp = tg->uclamp_req[clamp_id].value;
    7344                 :            :         rcu_read_unlock();
    7345                 :            : 
    7346                 :            :         if (util_clamp == SCHED_CAPACITY_SCALE) {
    7347                 :            :                 seq_puts(sf, "max\n");
    7348                 :            :                 return;
    7349                 :            :         }
    7350                 :            : 
    7351                 :            :         percent = tg->uclamp_pct[clamp_id];
    7352                 :            :         percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem);
    7353                 :            :         seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem);
    7354                 :            : }
    7355                 :            : 
    7356                 :            : static int cpu_uclamp_min_show(struct seq_file *sf, void *v)
    7357                 :            : {
    7358                 :            :         cpu_uclamp_print(sf, UCLAMP_MIN);
    7359                 :            :         return 0;
    7360                 :            : }
    7361                 :            : 
    7362                 :            : static int cpu_uclamp_max_show(struct seq_file *sf, void *v)
    7363                 :            : {
    7364                 :            :         cpu_uclamp_print(sf, UCLAMP_MAX);
    7365                 :            :         return 0;
    7366                 :            : }
    7367                 :            : #endif /* CONFIG_UCLAMP_TASK_GROUP */
    7368                 :            : 
    7369                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    7370                 :          0 : static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
    7371                 :            :                                 struct cftype *cftype, u64 shareval)
    7372                 :            : {
    7373                 :          0 :         if (shareval > scale_load_down(ULONG_MAX))
    7374                 :            :                 shareval = MAX_SHARES;
    7375                 :          0 :         return sched_group_set_shares(css_tg(css), scale_load(shareval));
    7376                 :            : }
    7377                 :            : 
    7378                 :          0 : static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
    7379                 :            :                                struct cftype *cft)
    7380                 :            : {
    7381                 :            :         struct task_group *tg = css_tg(css);
    7382                 :            : 
    7383                 :          0 :         return (u64) scale_load_down(tg->shares);
    7384                 :            : }
    7385                 :            : 
    7386                 :            : #ifdef CONFIG_CFS_BANDWIDTH
    7387                 :            : static DEFINE_MUTEX(cfs_constraints_mutex);
    7388                 :            : 
    7389                 :            : const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
    7390                 :            : static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
    7391                 :            : /* More than 203 days if BW_SHIFT equals 20. */
    7392                 :            : static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC;
    7393                 :            : 
    7394                 :            : static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
    7395                 :            : 
    7396                 :          0 : static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
    7397                 :            : {
    7398                 :            :         int i, ret = 0, runtime_enabled, runtime_was_enabled;
    7399                 :          0 :         struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
    7400                 :            : 
    7401                 :          0 :         if (tg == &root_task_group)
    7402                 :            :                 return -EINVAL;
    7403                 :            : 
    7404                 :            :         /*
    7405                 :            :          * Ensure we have at some amount of bandwidth every period.  This is
    7406                 :            :          * to prevent reaching a state of large arrears when throttled via
    7407                 :            :          * entity_tick() resulting in prolonged exit starvation.
    7408                 :            :          */
    7409                 :          0 :         if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
    7410                 :            :                 return -EINVAL;
    7411                 :            : 
    7412                 :            :         /*
    7413                 :            :          * Likewise, bound things on the otherside by preventing insane quota
    7414                 :            :          * periods.  This also allows us to normalize in computing quota
    7415                 :            :          * feasibility.
    7416                 :            :          */
    7417                 :          0 :         if (period > max_cfs_quota_period)
    7418                 :            :                 return -EINVAL;
    7419                 :            : 
    7420                 :            :         /*
    7421                 :            :          * Bound quota to defend quota against overflow during bandwidth shift.
    7422                 :            :          */
    7423                 :          0 :         if (quota != RUNTIME_INF && quota > max_cfs_runtime)
    7424                 :            :                 return -EINVAL;
    7425                 :            : 
    7426                 :            :         /*
    7427                 :            :          * Prevent race between setting of cfs_rq->runtime_enabled and
    7428                 :            :          * unthrottle_offline_cfs_rqs().
    7429                 :            :          */
    7430                 :            :         get_online_cpus();
    7431                 :          0 :         mutex_lock(&cfs_constraints_mutex);
    7432                 :          0 :         ret = __cfs_schedulable(tg, period, quota);
    7433                 :          0 :         if (ret)
    7434                 :            :                 goto out_unlock;
    7435                 :            : 
    7436                 :          0 :         runtime_enabled = quota != RUNTIME_INF;
    7437                 :          0 :         runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
    7438                 :            :         /*
    7439                 :            :          * If we need to toggle cfs_bandwidth_used, off->on must occur
    7440                 :            :          * before making related changes, and on->off must occur afterwards
    7441                 :            :          */
    7442                 :          0 :         if (runtime_enabled && !runtime_was_enabled)
    7443                 :          0 :                 cfs_bandwidth_usage_inc();
    7444                 :          0 :         raw_spin_lock_irq(&cfs_b->lock);
    7445                 :          0 :         cfs_b->period = ns_to_ktime(period);
    7446                 :          0 :         cfs_b->quota = quota;
    7447                 :            : 
    7448                 :          0 :         __refill_cfs_bandwidth_runtime(cfs_b);
    7449                 :            : 
    7450                 :            :         /* Restart the period timer (if active) to handle new period expiry: */
    7451                 :          0 :         if (runtime_enabled)
    7452                 :          0 :                 start_cfs_bandwidth(cfs_b);
    7453                 :            : 
    7454                 :          0 :         raw_spin_unlock_irq(&cfs_b->lock);
    7455                 :            : 
    7456                 :          0 :         for_each_online_cpu(i) {
    7457                 :          0 :                 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
    7458                 :          0 :                 struct rq *rq = cfs_rq->rq;
    7459                 :            :                 struct rq_flags rf;
    7460                 :            : 
    7461                 :            :                 rq_lock_irq(rq, &rf);
    7462                 :          0 :                 cfs_rq->runtime_enabled = runtime_enabled;
    7463                 :          0 :                 cfs_rq->runtime_remaining = 0;
    7464                 :            : 
    7465                 :          0 :                 if (cfs_rq->throttled)
    7466                 :          0 :                         unthrottle_cfs_rq(cfs_rq);
    7467                 :            :                 rq_unlock_irq(rq, &rf);
    7468                 :            :         }
    7469                 :          0 :         if (runtime_was_enabled && !runtime_enabled)
    7470                 :          0 :                 cfs_bandwidth_usage_dec();
    7471                 :            : out_unlock:
    7472                 :          0 :         mutex_unlock(&cfs_constraints_mutex);
    7473                 :            :         put_online_cpus();
    7474                 :            : 
    7475                 :          0 :         return ret;
    7476                 :            : }
    7477                 :            : 
    7478                 :          0 : static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
    7479                 :            : {
    7480                 :            :         u64 quota, period;
    7481                 :            : 
    7482                 :          0 :         period = ktime_to_ns(tg->cfs_bandwidth.period);
    7483                 :          0 :         if (cfs_quota_us < 0)
    7484                 :            :                 quota = RUNTIME_INF;
    7485                 :          0 :         else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
    7486                 :          0 :                 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
    7487                 :            :         else
    7488                 :            :                 return -EINVAL;
    7489                 :            : 
    7490                 :          0 :         return tg_set_cfs_bandwidth(tg, period, quota);
    7491                 :            : }
    7492                 :            : 
    7493                 :          0 : static long tg_get_cfs_quota(struct task_group *tg)
    7494                 :            : {
    7495                 :            :         u64 quota_us;
    7496                 :            : 
    7497                 :          0 :         if (tg->cfs_bandwidth.quota == RUNTIME_INF)
    7498                 :            :                 return -1;
    7499                 :            : 
    7500                 :            :         quota_us = tg->cfs_bandwidth.quota;
    7501                 :          0 :         do_div(quota_us, NSEC_PER_USEC);
    7502                 :            : 
    7503                 :          0 :         return quota_us;
    7504                 :            : }
    7505                 :            : 
    7506                 :          0 : static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
    7507                 :            : {
    7508                 :            :         u64 quota, period;
    7509                 :            : 
    7510                 :          0 :         if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
    7511                 :            :                 return -EINVAL;
    7512                 :            : 
    7513                 :          0 :         period = (u64)cfs_period_us * NSEC_PER_USEC;
    7514                 :          0 :         quota = tg->cfs_bandwidth.quota;
    7515                 :            : 
    7516                 :          0 :         return tg_set_cfs_bandwidth(tg, period, quota);
    7517                 :            : }
    7518                 :            : 
    7519                 :          0 : static long tg_get_cfs_period(struct task_group *tg)
    7520                 :            : {
    7521                 :            :         u64 cfs_period_us;
    7522                 :            : 
    7523                 :          0 :         cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
    7524                 :          0 :         do_div(cfs_period_us, NSEC_PER_USEC);
    7525                 :            : 
    7526                 :          0 :         return cfs_period_us;
    7527                 :            : }
    7528                 :            : 
    7529                 :          0 : static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
    7530                 :            :                                   struct cftype *cft)
    7531                 :            : {
    7532                 :          0 :         return tg_get_cfs_quota(css_tg(css));
    7533                 :            : }
    7534                 :            : 
    7535                 :          0 : static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
    7536                 :            :                                    struct cftype *cftype, s64 cfs_quota_us)
    7537                 :            : {
    7538                 :          0 :         return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
    7539                 :            : }
    7540                 :            : 
    7541                 :          0 : static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
    7542                 :            :                                    struct cftype *cft)
    7543                 :            : {
    7544                 :          0 :         return tg_get_cfs_period(css_tg(css));
    7545                 :            : }
    7546                 :            : 
    7547                 :          0 : static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
    7548                 :            :                                     struct cftype *cftype, u64 cfs_period_us)
    7549                 :            : {
    7550                 :          0 :         return tg_set_cfs_period(css_tg(css), cfs_period_us);
    7551                 :            : }
    7552                 :            : 
    7553                 :            : struct cfs_schedulable_data {
    7554                 :            :         struct task_group *tg;
    7555                 :            :         u64 period, quota;
    7556                 :            : };
    7557                 :            : 
    7558                 :            : /*
    7559                 :            :  * normalize group quota/period to be quota/max_period
    7560                 :            :  * note: units are usecs
    7561                 :            :  */
    7562                 :          0 : static u64 normalize_cfs_quota(struct task_group *tg,
    7563                 :            :                                struct cfs_schedulable_data *d)
    7564                 :            : {
    7565                 :            :         u64 quota, period;
    7566                 :            : 
    7567                 :          0 :         if (tg == d->tg) {
    7568                 :          0 :                 period = d->period;
    7569                 :          0 :                 quota = d->quota;
    7570                 :            :         } else {
    7571                 :          0 :                 period = tg_get_cfs_period(tg);
    7572                 :          0 :                 quota = tg_get_cfs_quota(tg);
    7573                 :            :         }
    7574                 :            : 
    7575                 :            :         /* note: these should typically be equivalent */
    7576                 :          0 :         if (quota == RUNTIME_INF || quota == -1)
    7577                 :            :                 return RUNTIME_INF;
    7578                 :            : 
    7579                 :          0 :         return to_ratio(period, quota);
    7580                 :            : }
    7581                 :            : 
    7582                 :          0 : static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
    7583                 :            : {
    7584                 :            :         struct cfs_schedulable_data *d = data;
    7585                 :            :         struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
    7586                 :            :         s64 quota = 0, parent_quota = -1;
    7587                 :            : 
    7588                 :          0 :         if (!tg->parent) {
    7589                 :            :                 quota = RUNTIME_INF;
    7590                 :            :         } else {
    7591                 :            :                 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
    7592                 :            : 
    7593                 :          0 :                 quota = normalize_cfs_quota(tg, d);
    7594                 :          0 :                 parent_quota = parent_b->hierarchical_quota;
    7595                 :            : 
    7596                 :            :                 /*
    7597                 :            :                  * Ensure max(child_quota) <= parent_quota.  On cgroup2,
    7598                 :            :                  * always take the min.  On cgroup1, only inherit when no
    7599                 :            :                  * limit is set:
    7600                 :            :                  */
    7601                 :          0 :                 if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
    7602                 :          0 :                         quota = min(quota, parent_quota);
    7603                 :            :                 } else {
    7604                 :          0 :                         if (quota == RUNTIME_INF)
    7605                 :            :                                 quota = parent_quota;
    7606                 :          0 :                         else if (parent_quota != RUNTIME_INF && quota > parent_quota)
    7607                 :            :                                 return -EINVAL;
    7608                 :            :                 }
    7609                 :            :         }
    7610                 :          0 :         cfs_b->hierarchical_quota = quota;
    7611                 :            : 
    7612                 :          0 :         return 0;
    7613                 :            : }
    7614                 :            : 
    7615                 :          0 : static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
    7616                 :            : {
    7617                 :            :         int ret;
    7618                 :          0 :         struct cfs_schedulable_data data = {
    7619                 :            :                 .tg = tg,
    7620                 :            :                 .period = period,
    7621                 :            :                 .quota = quota,
    7622                 :            :         };
    7623                 :            : 
    7624                 :          0 :         if (quota != RUNTIME_INF) {
    7625                 :          0 :                 do_div(data.period, NSEC_PER_USEC);
    7626                 :          0 :                 do_div(data.quota, NSEC_PER_USEC);
    7627                 :            :         }
    7628                 :            : 
    7629                 :            :         rcu_read_lock();
    7630                 :            :         ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
    7631                 :            :         rcu_read_unlock();
    7632                 :            : 
    7633                 :          0 :         return ret;
    7634                 :            : }
    7635                 :            : 
    7636                 :          0 : static int cpu_cfs_stat_show(struct seq_file *sf, void *v)
    7637                 :            : {
    7638                 :            :         struct task_group *tg = css_tg(seq_css(sf));
    7639                 :            :         struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
    7640                 :            : 
    7641                 :          0 :         seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
    7642                 :          0 :         seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
    7643                 :          0 :         seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
    7644                 :            : 
    7645                 :          0 :         if (schedstat_enabled() && tg != &root_task_group) {
    7646                 :            :                 u64 ws = 0;
    7647                 :            :                 int i;
    7648                 :            : 
    7649                 :          0 :                 for_each_possible_cpu(i)
    7650                 :          0 :                         ws += schedstat_val(tg->se[i]->statistics.wait_sum);
    7651                 :            : 
    7652                 :          0 :                 seq_printf(sf, "wait_sum %llu\n", ws);
    7653                 :            :         }
    7654                 :            : 
    7655                 :          0 :         return 0;
    7656                 :            : }
    7657                 :            : #endif /* CONFIG_CFS_BANDWIDTH */
    7658                 :            : #endif /* CONFIG_FAIR_GROUP_SCHED */
    7659                 :            : 
    7660                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    7661                 :            : static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
    7662                 :            :                                 struct cftype *cft, s64 val)
    7663                 :            : {
    7664                 :            :         return sched_group_set_rt_runtime(css_tg(css), val);
    7665                 :            : }
    7666                 :            : 
    7667                 :            : static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
    7668                 :            :                                struct cftype *cft)
    7669                 :            : {
    7670                 :            :         return sched_group_rt_runtime(css_tg(css));
    7671                 :            : }
    7672                 :            : 
    7673                 :            : static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
    7674                 :            :                                     struct cftype *cftype, u64 rt_period_us)
    7675                 :            : {
    7676                 :            :         return sched_group_set_rt_period(css_tg(css), rt_period_us);
    7677                 :            : }
    7678                 :            : 
    7679                 :            : static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
    7680                 :            :                                    struct cftype *cft)
    7681                 :            : {
    7682                 :            :         return sched_group_rt_period(css_tg(css));
    7683                 :            : }
    7684                 :            : #endif /* CONFIG_RT_GROUP_SCHED */
    7685                 :            : 
    7686                 :            : static struct cftype cpu_legacy_files[] = {
    7687                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    7688                 :            :         {
    7689                 :            :                 .name = "shares",
    7690                 :            :                 .read_u64 = cpu_shares_read_u64,
    7691                 :            :                 .write_u64 = cpu_shares_write_u64,
    7692                 :            :         },
    7693                 :            : #endif
    7694                 :            : #ifdef CONFIG_CFS_BANDWIDTH
    7695                 :            :         {
    7696                 :            :                 .name = "cfs_quota_us",
    7697                 :            :                 .read_s64 = cpu_cfs_quota_read_s64,
    7698                 :            :                 .write_s64 = cpu_cfs_quota_write_s64,
    7699                 :            :         },
    7700                 :            :         {
    7701                 :            :                 .name = "cfs_period_us",
    7702                 :            :                 .read_u64 = cpu_cfs_period_read_u64,
    7703                 :            :                 .write_u64 = cpu_cfs_period_write_u64,
    7704                 :            :         },
    7705                 :            :         {
    7706                 :            :                 .name = "stat",
    7707                 :            :                 .seq_show = cpu_cfs_stat_show,
    7708                 :            :         },
    7709                 :            : #endif
    7710                 :            : #ifdef CONFIG_RT_GROUP_SCHED
    7711                 :            :         {
    7712                 :            :                 .name = "rt_runtime_us",
    7713                 :            :                 .read_s64 = cpu_rt_runtime_read,
    7714                 :            :                 .write_s64 = cpu_rt_runtime_write,
    7715                 :            :         },
    7716                 :            :         {
    7717                 :            :                 .name = "rt_period_us",
    7718                 :            :                 .read_u64 = cpu_rt_period_read_uint,
    7719                 :            :                 .write_u64 = cpu_rt_period_write_uint,
    7720                 :            :         },
    7721                 :            : #endif
    7722                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    7723                 :            :         {
    7724                 :            :                 .name = "uclamp.min",
    7725                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7726                 :            :                 .seq_show = cpu_uclamp_min_show,
    7727                 :            :                 .write = cpu_uclamp_min_write,
    7728                 :            :         },
    7729                 :            :         {
    7730                 :            :                 .name = "uclamp.max",
    7731                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7732                 :            :                 .seq_show = cpu_uclamp_max_show,
    7733                 :            :                 .write = cpu_uclamp_max_write,
    7734                 :            :         },
    7735                 :            : #endif
    7736                 :            :         { }     /* Terminate */
    7737                 :            : };
    7738                 :            : 
    7739                 :          0 : static int cpu_extra_stat_show(struct seq_file *sf,
    7740                 :            :                                struct cgroup_subsys_state *css)
    7741                 :            : {
    7742                 :            : #ifdef CONFIG_CFS_BANDWIDTH
    7743                 :            :         {
    7744                 :            :                 struct task_group *tg = css_tg(css);
    7745                 :            :                 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
    7746                 :            :                 u64 throttled_usec;
    7747                 :            : 
    7748                 :          0 :                 throttled_usec = cfs_b->throttled_time;
    7749                 :          0 :                 do_div(throttled_usec, NSEC_PER_USEC);
    7750                 :            : 
    7751                 :          0 :                 seq_printf(sf, "nr_periods %d\n"
    7752                 :            :                            "nr_throttled %d\n"
    7753                 :            :                            "throttled_usec %llu\n",
    7754                 :            :                            cfs_b->nr_periods, cfs_b->nr_throttled,
    7755                 :            :                            throttled_usec);
    7756                 :            :         }
    7757                 :            : #endif
    7758                 :          0 :         return 0;
    7759                 :            : }
    7760                 :            : 
    7761                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    7762                 :          0 : static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css,
    7763                 :            :                                struct cftype *cft)
    7764                 :            : {
    7765                 :            :         struct task_group *tg = css_tg(css);
    7766                 :          0 :         u64 weight = scale_load_down(tg->shares);
    7767                 :            : 
    7768                 :          0 :         return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024);
    7769                 :            : }
    7770                 :            : 
    7771                 :          0 : static int cpu_weight_write_u64(struct cgroup_subsys_state *css,
    7772                 :            :                                 struct cftype *cft, u64 weight)
    7773                 :            : {
    7774                 :            :         /*
    7775                 :            :          * cgroup weight knobs should use the common MIN, DFL and MAX
    7776                 :            :          * values which are 1, 100 and 10000 respectively.  While it loses
    7777                 :            :          * a bit of range on both ends, it maps pretty well onto the shares
    7778                 :            :          * value used by scheduler and the round-trip conversions preserve
    7779                 :            :          * the original value over the entire range.
    7780                 :            :          */
    7781                 :          0 :         if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX)
    7782                 :            :                 return -ERANGE;
    7783                 :            : 
    7784                 :          0 :         weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL);
    7785                 :            : 
    7786                 :          0 :         return sched_group_set_shares(css_tg(css), scale_load(weight));
    7787                 :            : }
    7788                 :            : 
    7789                 :          0 : static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css,
    7790                 :            :                                     struct cftype *cft)
    7791                 :            : {
    7792                 :          0 :         unsigned long weight = scale_load_down(css_tg(css)->shares);
    7793                 :            :         int last_delta = INT_MAX;
    7794                 :            :         int prio, delta;
    7795                 :            : 
    7796                 :            :         /* find the closest nice value to the current weight */
    7797                 :          0 :         for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) {
    7798                 :          0 :                 delta = abs(sched_prio_to_weight[prio] - weight);
    7799                 :          0 :                 if (delta >= last_delta)
    7800                 :            :                         break;
    7801                 :            :                 last_delta = delta;
    7802                 :            :         }
    7803                 :            : 
    7804                 :          0 :         return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO);
    7805                 :            : }
    7806                 :            : 
    7807                 :          0 : static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css,
    7808                 :            :                                      struct cftype *cft, s64 nice)
    7809                 :            : {
    7810                 :            :         unsigned long weight;
    7811                 :            :         int idx;
    7812                 :            : 
    7813                 :          0 :         if (nice < MIN_NICE || nice > MAX_NICE)
    7814                 :            :                 return -ERANGE;
    7815                 :            : 
    7816                 :          0 :         idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO;
    7817                 :          0 :         idx = array_index_nospec(idx, 40);
    7818                 :          0 :         weight = sched_prio_to_weight[idx];
    7819                 :            : 
    7820                 :          0 :         return sched_group_set_shares(css_tg(css), scale_load(weight));
    7821                 :            : }
    7822                 :            : #endif
    7823                 :            : 
    7824                 :          0 : static void __maybe_unused cpu_period_quota_print(struct seq_file *sf,
    7825                 :            :                                                   long period, long quota)
    7826                 :            : {
    7827                 :          0 :         if (quota < 0)
    7828                 :          0 :                 seq_puts(sf, "max");
    7829                 :            :         else
    7830                 :          0 :                 seq_printf(sf, "%ld", quota);
    7831                 :            : 
    7832                 :          0 :         seq_printf(sf, " %ld\n", period);
    7833                 :          0 : }
    7834                 :            : 
    7835                 :            : /* caller should put the current value in *@periodp before calling */
    7836                 :          0 : static int __maybe_unused cpu_period_quota_parse(char *buf,
    7837                 :            :                                                  u64 *periodp, u64 *quotap)
    7838                 :            : {
    7839                 :            :         char tok[21];   /* U64_MAX */
    7840                 :            : 
    7841                 :          0 :         if (sscanf(buf, "%20s %llu", tok, periodp) < 1)
    7842                 :            :                 return -EINVAL;
    7843                 :            : 
    7844                 :          0 :         *periodp *= NSEC_PER_USEC;
    7845                 :            : 
    7846                 :          0 :         if (sscanf(tok, "%llu", quotap))
    7847                 :          0 :                 *quotap *= NSEC_PER_USEC;
    7848                 :          0 :         else if (!strcmp(tok, "max"))
    7849                 :          0 :                 *quotap = RUNTIME_INF;
    7850                 :            :         else
    7851                 :            :                 return -EINVAL;
    7852                 :            : 
    7853                 :            :         return 0;
    7854                 :            : }
    7855                 :            : 
    7856                 :            : #ifdef CONFIG_CFS_BANDWIDTH
    7857                 :          0 : static int cpu_max_show(struct seq_file *sf, void *v)
    7858                 :            : {
    7859                 :            :         struct task_group *tg = css_tg(seq_css(sf));
    7860                 :            : 
    7861                 :          0 :         cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg));
    7862                 :          0 :         return 0;
    7863                 :            : }
    7864                 :            : 
    7865                 :          0 : static ssize_t cpu_max_write(struct kernfs_open_file *of,
    7866                 :            :                              char *buf, size_t nbytes, loff_t off)
    7867                 :            : {
    7868                 :          0 :         struct task_group *tg = css_tg(of_css(of));
    7869                 :          0 :         u64 period = tg_get_cfs_period(tg);
    7870                 :            :         u64 quota;
    7871                 :            :         int ret;
    7872                 :            : 
    7873                 :          0 :         ret = cpu_period_quota_parse(buf, &period, &quota);
    7874                 :          0 :         if (!ret)
    7875                 :          0 :                 ret = tg_set_cfs_bandwidth(tg, period, quota);
    7876                 :          0 :         return ret ?: nbytes;
    7877                 :            : }
    7878                 :            : #endif
    7879                 :            : 
    7880                 :            : static struct cftype cpu_files[] = {
    7881                 :            : #ifdef CONFIG_FAIR_GROUP_SCHED
    7882                 :            :         {
    7883                 :            :                 .name = "weight",
    7884                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7885                 :            :                 .read_u64 = cpu_weight_read_u64,
    7886                 :            :                 .write_u64 = cpu_weight_write_u64,
    7887                 :            :         },
    7888                 :            :         {
    7889                 :            :                 .name = "weight.nice",
    7890                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7891                 :            :                 .read_s64 = cpu_weight_nice_read_s64,
    7892                 :            :                 .write_s64 = cpu_weight_nice_write_s64,
    7893                 :            :         },
    7894                 :            : #endif
    7895                 :            : #ifdef CONFIG_CFS_BANDWIDTH
    7896                 :            :         {
    7897                 :            :                 .name = "max",
    7898                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7899                 :            :                 .seq_show = cpu_max_show,
    7900                 :            :                 .write = cpu_max_write,
    7901                 :            :         },
    7902                 :            : #endif
    7903                 :            : #ifdef CONFIG_UCLAMP_TASK_GROUP
    7904                 :            :         {
    7905                 :            :                 .name = "uclamp.min",
    7906                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7907                 :            :                 .seq_show = cpu_uclamp_min_show,
    7908                 :            :                 .write = cpu_uclamp_min_write,
    7909                 :            :         },
    7910                 :            :         {
    7911                 :            :                 .name = "uclamp.max",
    7912                 :            :                 .flags = CFTYPE_NOT_ON_ROOT,
    7913                 :            :                 .seq_show = cpu_uclamp_max_show,
    7914                 :            :                 .write = cpu_uclamp_max_write,
    7915                 :            :         },
    7916                 :            : #endif
    7917                 :            :         { }     /* terminate */
    7918                 :            : };
    7919                 :            : 
    7920                 :            : struct cgroup_subsys cpu_cgrp_subsys = {
    7921                 :            :         .css_alloc      = cpu_cgroup_css_alloc,
    7922                 :            :         .css_online     = cpu_cgroup_css_online,
    7923                 :            :         .css_released   = cpu_cgroup_css_released,
    7924                 :            :         .css_free       = cpu_cgroup_css_free,
    7925                 :            :         .css_extra_stat_show = cpu_extra_stat_show,
    7926                 :            :         .fork           = cpu_cgroup_fork,
    7927                 :            :         .can_attach     = cpu_cgroup_can_attach,
    7928                 :            :         .attach         = cpu_cgroup_attach,
    7929                 :            :         .legacy_cftypes = cpu_legacy_files,
    7930                 :            :         .dfl_cftypes    = cpu_files,
    7931                 :            :         .early_init     = true,
    7932                 :            :         .threaded       = true,
    7933                 :            : };
    7934                 :            : 
    7935                 :            : #endif  /* CONFIG_CGROUP_SCHED */
    7936                 :            : 
    7937                 :          0 : void dump_cpu_task(int cpu)
    7938                 :            : {
    7939                 :          0 :         pr_info("Task dump for CPU %d:\n", cpu);
    7940                 :          0 :         sched_show_task(cpu_curr(cpu));
    7941                 :          0 : }
    7942                 :            : 
    7943                 :            : /*
    7944                 :            :  * Nice levels are multiplicative, with a gentle 10% change for every
    7945                 :            :  * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
    7946                 :            :  * nice 1, it will get ~10% less CPU time than another CPU-bound task
    7947                 :            :  * that remained on nice 0.
    7948                 :            :  *
    7949                 :            :  * The "10% effect" is relative and cumulative: from _any_ nice level,
    7950                 :            :  * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
    7951                 :            :  * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
    7952                 :            :  * If a task goes up by ~10% and another task goes down by ~10% then
    7953                 :            :  * the relative distance between them is ~25%.)
    7954                 :            :  */
    7955                 :            : const int sched_prio_to_weight[40] = {
    7956                 :            :  /* -20 */     88761,     71755,     56483,     46273,     36291,
    7957                 :            :  /* -15 */     29154,     23254,     18705,     14949,     11916,
    7958                 :            :  /* -10 */      9548,      7620,      6100,      4904,      3906,
    7959                 :            :  /*  -5 */      3121,      2501,      1991,      1586,      1277,
    7960                 :            :  /*   0 */      1024,       820,       655,       526,       423,
    7961                 :            :  /*   5 */       335,       272,       215,       172,       137,
    7962                 :            :  /*  10 */       110,        87,        70,        56,        45,
    7963                 :            :  /*  15 */        36,        29,        23,        18,        15,
    7964                 :            : };
    7965                 :            : 
    7966                 :            : /*
    7967                 :            :  * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
    7968                 :            :  *
    7969                 :            :  * In cases where the weight does not change often, we can use the
    7970                 :            :  * precalculated inverse to speed up arithmetics by turning divisions
    7971                 :            :  * into multiplications:
    7972                 :            :  */
    7973                 :            : const u32 sched_prio_to_wmult[40] = {
    7974                 :            :  /* -20 */     48388,     59856,     76040,     92818,    118348,
    7975                 :            :  /* -15 */    147320,    184698,    229616,    287308,    360437,
    7976                 :            :  /* -10 */    449829,    563644,    704093,    875809,   1099582,
    7977                 :            :  /*  -5 */   1376151,   1717300,   2157191,   2708050,   3363326,
    7978                 :            :  /*   0 */   4194304,   5237765,   6557202,   8165337,  10153587,
    7979                 :            :  /*   5 */  12820798,  15790321,  19976592,  24970740,  31350126,
    7980                 :            :  /*  10 */  39045157,  49367440,  61356676,  76695844,  95443717,
    7981                 :            :  /*  15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
    7982                 :            : };
    7983                 :            : 
    7984                 :            : #undef CREATE_TRACE_POINTS
    

Generated by: LCOV version 1.14