Skip to content

Commit 2ff678b

Browse files
KAGA-KOKOLinus Torvalds
authored andcommitted
[PATCH] hrtimer: switch itimers to hrtimer
switch itimers to a hrtimers-based implementation Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent df78488 commit 2ff678b

7 files changed

Lines changed: 65 additions & 68 deletions

File tree

‎fs/exec.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,10 @@ static inline int de_thread(struct task_struct *tsk)
632632
* synchronize with any firing (by calling del_timer_sync)
633633
* before we can safely let the old group leader die.
634634
*/
635-
sig->real_timer.data = (unsigned long)current;
635+
sig->real_timer.data = current;
636636
spin_unlock_irq(lock);
637-
if (del_timer_sync(&sig->real_timer))
638-
add_timer(&sig->real_timer);
637+
if (hrtimer_cancel(&sig->real_timer))
638+
hrtimer_restart(&sig->real_timer);
639639
spin_lock_irq(lock);
640640
}
641641
while (atomic_read(&sig->count) > count) {

‎fs/proc/array.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
330330
unsigned long min_flt = 0, maj_flt = 0;
331331
cputime_t cutime, cstime, utime, stime;
332332
unsigned long rsslim = 0;
333-
unsigned long it_real_value = 0;
333+
DEFINE_KTIME(it_real_value);
334334
struct task_struct *t;
335335
char tcomm[sizeof(task->comm)];
336336

@@ -386,7 +386,7 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
386386
utime = cputime_add(utime, task->signal->utime);
387387
stime = cputime_add(stime, task->signal->stime);
388388
}
389-
it_real_value = task->signal->it_real_value;
389+
it_real_value = task->signal->real_timer.expires;
390390
}
391391
ppid = pid_alive(task) ? task->group_leader->real_parent->tgid : 0;
392392
read_unlock(&tasklist_lock);
@@ -435,7 +435,7 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
435435
priority,
436436
nice,
437437
num_threads,
438-
jiffies_to_clock_t(it_real_value),
438+
(long) ktime_to_clock_t(it_real_value),
439439
start_time,
440440
vsize,
441441
mm ? get_mm_rss(mm) : 0,

‎include/linux/sched.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extern unsigned long nr_iowait(void);
105105
#include <linux/param.h>
106106
#include <linux/resource.h>
107107
#include <linux/timer.h>
108+
#include <linux/hrtimer.h>
108109

109110
#include <asm/processor.h>
110111

@@ -398,8 +399,8 @@ struct signal_struct {
398399
struct list_head posix_timers;
399400

400401
/* ITIMER_REAL timer for the process */
401-
struct timer_list real_timer;
402-
unsigned long it_real_value, it_real_incr;
402+
struct hrtimer real_timer;
403+
ktime_t it_real_incr;
403404

404405
/* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */
405406
cputime_t it_prof_expires, it_virt_expires;

‎include/linux/timer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ static inline void add_timer(struct timer_list *timer)
9696

9797
extern void init_timers(void);
9898
extern void run_local_timers(void);
99-
extern void it_real_fn(unsigned long);
99+
extern int it_real_fn(void *);
100100

101101
#endif

‎kernel/exit.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ fastcall NORET_TYPE void do_exit(long code)
842842
}
843843
group_dead = atomic_dec_and_test(&tsk->signal->live);
844844
if (group_dead) {
845-
del_timer_sync(&tsk->signal->real_timer);
845+
hrtimer_cancel(&tsk->signal->real_timer);
846846
exit_itimers(tsk->signal);
847847
acct_process(code);
848848
}

‎kernel/fork.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,10 +801,10 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
801801
init_sigpending(&sig->shared_pending);
802802
INIT_LIST_HEAD(&sig->posix_timers);
803803

804-
sig->it_real_value = sig->it_real_incr = 0;
804+
hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC);
805+
sig->it_real_incr.tv64 = 0;
805806
sig->real_timer.function = it_real_fn;
806-
sig->real_timer.data = (unsigned long) tsk;
807-
init_timer(&sig->real_timer);
807+
sig->real_timer.data = tsk;
808808

809809
sig->it_virt_expires = cputime_zero;
810810
sig->it_virt_incr = cputime_zero;

‎kernel/itimer.c‎

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,46 @@
1212
#include <linux/syscalls.h>
1313
#include <linux/time.h>
1414
#include <linux/posix-timers.h>
15+
#include <linux/hrtimer.h>
1516

1617
#include <asm/uaccess.h>
1718

18-
static unsigned long it_real_value(struct signal_struct *sig)
19+
/**
20+
* itimer_get_remtime - get remaining time for the timer
21+
*
22+
* @timer: the timer to read
23+
*
24+
* Returns the delta between the expiry time and now, which can be
25+
* less than zero or 1usec for an pending expired timer
26+
*/
27+
static struct timeval itimer_get_remtime(struct hrtimer *timer)
1928
{
20-
unsigned long val = 0;
21-
if (timer_pending(&sig->real_timer)) {
22-
val = sig->real_timer.expires - jiffies;
29+
ktime_t rem = hrtimer_get_remaining(timer);
2330

24-
/* look out for negative/zero itimer.. */
25-
if ((long) val <= 0)
26-
val = 1;
27-
}
28-
return val;
31+
/*
32+
* Racy but safe: if the itimer expires after the above
33+
* hrtimer_get_remtime() call but before this condition
34+
* then we return 0 - which is correct.
35+
*/
36+
if (hrtimer_active(timer)) {
37+
if (rem.tv64 <= 0)
38+
rem.tv64 = NSEC_PER_USEC;
39+
} else
40+
rem.tv64 = 0;
41+
42+
return ktime_to_timeval(rem);
2943
}
3044

3145
int do_getitimer(int which, struct itimerval *value)
3246
{
3347
struct task_struct *tsk = current;
34-
unsigned long interval, val;
3548
cputime_t cinterval, cval;
3649

3750
switch (which) {
3851
case ITIMER_REAL:
39-
spin_lock_irq(&tsk->sighand->siglock);
40-
interval = tsk->signal->it_real_incr;
41-
val = it_real_value(tsk->signal);
42-
spin_unlock_irq(&tsk->sighand->siglock);
43-
jiffies_to_timeval(val, &value->it_value);
44-
jiffies_to_timeval(interval, &value->it_interval);
52+
value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
53+
value->it_interval =
54+
ktime_to_timeval(tsk->signal->it_real_incr);
4555
break;
4656
case ITIMER_VIRTUAL:
4757
read_lock(&tasklist_lock);
@@ -113,59 +123,45 @@ asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
113123
}
114124

115125

116-
void it_real_fn(unsigned long __data)
126+
/*
127+
* The timer is automagically restarted, when interval != 0
128+
*/
129+
int it_real_fn(void *data)
117130
{
118-
struct task_struct * p = (struct task_struct *) __data;
119-
unsigned long inc = p->signal->it_real_incr;
131+
struct task_struct *tsk = (struct task_struct *) data;
120132

121-
send_group_sig_info(SIGALRM, SEND_SIG_PRIV, p);
133+
send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
122134

123-
/*
124-
* Now restart the timer if necessary. We don't need any locking
125-
* here because do_setitimer makes sure we have finished running
126-
* before it touches anything.
127-
* Note, we KNOW we are (or should be) at a jiffie edge here so
128-
* we don't need the +1 stuff. Also, we want to use the prior
129-
* expire value so as to not "slip" a jiffie if we are late.
130-
* Deal with requesting a time prior to "now" here rather than
131-
* in add_timer.
132-
*/
133-
if (!inc)
134-
return;
135-
while (time_before_eq(p->signal->real_timer.expires, jiffies))
136-
p->signal->real_timer.expires += inc;
137-
add_timer(&p->signal->real_timer);
135+
if (tsk->signal->it_real_incr.tv64 != 0) {
136+
hrtimer_forward(&tsk->signal->real_timer,
137+
tsk->signal->it_real_incr);
138+
139+
return HRTIMER_RESTART;
140+
}
141+
return HRTIMER_NORESTART;
138142
}
139143

140144
int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
141145
{
142146
struct task_struct *tsk = current;
143-
unsigned long val, interval, expires;
147+
struct hrtimer *timer;
148+
ktime_t expires;
144149
cputime_t cval, cinterval, nval, ninterval;
145150

146151
switch (which) {
147152
case ITIMER_REAL:
148-
again:
149-
spin_lock_irq(&tsk->sighand->siglock);
150-
interval = tsk->signal->it_real_incr;
151-
val = it_real_value(tsk->signal);
152-
/* We are sharing ->siglock with it_real_fn() */
153-
if (try_to_del_timer_sync(&tsk->signal->real_timer) < 0) {
154-
spin_unlock_irq(&tsk->sighand->siglock);
155-
goto again;
156-
}
157-
tsk->signal->it_real_incr =
158-
timeval_to_jiffies(&value->it_interval);
159-
expires = timeval_to_jiffies(&value->it_value);
160-
if (expires)
161-
mod_timer(&tsk->signal->real_timer,
162-
jiffies + 1 + expires);
163-
spin_unlock_irq(&tsk->sighand->siglock);
153+
timer = &tsk->signal->real_timer;
154+
hrtimer_cancel(timer);
164155
if (ovalue) {
165-
jiffies_to_timeval(val, &ovalue->it_value);
166-
jiffies_to_timeval(interval,
167-
&ovalue->it_interval);
156+
ovalue->it_value = itimer_get_remtime(timer);
157+
ovalue->it_interval
158+
= ktime_to_timeval(tsk->signal->it_real_incr);
168159
}
160+
tsk->signal->it_real_incr =
161+
timeval_to_ktime(value->it_interval);
162+
expires = timeval_to_ktime(value->it_value);
163+
if (expires.tv64 != 0)
164+
hrtimer_start(timer, expires, HRTIMER_REL);
169165
break;
170166
case ITIMER_VIRTUAL:
171167
nval = timeval_to_cputime(&value->it_value);

0 commit comments

Comments
 (0)