Re: [PATCH v3 2/6] KVM: VMX: Extend BUILD_CONTROLS_SHADOW macro to support 64-bit variation

Sean Christopherson
2021-08-05T22:33:06Z

On Thu, Aug 05, 2021, Zeng Guang wrote:
> From: Robert Hoo <robert.hu@linux.intel.com>
>
> The Tertiary VM-Exec Control, different from previous control fields, is 64
> bit. So extend BUILD_CONTROLS_SHADOW() by adding a 'bit' parameter, to
> support both 32 bit and 64 bit fields' auxiliary functions building.
> Also, define the auxiliary functions for Tertiary control field here, using
> the new BUILD_CONTROLS_SHADOW().
>
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Robert Hoo <robert.hu@linux.intel.com>
> ---
> arch/x86/kvm/vmx/vmx.h | 23 ++++++++++++-----------
> 1 file changed, 12 insertions(+), 11 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
> index 3979a947933a..945c6639ce24 100644
> --- a/arch/x86/kvm/vmx/vmx.h
> +++ b/arch/x86/kvm/vmx/vmx.h
> @@ -413,31 +413,32 @@ static inline u8 vmx_get_rvi(void)
> return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
> }
>
> -#define BUILD_CONTROLS_SHADOW(lname, uname) \
> -static inline void lname##_controls_set(struct vcpu_vmx *vmx, u32 val) \
> +#define BUILD_CONTROLS_SHADOW(lname, uname, bits) \
> +static inline void lname##_controls_set(struct vcpu_vmx *vmx, u##bits val) \

Align the trailing backslashes (with tabs when possible). It's a lot of unfortunate
churn, but it really does make the code easier to read. An alternative is to split
"static inline" to a separate line.

> { \
> if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
> - vmcs_write32(uname, val); \
> + vmcs_write##bits(uname, val); \
> vmx->loaded_vmcs->controls_shadow.lname = val; \
> } \
> } \
> -static inline u32 lname##_controls_get(struct vcpu_vmx *vmx) \
> +static inline u##bits lname##_controls_get(struct vcpu_vmx *vmx) \
> { \
> return vmx->loaded_vmcs->controls_shadow.lname; \
> } \
> -static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u32 val) \
> +static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u##bits val) \
> { \
> lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
> } \
> -static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u32 val) \
> +static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u##bits val) \
> { \
> lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
> }
> -BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS)
> -BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS)
> -BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL)
> -BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL)
> -BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL)
> +BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS, 32)
> +BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS, 32)
> +BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL, 32)
> +BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL, 32)
> +BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL, 32)
> +BUILD_CONTROLS_SHADOW(tertiary_exec, TERTIARY_VM_EXEC_CONTROL, 64)

This fails to compile because all the TERTIARY collateral is in a later patch.

I think I'd also prefer hiding the 32/64 param via more macros, e.g.

#define __BUILD_CONTROLS_SHADOW(lname, uname, bits) \
static inline void lname##_controls_set(struct vcpu_vmx *vmx, u##bits val) \
{ \
if (vmx->loaded_vmcs->controls_shadow.lname != val) { \
vmcs_write##bits(uname, val); \
vmx->loaded_vmcs->controls_shadow.lname = val; \
} \
} \
static inline u##bits lname##_controls_get(struct vcpu_vmx *vmx) \
{ \
return vmx->loaded_vmcs->controls_shadow.lname; \
} \
static inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u##bits val) \
{ \
lname##_controls_set(vmx, lname##_controls_get(vmx) | val); \
} \
static inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u##bits val) \
{ \
lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val); \
}
#define BUILD_CONTROLS_SHADOW(lname, uname) __BUILD_CONTROLS_SHADOW(lname, uname, 32)
#define BUILD_CONTROLS_SHADOW64(lname, uname) __BUILD_CONTROLS_SHADOW(lname, uname, 64)