• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5  *
6  * Test PR_GET_TIMERSLACK and PR_SET_TIMERSLACK of prctl(2).
7  * 1)Each thread has two associated timer slack values: a "default"
8  *   value, and a "current" value. PR_SET_TIMERSLACK sets the "current"
9  *   timer slack value for the calling thread.
10  * 2)When a new thread is created, the two timer slack values are made
11  *   the same as the "current" value of the creating thread.
12  * 3)The maximum timer slack value is ULONG_MAX. On 32bit machines, it
13  *   is a valid value(about 4s). On 64bit machines, it is about 500 years
14  *   and no person will set this over 4s.  prctl return value is int, so
15  *   we test themaximum value is INT_MAX.
16  * 4)we also check current value via /proc/self/timerslack_ns if it is
17  *  supported.
18  */
19 
20 #include <sys/prctl.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <linux/limits.h>
25 #include "lapi/syscalls.h"
26 #include "lapi/prctl.h"
27 #include "tst_test.h"
28 
29 #define PROC_TIMERSLACK_PATH "/proc/self/timerslack_ns"
30 
31 static void check_reset_timerslack(char *message);
32 static void check_get_timerslack(char *message, unsigned long value);
33 static void check_inherit_timerslack(char *message, unsigned long value);
34 static unsigned long origin_value;
35 
36 static struct tcase {
37 	void (*func_check)();
38 	unsigned long setvalue;
39 	unsigned long expvalue;
40 	char message[50];
41 } tcases[] = {
42 	{check_reset_timerslack, 0, 50000, "Reset"},
43 	{check_get_timerslack, 1, 1, "Min"},
44 	{check_get_timerslack, 70000, 70000, "Middle"},
45 	{check_get_timerslack, INT_MAX, INT_MAX, "Max"},
46 	{check_inherit_timerslack, 70000, 70000, "Child process"},
47 };
48 
49 static int proc_flag = 1;
50 
check_reset_timerslack(char * message)51 static void check_reset_timerslack(char *message)
52 {
53 	check_get_timerslack(message, origin_value);
54 }
55 
check_get_timerslack(char * message,unsigned long value)56 static void check_get_timerslack(char *message, unsigned long value)
57 {
58 	TEST(prctl(PR_GET_TIMERSLACK));
59 	if ((unsigned long)TST_RET == value)
60 		tst_res(TPASS, "%s prctl(PR_GET_TIMERSLACK) got %lu expectedly",
61 				message, value);
62 	else
63 		tst_res(TFAIL, "%s prctl(PR_GET_TIMERSLACK) expected %lu got %lu",
64 				message, value, TST_RET);
65 
66 	if (proc_flag)
67 		TST_ASSERT_INT(PROC_TIMERSLACK_PATH, value);
68 }
69 
check_inherit_timerslack(char * message,unsigned long value)70 static void check_inherit_timerslack(char *message, unsigned long value)
71 {
72 	int pid;
73 	unsigned long current_value;
74 	unsigned long default_value;
75 
76 	pid = SAFE_FORK();
77 	if (pid == 0) {
78 		current_value = prctl(PR_GET_TIMERSLACK);
79 		prctl(PR_SET_TIMERSLACK, 0);
80 		default_value = prctl(PR_GET_TIMERSLACK);
81 		if (current_value == value && default_value == value)
82 			tst_res(TPASS,
83 				"%s two timer slack values are made the same as the current value(%lu) of the creating thread.",
84 				message, value);
85 		else
86 			tst_res(TFAIL,
87 				"%s current_value is %lu, default value is %lu, the parent current value is %lu",
88 				message, current_value, default_value, value);
89 	}
90 
91 }
92 
verify_prctl(unsigned int n)93 static void verify_prctl(unsigned int n)
94 {
95 	struct tcase *tc = &tcases[n];
96 
97 	TEST(prctl(PR_SET_TIMERSLACK, tc->setvalue));
98 	if (TST_RET == -1) {
99 		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_TIMERSLACK, %lu) failed",
100 					  tc->setvalue);
101 		return;
102 	}
103 
104 	tst_res(TPASS, "prctl(PR_SET_TIMERSLACK, %lu) succeed", tc->setvalue);
105 	tc->func_check(tc->message, tc->expvalue);
106 }
107 
setup(void)108 static void setup(void)
109 {
110 	if (access(PROC_TIMERSLACK_PATH, F_OK) == -1) {
111 		tst_res(TCONF, "proc doesn't support timerslack_ns interface");
112 		proc_flag = 0;
113 	}
114 
115 	TEST(prctl(PR_GET_TIMERSLACK));
116 	origin_value = TST_RET;
117 	tst_res(TINFO, "current timerslack value is %lu", origin_value);
118 }
119 
120 static struct tst_test test = {
121 	.setup = setup,
122 	.test = verify_prctl,
123 	.tcnt = ARRAY_SIZE(tcases),
124 	.forks_child = 1,
125 };
126