xref: /linux/tools/testing/selftests/timers/posix_timers.c (revision 7a3d2740d1b1b5ed8d22e1e5430de3b5046cf6a9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4  *
5  * Selftests for a few posix timers interface.
6  *
7  * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com>
8  */
9 #define _GNU_SOURCE
10 #include <sys/prctl.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <stdio.h>
14 #include <signal.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <time.h>
19 #include <pthread.h>
20 #include <stdbool.h>
21 
22 #include "clock-helpers.h"
23 #include "kselftest.h"
24 
25 #define DELAY 2
26 
27 static void __fatal_error(const char *test, const char *name, const char *what)
28 {
29 	char buf[64];
30 	char *ret_str = NULL;
31 
32 	ret_str = strerror_r(errno, buf, sizeof(buf));
33 
34 	if (name && strlen(name) && ret_str)
35 		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, ret_str);
36 	else if (ret_str)
37 		ksft_exit_fail_msg("%s %s %s\n", test, what, ret_str);
38 	else
39 		ksft_exit_fail_msg("%s %s\n", test, what);
40 
41 }
42 
43 #define fatal_error(name, what)	__fatal_error(__func__, name, what)
44 
45 static volatile int done;
46 
47 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
48 static void user_loop(void)
49 {
50 	while (!done);
51 }
52 
53 /*
54  * Try to spend as much time as possible in kernelspace
55  * to elapse ITIMER_PROF.
56  */
57 static void kernel_loop(void)
58 {
59 	void *addr = sbrk(0);
60 	int err = 0;
61 
62 	while (!done && !err) {
63 		err = brk(addr + 4096);
64 		err |= brk(addr);
65 	}
66 }
67 
68 /*
69  * Sleep until ITIMER_REAL expiration.
70  */
71 static void idle_loop(void)
72 {
73 	pause();
74 }
75 
76 static void sig_handler(int nr)
77 {
78 	done = 1;
79 }
80 
81 static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2)
82 {
83 	int64_t diff;
84 
85 	diff = NSEC_PER_SEC * (int64_t)((int) t1.tv_sec - (int) t2.tv_sec);
86 	diff += ((int) t1.tv_nsec - (int) t2.tv_nsec);
87 	return diff;
88 }
89 
90 /*
91  * Check the expected timer expiration matches the GTOD elapsed delta since
92  * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
93  */
94 static int check_diff(struct timespec start, struct timespec end)
95 {
96 	long long diff = calcdiff_ns(end, start);
97 
98 	if (llabs(diff - DELAY * NSEC_PER_SEC) > NSEC_PER_SEC / 2) {
99 		printf("Diff too high: %lld ns..", diff);
100 		return -1;
101 	}
102 
103 	return 0;
104 }
105 
106 static void check_itimer(int which, const char *name)
107 {
108 	struct timespec start, end;
109 	struct itimerval val = {
110 		.it_value.tv_sec = DELAY,
111 	};
112 	int clock_id = CLOCK_REALTIME;
113 
114 	done = 0;
115 
116 	if (which == ITIMER_VIRTUAL) {
117 		clock_id = CLOCK_THREAD_CPUTIME_ID;
118 		signal(SIGVTALRM, sig_handler);
119 	} else if (which == ITIMER_PROF) {
120 		clock_id = CLOCK_THREAD_CPUTIME_ID;
121 		signal(SIGPROF, sig_handler);
122 	}
123 	else if (which == ITIMER_REAL)
124 		signal(SIGALRM, sig_handler);
125 
126 	if (clock_gettime(clock_id, &start))
127 		fatal_error(name, "clock_gettime()");
128 
129 	if (setitimer(which, &val, NULL) < 0)
130 		fatal_error(name, "setitimer()");
131 
132 	if (which == ITIMER_VIRTUAL)
133 		user_loop();
134 	else if (which == ITIMER_PROF)
135 		kernel_loop();
136 	else if (which == ITIMER_REAL)
137 		idle_loop();
138 
139 	if (clock_gettime(clock_id, &end))
140 		fatal_error(name, "clock_gettime()");
141 
142 	ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
143 }
144 
145 static void check_timer_create(int which)
146 {
147 	const char *name = clock_name(which);
148 	struct timespec start, end;
149 	struct itimerspec val = {
150 		.it_value.tv_sec = DELAY,
151 	};
152 	int clock_id = which;
153 	timer_t id;
154 
155 	done = 0;
156 
157 	if (timer_create(which, NULL, &id) < 0)
158 		fatal_error(name, "timer_create()");
159 
160 	if (signal(SIGALRM, sig_handler) == SIG_ERR)
161 		fatal_error(name, "signal()");
162 
163 	if (clock_gettime(clock_id, &start))
164 		fatal_error(name, "clock_gettime()");
165 
166 	if (timer_settime(id, 0, &val, NULL) < 0)
167 		fatal_error(name, "timer_settime()");
168 
169 	user_loop();
170 
171 	if (clock_gettime(clock_id, &end))
172 		fatal_error(name, "clock_gettime()");
173 
174 	ksft_test_result(check_diff(start, end) == 0,
175 			 "timer_create() per %s\n", name);
176 }
177 
178 static pthread_t ctd_thread;
179 static volatile int ctd_count, ctd_failed;
180 
181 static void ctd_sighandler(int sig)
182 {
183 	if (pthread_self() != ctd_thread)
184 		ctd_failed = 1;
185 	ctd_count--;
186 }
187 
188 static void *ctd_thread_func(void *arg)
189 {
190 	struct itimerspec val = {
191 		.it_value.tv_sec = 0,
192 		.it_value.tv_nsec = 1000 * 1000,
193 		.it_interval.tv_sec = 0,
194 		.it_interval.tv_nsec = 1000 * 1000,
195 	};
196 	timer_t id;
197 
198 	/* 1/10 seconds to ensure the leader sleeps */
199 	usleep(10000);
200 
201 	ctd_count = 100;
202 	if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id))
203 		fatal_error(NULL, "timer_create()");
204 	if (timer_settime(id, 0, &val, NULL))
205 		fatal_error(NULL, "timer_settime()");
206 	while (ctd_count > 0 && !ctd_failed)
207 		;
208 
209 	if (timer_delete(id))
210 		fatal_error(NULL, "timer_delete()");
211 
212 	return NULL;
213 }
214 
215 /*
216  * Test that only the running thread receives the timer signal.
217  */
218 static void check_timer_distribution(void)
219 {
220 	if (signal(SIGALRM, ctd_sighandler) == SIG_ERR)
221 		fatal_error(NULL, "signal()");
222 
223 	if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL))
224 		fatal_error(NULL, "pthread_create()");
225 
226 	if (pthread_join(ctd_thread, NULL))
227 		fatal_error(NULL, "pthread_join()");
228 
229 	if (!ctd_failed)
230 		ksft_test_result_pass("check signal distribution\n");
231 	else if (ksft_min_kernel_version(6, 3))
232 		ksft_test_result_fail("check signal distribution\n");
233 	else
234 		ksft_test_result_skip("check signal distribution (old kernel)\n");
235 }
236 
237 struct tmrsig {
238 	int	signals;
239 	int	overruns;
240 };
241 
242 static void siginfo_handler(int sig, siginfo_t *si, void *uc)
243 {
244 	struct tmrsig *tsig = si ? si->si_ptr : NULL;
245 
246 	if (tsig) {
247 		tsig->signals++;
248 		tsig->overruns += si->si_overrun;
249 	}
250 }
251 
252 static void *ignore_thread(void *arg)
253 {
254 	unsigned int *tid = arg;
255 	sigset_t set;
256 
257 	sigemptyset(&set);
258 	sigaddset(&set, SIGUSR1);
259 	if (sigprocmask(SIG_BLOCK, &set, NULL))
260 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
261 
262 	*tid = gettid();
263 	sleep(100);
264 
265 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
266 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
267 	return NULL;
268 }
269 
270 static void check_sig_ign(int thread)
271 {
272 	struct tmrsig tsig = { };
273 	struct itimerspec its;
274 	unsigned int tid = 0;
275 	struct sigaction sa;
276 	struct sigevent sev;
277 	pthread_t pthread;
278 	timer_t timerid;
279 	sigset_t set;
280 
281 	if (thread) {
282 		if (pthread_create(&pthread, NULL, ignore_thread, &tid))
283 			fatal_error(NULL, "pthread_create()");
284 		sleep(1);
285 	}
286 
287 	sa.sa_flags = SA_SIGINFO;
288 	sa.sa_sigaction = siginfo_handler;
289 	sigemptyset(&sa.sa_mask);
290 	if (sigaction(SIGUSR1, &sa, NULL))
291 		fatal_error(NULL, "sigaction()");
292 
293 	/* Block the signal */
294 	sigemptyset(&set);
295 	sigaddset(&set, SIGUSR1);
296 	if (sigprocmask(SIG_BLOCK, &set, NULL))
297 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
298 
299 	memset(&sev, 0, sizeof(sev));
300 	sev.sigev_notify = SIGEV_SIGNAL;
301 	sev.sigev_signo = SIGUSR1;
302 	sev.sigev_value.sival_ptr = &tsig;
303 	if (thread) {
304 		sev.sigev_notify = SIGEV_THREAD_ID;
305 		sev._sigev_un._tid = tid;
306 	}
307 
308 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
309 		fatal_error(NULL, "timer_create()");
310 
311 	/* Start the timer to expire in 100ms and 100ms intervals */
312 	its.it_value.tv_sec = 0;
313 	its.it_value.tv_nsec = 100000000;
314 	its.it_interval.tv_sec = 0;
315 	its.it_interval.tv_nsec = 100000000;
316 	timer_settime(timerid, 0, &its, NULL);
317 
318 	sleep(1);
319 
320 	/* Set the signal to be ignored */
321 	if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
322 		fatal_error(NULL, "signal(SIG_IGN)");
323 
324 	sleep(1);
325 
326 	if (thread) {
327 		/* Stop the thread first. No signal should be delivered to it */
328 		if (pthread_cancel(pthread))
329 			fatal_error(NULL, "pthread_cancel()");
330 		if (pthread_join(pthread, NULL))
331 			fatal_error(NULL, "pthread_join()");
332 	}
333 
334 	/* Restore the handler */
335 	if (sigaction(SIGUSR1, &sa, NULL))
336 		fatal_error(NULL, "sigaction()");
337 
338 	sleep(1);
339 
340 	/* Unblock it, which should deliver the signal in the !thread case*/
341 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
342 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
343 
344 	if (timer_delete(timerid))
345 		fatal_error(NULL, "timer_delete()");
346 
347 	if (!thread) {
348 		ksft_test_result(tsig.signals == 1 && tsig.overruns == 29,
349 				 "check_sig_ign SIGEV_SIGNAL\n");
350 	} else {
351 		ksft_test_result(tsig.signals == 0 && tsig.overruns == 0,
352 				 "check_sig_ign SIGEV_THREAD_ID\n");
353 	}
354 }
355 
356 static void check_rearm(void)
357 {
358 	struct tmrsig tsig = { };
359 	struct itimerspec its;
360 	struct sigaction sa;
361 	struct sigevent sev;
362 	timer_t timerid;
363 	sigset_t set;
364 
365 	sa.sa_flags = SA_SIGINFO;
366 	sa.sa_sigaction = siginfo_handler;
367 	sigemptyset(&sa.sa_mask);
368 	if (sigaction(SIGUSR1, &sa, NULL))
369 		fatal_error(NULL, "sigaction()");
370 
371 	/* Block the signal */
372 	sigemptyset(&set);
373 	sigaddset(&set, SIGUSR1);
374 	if (sigprocmask(SIG_BLOCK, &set, NULL))
375 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
376 
377 	memset(&sev, 0, sizeof(sev));
378 	sev.sigev_notify = SIGEV_SIGNAL;
379 	sev.sigev_signo = SIGUSR1;
380 	sev.sigev_value.sival_ptr = &tsig;
381 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
382 		fatal_error(NULL, "timer_create()");
383 
384 	/* Start the timer to expire in 100ms and 100ms intervals */
385 	its.it_value.tv_sec = 0;
386 	its.it_value.tv_nsec = 100000000;
387 	its.it_interval.tv_sec = 0;
388 	its.it_interval.tv_nsec = 100000000;
389 	if (timer_settime(timerid, 0, &its, NULL))
390 		fatal_error(NULL, "timer_settime()");
391 
392 	sleep(1);
393 
394 	/* Reprogram the timer to single shot */
395 	its.it_value.tv_sec = 10;
396 	its.it_value.tv_nsec = 0;
397 	its.it_interval.tv_sec = 0;
398 	its.it_interval.tv_nsec = 0;
399 	if (timer_settime(timerid, 0, &its, NULL))
400 		fatal_error(NULL, "timer_settime()");
401 
402 	/* Unblock it, which should not deliver a signal */
403 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
404 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
405 
406 	if (timer_delete(timerid))
407 		fatal_error(NULL, "timer_delete()");
408 
409 	ksft_test_result(!tsig.signals, "check_rearm\n");
410 }
411 
412 static void check_delete(void)
413 {
414 	struct tmrsig tsig = { };
415 	struct itimerspec its;
416 	struct sigaction sa;
417 	struct sigevent sev;
418 	timer_t timerid;
419 	sigset_t set;
420 
421 	sa.sa_flags = SA_SIGINFO;
422 	sa.sa_sigaction = siginfo_handler;
423 	sigemptyset(&sa.sa_mask);
424 	if (sigaction(SIGUSR1, &sa, NULL))
425 		fatal_error(NULL, "sigaction()");
426 
427 	/* Block the signal */
428 	sigemptyset(&set);
429 	sigaddset(&set, SIGUSR1);
430 	if (sigprocmask(SIG_BLOCK, &set, NULL))
431 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
432 
433 	memset(&sev, 0, sizeof(sev));
434 	sev.sigev_notify = SIGEV_SIGNAL;
435 	sev.sigev_signo = SIGUSR1;
436 	sev.sigev_value.sival_ptr = &tsig;
437 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
438 		fatal_error(NULL, "timer_create()");
439 
440 	/* Start the timer to expire in 100ms and 100ms intervals */
441 	its.it_value.tv_sec = 0;
442 	its.it_value.tv_nsec = 100000000;
443 	its.it_interval.tv_sec = 0;
444 	its.it_interval.tv_nsec = 100000000;
445 	if (timer_settime(timerid, 0, &its, NULL))
446 		fatal_error(NULL, "timer_settime()");
447 
448 	sleep(1);
449 
450 	if (timer_delete(timerid))
451 		fatal_error(NULL, "timer_delete()");
452 
453 	/* Unblock it, which should not deliver a signal */
454 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
455 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
456 
457 	ksft_test_result(!tsig.signals, "check_delete\n");
458 }
459 
460 static void check_sigev_none(int which)
461 {
462 	const char *name = clock_name(which);
463 	struct timespec start, now;
464 	struct itimerspec its;
465 	struct sigevent sev;
466 	timer_t timerid;
467 
468 	memset(&sev, 0, sizeof(sev));
469 	sev.sigev_notify = SIGEV_NONE;
470 
471 	if (timer_create(which, &sev, &timerid))
472 		fatal_error(name, "timer_create()");
473 
474 	/* Start the timer to expire in 100ms and 100ms intervals */
475 	its.it_value.tv_sec = 0;
476 	its.it_value.tv_nsec = 100000000;
477 	its.it_interval.tv_sec = 0;
478 	its.it_interval.tv_nsec = 100000000;
479 	timer_settime(timerid, 0, &its, NULL);
480 
481 	if (clock_gettime(which, &start))
482 		fatal_error(name, "clock_gettime()");
483 
484 	do {
485 		if (clock_gettime(which, &now))
486 			fatal_error(name, "clock_gettime()");
487 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
488 
489 	if (timer_gettime(timerid, &its))
490 		fatal_error(name, "timer_gettime()");
491 
492 	if (timer_delete(timerid))
493 		fatal_error(name, "timer_delete()");
494 
495 	ksft_test_result(its.it_value.tv_sec || its.it_value.tv_nsec,
496 			 "check_sigev_none %s\n", name);
497 }
498 
499 static void check_sigev_none_disarm(int which)
500 {
501 	const char *name = clock_name(which);
502 	struct itimerspec its;
503 	struct sigevent sev;
504 	timer_t timerid;
505 	bool armed;
506 
507 	memset(&sev, 0, sizeof(sev));
508 	sev.sigev_notify = SIGEV_NONE;
509 
510 	if (timer_create(which, &sev, &timerid))
511 		fatal_error(name, "timer_create()");
512 
513 	/* Start a oneshot timer which does not expire during the test */
514 	memset(&its, 0, sizeof(its));
515 	its.it_value.tv_sec = 100;
516 	if (timer_settime(timerid, 0, &its, NULL))
517 		fatal_error(name, "timer_settime()");
518 
519 	if (timer_gettime(timerid, &its))
520 		fatal_error(name, "timer_gettime()");
521 	armed = its.it_value.tv_sec || its.it_value.tv_nsec;
522 
523 	/* Disarm it. The remaining time must be reported as 0 */
524 	memset(&its, 0, sizeof(its));
525 	if (timer_settime(timerid, 0, &its, NULL))
526 		fatal_error(name, "timer_settime()");
527 
528 	if (timer_gettime(timerid, &its))
529 		fatal_error(name, "timer_gettime()");
530 
531 	if (timer_delete(timerid))
532 		fatal_error(name, "timer_delete()");
533 
534 	ksft_test_result(armed && !its.it_value.tv_sec && !its.it_value.tv_nsec,
535 			 "%s %s\n", __func__, name);
536 }
537 
538 static void check_gettime(int which)
539 {
540 	const char *name = clock_name(which);
541 	struct itimerspec its, prev;
542 	struct timespec start, now;
543 	struct sigevent sev;
544 	timer_t timerid;
545 	int wraps = 0;
546 	sigset_t set;
547 
548 	/* Block the signal */
549 	sigemptyset(&set);
550 	sigaddset(&set, SIGUSR1);
551 	if (sigprocmask(SIG_BLOCK, &set, NULL))
552 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
553 
554 	memset(&sev, 0, sizeof(sev));
555 	sev.sigev_notify = SIGEV_SIGNAL;
556 	sev.sigev_signo = SIGUSR1;
557 
558 	if (timer_create(which, &sev, &timerid))
559 		fatal_error(name, "timer_create()");
560 
561 	/* Start the timer to expire in 100ms and 100ms intervals */
562 	its.it_value.tv_sec = 0;
563 	its.it_value.tv_nsec = 100000000;
564 	its.it_interval.tv_sec = 0;
565 	its.it_interval.tv_nsec = 100000000;
566 	if (timer_settime(timerid, 0, &its, NULL))
567 		fatal_error(name, "timer_settime()");
568 
569 	if (timer_gettime(timerid, &prev))
570 		fatal_error(name, "timer_gettime()");
571 
572 	if (clock_gettime(which, &start))
573 		fatal_error(name, "clock_gettime()");
574 
575 	do {
576 		if (clock_gettime(which, &now))
577 			fatal_error(name, "clock_gettime()");
578 		if (timer_gettime(timerid, &its))
579 			fatal_error(name, "timer_gettime()");
580 		if (its.it_value.tv_nsec > prev.it_value.tv_nsec)
581 			wraps++;
582 		prev = its;
583 
584 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
585 
586 	if (timer_delete(timerid))
587 		fatal_error(name, "timer_delete()");
588 
589 	ksft_test_result(wraps > 1, "check_gettime %s\n", name);
590 }
591 
592 static void check_overrun(int which)
593 {
594 	const char *name = clock_name(which);
595 	struct timespec start, now;
596 	struct tmrsig tsig = { };
597 	struct itimerspec its;
598 	struct sigaction sa;
599 	struct sigevent sev;
600 	timer_t timerid;
601 	sigset_t set;
602 
603 	sa.sa_flags = SA_SIGINFO;
604 	sa.sa_sigaction = siginfo_handler;
605 	sigemptyset(&sa.sa_mask);
606 	if (sigaction(SIGUSR1, &sa, NULL))
607 		fatal_error(name, "sigaction()");
608 
609 	/* Block the signal */
610 	sigemptyset(&set);
611 	sigaddset(&set, SIGUSR1);
612 	if (sigprocmask(SIG_BLOCK, &set, NULL))
613 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
614 
615 	memset(&sev, 0, sizeof(sev));
616 	sev.sigev_notify = SIGEV_SIGNAL;
617 	sev.sigev_signo = SIGUSR1;
618 	sev.sigev_value.sival_ptr = &tsig;
619 	if (timer_create(which, &sev, &timerid))
620 		fatal_error(name, "timer_create()");
621 
622 	/* Start the timer to expire in 100ms and 100ms intervals */
623 	its.it_value.tv_sec = 0;
624 	its.it_value.tv_nsec = 100000000;
625 	its.it_interval.tv_sec = 0;
626 	its.it_interval.tv_nsec = 100000000;
627 	if (timer_settime(timerid, 0, &its, NULL))
628 		fatal_error(name, "timer_settime()");
629 
630 	if (clock_gettime(which, &start))
631 		fatal_error(name, "clock_gettime()");
632 
633 	do {
634 		if (clock_gettime(which, &now))
635 			fatal_error(name, "clock_gettime()");
636 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
637 
638 	/* Unblock it, which should deliver a signal */
639 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
640 		fatal_error(name, "sigprocmask(SIG_UNBLOCK)");
641 
642 	if (timer_delete(timerid))
643 		fatal_error(name, "timer_delete()");
644 
645 	ksft_test_result(tsig.signals == 1 && tsig.overruns == 9,
646 			 "check_overrun %s\n", name);
647 }
648 
649 #include <sys/syscall.h>
650 
651 static int do_timer_create(int *id)
652 {
653 	return syscall(__NR_timer_create, CLOCK_MONOTONIC, NULL, id);
654 }
655 
656 static int do_timer_delete(int id)
657 {
658 	return syscall(__NR_timer_delete, id);
659 }
660 
661 #ifndef PR_TIMER_CREATE_RESTORE_IDS
662 # define PR_TIMER_CREATE_RESTORE_IDS		77
663 # define PR_TIMER_CREATE_RESTORE_IDS_OFF	 0
664 # define PR_TIMER_CREATE_RESTORE_IDS_ON		 1
665 # define PR_TIMER_CREATE_RESTORE_IDS_GET	 2
666 #endif
667 
668 static void check_timer_create_exact(void)
669 {
670 	int id;
671 
672 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_ON, 0, 0, 0)) {
673 		switch (errno) {
674 		case EINVAL:
675 			ksft_test_result_skip("check timer create exact, not supported\n");
676 			return;
677 		default:
678 			ksft_test_result_skip("check timer create exact, errno = %d\n", errno);
679 			return;
680 		}
681 	}
682 
683 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_GET, 0, 0, 0) != 1)
684 		fatal_error(NULL, "prctl(GET) failed\n");
685 
686 	id = 8;
687 	if (do_timer_create(&id) < 0)
688 		fatal_error(NULL, "timer_create()");
689 
690 	if (do_timer_delete(id))
691 		fatal_error(NULL, "timer_delete()");
692 
693 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_OFF, 0, 0, 0))
694 		fatal_error(NULL, "prctl(OFF)");
695 
696 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_GET, 0, 0, 0) != 0)
697 		fatal_error(NULL, "prctl(GET) failed\n");
698 
699 	if (id != 8) {
700 		ksft_test_result_fail("check timer create exact %d != 8\n", id);
701 		return;
702 	}
703 
704 	/* Validate that it went back to normal mode and allocates ID 9 */
705 	if (do_timer_create(&id) < 0)
706 		fatal_error(NULL, "timer_create()");
707 
708 	if (do_timer_delete(id))
709 		fatal_error(NULL, "timer_delete()");
710 
711 	if (id == 9)
712 		ksft_test_result_pass("check timer create exact\n");
713 	else
714 		ksft_test_result_fail("check timer create exact. Disabling failed.\n");
715 }
716 
717 int main(int argc, char **argv)
718 {
719 	bool run_sig_ign_tests = ksft_min_kernel_version(6, 13);
720 
721 	ksft_print_header();
722 	if (run_sig_ign_tests) {
723 		ksft_set_plan(21);
724 	} else {
725 		ksft_set_plan(10);
726 	}
727 
728 	ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
729 	ksft_print_msg("based timers if other threads run on the CPU...\n");
730 
731 	check_timer_create_exact();
732 
733 	check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL");
734 	check_itimer(ITIMER_PROF, "ITIMER_PROF");
735 	check_itimer(ITIMER_REAL, "ITIMER_REAL");
736 	check_timer_create(CLOCK_THREAD_CPUTIME_ID);
737 
738 	/*
739 	 * It's unfortunately hard to reliably test a timer expiration
740 	 * on parallel multithread cputime. We could arm it to expire
741 	 * on DELAY * nr_threads, with nr_threads busy looping, then wait
742 	 * the normal DELAY since the time is elapsing nr_threads faster.
743 	 * But for that we need to ensure we have real physical free CPUs
744 	 * to ensure true parallelism. So test only one thread until we
745 	 * find a better solution.
746 	 */
747 	check_timer_create(CLOCK_PROCESS_CPUTIME_ID);
748 	check_timer_distribution();
749 
750 	if (run_sig_ign_tests) {
751 		check_sig_ign(0);
752 		check_sig_ign(1);
753 		check_rearm();
754 		check_delete();
755 		check_sigev_none(CLOCK_MONOTONIC);
756 		check_sigev_none(CLOCK_PROCESS_CPUTIME_ID);
757 		check_sigev_none_disarm(CLOCK_MONOTONIC);
758 		check_sigev_none_disarm(CLOCK_PROCESS_CPUTIME_ID);
759 		check_gettime(CLOCK_MONOTONIC);
760 		check_gettime(CLOCK_PROCESS_CPUTIME_ID);
761 		check_gettime(CLOCK_THREAD_CPUTIME_ID);
762 	} else {
763 		ksft_print_msg("Skipping SIG_IGN tests on kernel < 6.13\n");
764 	}
765 
766 	check_overrun(CLOCK_MONOTONIC);
767 	check_overrun(CLOCK_PROCESS_CPUTIME_ID);
768 	check_overrun(CLOCK_THREAD_CPUTIME_ID);
769 
770 	ksft_finished();
771 }
772