Re: infinite loop in read_hpet from ktime_get_boot_fast_ns

From: Jason A. Donenfeld
Date: Tue Jun 18 2019 - 13:39:42 EST


Hi Peter,

On Wed, Jun 12, 2019 at 2:29 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> > and the comment at the top mentions explicit sleep hooks. I wasn't
> > sure which function to use from here, though.
>
> Either local_clock() or cpu_clock(cpu). The sleep hooks are not
> something the consumer has to worry about.

I'm not sure whether this is another bug or just a misunderstanding on
what we meant about sleep, but I observed the following behavior,
below, with local_clock(). Notably, local_clock() isn't advanced
during sleep in the way that the other mechanisms are.

Regards,
Jason

[50865.699714] wireguard: local_clock: 1, jiffies: 1,
jiffies_monotoany: 1, ktime_boot: 1, ktime_boot_coarse: 1
[50866.723923] wireguard: local_clock: 2, jiffies: 2,
jiffies_monotoany: 2, ktime_boot: 2, ktime_boot_coarse: 2
[50867.747896] wireguard: local_clock: 3, jiffies: 3,
jiffies_monotoany: 3, ktime_boot: 3, ktime_boot_coarse: 3
[50868.772297] wireguard: local_clock: 4, jiffies: 4,
jiffies_monotoany: 4, ktime_boot: 4, ktime_boot_coarse: 4
[50869.796419] wireguard: local_clock: 5, jiffies: 5,
jiffies_monotoany: 5, ktime_boot: 5, ktime_boot_coarse: 5
[50870.820719] wireguard: local_clock: 6, jiffies: 6,
jiffies_monotoany: 6, ktime_boot: 6, ktime_boot_coarse: 6
[50871.834768] wireguard: local_clock: 7, jiffies: 7,
jiffies_monotoany: 10, ktime_boot: 10, ktime_boot_coarse: 10
[50869.918760] PM: suspend entry (deep)
[50872.846134] PM: suspend exit
[50872.874955] wireguard: local_clock: 8, jiffies: 8,
jiffies_monotoany: 11, ktime_boot: 11, ktime_boot_coarse: 11
[50873.899142] wireguard: local_clock: 9, jiffies: 9,
jiffies_monotoany: 12, ktime_boot: 12, ktime_boot_coarse: 12
[50874.923368] wireguard: local_clock: 10, jiffies: 10,
jiffies_monotoany: 13, ktime_boot: 13, ktime_boot_coarse: 13
[50875.947641] wireguard: local_clock: 11, jiffies: 11,
jiffies_monotoany: 14, ktime_boot: 14, ktime_boot_coarse: 14
[50876.971833] wireguard: local_clock: 12, jiffies: 12,
jiffies_monotoany: 15, ktime_boot: 15, ktime_boot_coarse: 15
[50877.995969] wireguard: local_clock: 13, jiffies: 13,
jiffies_monotoany: 16, ktime_boot: 16, ktime_boot_coarse: 16
[50879.020220] wireguard: local_clock: 14, jiffies: 14,
jiffies_monotoany: 17, ktime_boot: 17, ktime_boot_coarse: 17
[50880.044395] wireguard: local_clock: 15, jiffies: 15,
jiffies_monotoany: 18, ktime_boot: 18, ktime_boot_coarse: 18

static void ugh(struct work_struct *w)
{
int i;
u64 start_localclock = local_clock(), end_localclock;
u64 start_jiffies = jiffies64_to_nsecs(get_jiffies_64()), end_jiffies;
u64 start_jiffiesmonotoany =
ktime_to_ns(ktime_mono_to_any(ns_to_ktime(jiffies64_to_nsecs(get_jiffies_64())),
TK_OFFS_BOOT)), end_jiffiesmonotoany;
u64 start_ktimeboot = ktime_get_boot_ns(), end_ktimeboot;
u64 start_ktimebootcoarse =
ktime_to_ns(ktime_get_coarse_boottime()), end_ktimebootcoarse;

for (i = 0; i < 15; ++i) {
msleep(1000);
end_localclock = local_clock();
end_jiffies = jiffies64_to_nsecs(get_jiffies_64());
end_jiffiesmonotoany =
ktime_to_ns(ktime_mono_to_any(ns_to_ktime(jiffies64_to_nsecs(get_jiffies_64())),
TK_OFFS_BOOT));
end_ktimeboot = ktime_get_boot_ns();
end_ktimebootcoarse = ktime_to_ns(ktime_get_coarse_boottime());
pr_err("local_clock: %llu, jiffies: %llu, jiffies_monotoany: %llu,
ktime_boot: %llu, ktime_boot_coarse: %llu\n",
(end_localclock - start_localclock) / NSEC_PER_SEC,
(end_jiffies - start_jiffies) / NSEC_PER_SEC,
(end_jiffiesmonotoany - start_jiffiesmonotoany) / NSEC_PER_SEC,
(end_ktimeboot - start_ktimeboot) / NSEC_PER_SEC,
(end_ktimebootcoarse - start_ktimebootcoarse) / NSEC_PER_SEC);
}
}

static DECLARE_WORK(blah, ugh);

static int __init mod_init(void)
{
schedule_work(&blah);
return 0;
}