xref: /linux/tools/testing/selftests/bpf/prog_tests/tp_attach_query.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 void serial_test_tp_attach_query(void)
5 {
6 	const int num_progs = 3;
7 	int i, j, bytes, efd, err, prog_fd[num_progs], pmu_fd[num_progs];
8 	__u32 duration = 0, info_len, saved_prog_ids[num_progs];
9 	const char *file = "./test_tracepoint.bpf.o";
10 	struct perf_event_query_bpf *query;
11 	struct perf_event_attr attr = {};
12 	struct bpf_object *obj[num_progs];
13 	struct bpf_prog_info prog_info;
14 	char buf[256];
15 
16 	for (i = 0; i < num_progs; i++)
17 		obj[i] = NULL;
18 
19 	if (access("/sys/kernel/tracing/trace", F_OK) == 0) {
20 		snprintf(buf, sizeof(buf),
21 			 "/sys/kernel/tracing/events/sched/sched_switch/id");
22 	} else {
23 		snprintf(buf, sizeof(buf),
24 			 "/sys/kernel/debug/tracing/events/sched/sched_switch/id");
25 	}
26 	efd = open(buf, O_RDONLY, 0);
27 	if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno))
28 		return;
29 	bytes = read(efd, buf, sizeof(buf));
30 	close(efd);
31 	if (CHECK(bytes <= 0 || bytes >= sizeof(buf),
32 		  "read", "bytes %d errno %d\n", bytes, errno))
33 		return;
34 
35 	attr.config = strtol(buf, NULL, 0);
36 	attr.type = PERF_TYPE_TRACEPOINT;
37 	attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
38 	attr.sample_period = 1;
39 	attr.wakeup_events = 1;
40 
41 	query = malloc(sizeof(*query) + sizeof(__u32) * num_progs);
42 	for (i = 0; i < num_progs; i++) {
43 		err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj[i],
44 				    &prog_fd[i]);
45 		if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
46 			goto cleanup1;
47 
48 		bzero(&prog_info, sizeof(prog_info));
49 		prog_info.jited_prog_len = 0;
50 		prog_info.xlated_prog_len = 0;
51 		prog_info.nr_map_ids = 0;
52 		info_len = sizeof(prog_info);
53 		err = bpf_prog_get_info_by_fd(prog_fd[i], &prog_info,
54 					      &info_len);
55 		if (CHECK(err, "bpf_prog_get_info_by_fd", "err %d errno %d\n",
56 			  err, errno))
57 			goto cleanup1;
58 		saved_prog_ids[i] = prog_info.id;
59 
60 		pmu_fd[i] = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
61 				    0 /* cpu 0 */, -1 /* group id */,
62 				    0 /* flags */);
63 		if (CHECK(pmu_fd[i] < 0, "perf_event_open", "err %d errno %d\n",
64 			  pmu_fd[i], errno))
65 			goto cleanup2;
66 		err = ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
67 		if (CHECK(err, "perf_event_ioc_enable", "err %d errno %d\n",
68 			  err, errno))
69 			goto cleanup3;
70 
71 		if (i == 0) {
72 			/* check NULL prog array query */
73 			query->ids_len = num_progs;
74 			err = ioctl(pmu_fd[i], PERF_EVENT_IOC_QUERY_BPF, query);
75 			if (CHECK(err || query->prog_cnt != 0,
76 				  "perf_event_ioc_query_bpf",
77 				  "err %d errno %d query->prog_cnt %u\n",
78 				  err, errno, query->prog_cnt))
79 				goto cleanup3;
80 		}
81 
82 		err = ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[i]);
83 		if (CHECK(err, "perf_event_ioc_set_bpf", "err %d errno %d\n",
84 			  err, errno))
85 			goto cleanup3;
86 
87 		if (i == 1) {
88 			/* try to get # of programs only */
89 			query->ids_len = 0;
90 			err = ioctl(pmu_fd[i], PERF_EVENT_IOC_QUERY_BPF, query);
91 			if (CHECK(err || query->prog_cnt != 2,
92 				  "perf_event_ioc_query_bpf",
93 				  "err %d errno %d query->prog_cnt %u\n",
94 				  err, errno, query->prog_cnt))
95 				goto cleanup3;
96 
97 			/* try a few negative tests */
98 			/* invalid query pointer */
99 			err = ioctl(pmu_fd[i], PERF_EVENT_IOC_QUERY_BPF,
100 				    (struct perf_event_query_bpf *)0x1);
101 			if (CHECK(!err || errno != EFAULT,
102 				  "perf_event_ioc_query_bpf",
103 				  "err %d errno %d\n", err, errno))
104 				goto cleanup3;
105 
106 			/* no enough space */
107 			query->ids_len = 1;
108 			err = ioctl(pmu_fd[i], PERF_EVENT_IOC_QUERY_BPF, query);
109 			if (CHECK(!err || errno != ENOSPC || query->prog_cnt != 2,
110 				  "perf_event_ioc_query_bpf",
111 				  "err %d errno %d query->prog_cnt %u\n",
112 				  err, errno, query->prog_cnt))
113 				goto cleanup3;
114 		}
115 
116 		query->ids_len = num_progs;
117 		err = ioctl(pmu_fd[i], PERF_EVENT_IOC_QUERY_BPF, query);
118 		if (CHECK(err || query->prog_cnt != (i + 1),
119 			  "perf_event_ioc_query_bpf",
120 			  "err %d errno %d query->prog_cnt %u\n",
121 			  err, errno, query->prog_cnt))
122 			goto cleanup3;
123 		for (j = 0; j < i + 1; j++)
124 			if (CHECK(saved_prog_ids[j] != query->ids[j],
125 				  "perf_event_ioc_query_bpf",
126 				  "#%d saved_prog_id %x query prog_id %x\n",
127 				  j, saved_prog_ids[j], query->ids[j]))
128 				goto cleanup3;
129 	}
130 
131 	i = num_progs - 1;
132 	for (; i >= 0; i--) {
133  cleanup3:
134 		ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
135  cleanup2:
136 		close(pmu_fd[i]);
137  cleanup1:
138 		bpf_object__close(obj[i]);
139 	}
140 	free(query);
141 }
142