Skip to content

Commit 54cdfdb

Browse files
KAGA-KOKOLinus Torvalds
authored andcommitted
[PATCH] hrtimers: add high resolution timer support
Implement high resolution timers on top of the hrtimers infrastructure and the clockevents / tick-management framework. This provides accurate timers for all hrtimer subsystem users. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@elte.hu> Cc: john stultz <johnstul@us.ibm.com> Cc: Roman Zippel <zippel@linux-m68k.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d40891e commit 54cdfdb

8 files changed

Lines changed: 650 additions & 58 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ and is between 256 and 4096 characters. It is defined in the file
609609
highmem otherwise. This also works to reduce highmem
610610
size on bigger boxes.
611611

612+
highres= [KNL] Enable/disable high resolution timer mode.
613+
Valid parameters: "on", "off"
614+
Default: "on"
615+
612616
hisax= [HW,ISDN]
613617
See Documentation/isdn/README.HiSax.
614618

‎include/linux/hrtimer.h‎

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,35 @@ enum hrtimer_restart {
4141
};
4242

4343
/*
44-
* Bit values to track state of the timer
44+
* hrtimer callback modes:
45+
*
46+
* HRTIMER_CB_SOFTIRQ: Callback must run in softirq context
47+
* HRTIMER_CB_IRQSAFE: Callback may run in hardirq context
48+
* HRTIMER_CB_IRQSAFE_NO_RESTART: Callback may run in hardirq context and
49+
* does not restart the timer
50+
* HRTIMER_CB_IRQSAFE_NO_SOFTIRQ: Callback must run in softirq context
51+
* Special mode for tick emultation
52+
*/
53+
enum hrtimer_cb_mode {
54+
HRTIMER_CB_SOFTIRQ,
55+
HRTIMER_CB_IRQSAFE,
56+
HRTIMER_CB_IRQSAFE_NO_RESTART,
57+
HRTIMER_CB_IRQSAFE_NO_SOFTIRQ,
58+
};
59+
60+
/*
61+
* Values to track state of the timer
4562
*
4663
* Possible states:
4764
*
4865
* 0x00 inactive
4966
* 0x01 enqueued into rbtree
5067
* 0x02 callback function running
68+
* 0x04 callback pending (high resolution mode)
69+
*
70+
* Special case:
5171
* 0x03 callback function running and enqueued
5272
* (was requeued on another CPU)
53-
*
5473
* The "callback function running and enqueued" status is only possible on
5574
* SMP. It happens for example when a posix timer expired and the callback
5675
* queued a signal. Between dropping the lock which protects the posix timer
@@ -67,6 +86,7 @@ enum hrtimer_restart {
6786
#define HRTIMER_STATE_INACTIVE 0x00
6887
#define HRTIMER_STATE_ENQUEUED 0x01
6988
#define HRTIMER_STATE_CALLBACK 0x02
89+
#define HRTIMER_STATE_PENDING 0x04
7090

7191
/**
7292
* struct hrtimer - the basic hrtimer structure
@@ -77,15 +97,28 @@ enum hrtimer_restart {
7797
* @function: timer expiry callback function
7898
* @base: pointer to the timer base (per cpu and per clock)
7999
* @state: state information (See bit values above)
100+
* @cb_mode: high resolution timer feature to select the callback execution
101+
* mode
102+
* @cb_entry: list head to enqueue an expired timer into the callback list
103+
* @start_site: timer statistics field to store the site where the timer
104+
* was started
105+
* @start_comm: timer statistics field to store the name of the process which
106+
* started the timer
107+
* @start_pid: timer statistics field to store the pid of the task which
108+
* started the timer
80109
*
81-
* The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
110+
* The hrtimer structure must be initialized by hrtimer_init()
82111
*/
83112
struct hrtimer {
84113
struct rb_node node;
85114
ktime_t expires;
86115
enum hrtimer_restart (*function)(struct hrtimer *);
87116
struct hrtimer_clock_base *base;
88117
unsigned long state;
118+
#ifdef CONFIG_HIGH_RES_TIMERS
119+
enum hrtimer_cb_mode cb_mode;
120+
struct list_head cb_entry;
121+
#endif
89122
};
90123

91124
/**
@@ -110,6 +143,9 @@ struct hrtimer_sleeper {
110143
* @get_time: function to retrieve the current time of the clock
111144
* @get_softirq_time: function to retrieve the current time from the softirq
112145
* @softirq_time: the time when running the hrtimer queue in the softirq
146+
* @cb_pending: list of timers where the callback is pending
147+
* @offset: offset of this clock to the monotonic base
148+
* @reprogram: function to reprogram the timer event
113149
*/
114150
struct hrtimer_clock_base {
115151
struct hrtimer_cpu_base *cpu_base;
@@ -120,6 +156,12 @@ struct hrtimer_clock_base {
120156
ktime_t (*get_time)(void);
121157
ktime_t (*get_softirq_time)(void);
122158
ktime_t softirq_time;
159+
#ifdef CONFIG_HIGH_RES_TIMERS
160+
ktime_t offset;
161+
int (*reprogram)(struct hrtimer *t,
162+
struct hrtimer_clock_base *b,
163+
ktime_t n);
164+
#endif
123165
};
124166

125167
#define HRTIMER_MAX_CLOCK_BASES 2
@@ -131,19 +173,74 @@ struct hrtimer_clock_base {
131173
* @lock_key: the lock_class_key for use with lockdep
132174
* @clock_base: array of clock bases for this cpu
133175
* @curr_timer: the timer which is executing a callback right now
176+
* @expires_next: absolute time of the next event which was scheduled
177+
* via clock_set_next_event()
178+
* @hres_active: State of high resolution mode
179+
* @check_clocks: Indictator, when set evaluate time source and clock
180+
* event devices whether high resolution mode can be
181+
* activated.
182+
* @cb_pending: Expired timers are moved from the rbtree to this
183+
* list in the timer interrupt. The list is processed
184+
* in the softirq.
185+
* @nr_events: Total number of timer interrupt events
134186
*/
135187
struct hrtimer_cpu_base {
136188
spinlock_t lock;
137189
struct lock_class_key lock_key;
138190
struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
191+
#ifdef CONFIG_HIGH_RES_TIMERS
192+
ktime_t expires_next;
193+
int hres_active;
194+
struct list_head cb_pending;
195+
unsigned long nr_events;
196+
#endif
139197
};
140198

199+
#ifdef CONFIG_HIGH_RES_TIMERS
200+
struct clock_event_device;
201+
202+
extern void clock_was_set(void);
203+
extern void hrtimer_interrupt(struct clock_event_device *dev);
204+
205+
/*
206+
* In high resolution mode the time reference must be read accurate
207+
*/
208+
static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
209+
{
210+
return timer->base->get_time();
211+
}
212+
213+
/*
214+
* The resolution of the clocks. The resolution value is returned in
215+
* the clock_getres() system call to give application programmers an
216+
* idea of the (in)accuracy of timers. Timer values are rounded up to
217+
* this resolution values.
218+
*/
219+
# define KTIME_HIGH_RES (ktime_t) { .tv64 = 1 }
220+
# define KTIME_MONOTONIC_RES KTIME_HIGH_RES
221+
222+
#else
223+
224+
# define KTIME_MONOTONIC_RES KTIME_LOW_RES
225+
141226
/*
142227
* clock_was_set() is a NOP for non- high-resolution systems. The
143228
* time-sorted order guarantees that a timer does not expire early and
144229
* is expired in the next softirq when the clock was advanced.
145230
*/
146-
#define clock_was_set() do { } while (0)
231+
static inline void clock_was_set(void) { }
232+
233+
/*
234+
* In non high resolution mode the time reference is taken from
235+
* the base softirq time variable.
236+
*/
237+
static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
238+
{
239+
return timer->base->softirq_time;
240+
}
241+
242+
#endif
243+
147244
extern ktime_t ktime_get(void);
148245
extern ktime_t ktime_get_real(void);
149246

@@ -168,9 +265,7 @@ static inline int hrtimer_restart(struct hrtimer *timer)
168265
extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
169266
extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
170267

171-
#ifdef CONFIG_NO_IDLE_HZ
172268
extern ktime_t hrtimer_get_next_event(void);
173-
#endif
174269

175270
/*
176271
* A timer is active, when it is enqueued into the rbtree or the callback
@@ -181,6 +276,15 @@ static inline int hrtimer_active(const struct hrtimer *timer)
181276
return timer->state != HRTIMER_STATE_INACTIVE;
182277
}
183278

279+
/*
280+
* Helper function to check, whether the timer is on one of the queues
281+
*/
282+
static inline int hrtimer_is_queued(struct hrtimer *timer)
283+
{
284+
return timer->state &
285+
(HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
286+
}
287+
184288
/* Forward a hrtimer so it expires after now: */
185289
extern unsigned long
186290
hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);

‎include/linux/interrupt.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ enum
242242
BLOCK_SOFTIRQ,
243243
TASKLET_SOFTIRQ,
244244
SCHED_SOFTIRQ,
245+
#ifdef CONFIG_HIGH_RES_TIMERS
246+
HRTIMER_SOFTIRQ,
247+
#endif
245248
};
246249

247250
/* softirq mask and active fields moved to irq_cpustat_t in

‎include/linux/ktime.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ static inline s64 ktime_to_ns(const ktime_t kt)
261261
* idea of the (in)accuracy of timers. Timer values are rounded up to
262262
* this resolution values.
263263
*/
264-
#define KTIME_REALTIME_RES (ktime_t){ .tv64 = TICK_NSEC }
265-
#define KTIME_MONOTONIC_RES (ktime_t){ .tv64 = TICK_NSEC }
264+
#define KTIME_LOW_RES (ktime_t){ .tv64 = TICK_NSEC }
266265

267266
/* Get the monotonic time in timespec format: */
268267
extern void ktime_get_ts(struct timespec *ts);

0 commit comments

Comments
 (0)