Skip to content

Commit 6c8dca5

Browse files
rustyrussellLinus Torvalds
authored andcommitted
Provide timespec to guests rather than jiffies clock.
A non-periodic clock_event_device and the "jiffies" clock don't mix well: tick_handle_periodic() can go into an infinite loop. Currently lguest guests use the jiffies clock when the TSC is unusable. Instead, make the Host write the current time into the lguest page on every interrupt. This doesn't cost much but is more precise and at least as accurate as the jiffies clock. It also gets rid of the GET_WALLCLOCK hypercall. Also, delay setting sched_clock until our clock is set up, otherwise the early printk timestamps can go backwards (not harmful, just ugly). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a8a11f0 commit 6c8dca5

5 files changed

Lines changed: 60 additions & 25 deletions

File tree

‎drivers/lguest/hypercalls.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ static void do_hcall(struct lguest *lg, struct lguest_regs *regs)
6464
else
6565
guest_pagetable_flush_user(lg);
6666
break;
67-
case LHCALL_GET_WALLCLOCK: {
68-
/* The Guest wants to know the real time in seconds since 1970,
69-
* in good Unix tradition. */
70-
struct timespec ts;
71-
ktime_get_real_ts(&ts);
72-
regs->eax = ts.tv_sec;
73-
break;
74-
}
7567
case LHCALL_BIND_DMA:
7668
/* BIND_DMA really wants four arguments, but it's the only call
7769
* which does. So the Guest packs the number of buffers and
@@ -235,6 +227,9 @@ static void initialize(struct lguest *lg)
235227
|| put_user(lg->guestid, &lg->lguest_data->guestid))
236228
kill_guest(lg, "bad guest page %p", lg->lguest_data);
237229

230+
/* We write the current time into the Guest's data page once now. */
231+
write_timestamp(lg);
232+
238233
/* This is the one case where the above accesses might have been the
239234
* first write to a Guest page. This may have caused a copy-on-write
240235
* fault, but the Guest might be referring to the old (read-only)
@@ -293,3 +288,13 @@ void do_hypercalls(struct lguest *lg)
293288
clear_hcall(lg);
294289
}
295290
}
291+
292+
/* This routine supplies the Guest with time: it's used for wallclock time at
293+
* initial boot and as a rough time source if the TSC isn't available. */
294+
void write_timestamp(struct lguest *lg)
295+
{
296+
struct timespec now;
297+
ktime_get_real_ts(&now);
298+
if (put_user(now, &lg->lguest_data->time))
299+
kill_guest(lg, "Writing timestamp");
300+
}

‎drivers/lguest/interrupts_and_traps.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ void maybe_do_interrupt(struct lguest *lg)
175175
* the stack as well: virtual interrupts never do. */
176176
set_guest_interrupt(lg, idt->a, idt->b, 0);
177177
}
178+
179+
/* Every time we deliver an interrupt, we update the timestamp in the
180+
* Guest's lguest_data struct. It would be better for the Guest if we
181+
* did this more often, but it can actually be quite slow: doing it
182+
* here is a compromise which means at least it gets updated every
183+
* timer interrupt. */
184+
write_timestamp(lg);
178185
}
179186

180187
/*H:220 Now we've got the routines to deliver interrupts, delivering traps

‎drivers/lguest/lg.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
256256

257257
/* hypercalls.c: */
258258
void do_hypercalls(struct lguest *lg);
259+
void write_timestamp(struct lguest *lg);
259260

260261
/*L:035
261262
* Let's step aside for the moment, to study one important routine that's used

‎drivers/lguest/lguest.c‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -643,30 +643,54 @@ static void __init lguest_init_IRQ(void)
643643
* Time.
644644
*
645645
* It would be far better for everyone if the Guest had its own clock, but
646-
* until then it must ask the Host for the time.
646+
* until then the Host gives us the time on every interrupt.
647647
*/
648648
static unsigned long lguest_get_wallclock(void)
649649
{
650-
return hcall(LHCALL_GET_WALLCLOCK, 0, 0, 0);
650+
return lguest_data.time.tv_sec;
651651
}
652652

653-
/* If the Host tells us we can trust the TSC, we use that, otherwise we simply
654-
* use the imprecise but reliable "jiffies" counter. */
655653
static cycle_t lguest_clock_read(void)
656654
{
655+
unsigned long sec, nsec;
656+
657+
/* If the Host tells the TSC speed, we can trust that. */
657658
if (lguest_data.tsc_khz)
658659
return native_read_tsc();
659-
else
660-
return jiffies;
660+
661+
/* If we can't use the TSC, we read the time value written by the Host.
662+
* Since it's in two parts (seconds and nanoseconds), we risk reading
663+
* it just as it's changing from 99 & 0.999999999 to 100 and 0, and
664+
* getting 99 and 0. As Linux tends to come apart under the stress of
665+
* time travel, we must be careful: */
666+
do {
667+
/* First we read the seconds part. */
668+
sec = lguest_data.time.tv_sec;
669+
/* This read memory barrier tells the compiler and the CPU that
670+
* this can't be reordered: we have to complete the above
671+
* before going on. */
672+
rmb();
673+
/* Now we read the nanoseconds part. */
674+
nsec = lguest_data.time.tv_nsec;
675+
/* Make sure we've done that. */
676+
rmb();
677+
/* Now if the seconds part has changed, try again. */
678+
} while (unlikely(lguest_data.time.tv_sec != sec));
679+
680+
/* Our non-TSC clock is in real nanoseconds. */
681+
return sec*1000000000ULL + nsec;
661682
}
662683

663684
/* This is what we tell the kernel is our clocksource. */
664685
static struct clocksource lguest_clock = {
665686
.name = "lguest",
666687
.rating = 400,
667688
.read = lguest_clock_read,
689+
.mask = CLOCKSOURCE_MASK(64),
690+
.mult = 1,
668691
};
669692

693+
/* The "scheduler clock" is just our real clock, adjusted to start at zero */
670694
static unsigned long long lguest_sched_clock(void)
671695
{
672696
return cyc2ns(&lguest_clock, lguest_clock_read() - clock_base);
@@ -742,24 +766,21 @@ static void lguest_time_init(void)
742766
set_irq_handler(0, lguest_time_irq);
743767

744768
/* Our clock structure look like arch/i386/kernel/tsc.c if we can use
745-
* the TSC, otherwise it looks like kernel/time/jiffies.c. Either way,
746-
* the "rating" is initialized so high that it's always chosen over any
747-
* other clocksource. */
769+
* the TSC, otherwise it's a dumb nanosecond-resolution clock. Either
770+
* way, the "rating" is initialized so high that it's always chosen
771+
* over any other clocksource. */
748772
if (lguest_data.tsc_khz) {
749773
lguest_clock.shift = 22;
750774
lguest_clock.mult = clocksource_khz2mult(lguest_data.tsc_khz,
751775
lguest_clock.shift);
752-
lguest_clock.mask = CLOCKSOURCE_MASK(64);
753776
lguest_clock.flags = CLOCK_SOURCE_IS_CONTINUOUS;
754-
} else {
755-
/* To understand this, start at kernel/time/jiffies.c... */
756-
lguest_clock.shift = 8;
757-
lguest_clock.mult = (((u64)NSEC_PER_SEC<<8)/ACTHZ) << 8;
758-
lguest_clock.mask = CLOCKSOURCE_MASK(32);
759777
}
760778
clock_base = lguest_clock_read();
761779
clocksource_register(&lguest_clock);
762780

781+
/* Now we've set up our clock, we can use it as the scheduler clock */
782+
paravirt_ops.sched_clock = lguest_sched_clock;
783+
763784
/* We can't set cpumask in the initializer: damn C limitations! Set it
764785
* here and register our timer device. */
765786
lguest_clockevent.cpumask = cpumask_of_cpu(0);
@@ -996,7 +1017,6 @@ __init void lguest_init(void *boot)
9961017
paravirt_ops.time_init = lguest_time_init;
9971018
paravirt_ops.set_lazy_mode = lguest_lazy_mode;
9981019
paravirt_ops.wbinvd = lguest_wbinvd;
999-
paravirt_ops.sched_clock = lguest_sched_clock;
10001020
/* Now is a good time to look at the implementations of these functions
10011021
* before returning to the rest of lguest_init(). */
10021022

‎include/linux/lguest.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define LHCALL_TS 8
1818
#define LHCALL_SET_CLOCKEVENT 9
1919
#define LHCALL_HALT 10
20-
#define LHCALL_GET_WALLCLOCK 11
2120
#define LHCALL_BIND_DMA 12
2221
#define LHCALL_SEND_DMA 13
2322
#define LHCALL_SET_PTE 14
@@ -88,6 +87,9 @@ struct lguest_data
8887
* this address would normally be found. */
8988
unsigned long cr2;
9089

90+
/* Wallclock time set by the Host. */
91+
struct timespec time;
92+
9193
/* Async hypercall ring. Instead of directly making hypercalls, we can
9294
* place them in here for processing the next time the Host wants.
9395
* This batching can be quite efficient. */

0 commit comments

Comments
 (0)