xref: /linux/tools/testing/selftests/timers/posix_timers.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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 		signal(SIGVTALRM, sig_handler);
118 	else if (which == ITIMER_PROF) {
119 		clock_id = CLOCK_THREAD_CPUTIME_ID;
120 		signal(SIGPROF, sig_handler);
121 	}
122 	else if (which == ITIMER_REAL)
123 		signal(SIGALRM, sig_handler);
124 
125 	if (clock_gettime(clock_id, &start))
126 		fatal_error(name, "clock_gettime()");
127 
128 	if (setitimer(which, &val, NULL) < 0)
129 		fatal_error(name, "setitimer()");
130 
131 	if (which == ITIMER_VIRTUAL)
132 		user_loop();
133 	else if (which == ITIMER_PROF)
134 		kernel_loop();
135 	else if (which == ITIMER_REAL)
136 		idle_loop();
137 
138 	if (clock_gettime(clock_id, &end))
139 		fatal_error(name, "clock_gettime()");
140 
141 	ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
142 }
143 
144 static void check_timer_create(int which)
145 {
146 	const char *name = clock_name(which);
147 	struct timespec start, end;
148 	struct itimerspec val = {
149 		.it_value.tv_sec = DELAY,
150 	};
151 	int clock_id = CLOCK_REALTIME;
152 	timer_t id;
153 
154 	done = 0;
155 
156 	if (timer_create(which, NULL, &id) < 0)
157 		fatal_error(name, "timer_create()");
158 
159 	if (signal(SIGALRM, sig_handler) == SIG_ERR)
160 		fatal_error(name, "signal()");
161 
162 	if (clock_gettime(clock_id, &start))
163 		fatal_error(name, "clock_gettime()");
164 
165 	if (timer_settime(id, 0, &val, NULL) < 0)
166 		fatal_error(name, "timer_settime()");
167 
168 	user_loop();
169 
170 	if (clock_gettime(clock_id, &end))
171 		fatal_error(name, "clock_gettime()");
172 
173 	ksft_test_result(check_diff(start, end) == 0,
174 			 "timer_create() per %s\n", name);
175 }
176 
177 static pthread_t ctd_thread;
178 static volatile int ctd_count, ctd_failed;
179 
180 static void ctd_sighandler(int sig)
181 {
182 	if (pthread_self() != ctd_thread)
183 		ctd_failed = 1;
184 	ctd_count--;
185 }
186 
187 static void *ctd_thread_func(void *arg)
188 {
189 	struct itimerspec val = {
190 		.it_value.tv_sec = 0,
191 		.it_value.tv_nsec = 1000 * 1000,
192 		.it_interval.tv_sec = 0,
193 		.it_interval.tv_nsec = 1000 * 1000,
194 	};
195 	timer_t id;
196 
197 	/* 1/10 seconds to ensure the leader sleeps */
198 	usleep(10000);
199 
200 	ctd_count = 100;
201 	if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id))
202 		fatal_error(NULL, "timer_create()");
203 	if (timer_settime(id, 0, &val, NULL))
204 		fatal_error(NULL, "timer_settime()");
205 	while (ctd_count > 0 && !ctd_failed)
206 		;
207 
208 	if (timer_delete(id))
209 		fatal_error(NULL, "timer_delete()");
210 
211 	return NULL;
212 }
213 
214 /*
215  * Test that only the running thread receives the timer signal.
216  */
217 static void check_timer_distribution(void)
218 {
219 	if (signal(SIGALRM, ctd_sighandler) == SIG_ERR)
220 		fatal_error(NULL, "signal()");
221 
222 	if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL))
223 		fatal_error(NULL, "pthread_create()");
224 
225 	if (pthread_join(ctd_thread, NULL))
226 		fatal_error(NULL, "pthread_join()");
227 
228 	if (!ctd_failed)
229 		ksft_test_result_pass("check signal distribution\n");
230 	else if (ksft_min_kernel_version(6, 3))
231 		ksft_test_result_fail("check signal distribution\n");
232 	else
233 		ksft_test_result_skip("check signal distribution (old kernel)\n");
234 }
235 
236 struct tmrsig {
237 	int	signals;
238 	int	overruns;
239 };
240 
241 static void siginfo_handler(int sig, siginfo_t *si, void *uc)
242 {
243 	struct tmrsig *tsig = si ? si->si_ptr : NULL;
244 
245 	if (tsig) {
246 		tsig->signals++;
247 		tsig->overruns += si->si_overrun;
248 	}
249 }
250 
251 static void *ignore_thread(void *arg)
252 {
253 	unsigned int *tid = arg;
254 	sigset_t set;
255 
256 	sigemptyset(&set);
257 	sigaddset(&set, SIGUSR1);
258 	if (sigprocmask(SIG_BLOCK, &set, NULL))
259 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
260 
261 	*tid = gettid();
262 	sleep(100);
263 
264 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
265 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
266 	return NULL;
267 }
268 
269 static void check_sig_ign(int thread)
270 {
271 	struct tmrsig tsig = { };
272 	struct itimerspec its;
273 	unsigned int tid = 0;
274 	struct sigaction sa;
275 	struct sigevent sev;
276 	pthread_t pthread;
277 	timer_t timerid;
278 	sigset_t set;
279 
280 	if (thread) {
281 		if (pthread_create(&pthread, NULL, ignore_thread, &tid))
282 			fatal_error(NULL, "pthread_create()");
283 		sleep(1);
284 	}
285 
286 	sa.sa_flags = SA_SIGINFO;
287 	sa.sa_sigaction = siginfo_handler;
288 	sigemptyset(&sa.sa_mask);
289 	if (sigaction(SIGUSR1, &sa, NULL))
290 		fatal_error(NULL, "sigaction()");
291 
292 	/* Block the signal */
293 	sigemptyset(&set);
294 	sigaddset(&set, SIGUSR1);
295 	if (sigprocmask(SIG_BLOCK, &set, NULL))
296 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
297 
298 	memset(&sev, 0, sizeof(sev));
299 	sev.sigev_notify = SIGEV_SIGNAL;
300 	sev.sigev_signo = SIGUSR1;
301 	sev.sigev_value.sival_ptr = &tsig;
302 	if (thread) {
303 		sev.sigev_notify = SIGEV_THREAD_ID;
304 		sev._sigev_un._tid = tid;
305 	}
306 
307 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
308 		fatal_error(NULL, "timer_create()");
309 
310 	/* Start the timer to expire in 100ms and 100ms intervals */
311 	its.it_value.tv_sec = 0;
312 	its.it_value.tv_nsec = 100000000;
313 	its.it_interval.tv_sec = 0;
314 	its.it_interval.tv_nsec = 100000000;
315 	timer_settime(timerid, 0, &its, NULL);
316 
317 	sleep(1);
318 
319 	/* Set the signal to be ignored */
320 	if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
321 		fatal_error(NULL, "signal(SIG_IGN)");
322 
323 	sleep(1);
324 
325 	if (thread) {
326 		/* Stop the thread first. No signal should be delivered to it */
327 		if (pthread_cancel(pthread))
328 			fatal_error(NULL, "pthread_cancel()");
329 		if (pthread_join(pthread, NULL))
330 			fatal_error(NULL, "pthread_join()");
331 	}
332 
333 	/* Restore the handler */
334 	if (sigaction(SIGUSR1, &sa, NULL))
335 		fatal_error(NULL, "sigaction()");
336 
337 	sleep(1);
338 
339 	/* Unblock it, which should deliver the signal in the !thread case*/
340 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
341 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
342 
343 	if (timer_delete(timerid))
344 		fatal_error(NULL, "timer_delete()");
345 
346 	if (!thread) {
347 		ksft_test_result(tsig.signals == 1 && tsig.overruns == 29,
348 				 "check_sig_ign SIGEV_SIGNAL\n");
349 	} else {
350 		ksft_test_result(tsig.signals == 0 && tsig.overruns == 0,
351 				 "check_sig_ign SIGEV_THREAD_ID\n");
352 	}
353 }
354 
355 static void check_rearm(void)
356 {
357 	struct tmrsig tsig = { };
358 	struct itimerspec its;
359 	struct sigaction sa;
360 	struct sigevent sev;
361 	timer_t timerid;
362 	sigset_t set;
363 
364 	sa.sa_flags = SA_SIGINFO;
365 	sa.sa_sigaction = siginfo_handler;
366 	sigemptyset(&sa.sa_mask);
367 	if (sigaction(SIGUSR1, &sa, NULL))
368 		fatal_error(NULL, "sigaction()");
369 
370 	/* Block the signal */
371 	sigemptyset(&set);
372 	sigaddset(&set, SIGUSR1);
373 	if (sigprocmask(SIG_BLOCK, &set, NULL))
374 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
375 
376 	memset(&sev, 0, sizeof(sev));
377 	sev.sigev_notify = SIGEV_SIGNAL;
378 	sev.sigev_signo = SIGUSR1;
379 	sev.sigev_value.sival_ptr = &tsig;
380 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
381 		fatal_error(NULL, "timer_create()");
382 
383 	/* Start the timer to expire in 100ms and 100ms intervals */
384 	its.it_value.tv_sec = 0;
385 	its.it_value.tv_nsec = 100000000;
386 	its.it_interval.tv_sec = 0;
387 	its.it_interval.tv_nsec = 100000000;
388 	if (timer_settime(timerid, 0, &its, NULL))
389 		fatal_error(NULL, "timer_settime()");
390 
391 	sleep(1);
392 
393 	/* Reprogram the timer to single shot */
394 	its.it_value.tv_sec = 10;
395 	its.it_value.tv_nsec = 0;
396 	its.it_interval.tv_sec = 0;
397 	its.it_interval.tv_nsec = 0;
398 	if (timer_settime(timerid, 0, &its, NULL))
399 		fatal_error(NULL, "timer_settime()");
400 
401 	/* Unblock it, which should not deliver a signal */
402 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
403 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
404 
405 	if (timer_delete(timerid))
406 		fatal_error(NULL, "timer_delete()");
407 
408 	ksft_test_result(!tsig.signals, "check_rearm\n");
409 }
410 
411 static void check_delete(void)
412 {
413 	struct tmrsig tsig = { };
414 	struct itimerspec its;
415 	struct sigaction sa;
416 	struct sigevent sev;
417 	timer_t timerid;
418 	sigset_t set;
419 
420 	sa.sa_flags = SA_SIGINFO;
421 	sa.sa_sigaction = siginfo_handler;
422 	sigemptyset(&sa.sa_mask);
423 	if (sigaction(SIGUSR1, &sa, NULL))
424 		fatal_error(NULL, "sigaction()");
425 
426 	/* Block the signal */
427 	sigemptyset(&set);
428 	sigaddset(&set, SIGUSR1);
429 	if (sigprocmask(SIG_BLOCK, &set, NULL))
430 		fatal_error(NULL, "sigprocmask(SIG_BLOCK)");
431 
432 	memset(&sev, 0, sizeof(sev));
433 	sev.sigev_notify = SIGEV_SIGNAL;
434 	sev.sigev_signo = SIGUSR1;
435 	sev.sigev_value.sival_ptr = &tsig;
436 	if (timer_create(CLOCK_MONOTONIC, &sev, &timerid))
437 		fatal_error(NULL, "timer_create()");
438 
439 	/* Start the timer to expire in 100ms and 100ms intervals */
440 	its.it_value.tv_sec = 0;
441 	its.it_value.tv_nsec = 100000000;
442 	its.it_interval.tv_sec = 0;
443 	its.it_interval.tv_nsec = 100000000;
444 	if (timer_settime(timerid, 0, &its, NULL))
445 		fatal_error(NULL, "timer_settime()");
446 
447 	sleep(1);
448 
449 	if (timer_delete(timerid))
450 		fatal_error(NULL, "timer_delete()");
451 
452 	/* Unblock it, which should not deliver a signal */
453 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
454 		fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)");
455 
456 	ksft_test_result(!tsig.signals, "check_delete\n");
457 }
458 
459 static void check_sigev_none(int which)
460 {
461 	const char *name = clock_name(which);
462 	struct timespec start, now;
463 	struct itimerspec its;
464 	struct sigevent sev;
465 	timer_t timerid;
466 
467 	memset(&sev, 0, sizeof(sev));
468 	sev.sigev_notify = SIGEV_NONE;
469 
470 	if (timer_create(which, &sev, &timerid))
471 		fatal_error(name, "timer_create()");
472 
473 	/* Start the timer to expire in 100ms and 100ms intervals */
474 	its.it_value.tv_sec = 0;
475 	its.it_value.tv_nsec = 100000000;
476 	its.it_interval.tv_sec = 0;
477 	its.it_interval.tv_nsec = 100000000;
478 	timer_settime(timerid, 0, &its, NULL);
479 
480 	if (clock_gettime(which, &start))
481 		fatal_error(name, "clock_gettime()");
482 
483 	do {
484 		if (clock_gettime(which, &now))
485 			fatal_error(name, "clock_gettime()");
486 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
487 
488 	if (timer_gettime(timerid, &its))
489 		fatal_error(name, "timer_gettime()");
490 
491 	if (timer_delete(timerid))
492 		fatal_error(name, "timer_delete()");
493 
494 	ksft_test_result(its.it_value.tv_sec || its.it_value.tv_nsec,
495 			 "check_sigev_none %s\n", name);
496 }
497 
498 static void check_gettime(int which)
499 {
500 	const char *name = clock_name(which);
501 	struct itimerspec its, prev;
502 	struct timespec start, now;
503 	struct sigevent sev;
504 	timer_t timerid;
505 	int wraps = 0;
506 	sigset_t set;
507 
508 	/* Block the signal */
509 	sigemptyset(&set);
510 	sigaddset(&set, SIGUSR1);
511 	if (sigprocmask(SIG_BLOCK, &set, NULL))
512 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
513 
514 	memset(&sev, 0, sizeof(sev));
515 	sev.sigev_notify = SIGEV_SIGNAL;
516 	sev.sigev_signo = SIGUSR1;
517 
518 	if (timer_create(which, &sev, &timerid))
519 		fatal_error(name, "timer_create()");
520 
521 	/* Start the timer to expire in 100ms and 100ms intervals */
522 	its.it_value.tv_sec = 0;
523 	its.it_value.tv_nsec = 100000000;
524 	its.it_interval.tv_sec = 0;
525 	its.it_interval.tv_nsec = 100000000;
526 	if (timer_settime(timerid, 0, &its, NULL))
527 		fatal_error(name, "timer_settime()");
528 
529 	if (timer_gettime(timerid, &prev))
530 		fatal_error(name, "timer_gettime()");
531 
532 	if (clock_gettime(which, &start))
533 		fatal_error(name, "clock_gettime()");
534 
535 	do {
536 		if (clock_gettime(which, &now))
537 			fatal_error(name, "clock_gettime()");
538 		if (timer_gettime(timerid, &its))
539 			fatal_error(name, "timer_gettime()");
540 		if (its.it_value.tv_nsec > prev.it_value.tv_nsec)
541 			wraps++;
542 		prev = its;
543 
544 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
545 
546 	if (timer_delete(timerid))
547 		fatal_error(name, "timer_delete()");
548 
549 	ksft_test_result(wraps > 1, "check_gettime %s\n", name);
550 }
551 
552 static void check_overrun(int which)
553 {
554 	const char *name = clock_name(which);
555 	struct timespec start, now;
556 	struct tmrsig tsig = { };
557 	struct itimerspec its;
558 	struct sigaction sa;
559 	struct sigevent sev;
560 	timer_t timerid;
561 	sigset_t set;
562 
563 	sa.sa_flags = SA_SIGINFO;
564 	sa.sa_sigaction = siginfo_handler;
565 	sigemptyset(&sa.sa_mask);
566 	if (sigaction(SIGUSR1, &sa, NULL))
567 		fatal_error(name, "sigaction()");
568 
569 	/* Block the signal */
570 	sigemptyset(&set);
571 	sigaddset(&set, SIGUSR1);
572 	if (sigprocmask(SIG_BLOCK, &set, NULL))
573 		fatal_error(name, "sigprocmask(SIG_BLOCK)");
574 
575 	memset(&sev, 0, sizeof(sev));
576 	sev.sigev_notify = SIGEV_SIGNAL;
577 	sev.sigev_signo = SIGUSR1;
578 	sev.sigev_value.sival_ptr = &tsig;
579 	if (timer_create(which, &sev, &timerid))
580 		fatal_error(name, "timer_create()");
581 
582 	/* Start the timer to expire in 100ms and 100ms intervals */
583 	its.it_value.tv_sec = 0;
584 	its.it_value.tv_nsec = 100000000;
585 	its.it_interval.tv_sec = 0;
586 	its.it_interval.tv_nsec = 100000000;
587 	if (timer_settime(timerid, 0, &its, NULL))
588 		fatal_error(name, "timer_settime()");
589 
590 	if (clock_gettime(which, &start))
591 		fatal_error(name, "clock_gettime()");
592 
593 	do {
594 		if (clock_gettime(which, &now))
595 			fatal_error(name, "clock_gettime()");
596 	} while (calcdiff_ns(now, start) < NSEC_PER_SEC);
597 
598 	/* Unblock it, which should deliver a signal */
599 	if (sigprocmask(SIG_UNBLOCK, &set, NULL))
600 		fatal_error(name, "sigprocmask(SIG_UNBLOCK)");
601 
602 	if (timer_delete(timerid))
603 		fatal_error(name, "timer_delete()");
604 
605 	ksft_test_result(tsig.signals == 1 && tsig.overruns == 9,
606 			 "check_overrun %s\n", name);
607 }
608 
609 #include <sys/syscall.h>
610 
611 static int do_timer_create(int *id)
612 {
613 	return syscall(__NR_timer_create, CLOCK_MONOTONIC, NULL, id);
614 }
615 
616 static int do_timer_delete(int id)
617 {
618 	return syscall(__NR_timer_delete, id);
619 }
620 
621 #ifndef PR_TIMER_CREATE_RESTORE_IDS
622 # define PR_TIMER_CREATE_RESTORE_IDS		77
623 # define PR_TIMER_CREATE_RESTORE_IDS_OFF	 0
624 # define PR_TIMER_CREATE_RESTORE_IDS_ON		 1
625 # define PR_TIMER_CREATE_RESTORE_IDS_GET	 2
626 #endif
627 
628 static void check_timer_create_exact(void)
629 {
630 	int id;
631 
632 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_ON, 0, 0, 0)) {
633 		switch (errno) {
634 		case EINVAL:
635 			ksft_test_result_skip("check timer create exact, not supported\n");
636 			return;
637 		default:
638 			ksft_test_result_skip("check timer create exact, errno = %d\n", errno);
639 			return;
640 		}
641 	}
642 
643 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_GET, 0, 0, 0) != 1)
644 		fatal_error(NULL, "prctl(GET) failed\n");
645 
646 	id = 8;
647 	if (do_timer_create(&id) < 0)
648 		fatal_error(NULL, "timer_create()");
649 
650 	if (do_timer_delete(id))
651 		fatal_error(NULL, "timer_delete()");
652 
653 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_OFF, 0, 0, 0))
654 		fatal_error(NULL, "prctl(OFF)");
655 
656 	if (prctl(PR_TIMER_CREATE_RESTORE_IDS, PR_TIMER_CREATE_RESTORE_IDS_GET, 0, 0, 0) != 0)
657 		fatal_error(NULL, "prctl(GET) failed\n");
658 
659 	if (id != 8) {
660 		ksft_test_result_fail("check timer create exact %d != 8\n", id);
661 		return;
662 	}
663 
664 	/* Validate that it went back to normal mode and allocates ID 9 */
665 	if (do_timer_create(&id) < 0)
666 		fatal_error(NULL, "timer_create()");
667 
668 	if (do_timer_delete(id))
669 		fatal_error(NULL, "timer_delete()");
670 
671 	if (id == 9)
672 		ksft_test_result_pass("check timer create exact\n");
673 	else
674 		ksft_test_result_fail("check timer create exact. Disabling failed.\n");
675 }
676 
677 int main(int argc, char **argv)
678 {
679 	bool run_sig_ign_tests = ksft_min_kernel_version(6, 13);
680 
681 	ksft_print_header();
682 	if (run_sig_ign_tests) {
683 		ksft_set_plan(19);
684 	} else {
685 		ksft_set_plan(10);
686 	}
687 
688 	ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
689 	ksft_print_msg("based timers if other threads run on the CPU...\n");
690 
691 	check_timer_create_exact();
692 
693 	check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL");
694 	check_itimer(ITIMER_PROF, "ITIMER_PROF");
695 	check_itimer(ITIMER_REAL, "ITIMER_REAL");
696 	check_timer_create(CLOCK_THREAD_CPUTIME_ID);
697 
698 	/*
699 	 * It's unfortunately hard to reliably test a timer expiration
700 	 * on parallel multithread cputime. We could arm it to expire
701 	 * on DELAY * nr_threads, with nr_threads busy looping, then wait
702 	 * the normal DELAY since the time is elapsing nr_threads faster.
703 	 * But for that we need to ensure we have real physical free CPUs
704 	 * to ensure true parallelism. So test only one thread until we
705 	 * find a better solution.
706 	 */
707 	check_timer_create(CLOCK_PROCESS_CPUTIME_ID);
708 	check_timer_distribution();
709 
710 	if (run_sig_ign_tests) {
711 		check_sig_ign(0);
712 		check_sig_ign(1);
713 		check_rearm();
714 		check_delete();
715 		check_sigev_none(CLOCK_MONOTONIC);
716 		check_sigev_none(CLOCK_PROCESS_CPUTIME_ID);
717 		check_gettime(CLOCK_MONOTONIC);
718 		check_gettime(CLOCK_PROCESS_CPUTIME_ID);
719 		check_gettime(CLOCK_THREAD_CPUTIME_ID);
720 	} else {
721 		ksft_print_msg("Skipping SIG_IGN tests on kernel < 6.13\n");
722 	}
723 
724 	check_overrun(CLOCK_MONOTONIC);
725 	check_overrun(CLOCK_PROCESS_CPUTIME_ID);
726 	check_overrun(CLOCK_THREAD_CPUTIME_ID);
727 
728 	ksft_finished();
729 }
730