Skip to content

Commit a166fcf

Browse files
committed
Merge branch 'timers/nohz-posix-timers-v2' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks into timers/nohz
Pull posix cpu timers handling on full dynticks from Frederic Weisbecker. Signed-off-by: Ingo Molnar <mingo@kernel.org>
2 parents 2727872 + 555347f commit a166fcf

5 files changed

Lines changed: 118 additions & 16 deletions

File tree

‎include/linux/posix-timers.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ void run_posix_cpu_timers(struct task_struct *task);
122122
void posix_cpu_timers_exit(struct task_struct *task);
123123
void posix_cpu_timers_exit_group(struct task_struct *task);
124124

125+
bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk);
126+
125127
void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
126128
cputime_t *newval, cputime_t *oldval);
127129

‎include/linux/tick.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
160160
#ifdef CONFIG_NO_HZ_FULL
161161
extern void tick_nohz_init(void);
162162
extern int tick_nohz_full_cpu(int cpu);
163+
extern void tick_nohz_full_kick(void);
164+
extern void tick_nohz_full_kick_all(void);
163165
#else
164166
static inline void tick_nohz_init(void) { }
165167
static inline int tick_nohz_full_cpu(int cpu) { return 0; }
168+
static inline void tick_nohz_full_kick(void) { }
169+
static inline void tick_nohz_full_kick_all(void) { }
166170
#endif
167171

168172

‎kernel/posix-cpu-timers.c‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <linux/kernel_stat.h>
1111
#include <trace/events/timer.h>
1212
#include <linux/random.h>
13+
#include <linux/tick.h>
14+
#include <linux/workqueue.h>
1315

1416
/*
1517
* Called after updating RLIMIT_CPU to run cpu timer and update
@@ -153,6 +155,21 @@ static void bump_cpu_timer(struct k_itimer *timer,
153155
}
154156
}
155157

158+
/**
159+
* task_cputime_zero - Check a task_cputime struct for all zero fields.
160+
*
161+
* @cputime: The struct to compare.
162+
*
163+
* Checks @cputime to see if all fields are zero. Returns true if all fields
164+
* are zero, false if any field is nonzero.
165+
*/
166+
static inline int task_cputime_zero(const struct task_cputime *cputime)
167+
{
168+
if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
169+
return 1;
170+
return 0;
171+
}
172+
156173
static inline cputime_t prof_ticks(struct task_struct *p)
157174
{
158175
cputime_t utime, stime;
@@ -636,6 +653,37 @@ static int cpu_timer_sample_group(const clockid_t which_clock,
636653
return 0;
637654
}
638655

656+
#ifdef CONFIG_NO_HZ_FULL
657+
static void nohz_kick_work_fn(struct work_struct *work)
658+
{
659+
tick_nohz_full_kick_all();
660+
}
661+
662+
static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn);
663+
664+
/*
665+
* We need the IPIs to be sent from sane process context.
666+
* The posix cpu timers are always set with irqs disabled.
667+
*/
668+
static void posix_cpu_timer_kick_nohz(void)
669+
{
670+
schedule_work(&nohz_kick_work);
671+
}
672+
673+
bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk)
674+
{
675+
if (!task_cputime_zero(&tsk->cputime_expires))
676+
return true;
677+
678+
if (tsk->signal->cputimer.running)
679+
return true;
680+
681+
return false;
682+
}
683+
#else
684+
static inline void posix_cpu_timer_kick_nohz(void) { }
685+
#endif
686+
639687
/*
640688
* Guts of sys_timer_settime for CPU timers.
641689
* This is called with the timer locked and interrupts disabled.
@@ -794,6 +842,8 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
794842
sample_to_timespec(timer->it_clock,
795843
old_incr, &old->it_interval);
796844
}
845+
if (!ret)
846+
posix_cpu_timer_kick_nohz();
797847
return ret;
798848
}
799849

@@ -1008,21 +1058,6 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
10081058
}
10091059
}
10101060

1011-
/**
1012-
* task_cputime_zero - Check a task_cputime struct for all zero fields.
1013-
*
1014-
* @cputime: The struct to compare.
1015-
*
1016-
* Checks @cputime to see if all fields are zero. Returns true if all fields
1017-
* are zero, false if any field is nonzero.
1018-
*/
1019-
static inline int task_cputime_zero(const struct task_cputime *cputime)
1020-
{
1021-
if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
1022-
return 1;
1023-
return 0;
1024-
}
1025-
10261061
/*
10271062
* Check for any per-thread CPU timers that have fired and move them
10281063
* off the tsk->*_timers list onto the firing list. Per-thread timers
@@ -1336,6 +1371,13 @@ void run_posix_cpu_timers(struct task_struct *tsk)
13361371
cpu_timer_fire(timer);
13371372
spin_unlock(&timer->it_lock);
13381373
}
1374+
1375+
/*
1376+
* In case some timers were rescheduled after the queue got emptied,
1377+
* wake up full dynticks CPUs.
1378+
*/
1379+
if (tsk->signal->cputimer.running)
1380+
posix_cpu_timer_kick_nohz();
13391381
}
13401382

13411383
/*
@@ -1366,7 +1408,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
13661408
}
13671409

13681410
if (!*newval)
1369-
return;
1411+
goto out;
13701412
*newval += now.cpu;
13711413
}
13721414

@@ -1384,6 +1426,8 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
13841426
tsk->signal->cputime_expires.virt_exp = *newval;
13851427
break;
13861428
}
1429+
out:
1430+
posix_cpu_timer_kick_nohz();
13871431
}
13881432

13891433
static int do_cpu_nanosleep(const clockid_t which_clock, int flags,

‎kernel/time/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ config NO_HZ_FULL
111111
select RCU_USER_QS
112112
select RCU_NOCB_CPU
113113
select CONTEXT_TRACKING_FORCE
114+
select IRQ_WORK
114115
help
115116
Adaptively try to shutdown the tick whenever possible, even when
116117
the CPU is running tasks. Typically this requires running a single

‎kernel/time/tick-sched.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,57 @@ static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
147147
static cpumask_var_t nohz_full_mask;
148148
bool have_nohz_full_mask;
149149

150+
/*
151+
* Re-evaluate the need for the tick on the current CPU
152+
* and restart it if necessary.
153+
*/
154+
static void tick_nohz_full_check(void)
155+
{
156+
/*
157+
* STUB for now, will be filled with the full tick stop/restart
158+
* infrastructure patches
159+
*/
160+
}
161+
162+
static void nohz_full_kick_work_func(struct irq_work *work)
163+
{
164+
tick_nohz_full_check();
165+
}
166+
167+
static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
168+
.func = nohz_full_kick_work_func,
169+
};
170+
171+
/*
172+
* Kick the current CPU if it's full dynticks in order to force it to
173+
* re-evaluate its dependency on the tick and restart it if necessary.
174+
*/
175+
void tick_nohz_full_kick(void)
176+
{
177+
if (tick_nohz_full_cpu(smp_processor_id()))
178+
irq_work_queue(&__get_cpu_var(nohz_full_kick_work));
179+
}
180+
181+
static void nohz_full_kick_ipi(void *info)
182+
{
183+
tick_nohz_full_check();
184+
}
185+
186+
/*
187+
* Kick all full dynticks CPUs in order to force these to re-evaluate
188+
* their dependency on the tick and restart it if necessary.
189+
*/
190+
void tick_nohz_full_kick_all(void)
191+
{
192+
if (!have_nohz_full_mask)
193+
return;
194+
195+
preempt_disable();
196+
smp_call_function_many(nohz_full_mask,
197+
nohz_full_kick_ipi, NULL, false);
198+
preempt_enable();
199+
}
200+
150201
int tick_nohz_full_cpu(int cpu)
151202
{
152203
if (!have_nohz_full_mask)

0 commit comments

Comments
 (0)