Skip to contentSkip to conversation

Enable SMEP CPU feature support for XEN itself

PATCH Jan Beulich 3 participants 7 messages Jun 2, 2011 – Jun 3, 2011

lore
Share & tools
lore
#0 lore raw
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

"Li, Xin" 06/02/11 6:20 AM >>>

(quoted diff) +1 -0
+boolean_param("nosmep", disable_smep);
 
 struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
@@ -222,6 +225,17 @@
     c->x86_capability[4] = cap4;
 }
 
Quoted 8 lines
Quoted 6 lines

Review line 231

Quoted 4 lines
(quoted diff) +4 -0
+static void __cpuinit setup_smep(struct cpuinfo_x86 *c)
+{
+    if ( cpu_has(c, X86_FEATURE_SMEP) ) {
+        if( unlikely(disable_smep) ) {

... a __cpuinit function?

If change disable_smep to __cpuinitdata, this should be ok.

You would be okay, but as I wrote further down both are really only needed on the BP.

Quoted 14 lines
Quoted 5 lines

Review line 235

Quoted 4 lines
(quoted diff) +4 -0
+            setup_clear_cpu_cap(X86_FEATURE_SMEP);
+            clear_in_cr4(X86_CR4_SMEP);
+        } else
+            set_in_cr4(X86_CR4_SMEP);
Quoted 8 lines

Anyway, the whole thing is overkill - {set,clear}_in_cr4() write the updated bits to mmu_cr4_features, and these get loaded on secondary CPUs *before* you have any chance of looking at the CPUID bits. As with everything else, it's assumed that APs don't have less features than the BP, and hence you only need to set_in_cr4() once (on the BP). And then the function can be __init.

Quoted 4 lines

Do you mean?
if ( cpu_has(c, X86_FEATURE_SMEP) )
if( likely(!disable_smep) ) {
mmu_cr4_features |= X86_CR4_SMEP;

Why?

Quoted 1 line

set_in_cr4(0);

set_in_cr4(X86_CR4_SMEP) does exactly what you need.

Quoted 5 lines

} else
setup_clear_cpu_cap(X86_FEATURE_SMEP);

Sounds good ... but the code will be harder to read, as it implicitly set smep? Also where to put setup_smep thus it's only called in BP?

early_cpu_detect() would seem to be the most logical place, though it doesn't have all the x86_capabilities[] fields set up yet. The BP-only part at the end of identify_cpu() would also be a possible place.

trap_init() would be another possible (and reasonably logical) place.

Quoted 8 lines
Quoted 4 lines

The further down I get the uglier this looks. Can't you simply accumulate the user bit into a separate variable? That way the compiler also doesn't need to keep around all the l[1234]e variables.

At the beginning we did accumulate the user bit into a separate variable. However SMEP faults hardly happen while we keep accumulating user bit no matter it's a spurious fault or not, and even spurious faults are rare I guess.

Remember that we're going through this function for almost every page fault happening in Xen, and also for the majority of those originating from certain pv guests (when they have suppress_spurious_page_faults set).

Also, my comment was to a large part aiming at better legibility of the code you add.

Jan

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

#1 lore raw
Quoted 3 lines
Quoted 1 line

mmu_cr4_features |= X86_CR4_SMEP;

Why?

I replied in another reply to you, but just repeat here:
But it is a good idea to set X86_CR4_SMEP in mmu_cr4_features when SMEP Is available. thus

1) for PV, we can make patch like pv_guest_cr4_to_real_cr4

#define pv_guest_cr4_to_real_cr4(v)                         \
    (((v)->arch.pv_vcpu.ctrlreg[4]                          \
      | (mmu_cr4_features & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP))    \
      | ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0)          \
      | ((xsave_enabled(v))? X86_CR4_OSXSAVE : 0))          \
      & ~X86_CR4_DE)

when set cr4.

2) For HVM, we don't need to explicitly add SMEP when write to HOST_CR4.

Quoted 3 lines
Quoted 1 line

set_in_cr4(0);

set_in_cr4(X86_CR4_SMEP) does exactly what you need.

Yes, but once we have X86_CR4_SMEP in mmu_cr4_features, set_in_cr4(0) does the same thing except looks ugly.

Quoted 2 lines

} else

Quoted 1 line

setup_clear_cpu_cap(X86_FEATURE_SMEP);

This needs to be done on APs too. Thus I think we still need define setup_smep as __cpuinit.

Quoted 11 lines
Quoted 3 lines

At the beginning we did accumulate the user bit into a separate variable. However SMEP faults hardly happen while we keep accumulating user bit no matter it's a spurious fault or not, and even spurious faults are rare I guess.

Remember that we're going through this function for almost every page fault happening in Xen, and also for the majority of those originating from certain pv guests (when they have suppress_spurious_page_faults set).

Also, my comment was to a large part aiming at better legibility of the code you add.

Yes, for legibility we may change it back.
Thanks!
-Xin

#2 lore raw
Quoted 6 lines
Quoted 3 lines

} else

Quoted 1 line

setup_clear_cpu_cap(X86_FEATURE_SMEP);

This needs to be done on APs too. Thus I think we still need define setup_smep as __cpuinit.

This only applies to BP only, and actually all feature check is against BP even AP has a corresponding bit set. So your suggestion should work. Thanks!
-Xin

#3 lore raw
In reply to Li, Xin
Quoted 5 lines · Xin
Quoted 2 lines

} else

Quoted 1 line

setup_clear_cpu_cap(X86_FEATURE_SMEP);

This needs to be done on APs too. Thus I think we still need define setup_smep as __cpuinit.

The whole point of setup_clear_cpu_cap() is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.

-- Keir

#4 lore raw
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

} else

Quoted 1 line

setup_clear_cpu_cap(X86_FEATURE_SMEP);

This needs to be done on APs too. Thus I think we still need define setup_smep as __cpuinit.

The whole point of setup_clear_cpu_cap() is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.

Yeah, and we can't do leaf 7.0 detect in generic_identify. Or will again set SMEP in capabilities, but it should be ok. Thanks!
-Xin

#5 lore raw
Quoted 5 lines
Quoted 2 lines

The whole point of setup_clear_cpu_cap() is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.

Yeah, and we can't do leaf 7.0 detect in generic_identify. Or will again set SMEP in capabilities, but it should be ok.

I tried to not do leaf 7.0 detect on AP booting code, but system behaves abnormally (not hang, but "su -" never returns, after just revert the change, it runs well).

While at this point I want to focus on SMEP logic, we can make the improvements, to move such initialization to BP code only, later. Also it makes no much sense to only move disable_smep that way, we should move all such variables.

Thanks!
-Xin

#6 lore raw
Quoted 12 lines · Xin
Quoted 6 lines
Quoted 2 lines

The whole point of setup_clear_cpu_cap() is that it only needs to run on the BP and the change gets picked up by every AP automatically as it boots.

Yeah, and we can't do leaf 7.0 detect in generic_identify. Or will again set SMEP in
capabilities, but it should be ok.

I tried to not do leaf 7.0 detect on AP booting code, but system behaves abnormally
(not hang, but "su -" never returns, after just revert the change, it runs well).

Quoted 5 lines

While at this point I want to focus on SMEP logic, we can make the improvements,
to move such initialization to BP code only, later. Also it makes no much sense to only
move disable_smep that way, we should move all such variables.

Just get another rev of the patches out and we'll iterate if necessary.

-- Keir

Quoted 2 lines

Thanks!
-Xin