Re: [PATCH v2 1/2] tools/xg: Streamline cpu policy serialise/deserialise calls

2024-05-22 Thread Alejandro Vallejo
On 20/05/2024 14:47, Roger Pau Monné wrote:
>> @@ -917,17 +922,14 @@ int xc_cpu_policy_set_domain(xc_interface *xch, 
>> uint32_t domid,
>>   xc_cpu_policy_t *policy)
>>  {
>>  uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
>> -unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
>> -unsigned int nr_msrs = ARRAY_SIZE(policy->msrs);
>>  int rc;
>>  
>> -rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
>> - policy->msrs, &nr_msrs);
>> +rc = xc_cpu_policy_serialise(xch, policy);
>>  if ( rc )
>>  return rc;
>>  
>> -rc = xc_set_domain_cpu_policy(xch, domid, nr_leaves, policy->leaves,
>> -  nr_msrs, policy->msrs,
>> +rc = xc_set_domain_cpu_policy(xch, domid, policy->nr_leaves, 
>> policy->leaves,
>> +  policy->nr_msrs, policy->msrs,
> 
> I would be tempted to just pass the policy to
> xc_set_domain_cpu_policy() and get rid of the separate cpuid and msrs
> serialized arrays, but that hides (or makes it less obvious) that the
> policy needs to be serialized before providing to
> xc_set_domain_cpu_policy().  Just a rant, no need to change it here.

I'm still pondering what to do about that. I'd like to refactor all that
faff away as well, but I'm not sure how to do it cleanly yet. The
biggest danger I see is modifying one side of the policy and then wiping
those changes by mistake reserializing or deserializing at the wrong time.

Not for this series, I reckon.


>> +int xc_cpu_policy_get_msrs(xc_interface *xch,
>> +   const xc_cpu_policy_t *policy,
>> +   const xen_msr_entry_t **msrs,
>> +   uint32_t *nr)
>> +{
>> +if ( !policy )
>> +{
>> +ERROR("Failed to fetch MSRs from policy object");
>> +errno = -EINVAL;
>> +return -1;
>> +}
>> +
>> +*msrs = policy->msrs;
>> +*nr = policy->nr_msrs;
>> +
>> +return 0;
>> +}
> 
> My preference would probably be to return NULL or
> xen_{leaf,msr}_entry_t * from those, as we can then avoid an extra
> leaves/msrs parameter.  Again I'm fine with leaving it like this.
> 

It didn't feel right to have an output parameter as the return value
doubling as status code when another output is in the parameter list. I
can perfectly imagine someone grabbing "nr" and ignoring "msrs" because
"msrs" doesn't happen to be needed for them.

I think there's extra safety in making it harder to ignore the error.

>> -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,
>> +.length = policy->nr_leaves * sizeof(*policy->leaves),
>> +};
>> +if ( record.length )
>>  {
>> -rc = write_record(ctx, &cpuid);
>> +rc = write_record(ctx, &record);
>>  if ( rc )
>>  goto out;
>>  }
> 
> 
> You could maybe write this as:
> 
> if ( policy->nr_leaves )
> {
> const struct xc_sr_record r = {
> .type = REC_TYPE_X86_CPUID_POLICY,
> .data = policy->leaves,
> .length = policy->nr_leaves * sizeof(*policy->leaves),
> };
> 
> rc = write_record(ctx, &record);
> }
> 
> (same for the msr record)
> 

Ack. Looks nicer that way.

>>  
>> -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,
>> +.length = policy->nr_msrs * sizeof(*policy->msrs),
>> +};
>> +if ( record.length )
>>  {
>> -rc = write_record(ctx, &msrs);
>> +rc = write_record(ctx, &record);
>>  if ( rc )
>>  goto out;
>>  }
>> @@ -100,8 +84,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;
>> diff --git a/tools/misc/xen-cpuid.c b/tools/misc/xen-cpuid.c
>> index 8893547bebce..1c9ba6d32060 100644
>> --- a/tools/misc/xen-cpuid.c
>> +++ b/tools/misc/xen-cpuid.c
>> @@ -409,17 +409,21 @@ static void dump_info(xc_interface *xch, bool detail)
>>  free(fs);
>>  }
>>  
>> -static void print_policy(const char *name,
>> - xen_cpuid_leaf_t *leaves, uint32_t nr_leaves,
>> - xen_msr_entry_t *msrs, uint32_t nr_msrs)
>> +static void print_policy(xc_interface *xch, const char *name, const 
>> xc_cpu_policy_t *policy)
> 
> Line length.

Ack

> 
>>  {
>> -unsigned int l;
>> +const xen_cpuid_leaf_t *leaves;
>> +const xen_msr_entry_t *msrs;
>> +uint32_t nr_leaves, nr_msrs;
>> +
>> +if ( xc_cpu_policy_get_leaves(xch, policy, &leaves, &nr_leaves) ||
>> + xc_cpu_policy_get_msrs(xch, policy,

Re: [PATCH v2 1/2] tools/xg: Streamline cpu policy serialise/deserialise calls

2024-05-20 Thread Roger Pau Monné
On Fri, May 17, 2024 at 05:08:34PM +0100, Alejandro Vallejo wrote:
> The idea is to use xc_cpu_policy_t as a single object containing both the
> serialised and deserialised forms of the policy. Note that we need lengths
> for the arrays, as the serialised policies may be shorter than the array
> capacities.
> 
> * Add the serialised lengths to the struct so we can distinguish
>   between length and capacity of the serialisation buffers.
> * Remove explicit buffer+lengths in serialise/deserialise calls
>   and use the internal buffer inside xc_cpu_policy_t instead.
> * Refactor everything to use the new serialisation functions.
> * Remove redundant serialization calls and avoid allocating dynamic
>   memory aside from the policy objects in xen-cpuid. Also minor cleanup
>   in the policy print call sites.
> 
> No functional change intended.
> 
> Signed-off-by: Alejandro Vallejo 
> ---
> v2:
>   * Removed v1/patch1.
>   * Added the accessors suggested in feedback.
> ---
>  tools/include/xenguest.h|  8 ++-
>  tools/libs/guest/xg_cpuid_x86.c | 98 -
>  tools/libs/guest/xg_private.h   |  2 +
>  tools/libs/guest/xg_sr_common_x86.c | 54 ++--
>  tools/misc/xen-cpuid.c  | 43 -
>  5 files changed, 104 insertions(+), 101 deletions(-)
> 
> diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
> index e01f494b772a..563811cd8dde 100644
> --- a/tools/include/xenguest.h
> +++ b/tools/include/xenguest.h
> @@ -799,14 +799,16 @@ int xc_cpu_policy_set_domain(xc_interface *xch, 
> uint32_t domid,
>   xc_cpu_policy_t *policy);
>  
>  /* Manipulate a policy via architectural representations. */
> -int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *policy,
> -xen_cpuid_leaf_t *leaves, uint32_t *nr_leaves,
> -xen_msr_entry_t *msrs, uint32_t *nr_msrs);
> +int xc_cpu_policy_serialise(xc_interface *xch, xc_cpu_policy_t *policy);
>  int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
> const xen_cpuid_leaf_t *leaves,
> uint32_t nr);
>  int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,
>const xen_msr_entry_t *msrs, uint32_t nr);
> +int xc_cpu_policy_get_leaves(xc_interface *xch, const xc_cpu_policy_t 
> *policy,
> + const xen_cpuid_leaf_t **leaves, uint32_t *nr);
> +int xc_cpu_policy_get_msrs(xc_interface *xch, const xc_cpu_policy_t *policy,
> +   const xen_msr_entry_t **msrs, uint32_t *nr);
>  
>  /* Compatibility calculations. */
>  bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
> diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
> index 4453178100ad..4f4b86b59470 100644
> --- a/tools/libs/guest/xg_cpuid_x86.c
> +++ b/tools/libs/guest/xg_cpuid_x86.c
> @@ -834,14 +834,13 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy)
>  }
>  }
>  
> -static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy,
> -  unsigned int nr_leaves, unsigned int 
> nr_entries)
> +static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy)
>  {
>  uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
>  int rc;
>  
>  rc = x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves,
> -nr_leaves, &err_leaf, &err_subleaf);
> +policy->nr_leaves, &err_leaf, 
> &err_subleaf);
>  if ( rc )
>  {
>  if ( err_leaf != -1 )
> @@ -851,7 +850,7 @@ static int deserialize_policy(xc_interface *xch, 
> xc_cpu_policy_t *policy,
>  }
>  
>  rc = x86_msr_copy_from_buffer(&policy->policy, policy->msrs,
> -  nr_entries, &err_msr);
> +  policy->nr_msrs, &err_msr);
>  if ( rc )
>  {
>  if ( err_msr != -1 )
> @@ -878,7 +877,10 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned 
> int policy_idx,
>  return rc;
>  }
>  
> -rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
> +policy->nr_leaves = nr_leaves;
> +policy->nr_msrs = nr_msrs;
> +
> +rc = deserialize_policy(xch, policy);
>  if ( rc )
>  {
>  errno = -rc;
> @@ -903,7 +905,10 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t 
> domid,
>  return rc;
>  }
>  
> -rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
> +policy->nr_leaves = nr_leaves;
> +policy->nr_msrs = nr_msrs;
> +
> +rc = deserialize_policy(xch, policy);
>  if ( rc )
>  {
>  errno = -rc;
> @@ -917,17 +922,14 @@ int xc_cpu_policy_set_domain(xc_interface *xch, 
> uint32_t domid,
>   xc_cpu_policy_t *policy)
>  {
>  uint32_t err_le

[PATCH v2 1/2] tools/xg: Streamline cpu policy serialise/deserialise calls

2024-05-17 Thread Alejandro Vallejo
The idea is to use xc_cpu_policy_t as a single object containing both the
serialised and deserialised forms of the policy. Note that we need lengths
for the arrays, as the serialised policies may be shorter than the array
capacities.

* Add the serialised lengths to the struct so we can distinguish
  between length and capacity of the serialisation buffers.
* Remove explicit buffer+lengths in serialise/deserialise calls
  and use the internal buffer inside xc_cpu_policy_t instead.
* Refactor everything to use the new serialisation functions.
* Remove redundant serialization calls and avoid allocating dynamic
  memory aside from the policy objects in xen-cpuid. Also minor cleanup
  in the policy print call sites.

No functional change intended.

Signed-off-by: Alejandro Vallejo 
---
v2:
  * Removed v1/patch1.
  * Added the accessors suggested in feedback.
---
 tools/include/xenguest.h|  8 ++-
 tools/libs/guest/xg_cpuid_x86.c | 98 -
 tools/libs/guest/xg_private.h   |  2 +
 tools/libs/guest/xg_sr_common_x86.c | 54 ++--
 tools/misc/xen-cpuid.c  | 43 -
 5 files changed, 104 insertions(+), 101 deletions(-)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index e01f494b772a..563811cd8dde 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -799,14 +799,16 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t 
domid,
  xc_cpu_policy_t *policy);
 
 /* Manipulate a policy via architectural representations. */
-int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *policy,
-xen_cpuid_leaf_t *leaves, uint32_t *nr_leaves,
-xen_msr_entry_t *msrs, uint32_t *nr_msrs);
+int xc_cpu_policy_serialise(xc_interface *xch, xc_cpu_policy_t *policy);
 int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
const xen_cpuid_leaf_t *leaves,
uint32_t nr);
 int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,
   const xen_msr_entry_t *msrs, uint32_t nr);
+int xc_cpu_policy_get_leaves(xc_interface *xch, const xc_cpu_policy_t *policy,
+ const xen_cpuid_leaf_t **leaves, uint32_t *nr);
+int xc_cpu_policy_get_msrs(xc_interface *xch, const xc_cpu_policy_t *policy,
+   const xen_msr_entry_t **msrs, uint32_t *nr);
 
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 4453178100ad..4f4b86b59470 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -834,14 +834,13 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy)
 }
 }
 
-static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy,
-  unsigned int nr_leaves, unsigned int nr_entries)
+static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy)
 {
 uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
 int rc;
 
 rc = x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves,
-nr_leaves, &err_leaf, &err_subleaf);
+policy->nr_leaves, &err_leaf, 
&err_subleaf);
 if ( rc )
 {
 if ( err_leaf != -1 )
@@ -851,7 +850,7 @@ static int deserialize_policy(xc_interface *xch, 
xc_cpu_policy_t *policy,
 }
 
 rc = x86_msr_copy_from_buffer(&policy->policy, policy->msrs,
-  nr_entries, &err_msr);
+  policy->nr_msrs, &err_msr);
 if ( rc )
 {
 if ( err_msr != -1 )
@@ -878,7 +877,10 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned 
int policy_idx,
 return rc;
 }
 
-rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
+policy->nr_leaves = nr_leaves;
+policy->nr_msrs = nr_msrs;
+
+rc = deserialize_policy(xch, policy);
 if ( rc )
 {
 errno = -rc;
@@ -903,7 +905,10 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t 
domid,
 return rc;
 }
 
-rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
+policy->nr_leaves = nr_leaves;
+policy->nr_msrs = nr_msrs;
+
+rc = deserialize_policy(xch, policy);
 if ( rc )
 {
 errno = -rc;
@@ -917,17 +922,14 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t 
domid,
  xc_cpu_policy_t *policy)
 {
 uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
-unsigned int nr_msrs = ARRAY_SIZE(policy->msrs);
 int rc;
 
-rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
- policy->msrs, &nr_msrs);
+