1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz> 4 */ 5 6/*\ 7 * [Description] 8 * 9 * Test that clock_nanosleep() adds correctly an offset with absolute timeout 10 * and CLOCK_MONOTONIC inside of a timer namespace. 11 * 12 * After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the 13 * process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC 14 * and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. 15 */ 16 17#include <stdlib.h> 18#include "time64_variants.h" 19#include "tst_safe_clocks.h" 20#include "tst_timer.h" 21#include "lapi/sched.h" 22 23#define OFFSET_S 10 24#define SLEEP_US 100000 25 26static struct time64_variants variants[] = { 27 { .clock_gettime = libc_clock_gettime, .clock_nanosleep = libc_clock_nanosleep, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"}, 28 29#if (__NR_clock_nanosleep != __LTP__NR_INVALID_SYSCALL) 30 { .clock_gettime = sys_clock_gettime, .clock_nanosleep = sys_clock_nanosleep, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"}, 31#endif 32 33#if (__NR_clock_nanosleep_time64 != __LTP__NR_INVALID_SYSCALL) 34 { .clock_gettime = sys_clock_gettime64, .clock_nanosleep = sys_clock_nanosleep64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"}, 35#endif 36}; 37 38static void do_clock_gettime(struct time64_variants *tv, struct tst_ts *ts) 39{ 40 int ret; 41 42 ret = tv->clock_gettime(CLOCK_MONOTONIC, tst_ts_get(ts)); 43 if (ret == -1) 44 tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_MONOTONIC) failed"); 45} 46 47static void verify_clock_nanosleep(void) 48{ 49 struct time64_variants *tv = &variants[tst_variant]; 50 struct tst_ts start, end, sleep_abs; 51 52 tst_res(TINFO, "Testing variant: %s", tv->desc); 53 54 start.type = end.type = sleep_abs.type = tv->ts_type; 55 56 SAFE_UNSHARE(CLONE_NEWTIME); 57 58 SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", CLOCK_MONOTONIC, OFFSET_S); 59 60 do_clock_gettime(tv, &start); 61 62 sleep_abs = tst_ts_add_us(start, 1000000 * OFFSET_S + SLEEP_US); 63 64 if (!SAFE_FORK()) { 65 TEST(tv->clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, tst_ts_get(&sleep_abs), NULL)); 66 /* 67 * The return value and error number are differently set for 68 * libc syscall as compared to kernel syscall. 69 */ 70 if ((tv->clock_nanosleep == libc_clock_nanosleep) && TST_RET) { 71 TST_ERR = TST_RET; 72 TST_RET = -1; 73 } 74 75 if (TST_RET == -1) { 76 tst_res(TFAIL | TTERRNO, "clock_nanosleep(2) failed for clock %s", 77 tst_clock_name(CLOCK_MONOTONIC)); 78 } 79 80 exit(0); 81 } 82 83 SAFE_WAIT(NULL); 84 85 do_clock_gettime(tv, &end); 86 87 long long diff = tst_ts_diff_us(end, start); 88 89 if (diff > 5 * SLEEP_US) { 90 tst_res(TFAIL, "clock_nanosleep() slept too long %lli", diff); 91 return; 92 } 93 94 if (diff < SLEEP_US) { 95 tst_res(TFAIL, "clock_nanosleep() slept too short %lli", diff); 96 return; 97 } 98 99 tst_res(TPASS, "clock_nanosleep() slept correctly %lli", diff); 100} 101 102static struct tst_test test = { 103 .test_all = verify_clock_nanosleep, 104 .test_variants = ARRAY_SIZE(variants), 105 .needs_root = 1, 106 .forks_child = 1, 107 .needs_kconfigs = (const char *[]) { 108 "CONFIG_TIME_NS=y", 109 NULL 110 } 111}; 112