Commit 6cc82dc2 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'sched-urgent-2024-08-04' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull scheduler fixes from Thomas Gleixner:

 - When stime is larger than rtime due to accounting imprecision, then
   utime = rtime - stime becomes negative. As this is unsigned math, the
   result becomes a huge positive number.

   Cure it by resetting stime to rtime in that case, so utime becomes 0.

 - Restore consistent state when sched_cpu_deactivate() fails.

   When offlining a CPU fails in sched_cpu_deactivate() after the SMT
   present counter has been decremented, then the function aborts but
   fails to increment the SMT present counter and leaves it imbalanced.
   Consecutive operations cause it to underflow. Add the missing fixup
   for the error path.

   For SMT accounting the runqueue needs to marked online again in the
   error exit path to restore consistent state.

* tag 'sched-urgent-2024-08-04' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  sched/core: Fix unbalance set_rq_online/offline() in sched_cpu_deactivate()
  sched/core: Introduce sched_set_rq_on/offline() helper
  sched/smt: Fix unbalance sched_smt_present dec/inc
  sched/smt: Introduce sched_smt_present_inc/dec() helper
  sched/cputime: Fix mul_u64_u64_div_u64() precision for cputime
parents 1ddeb0ef fe7a11c7
...@@ -7845,6 +7845,30 @@ void set_rq_offline(struct rq *rq) ...@@ -7845,6 +7845,30 @@ void set_rq_offline(struct rq *rq)
} }
} }
static inline void sched_set_rq_online(struct rq *rq, int cpu)
{
struct rq_flags rf;
rq_lock_irqsave(rq, &rf);
if (rq->rd) {
BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
set_rq_online(rq);
}
rq_unlock_irqrestore(rq, &rf);
}
static inline void sched_set_rq_offline(struct rq *rq, int cpu)
{
struct rq_flags rf;
rq_lock_irqsave(rq, &rf);
if (rq->rd) {
BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
set_rq_offline(rq);
}
rq_unlock_irqrestore(rq, &rf);
}
/* /*
* used to mark begin/end of suspend/resume: * used to mark begin/end of suspend/resume:
*/ */
...@@ -7895,10 +7919,25 @@ static int cpuset_cpu_inactive(unsigned int cpu) ...@@ -7895,10 +7919,25 @@ static int cpuset_cpu_inactive(unsigned int cpu)
return 0; return 0;
} }
static inline void sched_smt_present_inc(int cpu)
{
#ifdef CONFIG_SCHED_SMT
if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
static_branch_inc_cpuslocked(&sched_smt_present);
#endif
}
static inline void sched_smt_present_dec(int cpu)
{
#ifdef CONFIG_SCHED_SMT
if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
static_branch_dec_cpuslocked(&sched_smt_present);
#endif
}
int sched_cpu_activate(unsigned int cpu) int sched_cpu_activate(unsigned int cpu)
{ {
struct rq *rq = cpu_rq(cpu); struct rq *rq = cpu_rq(cpu);
struct rq_flags rf;
/* /*
* Clear the balance_push callback and prepare to schedule * Clear the balance_push callback and prepare to schedule
...@@ -7906,13 +7945,10 @@ int sched_cpu_activate(unsigned int cpu) ...@@ -7906,13 +7945,10 @@ int sched_cpu_activate(unsigned int cpu)
*/ */
balance_push_set(cpu, false); balance_push_set(cpu, false);
#ifdef CONFIG_SCHED_SMT
/* /*
* When going up, increment the number of cores with SMT present. * When going up, increment the number of cores with SMT present.
*/ */
if (cpumask_weight(cpu_smt_mask(cpu)) == 2) sched_smt_present_inc(cpu);
static_branch_inc_cpuslocked(&sched_smt_present);
#endif
set_cpu_active(cpu, true); set_cpu_active(cpu, true);
if (sched_smp_initialized) { if (sched_smp_initialized) {
...@@ -7930,12 +7966,7 @@ int sched_cpu_activate(unsigned int cpu) ...@@ -7930,12 +7966,7 @@ int sched_cpu_activate(unsigned int cpu)
* 2) At runtime, if cpuset_cpu_active() fails to rebuild the * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
* domains. * domains.
*/ */
rq_lock_irqsave(rq, &rf); sched_set_rq_online(rq, cpu);
if (rq->rd) {
BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
set_rq_online(rq);
}
rq_unlock_irqrestore(rq, &rf);
return 0; return 0;
} }
...@@ -7943,7 +7974,6 @@ int sched_cpu_activate(unsigned int cpu) ...@@ -7943,7 +7974,6 @@ int sched_cpu_activate(unsigned int cpu)
int sched_cpu_deactivate(unsigned int cpu) int sched_cpu_deactivate(unsigned int cpu)
{ {
struct rq *rq = cpu_rq(cpu); struct rq *rq = cpu_rq(cpu);
struct rq_flags rf;
int ret; int ret;
/* /*
...@@ -7974,20 +8004,14 @@ int sched_cpu_deactivate(unsigned int cpu) ...@@ -7974,20 +8004,14 @@ int sched_cpu_deactivate(unsigned int cpu)
*/ */
synchronize_rcu(); synchronize_rcu();
rq_lock_irqsave(rq, &rf); sched_set_rq_offline(rq, cpu);
if (rq->rd) {
BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
set_rq_offline(rq);
}
rq_unlock_irqrestore(rq, &rf);
#ifdef CONFIG_SCHED_SMT
/* /*
* When going down, decrement the number of cores with SMT present. * When going down, decrement the number of cores with SMT present.
*/ */
if (cpumask_weight(cpu_smt_mask(cpu)) == 2) sched_smt_present_dec(cpu);
static_branch_dec_cpuslocked(&sched_smt_present);
#ifdef CONFIG_SCHED_SMT
sched_core_cpu_deactivate(cpu); sched_core_cpu_deactivate(cpu);
#endif #endif
...@@ -7997,6 +8021,8 @@ int sched_cpu_deactivate(unsigned int cpu) ...@@ -7997,6 +8021,8 @@ int sched_cpu_deactivate(unsigned int cpu)
sched_update_numa(cpu, false); sched_update_numa(cpu, false);
ret = cpuset_cpu_inactive(cpu); ret = cpuset_cpu_inactive(cpu);
if (ret) { if (ret) {
sched_smt_present_inc(cpu);
sched_set_rq_online(rq, cpu);
balance_push_set(cpu, false); balance_push_set(cpu, false);
set_cpu_active(cpu, true); set_cpu_active(cpu, true);
sched_update_numa(cpu, true); sched_update_numa(cpu, true);
......
...@@ -582,6 +582,12 @@ void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev, ...@@ -582,6 +582,12 @@ void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
} }
stime = mul_u64_u64_div_u64(stime, rtime, stime + utime); stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
/*
* Because mul_u64_u64_div_u64() can approximate on some
* achitectures; enforce the constraint that: a*b/(b+c) <= a.
*/
if (unlikely(stime > rtime))
stime = rtime;
update: update:
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment