Skip to contentSkip to conversation

x86/xsaves: calculate the xstate_comp_offsets base on xcomp_bv

PATCH Superseded Shuai Ruan 2 participants 4 messages Mar 4, 2016 – Mar 7, 2016 1 message not in the archive

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Shuai Ruan

author
#0 lore raw

PATCH v3 1/1 x86/xsaves: calculate the xstate_comp_offsets base on xcomp_bv

Previous patch using all available features calculate xstate_comp_offsets. This is wrong.This patch fix this bug by calculating the xstate_comp_offset based on xcomp_bv of current guest.
Also, the xstate_comp_offset should take alignment into consideration.

---
V3: Address comments from Jan:
1. fix xstate_comp_offsets used as static array problem.
2. change xstate_align from array to u64 and used as bitmap.
3. change calculating xstate_comp_offsets into three step.
1) whether component is set in xsavearea
2) whether component need align
3) add xstate_size[i-1]

V2: Address comments from Jan:
1. code style fix.
2. setup_xstate_comp take xcomp_bv as param.

 xen/arch/x86/domctl.c        | 21 +++++++++++---
 xen/arch/x86/xstate.c        | 66 +++++++++++++++++++++++++++++++-------------
 xen/include/asm-x86/xstate.h |  6 ++--
 3 files changed, 68 insertions(+), 25 deletions(-)
xen/arch/x86/domctl.c +17 -4
index 55aecdc..3f891b4 100644
@@ -934,8 +934,14 @@ long arch_do_domctl(
                     goto vcpuextstate_out;
                 }
 
-                expand_xsave_states(v, xsave_area,
-                                    size - 2 * sizeof(uint64_t));
+                ret = expand_xsave_states(v, xsave_area,
+                                          size - 2 * sizeof(uint64_t));
+                if ( ret )
+                {
+                    xfree(xsave_area);
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
 
                 if ( copy_to_guest_offset(evc->buffer, offset, xsave_area,
                                           size - 2 * sizeof(uint64_t)) )
@@ -1000,8 +1006,15 @@ long arch_do_domctl(
                 v->arch.xcr0_accum = _xcr0_accum;
                 if ( _xcr0_accum & XSTATE_NONLAZY )
                     v->arch.nonlazy_xstate_used = 1;
-                compress_xsave_states(v, _xsave_area,
-                                      evc->size - 2 * sizeof(uint64_t));
+                ret = compress_xsave_states(v, _xsave_area,
+                                            evc->size - 2 * sizeof(uint64_t));
+                if ( ret )
+                {
+                    xfree(receive_buf);
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
+
                 vcpu_unpause(v);
             }
             else
xen/arch/x86/xstate.c +47 -19

178 lines not on this page · raw message

xen/include/asm-x86/xstate.h +4 -2
index c28cea5..782f9bf 100644
@@ -46,6 +46,8 @@
 #define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
 #define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
 
+#define XSTATE_ALIGN64 (1U << 1)
+
 extern u64 xfeature_mask;
 extern unsigned int *xstate_sizes;
 
@@ -94,8 +96,8 @@ bool_t xsave_enabled(const struct vcpu *v);
 int __must_check validate_xstate(u64 xcr0, u64 xcr0_accum,
                                  const struct xsave_hdr *);
 int __must_check handle_xsetbv(u32 index, u64 new_bv);
-void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
-void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);
+int expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
+int compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);
 
 /* extended state init and cleanup functions */
 void xstate_free_save_area(struct vcpu *v);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.03.16 at 12:00, <[email protected]> wrote:
xen/arch/x86/domctl.c +0 -0
@@ -934,8 +934,14 @@ long arch_do_domctl(
                     goto vcpuextstate_out;
                 }
 

xen/arch/x86/domctl.c long arch_do_domctl( · line 944

Quoted 10 lines
xen/arch/x86/domctl.c +8 -2
-                expand_xsave_states(v, xsave_area,
-                                    size - 2 * sizeof(uint64_t));
+                ret = expand_xsave_states(v, xsave_area,
+                                          size - 2 * sizeof(uint64_t));
+                if ( ret )
+                {
+                    xfree(xsave_area);
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }

Well, while this is one way to deal with the problem, it's certainly not the most desirable one: We should try to avoid runtime allocations, failures of which then cause other things to fail (in perhaps not very graceful ways). And doing so is pretty simple here, and you even have two options: Either allocate a per-CPU array, or - considering that XCNTXT_MASK has only a limited number of bits set - even use an on-stack array of suitable (compile time determined from XCNTXT_MASK) size. If you want to save space, this could presumably even be uint16_t[], in which case I think it would further be okay if all 63 possible features were known (totaling to a variable size of 126 bytes).

This is even more so considering that you forgot to adjust hvm.c in a similar manner.

xen/arch/x86/domctl.c static int setup_xstate_features(bool_t bsp) · line 118

Quoted 10 lines
xen/arch/x86/domctl.c +4 -1
@@ -111,21 +111,27 @@ static int setup_xstate_features(bool_t bsp)
     for ( leaf = 2; leaf < xstate_features; leaf++ )
     {
         if ( bsp )
+        {
             cpuid_count(XSTATE_CPUID, leaf, &xstate_sizes[leaf],
-                        &xstate_offsets[leaf], &tmp, &tmp);
+                        &xstate_offsets[leaf], &ecx, &edx);
+            if ( ecx & XSTATE_ALIGN64 )
+                set_bit(leaf, &xstate_align);

__set_bit()

Quoted 13 lines
xen/arch/x86/domctl.c +0 -3
@@ -134,22 +140,26 @@ static void __init setup_xstate_comp(void)
      * in the fixed offsets in the xsave area in either compacted form
      * or standard form.
      */
-    xstate_comp_offsets[0] = 0;
     xstate_comp_offsets[1] = XSAVE_SSE_OFFSET;
 
     xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE;
 
     for ( i = 3; i < xstate_features; i++ )
     {
-        xstate_comp_offsets[i] = xstate_comp_offsets[i - 1] +
-                                 (((1ul << i) & xfeature_mask)

xen/arch/x86/domctl.c static void __init setup_xstate_comp(void) · line 156

Quoted 10 lines
xen/arch/x86/domctl.c +8 -2
-                                  ? xstate_sizes[i - 1] : 0);
-        ASSERT(xstate_comp_offsets[i] + xstate_sizes[i] <= xsave_cntxt_size);
+        if ( (1ul << i) & xcomp_bv )
+        {
+            xstate_comp_offsets[i] = test_bit(i, &xstate_align) ?
+                                     ROUNDUP(xstate_comp_offsets[i - 1], 64) :
+                                     xstate_comp_offsets[i -1];
+            xstate_comp_offsets[i] += xstate_sizes[i - 1];
+            ASSERT(xstate_comp_offsets[i] + xstate_sizes[i] <= xsave_cntxt_size);
+        }

This now seems wrong for the case when there are discontiguous bits in xcomp_bv, as you leave zeros in the array (which by itself is fine, but confuses the subsequent iteration). I think you'd benefit from having a local variable holding the offset you're currently at:

    offset = xstate_comp_offsets[2];
    for ( i = 2; i < xstate_features; i++ )
    {
        if ( (1ul << i) & xcomp_bv )
        {
            if ( test_bit(i, &xstate_align) )
                offset = ROUNDUP(offset, 64);
            xstate_comp_offsets[i] = offset;
            offset += xstate_sizes[i];
            ASSERT(offset <= xsave_cntxt_size);
        }
    }

or something along those lines.

xen/arch/x86/domctl.c static void __init setup_xstate_comp(void) · line 158

Quoted 3 lines
xen/arch/x86/domctl.c +1 -1
 static void *get_xsave_addr(struct xsave_struct *xsave,
-        unsigned int xfeature_idx)
+                            unsigned int *xstate_comp_offsets,

const

xen/arch/x86/domctl.c bool_t xsave_enabled(const struct vcpu *v); · line 100

Quoted 8 lines
xen/arch/x86/domctl.c +2 -2
@@ -94,8 +96,8 @@ bool_t xsave_enabled(const struct vcpu *v);
 int __must_check validate_xstate(u64 xcr0, u64 xcr0_accum,
                                  const struct xsave_hdr *);
 int __must_check handle_xsetbv(u32 index, u64 new_bv);
-void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
-void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);
+int expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
+int compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);

While with the above I assume these are going to remain what they have been, a general advice: If you convert a function from having void return type to something else, and that something else can indicate an error, use __must_check so the compiler can tell you whether you've forgotten to update some caller.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#2 lore raw
Quoted 18 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.03.16 at 12:00, <[email protected]> wrote:
xen/arch/x86/domctl.c +0 -0
@@ -934,8 +934,14 @@ long arch_do_domctl(
                     goto vcpuextstate_out;
                 }
 

xen/arch/x86/domctl.c long arch_do_domctl( · line 944

Quoted 10 lines
xen/arch/x86/domctl.c +8 -2
-                expand_xsave_states(v, xsave_area,
-                                    size - 2 * sizeof(uint64_t));
+                ret = expand_xsave_states(v, xsave_area,
+                                          size - 2 * sizeof(uint64_t));
+                if ( ret )
+                {
+                    xfree(xsave_area);
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
Quoted 8 lines

Well, while this is one way to deal with the problem, it's certainly not the most desirable one: We should try to avoid runtime allocations, failures of which then cause other things to fail (in perhaps not very graceful ways). And doing so is pretty simple here, and you even have two options: Either allocate a per-CPU array, or - considering that XCNTXT_MASK has only a limited number of bits set - even use an on-stack array of suitable (compile time determined from XCNTXT_MASK) size. If you

Thanks.
I will change it to on-stack array.
For "size compile time determined from XCNTXT_MASK", hweight64(XCNTXT_MASK) can return the num of bits set. But we need to caculte the highest bit set in XCNTXT_MASK at compile time, is there any macro can be used here ?

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]@linux.intel.com

#4 lore raw
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 07.03.16 at 11:19, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.03.16 at 12:00, <[email protected]> wrote:
xen/arch/x86/domctl.c +0 -0
@@ -934,8 +934,14 @@ long arch_do_domctl(
                     goto vcpuextstate_out;
                 }
 

xen/arch/x86/domctl.c long arch_do_domctl( · line 944

Quoted 10 lines
xen/arch/x86/domctl.c +8 -2
-                expand_xsave_states(v, xsave_area,
-                                    size - 2 * sizeof(uint64_t));
+                ret = expand_xsave_states(v, xsave_area,
+                                          size - 2 * sizeof(uint64_t));
+                if ( ret )
+                {
+                    xfree(xsave_area);
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
Quoted 8 lines

Well, while this is one way to deal with the problem, it's certainly not the most desirable one: We should try to avoid runtime allocations, failures of which then cause other things to fail (in perhaps not very graceful ways). And doing so is pretty simple here, and you even have two options: Either allocate a per-CPU array, or - considering that XCNTXT_MASK has only a limited number of bits set - even use an on-stack array of suitable (compile time determined from XCNTXT_MASK) size. If you

Quoted 5 lines

Thanks.
I will change it to on-stack array.
For "size compile time determined from XCNTXT_MASK", hweight64(XCNTXT_MASK) can return the num of bits set. But we need to caculte the highest bit set in XCNTXT_MASK at compile time, is there any macro can be used here ?

You may want to pull in Linux'es ilog2().

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel