1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Basic resctrl file system operations
4  *
5  * Copyright (C) 2018 Intel Corporation
6  *
7  * Authors:
8  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9  *    Fenghua Yu <fenghua.yu@intel.com>
10  */
11 #include "resctrl.h"
12 
13 int tests_run;
14 
15 static int find_resctrl_mount(char *buffer)
16 {
17 	FILE *mounts;
18 	char line[256], *fs, *mntpoint;
19 
20 	mounts = fopen("/proc/mounts", "r");
21 	if (!mounts) {
22 		perror("/proc/mounts");
23 		return -ENXIO;
24 	}
25 	while (!feof(mounts)) {
26 		if (!fgets(line, 256, mounts))
27 			break;
28 		fs = strtok(line, " \t");
29 		if (!fs)
30 			continue;
31 		mntpoint = strtok(NULL, " \t");
32 		if (!mntpoint)
33 			continue;
34 		fs = strtok(NULL, " \t");
35 		if (!fs)
36 			continue;
37 		if (strcmp(fs, "resctrl"))
38 			continue;
39 
40 		fclose(mounts);
41 		if (buffer)
42 			strncpy(buffer, mntpoint, 256);
43 
44 		return 0;
45 	}
46 
47 	fclose(mounts);
48 
49 	return -ENOENT;
50 }
51 
52 char cbm_mask[256];
53 
54 /*
55  * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
56  * @mum_resctrlfs:	Should the resctrl FS be remounted?
57  *
58  * If not mounted, mount it.
59  * If mounted and mum_resctrlfs then remount resctrl FS.
60  * If mounted and !mum_resctrlfs then noop
61  *
62  * Return: 0 on success, non-zero on failure
63  */
64 int remount_resctrlfs(bool mum_resctrlfs)
65 {
66 	char mountpoint[256];
67 	int ret;
68 
69 	ret = find_resctrl_mount(mountpoint);
70 	if (ret)
71 		strcpy(mountpoint, RESCTRL_PATH);
72 
73 	if (!ret && mum_resctrlfs && umount(mountpoint)) {
74 		printf("not ok unmounting \"%s\"\n", mountpoint);
75 		perror("# umount");
76 		tests_run++;
77 	}
78 
79 	if (!ret && !mum_resctrlfs)
80 		return 0;
81 
82 	ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
83 	printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "",
84 	       RESCTRL_PATH);
85 	if (ret)
86 		perror("# mount");
87 
88 	tests_run++;
89 
90 	return ret;
91 }
92 
93 int umount_resctrlfs(void)
94 {
95 	if (umount(RESCTRL_PATH)) {
96 		perror("# Unable to umount resctrl");
97 
98 		return errno;
99 	}
100 
101 	return 0;
102 }
103 
104 /*
105  * get_resource_id - Get socket number/l3 id for a specified CPU
106  * @cpu_no:	CPU number
107  * @resource_id: Socket number or l3_id
108  *
109  * Return: >= 0 on success, < 0 on failure.
110  */
111 int get_resource_id(int cpu_no, int *resource_id)
112 {
113 	char phys_pkg_path[1024];
114 	FILE *fp;
115 
116 	if (is_amd)
117 		sprintf(phys_pkg_path, "%s%d/cache/index3/id",
118 			PHYS_ID_PATH, cpu_no);
119 	else
120 		sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
121 			PHYS_ID_PATH, cpu_no);
122 
123 	fp = fopen(phys_pkg_path, "r");
124 	if (!fp) {
125 		perror("Failed to open physical_package_id");
126 
127 		return -1;
128 	}
129 	if (fscanf(fp, "%d", resource_id) <= 0) {
130 		perror("Could not get socket number or l3 id");
131 		fclose(fp);
132 
133 		return -1;
134 	}
135 	fclose(fp);
136 
137 	return 0;
138 }
139 
140 /*
141  * get_cache_size - Get cache size for a specified CPU
142  * @cpu_no:	CPU number
143  * @cache_type:	Cache level L2/L3
144  * @cache_size:	pointer to cache_size
145  *
146  * Return: = 0 on success, < 0 on failure.
147  */
148 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
149 {
150 	char cache_path[1024], cache_str[64];
151 	int length, i, cache_num;
152 	FILE *fp;
153 
154 	if (!strcmp(cache_type, "L3")) {
155 		cache_num = 3;
156 	} else if (!strcmp(cache_type, "L2")) {
157 		cache_num = 2;
158 	} else {
159 		perror("Invalid cache level");
160 		return -1;
161 	}
162 
163 	sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
164 		cpu_no, cache_num);
165 	fp = fopen(cache_path, "r");
166 	if (!fp) {
167 		perror("Failed to open cache size");
168 
169 		return -1;
170 	}
171 	if (fscanf(fp, "%s", cache_str) <= 0) {
172 		perror("Could not get cache_size");
173 		fclose(fp);
174 
175 		return -1;
176 	}
177 	fclose(fp);
178 
179 	length = (int)strlen(cache_str);
180 
181 	*cache_size = 0;
182 
183 	for (i = 0; i < length; i++) {
184 		if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
185 
186 			*cache_size = *cache_size * 10 + (cache_str[i] - '0');
187 
188 		else if (cache_str[i] == 'K')
189 
190 			*cache_size = *cache_size * 1024;
191 
192 		else if (cache_str[i] == 'M')
193 
194 			*cache_size = *cache_size * 1024 * 1024;
195 
196 		else
197 			break;
198 	}
199 
200 	return 0;
201 }
202 
203 #define CORE_SIBLINGS_PATH	"/sys/bus/cpu/devices/cpu"
204 
205 /*
206  * get_cbm_mask - Get cbm mask for given cache
207  * @cache_type:	Cache level L2/L3
208  *
209  * Mask is stored in cbm_mask which is global variable.
210  *
211  * Return: = 0 on success, < 0 on failure.
212  */
213 int get_cbm_mask(char *cache_type)
214 {
215 	char cbm_mask_path[1024];
216 	FILE *fp;
217 
218 	sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type);
219 
220 	fp = fopen(cbm_mask_path, "r");
221 	if (!fp) {
222 		perror("Failed to open cache level");
223 
224 		return -1;
225 	}
226 	if (fscanf(fp, "%s", cbm_mask) <= 0) {
227 		perror("Could not get max cbm_mask");
228 		fclose(fp);
229 
230 		return -1;
231 	}
232 	fclose(fp);
233 
234 	return 0;
235 }
236 
237 /*
238  * get_core_sibling - Get sibling core id from the same socket for given CPU
239  * @cpu_no:	CPU number
240  *
241  * Return:	> 0 on success, < 0 on failure.
242  */
243 int get_core_sibling(int cpu_no)
244 {
245 	char core_siblings_path[1024], cpu_list_str[64];
246 	int sibling_cpu_no = -1;
247 	FILE *fp;
248 
249 	sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
250 		CORE_SIBLINGS_PATH, cpu_no);
251 
252 	fp = fopen(core_siblings_path, "r");
253 	if (!fp) {
254 		perror("Failed to open core siblings path");
255 
256 		return -1;
257 	}
258 	if (fscanf(fp, "%s", cpu_list_str) <= 0) {
259 		perror("Could not get core_siblings list");
260 		fclose(fp);
261 
262 		return -1;
263 	}
264 	fclose(fp);
265 
266 	char *token = strtok(cpu_list_str, "-,");
267 
268 	while (token) {
269 		sibling_cpu_no = atoi(token);
270 		/* Skipping core 0 as we don't want to run test on core 0 */
271 		if (sibling_cpu_no != 0)
272 			break;
273 		token = strtok(NULL, "-,");
274 	}
275 
276 	return sibling_cpu_no;
277 }
278 
279 /*
280  * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
281  * @bm_pid:	PID that should be binded
282  * @cpu_no:	CPU number at which the PID would be binded
283  *
284  * Return: 0 on success, non-zero on failure
285  */
286 int taskset_benchmark(pid_t bm_pid, int cpu_no)
287 {
288 	cpu_set_t my_set;
289 
290 	CPU_ZERO(&my_set);
291 	CPU_SET(cpu_no, &my_set);
292 
293 	if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
294 		perror("Unable to taskset benchmark");
295 
296 		return -1;
297 	}
298 
299 	return 0;
300 }
301 
302 /*
303  * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
304  *		   in specified signal. Direct benchmark stdio to /dev/null.
305  * @signum:	signal number
306  * @info:	signal info
307  * @ucontext:	user context in signal handling
308  *
309  * Return: void
310  */
311 void run_benchmark(int signum, siginfo_t *info, void *ucontext)
312 {
313 	int operation, ret, malloc_and_init_memory, memflush;
314 	unsigned long span, buffer_span;
315 	char **benchmark_cmd;
316 	char resctrl_val[64];
317 	FILE *fp;
318 
319 	benchmark_cmd = info->si_ptr;
320 
321 	/*
322 	 * Direct stdio of child to /dev/null, so that only parent writes to
323 	 * stdio (console)
324 	 */
325 	fp = freopen("/dev/null", "w", stdout);
326 	if (!fp)
327 		PARENT_EXIT("Unable to direct benchmark status to /dev/null");
328 
329 	if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
330 		/* Execute default fill_buf benchmark */
331 		span = strtoul(benchmark_cmd[1], NULL, 10);
332 		malloc_and_init_memory = atoi(benchmark_cmd[2]);
333 		memflush =  atoi(benchmark_cmd[3]);
334 		operation = atoi(benchmark_cmd[4]);
335 		sprintf(resctrl_val, "%s", benchmark_cmd[5]);
336 
337 		if (strcmp(resctrl_val, "cqm") != 0)
338 			buffer_span = span * MB;
339 		else
340 			buffer_span = span;
341 
342 		if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush,
343 				 operation, resctrl_val))
344 			fprintf(stderr, "Error in running fill buffer\n");
345 	} else {
346 		/* Execute specified benchmark */
347 		ret = execvp(benchmark_cmd[0], benchmark_cmd);
348 		if (ret)
349 			perror("wrong\n");
350 	}
351 
352 	fclose(stdout);
353 	PARENT_EXIT("Unable to run specified benchmark");
354 }
355 
356 /*
357  * create_grp - Create a group only if one doesn't exist
358  * @grp_name:	Name of the group
359  * @grp:	Full path and name of the group
360  * @parent_grp:	Full path and name of the parent group
361  *
362  * Return: 0 on success, non-zero on failure
363  */
364 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
365 {
366 	int found_grp = 0;
367 	struct dirent *ep;
368 	DIR *dp;
369 
370 	/*
371 	 * At this point, we are guaranteed to have resctrl FS mounted and if
372 	 * length of grp_name == 0, it means, user wants to use root con_mon
373 	 * grp, so do nothing
374 	 */
375 	if (strlen(grp_name) == 0)
376 		return 0;
377 
378 	/* Check if requested grp exists or not */
379 	dp = opendir(parent_grp);
380 	if (dp) {
381 		while ((ep = readdir(dp)) != NULL) {
382 			if (strcmp(ep->d_name, grp_name) == 0)
383 				found_grp = 1;
384 		}
385 		closedir(dp);
386 	} else {
387 		perror("Unable to open resctrl for group");
388 
389 		return -1;
390 	}
391 
392 	/* Requested grp doesn't exist, hence create it */
393 	if (found_grp == 0) {
394 		if (mkdir(grp, 0) == -1) {
395 			perror("Unable to create group");
396 
397 			return -1;
398 		}
399 	}
400 
401 	return 0;
402 }
403 
404 static int write_pid_to_tasks(char *tasks, pid_t pid)
405 {
406 	FILE *fp;
407 
408 	fp = fopen(tasks, "w");
409 	if (!fp) {
410 		perror("Failed to open tasks file");
411 
412 		return -1;
413 	}
414 	if (fprintf(fp, "%d\n", pid) < 0) {
415 		perror("Failed to wr pid to tasks file");
416 		fclose(fp);
417 
418 		return -1;
419 	}
420 	fclose(fp);
421 
422 	return 0;
423 }
424 
425 /*
426  * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
427  * @bm_pid:		PID that should be written
428  * @ctrlgrp:		Name of the control monitor group (con_mon grp)
429  * @mongrp:		Name of the monitor group (mon grp)
430  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
431  *
432  * If a con_mon grp is requested, create it and write pid to it, otherwise
433  * write pid to root con_mon grp.
434  * If a mon grp is requested, create it and write pid to it, otherwise
435  * pid is not written, this means that pid is in con_mon grp and hence
436  * should consult con_mon grp's mon_data directory for results.
437  *
438  * Return: 0 on success, non-zero on failure
439  */
440 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
441 			    char *resctrl_val)
442 {
443 	char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
444 	char tasks[1024];
445 	int ret = 0;
446 
447 	if (strlen(ctrlgrp))
448 		sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
449 	else
450 		sprintf(controlgroup, "%s", RESCTRL_PATH);
451 
452 	/* Create control and monitoring group and write pid into it */
453 	ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
454 	if (ret)
455 		goto out;
456 	sprintf(tasks, "%s/tasks", controlgroup);
457 	ret = write_pid_to_tasks(tasks, bm_pid);
458 	if (ret)
459 		goto out;
460 
461 	/* Create mon grp and write pid into it for "mbm" and "cqm" test */
462 	if ((strcmp(resctrl_val, "cqm") == 0) ||
463 	    (strcmp(resctrl_val, "mbm") == 0)) {
464 		if (strlen(mongrp)) {
465 			sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
466 			sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
467 			ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
468 			if (ret)
469 				goto out;
470 
471 			sprintf(tasks, "%s/mon_groups/%s/tasks",
472 				controlgroup, mongrp);
473 			ret = write_pid_to_tasks(tasks, bm_pid);
474 			if (ret)
475 				goto out;
476 		}
477 	}
478 
479 out:
480 	printf("%sok writing benchmark parameters to resctrl FS\n",
481 	       ret ? "not " : "");
482 	if (ret)
483 		perror("# writing to resctrlfs");
484 
485 	tests_run++;
486 
487 	return ret;
488 }
489 
490 /*
491  * write_schemata - Update schemata of a con_mon grp
492  * @ctrlgrp:		Name of the con_mon grp
493  * @schemata:		Schemata that should be updated to
494  * @cpu_no:		CPU number that the benchmark PID is binded to
495  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
496  *
497  * Update schemata of a con_mon grp *only* if requested resctrl feature is
498  * allocation type
499  *
500  * Return: 0 on success, non-zero on failure
501  */
502 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
503 {
504 	char controlgroup[1024], schema[1024], reason[64];
505 	int resource_id, ret = 0;
506 	FILE *fp;
507 
508 	if ((strcmp(resctrl_val, "mba") != 0) &&
509 	    (strcmp(resctrl_val, "cat") != 0) &&
510 	    (strcmp(resctrl_val, "cqm") != 0))
511 		return -ENOENT;
512 
513 	if (!schemata) {
514 		printf("# Skipping empty schemata update\n");
515 
516 		return -1;
517 	}
518 
519 	if (get_resource_id(cpu_no, &resource_id) < 0) {
520 		sprintf(reason, "Failed to get resource id");
521 		ret = -1;
522 
523 		goto out;
524 	}
525 
526 	if (strlen(ctrlgrp) != 0)
527 		sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
528 	else
529 		sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
530 
531 	if (!strcmp(resctrl_val, "cat") || !strcmp(resctrl_val, "cqm"))
532 		sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
533 	if (strcmp(resctrl_val, "mba") == 0)
534 		sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
535 
536 	fp = fopen(controlgroup, "w");
537 	if (!fp) {
538 		sprintf(reason, "Failed to open control group");
539 		ret = -1;
540 
541 		goto out;
542 	}
543 
544 	if (fprintf(fp, "%s\n", schema) < 0) {
545 		sprintf(reason, "Failed to write schemata in control group");
546 		fclose(fp);
547 		ret = -1;
548 
549 		goto out;
550 	}
551 	fclose(fp);
552 
553 out:
554 	printf("%sok Write schema \"%s\" to resctrl FS%s%s\n",
555 	       ret ? "not " : "", schema, ret ? " # " : "",
556 	       ret ? reason : "");
557 	tests_run++;
558 
559 	return ret;
560 }
561 
562 bool check_resctrlfs_support(void)
563 {
564 	FILE *inf = fopen("/proc/filesystems", "r");
565 	DIR *dp;
566 	char *res;
567 	bool ret = false;
568 
569 	if (!inf)
570 		return false;
571 
572 	res = fgrep(inf, "nodev\tresctrl\n");
573 
574 	if (res) {
575 		ret = true;
576 		free(res);
577 	}
578 
579 	fclose(inf);
580 
581 	printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not ");
582 	tests_run++;
583 
584 	dp = opendir(RESCTRL_PATH);
585 	printf("%sok resctrl mountpoint \"%s\" exists\n",
586 	       dp ? "" : "not ", RESCTRL_PATH);
587 	if (dp)
588 		closedir(dp);
589 	tests_run++;
590 
591 	printf("# resctrl filesystem %s mounted\n",
592 	       find_resctrl_mount(NULL) ? "not" : "is");
593 
594 	return ret;
595 }
596 
597 char *fgrep(FILE *inf, const char *str)
598 {
599 	char line[256];
600 	int slen = strlen(str);
601 
602 	while (!feof(inf)) {
603 		if (!fgets(line, 256, inf))
604 			break;
605 		if (strncmp(line, str, slen))
606 			continue;
607 
608 		return strdup(line);
609 	}
610 
611 	return NULL;
612 }
613 
614 /*
615  * validate_resctrl_feature_request - Check if requested feature is valid.
616  * @resctrl_val:	Requested feature
617  *
618  * Return: 0 on success, non-zero on failure
619  */
620 bool validate_resctrl_feature_request(char *resctrl_val)
621 {
622 	FILE *inf = fopen("/proc/cpuinfo", "r");
623 	bool found = false;
624 	char *res;
625 
626 	if (!inf)
627 		return false;
628 
629 	res = fgrep(inf, "flags");
630 
631 	if (res) {
632 		char *s = strchr(res, ':');
633 
634 		found = s && !strstr(s, resctrl_val);
635 		free(res);
636 	}
637 	fclose(inf);
638 
639 	return found;
640 }
641 
642 int filter_dmesg(void)
643 {
644 	char line[1024];
645 	FILE *fp;
646 	int pipefds[2];
647 	pid_t pid;
648 	int ret;
649 
650 	ret = pipe(pipefds);
651 	if (ret) {
652 		perror("pipe");
653 		return ret;
654 	}
655 	pid = fork();
656 	if (pid == 0) {
657 		close(pipefds[0]);
658 		dup2(pipefds[1], STDOUT_FILENO);
659 		execlp("dmesg", "dmesg", NULL);
660 		perror("executing dmesg");
661 		exit(1);
662 	}
663 	close(pipefds[1]);
664 	fp = fdopen(pipefds[0], "r");
665 	if (!fp) {
666 		perror("fdopen(pipe)");
667 		kill(pid, SIGTERM);
668 
669 		return -1;
670 	}
671 
672 	while (fgets(line, 1024, fp)) {
673 		if (strstr(line, "intel_rdt:"))
674 			printf("# dmesg: %s", line);
675 		if (strstr(line, "resctrl:"))
676 			printf("# dmesg: %s", line);
677 	}
678 	fclose(fp);
679 	waitpid(pid, NULL, 0);
680 
681 	return 0;
682 }
683 
684 int validate_bw_report_request(char *bw_report)
685 {
686 	if (strcmp(bw_report, "reads") == 0)
687 		return 0;
688 	if (strcmp(bw_report, "writes") == 0)
689 		return 0;
690 	if (strcmp(bw_report, "nt-writes") == 0) {
691 		strcpy(bw_report, "writes");
692 		return 0;
693 	}
694 	if (strcmp(bw_report, "total") == 0)
695 		return 0;
696 
697 	fprintf(stderr, "Requested iMC B/W report type unavailable\n");
698 
699 	return -1;
700 }
701 
702 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
703 		    int group_fd, unsigned long flags)
704 {
705 	int ret;
706 
707 	ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
708 		      group_fd, flags);
709 	return ret;
710 }
711 
712 unsigned int count_bits(unsigned long n)
713 {
714 	unsigned int count = 0;
715 
716 	while (n) {
717 		count += n & 1;
718 		n >>= 1;
719 	}
720 
721 	return count;
722 }
723