Commit fcd48a21 authored by Ben Gardon's avatar Ben Gardon Committed by Paolo Bonzini

KVM: selftests: Remove dynamic memory allocation for stats header

There's no need to allocate dynamic memory for the stats header since
its size is known at compile time.
Reviewed-by: default avatarDavid Matlack <dmatlack@google.com>
Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
Signed-off-by: default avatarBen Gardon <bgardon@google.com>
Message-Id: <20220613212523.3436117-2-bgardon@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 2325d4dd
...@@ -26,56 +26,53 @@ static void stats_test(int stats_fd) ...@@ -26,56 +26,53 @@ static void stats_test(int stats_fd)
int i; int i;
size_t size_desc; size_t size_desc;
size_t size_data = 0; size_t size_data = 0;
struct kvm_stats_header *header; struct kvm_stats_header header;
char *id; char *id;
struct kvm_stats_desc *stats_desc; struct kvm_stats_desc *stats_desc;
u64 *stats_data; u64 *stats_data;
struct kvm_stats_desc *pdesc; struct kvm_stats_desc *pdesc;
/* Read kvm stats header */ /* Read kvm stats header */
header = malloc(sizeof(*header)); ret = read(stats_fd, &header, sizeof(header));
TEST_ASSERT(header, "Allocate memory for stats header"); TEST_ASSERT(ret == sizeof(header), "Read stats header");
size_desc = sizeof(*stats_desc) + header.name_size;
ret = read(stats_fd, header, sizeof(*header));
TEST_ASSERT(ret == sizeof(*header), "Read stats header");
size_desc = sizeof(*stats_desc) + header->name_size;
/* Read kvm stats id string */ /* Read kvm stats id string */
id = malloc(header->name_size); id = malloc(header.name_size);
TEST_ASSERT(id, "Allocate memory for id string"); TEST_ASSERT(id, "Allocate memory for id string");
ret = read(stats_fd, id, header->name_size); ret = read(stats_fd, id, header.name_size);
TEST_ASSERT(ret == header->name_size, "Read id string"); TEST_ASSERT(ret == header.name_size, "Read id string");
/* Check id string, that should start with "kvm" */ /* Check id string, that should start with "kvm" */
TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header->name_size, TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
"Invalid KVM stats type, id: %s", id); "Invalid KVM stats type, id: %s", id);
/* Sanity check for other fields in header */ /* Sanity check for other fields in header */
if (header->num_desc == 0) { if (header.num_desc == 0) {
printf("No KVM stats defined!"); printf("No KVM stats defined!");
return; return;
} }
/* Check overlap */ /* Check overlap */
TEST_ASSERT(header->desc_offset > 0 && header->data_offset > 0 TEST_ASSERT(header.desc_offset > 0 && header.data_offset > 0
&& header->desc_offset >= sizeof(*header) && header.desc_offset >= sizeof(header)
&& header->data_offset >= sizeof(*header), && header.data_offset >= sizeof(header),
"Invalid offset fields in header"); "Invalid offset fields in header");
TEST_ASSERT(header->desc_offset > header->data_offset || TEST_ASSERT(header.desc_offset > header.data_offset ||
(header->desc_offset + size_desc * header->num_desc <= (header.desc_offset + size_desc * header.num_desc <=
header->data_offset), header.data_offset),
"Descriptor block is overlapped with data block"); "Descriptor block is overlapped with data block");
/* Allocate memory for stats descriptors */ /* Allocate memory for stats descriptors */
stats_desc = calloc(header->num_desc, size_desc); stats_desc = calloc(header.num_desc, size_desc);
TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
/* Read kvm stats descriptors */ /* Read kvm stats descriptors */
ret = pread(stats_fd, stats_desc, ret = pread(stats_fd, stats_desc,
size_desc * header->num_desc, header->desc_offset); size_desc * header.num_desc, header.desc_offset);
TEST_ASSERT(ret == size_desc * header->num_desc, TEST_ASSERT(ret == size_desc * header.num_desc,
"Read KVM stats descriptors"); "Read KVM stats descriptors");
/* Sanity check for fields in descriptors */ /* Sanity check for fields in descriptors */
for (i = 0; i < header->num_desc; ++i) { for (i = 0; i < header.num_desc; ++i) {
pdesc = (void *)stats_desc + i * size_desc; pdesc = (void *)stats_desc + i * size_desc;
/* Check type,unit,base boundaries */ /* Check type,unit,base boundaries */
TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
...@@ -104,7 +101,7 @@ static void stats_test(int stats_fd) ...@@ -104,7 +101,7 @@ static void stats_test(int stats_fd)
break; break;
} }
/* Check name string */ /* Check name string */
TEST_ASSERT(strlen(pdesc->name) < header->name_size, TEST_ASSERT(strlen(pdesc->name) < header.name_size,
"KVM stats name(%s) too long", pdesc->name); "KVM stats name(%s) too long", pdesc->name);
/* Check size field, which should not be zero */ /* Check size field, which should not be zero */
TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0", TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
...@@ -124,14 +121,14 @@ static void stats_test(int stats_fd) ...@@ -124,14 +121,14 @@ static void stats_test(int stats_fd)
size_data += pdesc->size * sizeof(*stats_data); size_data += pdesc->size * sizeof(*stats_data);
} }
/* Check overlap */ /* Check overlap */
TEST_ASSERT(header->data_offset >= header->desc_offset TEST_ASSERT(header.data_offset >= header.desc_offset
|| header->data_offset + size_data <= header->desc_offset, || header.data_offset + size_data <= header.desc_offset,
"Data block is overlapped with Descriptor block"); "Data block is overlapped with Descriptor block");
/* Check validity of all stats data size */ /* Check validity of all stats data size */
TEST_ASSERT(size_data >= header->num_desc * sizeof(*stats_data), TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data),
"Data size is not correct"); "Data size is not correct");
/* Check stats offset */ /* Check stats offset */
for (i = 0; i < header->num_desc; ++i) { for (i = 0; i < header.num_desc; ++i) {
pdesc = (void *)stats_desc + i * size_desc; pdesc = (void *)stats_desc + i * size_desc;
TEST_ASSERT(pdesc->offset < size_data, TEST_ASSERT(pdesc->offset < size_data,
"Invalid offset (%u) for stats: %s", "Invalid offset (%u) for stats: %s",
...@@ -142,15 +139,15 @@ static void stats_test(int stats_fd) ...@@ -142,15 +139,15 @@ static void stats_test(int stats_fd)
stats_data = malloc(size_data); stats_data = malloc(size_data);
TEST_ASSERT(stats_data, "Allocate memory for stats data"); TEST_ASSERT(stats_data, "Allocate memory for stats data");
/* Read kvm stats data as a bulk */ /* Read kvm stats data as a bulk */
ret = pread(stats_fd, stats_data, size_data, header->data_offset); ret = pread(stats_fd, stats_data, size_data, header.data_offset);
TEST_ASSERT(ret == size_data, "Read KVM stats data"); TEST_ASSERT(ret == size_data, "Read KVM stats data");
/* Read kvm stats data one by one */ /* Read kvm stats data one by one */
size_data = 0; size_data = 0;
for (i = 0; i < header->num_desc; ++i) { for (i = 0; i < header.num_desc; ++i) {
pdesc = (void *)stats_desc + i * size_desc; pdesc = (void *)stats_desc + i * size_desc;
ret = pread(stats_fd, stats_data, ret = pread(stats_fd, stats_data,
pdesc->size * sizeof(*stats_data), pdesc->size * sizeof(*stats_data),
header->data_offset + size_data); header.data_offset + size_data);
TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data), TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
"Read data of KVM stats: %s", pdesc->name); "Read data of KVM stats: %s", pdesc->name);
size_data += pdesc->size * sizeof(*stats_data); size_data += pdesc->size * sizeof(*stats_data);
...@@ -159,7 +156,6 @@ static void stats_test(int stats_fd) ...@@ -159,7 +156,6 @@ static void stats_test(int stats_fd)
free(stats_data); free(stats_data);
free(stats_desc); free(stats_desc);
free(id); free(id);
free(header);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment