Cleanup and code duplication removal in xenguest
cover Cleanup and code duplication removal in xenguest
NOTE: This series is still under test
This series tries to bind the current xc_cpu_policy_t and its serialization buffers. Having them separate makes it hard to extend of modify the serialized policy structure and needlessly leaks details in places that don't need them, causing a lot of boilerplate and code duplication. The resulting code is >100LoC leaner.
The series is not as aggressive as it could be, but it's enough to unlock future work regarding cpu policy extensions and ought to make toolstack interactions a lot faster.
Patch 1: Remove the fixed buffers in xc_cpu_policy_t and create pointers
populated during xc_cpu_policy_init() instead.
Patch 2: Removes boilerplate by making use of the newly created buffers Patch 3: Adds a couple of helpers to keep the 2 forms inside the
xc_cpu_policy_t object consistently in sync.
Patch 4: Splits the common set_cpu_policy wrapper in 2. One to send the
deserialized form of the policy object (after serializing it internally first) and another to send the serialized form directly.
Patch 5: Uses the previous patches to avoid a lot of boilerplate in the
main policy manipulation routine.
Patch 6: Remove code duplication in the xend-style override setters Patch 7: Final cleanup so the get_cpu_policy wrappers can operate on policy
objects directly
Alejandro Vallejo (7):
tools/xenguest: Dynamically allocate xc_cpu_policy_t contents tools/xg: Simplify write_x86_cpu_policy_records() tools/xg: Add self-(de)serialisation functions for cpu_policy tools/xg: Split xc_cpu_policy_set_domain() tools/xg: Streamline xc_cpuid_apply_policy() tools/xg: Simplify xc_cpuid_xend_policy() and xc_msr_policy() tools/xg: Simplify hypercall stubs
tools/include/xenguest.h | 11 +- tools/libs/guest/xg_cpuid_x86.c | 626 +++++++++++---------------- tools/libs/guest/xg_private.h | 17 +- tools/libs/guest/xg_sr_common_x86.c | 55 +-- tools/misc/xen-cpuid.c | 2 +- tools/tests/tsx/test-tsx.c | 61 +-- xen/include/xen/lib/x86/cpu-policy.h | 2 +- 7 files changed, 318 insertions(+), 456 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/7 tools/xenguest: Dynamically allocate xc_cpu_policy_t contents
This is part of a larger effort to reduce technical debt in this area and prevent policy internals from leaking in plumbing code that needs not be aware of them.
This patch turns the internal static buffers into dynamic ones and performs the allocations based on policy sizes probed using a hypercall. This is meant to help dealing with mismatched versions of toolstack/Xen, as no assumptions are made with regards of the sizes of the buffers. With this, we are now able to use these buffers for serialization instead of out-of-band data structures.
The scheme is simple and involves keeping track of the buffer occupancy through the "len" field and its capacity through the "allocated" field; think "vector" in higher-level languages. Both trackers refer to entries rather than octets because most of the code deals in those terms.
While at this, make a minor change to MSR_MAX_SERIALISED_ENTRIES so it's unsigned in order to please the max() macro.
--- tools/include/xenguest.h | 2 +- tools/libs/guest/xg_cpuid_x86.c | 86 +++++++++++++++++++++------- tools/libs/guest/xg_private.h | 17 +++++- tools/libs/guest/xg_sr_common_x86.c | 2 +- tools/misc/xen-cpuid.c | 2 +- tools/tests/tsx/test-tsx.c | 61 +++++++++++--------- xen/include/xen/lib/x86/cpu-policy.h | 2 +- 7 files changed, 119 insertions(+), 53 deletions(-)
tools/include/xenguest.h +1 -1
@@ -787,7 +787,7 @@ xen_pfn_t *xc_map_m2p(xc_interface *xch,typedef struct xc_cpu_policy xc_cpu_policy_t ;/* Create and free a xc_cpu_policy object. */ -xc_cpu_policy_t *xc_cpu_policy_init (void ); +xc_cpu_policy_t *xc_cpu_policy_init (xc_interface *xch);void xc_cpu_policy_destroy (xc_cpu_policy_t *policy);/* Retrieve a system policy, or get/set a domains policy. */
tools/libs/guest/xg_cpuid_x86.c +66 -20
148 lines not on this page · raw message
tools/libs/guest/xg_private.h +15 -2
@@ -174,9 +174,22 @@ int pin_table(xc_interface *xch, unsigned int type, unsigned long mfn,#include <xen/lib/x86/cpu-policy.h> struct xc_cpu_policy { +/** Deserialised policy */ struct cpu_policy policy; -xen_cpuid_leaf_t leaves[CPUID_MAX_SERIALISED_LEAVES ]; -xen_msr_entry_t msrs[MSR_MAX_SERIALISED_ENTRIES ]; + +/** Serialised buffer of CPUID leaves */ +struct { +xen_cpuid_leaf_t *buf;/**< Base address */ +size_t allocated;/**< Memory allocated to the buffer (in entries) */ +size_t len;/**< Number of entries in use (in entries) */ + } leaves; + +/** Serialised buffer of MSRs */ +struct { +xen_msr_entry_t *buf;/**< Base address */ +size_t allocated;/**< Memory allocated to the buffer (in entries) */ +size_t len;/**< Number of entries in use (in entries) */ + } msrs; };#endif /* x86 */
tools/libs/guest/xg_sr_common_x86.c +1 -1
@@ -59,7 +59,7 @@ int write_x86_cpu_policy_records(struct xc_sr_context *ctx) 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 (); + policy =xc_cpu_policy_init (xch);if ( !cpuid.data || !msrs.data || !policy ) {ERROR ("Cannot allocate memory for CPU Policy" );
tools/misc/xen-cpuid.c +1 -1
@@ -532,7 +532,7 @@ int main(int argc, char **argv)uint32_t i, max_leaves, max_msrs;xc_interface *xch =xc_interface_open (0 ,0 ,0 ); -xc_cpu_policy_t *policy =xc_cpu_policy_init (); +xc_cpu_policy_t *policy =xc_cpu_policy_init (xch);if ( !xch )err (1 ,"xc_interface_open" );
tools/tests/tsx/test-tsx.c +34 -27
147 lines not on this page · raw message
xen/include/xen/lib/x86/cpu-policy.h +1 -1
@@ -110,7 +110,7 @@ const char *x86_cpuid_vendor_to_str(unsigned int vendor); 2 /* hv_limit and hv2_limit */ )/* Maximum number of MSRs written when serialising a cpu_policy. */ -#define MSR_MAX_SERIALISED_ENTRIES 2 +#define MSR_MAX_SERIALISED_ENTRIES 2Ustruct cpu_policy {
Signature · 1 line
2.34.1
PATCH 2/7 tools/xg: Simplify write_x86_cpu_policy_records()
In reply to Alejandro VallejoWith 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.
--- tools/libs/guest/xg_sr_common_x86.c | 55 +++++++++-------------------- 1 file changed, 17 insertions(+), 38 deletions(-)
tools/libs/guest/xg_sr_common_x86.c +17 -38
@@ -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;
Signature · 1 line
2.34.1
PATCH 3/7 tools/xg: Add self-(de)serialisation functions for cpu_policy
In reply to Alejandro VallejoThese allow a policy to internally (de)serialize itself, so that we don't have to carry around serialization buffers when perfectly good ones are present inside.
Both moved on top of the xend overrides as they are needed there in future patches
--- tools/libs/guest/xg_cpuid_x86.c | 90 ++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 41 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +49 -41
@@ -254,6 +254,50 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid,return ret; } +static int cpu_policy_deserialise_on_self (xc_interface *xch,xc_cpu_policy_t *p) +{ +uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; +int rc; + + rc =x86_cpuid_copy_from_buffer (&p->policy, p->leaves.buf, p->leaves.len, + &err_leaf, &err_subleaf); +if ( rc ) + { +if ( err_leaf !=-1 ) +ERROR ("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)" , + err_leaf, err_subleaf, -rc,strerror (-rc)); +return rc; + } + + rc =x86_msr_copy_from_buffer (&p->policy, p->msrs.buf, p->msrs.len, &err_msr); +if ( rc ) + { +if ( err_msr !=-1 ) +ERROR ("Failed to deserialise MSR (err MSR %#x) (%d = %s)" , + err_msr, -rc,strerror (-rc)); +return rc; + } + +return 0 ; +} + +static int cpu_policy_serialise_on_self (xc_interface *xch,xc_cpu_policy_t *p) +{ +uint32_t nr_leaves = p->leaves.allocated; +uint32_t nr_msrs = p->msrs.allocated; +int rc =xc_cpu_policy_serialise (xch, p, + p->leaves.buf, &nr_leaves, + p->msrs.buf, &nr_msrs); + +if ( !rc ) + { + p->leaves.len = nr_leaves; + p->msrs.len = nr_msrs; + } + +return rc; +} +static int compare_leaves (const void *l,const void *r) {const xen_cpuid_leaf_t *lhs = l;
@@ -883,35 +927,6 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) errno = err; } -static int deserialize_policy (xc_interface *xch,xc_cpu_policy_t *policy, -unsigned int nr_leaves,unsigned int nr_entries) -{ -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; -int rc; - - rc =x86_cpuid_copy_from_buffer (&policy->policy, policy->leaves.buf, - nr_leaves, &err_leaf, &err_subleaf); -if ( rc ) - { -if ( err_leaf !=-1 ) -ERROR ("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)" , - err_leaf, err_subleaf, -rc,strerror (-rc)); -return rc; - } - - rc =x86_msr_copy_from_buffer (&policy->policy, policy->msrs.buf, - nr_entries, &err_msr); -if ( rc ) - { -if ( err_msr !=-1 ) -ERROR ("Failed to deserialise MSR (err MSR %#x) (%d = %s)" , - err_msr, -rc,strerror (-rc)); -return rc; - } - -return 0 ; -} -int xc_cpu_policy_get_system (xc_interface *xch,unsigned int policy_idx,xc_cpu_policy_t *policy) {
@@ -931,7 +946,7 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, policy->leaves.len = nr_leaves; policy->msrs.len = nr_msrs; - rc =deserialize_policy (xch, policy, nr_leaves, nr_msrs); + rc =cpu_policy_deserialise_on_self (xch, policy);if ( rc ) { errno = -rc;
@@ -960,7 +975,7 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, policy->leaves.len = nr_leaves; policy->msrs.len = nr_msrs; - rc =deserialize_policy (xch, policy, nr_leaves, nr_msrs); + rc =cpu_policy_deserialise_on_self (xch, policy);if ( rc ) { errno = -rc;
@@ -974,22 +989,15 @@ 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 = policy->leaves.allocated; -unsigned int nr_msrs = policy->msrs.allocated;int rc; - rc =xc_cpu_policy_serialise (xch, policy, - policy->leaves.buf, &nr_leaves, - policy->msrs.buf, &nr_msrs); + rc =cpu_policy_serialise_on_self (xch, policy);if ( rc )return rc; - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc =xc_set_domain_cpu_policy (xch, domid, - nr_leaves, policy->leaves.buf, - nr_msrs, policy->msrs.buf, + policy->leaves.len, policy->leaves.buf, + policy->msrs.len, policy->msrs.buf, &err_leaf, &err_subleaf, &err_msr);if ( rc ) {
Signature · 1 line
2.34.1
PATCH 4/7 tools/xg: Split xc_cpu_policy_set_domain()
In reply to Alejandro Vallejoxc_cpu_policy_set_domain_from_serialised() converts the cpu policy into its serialised form first and then sends that to Xen. Meanwhile, xc_cpu_policy_domain_set_from_deserialised() uses whatever is already in the internal serialisation buffer of the policy object.
This split helps in a future patch where modifications are done in the serialized form and we don't want to do a serialization round-trip to send it to Xen.
--- tools/include/xenguest.h | 8 ++++++-- tools/libs/guest/xg_cpuid_x86.c | 24 ++++++++++++++---------- tools/tests/tsx/test-tsx.c | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-)
tools/include/xenguest.h +6 -2
@@ -795,8 +795,12 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, xc_cpu_policy_t *policy);int xc_cpu_policy_get_domain (xc_interface *xch,uint32_t domid,xc_cpu_policy_t *policy); -int xc_cpu_policy_set_domain (xc_interface *xch,uint32_t domid, -xc_cpu_policy_t *policy); +int xc_cpu_policy_set_domain_from_serialised (xc_interface *xch, +uint32_t domid, +xc_cpu_policy_t *policy); +int xc_cpu_policy_set_domain_from_deserialised (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,
tools/libs/guest/xg_cpuid_x86.c +14 -10
@@ -985,20 +985,24 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid,return rc; } -int xc_cpu_policy_set_domain (xc_interface *xch,uint32_t domid, -xc_cpu_policy_t *policy) +int xc_cpu_policy_set_domain_from_deserialised (xc_interface *xch,uint32_t domid, +xc_cpu_policy_t *policy) { -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ;int rc; - - rc =cpu_policy_serialise_on_self (xch, policy); -if ( rc ) +if ( (rc =cpu_policy_serialise_on_self (xch, policy)) )return rc; - rc =xc_set_domain_cpu_policy (xch, domid, - policy->leaves.len, policy->leaves.buf, - policy->msrs.len, policy->msrs.buf, - &err_leaf, &err_subleaf, &err_msr); +return xc_cpu_policy_set_domain_from_serialised (xch, domid, policy); +} + +int xc_cpu_policy_set_domain_from_serialised (xc_interface *xch,uint32_t domid, +xc_cpu_policy_t *policy) +{ +uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; +int rc =xc_set_domain_cpu_policy (xch, domid, + policy->leaves.len, policy->leaves.buf, + policy->msrs.len, policy->msrs.buf, + &err_leaf, &err_subleaf, &err_msr);if ( rc ) { ERROR("Failed to set domain %u policy (%d = %s)", domid, -rc,
tools/tests/tsx/test-tsx.c +1 -1
@@ -405,7 +405,7 @@ static void test_guest(struct xen_domctl_createdomain *c) (bitmaskof(X86_FEATURE_HLE) | bitmaskof(X86_FEATURE_RTM)));/* Set the new policy. */ - rc =xc_cpu_policy_set_domain (xch, domid, guest_policy); + rc =xc_cpu_policy_set_domain_from_deserialised (xch, domid, guest_policy);if ( rc ) { fail(" Failed to set domain policy: %d - %s\n",
Signature · 1 line
2.34.1
PATCH 6/7 tools/xg: Simplify xc_cpuid_xend_policy() and xc_msr_policy()
In reply to Alejandro VallejoRemove all duplication in CPUID and MSR xend-style overrides. They had an incredible amount of overhead for no good reason.
After this patch, CPU policy application takes a number of hypercalls to recover the policy state and then those are passed to the xend-style override code so it can avoid them.
Furthermore, the ultimate reapplication of the policy to the domain in Xen is done only once after both CPUID and MSRs have been fixed up.
BUG!!! apply_policy is sending the policy after deserialise when it poked at it in its serialised form.
--- tools/libs/guest/xg_cpuid_x86.c | 261 +++++--------------------------- 1 file changed, 38 insertions(+), 223 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +38 -223
@@ -312,102 +312,32 @@ static int compare_leaves(const void *l, const void *r)return 0 ; } -static xen_cpuid_leaf_t *find_leaf ( -xen_cpuid_leaf_t *leaves,unsigned int nr_leaves, -const struct xc_xend_cpuid *xend) +static xen_cpuid_leaf_t *find_leaf (xc_cpu_policy_t *p, +const struct xc_xend_cpuid *xend) {const xen_cpuid_leaf_t key = { xend->leaf, xend->subleaf }; -return bsearch (&key, leaves, nr_leaves,sizeof (*leaves), compare_leaves); +return bsearch (&key, p->leaves.buf, p->leaves.len, +sizeof (*p->leaves.buf), compare_leaves); }static int xc_cpuid_xend_policy ( -xc_interface *xch,uint32_t domid,const struct xc_xend_cpuid *xend) +xc_interface *xch,uint32_t domid, +const struct xc_xend_cpuid *xend, +xc_cpu_policy_t *host, +xc_cpu_policy_t *def, +xc_cpu_policy_t *cur) { -int rc; -bool hvm; -xc_domaininfo_t di; -unsigned int nr_leaves, nr_msrs; -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; -/* -* Three full policies. The host, default for the domain type, -* and domain current. -*/ -xen_cpuid_leaf_t *host =NULL , *def =NULL , *cur =NULL ; -unsigned int nr_host, nr_def, nr_cur; - -if ( (rc =xc_domain_getinfo_single (xch, domid, &di)) <0 ) - { -PERROR ("Failed to obtain d%d info" , domid); - rc = -errno; - goto fail; - } - hvm = di.flags & XEN_DOMINF_hvm_guest; - - rc =xc_cpu_policy_get_size (xch, &nr_leaves, &nr_msrs); -if ( rc ) - { -PERROR ("Failed to obtain policy info size" ); - rc = -errno; - goto fail; - } - - rc = -ENOMEM ; -if ( (host =calloc (nr_leaves,sizeof (*host))) ==NULL || - (def =calloc (nr_leaves,sizeof (*def))) ==NULL || - (cur =calloc (nr_leaves,sizeof (*cur))) ==NULL ) - { -ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); - goto fail; - } - -/* Get the domain's current policy. */ - nr_msrs =0 ; - nr_cur = nr_leaves; - rc =get_domain_cpu_policy (xch, domid, &nr_cur, cur, &nr_msrs,NULL ); -if ( rc ) - { -PERROR ("Failed to obtain d%d current policy" , domid); - rc = -errno; - goto fail; - } - -/* Get the domain type's default policy. */ - nr_msrs =0 ; - nr_def = nr_leaves; - rc =get_system_cpu_policy (xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default - : XEN_SYSCTL_cpu_policy_pv_default, - &nr_def, def, &nr_msrs,NULL ); -if ( rc ) - { -PERROR ("Failed to obtain %s def policy" , hvm ?"hvm" :"pv" ); - rc = -errno; - goto fail; - } - -/* Get the host policy. */ - nr_msrs =0 ; - nr_host = nr_leaves; - rc =get_system_cpu_policy (xch, XEN_SYSCTL_cpu_policy_host, - &nr_host, host, &nr_msrs,NULL ); -if ( rc ) - { -PERROR ("Failed to obtain host policy" ); - rc = -errno; - goto fail; - } - - rc = -EINVAL ;for ( ; xend->leaf !=XEN_CPUID_INPUT_UNUSED ; ++xend ) { -xen_cpuid_leaf_t *cur_leaf =find_leaf (cur, nr_cur, xend); -const xen_cpuid_leaf_t *def_leaf =find_leaf (def, nr_def, xend); -const xen_cpuid_leaf_t *host_leaf =find_leaf (host, nr_host, xend); +xen_cpuid_leaf_t *cur_leaf =find_leaf (cur, xend); +const xen_cpuid_leaf_t *def_leaf =find_leaf (def, xend); +const xen_cpuid_leaf_t *host_leaf =find_leaf (host, xend);if ( cur_leaf ==NULL || def_leaf ==NULL || host_leaf ==NULL ) {ERROR ("Missing leaf %#x, subleaf %#x" , xend->leaf, xend->subleaf); - goto fail; +return -EINVAL ; }for (unsigned int i =0 ; i <ARRAY_SIZE (xend->policy); i++ )
@@ -436,7 +366,7 @@ static int xc_cpuid_xend_policy( {ERROR ("Bad character '%c' in policy[%d] string '%s'" , xend->policy[i][j], i, xend->policy[i]); - goto fail; +return -EINVAL ; }clear_bit (31 - j, cur_reg);
@@ -446,25 +376,7 @@ static int xc_cpuid_xend_policy( } } -/* Feed the transformed currrent policy back up to Xen. */ - rc =xc_set_domain_cpu_policy (xch, domid, nr_cur, cur,0 ,NULL , - &err_leaf, &err_subleaf, &err_msr); -if ( rc ) - { -PERROR ("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr %#x)" , - domid, err_leaf, err_subleaf, err_msr); - rc = -errno; - goto fail; - } - -/* Success! */ - - fail: -free (cur); -free (def); -free (host); - -return rc; +return 0 ; }static int compare_msr (const void *l,const void *r)
@@ -478,104 +390,31 @@ static int compare_msr(const void *l, const void *r)return lhs->idx < rhs->idx ?-1 :1 ; } -static xen_msr_entry_t *find_msr ( -xen_msr_entry_t *msrs,unsigned int nr_msrs, -uint32_t index) +static xen_msr_entry_t *find_msr (xc_cpu_policy_t *p,uint32_t index) {const xen_msr_entry_t key = { .idx = index }; -return bsearch (&key, msrs, nr_msrs,sizeof (*msrs), compare_msr); +return bsearch (&key, p->msrs.buf, p->msrs.len, +sizeof (*p->msrs.buf), compare_msr); } -static int xc_msr_policy (xc_interface *xch,domid_t domid, -const struct xc_msr *msr) +const struct xc_msr *msr, +xc_cpu_policy_t *host, +xc_cpu_policy_t *def, +xc_cpu_policy_t *cur) { -int rc; -bool hvm; -xc_domaininfo_t di; -unsigned int nr_leaves, nr_msrs; -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; -/* -* Three full policies. The host, default for the domain type, -* and domain current. -*/ -xen_msr_entry_t *host =NULL , *def =NULL , *cur =NULL ; -unsigned int nr_host, nr_def, nr_cur; - -if ( (rc =xc_domain_getinfo_single (xch, domid, &di)) <0 ) - { -PERROR ("Failed to obtain d%d info" , domid); - rc = -errno; - goto out; - } - hvm = di.flags & XEN_DOMINF_hvm_guest; - - rc =xc_cpu_policy_get_size (xch, &nr_leaves, &nr_msrs); -if ( rc ) - { -PERROR ("Failed to obtain policy info size" ); - rc = -errno; - goto out; - } - -if ( (host =calloc (nr_msrs,sizeof (*host))) ==NULL || - (def =calloc (nr_msrs,sizeof (*def))) ==NULL || - (cur =calloc (nr_msrs,sizeof (*cur))) ==NULL ) - { -ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); - rc = -ENOMEM ; - goto out; - } - -/* Get the domain's current policy. */ - nr_leaves =0 ; - nr_cur = nr_msrs; - rc =get_domain_cpu_policy (xch, domid, &nr_leaves,NULL , &nr_cur, cur); -if ( rc ) - { -PERROR ("Failed to obtain d%d current policy" , domid); - rc = -errno; - goto out; - } - -/* Get the domain type's default policy. */ - nr_leaves =0 ; - nr_def = nr_msrs; - rc =get_system_cpu_policy (xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default - : XEN_SYSCTL_cpu_policy_pv_default, - &nr_leaves,NULL , &nr_def, def); -if ( rc ) - { -PERROR ("Failed to obtain %s def policy" , hvm ?"hvm" :"pv" ); - rc = -errno; - goto out; - } - -/* Get the host policy. */ - nr_leaves =0 ; - nr_host = nr_msrs; - rc =get_system_cpu_policy (xch, XEN_SYSCTL_cpu_policy_host, - &nr_leaves,NULL , &nr_host, host); -if ( rc ) - { -PERROR ("Failed to obtain host policy" ); - rc = -errno; - goto out; - } -for ( ; msr->index !=XC_MSR_INPUT_UNUSED ; ++msr ) { -xen_msr_entry_t *cur_msr =find_msr (cur, nr_cur, msr->index); -const xen_msr_entry_t *def_msr =find_msr (def, nr_def, msr->index); -const xen_msr_entry_t *host_msr =find_msr (host, nr_host, msr->index); +xen_msr_entry_t *cur_msr =find_msr (cur, msr->index); +const xen_msr_entry_t *def_msr =find_msr (def, msr->index); +const xen_msr_entry_t *host_msr =find_msr (host, msr->index);unsigned int i;if ( cur_msr ==NULL || def_msr ==NULL || host_msr ==NULL ) {ERROR ("Missing MSR %#x" , msr->index); - rc = -ENOENT ; - goto out; +return -ENOENT ; }for ( i =0 ; i <ARRAY_SIZE (msr->policy) -1 ; i++ )
@@ -594,8 +433,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t domid, {ERROR ("MSR index %#x: bad character '%c' in policy string '%s'" , msr->index, msr->policy[i], msr->policy); - rc = -EINVAL ; - goto out; +return -EINVAL ; }if ( val )
@@ -605,25 +443,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t domid, } } -/* Feed the transformed policy back up to Xen. */ - rc =xc_set_domain_cpu_policy (xch, domid,0 ,NULL , nr_cur, cur, - &err_leaf, &err_subleaf, &err_msr); -if ( rc ) - { -PERROR ("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr %#x)" , - domid, err_leaf, err_subleaf, err_msr); - rc = -errno; - goto out; - } - -/* Success! */ - - out: -free (cur); -free (def); -free (host); - -return rc; +return 0 ; } int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,
@@ -822,25 +642,20 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, } } -/* Re-apply the mutated policy onto the domain */ - rc =xc_cpu_policy_set_domain (xch, domid, cur); -if ( rc ) - { - rc = -errno; +/* +* Ensure changes we just made are reflected in the serialised form of +* the policy, as otherwise they they will be missed by the xend overrides +*/ +if ( (rc =cpu_policy_serialise_on_self (xch, cur)) ) goto out; - } -if ( xend && (rc =xc_cpuid_xend_policy (xch, domid, xend)) ) +/* Apply xend-style overrides */ +if ( (xend && (rc =xc_cpuid_xend_policy (xch, domid, xend, host, def, cur))) || + (msr && (rc =xc_msr_policy (xch, domid, msr, host, def, cur))) ) goto out; -if ( msr ) - { - rc =xc_msr_policy (xch, domid, msr); -if ( rc ) - goto out; - } - - rc =0 ; +/* Re-apply the mutated policy onto the domain */ + rc =xc_cpu_policy_set_domain_from_serialised (xch, domid, cur); out:xc_cpu_policy_destroy (cur);
Signature · 1 line
2.34.1
PATCH 5/7 tools/xg: Streamline xc_cpuid_apply_policy()
In reply to Alejandro VallejoInstantiates host, default and domain policies in order to clean up a lot of boilerplate hypercalls. This is partial work in order to deduplicate the same hypercalls being used when updating CPUID and MSR parts of the policy.
--- tools/include/xenguest.h | 1 + tools/libs/guest/xg_cpuid_x86.c | 184 ++++++++++++++++---------------- 2 files changed, 93 insertions(+), 92 deletions(-)
tools/include/xenguest.h +1 -0
@@ -788,6 +788,7 @@ typedef struct xc_cpu_policy xc_cpu_policy_t;/* Create and free a xc_cpu_policy object. */ xc_cpu_policy_t *xc_cpu_policy_init (xc_interface *xch); +xc_cpu_policy_t *xc_cpu_policy_clone (const xc_cpu_policy_t *other);void xc_cpu_policy_destroy (xc_cpu_policy_t *policy);/* Retrieve a system policy, or get/set a domains policy. */
tools/libs/guest/xg_cpuid_x86.c +92 -92
299 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 7/7 tools/xg: Simplify hypercall stubs
In reply to Alejandro VallejoNow there are no pending dependencies on the current form of the hypercall stubs. Replace them with simpler forms that only take the xc_cpu_policy object. This way the plumbing logic becomes a lot simpler, allowing the policy to be extended without touching the plumbing code.
--- tools/libs/guest/xg_cpuid_x86.c | 59 ++++++++++++--------------------- 1 file changed, 22 insertions(+), 37 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +22 -37
@@ -125,16 +125,17 @@ int xc_cpu_policy_get_size(xc_interface *xch, uint32_t *nr_leaves,return ret; } -static int get_system_cpu_policy (xc_interface *xch,uint32_t index, -uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves, -uint32_t *nr_msrs,xen_msr_entry_t *msrs) +static int get_system_cpu_policy_serialised (xc_interface *xch,uint32_t index, +xc_cpu_policy_t *p) {struct xen_sysctl sysctl = {}; +xen_cpuid_leaf_t *leaves = p->leaves.buf; +xen_msr_entry_t *msrs = p->msrs.buf;DECLARE_HYPERCALL_BOUNCE (leaves, - *nr_leaves *sizeof (*leaves), + p->leaves.allocated *sizeof (*leaves),XC_HYPERCALL_BUFFER_BOUNCE_OUT );DECLARE_HYPERCALL_BOUNCE (msrs, - *nr_msrs *sizeof (*msrs), + p->msrs.allocated *sizeof (*msrs),XC_HYPERCALL_BUFFER_BOUNCE_OUT );int ret;
@@ -144,9 +145,9 @@ static int get_system_cpu_policy(xc_interface *xch, uint32_t index, sysctl.cmd = XEN_SYSCTL_get_cpu_policy; sysctl.u.cpu_policy.index = index; - sysctl.u.cpu_policy.nr_leaves = *nr_leaves; + sysctl.u.cpu_policy.nr_leaves = p->leaves.allocated;set_xen_guest_handle (sysctl.u.cpu_policy.leaves, leaves); - sysctl.u.cpu_policy.nr_msrs = *nr_msrs; + sysctl.u.cpu_policy.nr_msrs = p->msrs.allocated;set_xen_guest_handle (sysctl.u.cpu_policy.msrs, msrs); ret =do_sysctl (xch, &sysctl);
@@ -156,23 +157,24 @@ static int get_system_cpu_policy(xc_interface *xch, uint32_t index,if ( !ret ) { - *nr_leaves = sysctl.u.cpu_policy.nr_leaves; - *nr_msrs = sysctl.u.cpu_policy.nr_msrs; + p->leaves.len = sysctl.u.cpu_policy.nr_leaves; + p->msrs.len = sysctl.u.cpu_policy.nr_msrs; }return ret; } -static int get_domain_cpu_policy (xc_interface *xch,uint32_t domid, -uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves, -uint32_t *nr_msrs,xen_msr_entry_t *msrs) +static int get_domain_cpu_policy_serialised (xc_interface *xch,uint32_t domid, +xc_cpu_policy_t *p) {DECLARE_DOMCTL ; +xen_cpuid_leaf_t *leaves = p->leaves.buf; +xen_msr_entry_t *msrs = p->msrs.buf;DECLARE_HYPERCALL_BOUNCE (leaves, - *nr_leaves *sizeof (*leaves), + p->leaves.allocated *sizeof (*leaves),XC_HYPERCALL_BUFFER_BOUNCE_OUT );DECLARE_HYPERCALL_BOUNCE (msrs, - *nr_msrs *sizeof (*msrs), + p->msrs.allocated *sizeof (*msrs),XC_HYPERCALL_BUFFER_BOUNCE_OUT );int ret;
@@ -182,9 +184,9 @@ static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, domctl.cmd = XEN_DOMCTL_get_cpu_policy; domctl.domain = domid; - domctl.u.cpu_policy.nr_leaves = *nr_leaves; + domctl.u.cpu_policy.nr_leaves = p->leaves.allocated;set_xen_guest_handle (domctl.u.cpu_policy.leaves, leaves); - domctl.u.cpu_policy.nr_msrs = *nr_msrs; + domctl.u.cpu_policy.nr_msrs = p->msrs.allocated;set_xen_guest_handle (domctl.u.cpu_policy.msrs, msrs); ret =do_domctl (xch, &domctl);
@@ -194,8 +196,8 @@ static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid,if ( !ret ) { - *nr_leaves = domctl.u.cpu_policy.nr_leaves; - *nr_msrs = domctl.u.cpu_policy.nr_msrs; + p->leaves.len = domctl.u.cpu_policy.nr_leaves; + p->msrs.len = domctl.u.cpu_policy.nr_msrs; }return ret;
@@ -745,22 +747,14 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy)int xc_cpu_policy_get_system (xc_interface *xch,unsigned int policy_idx,xc_cpu_policy_t *policy) { -unsigned int nr_leaves = policy->leaves.allocated; -unsigned int nr_msrs = policy->msrs.allocated;int rc; - rc =get_system_cpu_policy (xch, policy_idx, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); -if ( rc ) +if ( (rc =get_system_cpu_policy_serialised (xch, policy_idx, policy)) ) {PERROR ("Failed to obtain %u policy" , policy_idx);return rc; } - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc =cpu_policy_deserialise_on_self (xch, policy);if ( rc ) {
@@ -774,22 +768,13 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx,int xc_cpu_policy_get_domain (xc_interface *xch,uint32_t domid,xc_cpu_policy_t *policy) { -unsigned int nr_leaves = policy->leaves.allocated; -unsigned int nr_msrs = policy->msrs.allocated; -int rc; - - rc =get_domain_cpu_policy (xch, domid, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); +int rc =get_domain_cpu_policy_serialised (xch, domid, policy);if ( rc ) {PERROR ("Failed to obtain domain %u policy" , domid);return rc; } - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc =cpu_policy_deserialise_on_self (xch, policy);if ( rc ) {
Signature · 1 line
2.34.1
Re: [PATCH 6/7] tools/xg: Simplify xc_cpuid_xend_policy() and xc_msr_policy()
In reply to Alejandro VallejoHi, while rebasing with something else I noticed that...
Quoted 10 lines · Alejandro Vallejo
Remove all duplication in CPUID and MSR xend-style overrides. They had an incredible amount of overhead for no good reason.
After this patch, CPU policy application takes a number of hypercalls to recover the policy state and then those are passed to the xend-style override code so it can avoid them.
Furthermore, the ultimate reapplication of the policy to the domain in Xen is done only once after both CPUID and MSRs have been fixed up.
Quoted 7 lines
BUG!!!
apply_policyis sending the policy after deserialise when it poked at it in its serialised form.Signed-off-by Alejandro Vallejo <[email protected]>--- tools/libs/guest/xg_cpuid_x86.c | 261 +++++--------------------------- 1 file changed, 38 insertions(+), 223 deletions(-)
there's a blunder in the commit message. This was a note to myself to fix something while in mid-dev, and I did, but then didn't remove the paragraph from the commit message.
Obviously those last 2 lines before the S-by shouldn't be there.
Cheers,
Alejandro
Re: [PATCH 1/7] tools/xenguest: Dynamically allocate xc_cpu_policy_t contents
In reply to Alejandro Vallejotools/libs/guest/xg_cpuid_x86.c out: · line 880
Quoted 13 lines · Alejandro Vallejo
void
xc_cpu_policy_destroy(xc_cpu_policy_t *policy){tools/libs/guest/xg_cpuid_x86.c +9
-2-if ( policy ) -free (policy); +int err = errno; + +if ( !policy ) +return ; + +if ( policy->leaves.buf ) +free (policy->leaves.buf); +if ( policy->msrs.buf ) +free (policy->msrs.buf);
FYI: free() already check for NULL, so the extra if() is unnecessary.
Otherwise, patch looks fine:
Thanks,
Signature · 1 line
Anthony PERARD