1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Unit test for the clocksource watchdog.
4 *
5 * Copyright (C) 2021 Facebook, Inc.
6 *
7 * Author: Paul E. McKenney <[email protected]>
8 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/device.h>
12 #include <linux/clocksource.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
16 #include <linux/tick.h>
17 #include <linux/kthread.h>
18 #include <linux/delay.h>
19 #include <linux/prandom.h>
20 #include <linux/cpu.h>
21
22 #include "tick-internal.h"
23
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("Clocksource watchdog unit test");
26 MODULE_AUTHOR("Paul E. McKenney <[email protected]>");
27
28 static int holdoff = IS_BUILTIN(CONFIG_TEST_CLOCKSOURCE_WATCHDOG) ? 10 : 0;
29 module_param(holdoff, int, 0444);
30 MODULE_PARM_DESC(holdoff, "Time to wait to start test (s).");
31
32 /* Watchdog kthread's task_struct pointer for debug purposes. */
33 static struct task_struct *wdtest_task;
34
wdtest_jiffies_read(struct clocksource * cs)35 static u64 wdtest_jiffies_read(struct clocksource *cs)
36 {
37 return (u64)jiffies;
38 }
39
40 static struct clocksource clocksource_wdtest_jiffies = {
41 .name = "wdtest-jiffies",
42 .rating = 1, /* lowest valid rating*/
43 .uncertainty_margin = TICK_NSEC,
44 .read = wdtest_jiffies_read,
45 .mask = CLOCKSOURCE_MASK(32),
46 .flags = CLOCK_SOURCE_MUST_VERIFY,
47 .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */
48 .shift = JIFFIES_SHIFT,
49 .max_cycles = 10,
50 };
51
52 static int wdtest_ktime_read_ndelays;
53 static bool wdtest_ktime_read_fuzz;
54
wdtest_ktime_read(struct clocksource * cs)55 static u64 wdtest_ktime_read(struct clocksource *cs)
56 {
57 int wkrn = READ_ONCE(wdtest_ktime_read_ndelays);
58 static int sign = 1;
59 u64 ret;
60
61 if (wkrn) {
62 udelay(cs->uncertainty_margin / 250);
63 WRITE_ONCE(wdtest_ktime_read_ndelays, wkrn - 1);
64 }
65 ret = ktime_get_real_fast_ns();
66 if (READ_ONCE(wdtest_ktime_read_fuzz)) {
67 sign = -sign;
68 ret = ret + sign * 100 * NSEC_PER_MSEC;
69 }
70 return ret;
71 }
72
wdtest_ktime_cs_mark_unstable(struct clocksource * cs)73 static void wdtest_ktime_cs_mark_unstable(struct clocksource *cs)
74 {
75 pr_info("--- Marking %s unstable due to clocksource watchdog.\n", cs->name);
76 }
77
78 #define KTIME_FLAGS (CLOCK_SOURCE_IS_CONTINUOUS | \
79 CLOCK_SOURCE_VALID_FOR_HRES | \
80 CLOCK_SOURCE_MUST_VERIFY | \
81 CLOCK_SOURCE_VERIFY_PERCPU)
82
83 static struct clocksource clocksource_wdtest_ktime = {
84 .name = "wdtest-ktime",
85 .rating = 300,
86 .read = wdtest_ktime_read,
87 .mask = CLOCKSOURCE_MASK(64),
88 .flags = KTIME_FLAGS,
89 .mark_unstable = wdtest_ktime_cs_mark_unstable,
90 .list = LIST_HEAD_INIT(clocksource_wdtest_ktime.list),
91 };
92
93 /* Reset the clocksource if needed. */
wdtest_ktime_clocksource_reset(void)94 static void wdtest_ktime_clocksource_reset(void)
95 {
96 if (clocksource_wdtest_ktime.flags & CLOCK_SOURCE_UNSTABLE) {
97 clocksource_unregister(&clocksource_wdtest_ktime);
98 clocksource_wdtest_ktime.flags = KTIME_FLAGS;
99 schedule_timeout_uninterruptible(HZ / 10);
100 clocksource_register_khz(&clocksource_wdtest_ktime, 1000 * 1000);
101 }
102 }
103
104 /* Run the specified series of watchdog tests. */
wdtest_func(void * arg)105 static int wdtest_func(void *arg)
106 {
107 unsigned long j1, j2;
108 int i, max_retries;
109 char *s;
110
111 schedule_timeout_uninterruptible(holdoff * HZ);
112
113 /*
114 * Verify that jiffies-like clocksources get the manually
115 * specified uncertainty margin.
116 */
117 pr_info("--- Verify jiffies-like uncertainty margin.\n");
118 __clocksource_register(&clocksource_wdtest_jiffies);
119 WARN_ON_ONCE(clocksource_wdtest_jiffies.uncertainty_margin != TICK_NSEC);
120
121 j1 = clocksource_wdtest_jiffies.read(&clocksource_wdtest_jiffies);
122 schedule_timeout_uninterruptible(HZ);
123 j2 = clocksource_wdtest_jiffies.read(&clocksource_wdtest_jiffies);
124 WARN_ON_ONCE(j1 == j2);
125
126 clocksource_unregister(&clocksource_wdtest_jiffies);
127
128 /*
129 * Verify that tsc-like clocksources are assigned a reasonable
130 * uncertainty margin.
131 */
132 pr_info("--- Verify tsc-like uncertainty margin.\n");
133 clocksource_register_khz(&clocksource_wdtest_ktime, 1000 * 1000);
134 WARN_ON_ONCE(clocksource_wdtest_ktime.uncertainty_margin < NSEC_PER_USEC);
135
136 j1 = clocksource_wdtest_ktime.read(&clocksource_wdtest_ktime);
137 udelay(1);
138 j2 = clocksource_wdtest_ktime.read(&clocksource_wdtest_ktime);
139 pr_info("--- tsc-like times: %lu - %lu = %lu.\n", j2, j1, j2 - j1);
140 WARN_ONCE(time_before(j2, j1 + NSEC_PER_USEC),
141 "Expected at least 1000ns, got %lu.\n", j2 - j1);
142
143 /* Verify tsc-like stability with various numbers of errors injected. */
144 max_retries = clocksource_get_max_watchdog_retry();
145 for (i = 0; i <= max_retries + 1; i++) {
146 if (i <= 1 && i < max_retries)
147 s = "";
148 else if (i <= max_retries)
149 s = ", expect message";
150 else
151 s = ", expect clock skew";
152 pr_info("--- Watchdog with %dx error injection, %d retries%s.\n", i, max_retries, s);
153 WRITE_ONCE(wdtest_ktime_read_ndelays, i);
154 schedule_timeout_uninterruptible(2 * HZ);
155 WARN_ON_ONCE(READ_ONCE(wdtest_ktime_read_ndelays));
156 WARN_ON_ONCE((i <= max_retries) !=
157 !(clocksource_wdtest_ktime.flags & CLOCK_SOURCE_UNSTABLE));
158 wdtest_ktime_clocksource_reset();
159 }
160
161 /* Verify tsc-like stability with clock-value-fuzz error injection. */
162 pr_info("--- Watchdog clock-value-fuzz error injection, expect clock skew and per-CPU mismatches.\n");
163 WRITE_ONCE(wdtest_ktime_read_fuzz, true);
164 schedule_timeout_uninterruptible(2 * HZ);
165 WARN_ON_ONCE(!(clocksource_wdtest_ktime.flags & CLOCK_SOURCE_UNSTABLE));
166 clocksource_verify_percpu(&clocksource_wdtest_ktime);
167 WRITE_ONCE(wdtest_ktime_read_fuzz, false);
168
169 clocksource_unregister(&clocksource_wdtest_ktime);
170
171 pr_info("--- Done with test.\n");
172 return 0;
173 }
174
wdtest_print_module_parms(void)175 static void wdtest_print_module_parms(void)
176 {
177 pr_alert("--- holdoff=%d\n", holdoff);
178 }
179
180 /* Cleanup function. */
clocksource_wdtest_cleanup(void)181 static void clocksource_wdtest_cleanup(void)
182 {
183 }
184
clocksource_wdtest_init(void)185 static int __init clocksource_wdtest_init(void)
186 {
187 int ret = 0;
188
189 wdtest_print_module_parms();
190
191 /* Create watchdog-test task. */
192 wdtest_task = kthread_run(wdtest_func, NULL, "wdtest");
193 if (IS_ERR(wdtest_task)) {
194 ret = PTR_ERR(wdtest_task);
195 pr_warn("%s: Failed to create wdtest kthread.\n", __func__);
196 wdtest_task = NULL;
197 return ret;
198 }
199
200 return 0;
201 }
202
203 module_init(clocksource_wdtest_init);
204 module_exit(clocksource_wdtest_cleanup);
205