Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "timens.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Test shouldn't be run for a day, so add 10 days to child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * time and check parent's time to be in the same day.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define DAY_IN_SEC			(60*60*24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define TEN_DAYS_IN_SEC			(10*DAY_IN_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct test_clock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	clockid_t id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 * off_id is -1 if a clock has own offset, or it contains an index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 * which contains a right offset of this clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int off_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	time_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ct(clock, off_id)	{ clock, #clock, off_id }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static struct test_clock clocks[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	ct(CLOCK_BOOTTIME, -1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	ct(CLOCK_BOOTTIME_ALARM, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	ct(CLOCK_MONOTONIC, -1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	ct(CLOCK_MONOTONIC_COARSE, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	ct(CLOCK_MONOTONIC_RAW, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #undef ct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int child_ns, parent_ns = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int switch_ns(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (setns(fd, CLONE_NEWTIME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		pr_perror("setns()");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int init_namespaces(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	char path[] = "/proc/self/ns/time_for_children";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct stat st1, st2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (parent_ns == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		parent_ns = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (parent_ns <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			return pr_perror("Unable to open %s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (fstat(parent_ns, &st1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return pr_perror("Unable to stat the parent timens");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (unshare_timens())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return  -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	child_ns = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (child_ns <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return pr_perror("Unable to open %s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (fstat(child_ns, &st2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return pr_perror("Unable to stat the timens");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (st1.st_ino == st2.st_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return pr_perror("The same child_ns after CLONE_NEWTIME");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int test_gettime(clockid_t clock_index, bool raw_syscall, time_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct timespec child_ts_new, parent_ts_old, cur_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	char *entry = raw_syscall ? "syscall" : "vdso";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	double precision = 0.0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (check_skip(clocks[clock_index].id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	switch (clocks[clock_index].id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	case CLOCK_MONOTONIC_COARSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	case CLOCK_MONOTONIC_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		precision = -2.0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (switch_ns(parent_ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return pr_err("switch_ns(%d)", child_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (_gettime(clocks[clock_index].id, &parent_ts_old, raw_syscall))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	child_ts_new.tv_nsec = parent_ts_old.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	child_ts_new.tv_sec = parent_ts_old.tv_sec + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (switch_ns(child_ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return pr_err("switch_ns(%d)", child_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (_gettime(clocks[clock_index].id, &cur_ts, raw_syscall))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (difftime(cur_ts.tv_sec, child_ts_new.tv_sec) < precision) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		ksft_test_result_fail(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			"Child's %s (%s) time has not changed: %lu -> %lu [%lu]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			clocks[clock_index].name, entry, parent_ts_old.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			child_ts_new.tv_sec, cur_ts.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (switch_ns(parent_ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return pr_err("switch_ns(%d)", parent_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (_gettime(clocks[clock_index].id, &cur_ts, raw_syscall))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (difftime(cur_ts.tv_sec, parent_ts_old.tv_sec) > DAY_IN_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ksft_test_result_fail(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			"Parent's %s (%s) time has changed: %lu -> %lu [%lu]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			clocks[clock_index].name, entry, parent_ts_old.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			child_ts_new.tv_sec, cur_ts.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		/* Let's play nice and put it closer to original */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		clock_settime(clocks[clock_index].id, &cur_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	ksft_test_result_pass("Passed for %s (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				clocks[clock_index].name, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	time_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	nscheck();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	check_supported_timers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	ksft_set_plan(ARRAY_SIZE(clocks) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (init_namespaces())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* Offsets have to be set before tasks enter the namespace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	for (i = 0; i < ARRAY_SIZE(clocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (clocks[i].off_id != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		offset = TEN_DAYS_IN_SEC + i * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		clocks[i].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (_settime(clocks[i].id, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	for (i = 0; i < ARRAY_SIZE(clocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (clocks[i].off_id != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			offset = clocks[clocks[i].off_id].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			offset = clocks[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		ret |= test_gettime(i, true, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		ret |= test_gettime(i, false, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return !!ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }