Merge tag 'sched-core-2021-08-30' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / tools / testing / selftests / kvm / kvm_binary_stats_test.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kvm_binary_stats_test
4  *
5  * Copyright (C) 2021, Google LLC.
6  *
7  * Test the fd-based interface for KVM statistics.
8  */
9
10 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include "test_util.h"
18
19 #include "kvm_util.h"
20 #include "asm/kvm.h"
21 #include "linux/kvm.h"
22
23 static void stats_test(int stats_fd)
24 {
25         ssize_t ret;
26         int i;
27         size_t size_desc;
28         size_t size_data = 0;
29         struct kvm_stats_header *header;
30         char *id;
31         struct kvm_stats_desc *stats_desc;
32         u64 *stats_data;
33         struct kvm_stats_desc *pdesc;
34
35         /* Read kvm stats header */
36         header = malloc(sizeof(*header));
37         TEST_ASSERT(header, "Allocate memory for stats header");
38
39         ret = read(stats_fd, header, sizeof(*header));
40         TEST_ASSERT(ret == sizeof(*header), "Read stats header");
41         size_desc = sizeof(*stats_desc) + header->name_size;
42
43         /* Read kvm stats id string */
44         id = malloc(header->name_size);
45         TEST_ASSERT(id, "Allocate memory for id string");
46         ret = read(stats_fd, id, header->name_size);
47         TEST_ASSERT(ret == header->name_size, "Read id string");
48
49         /* Check id string, that should start with "kvm" */
50         TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header->name_size,
51                                 "Invalid KVM stats type, id: %s", id);
52
53         /* Sanity check for other fields in header */
54         if (header->num_desc == 0) {
55                 printf("No KVM stats defined!");
56                 return;
57         }
58         /* Check overlap */
59         TEST_ASSERT(header->desc_offset > 0 && header->data_offset > 0
60                         && header->desc_offset >= sizeof(*header)
61                         && header->data_offset >= sizeof(*header),
62                         "Invalid offset fields in header");
63         TEST_ASSERT(header->desc_offset > header->data_offset ||
64                         (header->desc_offset + size_desc * header->num_desc <=
65                                                         header->data_offset),
66                         "Descriptor block is overlapped with data block");
67
68         /* Allocate memory for stats descriptors */
69         stats_desc = calloc(header->num_desc, size_desc);
70         TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
71         /* Read kvm stats descriptors */
72         ret = pread(stats_fd, stats_desc,
73                         size_desc * header->num_desc, header->desc_offset);
74         TEST_ASSERT(ret == size_desc * header->num_desc,
75                         "Read KVM stats descriptors");
76
77         /* Sanity check for fields in descriptors */
78         for (i = 0; i < header->num_desc; ++i) {
79                 pdesc = (void *)stats_desc + i * size_desc;
80                 /* Check type,unit,base boundaries */
81                 TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
82                                 <= KVM_STATS_TYPE_MAX, "Unknown KVM stats type");
83                 TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK)
84                                 <= KVM_STATS_UNIT_MAX, "Unknown KVM stats unit");
85                 TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK)
86                                 <= KVM_STATS_BASE_MAX, "Unknown KVM stats base");
87                 /* Check exponent for stats unit
88                  * Exponent for counter should be greater than or equal to 0
89                  * Exponent for unit bytes should be greater than or equal to 0
90                  * Exponent for unit seconds should be less than or equal to 0
91                  * Exponent for unit clock cycles should be greater than or
92                  * equal to 0
93                  */
94                 switch (pdesc->flags & KVM_STATS_UNIT_MASK) {
95                 case KVM_STATS_UNIT_NONE:
96                 case KVM_STATS_UNIT_BYTES:
97                 case KVM_STATS_UNIT_CYCLES:
98                         TEST_ASSERT(pdesc->exponent >= 0,
99                                         "Unsupported KVM stats unit");
100                         break;
101                 case KVM_STATS_UNIT_SECONDS:
102                         TEST_ASSERT(pdesc->exponent <= 0,
103                                         "Unsupported KVM stats unit");
104                         break;
105                 }
106                 /* Check name string */
107                 TEST_ASSERT(strlen(pdesc->name) < header->name_size,
108                                 "KVM stats name(%s) too long", pdesc->name);
109                 /* Check size field, which should not be zero */
110                 TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
111                                 pdesc->name);
112                 size_data += pdesc->size * sizeof(*stats_data);
113         }
114         /* Check overlap */
115         TEST_ASSERT(header->data_offset >= header->desc_offset
116                 || header->data_offset + size_data <= header->desc_offset,
117                 "Data block is overlapped with Descriptor block");
118         /* Check validity of all stats data size */
119         TEST_ASSERT(size_data >= header->num_desc * sizeof(*stats_data),
120                         "Data size is not correct");
121         /* Check stats offset */
122         for (i = 0; i < header->num_desc; ++i) {
123                 pdesc = (void *)stats_desc + i * size_desc;
124                 TEST_ASSERT(pdesc->offset < size_data,
125                         "Invalid offset (%u) for stats: %s",
126                         pdesc->offset, pdesc->name);
127         }
128
129         /* Allocate memory for stats data */
130         stats_data = malloc(size_data);
131         TEST_ASSERT(stats_data, "Allocate memory for stats data");
132         /* Read kvm stats data as a bulk */
133         ret = pread(stats_fd, stats_data, size_data, header->data_offset);
134         TEST_ASSERT(ret == size_data, "Read KVM stats data");
135         /* Read kvm stats data one by one */
136         size_data = 0;
137         for (i = 0; i < header->num_desc; ++i) {
138                 pdesc = (void *)stats_desc + i * size_desc;
139                 ret = pread(stats_fd, stats_data,
140                                 pdesc->size * sizeof(*stats_data),
141                                 header->data_offset + size_data);
142                 TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
143                                 "Read data of KVM stats: %s", pdesc->name);
144                 size_data += pdesc->size * sizeof(*stats_data);
145         }
146
147         free(stats_data);
148         free(stats_desc);
149         free(id);
150         free(header);
151 }
152
153
154 static void vm_stats_test(struct kvm_vm *vm)
155 {
156         int stats_fd;
157
158         /* Get fd for VM stats */
159         stats_fd = vm_get_stats_fd(vm);
160         TEST_ASSERT(stats_fd >= 0, "Get VM stats fd");
161
162         stats_test(stats_fd);
163         close(stats_fd);
164         TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
165 }
166
167 static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
168 {
169         int stats_fd;
170
171         /* Get fd for VCPU stats */
172         stats_fd = vcpu_get_stats_fd(vm, vcpu_id);
173         TEST_ASSERT(stats_fd >= 0, "Get VCPU stats fd");
174
175         stats_test(stats_fd);
176         close(stats_fd);
177         TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
178 }
179
180 #define DEFAULT_NUM_VM          4
181 #define DEFAULT_NUM_VCPU        4
182
183 /*
184  * Usage: kvm_bin_form_stats [#vm] [#vcpu]
185  * The first parameter #vm set the number of VMs being created.
186  * The second parameter #vcpu set the number of VCPUs being created.
187  * By default, DEFAULT_NUM_VM VM and DEFAULT_NUM_VCPU VCPU for the VM would be
188  * created for testing.
189  */
190
191 int main(int argc, char *argv[])
192 {
193         int i, j;
194         struct kvm_vm **vms;
195         int max_vm = DEFAULT_NUM_VM;
196         int max_vcpu = DEFAULT_NUM_VCPU;
197
198         /* Get the number of VMs and VCPUs that would be created for testing. */
199         if (argc > 1) {
200                 max_vm = strtol(argv[1], NULL, 0);
201                 if (max_vm <= 0)
202                         max_vm = DEFAULT_NUM_VM;
203         }
204         if (argc > 2) {
205                 max_vcpu = strtol(argv[2], NULL, 0);
206                 if (max_vcpu <= 0)
207                         max_vcpu = DEFAULT_NUM_VCPU;
208         }
209
210         /* Check the extension for binary stats */
211         if (kvm_check_cap(KVM_CAP_BINARY_STATS_FD) <= 0) {
212                 print_skip("Binary form statistics interface is not supported");
213                 exit(KSFT_SKIP);
214         }
215
216         /* Create VMs and VCPUs */
217         vms = malloc(sizeof(vms[0]) * max_vm);
218         TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
219         for (i = 0; i < max_vm; ++i) {
220                 vms[i] = vm_create(VM_MODE_DEFAULT,
221                                 DEFAULT_GUEST_PHY_PAGES, O_RDWR);
222                 for (j = 0; j < max_vcpu; ++j)
223                         vm_vcpu_add(vms[i], j);
224         }
225
226         /* Check stats read for every VM and VCPU */
227         for (i = 0; i < max_vm; ++i) {
228                 vm_stats_test(vms[i]);
229                 for (j = 0; j < max_vcpu; ++j)
230                         vcpu_stats_test(vms[i], j);
231         }
232
233         for (i = 0; i < max_vm; ++i)
234                 kvm_vm_free(vms[i]);
235         free(vms);
236         return 0;
237 }