diff mbox series

[2/7] tools/xg: Simplify write_x86_cpu_policy_records()

Message ID 20231107154921.54979-3-alejandro.vallejo@cloud.com (mailing list archive)
State New, archived
Headers show
Series Cleanup and code duplication removal in xenguest | expand

Commit Message

Alejandro Vallejo Nov. 7, 2023, 3:49 p.m. UTC
With the policy automatically getting appropriate serialised buffer sizes,
we can remove boilerplate from this function. Furthermore, the extra
dynamic allocations aren't needed anymore as the serialised buffers inside
the policy can be used instead.

Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
 tools/libs/guest/xg_sr_common_x86.c | 55 +++++++++--------------------
 1 file changed, 17 insertions(+), 38 deletions(-)
diff mbox series

Patch

diff --git a/tools/libs/guest/xg_sr_common_x86.c b/tools/libs/guest/xg_sr_common_x86.c
index ad63c675ed..c8fd64775f 100644
--- a/tools/libs/guest/xg_sr_common_x86.c
+++ b/tools/libs/guest/xg_sr_common_x86.c
@@ -44,55 +44,36 @@  int handle_x86_tsc_info(struct xc_sr_context *ctx, struct xc_sr_record *rec)
 
 int write_x86_cpu_policy_records(struct xc_sr_context *ctx)
 {
+    int rc = -1;
     xc_interface *xch = ctx->xch;
-    struct xc_sr_record cpuid = { .type = REC_TYPE_X86_CPUID_POLICY, };
-    struct xc_sr_record msrs  = { .type = REC_TYPE_X86_MSR_POLICY, };
-    uint32_t nr_leaves = 0, nr_msrs = 0;
-    xc_cpu_policy_t *policy = NULL;
-    int rc;
-
-    if ( xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs) < 0 )
-    {
-        PERROR("Unable to get CPU Policy size");
-        return -1;
-    }
+    struct xc_sr_record record;
+    xc_cpu_policy_t *policy = xc_cpu_policy_init(xch);
 
-    cpuid.data = malloc(nr_leaves * sizeof(xen_cpuid_leaf_t));
-    msrs.data  = malloc(nr_msrs   * sizeof(xen_msr_entry_t));
-    policy = xc_cpu_policy_init(xch);
-    if ( !cpuid.data || !msrs.data || !policy )
-    {
-        ERROR("Cannot allocate memory for CPU Policy");
-        rc = -1;
-        goto out;
-    }
-
-    if ( xc_cpu_policy_get_domain(xch, ctx->domid, policy) )
+    if ( !policy ||
+         (rc = xc_cpu_policy_get_domain(xch, ctx->domid, policy)) )
     {
         PERROR("Unable to get d%d CPU Policy", ctx->domid);
-        rc = -1;
-        goto out;
-    }
-    if ( xc_cpu_policy_serialise(xch, policy, cpuid.data, &nr_leaves,
-                                 msrs.data, &nr_msrs) )
-    {
-        PERROR("Unable to serialize d%d CPU Policy", ctx->domid);
-        rc = -1;
         goto out;
     }
 
-    cpuid.length = nr_leaves * sizeof(xen_cpuid_leaf_t);
-    if ( cpuid.length )
+    record = (struct xc_sr_record){
+        .type = REC_TYPE_X86_CPUID_POLICY, .data = policy->leaves.buf,
+        .length = policy->leaves.len * sizeof(*policy->leaves.buf),
+    };
+    if ( record.length )
     {
-        rc = write_record(ctx, &cpuid);
+        rc = write_record(ctx, &record);
         if ( rc )
             goto out;
     }
 
-    msrs.length = nr_msrs * sizeof(xen_msr_entry_t);
-    if ( msrs.length )
+    record = (struct xc_sr_record){
+        .type = REC_TYPE_X86_MSR_POLICY, .data = policy->msrs.buf,
+        .length = policy->msrs.len * sizeof(*policy->msrs.buf),
+    };
+    if ( record.length )
     {
-        rc = write_record(ctx, &msrs);
+        rc = write_record(ctx, &record);
         if ( rc )
             goto out;
     }
@@ -100,8 +81,6 @@  int write_x86_cpu_policy_records(struct xc_sr_context *ctx)
     rc = 0;
 
  out:
-    free(cpuid.data);
-    free(msrs.data);
     xc_cpu_policy_destroy(policy);
 
     return rc;