1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Speed Select -- Allow speed select to daemonize
4  * Copyright (c) 2022 Intel Corporation.
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/file.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <signal.h>
19 #include <time.h>
20 
21 #include "isst.h"
22 
23 static int per_package_levels_info[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
24 static time_t per_package_levels_tm[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE];
25 
26 static void init_levels(void)
27 {
28 	int i, j, k;
29 
30 	for (i = 0; i < MAX_PACKAGE_COUNT; ++i)
31 		for (j = 0; j < MAX_DIE_PER_PACKAGE; ++j)
32 			for (k = 0; k < MAX_PUNIT_PER_DIE; ++k)
33 				per_package_levels_info[i][j][k] = -1;
34 }
35 
36 void process_level_change(struct isst_id *id)
37 {
38 	struct isst_pkg_ctdp_level_info ctdp_level;
39 	struct isst_pkg_ctdp pkg_dev;
40 	time_t tm;
41 	int ret;
42 
43 	if (id->pkg < 0 || id->die < 0 || id->punit < 0) {
44 		debug_printf("Invalid package/die info for cpu:%d\n", id->cpu);
45 		return;
46 	}
47 
48 	tm = time(NULL);
49 	if (tm - per_package_levels_tm[id->pkg][id->die][id->punit] < 2)
50 		return;
51 
52 	per_package_levels_tm[id->pkg][id->die][id->punit] = tm;
53 
54 	ret = isst_get_ctdp_levels(id, &pkg_dev);
55 	if (ret) {
56 		debug_printf("Can't get tdp levels for cpu:%d\n", id->cpu);
57 		return;
58 	}
59 
60 	debug_printf("Get Config level %d pkg:%d die:%d current_level:%d\n", id->cpu,
61 		      id->pkg, id->die, pkg_dev.current_level);
62 
63 	if (pkg_dev.locked) {
64 		debug_printf("config TDP s locked \n");
65 		return;
66 	}
67 
68 	if (per_package_levels_info[id->pkg][id->die][id->punit] == pkg_dev.current_level)
69 		return;
70 
71 	debug_printf("**Config level change for cpu:%d pkg:%d die:%d from %d to %d\n",
72 		      id->cpu, id->pkg, id->die, per_package_levels_info[id->pkg][id->die][id->punit],
73 		      pkg_dev.current_level);
74 
75 	per_package_levels_info[id->pkg][id->die][id->punit] = pkg_dev.current_level;
76 
77 	ctdp_level.core_cpumask_size =
78 		alloc_cpu_set(&ctdp_level.core_cpumask);
79 	ret = isst_get_coremask_info(id, pkg_dev.current_level, &ctdp_level);
80 	if (ret) {
81 		free_cpu_set(ctdp_level.core_cpumask);
82 		debug_printf("Can't get core_mask:%d\n", id->cpu);
83 		return;
84 	}
85 
86 	if (use_cgroupv2()) {
87 		int ret;
88 
89 		ret = enable_cpuset_controller();
90 		if (ret)
91 			goto use_offline;
92 
93 		isolate_cpus(id, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask, pkg_dev.current_level);
94 
95 		goto free_mask;
96 	}
97 
98 use_offline:
99 	if (ctdp_level.cpu_count) {
100 		int i, max_cpus = get_topo_max_cpus();
101 		for (i = 0; i < max_cpus; ++i) {
102 			if (!is_cpu_in_power_domain(i, id))
103 				continue;
104 			if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
105 				fprintf(stderr, "online cpu %d\n", i);
106 				set_cpu_online_offline(i, 1);
107 			} else {
108 				fprintf(stderr, "offline cpu %d\n", i);
109 				set_cpu_online_offline(i, 0);
110 			}
111 		}
112 	}
113 free_mask:
114 	free_cpu_set(ctdp_level.core_cpumask);
115 }
116 
117 static void _poll_for_config_change(struct isst_id *id, void *arg1, void *arg2,
118 				    void *arg3, void *arg4)
119 {
120 	process_level_change(id);
121 }
122 
123 static void poll_for_config_change(void)
124 {
125 	for_each_online_power_domain_in_set(_poll_for_config_change, NULL, NULL,
126 				       NULL, NULL);
127 }
128 
129 static int done = 0;
130 static int pid_file_handle;
131 
132 static void signal_handler(int sig)
133 {
134 	switch (sig) {
135 	case SIGINT:
136 	case SIGTERM:
137 		done = 1;
138 		hfi_exit();
139 		exit(0);
140 		break;
141 	default:
142 		break;
143 	}
144 }
145 
146 static void daemonize(char *rundir, char *pidfile)
147 {
148 	int pid, sid, i;
149 	char str[10];
150 	struct sigaction sig_actions;
151 	sigset_t sig_set;
152 	int ret;
153 
154 	if (getppid() == 1)
155 		return;
156 
157 	sigemptyset(&sig_set);
158 	sigaddset(&sig_set, SIGCHLD);
159 	sigaddset(&sig_set, SIGTSTP);
160 	sigaddset(&sig_set, SIGTTOU);
161 	sigaddset(&sig_set, SIGTTIN);
162 	sigprocmask(SIG_BLOCK, &sig_set, NULL);
163 
164 	sig_actions.sa_handler = signal_handler;
165 	sigemptyset(&sig_actions.sa_mask);
166 	sig_actions.sa_flags = 0;
167 
168 	sigaction(SIGHUP, &sig_actions, NULL);
169 	sigaction(SIGTERM, &sig_actions, NULL);
170 	sigaction(SIGINT, &sig_actions, NULL);
171 
172 	pid = fork();
173 	if (pid < 0) {
174 		/* Could not fork */
175 		exit(EXIT_FAILURE);
176 	}
177 	if (pid > 0)
178 		exit(EXIT_SUCCESS);
179 
180 	umask(027);
181 
182 	sid = setsid();
183 	if (sid < 0)
184 		exit(EXIT_FAILURE);
185 
186 	/* close all descriptors */
187 	for (i = getdtablesize(); i >= 0; --i)
188 		close(i);
189 
190 	i = open("/dev/null", O_RDWR);
191 	if (i < 0)
192 		exit(EXIT_FAILURE);
193 
194 	ret = dup(i);
195 	if (ret == -1)
196 		exit(EXIT_FAILURE);
197 
198 	ret = chdir(rundir);
199 	if (ret == -1)
200 		exit(EXIT_FAILURE);
201 
202 	pid_file_handle = open(pidfile, O_RDWR | O_CREAT, 0600);
203 	if (pid_file_handle == -1) {
204 		/* Couldn't open lock file */
205 		exit(1);
206 	}
207 	/* Try to lock file */
208 #ifdef LOCKF_SUPPORT
209 	if (lockf(pid_file_handle, F_TLOCK, 0) == -1) {
210 #else
211 	if (flock(pid_file_handle, LOCK_EX|LOCK_NB) < 0) {
212 #endif
213 		/* Couldn't get lock on lock file */
214 		fprintf(stderr, "Couldn't get lock file %d\n", getpid());
215 		exit(1);
216 	}
217 	snprintf(str, sizeof(str), "%d\n", getpid());
218 	ret = write(pid_file_handle, str, strlen(str));
219 	if (ret == -1)
220 		exit(EXIT_FAILURE);
221 
222 	close(i);
223 }
224 
225 int isst_daemon(int debug_mode, int poll_interval, int no_daemon)
226 {
227 	int ret;
228 
229 	if (!no_daemon && poll_interval < 0 && !debug_mode) {
230 		fprintf(stderr, "OOB mode is enabled and will run as daemon\n");
231 		daemonize((char *) "/tmp/",
232 				(char *)"/tmp/hfi-events.pid");
233 	} else {
234 		signal(SIGINT, signal_handler);
235 	}
236 
237 	init_levels();
238 
239 	if (poll_interval < 0) {
240 		ret = hfi_main();
241 		if (ret) {
242 			fprintf(stderr, "HFI initialization failed\n");
243 		}
244 		fprintf(stderr, "Must specify poll-interval\n");
245 		return ret;
246 	}
247 
248 	debug_printf("Starting loop\n");
249 	while (!done) {
250 		sleep(poll_interval);
251 		poll_for_config_change();
252 	}
253 
254 	return 0;
255 }
256