trace_sched_util_est_se_tp(&p->se);
 }
 
+static inline int util_fits_cpu(unsigned long util,
+                               unsigned long uclamp_min,
+                               unsigned long uclamp_max,
+                               int cpu)
+{
+       unsigned long capacity_orig, capacity_orig_thermal;
+       unsigned long capacity = capacity_of(cpu);
+       bool fits, uclamp_max_fits;
+
+       /*
+        * Check if the real util fits without any uclamp boost/cap applied.
+        */
+       fits = fits_capacity(util, capacity);
+
+       if (!uclamp_is_used())
+               return fits;
+
+       /*
+        * We must use capacity_orig_of() for comparing against uclamp_min and
+        * uclamp_max. We only care about capacity pressure (by using
+        * capacity_of()) for comparing against the real util.
+        *
+        * If a task is boosted to 1024 for example, we don't want a tiny
+        * pressure to skew the check whether it fits a CPU or not.
+        *
+        * Similarly if a task is capped to capacity_orig_of(little_cpu), it
+        * should fit a little cpu even if there's some pressure.
+        *
+        * Only exception is for thermal pressure since it has a direct impact
+        * on available OPP of the system.
+        *
+        * We honour it for uclamp_min only as a drop in performance level
+        * could result in not getting the requested minimum performance level.
+        *
+        * For uclamp_max, we can tolerate a drop in performance level as the
+        * goal is to cap the task. So it's okay if it's getting less.
+        *
+        * In case of capacity inversion, which is not handled yet, we should
+        * honour the inverted capacity for both uclamp_min and uclamp_max all
+        * the time.
+        */
+       capacity_orig = capacity_orig_of(cpu);
+       capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu);
+
+       /*
+        * We want to force a task to fit a cpu as implied by uclamp_max.
+        * But we do have some corner cases to cater for..
+        *
+        *
+        *                                 C=z
+        *   |                             ___
+        *   |                  C=y       |   |
+        *   |_ _ _ _ _ _ _ _ _ ___ _ _ _ | _ | _ _ _ _ _  uclamp_max
+        *   |      C=x        |   |      |   |
+        *   |      ___        |   |      |   |
+        *   |     |   |       |   |      |   |    (util somewhere in this region)
+        *   |     |   |       |   |      |   |
+        *   |     |   |       |   |      |   |
+        *   +----------------------------------------
+        *         cpu0        cpu1       cpu2
+        *
+        *   In the above example if a task is capped to a specific performance
+        *   point, y, then when:
+        *
+        *   * util = 80% of x then it does not fit on cpu0 and should migrate
+        *     to cpu1
+        *   * util = 80% of y then it is forced to fit on cpu1 to honour
+        *     uclamp_max request.
+        *
+        *   which is what we're enforcing here. A task always fits if
+        *   uclamp_max <= capacity_orig. But when uclamp_max > capacity_orig,
+        *   the normal upmigration rules should withhold still.
+        *
+        *   Only exception is when we are on max capacity, then we need to be
+        *   careful not to block overutilized state. This is so because:
+        *
+        *     1. There's no concept of capping at max_capacity! We can't go
+        *        beyond this performance level anyway.
+        *     2. The system is being saturated when we're operating near
+        *        max capacity, it doesn't make sense to block overutilized.
+        */
+       uclamp_max_fits = (capacity_orig == SCHED_CAPACITY_SCALE) && (uclamp_max == SCHED_CAPACITY_SCALE);
+       uclamp_max_fits = !uclamp_max_fits && (uclamp_max <= capacity_orig);
+       fits = fits || uclamp_max_fits;
+
+       /*
+        *
+        *                                 C=z
+        *   |                             ___       (region a, capped, util >= uclamp_max)
+        *   |                  C=y       |   |
+        *   |_ _ _ _ _ _ _ _ _ ___ _ _ _ | _ | _ _ _ _ _ uclamp_max
+        *   |      C=x        |   |      |   |
+        *   |      ___        |   |      |   |      (region b, uclamp_min <= util <= uclamp_max)
+        *   |_ _ _|_ _|_ _ _ _| _ | _ _ _| _ | _ _ _ _ _ uclamp_min
+        *   |     |   |       |   |      |   |
+        *   |     |   |       |   |      |   |      (region c, boosted, util < uclamp_min)
+        *   +----------------------------------------
+        *         cpu0        cpu1       cpu2
+        *
+        * a) If util > uclamp_max, then we're capped, we don't care about
+        *    actual fitness value here. We only care if uclamp_max fits
+        *    capacity without taking margin/pressure into account.
+        *    See comment above.
+        *
+        * b) If uclamp_min <= util <= uclamp_max, then the normal
+        *    fits_capacity() rules apply. Except we need to ensure that we
+        *    enforce we remain within uclamp_max, see comment above.
+        *
+        * c) If util < uclamp_min, then we are boosted. Same as (b) but we
+        *    need to take into account the boosted value fits the CPU without
+        *    taking margin/pressure into account.
+        *
+        * Cases (a) and (b) are handled in the 'fits' variable already. We
+        * just need to consider an extra check for case (c) after ensuring we
+        * handle the case uclamp_min > uclamp_max.
+        */
+       uclamp_min = min(uclamp_min, uclamp_max);
+       if (util < uclamp_min && capacity_orig != SCHED_CAPACITY_SCALE)
+               fits = fits && (uclamp_min <= capacity_orig_thermal);
+
+       return fits;
+}
+
 static inline int task_fits_capacity(struct task_struct *p,
                                     unsigned long capacity)
 {