From: Jim Mattson Subject: [PATCH v2 3/3] kvm: vmx: Reduce size of vmcs_field_to_offset_table Date: Fri, 22 Dec 2017 12:13:13 -0800 Message-ID: <20171222201313.98795-1-jmattson@google.com> References: Cc: Jim Mattson To: kvm@vger.kernel.org, Paolo Bonzini Return-path: Received: from mail-io0-f194.google.com ([209.85.223.194]:38092 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756515AbdLVUN0 (ORCPT ); Fri, 22 Dec 2017 15:13:26 -0500 Received: by mail-io0-f194.google.com with SMTP id 87so21534153ior.5 for ; Fri, 22 Dec 2017 12:13:26 -0800 (PST) In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: The vmcs_field_to_offset_table was a rather sparse table of short integers with a maximum index of 0x6c16, amounting to 55342 bytes. Now that we are considering support for multiple VMCS12 formats, it would be unfortunate to replicate that large, sparse table. Rotating the field encoding (as a 16-bit integer) left by 6 reduces that table to 5926 bytes. Suggested-by: Paolo Bonzini Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 1847000fbb0c..fec38a1614b5 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -714,10 +714,12 @@ static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) return &(to_vmx(vcpu)->pi_desc); } +#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) -#define FIELD(number, name) [number] = VMCS12_OFFSET(name) -#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ - [number##_HIGH] = VMCS12_OFFSET(name)+4 +#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) +#define FIELD64(number, name) \ + FIELD(number, name), \ + [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) static unsigned long shadow_read_only_fields[] = { @@ -925,13 +927,17 @@ static const unsigned short vmcs_field_to_offset_table[] = { static inline short vmcs_field_to_offset(unsigned long field) { - BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); + unsigned index; + + if (field >> 15) + return -ENOENT; - if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || - vmcs_field_to_offset_table[field] == 0) + index = ROL16(field, 6); + if (index >= ARRAY_SIZE(vmcs_field_to_offset_table) || + vmcs_field_to_offset_table[index] == 0) return -ENOENT; - return vmcs_field_to_offset_table[field]; + return vmcs_field_to_offset_table[index]; } static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) -- 2.15.1.620.gb9897f4670-goog