Skip to content

Commit f1b8274

Browse files
Martin SchwidefskyKAGA-KOKO
authored andcommitted
clocksource: Cleanup clocksource selection
If a non high-resolution clocksource is first set as override clock and then registered it becomes active even if the system is in one-shot mode. Move the override check from sysfs_override_clocksource to the clocksource selection. That fixes the bug and simplifies the code. The check in clocksource_register for double registration of the same clocksource is removed without replacement. To find the initial clocksource a new weak function in jiffies.c is defined that returns the jiffies clocksource. The architecture code can then override the weak function with a more suitable clocksource, e.g. the TOD clock on s390. [ tglx: Folded in a fix from John Stultz ] Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Acked-by: John Stultz <johnstul@us.ibm.com> Cc: Daniel Walker <dwalker@fifo99.com> LKML-Reference: <20090814134808.388024160@de.ibm.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 1be3967 commit f1b8274

5 files changed

Lines changed: 64 additions & 86 deletions

File tree

‎arch/s390/kernel/time.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ static struct clocksource clocksource_tod = {
205205
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
206206
};
207207

208+
struct clocksource * __init clocksource_default_clock(void)
209+
{
210+
return &clocksource_tod;
211+
}
208212

209213
void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
210214
{

‎include/linux/clocksource.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/list.h>
1515
#include <linux/cache.h>
1616
#include <linux/timer.h>
17+
#include <linux/init.h>
1718
#include <asm/div64.h>
1819
#include <asm/io.h>
1920

@@ -322,6 +323,7 @@ extern void clocksource_touch_watchdog(void);
322323
extern struct clocksource* clocksource_get_next(void);
323324
extern void clocksource_change_rating(struct clocksource *cs, int rating);
324325
extern void clocksource_resume(void);
326+
extern struct clocksource * __init __weak clocksource_default_clock(void);
325327

326328
#ifdef CONFIG_GENERIC_TIME_VSYSCALL
327329
extern void update_vsyscall(struct timespec *ts, struct clocksource *c);

‎kernel/time/clocksource.c‎

Lines changed: 51 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
*
2222
* TODO WishList:
2323
* o Allow clocksource drivers to be unregistered
24-
* o get rid of clocksource_jiffies extern
2524
*/
2625

2726
#include <linux/clocksource.h>
@@ -107,12 +106,9 @@ u64 timecounter_cyc2time(struct timecounter *tc,
107106
}
108107
EXPORT_SYMBOL(timecounter_cyc2time);
109108

110-
/* XXX - Would like a better way for initializing curr_clocksource */
111-
extern struct clocksource clocksource_jiffies;
112-
113109
/*[Clocksource internal variables]---------
114110
* curr_clocksource:
115-
* currently selected clocksource. Initialized to clocksource_jiffies.
111+
* currently selected clocksource.
116112
* next_clocksource:
117113
* pending next selected clocksource.
118114
* clocksource_list:
@@ -123,9 +119,8 @@ extern struct clocksource clocksource_jiffies;
123119
* override_name:
124120
* Name of the user-specified clocksource.
125121
*/
126-
static struct clocksource *curr_clocksource = &clocksource_jiffies;
122+
static struct clocksource *curr_clocksource;
127123
static struct clocksource *next_clocksource;
128-
static struct clocksource *clocksource_override;
129124
static LIST_HEAD(clocksource_list);
130125
static DEFINE_SPINLOCK(clocksource_lock);
131126
static char override_name[32];
@@ -320,6 +315,7 @@ void clocksource_touch_watchdog(void)
320315
clocksource_resume_watchdog();
321316
}
322317

318+
#ifdef CONFIG_GENERIC_TIME
323319
/**
324320
* clocksource_get_next - Returns the selected clocksource
325321
*
@@ -339,56 +335,65 @@ struct clocksource *clocksource_get_next(void)
339335
}
340336

341337
/**
342-
* select_clocksource - Selects the best registered clocksource.
338+
* clocksource_select - Select the best clocksource available
343339
*
344340
* Private function. Must hold clocksource_lock when called.
345341
*
346342
* Select the clocksource with the best rating, or the clocksource,
347343
* which is selected by userspace override.
348344
*/
349-
static struct clocksource *select_clocksource(void)
345+
static void clocksource_select(void)
350346
{
351-
struct clocksource *next;
347+
struct clocksource *best, *cs;
352348

353349
if (list_empty(&clocksource_list))
354-
return NULL;
350+
return;
351+
/* First clocksource on the list has the best rating. */
352+
best = list_first_entry(&clocksource_list, struct clocksource, list);
353+
/* Check for the override clocksource. */
354+
list_for_each_entry(cs, &clocksource_list, list) {
355+
if (strcmp(cs->name, override_name) != 0)
356+
continue;
357+
/*
358+
* Check to make sure we don't switch to a non-highres
359+
* capable clocksource if the tick code is in oneshot
360+
* mode (highres or nohz)
361+
*/
362+
if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
363+
tick_oneshot_mode_active()) {
364+
/* Override clocksource cannot be used. */
365+
printk(KERN_WARNING "Override clocksource %s is not "
366+
"HRT compatible. Cannot switch while in "
367+
"HRT/NOHZ mode\n", cs->name);
368+
override_name[0] = 0;
369+
} else
370+
/* Override clocksource can be used. */
371+
best = cs;
372+
break;
373+
}
374+
if (curr_clocksource != best)
375+
next_clocksource = best;
376+
}
355377

356-
if (clocksource_override)
357-
next = clocksource_override;
358-
else
359-
next = list_entry(clocksource_list.next, struct clocksource,
360-
list);
378+
#else /* CONFIG_GENERIC_TIME */
361379

362-
if (next == curr_clocksource)
363-
return NULL;
380+
static void clocksource_select(void) { }
364381

365-
return next;
366-
}
382+
#endif
367383

368384
/*
369385
* Enqueue the clocksource sorted by rating
370386
*/
371-
static int clocksource_enqueue(struct clocksource *c)
387+
static void clocksource_enqueue(struct clocksource *cs)
372388
{
373-
struct list_head *tmp, *entry = &clocksource_list;
374-
375-
list_for_each(tmp, &clocksource_list) {
376-
struct clocksource *cs;
389+
struct list_head *entry = &clocksource_list;
390+
struct clocksource *tmp;
377391

378-
cs = list_entry(tmp, struct clocksource, list);
379-
if (cs == c)
380-
return -EBUSY;
392+
list_for_each_entry(tmp, &clocksource_list, list)
381393
/* Keep track of the place, where to insert */
382-
if (cs->rating >= c->rating)
383-
entry = tmp;
384-
}
385-
list_add(&c->list, entry);
386-
387-
if (strlen(c->name) == strlen(override_name) &&
388-
!strcmp(c->name, override_name))
389-
clocksource_override = c;
390-
391-
return 0;
394+
if (tmp->rating >= cs->rating)
395+
entry = &tmp->list;
396+
list_add(&cs->list, entry);
392397
}
393398

394399
/**
@@ -397,19 +402,16 @@ static int clocksource_enqueue(struct clocksource *c)
397402
*
398403
* Returns -EBUSY if registration fails, zero otherwise.
399404
*/
400-
int clocksource_register(struct clocksource *c)
405+
int clocksource_register(struct clocksource *cs)
401406
{
402407
unsigned long flags;
403-
int ret;
404408

405409
spin_lock_irqsave(&clocksource_lock, flags);
406-
ret = clocksource_enqueue(c);
407-
if (!ret)
408-
next_clocksource = select_clocksource();
410+
clocksource_enqueue(cs);
411+
clocksource_select();
409412
spin_unlock_irqrestore(&clocksource_lock, flags);
410-
if (!ret)
411-
clocksource_check_watchdog(c);
412-
return ret;
413+
clocksource_check_watchdog(cs);
414+
return 0;
413415
}
414416
EXPORT_SYMBOL(clocksource_register);
415417

@@ -425,7 +427,7 @@ void clocksource_change_rating(struct clocksource *cs, int rating)
425427
list_del(&cs->list);
426428
cs->rating = rating;
427429
clocksource_enqueue(cs);
428-
next_clocksource = select_clocksource();
430+
clocksource_select();
429431
spin_unlock_irqrestore(&clocksource_lock, flags);
430432
}
431433

@@ -438,9 +440,7 @@ void clocksource_unregister(struct clocksource *cs)
438440

439441
spin_lock_irqsave(&clocksource_lock, flags);
440442
list_del(&cs->list);
441-
if (clocksource_override == cs)
442-
clocksource_override = NULL;
443-
next_clocksource = select_clocksource();
443+
clocksource_select();
444444
spin_unlock_irqrestore(&clocksource_lock, flags);
445445
}
446446

@@ -478,9 +478,7 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
478478
struct sysdev_attribute *attr,
479479
const char *buf, size_t count)
480480
{
481-
struct clocksource *ovr = NULL;
482481
size_t ret = count;
483-
int len;
484482

485483
/* strings from sysfs write are not 0 terminated! */
486484
if (count >= sizeof(override_name))
@@ -495,37 +493,7 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
495493
if (count > 0)
496494
memcpy(override_name, buf, count);
497495
override_name[count] = 0;
498-
499-
len = strlen(override_name);
500-
if (len) {
501-
struct clocksource *cs;
502-
503-
ovr = clocksource_override;
504-
/* try to select it: */
505-
list_for_each_entry(cs, &clocksource_list, list) {
506-
if (strlen(cs->name) == len &&
507-
!strcmp(cs->name, override_name))
508-
ovr = cs;
509-
}
510-
}
511-
512-
/*
513-
* Check to make sure we don't switch to a non-highres capable
514-
* clocksource if the tick code is in oneshot mode (highres or nohz)
515-
*/
516-
if (tick_oneshot_mode_active() && ovr &&
517-
!(ovr->flags & CLOCK_SOURCE_VALID_FOR_HRES)) {
518-
printk(KERN_WARNING "%s clocksource is not HRT compatible. "
519-
"Cannot switch while in HRT/NOHZ mode\n", ovr->name);
520-
ovr = NULL;
521-
override_name[0] = 0;
522-
}
523-
524-
/* Reselect, when the override name has changed */
525-
if (ovr != clocksource_override) {
526-
clocksource_override = ovr;
527-
next_clocksource = select_clocksource();
528-
}
496+
clocksource_select();
529497

530498
spin_unlock_irq(&clocksource_lock);
531499

‎kernel/time/jiffies.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ struct clocksource clocksource_jiffies = {
6161
.read = jiffies_read,
6262
.mask = 0xffffffff, /*32bits*/
6363
.mult = NSEC_PER_JIFFY << JIFFIES_SHIFT, /* details above */
64-
.mult_orig = NSEC_PER_JIFFY << JIFFIES_SHIFT,
6564
.shift = JIFFIES_SHIFT,
6665
};
6766

@@ -71,3 +70,8 @@ static int __init init_jiffies_clocksource(void)
7170
}
7271

7372
core_initcall(init_jiffies_clocksource);
73+
74+
struct clocksource * __init __weak clocksource_default_clock(void)
75+
{
76+
return &clocksource_jiffies;
77+
}

‎kernel/time/timekeeping.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static void change_clocksource(void)
269269

270270
new = clocksource_get_next();
271271

272-
if (clock == new)
272+
if (!new || clock == new)
273273
return;
274274

275275
clocksource_forward_now();
@@ -446,7 +446,7 @@ void __init timekeeping_init(void)
446446

447447
ntp_init();
448448

449-
clock = clocksource_get_next();
449+
clock = clocksource_default_clock();
450450
if (clock->enable)
451451
clock->enable(clock);
452452
/* set mult_orig on enable */

0 commit comments

Comments
 (0)