Skip to content

Commit ab8177b

Browse files
committed
hrtimers: Avoid touching inactive timer bases
Instead of iterating over all possible timer bases avoid it by marking the active bases in the cpu base. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Peter Zijlstra <peterz@infradead.org>
1 parent f24444b commit ab8177b

6 files changed

Lines changed: 29 additions & 19 deletions

File tree

‎include/linux/hrtimer.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ struct hrtimer_sleeper {
143143
*/
144144
struct hrtimer_clock_base {
145145
struct hrtimer_cpu_base *cpu_base;
146-
clockid_t index;
146+
int index;
147+
clockid_t clockid;
147148
struct timerqueue_head active;
148149
ktime_t resolution;
149150
ktime_t (*get_time)(void);
@@ -162,7 +163,7 @@ enum hrtimer_base_type {
162163
* struct hrtimer_cpu_base - the per cpu clock bases
163164
* @lock: lock protecting the base and associated clock bases
164165
* and timers
165-
* @clock_base: array of clock bases for this cpu
166+
* @active_bases: Bitfield to mark bases with active timers
166167
* @expires_next: absolute time of the next event which was scheduled
167168
* via clock_set_next_event()
168169
* @hres_active: State of high resolution mode
@@ -171,9 +172,11 @@ enum hrtimer_base_type {
171172
* @nr_retries: Total number of hrtimer interrupt retries
172173
* @nr_hangs: Total number of hrtimer interrupt hangs
173174
* @max_hang_time: Maximum time spent in hrtimer_interrupt
175+
* @clock_base: array of clock bases for this cpu
174176
*/
175177
struct hrtimer_cpu_base {
176178
raw_spinlock_t lock;
179+
unsigned long active_bases;
177180
#ifdef CONFIG_HIGH_RES_TIMERS
178181
ktime_t expires_next;
179182
int hres_active;

‎include/linux/thread_info.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct restart_block {
2929
} futex;
3030
/* For nanosleep */
3131
struct {
32-
clockid_t index;
32+
clockid_t clockid;
3333
struct timespec __user *rmtp;
3434
#ifdef CONFIG_COMPAT
3535
struct compat_timespec __user *compat_rmtp;

‎kernel/hrtimer.c‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
6464
.clock_base =
6565
{
6666
{
67-
.index = CLOCK_REALTIME,
67+
.index = HRTIMER_BASE_REALTIME,
68+
.clockid = CLOCK_REALTIME,
6869
.get_time = &ktime_get_real,
6970
.resolution = KTIME_LOW_RES,
7071
},
7172
{
72-
.index = CLOCK_MONOTONIC,
73+
.index = HRTIMER_BASE_MONOTONIC,
74+
.clockid = CLOCK_MONOTONIC,
7375
.get_time = &ktime_get,
7476
.resolution = KTIME_LOW_RES,
7577
},
7678
{
77-
.index = CLOCK_BOOTTIME,
79+
.index = HRTIMER_BASE_BOOTTIME,
80+
.clockid = CLOCK_BOOTTIME,
7881
.get_time = &ktime_get_boottime,
7982
.resolution = KTIME_LOW_RES,
8083
},
@@ -196,7 +199,7 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
196199
struct hrtimer_cpu_base *new_cpu_base;
197200
int this_cpu = smp_processor_id();
198201
int cpu = hrtimer_get_target(this_cpu, pinned);
199-
int basenum = hrtimer_clockid_to_base(base->index);
202+
int basenum = base->index;
200203

201204
again:
202205
new_cpu_base = &per_cpu(hrtimer_bases, cpu);
@@ -857,6 +860,7 @@ static int enqueue_hrtimer(struct hrtimer *timer,
857860
debug_activate(timer);
858861

859862
timerqueue_add(&base->active, &timer->node);
863+
base->cpu_base->active_bases |= 1 << base->index;
860864

861865
/*
862866
* HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
@@ -898,6 +902,8 @@ static void __remove_hrtimer(struct hrtimer *timer,
898902
#endif
899903
}
900904
timerqueue_del(&base->active, &timer->node);
905+
if (!timerqueue_getnext(&base->active))
906+
base->cpu_base->active_bases &= ~(1 << base->index);
901907
out:
902908
timer->state = newstate;
903909
}
@@ -1235,7 +1241,6 @@ static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
12351241
void hrtimer_interrupt(struct clock_event_device *dev)
12361242
{
12371243
struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1238-
struct hrtimer_clock_base *base;
12391244
ktime_t expires_next, now, entry_time, delta;
12401245
int i, retries = 0;
12411246

@@ -1257,12 +1262,15 @@ void hrtimer_interrupt(struct clock_event_device *dev)
12571262
*/
12581263
cpu_base->expires_next.tv64 = KTIME_MAX;
12591264

1260-
base = cpu_base->clock_base;
1261-
12621265
for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1263-
ktime_t basenow;
1266+
struct hrtimer_clock_base *base;
12641267
struct timerqueue_node *node;
1268+
ktime_t basenow;
1269+
1270+
if (!(cpu_base->active_bases & (1 << i)))
1271+
continue;
12651272

1273+
base = cpu_base->clock_base + i;
12661274
basenow = ktime_add(now, base->offset);
12671275

12681276
while ((node = timerqueue_getnext(&base->active))) {
@@ -1295,7 +1303,6 @@ void hrtimer_interrupt(struct clock_event_device *dev)
12951303

12961304
__run_hrtimer(timer, &basenow);
12971305
}
1298-
base++;
12991306
}
13001307

13011308
/*
@@ -1526,7 +1533,7 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
15261533
struct timespec __user *rmtp;
15271534
int ret = 0;
15281535

1529-
hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1536+
hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
15301537
HRTIMER_MODE_ABS);
15311538
hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
15321539

@@ -1578,7 +1585,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
15781585

15791586
restart = &current_thread_info()->restart_block;
15801587
restart->fn = hrtimer_nanosleep_restart;
1581-
restart->nanosleep.index = t.timer.base->index;
1588+
restart->nanosleep.clockid = t.timer.base->clockid;
15821589
restart->nanosleep.rmtp = rmtp;
15831590
restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
15841591

‎kernel/posix-cpu-timers.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
15141514
return -EFAULT;
15151515

15161516
restart_block->fn = posix_cpu_nsleep_restart;
1517-
restart_block->nanosleep.index = which_clock;
1517+
restart_block->nanosleep.clockid = which_clock;
15181518
restart_block->nanosleep.rmtp = rmtp;
15191519
restart_block->nanosleep.expires = timespec_to_ns(rqtp);
15201520
}
@@ -1523,7 +1523,7 @@ static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
15231523

15241524
static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
15251525
{
1526-
clockid_t which_clock = restart_block->nanosleep.index;
1526+
clockid_t which_clock = restart_block->nanosleep.clockid;
15271527
struct timespec t;
15281528
struct itimerspec it;
15291529
int error;

‎kernel/posix-timers.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
10561056
*/
10571057
long clock_nanosleep_restart(struct restart_block *restart_block)
10581058
{
1059-
clockid_t which_clock = restart_block->nanosleep.index;
1059+
clockid_t which_clock = restart_block->nanosleep.clockid;
10601060
struct k_clock *kc = clockid_to_kclock(which_clock);
10611061

10621062
if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))

‎kernel/time/alarmtimer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
494494
*/
495495
static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
496496
{
497-
enum alarmtimer_type type = restart->nanosleep.index;
497+
enum alarmtimer_type type = restart->nanosleep.clockid;
498498
ktime_t exp;
499499
struct timespec __user *rmtp;
500500
struct alarm alarm;
@@ -573,7 +573,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
573573

574574
restart = &current_thread_info()->restart_block;
575575
restart->fn = alarm_timer_nsleep_restart;
576-
restart->nanosleep.index = type;
576+
restart->nanosleep.clockid = type;
577577
restart->nanosleep.expires = exp.tv64;
578578
restart->nanosleep.rmtp = rmtp;
579579
ret = -ERESTART_RESTARTBLOCK;

0 commit comments

Comments
 (0)