From: Sean Christopherson <seanjc@google.com>
To: Ben Gardon <bgardon@google.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>,
David Matlack <dmatlack@google.com>,
Jim Mattson <jmattson@google.com>,
David Dunn <daviddunn@google.com>,
Jing Zhang <jingzhangos@google.com>,
Junaid Shahid <junaids@google.com>
Subject: Re: [PATCH v7 03/11] KVM: selftests: Read binary stats desc in lib
Date: Thu, 5 May 2022 17:08:33 +0000 [thread overview]
Message-ID: <YnQEkVQJlhTTo6BQ@google.com> (raw)
In-Reply-To: <20220503183045.978509-4-bgardon@google.com>
On Tue, May 03, 2022, Ben Gardon wrote:
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 1d75d41f92dc..12fa8cc88043 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -2577,3 +2577,41 @@ void read_stats_header(int stats_fd, struct kvm_stats_header *header)
> ret = read(stats_fd, header, sizeof(*header));
> TEST_ASSERT(ret == sizeof(*header), "Read stats header");
> }
> +
> +static ssize_t stats_descs_size(struct kvm_stats_header *header)
Please spell out "descriptors", I find it difficult to visually differentiate
desc vs. descs. I don't mind the short version for variable names, but for helpers
provided by the library I think the added clarity is worth the verbosity.
> +{
> + return header->num_desc *
> + (sizeof(struct kvm_stats_desc) + header->name_size);
> +}
Aha! This is the right patch to deal with the "variable" name_size. Rather than
open code the adjustment for header->name_size, add a helper for _that_. Then
read_stats_descriptors() can do:
desc_size = get_stats_descriptor_size(header);
total_size = header->num_desc * get_stats_descriptor_size(header);
stats_desc = calloc(header->num_desc, desc_size);
TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
ret = pread(stats_fd, stats_desc, total_size, header->desc_offset);
TEST_ASSERT(ret == total_size, "Read KVM stats descriptors");
Those helpers provide an even better place for the comments that my cleanup patch
adds.
> +
> +/*
> + * Read binary stats descriptors
> + *
> + * Input Args:
> + * stats_fd - the file descriptor for the binary stats file from which to read
> + * header - the binary stats metadata header corresponding to the given FD
> + *
> + * Output Args: None
> + *
> + * Return:
> + * A pointer to a newly allocated series of stat descriptors.
> + * Caller is responsible for freeing the returned kvm_stats_desc.
> + *
> + * Read the stats descriptors from the binary stats interface.
> + */
> +struct kvm_stats_desc *read_stats_desc(int stats_fd,
This be "read_stats_descriptors" (or read_stats_descs() if someone objects to the
verbose name) to make it clear this reads multiple desriptors.
E.g. this on top (compile tested only)
---
.../selftests/kvm/include/kvm_util_base.h | 26 +++++++++++++++++--
.../selftests/kvm/kvm_binary_stats_test.c | 7 ++---
tools/testing/selftests/kvm/lib/kvm_util.c | 23 +++++++---------
3 files changed, 37 insertions(+), 19 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index fabe46ddc1b2..e31f7113a529 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -401,8 +401,30 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);
int vm_get_stats_fd(struct kvm_vm *vm);
int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
void read_stats_header(int stats_fd, struct kvm_stats_header *header);
-struct kvm_stats_desc *read_stats_desc(int stats_fd,
- struct kvm_stats_header *header);
+struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
+ struct kvm_stats_header *header);
+
+static inline ssize_t get_stats_descriptor_size(struct kvm_stats_header *header)
+{
+ /*
+ * The base size of the descriptor is defined by KVM's ABI, but the
+ * size of the name field is variable as far as KVM's ABI is concerned.
+ * But, the size of name is constant for a given instance of KVM and
+ * is provided by KVM in the overall stats header.
+ */
+ return sizeof(struct kvm_stats_desc) + header->name_size;
+}
+
+static inline struct kvm_stats_desc *get_stats_descriptor(struct kvm_stats_desc *stats,
+ int index,
+ struct kvm_stats_header *header)
+{
+ /*
+ * Note, size_desc includes the of the name field, which is
+ * variable, i.e. this is NOT equivalent to &stats_desc[i].
+ */
+ return (void *)stats + index * get_stats_descriptor_size(header);
+}
uint32_t guest_get_vcpuid(void);
diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index b49fae45db1e..6252e3d6e964 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -35,7 +35,7 @@ static void stats_test(int stats_fd)
/* Read kvm stats header */
read_stats_header(stats_fd, &header);
- size_desc = sizeof(*stats_desc) + header.name_size;
+ size_desc = get_stats_descriptor_size(&header);
/* Read kvm stats id string */
id = malloc(header.name_size);
@@ -63,11 +63,12 @@ static void stats_test(int stats_fd)
"Descriptor block is overlapped with data block");
/* Read kvm stats descriptors */
- stats_desc = read_stats_desc(stats_fd, &header);
+ stats_desc = read_stats_descriptors(stats_fd, &header);
/* Sanity check for fields in descriptors */
for (i = 0; i < header.num_desc; ++i) {
- pdesc = (void *)stats_desc + i * size_desc;
+ pdesc = get_stats_descriptor(stats_desc, i, &header);
+
/* Check type,unit,base boundaries */
TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
<= KVM_STATS_TYPE_MAX, "Unknown KVM stats type");
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 12fa8cc88043..4374c553de1f 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -2578,12 +2578,6 @@ void read_stats_header(int stats_fd, struct kvm_stats_header *header)
TEST_ASSERT(ret == sizeof(*header), "Read stats header");
}
-static ssize_t stats_descs_size(struct kvm_stats_header *header)
-{
- return header->num_desc *
- (sizeof(struct kvm_stats_desc) + header->name_size);
-}
-
/*
* Read binary stats descriptors
*
@@ -2599,19 +2593,20 @@ static ssize_t stats_descs_size(struct kvm_stats_header *header)
*
* Read the stats descriptors from the binary stats interface.
*/
-struct kvm_stats_desc *read_stats_desc(int stats_fd,
- struct kvm_stats_header *header)
+struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
+ struct kvm_stats_header *header)
{
+ ssize_t ret, desc_size, total_size;
struct kvm_stats_desc *stats_desc;
- ssize_t ret;
- stats_desc = malloc(stats_descs_size(header));
+ desc_size = get_stats_descriptor_size(header);
+ total_size = header->num_desc * get_stats_descriptor_size(header);
+
+ stats_desc = calloc(header->num_desc, desc_size);
TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
- ret = pread(stats_fd, stats_desc, stats_descs_size(header),
- header->desc_offset);
- TEST_ASSERT(ret == stats_descs_size(header),
- "Read KVM stats descriptors");
+ ret = pread(stats_fd, stats_desc, total_size, header->desc_offset);
+ TEST_ASSERT(ret == total_size, "Read KVM stats descriptors");
return stats_desc;
}
base-commit: 6d8fd8c4f5fa1da6fa63da1d127b2804e79b1092
--
next prev parent reply other threads:[~2022-05-05 17:08 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-03 18:30 [PATCH v7 00/11] KVM: x86: Add a cap to disable NX hugepages on a VM Ben Gardon
2022-05-03 18:30 ` [PATCH v7 01/11] KVM: selftests: Remove dynamic memory allocation for stats header Ben Gardon
2022-05-03 18:30 ` [PATCH v7 02/11] KVM: selftests: Read binary stats header in lib Ben Gardon
2022-05-03 18:30 ` [PATCH v7 03/11] KVM: selftests: Read binary stats desc " Ben Gardon
2022-05-05 17:08 ` Sean Christopherson [this message]
2022-05-05 17:13 ` Sean Christopherson
2022-05-03 18:30 ` [PATCH v7 04/11] KVM: selftests: Clean up coding style in binary stats test Ben Gardon
2022-05-03 18:30 ` [PATCH v7 05/11] KVM: selftests: Read binary stat data in lib Ben Gardon
2022-05-05 18:06 ` Sean Christopherson
2022-05-05 18:38 ` Sean Christopherson
2022-05-03 18:30 ` [PATCH v7 06/11] KVM: selftests: Add NX huge pages test Ben Gardon
2022-05-05 18:59 ` Sean Christopherson
2022-05-03 18:30 ` [PATCH v7 07/11] KVM: x86: Fix errant brace in KVM capability handling Ben Gardon
2022-05-03 18:30 ` [PATCH v7 08/11] KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis Ben Gardon
2022-05-03 18:30 ` [PATCH v7 09/11] KVM: selftests: Factor out calculation of pages needed for a VM Ben Gardon
2022-05-03 18:30 ` [PATCH v7 10/11] KVM: selftests: Test disabling NX hugepages on " Ben Gardon
2022-05-03 18:34 ` Ben Gardon
2022-05-05 19:14 ` Sean Christopherson
2022-05-03 18:30 ` [PATCH v7 11/11] KVM: selftests: Cache binary stats metadata for duration of test Ben Gardon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YnQEkVQJlhTTo6BQ@google.com \
--to=seanjc@google.com \
--cc=bgardon@google.com \
--cc=daviddunn@google.com \
--cc=dmatlack@google.com \
--cc=jingzhangos@google.com \
--cc=jmattson@google.com \
--cc=junaids@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.