Skip to content

Commit 00357f5

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched/nohz: Clean up nohz enter/exit
The primary observation is that nohz enter/exit is always from the current CPU, therefore NOHZ_TICK_STOPPED does not in fact need to be an atomic. Secondary is that we appear to have 2 nearly identical hooks in the nohz enter code, set_cpu_sd_state_idle() and nohz_balance_enter_idle(). Fold the whole set_cpu_sd_state thing into nohz_balance_{enter,exit}_idle. Removes an atomic op from both enter and exit paths. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent e022e0d commit 00357f5

File tree

5 files changed

+43
-52
lines changed

5 files changed

+43
-52
lines changed

include/linux/sched/nohz.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ static inline void cpu_load_update_nohz_stop(void) { }
1616

1717
#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
1818
extern void nohz_balance_enter_idle(int cpu);
19-
extern void set_cpu_sd_state_idle(void);
2019
extern int get_nohz_timer_target(void);
2120
#else
2221
static inline void nohz_balance_enter_idle(int cpu) { }
23-
static inline void set_cpu_sd_state_idle(void) { }
2422
#endif
2523

2624
#ifdef CONFIG_NO_HZ_COMMON

kernel/sched/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5861,7 +5861,7 @@ int sched_cpu_dying(unsigned int cpu)
58615861

58625862
calc_load_migrate(rq);
58635863
update_max_interval();
5864-
nohz_balance_exit_idle(cpu);
5864+
nohz_balance_exit_idle(rq);
58655865
hrtick_clear(rq);
58665866
return 0;
58675867
}

kernel/sched/fair.c

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9103,23 +9103,6 @@ static inline int find_new_ilb(void)
91039103
return nr_cpu_ids;
91049104
}
91059105

9106-
static inline void set_cpu_sd_state_busy(void)
9107-
{
9108-
struct sched_domain *sd;
9109-
int cpu = smp_processor_id();
9110-
9111-
rcu_read_lock();
9112-
sd = rcu_dereference(per_cpu(sd_llc, cpu));
9113-
9114-
if (!sd || !sd->nohz_idle)
9115-
goto unlock;
9116-
sd->nohz_idle = 0;
9117-
9118-
atomic_inc(&sd->shared->nr_busy_cpus);
9119-
unlock:
9120-
rcu_read_unlock();
9121-
}
9122-
91239106
/*
91249107
* Kick a CPU to do the nohz balancing, if it is time for it. We pick the
91259108
* nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
@@ -9175,8 +9158,7 @@ static void nohz_balancer_kick(struct rq *rq)
91759158
* We may be recently in ticked or tickless idle mode. At the first
91769159
* busy tick after returning from idle, we will update the busy stats.
91779160
*/
9178-
set_cpu_sd_state_busy();
9179-
nohz_balance_exit_idle(cpu);
9161+
nohz_balance_exit_idle(rq);
91809162

91819163
/*
91829164
* None are in tickless mode and hence no need for NOHZ idle load
@@ -9240,27 +9222,39 @@ static void nohz_balancer_kick(struct rq *rq)
92409222
kick_ilb(flags);
92419223
}
92429224

9243-
void nohz_balance_exit_idle(unsigned int cpu)
9225+
static void set_cpu_sd_state_busy(int cpu)
92449226
{
9245-
unsigned int flags = atomic_read(nohz_flags(cpu));
9227+
struct sched_domain *sd;
92469228

9247-
if (unlikely(flags & NOHZ_TICK_STOPPED)) {
9248-
/*
9249-
* Completely isolated CPUs don't ever set, so we must test.
9250-
*/
9251-
if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
9252-
cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
9253-
atomic_dec(&nohz.nr_cpus);
9254-
}
9229+
rcu_read_lock();
9230+
sd = rcu_dereference(per_cpu(sd_llc, cpu));
92559231

9256-
atomic_andnot(NOHZ_TICK_STOPPED, nohz_flags(cpu));
9257-
}
9232+
if (!sd || !sd->nohz_idle)
9233+
goto unlock;
9234+
sd->nohz_idle = 0;
9235+
9236+
atomic_inc(&sd->shared->nr_busy_cpus);
9237+
unlock:
9238+
rcu_read_unlock();
92589239
}
92599240

9260-
void set_cpu_sd_state_idle(void)
9241+
void nohz_balance_exit_idle(struct rq *rq)
9242+
{
9243+
SCHED_WARN_ON(rq != this_rq());
9244+
9245+
if (likely(!rq->nohz_tick_stopped))
9246+
return;
9247+
9248+
rq->nohz_tick_stopped = 0;
9249+
cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask);
9250+
atomic_dec(&nohz.nr_cpus);
9251+
9252+
set_cpu_sd_state_busy(rq->cpu);
9253+
}
9254+
9255+
static void set_cpu_sd_state_idle(int cpu)
92619256
{
92629257
struct sched_domain *sd;
9263-
int cpu = smp_processor_id();
92649258

92659259
rcu_read_lock();
92669260
sd = rcu_dereference(per_cpu(sd_llc, cpu));
@@ -9280,6 +9274,10 @@ void set_cpu_sd_state_idle(void)
92809274
*/
92819275
void nohz_balance_enter_idle(int cpu)
92829276
{
9277+
struct rq *rq = cpu_rq(cpu);
9278+
9279+
SCHED_WARN_ON(cpu != smp_processor_id());
9280+
92839281
/* If this CPU is going down, then nothing needs to be done: */
92849282
if (!cpu_active(cpu))
92859283
return;
@@ -9288,16 +9286,19 @@ void nohz_balance_enter_idle(int cpu)
92889286
if (!housekeeping_cpu(cpu, HK_FLAG_SCHED))
92899287
return;
92909288

9291-
if (atomic_read(nohz_flags(cpu)) & NOHZ_TICK_STOPPED)
9289+
if (rq->nohz_tick_stopped)
92929290
return;
92939291

92949292
/* If we're a completely isolated CPU, we don't play: */
9295-
if (on_null_domain(cpu_rq(cpu)))
9293+
if (on_null_domain(rq))
92969294
return;
92979295

9296+
rq->nohz_tick_stopped = 1;
9297+
92989298
cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
92999299
atomic_inc(&nohz.nr_cpus);
9300-
atomic_or(NOHZ_TICK_STOPPED, nohz_flags(cpu));
9300+
9301+
set_cpu_sd_state_idle(cpu);
93019302
}
93029303
#else
93039304
static inline void nohz_balancer_kick(struct rq *rq) { }

kernel/sched/sched.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ struct rq {
764764
unsigned long last_load_update_tick;
765765
unsigned long last_blocked_load_update_tick;
766766
#endif /* CONFIG_SMP */
767+
unsigned int nohz_tick_stopped;
767768
atomic_t nohz_flags;
768769
#endif /* CONFIG_NO_HZ_COMMON */
769770

@@ -2035,21 +2036,19 @@ extern void cfs_bandwidth_usage_inc(void);
20352036
extern void cfs_bandwidth_usage_dec(void);
20362037

20372038
#ifdef CONFIG_NO_HZ_COMMON
2038-
#define NOHZ_TICK_STOPPED_BIT 0
2039-
#define NOHZ_BALANCE_KICK_BIT 1
2040-
#define NOHZ_STATS_KICK_BIT 2
2039+
#define NOHZ_BALANCE_KICK_BIT 0
2040+
#define NOHZ_STATS_KICK_BIT 1
20412041

2042-
#define NOHZ_TICK_STOPPED BIT(NOHZ_TICK_STOPPED_BIT)
20432042
#define NOHZ_BALANCE_KICK BIT(NOHZ_BALANCE_KICK_BIT)
20442043
#define NOHZ_STATS_KICK BIT(NOHZ_STATS_KICK_BIT)
20452044

20462045
#define NOHZ_KICK_MASK (NOHZ_BALANCE_KICK | NOHZ_STATS_KICK)
20472046

20482047
#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
20492048

2050-
extern void nohz_balance_exit_idle(unsigned int cpu);
2049+
extern void nohz_balance_exit_idle(struct rq *rq);
20512050
#else
2052-
static inline void nohz_balance_exit_idle(unsigned int cpu) { }
2051+
static inline void nohz_balance_exit_idle(struct rq *rq) { }
20532052
#endif
20542053

20552054

kernel/time/tick-sched.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -954,13 +954,6 @@ void tick_nohz_idle_enter(void)
954954
struct tick_sched *ts;
955955

956956
lockdep_assert_irqs_enabled();
957-
/*
958-
* Update the idle state in the scheduler domain hierarchy
959-
* when tick_nohz_stop_sched_tick() is called from the idle loop.
960-
* State will be updated to busy during the first busy tick after
961-
* exiting idle.
962-
*/
963-
set_cpu_sd_state_idle();
964957

965958
local_irq_disable();
966959

0 commit comments

Comments
 (0)