1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/* 7f08c3bdfSopenharmony_ci * [Description] 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the 10f08c3bdfSopenharmony_ci * process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC 11f08c3bdfSopenharmony_ci * and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * The child processes also switch to the initial parent namespace and checks 14f08c3bdfSopenharmony_ci * that the offset is set to 0. 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#define _GNU_SOURCE 18f08c3bdfSopenharmony_ci#include "time64_variants.h" 19f08c3bdfSopenharmony_ci#include "tst_safe_clocks.h" 20f08c3bdfSopenharmony_ci#include "tst_timer.h" 21f08c3bdfSopenharmony_ci#include "lapi/sched.h" 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_cistatic struct tcase { 24f08c3bdfSopenharmony_ci int clk_id; 25f08c3bdfSopenharmony_ci int clk_off; 26f08c3bdfSopenharmony_ci int off; 27f08c3bdfSopenharmony_ci} tcases[] = { 28f08c3bdfSopenharmony_ci {CLOCK_MONOTONIC, CLOCK_MONOTONIC, 10}, 29f08c3bdfSopenharmony_ci {CLOCK_BOOTTIME, CLOCK_BOOTTIME, 10}, 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci {CLOCK_MONOTONIC, CLOCK_MONOTONIC, -10}, 32f08c3bdfSopenharmony_ci {CLOCK_BOOTTIME, CLOCK_BOOTTIME, -10}, 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci {CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC, 100}, 35f08c3bdfSopenharmony_ci {CLOCK_MONOTONIC_COARSE, CLOCK_MONOTONIC, 100}, 36f08c3bdfSopenharmony_ci}; 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic struct tst_ts now, then, parent_then; 39f08c3bdfSopenharmony_cistatic int parent_ns; 40f08c3bdfSopenharmony_cistatic long long delta = 10; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic struct time64_variants variants[] = { 43f08c3bdfSopenharmony_ci { .clock_gettime = libc_clock_gettime, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"}, 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci#if (__NR_clock_gettime != __LTP__NR_INVALID_SYSCALL) 46f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"}, 47f08c3bdfSopenharmony_ci#endif 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci#if (__NR_clock_gettime64 != __LTP__NR_INVALID_SYSCALL) 50f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"}, 51f08c3bdfSopenharmony_ci#endif 52f08c3bdfSopenharmony_ci}; 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void child(struct time64_variants *tv, struct tcase *tc) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci long long diff; 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci if (tv->clock_gettime(tc->clk_id, tst_ts_get(&then))) { 59f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed", 60f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id)); 61f08c3bdfSopenharmony_ci return; 62f08c3bdfSopenharmony_ci } 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci SAFE_SETNS(parent_ns, CLONE_NEWTIME); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci if (tv->clock_gettime(tc->clk_id, tst_ts_get(&parent_then))) { 67f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "clock_gettime(%s) failed", 68f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id)); 69f08c3bdfSopenharmony_ci return; 70f08c3bdfSopenharmony_ci } 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci diff = tst_ts_diff_ms(then, now); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci if (diff - tc->off * 1000 > delta) { 75f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong offset (%s) read %llims", 76f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id), diff); 77f08c3bdfSopenharmony_ci } else { 78f08c3bdfSopenharmony_ci tst_res(TPASS, "Offset (%s) is correct %llims", 79f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id), diff); 80f08c3bdfSopenharmony_ci } 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci diff = tst_ts_diff_ms(parent_then, now); 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci if (diff > delta) { 85f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong offset (%s) read %llims", 86f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id), diff); 87f08c3bdfSopenharmony_ci } else { 88f08c3bdfSopenharmony_ci tst_res(TPASS, "Offset (%s) is correct %llims", 89f08c3bdfSopenharmony_ci tst_clock_name(tc->clk_id), diff); 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci} 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_cistatic void verify_ns_clock(unsigned int n) 94f08c3bdfSopenharmony_ci{ 95f08c3bdfSopenharmony_ci struct time64_variants *tv = &variants[tst_variant]; 96f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci SAFE_UNSHARE(CLONE_NEWTIME); 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", 101f08c3bdfSopenharmony_ci tc->clk_off, tc->off); 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci if (tv->clock_gettime(tc->clk_id, tst_ts_get(&now))) { 104f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "%d clock_gettime(%s) failed", 105f08c3bdfSopenharmony_ci __LINE__, tst_clock_name(tc->clk_id)); 106f08c3bdfSopenharmony_ci return; 107f08c3bdfSopenharmony_ci } 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci if (!SAFE_FORK()) 110f08c3bdfSopenharmony_ci child(tv, tc); 111f08c3bdfSopenharmony_ci} 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_cistatic void setup(void) 114f08c3bdfSopenharmony_ci{ 115f08c3bdfSopenharmony_ci struct time64_variants *tv = &variants[tst_variant]; 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci if (tst_is_virt(VIRT_ANY)) { 118f08c3bdfSopenharmony_ci tst_res(TINFO, "Running in a VM, multiply the delta by 10."); 119f08c3bdfSopenharmony_ci delta *= 10; 120f08c3bdfSopenharmony_ci } 121f08c3bdfSopenharmony_ci 122f08c3bdfSopenharmony_ci now.type = then.type = parent_then.type = tv->ts_type; 123f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc); 124f08c3bdfSopenharmony_ci parent_ns = SAFE_OPEN("/proc/self/ns/time_for_children", O_RDONLY); 125f08c3bdfSopenharmony_ci} 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_cistatic void cleanup(void) 128f08c3bdfSopenharmony_ci{ 129f08c3bdfSopenharmony_ci SAFE_CLOSE(parent_ns); 130f08c3bdfSopenharmony_ci} 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_cistatic struct tst_test test = { 133f08c3bdfSopenharmony_ci .setup = setup, 134f08c3bdfSopenharmony_ci .cleanup = cleanup, 135f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 136f08c3bdfSopenharmony_ci .test = verify_ns_clock, 137f08c3bdfSopenharmony_ci .test_variants = ARRAY_SIZE(variants), 138f08c3bdfSopenharmony_ci .needs_root = 1, 139f08c3bdfSopenharmony_ci .forks_child = 1, 140f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]) { 141f08c3bdfSopenharmony_ci "CONFIG_TIME_NS=y", 142f08c3bdfSopenharmony_ci NULL 143f08c3bdfSopenharmony_ci } 144f08c3bdfSopenharmony_ci}; 145